blob: b5bd6bd8fd12ac41e382f97d0948955c36933dc9 [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>
Phil Dibowitz93362a82010-07-22 00:05:01 +020023#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070024#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010025#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/uaccess.h>
29#include <asm/byteorder.h>
30
31#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080033/* if we are in debug mode, always announce new devices */
34#ifdef DEBUG
35#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
36#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
37#endif
38#endif
39
Alan Stern1bb5f662006-11-06 11:56:13 -050040struct usb_hub {
41 struct device *intfdev; /* the "interface" device */
42 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040043 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050044 struct urb *urb; /* for interrupt polling pipe */
45
46 /* buffer for urb ... with extra space in case of babble */
47 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050048 union {
49 struct usb_hub_status hub;
50 struct usb_port_status port;
51 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050052 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050053
54 int error; /* last reported error */
55 int nerrors; /* track consecutive errors */
56
57 struct list_head event_list; /* hubs w/data or errs ready */
58 unsigned long event_bits[1]; /* status change bitmask */
59 unsigned long change_bits[1]; /* ports with logical connect
60 status change */
61 unsigned long busy_bits[1]; /* ports being reset or
62 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040063 unsigned long removed_bits[1]; /* ports with a "removed"
64 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080065 unsigned long wakeup_bits[1]; /* ports that have signaled
66 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050067#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
68#error event_bits[] is too short!
69#endif
70
71 struct usb_hub_descriptor *descriptor; /* class descriptor */
72 struct usb_tt tt; /* Transaction Translator */
73
74 unsigned mA_per_port; /* current for each child */
75
76 unsigned limited_power:1;
77 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040078 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050079
80 unsigned has_indicators:1;
81 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000082 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -040083 struct delayed_work init_work;
Lan Tianyu336c5c32012-07-06 14:13:52 +080084 struct dev_state **port_owners;
Alan Stern1bb5f662006-11-06 11:56:13 -050085};
86
John Youndbe79bb2001-09-17 00:00:00 -070087static inline int hub_is_superspeed(struct usb_device *hdev)
88{
Aman Deep7bf01182011-12-08 12:05:22 +053089 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070090}
Alan Stern1bb5f662006-11-06 11:56:13 -050091
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070092/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050093 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
95static DEFINE_SPINLOCK(device_state_lock);
96
97/* khubd's worklist and its lock */
98static DEFINE_SPINLOCK(hub_event_lock);
99static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
100
101/* Wakes up khubd */
102static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
103
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700104static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030107static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108module_param (blinkenlights, bool, S_IRUGO);
109MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
110
111/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200112 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
113 * 10 seconds to send reply for the initial 64-byte descriptor request.
114 */
115/* define initial 64-byte descriptor request timeout in milliseconds */
116static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
117module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800118MODULE_PARM_DESC(initial_descriptor_timeout,
119 "initial 64-byte descriptor request timeout in milliseconds "
120 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200121
122/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * As of 2.6.10 we introduce a new USB device initialization scheme which
124 * closely resembles the way Windows works. Hopefully it will be compatible
125 * with a wider range of devices than the old scheme. However some previously
126 * working devices may start giving rise to "device not accepting address"
127 * errors; if that happens the user can try the old scheme by adjusting the
128 * following module parameters.
129 *
130 * For maximum flexibility there are two boolean parameters to control the
131 * hub driver's behavior. On the first initialization attempt, if the
132 * "old_scheme_first" parameter is set then the old scheme will be used,
133 * otherwise the new scheme is used. If that fails and "use_both_schemes"
134 * is set, then the driver will make another attempt, using the other scheme.
135 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030136static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
138MODULE_PARM_DESC(old_scheme_first,
139 "start with the old device initialization scheme");
140
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030141static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
143MODULE_PARM_DESC(use_both_schemes,
144 "try the other device initialization scheme if the "
145 "first one fails");
146
Alan Stern32fe0192007-10-10 16:27:07 -0400147/* Mutual exclusion for EHCI CF initialization. This interferes with
148 * port reset on some companion controllers.
149 */
150DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
151EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
152
Alan Stern948fea32008-04-28 11:07:07 -0400153#define HUB_DEBOUNCE_TIMEOUT 1500
154#define HUB_DEBOUNCE_STEP 25
155#define HUB_DEBOUNCE_STABLE 100
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Ming Lei742120c2008-06-18 22:00:29 +0800158static int usb_reset_and_verify_device(struct usb_device *udev);
159
Sarah Sharp131dec32010-12-06 21:00:19 -0800160static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Sarah Sharp131dec32010-12-06 21:00:19 -0800162 if (hub_is_superspeed(hub->hdev))
163 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500164 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500166 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return "1.5 Mb/s";
168 else
169 return "12 Mb/s";
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400173static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700175 if (!hdev || !hdev->actconfig)
Alan Stern251180842009-07-22 14:41:18 -0400176 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return usb_get_intfdata(hdev->actconfig->interface[0]);
178}
179
Sarah Sharpd9b20992012-02-20 08:31:26 -0800180static int usb_device_supports_lpm(struct usb_device *udev)
181{
182 /* USB 2.1 (and greater) devices indicate LPM support through
183 * their USB 2.0 Extended Capabilities BOS descriptor.
184 */
185 if (udev->speed == USB_SPEED_HIGH) {
186 if (udev->bos->ext_cap &&
187 (USB_LPM_SUPPORT &
188 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
189 return 1;
190 return 0;
191 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800192
193 /* All USB 3.0 must support LPM, but we need their max exit latency
194 * information from the SuperSpeed Extended Capabilities BOS descriptor.
195 */
196 if (!udev->bos->ss_cap) {
197 dev_warn(&udev->dev, "No LPM exit latency info found. "
198 "Power management will be impacted.\n");
199 return 0;
200 }
201 if (udev->parent->lpm_capable)
202 return 1;
203
204 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
205 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800206 return 0;
207}
208
Sarah Sharp51e0a012012-02-20 12:02:19 -0800209/*
210 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
211 * either U1 or U2.
212 */
213static void usb_set_lpm_mel(struct usb_device *udev,
214 struct usb3_lpm_parameters *udev_lpm_params,
215 unsigned int udev_exit_latency,
216 struct usb_hub *hub,
217 struct usb3_lpm_parameters *hub_lpm_params,
218 unsigned int hub_exit_latency)
219{
220 unsigned int total_mel;
221 unsigned int device_mel;
222 unsigned int hub_mel;
223
224 /*
225 * Calculate the time it takes to transition all links from the roothub
226 * to the parent hub into U0. The parent hub must then decode the
227 * packet (hub header decode latency) to figure out which port it was
228 * bound for.
229 *
230 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
231 * means 0.1us). Multiply that by 100 to get nanoseconds.
232 */
233 total_mel = hub_lpm_params->mel +
234 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
235
236 /*
237 * How long will it take to transition the downstream hub's port into
238 * U0? The greater of either the hub exit latency or the device exit
239 * latency.
240 *
241 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
242 * Multiply that by 1000 to get nanoseconds.
243 */
244 device_mel = udev_exit_latency * 1000;
245 hub_mel = hub_exit_latency * 1000;
246 if (device_mel > hub_mel)
247 total_mel += device_mel;
248 else
249 total_mel += hub_mel;
250
251 udev_lpm_params->mel = total_mel;
252}
253
254/*
255 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
256 * a transition from either U1 or U2.
257 */
258static void usb_set_lpm_pel(struct usb_device *udev,
259 struct usb3_lpm_parameters *udev_lpm_params,
260 unsigned int udev_exit_latency,
261 struct usb_hub *hub,
262 struct usb3_lpm_parameters *hub_lpm_params,
263 unsigned int hub_exit_latency,
264 unsigned int port_to_port_exit_latency)
265{
266 unsigned int first_link_pel;
267 unsigned int hub_pel;
268
269 /*
270 * First, the device sends an LFPS to transition the link between the
271 * device and the parent hub into U0. The exit latency is the bigger of
272 * the device exit latency or the hub exit latency.
273 */
274 if (udev_exit_latency > hub_exit_latency)
275 first_link_pel = udev_exit_latency * 1000;
276 else
277 first_link_pel = hub_exit_latency * 1000;
278
279 /*
280 * When the hub starts to receive the LFPS, there is a slight delay for
281 * it to figure out that one of the ports is sending an LFPS. Then it
282 * will forward the LFPS to its upstream link. The exit latency is the
283 * delay, plus the PEL that we calculated for this hub.
284 */
285 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
286
287 /*
288 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
289 * is the greater of the two exit latencies.
290 */
291 if (first_link_pel > hub_pel)
292 udev_lpm_params->pel = first_link_pel;
293 else
294 udev_lpm_params->pel = hub_pel;
295}
296
297/*
298 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
299 * when a device initiates a transition to U0, until when it will receive the
300 * first packet from the host controller.
301 *
302 * Section C.1.5.1 describes the four components to this:
303 * - t1: device PEL
304 * - t2: time for the ERDY to make it from the device to the host.
305 * - t3: a host-specific delay to process the ERDY.
306 * - t4: time for the packet to make it from the host to the device.
307 *
308 * t3 is specific to both the xHCI host and the platform the host is integrated
309 * into. The Intel HW folks have said it's negligible, FIXME if a different
310 * vendor says otherwise.
311 */
312static void usb_set_lpm_sel(struct usb_device *udev,
313 struct usb3_lpm_parameters *udev_lpm_params)
314{
315 struct usb_device *parent;
316 unsigned int num_hubs;
317 unsigned int total_sel;
318
319 /* t1 = device PEL */
320 total_sel = udev_lpm_params->pel;
321 /* How many external hubs are in between the device & the root port. */
322 for (parent = udev->parent, num_hubs = 0; parent->parent;
323 parent = parent->parent)
324 num_hubs++;
325 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
326 if (num_hubs > 0)
327 total_sel += 2100 + 250 * (num_hubs - 1);
328
329 /* t4 = 250ns * num_hubs */
330 total_sel += 250 * num_hubs;
331
332 udev_lpm_params->sel = total_sel;
333}
334
335static void usb_set_lpm_parameters(struct usb_device *udev)
336{
337 struct usb_hub *hub;
338 unsigned int port_to_port_delay;
339 unsigned int udev_u1_del;
340 unsigned int udev_u2_del;
341 unsigned int hub_u1_del;
342 unsigned int hub_u2_del;
343
344 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
345 return;
346
347 hub = hdev_to_hub(udev->parent);
348 /* It doesn't take time to transition the roothub into U0, since it
349 * doesn't have an upstream link.
350 */
351 if (!hub)
352 return;
353
354 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
355 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
356 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
357 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
358
359 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
360 hub, &udev->parent->u1_params, hub_u1_del);
361
362 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
363 hub, &udev->parent->u2_params, hub_u2_del);
364
365 /*
366 * Appendix C, section C.2.2.2, says that there is a slight delay from
367 * when the parent hub notices the downstream port is trying to
368 * transition to U0 to when the hub initiates a U0 transition on its
369 * upstream port. The section says the delays are tPort2PortU1EL and
370 * tPort2PortU2EL, but it doesn't define what they are.
371 *
372 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
373 * about the same delays. Use the maximum delay calculations from those
374 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
375 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
376 * assume the device exit latencies they are talking about are the hub
377 * exit latencies.
378 *
379 * What do we do if the U2 exit latency is less than the U1 exit
380 * latency? It's possible, although not likely...
381 */
382 port_to_port_delay = 1;
383
384 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
385 hub, &udev->parent->u1_params, hub_u1_del,
386 port_to_port_delay);
387
388 if (hub_u2_del > hub_u1_del)
389 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
390 else
391 port_to_port_delay = 1 + hub_u1_del;
392
393 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
394 hub, &udev->parent->u2_params, hub_u2_del,
395 port_to_port_delay);
396
397 /* Now that we've got PEL, calculate SEL. */
398 usb_set_lpm_sel(udev, &udev->u1_params);
399 usb_set_lpm_sel(udev, &udev->u2_params);
400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700403static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
John Youndbe79bb2001-09-17 00:00:00 -0700405 int i, ret, size;
406 unsigned dtype;
407
408 if (hub_is_superspeed(hdev)) {
409 dtype = USB_DT_SS_HUB;
410 size = USB_DT_SS_HUB_SIZE;
411 } else {
412 dtype = USB_DT_HUB;
413 size = sizeof(struct usb_hub_descriptor);
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 for (i = 0; i < 3; i++) {
417 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
418 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700419 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 USB_CTRL_GET_TIMEOUT);
421 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
422 return ret;
423 }
424 return -EINVAL;
425}
426
427/*
428 * USB 2.0 spec Section 11.24.2.1
429 */
430static int clear_hub_feature(struct usb_device *hdev, int feature)
431{
432 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
433 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
434}
435
436/*
437 * USB 2.0 spec Section 11.24.2.2
438 */
439static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
440{
441 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
442 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
443 NULL, 0, 1000);
444}
445
446/*
447 * USB 2.0 spec Section 11.24.2.13
448 */
449static int set_port_feature(struct usb_device *hdev, int port1, int feature)
450{
451 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
452 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
453 NULL, 0, 1000);
454}
455
456/*
457 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
458 * for info about using port indicators
459 */
460static void set_port_led(
461 struct usb_hub *hub,
462 int port1,
463 int selector
464)
465{
466 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
467 USB_PORT_FEAT_INDICATOR);
468 if (status < 0)
469 dev_dbg (hub->intfdev,
470 "port %d indicator %s status %d\n",
471 port1,
472 ({ char *s; switch (selector) {
473 case HUB_LED_AMBER: s = "amber"; break;
474 case HUB_LED_GREEN: s = "green"; break;
475 case HUB_LED_OFF: s = "off"; break;
476 case HUB_LED_AUTO: s = "auto"; break;
477 default: s = "??"; break;
478 }; s; }),
479 status);
480}
481
482#define LED_CYCLE_PERIOD ((2*HZ)/3)
483
David Howellsc4028952006-11-22 14:57:56 +0000484static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
David Howellsc4028952006-11-22 14:57:56 +0000486 struct usb_hub *hub =
487 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 struct usb_device *hdev = hub->hdev;
489 unsigned i;
490 unsigned changed = 0;
491 int cursor = -1;
492
493 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
494 return;
495
496 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
497 unsigned selector, mode;
498
499 /* 30%-50% duty cycle */
500
501 switch (hub->indicator[i]) {
502 /* cycle marker */
503 case INDICATOR_CYCLE:
504 cursor = i;
505 selector = HUB_LED_AUTO;
506 mode = INDICATOR_AUTO;
507 break;
508 /* blinking green = sw attention */
509 case INDICATOR_GREEN_BLINK:
510 selector = HUB_LED_GREEN;
511 mode = INDICATOR_GREEN_BLINK_OFF;
512 break;
513 case INDICATOR_GREEN_BLINK_OFF:
514 selector = HUB_LED_OFF;
515 mode = INDICATOR_GREEN_BLINK;
516 break;
517 /* blinking amber = hw attention */
518 case INDICATOR_AMBER_BLINK:
519 selector = HUB_LED_AMBER;
520 mode = INDICATOR_AMBER_BLINK_OFF;
521 break;
522 case INDICATOR_AMBER_BLINK_OFF:
523 selector = HUB_LED_OFF;
524 mode = INDICATOR_AMBER_BLINK;
525 break;
526 /* blink green/amber = reserved */
527 case INDICATOR_ALT_BLINK:
528 selector = HUB_LED_GREEN;
529 mode = INDICATOR_ALT_BLINK_OFF;
530 break;
531 case INDICATOR_ALT_BLINK_OFF:
532 selector = HUB_LED_AMBER;
533 mode = INDICATOR_ALT_BLINK;
534 break;
535 default:
536 continue;
537 }
538 if (selector != HUB_LED_AUTO)
539 changed = 1;
540 set_port_led(hub, i + 1, selector);
541 hub->indicator[i] = mode;
542 }
543 if (!changed && blinkenlights) {
544 cursor++;
545 cursor %= hub->descriptor->bNbrPorts;
546 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
547 hub->indicator[cursor] = INDICATOR_CYCLE;
548 changed++;
549 }
550 if (changed)
551 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
552}
553
554/* use a short timeout for hub/port status fetches */
555#define USB_STS_TIMEOUT 1000
556#define USB_STS_RETRIES 5
557
558/*
559 * USB 2.0 spec Section 11.24.2.6
560 */
561static int get_hub_status(struct usb_device *hdev,
562 struct usb_hub_status *data)
563{
564 int i, status = -ETIMEDOUT;
565
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200566 for (i = 0; i < USB_STS_RETRIES &&
567 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
569 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
570 data, sizeof(*data), USB_STS_TIMEOUT);
571 }
572 return status;
573}
574
575/*
576 * USB 2.0 spec Section 11.24.2.7
577 */
578static int get_port_status(struct usb_device *hdev, int port1,
579 struct usb_port_status *data)
580{
581 int i, status = -ETIMEDOUT;
582
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200583 for (i = 0; i < USB_STS_RETRIES &&
584 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
586 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
587 data, sizeof(*data), USB_STS_TIMEOUT);
588 }
589 return status;
590}
591
Alan Stern3eb14912008-03-03 15:15:43 -0500592static int hub_port_status(struct usb_hub *hub, int port1,
593 u16 *status, u16 *change)
594{
595 int ret;
596
597 mutex_lock(&hub->status_mutex);
598 ret = get_port_status(hub->hdev, port1, &hub->status->port);
599 if (ret < 4) {
600 dev_err(hub->intfdev,
601 "%s failed (err = %d)\n", __func__, ret);
602 if (ret >= 0)
603 ret = -EIO;
604 } else {
605 *status = le16_to_cpu(hub->status->port.wPortStatus);
606 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700607
Alan Stern3eb14912008-03-03 15:15:43 -0500608 ret = 0;
609 }
610 mutex_unlock(&hub->status_mutex);
611 return ret;
612}
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614static void kick_khubd(struct usb_hub *hub)
615{
616 unsigned long flags;
617
618 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200619 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500621
622 /* Suppress autosuspend until khubd runs */
623 usb_autopm_get_interface_no_resume(
624 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 wake_up(&khubd_wait);
626 }
627 spin_unlock_irqrestore(&hub_event_lock, flags);
628}
629
630void usb_kick_khubd(struct usb_device *hdev)
631{
Alan Stern251180842009-07-22 14:41:18 -0400632 struct usb_hub *hub = hdev_to_hub(hdev);
633
634 if (hub)
635 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800638/*
639 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
640 * Notification, which indicates it had initiated remote wakeup.
641 *
642 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
643 * device initiates resume, so the USB core will not receive notice of the
644 * resume through the normal hub interrupt URB.
645 */
646void usb_wakeup_notification(struct usb_device *hdev,
647 unsigned int portnum)
648{
649 struct usb_hub *hub;
650
651 if (!hdev)
652 return;
653
654 hub = hdev_to_hub(hdev);
655 if (hub) {
656 set_bit(portnum, hub->wakeup_bits);
657 kick_khubd(hub);
658 }
659}
660EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100663static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200665 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400666 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100667 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 unsigned long bits;
669
Alan Sterne0152682007-08-24 15:42:52 -0400670 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 case -ENOENT: /* synchronous unlink */
672 case -ECONNRESET: /* async unlink */
673 case -ESHUTDOWN: /* hardware going away */
674 return;
675
676 default: /* presumably an error */
677 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400678 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if ((++hub->nerrors < 10) || hub->error)
680 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400681 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* let khubd handle things */
685 case 0: /* we got data: port status changed */
686 bits = 0;
687 for (i = 0; i < urb->actual_length; ++i)
688 bits |= ((unsigned long) ((*hub->buffer)[i]))
689 << (i*8);
690 hub->event_bits[0] = bits;
691 break;
692 }
693
694 hub->nerrors = 0;
695
696 /* Something happened, let khubd figure it out */
697 kick_khubd(hub);
698
699resubmit:
700 if (hub->quiescing)
701 return;
702
703 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
704 && status != -ENODEV && status != -EPERM)
705 dev_err (hub->intfdev, "resubmit --> %d\n", status);
706}
707
708/* USB 2.0 spec Section 11.24.2.3 */
709static inline int
710hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
711{
Alan Sternc2f65952009-11-18 11:37:15 -0500712 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
714 tt, NULL, 0, 1000);
715}
716
717/*
718 * enumeration blocks khubd for a long time. we use keventd instead, since
719 * long blocking there is the exception, not the rule. accordingly, HCDs
720 * talking to TTs must queue control transfers (not just bulk and iso), so
721 * both can talk to the same hub concurrently.
722 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400723static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
David Howellsc4028952006-11-22 14:57:56 +0000725 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400726 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400728 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 spin_lock_irqsave (&hub->tt.lock, flags);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400731 while (--limit && !list_empty (&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400732 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct usb_tt_clear *clear;
734 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400735 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 int status;
737
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400738 next = hub->tt.clear_list.next;
739 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 list_del (&clear->clear_list);
741
742 /* drop lock so HCD can concurrently report other TT errors */
743 spin_unlock_irqrestore (&hub->tt.lock, flags);
744 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (status)
746 dev_err (&hdev->dev,
747 "clear tt %d (%04x) error %d\n",
748 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400749
750 /* Tell the HCD, even if the operation failed */
751 drv = clear->hcd->driver;
752 if (drv->clear_tt_buffer_complete)
753 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
754
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700755 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400756 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 spin_unlock_irqrestore (&hub->tt.lock, flags);
759}
760
761/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400762 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
763 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *
765 * High speed HCDs use this to tell the hub driver that some split control or
766 * bulk transaction failed in a way that requires clearing internal state of
767 * a transaction translator. This is normally detected (and reported) from
768 * interrupt context.
769 *
770 * It may not be possible for that hub to handle additional full (or low)
771 * speed transactions until that state is fully cleared out.
772 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400773int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400775 struct usb_device *udev = urb->dev;
776 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct usb_tt *tt = udev->tt;
778 unsigned long flags;
779 struct usb_tt_clear *clear;
780
781 /* we've got to cope with an arbitrary number of pending TT clears,
782 * since each TT has "at least two" buffers that can need it (and
783 * there can be many TTs per hub). even if they're uncommon.
784 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800785 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
787 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400788 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
790
791 /* info that CLEAR_TT_BUFFER needs */
792 clear->tt = tt->multi ? udev->ttport : 1;
793 clear->devinfo = usb_pipeendpoint (pipe);
794 clear->devinfo |= udev->devnum << 4;
795 clear->devinfo |= usb_pipecontrol (pipe)
796 ? (USB_ENDPOINT_XFER_CONTROL << 11)
797 : (USB_ENDPOINT_XFER_BULK << 11);
798 if (usb_pipein (pipe))
799 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400800
801 /* info for completion callback */
802 clear->hcd = bus_to_hcd(udev->bus);
803 clear->ep = urb->ep;
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* tell keventd to clear state for this TT */
806 spin_lock_irqsave (&tt->lock, flags);
807 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400808 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400810 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400812EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Alan Stern8520f382008-09-22 14:44:26 -0400814/* If do_delay is false, return the number of milliseconds the caller
815 * needs to delay.
816 */
817static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700820 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400821 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400822 u16 wHubCharacteristics =
823 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Alan Stern4489a572006-04-27 15:54:22 -0400825 /* Enable power on each port. Some hubs have reserved values
826 * of LPSM (> 2) in their descriptors, even though they are
827 * USB 2.0 hubs. Some hubs do not implement port-power switching
828 * but only emulate it. In all cases, the ports won't work
829 * unless we send these messages to the hub.
830 */
831 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400833 else
834 dev_dbg(hub->intfdev, "trying to enable port power on "
835 "non-switchable hub\n");
836 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
837 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
David Brownellb7896962005-08-31 10:41:44 -0700839 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400840 delay = max(pgood_delay, (unsigned) 100);
841 if (do_delay)
842 msleep(delay);
843 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846static int hub_hub_status(struct usb_hub *hub,
847 u16 *status, u16 *change)
848{
849 int ret;
850
Alan Sterndb90e7a2007-02-05 09:56:15 -0500851 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ret = get_hub_status(hub->hdev, &hub->status->hub);
853 if (ret < 0)
854 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800855 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 else {
857 *status = le16_to_cpu(hub->status->hub.wHubStatus);
858 *change = le16_to_cpu(hub->status->hub.wHubChange);
859 ret = 0;
860 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500861 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return ret;
863}
864
Alan Stern8b28c752005-08-10 17:04:13 -0400865static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
866{
867 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400868 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400869
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700870 if (hdev->children[port1-1] && set_state)
871 usb_set_device_state(hdev->children[port1-1],
Alan Stern8b28c752005-08-10 17:04:13 -0400872 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700873 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400874 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400875 if (ret)
876 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400877 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400878 return ret;
879}
880
Alan Stern0458d5b2007-05-04 11:52:20 -0400881/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800882 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400883 * time later khubd will disconnect() any existing usb_device on the port
884 * and will re-enumerate if there actually is a device attached.
885 */
886static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
887{
888 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
889 hub_port_disable(hub, port1, 1);
890
891 /* FIXME let caller ask to power down the port:
892 * - some devices won't enumerate without a VBUS power cycle
893 * - SRP saves power that way
894 * - ... new call, TBD ...
895 * That's easy if this hub can switch power per-port, and
896 * khubd reactivates the port later (timer, SRP, etc).
897 * Powerdown must be optional, because of reset/DFU.
898 */
899
900 set_bit(port1, hub->change_bits);
901 kick_khubd(hub);
902}
903
Alan Stern253e0572009-10-27 15:20:13 -0400904/**
905 * usb_remove_device - disable a device's port on its parent hub
906 * @udev: device to be disabled and removed
907 * Context: @udev locked, must be able to sleep.
908 *
909 * After @udev's port has been disabled, khubd is notified and it will
910 * see that the device has been disconnected. When the device is
911 * physically unplugged and something is plugged in, the events will
912 * be received and processed normally.
913 */
914int usb_remove_device(struct usb_device *udev)
915{
916 struct usb_hub *hub;
917 struct usb_interface *intf;
918
919 if (!udev->parent) /* Can't remove a root hub */
920 return -EINVAL;
921 hub = hdev_to_hub(udev->parent);
922 intf = to_usb_interface(hub->intfdev);
923
924 usb_autopm_get_interface(intf);
925 set_bit(udev->portnum, hub->removed_bits);
926 hub_port_logical_disconnect(hub, udev->portnum);
927 usb_autopm_put_interface(intf);
928 return 0;
929}
930
Alan Stern6ee0b272008-04-28 11:06:42 -0400931enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500932 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400933 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400934};
Alan Stern5e6effa2008-03-03 15:15:51 -0500935
Alan Stern8520f382008-09-22 14:44:26 -0400936static void hub_init_func2(struct work_struct *ws);
937static void hub_init_func3(struct work_struct *ws);
938
Alan Sternf2835212008-04-28 11:07:17 -0400939static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500940{
941 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800942 struct usb_hcd *hcd;
943 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500944 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400945 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400946 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400947 unsigned delay;
948
949 /* Continue a partial initialization */
950 if (type == HUB_INIT2)
951 goto init2;
952 if (type == HUB_INIT3)
953 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500954
Elric Fua45aa3b2012-02-18 13:32:27 +0800955 /* The superspeed hub except for root hub has to use Hub Depth
956 * value as an offset into the route string to locate the bits
957 * it uses to determine the downstream port number. So hub driver
958 * should send a set hub depth request to superspeed hub after
959 * the superspeed hub is set configuration in initialization or
960 * reset procedure.
961 *
962 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400963 * For any other type of activation, turn it on.
964 */
Alan Stern8520f382008-09-22 14:44:26 -0400965 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800966 if (hdev->parent && hub_is_superspeed(hdev)) {
967 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
968 HUB_SET_DEPTH, USB_RT_HUB,
969 hdev->level - 1, 0, NULL, 0,
970 USB_CTRL_SET_TIMEOUT);
971 if (ret < 0)
972 dev_err(hub->intfdev,
973 "set hub depth failed\n");
974 }
Alan Stern8520f382008-09-22 14:44:26 -0400975
976 /* Speed up system boot by using a delayed_work for the
977 * hub's initial power-up delays. This is pretty awkward
978 * and the implementation looks like a home-brewed sort of
979 * setjmp/longjmp, but it saves at least 100 ms for each
980 * root hub (assuming usbcore is compiled into the kernel
981 * rather than as a module). It adds up.
982 *
983 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
984 * because for those activation types the ports have to be
985 * operational when we return. In theory this could be done
986 * for HUB_POST_RESET, but it's easier not to.
987 */
988 if (type == HUB_INIT) {
989 delay = hub_power_on(hub, false);
990 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
991 schedule_delayed_work(&hub->init_work,
992 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -0400993
994 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -0500995 usb_autopm_get_interface_no_resume(
996 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -0400997 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -0800998 } else if (type == HUB_RESET_RESUME) {
999 /* The internal host controller state for the hub device
1000 * may be gone after a host power loss on system resume.
1001 * Update the device's info so the HW knows it's a hub.
1002 */
1003 hcd = bus_to_hcd(hdev->bus);
1004 if (hcd->driver->update_hub_device) {
1005 ret = hcd->driver->update_hub_device(hcd, hdev,
1006 &hub->tt, GFP_NOIO);
1007 if (ret < 0) {
1008 dev_err(hub->intfdev, "Host not "
1009 "accepting hub info "
1010 "update.\n");
1011 dev_err(hub->intfdev, "LS/FS devices "
1012 "and hubs may not work "
1013 "under this hub\n.");
1014 }
1015 }
1016 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001017 } else {
1018 hub_power_on(hub, true);
1019 }
1020 }
1021 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001022
Alan Stern6ee0b272008-04-28 11:06:42 -04001023 /* Check each port and set hub->change_bits to let khubd know
1024 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001025 */
1026 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001027 struct usb_device *udev = hdev->children[port1-1];
Alan Stern5e6effa2008-03-03 15:15:51 -05001028 u16 portstatus, portchange;
1029
Alan Stern6ee0b272008-04-28 11:06:42 -04001030 portstatus = portchange = 0;
1031 status = hub_port_status(hub, port1, &portstatus, &portchange);
1032 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1033 dev_dbg(hub->intfdev,
1034 "port %d: status %04x change %04x\n",
1035 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001036
Alan Stern6ee0b272008-04-28 11:06:42 -04001037 /* After anything other than HUB_RESUME (i.e., initialization
1038 * or any sort of reset), every port should be disabled.
1039 * Unconnected ports should likewise be disabled (paranoia),
1040 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001041 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001042 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1043 type != HUB_RESUME ||
1044 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1045 !udev ||
1046 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001047 /*
1048 * USB3 protocol ports will automatically transition
1049 * to Enabled state when detect an USB3.0 device attach.
1050 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001051 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001052 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001053 clear_port_feature(hdev, port1,
1054 USB_PORT_FEAT_ENABLE);
1055 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001056 } else {
1057 /* Pretend that power was lost for USB3 devs */
1058 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001059 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001060 }
1061
Alan Stern948fea32008-04-28 11:07:07 -04001062 /* Clear status-change flags; we'll debounce later */
1063 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1064 need_debounce_delay = true;
1065 clear_port_feature(hub->hdev, port1,
1066 USB_PORT_FEAT_C_CONNECTION);
1067 }
1068 if (portchange & USB_PORT_STAT_C_ENABLE) {
1069 need_debounce_delay = true;
1070 clear_port_feature(hub->hdev, port1,
1071 USB_PORT_FEAT_C_ENABLE);
1072 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001073 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1074 hub_is_superspeed(hub->hdev)) {
1075 need_debounce_delay = true;
1076 clear_port_feature(hub->hdev, port1,
1077 USB_PORT_FEAT_C_BH_PORT_RESET);
1078 }
Alan Stern253e0572009-10-27 15:20:13 -04001079 /* We can forget about a "removed" device when there's a
1080 * physical disconnect or the connect status changes.
1081 */
1082 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1083 (portchange & USB_PORT_STAT_C_CONNECTION))
1084 clear_bit(port1, hub->removed_bits);
1085
Alan Stern6ee0b272008-04-28 11:06:42 -04001086 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1087 /* Tell khubd to disconnect the device or
1088 * check for a new connection
1089 */
1090 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1091 set_bit(port1, hub->change_bits);
1092
1093 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001094 bool port_resumed = (portstatus &
1095 USB_PORT_STAT_LINK_STATE) ==
1096 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001097 /* The power session apparently survived the resume.
1098 * If there was an overcurrent or suspend change
1099 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001100 * take care of it. Look at the port link state
1101 * for USB 3.0 hubs, since they don't have a suspend
1102 * change bit, and they don't set the port link change
1103 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001104 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001105 if (portchange || (hub_is_superspeed(hub->hdev) &&
1106 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001107 set_bit(port1, hub->change_bits);
1108
1109 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001110#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001111 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001112#endif
Alan Stern8808f002008-04-28 11:06:55 -04001113 set_bit(port1, hub->change_bits);
1114
Alan Stern6ee0b272008-04-28 11:06:42 -04001115 } else {
1116 /* The power session is gone; tell khubd */
1117 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1118 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001119 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001120 }
1121
Alan Stern948fea32008-04-28 11:07:07 -04001122 /* If no port-status-change flags were set, we don't need any
1123 * debouncing. If flags were set we can try to debounce the
1124 * ports all at once right now, instead of letting khubd do them
1125 * one at a time later on.
1126 *
1127 * If any port-status changes do occur during this delay, khubd
1128 * will see them later and handle them normally.
1129 */
Alan Stern8520f382008-09-22 14:44:26 -04001130 if (need_debounce_delay) {
1131 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001132
Alan Stern8520f382008-09-22 14:44:26 -04001133 /* Don't do a long sleep inside a workqueue routine */
1134 if (type == HUB_INIT2) {
1135 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1136 schedule_delayed_work(&hub->init_work,
1137 msecs_to_jiffies(delay));
1138 return; /* Continues at init3: below */
1139 } else {
1140 msleep(delay);
1141 }
1142 }
1143 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001144 hub->quiescing = 0;
1145
1146 status = usb_submit_urb(hub->urb, GFP_NOIO);
1147 if (status < 0)
1148 dev_err(hub->intfdev, "activate --> %d\n", status);
1149 if (hub->has_indicators && blinkenlights)
1150 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1151
1152 /* Scan all ports that need attention */
1153 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001154
1155 /* Allow autosuspend if it was suppressed */
1156 if (type <= HUB_INIT3)
1157 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001158}
1159
Alan Stern8520f382008-09-22 14:44:26 -04001160/* Implement the continuations for the delays above */
1161static void hub_init_func2(struct work_struct *ws)
1162{
1163 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1164
1165 hub_activate(hub, HUB_INIT2);
1166}
1167
1168static void hub_init_func3(struct work_struct *ws)
1169{
1170 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1171
1172 hub_activate(hub, HUB_INIT3);
1173}
1174
Alan Stern43303542008-04-28 11:07:31 -04001175enum hub_quiescing_type {
1176 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1177};
1178
1179static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1180{
1181 struct usb_device *hdev = hub->hdev;
1182 int i;
1183
Alan Stern8520f382008-09-22 14:44:26 -04001184 cancel_delayed_work_sync(&hub->init_work);
1185
Alan Stern43303542008-04-28 11:07:31 -04001186 /* khubd and related activity won't re-trigger */
1187 hub->quiescing = 1;
1188
1189 if (type != HUB_SUSPEND) {
1190 /* Disconnect all the children */
1191 for (i = 0; i < hdev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001192 if (hdev->children[i])
1193 usb_disconnect(&hdev->children[i]);
Alan Stern43303542008-04-28 11:07:31 -04001194 }
1195 }
1196
1197 /* Stop khubd and related activity */
1198 usb_kill_urb(hub->urb);
1199 if (hub->has_indicators)
1200 cancel_delayed_work_sync(&hub->leds);
1201 if (hub->tt.hub)
Alan Sterncb88a1b2009-06-29 10:43:32 -04001202 cancel_work_sync(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001203}
1204
Alan Stern3eb14912008-03-03 15:15:43 -05001205/* caller has locked the hub device */
1206static int hub_pre_reset(struct usb_interface *intf)
1207{
1208 struct usb_hub *hub = usb_get_intfdata(intf);
1209
Alan Stern43303542008-04-28 11:07:31 -04001210 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001211 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001212}
1213
1214/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001215static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001216{
Alan Stern7de18d82006-06-01 13:37:24 -04001217 struct usb_hub *hub = usb_get_intfdata(intf);
1218
Alan Sternf2835212008-04-28 11:07:17 -04001219 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001220 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001221}
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223static int hub_configure(struct usb_hub *hub,
1224 struct usb_endpoint_descriptor *endpoint)
1225{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001226 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct usb_device *hdev = hub->hdev;
1228 struct device *hub_dev = hub->intfdev;
1229 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001230 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 unsigned int pipe;
1232 int maxp, ret;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001233 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Alan Sternd697cdd2009-10-27 15:18:46 -04001235 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 ret = -ENOMEM;
1238 goto fail;
1239 }
1240
1241 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1242 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 ret = -ENOMEM;
1244 goto fail;
1245 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001246 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1249 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 ret = -ENOMEM;
1251 goto fail;
1252 }
1253
1254 /* Request the entire hub descriptor.
1255 * hub->descriptor can handle USB_MAXCHILDREN ports,
1256 * but the hub can/will return fewer bytes here.
1257 */
John Youndbe79bb2001-09-17 00:00:00 -07001258 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (ret < 0) {
1260 message = "can't read hub descriptor";
1261 goto fail;
1262 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1263 message = "hub has too many ports!";
1264 ret = -ENODEV;
1265 goto fail;
1266 }
1267
1268 hdev->maxchild = hub->descriptor->bNbrPorts;
1269 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1270 (hdev->maxchild == 1) ? "" : "s");
1271
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001272 hdev->children = kzalloc(hdev->maxchild *
1273 sizeof(struct usb_device *), GFP_KERNEL);
Lan Tianyu336c5c32012-07-06 14:13:52 +08001274 hub->port_owners = kzalloc(hdev->maxchild * sizeof(struct dev_state *),
1275 GFP_KERNEL);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001276 if (!hdev->children || !hub->port_owners) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001277 ret = -ENOMEM;
1278 goto fail;
1279 }
1280
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001281 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
John Youndbe79bb2001-09-17 00:00:00 -07001283 /* FIXME for USB 3.0, skip for now */
1284 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1285 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int i;
1287 char portstr [USB_MAXCHILDREN + 1];
1288
1289 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001290 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1292 ? 'F' : 'R';
1293 portstr[hdev->maxchild] = 0;
1294 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1295 } else
1296 dev_dbg(hub_dev, "standalone hub\n");
1297
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001298 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301299 case HUB_CHAR_COMMON_LPSM:
1300 dev_dbg(hub_dev, "ganged power switching\n");
1301 break;
1302 case HUB_CHAR_INDV_PORT_LPSM:
1303 dev_dbg(hub_dev, "individual port power switching\n");
1304 break;
1305 case HUB_CHAR_NO_LPSM:
1306 case HUB_CHAR_LPSM:
1307 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1308 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001311 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301312 case HUB_CHAR_COMMON_OCPM:
1313 dev_dbg(hub_dev, "global over-current protection\n");
1314 break;
1315 case HUB_CHAR_INDV_PORT_OCPM:
1316 dev_dbg(hub_dev, "individual port over-current protection\n");
1317 break;
1318 case HUB_CHAR_NO_OCPM:
1319 case HUB_CHAR_OCPM:
1320 dev_dbg(hub_dev, "no over-current protection\n");
1321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
1324 spin_lock_init (&hub->tt.lock);
1325 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001326 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301328 case USB_HUB_PR_FS:
1329 break;
1330 case USB_HUB_PR_HS_SINGLE_TT:
1331 dev_dbg(hub_dev, "Single TT\n");
1332 hub->tt.hub = hdev;
1333 break;
1334 case USB_HUB_PR_HS_MULTI_TT:
1335 ret = usb_set_interface(hdev, 0, 1);
1336 if (ret == 0) {
1337 dev_dbg(hub_dev, "TT per port\n");
1338 hub->tt.multi = 1;
1339 } else
1340 dev_err(hub_dev, "Using single TT (err %d)\n",
1341 ret);
1342 hub->tt.hub = hdev;
1343 break;
1344 case USB_HUB_PR_SS:
1345 /* USB 3.0 hubs don't have a TT */
1346 break;
1347 default:
1348 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1349 hdev->descriptor.bDeviceProtocol);
1350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001353 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001354 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001355 case HUB_TTTT_8_BITS:
1356 if (hdev->descriptor.bDeviceProtocol != 0) {
1357 hub->tt.think_time = 666;
1358 dev_dbg(hub_dev, "TT requires at most %d "
1359 "FS bit times (%d ns)\n",
1360 8, hub->tt.think_time);
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001363 case HUB_TTTT_16_BITS:
1364 hub->tt.think_time = 666 * 2;
1365 dev_dbg(hub_dev, "TT requires at most %d "
1366 "FS bit times (%d ns)\n",
1367 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001369 case HUB_TTTT_24_BITS:
1370 hub->tt.think_time = 666 * 3;
1371 dev_dbg(hub_dev, "TT requires at most %d "
1372 "FS bit times (%d ns)\n",
1373 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001375 case HUB_TTTT_32_BITS:
1376 hub->tt.think_time = 666 * 4;
1377 dev_dbg(hub_dev, "TT requires at most %d "
1378 "FS bit times (%d ns)\n",
1379 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
1381 }
1382
1383 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001384 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 hub->has_indicators = 1;
1386 dev_dbg(hub_dev, "Port indicators are supported\n");
1387 }
1388
1389 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1390 hub->descriptor->bPwrOn2PwrGood * 2);
1391
1392 /* power budgeting mostly matters with bus-powered hubs,
1393 * and battery-powered root hubs (may provide just 8 mA).
1394 */
1395 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001396 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 message = "can't get hub status";
1398 goto fail;
1399 }
Alan Stern7d35b922005-04-25 11:18:32 -04001400 le16_to_cpus(&hubstatus);
1401 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001402 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1403 hub->mA_per_port = 500;
1404 else {
1405 hub->mA_per_port = hdev->bus_mA;
1406 hub->limited_power = 1;
1407 }
Alan Stern7d35b922005-04-25 11:18:32 -04001408 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1410 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001411 hub->limited_power = 1;
1412 if (hdev->maxchild > 0) {
1413 int remaining = hdev->bus_mA -
1414 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Alan Stern55c52712005-11-23 12:03:12 -05001416 if (remaining < hdev->maxchild * 100)
1417 dev_warn(hub_dev,
1418 "insufficient power available "
1419 "to use all downstream ports\n");
1420 hub->mA_per_port = 100; /* 7.2.1.1 */
1421 }
1422 } else { /* Self-powered external hub */
1423 /* FIXME: What about battery-powered external hubs that
1424 * provide less current per port? */
1425 hub->mA_per_port = 500;
1426 }
1427 if (hub->mA_per_port < 500)
1428 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1429 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001431 /* Update the HCD's internal representation of this hub before khubd
1432 * starts getting port status changes for devices under the hub.
1433 */
1434 hcd = bus_to_hcd(hdev->bus);
1435 if (hcd->driver->update_hub_device) {
1436 ret = hcd->driver->update_hub_device(hcd, hdev,
1437 &hub->tt, GFP_KERNEL);
1438 if (ret < 0) {
1439 message = "can't update HCD hub info";
1440 goto fail;
1441 }
1442 }
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1445 if (ret < 0) {
1446 message = "can't get hub status";
1447 goto fail;
1448 }
1449
1450 /* local power status reports aren't always correct */
1451 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1452 dev_dbg(hub_dev, "local power source is %s\n",
1453 (hubstatus & HUB_STATUS_LOCAL_POWER)
1454 ? "lost (inactive)" : "good");
1455
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001456 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 dev_dbg(hub_dev, "%sover-current condition exists\n",
1458 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1459
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001460 /* set up the interrupt endpoint
1461 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1462 * bytes as USB2.0[11.12.3] says because some hubs are known
1463 * to send more data (and thus cause overflow). For root hubs,
1464 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1465 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1467 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1468
1469 if (maxp > sizeof(*hub->buffer))
1470 maxp = sizeof(*hub->buffer);
1471
1472 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1473 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 ret = -ENOMEM;
1475 goto fail;
1476 }
1477
1478 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1479 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 /* maybe cycle the hub leds */
1482 if (hub->has_indicators && blinkenlights)
1483 hub->indicator [0] = INDICATOR_CYCLE;
1484
Alan Sternf2835212008-04-28 11:07:17 -04001485 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return 0;
1487
1488fail:
1489 dev_err (hub_dev, "config failed, %s (err %d)\n",
1490 message, ret);
1491 /* hub_disconnect() frees urb and descriptor */
1492 return ret;
1493}
1494
Alan Sterne8054852007-05-04 11:55:11 -04001495static void hub_release(struct kref *kref)
1496{
1497 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1498
1499 usb_put_intf(to_usb_interface(hub->intfdev));
1500 kfree(hub);
1501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static unsigned highspeed_hubs;
1504
1505static void hub_disconnect(struct usb_interface *intf)
1506{
Huajun Li88162302012-03-12 21:00:19 +08001507 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001508 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Sterne8054852007-05-04 11:55:11 -04001509
1510 /* Take the hub off the event list and don't let it be added again */
1511 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001512 if (!list_empty(&hub->event_list)) {
1513 list_del_init(&hub->event_list);
1514 usb_autopm_put_interface_no_suspend(intf);
1515 }
Alan Sterne8054852007-05-04 11:55:11 -04001516 hub->disconnected = 1;
1517 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Alan Stern7de18d82006-06-01 13:37:24 -04001519 /* Disconnect all children and quiesce the hub */
1520 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001521 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001522
Alan Stern8b28c752005-08-10 17:04:13 -04001523 usb_set_intfdata (intf, NULL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001524 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Alan Sterne8054852007-05-04 11:55:11 -04001526 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 highspeed_hubs--;
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 usb_free_urb(hub->urb);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001530 kfree(hdev->children);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001531 kfree(hub->port_owners);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001532 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001533 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001534 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Alan Sterne8054852007-05-04 11:55:11 -04001536 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1540{
1541 struct usb_host_interface *desc;
1542 struct usb_endpoint_descriptor *endpoint;
1543 struct usb_device *hdev;
1544 struct usb_hub *hub;
1545
1546 desc = intf->cur_altsetting;
1547 hdev = interface_to_usbdev(intf);
1548
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001549 /* Hubs have proper suspend/resume support. */
1550 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001551
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001552 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001553 dev_err(&intf->dev,
1554 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001555 return -E2BIG;
1556 }
1557
David Brownell89ccbdc2006-04-02 10:18:09 -08001558#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1559 if (hdev->parent) {
1560 dev_warn(&intf->dev, "ignoring external hub\n");
1561 return -ENODEV;
1562 }
1563#endif
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* Some hubs have a subclass of 1, which AFAICT according to the */
1566 /* specs is not defined, but it works */
1567 if ((desc->desc.bInterfaceSubClass != 0) &&
1568 (desc->desc.bInterfaceSubClass != 1)) {
1569descriptor_error:
1570 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1571 return -EIO;
1572 }
1573
1574 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1575 if (desc->desc.bNumEndpoints != 1)
1576 goto descriptor_error;
1577
1578 endpoint = &desc->endpoint[0].desc;
1579
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001580 /* If it's not an interrupt in endpoint, we'd better punt! */
1581 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 goto descriptor_error;
1583
1584 /* We found a hub */
1585 dev_info (&intf->dev, "USB hub found\n");
1586
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001587 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (!hub) {
1589 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1590 return -ENOMEM;
1591 }
1592
Alan Sterne8054852007-05-04 11:55:11 -04001593 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 INIT_LIST_HEAD(&hub->event_list);
1595 hub->intfdev = &intf->dev;
1596 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001597 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001598 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001599 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001602 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 if (hdev->speed == USB_SPEED_HIGH)
1605 highspeed_hubs++;
1606
1607 if (hub_configure(hub, endpoint) >= 0)
1608 return 0;
1609
1610 hub_disconnect (intf);
1611 return -ENODEV;
1612}
1613
1614static int
1615hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1616{
1617 struct usb_device *hdev = interface_to_usbdev (intf);
1618
1619 /* assert ifno == 0 (part of hub spec) */
1620 switch (code) {
1621 case USBDEVFS_HUB_PORTINFO: {
1622 struct usbdevfs_hub_portinfo *info = user_data;
1623 int i;
1624
1625 spin_lock_irq(&device_state_lock);
1626 if (hdev->devnum <= 0)
1627 info->nports = 0;
1628 else {
1629 info->nports = hdev->maxchild;
1630 for (i = 0; i < info->nports; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001631 if (hdev->children[i] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 info->port[i] = 0;
1633 else
1634 info->port[i] =
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001635 hdev->children[i]->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 }
1638 spin_unlock_irq(&device_state_lock);
1639
1640 return info->nports + 1;
1641 }
1642
1643 default:
1644 return -ENOSYS;
1645 }
1646}
1647
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001648/*
1649 * Allow user programs to claim ports on a hub. When a device is attached
1650 * to one of these "claimed" ports, the program will "own" the device.
1651 */
1652static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001653 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001654{
1655 if (hdev->state == USB_STATE_NOTATTACHED)
1656 return -ENODEV;
1657 if (port1 == 0 || port1 > hdev->maxchild)
1658 return -EINVAL;
1659
1660 /* This assumes that devices not managed by the hub driver
1661 * will always have maxchild equal to 0.
1662 */
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001663 *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001664 return 0;
1665}
1666
1667/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001668int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1669 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001670{
1671 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001672 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001673
1674 rc = find_port_owner(hdev, port1, &powner);
1675 if (rc)
1676 return rc;
1677 if (*powner)
1678 return -EBUSY;
1679 *powner = owner;
1680 return rc;
1681}
1682
Lan Tianyu336c5c32012-07-06 14:13:52 +08001683int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1684 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001685{
1686 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001687 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001688
1689 rc = find_port_owner(hdev, port1, &powner);
1690 if (rc)
1691 return rc;
1692 if (*powner != owner)
1693 return -ENOENT;
1694 *powner = NULL;
1695 return rc;
1696}
1697
Lan Tianyu336c5c32012-07-06 14:13:52 +08001698void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001699{
1700 int n;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001701 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001702
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001703 n = find_port_owner(hdev, 1, &powner);
1704 if (n == 0) {
1705 for (; n < hdev->maxchild; (++n, ++powner)) {
1706 if (*powner == owner)
1707 *powner = NULL;
1708 }
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001709 }
1710}
1711
1712/* The caller must hold udev's lock */
1713bool usb_device_is_owned(struct usb_device *udev)
1714{
1715 struct usb_hub *hub;
1716
1717 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1718 return false;
1719 hub = hdev_to_hub(udev->parent);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001720 return !!hub->port_owners[udev->portnum - 1];
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001721}
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1725{
1726 int i;
1727
1728 for (i = 0; i < udev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001729 if (udev->children[i])
1730 recursively_mark_NOTATTACHED(udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001732 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001733 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 udev->state = USB_STATE_NOTATTACHED;
1735}
1736
1737/**
1738 * usb_set_device_state - change a device's current state (usbcore, hcds)
1739 * @udev: pointer to device whose state should be changed
1740 * @new_state: new state value to be stored
1741 *
1742 * udev->state is _not_ fully protected by the device lock. Although
1743 * most transitions are made only while holding the lock, the state can
1744 * can change to USB_STATE_NOTATTACHED at almost any time. This
1745 * is so that devices can be marked as disconnected as soon as possible,
1746 * without having to wait for any semaphores to be released. As a result,
1747 * all changes to any device's state must be protected by the
1748 * device_state_lock spinlock.
1749 *
1750 * Once a device has been added to the device tree, all changes to its state
1751 * should be made using this routine. The state should _not_ be set directly.
1752 *
1753 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1754 * Otherwise udev->state is set to new_state, and if new_state is
1755 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1756 * to USB_STATE_NOTATTACHED.
1757 */
1758void usb_set_device_state(struct usb_device *udev,
1759 enum usb_device_state new_state)
1760{
1761 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001762 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 spin_lock_irqsave(&device_state_lock, flags);
1765 if (udev->state == USB_STATE_NOTATTACHED)
1766 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001767 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001768
1769 /* root hub wakeup capabilities are managed out-of-band
1770 * and may involve silicon errata ... ignore them here.
1771 */
1772 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001773 if (udev->state == USB_STATE_SUSPENDED
1774 || new_state == USB_STATE_SUSPENDED)
1775 ; /* No change to wakeup settings */
1776 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001777 wakeup = udev->actconfig->desc.bmAttributes
1778 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001779 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001780 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001781 }
Sarah Sharp15123002007-12-21 16:54:15 -08001782 if (udev->state == USB_STATE_SUSPENDED &&
1783 new_state != USB_STATE_SUSPENDED)
1784 udev->active_duration -= jiffies;
1785 else if (new_state == USB_STATE_SUSPENDED &&
1786 udev->state != USB_STATE_SUSPENDED)
1787 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001788 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001789 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 recursively_mark_NOTATTACHED(udev);
1791 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001792 if (wakeup >= 0)
1793 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
David Vrabel6da9c992009-02-18 14:43:47 +00001795EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001797/*
Alan Stern3b29b682011-02-22 09:53:41 -05001798 * Choose a device number.
1799 *
1800 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1801 * USB-2.0 buses they are also used as device addresses, however on
1802 * USB-3.0 buses the address is assigned by the controller hardware
1803 * and it usually is not the same as the device number.
1804 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001805 * WUSB devices are simple: they have no hubs behind, so the mapping
1806 * device <-> virtual port number becomes 1:1. Why? to simplify the
1807 * life of the device connection logic in
1808 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1809 * handshake we need to assign a temporary address in the unauthorized
1810 * space. For simplicity we use the first virtual port number found to
1811 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1812 * and that becomes it's address [X < 128] or its unauthorized address
1813 * [X | 0x80].
1814 *
1815 * We add 1 as an offset to the one-based USB-stack port number
1816 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1817 * 0 is reserved by USB for default address; (b) Linux's USB stack
1818 * uses always #1 for the root hub of the controller. So USB stack's
1819 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001820 *
1821 * Devices connected under xHCI are not as simple. The host controller
1822 * supports virtualization, so the hardware assigns device addresses and
1823 * the HCD must setup data structures before issuing a set address
1824 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001825 */
Alan Stern3b29b682011-02-22 09:53:41 -05001826static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 int devnum;
1829 struct usb_bus *bus = udev->bus;
1830
1831 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001832 if (udev->wusb) {
1833 devnum = udev->portnum + 1;
1834 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1835 } else {
1836 /* Try to allocate the next devnum beginning at
1837 * bus->devnum_next. */
1838 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1839 bus->devnum_next);
1840 if (devnum >= 128)
1841 devnum = find_next_zero_bit(bus->devmap.devicemap,
1842 128, 1);
1843 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 if (devnum < 128) {
1846 set_bit(devnum, bus->devmap.devicemap);
1847 udev->devnum = devnum;
1848 }
1849}
1850
Alan Stern3b29b682011-02-22 09:53:41 -05001851static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 if (udev->devnum > 0) {
1854 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1855 udev->devnum = -1;
1856 }
1857}
1858
Alan Stern3b29b682011-02-22 09:53:41 -05001859static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001860{
1861 /* The address for a WUSB device is managed by wusbcore. */
1862 if (!udev->wusb)
1863 udev->devnum = devnum;
1864}
1865
Herbert Xuf7410ce2010-01-10 20:15:03 +11001866static void hub_free_dev(struct usb_device *udev)
1867{
1868 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1869
1870 /* Root hubs aren't real devices, so don't free HCD resources */
1871 if (hcd->driver->free_dev && udev->parent)
1872 hcd->driver->free_dev(hcd, udev);
1873}
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875/**
1876 * usb_disconnect - disconnect a device (usbcore-internal)
1877 * @pdev: pointer to device being disconnected
1878 * Context: !in_interrupt ()
1879 *
1880 * Something got disconnected. Get rid of it and all of its children.
1881 *
1882 * If *pdev is a normal device then the parent hub must already be locked.
1883 * If *pdev is a root hub then this routine will acquire the
1884 * usb_bus_list_lock on behalf of the caller.
1885 *
1886 * Only hub drivers (including virtual root hub drivers for host
1887 * controllers) should ever call this.
1888 *
1889 * This call is synchronous, and may not be used in an interrupt context.
1890 */
1891void usb_disconnect(struct usb_device **pdev)
1892{
1893 struct usb_device *udev = *pdev;
1894 int i;
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 /* mark the device as inactive, so any further urb submissions for
1897 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001898 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
1900 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001901 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1902 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001904 usb_lock_device(udev);
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001907 for (i = 0; i < udev->maxchild; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001908 if (udev->children[i])
1909 usb_disconnect(&udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
1912 /* deallocate hcd/hardware state ... nuking all pending urbs and
1913 * cleaning up all state associated with the current configuration
1914 * so that the hardware is now fully quiesced.
1915 */
Alan Stern782da722006-07-01 22:09:35 -04001916 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001918 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Alan Stern3b23dd62008-12-05 14:10:34 -05001920 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001921 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001922
Alan Stern782da722006-07-01 22:09:35 -04001923 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001924 * for de-configuring the device and invoking the remove-device
1925 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001926 */
1927 device_del(&udev->dev);
1928
1929 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 * (or root_hub) pointer.
1931 */
Alan Stern3b29b682011-02-22 09:53:41 -05001932 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /* Avoid races with recursively_mark_NOTATTACHED() */
1935 spin_lock_irq(&device_state_lock);
1936 *pdev = NULL;
1937 spin_unlock_irq(&device_state_lock);
1938
Herbert Xuf7410ce2010-01-10 20:15:03 +11001939 hub_free_dev(udev);
1940
Alan Stern782da722006-07-01 22:09:35 -04001941 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001944#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945static void show_string(struct usb_device *udev, char *id, char *string)
1946{
1947 if (!string)
1948 return;
1949 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1950}
1951
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001952static void announce_device(struct usb_device *udev)
1953{
1954 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1955 le16_to_cpu(udev->descriptor.idVendor),
1956 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001957 dev_info(&udev->dev,
1958 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001959 udev->descriptor.iManufacturer,
1960 udev->descriptor.iProduct,
1961 udev->descriptor.iSerialNumber);
1962 show_string(udev, "Product", udev->product);
1963 show_string(udev, "Manufacturer", udev->manufacturer);
1964 show_string(udev, "SerialNumber", udev->serial);
1965}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001967static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968#endif
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970#ifdef CONFIG_USB_OTG
1971#include "otg_whitelist.h"
1972#endif
1973
Oliver Neukum3ede7602007-01-11 14:35:50 +01001974/**
Alan Stern8d8558d2009-12-08 15:50:41 -05001975 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01001976 * @udev: newly addressed device (in ADDRESS state)
1977 *
Alan Stern8d8558d2009-12-08 15:50:41 -05001978 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01001979 */
Alan Stern8d8558d2009-12-08 15:50:41 -05001980static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001982 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984#ifdef CONFIG_USB_OTG
1985 /*
1986 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1987 * to wake us after we've powered off VBUS; and HNP, switching roles
1988 * "host" to "peripheral". The OTG descriptor helps figure this out.
1989 */
1990 if (!udev->bus->is_b_host
1991 && udev->config
1992 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02001993 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 struct usb_bus *bus = udev->bus;
1995
1996 /* descriptor may appear anywhere in config */
1997 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1998 le16_to_cpu(udev->config[0].desc.wTotalLength),
1999 USB_DT_OTG, (void **) &desc) == 0) {
2000 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002001 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 dev_info(&udev->dev,
2004 "Dual-Role OTG device on %sHNP port\n",
2005 (port1 == bus->otg_port)
2006 ? "" : "non-");
2007
2008 /* enable HNP before suspend, it's simpler */
2009 if (port1 == bus->otg_port)
2010 bus->b_hnp_enable = 1;
2011 err = usb_control_msg(udev,
2012 usb_sndctrlpipe(udev, 0),
2013 USB_REQ_SET_FEATURE, 0,
2014 bus->b_hnp_enable
2015 ? USB_DEVICE_B_HNP_ENABLE
2016 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2017 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2018 if (err < 0) {
2019 /* OTG MESSAGE: report errors here,
2020 * customize to match your product.
2021 */
2022 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002023 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 err);
2025 bus->b_hnp_enable = 0;
2026 }
2027 }
2028 }
2029 }
2030
2031 if (!is_targeted(udev)) {
2032
2033 /* Maybe it can talk to us, though we can't talk to it.
2034 * (Includes HNP test device.)
2035 */
2036 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002037 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (err < 0)
2039 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2040 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002041 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 goto fail;
2043 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002044fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002046 return err;
2047}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002049
2050/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002051 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002052 * @udev: newly addressed device (in ADDRESS state)
2053 *
2054 * This is only called by usb_new_device() and usb_authorize_device()
2055 * and FIXME -- all comments that apply to them apply here wrt to
2056 * environment.
2057 *
2058 * If the device is WUSB and not authorized, we don't attempt to read
2059 * the string descriptors, as they will be errored out by the device
2060 * until it has been authorized.
2061 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002062static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002063{
2064 int err;
2065
2066 if (udev->config == NULL) {
2067 err = usb_get_configuration(udev);
2068 if (err < 0) {
2069 dev_err(&udev->dev, "can't read configurations, error %d\n",
2070 err);
2071 goto fail;
2072 }
2073 }
2074 if (udev->wusb == 1 && udev->authorized == 0) {
2075 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2076 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2077 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2078 }
2079 else {
2080 /* read the standard strings and cache them if present */
2081 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2082 udev->manufacturer = usb_cache_string(udev,
2083 udev->descriptor.iManufacturer);
2084 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2085 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002086 err = usb_enumerate_device_otg(udev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002087fail:
2088 return err;
2089}
2090
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002091static void set_usb_port_removable(struct usb_device *udev)
2092{
2093 struct usb_device *hdev = udev->parent;
2094 struct usb_hub *hub;
2095 u8 port = udev->portnum;
2096 u16 wHubCharacteristics;
2097 bool removable = true;
2098
2099 if (!hdev)
2100 return;
2101
2102 hub = hdev_to_hub(udev->parent);
2103
2104 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2105
2106 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2107 return;
2108
2109 if (hub_is_superspeed(hdev)) {
2110 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2111 removable = false;
2112 } else {
2113 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2114 removable = false;
2115 }
2116
2117 if (removable)
2118 udev->removable = USB_DEVICE_REMOVABLE;
2119 else
2120 udev->removable = USB_DEVICE_FIXED;
2121}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002122
2123/**
2124 * usb_new_device - perform initial device setup (usbcore-internal)
2125 * @udev: newly addressed device (in ADDRESS state)
2126 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002127 * This is called with devices which have been detected but not fully
2128 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002129 * for any device configuration. The caller must have locked either
2130 * the parent hub (if udev is a normal device) or else the
2131 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2132 * udev has already been installed, but udev is not yet visible through
2133 * sysfs or other filesystem code.
2134 *
2135 * It will return if the device is configured properly or not. Zero if
2136 * the interface was registered with the driver core; else a negative
2137 * errno value.
2138 *
2139 * This call is synchronous, and may not be used in an interrupt context.
2140 *
2141 * Only the hub driver or root-hub registrar should ever call this.
2142 */
2143int usb_new_device(struct usb_device *udev)
2144{
2145 int err;
2146
Dan Streetman16985402010-01-06 09:56:53 -05002147 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002148 /* Initialize non-root-hub device wakeup to disabled;
2149 * device (un)configuration controls wakeup capable
2150 * sysfs power/wakeup controls wakeup enabled/disabled
2151 */
2152 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002153 }
2154
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002155 /* Tell the runtime-PM framework the device is active */
2156 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002157 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002158 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002159 pm_runtime_enable(&udev->dev);
2160
Alan Sternc08512c2010-11-15 15:57:58 -05002161 /* By default, forbid autosuspend for all devices. It will be
2162 * allowed for hubs during binding.
2163 */
2164 usb_disable_autosuspend(udev);
2165
Alan Stern8d8558d2009-12-08 15:50:41 -05002166 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002167 if (err < 0)
2168 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002169 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2170 udev->devnum, udev->bus->busnum,
2171 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002172 /* export the usbdev device-node for libusb */
2173 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2174 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2175
Alan Stern6cd13202008-11-13 15:08:30 -05002176 /* Tell the world! */
2177 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002178
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002179 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002180
2181 /*
2182 * check whether the hub marks this port as non-removable. Do it
2183 * now so that platform-specific data can override it in
2184 * device_add()
2185 */
2186 if (udev->parent)
2187 set_usb_port_removable(udev);
2188
Alan Stern782da722006-07-01 22:09:35 -04002189 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002190 * for configuring the device and invoking the add-device
2191 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002192 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002193 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (err) {
2195 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2196 goto fail;
2197 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002198
Alan Stern3b23dd62008-12-05 14:10:34 -05002199 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002200 usb_mark_last_busy(udev);
2201 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002202 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204fail:
2205 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002206 pm_runtime_disable(&udev->dev);
2207 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002211
2212/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002213 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2214 * @usb_dev: USB device
2215 *
2216 * Move the USB device to a very basic state where interfaces are disabled
2217 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002218 *
2219 * We share a lock (that we have) with device_del(), so we need to
2220 * defer its call.
2221 */
2222int usb_deauthorize_device(struct usb_device *usb_dev)
2223{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002224 usb_lock_device(usb_dev);
2225 if (usb_dev->authorized == 0)
2226 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002227
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002228 usb_dev->authorized = 0;
2229 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002230
2231 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002232 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002233 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002234 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002235 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002236 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002237
2238 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002239 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002240
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002241out_unauthorized:
2242 usb_unlock_device(usb_dev);
2243 return 0;
2244}
2245
2246
2247int usb_authorize_device(struct usb_device *usb_dev)
2248{
2249 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002250
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002251 usb_lock_device(usb_dev);
2252 if (usb_dev->authorized == 1)
2253 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002254
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002255 result = usb_autoresume_device(usb_dev);
2256 if (result < 0) {
2257 dev_err(&usb_dev->dev,
2258 "can't autoresume for authorization: %d\n", result);
2259 goto error_autoresume;
2260 }
2261 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2262 if (result < 0) {
2263 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2264 "authorization: %d\n", result);
2265 goto error_device_descriptor;
2266 }
Alan Sternda307122009-12-08 15:54:44 -05002267
2268 kfree(usb_dev->product);
2269 usb_dev->product = NULL;
2270 kfree(usb_dev->manufacturer);
2271 usb_dev->manufacturer = NULL;
2272 kfree(usb_dev->serial);
2273 usb_dev->serial = NULL;
2274
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002275 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002276 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002277 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002278 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002279 /* Choose and set the configuration. This registers the interfaces
2280 * with the driver core and lets interface drivers bind to them.
2281 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002282 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002283 if (c >= 0) {
2284 result = usb_set_configuration(usb_dev, c);
2285 if (result) {
2286 dev_err(&usb_dev->dev,
2287 "can't set config #%d, error %d\n", c, result);
2288 /* This need not be fatal. The user can try to
2289 * set other configurations. */
2290 }
2291 }
2292 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002293
Alan Stern8d8558d2009-12-08 15:50:41 -05002294error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002295error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002296 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002297error_autoresume:
2298out_authorized:
2299 usb_unlock_device(usb_dev); // complements locktree
2300 return result;
2301}
2302
2303
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002304/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2305static unsigned hub_is_wusb(struct usb_hub *hub)
2306{
2307 struct usb_hcd *hcd;
2308 if (hub->hdev->parent != NULL) /* not a root hub? */
2309 return 0;
2310 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2311 return hcd->wireless;
2312}
2313
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315#define PORT_RESET_TRIES 5
2316#define SET_ADDRESS_TRIES 2
2317#define GET_DESCRIPTOR_TRIES 2
2318#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302319#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2322#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002323#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324#define HUB_LONG_RESET_TIME 200
2325#define HUB_RESET_TIMEOUT 500
2326
Sarah Sharp10d674a2011-09-14 14:24:52 -07002327static int hub_port_reset(struct usb_hub *hub, int port1,
2328 struct usb_device *udev, unsigned int delay, bool warm);
2329
2330/* Is a USB 3.0 port in the Inactive state? */
2331static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus)
2332{
2333 return hub_is_superspeed(hub->hdev) &&
2334 (portstatus & USB_PORT_STAT_LINK_STATE) ==
2335 USB_SS_PORT_LS_SS_INACTIVE;
2336}
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002339 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 int delay_time, ret;
2342 u16 portstatus;
2343 u16 portchange;
2344
2345 for (delay_time = 0;
2346 delay_time < HUB_RESET_TIMEOUT;
2347 delay_time += delay) {
2348 /* wait to give the device a chance to reset */
2349 msleep(delay);
2350
2351 /* read and decode port status */
2352 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2353 if (ret < 0)
2354 return ret;
2355
Andiry Xu75d7cf72011-09-13 16:41:11 -07002356 /*
2357 * Some buggy devices require a warm reset to be issued even
2358 * when the port appears not to be connected.
2359 */
2360 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002361 /*
2362 * Some buggy devices can cause an NEC host controller
2363 * to transition to the "Error" state after a hot port
2364 * reset. This will show up as the port state in
2365 * "Inactive", and the port may also report a
2366 * disconnect. Forcing a warm port reset seems to make
2367 * the device work.
2368 *
2369 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2370 */
2371 if (hub_port_inactive(hub, portstatus)) {
2372 int ret;
2373
2374 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2375 clear_port_feature(hub->hdev, port1,
2376 USB_PORT_FEAT_C_CONNECTION);
2377 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2378 clear_port_feature(hub->hdev, port1,
2379 USB_PORT_FEAT_C_PORT_LINK_STATE);
2380 if (portchange & USB_PORT_STAT_C_RESET)
2381 clear_port_feature(hub->hdev, port1,
2382 USB_PORT_FEAT_C_RESET);
2383 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2384 port1);
2385 ret = hub_port_reset(hub, port1,
2386 udev, HUB_BH_RESET_TIME,
2387 true);
2388 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2389 clear_port_feature(hub->hdev, port1,
2390 USB_PORT_FEAT_C_CONNECTION);
2391 return ret;
2392 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002393 /* Device went away? */
2394 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2395 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Andiry Xu75d7cf72011-09-13 16:41:11 -07002397 /* bomb out completely if the connection bounced */
2398 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2399 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Andiry Xu75d7cf72011-09-13 16:41:11 -07002401 /* if we`ve finished resetting, then break out of
2402 * the loop
2403 */
2404 if (!(portstatus & USB_PORT_STAT_RESET) &&
2405 (portstatus & USB_PORT_STAT_ENABLE)) {
2406 if (hub_is_wusb(hub))
2407 udev->speed = USB_SPEED_WIRELESS;
2408 else if (hub_is_superspeed(hub->hdev))
2409 udev->speed = USB_SPEED_SUPER;
2410 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2411 udev->speed = USB_SPEED_HIGH;
2412 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2413 udev->speed = USB_SPEED_LOW;
2414 else
2415 udev->speed = USB_SPEED_FULL;
2416 return 0;
2417 }
2418 } else {
2419 if (portchange & USB_PORT_STAT_C_BH_RESET)
2420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422
2423 /* switch to the long delay after two short delay failures */
2424 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2425 delay = HUB_LONG_RESET_TIME;
2426
2427 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002428 "port %d not %sreset yet, waiting %dms\n",
2429 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431
2432 return -EBUSY;
2433}
2434
Andiry Xu75d7cf72011-09-13 16:41:11 -07002435static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2436 struct usb_device *udev, int *status, bool warm)
2437{
2438 switch (*status) {
2439 case 0:
2440 if (!warm) {
2441 struct usb_hcd *hcd;
2442 /* TRSTRCY = 10 ms; plus some extra */
2443 msleep(10 + 40);
2444 update_devnum(udev, 0);
2445 hcd = bus_to_hcd(udev->bus);
2446 if (hcd->driver->reset_device) {
2447 *status = hcd->driver->reset_device(hcd, udev);
2448 if (*status < 0) {
2449 dev_err(&udev->dev, "Cannot reset "
2450 "HCD device state\n");
2451 break;
2452 }
2453 }
2454 }
2455 /* FALL THROUGH */
2456 case -ENOTCONN:
2457 case -ENODEV:
2458 clear_port_feature(hub->hdev,
2459 port1, USB_PORT_FEAT_C_RESET);
2460 /* FIXME need disconnect() for NOTATTACHED device */
2461 if (warm) {
2462 clear_port_feature(hub->hdev, port1,
2463 USB_PORT_FEAT_C_BH_PORT_RESET);
2464 clear_port_feature(hub->hdev, port1,
2465 USB_PORT_FEAT_C_PORT_LINK_STATE);
2466 } else {
2467 usb_set_device_state(udev, *status
2468 ? USB_STATE_NOTATTACHED
2469 : USB_STATE_DEFAULT);
2470 }
2471 break;
2472 }
2473}
2474
2475/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002477 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
2479 int i, status;
2480
Andiry Xu75d7cf72011-09-13 16:41:11 -07002481 if (!warm) {
2482 /* Block EHCI CF initialization during the port reset.
2483 * Some companion controllers don't like it when they mix.
2484 */
2485 down_read(&ehci_cf_port_reset_rwsem);
2486 } else {
2487 if (!hub_is_superspeed(hub->hdev)) {
2488 dev_err(hub->intfdev, "only USB3 hub support "
2489 "warm reset\n");
2490 return -EINVAL;
2491 }
2492 }
Alan Stern32fe0192007-10-10 16:27:07 -04002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 /* Reset the port */
2495 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002496 status = set_port_feature(hub->hdev, port1, (warm ?
2497 USB_PORT_FEAT_BH_PORT_RESET :
2498 USB_PORT_FEAT_RESET));
2499 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002501 "cannot %sreset port %d (err = %d)\n",
2502 warm ? "warm " : "", port1, status);
2503 } else {
2504 status = hub_port_wait_reset(hub, port1, udev, delay,
2505 warm);
David Brownelldd165252005-08-31 10:45:25 -07002506 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 dev_dbg(hub->intfdev,
2508 "port_wait_reset: err = %d\n",
2509 status);
2510 }
2511
2512 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002513 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2514 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002515 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
2517
2518 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002519 "port %d not enabled, trying %sreset again...\n",
2520 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 delay = HUB_LONG_RESET_TIME;
2522 }
2523
2524 dev_err (hub->intfdev,
2525 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2526 port1);
2527
Andiry Xu75d7cf72011-09-13 16:41:11 -07002528done:
2529 if (!warm)
2530 up_read(&ehci_cf_port_reset_rwsem);
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 return status;
2533}
2534
Andiry Xu0ed9a572011-04-27 18:07:43 +08002535/* Check if a port is power on */
2536static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2537{
2538 int ret = 0;
2539
2540 if (hub_is_superspeed(hub->hdev)) {
2541 if (portstatus & USB_SS_PORT_STAT_POWER)
2542 ret = 1;
2543 } else {
2544 if (portstatus & USB_PORT_STAT_POWER)
2545 ret = 1;
2546 }
2547
2548 return ret;
2549}
2550
Alan Sternd388dab2006-07-01 22:14:24 -04002551#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Andiry Xu0ed9a572011-04-27 18:07:43 +08002553/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2554static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2555{
2556 int ret = 0;
2557
2558 if (hub_is_superspeed(hub->hdev)) {
2559 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2560 == USB_SS_PORT_LS_U3)
2561 ret = 1;
2562 } else {
2563 if (portstatus & USB_PORT_STAT_SUSPEND)
2564 ret = 1;
2565 }
2566
2567 return ret;
2568}
Alan Sternb01b03f2008-04-28 11:06:11 -04002569
2570/* Determine whether the device on a port is ready for a normal resume,
2571 * is ready for a reset-resume, or should be disconnected.
2572 */
2573static int check_port_resume_type(struct usb_device *udev,
2574 struct usb_hub *hub, int port1,
2575 int status, unsigned portchange, unsigned portstatus)
2576{
2577 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002578 if (status || port_is_suspended(hub, portstatus) ||
2579 !port_is_power_on(hub, portstatus) ||
2580 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002581 if (status >= 0)
2582 status = -ENODEV;
2583 }
2584
Alan Stern86c57ed2008-06-30 11:14:43 -04002585 /* Can't do a normal resume if the port isn't enabled,
2586 * so try a reset-resume instead.
2587 */
2588 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2589 if (udev->persist_enabled)
2590 udev->reset_resume = 1;
2591 else
2592 status = -ENODEV;
2593 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002594
2595 if (status) {
2596 dev_dbg(hub->intfdev,
2597 "port %d status %04x.%04x after resume, %d\n",
2598 port1, portchange, portstatus, status);
2599 } else if (udev->reset_resume) {
2600
2601 /* Late port handoff can set status-change bits */
2602 if (portchange & USB_PORT_STAT_C_CONNECTION)
2603 clear_port_feature(hub->hdev, port1,
2604 USB_PORT_FEAT_C_CONNECTION);
2605 if (portchange & USB_PORT_STAT_C_ENABLE)
2606 clear_port_feature(hub->hdev, port1,
2607 USB_PORT_FEAT_C_ENABLE);
2608 }
2609
2610 return status;
2611}
2612
Sarah Sharpf74631e2012-06-25 12:08:08 -07002613static bool usb_device_supports_ltm(struct usb_device *udev)
2614{
2615 if (udev->speed != USB_SPEED_SUPER || !udev->bos || !udev->bos->ss_cap)
2616 return false;
2617 return udev->bos->ss_cap->bmAttributes & USB_LTM_SUPPORT;
2618}
2619
2620int usb_disable_ltm(struct usb_device *udev)
2621{
2622 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2623
2624 /* Check if the roothub and device supports LTM. */
2625 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2626 !usb_device_supports_ltm(udev))
2627 return 0;
2628
2629 /* Clear Feature LTM Enable can only be sent if the device is
2630 * configured.
2631 */
2632 if (!udev->actconfig)
2633 return 0;
2634
2635 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2636 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2637 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2638 USB_CTRL_SET_TIMEOUT);
2639}
2640EXPORT_SYMBOL_GPL(usb_disable_ltm);
2641
2642void usb_enable_ltm(struct usb_device *udev)
2643{
2644 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2645
2646 /* Check if the roothub and device supports LTM. */
2647 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2648 !usb_device_supports_ltm(udev))
2649 return;
2650
2651 /* Set Feature LTM Enable can only be sent if the device is
2652 * configured.
2653 */
2654 if (!udev->actconfig)
2655 return;
2656
2657 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2658 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2659 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2660 USB_CTRL_SET_TIMEOUT);
2661}
2662EXPORT_SYMBOL_GPL(usb_enable_ltm);
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664#ifdef CONFIG_USB_SUSPEND
2665
2666/*
Alan Stern140d8f62006-07-01 22:07:21 -04002667 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002668 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002669 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 *
2671 * Suspends a USB device that isn't in active use, conserving power.
2672 * Devices may wake out of a suspend, if anything important happens,
2673 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002674 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 * to disconnect devices while they are suspended.
2676 *
David Brownell390a8c32005-09-13 19:57:27 -07002677 * This only affects the USB hardware for a device; its interfaces
2678 * (and, for hubs, child devices) must already have been suspended.
2679 *
Alan Stern624d6c02007-05-30 15:35:16 -04002680 * Selective port suspend reduces power; most suspended devices draw
2681 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2682 * All devices below the suspended port are also suspended.
2683 *
2684 * Devices leave suspend state when the host wakes them up. Some devices
2685 * also support "remote wakeup", where the device can activate the USB
2686 * tree above them to deliver data, such as a keypress or packet. In
2687 * some cases, this wakes the USB host.
2688 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 * Suspending OTG devices may trigger HNP, if that's been enabled
2690 * between a pair of dual-role devices. That will change roles, such
2691 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2692 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002693 * Devices on USB hub ports have only one "suspend" state, corresponding
2694 * to ACPI D2, "may cause the device to lose some context".
2695 * State transitions include:
2696 *
2697 * - suspend, resume ... when the VBUS power link stays live
2698 * - suspend, disconnect ... VBUS lost
2699 *
2700 * Once VBUS drop breaks the circuit, the port it's using has to go through
2701 * normal re-enumeration procedures, starting with enabling VBUS power.
2702 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2703 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2704 * timer, no SRP, no requests through sysfs.
2705 *
2706 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2707 * the root hub for their bus goes into global suspend ... so we don't
2708 * (falsely) update the device power state to say it suspended.
2709 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 * Returns 0 on success, else negative errno.
2711 */
Alan Stern65bfd292008-11-25 16:39:18 -05002712int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Alan Stern624d6c02007-05-30 15:35:16 -04002714 struct usb_hub *hub = hdev_to_hub(udev->parent);
2715 int port1 = udev->portnum;
2716 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002717
Alan Stern624d6c02007-05-30 15:35:16 -04002718 /* enable remote wakeup when appropriate; this lets the device
2719 * wake up the upstream hub (including maybe the root hub).
2720 *
2721 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2722 * we don't explicitly enable it here.
2723 */
2724 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002725 if (!hub_is_superspeed(hub->hdev)) {
2726 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2727 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2728 USB_DEVICE_REMOTE_WAKEUP, 0,
2729 NULL, 0,
2730 USB_CTRL_SET_TIMEOUT);
2731 } else {
2732 /* Assume there's only one function on the USB 3.0
2733 * device and enable remote wake for the first
2734 * interface. FIXME if the interface association
2735 * descriptor shows there's more than one function.
2736 */
2737 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2738 USB_REQ_SET_FEATURE,
2739 USB_RECIP_INTERFACE,
2740 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002741 USB_INTRF_FUNC_SUSPEND_RW |
2742 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002743 NULL, 0,
2744 USB_CTRL_SET_TIMEOUT);
2745 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002746 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002747 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2748 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002749 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002750 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002751 return status;
2752 }
Alan Stern624d6c02007-05-30 15:35:16 -04002753 }
2754
Andiry Xu65580b432011-09-23 14:19:52 -07002755 /* disable USB2 hardware LPM */
2756 if (udev->usb2_hw_lpm_enabled == 1)
2757 usb_set_usb2_hardware_lpm(udev, 0);
2758
Sarah Sharpf74631e2012-06-25 12:08:08 -07002759 if (usb_disable_ltm(udev)) {
2760 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2761 __func__);
2762 return -ENOMEM;
2763 }
Sarah Sharp83060952012-05-02 14:25:52 -07002764 if (usb_unlocked_disable_lpm(udev)) {
2765 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2766 __func__);
2767 return -ENOMEM;
2768 }
2769
Alan Stern624d6c02007-05-30 15:35:16 -04002770 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002771 if (hub_is_superspeed(hub->hdev))
2772 status = set_port_feature(hub->hdev,
2773 port1 | (USB_SS_PORT_LS_U3 << 3),
2774 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002775 else
2776 status = set_port_feature(hub->hdev, port1,
2777 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002778 if (status) {
2779 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2780 port1, status);
2781 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002782 if (udev->do_remote_wakeup)
2783 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002784 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2785 USB_DEVICE_REMOTE_WAKEUP, 0,
2786 NULL, 0,
2787 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002788
Andiry Xuc3e751e2012-05-05 00:50:10 +08002789 /* Try to enable USB2 hardware LPM again */
2790 if (udev->usb2_hw_lpm_capable == 1)
2791 usb_set_usb2_hardware_lpm(udev, 1);
2792
Sarah Sharpf74631e2012-06-25 12:08:08 -07002793 /* Try to enable USB3 LTM and LPM again */
2794 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002795 usb_unlocked_enable_lpm(udev);
2796
Alan Sterncbb33002011-06-15 16:29:16 -04002797 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002798 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002799 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002800 } else {
2801 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002802 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2803 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2804 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002805 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2806 msleep(10);
2807 }
Alan Sternc08512c2010-11-15 15:57:58 -05002808 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002809 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810}
David Brownellf3f32532005-09-22 22:37:29 -07002811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812/*
David Brownell390a8c32005-09-13 19:57:27 -07002813 * If the USB "suspend" state is in use (rather than "global suspend"),
2814 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002815 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 * hardware resume signaling is finished, either because of selective
2817 * resume (by host) or remote wakeup (by device) ... now see what changed
2818 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002819 *
2820 * If @udev->reset_resume is set then the device is reset before the
2821 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 */
Alan Stern140d8f62006-07-01 22:07:21 -04002823static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Alan Stern54515fe2007-05-30 15:38:58 -04002825 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 u16 devstatus;
2827
2828 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002829 dev_dbg(&udev->dev, "%s\n",
2830 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 /* usb ch9 identifies four variants of SUSPENDED, based on what
2833 * state the device resumes to. Linux currently won't see the
2834 * first two on the host side; they'd be inside hub_port_init()
2835 * during many timeouts, but khubd can't suspend until later.
2836 */
2837 usb_set_device_state(udev, udev->actconfig
2838 ? USB_STATE_CONFIGURED
2839 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Alan Stern54515fe2007-05-30 15:38:58 -04002841 /* 10.5.4.5 says not to reset a suspended port if the attached
2842 * device is enabled for remote wakeup. Hence the reset
2843 * operation is carried out here, after the port has been
2844 * resumed.
2845 */
2846 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002847 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002848 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 /* 10.5.4.5 says be sure devices in the tree are still there.
2851 * For now let's assume the device didn't go crazy on resume,
2852 * and device drivers will know about any resume quirks.
2853 */
Alan Stern54515fe2007-05-30 15:38:58 -04002854 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002855 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002856 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2857 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002858 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002859
2860 /* If a normal resume failed, try doing a reset-resume */
2861 if (status && !udev->reset_resume && udev->persist_enabled) {
2862 dev_dbg(&udev->dev, "retry with reset-resume\n");
2863 udev->reset_resume = 1;
2864 goto retry_reset_resume;
2865 }
Alan Stern54515fe2007-05-30 15:38:58 -04002866 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002867
Alan Stern624d6c02007-05-30 15:35:16 -04002868 if (status) {
2869 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2870 status);
2871 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002873 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 status = usb_control_msg(udev,
2875 usb_sndctrlpipe(udev, 0),
2876 USB_REQ_CLEAR_FEATURE,
2877 USB_RECIP_DEVICE,
2878 USB_DEVICE_REMOTE_WAKEUP, 0,
2879 NULL, 0,
2880 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002881 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002882 dev_dbg(&udev->dev,
2883 "disable remote wakeup, status %d\n",
2884 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888 return status;
2889}
2890
Alan Stern624d6c02007-05-30 15:35:16 -04002891/*
2892 * usb_port_resume - re-activate a suspended usb device's upstream port
2893 * @udev: device to re-activate, not a root hub
2894 * Context: must be able to sleep; device not locked; pm locks held
2895 *
2896 * This will re-activate the suspended device, increasing power usage
2897 * while letting drivers communicate again with its endpoints.
2898 * USB resume explicitly guarantees that the power session between
2899 * the host and the device is the same as it was when the device
2900 * suspended.
2901 *
Alan Sternfeccc302008-03-03 15:15:59 -05002902 * If @udev->reset_resume is set then this routine won't check that the
2903 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002904 * reset @udev. The end result is that a broken power session can be
2905 * recovered and @udev will appear to persist across a loss of VBUS power.
2906 *
2907 * For example, if a host controller doesn't maintain VBUS suspend current
2908 * during a system sleep or is reset when the system wakes up, all the USB
2909 * power sessions below it will be broken. This is especially troublesome
2910 * for mass-storage devices containing mounted filesystems, since the
2911 * device will appear to have disconnected and all the memory mappings
2912 * to it will be lost. Using the USB_PERSIST facility, the device can be
2913 * made to appear as if it had not disconnected.
2914 *
Ming Lei742120c2008-06-18 22:00:29 +08002915 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002916 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002917 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2918 * quite possible for a device to remain unaltered but its media to be
2919 * changed. If the user replaces a flash memory card while the system is
2920 * asleep, he will have only himself to blame when the filesystem on the
2921 * new card is corrupted and the system crashes.
2922 *
Alan Stern624d6c02007-05-30 15:35:16 -04002923 * Returns 0 on success, else negative errno.
2924 */
Alan Stern65bfd292008-11-25 16:39:18 -05002925int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
Alan Stern624d6c02007-05-30 15:35:16 -04002927 struct usb_hub *hub = hdev_to_hub(udev->parent);
2928 int port1 = udev->portnum;
2929 int status;
2930 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05002931
2932 /* Skip the initial Clear-Suspend step for a remote wakeup */
2933 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08002934 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05002935 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2938
Alan Sternd5cbad42006-08-11 16:52:39 -04002939 set_bit(port1, hub->busy_bits);
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08002942 if (hub_is_superspeed(hub->hdev))
2943 status = set_port_feature(hub->hdev,
2944 port1 | (USB_SS_PORT_LS_U0 << 3),
2945 USB_PORT_FEAT_LINK_STATE);
2946 else
2947 status = clear_port_feature(hub->hdev,
2948 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002950 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
2951 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04002954 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02002955 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 msleep(25);
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2959 * stop resume signaling. Then finish the resume
2960 * sequence.
2961 */
Alan Sternd25450c2006-11-20 11:14:30 -05002962 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04002963
Alan Sternb01b03f2008-04-28 11:06:11 -04002964 /* TRSMRCY = 10 msec */
2965 msleep(10);
2966 }
Alan Stern54515fe2007-05-30 15:38:58 -04002967
Alan Sternb01b03f2008-04-28 11:06:11 -04002968 SuspendCleared:
2969 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08002970 if (hub_is_superspeed(hub->hdev)) {
2971 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2972 clear_port_feature(hub->hdev, port1,
2973 USB_PORT_FEAT_C_PORT_LINK_STATE);
2974 } else {
2975 if (portchange & USB_PORT_STAT_C_SUSPEND)
2976 clear_port_feature(hub->hdev, port1,
2977 USB_PORT_FEAT_C_SUSPEND);
2978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Alan Sternd5cbad42006-08-11 16:52:39 -04002981 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04002982
Alan Sternb01b03f2008-04-28 11:06:11 -04002983 status = check_port_resume_type(udev,
2984 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04002985 if (status == 0)
2986 status = finish_port_resume(udev);
2987 if (status < 0) {
2988 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2989 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07002990 } else {
2991 /* Try to enable USB2 hardware LPM */
2992 if (udev->usb2_hw_lpm_capable == 1)
2993 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07002994
Sarah Sharpf74631e2012-06-25 12:08:08 -07002995 /* Try to enable USB3 LTM and LPM */
2996 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002997 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002998 }
Andiry Xu65580b432011-09-23 14:19:52 -07002999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return status;
3001}
3002
Alan Stern8808f002008-04-28 11:06:55 -04003003/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003004int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 int status = 0;
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003009 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003010 status = usb_autoresume_device(udev);
3011 if (status == 0) {
3012 /* Let the drivers do their thing, then... */
3013 usb_autosuspend_device(udev);
3014 }
Alan Sternd25450c2006-11-20 11:14:30 -05003015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 return status;
3017}
3018
Alan Sternd388dab2006-07-01 22:14:24 -04003019#else /* CONFIG_USB_SUSPEND */
3020
3021/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3022
Alan Stern65bfd292008-11-25 16:39:18 -05003023int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003024{
3025 return 0;
3026}
3027
Alan Sternb01b03f2008-04-28 11:06:11 -04003028/* However we may need to do a reset-resume */
3029
Alan Stern65bfd292008-11-25 16:39:18 -05003030int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003031{
Alan Sternb01b03f2008-04-28 11:06:11 -04003032 struct usb_hub *hub = hdev_to_hub(udev->parent);
3033 int port1 = udev->portnum;
3034 int status;
3035 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003036
Alan Sternb01b03f2008-04-28 11:06:11 -04003037 status = hub_port_status(hub, port1, &portstatus, &portchange);
3038 status = check_port_resume_type(udev,
3039 hub, port1, status, portchange, portstatus);
3040
3041 if (status) {
3042 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3043 hub_port_logical_disconnect(hub, port1);
3044 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003045 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003046 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003047 }
3048 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003049}
3050
Alan Sternd388dab2006-07-01 22:14:24 -04003051#endif
3052
David Brownelldb690872005-09-13 19:56:33 -07003053static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 struct usb_hub *hub = usb_get_intfdata (intf);
3056 struct usb_device *hdev = hub->hdev;
3057 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003058 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
Alan Sterncbb33002011-06-15 16:29:16 -04003060 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3062 struct usb_device *udev;
3063
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003064 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04003065 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003066 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003067 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003068 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003071 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3072 /* Enable hub to send remote wakeup for all ports. */
3073 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3074 status = set_port_feature(hdev,
3075 port1 |
3076 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3077 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3078 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3079 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3080 }
3081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Harvey Harrison441b62c2008-03-03 16:08:34 -08003083 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003084
David Brownellc9f89fa2005-09-13 19:57:04 -07003085 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003086 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
3090static int hub_resume(struct usb_interface *intf)
3091{
Alan Stern5e6effa2008-03-03 15:15:51 -05003092 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Alan Stern5e6effa2008-03-03 15:15:51 -05003094 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003095 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return 0;
3097}
3098
Alan Sternb41a60e2007-05-30 15:39:33 -04003099static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003100{
Alan Sternb41a60e2007-05-30 15:39:33 -04003101 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003102
Alan Stern5e6effa2008-03-03 15:15:51 -05003103 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003104 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003105 return 0;
3106}
3107
Alan Stern54515fe2007-05-30 15:38:58 -04003108/**
3109 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3110 * @rhdev: struct usb_device for the root hub
3111 *
3112 * The USB host controller driver calls this function when its root hub
3113 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003114 * has been reset. The routine marks @rhdev as having lost power.
3115 * When the hub driver is resumed it will take notice and carry out
3116 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3117 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003118 */
3119void usb_root_hub_lost_power(struct usb_device *rhdev)
3120{
3121 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3122 rhdev->reset_resume = 1;
3123}
3124EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3125
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003126static const char * const usb3_lpm_names[] = {
3127 "U0",
3128 "U1",
3129 "U2",
3130 "U3",
3131};
3132
3133/*
3134 * Send a Set SEL control transfer to the device, prior to enabling
3135 * device-initiated U1 or U2. This lets the device know the exit latencies from
3136 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3137 * packet from the host.
3138 *
3139 * This function will fail if the SEL or PEL values for udev are greater than
3140 * the maximum allowed values for the link state to be enabled.
3141 */
3142static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3143{
3144 struct usb_set_sel_req *sel_values;
3145 unsigned long long u1_sel;
3146 unsigned long long u1_pel;
3147 unsigned long long u2_sel;
3148 unsigned long long u2_pel;
3149 int ret;
3150
3151 /* Convert SEL and PEL stored in ns to us */
3152 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3153 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3154 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3155 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3156
3157 /*
3158 * Make sure that the calculated SEL and PEL values for the link
3159 * state we're enabling aren't bigger than the max SEL/PEL
3160 * value that will fit in the SET SEL control transfer.
3161 * Otherwise the device would get an incorrect idea of the exit
3162 * latency for the link state, and could start a device-initiated
3163 * U1/U2 when the exit latencies are too high.
3164 */
3165 if ((state == USB3_LPM_U1 &&
3166 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3167 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3168 (state == USB3_LPM_U2 &&
3169 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3170 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3171 dev_dbg(&udev->dev, "Device-initiated %s disabled due "
3172 "to long SEL %llu ms or PEL %llu ms\n",
3173 usb3_lpm_names[state], u1_sel, u1_pel);
3174 return -EINVAL;
3175 }
3176
3177 /*
3178 * If we're enabling device-initiated LPM for one link state,
3179 * but the other link state has a too high SEL or PEL value,
3180 * just set those values to the max in the Set SEL request.
3181 */
3182 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3183 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3184
3185 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3186 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3187
3188 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3189 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3190
3191 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3192 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3193
3194 /*
3195 * usb_enable_lpm() can be called as part of a failed device reset,
3196 * which may be initiated by an error path of a mass storage driver.
3197 * Therefore, use GFP_NOIO.
3198 */
3199 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3200 if (!sel_values)
3201 return -ENOMEM;
3202
3203 sel_values->u1_sel = u1_sel;
3204 sel_values->u1_pel = u1_pel;
3205 sel_values->u2_sel = cpu_to_le16(u2_sel);
3206 sel_values->u2_pel = cpu_to_le16(u2_pel);
3207
3208 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3209 USB_REQ_SET_SEL,
3210 USB_RECIP_DEVICE,
3211 0, 0,
3212 sel_values, sizeof *(sel_values),
3213 USB_CTRL_SET_TIMEOUT);
3214 kfree(sel_values);
3215 return ret;
3216}
3217
3218/*
3219 * Enable or disable device-initiated U1 or U2 transitions.
3220 */
3221static int usb_set_device_initiated_lpm(struct usb_device *udev,
3222 enum usb3_link_state state, bool enable)
3223{
3224 int ret;
3225 int feature;
3226
3227 switch (state) {
3228 case USB3_LPM_U1:
3229 feature = USB_DEVICE_U1_ENABLE;
3230 break;
3231 case USB3_LPM_U2:
3232 feature = USB_DEVICE_U2_ENABLE;
3233 break;
3234 default:
3235 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3236 __func__, enable ? "enable" : "disable");
3237 return -EINVAL;
3238 }
3239
3240 if (udev->state != USB_STATE_CONFIGURED) {
3241 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3242 "for unconfigured device.\n",
3243 __func__, enable ? "enable" : "disable",
3244 usb3_lpm_names[state]);
3245 return 0;
3246 }
3247
3248 if (enable) {
3249 /*
3250 * First, let the device know about the exit latencies
3251 * associated with the link state we're about to enable.
3252 */
3253 ret = usb_req_set_sel(udev, state);
3254 if (ret < 0) {
3255 dev_warn(&udev->dev, "Set SEL for device-initiated "
3256 "%s failed.\n", usb3_lpm_names[state]);
3257 return -EBUSY;
3258 }
3259 /*
3260 * Now send the control transfer to enable device-initiated LPM
3261 * for either U1 or U2.
3262 */
3263 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3264 USB_REQ_SET_FEATURE,
3265 USB_RECIP_DEVICE,
3266 feature,
3267 0, NULL, 0,
3268 USB_CTRL_SET_TIMEOUT);
3269 } else {
3270 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3271 USB_REQ_CLEAR_FEATURE,
3272 USB_RECIP_DEVICE,
3273 feature,
3274 0, NULL, 0,
3275 USB_CTRL_SET_TIMEOUT);
3276 }
3277 if (ret < 0) {
3278 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3279 enable ? "Enable" : "Disable",
3280 usb3_lpm_names[state]);
3281 return -EBUSY;
3282 }
3283 return 0;
3284}
3285
3286static int usb_set_lpm_timeout(struct usb_device *udev,
3287 enum usb3_link_state state, int timeout)
3288{
3289 int ret;
3290 int feature;
3291
3292 switch (state) {
3293 case USB3_LPM_U1:
3294 feature = USB_PORT_FEAT_U1_TIMEOUT;
3295 break;
3296 case USB3_LPM_U2:
3297 feature = USB_PORT_FEAT_U2_TIMEOUT;
3298 break;
3299 default:
3300 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3301 __func__);
3302 return -EINVAL;
3303 }
3304
3305 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3306 timeout != USB3_LPM_DEVICE_INITIATED) {
3307 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3308 "which is a reserved value.\n",
3309 usb3_lpm_names[state], timeout);
3310 return -EINVAL;
3311 }
3312
3313 ret = set_port_feature(udev->parent,
3314 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3315 feature);
3316 if (ret < 0) {
3317 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3318 "error code %i\n", usb3_lpm_names[state],
3319 timeout, ret);
3320 return -EBUSY;
3321 }
3322 if (state == USB3_LPM_U1)
3323 udev->u1_params.timeout = timeout;
3324 else
3325 udev->u2_params.timeout = timeout;
3326 return 0;
3327}
3328
3329/*
3330 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3331 * U1/U2 entry.
3332 *
3333 * We will attempt to enable U1 or U2, but there are no guarantees that the
3334 * control transfers to set the hub timeout or enable device-initiated U1/U2
3335 * will be successful.
3336 *
3337 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3338 * driver know about it. If that call fails, it should be harmless, and just
3339 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3340 */
3341static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3342 enum usb3_link_state state)
3343{
3344 int timeout;
3345
3346 /* We allow the host controller to set the U1/U2 timeout internally
3347 * first, so that it can change its schedule to account for the
3348 * additional latency to send data to a device in a lower power
3349 * link state.
3350 */
3351 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3352
3353 /* xHCI host controller doesn't want to enable this LPM state. */
3354 if (timeout == 0)
3355 return;
3356
3357 if (timeout < 0) {
3358 dev_warn(&udev->dev, "Could not enable %s link state, "
3359 "xHCI error %i.\n", usb3_lpm_names[state],
3360 timeout);
3361 return;
3362 }
3363
3364 if (usb_set_lpm_timeout(udev, state, timeout))
3365 /* If we can't set the parent hub U1/U2 timeout,
3366 * device-initiated LPM won't be allowed either, so let the xHCI
3367 * host know that this link state won't be enabled.
3368 */
3369 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3370
3371 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3372 else if (udev->actconfig)
3373 usb_set_device_initiated_lpm(udev, state, true);
3374
3375}
3376
3377/*
3378 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3379 * U1/U2 entry.
3380 *
3381 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3382 * If zero is returned, the parent will not allow the link to go into U1/U2.
3383 *
3384 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3385 * it won't have an effect on the bus link state because the parent hub will
3386 * still disallow device-initiated U1/U2 entry.
3387 *
3388 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3389 * possible. The result will be slightly more bus bandwidth will be taken up
3390 * (to account for U1/U2 exit latency), but it should be harmless.
3391 */
3392static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3393 enum usb3_link_state state)
3394{
3395 int feature;
3396
3397 switch (state) {
3398 case USB3_LPM_U1:
3399 feature = USB_PORT_FEAT_U1_TIMEOUT;
3400 break;
3401 case USB3_LPM_U2:
3402 feature = USB_PORT_FEAT_U2_TIMEOUT;
3403 break;
3404 default:
3405 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3406 __func__);
3407 return -EINVAL;
3408 }
3409
3410 if (usb_set_lpm_timeout(udev, state, 0))
3411 return -EBUSY;
3412
3413 usb_set_device_initiated_lpm(udev, state, false);
3414
3415 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3416 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3417 "bus schedule bandwidth may be impacted.\n",
3418 usb3_lpm_names[state]);
3419 return 0;
3420}
3421
3422/*
3423 * Disable hub-initiated and device-initiated U1 and U2 entry.
3424 * Caller must own the bandwidth_mutex.
3425 *
3426 * This will call usb_enable_lpm() on failure, which will decrement
3427 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3428 */
3429int usb_disable_lpm(struct usb_device *udev)
3430{
3431 struct usb_hcd *hcd;
3432
3433 if (!udev || !udev->parent ||
3434 udev->speed != USB_SPEED_SUPER ||
3435 !udev->lpm_capable)
3436 return 0;
3437
3438 hcd = bus_to_hcd(udev->bus);
3439 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3440 return 0;
3441
3442 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003443 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003444 return 0;
3445
3446 /* If LPM is enabled, attempt to disable it. */
3447 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3448 goto enable_lpm;
3449 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3450 goto enable_lpm;
3451
3452 return 0;
3453
3454enable_lpm:
3455 usb_enable_lpm(udev);
3456 return -EBUSY;
3457}
3458EXPORT_SYMBOL_GPL(usb_disable_lpm);
3459
3460/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3461int usb_unlocked_disable_lpm(struct usb_device *udev)
3462{
3463 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3464 int ret;
3465
3466 if (!hcd)
3467 return -EINVAL;
3468
3469 mutex_lock(hcd->bandwidth_mutex);
3470 ret = usb_disable_lpm(udev);
3471 mutex_unlock(hcd->bandwidth_mutex);
3472
3473 return ret;
3474}
3475EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3476
3477/*
3478 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3479 * xHCI host policy may prevent U1 or U2 from being enabled.
3480 *
3481 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3482 * until the lpm_disable_count drops to zero. Caller must own the
3483 * bandwidth_mutex.
3484 */
3485void usb_enable_lpm(struct usb_device *udev)
3486{
3487 struct usb_hcd *hcd;
3488
3489 if (!udev || !udev->parent ||
3490 udev->speed != USB_SPEED_SUPER ||
3491 !udev->lpm_capable)
3492 return;
3493
3494 udev->lpm_disable_count--;
3495 hcd = bus_to_hcd(udev->bus);
3496 /* Double check that we can both enable and disable LPM.
3497 * Device must be configured to accept set feature U1/U2 timeout.
3498 */
3499 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3500 !hcd->driver->disable_usb3_lpm_timeout)
3501 return;
3502
3503 if (udev->lpm_disable_count > 0)
3504 return;
3505
3506 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3507 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3508}
3509EXPORT_SYMBOL_GPL(usb_enable_lpm);
3510
3511/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3512void usb_unlocked_enable_lpm(struct usb_device *udev)
3513{
3514 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3515
3516 if (!hcd)
3517 return;
3518
3519 mutex_lock(hcd->bandwidth_mutex);
3520 usb_enable_lpm(udev);
3521 mutex_unlock(hcd->bandwidth_mutex);
3522}
3523EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3524
3525
Alan Sternd388dab2006-07-01 22:14:24 -04003526#else /* CONFIG_PM */
3527
Alan Sternf07600c2007-05-30 15:38:16 -04003528#define hub_suspend NULL
3529#define hub_resume NULL
3530#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003531
3532int usb_disable_lpm(struct usb_device *udev)
3533{
3534 return 0;
3535}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003536EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003537
3538void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003539EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003540
3541int usb_unlocked_disable_lpm(struct usb_device *udev)
3542{
3543 return 0;
3544}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003545EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003546
3547void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003548EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003549
3550int usb_disable_ltm(struct usb_device *udev)
3551{
3552 return 0;
3553}
3554EXPORT_SYMBOL_GPL(usb_disable_ltm);
3555
3556void usb_enable_ltm(struct usb_device *udev) { }
3557EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003558#endif
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
3561/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3562 *
3563 * Between connect detection and reset signaling there must be a delay
3564 * of 100ms at least for debounce and power-settling. The corresponding
3565 * timer shall restart whenever the downstream port detects a disconnect.
3566 *
3567 * Apparently there are some bluetooth and irda-dongles and a number of
3568 * low-speed devices for which this debounce period may last over a second.
3569 * Not covered by the spec - but easy to deal with.
3570 *
3571 * This implementation uses a 1500ms total debounce timeout; if the
3572 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3573 * every 25ms for transient disconnects. When the port status has been
3574 * unchanged for 100ms it returns the port status.
3575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576static int hub_port_debounce(struct usb_hub *hub, int port1)
3577{
3578 int ret;
3579 int total_time, stable_time = 0;
3580 u16 portchange, portstatus;
3581 unsigned connection = 0xffff;
3582
3583 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3584 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3585 if (ret < 0)
3586 return ret;
3587
3588 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3589 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3590 stable_time += HUB_DEBOUNCE_STEP;
3591 if (stable_time >= HUB_DEBOUNCE_STABLE)
3592 break;
3593 } else {
3594 stable_time = 0;
3595 connection = portstatus & USB_PORT_STAT_CONNECTION;
3596 }
3597
3598 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3599 clear_port_feature(hub->hdev, port1,
3600 USB_PORT_FEAT_C_CONNECTION);
3601 }
3602
3603 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3604 break;
3605 msleep(HUB_DEBOUNCE_STEP);
3606 }
3607
3608 dev_dbg (hub->intfdev,
3609 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3610 port1, total_time, stable_time, portstatus);
3611
3612 if (stable_time < HUB_DEBOUNCE_STABLE)
3613 return -ETIMEDOUT;
3614 return portstatus;
3615}
3616
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003617void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618{
Alan Sternddeac4e2009-01-15 17:03:33 -05003619 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3620 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003621 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003623EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
3625#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3626#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3627
Alan Stern4326ed02007-07-30 17:08:43 -04003628static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
3630 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003631 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Sarah Sharpc6515272009-04-27 19:57:26 -07003633 /*
3634 * The host controller will choose the device address,
3635 * instead of the core having chosen it earlier
3636 */
3637 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 return -EINVAL;
3639 if (udev->state == USB_STATE_ADDRESS)
3640 return 0;
3641 if (udev->state != USB_STATE_DEFAULT)
3642 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003643 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003644 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003645 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003646 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3647 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3648 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003650 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003651 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003653 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 }
3655 return retval;
3656}
3657
3658/* Reset device, (re)assign address, get device descriptor.
3659 * Device connection must be stable, no more debouncing needed.
3660 * Returns device in USB_STATE_ADDRESS, except on error.
3661 *
3662 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003663 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 * newly detected device that is not accessible through any global
3665 * pointers, it's not necessary to lock the device.
3666 */
3667static int
3668hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3669 int retry_counter)
3670{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003671 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003674 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 int i, j, retval;
3676 unsigned delay = HUB_SHORT_RESET_TIME;
3677 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003678 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003679 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
3681 /* root hub ports have a slightly longer reset period
3682 * (from USB 2.0 spec, section 7.1.7.5)
3683 */
3684 if (!hdev->parent) {
3685 delay = HUB_ROOT_RESET_TIME;
3686 if (port1 == hdev->bus->otg_port)
3687 hdev->bus->b_hnp_enable = 0;
3688 }
3689
3690 /* Some low speed devices have problems with the quick delay, so */
3691 /* be a bit pessimistic with those devices. RHbug #23670 */
3692 if (oldspeed == USB_SPEED_LOW)
3693 delay = HUB_LONG_RESET_TIME;
3694
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003695 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Luben Tuikov07194ab2011-02-11 11:33:10 -08003697 /* Reset the device; full speed may morph to high speed */
3698 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003699 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003700 if (retval < 0) /* error or disconnect */
3701 goto fail;
3702 /* success, speed is known */
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 retval = -ENODEV;
3705
3706 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3707 dev_dbg(&udev->dev, "device reset changed speed!\n");
3708 goto fail;
3709 }
3710 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003711
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3713 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003714 * For Wireless USB devices, ep0 max packet is always 512 (tho
3715 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 */
3717 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003718 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003719 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003720 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003723 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 break;
3725 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3726 /* to determine the ep0 maxpacket size, try to read
3727 * the device descriptor to get bMaxPacketSize0 and
3728 * then correct our initial guess.
3729 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003730 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 break;
3732 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003733 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 break;
3735 default:
3736 goto fail;
3737 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003738
3739 if (udev->speed == USB_SPEED_WIRELESS)
3740 speed = "variable speed Wireless";
3741 else
3742 speed = usb_speed_string(udev->speed);
3743
Sarah Sharpc6515272009-04-27 19:57:26 -07003744 if (udev->speed != USB_SPEED_SUPER)
3745 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003746 "%s %s USB device number %d using %s\n",
3747 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003748 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749
3750 /* Set up TT records, if needed */
3751 if (hdev->tt) {
3752 udev->tt = hdev->tt;
3753 udev->ttport = hdev->ttport;
3754 } else if (udev->speed != USB_SPEED_HIGH
3755 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003756 if (!hub->tt.hub) {
3757 dev_err(&udev->dev, "parent hub has no TT\n");
3758 retval = -EINVAL;
3759 goto fail;
3760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 udev->tt = &hub->tt;
3762 udev->ttport = port1;
3763 }
3764
3765 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3766 * Because device hardware and firmware is sometimes buggy in
3767 * this area, and this is how Linux has done it for ages.
3768 * Change it cautiously.
3769 *
3770 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3771 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3772 * so it may help with some non-standards-compliant devices.
3773 * Otherwise we start with SET_ADDRESS and then try to read the
3774 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3775 * value.
3776 */
3777 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003778 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 struct usb_device_descriptor *buf;
3780 int r = 0;
3781
3782#define GET_DESCRIPTOR_BUFSIZE 64
3783 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3784 if (!buf) {
3785 retval = -ENOMEM;
3786 continue;
3787 }
3788
Alan Sternb89ee192007-05-11 10:19:04 -04003789 /* Retry on all errors; some devices are flakey.
3790 * 255 is for WUSB devices, we actually need to use
3791 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 */
3793 for (j = 0; j < 3; ++j) {
3794 buf->bMaxPacketSize0 = 0;
3795 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3796 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3797 USB_DT_DEVICE << 8, 0,
3798 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003799 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003801 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 if (buf->bDescriptorType ==
3803 USB_DT_DEVICE) {
3804 r = 0;
3805 break;
3806 }
3807 /* FALL THROUGH */
3808 default:
3809 if (r == 0)
3810 r = -EPROTO;
3811 break;
3812 }
3813 if (r == 0)
3814 break;
3815 }
3816 udev->descriptor.bMaxPacketSize0 =
3817 buf->bMaxPacketSize0;
3818 kfree(buf);
3819
Andiry Xu75d7cf72011-09-13 16:41:11 -07003820 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 if (retval < 0) /* error or disconnect */
3822 goto fail;
3823 if (oldspeed != udev->speed) {
3824 dev_dbg(&udev->dev,
3825 "device reset changed speed!\n");
3826 retval = -ENODEV;
3827 goto fail;
3828 }
3829 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003830 dev_err(&udev->dev,
3831 "device descriptor read/64, error %d\n",
3832 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 retval = -EMSGSIZE;
3834 continue;
3835 }
3836#undef GET_DESCRIPTOR_BUFSIZE
3837 }
3838
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003839 /*
3840 * If device is WUSB, we already assigned an
3841 * unauthorized address in the Connect Ack sequence;
3842 * authorization will assign the final address.
3843 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003844 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003845 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3846 retval = hub_set_address(udev, devnum);
3847 if (retval >= 0)
3848 break;
3849 msleep(200);
3850 }
3851 if (retval < 0) {
3852 dev_err(&udev->dev,
3853 "device not accepting address %d, error %d\n",
3854 devnum, retval);
3855 goto fail;
3856 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003857 if (udev->speed == USB_SPEED_SUPER) {
3858 devnum = udev->devnum;
3859 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003860 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003861 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003862 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003863 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003864
3865 /* cope with hardware quirkiness:
3866 * - let SET_ADDRESS settle, some device hardware wants it
3867 * - read ep0 maxpacket even for high and low speed,
3868 */
3869 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003870 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
3874 retval = usb_get_device_descriptor(udev, 8);
3875 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003876 dev_err(&udev->dev,
3877 "device descriptor read/8, error %d\n",
3878 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 if (retval >= 0)
3880 retval = -EMSGSIZE;
3881 } else {
3882 retval = 0;
3883 break;
3884 }
3885 }
3886 if (retval)
3887 goto fail;
3888
Elric Fud8aec3d2012-03-26 21:16:02 +08003889 /*
3890 * Some superspeed devices have finished the link training process
3891 * and attached to a superspeed hub port, but the device descriptor
3892 * got from those devices show they aren't superspeed devices. Warm
3893 * reset the port attached by the devices can fix them.
3894 */
3895 if ((udev->speed == USB_SPEED_SUPER) &&
3896 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3897 dev_err(&udev->dev, "got a wrong device descriptor, "
3898 "warm reset device\n");
3899 hub_port_reset(hub, port1, udev,
3900 HUB_BH_RESET_TIME, true);
3901 retval = -EINVAL;
3902 goto fail;
3903 }
3904
Sarah Sharp6b403b02009-04-27 19:54:10 -07003905 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3906 udev->speed == USB_SPEED_SUPER)
3907 i = 512;
3908 else
3909 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003910 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003911 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003913 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 retval = -EMSGSIZE;
3915 goto fail;
3916 }
Alan Stern56626a72010-10-14 15:25:21 -04003917 if (udev->speed == USB_SPEED_FULL)
3918 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3919 else
3920 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003922 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 }
3924
3925 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3926 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003927 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3928 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (retval >= 0)
3930 retval = -ENOMSG;
3931 goto fail;
3932 }
3933
Andiry Xu1ff4df52011-09-23 14:19:48 -07003934 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3935 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003936 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08003937 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003938 usb_set_lpm_parameters(udev);
3939 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07003940 }
Andiry Xu3148bf02011-09-23 14:19:47 -07003941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08003943 /* notify HCD that we have a device connected and addressed */
3944 if (hcd->driver->update_device)
3945 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946fail:
Alan Stern4326ed02007-07-30 17:08:43 -04003947 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05003949 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04003950 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003951 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 return retval;
3953}
3954
3955static void
3956check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
3957{
3958 struct usb_qualifier_descriptor *qual;
3959 int status;
3960
Christoph Lametere94b1762006-12-06 20:33:17 -08003961 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 if (qual == NULL)
3963 return;
3964
3965 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
3966 qual, sizeof *qual);
3967 if (status == sizeof *qual) {
3968 dev_info(&udev->dev, "not running at top speed; "
3969 "connect to a high speed hub\n");
3970 /* hub LEDs are probably harder to miss than syslog */
3971 if (hub->has_indicators) {
3972 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003973 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 }
3975 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003976 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977}
3978
3979static unsigned
3980hub_power_remaining (struct usb_hub *hub)
3981{
3982 struct usb_device *hdev = hub->hdev;
3983 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05003984 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Alan Stern55c52712005-11-23 12:03:12 -05003986 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 return 0;
3988
Alan Stern55c52712005-11-23 12:03:12 -05003989 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
3990 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003991 struct usb_device *udev = hdev->children[port1 - 1];
Alan Stern55c52712005-11-23 12:03:12 -05003992 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994 if (!udev)
3995 continue;
3996
Alan Stern55c52712005-11-23 12:03:12 -05003997 /* Unconfigured devices may not use more than 100mA,
3998 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004000 delta = udev->actconfig->desc.bMaxPower * 2;
4001 else if (port1 != udev->bus->otg_port || hdev->parent)
4002 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 else
Alan Stern55c52712005-11-23 12:03:12 -05004004 delta = 8;
4005 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004006 dev_warn(&udev->dev,
4007 "%dmA is over %umA budget for port %d!\n",
4008 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 remaining -= delta;
4010 }
4011 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004012 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4013 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 remaining = 0;
4015 }
4016 return remaining;
4017}
4018
4019/* Handle physical or logical connection change events.
4020 * This routine is called when:
4021 * a port connection-change occurs;
4022 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004023 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 * a firmware download)
4025 * caller already locked the hub
4026 */
4027static void hub_port_connect_change(struct usb_hub *hub, int port1,
4028 u16 portstatus, u16 portchange)
4029{
4030 struct usb_device *hdev = hub->hdev;
4031 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304032 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004033 unsigned wHubCharacteristics =
4034 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004035 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004037
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 dev_dbg (hub_dev,
4039 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004040 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
4042 if (hub->has_indicators) {
4043 set_port_led(hub, port1, HUB_LED_AUTO);
4044 hub->indicator[port1-1] = INDICATOR_AUTO;
4045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046
4047#ifdef CONFIG_USB_OTG
4048 /* during HNP, don't repeat the debounce */
4049 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004050 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4051 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052#endif
4053
Alan Stern8808f002008-04-28 11:06:55 -04004054 /* Try to resuscitate an existing device */
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004055 udev = hdev->children[port1-1];
Alan Stern8808f002008-04-28 11:06:55 -04004056 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4057 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004058 usb_lock_device(udev);
4059 if (portstatus & USB_PORT_STAT_ENABLE) {
4060 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004061
4062#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004063 } else if (udev->state == USB_STATE_SUSPENDED &&
4064 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004065 /* For a suspended device, treat this as a
4066 * remote wakeup event.
4067 */
Alan Stern0534d462010-01-08 12:56:30 -05004068 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004069#endif
4070
4071 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004072 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004073 }
4074 usb_unlock_device(udev);
4075
4076 if (status == 0) {
4077 clear_bit(port1, hub->change_bits);
4078 return;
4079 }
4080 }
4081
Alan Stern24618b02008-04-28 11:06:28 -04004082 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004083 if (udev)
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004084 usb_disconnect(&hdev->children[port1-1]);
Alan Stern24618b02008-04-28 11:06:28 -04004085 clear_bit(port1, hub->change_bits);
4086
Alan Stern253e0572009-10-27 15:20:13 -04004087 /* We can forget about a "removed" device when there's a physical
4088 * disconnect or the connect status changes.
4089 */
4090 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4091 (portchange & USB_PORT_STAT_C_CONNECTION))
4092 clear_bit(port1, hub->removed_bits);
4093
Alan Stern5257d972008-09-22 14:43:08 -04004094 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4095 USB_PORT_STAT_C_ENABLE)) {
4096 status = hub_port_debounce(hub, port1);
4097 if (status < 0) {
4098 if (printk_ratelimit())
4099 dev_err(hub_dev, "connect-debounce failed, "
4100 "port %d disabled\n", port1);
4101 portstatus &= ~USB_PORT_STAT_CONNECTION;
4102 } else {
4103 portstatus = status;
4104 }
4105 }
4106
Alan Stern253e0572009-10-27 15:20:13 -04004107 /* Return now if debouncing failed or nothing is connected or
4108 * the device was "removed".
4109 */
4110 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4111 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
4113 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004114 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004115 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (portstatus & USB_PORT_STAT_ENABLE)
4119 goto done;
4120 return;
4121 }
4122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
4125 /* reallocate for each attempt, since references
4126 * to the previous one can escape in various ways
4127 */
4128 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4129 if (!udev) {
4130 dev_err (hub_dev,
4131 "couldn't allocate port %d usb_device\n",
4132 port1);
4133 goto done;
4134 }
4135
4136 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004137 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004138 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004139 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004140
Sarah Sharp131dec32010-12-06 21:00:19 -08004141 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4142 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004143 udev->speed = USB_SPEED_SUPER;
4144 else
4145 udev->speed = USB_SPEED_UNKNOWN;
4146
Alan Stern3b29b682011-02-22 09:53:41 -05004147 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004148 if (udev->devnum <= 0) {
4149 status = -ENOTCONN; /* Don't retry */
4150 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004151 }
4152
Sarah Sharpe7b77172009-04-27 19:54:26 -07004153 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 status = hub_port_init(hub, udev, port1, i);
4155 if (status < 0)
4156 goto loop;
4157
Phil Dibowitz93362a82010-07-22 00:05:01 +02004158 usb_detect_quirks(udev);
4159 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4160 msleep(1000);
4161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 /* consecutive bus-powered hubs aren't reliable; they can
4163 * violate the voltage drop budget. if the new child has
4164 * a "powered" LED, users should notice we didn't enable it
4165 * (without reading syslog), even without per-port LEDs
4166 * on the parent.
4167 */
4168 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004169 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 u16 devstat;
4171
4172 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4173 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004174 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 dev_dbg(&udev->dev, "get status %d ?\n", status);
4176 goto loop_disable;
4177 }
Alan Stern55c52712005-11-23 12:03:12 -05004178 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4180 dev_err(&udev->dev,
4181 "can't connect bus-powered hub "
4182 "to this port\n");
4183 if (hub->has_indicators) {
4184 hub->indicator[port1-1] =
4185 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004186 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 }
4188 status = -ENOTCONN; /* Don't retry */
4189 goto loop_disable;
4190 }
4191 }
4192
4193 /* check for devices running slower than they could */
4194 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4195 && udev->speed == USB_SPEED_FULL
4196 && highspeed_hubs != 0)
4197 check_highspeed (hub, udev, port1);
4198
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004199 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 * udev becomes globally accessible, although presumably
4201 * no one will look at it until hdev is unlocked.
4202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 status = 0;
4204
4205 /* We mustn't add new devices if the parent hub has
4206 * been disconnected; we would race with the
4207 * recursively_mark_NOTATTACHED() routine.
4208 */
4209 spin_lock_irq(&device_state_lock);
4210 if (hdev->state == USB_STATE_NOTATTACHED)
4211 status = -ENOTCONN;
4212 else
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004213 hdev->children[port1-1] = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 spin_unlock_irq(&device_state_lock);
4215
4216 /* Run it through the hoops (find a driver, etc) */
4217 if (!status) {
4218 status = usb_new_device(udev);
4219 if (status) {
4220 spin_lock_irq(&device_state_lock);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004221 hdev->children[port1-1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 spin_unlock_irq(&device_state_lock);
4223 }
4224 }
4225
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 if (status)
4227 goto loop_disable;
4228
4229 status = hub_power_remaining(hub);
4230 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004231 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
4233 return;
4234
4235loop_disable:
4236 hub_port_disable(hub, port1, 1);
4237loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004238 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004239 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004240 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004242 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 break;
4244 }
Alan Stern3a311552008-05-20 16:58:29 -04004245 if (hub->hdev->parent ||
4246 !hcd->driver->port_handed_over ||
4247 !(hcd->driver->port_handed_over)(hcd, port1))
4248 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4249 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251done:
4252 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304253 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4254 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
Sarah Sharp714b07b2012-01-24 13:53:18 -08004257/* Returns 1 if there was a remote wakeup and a connect status change. */
4258static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004259 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004260{
4261 struct usb_device *hdev;
4262 struct usb_device *udev;
4263 int connect_change = 0;
4264 int ret;
4265
4266 hdev = hub->hdev;
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004267 udev = hdev->children[port-1];
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004268 if (!hub_is_superspeed(hdev)) {
4269 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4270 return 0;
4271 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4272 } else {
4273 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004274 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4275 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004276 return 0;
4277 }
4278
Sarah Sharp714b07b2012-01-24 13:53:18 -08004279 if (udev) {
4280 /* TRSMRCY = 10 msec */
4281 msleep(10);
4282
4283 usb_lock_device(udev);
4284 ret = usb_remote_wakeup(udev);
4285 usb_unlock_device(udev);
4286 if (ret < 0)
4287 connect_change = 1;
4288 } else {
4289 ret = -ENODEV;
4290 hub_port_disable(hub, port, 1);
4291 }
4292 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4293 port, ret);
4294 return connect_change;
4295}
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297static void hub_events(void)
4298{
4299 struct list_head *tmp;
4300 struct usb_device *hdev;
4301 struct usb_interface *intf;
4302 struct usb_hub *hub;
4303 struct device *hub_dev;
4304 u16 hubstatus;
4305 u16 hubchange;
4306 u16 portstatus;
4307 u16 portchange;
4308 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004309 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 /*
4312 * We restart the list every time to avoid a deadlock with
4313 * deleting hubs downstream from this one. This should be
4314 * safe since we delete the hub from the event list.
4315 * Not the most efficient, but avoids deadlocks.
4316 */
4317 while (1) {
4318
4319 /* Grab the first entry at the beginning of the list */
4320 spin_lock_irq(&hub_event_lock);
4321 if (list_empty(&hub_event_list)) {
4322 spin_unlock_irq(&hub_event_lock);
4323 break;
4324 }
4325
4326 tmp = hub_event_list.next;
4327 list_del_init(tmp);
4328
4329 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004330 kref_get(&hub->kref);
4331 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Alan Sterne8054852007-05-04 11:55:11 -04004333 hdev = hub->hdev;
4334 hub_dev = hub->intfdev;
4335 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004336 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 hdev->state, hub->descriptor
4338 ? hub->descriptor->bNbrPorts
4339 : 0,
4340 /* NOTE: expects max 15 ports... */
4341 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004342 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 /* Lock the device, then check to see if we were
4345 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004346 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004347 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004348 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 /* If the hub has died, clean up after it */
4351 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004352 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004353 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 goto loop;
4355 }
4356
Alan Stern40f122f2006-11-09 14:44:33 -05004357 /* Autoresume */
4358 ret = usb_autopm_get_interface(intf);
4359 if (ret) {
4360 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004362 }
4363
4364 /* If this is an inactive hub, do nothing */
4365 if (hub->quiescing)
4366 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368 if (hub->error) {
4369 dev_dbg (hub_dev, "resetting for error %d\n",
4370 hub->error);
4371
Ming Lei742120c2008-06-18 22:00:29 +08004372 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 if (ret) {
4374 dev_dbg (hub_dev,
4375 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004376 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 }
4378
4379 hub->nerrors = 0;
4380 hub->error = 0;
4381 }
4382
4383 /* deal with port status changes */
4384 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4385 if (test_bit(i, hub->busy_bits))
4386 continue;
4387 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004388 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004390 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 continue;
4392
4393 ret = hub_port_status(hub, i,
4394 &portstatus, &portchange);
4395 if (ret < 0)
4396 continue;
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4399 clear_port_feature(hdev, i,
4400 USB_PORT_FEAT_C_CONNECTION);
4401 connect_change = 1;
4402 }
4403
4404 if (portchange & USB_PORT_STAT_C_ENABLE) {
4405 if (!connect_change)
4406 dev_dbg (hub_dev,
4407 "port %d enable change, "
4408 "status %08x\n",
4409 i, portstatus);
4410 clear_port_feature(hdev, i,
4411 USB_PORT_FEAT_C_ENABLE);
4412
4413 /*
4414 * EM interference sometimes causes badly
4415 * shielded USB devices to be shutdown by
4416 * the hub, this hack enables them again.
4417 * Works at least with mouse driver.
4418 */
4419 if (!(portstatus & USB_PORT_STAT_ENABLE)
4420 && !connect_change
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004421 && hdev->children[i-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 dev_err (hub_dev,
4423 "port %i "
4424 "disabled by hub (EMI?), "
4425 "re-enabling...\n",
4426 i);
4427 connect_change = 1;
4428 }
4429 }
4430
Sarah Sharp72937e12012-01-24 11:46:50 -08004431 if (hub_handle_remote_wakeup(hub, i,
4432 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004433 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004436 u16 status = 0;
4437 u16 unused;
4438
4439 dev_dbg(hub_dev, "over-current change on port "
4440 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 clear_port_feature(hdev, i,
4442 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004443 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004444 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004445 hub_port_status(hub, i, &status, &unused);
4446 if (status & USB_PORT_STAT_OVERCURRENT)
4447 dev_err(hub_dev, "over-current "
4448 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 }
4450
4451 if (portchange & USB_PORT_STAT_C_RESET) {
4452 dev_dbg (hub_dev,
4453 "reset change on port %d\n",
4454 i);
4455 clear_port_feature(hdev, i,
4456 USB_PORT_FEAT_C_RESET);
4457 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004458 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4459 hub_is_superspeed(hub->hdev)) {
4460 dev_dbg(hub_dev,
4461 "warm reset change on port %d\n",
4462 i);
4463 clear_port_feature(hdev, i,
4464 USB_PORT_FEAT_C_BH_PORT_RESET);
4465 }
John Youndbe79bb2001-09-17 00:00:00 -07004466 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4467 clear_port_feature(hub->hdev, i,
4468 USB_PORT_FEAT_C_PORT_LINK_STATE);
4469 }
4470 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4471 dev_warn(hub_dev,
4472 "config error on port %d\n",
4473 i);
4474 clear_port_feature(hub->hdev, i,
4475 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Andiry Xu5e467f62011-04-27 18:07:54 +08004478 /* Warm reset a USB3 protocol port if it's in
4479 * SS.Inactive state.
4480 */
4481 if (hub_is_superspeed(hub->hdev) &&
4482 (portstatus & USB_PORT_STAT_LINK_STATE)
4483 == USB_SS_PORT_LS_SS_INACTIVE) {
4484 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004485 hub_port_reset(hub, i, NULL,
4486 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004487 }
4488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 if (connect_change)
4490 hub_port_connect_change(hub, i,
4491 portstatus, portchange);
4492 } /* end for i */
4493
4494 /* deal with hub status changes */
4495 if (test_and_clear_bit(0, hub->event_bits) == 0)
4496 ; /* do nothing */
4497 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4498 dev_err (hub_dev, "get_hub_status failed\n");
4499 else {
4500 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4501 dev_dbg (hub_dev, "power change\n");
4502 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004503 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4504 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004505 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004506 else
4507 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 }
4509 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004510 u16 status = 0;
4511 u16 unused;
4512
4513 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004515 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004516 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004517 hub_hub_status(hub, &status, &unused);
4518 if (status & HUB_STATUS_OVERCURRENT)
4519 dev_err(hub_dev, "over-current "
4520 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 }
4522 }
4523
Alan Stern8e4ceb32009-12-07 13:01:37 -05004524 loop_autopm:
4525 /* Balance the usb_autopm_get_interface() above */
4526 usb_autopm_put_interface_no_suspend(intf);
4527 loop:
4528 /* Balance the usb_autopm_get_interface_no_resume() in
4529 * kick_khubd() and allow autosuspend.
4530 */
4531 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004532 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004534 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
4536 } /* end while (1) */
4537}
4538
4539static int hub_thread(void *__unused)
4540{
Alan Stern3bb1af52008-03-03 15:15:36 -05004541 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4542 * port handover. Otherwise it might see that a full-speed device
4543 * was gone before the EHCI controller had handed its port over to
4544 * the companion full-speed controller.
4545 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004546 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 do {
4549 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004550 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004551 !list_empty(&hub_event_list) ||
4552 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004553 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004555 pr_debug("%s: khubd exiting\n", usbcore_name);
4556 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557}
4558
Németh Márton1e927d92010-01-10 15:34:53 +01004559static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4561 .bDeviceClass = USB_CLASS_HUB},
4562 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4563 .bInterfaceClass = USB_CLASS_HUB},
4564 { } /* Terminating entry */
4565};
4566
4567MODULE_DEVICE_TABLE (usb, hub_id_table);
4568
4569static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 .name = "hub",
4571 .probe = hub_probe,
4572 .disconnect = hub_disconnect,
4573 .suspend = hub_suspend,
4574 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004575 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004576 .pre_reset = hub_pre_reset,
4577 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004578 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004580 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581};
4582
4583int usb_hub_init(void)
4584{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 if (usb_register(&hub_driver) < 0) {
4586 printk(KERN_ERR "%s: can't register hub driver\n",
4587 usbcore_name);
4588 return -1;
4589 }
4590
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004591 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4592 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
4595 /* Fall through if kernel_thread failed */
4596 usb_deregister(&hub_driver);
4597 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4598
4599 return -1;
4600}
4601
4602void usb_hub_cleanup(void)
4603{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004604 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 /*
4607 * Hub resources are freed for us by usb_deregister. It calls
4608 * usb_driver_purge on every device which in turn calls that
4609 * devices disconnect function if it is using this driver.
4610 * The hub_disconnect function takes care of releasing the
4611 * individual hub resources. -greg
4612 */
4613 usb_deregister(&hub_driver);
4614} /* usb_hub_cleanup() */
4615
Alan Sterneb764c42008-03-03 15:16:04 -05004616static int descriptors_changed(struct usb_device *udev,
4617 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
Alan Sterneb764c42008-03-03 15:16:04 -05004619 int changed = 0;
4620 unsigned index;
4621 unsigned serial_len = 0;
4622 unsigned len;
4623 unsigned old_length;
4624 int length;
4625 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
Alan Sterneb764c42008-03-03 15:16:04 -05004627 if (memcmp(&udev->descriptor, old_device_descriptor,
4628 sizeof(*old_device_descriptor)) != 0)
4629 return 1;
4630
4631 /* Since the idVendor, idProduct, and bcdDevice values in the
4632 * device descriptor haven't changed, we will assume the
4633 * Manufacturer and Product strings haven't changed either.
4634 * But the SerialNumber string could be different (e.g., a
4635 * different flash card of the same brand).
4636 */
4637 if (udev->serial)
4638 serial_len = strlen(udev->serial) + 1;
4639
4640 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004642 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4643 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
Alan Sterneb764c42008-03-03 15:16:04 -05004645
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004646 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 if (buf == NULL) {
4648 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4649 /* assume the worst */
4650 return 1;
4651 }
4652 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004653 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4655 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004656 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 dev_dbg(&udev->dev, "config index %d, error %d\n",
4658 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004659 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 break;
4661 }
4662 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4663 != 0) {
4664 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004665 index,
4666 ((struct usb_config_descriptor *) buf)->
4667 bConfigurationValue);
4668 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 break;
4670 }
4671 }
Alan Sterneb764c42008-03-03 15:16:04 -05004672
4673 if (!changed && serial_len) {
4674 length = usb_string(udev, udev->descriptor.iSerialNumber,
4675 buf, serial_len);
4676 if (length + 1 != serial_len) {
4677 dev_dbg(&udev->dev, "serial string error %d\n",
4678 length);
4679 changed = 1;
4680 } else if (memcmp(buf, udev->serial, length) != 0) {
4681 dev_dbg(&udev->dev, "serial string changed\n");
4682 changed = 1;
4683 }
4684 }
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004687 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688}
4689
4690/**
Ming Lei742120c2008-06-18 22:00:29 +08004691 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4693 *
Alan Stern79efa092006-06-01 13:33:42 -04004694 * WARNING - don't use this routine to reset a composite device
4695 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004696 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 *
4698 * Do a port reset, reassign the device's address, and establish its
4699 * former operating configuration. If the reset fails, or the device's
4700 * descriptors change from their values before the reset, or the original
4701 * configuration and altsettings cannot be restored, a flag will be set
4702 * telling khubd to pretend the device has been disconnected and then
4703 * re-connected. All drivers will be unbound, and the device will be
4704 * re-enumerated and probed all over again.
4705 *
4706 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4707 * flagged for logical disconnection, or some other negative error code
4708 * if the reset wasn't even attempted.
4709 *
4710 * The caller must own the device lock. For example, it's safe to use
4711 * this from a driver probe() routine after downloading new firmware.
4712 * For calls that might not occur during probe(), drivers should lock
4713 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004714 *
4715 * Locking exception: This routine may also be called from within an
4716 * autoresume handler. Such usage won't conflict with other tasks
4717 * holding the device lock because these tasks should always call
4718 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 */
Ming Lei742120c2008-06-18 22:00:29 +08004720static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721{
4722 struct usb_device *parent_hdev = udev->parent;
4723 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004724 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004726 int i, ret = 0;
4727 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
4729 if (udev->state == USB_STATE_NOTATTACHED ||
4730 udev->state == USB_STATE_SUSPENDED) {
4731 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4732 udev->state);
4733 return -EINVAL;
4734 }
4735
4736 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004737 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004738 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 return -EISDIR;
4740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 parent_hub = hdev_to_hub(parent_hdev);
4742
Sarah Sharpf74631e2012-06-25 12:08:08 -07004743 /* Disable LPM and LTM while we reset the device and reinstall the alt
4744 * settings. Device-initiated LPM settings, and system exit latency
4745 * settings are cleared when the device is reset, so we have to set
4746 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004747 */
4748 ret = usb_unlocked_disable_lpm(udev);
4749 if (ret) {
4750 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4751 goto re_enumerate;
4752 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004753 ret = usb_disable_ltm(udev);
4754 if (ret) {
4755 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4756 __func__);
4757 goto re_enumerate;
4758 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 set_bit(port1, parent_hub->busy_bits);
4761 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4762
4763 /* ep0 maxpacket size may change; let the HCD know about it.
4764 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004765 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004767 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 break;
4769 }
4770 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 if (ret < 0)
4773 goto re_enumerate;
4774
4775 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004776 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 dev_info(&udev->dev, "device firmware changed\n");
4778 udev->descriptor = descriptor; /* for disconnect() calls */
4779 goto re_enumerate;
4780 }
Alan Stern4fe03872009-02-26 10:21:02 -05004781
4782 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 if (!udev->actconfig)
4784 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004785
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004786 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004787 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4788 if (ret < 0) {
4789 dev_warn(&udev->dev,
4790 "Busted HC? Not enough HCD resources for "
4791 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004792 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004793 goto re_enumerate;
4794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4796 USB_REQ_SET_CONFIGURATION, 0,
4797 udev->actconfig->desc.bConfigurationValue, 0,
4798 NULL, 0, USB_CTRL_SET_TIMEOUT);
4799 if (ret < 0) {
4800 dev_err(&udev->dev,
4801 "can't restore configuration #%d (error=%d)\n",
4802 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004803 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 goto re_enumerate;
4805 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004806 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4808
Alan Stern4fe03872009-02-26 10:21:02 -05004809 /* Put interfaces back into the same altsettings as before.
4810 * Don't bother to send the Set-Interface request for interfaces
4811 * that were already in altsetting 0; besides being unnecessary,
4812 * many devices can't handle it. Instead just reset the host-side
4813 * endpoint state.
4814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004816 struct usb_host_config *config = udev->actconfig;
4817 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 struct usb_interface_descriptor *desc;
4819
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004821 if (desc->bAlternateSetting == 0) {
4822 usb_disable_interface(udev, intf, true);
4823 usb_enable_interface(udev, intf, true);
4824 ret = 0;
4825 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004826 /* Let the bandwidth allocation function know that this
4827 * device has been reset, and it will have to use
4828 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004829 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004830 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004831 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4832 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004833 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 if (ret < 0) {
4836 dev_err(&udev->dev, "failed to restore interface %d "
4837 "altsetting %d (error=%d)\n",
4838 desc->bInterfaceNumber,
4839 desc->bAlternateSetting,
4840 ret);
4841 goto re_enumerate;
4842 }
4843 }
4844
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004845done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07004846 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Sarah Sharp83060952012-05-02 14:25:52 -07004847 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004848 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 return 0;
4850
4851re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004852 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 hub_port_logical_disconnect(parent_hub, port1);
4854 return -ENODEV;
4855}
Alan Stern79efa092006-06-01 13:33:42 -04004856
4857/**
Ming Lei742120c2008-06-18 22:00:29 +08004858 * usb_reset_device - warn interface drivers and perform a USB port reset
Alan Stern79efa092006-06-01 13:33:42 -04004859 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
Alan Stern79efa092006-06-01 13:33:42 -04004860 *
4861 * Warns all drivers bound to registered interfaces (using their pre_reset
4862 * method), performs the port reset, and then lets the drivers know that
4863 * the reset is over (using their post_reset method).
4864 *
Ming Lei742120c2008-06-18 22:00:29 +08004865 * Return value is the same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04004866 *
4867 * The caller must own the device lock. For example, it's safe to use
4868 * this from a driver probe() routine after downloading new firmware.
4869 * For calls that might not occur during probe(), drivers should lock
4870 * the device using usb_lock_device_for_reset().
Alan Stern78d9a482008-06-23 16:00:40 -04004871 *
4872 * If an interface is currently being probed or disconnected, we assume
4873 * its driver knows how to handle resets. For all other interfaces,
4874 * if the driver doesn't have pre_reset and post_reset methods then
4875 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004876 */
Ming Lei742120c2008-06-18 22:00:29 +08004877int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004878{
4879 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004880 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004881 struct usb_host_config *config = udev->actconfig;
4882
4883 if (udev->state == USB_STATE_NOTATTACHED ||
4884 udev->state == USB_STATE_SUSPENDED) {
4885 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4886 udev->state);
4887 return -EINVAL;
4888 }
4889
Alan Stern645daaa2006-08-30 15:47:02 -04004890 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004891 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004892
Alan Stern79efa092006-06-01 13:33:42 -04004893 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004894 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004895 struct usb_interface *cintf = config->interface[i];
4896 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004897 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004898
4899 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004900 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004901 if (drv->pre_reset && drv->post_reset)
4902 unbind = (drv->pre_reset)(cintf);
4903 else if (cintf->condition ==
4904 USB_INTERFACE_BOUND)
4905 unbind = 1;
4906 if (unbind)
4907 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004908 }
4909 }
4910 }
4911
Ming Lei742120c2008-06-18 22:00:29 +08004912 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004913
4914 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004915 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004916 struct usb_interface *cintf = config->interface[i];
4917 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004918 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004919
Alan Stern78d9a482008-06-23 16:00:40 -04004920 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004921 drv = to_usb_driver(cintf->dev.driver);
4922 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004923 rebind = (drv->post_reset)(cintf);
4924 else if (cintf->condition ==
4925 USB_INTERFACE_BOUND)
4926 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04004927 }
Alan Stern6c640942008-10-21 15:40:03 -04004928 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04004929 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004930 }
4931 }
4932
Alan Stern94fcda12006-11-20 11:38:46 -05004933 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004934 return ret;
4935}
Ming Lei742120c2008-06-18 22:00:29 +08004936EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08004937
4938
4939/**
4940 * usb_queue_reset_device - Reset a USB device from an atomic context
4941 * @iface: USB interface belonging to the device to reset
4942 *
4943 * This function can be used to reset a USB device from an atomic
4944 * context, where usb_reset_device() won't work (as it blocks).
4945 *
4946 * Doing a reset via this method is functionally equivalent to calling
4947 * usb_reset_device(), except for the fact that it is delayed to a
4948 * workqueue. This means that any drivers bound to other interfaces
4949 * might be unbound, as well as users from usbfs in user space.
4950 *
4951 * Corner cases:
4952 *
4953 * - Scheduling two resets at the same time from two different drivers
4954 * attached to two different interfaces of the same device is
4955 * possible; depending on how the driver attached to each interface
4956 * handles ->pre_reset(), the second reset might happen or not.
4957 *
4958 * - If a driver is unbound and it had a pending reset, the reset will
4959 * be cancelled.
4960 *
4961 * - This function can be called during .probe() or .disconnect()
4962 * times. On return from .disconnect(), any pending resets will be
4963 * cancelled.
4964 *
4965 * There is no no need to lock/unlock the @reset_ws as schedule_work()
4966 * does its own.
4967 *
4968 * NOTE: We don't do any reference count tracking because it is not
4969 * needed. The lifecycle of the work_struct is tied to the
4970 * usb_interface. Before destroying the interface we cancel the
4971 * work_struct, so the fact that work_struct is queued and or
4972 * running means the interface (and thus, the device) exist and
4973 * are referenced.
4974 */
4975void usb_queue_reset_device(struct usb_interface *iface)
4976{
4977 schedule_work(&iface->reset_ws);
4978}
4979EXPORT_SYMBOL_GPL(usb_queue_reset_device);