blob: 5219507bf22702225c8f1ba8da9369d4383a202d [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;
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -070084 void **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);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001274 hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
1275 if (!hdev->children || !hub->port_owners) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001276 ret = -ENOMEM;
1277 goto fail;
1278 }
1279
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001280 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
John Youndbe79bb2001-09-17 00:00:00 -07001282 /* FIXME for USB 3.0, skip for now */
1283 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1284 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int i;
1286 char portstr [USB_MAXCHILDREN + 1];
1287
1288 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001289 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1291 ? 'F' : 'R';
1292 portstr[hdev->maxchild] = 0;
1293 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1294 } else
1295 dev_dbg(hub_dev, "standalone hub\n");
1296
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001297 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301298 case HUB_CHAR_COMMON_LPSM:
1299 dev_dbg(hub_dev, "ganged power switching\n");
1300 break;
1301 case HUB_CHAR_INDV_PORT_LPSM:
1302 dev_dbg(hub_dev, "individual port power switching\n");
1303 break;
1304 case HUB_CHAR_NO_LPSM:
1305 case HUB_CHAR_LPSM:
1306 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001310 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301311 case HUB_CHAR_COMMON_OCPM:
1312 dev_dbg(hub_dev, "global over-current protection\n");
1313 break;
1314 case HUB_CHAR_INDV_PORT_OCPM:
1315 dev_dbg(hub_dev, "individual port over-current protection\n");
1316 break;
1317 case HUB_CHAR_NO_OCPM:
1318 case HUB_CHAR_OCPM:
1319 dev_dbg(hub_dev, "no over-current protection\n");
1320 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322
1323 spin_lock_init (&hub->tt.lock);
1324 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001325 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301327 case USB_HUB_PR_FS:
1328 break;
1329 case USB_HUB_PR_HS_SINGLE_TT:
1330 dev_dbg(hub_dev, "Single TT\n");
1331 hub->tt.hub = hdev;
1332 break;
1333 case USB_HUB_PR_HS_MULTI_TT:
1334 ret = usb_set_interface(hdev, 0, 1);
1335 if (ret == 0) {
1336 dev_dbg(hub_dev, "TT per port\n");
1337 hub->tt.multi = 1;
1338 } else
1339 dev_err(hub_dev, "Using single TT (err %d)\n",
1340 ret);
1341 hub->tt.hub = hdev;
1342 break;
1343 case USB_HUB_PR_SS:
1344 /* USB 3.0 hubs don't have a TT */
1345 break;
1346 default:
1347 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1348 hdev->descriptor.bDeviceProtocol);
1349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001352 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001353 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001354 case HUB_TTTT_8_BITS:
1355 if (hdev->descriptor.bDeviceProtocol != 0) {
1356 hub->tt.think_time = 666;
1357 dev_dbg(hub_dev, "TT requires at most %d "
1358 "FS bit times (%d ns)\n",
1359 8, hub->tt.think_time);
1360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001362 case HUB_TTTT_16_BITS:
1363 hub->tt.think_time = 666 * 2;
1364 dev_dbg(hub_dev, "TT requires at most %d "
1365 "FS bit times (%d ns)\n",
1366 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001368 case HUB_TTTT_24_BITS:
1369 hub->tt.think_time = 666 * 3;
1370 dev_dbg(hub_dev, "TT requires at most %d "
1371 "FS bit times (%d ns)\n",
1372 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001374 case HUB_TTTT_32_BITS:
1375 hub->tt.think_time = 666 * 4;
1376 dev_dbg(hub_dev, "TT requires at most %d "
1377 "FS bit times (%d ns)\n",
1378 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 break;
1380 }
1381
1382 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001383 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 hub->has_indicators = 1;
1385 dev_dbg(hub_dev, "Port indicators are supported\n");
1386 }
1387
1388 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1389 hub->descriptor->bPwrOn2PwrGood * 2);
1390
1391 /* power budgeting mostly matters with bus-powered hubs,
1392 * and battery-powered root hubs (may provide just 8 mA).
1393 */
1394 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001395 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 message = "can't get hub status";
1397 goto fail;
1398 }
Alan Stern7d35b922005-04-25 11:18:32 -04001399 le16_to_cpus(&hubstatus);
1400 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001401 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1402 hub->mA_per_port = 500;
1403 else {
1404 hub->mA_per_port = hdev->bus_mA;
1405 hub->limited_power = 1;
1406 }
Alan Stern7d35b922005-04-25 11:18:32 -04001407 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1409 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001410 hub->limited_power = 1;
1411 if (hdev->maxchild > 0) {
1412 int remaining = hdev->bus_mA -
1413 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Alan Stern55c52712005-11-23 12:03:12 -05001415 if (remaining < hdev->maxchild * 100)
1416 dev_warn(hub_dev,
1417 "insufficient power available "
1418 "to use all downstream ports\n");
1419 hub->mA_per_port = 100; /* 7.2.1.1 */
1420 }
1421 } else { /* Self-powered external hub */
1422 /* FIXME: What about battery-powered external hubs that
1423 * provide less current per port? */
1424 hub->mA_per_port = 500;
1425 }
1426 if (hub->mA_per_port < 500)
1427 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1428 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001430 /* Update the HCD's internal representation of this hub before khubd
1431 * starts getting port status changes for devices under the hub.
1432 */
1433 hcd = bus_to_hcd(hdev->bus);
1434 if (hcd->driver->update_hub_device) {
1435 ret = hcd->driver->update_hub_device(hcd, hdev,
1436 &hub->tt, GFP_KERNEL);
1437 if (ret < 0) {
1438 message = "can't update HCD hub info";
1439 goto fail;
1440 }
1441 }
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1444 if (ret < 0) {
1445 message = "can't get hub status";
1446 goto fail;
1447 }
1448
1449 /* local power status reports aren't always correct */
1450 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1451 dev_dbg(hub_dev, "local power source is %s\n",
1452 (hubstatus & HUB_STATUS_LOCAL_POWER)
1453 ? "lost (inactive)" : "good");
1454
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001455 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 dev_dbg(hub_dev, "%sover-current condition exists\n",
1457 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1458
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001459 /* set up the interrupt endpoint
1460 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1461 * bytes as USB2.0[11.12.3] says because some hubs are known
1462 * to send more data (and thus cause overflow). For root hubs,
1463 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1464 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1466 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1467
1468 if (maxp > sizeof(*hub->buffer))
1469 maxp = sizeof(*hub->buffer);
1470
1471 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1472 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 ret = -ENOMEM;
1474 goto fail;
1475 }
1476
1477 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1478 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 /* maybe cycle the hub leds */
1481 if (hub->has_indicators && blinkenlights)
1482 hub->indicator [0] = INDICATOR_CYCLE;
1483
Alan Sternf2835212008-04-28 11:07:17 -04001484 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return 0;
1486
1487fail:
1488 dev_err (hub_dev, "config failed, %s (err %d)\n",
1489 message, ret);
1490 /* hub_disconnect() frees urb and descriptor */
1491 return ret;
1492}
1493
Alan Sterne8054852007-05-04 11:55:11 -04001494static void hub_release(struct kref *kref)
1495{
1496 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1497
1498 usb_put_intf(to_usb_interface(hub->intfdev));
1499 kfree(hub);
1500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502static unsigned highspeed_hubs;
1503
1504static void hub_disconnect(struct usb_interface *intf)
1505{
Huajun Li88162302012-03-12 21:00:19 +08001506 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001507 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Sterne8054852007-05-04 11:55:11 -04001508
1509 /* Take the hub off the event list and don't let it be added again */
1510 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001511 if (!list_empty(&hub->event_list)) {
1512 list_del_init(&hub->event_list);
1513 usb_autopm_put_interface_no_suspend(intf);
1514 }
Alan Sterne8054852007-05-04 11:55:11 -04001515 hub->disconnected = 1;
1516 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Alan Stern7de18d82006-06-01 13:37:24 -04001518 /* Disconnect all children and quiesce the hub */
1519 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001520 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001521
Alan Stern8b28c752005-08-10 17:04:13 -04001522 usb_set_intfdata (intf, NULL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001523 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Alan Sterne8054852007-05-04 11:55:11 -04001525 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 highspeed_hubs--;
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 usb_free_urb(hub->urb);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001529 kfree(hdev->children);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001530 kfree(hub->port_owners);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001531 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001532 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001533 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Alan Sterne8054852007-05-04 11:55:11 -04001535 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
1538static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1539{
1540 struct usb_host_interface *desc;
1541 struct usb_endpoint_descriptor *endpoint;
1542 struct usb_device *hdev;
1543 struct usb_hub *hub;
1544
1545 desc = intf->cur_altsetting;
1546 hdev = interface_to_usbdev(intf);
1547
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001548 /* Hubs have proper suspend/resume support. */
1549 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001550
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001551 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001552 dev_err(&intf->dev,
1553 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001554 return -E2BIG;
1555 }
1556
David Brownell89ccbdc2006-04-02 10:18:09 -08001557#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1558 if (hdev->parent) {
1559 dev_warn(&intf->dev, "ignoring external hub\n");
1560 return -ENODEV;
1561 }
1562#endif
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* Some hubs have a subclass of 1, which AFAICT according to the */
1565 /* specs is not defined, but it works */
1566 if ((desc->desc.bInterfaceSubClass != 0) &&
1567 (desc->desc.bInterfaceSubClass != 1)) {
1568descriptor_error:
1569 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1570 return -EIO;
1571 }
1572
1573 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1574 if (desc->desc.bNumEndpoints != 1)
1575 goto descriptor_error;
1576
1577 endpoint = &desc->endpoint[0].desc;
1578
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001579 /* If it's not an interrupt in endpoint, we'd better punt! */
1580 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 goto descriptor_error;
1582
1583 /* We found a hub */
1584 dev_info (&intf->dev, "USB hub found\n");
1585
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001586 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (!hub) {
1588 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1589 return -ENOMEM;
1590 }
1591
Alan Sterne8054852007-05-04 11:55:11 -04001592 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 INIT_LIST_HEAD(&hub->event_list);
1594 hub->intfdev = &intf->dev;
1595 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001596 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001597 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001598 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001601 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 if (hdev->speed == USB_SPEED_HIGH)
1604 highspeed_hubs++;
1605
1606 if (hub_configure(hub, endpoint) >= 0)
1607 return 0;
1608
1609 hub_disconnect (intf);
1610 return -ENODEV;
1611}
1612
1613static int
1614hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1615{
1616 struct usb_device *hdev = interface_to_usbdev (intf);
1617
1618 /* assert ifno == 0 (part of hub spec) */
1619 switch (code) {
1620 case USBDEVFS_HUB_PORTINFO: {
1621 struct usbdevfs_hub_portinfo *info = user_data;
1622 int i;
1623
1624 spin_lock_irq(&device_state_lock);
1625 if (hdev->devnum <= 0)
1626 info->nports = 0;
1627 else {
1628 info->nports = hdev->maxchild;
1629 for (i = 0; i < info->nports; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001630 if (hdev->children[i] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 info->port[i] = 0;
1632 else
1633 info->port[i] =
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001634 hdev->children[i]->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636 }
1637 spin_unlock_irq(&device_state_lock);
1638
1639 return info->nports + 1;
1640 }
1641
1642 default:
1643 return -ENOSYS;
1644 }
1645}
1646
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001647/*
1648 * Allow user programs to claim ports on a hub. When a device is attached
1649 * to one of these "claimed" ports, the program will "own" the device.
1650 */
1651static int find_port_owner(struct usb_device *hdev, unsigned port1,
1652 void ***ppowner)
1653{
1654 if (hdev->state == USB_STATE_NOTATTACHED)
1655 return -ENODEV;
1656 if (port1 == 0 || port1 > hdev->maxchild)
1657 return -EINVAL;
1658
1659 /* This assumes that devices not managed by the hub driver
1660 * will always have maxchild equal to 0.
1661 */
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001662 *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001663 return 0;
1664}
1665
1666/* In the following three functions, the caller must hold hdev's lock */
1667int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
1668{
1669 int rc;
1670 void **powner;
1671
1672 rc = find_port_owner(hdev, port1, &powner);
1673 if (rc)
1674 return rc;
1675 if (*powner)
1676 return -EBUSY;
1677 *powner = owner;
1678 return rc;
1679}
1680
1681int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
1682{
1683 int rc;
1684 void **powner;
1685
1686 rc = find_port_owner(hdev, port1, &powner);
1687 if (rc)
1688 return rc;
1689 if (*powner != owner)
1690 return -ENOENT;
1691 *powner = NULL;
1692 return rc;
1693}
1694
1695void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
1696{
1697 int n;
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001698 void **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001699
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001700 n = find_port_owner(hdev, 1, &powner);
1701 if (n == 0) {
1702 for (; n < hdev->maxchild; (++n, ++powner)) {
1703 if (*powner == owner)
1704 *powner = NULL;
1705 }
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001706 }
1707}
1708
1709/* The caller must hold udev's lock */
1710bool usb_device_is_owned(struct usb_device *udev)
1711{
1712 struct usb_hub *hub;
1713
1714 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1715 return false;
1716 hub = hdev_to_hub(udev->parent);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001717 return !!hub->port_owners[udev->portnum - 1];
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001718}
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1722{
1723 int i;
1724
1725 for (i = 0; i < udev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001726 if (udev->children[i])
1727 recursively_mark_NOTATTACHED(udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001729 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001730 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 udev->state = USB_STATE_NOTATTACHED;
1732}
1733
1734/**
1735 * usb_set_device_state - change a device's current state (usbcore, hcds)
1736 * @udev: pointer to device whose state should be changed
1737 * @new_state: new state value to be stored
1738 *
1739 * udev->state is _not_ fully protected by the device lock. Although
1740 * most transitions are made only while holding the lock, the state can
1741 * can change to USB_STATE_NOTATTACHED at almost any time. This
1742 * is so that devices can be marked as disconnected as soon as possible,
1743 * without having to wait for any semaphores to be released. As a result,
1744 * all changes to any device's state must be protected by the
1745 * device_state_lock spinlock.
1746 *
1747 * Once a device has been added to the device tree, all changes to its state
1748 * should be made using this routine. The state should _not_ be set directly.
1749 *
1750 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1751 * Otherwise udev->state is set to new_state, and if new_state is
1752 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1753 * to USB_STATE_NOTATTACHED.
1754 */
1755void usb_set_device_state(struct usb_device *udev,
1756 enum usb_device_state new_state)
1757{
1758 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001759 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 spin_lock_irqsave(&device_state_lock, flags);
1762 if (udev->state == USB_STATE_NOTATTACHED)
1763 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001764 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001765
1766 /* root hub wakeup capabilities are managed out-of-band
1767 * and may involve silicon errata ... ignore them here.
1768 */
1769 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001770 if (udev->state == USB_STATE_SUSPENDED
1771 || new_state == USB_STATE_SUSPENDED)
1772 ; /* No change to wakeup settings */
1773 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001774 wakeup = udev->actconfig->desc.bmAttributes
1775 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001776 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001777 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001778 }
Sarah Sharp15123002007-12-21 16:54:15 -08001779 if (udev->state == USB_STATE_SUSPENDED &&
1780 new_state != USB_STATE_SUSPENDED)
1781 udev->active_duration -= jiffies;
1782 else if (new_state == USB_STATE_SUSPENDED &&
1783 udev->state != USB_STATE_SUSPENDED)
1784 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001785 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001786 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 recursively_mark_NOTATTACHED(udev);
1788 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001789 if (wakeup >= 0)
1790 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
David Vrabel6da9c992009-02-18 14:43:47 +00001792EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001794/*
Alan Stern3b29b682011-02-22 09:53:41 -05001795 * Choose a device number.
1796 *
1797 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1798 * USB-2.0 buses they are also used as device addresses, however on
1799 * USB-3.0 buses the address is assigned by the controller hardware
1800 * and it usually is not the same as the device number.
1801 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001802 * WUSB devices are simple: they have no hubs behind, so the mapping
1803 * device <-> virtual port number becomes 1:1. Why? to simplify the
1804 * life of the device connection logic in
1805 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1806 * handshake we need to assign a temporary address in the unauthorized
1807 * space. For simplicity we use the first virtual port number found to
1808 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1809 * and that becomes it's address [X < 128] or its unauthorized address
1810 * [X | 0x80].
1811 *
1812 * We add 1 as an offset to the one-based USB-stack port number
1813 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1814 * 0 is reserved by USB for default address; (b) Linux's USB stack
1815 * uses always #1 for the root hub of the controller. So USB stack's
1816 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001817 *
1818 * Devices connected under xHCI are not as simple. The host controller
1819 * supports virtualization, so the hardware assigns device addresses and
1820 * the HCD must setup data structures before issuing a set address
1821 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001822 */
Alan Stern3b29b682011-02-22 09:53:41 -05001823static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
1825 int devnum;
1826 struct usb_bus *bus = udev->bus;
1827
1828 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001829 if (udev->wusb) {
1830 devnum = udev->portnum + 1;
1831 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1832 } else {
1833 /* Try to allocate the next devnum beginning at
1834 * bus->devnum_next. */
1835 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1836 bus->devnum_next);
1837 if (devnum >= 128)
1838 devnum = find_next_zero_bit(bus->devmap.devicemap,
1839 128, 1);
1840 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (devnum < 128) {
1843 set_bit(devnum, bus->devmap.devicemap);
1844 udev->devnum = devnum;
1845 }
1846}
1847
Alan Stern3b29b682011-02-22 09:53:41 -05001848static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 if (udev->devnum > 0) {
1851 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1852 udev->devnum = -1;
1853 }
1854}
1855
Alan Stern3b29b682011-02-22 09:53:41 -05001856static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001857{
1858 /* The address for a WUSB device is managed by wusbcore. */
1859 if (!udev->wusb)
1860 udev->devnum = devnum;
1861}
1862
Herbert Xuf7410ce2010-01-10 20:15:03 +11001863static void hub_free_dev(struct usb_device *udev)
1864{
1865 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1866
1867 /* Root hubs aren't real devices, so don't free HCD resources */
1868 if (hcd->driver->free_dev && udev->parent)
1869 hcd->driver->free_dev(hcd, udev);
1870}
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/**
1873 * usb_disconnect - disconnect a device (usbcore-internal)
1874 * @pdev: pointer to device being disconnected
1875 * Context: !in_interrupt ()
1876 *
1877 * Something got disconnected. Get rid of it and all of its children.
1878 *
1879 * If *pdev is a normal device then the parent hub must already be locked.
1880 * If *pdev is a root hub then this routine will acquire the
1881 * usb_bus_list_lock on behalf of the caller.
1882 *
1883 * Only hub drivers (including virtual root hub drivers for host
1884 * controllers) should ever call this.
1885 *
1886 * This call is synchronous, and may not be used in an interrupt context.
1887 */
1888void usb_disconnect(struct usb_device **pdev)
1889{
1890 struct usb_device *udev = *pdev;
1891 int i;
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 /* mark the device as inactive, so any further urb submissions for
1894 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001895 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 */
1897 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001898 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1899 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001901 usb_lock_device(udev);
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001904 for (i = 0; i < udev->maxchild; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001905 if (udev->children[i])
1906 usb_disconnect(&udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
1909 /* deallocate hcd/hardware state ... nuking all pending urbs and
1910 * cleaning up all state associated with the current configuration
1911 * so that the hardware is now fully quiesced.
1912 */
Alan Stern782da722006-07-01 22:09:35 -04001913 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001915 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Alan Stern3b23dd62008-12-05 14:10:34 -05001917 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001918 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001919
Alan Stern782da722006-07-01 22:09:35 -04001920 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001921 * for de-configuring the device and invoking the remove-device
1922 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001923 */
1924 device_del(&udev->dev);
1925
1926 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 * (or root_hub) pointer.
1928 */
Alan Stern3b29b682011-02-22 09:53:41 -05001929 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 /* Avoid races with recursively_mark_NOTATTACHED() */
1932 spin_lock_irq(&device_state_lock);
1933 *pdev = NULL;
1934 spin_unlock_irq(&device_state_lock);
1935
Herbert Xuf7410ce2010-01-10 20:15:03 +11001936 hub_free_dev(udev);
1937
Alan Stern782da722006-07-01 22:09:35 -04001938 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001941#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942static void show_string(struct usb_device *udev, char *id, char *string)
1943{
1944 if (!string)
1945 return;
1946 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1947}
1948
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001949static void announce_device(struct usb_device *udev)
1950{
1951 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1952 le16_to_cpu(udev->descriptor.idVendor),
1953 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001954 dev_info(&udev->dev,
1955 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001956 udev->descriptor.iManufacturer,
1957 udev->descriptor.iProduct,
1958 udev->descriptor.iSerialNumber);
1959 show_string(udev, "Product", udev->product);
1960 show_string(udev, "Manufacturer", udev->manufacturer);
1961 show_string(udev, "SerialNumber", udev->serial);
1962}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001964static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965#endif
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967#ifdef CONFIG_USB_OTG
1968#include "otg_whitelist.h"
1969#endif
1970
Oliver Neukum3ede7602007-01-11 14:35:50 +01001971/**
Alan Stern8d8558d2009-12-08 15:50:41 -05001972 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01001973 * @udev: newly addressed device (in ADDRESS state)
1974 *
Alan Stern8d8558d2009-12-08 15:50:41 -05001975 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01001976 */
Alan Stern8d8558d2009-12-08 15:50:41 -05001977static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001979 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981#ifdef CONFIG_USB_OTG
1982 /*
1983 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1984 * to wake us after we've powered off VBUS; and HNP, switching roles
1985 * "host" to "peripheral". The OTG descriptor helps figure this out.
1986 */
1987 if (!udev->bus->is_b_host
1988 && udev->config
1989 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02001990 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 struct usb_bus *bus = udev->bus;
1992
1993 /* descriptor may appear anywhere in config */
1994 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1995 le16_to_cpu(udev->config[0].desc.wTotalLength),
1996 USB_DT_OTG, (void **) &desc) == 0) {
1997 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05001998 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07001999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 dev_info(&udev->dev,
2001 "Dual-Role OTG device on %sHNP port\n",
2002 (port1 == bus->otg_port)
2003 ? "" : "non-");
2004
2005 /* enable HNP before suspend, it's simpler */
2006 if (port1 == bus->otg_port)
2007 bus->b_hnp_enable = 1;
2008 err = usb_control_msg(udev,
2009 usb_sndctrlpipe(udev, 0),
2010 USB_REQ_SET_FEATURE, 0,
2011 bus->b_hnp_enable
2012 ? USB_DEVICE_B_HNP_ENABLE
2013 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2014 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2015 if (err < 0) {
2016 /* OTG MESSAGE: report errors here,
2017 * customize to match your product.
2018 */
2019 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002020 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 err);
2022 bus->b_hnp_enable = 0;
2023 }
2024 }
2025 }
2026 }
2027
2028 if (!is_targeted(udev)) {
2029
2030 /* Maybe it can talk to us, though we can't talk to it.
2031 * (Includes HNP test device.)
2032 */
2033 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002034 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (err < 0)
2036 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2037 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002038 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto fail;
2040 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002041fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002043 return err;
2044}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002046
2047/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002048 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002049 * @udev: newly addressed device (in ADDRESS state)
2050 *
2051 * This is only called by usb_new_device() and usb_authorize_device()
2052 * and FIXME -- all comments that apply to them apply here wrt to
2053 * environment.
2054 *
2055 * If the device is WUSB and not authorized, we don't attempt to read
2056 * the string descriptors, as they will be errored out by the device
2057 * until it has been authorized.
2058 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002059static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002060{
2061 int err;
2062
2063 if (udev->config == NULL) {
2064 err = usb_get_configuration(udev);
2065 if (err < 0) {
2066 dev_err(&udev->dev, "can't read configurations, error %d\n",
2067 err);
2068 goto fail;
2069 }
2070 }
2071 if (udev->wusb == 1 && udev->authorized == 0) {
2072 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2073 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2074 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2075 }
2076 else {
2077 /* read the standard strings and cache them if present */
2078 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2079 udev->manufacturer = usb_cache_string(udev,
2080 udev->descriptor.iManufacturer);
2081 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2082 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002083 err = usb_enumerate_device_otg(udev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002084fail:
2085 return err;
2086}
2087
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002088static void set_usb_port_removable(struct usb_device *udev)
2089{
2090 struct usb_device *hdev = udev->parent;
2091 struct usb_hub *hub;
2092 u8 port = udev->portnum;
2093 u16 wHubCharacteristics;
2094 bool removable = true;
2095
2096 if (!hdev)
2097 return;
2098
2099 hub = hdev_to_hub(udev->parent);
2100
2101 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2102
2103 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2104 return;
2105
2106 if (hub_is_superspeed(hdev)) {
2107 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2108 removable = false;
2109 } else {
2110 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2111 removable = false;
2112 }
2113
2114 if (removable)
2115 udev->removable = USB_DEVICE_REMOVABLE;
2116 else
2117 udev->removable = USB_DEVICE_FIXED;
2118}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002119
2120/**
2121 * usb_new_device - perform initial device setup (usbcore-internal)
2122 * @udev: newly addressed device (in ADDRESS state)
2123 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002124 * This is called with devices which have been detected but not fully
2125 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002126 * for any device configuration. The caller must have locked either
2127 * the parent hub (if udev is a normal device) or else the
2128 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2129 * udev has already been installed, but udev is not yet visible through
2130 * sysfs or other filesystem code.
2131 *
2132 * It will return if the device is configured properly or not. Zero if
2133 * the interface was registered with the driver core; else a negative
2134 * errno value.
2135 *
2136 * This call is synchronous, and may not be used in an interrupt context.
2137 *
2138 * Only the hub driver or root-hub registrar should ever call this.
2139 */
2140int usb_new_device(struct usb_device *udev)
2141{
2142 int err;
2143
Dan Streetman16985402010-01-06 09:56:53 -05002144 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002145 /* Initialize non-root-hub device wakeup to disabled;
2146 * device (un)configuration controls wakeup capable
2147 * sysfs power/wakeup controls wakeup enabled/disabled
2148 */
2149 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002150 }
2151
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002152 /* Tell the runtime-PM framework the device is active */
2153 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002154 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002155 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002156 pm_runtime_enable(&udev->dev);
2157
Alan Sternc08512c2010-11-15 15:57:58 -05002158 /* By default, forbid autosuspend for all devices. It will be
2159 * allowed for hubs during binding.
2160 */
2161 usb_disable_autosuspend(udev);
2162
Alan Stern8d8558d2009-12-08 15:50:41 -05002163 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002164 if (err < 0)
2165 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002166 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2167 udev->devnum, udev->bus->busnum,
2168 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002169 /* export the usbdev device-node for libusb */
2170 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2171 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2172
Alan Stern6cd13202008-11-13 15:08:30 -05002173 /* Tell the world! */
2174 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002175
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002176 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002177
2178 /*
2179 * check whether the hub marks this port as non-removable. Do it
2180 * now so that platform-specific data can override it in
2181 * device_add()
2182 */
2183 if (udev->parent)
2184 set_usb_port_removable(udev);
2185
Alan Stern782da722006-07-01 22:09:35 -04002186 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002187 * for configuring the device and invoking the add-device
2188 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002189 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002190 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (err) {
2192 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2193 goto fail;
2194 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002195
Alan Stern3b23dd62008-12-05 14:10:34 -05002196 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002197 usb_mark_last_busy(udev);
2198 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201fail:
2202 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002203 pm_runtime_disable(&udev->dev);
2204 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002205 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002208
2209/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002210 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2211 * @usb_dev: USB device
2212 *
2213 * Move the USB device to a very basic state where interfaces are disabled
2214 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002215 *
2216 * We share a lock (that we have) with device_del(), so we need to
2217 * defer its call.
2218 */
2219int usb_deauthorize_device(struct usb_device *usb_dev)
2220{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002221 usb_lock_device(usb_dev);
2222 if (usb_dev->authorized == 0)
2223 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002224
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002225 usb_dev->authorized = 0;
2226 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002227
2228 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002229 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002230 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002231 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002232 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002233 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002234
2235 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002236 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002237
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002238out_unauthorized:
2239 usb_unlock_device(usb_dev);
2240 return 0;
2241}
2242
2243
2244int usb_authorize_device(struct usb_device *usb_dev)
2245{
2246 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002247
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002248 usb_lock_device(usb_dev);
2249 if (usb_dev->authorized == 1)
2250 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002251
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002252 result = usb_autoresume_device(usb_dev);
2253 if (result < 0) {
2254 dev_err(&usb_dev->dev,
2255 "can't autoresume for authorization: %d\n", result);
2256 goto error_autoresume;
2257 }
2258 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2259 if (result < 0) {
2260 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2261 "authorization: %d\n", result);
2262 goto error_device_descriptor;
2263 }
Alan Sternda307122009-12-08 15:54:44 -05002264
2265 kfree(usb_dev->product);
2266 usb_dev->product = NULL;
2267 kfree(usb_dev->manufacturer);
2268 usb_dev->manufacturer = NULL;
2269 kfree(usb_dev->serial);
2270 usb_dev->serial = NULL;
2271
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002272 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002273 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002274 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002275 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002276 /* Choose and set the configuration. This registers the interfaces
2277 * with the driver core and lets interface drivers bind to them.
2278 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002279 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002280 if (c >= 0) {
2281 result = usb_set_configuration(usb_dev, c);
2282 if (result) {
2283 dev_err(&usb_dev->dev,
2284 "can't set config #%d, error %d\n", c, result);
2285 /* This need not be fatal. The user can try to
2286 * set other configurations. */
2287 }
2288 }
2289 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002290
Alan Stern8d8558d2009-12-08 15:50:41 -05002291error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002292error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002293 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002294error_autoresume:
2295out_authorized:
2296 usb_unlock_device(usb_dev); // complements locktree
2297 return result;
2298}
2299
2300
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002301/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2302static unsigned hub_is_wusb(struct usb_hub *hub)
2303{
2304 struct usb_hcd *hcd;
2305 if (hub->hdev->parent != NULL) /* not a root hub? */
2306 return 0;
2307 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2308 return hcd->wireless;
2309}
2310
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312#define PORT_RESET_TRIES 5
2313#define SET_ADDRESS_TRIES 2
2314#define GET_DESCRIPTOR_TRIES 2
2315#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302316#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2319#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002320#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321#define HUB_LONG_RESET_TIME 200
2322#define HUB_RESET_TIMEOUT 500
2323
Sarah Sharp10d674a2011-09-14 14:24:52 -07002324static int hub_port_reset(struct usb_hub *hub, int port1,
2325 struct usb_device *udev, unsigned int delay, bool warm);
2326
2327/* Is a USB 3.0 port in the Inactive state? */
2328static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus)
2329{
2330 return hub_is_superspeed(hub->hdev) &&
2331 (portstatus & USB_PORT_STAT_LINK_STATE) ==
2332 USB_SS_PORT_LS_SS_INACTIVE;
2333}
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002336 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
2338 int delay_time, ret;
2339 u16 portstatus;
2340 u16 portchange;
2341
2342 for (delay_time = 0;
2343 delay_time < HUB_RESET_TIMEOUT;
2344 delay_time += delay) {
2345 /* wait to give the device a chance to reset */
2346 msleep(delay);
2347
2348 /* read and decode port status */
2349 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2350 if (ret < 0)
2351 return ret;
2352
Andiry Xu75d7cf72011-09-13 16:41:11 -07002353 /*
2354 * Some buggy devices require a warm reset to be issued even
2355 * when the port appears not to be connected.
2356 */
2357 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002358 /*
2359 * Some buggy devices can cause an NEC host controller
2360 * to transition to the "Error" state after a hot port
2361 * reset. This will show up as the port state in
2362 * "Inactive", and the port may also report a
2363 * disconnect. Forcing a warm port reset seems to make
2364 * the device work.
2365 *
2366 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2367 */
2368 if (hub_port_inactive(hub, portstatus)) {
2369 int ret;
2370
2371 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2372 clear_port_feature(hub->hdev, port1,
2373 USB_PORT_FEAT_C_CONNECTION);
2374 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2375 clear_port_feature(hub->hdev, port1,
2376 USB_PORT_FEAT_C_PORT_LINK_STATE);
2377 if (portchange & USB_PORT_STAT_C_RESET)
2378 clear_port_feature(hub->hdev, port1,
2379 USB_PORT_FEAT_C_RESET);
2380 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2381 port1);
2382 ret = hub_port_reset(hub, port1,
2383 udev, HUB_BH_RESET_TIME,
2384 true);
2385 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2386 clear_port_feature(hub->hdev, port1,
2387 USB_PORT_FEAT_C_CONNECTION);
2388 return ret;
2389 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002390 /* Device went away? */
2391 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2392 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Andiry Xu75d7cf72011-09-13 16:41:11 -07002394 /* bomb out completely if the connection bounced */
2395 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2396 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Andiry Xu75d7cf72011-09-13 16:41:11 -07002398 /* if we`ve finished resetting, then break out of
2399 * the loop
2400 */
2401 if (!(portstatus & USB_PORT_STAT_RESET) &&
2402 (portstatus & USB_PORT_STAT_ENABLE)) {
2403 if (hub_is_wusb(hub))
2404 udev->speed = USB_SPEED_WIRELESS;
2405 else if (hub_is_superspeed(hub->hdev))
2406 udev->speed = USB_SPEED_SUPER;
2407 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2408 udev->speed = USB_SPEED_HIGH;
2409 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2410 udev->speed = USB_SPEED_LOW;
2411 else
2412 udev->speed = USB_SPEED_FULL;
2413 return 0;
2414 }
2415 } else {
2416 if (portchange & USB_PORT_STAT_C_BH_RESET)
2417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
2419
2420 /* switch to the long delay after two short delay failures */
2421 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2422 delay = HUB_LONG_RESET_TIME;
2423
2424 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002425 "port %d not %sreset yet, waiting %dms\n",
2426 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428
2429 return -EBUSY;
2430}
2431
Andiry Xu75d7cf72011-09-13 16:41:11 -07002432static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2433 struct usb_device *udev, int *status, bool warm)
2434{
2435 switch (*status) {
2436 case 0:
2437 if (!warm) {
2438 struct usb_hcd *hcd;
2439 /* TRSTRCY = 10 ms; plus some extra */
2440 msleep(10 + 40);
2441 update_devnum(udev, 0);
2442 hcd = bus_to_hcd(udev->bus);
2443 if (hcd->driver->reset_device) {
2444 *status = hcd->driver->reset_device(hcd, udev);
2445 if (*status < 0) {
2446 dev_err(&udev->dev, "Cannot reset "
2447 "HCD device state\n");
2448 break;
2449 }
2450 }
2451 }
2452 /* FALL THROUGH */
2453 case -ENOTCONN:
2454 case -ENODEV:
2455 clear_port_feature(hub->hdev,
2456 port1, USB_PORT_FEAT_C_RESET);
2457 /* FIXME need disconnect() for NOTATTACHED device */
2458 if (warm) {
2459 clear_port_feature(hub->hdev, port1,
2460 USB_PORT_FEAT_C_BH_PORT_RESET);
2461 clear_port_feature(hub->hdev, port1,
2462 USB_PORT_FEAT_C_PORT_LINK_STATE);
2463 } else {
2464 usb_set_device_state(udev, *status
2465 ? USB_STATE_NOTATTACHED
2466 : USB_STATE_DEFAULT);
2467 }
2468 break;
2469 }
2470}
2471
2472/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002474 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 int i, status;
2477
Andiry Xu75d7cf72011-09-13 16:41:11 -07002478 if (!warm) {
2479 /* Block EHCI CF initialization during the port reset.
2480 * Some companion controllers don't like it when they mix.
2481 */
2482 down_read(&ehci_cf_port_reset_rwsem);
2483 } else {
2484 if (!hub_is_superspeed(hub->hdev)) {
2485 dev_err(hub->intfdev, "only USB3 hub support "
2486 "warm reset\n");
2487 return -EINVAL;
2488 }
2489 }
Alan Stern32fe0192007-10-10 16:27:07 -04002490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 /* Reset the port */
2492 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002493 status = set_port_feature(hub->hdev, port1, (warm ?
2494 USB_PORT_FEAT_BH_PORT_RESET :
2495 USB_PORT_FEAT_RESET));
2496 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002498 "cannot %sreset port %d (err = %d)\n",
2499 warm ? "warm " : "", port1, status);
2500 } else {
2501 status = hub_port_wait_reset(hub, port1, udev, delay,
2502 warm);
David Brownelldd165252005-08-31 10:45:25 -07002503 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 dev_dbg(hub->intfdev,
2505 "port_wait_reset: err = %d\n",
2506 status);
2507 }
2508
2509 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002510 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2511 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002512 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 }
2514
2515 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002516 "port %d not enabled, trying %sreset again...\n",
2517 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 delay = HUB_LONG_RESET_TIME;
2519 }
2520
2521 dev_err (hub->intfdev,
2522 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2523 port1);
2524
Andiry Xu75d7cf72011-09-13 16:41:11 -07002525done:
2526 if (!warm)
2527 up_read(&ehci_cf_port_reset_rwsem);
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return status;
2530}
2531
Andiry Xu0ed9a572011-04-27 18:07:43 +08002532/* Check if a port is power on */
2533static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2534{
2535 int ret = 0;
2536
2537 if (hub_is_superspeed(hub->hdev)) {
2538 if (portstatus & USB_SS_PORT_STAT_POWER)
2539 ret = 1;
2540 } else {
2541 if (portstatus & USB_PORT_STAT_POWER)
2542 ret = 1;
2543 }
2544
2545 return ret;
2546}
2547
Alan Sternd388dab2006-07-01 22:14:24 -04002548#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Andiry Xu0ed9a572011-04-27 18:07:43 +08002550/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2551static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2552{
2553 int ret = 0;
2554
2555 if (hub_is_superspeed(hub->hdev)) {
2556 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2557 == USB_SS_PORT_LS_U3)
2558 ret = 1;
2559 } else {
2560 if (portstatus & USB_PORT_STAT_SUSPEND)
2561 ret = 1;
2562 }
2563
2564 return ret;
2565}
Alan Sternb01b03f2008-04-28 11:06:11 -04002566
2567/* Determine whether the device on a port is ready for a normal resume,
2568 * is ready for a reset-resume, or should be disconnected.
2569 */
2570static int check_port_resume_type(struct usb_device *udev,
2571 struct usb_hub *hub, int port1,
2572 int status, unsigned portchange, unsigned portstatus)
2573{
2574 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002575 if (status || port_is_suspended(hub, portstatus) ||
2576 !port_is_power_on(hub, portstatus) ||
2577 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002578 if (status >= 0)
2579 status = -ENODEV;
2580 }
2581
Alan Stern86c57ed2008-06-30 11:14:43 -04002582 /* Can't do a normal resume if the port isn't enabled,
2583 * so try a reset-resume instead.
2584 */
2585 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2586 if (udev->persist_enabled)
2587 udev->reset_resume = 1;
2588 else
2589 status = -ENODEV;
2590 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002591
2592 if (status) {
2593 dev_dbg(hub->intfdev,
2594 "port %d status %04x.%04x after resume, %d\n",
2595 port1, portchange, portstatus, status);
2596 } else if (udev->reset_resume) {
2597
2598 /* Late port handoff can set status-change bits */
2599 if (portchange & USB_PORT_STAT_C_CONNECTION)
2600 clear_port_feature(hub->hdev, port1,
2601 USB_PORT_FEAT_C_CONNECTION);
2602 if (portchange & USB_PORT_STAT_C_ENABLE)
2603 clear_port_feature(hub->hdev, port1,
2604 USB_PORT_FEAT_C_ENABLE);
2605 }
2606
2607 return status;
2608}
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610#ifdef CONFIG_USB_SUSPEND
2611
2612/*
Alan Stern140d8f62006-07-01 22:07:21 -04002613 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002614 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002615 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 *
2617 * Suspends a USB device that isn't in active use, conserving power.
2618 * Devices may wake out of a suspend, if anything important happens,
2619 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002620 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 * to disconnect devices while they are suspended.
2622 *
David Brownell390a8c32005-09-13 19:57:27 -07002623 * This only affects the USB hardware for a device; its interfaces
2624 * (and, for hubs, child devices) must already have been suspended.
2625 *
Alan Stern624d6c02007-05-30 15:35:16 -04002626 * Selective port suspend reduces power; most suspended devices draw
2627 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2628 * All devices below the suspended port are also suspended.
2629 *
2630 * Devices leave suspend state when the host wakes them up. Some devices
2631 * also support "remote wakeup", where the device can activate the USB
2632 * tree above them to deliver data, such as a keypress or packet. In
2633 * some cases, this wakes the USB host.
2634 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 * Suspending OTG devices may trigger HNP, if that's been enabled
2636 * between a pair of dual-role devices. That will change roles, such
2637 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2638 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002639 * Devices on USB hub ports have only one "suspend" state, corresponding
2640 * to ACPI D2, "may cause the device to lose some context".
2641 * State transitions include:
2642 *
2643 * - suspend, resume ... when the VBUS power link stays live
2644 * - suspend, disconnect ... VBUS lost
2645 *
2646 * Once VBUS drop breaks the circuit, the port it's using has to go through
2647 * normal re-enumeration procedures, starting with enabling VBUS power.
2648 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2649 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2650 * timer, no SRP, no requests through sysfs.
2651 *
2652 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2653 * the root hub for their bus goes into global suspend ... so we don't
2654 * (falsely) update the device power state to say it suspended.
2655 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 * Returns 0 on success, else negative errno.
2657 */
Alan Stern65bfd292008-11-25 16:39:18 -05002658int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
Alan Stern624d6c02007-05-30 15:35:16 -04002660 struct usb_hub *hub = hdev_to_hub(udev->parent);
2661 int port1 = udev->portnum;
2662 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002663
Alan Stern624d6c02007-05-30 15:35:16 -04002664 /* enable remote wakeup when appropriate; this lets the device
2665 * wake up the upstream hub (including maybe the root hub).
2666 *
2667 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2668 * we don't explicitly enable it here.
2669 */
2670 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002671 if (!hub_is_superspeed(hub->hdev)) {
2672 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2673 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2674 USB_DEVICE_REMOTE_WAKEUP, 0,
2675 NULL, 0,
2676 USB_CTRL_SET_TIMEOUT);
2677 } else {
2678 /* Assume there's only one function on the USB 3.0
2679 * device and enable remote wake for the first
2680 * interface. FIXME if the interface association
2681 * descriptor shows there's more than one function.
2682 */
2683 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2684 USB_REQ_SET_FEATURE,
2685 USB_RECIP_INTERFACE,
2686 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002687 USB_INTRF_FUNC_SUSPEND_RW |
2688 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002689 NULL, 0,
2690 USB_CTRL_SET_TIMEOUT);
2691 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002692 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002693 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2694 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002695 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002696 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002697 return status;
2698 }
Alan Stern624d6c02007-05-30 15:35:16 -04002699 }
2700
Andiry Xu65580b432011-09-23 14:19:52 -07002701 /* disable USB2 hardware LPM */
2702 if (udev->usb2_hw_lpm_enabled == 1)
2703 usb_set_usb2_hardware_lpm(udev, 0);
2704
Alan Stern624d6c02007-05-30 15:35:16 -04002705 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002706 if (hub_is_superspeed(hub->hdev))
2707 status = set_port_feature(hub->hdev,
2708 port1 | (USB_SS_PORT_LS_U3 << 3),
2709 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002710 else
2711 status = set_port_feature(hub->hdev, port1,
2712 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002713 if (status) {
2714 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2715 port1, status);
2716 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002717 if (udev->do_remote_wakeup)
2718 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002719 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2720 USB_DEVICE_REMOTE_WAKEUP, 0,
2721 NULL, 0,
2722 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002723
Andiry Xuc3e751e2012-05-05 00:50:10 +08002724 /* Try to enable USB2 hardware LPM again */
2725 if (udev->usb2_hw_lpm_capable == 1)
2726 usb_set_usb2_hardware_lpm(udev, 1);
2727
Alan Sterncbb33002011-06-15 16:29:16 -04002728 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002729 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002730 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002731 } else {
2732 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002733 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2734 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2735 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002736 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2737 msleep(10);
2738 }
Alan Sternc08512c2010-11-15 15:57:58 -05002739 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002740 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
David Brownellf3f32532005-09-22 22:37:29 -07002742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743/*
David Brownell390a8c32005-09-13 19:57:27 -07002744 * If the USB "suspend" state is in use (rather than "global suspend"),
2745 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002746 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 * hardware resume signaling is finished, either because of selective
2748 * resume (by host) or remote wakeup (by device) ... now see what changed
2749 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002750 *
2751 * If @udev->reset_resume is set then the device is reset before the
2752 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 */
Alan Stern140d8f62006-07-01 22:07:21 -04002754static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Alan Stern54515fe2007-05-30 15:38:58 -04002756 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 u16 devstatus;
2758
2759 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002760 dev_dbg(&udev->dev, "%s\n",
2761 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 /* usb ch9 identifies four variants of SUSPENDED, based on what
2764 * state the device resumes to. Linux currently won't see the
2765 * first two on the host side; they'd be inside hub_port_init()
2766 * during many timeouts, but khubd can't suspend until later.
2767 */
2768 usb_set_device_state(udev, udev->actconfig
2769 ? USB_STATE_CONFIGURED
2770 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Alan Stern54515fe2007-05-30 15:38:58 -04002772 /* 10.5.4.5 says not to reset a suspended port if the attached
2773 * device is enabled for remote wakeup. Hence the reset
2774 * operation is carried out here, after the port has been
2775 * resumed.
2776 */
2777 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002778 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002779 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002780
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 /* 10.5.4.5 says be sure devices in the tree are still there.
2782 * For now let's assume the device didn't go crazy on resume,
2783 * and device drivers will know about any resume quirks.
2784 */
Alan Stern54515fe2007-05-30 15:38:58 -04002785 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002786 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002787 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2788 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002789 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002790
2791 /* If a normal resume failed, try doing a reset-resume */
2792 if (status && !udev->reset_resume && udev->persist_enabled) {
2793 dev_dbg(&udev->dev, "retry with reset-resume\n");
2794 udev->reset_resume = 1;
2795 goto retry_reset_resume;
2796 }
Alan Stern54515fe2007-05-30 15:38:58 -04002797 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002798
Alan Stern624d6c02007-05-30 15:35:16 -04002799 if (status) {
2800 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2801 status);
2802 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002804 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 status = usb_control_msg(udev,
2806 usb_sndctrlpipe(udev, 0),
2807 USB_REQ_CLEAR_FEATURE,
2808 USB_RECIP_DEVICE,
2809 USB_DEVICE_REMOTE_WAKEUP, 0,
2810 NULL, 0,
2811 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002812 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002813 dev_dbg(&udev->dev,
2814 "disable remote wakeup, status %d\n",
2815 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
2819 return status;
2820}
2821
Alan Stern624d6c02007-05-30 15:35:16 -04002822/*
2823 * usb_port_resume - re-activate a suspended usb device's upstream port
2824 * @udev: device to re-activate, not a root hub
2825 * Context: must be able to sleep; device not locked; pm locks held
2826 *
2827 * This will re-activate the suspended device, increasing power usage
2828 * while letting drivers communicate again with its endpoints.
2829 * USB resume explicitly guarantees that the power session between
2830 * the host and the device is the same as it was when the device
2831 * suspended.
2832 *
Alan Sternfeccc302008-03-03 15:15:59 -05002833 * If @udev->reset_resume is set then this routine won't check that the
2834 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002835 * reset @udev. The end result is that a broken power session can be
2836 * recovered and @udev will appear to persist across a loss of VBUS power.
2837 *
2838 * For example, if a host controller doesn't maintain VBUS suspend current
2839 * during a system sleep or is reset when the system wakes up, all the USB
2840 * power sessions below it will be broken. This is especially troublesome
2841 * for mass-storage devices containing mounted filesystems, since the
2842 * device will appear to have disconnected and all the memory mappings
2843 * to it will be lost. Using the USB_PERSIST facility, the device can be
2844 * made to appear as if it had not disconnected.
2845 *
Ming Lei742120c2008-06-18 22:00:29 +08002846 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002847 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002848 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2849 * quite possible for a device to remain unaltered but its media to be
2850 * changed. If the user replaces a flash memory card while the system is
2851 * asleep, he will have only himself to blame when the filesystem on the
2852 * new card is corrupted and the system crashes.
2853 *
Alan Stern624d6c02007-05-30 15:35:16 -04002854 * Returns 0 on success, else negative errno.
2855 */
Alan Stern65bfd292008-11-25 16:39:18 -05002856int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Alan Stern624d6c02007-05-30 15:35:16 -04002858 struct usb_hub *hub = hdev_to_hub(udev->parent);
2859 int port1 = udev->portnum;
2860 int status;
2861 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05002862
2863 /* Skip the initial Clear-Suspend step for a remote wakeup */
2864 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08002865 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05002866 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2869
Alan Sternd5cbad42006-08-11 16:52:39 -04002870 set_bit(port1, hub->busy_bits);
2871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08002873 if (hub_is_superspeed(hub->hdev))
2874 status = set_port_feature(hub->hdev,
2875 port1 | (USB_SS_PORT_LS_U0 << 3),
2876 USB_PORT_FEAT_LINK_STATE);
2877 else
2878 status = clear_port_feature(hub->hdev,
2879 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002881 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
2882 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04002885 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02002886 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 msleep(25);
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2890 * stop resume signaling. Then finish the resume
2891 * sequence.
2892 */
Alan Sternd25450c2006-11-20 11:14:30 -05002893 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04002894
Alan Sternb01b03f2008-04-28 11:06:11 -04002895 /* TRSMRCY = 10 msec */
2896 msleep(10);
2897 }
Alan Stern54515fe2007-05-30 15:38:58 -04002898
Alan Sternb01b03f2008-04-28 11:06:11 -04002899 SuspendCleared:
2900 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08002901 if (hub_is_superspeed(hub->hdev)) {
2902 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2903 clear_port_feature(hub->hdev, port1,
2904 USB_PORT_FEAT_C_PORT_LINK_STATE);
2905 } else {
2906 if (portchange & USB_PORT_STAT_C_SUSPEND)
2907 clear_port_feature(hub->hdev, port1,
2908 USB_PORT_FEAT_C_SUSPEND);
2909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Alan Sternd5cbad42006-08-11 16:52:39 -04002912 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04002913
Alan Sternb01b03f2008-04-28 11:06:11 -04002914 status = check_port_resume_type(udev,
2915 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04002916 if (status == 0)
2917 status = finish_port_resume(udev);
2918 if (status < 0) {
2919 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2920 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07002921 } else {
2922 /* Try to enable USB2 hardware LPM */
2923 if (udev->usb2_hw_lpm_capable == 1)
2924 usb_set_usb2_hardware_lpm(udev, 1);
Alan Stern54515fe2007-05-30 15:38:58 -04002925 }
Andiry Xu65580b432011-09-23 14:19:52 -07002926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return status;
2928}
2929
Alan Stern8808f002008-04-28 11:06:55 -04002930/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05002931int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
2933 int status = 0;
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04002936 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002937 status = usb_autoresume_device(udev);
2938 if (status == 0) {
2939 /* Let the drivers do their thing, then... */
2940 usb_autosuspend_device(udev);
2941 }
Alan Sternd25450c2006-11-20 11:14:30 -05002942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return status;
2944}
2945
Alan Sternd388dab2006-07-01 22:14:24 -04002946#else /* CONFIG_USB_SUSPEND */
2947
2948/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2949
Alan Stern65bfd292008-11-25 16:39:18 -05002950int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04002951{
2952 return 0;
2953}
2954
Alan Sternb01b03f2008-04-28 11:06:11 -04002955/* However we may need to do a reset-resume */
2956
Alan Stern65bfd292008-11-25 16:39:18 -05002957int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04002958{
Alan Sternb01b03f2008-04-28 11:06:11 -04002959 struct usb_hub *hub = hdev_to_hub(udev->parent);
2960 int port1 = udev->portnum;
2961 int status;
2962 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04002963
Alan Sternb01b03f2008-04-28 11:06:11 -04002964 status = hub_port_status(hub, port1, &portstatus, &portchange);
2965 status = check_port_resume_type(udev,
2966 hub, port1, status, portchange, portstatus);
2967
2968 if (status) {
2969 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2970 hub_port_logical_disconnect(hub, port1);
2971 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04002972 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08002973 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002974 }
2975 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04002976}
2977
Alan Sternd388dab2006-07-01 22:14:24 -04002978#endif
2979
David Brownelldb690872005-09-13 19:56:33 -07002980static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 struct usb_hub *hub = usb_get_intfdata (intf);
2983 struct usb_device *hdev = hub->hdev;
2984 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08002985 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Alan Sterncbb33002011-06-15 16:29:16 -04002987 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
2989 struct usb_device *udev;
2990
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07002991 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04002992 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04002993 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002994 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002995 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07002996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08002998 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
2999 /* Enable hub to send remote wakeup for all ports. */
3000 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3001 status = set_port_feature(hdev,
3002 port1 |
3003 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3004 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3005 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3006 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3007 }
3008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Harvey Harrison441b62c2008-03-03 16:08:34 -08003010 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003011
David Brownellc9f89fa2005-09-13 19:57:04 -07003012 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003013 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017static int hub_resume(struct usb_interface *intf)
3018{
Alan Stern5e6effa2008-03-03 15:15:51 -05003019 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Alan Stern5e6effa2008-03-03 15:15:51 -05003021 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003022 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return 0;
3024}
3025
Alan Sternb41a60e2007-05-30 15:39:33 -04003026static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003027{
Alan Sternb41a60e2007-05-30 15:39:33 -04003028 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003029
Alan Stern5e6effa2008-03-03 15:15:51 -05003030 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003031 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003032 return 0;
3033}
3034
Alan Stern54515fe2007-05-30 15:38:58 -04003035/**
3036 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3037 * @rhdev: struct usb_device for the root hub
3038 *
3039 * The USB host controller driver calls this function when its root hub
3040 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003041 * has been reset. The routine marks @rhdev as having lost power.
3042 * When the hub driver is resumed it will take notice and carry out
3043 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3044 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003045 */
3046void usb_root_hub_lost_power(struct usb_device *rhdev)
3047{
3048 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3049 rhdev->reset_resume = 1;
3050}
3051EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3052
Alan Sternd388dab2006-07-01 22:14:24 -04003053#else /* CONFIG_PM */
3054
Alan Sternf07600c2007-05-30 15:38:16 -04003055#define hub_suspend NULL
3056#define hub_resume NULL
3057#define hub_reset_resume NULL
Alan Sternd388dab2006-07-01 22:14:24 -04003058#endif
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3062 *
3063 * Between connect detection and reset signaling there must be a delay
3064 * of 100ms at least for debounce and power-settling. The corresponding
3065 * timer shall restart whenever the downstream port detects a disconnect.
3066 *
3067 * Apparently there are some bluetooth and irda-dongles and a number of
3068 * low-speed devices for which this debounce period may last over a second.
3069 * Not covered by the spec - but easy to deal with.
3070 *
3071 * This implementation uses a 1500ms total debounce timeout; if the
3072 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3073 * every 25ms for transient disconnects. When the port status has been
3074 * unchanged for 100ms it returns the port status.
3075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076static int hub_port_debounce(struct usb_hub *hub, int port1)
3077{
3078 int ret;
3079 int total_time, stable_time = 0;
3080 u16 portchange, portstatus;
3081 unsigned connection = 0xffff;
3082
3083 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3084 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3085 if (ret < 0)
3086 return ret;
3087
3088 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3089 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3090 stable_time += HUB_DEBOUNCE_STEP;
3091 if (stable_time >= HUB_DEBOUNCE_STABLE)
3092 break;
3093 } else {
3094 stable_time = 0;
3095 connection = portstatus & USB_PORT_STAT_CONNECTION;
3096 }
3097
3098 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3099 clear_port_feature(hub->hdev, port1,
3100 USB_PORT_FEAT_C_CONNECTION);
3101 }
3102
3103 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3104 break;
3105 msleep(HUB_DEBOUNCE_STEP);
3106 }
3107
3108 dev_dbg (hub->intfdev,
3109 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3110 port1, total_time, stable_time, portstatus);
3111
3112 if (stable_time < HUB_DEBOUNCE_STABLE)
3113 return -ETIMEDOUT;
3114 return portstatus;
3115}
3116
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003117void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Alan Sternddeac4e2009-01-15 17:03:33 -05003119 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3120 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003121 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003123EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3126#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3127
Alan Stern4326ed02007-07-30 17:08:43 -04003128static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
3130 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003131 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Sarah Sharpc6515272009-04-27 19:57:26 -07003133 /*
3134 * The host controller will choose the device address,
3135 * instead of the core having chosen it earlier
3136 */
3137 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return -EINVAL;
3139 if (udev->state == USB_STATE_ADDRESS)
3140 return 0;
3141 if (udev->state != USB_STATE_DEFAULT)
3142 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003143 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003144 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003145 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003146 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3147 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3148 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003150 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003151 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003153 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 }
3155 return retval;
3156}
3157
3158/* Reset device, (re)assign address, get device descriptor.
3159 * Device connection must be stable, no more debouncing needed.
3160 * Returns device in USB_STATE_ADDRESS, except on error.
3161 *
3162 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003163 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 * newly detected device that is not accessible through any global
3165 * pointers, it's not necessary to lock the device.
3166 */
3167static int
3168hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3169 int retry_counter)
3170{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003171 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003174 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 int i, j, retval;
3176 unsigned delay = HUB_SHORT_RESET_TIME;
3177 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003178 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003179 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 /* root hub ports have a slightly longer reset period
3182 * (from USB 2.0 spec, section 7.1.7.5)
3183 */
3184 if (!hdev->parent) {
3185 delay = HUB_ROOT_RESET_TIME;
3186 if (port1 == hdev->bus->otg_port)
3187 hdev->bus->b_hnp_enable = 0;
3188 }
3189
3190 /* Some low speed devices have problems with the quick delay, so */
3191 /* be a bit pessimistic with those devices. RHbug #23670 */
3192 if (oldspeed == USB_SPEED_LOW)
3193 delay = HUB_LONG_RESET_TIME;
3194
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003195 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
Luben Tuikov07194ab2011-02-11 11:33:10 -08003197 /* Reset the device; full speed may morph to high speed */
3198 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003199 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003200 if (retval < 0) /* error or disconnect */
3201 goto fail;
3202 /* success, speed is known */
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 retval = -ENODEV;
3205
3206 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3207 dev_dbg(&udev->dev, "device reset changed speed!\n");
3208 goto fail;
3209 }
3210 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3213 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003214 * For Wireless USB devices, ep0 max packet is always 512 (tho
3215 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 */
3217 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003218 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003219 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003220 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003223 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 break;
3225 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3226 /* to determine the ep0 maxpacket size, try to read
3227 * the device descriptor to get bMaxPacketSize0 and
3228 * then correct our initial guess.
3229 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003230 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 break;
3232 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003233 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 break;
3235 default:
3236 goto fail;
3237 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003238
3239 if (udev->speed == USB_SPEED_WIRELESS)
3240 speed = "variable speed Wireless";
3241 else
3242 speed = usb_speed_string(udev->speed);
3243
Sarah Sharpc6515272009-04-27 19:57:26 -07003244 if (udev->speed != USB_SPEED_SUPER)
3245 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003246 "%s %s USB device number %d using %s\n",
3247 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003248 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
3250 /* Set up TT records, if needed */
3251 if (hdev->tt) {
3252 udev->tt = hdev->tt;
3253 udev->ttport = hdev->ttport;
3254 } else if (udev->speed != USB_SPEED_HIGH
3255 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003256 if (!hub->tt.hub) {
3257 dev_err(&udev->dev, "parent hub has no TT\n");
3258 retval = -EINVAL;
3259 goto fail;
3260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 udev->tt = &hub->tt;
3262 udev->ttport = port1;
3263 }
3264
3265 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3266 * Because device hardware and firmware is sometimes buggy in
3267 * this area, and this is how Linux has done it for ages.
3268 * Change it cautiously.
3269 *
3270 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3271 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3272 * so it may help with some non-standards-compliant devices.
3273 * Otherwise we start with SET_ADDRESS and then try to read the
3274 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3275 * value.
3276 */
3277 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003278 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 struct usb_device_descriptor *buf;
3280 int r = 0;
3281
3282#define GET_DESCRIPTOR_BUFSIZE 64
3283 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3284 if (!buf) {
3285 retval = -ENOMEM;
3286 continue;
3287 }
3288
Alan Sternb89ee192007-05-11 10:19:04 -04003289 /* Retry on all errors; some devices are flakey.
3290 * 255 is for WUSB devices, we actually need to use
3291 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 */
3293 for (j = 0; j < 3; ++j) {
3294 buf->bMaxPacketSize0 = 0;
3295 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3296 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3297 USB_DT_DEVICE << 8, 0,
3298 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003299 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003301 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 if (buf->bDescriptorType ==
3303 USB_DT_DEVICE) {
3304 r = 0;
3305 break;
3306 }
3307 /* FALL THROUGH */
3308 default:
3309 if (r == 0)
3310 r = -EPROTO;
3311 break;
3312 }
3313 if (r == 0)
3314 break;
3315 }
3316 udev->descriptor.bMaxPacketSize0 =
3317 buf->bMaxPacketSize0;
3318 kfree(buf);
3319
Andiry Xu75d7cf72011-09-13 16:41:11 -07003320 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 if (retval < 0) /* error or disconnect */
3322 goto fail;
3323 if (oldspeed != udev->speed) {
3324 dev_dbg(&udev->dev,
3325 "device reset changed speed!\n");
3326 retval = -ENODEV;
3327 goto fail;
3328 }
3329 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003330 dev_err(&udev->dev,
3331 "device descriptor read/64, error %d\n",
3332 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 retval = -EMSGSIZE;
3334 continue;
3335 }
3336#undef GET_DESCRIPTOR_BUFSIZE
3337 }
3338
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003339 /*
3340 * If device is WUSB, we already assigned an
3341 * unauthorized address in the Connect Ack sequence;
3342 * authorization will assign the final address.
3343 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003344 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003345 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3346 retval = hub_set_address(udev, devnum);
3347 if (retval >= 0)
3348 break;
3349 msleep(200);
3350 }
3351 if (retval < 0) {
3352 dev_err(&udev->dev,
3353 "device not accepting address %d, error %d\n",
3354 devnum, retval);
3355 goto fail;
3356 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003357 if (udev->speed == USB_SPEED_SUPER) {
3358 devnum = udev->devnum;
3359 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003360 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003361 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003362 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003363 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003364
3365 /* cope with hardware quirkiness:
3366 * - let SET_ADDRESS settle, some device hardware wants it
3367 * - read ep0 maxpacket even for high and low speed,
3368 */
3369 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003370 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
3374 retval = usb_get_device_descriptor(udev, 8);
3375 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003376 dev_err(&udev->dev,
3377 "device descriptor read/8, error %d\n",
3378 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (retval >= 0)
3380 retval = -EMSGSIZE;
3381 } else {
3382 retval = 0;
3383 break;
3384 }
3385 }
3386 if (retval)
3387 goto fail;
3388
Elric Fud8aec3d2012-03-26 21:16:02 +08003389 /*
3390 * Some superspeed devices have finished the link training process
3391 * and attached to a superspeed hub port, but the device descriptor
3392 * got from those devices show they aren't superspeed devices. Warm
3393 * reset the port attached by the devices can fix them.
3394 */
3395 if ((udev->speed == USB_SPEED_SUPER) &&
3396 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3397 dev_err(&udev->dev, "got a wrong device descriptor, "
3398 "warm reset device\n");
3399 hub_port_reset(hub, port1, udev,
3400 HUB_BH_RESET_TIME, true);
3401 retval = -EINVAL;
3402 goto fail;
3403 }
3404
Sarah Sharp6b403b02009-04-27 19:54:10 -07003405 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3406 udev->speed == USB_SPEED_SUPER)
3407 i = 512;
3408 else
3409 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003410 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003411 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003413 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 retval = -EMSGSIZE;
3415 goto fail;
3416 }
Alan Stern56626a72010-10-14 15:25:21 -04003417 if (udev->speed == USB_SPEED_FULL)
3418 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3419 else
3420 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003422 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
3424
3425 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3426 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003427 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3428 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 if (retval >= 0)
3430 retval = -ENOMSG;
3431 goto fail;
3432 }
3433
Andiry Xu1ff4df52011-09-23 14:19:48 -07003434 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3435 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003436 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08003437 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003438 usb_set_lpm_parameters(udev);
3439 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07003440 }
Andiry Xu3148bf02011-09-23 14:19:47 -07003441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08003443 /* notify HCD that we have a device connected and addressed */
3444 if (hcd->driver->update_device)
3445 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446fail:
Alan Stern4326ed02007-07-30 17:08:43 -04003447 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05003449 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04003450 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003451 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return retval;
3453}
3454
3455static void
3456check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
3457{
3458 struct usb_qualifier_descriptor *qual;
3459 int status;
3460
Christoph Lametere94b1762006-12-06 20:33:17 -08003461 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (qual == NULL)
3463 return;
3464
3465 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
3466 qual, sizeof *qual);
3467 if (status == sizeof *qual) {
3468 dev_info(&udev->dev, "not running at top speed; "
3469 "connect to a high speed hub\n");
3470 /* hub LEDs are probably harder to miss than syslog */
3471 if (hub->has_indicators) {
3472 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003473 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 }
3475 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003476 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
3479static unsigned
3480hub_power_remaining (struct usb_hub *hub)
3481{
3482 struct usb_device *hdev = hub->hdev;
3483 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05003484 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Alan Stern55c52712005-11-23 12:03:12 -05003486 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 return 0;
3488
Alan Stern55c52712005-11-23 12:03:12 -05003489 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
3490 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003491 struct usb_device *udev = hdev->children[port1 - 1];
Alan Stern55c52712005-11-23 12:03:12 -05003492 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494 if (!udev)
3495 continue;
3496
Alan Stern55c52712005-11-23 12:03:12 -05003497 /* Unconfigured devices may not use more than 100mA,
3498 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05003500 delta = udev->actconfig->desc.bMaxPower * 2;
3501 else if (port1 != udev->bus->otg_port || hdev->parent)
3502 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 else
Alan Stern55c52712005-11-23 12:03:12 -05003504 delta = 8;
3505 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003506 dev_warn(&udev->dev,
3507 "%dmA is over %umA budget for port %d!\n",
3508 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 remaining -= delta;
3510 }
3511 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05003512 dev_warn(hub->intfdev, "%dmA over power budget!\n",
3513 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 remaining = 0;
3515 }
3516 return remaining;
3517}
3518
3519/* Handle physical or logical connection change events.
3520 * This routine is called when:
3521 * a port connection-change occurs;
3522 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08003523 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 * a firmware download)
3525 * caller already locked the hub
3526 */
3527static void hub_port_connect_change(struct usb_hub *hub, int port1,
3528 u16 portstatus, u16 portchange)
3529{
3530 struct usb_device *hdev = hub->hdev;
3531 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05303532 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04003533 unsigned wHubCharacteristics =
3534 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04003535 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04003537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 dev_dbg (hub_dev,
3539 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08003540 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 if (hub->has_indicators) {
3543 set_port_led(hub, port1, HUB_LED_AUTO);
3544 hub->indicator[port1-1] = INDICATOR_AUTO;
3545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546
3547#ifdef CONFIG_USB_OTG
3548 /* during HNP, don't repeat the debounce */
3549 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04003550 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
3551 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552#endif
3553
Alan Stern8808f002008-04-28 11:06:55 -04003554 /* Try to resuscitate an existing device */
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003555 udev = hdev->children[port1-1];
Alan Stern8808f002008-04-28 11:06:55 -04003556 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
3557 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04003558 usb_lock_device(udev);
3559 if (portstatus & USB_PORT_STAT_ENABLE) {
3560 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04003561
3562#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04003563 } else if (udev->state == USB_STATE_SUSPENDED &&
3564 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04003565 /* For a suspended device, treat this as a
3566 * remote wakeup event.
3567 */
Alan Stern0534d462010-01-08 12:56:30 -05003568 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04003569#endif
3570
3571 } else {
Alan Stern5257d972008-09-22 14:43:08 -04003572 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04003573 }
3574 usb_unlock_device(udev);
3575
3576 if (status == 0) {
3577 clear_bit(port1, hub->change_bits);
3578 return;
3579 }
3580 }
3581
Alan Stern24618b02008-04-28 11:06:28 -04003582 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04003583 if (udev)
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003584 usb_disconnect(&hdev->children[port1-1]);
Alan Stern24618b02008-04-28 11:06:28 -04003585 clear_bit(port1, hub->change_bits);
3586
Alan Stern253e0572009-10-27 15:20:13 -04003587 /* We can forget about a "removed" device when there's a physical
3588 * disconnect or the connect status changes.
3589 */
3590 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3591 (portchange & USB_PORT_STAT_C_CONNECTION))
3592 clear_bit(port1, hub->removed_bits);
3593
Alan Stern5257d972008-09-22 14:43:08 -04003594 if (portchange & (USB_PORT_STAT_C_CONNECTION |
3595 USB_PORT_STAT_C_ENABLE)) {
3596 status = hub_port_debounce(hub, port1);
3597 if (status < 0) {
3598 if (printk_ratelimit())
3599 dev_err(hub_dev, "connect-debounce failed, "
3600 "port %d disabled\n", port1);
3601 portstatus &= ~USB_PORT_STAT_CONNECTION;
3602 } else {
3603 portstatus = status;
3604 }
3605 }
3606
Alan Stern253e0572009-10-27 15:20:13 -04003607 /* Return now if debouncing failed or nothing is connected or
3608 * the device was "removed".
3609 */
3610 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3611 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
3613 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07003614 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08003615 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04003617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 if (portstatus & USB_PORT_STAT_ENABLE)
3619 goto done;
3620 return;
3621 }
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
3625 /* reallocate for each attempt, since references
3626 * to the previous one can escape in various ways
3627 */
3628 udev = usb_alloc_dev(hdev, hdev->bus, port1);
3629 if (!udev) {
3630 dev_err (hub_dev,
3631 "couldn't allocate port %d usb_device\n",
3632 port1);
3633 goto done;
3634 }
3635
3636 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05003637 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04003638 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07003639 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05003640
Sarah Sharp131dec32010-12-06 21:00:19 -08003641 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
3642 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07003643 udev->speed = USB_SPEED_SUPER;
3644 else
3645 udev->speed = USB_SPEED_UNKNOWN;
3646
Alan Stern3b29b682011-02-22 09:53:41 -05003647 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003648 if (udev->devnum <= 0) {
3649 status = -ENOTCONN; /* Don't retry */
3650 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07003651 }
3652
Sarah Sharpe7b77172009-04-27 19:54:26 -07003653 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 status = hub_port_init(hub, udev, port1, i);
3655 if (status < 0)
3656 goto loop;
3657
Phil Dibowitz93362a82010-07-22 00:05:01 +02003658 usb_detect_quirks(udev);
3659 if (udev->quirks & USB_QUIRK_DELAY_INIT)
3660 msleep(1000);
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 /* consecutive bus-powered hubs aren't reliable; they can
3663 * violate the voltage drop budget. if the new child has
3664 * a "powered" LED, users should notice we didn't enable it
3665 * (without reading syslog), even without per-port LEDs
3666 * on the parent.
3667 */
3668 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05003669 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 u16 devstat;
3671
3672 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
3673 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05003674 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 dev_dbg(&udev->dev, "get status %d ?\n", status);
3676 goto loop_disable;
3677 }
Alan Stern55c52712005-11-23 12:03:12 -05003678 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
3680 dev_err(&udev->dev,
3681 "can't connect bus-powered hub "
3682 "to this port\n");
3683 if (hub->has_indicators) {
3684 hub->indicator[port1-1] =
3685 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003686 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
3688 status = -ENOTCONN; /* Don't retry */
3689 goto loop_disable;
3690 }
3691 }
3692
3693 /* check for devices running slower than they could */
3694 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
3695 && udev->speed == USB_SPEED_FULL
3696 && highspeed_hubs != 0)
3697 check_highspeed (hub, udev, port1);
3698
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003699 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 * udev becomes globally accessible, although presumably
3701 * no one will look at it until hdev is unlocked.
3702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 status = 0;
3704
3705 /* We mustn't add new devices if the parent hub has
3706 * been disconnected; we would race with the
3707 * recursively_mark_NOTATTACHED() routine.
3708 */
3709 spin_lock_irq(&device_state_lock);
3710 if (hdev->state == USB_STATE_NOTATTACHED)
3711 status = -ENOTCONN;
3712 else
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003713 hdev->children[port1-1] = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 spin_unlock_irq(&device_state_lock);
3715
3716 /* Run it through the hoops (find a driver, etc) */
3717 if (!status) {
3718 status = usb_new_device(udev);
3719 if (status) {
3720 spin_lock_irq(&device_state_lock);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003721 hdev->children[port1-1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 spin_unlock_irq(&device_state_lock);
3723 }
3724 }
3725
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 if (status)
3727 goto loop_disable;
3728
3729 status = hub_power_remaining(hub);
3730 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05003731 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
3733 return;
3734
3735loop_disable:
3736 hub_port_disable(hub, port1, 1);
3737loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003738 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05003739 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11003740 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07003742 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 break;
3744 }
Alan Stern3a311552008-05-20 16:58:29 -04003745 if (hub->hdev->parent ||
3746 !hcd->driver->port_handed_over ||
3747 !(hcd->driver->port_handed_over)(hcd, port1))
3748 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
3749 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
3751done:
3752 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05303753 if (hcd->driver->relinquish_port && !hub->hdev->parent)
3754 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755}
3756
Sarah Sharp714b07b2012-01-24 13:53:18 -08003757/* Returns 1 if there was a remote wakeup and a connect status change. */
3758static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08003759 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08003760{
3761 struct usb_device *hdev;
3762 struct usb_device *udev;
3763 int connect_change = 0;
3764 int ret;
3765
3766 hdev = hub->hdev;
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003767 udev = hdev->children[port-1];
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003768 if (!hub_is_superspeed(hdev)) {
3769 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3770 return 0;
3771 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3772 } else {
3773 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08003774 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3775 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003776 return 0;
3777 }
3778
Sarah Sharp714b07b2012-01-24 13:53:18 -08003779 if (udev) {
3780 /* TRSMRCY = 10 msec */
3781 msleep(10);
3782
3783 usb_lock_device(udev);
3784 ret = usb_remote_wakeup(udev);
3785 usb_unlock_device(udev);
3786 if (ret < 0)
3787 connect_change = 1;
3788 } else {
3789 ret = -ENODEV;
3790 hub_port_disable(hub, port, 1);
3791 }
3792 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
3793 port, ret);
3794 return connect_change;
3795}
3796
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797static void hub_events(void)
3798{
3799 struct list_head *tmp;
3800 struct usb_device *hdev;
3801 struct usb_interface *intf;
3802 struct usb_hub *hub;
3803 struct device *hub_dev;
3804 u16 hubstatus;
3805 u16 hubchange;
3806 u16 portstatus;
3807 u16 portchange;
3808 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003809 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810
3811 /*
3812 * We restart the list every time to avoid a deadlock with
3813 * deleting hubs downstream from this one. This should be
3814 * safe since we delete the hub from the event list.
3815 * Not the most efficient, but avoids deadlocks.
3816 */
3817 while (1) {
3818
3819 /* Grab the first entry at the beginning of the list */
3820 spin_lock_irq(&hub_event_lock);
3821 if (list_empty(&hub_event_list)) {
3822 spin_unlock_irq(&hub_event_lock);
3823 break;
3824 }
3825
3826 tmp = hub_event_list.next;
3827 list_del_init(tmp);
3828
3829 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04003830 kref_get(&hub->kref);
3831 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Alan Sterne8054852007-05-04 11:55:11 -04003833 hdev = hub->hdev;
3834 hub_dev = hub->intfdev;
3835 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05003836 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 hdev->state, hub->descriptor
3838 ? hub->descriptor->bNbrPorts
3839 : 0,
3840 /* NOTE: expects max 15 ports... */
3841 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05003842 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 /* Lock the device, then check to see if we were
3845 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04003846 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04003847 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003848 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
3850 /* If the hub has died, clean up after it */
3851 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04003852 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04003853 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 goto loop;
3855 }
3856
Alan Stern40f122f2006-11-09 14:44:33 -05003857 /* Autoresume */
3858 ret = usb_autopm_get_interface(intf);
3859 if (ret) {
3860 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05003862 }
3863
3864 /* If this is an inactive hub, do nothing */
3865 if (hub->quiescing)
3866 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 if (hub->error) {
3869 dev_dbg (hub_dev, "resetting for error %d\n",
3870 hub->error);
3871
Ming Lei742120c2008-06-18 22:00:29 +08003872 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 if (ret) {
3874 dev_dbg (hub_dev,
3875 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05003876 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
3878
3879 hub->nerrors = 0;
3880 hub->error = 0;
3881 }
3882
3883 /* deal with port status changes */
3884 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
3885 if (test_bit(i, hub->busy_bits))
3886 continue;
3887 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08003888 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003890 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 continue;
3892
3893 ret = hub_port_status(hub, i,
3894 &portstatus, &portchange);
3895 if (ret < 0)
3896 continue;
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3899 clear_port_feature(hdev, i,
3900 USB_PORT_FEAT_C_CONNECTION);
3901 connect_change = 1;
3902 }
3903
3904 if (portchange & USB_PORT_STAT_C_ENABLE) {
3905 if (!connect_change)
3906 dev_dbg (hub_dev,
3907 "port %d enable change, "
3908 "status %08x\n",
3909 i, portstatus);
3910 clear_port_feature(hdev, i,
3911 USB_PORT_FEAT_C_ENABLE);
3912
3913 /*
3914 * EM interference sometimes causes badly
3915 * shielded USB devices to be shutdown by
3916 * the hub, this hack enables them again.
3917 * Works at least with mouse driver.
3918 */
3919 if (!(portstatus & USB_PORT_STAT_ENABLE)
3920 && !connect_change
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003921 && hdev->children[i-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 dev_err (hub_dev,
3923 "port %i "
3924 "disabled by hub (EMI?), "
3925 "re-enabling...\n",
3926 i);
3927 connect_change = 1;
3928 }
3929 }
3930
Sarah Sharp72937e12012-01-24 11:46:50 -08003931 if (hub_handle_remote_wakeup(hub, i,
3932 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08003933 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04003934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01003936 u16 status = 0;
3937 u16 unused;
3938
3939 dev_dbg(hub_dev, "over-current change on port "
3940 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 clear_port_feature(hdev, i,
3942 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01003943 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04003944 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01003945 hub_port_status(hub, i, &status, &unused);
3946 if (status & USB_PORT_STAT_OVERCURRENT)
3947 dev_err(hub_dev, "over-current "
3948 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
3950
3951 if (portchange & USB_PORT_STAT_C_RESET) {
3952 dev_dbg (hub_dev,
3953 "reset change on port %d\n",
3954 i);
3955 clear_port_feature(hdev, i,
3956 USB_PORT_FEAT_C_RESET);
3957 }
Sarah Sharpc7061572010-12-06 15:08:20 -08003958 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
3959 hub_is_superspeed(hub->hdev)) {
3960 dev_dbg(hub_dev,
3961 "warm reset change on port %d\n",
3962 i);
3963 clear_port_feature(hdev, i,
3964 USB_PORT_FEAT_C_BH_PORT_RESET);
3965 }
John Youndbe79bb2001-09-17 00:00:00 -07003966 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
3967 clear_port_feature(hub->hdev, i,
3968 USB_PORT_FEAT_C_PORT_LINK_STATE);
3969 }
3970 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
3971 dev_warn(hub_dev,
3972 "config error on port %d\n",
3973 i);
3974 clear_port_feature(hub->hdev, i,
3975 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
3976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Andiry Xu5e467f62011-04-27 18:07:54 +08003978 /* Warm reset a USB3 protocol port if it's in
3979 * SS.Inactive state.
3980 */
3981 if (hub_is_superspeed(hub->hdev) &&
3982 (portstatus & USB_PORT_STAT_LINK_STATE)
3983 == USB_SS_PORT_LS_SS_INACTIVE) {
3984 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07003985 hub_port_reset(hub, i, NULL,
3986 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08003987 }
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (connect_change)
3990 hub_port_connect_change(hub, i,
3991 portstatus, portchange);
3992 } /* end for i */
3993
3994 /* deal with hub status changes */
3995 if (test_and_clear_bit(0, hub->event_bits) == 0)
3996 ; /* do nothing */
3997 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
3998 dev_err (hub_dev, "get_hub_status failed\n");
3999 else {
4000 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4001 dev_dbg (hub_dev, "power change\n");
4002 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004003 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4004 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004005 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004006 else
4007 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
4009 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004010 u16 status = 0;
4011 u16 unused;
4012
4013 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004015 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004016 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004017 hub_hub_status(hub, &status, &unused);
4018 if (status & HUB_STATUS_OVERCURRENT)
4019 dev_err(hub_dev, "over-current "
4020 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 }
4022 }
4023
Alan Stern8e4ceb32009-12-07 13:01:37 -05004024 loop_autopm:
4025 /* Balance the usb_autopm_get_interface() above */
4026 usb_autopm_put_interface_no_suspend(intf);
4027 loop:
4028 /* Balance the usb_autopm_get_interface_no_resume() in
4029 * kick_khubd() and allow autosuspend.
4030 */
4031 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004032 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004034 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 } /* end while (1) */
4037}
4038
4039static int hub_thread(void *__unused)
4040{
Alan Stern3bb1af52008-03-03 15:15:36 -05004041 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4042 * port handover. Otherwise it might see that a full-speed device
4043 * was gone before the EHCI controller had handed its port over to
4044 * the companion full-speed controller.
4045 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004046 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 do {
4049 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004050 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004051 !list_empty(&hub_event_list) ||
4052 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004053 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004055 pr_debug("%s: khubd exiting\n", usbcore_name);
4056 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057}
4058
Németh Márton1e927d92010-01-10 15:34:53 +01004059static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4061 .bDeviceClass = USB_CLASS_HUB},
4062 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4063 .bInterfaceClass = USB_CLASS_HUB},
4064 { } /* Terminating entry */
4065};
4066
4067MODULE_DEVICE_TABLE (usb, hub_id_table);
4068
4069static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 .name = "hub",
4071 .probe = hub_probe,
4072 .disconnect = hub_disconnect,
4073 .suspend = hub_suspend,
4074 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004075 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004076 .pre_reset = hub_pre_reset,
4077 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004078 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004080 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081};
4082
4083int usb_hub_init(void)
4084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 if (usb_register(&hub_driver) < 0) {
4086 printk(KERN_ERR "%s: can't register hub driver\n",
4087 usbcore_name);
4088 return -1;
4089 }
4090
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004091 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4092 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
4095 /* Fall through if kernel_thread failed */
4096 usb_deregister(&hub_driver);
4097 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4098
4099 return -1;
4100}
4101
4102void usb_hub_cleanup(void)
4103{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004104 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
4106 /*
4107 * Hub resources are freed for us by usb_deregister. It calls
4108 * usb_driver_purge on every device which in turn calls that
4109 * devices disconnect function if it is using this driver.
4110 * The hub_disconnect function takes care of releasing the
4111 * individual hub resources. -greg
4112 */
4113 usb_deregister(&hub_driver);
4114} /* usb_hub_cleanup() */
4115
Alan Sterneb764c42008-03-03 15:16:04 -05004116static int descriptors_changed(struct usb_device *udev,
4117 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
Alan Sterneb764c42008-03-03 15:16:04 -05004119 int changed = 0;
4120 unsigned index;
4121 unsigned serial_len = 0;
4122 unsigned len;
4123 unsigned old_length;
4124 int length;
4125 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Alan Sterneb764c42008-03-03 15:16:04 -05004127 if (memcmp(&udev->descriptor, old_device_descriptor,
4128 sizeof(*old_device_descriptor)) != 0)
4129 return 1;
4130
4131 /* Since the idVendor, idProduct, and bcdDevice values in the
4132 * device descriptor haven't changed, we will assume the
4133 * Manufacturer and Product strings haven't changed either.
4134 * But the SerialNumber string could be different (e.g., a
4135 * different flash card of the same brand).
4136 */
4137 if (udev->serial)
4138 serial_len = strlen(udev->serial) + 1;
4139
4140 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004142 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4143 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 }
Alan Sterneb764c42008-03-03 15:16:04 -05004145
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004146 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 if (buf == NULL) {
4148 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4149 /* assume the worst */
4150 return 1;
4151 }
4152 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004153 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4155 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004156 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 dev_dbg(&udev->dev, "config index %d, error %d\n",
4158 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004159 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 break;
4161 }
4162 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4163 != 0) {
4164 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004165 index,
4166 ((struct usb_config_descriptor *) buf)->
4167 bConfigurationValue);
4168 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 break;
4170 }
4171 }
Alan Sterneb764c42008-03-03 15:16:04 -05004172
4173 if (!changed && serial_len) {
4174 length = usb_string(udev, udev->descriptor.iSerialNumber,
4175 buf, serial_len);
4176 if (length + 1 != serial_len) {
4177 dev_dbg(&udev->dev, "serial string error %d\n",
4178 length);
4179 changed = 1;
4180 } else if (memcmp(buf, udev->serial, length) != 0) {
4181 dev_dbg(&udev->dev, "serial string changed\n");
4182 changed = 1;
4183 }
4184 }
4185
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004187 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
4190/**
Ming Lei742120c2008-06-18 22:00:29 +08004191 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4193 *
Alan Stern79efa092006-06-01 13:33:42 -04004194 * WARNING - don't use this routine to reset a composite device
4195 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004196 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 *
4198 * Do a port reset, reassign the device's address, and establish its
4199 * former operating configuration. If the reset fails, or the device's
4200 * descriptors change from their values before the reset, or the original
4201 * configuration and altsettings cannot be restored, a flag will be set
4202 * telling khubd to pretend the device has been disconnected and then
4203 * re-connected. All drivers will be unbound, and the device will be
4204 * re-enumerated and probed all over again.
4205 *
4206 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4207 * flagged for logical disconnection, or some other negative error code
4208 * if the reset wasn't even attempted.
4209 *
4210 * The caller must own the device lock. For example, it's safe to use
4211 * this from a driver probe() routine after downloading new firmware.
4212 * For calls that might not occur during probe(), drivers should lock
4213 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004214 *
4215 * Locking exception: This routine may also be called from within an
4216 * autoresume handler. Such usage won't conflict with other tasks
4217 * holding the device lock because these tasks should always call
4218 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 */
Ming Lei742120c2008-06-18 22:00:29 +08004220static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
4222 struct usb_device *parent_hdev = udev->parent;
4223 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004224 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004226 int i, ret = 0;
4227 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
4229 if (udev->state == USB_STATE_NOTATTACHED ||
4230 udev->state == USB_STATE_SUSPENDED) {
4231 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4232 udev->state);
4233 return -EINVAL;
4234 }
4235
4236 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004237 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004238 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 return -EISDIR;
4240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 parent_hub = hdev_to_hub(parent_hdev);
4242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 set_bit(port1, parent_hub->busy_bits);
4244 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4245
4246 /* ep0 maxpacket size may change; let the HCD know about it.
4247 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004248 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004250 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 break;
4252 }
4253 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 if (ret < 0)
4256 goto re_enumerate;
4257
4258 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004259 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 dev_info(&udev->dev, "device firmware changed\n");
4261 udev->descriptor = descriptor; /* for disconnect() calls */
4262 goto re_enumerate;
4263 }
Alan Stern4fe03872009-02-26 10:21:02 -05004264
4265 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 if (!udev->actconfig)
4267 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004268
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004269 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004270 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4271 if (ret < 0) {
4272 dev_warn(&udev->dev,
4273 "Busted HC? Not enough HCD resources for "
4274 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004275 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004276 goto re_enumerate;
4277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4279 USB_REQ_SET_CONFIGURATION, 0,
4280 udev->actconfig->desc.bConfigurationValue, 0,
4281 NULL, 0, USB_CTRL_SET_TIMEOUT);
4282 if (ret < 0) {
4283 dev_err(&udev->dev,
4284 "can't restore configuration #%d (error=%d)\n",
4285 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004286 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 goto re_enumerate;
4288 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004289 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4291
Alan Stern4fe03872009-02-26 10:21:02 -05004292 /* Put interfaces back into the same altsettings as before.
4293 * Don't bother to send the Set-Interface request for interfaces
4294 * that were already in altsetting 0; besides being unnecessary,
4295 * many devices can't handle it. Instead just reset the host-side
4296 * endpoint state.
4297 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004299 struct usb_host_config *config = udev->actconfig;
4300 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 struct usb_interface_descriptor *desc;
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004304 if (desc->bAlternateSetting == 0) {
4305 usb_disable_interface(udev, intf, true);
4306 usb_enable_interface(udev, intf, true);
4307 ret = 0;
4308 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004309 /* Let the bandwidth allocation function know that this
4310 * device has been reset, and it will have to use
4311 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004312 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004313 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004314 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4315 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004316 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 if (ret < 0) {
4319 dev_err(&udev->dev, "failed to restore interface %d "
4320 "altsetting %d (error=%d)\n",
4321 desc->bInterfaceNumber,
4322 desc->bAlternateSetting,
4323 ret);
4324 goto re_enumerate;
4325 }
4326 }
4327
4328done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return 0;
4330
4331re_enumerate:
4332 hub_port_logical_disconnect(parent_hub, port1);
4333 return -ENODEV;
4334}
Alan Stern79efa092006-06-01 13:33:42 -04004335
4336/**
Ming Lei742120c2008-06-18 22:00:29 +08004337 * usb_reset_device - warn interface drivers and perform a USB port reset
Alan Stern79efa092006-06-01 13:33:42 -04004338 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
Alan Stern79efa092006-06-01 13:33:42 -04004339 *
4340 * Warns all drivers bound to registered interfaces (using their pre_reset
4341 * method), performs the port reset, and then lets the drivers know that
4342 * the reset is over (using their post_reset method).
4343 *
Ming Lei742120c2008-06-18 22:00:29 +08004344 * Return value is the same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04004345 *
4346 * The caller must own the device lock. For example, it's safe to use
4347 * this from a driver probe() routine after downloading new firmware.
4348 * For calls that might not occur during probe(), drivers should lock
4349 * the device using usb_lock_device_for_reset().
Alan Stern78d9a482008-06-23 16:00:40 -04004350 *
4351 * If an interface is currently being probed or disconnected, we assume
4352 * its driver knows how to handle resets. For all other interfaces,
4353 * if the driver doesn't have pre_reset and post_reset methods then
4354 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004355 */
Ming Lei742120c2008-06-18 22:00:29 +08004356int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004357{
4358 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004359 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004360 struct usb_host_config *config = udev->actconfig;
4361
4362 if (udev->state == USB_STATE_NOTATTACHED ||
4363 udev->state == USB_STATE_SUSPENDED) {
4364 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4365 udev->state);
4366 return -EINVAL;
4367 }
4368
Alan Stern645daaa2006-08-30 15:47:02 -04004369 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004370 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004371
Alan Stern79efa092006-06-01 13:33:42 -04004372 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004373 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004374 struct usb_interface *cintf = config->interface[i];
4375 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004376 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004377
4378 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004379 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004380 if (drv->pre_reset && drv->post_reset)
4381 unbind = (drv->pre_reset)(cintf);
4382 else if (cintf->condition ==
4383 USB_INTERFACE_BOUND)
4384 unbind = 1;
4385 if (unbind)
4386 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004387 }
4388 }
4389 }
4390
Ming Lei742120c2008-06-18 22:00:29 +08004391 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004392
4393 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004394 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004395 struct usb_interface *cintf = config->interface[i];
4396 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004397 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004398
Alan Stern78d9a482008-06-23 16:00:40 -04004399 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004400 drv = to_usb_driver(cintf->dev.driver);
4401 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004402 rebind = (drv->post_reset)(cintf);
4403 else if (cintf->condition ==
4404 USB_INTERFACE_BOUND)
4405 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04004406 }
Alan Stern6c640942008-10-21 15:40:03 -04004407 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04004408 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004409 }
4410 }
4411
Alan Stern94fcda12006-11-20 11:38:46 -05004412 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004413 return ret;
4414}
Ming Lei742120c2008-06-18 22:00:29 +08004415EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08004416
4417
4418/**
4419 * usb_queue_reset_device - Reset a USB device from an atomic context
4420 * @iface: USB interface belonging to the device to reset
4421 *
4422 * This function can be used to reset a USB device from an atomic
4423 * context, where usb_reset_device() won't work (as it blocks).
4424 *
4425 * Doing a reset via this method is functionally equivalent to calling
4426 * usb_reset_device(), except for the fact that it is delayed to a
4427 * workqueue. This means that any drivers bound to other interfaces
4428 * might be unbound, as well as users from usbfs in user space.
4429 *
4430 * Corner cases:
4431 *
4432 * - Scheduling two resets at the same time from two different drivers
4433 * attached to two different interfaces of the same device is
4434 * possible; depending on how the driver attached to each interface
4435 * handles ->pre_reset(), the second reset might happen or not.
4436 *
4437 * - If a driver is unbound and it had a pending reset, the reset will
4438 * be cancelled.
4439 *
4440 * - This function can be called during .probe() or .disconnect()
4441 * times. On return from .disconnect(), any pending resets will be
4442 * cancelled.
4443 *
4444 * There is no no need to lock/unlock the @reset_ws as schedule_work()
4445 * does its own.
4446 *
4447 * NOTE: We don't do any reference count tracking because it is not
4448 * needed. The lifecycle of the work_struct is tied to the
4449 * usb_interface. Before destroying the interface we cancel the
4450 * work_struct, so the fact that work_struct is queued and or
4451 * running means the interface (and thus, the device) exist and
4452 * are referenced.
4453 */
4454void usb_queue_reset_device(struct usb_interface *iface)
4455{
4456 schedule_work(&iface->reset_ws);
4457}
4458EXPORT_SYMBOL_GPL(usb_queue_reset_device);