blob: e729e94cb751672b5a486f140059c2e886281f0f [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 Tianyuff823c792012-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 Tianyuff823c792012-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);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400742 while (--limit && !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
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400749 next = hub->tt.clear_list.next;
750 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 list_del (&clear->clear_list);
752
753 /* drop lock so HCD can concurrently report other TT errors */
754 spin_unlock_irqrestore (&hub->tt.lock, flags);
755 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (status)
757 dev_err (&hdev->dev,
758 "clear tt %d (%04x) error %d\n",
759 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400760
761 /* Tell the HCD, even if the operation failed */
762 drv = clear->hcd->driver;
763 if (drv->clear_tt_buffer_complete)
764 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
765
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700766 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400767 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 spin_unlock_irqrestore (&hub->tt.lock, flags);
770}
771
772/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400773 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
774 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 *
776 * High speed HCDs use this to tell the hub driver that some split control or
777 * bulk transaction failed in a way that requires clearing internal state of
778 * a transaction translator. This is normally detected (and reported) from
779 * interrupt context.
780 *
781 * It may not be possible for that hub to handle additional full (or low)
782 * speed transactions until that state is fully cleared out.
783 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400784int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400786 struct usb_device *udev = urb->dev;
787 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct usb_tt *tt = udev->tt;
789 unsigned long flags;
790 struct usb_tt_clear *clear;
791
792 /* we've got to cope with an arbitrary number of pending TT clears,
793 * since each TT has "at least two" buffers that can need it (and
794 * there can be many TTs per hub). even if they're uncommon.
795 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800796 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
798 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400799 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
802 /* info that CLEAR_TT_BUFFER needs */
803 clear->tt = tt->multi ? udev->ttport : 1;
804 clear->devinfo = usb_pipeendpoint (pipe);
805 clear->devinfo |= udev->devnum << 4;
806 clear->devinfo |= usb_pipecontrol (pipe)
807 ? (USB_ENDPOINT_XFER_CONTROL << 11)
808 : (USB_ENDPOINT_XFER_BULK << 11);
809 if (usb_pipein (pipe))
810 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400811
812 /* info for completion callback */
813 clear->hcd = bus_to_hcd(udev->bus);
814 clear->ep = urb->ep;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* tell keventd to clear state for this TT */
817 spin_lock_irqsave (&tt->lock, flags);
818 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400819 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400823EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Alan Stern8520f382008-09-22 14:44:26 -0400825/* If do_delay is false, return the number of milliseconds the caller
826 * needs to delay.
827 */
828static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700831 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400832 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400833 u16 wHubCharacteristics =
834 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Alan Stern4489a572006-04-27 15:54:22 -0400836 /* Enable power on each port. Some hubs have reserved values
837 * of LPSM (> 2) in their descriptors, even though they are
838 * USB 2.0 hubs. Some hubs do not implement port-power switching
839 * but only emulate it. In all cases, the ports won't work
840 * unless we send these messages to the hub.
841 */
842 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400844 else
845 dev_dbg(hub->intfdev, "trying to enable port power on "
846 "non-switchable hub\n");
847 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
Greg Kroah-Hartmana0693bd2012-09-24 13:04:02 -0700848 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
David Brownellb7896962005-08-31 10:41:44 -0700850 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400851 delay = max(pgood_delay, (unsigned) 100);
852 if (do_delay)
853 msleep(delay);
854 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857static int hub_hub_status(struct usb_hub *hub,
858 u16 *status, u16 *change)
859{
860 int ret;
861
Alan Sterndb90e7a2007-02-05 09:56:15 -0500862 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 ret = get_hub_status(hub->hdev, &hub->status->hub);
864 if (ret < 0)
865 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800866 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 else {
868 *status = le16_to_cpu(hub->status->hub.wHubStatus);
869 *change = le16_to_cpu(hub->status->hub.wHubChange);
870 ret = 0;
871 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500872 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return ret;
874}
875
Alan Stern8b28c752005-08-10 17:04:13 -0400876static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
877{
878 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400879 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400880
Lan Tianyuff823c792012-09-05 13:44:32 +0800881 if (hub->ports[port1 - 1]->child && set_state)
882 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400883 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700884 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400885 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400886 if (ret)
887 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400888 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400889 return ret;
890}
891
Alan Stern0458d5b2007-05-04 11:52:20 -0400892/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800893 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400894 * time later khubd will disconnect() any existing usb_device on the port
895 * and will re-enumerate if there actually is a device attached.
896 */
897static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
898{
899 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
900 hub_port_disable(hub, port1, 1);
901
902 /* FIXME let caller ask to power down the port:
903 * - some devices won't enumerate without a VBUS power cycle
904 * - SRP saves power that way
905 * - ... new call, TBD ...
906 * That's easy if this hub can switch power per-port, and
907 * khubd reactivates the port later (timer, SRP, etc).
908 * Powerdown must be optional, because of reset/DFU.
909 */
910
911 set_bit(port1, hub->change_bits);
912 kick_khubd(hub);
913}
914
Alan Stern253e0572009-10-27 15:20:13 -0400915/**
916 * usb_remove_device - disable a device's port on its parent hub
917 * @udev: device to be disabled and removed
918 * Context: @udev locked, must be able to sleep.
919 *
920 * After @udev's port has been disabled, khubd is notified and it will
921 * see that the device has been disconnected. When the device is
922 * physically unplugged and something is plugged in, the events will
923 * be received and processed normally.
924 */
925int usb_remove_device(struct usb_device *udev)
926{
927 struct usb_hub *hub;
928 struct usb_interface *intf;
929
930 if (!udev->parent) /* Can't remove a root hub */
931 return -EINVAL;
932 hub = hdev_to_hub(udev->parent);
933 intf = to_usb_interface(hub->intfdev);
934
935 usb_autopm_get_interface(intf);
936 set_bit(udev->portnum, hub->removed_bits);
937 hub_port_logical_disconnect(hub, udev->portnum);
938 usb_autopm_put_interface(intf);
939 return 0;
940}
941
Alan Stern6ee0b272008-04-28 11:06:42 -0400942enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500943 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400944 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400945};
Alan Stern5e6effa2008-03-03 15:15:51 -0500946
Alan Stern8520f382008-09-22 14:44:26 -0400947static void hub_init_func2(struct work_struct *ws);
948static void hub_init_func3(struct work_struct *ws);
949
Alan Sternf2835212008-04-28 11:07:17 -0400950static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500951{
952 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800953 struct usb_hcd *hcd;
954 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500955 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400956 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400957 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400958 unsigned delay;
959
960 /* Continue a partial initialization */
961 if (type == HUB_INIT2)
962 goto init2;
963 if (type == HUB_INIT3)
964 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500965
Elric Fua45aa3b2012-02-18 13:32:27 +0800966 /* The superspeed hub except for root hub has to use Hub Depth
967 * value as an offset into the route string to locate the bits
968 * it uses to determine the downstream port number. So hub driver
969 * should send a set hub depth request to superspeed hub after
970 * the superspeed hub is set configuration in initialization or
971 * reset procedure.
972 *
973 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400974 * For any other type of activation, turn it on.
975 */
Alan Stern8520f382008-09-22 14:44:26 -0400976 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800977 if (hdev->parent && hub_is_superspeed(hdev)) {
978 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
979 HUB_SET_DEPTH, USB_RT_HUB,
980 hdev->level - 1, 0, NULL, 0,
981 USB_CTRL_SET_TIMEOUT);
982 if (ret < 0)
983 dev_err(hub->intfdev,
984 "set hub depth failed\n");
985 }
Alan Stern8520f382008-09-22 14:44:26 -0400986
987 /* Speed up system boot by using a delayed_work for the
988 * hub's initial power-up delays. This is pretty awkward
989 * and the implementation looks like a home-brewed sort of
990 * setjmp/longjmp, but it saves at least 100 ms for each
991 * root hub (assuming usbcore is compiled into the kernel
992 * rather than as a module). It adds up.
993 *
994 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
995 * because for those activation types the ports have to be
996 * operational when we return. In theory this could be done
997 * for HUB_POST_RESET, but it's easier not to.
998 */
999 if (type == HUB_INIT) {
1000 delay = hub_power_on(hub, false);
1001 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1002 schedule_delayed_work(&hub->init_work,
1003 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001004
1005 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001006 usb_autopm_get_interface_no_resume(
1007 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001008 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001009 } else if (type == HUB_RESET_RESUME) {
1010 /* The internal host controller state for the hub device
1011 * may be gone after a host power loss on system resume.
1012 * Update the device's info so the HW knows it's a hub.
1013 */
1014 hcd = bus_to_hcd(hdev->bus);
1015 if (hcd->driver->update_hub_device) {
1016 ret = hcd->driver->update_hub_device(hcd, hdev,
1017 &hub->tt, GFP_NOIO);
1018 if (ret < 0) {
1019 dev_err(hub->intfdev, "Host not "
1020 "accepting hub info "
1021 "update.\n");
1022 dev_err(hub->intfdev, "LS/FS devices "
1023 "and hubs may not work "
1024 "under this hub\n.");
1025 }
1026 }
1027 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001028 } else {
1029 hub_power_on(hub, true);
1030 }
1031 }
1032 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001033
Alan Stern6ee0b272008-04-28 11:06:42 -04001034 /* Check each port and set hub->change_bits to let khubd know
1035 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001036 */
1037 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001038 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001039 u16 portstatus, portchange;
1040
Alan Stern6ee0b272008-04-28 11:06:42 -04001041 portstatus = portchange = 0;
1042 status = hub_port_status(hub, port1, &portstatus, &portchange);
1043 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1044 dev_dbg(hub->intfdev,
1045 "port %d: status %04x change %04x\n",
1046 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001047
Alan Stern6ee0b272008-04-28 11:06:42 -04001048 /* After anything other than HUB_RESUME (i.e., initialization
1049 * or any sort of reset), every port should be disabled.
1050 * Unconnected ports should likewise be disabled (paranoia),
1051 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001052 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001053 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1054 type != HUB_RESUME ||
1055 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1056 !udev ||
1057 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001058 /*
1059 * USB3 protocol ports will automatically transition
1060 * to Enabled state when detect an USB3.0 device attach.
1061 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001062 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001063 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001064 clear_port_feature(hdev, port1,
1065 USB_PORT_FEAT_ENABLE);
1066 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001067 } else {
1068 /* Pretend that power was lost for USB3 devs */
1069 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001070 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001071 }
1072
Alan Stern948fea32008-04-28 11:07:07 -04001073 /* Clear status-change flags; we'll debounce later */
1074 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1075 need_debounce_delay = true;
1076 clear_port_feature(hub->hdev, port1,
1077 USB_PORT_FEAT_C_CONNECTION);
1078 }
1079 if (portchange & USB_PORT_STAT_C_ENABLE) {
1080 need_debounce_delay = true;
1081 clear_port_feature(hub->hdev, port1,
1082 USB_PORT_FEAT_C_ENABLE);
1083 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001084 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1085 hub_is_superspeed(hub->hdev)) {
1086 need_debounce_delay = true;
1087 clear_port_feature(hub->hdev, port1,
1088 USB_PORT_FEAT_C_BH_PORT_RESET);
1089 }
Alan Stern253e0572009-10-27 15:20:13 -04001090 /* We can forget about a "removed" device when there's a
1091 * physical disconnect or the connect status changes.
1092 */
1093 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1094 (portchange & USB_PORT_STAT_C_CONNECTION))
1095 clear_bit(port1, hub->removed_bits);
1096
Alan Stern6ee0b272008-04-28 11:06:42 -04001097 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1098 /* Tell khubd to disconnect the device or
1099 * check for a new connection
1100 */
1101 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1102 set_bit(port1, hub->change_bits);
1103
1104 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001105 bool port_resumed = (portstatus &
1106 USB_PORT_STAT_LINK_STATE) ==
1107 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001108 /* The power session apparently survived the resume.
1109 * If there was an overcurrent or suspend change
1110 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001111 * take care of it. Look at the port link state
1112 * for USB 3.0 hubs, since they don't have a suspend
1113 * change bit, and they don't set the port link change
1114 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001115 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001116 if (portchange || (hub_is_superspeed(hub->hdev) &&
1117 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001118 set_bit(port1, hub->change_bits);
1119
1120 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001121#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001122 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001123#endif
Alan Stern8808f002008-04-28 11:06:55 -04001124 set_bit(port1, hub->change_bits);
1125
Alan Stern6ee0b272008-04-28 11:06:42 -04001126 } else {
1127 /* The power session is gone; tell khubd */
1128 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1129 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001130 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001131 }
1132
Alan Stern948fea32008-04-28 11:07:07 -04001133 /* If no port-status-change flags were set, we don't need any
1134 * debouncing. If flags were set we can try to debounce the
1135 * ports all at once right now, instead of letting khubd do them
1136 * one at a time later on.
1137 *
1138 * If any port-status changes do occur during this delay, khubd
1139 * will see them later and handle them normally.
1140 */
Alan Stern8520f382008-09-22 14:44:26 -04001141 if (need_debounce_delay) {
1142 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001143
Alan Stern8520f382008-09-22 14:44:26 -04001144 /* Don't do a long sleep inside a workqueue routine */
1145 if (type == HUB_INIT2) {
1146 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1147 schedule_delayed_work(&hub->init_work,
1148 msecs_to_jiffies(delay));
1149 return; /* Continues at init3: below */
1150 } else {
1151 msleep(delay);
1152 }
1153 }
1154 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001155 hub->quiescing = 0;
1156
1157 status = usb_submit_urb(hub->urb, GFP_NOIO);
1158 if (status < 0)
1159 dev_err(hub->intfdev, "activate --> %d\n", status);
1160 if (hub->has_indicators && blinkenlights)
1161 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1162
1163 /* Scan all ports that need attention */
1164 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001165
1166 /* Allow autosuspend if it was suppressed */
1167 if (type <= HUB_INIT3)
1168 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001169}
1170
Alan Stern8520f382008-09-22 14:44:26 -04001171/* Implement the continuations for the delays above */
1172static void hub_init_func2(struct work_struct *ws)
1173{
1174 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1175
1176 hub_activate(hub, HUB_INIT2);
1177}
1178
1179static void hub_init_func3(struct work_struct *ws)
1180{
1181 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1182
1183 hub_activate(hub, HUB_INIT3);
1184}
1185
Alan Stern43303542008-04-28 11:07:31 -04001186enum hub_quiescing_type {
1187 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1188};
1189
1190static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1191{
1192 struct usb_device *hdev = hub->hdev;
1193 int i;
1194
Alan Stern8520f382008-09-22 14:44:26 -04001195 cancel_delayed_work_sync(&hub->init_work);
1196
Alan Stern43303542008-04-28 11:07:31 -04001197 /* khubd and related activity won't re-trigger */
1198 hub->quiescing = 1;
1199
1200 if (type != HUB_SUSPEND) {
1201 /* Disconnect all the children */
1202 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001203 if (hub->ports[i]->child)
1204 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001205 }
1206 }
1207
1208 /* Stop khubd and related activity */
1209 usb_kill_urb(hub->urb);
1210 if (hub->has_indicators)
1211 cancel_delayed_work_sync(&hub->leds);
1212 if (hub->tt.hub)
Alan Sterncb88a1b2009-06-29 10:43:32 -04001213 cancel_work_sync(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001214}
1215
Alan Stern3eb14912008-03-03 15:15:43 -05001216/* caller has locked the hub device */
1217static int hub_pre_reset(struct usb_interface *intf)
1218{
1219 struct usb_hub *hub = usb_get_intfdata(intf);
1220
Alan Stern43303542008-04-28 11:07:31 -04001221 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001222 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001223}
1224
1225/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001226static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001227{
Alan Stern7de18d82006-06-01 13:37:24 -04001228 struct usb_hub *hub = usb_get_intfdata(intf);
1229
Alan Sternf2835212008-04-28 11:07:17 -04001230 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001231 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001232}
1233
Lan Tianyufa2a9562012-09-05 13:44:31 +08001234static void usb_port_device_release(struct device *dev)
1235{
1236 struct usb_port *port_dev = to_usb_port(dev);
1237
1238 kfree(port_dev);
1239}
1240
1241static void usb_hub_remove_port_device(struct usb_hub *hub,
1242 int port1)
1243{
1244 device_unregister(&hub->ports[port1 - 1]->dev);
1245}
1246
1247struct device_type usb_port_device_type = {
1248 .name = "usb_port",
1249 .release = usb_port_device_release,
1250};
1251
1252static int usb_hub_create_port_device(struct usb_hub *hub,
1253 int port1)
1254{
1255 struct usb_port *port_dev = NULL;
1256 int retval;
1257
1258 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1259 if (!port_dev) {
1260 retval = -ENOMEM;
1261 goto exit;
1262 }
1263
1264 hub->ports[port1 - 1] = port_dev;
1265 port_dev->dev.parent = hub->intfdev;
1266 port_dev->dev.type = &usb_port_device_type;
1267 dev_set_name(&port_dev->dev, "port%d", port1);
1268
1269 retval = device_register(&port_dev->dev);
1270 if (retval)
1271 goto error_register;
1272 return 0;
1273
1274error_register:
1275 put_device(&port_dev->dev);
1276exit:
1277 return retval;
1278}
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static int hub_configure(struct usb_hub *hub,
1281 struct usb_endpoint_descriptor *endpoint)
1282{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001283 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 struct usb_device *hdev = hub->hdev;
1285 struct device *hub_dev = hub->intfdev;
1286 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001287 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001289 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001290 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Alan Sternd697cdd2009-10-27 15:18:46 -04001292 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 ret = -ENOMEM;
1295 goto fail;
1296 }
1297
1298 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1299 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 ret = -ENOMEM;
1301 goto fail;
1302 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001303 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1306 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 ret = -ENOMEM;
1308 goto fail;
1309 }
1310
1311 /* Request the entire hub descriptor.
1312 * hub->descriptor can handle USB_MAXCHILDREN ports,
1313 * but the hub can/will return fewer bytes here.
1314 */
John Youndbe79bb2001-09-17 00:00:00 -07001315 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (ret < 0) {
1317 message = "can't read hub descriptor";
1318 goto fail;
1319 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1320 message = "hub has too many ports!";
1321 ret = -ENODEV;
1322 goto fail;
1323 }
1324
1325 hdev->maxchild = hub->descriptor->bNbrPorts;
1326 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1327 (hdev->maxchild == 1) ? "" : "s");
1328
Lan Tianyufa2a9562012-09-05 13:44:31 +08001329 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1330 GFP_KERNEL);
Lan Tianyuff823c792012-09-05 13:44:32 +08001331 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001332 ret = -ENOMEM;
1333 goto fail;
1334 }
1335
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001336 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
John Youndbe79bb2001-09-17 00:00:00 -07001338 /* FIXME for USB 3.0, skip for now */
1339 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1340 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int i;
1342 char portstr [USB_MAXCHILDREN + 1];
1343
1344 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001345 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1347 ? 'F' : 'R';
1348 portstr[hdev->maxchild] = 0;
1349 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1350 } else
1351 dev_dbg(hub_dev, "standalone hub\n");
1352
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001353 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301354 case HUB_CHAR_COMMON_LPSM:
1355 dev_dbg(hub_dev, "ganged power switching\n");
1356 break;
1357 case HUB_CHAR_INDV_PORT_LPSM:
1358 dev_dbg(hub_dev, "individual port power switching\n");
1359 break;
1360 case HUB_CHAR_NO_LPSM:
1361 case HUB_CHAR_LPSM:
1362 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001366 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301367 case HUB_CHAR_COMMON_OCPM:
1368 dev_dbg(hub_dev, "global over-current protection\n");
1369 break;
1370 case HUB_CHAR_INDV_PORT_OCPM:
1371 dev_dbg(hub_dev, "individual port over-current protection\n");
1372 break;
1373 case HUB_CHAR_NO_OCPM:
1374 case HUB_CHAR_OCPM:
1375 dev_dbg(hub_dev, "no over-current protection\n");
1376 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
1379 spin_lock_init (&hub->tt.lock);
1380 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001381 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301383 case USB_HUB_PR_FS:
1384 break;
1385 case USB_HUB_PR_HS_SINGLE_TT:
1386 dev_dbg(hub_dev, "Single TT\n");
1387 hub->tt.hub = hdev;
1388 break;
1389 case USB_HUB_PR_HS_MULTI_TT:
1390 ret = usb_set_interface(hdev, 0, 1);
1391 if (ret == 0) {
1392 dev_dbg(hub_dev, "TT per port\n");
1393 hub->tt.multi = 1;
1394 } else
1395 dev_err(hub_dev, "Using single TT (err %d)\n",
1396 ret);
1397 hub->tt.hub = hdev;
1398 break;
1399 case USB_HUB_PR_SS:
1400 /* USB 3.0 hubs don't have a TT */
1401 break;
1402 default:
1403 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1404 hdev->descriptor.bDeviceProtocol);
1405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001408 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001409 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001410 case HUB_TTTT_8_BITS:
1411 if (hdev->descriptor.bDeviceProtocol != 0) {
1412 hub->tt.think_time = 666;
1413 dev_dbg(hub_dev, "TT requires at most %d "
1414 "FS bit times (%d ns)\n",
1415 8, hub->tt.think_time);
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001418 case HUB_TTTT_16_BITS:
1419 hub->tt.think_time = 666 * 2;
1420 dev_dbg(hub_dev, "TT requires at most %d "
1421 "FS bit times (%d ns)\n",
1422 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001424 case HUB_TTTT_24_BITS:
1425 hub->tt.think_time = 666 * 3;
1426 dev_dbg(hub_dev, "TT requires at most %d "
1427 "FS bit times (%d ns)\n",
1428 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001430 case HUB_TTTT_32_BITS:
1431 hub->tt.think_time = 666 * 4;
1432 dev_dbg(hub_dev, "TT requires at most %d "
1433 "FS bit times (%d ns)\n",
1434 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436 }
1437
1438 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001439 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 hub->has_indicators = 1;
1441 dev_dbg(hub_dev, "Port indicators are supported\n");
1442 }
1443
1444 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1445 hub->descriptor->bPwrOn2PwrGood * 2);
1446
1447 /* power budgeting mostly matters with bus-powered hubs,
1448 * and battery-powered root hubs (may provide just 8 mA).
1449 */
1450 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001451 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 message = "can't get hub status";
1453 goto fail;
1454 }
Alan Stern7d35b922005-04-25 11:18:32 -04001455 le16_to_cpus(&hubstatus);
1456 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001457 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1458 hub->mA_per_port = 500;
1459 else {
1460 hub->mA_per_port = hdev->bus_mA;
1461 hub->limited_power = 1;
1462 }
Alan Stern7d35b922005-04-25 11:18:32 -04001463 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1465 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001466 hub->limited_power = 1;
1467 if (hdev->maxchild > 0) {
1468 int remaining = hdev->bus_mA -
1469 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Alan Stern55c52712005-11-23 12:03:12 -05001471 if (remaining < hdev->maxchild * 100)
1472 dev_warn(hub_dev,
1473 "insufficient power available "
1474 "to use all downstream ports\n");
1475 hub->mA_per_port = 100; /* 7.2.1.1 */
1476 }
1477 } else { /* Self-powered external hub */
1478 /* FIXME: What about battery-powered external hubs that
1479 * provide less current per port? */
1480 hub->mA_per_port = 500;
1481 }
1482 if (hub->mA_per_port < 500)
1483 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1484 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001486 /* Update the HCD's internal representation of this hub before khubd
1487 * starts getting port status changes for devices under the hub.
1488 */
1489 hcd = bus_to_hcd(hdev->bus);
1490 if (hcd->driver->update_hub_device) {
1491 ret = hcd->driver->update_hub_device(hcd, hdev,
1492 &hub->tt, GFP_KERNEL);
1493 if (ret < 0) {
1494 message = "can't update HCD hub info";
1495 goto fail;
1496 }
1497 }
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1500 if (ret < 0) {
1501 message = "can't get hub status";
1502 goto fail;
1503 }
1504
1505 /* local power status reports aren't always correct */
1506 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1507 dev_dbg(hub_dev, "local power source is %s\n",
1508 (hubstatus & HUB_STATUS_LOCAL_POWER)
1509 ? "lost (inactive)" : "good");
1510
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001511 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 dev_dbg(hub_dev, "%sover-current condition exists\n",
1513 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1514
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001515 /* set up the interrupt endpoint
1516 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1517 * bytes as USB2.0[11.12.3] says because some hubs are known
1518 * to send more data (and thus cause overflow). For root hubs,
1519 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1520 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1522 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1523
1524 if (maxp > sizeof(*hub->buffer))
1525 maxp = sizeof(*hub->buffer);
1526
1527 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1528 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 ret = -ENOMEM;
1530 goto fail;
1531 }
1532
1533 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1534 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 /* maybe cycle the hub leds */
1537 if (hub->has_indicators && blinkenlights)
1538 hub->indicator [0] = INDICATOR_CYCLE;
1539
Lan Tianyufa2a9562012-09-05 13:44:31 +08001540 for (i = 0; i < hdev->maxchild; i++)
1541 if (usb_hub_create_port_device(hub, i + 1) < 0)
1542 dev_err(hub->intfdev,
1543 "couldn't create port%d device.\n", i + 1);
1544
Alan Sternf2835212008-04-28 11:07:17 -04001545 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547
1548fail:
1549 dev_err (hub_dev, "config failed, %s (err %d)\n",
1550 message, ret);
1551 /* hub_disconnect() frees urb and descriptor */
1552 return ret;
1553}
1554
Alan Sterne8054852007-05-04 11:55:11 -04001555static void hub_release(struct kref *kref)
1556{
1557 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1558
1559 usb_put_intf(to_usb_interface(hub->intfdev));
1560 kfree(hub);
1561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563static unsigned highspeed_hubs;
1564
1565static void hub_disconnect(struct usb_interface *intf)
1566{
Huajun Li88162302012-03-12 21:00:19 +08001567 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001568 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001569 int i;
1570
Alan Sterne8054852007-05-04 11:55:11 -04001571 /* Take the hub off the event list and don't let it be added again */
1572 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001573 if (!list_empty(&hub->event_list)) {
1574 list_del_init(&hub->event_list);
1575 usb_autopm_put_interface_no_suspend(intf);
1576 }
Alan Sterne8054852007-05-04 11:55:11 -04001577 hub->disconnected = 1;
1578 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Alan Stern7de18d82006-06-01 13:37:24 -04001580 /* Disconnect all children and quiesce the hub */
1581 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001582 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001583
Alan Stern8b28c752005-08-10 17:04:13 -04001584 usb_set_intfdata (intf, NULL);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001585
1586 for (i = 0; i < hdev->maxchild; i++)
1587 usb_hub_remove_port_device(hub, i + 1);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001588 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Alan Sterne8054852007-05-04 11:55:11 -04001590 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 highspeed_hubs--;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001594 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001595 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001596 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001597 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Alan Sterne8054852007-05-04 11:55:11 -04001599 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
1602static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1603{
1604 struct usb_host_interface *desc;
1605 struct usb_endpoint_descriptor *endpoint;
1606 struct usb_device *hdev;
1607 struct usb_hub *hub;
1608
1609 desc = intf->cur_altsetting;
1610 hdev = interface_to_usbdev(intf);
1611
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001612 /* Hubs have proper suspend/resume support. */
1613 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001614
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001615 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001616 dev_err(&intf->dev,
1617 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001618 return -E2BIG;
1619 }
1620
David Brownell89ccbdc2006-04-02 10:18:09 -08001621#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1622 if (hdev->parent) {
1623 dev_warn(&intf->dev, "ignoring external hub\n");
1624 return -ENODEV;
1625 }
1626#endif
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /* Some hubs have a subclass of 1, which AFAICT according to the */
1629 /* specs is not defined, but it works */
1630 if ((desc->desc.bInterfaceSubClass != 0) &&
1631 (desc->desc.bInterfaceSubClass != 1)) {
1632descriptor_error:
1633 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1634 return -EIO;
1635 }
1636
1637 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1638 if (desc->desc.bNumEndpoints != 1)
1639 goto descriptor_error;
1640
1641 endpoint = &desc->endpoint[0].desc;
1642
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001643 /* If it's not an interrupt in endpoint, we'd better punt! */
1644 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 goto descriptor_error;
1646
1647 /* We found a hub */
1648 dev_info (&intf->dev, "USB hub found\n");
1649
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001650 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (!hub) {
1652 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1653 return -ENOMEM;
1654 }
1655
Alan Sterne8054852007-05-04 11:55:11 -04001656 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 INIT_LIST_HEAD(&hub->event_list);
1658 hub->intfdev = &intf->dev;
1659 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001660 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001661 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001662 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001665 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 if (hdev->speed == USB_SPEED_HIGH)
1668 highspeed_hubs++;
1669
1670 if (hub_configure(hub, endpoint) >= 0)
1671 return 0;
1672
1673 hub_disconnect (intf);
1674 return -ENODEV;
1675}
1676
1677static int
1678hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1679{
1680 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuff823c792012-09-05 13:44:32 +08001681 struct usb_hub *hub = hdev_to_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 /* assert ifno == 0 (part of hub spec) */
1684 switch (code) {
1685 case USBDEVFS_HUB_PORTINFO: {
1686 struct usbdevfs_hub_portinfo *info = user_data;
1687 int i;
1688
1689 spin_lock_irq(&device_state_lock);
1690 if (hdev->devnum <= 0)
1691 info->nports = 0;
1692 else {
1693 info->nports = hdev->maxchild;
1694 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001695 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 info->port[i] = 0;
1697 else
1698 info->port[i] =
Lan Tianyuff823c792012-09-05 13:44:32 +08001699 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 }
1702 spin_unlock_irq(&device_state_lock);
1703
1704 return info->nports + 1;
1705 }
1706
1707 default:
1708 return -ENOSYS;
1709 }
1710}
1711
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001712/*
1713 * Allow user programs to claim ports on a hub. When a device is attached
1714 * to one of these "claimed" ports, the program will "own" the device.
1715 */
1716static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001717 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001718{
1719 if (hdev->state == USB_STATE_NOTATTACHED)
1720 return -ENODEV;
1721 if (port1 == 0 || port1 > hdev->maxchild)
1722 return -EINVAL;
1723
1724 /* This assumes that devices not managed by the hub driver
1725 * will always have maxchild equal to 0.
1726 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001727 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001728 return 0;
1729}
1730
1731/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001732int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1733 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001734{
1735 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001736 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001737
1738 rc = find_port_owner(hdev, port1, &powner);
1739 if (rc)
1740 return rc;
1741 if (*powner)
1742 return -EBUSY;
1743 *powner = owner;
1744 return rc;
1745}
1746
Lan Tianyu336c5c32012-07-06 14:13:52 +08001747int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1748 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001749{
1750 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001751 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001752
1753 rc = find_port_owner(hdev, port1, &powner);
1754 if (rc)
1755 return rc;
1756 if (*powner != owner)
1757 return -ENOENT;
1758 *powner = NULL;
1759 return rc;
1760}
1761
Lan Tianyu336c5c32012-07-06 14:13:52 +08001762void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001763{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001764 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001765 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001766
Lan Tianyufa2a9562012-09-05 13:44:31 +08001767 for (n = 0; n < hdev->maxchild; n++) {
1768 if (hub->ports[n]->port_owner == owner)
1769 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001770 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001771
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001772}
1773
1774/* The caller must hold udev's lock */
1775bool usb_device_is_owned(struct usb_device *udev)
1776{
1777 struct usb_hub *hub;
1778
1779 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1780 return false;
1781 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001782 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1786{
Lan Tianyuff823c792012-09-05 13:44:32 +08001787 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int i;
1789
1790 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001791 if (hub->ports[i]->child)
1792 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001794 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001795 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 udev->state = USB_STATE_NOTATTACHED;
1797}
1798
1799/**
1800 * usb_set_device_state - change a device's current state (usbcore, hcds)
1801 * @udev: pointer to device whose state should be changed
1802 * @new_state: new state value to be stored
1803 *
1804 * udev->state is _not_ fully protected by the device lock. Although
1805 * most transitions are made only while holding the lock, the state can
1806 * can change to USB_STATE_NOTATTACHED at almost any time. This
1807 * is so that devices can be marked as disconnected as soon as possible,
1808 * without having to wait for any semaphores to be released. As a result,
1809 * all changes to any device's state must be protected by the
1810 * device_state_lock spinlock.
1811 *
1812 * Once a device has been added to the device tree, all changes to its state
1813 * should be made using this routine. The state should _not_ be set directly.
1814 *
1815 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1816 * Otherwise udev->state is set to new_state, and if new_state is
1817 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1818 * to USB_STATE_NOTATTACHED.
1819 */
1820void usb_set_device_state(struct usb_device *udev,
1821 enum usb_device_state new_state)
1822{
1823 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001824 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 spin_lock_irqsave(&device_state_lock, flags);
1827 if (udev->state == USB_STATE_NOTATTACHED)
1828 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001829 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001830
1831 /* root hub wakeup capabilities are managed out-of-band
1832 * and may involve silicon errata ... ignore them here.
1833 */
1834 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001835 if (udev->state == USB_STATE_SUSPENDED
1836 || new_state == USB_STATE_SUSPENDED)
1837 ; /* No change to wakeup settings */
1838 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001839 wakeup = udev->actconfig->desc.bmAttributes
1840 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001841 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001842 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001843 }
Sarah Sharp15123002007-12-21 16:54:15 -08001844 if (udev->state == USB_STATE_SUSPENDED &&
1845 new_state != USB_STATE_SUSPENDED)
1846 udev->active_duration -= jiffies;
1847 else if (new_state == USB_STATE_SUSPENDED &&
1848 udev->state != USB_STATE_SUSPENDED)
1849 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001850 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001851 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 recursively_mark_NOTATTACHED(udev);
1853 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001854 if (wakeup >= 0)
1855 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856}
David Vrabel6da9c992009-02-18 14:43:47 +00001857EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001859/*
Alan Stern3b29b682011-02-22 09:53:41 -05001860 * Choose a device number.
1861 *
1862 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1863 * USB-2.0 buses they are also used as device addresses, however on
1864 * USB-3.0 buses the address is assigned by the controller hardware
1865 * and it usually is not the same as the device number.
1866 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001867 * WUSB devices are simple: they have no hubs behind, so the mapping
1868 * device <-> virtual port number becomes 1:1. Why? to simplify the
1869 * life of the device connection logic in
1870 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1871 * handshake we need to assign a temporary address in the unauthorized
1872 * space. For simplicity we use the first virtual port number found to
1873 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1874 * and that becomes it's address [X < 128] or its unauthorized address
1875 * [X | 0x80].
1876 *
1877 * We add 1 as an offset to the one-based USB-stack port number
1878 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1879 * 0 is reserved by USB for default address; (b) Linux's USB stack
1880 * uses always #1 for the root hub of the controller. So USB stack's
1881 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001882 *
1883 * Devices connected under xHCI are not as simple. The host controller
1884 * supports virtualization, so the hardware assigns device addresses and
1885 * the HCD must setup data structures before issuing a set address
1886 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001887 */
Alan Stern3b29b682011-02-22 09:53:41 -05001888static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
1890 int devnum;
1891 struct usb_bus *bus = udev->bus;
1892
1893 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001894 if (udev->wusb) {
1895 devnum = udev->portnum + 1;
1896 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1897 } else {
1898 /* Try to allocate the next devnum beginning at
1899 * bus->devnum_next. */
1900 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1901 bus->devnum_next);
1902 if (devnum >= 128)
1903 devnum = find_next_zero_bit(bus->devmap.devicemap,
1904 128, 1);
1905 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (devnum < 128) {
1908 set_bit(devnum, bus->devmap.devicemap);
1909 udev->devnum = devnum;
1910 }
1911}
1912
Alan Stern3b29b682011-02-22 09:53:41 -05001913static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
1915 if (udev->devnum > 0) {
1916 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1917 udev->devnum = -1;
1918 }
1919}
1920
Alan Stern3b29b682011-02-22 09:53:41 -05001921static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001922{
1923 /* The address for a WUSB device is managed by wusbcore. */
1924 if (!udev->wusb)
1925 udev->devnum = devnum;
1926}
1927
Herbert Xuf7410ce2010-01-10 20:15:03 +11001928static void hub_free_dev(struct usb_device *udev)
1929{
1930 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1931
1932 /* Root hubs aren't real devices, so don't free HCD resources */
1933 if (hcd->driver->free_dev && udev->parent)
1934 hcd->driver->free_dev(hcd, udev);
1935}
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937/**
1938 * usb_disconnect - disconnect a device (usbcore-internal)
1939 * @pdev: pointer to device being disconnected
1940 * Context: !in_interrupt ()
1941 *
1942 * Something got disconnected. Get rid of it and all of its children.
1943 *
1944 * If *pdev is a normal device then the parent hub must already be locked.
1945 * If *pdev is a root hub then this routine will acquire the
1946 * usb_bus_list_lock on behalf of the caller.
1947 *
1948 * Only hub drivers (including virtual root hub drivers for host
1949 * controllers) should ever call this.
1950 *
1951 * This call is synchronous, and may not be used in an interrupt context.
1952 */
1953void usb_disconnect(struct usb_device **pdev)
1954{
1955 struct usb_device *udev = *pdev;
Lan Tianyuff823c792012-09-05 13:44:32 +08001956 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 int i;
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /* mark the device as inactive, so any further urb submissions for
1960 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001961 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
1963 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001964 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1965 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001967 usb_lock_device(udev);
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001970 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001971 if (hub->ports[i]->child)
1972 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974
1975 /* deallocate hcd/hardware state ... nuking all pending urbs and
1976 * cleaning up all state associated with the current configuration
1977 * so that the hardware is now fully quiesced.
1978 */
Alan Stern782da722006-07-01 22:09:35 -04001979 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001981 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Alan Stern3b23dd62008-12-05 14:10:34 -05001983 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001984 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001985
Alan Stern782da722006-07-01 22:09:35 -04001986 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001987 * for de-configuring the device and invoking the remove-device
1988 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001989 */
1990 device_del(&udev->dev);
1991
1992 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 * (or root_hub) pointer.
1994 */
Alan Stern3b29b682011-02-22 09:53:41 -05001995 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 /* Avoid races with recursively_mark_NOTATTACHED() */
1998 spin_lock_irq(&device_state_lock);
1999 *pdev = NULL;
2000 spin_unlock_irq(&device_state_lock);
2001
Herbert Xuf7410ce2010-01-10 20:15:03 +11002002 hub_free_dev(udev);
2003
Alan Stern782da722006-07-01 22:09:35 -04002004 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002007#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008static void show_string(struct usb_device *udev, char *id, char *string)
2009{
2010 if (!string)
2011 return;
2012 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2013}
2014
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002015static void announce_device(struct usb_device *udev)
2016{
2017 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2018 le16_to_cpu(udev->descriptor.idVendor),
2019 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002020 dev_info(&udev->dev,
2021 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002022 udev->descriptor.iManufacturer,
2023 udev->descriptor.iProduct,
2024 udev->descriptor.iSerialNumber);
2025 show_string(udev, "Product", udev->product);
2026 show_string(udev, "Manufacturer", udev->manufacturer);
2027 show_string(udev, "SerialNumber", udev->serial);
2028}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002030static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031#endif
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033#ifdef CONFIG_USB_OTG
2034#include "otg_whitelist.h"
2035#endif
2036
Oliver Neukum3ede7602007-01-11 14:35:50 +01002037/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002038 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002039 * @udev: newly addressed device (in ADDRESS state)
2040 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002041 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002042 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002043static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002045 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047#ifdef CONFIG_USB_OTG
2048 /*
2049 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2050 * to wake us after we've powered off VBUS; and HNP, switching roles
2051 * "host" to "peripheral". The OTG descriptor helps figure this out.
2052 */
2053 if (!udev->bus->is_b_host
2054 && udev->config
2055 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002056 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 struct usb_bus *bus = udev->bus;
2058
2059 /* descriptor may appear anywhere in config */
2060 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2061 le16_to_cpu(udev->config[0].desc.wTotalLength),
2062 USB_DT_OTG, (void **) &desc) == 0) {
2063 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002064 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 dev_info(&udev->dev,
2067 "Dual-Role OTG device on %sHNP port\n",
2068 (port1 == bus->otg_port)
2069 ? "" : "non-");
2070
2071 /* enable HNP before suspend, it's simpler */
2072 if (port1 == bus->otg_port)
2073 bus->b_hnp_enable = 1;
2074 err = usb_control_msg(udev,
2075 usb_sndctrlpipe(udev, 0),
2076 USB_REQ_SET_FEATURE, 0,
2077 bus->b_hnp_enable
2078 ? USB_DEVICE_B_HNP_ENABLE
2079 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2080 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2081 if (err < 0) {
2082 /* OTG MESSAGE: report errors here,
2083 * customize to match your product.
2084 */
2085 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002086 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 err);
2088 bus->b_hnp_enable = 0;
2089 }
2090 }
2091 }
2092 }
2093
2094 if (!is_targeted(udev)) {
2095
2096 /* Maybe it can talk to us, though we can't talk to it.
2097 * (Includes HNP test device.)
2098 */
2099 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002100 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (err < 0)
2102 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2103 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002104 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 goto fail;
2106 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002107fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002109 return err;
2110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002112
2113/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002114 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002115 * @udev: newly addressed device (in ADDRESS state)
2116 *
2117 * This is only called by usb_new_device() and usb_authorize_device()
2118 * and FIXME -- all comments that apply to them apply here wrt to
2119 * environment.
2120 *
2121 * If the device is WUSB and not authorized, we don't attempt to read
2122 * the string descriptors, as they will be errored out by the device
2123 * until it has been authorized.
2124 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002125static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002126{
2127 int err;
2128
2129 if (udev->config == NULL) {
2130 err = usb_get_configuration(udev);
2131 if (err < 0) {
2132 dev_err(&udev->dev, "can't read configurations, error %d\n",
2133 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002134 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002135 }
2136 }
2137 if (udev->wusb == 1 && udev->authorized == 0) {
2138 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2139 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2140 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2141 }
2142 else {
2143 /* read the standard strings and cache them if present */
2144 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2145 udev->manufacturer = usb_cache_string(udev,
2146 udev->descriptor.iManufacturer);
2147 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2148 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002149 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002150 if (err < 0)
2151 return err;
2152
2153 usb_detect_interface_quirks(udev);
2154
2155 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002156}
2157
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002158static void set_usb_port_removable(struct usb_device *udev)
2159{
2160 struct usb_device *hdev = udev->parent;
2161 struct usb_hub *hub;
2162 u8 port = udev->portnum;
2163 u16 wHubCharacteristics;
2164 bool removable = true;
2165
2166 if (!hdev)
2167 return;
2168
2169 hub = hdev_to_hub(udev->parent);
2170
2171 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2172
2173 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2174 return;
2175
2176 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002177 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2178 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002179 removable = false;
2180 } else {
2181 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2182 removable = false;
2183 }
2184
2185 if (removable)
2186 udev->removable = USB_DEVICE_REMOVABLE;
2187 else
2188 udev->removable = USB_DEVICE_FIXED;
2189}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002190
2191/**
2192 * usb_new_device - perform initial device setup (usbcore-internal)
2193 * @udev: newly addressed device (in ADDRESS state)
2194 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002195 * This is called with devices which have been detected but not fully
2196 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002197 * for any device configuration. The caller must have locked either
2198 * the parent hub (if udev is a normal device) or else the
2199 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2200 * udev has already been installed, but udev is not yet visible through
2201 * sysfs or other filesystem code.
2202 *
2203 * It will return if the device is configured properly or not. Zero if
2204 * the interface was registered with the driver core; else a negative
2205 * errno value.
2206 *
2207 * This call is synchronous, and may not be used in an interrupt context.
2208 *
2209 * Only the hub driver or root-hub registrar should ever call this.
2210 */
2211int usb_new_device(struct usb_device *udev)
2212{
2213 int err;
2214
Dan Streetman16985402010-01-06 09:56:53 -05002215 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002216 /* Initialize non-root-hub device wakeup to disabled;
2217 * device (un)configuration controls wakeup capable
2218 * sysfs power/wakeup controls wakeup enabled/disabled
2219 */
2220 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002221 }
2222
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002223 /* Tell the runtime-PM framework the device is active */
2224 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002225 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002226 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002227 pm_runtime_enable(&udev->dev);
2228
Alan Sternc08512c2010-11-15 15:57:58 -05002229 /* By default, forbid autosuspend for all devices. It will be
2230 * allowed for hubs during binding.
2231 */
2232 usb_disable_autosuspend(udev);
2233
Alan Stern8d8558d2009-12-08 15:50:41 -05002234 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002235 if (err < 0)
2236 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002237 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2238 udev->devnum, udev->bus->busnum,
2239 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002240 /* export the usbdev device-node for libusb */
2241 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2242 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2243
Alan Stern6cd13202008-11-13 15:08:30 -05002244 /* Tell the world! */
2245 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002246
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002247 if (udev->serial)
2248 add_device_randomness(udev->serial, strlen(udev->serial));
2249 if (udev->product)
2250 add_device_randomness(udev->product, strlen(udev->product));
2251 if (udev->manufacturer)
2252 add_device_randomness(udev->manufacturer,
2253 strlen(udev->manufacturer));
2254
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002255 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002256
2257 /*
2258 * check whether the hub marks this port as non-removable. Do it
2259 * now so that platform-specific data can override it in
2260 * device_add()
2261 */
2262 if (udev->parent)
2263 set_usb_port_removable(udev);
2264
Alan Stern782da722006-07-01 22:09:35 -04002265 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002266 * for configuring the device and invoking the add-device
2267 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002268 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002269 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (err) {
2271 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2272 goto fail;
2273 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002274
Alan Stern3b23dd62008-12-05 14:10:34 -05002275 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002276 usb_mark_last_busy(udev);
2277 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002278 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280fail:
2281 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002282 pm_runtime_disable(&udev->dev);
2283 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002284 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002287
2288/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002289 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2290 * @usb_dev: USB device
2291 *
2292 * Move the USB device to a very basic state where interfaces are disabled
2293 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002294 *
2295 * We share a lock (that we have) with device_del(), so we need to
2296 * defer its call.
2297 */
2298int usb_deauthorize_device(struct usb_device *usb_dev)
2299{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002300 usb_lock_device(usb_dev);
2301 if (usb_dev->authorized == 0)
2302 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002303
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002304 usb_dev->authorized = 0;
2305 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002306
2307 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002308 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002309 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002310 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002311 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002312 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002313
2314 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002315 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002316
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002317out_unauthorized:
2318 usb_unlock_device(usb_dev);
2319 return 0;
2320}
2321
2322
2323int usb_authorize_device(struct usb_device *usb_dev)
2324{
2325 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002326
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002327 usb_lock_device(usb_dev);
2328 if (usb_dev->authorized == 1)
2329 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002330
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002331 result = usb_autoresume_device(usb_dev);
2332 if (result < 0) {
2333 dev_err(&usb_dev->dev,
2334 "can't autoresume for authorization: %d\n", result);
2335 goto error_autoresume;
2336 }
2337 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2338 if (result < 0) {
2339 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2340 "authorization: %d\n", result);
2341 goto error_device_descriptor;
2342 }
Alan Sternda307122009-12-08 15:54:44 -05002343
2344 kfree(usb_dev->product);
2345 usb_dev->product = NULL;
2346 kfree(usb_dev->manufacturer);
2347 usb_dev->manufacturer = NULL;
2348 kfree(usb_dev->serial);
2349 usb_dev->serial = NULL;
2350
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002351 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002352 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002353 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002354 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002355 /* Choose and set the configuration. This registers the interfaces
2356 * with the driver core and lets interface drivers bind to them.
2357 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002358 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002359 if (c >= 0) {
2360 result = usb_set_configuration(usb_dev, c);
2361 if (result) {
2362 dev_err(&usb_dev->dev,
2363 "can't set config #%d, error %d\n", c, result);
2364 /* This need not be fatal. The user can try to
2365 * set other configurations. */
2366 }
2367 }
2368 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002369
Alan Stern8d8558d2009-12-08 15:50:41 -05002370error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002371error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002372 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002373error_autoresume:
2374out_authorized:
2375 usb_unlock_device(usb_dev); // complements locktree
2376 return result;
2377}
2378
2379
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002380/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2381static unsigned hub_is_wusb(struct usb_hub *hub)
2382{
2383 struct usb_hcd *hcd;
2384 if (hub->hdev->parent != NULL) /* not a root hub? */
2385 return 0;
2386 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2387 return hcd->wireless;
2388}
2389
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391#define PORT_RESET_TRIES 5
2392#define SET_ADDRESS_TRIES 2
2393#define GET_DESCRIPTOR_TRIES 2
2394#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302395#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
2397#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2398#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002399#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400#define HUB_LONG_RESET_TIME 200
2401#define HUB_RESET_TIMEOUT 500
2402
Sarah Sharp10d674a2011-09-14 14:24:52 -07002403static int hub_port_reset(struct usb_hub *hub, int port1,
2404 struct usb_device *udev, unsigned int delay, bool warm);
2405
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002406/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2407 * Port worm reset is required to recover
2408 */
2409static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002410{
2411 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002412 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2413 USB_SS_PORT_LS_SS_INACTIVE) ||
2414 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2415 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002416}
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002419 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420{
2421 int delay_time, ret;
2422 u16 portstatus;
2423 u16 portchange;
2424
2425 for (delay_time = 0;
2426 delay_time < HUB_RESET_TIMEOUT;
2427 delay_time += delay) {
2428 /* wait to give the device a chance to reset */
2429 msleep(delay);
2430
2431 /* read and decode port status */
2432 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2433 if (ret < 0)
2434 return ret;
2435
Andiry Xu75d7cf72011-09-13 16:41:11 -07002436 /*
2437 * Some buggy devices require a warm reset to be issued even
2438 * when the port appears not to be connected.
2439 */
2440 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002441 /*
2442 * Some buggy devices can cause an NEC host controller
2443 * to transition to the "Error" state after a hot port
2444 * reset. This will show up as the port state in
2445 * "Inactive", and the port may also report a
2446 * disconnect. Forcing a warm port reset seems to make
2447 * the device work.
2448 *
2449 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2450 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002451 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002452 int ret;
2453
2454 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2455 clear_port_feature(hub->hdev, port1,
2456 USB_PORT_FEAT_C_CONNECTION);
2457 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2458 clear_port_feature(hub->hdev, port1,
2459 USB_PORT_FEAT_C_PORT_LINK_STATE);
2460 if (portchange & USB_PORT_STAT_C_RESET)
2461 clear_port_feature(hub->hdev, port1,
2462 USB_PORT_FEAT_C_RESET);
2463 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2464 port1);
2465 ret = hub_port_reset(hub, port1,
2466 udev, HUB_BH_RESET_TIME,
2467 true);
2468 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2469 clear_port_feature(hub->hdev, port1,
2470 USB_PORT_FEAT_C_CONNECTION);
2471 return ret;
2472 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002473 /* Device went away? */
2474 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2475 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Andiry Xu75d7cf72011-09-13 16:41:11 -07002477 /* bomb out completely if the connection bounced */
2478 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2479 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Andiry Xu75d7cf72011-09-13 16:41:11 -07002481 /* if we`ve finished resetting, then break out of
2482 * the loop
2483 */
2484 if (!(portstatus & USB_PORT_STAT_RESET) &&
2485 (portstatus & USB_PORT_STAT_ENABLE)) {
2486 if (hub_is_wusb(hub))
2487 udev->speed = USB_SPEED_WIRELESS;
2488 else if (hub_is_superspeed(hub->hdev))
2489 udev->speed = USB_SPEED_SUPER;
2490 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2491 udev->speed = USB_SPEED_HIGH;
2492 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2493 udev->speed = USB_SPEED_LOW;
2494 else
2495 udev->speed = USB_SPEED_FULL;
2496 return 0;
2497 }
2498 } else {
2499 if (portchange & USB_PORT_STAT_C_BH_RESET)
2500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
2502
2503 /* switch to the long delay after two short delay failures */
2504 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2505 delay = HUB_LONG_RESET_TIME;
2506
2507 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002508 "port %d not %sreset yet, waiting %dms\n",
2509 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 }
2511
2512 return -EBUSY;
2513}
2514
Andiry Xu75d7cf72011-09-13 16:41:11 -07002515static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2516 struct usb_device *udev, int *status, bool warm)
2517{
2518 switch (*status) {
2519 case 0:
2520 if (!warm) {
2521 struct usb_hcd *hcd;
2522 /* TRSTRCY = 10 ms; plus some extra */
2523 msleep(10 + 40);
2524 update_devnum(udev, 0);
2525 hcd = bus_to_hcd(udev->bus);
2526 if (hcd->driver->reset_device) {
2527 *status = hcd->driver->reset_device(hcd, udev);
2528 if (*status < 0) {
2529 dev_err(&udev->dev, "Cannot reset "
2530 "HCD device state\n");
2531 break;
2532 }
2533 }
2534 }
2535 /* FALL THROUGH */
2536 case -ENOTCONN:
2537 case -ENODEV:
2538 clear_port_feature(hub->hdev,
2539 port1, USB_PORT_FEAT_C_RESET);
2540 /* FIXME need disconnect() for NOTATTACHED device */
2541 if (warm) {
2542 clear_port_feature(hub->hdev, port1,
2543 USB_PORT_FEAT_C_BH_PORT_RESET);
2544 clear_port_feature(hub->hdev, port1,
2545 USB_PORT_FEAT_C_PORT_LINK_STATE);
2546 } else {
2547 usb_set_device_state(udev, *status
2548 ? USB_STATE_NOTATTACHED
2549 : USB_STATE_DEFAULT);
2550 }
2551 break;
2552 }
2553}
2554
2555/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002557 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
2559 int i, status;
2560
Andiry Xu75d7cf72011-09-13 16:41:11 -07002561 if (!warm) {
2562 /* Block EHCI CF initialization during the port reset.
2563 * Some companion controllers don't like it when they mix.
2564 */
2565 down_read(&ehci_cf_port_reset_rwsem);
2566 } else {
2567 if (!hub_is_superspeed(hub->hdev)) {
2568 dev_err(hub->intfdev, "only USB3 hub support "
2569 "warm reset\n");
2570 return -EINVAL;
2571 }
2572 }
Alan Stern32fe0192007-10-10 16:27:07 -04002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 /* Reset the port */
2575 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002576 status = set_port_feature(hub->hdev, port1, (warm ?
2577 USB_PORT_FEAT_BH_PORT_RESET :
2578 USB_PORT_FEAT_RESET));
2579 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002581 "cannot %sreset port %d (err = %d)\n",
2582 warm ? "warm " : "", port1, status);
2583 } else {
2584 status = hub_port_wait_reset(hub, port1, udev, delay,
2585 warm);
David Brownelldd165252005-08-31 10:45:25 -07002586 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 dev_dbg(hub->intfdev,
2588 "port_wait_reset: err = %d\n",
2589 status);
2590 }
2591
2592 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002593 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2594 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002595 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
2597
2598 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002599 "port %d not enabled, trying %sreset again...\n",
2600 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 delay = HUB_LONG_RESET_TIME;
2602 }
2603
2604 dev_err (hub->intfdev,
2605 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2606 port1);
2607
Andiry Xu75d7cf72011-09-13 16:41:11 -07002608done:
2609 if (!warm)
2610 up_read(&ehci_cf_port_reset_rwsem);
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 return status;
2613}
2614
Andiry Xu0ed9a572011-04-27 18:07:43 +08002615/* Check if a port is power on */
2616static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2617{
2618 int ret = 0;
2619
2620 if (hub_is_superspeed(hub->hdev)) {
2621 if (portstatus & USB_SS_PORT_STAT_POWER)
2622 ret = 1;
2623 } else {
2624 if (portstatus & USB_PORT_STAT_POWER)
2625 ret = 1;
2626 }
2627
2628 return ret;
2629}
2630
Alan Sternd388dab2006-07-01 22:14:24 -04002631#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Andiry Xu0ed9a572011-04-27 18:07:43 +08002633/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2634static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2635{
2636 int ret = 0;
2637
2638 if (hub_is_superspeed(hub->hdev)) {
2639 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2640 == USB_SS_PORT_LS_U3)
2641 ret = 1;
2642 } else {
2643 if (portstatus & USB_PORT_STAT_SUSPEND)
2644 ret = 1;
2645 }
2646
2647 return ret;
2648}
Alan Sternb01b03f2008-04-28 11:06:11 -04002649
2650/* Determine whether the device on a port is ready for a normal resume,
2651 * is ready for a reset-resume, or should be disconnected.
2652 */
2653static int check_port_resume_type(struct usb_device *udev,
2654 struct usb_hub *hub, int port1,
2655 int status, unsigned portchange, unsigned portstatus)
2656{
2657 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002658 if (status || port_is_suspended(hub, portstatus) ||
2659 !port_is_power_on(hub, portstatus) ||
2660 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002661 if (status >= 0)
2662 status = -ENODEV;
2663 }
2664
Alan Stern86c57ed2008-06-30 11:14:43 -04002665 /* Can't do a normal resume if the port isn't enabled,
2666 * so try a reset-resume instead.
2667 */
2668 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2669 if (udev->persist_enabled)
2670 udev->reset_resume = 1;
2671 else
2672 status = -ENODEV;
2673 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002674
2675 if (status) {
2676 dev_dbg(hub->intfdev,
2677 "port %d status %04x.%04x after resume, %d\n",
2678 port1, portchange, portstatus, status);
2679 } else if (udev->reset_resume) {
2680
2681 /* Late port handoff can set status-change bits */
2682 if (portchange & USB_PORT_STAT_C_CONNECTION)
2683 clear_port_feature(hub->hdev, port1,
2684 USB_PORT_FEAT_C_CONNECTION);
2685 if (portchange & USB_PORT_STAT_C_ENABLE)
2686 clear_port_feature(hub->hdev, port1,
2687 USB_PORT_FEAT_C_ENABLE);
2688 }
2689
2690 return status;
2691}
2692
Sarah Sharpf74631e2012-06-25 12:08:08 -07002693int usb_disable_ltm(struct usb_device *udev)
2694{
2695 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2696
2697 /* Check if the roothub and device supports LTM. */
2698 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2699 !usb_device_supports_ltm(udev))
2700 return 0;
2701
2702 /* Clear Feature LTM Enable can only be sent if the device is
2703 * configured.
2704 */
2705 if (!udev->actconfig)
2706 return 0;
2707
2708 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2709 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2710 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2711 USB_CTRL_SET_TIMEOUT);
2712}
2713EXPORT_SYMBOL_GPL(usb_disable_ltm);
2714
2715void usb_enable_ltm(struct usb_device *udev)
2716{
2717 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2718
2719 /* Check if the roothub and device supports LTM. */
2720 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2721 !usb_device_supports_ltm(udev))
2722 return;
2723
2724 /* Set Feature LTM Enable can only be sent if the device is
2725 * configured.
2726 */
2727 if (!udev->actconfig)
2728 return;
2729
2730 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2731 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2732 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2733 USB_CTRL_SET_TIMEOUT);
2734}
2735EXPORT_SYMBOL_GPL(usb_enable_ltm);
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737#ifdef CONFIG_USB_SUSPEND
2738
2739/*
Alan Stern140d8f62006-07-01 22:07:21 -04002740 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002741 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002742 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 *
2744 * Suspends a USB device that isn't in active use, conserving power.
2745 * Devices may wake out of a suspend, if anything important happens,
2746 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002747 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 * to disconnect devices while they are suspended.
2749 *
David Brownell390a8c32005-09-13 19:57:27 -07002750 * This only affects the USB hardware for a device; its interfaces
2751 * (and, for hubs, child devices) must already have been suspended.
2752 *
Alan Stern624d6c02007-05-30 15:35:16 -04002753 * Selective port suspend reduces power; most suspended devices draw
2754 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2755 * All devices below the suspended port are also suspended.
2756 *
2757 * Devices leave suspend state when the host wakes them up. Some devices
2758 * also support "remote wakeup", where the device can activate the USB
2759 * tree above them to deliver data, such as a keypress or packet. In
2760 * some cases, this wakes the USB host.
2761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 * Suspending OTG devices may trigger HNP, if that's been enabled
2763 * between a pair of dual-role devices. That will change roles, such
2764 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2765 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002766 * Devices on USB hub ports have only one "suspend" state, corresponding
2767 * to ACPI D2, "may cause the device to lose some context".
2768 * State transitions include:
2769 *
2770 * - suspend, resume ... when the VBUS power link stays live
2771 * - suspend, disconnect ... VBUS lost
2772 *
2773 * Once VBUS drop breaks the circuit, the port it's using has to go through
2774 * normal re-enumeration procedures, starting with enabling VBUS power.
2775 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2776 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2777 * timer, no SRP, no requests through sysfs.
2778 *
2779 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2780 * the root hub for their bus goes into global suspend ... so we don't
2781 * (falsely) update the device power state to say it suspended.
2782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 * Returns 0 on success, else negative errno.
2784 */
Alan Stern65bfd292008-11-25 16:39:18 -05002785int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
Alan Stern624d6c02007-05-30 15:35:16 -04002787 struct usb_hub *hub = hdev_to_hub(udev->parent);
2788 int port1 = udev->portnum;
2789 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002790
Alan Stern624d6c02007-05-30 15:35:16 -04002791 /* enable remote wakeup when appropriate; this lets the device
2792 * wake up the upstream hub (including maybe the root hub).
2793 *
2794 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2795 * we don't explicitly enable it here.
2796 */
2797 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002798 if (!hub_is_superspeed(hub->hdev)) {
2799 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2800 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2801 USB_DEVICE_REMOTE_WAKEUP, 0,
2802 NULL, 0,
2803 USB_CTRL_SET_TIMEOUT);
2804 } else {
2805 /* Assume there's only one function on the USB 3.0
2806 * device and enable remote wake for the first
2807 * interface. FIXME if the interface association
2808 * descriptor shows there's more than one function.
2809 */
2810 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2811 USB_REQ_SET_FEATURE,
2812 USB_RECIP_INTERFACE,
2813 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002814 USB_INTRF_FUNC_SUSPEND_RW |
2815 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002816 NULL, 0,
2817 USB_CTRL_SET_TIMEOUT);
2818 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002819 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002820 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2821 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002822 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002823 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002824 return status;
2825 }
Alan Stern624d6c02007-05-30 15:35:16 -04002826 }
2827
Andiry Xu65580b432011-09-23 14:19:52 -07002828 /* disable USB2 hardware LPM */
2829 if (udev->usb2_hw_lpm_enabled == 1)
2830 usb_set_usb2_hardware_lpm(udev, 0);
2831
Sarah Sharpf74631e2012-06-25 12:08:08 -07002832 if (usb_disable_ltm(udev)) {
2833 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2834 __func__);
2835 return -ENOMEM;
2836 }
Sarah Sharp83060952012-05-02 14:25:52 -07002837 if (usb_unlocked_disable_lpm(udev)) {
2838 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2839 __func__);
2840 return -ENOMEM;
2841 }
2842
Alan Stern624d6c02007-05-30 15:35:16 -04002843 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002844 if (hub_is_superspeed(hub->hdev))
2845 status = set_port_feature(hub->hdev,
2846 port1 | (USB_SS_PORT_LS_U3 << 3),
2847 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002848 else
2849 status = set_port_feature(hub->hdev, port1,
2850 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002851 if (status) {
2852 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2853 port1, status);
2854 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002855 if (udev->do_remote_wakeup)
2856 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002857 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2858 USB_DEVICE_REMOTE_WAKEUP, 0,
2859 NULL, 0,
2860 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002861
Andiry Xuc3e751e2012-05-05 00:50:10 +08002862 /* Try to enable USB2 hardware LPM again */
2863 if (udev->usb2_hw_lpm_capable == 1)
2864 usb_set_usb2_hardware_lpm(udev, 1);
2865
Sarah Sharpf74631e2012-06-25 12:08:08 -07002866 /* Try to enable USB3 LTM and LPM again */
2867 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002868 usb_unlocked_enable_lpm(udev);
2869
Alan Sterncbb33002011-06-15 16:29:16 -04002870 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002871 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002872 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002873 } else {
2874 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002875 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2876 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2877 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002878 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Sternbfd1e912012-10-19 11:03:39 -04002879 udev->port_is_suspended = 1;
Alan Stern624d6c02007-05-30 15:35:16 -04002880 msleep(10);
2881 }
Alan Sternc08512c2010-11-15 15:57:58 -05002882 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002883 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
David Brownellf3f32532005-09-22 22:37:29 -07002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886/*
David Brownell390a8c32005-09-13 19:57:27 -07002887 * If the USB "suspend" state is in use (rather than "global suspend"),
2888 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002889 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 * hardware resume signaling is finished, either because of selective
2891 * resume (by host) or remote wakeup (by device) ... now see what changed
2892 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002893 *
2894 * If @udev->reset_resume is set then the device is reset before the
2895 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 */
Alan Stern140d8f62006-07-01 22:07:21 -04002897static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Alan Stern54515fe2007-05-30 15:38:58 -04002899 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 u16 devstatus;
2901
2902 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002903 dev_dbg(&udev->dev, "%s\n",
2904 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906 /* usb ch9 identifies four variants of SUSPENDED, based on what
2907 * state the device resumes to. Linux currently won't see the
2908 * first two on the host side; they'd be inside hub_port_init()
2909 * during many timeouts, but khubd can't suspend until later.
2910 */
2911 usb_set_device_state(udev, udev->actconfig
2912 ? USB_STATE_CONFIGURED
2913 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Alan Stern54515fe2007-05-30 15:38:58 -04002915 /* 10.5.4.5 says not to reset a suspended port if the attached
2916 * device is enabled for remote wakeup. Hence the reset
2917 * operation is carried out here, after the port has been
2918 * resumed.
2919 */
2920 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002921 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002922 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 /* 10.5.4.5 says be sure devices in the tree are still there.
2925 * For now let's assume the device didn't go crazy on resume,
2926 * and device drivers will know about any resume quirks.
2927 */
Alan Stern54515fe2007-05-30 15:38:58 -04002928 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002929 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002930 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2931 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002932 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002933
2934 /* If a normal resume failed, try doing a reset-resume */
2935 if (status && !udev->reset_resume && udev->persist_enabled) {
2936 dev_dbg(&udev->dev, "retry with reset-resume\n");
2937 udev->reset_resume = 1;
2938 goto retry_reset_resume;
2939 }
Alan Stern54515fe2007-05-30 15:38:58 -04002940 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002941
Alan Stern624d6c02007-05-30 15:35:16 -04002942 if (status) {
2943 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2944 status);
2945 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002947 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 status = usb_control_msg(udev,
2949 usb_sndctrlpipe(udev, 0),
2950 USB_REQ_CLEAR_FEATURE,
2951 USB_RECIP_DEVICE,
2952 USB_DEVICE_REMOTE_WAKEUP, 0,
2953 NULL, 0,
2954 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002955 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002956 dev_dbg(&udev->dev,
2957 "disable remote wakeup, status %d\n",
2958 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
2962 return status;
2963}
2964
Alan Stern624d6c02007-05-30 15:35:16 -04002965/*
2966 * usb_port_resume - re-activate a suspended usb device's upstream port
2967 * @udev: device to re-activate, not a root hub
2968 * Context: must be able to sleep; device not locked; pm locks held
2969 *
2970 * This will re-activate the suspended device, increasing power usage
2971 * while letting drivers communicate again with its endpoints.
2972 * USB resume explicitly guarantees that the power session between
2973 * the host and the device is the same as it was when the device
2974 * suspended.
2975 *
Alan Sternfeccc302008-03-03 15:15:59 -05002976 * If @udev->reset_resume is set then this routine won't check that the
2977 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002978 * reset @udev. The end result is that a broken power session can be
2979 * recovered and @udev will appear to persist across a loss of VBUS power.
2980 *
2981 * For example, if a host controller doesn't maintain VBUS suspend current
2982 * during a system sleep or is reset when the system wakes up, all the USB
2983 * power sessions below it will be broken. This is especially troublesome
2984 * for mass-storage devices containing mounted filesystems, since the
2985 * device will appear to have disconnected and all the memory mappings
2986 * to it will be lost. Using the USB_PERSIST facility, the device can be
2987 * made to appear as if it had not disconnected.
2988 *
Ming Lei742120c2008-06-18 22:00:29 +08002989 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002990 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002991 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2992 * quite possible for a device to remain unaltered but its media to be
2993 * changed. If the user replaces a flash memory card while the system is
2994 * asleep, he will have only himself to blame when the filesystem on the
2995 * new card is corrupted and the system crashes.
2996 *
Alan Stern624d6c02007-05-30 15:35:16 -04002997 * Returns 0 on success, else negative errno.
2998 */
Alan Stern65bfd292008-11-25 16:39:18 -05002999int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Alan Stern624d6c02007-05-30 15:35:16 -04003001 struct usb_hub *hub = hdev_to_hub(udev->parent);
3002 int port1 = udev->portnum;
3003 int status;
3004 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003005
3006 /* Skip the initial Clear-Suspend step for a remote wakeup */
3007 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003008 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003009 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
3011 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3012
Alan Sternd5cbad42006-08-11 16:52:39 -04003013 set_bit(port1, hub->busy_bits);
3014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003016 if (hub_is_superspeed(hub->hdev))
3017 status = set_port_feature(hub->hdev,
3018 port1 | (USB_SS_PORT_LS_U0 << 3),
3019 USB_PORT_FEAT_LINK_STATE);
3020 else
3021 status = clear_port_feature(hub->hdev,
3022 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003024 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3025 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003028 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003029 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 msleep(25);
3031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3033 * stop resume signaling. Then finish the resume
3034 * sequence.
3035 */
Alan Sternd25450c2006-11-20 11:14:30 -05003036 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003037
Alan Sternb01b03f2008-04-28 11:06:11 -04003038 /* TRSMRCY = 10 msec */
3039 msleep(10);
3040 }
Alan Stern54515fe2007-05-30 15:38:58 -04003041
Alan Sternb01b03f2008-04-28 11:06:11 -04003042 SuspendCleared:
3043 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003044 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003045 if (hub_is_superspeed(hub->hdev)) {
3046 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3047 clear_port_feature(hub->hdev, port1,
3048 USB_PORT_FEAT_C_PORT_LINK_STATE);
3049 } else {
3050 if (portchange & USB_PORT_STAT_C_SUSPEND)
3051 clear_port_feature(hub->hdev, port1,
3052 USB_PORT_FEAT_C_SUSPEND);
3053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Alan Sternd5cbad42006-08-11 16:52:39 -04003056 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003057
Alan Sternb01b03f2008-04-28 11:06:11 -04003058 status = check_port_resume_type(udev,
3059 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003060 if (status == 0)
3061 status = finish_port_resume(udev);
3062 if (status < 0) {
3063 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3064 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003065 } else {
3066 /* Try to enable USB2 hardware LPM */
3067 if (udev->usb2_hw_lpm_capable == 1)
3068 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003069
Sarah Sharpf74631e2012-06-25 12:08:08 -07003070 /* Try to enable USB3 LTM and LPM */
3071 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003072 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003073 }
Andiry Xu65580b432011-09-23 14:19:52 -07003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return status;
3076}
3077
Alan Stern8808f002008-04-28 11:06:55 -04003078/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003079int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
3081 int status = 0;
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003084 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003085 status = usb_autoresume_device(udev);
3086 if (status == 0) {
3087 /* Let the drivers do their thing, then... */
3088 usb_autosuspend_device(udev);
3089 }
Alan Sternd25450c2006-11-20 11:14:30 -05003090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return status;
3092}
3093
Alan Sternd388dab2006-07-01 22:14:24 -04003094#else /* CONFIG_USB_SUSPEND */
3095
3096/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3097
Alan Stern65bfd292008-11-25 16:39:18 -05003098int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003099{
3100 return 0;
3101}
3102
Alan Sternb01b03f2008-04-28 11:06:11 -04003103/* However we may need to do a reset-resume */
3104
Alan Stern65bfd292008-11-25 16:39:18 -05003105int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003106{
Alan Sternb01b03f2008-04-28 11:06:11 -04003107 struct usb_hub *hub = hdev_to_hub(udev->parent);
3108 int port1 = udev->portnum;
3109 int status;
3110 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003111
Alan Sternb01b03f2008-04-28 11:06:11 -04003112 status = hub_port_status(hub, port1, &portstatus, &portchange);
3113 status = check_port_resume_type(udev,
3114 hub, port1, status, portchange, portstatus);
3115
3116 if (status) {
3117 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3118 hub_port_logical_disconnect(hub, port1);
3119 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003120 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003121 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003122 }
3123 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003124}
3125
Alan Sternd388dab2006-07-01 22:14:24 -04003126#endif
3127
David Brownelldb690872005-09-13 19:56:33 -07003128static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 struct usb_hub *hub = usb_get_intfdata (intf);
3131 struct usb_device *hdev = hub->hdev;
3132 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003133 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134
Alan Sterncbb33002011-06-15 16:29:16 -04003135 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3137 struct usb_device *udev;
3138
Lan Tianyuff823c792012-09-05 13:44:32 +08003139 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003140 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003141 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003142 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003143 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003146 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3147 /* Enable hub to send remote wakeup for all ports. */
3148 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3149 status = set_port_feature(hdev,
3150 port1 |
3151 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3152 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3153 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3154 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3155 }
3156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Harvey Harrison441b62c2008-03-03 16:08:34 -08003158 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003159
David Brownellc9f89fa2005-09-13 19:57:04 -07003160 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003161 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
3164
3165static int hub_resume(struct usb_interface *intf)
3166{
Alan Stern5e6effa2008-03-03 15:15:51 -05003167 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
Alan Stern5e6effa2008-03-03 15:15:51 -05003169 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003170 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return 0;
3172}
3173
Alan Sternb41a60e2007-05-30 15:39:33 -04003174static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003175{
Alan Sternb41a60e2007-05-30 15:39:33 -04003176 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003177
Alan Stern5e6effa2008-03-03 15:15:51 -05003178 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003179 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003180 return 0;
3181}
3182
Alan Stern54515fe2007-05-30 15:38:58 -04003183/**
3184 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3185 * @rhdev: struct usb_device for the root hub
3186 *
3187 * The USB host controller driver calls this function when its root hub
3188 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003189 * has been reset. The routine marks @rhdev as having lost power.
3190 * When the hub driver is resumed it will take notice and carry out
3191 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3192 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003193 */
3194void usb_root_hub_lost_power(struct usb_device *rhdev)
3195{
3196 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3197 rhdev->reset_resume = 1;
3198}
3199EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3200
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003201static const char * const usb3_lpm_names[] = {
3202 "U0",
3203 "U1",
3204 "U2",
3205 "U3",
3206};
3207
3208/*
3209 * Send a Set SEL control transfer to the device, prior to enabling
3210 * device-initiated U1 or U2. This lets the device know the exit latencies from
3211 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3212 * packet from the host.
3213 *
3214 * This function will fail if the SEL or PEL values for udev are greater than
3215 * the maximum allowed values for the link state to be enabled.
3216 */
3217static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3218{
3219 struct usb_set_sel_req *sel_values;
3220 unsigned long long u1_sel;
3221 unsigned long long u1_pel;
3222 unsigned long long u2_sel;
3223 unsigned long long u2_pel;
3224 int ret;
3225
3226 /* Convert SEL and PEL stored in ns to us */
3227 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3228 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3229 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3230 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3231
3232 /*
3233 * Make sure that the calculated SEL and PEL values for the link
3234 * state we're enabling aren't bigger than the max SEL/PEL
3235 * value that will fit in the SET SEL control transfer.
3236 * Otherwise the device would get an incorrect idea of the exit
3237 * latency for the link state, and could start a device-initiated
3238 * U1/U2 when the exit latencies are too high.
3239 */
3240 if ((state == USB3_LPM_U1 &&
3241 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3242 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3243 (state == USB3_LPM_U2 &&
3244 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3245 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003246 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 -07003247 usb3_lpm_names[state], u1_sel, u1_pel);
3248 return -EINVAL;
3249 }
3250
3251 /*
3252 * If we're enabling device-initiated LPM for one link state,
3253 * but the other link state has a too high SEL or PEL value,
3254 * just set those values to the max in the Set SEL request.
3255 */
3256 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3257 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3258
3259 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3260 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3261
3262 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3263 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3264
3265 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3266 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3267
3268 /*
3269 * usb_enable_lpm() can be called as part of a failed device reset,
3270 * which may be initiated by an error path of a mass storage driver.
3271 * Therefore, use GFP_NOIO.
3272 */
3273 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3274 if (!sel_values)
3275 return -ENOMEM;
3276
3277 sel_values->u1_sel = u1_sel;
3278 sel_values->u1_pel = u1_pel;
3279 sel_values->u2_sel = cpu_to_le16(u2_sel);
3280 sel_values->u2_pel = cpu_to_le16(u2_pel);
3281
3282 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3283 USB_REQ_SET_SEL,
3284 USB_RECIP_DEVICE,
3285 0, 0,
3286 sel_values, sizeof *(sel_values),
3287 USB_CTRL_SET_TIMEOUT);
3288 kfree(sel_values);
3289 return ret;
3290}
3291
3292/*
3293 * Enable or disable device-initiated U1 or U2 transitions.
3294 */
3295static int usb_set_device_initiated_lpm(struct usb_device *udev,
3296 enum usb3_link_state state, bool enable)
3297{
3298 int ret;
3299 int feature;
3300
3301 switch (state) {
3302 case USB3_LPM_U1:
3303 feature = USB_DEVICE_U1_ENABLE;
3304 break;
3305 case USB3_LPM_U2:
3306 feature = USB_DEVICE_U2_ENABLE;
3307 break;
3308 default:
3309 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3310 __func__, enable ? "enable" : "disable");
3311 return -EINVAL;
3312 }
3313
3314 if (udev->state != USB_STATE_CONFIGURED) {
3315 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3316 "for unconfigured device.\n",
3317 __func__, enable ? "enable" : "disable",
3318 usb3_lpm_names[state]);
3319 return 0;
3320 }
3321
3322 if (enable) {
3323 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003324 * Now send the control transfer to enable device-initiated LPM
3325 * for either U1 or U2.
3326 */
3327 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3328 USB_REQ_SET_FEATURE,
3329 USB_RECIP_DEVICE,
3330 feature,
3331 0, NULL, 0,
3332 USB_CTRL_SET_TIMEOUT);
3333 } else {
3334 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3335 USB_REQ_CLEAR_FEATURE,
3336 USB_RECIP_DEVICE,
3337 feature,
3338 0, NULL, 0,
3339 USB_CTRL_SET_TIMEOUT);
3340 }
3341 if (ret < 0) {
3342 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3343 enable ? "Enable" : "Disable",
3344 usb3_lpm_names[state]);
3345 return -EBUSY;
3346 }
3347 return 0;
3348}
3349
3350static int usb_set_lpm_timeout(struct usb_device *udev,
3351 enum usb3_link_state state, int timeout)
3352{
3353 int ret;
3354 int feature;
3355
3356 switch (state) {
3357 case USB3_LPM_U1:
3358 feature = USB_PORT_FEAT_U1_TIMEOUT;
3359 break;
3360 case USB3_LPM_U2:
3361 feature = USB_PORT_FEAT_U2_TIMEOUT;
3362 break;
3363 default:
3364 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3365 __func__);
3366 return -EINVAL;
3367 }
3368
3369 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3370 timeout != USB3_LPM_DEVICE_INITIATED) {
3371 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3372 "which is a reserved value.\n",
3373 usb3_lpm_names[state], timeout);
3374 return -EINVAL;
3375 }
3376
3377 ret = set_port_feature(udev->parent,
3378 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3379 feature);
3380 if (ret < 0) {
3381 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3382 "error code %i\n", usb3_lpm_names[state],
3383 timeout, ret);
3384 return -EBUSY;
3385 }
3386 if (state == USB3_LPM_U1)
3387 udev->u1_params.timeout = timeout;
3388 else
3389 udev->u2_params.timeout = timeout;
3390 return 0;
3391}
3392
3393/*
3394 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3395 * U1/U2 entry.
3396 *
3397 * We will attempt to enable U1 or U2, but there are no guarantees that the
3398 * control transfers to set the hub timeout or enable device-initiated U1/U2
3399 * will be successful.
3400 *
3401 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3402 * driver know about it. If that call fails, it should be harmless, and just
3403 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3404 */
3405static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3406 enum usb3_link_state state)
3407{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003408 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003409 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3410 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3411
3412 /* If the device says it doesn't have *any* exit latency to come out of
3413 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3414 * state.
3415 */
3416 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3417 (state == USB3_LPM_U2 && u2_mel == 0))
3418 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003419
Sarah Sharp65a95b72012-10-05 10:32:07 -07003420 /*
3421 * First, let the device know about the exit latencies
3422 * associated with the link state we're about to enable.
3423 */
3424 ret = usb_req_set_sel(udev, state);
3425 if (ret < 0) {
3426 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3427 usb3_lpm_names[state]);
3428 return;
3429 }
3430
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003431 /* We allow the host controller to set the U1/U2 timeout internally
3432 * first, so that it can change its schedule to account for the
3433 * additional latency to send data to a device in a lower power
3434 * link state.
3435 */
3436 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3437
3438 /* xHCI host controller doesn't want to enable this LPM state. */
3439 if (timeout == 0)
3440 return;
3441
3442 if (timeout < 0) {
3443 dev_warn(&udev->dev, "Could not enable %s link state, "
3444 "xHCI error %i.\n", usb3_lpm_names[state],
3445 timeout);
3446 return;
3447 }
3448
3449 if (usb_set_lpm_timeout(udev, state, timeout))
3450 /* If we can't set the parent hub U1/U2 timeout,
3451 * device-initiated LPM won't be allowed either, so let the xHCI
3452 * host know that this link state won't be enabled.
3453 */
3454 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3455
3456 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3457 else if (udev->actconfig)
3458 usb_set_device_initiated_lpm(udev, state, true);
3459
3460}
3461
3462/*
3463 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3464 * U1/U2 entry.
3465 *
3466 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3467 * If zero is returned, the parent will not allow the link to go into U1/U2.
3468 *
3469 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3470 * it won't have an effect on the bus link state because the parent hub will
3471 * still disallow device-initiated U1/U2 entry.
3472 *
3473 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3474 * possible. The result will be slightly more bus bandwidth will be taken up
3475 * (to account for U1/U2 exit latency), but it should be harmless.
3476 */
3477static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3478 enum usb3_link_state state)
3479{
3480 int feature;
3481
3482 switch (state) {
3483 case USB3_LPM_U1:
3484 feature = USB_PORT_FEAT_U1_TIMEOUT;
3485 break;
3486 case USB3_LPM_U2:
3487 feature = USB_PORT_FEAT_U2_TIMEOUT;
3488 break;
3489 default:
3490 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3491 __func__);
3492 return -EINVAL;
3493 }
3494
3495 if (usb_set_lpm_timeout(udev, state, 0))
3496 return -EBUSY;
3497
3498 usb_set_device_initiated_lpm(udev, state, false);
3499
3500 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3501 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3502 "bus schedule bandwidth may be impacted.\n",
3503 usb3_lpm_names[state]);
3504 return 0;
3505}
3506
3507/*
3508 * Disable hub-initiated and device-initiated U1 and U2 entry.
3509 * Caller must own the bandwidth_mutex.
3510 *
3511 * This will call usb_enable_lpm() on failure, which will decrement
3512 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3513 */
3514int usb_disable_lpm(struct usb_device *udev)
3515{
3516 struct usb_hcd *hcd;
3517
3518 if (!udev || !udev->parent ||
3519 udev->speed != USB_SPEED_SUPER ||
3520 !udev->lpm_capable)
3521 return 0;
3522
3523 hcd = bus_to_hcd(udev->bus);
3524 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3525 return 0;
3526
3527 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003528 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003529 return 0;
3530
3531 /* If LPM is enabled, attempt to disable it. */
3532 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3533 goto enable_lpm;
3534 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3535 goto enable_lpm;
3536
3537 return 0;
3538
3539enable_lpm:
3540 usb_enable_lpm(udev);
3541 return -EBUSY;
3542}
3543EXPORT_SYMBOL_GPL(usb_disable_lpm);
3544
3545/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3546int usb_unlocked_disable_lpm(struct usb_device *udev)
3547{
3548 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3549 int ret;
3550
3551 if (!hcd)
3552 return -EINVAL;
3553
3554 mutex_lock(hcd->bandwidth_mutex);
3555 ret = usb_disable_lpm(udev);
3556 mutex_unlock(hcd->bandwidth_mutex);
3557
3558 return ret;
3559}
3560EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3561
3562/*
3563 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3564 * xHCI host policy may prevent U1 or U2 from being enabled.
3565 *
3566 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3567 * until the lpm_disable_count drops to zero. Caller must own the
3568 * bandwidth_mutex.
3569 */
3570void usb_enable_lpm(struct usb_device *udev)
3571{
3572 struct usb_hcd *hcd;
3573
3574 if (!udev || !udev->parent ||
3575 udev->speed != USB_SPEED_SUPER ||
3576 !udev->lpm_capable)
3577 return;
3578
3579 udev->lpm_disable_count--;
3580 hcd = bus_to_hcd(udev->bus);
3581 /* Double check that we can both enable and disable LPM.
3582 * Device must be configured to accept set feature U1/U2 timeout.
3583 */
3584 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3585 !hcd->driver->disable_usb3_lpm_timeout)
3586 return;
3587
3588 if (udev->lpm_disable_count > 0)
3589 return;
3590
3591 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3592 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3593}
3594EXPORT_SYMBOL_GPL(usb_enable_lpm);
3595
3596/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3597void usb_unlocked_enable_lpm(struct usb_device *udev)
3598{
3599 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3600
3601 if (!hcd)
3602 return;
3603
3604 mutex_lock(hcd->bandwidth_mutex);
3605 usb_enable_lpm(udev);
3606 mutex_unlock(hcd->bandwidth_mutex);
3607}
3608EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3609
3610
Alan Sternd388dab2006-07-01 22:14:24 -04003611#else /* CONFIG_PM */
3612
Alan Sternf07600c2007-05-30 15:38:16 -04003613#define hub_suspend NULL
3614#define hub_resume NULL
3615#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003616
3617int usb_disable_lpm(struct usb_device *udev)
3618{
3619 return 0;
3620}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003621EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003622
3623void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003624EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003625
3626int usb_unlocked_disable_lpm(struct usb_device *udev)
3627{
3628 return 0;
3629}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003630EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003631
3632void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003633EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003634
3635int usb_disable_ltm(struct usb_device *udev)
3636{
3637 return 0;
3638}
3639EXPORT_SYMBOL_GPL(usb_disable_ltm);
3640
3641void usb_enable_ltm(struct usb_device *udev) { }
3642EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003643#endif
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
3646/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3647 *
3648 * Between connect detection and reset signaling there must be a delay
3649 * of 100ms at least for debounce and power-settling. The corresponding
3650 * timer shall restart whenever the downstream port detects a disconnect.
3651 *
3652 * Apparently there are some bluetooth and irda-dongles and a number of
3653 * low-speed devices for which this debounce period may last over a second.
3654 * Not covered by the spec - but easy to deal with.
3655 *
3656 * This implementation uses a 1500ms total debounce timeout; if the
3657 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3658 * every 25ms for transient disconnects. When the port status has been
3659 * unchanged for 100ms it returns the port status.
3660 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661static int hub_port_debounce(struct usb_hub *hub, int port1)
3662{
3663 int ret;
3664 int total_time, stable_time = 0;
3665 u16 portchange, portstatus;
3666 unsigned connection = 0xffff;
3667
3668 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3669 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3670 if (ret < 0)
3671 return ret;
3672
3673 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3674 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3675 stable_time += HUB_DEBOUNCE_STEP;
3676 if (stable_time >= HUB_DEBOUNCE_STABLE)
3677 break;
3678 } else {
3679 stable_time = 0;
3680 connection = portstatus & USB_PORT_STAT_CONNECTION;
3681 }
3682
3683 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3684 clear_port_feature(hub->hdev, port1,
3685 USB_PORT_FEAT_C_CONNECTION);
3686 }
3687
3688 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3689 break;
3690 msleep(HUB_DEBOUNCE_STEP);
3691 }
3692
3693 dev_dbg (hub->intfdev,
3694 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3695 port1, total_time, stable_time, portstatus);
3696
3697 if (stable_time < HUB_DEBOUNCE_STABLE)
3698 return -ETIMEDOUT;
3699 return portstatus;
3700}
3701
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003702void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
Alan Sternddeac4e2009-01-15 17:03:33 -05003704 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3705 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003706 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003708EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3711#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3712
Alan Stern4326ed02007-07-30 17:08:43 -04003713static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
3715 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003716 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
Sarah Sharpc6515272009-04-27 19:57:26 -07003718 /*
3719 * The host controller will choose the device address,
3720 * instead of the core having chosen it earlier
3721 */
3722 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 return -EINVAL;
3724 if (udev->state == USB_STATE_ADDRESS)
3725 return 0;
3726 if (udev->state != USB_STATE_DEFAULT)
3727 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003728 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003729 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003730 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003731 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3732 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3733 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003735 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003736 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003738 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 }
3740 return retval;
3741}
3742
3743/* Reset device, (re)assign address, get device descriptor.
3744 * Device connection must be stable, no more debouncing needed.
3745 * Returns device in USB_STATE_ADDRESS, except on error.
3746 *
3747 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003748 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 * newly detected device that is not accessible through any global
3750 * pointers, it's not necessary to lock the device.
3751 */
3752static int
3753hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3754 int retry_counter)
3755{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003756 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
3758 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003759 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 int i, j, retval;
3761 unsigned delay = HUB_SHORT_RESET_TIME;
3762 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003763 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003764 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765
3766 /* root hub ports have a slightly longer reset period
3767 * (from USB 2.0 spec, section 7.1.7.5)
3768 */
3769 if (!hdev->parent) {
3770 delay = HUB_ROOT_RESET_TIME;
3771 if (port1 == hdev->bus->otg_port)
3772 hdev->bus->b_hnp_enable = 0;
3773 }
3774
3775 /* Some low speed devices have problems with the quick delay, so */
3776 /* be a bit pessimistic with those devices. RHbug #23670 */
3777 if (oldspeed == USB_SPEED_LOW)
3778 delay = HUB_LONG_RESET_TIME;
3779
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003780 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
Luben Tuikov07194ab2011-02-11 11:33:10 -08003782 /* Reset the device; full speed may morph to high speed */
3783 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003784 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003785 if (retval < 0) /* error or disconnect */
3786 goto fail;
3787 /* success, speed is known */
3788
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 retval = -ENODEV;
3790
3791 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3792 dev_dbg(&udev->dev, "device reset changed speed!\n");
3793 goto fail;
3794 }
3795 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003796
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3798 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003799 * For Wireless USB devices, ep0 max packet is always 512 (tho
3800 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 */
3802 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003803 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003804 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003805 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003806 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003808 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 break;
3810 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3811 /* to determine the ep0 maxpacket size, try to read
3812 * the device descriptor to get bMaxPacketSize0 and
3813 * then correct our initial guess.
3814 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003815 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 break;
3817 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003818 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 break;
3820 default:
3821 goto fail;
3822 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003823
3824 if (udev->speed == USB_SPEED_WIRELESS)
3825 speed = "variable speed Wireless";
3826 else
3827 speed = usb_speed_string(udev->speed);
3828
Sarah Sharpc6515272009-04-27 19:57:26 -07003829 if (udev->speed != USB_SPEED_SUPER)
3830 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003831 "%s %s USB device number %d using %s\n",
3832 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003833 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
3835 /* Set up TT records, if needed */
3836 if (hdev->tt) {
3837 udev->tt = hdev->tt;
3838 udev->ttport = hdev->ttport;
3839 } else if (udev->speed != USB_SPEED_HIGH
3840 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003841 if (!hub->tt.hub) {
3842 dev_err(&udev->dev, "parent hub has no TT\n");
3843 retval = -EINVAL;
3844 goto fail;
3845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 udev->tt = &hub->tt;
3847 udev->ttport = port1;
3848 }
3849
3850 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3851 * Because device hardware and firmware is sometimes buggy in
3852 * this area, and this is how Linux has done it for ages.
3853 * Change it cautiously.
3854 *
3855 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3856 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3857 * so it may help with some non-standards-compliant devices.
3858 * Otherwise we start with SET_ADDRESS and then try to read the
3859 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3860 * value.
3861 */
3862 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003863 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 struct usb_device_descriptor *buf;
3865 int r = 0;
3866
3867#define GET_DESCRIPTOR_BUFSIZE 64
3868 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3869 if (!buf) {
3870 retval = -ENOMEM;
3871 continue;
3872 }
3873
Alan Sternb89ee192007-05-11 10:19:04 -04003874 /* Retry on all errors; some devices are flakey.
3875 * 255 is for WUSB devices, we actually need to use
3876 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 */
3878 for (j = 0; j < 3; ++j) {
3879 buf->bMaxPacketSize0 = 0;
3880 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3881 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3882 USB_DT_DEVICE << 8, 0,
3883 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003884 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003886 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 if (buf->bDescriptorType ==
3888 USB_DT_DEVICE) {
3889 r = 0;
3890 break;
3891 }
3892 /* FALL THROUGH */
3893 default:
3894 if (r == 0)
3895 r = -EPROTO;
3896 break;
3897 }
3898 if (r == 0)
3899 break;
3900 }
3901 udev->descriptor.bMaxPacketSize0 =
3902 buf->bMaxPacketSize0;
3903 kfree(buf);
3904
Andiry Xu75d7cf72011-09-13 16:41:11 -07003905 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 if (retval < 0) /* error or disconnect */
3907 goto fail;
3908 if (oldspeed != udev->speed) {
3909 dev_dbg(&udev->dev,
3910 "device reset changed speed!\n");
3911 retval = -ENODEV;
3912 goto fail;
3913 }
3914 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003915 dev_err(&udev->dev,
3916 "device descriptor read/64, error %d\n",
3917 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 retval = -EMSGSIZE;
3919 continue;
3920 }
3921#undef GET_DESCRIPTOR_BUFSIZE
3922 }
3923
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003924 /*
3925 * If device is WUSB, we already assigned an
3926 * unauthorized address in the Connect Ack sequence;
3927 * authorization will assign the final address.
3928 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003929 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003930 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3931 retval = hub_set_address(udev, devnum);
3932 if (retval >= 0)
3933 break;
3934 msleep(200);
3935 }
3936 if (retval < 0) {
3937 dev_err(&udev->dev,
3938 "device not accepting address %d, error %d\n",
3939 devnum, retval);
3940 goto fail;
3941 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003942 if (udev->speed == USB_SPEED_SUPER) {
3943 devnum = udev->devnum;
3944 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003945 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003946 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003947 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003948 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003949
3950 /* cope with hardware quirkiness:
3951 * - let SET_ADDRESS settle, some device hardware wants it
3952 * - read ep0 maxpacket even for high and low speed,
3953 */
3954 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003955 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
3959 retval = usb_get_device_descriptor(udev, 8);
3960 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003961 dev_err(&udev->dev,
3962 "device descriptor read/8, error %d\n",
3963 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 if (retval >= 0)
3965 retval = -EMSGSIZE;
3966 } else {
3967 retval = 0;
3968 break;
3969 }
3970 }
3971 if (retval)
3972 goto fail;
3973
Elric Fud8aec3d2012-03-26 21:16:02 +08003974 /*
3975 * Some superspeed devices have finished the link training process
3976 * and attached to a superspeed hub port, but the device descriptor
3977 * got from those devices show they aren't superspeed devices. Warm
3978 * reset the port attached by the devices can fix them.
3979 */
3980 if ((udev->speed == USB_SPEED_SUPER) &&
3981 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3982 dev_err(&udev->dev, "got a wrong device descriptor, "
3983 "warm reset device\n");
3984 hub_port_reset(hub, port1, udev,
3985 HUB_BH_RESET_TIME, true);
3986 retval = -EINVAL;
3987 goto fail;
3988 }
3989
Sarah Sharp6b403b02009-04-27 19:54:10 -07003990 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3991 udev->speed == USB_SPEED_SUPER)
3992 i = 512;
3993 else
3994 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003995 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003996 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003998 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 retval = -EMSGSIZE;
4000 goto fail;
4001 }
Alan Stern56626a72010-10-14 15:25:21 -04004002 if (udev->speed == USB_SPEED_FULL)
4003 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4004 else
4005 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004007 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
4009
4010 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4011 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004012 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4013 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (retval >= 0)
4015 retval = -ENOMSG;
4016 goto fail;
4017 }
4018
Andiry Xu1ff4df52011-09-23 14:19:48 -07004019 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4020 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004021 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004022 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004023 usb_set_lpm_parameters(udev);
4024 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004025 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004026
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004028 /* notify HCD that we have a device connected and addressed */
4029 if (hcd->driver->update_device)
4030 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004032 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004034 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004035 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004036 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 return retval;
4038}
4039
4040static void
4041check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4042{
4043 struct usb_qualifier_descriptor *qual;
4044 int status;
4045
Christoph Lametere94b1762006-12-06 20:33:17 -08004046 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 if (qual == NULL)
4048 return;
4049
4050 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4051 qual, sizeof *qual);
4052 if (status == sizeof *qual) {
4053 dev_info(&udev->dev, "not running at top speed; "
4054 "connect to a high speed hub\n");
4055 /* hub LEDs are probably harder to miss than syslog */
4056 if (hub->has_indicators) {
4057 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004058 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 }
4060 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004061 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
4064static unsigned
4065hub_power_remaining (struct usb_hub *hub)
4066{
4067 struct usb_device *hdev = hub->hdev;
4068 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004069 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Alan Stern55c52712005-11-23 12:03:12 -05004071 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 return 0;
4073
Alan Stern55c52712005-11-23 12:03:12 -05004074 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4075 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c792012-09-05 13:44:32 +08004076 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004077 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
4079 if (!udev)
4080 continue;
4081
Alan Stern55c52712005-11-23 12:03:12 -05004082 /* Unconfigured devices may not use more than 100mA,
4083 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004085 delta = udev->actconfig->desc.bMaxPower * 2;
4086 else if (port1 != udev->bus->otg_port || hdev->parent)
4087 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 else
Alan Stern55c52712005-11-23 12:03:12 -05004089 delta = 8;
4090 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004091 dev_warn(&udev->dev,
4092 "%dmA is over %umA budget for port %d!\n",
4093 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 remaining -= delta;
4095 }
4096 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004097 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4098 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 remaining = 0;
4100 }
4101 return remaining;
4102}
4103
4104/* Handle physical or logical connection change events.
4105 * This routine is called when:
4106 * a port connection-change occurs;
4107 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004108 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 * a firmware download)
4110 * caller already locked the hub
4111 */
4112static void hub_port_connect_change(struct usb_hub *hub, int port1,
4113 u16 portstatus, u16 portchange)
4114{
4115 struct usb_device *hdev = hub->hdev;
4116 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304117 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004118 unsigned wHubCharacteristics =
4119 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004120 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 dev_dbg (hub_dev,
4124 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004125 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127 if (hub->has_indicators) {
4128 set_port_led(hub, port1, HUB_LED_AUTO);
4129 hub->indicator[port1-1] = INDICATOR_AUTO;
4130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
4132#ifdef CONFIG_USB_OTG
4133 /* during HNP, don't repeat the debounce */
4134 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004135 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4136 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137#endif
4138
Alan Stern8808f002008-04-28 11:06:55 -04004139 /* Try to resuscitate an existing device */
Lan Tianyuff823c792012-09-05 13:44:32 +08004140 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004141 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4142 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004143 usb_lock_device(udev);
4144 if (portstatus & USB_PORT_STAT_ENABLE) {
4145 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004146
4147#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004148 } else if (udev->state == USB_STATE_SUSPENDED &&
4149 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004150 /* For a suspended device, treat this as a
4151 * remote wakeup event.
4152 */
Alan Stern0534d462010-01-08 12:56:30 -05004153 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004154#endif
4155
4156 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004157 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004158 }
4159 usb_unlock_device(udev);
4160
4161 if (status == 0) {
4162 clear_bit(port1, hub->change_bits);
4163 return;
4164 }
4165 }
4166
Alan Stern24618b02008-04-28 11:06:28 -04004167 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004168 if (udev)
Lan Tianyuff823c792012-09-05 13:44:32 +08004169 usb_disconnect(&hub->ports[port1 - 1]->child);
Alan Stern24618b02008-04-28 11:06:28 -04004170 clear_bit(port1, hub->change_bits);
4171
Alan Stern253e0572009-10-27 15:20:13 -04004172 /* We can forget about a "removed" device when there's a physical
4173 * disconnect or the connect status changes.
4174 */
4175 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4176 (portchange & USB_PORT_STAT_C_CONNECTION))
4177 clear_bit(port1, hub->removed_bits);
4178
Alan Stern5257d972008-09-22 14:43:08 -04004179 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4180 USB_PORT_STAT_C_ENABLE)) {
4181 status = hub_port_debounce(hub, port1);
4182 if (status < 0) {
4183 if (printk_ratelimit())
4184 dev_err(hub_dev, "connect-debounce failed, "
4185 "port %d disabled\n", port1);
4186 portstatus &= ~USB_PORT_STAT_CONNECTION;
4187 } else {
4188 portstatus = status;
4189 }
4190 }
4191
Richard Zhao925aa462012-07-11 11:09:28 +08004192 if (hcd->phy && !hdev->parent) {
4193 if (portstatus & USB_PORT_STAT_CONNECTION)
4194 usb_phy_notify_connect(hcd->phy, port1);
4195 else
4196 usb_phy_notify_disconnect(hcd->phy, port1);
4197 }
4198
Alan Stern253e0572009-10-27 15:20:13 -04004199 /* Return now if debouncing failed or nothing is connected or
4200 * the device was "removed".
4201 */
4202 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4203 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004206 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004207 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 if (portstatus & USB_PORT_STAT_ENABLE)
4211 goto done;
4212 return;
4213 }
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
4217 /* reallocate for each attempt, since references
4218 * to the previous one can escape in various ways
4219 */
4220 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4221 if (!udev) {
4222 dev_err (hub_dev,
4223 "couldn't allocate port %d usb_device\n",
4224 port1);
4225 goto done;
4226 }
4227
4228 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004229 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004230 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004231 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004232
Sarah Sharp131dec32010-12-06 21:00:19 -08004233 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4234 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004235 udev->speed = USB_SPEED_SUPER;
4236 else
4237 udev->speed = USB_SPEED_UNKNOWN;
4238
Alan Stern3b29b682011-02-22 09:53:41 -05004239 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004240 if (udev->devnum <= 0) {
4241 status = -ENOTCONN; /* Don't retry */
4242 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004243 }
4244
Sarah Sharpe7b77172009-04-27 19:54:26 -07004245 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 status = hub_port_init(hub, udev, port1, i);
4247 if (status < 0)
4248 goto loop;
4249
Phil Dibowitz93362a82010-07-22 00:05:01 +02004250 usb_detect_quirks(udev);
4251 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4252 msleep(1000);
4253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 /* consecutive bus-powered hubs aren't reliable; they can
4255 * violate the voltage drop budget. if the new child has
4256 * a "powered" LED, users should notice we didn't enable it
4257 * (without reading syslog), even without per-port LEDs
4258 * on the parent.
4259 */
4260 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004261 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 u16 devstat;
4263
4264 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4265 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004266 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 dev_dbg(&udev->dev, "get status %d ?\n", status);
4268 goto loop_disable;
4269 }
Alan Stern55c52712005-11-23 12:03:12 -05004270 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4272 dev_err(&udev->dev,
4273 "can't connect bus-powered hub "
4274 "to this port\n");
4275 if (hub->has_indicators) {
4276 hub->indicator[port1-1] =
4277 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004278 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 }
4280 status = -ENOTCONN; /* Don't retry */
4281 goto loop_disable;
4282 }
4283 }
4284
4285 /* check for devices running slower than they could */
4286 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4287 && udev->speed == USB_SPEED_FULL
4288 && highspeed_hubs != 0)
4289 check_highspeed (hub, udev, port1);
4290
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004291 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 * udev becomes globally accessible, although presumably
4293 * no one will look at it until hdev is unlocked.
4294 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 status = 0;
4296
4297 /* We mustn't add new devices if the parent hub has
4298 * been disconnected; we would race with the
4299 * recursively_mark_NOTATTACHED() routine.
4300 */
4301 spin_lock_irq(&device_state_lock);
4302 if (hdev->state == USB_STATE_NOTATTACHED)
4303 status = -ENOTCONN;
4304 else
Lan Tianyuff823c792012-09-05 13:44:32 +08004305 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 spin_unlock_irq(&device_state_lock);
4307
4308 /* Run it through the hoops (find a driver, etc) */
4309 if (!status) {
4310 status = usb_new_device(udev);
4311 if (status) {
4312 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c792012-09-05 13:44:32 +08004313 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 spin_unlock_irq(&device_state_lock);
4315 }
4316 }
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 if (status)
4319 goto loop_disable;
4320
4321 status = hub_power_remaining(hub);
4322 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004323 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
4325 return;
4326
4327loop_disable:
4328 hub_port_disable(hub, port1, 1);
4329loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004330 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004331 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004332 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004334 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 break;
4336 }
Alan Stern3a311552008-05-20 16:58:29 -04004337 if (hub->hdev->parent ||
4338 !hcd->driver->port_handed_over ||
4339 !(hcd->driver->port_handed_over)(hcd, port1))
4340 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4341 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343done:
4344 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304345 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4346 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347}
4348
Sarah Sharp714b07b2012-01-24 13:53:18 -08004349/* Returns 1 if there was a remote wakeup and a connect status change. */
4350static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004351 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004352{
4353 struct usb_device *hdev;
4354 struct usb_device *udev;
4355 int connect_change = 0;
4356 int ret;
4357
4358 hdev = hub->hdev;
Lan Tianyuff823c792012-09-05 13:44:32 +08004359 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004360 if (!hub_is_superspeed(hdev)) {
4361 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4362 return 0;
4363 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4364 } else {
4365 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004366 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4367 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004368 return 0;
4369 }
4370
Sarah Sharp714b07b2012-01-24 13:53:18 -08004371 if (udev) {
4372 /* TRSMRCY = 10 msec */
4373 msleep(10);
4374
4375 usb_lock_device(udev);
4376 ret = usb_remote_wakeup(udev);
4377 usb_unlock_device(udev);
4378 if (ret < 0)
4379 connect_change = 1;
4380 } else {
4381 ret = -ENODEV;
4382 hub_port_disable(hub, port, 1);
4383 }
4384 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4385 port, ret);
4386 return connect_change;
4387}
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389static void hub_events(void)
4390{
4391 struct list_head *tmp;
4392 struct usb_device *hdev;
4393 struct usb_interface *intf;
4394 struct usb_hub *hub;
4395 struct device *hub_dev;
4396 u16 hubstatus;
4397 u16 hubchange;
4398 u16 portstatus;
4399 u16 portchange;
4400 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004401 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
4403 /*
4404 * We restart the list every time to avoid a deadlock with
4405 * deleting hubs downstream from this one. This should be
4406 * safe since we delete the hub from the event list.
4407 * Not the most efficient, but avoids deadlocks.
4408 */
4409 while (1) {
4410
4411 /* Grab the first entry at the beginning of the list */
4412 spin_lock_irq(&hub_event_lock);
4413 if (list_empty(&hub_event_list)) {
4414 spin_unlock_irq(&hub_event_lock);
4415 break;
4416 }
4417
4418 tmp = hub_event_list.next;
4419 list_del_init(tmp);
4420
4421 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004422 kref_get(&hub->kref);
4423 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Alan Sterne8054852007-05-04 11:55:11 -04004425 hdev = hub->hdev;
4426 hub_dev = hub->intfdev;
4427 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004428 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 hdev->state, hub->descriptor
4430 ? hub->descriptor->bNbrPorts
4431 : 0,
4432 /* NOTE: expects max 15 ports... */
4433 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004434 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 /* Lock the device, then check to see if we were
4437 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004438 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004439 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004440 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
4442 /* If the hub has died, clean up after it */
4443 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004444 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004445 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 goto loop;
4447 }
4448
Alan Stern40f122f2006-11-09 14:44:33 -05004449 /* Autoresume */
4450 ret = usb_autopm_get_interface(intf);
4451 if (ret) {
4452 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004454 }
4455
4456 /* If this is an inactive hub, do nothing */
4457 if (hub->quiescing)
4458 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
4460 if (hub->error) {
4461 dev_dbg (hub_dev, "resetting for error %d\n",
4462 hub->error);
4463
Ming Lei742120c2008-06-18 22:00:29 +08004464 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 if (ret) {
4466 dev_dbg (hub_dev,
4467 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004468 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 }
4470
4471 hub->nerrors = 0;
4472 hub->error = 0;
4473 }
4474
4475 /* deal with port status changes */
4476 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4477 if (test_bit(i, hub->busy_bits))
4478 continue;
4479 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004480 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004482 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 continue;
4484
4485 ret = hub_port_status(hub, i,
4486 &portstatus, &portchange);
4487 if (ret < 0)
4488 continue;
4489
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4491 clear_port_feature(hdev, i,
4492 USB_PORT_FEAT_C_CONNECTION);
4493 connect_change = 1;
4494 }
4495
4496 if (portchange & USB_PORT_STAT_C_ENABLE) {
4497 if (!connect_change)
4498 dev_dbg (hub_dev,
4499 "port %d enable change, "
4500 "status %08x\n",
4501 i, portstatus);
4502 clear_port_feature(hdev, i,
4503 USB_PORT_FEAT_C_ENABLE);
4504
4505 /*
4506 * EM interference sometimes causes badly
4507 * shielded USB devices to be shutdown by
4508 * the hub, this hack enables them again.
4509 * Works at least with mouse driver.
4510 */
4511 if (!(portstatus & USB_PORT_STAT_ENABLE)
4512 && !connect_change
Lan Tianyuff823c792012-09-05 13:44:32 +08004513 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 dev_err (hub_dev,
4515 "port %i "
4516 "disabled by hub (EMI?), "
4517 "re-enabling...\n",
4518 i);
4519 connect_change = 1;
4520 }
4521 }
4522
Sarah Sharp72937e12012-01-24 11:46:50 -08004523 if (hub_handle_remote_wakeup(hub, i,
4524 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004525 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004526
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004528 u16 status = 0;
4529 u16 unused;
4530
4531 dev_dbg(hub_dev, "over-current change on port "
4532 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 clear_port_feature(hdev, i,
4534 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004535 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004536 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004537 hub_port_status(hub, i, &status, &unused);
4538 if (status & USB_PORT_STAT_OVERCURRENT)
4539 dev_err(hub_dev, "over-current "
4540 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 }
4542
4543 if (portchange & USB_PORT_STAT_C_RESET) {
4544 dev_dbg (hub_dev,
4545 "reset change on port %d\n",
4546 i);
4547 clear_port_feature(hdev, i,
4548 USB_PORT_FEAT_C_RESET);
4549 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004550 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4551 hub_is_superspeed(hub->hdev)) {
4552 dev_dbg(hub_dev,
4553 "warm reset change on port %d\n",
4554 i);
4555 clear_port_feature(hdev, i,
4556 USB_PORT_FEAT_C_BH_PORT_RESET);
4557 }
John Youndbe79bb2001-09-17 00:00:00 -07004558 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4559 clear_port_feature(hub->hdev, i,
4560 USB_PORT_FEAT_C_PORT_LINK_STATE);
4561 }
4562 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4563 dev_warn(hub_dev,
4564 "config error on port %d\n",
4565 i);
4566 clear_port_feature(hub->hdev, i,
4567 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
Andiry Xu5e467f62011-04-27 18:07:54 +08004570 /* Warm reset a USB3 protocol port if it's in
4571 * SS.Inactive state.
4572 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004573 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004574 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004575 hub_port_reset(hub, i, NULL,
4576 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004577 }
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 if (connect_change)
4580 hub_port_connect_change(hub, i,
4581 portstatus, portchange);
4582 } /* end for i */
4583
4584 /* deal with hub status changes */
4585 if (test_and_clear_bit(0, hub->event_bits) == 0)
4586 ; /* do nothing */
4587 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4588 dev_err (hub_dev, "get_hub_status failed\n");
4589 else {
4590 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4591 dev_dbg (hub_dev, "power change\n");
4592 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004593 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4594 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004595 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004596 else
4597 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 }
4599 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004600 u16 status = 0;
4601 u16 unused;
4602
4603 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004605 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004606 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004607 hub_hub_status(hub, &status, &unused);
4608 if (status & HUB_STATUS_OVERCURRENT)
4609 dev_err(hub_dev, "over-current "
4610 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
4612 }
4613
Alan Stern8e4ceb32009-12-07 13:01:37 -05004614 loop_autopm:
4615 /* Balance the usb_autopm_get_interface() above */
4616 usb_autopm_put_interface_no_suspend(intf);
4617 loop:
4618 /* Balance the usb_autopm_get_interface_no_resume() in
4619 * kick_khubd() and allow autosuspend.
4620 */
4621 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004622 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004624 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625
4626 } /* end while (1) */
4627}
4628
4629static int hub_thread(void *__unused)
4630{
Alan Stern3bb1af52008-03-03 15:15:36 -05004631 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4632 * port handover. Otherwise it might see that a full-speed device
4633 * was gone before the EHCI controller had handed its port over to
4634 * the companion full-speed controller.
4635 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004636 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 do {
4639 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004640 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004641 !list_empty(&hub_event_list) ||
4642 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004643 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004645 pr_debug("%s: khubd exiting\n", usbcore_name);
4646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647}
4648
Németh Márton1e927d92010-01-10 15:34:53 +01004649static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4651 .bDeviceClass = USB_CLASS_HUB},
4652 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4653 .bInterfaceClass = USB_CLASS_HUB},
4654 { } /* Terminating entry */
4655};
4656
4657MODULE_DEVICE_TABLE (usb, hub_id_table);
4658
4659static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 .name = "hub",
4661 .probe = hub_probe,
4662 .disconnect = hub_disconnect,
4663 .suspend = hub_suspend,
4664 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004665 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004666 .pre_reset = hub_pre_reset,
4667 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004668 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004670 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671};
4672
4673int usb_hub_init(void)
4674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 if (usb_register(&hub_driver) < 0) {
4676 printk(KERN_ERR "%s: can't register hub driver\n",
4677 usbcore_name);
4678 return -1;
4679 }
4680
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004681 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4682 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
4685 /* Fall through if kernel_thread failed */
4686 usb_deregister(&hub_driver);
4687 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4688
4689 return -1;
4690}
4691
4692void usb_hub_cleanup(void)
4693{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004694 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
4696 /*
4697 * Hub resources are freed for us by usb_deregister. It calls
4698 * usb_driver_purge on every device which in turn calls that
4699 * devices disconnect function if it is using this driver.
4700 * The hub_disconnect function takes care of releasing the
4701 * individual hub resources. -greg
4702 */
4703 usb_deregister(&hub_driver);
4704} /* usb_hub_cleanup() */
4705
Alan Sterneb764c42008-03-03 15:16:04 -05004706static int descriptors_changed(struct usb_device *udev,
4707 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
Alan Sterneb764c42008-03-03 15:16:04 -05004709 int changed = 0;
4710 unsigned index;
4711 unsigned serial_len = 0;
4712 unsigned len;
4713 unsigned old_length;
4714 int length;
4715 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
Alan Sterneb764c42008-03-03 15:16:04 -05004717 if (memcmp(&udev->descriptor, old_device_descriptor,
4718 sizeof(*old_device_descriptor)) != 0)
4719 return 1;
4720
4721 /* Since the idVendor, idProduct, and bcdDevice values in the
4722 * device descriptor haven't changed, we will assume the
4723 * Manufacturer and Product strings haven't changed either.
4724 * But the SerialNumber string could be different (e.g., a
4725 * different flash card of the same brand).
4726 */
4727 if (udev->serial)
4728 serial_len = strlen(udev->serial) + 1;
4729
4730 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004732 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4733 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
Alan Sterneb764c42008-03-03 15:16:04 -05004735
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004736 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 if (buf == NULL) {
4738 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4739 /* assume the worst */
4740 return 1;
4741 }
4742 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004743 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4745 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004746 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 dev_dbg(&udev->dev, "config index %d, error %d\n",
4748 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004749 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 break;
4751 }
4752 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4753 != 0) {
4754 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004755 index,
4756 ((struct usb_config_descriptor *) buf)->
4757 bConfigurationValue);
4758 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 break;
4760 }
4761 }
Alan Sterneb764c42008-03-03 15:16:04 -05004762
4763 if (!changed && serial_len) {
4764 length = usb_string(udev, udev->descriptor.iSerialNumber,
4765 buf, serial_len);
4766 if (length + 1 != serial_len) {
4767 dev_dbg(&udev->dev, "serial string error %d\n",
4768 length);
4769 changed = 1;
4770 } else if (memcmp(buf, udev->serial, length) != 0) {
4771 dev_dbg(&udev->dev, "serial string changed\n");
4772 changed = 1;
4773 }
4774 }
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004777 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778}
4779
4780/**
Ming Lei742120c2008-06-18 22:00:29 +08004781 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4783 *
Alan Stern79efa092006-06-01 13:33:42 -04004784 * WARNING - don't use this routine to reset a composite device
4785 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004786 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 *
4788 * Do a port reset, reassign the device's address, and establish its
4789 * former operating configuration. If the reset fails, or the device's
4790 * descriptors change from their values before the reset, or the original
4791 * configuration and altsettings cannot be restored, a flag will be set
4792 * telling khubd to pretend the device has been disconnected and then
4793 * re-connected. All drivers will be unbound, and the device will be
4794 * re-enumerated and probed all over again.
4795 *
4796 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4797 * flagged for logical disconnection, or some other negative error code
4798 * if the reset wasn't even attempted.
4799 *
4800 * The caller must own the device lock. For example, it's safe to use
4801 * this from a driver probe() routine after downloading new firmware.
4802 * For calls that might not occur during probe(), drivers should lock
4803 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004804 *
4805 * Locking exception: This routine may also be called from within an
4806 * autoresume handler. Such usage won't conflict with other tasks
4807 * holding the device lock because these tasks should always call
4808 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 */
Ming Lei742120c2008-06-18 22:00:29 +08004810static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811{
4812 struct usb_device *parent_hdev = udev->parent;
4813 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004814 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004816 int i, ret = 0;
4817 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
4819 if (udev->state == USB_STATE_NOTATTACHED ||
4820 udev->state == USB_STATE_SUSPENDED) {
4821 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4822 udev->state);
4823 return -EINVAL;
4824 }
4825
4826 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004827 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004828 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 return -EISDIR;
4830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 parent_hub = hdev_to_hub(parent_hdev);
4832
Sarah Sharpf74631e2012-06-25 12:08:08 -07004833 /* Disable LPM and LTM while we reset the device and reinstall the alt
4834 * settings. Device-initiated LPM settings, and system exit latency
4835 * settings are cleared when the device is reset, so we have to set
4836 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004837 */
4838 ret = usb_unlocked_disable_lpm(udev);
4839 if (ret) {
4840 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4841 goto re_enumerate;
4842 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004843 ret = usb_disable_ltm(udev);
4844 if (ret) {
4845 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4846 __func__);
4847 goto re_enumerate;
4848 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 set_bit(port1, parent_hub->busy_bits);
4851 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4852
4853 /* ep0 maxpacket size may change; let the HCD know about it.
4854 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004855 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004857 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 break;
4859 }
4860 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 if (ret < 0)
4863 goto re_enumerate;
4864
4865 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004866 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 dev_info(&udev->dev, "device firmware changed\n");
4868 udev->descriptor = descriptor; /* for disconnect() calls */
4869 goto re_enumerate;
4870 }
Alan Stern4fe03872009-02-26 10:21:02 -05004871
4872 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 if (!udev->actconfig)
4874 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004875
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004876 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004877 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4878 if (ret < 0) {
4879 dev_warn(&udev->dev,
4880 "Busted HC? Not enough HCD resources for "
4881 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004882 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004883 goto re_enumerate;
4884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4886 USB_REQ_SET_CONFIGURATION, 0,
4887 udev->actconfig->desc.bConfigurationValue, 0,
4888 NULL, 0, USB_CTRL_SET_TIMEOUT);
4889 if (ret < 0) {
4890 dev_err(&udev->dev,
4891 "can't restore configuration #%d (error=%d)\n",
4892 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004893 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 goto re_enumerate;
4895 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004896 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4898
Alan Stern4fe03872009-02-26 10:21:02 -05004899 /* Put interfaces back into the same altsettings as before.
4900 * Don't bother to send the Set-Interface request for interfaces
4901 * that were already in altsetting 0; besides being unnecessary,
4902 * many devices can't handle it. Instead just reset the host-side
4903 * endpoint state.
4904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004906 struct usb_host_config *config = udev->actconfig;
4907 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 struct usb_interface_descriptor *desc;
4909
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004911 if (desc->bAlternateSetting == 0) {
4912 usb_disable_interface(udev, intf, true);
4913 usb_enable_interface(udev, intf, true);
4914 ret = 0;
4915 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004916 /* Let the bandwidth allocation function know that this
4917 * device has been reset, and it will have to use
4918 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004919 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004920 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004921 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4922 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004923 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 if (ret < 0) {
4926 dev_err(&udev->dev, "failed to restore interface %d "
4927 "altsetting %d (error=%d)\n",
4928 desc->bInterfaceNumber,
4929 desc->bAlternateSetting,
4930 ret);
4931 goto re_enumerate;
4932 }
4933 }
4934
4935done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07004936 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04004937 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004938 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 return 0;
4940
4941re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004942 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 hub_port_logical_disconnect(parent_hub, port1);
4944 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945}
4946
4947/**
4948 * usb_reset_device - warn interface drivers and perform a USB port reset
4949 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4950 *
Alan Stern79efa092006-06-01 13:33:42 -04004951 * Warns all drivers bound to registered interfaces (using their pre_reset
4952 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08004953 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04004954 *
Alan Stern79efa092006-06-01 13:33:42 -04004955 * Return value is the same as for usb_reset_and_verify_device().
4956 *
4957 * The caller must own the device lock. For example, it's safe to use
4958 * this from a driver probe() routine after downloading new firmware.
4959 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08004960 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04004961 *
4962 * If an interface is currently being probed or disconnected, we assume
4963 * its driver knows how to handle resets. For all other interfaces,
4964 * if the driver doesn't have pre_reset and post_reset methods then
4965 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004966 */
Ming Lei742120c2008-06-18 22:00:29 +08004967int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004968{
4969 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004970 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004971 struct usb_host_config *config = udev->actconfig;
4972
4973 if (udev->state == USB_STATE_NOTATTACHED ||
4974 udev->state == USB_STATE_SUSPENDED) {
4975 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4976 udev->state);
4977 return -EINVAL;
4978 }
4979
Alan Stern645daaa2006-08-30 15:47:02 -04004980 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004981 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004982
Alan Stern79efa092006-06-01 13:33:42 -04004983 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004984 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004985 struct usb_interface *cintf = config->interface[i];
4986 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004987 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004988
4989 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004990 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004991 if (drv->pre_reset && drv->post_reset)
4992 unbind = (drv->pre_reset)(cintf);
4993 else if (cintf->condition ==
4994 USB_INTERFACE_BOUND)
4995 unbind = 1;
4996 if (unbind)
4997 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004998 }
4999 }
5000 }
5001
Ming Lei742120c2008-06-18 22:00:29 +08005002 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005003
5004 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005005 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005006 struct usb_interface *cintf = config->interface[i];
5007 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005008 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005009
Alan Stern78d9a482008-06-23 16:00:40 -04005010 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005011 drv = to_usb_driver(cintf->dev.driver);
5012 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005013 rebind = (drv->post_reset)(cintf);
5014 else if (cintf->condition ==
5015 USB_INTERFACE_BOUND)
5016 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005017 }
Alan Stern6c640942008-10-21 15:40:03 -04005018 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005019 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005020 }
5021 }
5022
Alan Stern94fcda12006-11-20 11:38:46 -05005023 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005024 return ret;
5025}
Ming Lei742120c2008-06-18 22:00:29 +08005026EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005027
5028
5029/**
5030 * usb_queue_reset_device - Reset a USB device from an atomic context
5031 * @iface: USB interface belonging to the device to reset
5032 *
5033 * This function can be used to reset a USB device from an atomic
5034 * context, where usb_reset_device() won't work (as it blocks).
5035 *
5036 * Doing a reset via this method is functionally equivalent to calling
5037 * usb_reset_device(), except for the fact that it is delayed to a
5038 * workqueue. This means that any drivers bound to other interfaces
5039 * might be unbound, as well as users from usbfs in user space.
5040 *
5041 * Corner cases:
5042 *
5043 * - Scheduling two resets at the same time from two different drivers
5044 * attached to two different interfaces of the same device is
5045 * possible; depending on how the driver attached to each interface
5046 * handles ->pre_reset(), the second reset might happen or not.
5047 *
5048 * - If a driver is unbound and it had a pending reset, the reset will
5049 * be cancelled.
5050 *
5051 * - This function can be called during .probe() or .disconnect()
5052 * times. On return from .disconnect(), any pending resets will be
5053 * cancelled.
5054 *
5055 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5056 * does its own.
5057 *
5058 * NOTE: We don't do any reference count tracking because it is not
5059 * needed. The lifecycle of the work_struct is tied to the
5060 * usb_interface. Before destroying the interface we cancel the
5061 * work_struct, so the fact that work_struct is queued and or
5062 * running means the interface (and thus, the device) exist and
5063 * are referenced.
5064 */
5065void usb_queue_reset_device(struct usb_interface *iface)
5066{
5067 schedule_work(&iface->reset_ws);
5068}
5069EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c792012-09-05 13:44:32 +08005070
5071/**
5072 * usb_hub_find_child - Get the pointer of child device
5073 * attached to the port which is specified by @port1.
5074 * @hdev: USB device belonging to the usb hub
5075 * @port1: port num to indicate which port the child device
5076 * is attached to.
5077 *
5078 * USB drivers call this function to get hub's child device
5079 * pointer.
5080 *
5081 * Return NULL if input param is invalid and
5082 * child's usb_device pointer if non-NULL.
5083 */
5084struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5085 int port1)
5086{
5087 struct usb_hub *hub = hdev_to_hub(hdev);
5088
5089 if (port1 < 1 || port1 > hdev->maxchild)
5090 return NULL;
5091 return hub->ports[port1 - 1]->child;
5092}
5093EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005094
Lan Tianyu05f91682012-09-05 13:44:34 +08005095/**
5096 * usb_set_hub_port_connect_type - set hub port connect type.
5097 * @hdev: USB device belonging to the usb hub
5098 * @port1: port num of the port
5099 * @type: connect type of the port
5100 */
5101void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5102 enum usb_port_connect_type type)
5103{
5104 struct usb_hub *hub = hdev_to_hub(hdev);
5105
5106 hub->ports[port1 - 1]->connect_type = type;
5107}
5108
5109/**
5110 * usb_get_hub_port_connect_type - Get the port's connect type
5111 * @hdev: USB device belonging to the usb hub
5112 * @port1: port num of the port
5113 *
5114 * Return connect type of the port and if input params are
5115 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5116 */
5117enum usb_port_connect_type
5118usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5119{
5120 struct usb_hub *hub = hdev_to_hub(hdev);
5121
5122 return hub->ports[port1 - 1]->connect_type;
5123}
5124
Lan Tianyud5575422012-09-05 13:44:33 +08005125#ifdef CONFIG_ACPI
5126/**
5127 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5128 * @hdev: USB device belonging to the usb hub
5129 * @port1: port num of the port
5130 *
5131 * Return port's acpi handle if successful, NULL if params are
5132 * invaild.
5133 */
5134acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5135 int port1)
5136{
5137 struct usb_hub *hub = hdev_to_hub(hdev);
5138
5139 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5140}
5141#endif