blob: 821126eb81762ee2b57c971eec611c1e9a4e7ef6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB hub driver.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/completion.h>
16#include <linux/sched.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020022#include <linux/usb/hcd.h>
Richard Zhao925aa462012-07-11 11:09:28 +080023#include <linux/usb/otg.h>
Phil Dibowitz93362a82010-07-22 00:05:01 +020024#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070025#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010026#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <asm/byteorder.h>
31
32#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080034/* if we are in debug mode, always announce new devices */
35#ifdef DEBUG
36#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
37#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
38#endif
39#endif
40
Alan Stern1bb5f662006-11-06 11:56:13 -050041struct usb_hub {
42 struct device *intfdev; /* the "interface" device */
43 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040044 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050045 struct urb *urb; /* for interrupt polling pipe */
46
47 /* buffer for urb ... with extra space in case of babble */
48 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050049 union {
50 struct usb_hub_status hub;
51 struct usb_port_status port;
52 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050053 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050054
55 int error; /* last reported error */
56 int nerrors; /* track consecutive errors */
57
58 struct list_head event_list; /* hubs w/data or errs ready */
59 unsigned long event_bits[1]; /* status change bitmask */
60 unsigned long change_bits[1]; /* ports with logical connect
61 status change */
62 unsigned long busy_bits[1]; /* ports being reset or
63 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040064 unsigned long removed_bits[1]; /* ports with a "removed"
65 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080066 unsigned long wakeup_bits[1]; /* ports that have signaled
67 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050068#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
69#error event_bits[] is too short!
70#endif
71
72 struct usb_hub_descriptor *descriptor; /* class descriptor */
73 struct usb_tt tt; /* Transaction Translator */
74
75 unsigned mA_per_port; /* current for each child */
76
77 unsigned limited_power:1;
78 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040079 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050080
81 unsigned has_indicators:1;
82 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000083 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -040084 struct delayed_work init_work;
Lan Tianyu336c5c32012-07-06 14:13:52 +080085 struct dev_state **port_owners;
Alan Stern1bb5f662006-11-06 11:56:13 -050086};
87
John Youndbe79bb2001-09-17 00:00:00 -070088static inline int hub_is_superspeed(struct usb_device *hdev)
89{
Aman Deep7bf01182011-12-08 12:05:22 +053090 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070091}
Alan Stern1bb5f662006-11-06 11:56:13 -050092
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070093/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050094 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
96static DEFINE_SPINLOCK(device_state_lock);
97
98/* khubd's worklist and its lock */
99static DEFINE_SPINLOCK(hub_event_lock);
100static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
101
102/* Wakes up khubd */
103static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
104
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700105static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030108static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109module_param (blinkenlights, bool, S_IRUGO);
110MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
111
112/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200113 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
114 * 10 seconds to send reply for the initial 64-byte descriptor request.
115 */
116/* define initial 64-byte descriptor request timeout in milliseconds */
117static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
118module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800119MODULE_PARM_DESC(initial_descriptor_timeout,
120 "initial 64-byte descriptor request timeout in milliseconds "
121 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200122
123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * As of 2.6.10 we introduce a new USB device initialization scheme which
125 * closely resembles the way Windows works. Hopefully it will be compatible
126 * with a wider range of devices than the old scheme. However some previously
127 * working devices may start giving rise to "device not accepting address"
128 * errors; if that happens the user can try the old scheme by adjusting the
129 * following module parameters.
130 *
131 * For maximum flexibility there are two boolean parameters to control the
132 * hub driver's behavior. On the first initialization attempt, if the
133 * "old_scheme_first" parameter is set then the old scheme will be used,
134 * otherwise the new scheme is used. If that fails and "use_both_schemes"
135 * is set, then the driver will make another attempt, using the other scheme.
136 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030137static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
139MODULE_PARM_DESC(old_scheme_first,
140 "start with the old device initialization scheme");
141
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030142static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
144MODULE_PARM_DESC(use_both_schemes,
145 "try the other device initialization scheme if the "
146 "first one fails");
147
Alan Stern32fe0192007-10-10 16:27:07 -0400148/* Mutual exclusion for EHCI CF initialization. This interferes with
149 * port reset on some companion controllers.
150 */
151DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
152EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
153
Alan Stern948fea32008-04-28 11:07:07 -0400154#define HUB_DEBOUNCE_TIMEOUT 1500
155#define HUB_DEBOUNCE_STEP 25
156#define HUB_DEBOUNCE_STABLE 100
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Ming Lei742120c2008-06-18 22:00:29 +0800159static int usb_reset_and_verify_device(struct usb_device *udev);
160
Sarah Sharp131dec32010-12-06 21:00:19 -0800161static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Sarah Sharp131dec32010-12-06 21:00:19 -0800163 if (hub_is_superspeed(hub->hdev))
164 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500165 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500167 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return "1.5 Mb/s";
169 else
170 return "12 Mb/s";
171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400174static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700176 if (!hdev || !hdev->actconfig)
Alan Stern251180842009-07-22 14:41:18 -0400177 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return usb_get_intfdata(hdev->actconfig->interface[0]);
179}
180
Sarah Sharpd9b20992012-02-20 08:31:26 -0800181static int usb_device_supports_lpm(struct usb_device *udev)
182{
183 /* USB 2.1 (and greater) devices indicate LPM support through
184 * their USB 2.0 Extended Capabilities BOS descriptor.
185 */
186 if (udev->speed == USB_SPEED_HIGH) {
187 if (udev->bos->ext_cap &&
188 (USB_LPM_SUPPORT &
189 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
190 return 1;
191 return 0;
192 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800193
194 /* All USB 3.0 must support LPM, but we need their max exit latency
195 * information from the SuperSpeed Extended Capabilities BOS descriptor.
196 */
197 if (!udev->bos->ss_cap) {
198 dev_warn(&udev->dev, "No LPM exit latency info found. "
199 "Power management will be impacted.\n");
200 return 0;
201 }
202 if (udev->parent->lpm_capable)
203 return 1;
204
205 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
206 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800207 return 0;
208}
209
Sarah Sharp51e0a012012-02-20 12:02:19 -0800210/*
211 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
212 * either U1 or U2.
213 */
214static void usb_set_lpm_mel(struct usb_device *udev,
215 struct usb3_lpm_parameters *udev_lpm_params,
216 unsigned int udev_exit_latency,
217 struct usb_hub *hub,
218 struct usb3_lpm_parameters *hub_lpm_params,
219 unsigned int hub_exit_latency)
220{
221 unsigned int total_mel;
222 unsigned int device_mel;
223 unsigned int hub_mel;
224
225 /*
226 * Calculate the time it takes to transition all links from the roothub
227 * to the parent hub into U0. The parent hub must then decode the
228 * packet (hub header decode latency) to figure out which port it was
229 * bound for.
230 *
231 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
232 * means 0.1us). Multiply that by 100 to get nanoseconds.
233 */
234 total_mel = hub_lpm_params->mel +
235 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
236
237 /*
238 * How long will it take to transition the downstream hub's port into
239 * U0? The greater of either the hub exit latency or the device exit
240 * latency.
241 *
242 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
243 * Multiply that by 1000 to get nanoseconds.
244 */
245 device_mel = udev_exit_latency * 1000;
246 hub_mel = hub_exit_latency * 1000;
247 if (device_mel > hub_mel)
248 total_mel += device_mel;
249 else
250 total_mel += hub_mel;
251
252 udev_lpm_params->mel = total_mel;
253}
254
255/*
256 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
257 * a transition from either U1 or U2.
258 */
259static void usb_set_lpm_pel(struct usb_device *udev,
260 struct usb3_lpm_parameters *udev_lpm_params,
261 unsigned int udev_exit_latency,
262 struct usb_hub *hub,
263 struct usb3_lpm_parameters *hub_lpm_params,
264 unsigned int hub_exit_latency,
265 unsigned int port_to_port_exit_latency)
266{
267 unsigned int first_link_pel;
268 unsigned int hub_pel;
269
270 /*
271 * First, the device sends an LFPS to transition the link between the
272 * device and the parent hub into U0. The exit latency is the bigger of
273 * the device exit latency or the hub exit latency.
274 */
275 if (udev_exit_latency > hub_exit_latency)
276 first_link_pel = udev_exit_latency * 1000;
277 else
278 first_link_pel = hub_exit_latency * 1000;
279
280 /*
281 * When the hub starts to receive the LFPS, there is a slight delay for
282 * it to figure out that one of the ports is sending an LFPS. Then it
283 * will forward the LFPS to its upstream link. The exit latency is the
284 * delay, plus the PEL that we calculated for this hub.
285 */
286 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
287
288 /*
289 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
290 * is the greater of the two exit latencies.
291 */
292 if (first_link_pel > hub_pel)
293 udev_lpm_params->pel = first_link_pel;
294 else
295 udev_lpm_params->pel = hub_pel;
296}
297
298/*
299 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
300 * when a device initiates a transition to U0, until when it will receive the
301 * first packet from the host controller.
302 *
303 * Section C.1.5.1 describes the four components to this:
304 * - t1: device PEL
305 * - t2: time for the ERDY to make it from the device to the host.
306 * - t3: a host-specific delay to process the ERDY.
307 * - t4: time for the packet to make it from the host to the device.
308 *
309 * t3 is specific to both the xHCI host and the platform the host is integrated
310 * into. The Intel HW folks have said it's negligible, FIXME if a different
311 * vendor says otherwise.
312 */
313static void usb_set_lpm_sel(struct usb_device *udev,
314 struct usb3_lpm_parameters *udev_lpm_params)
315{
316 struct usb_device *parent;
317 unsigned int num_hubs;
318 unsigned int total_sel;
319
320 /* t1 = device PEL */
321 total_sel = udev_lpm_params->pel;
322 /* How many external hubs are in between the device & the root port. */
323 for (parent = udev->parent, num_hubs = 0; parent->parent;
324 parent = parent->parent)
325 num_hubs++;
326 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
327 if (num_hubs > 0)
328 total_sel += 2100 + 250 * (num_hubs - 1);
329
330 /* t4 = 250ns * num_hubs */
331 total_sel += 250 * num_hubs;
332
333 udev_lpm_params->sel = total_sel;
334}
335
336static void usb_set_lpm_parameters(struct usb_device *udev)
337{
338 struct usb_hub *hub;
339 unsigned int port_to_port_delay;
340 unsigned int udev_u1_del;
341 unsigned int udev_u2_del;
342 unsigned int hub_u1_del;
343 unsigned int hub_u2_del;
344
345 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
346 return;
347
348 hub = hdev_to_hub(udev->parent);
349 /* It doesn't take time to transition the roothub into U0, since it
350 * doesn't have an upstream link.
351 */
352 if (!hub)
353 return;
354
355 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
356 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
357 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
358 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
359
360 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
361 hub, &udev->parent->u1_params, hub_u1_del);
362
363 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
364 hub, &udev->parent->u2_params, hub_u2_del);
365
366 /*
367 * Appendix C, section C.2.2.2, says that there is a slight delay from
368 * when the parent hub notices the downstream port is trying to
369 * transition to U0 to when the hub initiates a U0 transition on its
370 * upstream port. The section says the delays are tPort2PortU1EL and
371 * tPort2PortU2EL, but it doesn't define what they are.
372 *
373 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
374 * about the same delays. Use the maximum delay calculations from those
375 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
376 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
377 * assume the device exit latencies they are talking about are the hub
378 * exit latencies.
379 *
380 * What do we do if the U2 exit latency is less than the U1 exit
381 * latency? It's possible, although not likely...
382 */
383 port_to_port_delay = 1;
384
385 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
386 hub, &udev->parent->u1_params, hub_u1_del,
387 port_to_port_delay);
388
389 if (hub_u2_del > hub_u1_del)
390 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
391 else
392 port_to_port_delay = 1 + hub_u1_del;
393
394 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
395 hub, &udev->parent->u2_params, hub_u2_del,
396 port_to_port_delay);
397
398 /* Now that we've got PEL, calculate SEL. */
399 usb_set_lpm_sel(udev, &udev->u1_params);
400 usb_set_lpm_sel(udev, &udev->u2_params);
401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700404static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
John Youndbe79bb2001-09-17 00:00:00 -0700406 int i, ret, size;
407 unsigned dtype;
408
409 if (hub_is_superspeed(hdev)) {
410 dtype = USB_DT_SS_HUB;
411 size = USB_DT_SS_HUB_SIZE;
412 } else {
413 dtype = USB_DT_HUB;
414 size = sizeof(struct usb_hub_descriptor);
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 for (i = 0; i < 3; i++) {
418 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
419 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700420 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 USB_CTRL_GET_TIMEOUT);
422 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
423 return ret;
424 }
425 return -EINVAL;
426}
427
428/*
429 * USB 2.0 spec Section 11.24.2.1
430 */
431static int clear_hub_feature(struct usb_device *hdev, int feature)
432{
433 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
434 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
435}
436
437/*
438 * USB 2.0 spec Section 11.24.2.2
439 */
440static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
441{
442 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
443 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
444 NULL, 0, 1000);
445}
446
447/*
448 * USB 2.0 spec Section 11.24.2.13
449 */
450static int set_port_feature(struct usb_device *hdev, int port1, int feature)
451{
452 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
453 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
454 NULL, 0, 1000);
455}
456
457/*
458 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
459 * for info about using port indicators
460 */
461static void set_port_led(
462 struct usb_hub *hub,
463 int port1,
464 int selector
465)
466{
467 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
468 USB_PORT_FEAT_INDICATOR);
469 if (status < 0)
470 dev_dbg (hub->intfdev,
471 "port %d indicator %s status %d\n",
472 port1,
473 ({ char *s; switch (selector) {
474 case HUB_LED_AMBER: s = "amber"; break;
475 case HUB_LED_GREEN: s = "green"; break;
476 case HUB_LED_OFF: s = "off"; break;
477 case HUB_LED_AUTO: s = "auto"; break;
478 default: s = "??"; break;
479 }; s; }),
480 status);
481}
482
483#define LED_CYCLE_PERIOD ((2*HZ)/3)
484
David Howellsc4028952006-11-22 14:57:56 +0000485static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
David Howellsc4028952006-11-22 14:57:56 +0000487 struct usb_hub *hub =
488 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct usb_device *hdev = hub->hdev;
490 unsigned i;
491 unsigned changed = 0;
492 int cursor = -1;
493
494 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
495 return;
496
497 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
498 unsigned selector, mode;
499
500 /* 30%-50% duty cycle */
501
502 switch (hub->indicator[i]) {
503 /* cycle marker */
504 case INDICATOR_CYCLE:
505 cursor = i;
506 selector = HUB_LED_AUTO;
507 mode = INDICATOR_AUTO;
508 break;
509 /* blinking green = sw attention */
510 case INDICATOR_GREEN_BLINK:
511 selector = HUB_LED_GREEN;
512 mode = INDICATOR_GREEN_BLINK_OFF;
513 break;
514 case INDICATOR_GREEN_BLINK_OFF:
515 selector = HUB_LED_OFF;
516 mode = INDICATOR_GREEN_BLINK;
517 break;
518 /* blinking amber = hw attention */
519 case INDICATOR_AMBER_BLINK:
520 selector = HUB_LED_AMBER;
521 mode = INDICATOR_AMBER_BLINK_OFF;
522 break;
523 case INDICATOR_AMBER_BLINK_OFF:
524 selector = HUB_LED_OFF;
525 mode = INDICATOR_AMBER_BLINK;
526 break;
527 /* blink green/amber = reserved */
528 case INDICATOR_ALT_BLINK:
529 selector = HUB_LED_GREEN;
530 mode = INDICATOR_ALT_BLINK_OFF;
531 break;
532 case INDICATOR_ALT_BLINK_OFF:
533 selector = HUB_LED_AMBER;
534 mode = INDICATOR_ALT_BLINK;
535 break;
536 default:
537 continue;
538 }
539 if (selector != HUB_LED_AUTO)
540 changed = 1;
541 set_port_led(hub, i + 1, selector);
542 hub->indicator[i] = mode;
543 }
544 if (!changed && blinkenlights) {
545 cursor++;
546 cursor %= hub->descriptor->bNbrPorts;
547 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
548 hub->indicator[cursor] = INDICATOR_CYCLE;
549 changed++;
550 }
551 if (changed)
552 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
553}
554
555/* use a short timeout for hub/port status fetches */
556#define USB_STS_TIMEOUT 1000
557#define USB_STS_RETRIES 5
558
559/*
560 * USB 2.0 spec Section 11.24.2.6
561 */
562static int get_hub_status(struct usb_device *hdev,
563 struct usb_hub_status *data)
564{
565 int i, status = -ETIMEDOUT;
566
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200567 for (i = 0; i < USB_STS_RETRIES &&
568 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
570 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
571 data, sizeof(*data), USB_STS_TIMEOUT);
572 }
573 return status;
574}
575
576/*
577 * USB 2.0 spec Section 11.24.2.7
578 */
579static int get_port_status(struct usb_device *hdev, int port1,
580 struct usb_port_status *data)
581{
582 int i, status = -ETIMEDOUT;
583
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200584 for (i = 0; i < USB_STS_RETRIES &&
585 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
587 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
588 data, sizeof(*data), USB_STS_TIMEOUT);
589 }
590 return status;
591}
592
Alan Stern3eb14912008-03-03 15:15:43 -0500593static int hub_port_status(struct usb_hub *hub, int port1,
594 u16 *status, u16 *change)
595{
596 int ret;
597
598 mutex_lock(&hub->status_mutex);
599 ret = get_port_status(hub->hdev, port1, &hub->status->port);
600 if (ret < 4) {
601 dev_err(hub->intfdev,
602 "%s failed (err = %d)\n", __func__, ret);
603 if (ret >= 0)
604 ret = -EIO;
605 } else {
606 *status = le16_to_cpu(hub->status->port.wPortStatus);
607 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700608
Alan Stern3eb14912008-03-03 15:15:43 -0500609 ret = 0;
610 }
611 mutex_unlock(&hub->status_mutex);
612 return ret;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615static void kick_khubd(struct usb_hub *hub)
616{
617 unsigned long flags;
618
619 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200620 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500622
623 /* Suppress autosuspend until khubd runs */
624 usb_autopm_get_interface_no_resume(
625 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 wake_up(&khubd_wait);
627 }
628 spin_unlock_irqrestore(&hub_event_lock, flags);
629}
630
631void usb_kick_khubd(struct usb_device *hdev)
632{
Alan Stern251180842009-07-22 14:41:18 -0400633 struct usb_hub *hub = hdev_to_hub(hdev);
634
635 if (hub)
636 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800639/*
640 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
641 * Notification, which indicates it had initiated remote wakeup.
642 *
643 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
644 * device initiates resume, so the USB core will not receive notice of the
645 * resume through the normal hub interrupt URB.
646 */
647void usb_wakeup_notification(struct usb_device *hdev,
648 unsigned int portnum)
649{
650 struct usb_hub *hub;
651
652 if (!hdev)
653 return;
654
655 hub = hdev_to_hub(hdev);
656 if (hub) {
657 set_bit(portnum, hub->wakeup_bits);
658 kick_khubd(hub);
659 }
660}
661EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100664static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200666 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400667 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100668 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 unsigned long bits;
670
Alan Sterne0152682007-08-24 15:42:52 -0400671 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 case -ENOENT: /* synchronous unlink */
673 case -ECONNRESET: /* async unlink */
674 case -ESHUTDOWN: /* hardware going away */
675 return;
676
677 default: /* presumably an error */
678 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400679 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if ((++hub->nerrors < 10) || hub->error)
681 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400682 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /* let khubd handle things */
686 case 0: /* we got data: port status changed */
687 bits = 0;
688 for (i = 0; i < urb->actual_length; ++i)
689 bits |= ((unsigned long) ((*hub->buffer)[i]))
690 << (i*8);
691 hub->event_bits[0] = bits;
692 break;
693 }
694
695 hub->nerrors = 0;
696
697 /* Something happened, let khubd figure it out */
698 kick_khubd(hub);
699
700resubmit:
701 if (hub->quiescing)
702 return;
703
704 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
705 && status != -ENODEV && status != -EPERM)
706 dev_err (hub->intfdev, "resubmit --> %d\n", status);
707}
708
709/* USB 2.0 spec Section 11.24.2.3 */
710static inline int
711hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
712{
Alan Sternc2f65952009-11-18 11:37:15 -0500713 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
715 tt, NULL, 0, 1000);
716}
717
718/*
719 * enumeration blocks khubd for a long time. we use keventd instead, since
720 * long blocking there is the exception, not the rule. accordingly, HCDs
721 * talking to TTs must queue control transfers (not just bulk and iso), so
722 * both can talk to the same hub concurrently.
723 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400724static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
David Howellsc4028952006-11-22 14:57:56 +0000726 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400727 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400729 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 spin_lock_irqsave (&hub->tt.lock, flags);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400732 while (--limit && !list_empty (&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400733 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 struct usb_tt_clear *clear;
735 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400736 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int status;
738
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400739 next = hub->tt.clear_list.next;
740 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 list_del (&clear->clear_list);
742
743 /* drop lock so HCD can concurrently report other TT errors */
744 spin_unlock_irqrestore (&hub->tt.lock, flags);
745 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (status)
747 dev_err (&hdev->dev,
748 "clear tt %d (%04x) error %d\n",
749 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400750
751 /* Tell the HCD, even if the operation failed */
752 drv = clear->hcd->driver;
753 if (drv->clear_tt_buffer_complete)
754 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
755
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700756 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400757 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759 spin_unlock_irqrestore (&hub->tt.lock, flags);
760}
761
762/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400763 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
764 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
766 * High speed HCDs use this to tell the hub driver that some split control or
767 * bulk transaction failed in a way that requires clearing internal state of
768 * a transaction translator. This is normally detected (and reported) from
769 * interrupt context.
770 *
771 * It may not be possible for that hub to handle additional full (or low)
772 * speed transactions until that state is fully cleared out.
773 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400774int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400776 struct usb_device *udev = urb->dev;
777 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct usb_tt *tt = udev->tt;
779 unsigned long flags;
780 struct usb_tt_clear *clear;
781
782 /* we've got to cope with an arbitrary number of pending TT clears,
783 * since each TT has "at least two" buffers that can need it (and
784 * there can be many TTs per hub). even if they're uncommon.
785 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800786 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
788 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400789 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 /* info that CLEAR_TT_BUFFER needs */
793 clear->tt = tt->multi ? udev->ttport : 1;
794 clear->devinfo = usb_pipeendpoint (pipe);
795 clear->devinfo |= udev->devnum << 4;
796 clear->devinfo |= usb_pipecontrol (pipe)
797 ? (USB_ENDPOINT_XFER_CONTROL << 11)
798 : (USB_ENDPOINT_XFER_BULK << 11);
799 if (usb_pipein (pipe))
800 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400801
802 /* info for completion callback */
803 clear->hcd = bus_to_hcd(udev->bus);
804 clear->ep = urb->ep;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /* tell keventd to clear state for this TT */
807 spin_lock_irqsave (&tt->lock, flags);
808 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400809 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400813EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Alan Stern8520f382008-09-22 14:44:26 -0400815/* If do_delay is false, return the number of milliseconds the caller
816 * needs to delay.
817 */
818static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700821 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400822 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400823 u16 wHubCharacteristics =
824 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Alan Stern4489a572006-04-27 15:54:22 -0400826 /* Enable power on each port. Some hubs have reserved values
827 * of LPSM (> 2) in their descriptors, even though they are
828 * USB 2.0 hubs. Some hubs do not implement port-power switching
829 * but only emulate it. In all cases, the ports won't work
830 * unless we send these messages to the hub.
831 */
832 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400834 else
835 dev_dbg(hub->intfdev, "trying to enable port power on "
836 "non-switchable hub\n");
837 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
838 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
David Brownellb7896962005-08-31 10:41:44 -0700840 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400841 delay = max(pgood_delay, (unsigned) 100);
842 if (do_delay)
843 msleep(delay);
844 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static int hub_hub_status(struct usb_hub *hub,
848 u16 *status, u16 *change)
849{
850 int ret;
851
Alan Sterndb90e7a2007-02-05 09:56:15 -0500852 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 ret = get_hub_status(hub->hdev, &hub->status->hub);
854 if (ret < 0)
855 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800856 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else {
858 *status = le16_to_cpu(hub->status->hub.wHubStatus);
859 *change = le16_to_cpu(hub->status->hub.wHubChange);
860 ret = 0;
861 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500862 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 return ret;
864}
865
Alan Stern8b28c752005-08-10 17:04:13 -0400866static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
867{
868 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400869 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400870
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700871 if (hdev->children[port1-1] && set_state)
872 usb_set_device_state(hdev->children[port1-1],
Alan Stern8b28c752005-08-10 17:04:13 -0400873 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700874 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400875 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400876 if (ret)
877 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400878 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400879 return ret;
880}
881
Alan Stern0458d5b2007-05-04 11:52:20 -0400882/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800883 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400884 * time later khubd will disconnect() any existing usb_device on the port
885 * and will re-enumerate if there actually is a device attached.
886 */
887static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
888{
889 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
890 hub_port_disable(hub, port1, 1);
891
892 /* FIXME let caller ask to power down the port:
893 * - some devices won't enumerate without a VBUS power cycle
894 * - SRP saves power that way
895 * - ... new call, TBD ...
896 * That's easy if this hub can switch power per-port, and
897 * khubd reactivates the port later (timer, SRP, etc).
898 * Powerdown must be optional, because of reset/DFU.
899 */
900
901 set_bit(port1, hub->change_bits);
902 kick_khubd(hub);
903}
904
Alan Stern253e0572009-10-27 15:20:13 -0400905/**
906 * usb_remove_device - disable a device's port on its parent hub
907 * @udev: device to be disabled and removed
908 * Context: @udev locked, must be able to sleep.
909 *
910 * After @udev's port has been disabled, khubd is notified and it will
911 * see that the device has been disconnected. When the device is
912 * physically unplugged and something is plugged in, the events will
913 * be received and processed normally.
914 */
915int usb_remove_device(struct usb_device *udev)
916{
917 struct usb_hub *hub;
918 struct usb_interface *intf;
919
920 if (!udev->parent) /* Can't remove a root hub */
921 return -EINVAL;
922 hub = hdev_to_hub(udev->parent);
923 intf = to_usb_interface(hub->intfdev);
924
925 usb_autopm_get_interface(intf);
926 set_bit(udev->portnum, hub->removed_bits);
927 hub_port_logical_disconnect(hub, udev->portnum);
928 usb_autopm_put_interface(intf);
929 return 0;
930}
931
Alan Stern6ee0b272008-04-28 11:06:42 -0400932enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500933 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400934 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400935};
Alan Stern5e6effa2008-03-03 15:15:51 -0500936
Alan Stern8520f382008-09-22 14:44:26 -0400937static void hub_init_func2(struct work_struct *ws);
938static void hub_init_func3(struct work_struct *ws);
939
Alan Sternf2835212008-04-28 11:07:17 -0400940static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500941{
942 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800943 struct usb_hcd *hcd;
944 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500945 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400946 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400947 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400948 unsigned delay;
949
950 /* Continue a partial initialization */
951 if (type == HUB_INIT2)
952 goto init2;
953 if (type == HUB_INIT3)
954 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500955
Elric Fua45aa3b2012-02-18 13:32:27 +0800956 /* The superspeed hub except for root hub has to use Hub Depth
957 * value as an offset into the route string to locate the bits
958 * it uses to determine the downstream port number. So hub driver
959 * should send a set hub depth request to superspeed hub after
960 * the superspeed hub is set configuration in initialization or
961 * reset procedure.
962 *
963 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400964 * For any other type of activation, turn it on.
965 */
Alan Stern8520f382008-09-22 14:44:26 -0400966 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800967 if (hdev->parent && hub_is_superspeed(hdev)) {
968 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
969 HUB_SET_DEPTH, USB_RT_HUB,
970 hdev->level - 1, 0, NULL, 0,
971 USB_CTRL_SET_TIMEOUT);
972 if (ret < 0)
973 dev_err(hub->intfdev,
974 "set hub depth failed\n");
975 }
Alan Stern8520f382008-09-22 14:44:26 -0400976
977 /* Speed up system boot by using a delayed_work for the
978 * hub's initial power-up delays. This is pretty awkward
979 * and the implementation looks like a home-brewed sort of
980 * setjmp/longjmp, but it saves at least 100 ms for each
981 * root hub (assuming usbcore is compiled into the kernel
982 * rather than as a module). It adds up.
983 *
984 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
985 * because for those activation types the ports have to be
986 * operational when we return. In theory this could be done
987 * for HUB_POST_RESET, but it's easier not to.
988 */
989 if (type == HUB_INIT) {
990 delay = hub_power_on(hub, false);
991 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
992 schedule_delayed_work(&hub->init_work,
993 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -0400994
995 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -0500996 usb_autopm_get_interface_no_resume(
997 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -0400998 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -0800999 } else if (type == HUB_RESET_RESUME) {
1000 /* The internal host controller state for the hub device
1001 * may be gone after a host power loss on system resume.
1002 * Update the device's info so the HW knows it's a hub.
1003 */
1004 hcd = bus_to_hcd(hdev->bus);
1005 if (hcd->driver->update_hub_device) {
1006 ret = hcd->driver->update_hub_device(hcd, hdev,
1007 &hub->tt, GFP_NOIO);
1008 if (ret < 0) {
1009 dev_err(hub->intfdev, "Host not "
1010 "accepting hub info "
1011 "update.\n");
1012 dev_err(hub->intfdev, "LS/FS devices "
1013 "and hubs may not work "
1014 "under this hub\n.");
1015 }
1016 }
1017 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001018 } else {
1019 hub_power_on(hub, true);
1020 }
1021 }
1022 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001023
Alan Stern6ee0b272008-04-28 11:06:42 -04001024 /* Check each port and set hub->change_bits to let khubd know
1025 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001026 */
1027 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001028 struct usb_device *udev = hdev->children[port1-1];
Alan Stern5e6effa2008-03-03 15:15:51 -05001029 u16 portstatus, portchange;
1030
Alan Stern6ee0b272008-04-28 11:06:42 -04001031 portstatus = portchange = 0;
1032 status = hub_port_status(hub, port1, &portstatus, &portchange);
1033 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1034 dev_dbg(hub->intfdev,
1035 "port %d: status %04x change %04x\n",
1036 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001037
Alan Stern6ee0b272008-04-28 11:06:42 -04001038 /* After anything other than HUB_RESUME (i.e., initialization
1039 * or any sort of reset), every port should be disabled.
1040 * Unconnected ports should likewise be disabled (paranoia),
1041 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001042 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001043 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1044 type != HUB_RESUME ||
1045 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1046 !udev ||
1047 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001048 /*
1049 * USB3 protocol ports will automatically transition
1050 * to Enabled state when detect an USB3.0 device attach.
1051 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001052 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001053 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001054 clear_port_feature(hdev, port1,
1055 USB_PORT_FEAT_ENABLE);
1056 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001057 } else {
1058 /* Pretend that power was lost for USB3 devs */
1059 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001060 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001061 }
1062
Alan Stern948fea32008-04-28 11:07:07 -04001063 /* Clear status-change flags; we'll debounce later */
1064 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1065 need_debounce_delay = true;
1066 clear_port_feature(hub->hdev, port1,
1067 USB_PORT_FEAT_C_CONNECTION);
1068 }
1069 if (portchange & USB_PORT_STAT_C_ENABLE) {
1070 need_debounce_delay = true;
1071 clear_port_feature(hub->hdev, port1,
1072 USB_PORT_FEAT_C_ENABLE);
1073 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001074 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1075 hub_is_superspeed(hub->hdev)) {
1076 need_debounce_delay = true;
1077 clear_port_feature(hub->hdev, port1,
1078 USB_PORT_FEAT_C_BH_PORT_RESET);
1079 }
Alan Stern253e0572009-10-27 15:20:13 -04001080 /* We can forget about a "removed" device when there's a
1081 * physical disconnect or the connect status changes.
1082 */
1083 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1084 (portchange & USB_PORT_STAT_C_CONNECTION))
1085 clear_bit(port1, hub->removed_bits);
1086
Alan Stern6ee0b272008-04-28 11:06:42 -04001087 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1088 /* Tell khubd to disconnect the device or
1089 * check for a new connection
1090 */
1091 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1092 set_bit(port1, hub->change_bits);
1093
1094 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001095 bool port_resumed = (portstatus &
1096 USB_PORT_STAT_LINK_STATE) ==
1097 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001098 /* The power session apparently survived the resume.
1099 * If there was an overcurrent or suspend change
1100 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001101 * take care of it. Look at the port link state
1102 * for USB 3.0 hubs, since they don't have a suspend
1103 * change bit, and they don't set the port link change
1104 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001105 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001106 if (portchange || (hub_is_superspeed(hub->hdev) &&
1107 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001108 set_bit(port1, hub->change_bits);
1109
1110 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001111#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001112 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001113#endif
Alan Stern8808f002008-04-28 11:06:55 -04001114 set_bit(port1, hub->change_bits);
1115
Alan Stern6ee0b272008-04-28 11:06:42 -04001116 } else {
1117 /* The power session is gone; tell khubd */
1118 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1119 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001120 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001121 }
1122
Alan Stern948fea32008-04-28 11:07:07 -04001123 /* If no port-status-change flags were set, we don't need any
1124 * debouncing. If flags were set we can try to debounce the
1125 * ports all at once right now, instead of letting khubd do them
1126 * one at a time later on.
1127 *
1128 * If any port-status changes do occur during this delay, khubd
1129 * will see them later and handle them normally.
1130 */
Alan Stern8520f382008-09-22 14:44:26 -04001131 if (need_debounce_delay) {
1132 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001133
Alan Stern8520f382008-09-22 14:44:26 -04001134 /* Don't do a long sleep inside a workqueue routine */
1135 if (type == HUB_INIT2) {
1136 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1137 schedule_delayed_work(&hub->init_work,
1138 msecs_to_jiffies(delay));
1139 return; /* Continues at init3: below */
1140 } else {
1141 msleep(delay);
1142 }
1143 }
1144 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001145 hub->quiescing = 0;
1146
1147 status = usb_submit_urb(hub->urb, GFP_NOIO);
1148 if (status < 0)
1149 dev_err(hub->intfdev, "activate --> %d\n", status);
1150 if (hub->has_indicators && blinkenlights)
1151 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1152
1153 /* Scan all ports that need attention */
1154 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001155
1156 /* Allow autosuspend if it was suppressed */
1157 if (type <= HUB_INIT3)
1158 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001159}
1160
Alan Stern8520f382008-09-22 14:44:26 -04001161/* Implement the continuations for the delays above */
1162static void hub_init_func2(struct work_struct *ws)
1163{
1164 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1165
1166 hub_activate(hub, HUB_INIT2);
1167}
1168
1169static void hub_init_func3(struct work_struct *ws)
1170{
1171 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1172
1173 hub_activate(hub, HUB_INIT3);
1174}
1175
Alan Stern43303542008-04-28 11:07:31 -04001176enum hub_quiescing_type {
1177 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1178};
1179
1180static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1181{
1182 struct usb_device *hdev = hub->hdev;
1183 int i;
1184
Alan Stern8520f382008-09-22 14:44:26 -04001185 cancel_delayed_work_sync(&hub->init_work);
1186
Alan Stern43303542008-04-28 11:07:31 -04001187 /* khubd and related activity won't re-trigger */
1188 hub->quiescing = 1;
1189
1190 if (type != HUB_SUSPEND) {
1191 /* Disconnect all the children */
1192 for (i = 0; i < hdev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001193 if (hdev->children[i])
1194 usb_disconnect(&hdev->children[i]);
Alan Stern43303542008-04-28 11:07:31 -04001195 }
1196 }
1197
1198 /* Stop khubd and related activity */
1199 usb_kill_urb(hub->urb);
1200 if (hub->has_indicators)
1201 cancel_delayed_work_sync(&hub->leds);
1202 if (hub->tt.hub)
Alan Sterncb88a1b2009-06-29 10:43:32 -04001203 cancel_work_sync(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001204}
1205
Alan Stern3eb14912008-03-03 15:15:43 -05001206/* caller has locked the hub device */
1207static int hub_pre_reset(struct usb_interface *intf)
1208{
1209 struct usb_hub *hub = usb_get_intfdata(intf);
1210
Alan Stern43303542008-04-28 11:07:31 -04001211 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001212 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001213}
1214
1215/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001216static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001217{
Alan Stern7de18d82006-06-01 13:37:24 -04001218 struct usb_hub *hub = usb_get_intfdata(intf);
1219
Alan Sternf2835212008-04-28 11:07:17 -04001220 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001221 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001222}
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224static int hub_configure(struct usb_hub *hub,
1225 struct usb_endpoint_descriptor *endpoint)
1226{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001227 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct usb_device *hdev = hub->hdev;
1229 struct device *hub_dev = hub->intfdev;
1230 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001231 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 unsigned int pipe;
1233 int maxp, ret;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001234 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Alan Sternd697cdd2009-10-27 15:18:46 -04001236 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 ret = -ENOMEM;
1239 goto fail;
1240 }
1241
1242 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1243 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 ret = -ENOMEM;
1245 goto fail;
1246 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001247 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1250 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 ret = -ENOMEM;
1252 goto fail;
1253 }
1254
1255 /* Request the entire hub descriptor.
1256 * hub->descriptor can handle USB_MAXCHILDREN ports,
1257 * but the hub can/will return fewer bytes here.
1258 */
John Youndbe79bb2001-09-17 00:00:00 -07001259 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (ret < 0) {
1261 message = "can't read hub descriptor";
1262 goto fail;
1263 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1264 message = "hub has too many ports!";
1265 ret = -ENODEV;
1266 goto fail;
1267 }
1268
1269 hdev->maxchild = hub->descriptor->bNbrPorts;
1270 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1271 (hdev->maxchild == 1) ? "" : "s");
1272
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001273 hdev->children = kzalloc(hdev->maxchild *
1274 sizeof(struct usb_device *), GFP_KERNEL);
Lan Tianyu336c5c32012-07-06 14:13:52 +08001275 hub->port_owners = kzalloc(hdev->maxchild * sizeof(struct dev_state *),
1276 GFP_KERNEL);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001277 if (!hdev->children || !hub->port_owners) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001278 ret = -ENOMEM;
1279 goto fail;
1280 }
1281
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001282 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
John Youndbe79bb2001-09-17 00:00:00 -07001284 /* FIXME for USB 3.0, skip for now */
1285 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1286 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int i;
1288 char portstr [USB_MAXCHILDREN + 1];
1289
1290 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001291 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1293 ? 'F' : 'R';
1294 portstr[hdev->maxchild] = 0;
1295 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1296 } else
1297 dev_dbg(hub_dev, "standalone hub\n");
1298
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001299 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301300 case HUB_CHAR_COMMON_LPSM:
1301 dev_dbg(hub_dev, "ganged power switching\n");
1302 break;
1303 case HUB_CHAR_INDV_PORT_LPSM:
1304 dev_dbg(hub_dev, "individual port power switching\n");
1305 break;
1306 case HUB_CHAR_NO_LPSM:
1307 case HUB_CHAR_LPSM:
1308 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1309 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001312 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301313 case HUB_CHAR_COMMON_OCPM:
1314 dev_dbg(hub_dev, "global over-current protection\n");
1315 break;
1316 case HUB_CHAR_INDV_PORT_OCPM:
1317 dev_dbg(hub_dev, "individual port over-current protection\n");
1318 break;
1319 case HUB_CHAR_NO_OCPM:
1320 case HUB_CHAR_OCPM:
1321 dev_dbg(hub_dev, "no over-current protection\n");
1322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
1325 spin_lock_init (&hub->tt.lock);
1326 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001327 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301329 case USB_HUB_PR_FS:
1330 break;
1331 case USB_HUB_PR_HS_SINGLE_TT:
1332 dev_dbg(hub_dev, "Single TT\n");
1333 hub->tt.hub = hdev;
1334 break;
1335 case USB_HUB_PR_HS_MULTI_TT:
1336 ret = usb_set_interface(hdev, 0, 1);
1337 if (ret == 0) {
1338 dev_dbg(hub_dev, "TT per port\n");
1339 hub->tt.multi = 1;
1340 } else
1341 dev_err(hub_dev, "Using single TT (err %d)\n",
1342 ret);
1343 hub->tt.hub = hdev;
1344 break;
1345 case USB_HUB_PR_SS:
1346 /* USB 3.0 hubs don't have a TT */
1347 break;
1348 default:
1349 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1350 hdev->descriptor.bDeviceProtocol);
1351 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001354 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001355 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001356 case HUB_TTTT_8_BITS:
1357 if (hdev->descriptor.bDeviceProtocol != 0) {
1358 hub->tt.think_time = 666;
1359 dev_dbg(hub_dev, "TT requires at most %d "
1360 "FS bit times (%d ns)\n",
1361 8, hub->tt.think_time);
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001364 case HUB_TTTT_16_BITS:
1365 hub->tt.think_time = 666 * 2;
1366 dev_dbg(hub_dev, "TT requires at most %d "
1367 "FS bit times (%d ns)\n",
1368 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001370 case HUB_TTTT_24_BITS:
1371 hub->tt.think_time = 666 * 3;
1372 dev_dbg(hub_dev, "TT requires at most %d "
1373 "FS bit times (%d ns)\n",
1374 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001376 case HUB_TTTT_32_BITS:
1377 hub->tt.think_time = 666 * 4;
1378 dev_dbg(hub_dev, "TT requires at most %d "
1379 "FS bit times (%d ns)\n",
1380 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 break;
1382 }
1383
1384 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001385 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 hub->has_indicators = 1;
1387 dev_dbg(hub_dev, "Port indicators are supported\n");
1388 }
1389
1390 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1391 hub->descriptor->bPwrOn2PwrGood * 2);
1392
1393 /* power budgeting mostly matters with bus-powered hubs,
1394 * and battery-powered root hubs (may provide just 8 mA).
1395 */
1396 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001397 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 message = "can't get hub status";
1399 goto fail;
1400 }
Alan Stern7d35b922005-04-25 11:18:32 -04001401 le16_to_cpus(&hubstatus);
1402 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001403 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1404 hub->mA_per_port = 500;
1405 else {
1406 hub->mA_per_port = hdev->bus_mA;
1407 hub->limited_power = 1;
1408 }
Alan Stern7d35b922005-04-25 11:18:32 -04001409 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1411 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001412 hub->limited_power = 1;
1413 if (hdev->maxchild > 0) {
1414 int remaining = hdev->bus_mA -
1415 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Alan Stern55c52712005-11-23 12:03:12 -05001417 if (remaining < hdev->maxchild * 100)
1418 dev_warn(hub_dev,
1419 "insufficient power available "
1420 "to use all downstream ports\n");
1421 hub->mA_per_port = 100; /* 7.2.1.1 */
1422 }
1423 } else { /* Self-powered external hub */
1424 /* FIXME: What about battery-powered external hubs that
1425 * provide less current per port? */
1426 hub->mA_per_port = 500;
1427 }
1428 if (hub->mA_per_port < 500)
1429 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1430 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001432 /* Update the HCD's internal representation of this hub before khubd
1433 * starts getting port status changes for devices under the hub.
1434 */
1435 hcd = bus_to_hcd(hdev->bus);
1436 if (hcd->driver->update_hub_device) {
1437 ret = hcd->driver->update_hub_device(hcd, hdev,
1438 &hub->tt, GFP_KERNEL);
1439 if (ret < 0) {
1440 message = "can't update HCD hub info";
1441 goto fail;
1442 }
1443 }
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1446 if (ret < 0) {
1447 message = "can't get hub status";
1448 goto fail;
1449 }
1450
1451 /* local power status reports aren't always correct */
1452 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1453 dev_dbg(hub_dev, "local power source is %s\n",
1454 (hubstatus & HUB_STATUS_LOCAL_POWER)
1455 ? "lost (inactive)" : "good");
1456
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001457 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 dev_dbg(hub_dev, "%sover-current condition exists\n",
1459 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1460
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001461 /* set up the interrupt endpoint
1462 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1463 * bytes as USB2.0[11.12.3] says because some hubs are known
1464 * to send more data (and thus cause overflow). For root hubs,
1465 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1466 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1468 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1469
1470 if (maxp > sizeof(*hub->buffer))
1471 maxp = sizeof(*hub->buffer);
1472
1473 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1474 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 ret = -ENOMEM;
1476 goto fail;
1477 }
1478
1479 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1480 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 /* maybe cycle the hub leds */
1483 if (hub->has_indicators && blinkenlights)
1484 hub->indicator [0] = INDICATOR_CYCLE;
1485
Alan Sternf2835212008-04-28 11:07:17 -04001486 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return 0;
1488
1489fail:
1490 dev_err (hub_dev, "config failed, %s (err %d)\n",
1491 message, ret);
1492 /* hub_disconnect() frees urb and descriptor */
1493 return ret;
1494}
1495
Alan Sterne8054852007-05-04 11:55:11 -04001496static void hub_release(struct kref *kref)
1497{
1498 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1499
1500 usb_put_intf(to_usb_interface(hub->intfdev));
1501 kfree(hub);
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504static unsigned highspeed_hubs;
1505
1506static void hub_disconnect(struct usb_interface *intf)
1507{
Huajun Li88162302012-03-12 21:00:19 +08001508 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001509 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Sterne8054852007-05-04 11:55:11 -04001510
1511 /* Take the hub off the event list and don't let it be added again */
1512 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001513 if (!list_empty(&hub->event_list)) {
1514 list_del_init(&hub->event_list);
1515 usb_autopm_put_interface_no_suspend(intf);
1516 }
Alan Sterne8054852007-05-04 11:55:11 -04001517 hub->disconnected = 1;
1518 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Alan Stern7de18d82006-06-01 13:37:24 -04001520 /* Disconnect all children and quiesce the hub */
1521 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001522 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001523
Alan Stern8b28c752005-08-10 17:04:13 -04001524 usb_set_intfdata (intf, NULL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001525 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Alan Sterne8054852007-05-04 11:55:11 -04001527 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 highspeed_hubs--;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 usb_free_urb(hub->urb);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001531 kfree(hdev->children);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001532 kfree(hub->port_owners);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001533 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001534 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001535 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Alan Sterne8054852007-05-04 11:55:11 -04001537 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
1540static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1541{
1542 struct usb_host_interface *desc;
1543 struct usb_endpoint_descriptor *endpoint;
1544 struct usb_device *hdev;
1545 struct usb_hub *hub;
1546
1547 desc = intf->cur_altsetting;
1548 hdev = interface_to_usbdev(intf);
1549
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001550 /* Hubs have proper suspend/resume support. */
1551 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001552
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001553 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001554 dev_err(&intf->dev,
1555 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001556 return -E2BIG;
1557 }
1558
David Brownell89ccbdc2006-04-02 10:18:09 -08001559#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1560 if (hdev->parent) {
1561 dev_warn(&intf->dev, "ignoring external hub\n");
1562 return -ENODEV;
1563 }
1564#endif
1565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 /* Some hubs have a subclass of 1, which AFAICT according to the */
1567 /* specs is not defined, but it works */
1568 if ((desc->desc.bInterfaceSubClass != 0) &&
1569 (desc->desc.bInterfaceSubClass != 1)) {
1570descriptor_error:
1571 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1572 return -EIO;
1573 }
1574
1575 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1576 if (desc->desc.bNumEndpoints != 1)
1577 goto descriptor_error;
1578
1579 endpoint = &desc->endpoint[0].desc;
1580
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001581 /* If it's not an interrupt in endpoint, we'd better punt! */
1582 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 goto descriptor_error;
1584
1585 /* We found a hub */
1586 dev_info (&intf->dev, "USB hub found\n");
1587
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001588 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (!hub) {
1590 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1591 return -ENOMEM;
1592 }
1593
Alan Sterne8054852007-05-04 11:55:11 -04001594 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 INIT_LIST_HEAD(&hub->event_list);
1596 hub->intfdev = &intf->dev;
1597 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001598 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001599 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001600 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001603 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (hdev->speed == USB_SPEED_HIGH)
1606 highspeed_hubs++;
1607
1608 if (hub_configure(hub, endpoint) >= 0)
1609 return 0;
1610
1611 hub_disconnect (intf);
1612 return -ENODEV;
1613}
1614
1615static int
1616hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1617{
1618 struct usb_device *hdev = interface_to_usbdev (intf);
1619
1620 /* assert ifno == 0 (part of hub spec) */
1621 switch (code) {
1622 case USBDEVFS_HUB_PORTINFO: {
1623 struct usbdevfs_hub_portinfo *info = user_data;
1624 int i;
1625
1626 spin_lock_irq(&device_state_lock);
1627 if (hdev->devnum <= 0)
1628 info->nports = 0;
1629 else {
1630 info->nports = hdev->maxchild;
1631 for (i = 0; i < info->nports; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001632 if (hdev->children[i] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 info->port[i] = 0;
1634 else
1635 info->port[i] =
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001636 hdev->children[i]->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638 }
1639 spin_unlock_irq(&device_state_lock);
1640
1641 return info->nports + 1;
1642 }
1643
1644 default:
1645 return -ENOSYS;
1646 }
1647}
1648
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001649/*
1650 * Allow user programs to claim ports on a hub. When a device is attached
1651 * to one of these "claimed" ports, the program will "own" the device.
1652 */
1653static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001654 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001655{
1656 if (hdev->state == USB_STATE_NOTATTACHED)
1657 return -ENODEV;
1658 if (port1 == 0 || port1 > hdev->maxchild)
1659 return -EINVAL;
1660
1661 /* This assumes that devices not managed by the hub driver
1662 * will always have maxchild equal to 0.
1663 */
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001664 *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001665 return 0;
1666}
1667
1668/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001669int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1670 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001671{
1672 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001673 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001674
1675 rc = find_port_owner(hdev, port1, &powner);
1676 if (rc)
1677 return rc;
1678 if (*powner)
1679 return -EBUSY;
1680 *powner = owner;
1681 return rc;
1682}
1683
Lan Tianyu336c5c32012-07-06 14:13:52 +08001684int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1685 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001686{
1687 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001688 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001689
1690 rc = find_port_owner(hdev, port1, &powner);
1691 if (rc)
1692 return rc;
1693 if (*powner != owner)
1694 return -ENOENT;
1695 *powner = NULL;
1696 return rc;
1697}
1698
Lan Tianyu336c5c32012-07-06 14:13:52 +08001699void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001700{
1701 int n;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001702 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001703
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001704 n = find_port_owner(hdev, 1, &powner);
1705 if (n == 0) {
1706 for (; n < hdev->maxchild; (++n, ++powner)) {
1707 if (*powner == owner)
1708 *powner = NULL;
1709 }
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001710 }
1711}
1712
1713/* The caller must hold udev's lock */
1714bool usb_device_is_owned(struct usb_device *udev)
1715{
1716 struct usb_hub *hub;
1717
1718 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1719 return false;
1720 hub = hdev_to_hub(udev->parent);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001721 return !!hub->port_owners[udev->portnum - 1];
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001722}
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1726{
1727 int i;
1728
1729 for (i = 0; i < udev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001730 if (udev->children[i])
1731 recursively_mark_NOTATTACHED(udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001733 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001734 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 udev->state = USB_STATE_NOTATTACHED;
1736}
1737
1738/**
1739 * usb_set_device_state - change a device's current state (usbcore, hcds)
1740 * @udev: pointer to device whose state should be changed
1741 * @new_state: new state value to be stored
1742 *
1743 * udev->state is _not_ fully protected by the device lock. Although
1744 * most transitions are made only while holding the lock, the state can
1745 * can change to USB_STATE_NOTATTACHED at almost any time. This
1746 * is so that devices can be marked as disconnected as soon as possible,
1747 * without having to wait for any semaphores to be released. As a result,
1748 * all changes to any device's state must be protected by the
1749 * device_state_lock spinlock.
1750 *
1751 * Once a device has been added to the device tree, all changes to its state
1752 * should be made using this routine. The state should _not_ be set directly.
1753 *
1754 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1755 * Otherwise udev->state is set to new_state, and if new_state is
1756 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1757 * to USB_STATE_NOTATTACHED.
1758 */
1759void usb_set_device_state(struct usb_device *udev,
1760 enum usb_device_state new_state)
1761{
1762 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001763 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 spin_lock_irqsave(&device_state_lock, flags);
1766 if (udev->state == USB_STATE_NOTATTACHED)
1767 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001768 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001769
1770 /* root hub wakeup capabilities are managed out-of-band
1771 * and may involve silicon errata ... ignore them here.
1772 */
1773 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001774 if (udev->state == USB_STATE_SUSPENDED
1775 || new_state == USB_STATE_SUSPENDED)
1776 ; /* No change to wakeup settings */
1777 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001778 wakeup = udev->actconfig->desc.bmAttributes
1779 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001780 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001781 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001782 }
Sarah Sharp15123002007-12-21 16:54:15 -08001783 if (udev->state == USB_STATE_SUSPENDED &&
1784 new_state != USB_STATE_SUSPENDED)
1785 udev->active_duration -= jiffies;
1786 else if (new_state == USB_STATE_SUSPENDED &&
1787 udev->state != USB_STATE_SUSPENDED)
1788 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001789 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001790 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 recursively_mark_NOTATTACHED(udev);
1792 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001793 if (wakeup >= 0)
1794 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
David Vrabel6da9c992009-02-18 14:43:47 +00001796EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001798/*
Alan Stern3b29b682011-02-22 09:53:41 -05001799 * Choose a device number.
1800 *
1801 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1802 * USB-2.0 buses they are also used as device addresses, however on
1803 * USB-3.0 buses the address is assigned by the controller hardware
1804 * and it usually is not the same as the device number.
1805 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001806 * WUSB devices are simple: they have no hubs behind, so the mapping
1807 * device <-> virtual port number becomes 1:1. Why? to simplify the
1808 * life of the device connection logic in
1809 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1810 * handshake we need to assign a temporary address in the unauthorized
1811 * space. For simplicity we use the first virtual port number found to
1812 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1813 * and that becomes it's address [X < 128] or its unauthorized address
1814 * [X | 0x80].
1815 *
1816 * We add 1 as an offset to the one-based USB-stack port number
1817 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1818 * 0 is reserved by USB for default address; (b) Linux's USB stack
1819 * uses always #1 for the root hub of the controller. So USB stack's
1820 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001821 *
1822 * Devices connected under xHCI are not as simple. The host controller
1823 * supports virtualization, so the hardware assigns device addresses and
1824 * the HCD must setup data structures before issuing a set address
1825 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001826 */
Alan Stern3b29b682011-02-22 09:53:41 -05001827static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 int devnum;
1830 struct usb_bus *bus = udev->bus;
1831
1832 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001833 if (udev->wusb) {
1834 devnum = udev->portnum + 1;
1835 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1836 } else {
1837 /* Try to allocate the next devnum beginning at
1838 * bus->devnum_next. */
1839 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1840 bus->devnum_next);
1841 if (devnum >= 128)
1842 devnum = find_next_zero_bit(bus->devmap.devicemap,
1843 128, 1);
1844 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (devnum < 128) {
1847 set_bit(devnum, bus->devmap.devicemap);
1848 udev->devnum = devnum;
1849 }
1850}
1851
Alan Stern3b29b682011-02-22 09:53:41 -05001852static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
1854 if (udev->devnum > 0) {
1855 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1856 udev->devnum = -1;
1857 }
1858}
1859
Alan Stern3b29b682011-02-22 09:53:41 -05001860static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001861{
1862 /* The address for a WUSB device is managed by wusbcore. */
1863 if (!udev->wusb)
1864 udev->devnum = devnum;
1865}
1866
Herbert Xuf7410ce2010-01-10 20:15:03 +11001867static void hub_free_dev(struct usb_device *udev)
1868{
1869 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1870
1871 /* Root hubs aren't real devices, so don't free HCD resources */
1872 if (hcd->driver->free_dev && udev->parent)
1873 hcd->driver->free_dev(hcd, udev);
1874}
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876/**
1877 * usb_disconnect - disconnect a device (usbcore-internal)
1878 * @pdev: pointer to device being disconnected
1879 * Context: !in_interrupt ()
1880 *
1881 * Something got disconnected. Get rid of it and all of its children.
1882 *
1883 * If *pdev is a normal device then the parent hub must already be locked.
1884 * If *pdev is a root hub then this routine will acquire the
1885 * usb_bus_list_lock on behalf of the caller.
1886 *
1887 * Only hub drivers (including virtual root hub drivers for host
1888 * controllers) should ever call this.
1889 *
1890 * This call is synchronous, and may not be used in an interrupt context.
1891 */
1892void usb_disconnect(struct usb_device **pdev)
1893{
1894 struct usb_device *udev = *pdev;
1895 int i;
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* mark the device as inactive, so any further urb submissions for
1898 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001899 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 */
1901 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001902 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1903 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001905 usb_lock_device(udev);
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001908 for (i = 0; i < udev->maxchild; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001909 if (udev->children[i])
1910 usb_disconnect(&udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
1913 /* deallocate hcd/hardware state ... nuking all pending urbs and
1914 * cleaning up all state associated with the current configuration
1915 * so that the hardware is now fully quiesced.
1916 */
Alan Stern782da722006-07-01 22:09:35 -04001917 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001919 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Alan Stern3b23dd62008-12-05 14:10:34 -05001921 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001922 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001923
Alan Stern782da722006-07-01 22:09:35 -04001924 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001925 * for de-configuring the device and invoking the remove-device
1926 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001927 */
1928 device_del(&udev->dev);
1929
1930 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * (or root_hub) pointer.
1932 */
Alan Stern3b29b682011-02-22 09:53:41 -05001933 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* Avoid races with recursively_mark_NOTATTACHED() */
1936 spin_lock_irq(&device_state_lock);
1937 *pdev = NULL;
1938 spin_unlock_irq(&device_state_lock);
1939
Herbert Xuf7410ce2010-01-10 20:15:03 +11001940 hub_free_dev(udev);
1941
Alan Stern782da722006-07-01 22:09:35 -04001942 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001945#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946static void show_string(struct usb_device *udev, char *id, char *string)
1947{
1948 if (!string)
1949 return;
1950 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1951}
1952
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001953static void announce_device(struct usb_device *udev)
1954{
1955 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1956 le16_to_cpu(udev->descriptor.idVendor),
1957 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001958 dev_info(&udev->dev,
1959 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001960 udev->descriptor.iManufacturer,
1961 udev->descriptor.iProduct,
1962 udev->descriptor.iSerialNumber);
1963 show_string(udev, "Product", udev->product);
1964 show_string(udev, "Manufacturer", udev->manufacturer);
1965 show_string(udev, "SerialNumber", udev->serial);
1966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001968static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969#endif
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971#ifdef CONFIG_USB_OTG
1972#include "otg_whitelist.h"
1973#endif
1974
Oliver Neukum3ede7602007-01-11 14:35:50 +01001975/**
Alan Stern8d8558d2009-12-08 15:50:41 -05001976 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01001977 * @udev: newly addressed device (in ADDRESS state)
1978 *
Alan Stern8d8558d2009-12-08 15:50:41 -05001979 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01001980 */
Alan Stern8d8558d2009-12-08 15:50:41 -05001981static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001983 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985#ifdef CONFIG_USB_OTG
1986 /*
1987 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1988 * to wake us after we've powered off VBUS; and HNP, switching roles
1989 * "host" to "peripheral". The OTG descriptor helps figure this out.
1990 */
1991 if (!udev->bus->is_b_host
1992 && udev->config
1993 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02001994 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 struct usb_bus *bus = udev->bus;
1996
1997 /* descriptor may appear anywhere in config */
1998 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1999 le16_to_cpu(udev->config[0].desc.wTotalLength),
2000 USB_DT_OTG, (void **) &desc) == 0) {
2001 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002002 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 dev_info(&udev->dev,
2005 "Dual-Role OTG device on %sHNP port\n",
2006 (port1 == bus->otg_port)
2007 ? "" : "non-");
2008
2009 /* enable HNP before suspend, it's simpler */
2010 if (port1 == bus->otg_port)
2011 bus->b_hnp_enable = 1;
2012 err = usb_control_msg(udev,
2013 usb_sndctrlpipe(udev, 0),
2014 USB_REQ_SET_FEATURE, 0,
2015 bus->b_hnp_enable
2016 ? USB_DEVICE_B_HNP_ENABLE
2017 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2018 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2019 if (err < 0) {
2020 /* OTG MESSAGE: report errors here,
2021 * customize to match your product.
2022 */
2023 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002024 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 err);
2026 bus->b_hnp_enable = 0;
2027 }
2028 }
2029 }
2030 }
2031
2032 if (!is_targeted(udev)) {
2033
2034 /* Maybe it can talk to us, though we can't talk to it.
2035 * (Includes HNP test device.)
2036 */
2037 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002038 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (err < 0)
2040 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2041 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002042 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 goto fail;
2044 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002045fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002047 return err;
2048}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002050
2051/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002052 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002053 * @udev: newly addressed device (in ADDRESS state)
2054 *
2055 * This is only called by usb_new_device() and usb_authorize_device()
2056 * and FIXME -- all comments that apply to them apply here wrt to
2057 * environment.
2058 *
2059 * If the device is WUSB and not authorized, we don't attempt to read
2060 * the string descriptors, as they will be errored out by the device
2061 * until it has been authorized.
2062 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002063static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002064{
2065 int err;
2066
2067 if (udev->config == NULL) {
2068 err = usb_get_configuration(udev);
2069 if (err < 0) {
2070 dev_err(&udev->dev, "can't read configurations, error %d\n",
2071 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002072 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002073 }
2074 }
2075 if (udev->wusb == 1 && udev->authorized == 0) {
2076 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2077 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2078 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2079 }
2080 else {
2081 /* read the standard strings and cache them if present */
2082 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2083 udev->manufacturer = usb_cache_string(udev,
2084 udev->descriptor.iManufacturer);
2085 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2086 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002087 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002088 if (err < 0)
2089 return err;
2090
2091 usb_detect_interface_quirks(udev);
2092
2093 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002094}
2095
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002096static void set_usb_port_removable(struct usb_device *udev)
2097{
2098 struct usb_device *hdev = udev->parent;
2099 struct usb_hub *hub;
2100 u8 port = udev->portnum;
2101 u16 wHubCharacteristics;
2102 bool removable = true;
2103
2104 if (!hdev)
2105 return;
2106
2107 hub = hdev_to_hub(udev->parent);
2108
2109 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2110
2111 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2112 return;
2113
2114 if (hub_is_superspeed(hdev)) {
2115 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2116 removable = false;
2117 } else {
2118 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2119 removable = false;
2120 }
2121
2122 if (removable)
2123 udev->removable = USB_DEVICE_REMOVABLE;
2124 else
2125 udev->removable = USB_DEVICE_FIXED;
2126}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002127
2128/**
2129 * usb_new_device - perform initial device setup (usbcore-internal)
2130 * @udev: newly addressed device (in ADDRESS state)
2131 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002132 * This is called with devices which have been detected but not fully
2133 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002134 * for any device configuration. The caller must have locked either
2135 * the parent hub (if udev is a normal device) or else the
2136 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2137 * udev has already been installed, but udev is not yet visible through
2138 * sysfs or other filesystem code.
2139 *
2140 * It will return if the device is configured properly or not. Zero if
2141 * the interface was registered with the driver core; else a negative
2142 * errno value.
2143 *
2144 * This call is synchronous, and may not be used in an interrupt context.
2145 *
2146 * Only the hub driver or root-hub registrar should ever call this.
2147 */
2148int usb_new_device(struct usb_device *udev)
2149{
2150 int err;
2151
Dan Streetman16985402010-01-06 09:56:53 -05002152 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002153 /* Initialize non-root-hub device wakeup to disabled;
2154 * device (un)configuration controls wakeup capable
2155 * sysfs power/wakeup controls wakeup enabled/disabled
2156 */
2157 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002158 }
2159
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002160 /* Tell the runtime-PM framework the device is active */
2161 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002162 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002163 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002164 pm_runtime_enable(&udev->dev);
2165
Alan Sternc08512c2010-11-15 15:57:58 -05002166 /* By default, forbid autosuspend for all devices. It will be
2167 * allowed for hubs during binding.
2168 */
2169 usb_disable_autosuspend(udev);
2170
Alan Stern8d8558d2009-12-08 15:50:41 -05002171 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002172 if (err < 0)
2173 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002174 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2175 udev->devnum, udev->bus->busnum,
2176 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002177 /* export the usbdev device-node for libusb */
2178 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2179 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2180
Alan Stern6cd13202008-11-13 15:08:30 -05002181 /* Tell the world! */
2182 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002183
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002184 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002185
2186 /*
2187 * check whether the hub marks this port as non-removable. Do it
2188 * now so that platform-specific data can override it in
2189 * device_add()
2190 */
2191 if (udev->parent)
2192 set_usb_port_removable(udev);
2193
Alan Stern782da722006-07-01 22:09:35 -04002194 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002195 * for configuring the device and invoking the add-device
2196 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002197 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002198 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (err) {
2200 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2201 goto fail;
2202 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002203
Alan Stern3b23dd62008-12-05 14:10:34 -05002204 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002205 usb_mark_last_busy(udev);
2206 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002207 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209fail:
2210 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002211 pm_runtime_disable(&udev->dev);
2212 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002216
2217/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002218 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2219 * @usb_dev: USB device
2220 *
2221 * Move the USB device to a very basic state where interfaces are disabled
2222 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002223 *
2224 * We share a lock (that we have) with device_del(), so we need to
2225 * defer its call.
2226 */
2227int usb_deauthorize_device(struct usb_device *usb_dev)
2228{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002229 usb_lock_device(usb_dev);
2230 if (usb_dev->authorized == 0)
2231 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002232
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002233 usb_dev->authorized = 0;
2234 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002235
2236 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002237 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002238 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002239 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002240 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002241 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002242
2243 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002244 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002245
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002246out_unauthorized:
2247 usb_unlock_device(usb_dev);
2248 return 0;
2249}
2250
2251
2252int usb_authorize_device(struct usb_device *usb_dev)
2253{
2254 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002255
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002256 usb_lock_device(usb_dev);
2257 if (usb_dev->authorized == 1)
2258 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002259
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002260 result = usb_autoresume_device(usb_dev);
2261 if (result < 0) {
2262 dev_err(&usb_dev->dev,
2263 "can't autoresume for authorization: %d\n", result);
2264 goto error_autoresume;
2265 }
2266 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2267 if (result < 0) {
2268 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2269 "authorization: %d\n", result);
2270 goto error_device_descriptor;
2271 }
Alan Sternda307122009-12-08 15:54:44 -05002272
2273 kfree(usb_dev->product);
2274 usb_dev->product = NULL;
2275 kfree(usb_dev->manufacturer);
2276 usb_dev->manufacturer = NULL;
2277 kfree(usb_dev->serial);
2278 usb_dev->serial = NULL;
2279
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002280 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002281 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002282 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002283 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002284 /* Choose and set the configuration. This registers the interfaces
2285 * with the driver core and lets interface drivers bind to them.
2286 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002287 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002288 if (c >= 0) {
2289 result = usb_set_configuration(usb_dev, c);
2290 if (result) {
2291 dev_err(&usb_dev->dev,
2292 "can't set config #%d, error %d\n", c, result);
2293 /* This need not be fatal. The user can try to
2294 * set other configurations. */
2295 }
2296 }
2297 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002298
Alan Stern8d8558d2009-12-08 15:50:41 -05002299error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002300error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002301 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002302error_autoresume:
2303out_authorized:
2304 usb_unlock_device(usb_dev); // complements locktree
2305 return result;
2306}
2307
2308
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002309/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2310static unsigned hub_is_wusb(struct usb_hub *hub)
2311{
2312 struct usb_hcd *hcd;
2313 if (hub->hdev->parent != NULL) /* not a root hub? */
2314 return 0;
2315 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2316 return hcd->wireless;
2317}
2318
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320#define PORT_RESET_TRIES 5
2321#define SET_ADDRESS_TRIES 2
2322#define GET_DESCRIPTOR_TRIES 2
2323#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302324#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2327#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002328#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329#define HUB_LONG_RESET_TIME 200
2330#define HUB_RESET_TIMEOUT 500
2331
Sarah Sharp10d674a2011-09-14 14:24:52 -07002332static int hub_port_reset(struct usb_hub *hub, int port1,
2333 struct usb_device *udev, unsigned int delay, bool warm);
2334
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002335/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2336 * Port worm reset is required to recover
2337 */
2338static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002339{
2340 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002341 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2342 USB_SS_PORT_LS_SS_INACTIVE) ||
2343 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2344 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002345}
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002348 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
2350 int delay_time, ret;
2351 u16 portstatus;
2352 u16 portchange;
2353
2354 for (delay_time = 0;
2355 delay_time < HUB_RESET_TIMEOUT;
2356 delay_time += delay) {
2357 /* wait to give the device a chance to reset */
2358 msleep(delay);
2359
2360 /* read and decode port status */
2361 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2362 if (ret < 0)
2363 return ret;
2364
Andiry Xu75d7cf72011-09-13 16:41:11 -07002365 /*
2366 * Some buggy devices require a warm reset to be issued even
2367 * when the port appears not to be connected.
2368 */
2369 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002370 /*
2371 * Some buggy devices can cause an NEC host controller
2372 * to transition to the "Error" state after a hot port
2373 * reset. This will show up as the port state in
2374 * "Inactive", and the port may also report a
2375 * disconnect. Forcing a warm port reset seems to make
2376 * the device work.
2377 *
2378 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2379 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002380 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002381 int ret;
2382
2383 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2384 clear_port_feature(hub->hdev, port1,
2385 USB_PORT_FEAT_C_CONNECTION);
2386 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2387 clear_port_feature(hub->hdev, port1,
2388 USB_PORT_FEAT_C_PORT_LINK_STATE);
2389 if (portchange & USB_PORT_STAT_C_RESET)
2390 clear_port_feature(hub->hdev, port1,
2391 USB_PORT_FEAT_C_RESET);
2392 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2393 port1);
2394 ret = hub_port_reset(hub, port1,
2395 udev, HUB_BH_RESET_TIME,
2396 true);
2397 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2398 clear_port_feature(hub->hdev, port1,
2399 USB_PORT_FEAT_C_CONNECTION);
2400 return ret;
2401 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002402 /* Device went away? */
2403 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2404 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Andiry Xu75d7cf72011-09-13 16:41:11 -07002406 /* bomb out completely if the connection bounced */
2407 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2408 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Andiry Xu75d7cf72011-09-13 16:41:11 -07002410 /* if we`ve finished resetting, then break out of
2411 * the loop
2412 */
2413 if (!(portstatus & USB_PORT_STAT_RESET) &&
2414 (portstatus & USB_PORT_STAT_ENABLE)) {
2415 if (hub_is_wusb(hub))
2416 udev->speed = USB_SPEED_WIRELESS;
2417 else if (hub_is_superspeed(hub->hdev))
2418 udev->speed = USB_SPEED_SUPER;
2419 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2420 udev->speed = USB_SPEED_HIGH;
2421 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2422 udev->speed = USB_SPEED_LOW;
2423 else
2424 udev->speed = USB_SPEED_FULL;
2425 return 0;
2426 }
2427 } else {
2428 if (portchange & USB_PORT_STAT_C_BH_RESET)
2429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
2431
2432 /* switch to the long delay after two short delay failures */
2433 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2434 delay = HUB_LONG_RESET_TIME;
2435
2436 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002437 "port %d not %sreset yet, waiting %dms\n",
2438 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440
2441 return -EBUSY;
2442}
2443
Andiry Xu75d7cf72011-09-13 16:41:11 -07002444static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2445 struct usb_device *udev, int *status, bool warm)
2446{
2447 switch (*status) {
2448 case 0:
2449 if (!warm) {
2450 struct usb_hcd *hcd;
2451 /* TRSTRCY = 10 ms; plus some extra */
2452 msleep(10 + 40);
2453 update_devnum(udev, 0);
2454 hcd = bus_to_hcd(udev->bus);
2455 if (hcd->driver->reset_device) {
2456 *status = hcd->driver->reset_device(hcd, udev);
2457 if (*status < 0) {
2458 dev_err(&udev->dev, "Cannot reset "
2459 "HCD device state\n");
2460 break;
2461 }
2462 }
2463 }
2464 /* FALL THROUGH */
2465 case -ENOTCONN:
2466 case -ENODEV:
2467 clear_port_feature(hub->hdev,
2468 port1, USB_PORT_FEAT_C_RESET);
2469 /* FIXME need disconnect() for NOTATTACHED device */
2470 if (warm) {
2471 clear_port_feature(hub->hdev, port1,
2472 USB_PORT_FEAT_C_BH_PORT_RESET);
2473 clear_port_feature(hub->hdev, port1,
2474 USB_PORT_FEAT_C_PORT_LINK_STATE);
2475 } else {
2476 usb_set_device_state(udev, *status
2477 ? USB_STATE_NOTATTACHED
2478 : USB_STATE_DEFAULT);
2479 }
2480 break;
2481 }
2482}
2483
2484/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002486 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
2488 int i, status;
2489
Andiry Xu75d7cf72011-09-13 16:41:11 -07002490 if (!warm) {
2491 /* Block EHCI CF initialization during the port reset.
2492 * Some companion controllers don't like it when they mix.
2493 */
2494 down_read(&ehci_cf_port_reset_rwsem);
2495 } else {
2496 if (!hub_is_superspeed(hub->hdev)) {
2497 dev_err(hub->intfdev, "only USB3 hub support "
2498 "warm reset\n");
2499 return -EINVAL;
2500 }
2501 }
Alan Stern32fe0192007-10-10 16:27:07 -04002502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 /* Reset the port */
2504 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002505 status = set_port_feature(hub->hdev, port1, (warm ?
2506 USB_PORT_FEAT_BH_PORT_RESET :
2507 USB_PORT_FEAT_RESET));
2508 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002510 "cannot %sreset port %d (err = %d)\n",
2511 warm ? "warm " : "", port1, status);
2512 } else {
2513 status = hub_port_wait_reset(hub, port1, udev, delay,
2514 warm);
David Brownelldd165252005-08-31 10:45:25 -07002515 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 dev_dbg(hub->intfdev,
2517 "port_wait_reset: err = %d\n",
2518 status);
2519 }
2520
2521 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002522 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2523 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002524 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
2526
2527 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002528 "port %d not enabled, trying %sreset again...\n",
2529 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 delay = HUB_LONG_RESET_TIME;
2531 }
2532
2533 dev_err (hub->intfdev,
2534 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2535 port1);
2536
Andiry Xu75d7cf72011-09-13 16:41:11 -07002537done:
2538 if (!warm)
2539 up_read(&ehci_cf_port_reset_rwsem);
2540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 return status;
2542}
2543
Andiry Xu0ed9a572011-04-27 18:07:43 +08002544/* Check if a port is power on */
2545static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2546{
2547 int ret = 0;
2548
2549 if (hub_is_superspeed(hub->hdev)) {
2550 if (portstatus & USB_SS_PORT_STAT_POWER)
2551 ret = 1;
2552 } else {
2553 if (portstatus & USB_PORT_STAT_POWER)
2554 ret = 1;
2555 }
2556
2557 return ret;
2558}
2559
Alan Sternd388dab2006-07-01 22:14:24 -04002560#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Andiry Xu0ed9a572011-04-27 18:07:43 +08002562/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2563static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2564{
2565 int ret = 0;
2566
2567 if (hub_is_superspeed(hub->hdev)) {
2568 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2569 == USB_SS_PORT_LS_U3)
2570 ret = 1;
2571 } else {
2572 if (portstatus & USB_PORT_STAT_SUSPEND)
2573 ret = 1;
2574 }
2575
2576 return ret;
2577}
Alan Sternb01b03f2008-04-28 11:06:11 -04002578
2579/* Determine whether the device on a port is ready for a normal resume,
2580 * is ready for a reset-resume, or should be disconnected.
2581 */
2582static int check_port_resume_type(struct usb_device *udev,
2583 struct usb_hub *hub, int port1,
2584 int status, unsigned portchange, unsigned portstatus)
2585{
2586 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002587 if (status || port_is_suspended(hub, portstatus) ||
2588 !port_is_power_on(hub, portstatus) ||
2589 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002590 if (status >= 0)
2591 status = -ENODEV;
2592 }
2593
Alan Stern86c57ed2008-06-30 11:14:43 -04002594 /* Can't do a normal resume if the port isn't enabled,
2595 * so try a reset-resume instead.
2596 */
2597 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2598 if (udev->persist_enabled)
2599 udev->reset_resume = 1;
2600 else
2601 status = -ENODEV;
2602 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002603
2604 if (status) {
2605 dev_dbg(hub->intfdev,
2606 "port %d status %04x.%04x after resume, %d\n",
2607 port1, portchange, portstatus, status);
2608 } else if (udev->reset_resume) {
2609
2610 /* Late port handoff can set status-change bits */
2611 if (portchange & USB_PORT_STAT_C_CONNECTION)
2612 clear_port_feature(hub->hdev, port1,
2613 USB_PORT_FEAT_C_CONNECTION);
2614 if (portchange & USB_PORT_STAT_C_ENABLE)
2615 clear_port_feature(hub->hdev, port1,
2616 USB_PORT_FEAT_C_ENABLE);
2617 }
2618
2619 return status;
2620}
2621
Sarah Sharpf74631e2012-06-25 12:08:08 -07002622int usb_disable_ltm(struct usb_device *udev)
2623{
2624 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2625
2626 /* Check if the roothub and device supports LTM. */
2627 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2628 !usb_device_supports_ltm(udev))
2629 return 0;
2630
2631 /* Clear Feature LTM Enable can only be sent if the device is
2632 * configured.
2633 */
2634 if (!udev->actconfig)
2635 return 0;
2636
2637 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2638 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2639 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2640 USB_CTRL_SET_TIMEOUT);
2641}
2642EXPORT_SYMBOL_GPL(usb_disable_ltm);
2643
2644void usb_enable_ltm(struct usb_device *udev)
2645{
2646 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2647
2648 /* Check if the roothub and device supports LTM. */
2649 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2650 !usb_device_supports_ltm(udev))
2651 return;
2652
2653 /* Set Feature LTM Enable can only be sent if the device is
2654 * configured.
2655 */
2656 if (!udev->actconfig)
2657 return;
2658
2659 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2660 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2661 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2662 USB_CTRL_SET_TIMEOUT);
2663}
2664EXPORT_SYMBOL_GPL(usb_enable_ltm);
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666#ifdef CONFIG_USB_SUSPEND
2667
2668/*
Alan Stern140d8f62006-07-01 22:07:21 -04002669 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002670 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002671 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 *
2673 * Suspends a USB device that isn't in active use, conserving power.
2674 * Devices may wake out of a suspend, if anything important happens,
2675 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002676 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 * to disconnect devices while they are suspended.
2678 *
David Brownell390a8c32005-09-13 19:57:27 -07002679 * This only affects the USB hardware for a device; its interfaces
2680 * (and, for hubs, child devices) must already have been suspended.
2681 *
Alan Stern624d6c02007-05-30 15:35:16 -04002682 * Selective port suspend reduces power; most suspended devices draw
2683 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2684 * All devices below the suspended port are also suspended.
2685 *
2686 * Devices leave suspend state when the host wakes them up. Some devices
2687 * also support "remote wakeup", where the device can activate the USB
2688 * tree above them to deliver data, such as a keypress or packet. In
2689 * some cases, this wakes the USB host.
2690 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 * Suspending OTG devices may trigger HNP, if that's been enabled
2692 * between a pair of dual-role devices. That will change roles, such
2693 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2694 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002695 * Devices on USB hub ports have only one "suspend" state, corresponding
2696 * to ACPI D2, "may cause the device to lose some context".
2697 * State transitions include:
2698 *
2699 * - suspend, resume ... when the VBUS power link stays live
2700 * - suspend, disconnect ... VBUS lost
2701 *
2702 * Once VBUS drop breaks the circuit, the port it's using has to go through
2703 * normal re-enumeration procedures, starting with enabling VBUS power.
2704 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2705 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2706 * timer, no SRP, no requests through sysfs.
2707 *
2708 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2709 * the root hub for their bus goes into global suspend ... so we don't
2710 * (falsely) update the device power state to say it suspended.
2711 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 * Returns 0 on success, else negative errno.
2713 */
Alan Stern65bfd292008-11-25 16:39:18 -05002714int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Alan Stern624d6c02007-05-30 15:35:16 -04002716 struct usb_hub *hub = hdev_to_hub(udev->parent);
2717 int port1 = udev->portnum;
2718 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002719
Alan Stern624d6c02007-05-30 15:35:16 -04002720 /* enable remote wakeup when appropriate; this lets the device
2721 * wake up the upstream hub (including maybe the root hub).
2722 *
2723 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2724 * we don't explicitly enable it here.
2725 */
2726 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002727 if (!hub_is_superspeed(hub->hdev)) {
2728 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2729 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2730 USB_DEVICE_REMOTE_WAKEUP, 0,
2731 NULL, 0,
2732 USB_CTRL_SET_TIMEOUT);
2733 } else {
2734 /* Assume there's only one function on the USB 3.0
2735 * device and enable remote wake for the first
2736 * interface. FIXME if the interface association
2737 * descriptor shows there's more than one function.
2738 */
2739 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2740 USB_REQ_SET_FEATURE,
2741 USB_RECIP_INTERFACE,
2742 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002743 USB_INTRF_FUNC_SUSPEND_RW |
2744 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002745 NULL, 0,
2746 USB_CTRL_SET_TIMEOUT);
2747 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002748 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002749 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2750 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002751 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002752 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002753 return status;
2754 }
Alan Stern624d6c02007-05-30 15:35:16 -04002755 }
2756
Andiry Xu65580b432011-09-23 14:19:52 -07002757 /* disable USB2 hardware LPM */
2758 if (udev->usb2_hw_lpm_enabled == 1)
2759 usb_set_usb2_hardware_lpm(udev, 0);
2760
Sarah Sharpf74631e2012-06-25 12:08:08 -07002761 if (usb_disable_ltm(udev)) {
2762 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2763 __func__);
2764 return -ENOMEM;
2765 }
Sarah Sharp83060952012-05-02 14:25:52 -07002766 if (usb_unlocked_disable_lpm(udev)) {
2767 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2768 __func__);
2769 return -ENOMEM;
2770 }
2771
Alan Stern624d6c02007-05-30 15:35:16 -04002772 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002773 if (hub_is_superspeed(hub->hdev))
2774 status = set_port_feature(hub->hdev,
2775 port1 | (USB_SS_PORT_LS_U3 << 3),
2776 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002777 else
2778 status = set_port_feature(hub->hdev, port1,
2779 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002780 if (status) {
2781 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2782 port1, status);
2783 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002784 if (udev->do_remote_wakeup)
2785 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002786 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2787 USB_DEVICE_REMOTE_WAKEUP, 0,
2788 NULL, 0,
2789 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002790
Andiry Xuc3e751e2012-05-05 00:50:10 +08002791 /* Try to enable USB2 hardware LPM again */
2792 if (udev->usb2_hw_lpm_capable == 1)
2793 usb_set_usb2_hardware_lpm(udev, 1);
2794
Sarah Sharpf74631e2012-06-25 12:08:08 -07002795 /* Try to enable USB3 LTM and LPM again */
2796 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002797 usb_unlocked_enable_lpm(udev);
2798
Alan Sterncbb33002011-06-15 16:29:16 -04002799 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002800 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002801 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002802 } else {
2803 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002804 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2805 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2806 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002807 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2808 msleep(10);
2809 }
Alan Sternc08512c2010-11-15 15:57:58 -05002810 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002811 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
David Brownellf3f32532005-09-22 22:37:29 -07002813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814/*
David Brownell390a8c32005-09-13 19:57:27 -07002815 * If the USB "suspend" state is in use (rather than "global suspend"),
2816 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002817 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 * hardware resume signaling is finished, either because of selective
2819 * resume (by host) or remote wakeup (by device) ... now see what changed
2820 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002821 *
2822 * If @udev->reset_resume is set then the device is reset before the
2823 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 */
Alan Stern140d8f62006-07-01 22:07:21 -04002825static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826{
Alan Stern54515fe2007-05-30 15:38:58 -04002827 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 u16 devstatus;
2829
2830 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002831 dev_dbg(&udev->dev, "%s\n",
2832 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 /* usb ch9 identifies four variants of SUSPENDED, based on what
2835 * state the device resumes to. Linux currently won't see the
2836 * first two on the host side; they'd be inside hub_port_init()
2837 * during many timeouts, but khubd can't suspend until later.
2838 */
2839 usb_set_device_state(udev, udev->actconfig
2840 ? USB_STATE_CONFIGURED
2841 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Alan Stern54515fe2007-05-30 15:38:58 -04002843 /* 10.5.4.5 says not to reset a suspended port if the attached
2844 * device is enabled for remote wakeup. Hence the reset
2845 * operation is carried out here, after the port has been
2846 * resumed.
2847 */
2848 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002849 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002850 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 /* 10.5.4.5 says be sure devices in the tree are still there.
2853 * For now let's assume the device didn't go crazy on resume,
2854 * and device drivers will know about any resume quirks.
2855 */
Alan Stern54515fe2007-05-30 15:38:58 -04002856 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002857 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002858 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2859 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002860 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002861
2862 /* If a normal resume failed, try doing a reset-resume */
2863 if (status && !udev->reset_resume && udev->persist_enabled) {
2864 dev_dbg(&udev->dev, "retry with reset-resume\n");
2865 udev->reset_resume = 1;
2866 goto retry_reset_resume;
2867 }
Alan Stern54515fe2007-05-30 15:38:58 -04002868 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002869
Alan Stern624d6c02007-05-30 15:35:16 -04002870 if (status) {
2871 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2872 status);
2873 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002875 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 status = usb_control_msg(udev,
2877 usb_sndctrlpipe(udev, 0),
2878 USB_REQ_CLEAR_FEATURE,
2879 USB_RECIP_DEVICE,
2880 USB_DEVICE_REMOTE_WAKEUP, 0,
2881 NULL, 0,
2882 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002883 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002884 dev_dbg(&udev->dev,
2885 "disable remote wakeup, status %d\n",
2886 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890 return status;
2891}
2892
Alan Stern624d6c02007-05-30 15:35:16 -04002893/*
2894 * usb_port_resume - re-activate a suspended usb device's upstream port
2895 * @udev: device to re-activate, not a root hub
2896 * Context: must be able to sleep; device not locked; pm locks held
2897 *
2898 * This will re-activate the suspended device, increasing power usage
2899 * while letting drivers communicate again with its endpoints.
2900 * USB resume explicitly guarantees that the power session between
2901 * the host and the device is the same as it was when the device
2902 * suspended.
2903 *
Alan Sternfeccc302008-03-03 15:15:59 -05002904 * If @udev->reset_resume is set then this routine won't check that the
2905 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002906 * reset @udev. The end result is that a broken power session can be
2907 * recovered and @udev will appear to persist across a loss of VBUS power.
2908 *
2909 * For example, if a host controller doesn't maintain VBUS suspend current
2910 * during a system sleep or is reset when the system wakes up, all the USB
2911 * power sessions below it will be broken. This is especially troublesome
2912 * for mass-storage devices containing mounted filesystems, since the
2913 * device will appear to have disconnected and all the memory mappings
2914 * to it will be lost. Using the USB_PERSIST facility, the device can be
2915 * made to appear as if it had not disconnected.
2916 *
Ming Lei742120c2008-06-18 22:00:29 +08002917 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002918 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002919 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2920 * quite possible for a device to remain unaltered but its media to be
2921 * changed. If the user replaces a flash memory card while the system is
2922 * asleep, he will have only himself to blame when the filesystem on the
2923 * new card is corrupted and the system crashes.
2924 *
Alan Stern624d6c02007-05-30 15:35:16 -04002925 * Returns 0 on success, else negative errno.
2926 */
Alan Stern65bfd292008-11-25 16:39:18 -05002927int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Alan Stern624d6c02007-05-30 15:35:16 -04002929 struct usb_hub *hub = hdev_to_hub(udev->parent);
2930 int port1 = udev->portnum;
2931 int status;
2932 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05002933
2934 /* Skip the initial Clear-Suspend step for a remote wakeup */
2935 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08002936 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05002937 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2940
Alan Sternd5cbad42006-08-11 16:52:39 -04002941 set_bit(port1, hub->busy_bits);
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08002944 if (hub_is_superspeed(hub->hdev))
2945 status = set_port_feature(hub->hdev,
2946 port1 | (USB_SS_PORT_LS_U0 << 3),
2947 USB_PORT_FEAT_LINK_STATE);
2948 else
2949 status = clear_port_feature(hub->hdev,
2950 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002952 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
2953 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04002956 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02002957 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 msleep(25);
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2961 * stop resume signaling. Then finish the resume
2962 * sequence.
2963 */
Alan Sternd25450c2006-11-20 11:14:30 -05002964 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04002965
Alan Sternb01b03f2008-04-28 11:06:11 -04002966 /* TRSMRCY = 10 msec */
2967 msleep(10);
2968 }
Alan Stern54515fe2007-05-30 15:38:58 -04002969
Alan Sternb01b03f2008-04-28 11:06:11 -04002970 SuspendCleared:
2971 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08002972 if (hub_is_superspeed(hub->hdev)) {
2973 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2974 clear_port_feature(hub->hdev, port1,
2975 USB_PORT_FEAT_C_PORT_LINK_STATE);
2976 } else {
2977 if (portchange & USB_PORT_STAT_C_SUSPEND)
2978 clear_port_feature(hub->hdev, port1,
2979 USB_PORT_FEAT_C_SUSPEND);
2980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Alan Sternd5cbad42006-08-11 16:52:39 -04002983 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04002984
Alan Sternb01b03f2008-04-28 11:06:11 -04002985 status = check_port_resume_type(udev,
2986 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04002987 if (status == 0)
2988 status = finish_port_resume(udev);
2989 if (status < 0) {
2990 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2991 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07002992 } else {
2993 /* Try to enable USB2 hardware LPM */
2994 if (udev->usb2_hw_lpm_capable == 1)
2995 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07002996
Sarah Sharpf74631e2012-06-25 12:08:08 -07002997 /* Try to enable USB3 LTM and LPM */
2998 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002999 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003000 }
Andiry Xu65580b432011-09-23 14:19:52 -07003001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return status;
3003}
3004
Alan Stern8808f002008-04-28 11:06:55 -04003005/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003006int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 int status = 0;
3009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003011 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003012 status = usb_autoresume_device(udev);
3013 if (status == 0) {
3014 /* Let the drivers do their thing, then... */
3015 usb_autosuspend_device(udev);
3016 }
Alan Sternd25450c2006-11-20 11:14:30 -05003017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return status;
3019}
3020
Alan Sternd388dab2006-07-01 22:14:24 -04003021#else /* CONFIG_USB_SUSPEND */
3022
3023/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3024
Alan Stern65bfd292008-11-25 16:39:18 -05003025int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003026{
3027 return 0;
3028}
3029
Alan Sternb01b03f2008-04-28 11:06:11 -04003030/* However we may need to do a reset-resume */
3031
Alan Stern65bfd292008-11-25 16:39:18 -05003032int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003033{
Alan Sternb01b03f2008-04-28 11:06:11 -04003034 struct usb_hub *hub = hdev_to_hub(udev->parent);
3035 int port1 = udev->portnum;
3036 int status;
3037 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003038
Alan Sternb01b03f2008-04-28 11:06:11 -04003039 status = hub_port_status(hub, port1, &portstatus, &portchange);
3040 status = check_port_resume_type(udev,
3041 hub, port1, status, portchange, portstatus);
3042
3043 if (status) {
3044 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3045 hub_port_logical_disconnect(hub, port1);
3046 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003047 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003048 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003049 }
3050 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003051}
3052
Alan Sternd388dab2006-07-01 22:14:24 -04003053#endif
3054
David Brownelldb690872005-09-13 19:56:33 -07003055static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
3057 struct usb_hub *hub = usb_get_intfdata (intf);
3058 struct usb_device *hdev = hub->hdev;
3059 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003060 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
Alan Sterncbb33002011-06-15 16:29:16 -04003062 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3064 struct usb_device *udev;
3065
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003066 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04003067 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003068 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003069 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003070 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003073 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3074 /* Enable hub to send remote wakeup for all ports. */
3075 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3076 status = set_port_feature(hdev,
3077 port1 |
3078 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3079 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3080 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3081 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3082 }
3083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Harvey Harrison441b62c2008-03-03 16:08:34 -08003085 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003086
David Brownellc9f89fa2005-09-13 19:57:04 -07003087 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003088 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
3092static int hub_resume(struct usb_interface *intf)
3093{
Alan Stern5e6effa2008-03-03 15:15:51 -05003094 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Alan Stern5e6effa2008-03-03 15:15:51 -05003096 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003097 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 return 0;
3099}
3100
Alan Sternb41a60e2007-05-30 15:39:33 -04003101static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003102{
Alan Sternb41a60e2007-05-30 15:39:33 -04003103 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003104
Alan Stern5e6effa2008-03-03 15:15:51 -05003105 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003106 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003107 return 0;
3108}
3109
Alan Stern54515fe2007-05-30 15:38:58 -04003110/**
3111 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3112 * @rhdev: struct usb_device for the root hub
3113 *
3114 * The USB host controller driver calls this function when its root hub
3115 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003116 * has been reset. The routine marks @rhdev as having lost power.
3117 * When the hub driver is resumed it will take notice and carry out
3118 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3119 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003120 */
3121void usb_root_hub_lost_power(struct usb_device *rhdev)
3122{
3123 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3124 rhdev->reset_resume = 1;
3125}
3126EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3127
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003128static const char * const usb3_lpm_names[] = {
3129 "U0",
3130 "U1",
3131 "U2",
3132 "U3",
3133};
3134
3135/*
3136 * Send a Set SEL control transfer to the device, prior to enabling
3137 * device-initiated U1 or U2. This lets the device know the exit latencies from
3138 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3139 * packet from the host.
3140 *
3141 * This function will fail if the SEL or PEL values for udev are greater than
3142 * the maximum allowed values for the link state to be enabled.
3143 */
3144static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3145{
3146 struct usb_set_sel_req *sel_values;
3147 unsigned long long u1_sel;
3148 unsigned long long u1_pel;
3149 unsigned long long u2_sel;
3150 unsigned long long u2_pel;
3151 int ret;
3152
3153 /* Convert SEL and PEL stored in ns to us */
3154 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3155 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3156 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3157 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3158
3159 /*
3160 * Make sure that the calculated SEL and PEL values for the link
3161 * state we're enabling aren't bigger than the max SEL/PEL
3162 * value that will fit in the SET SEL control transfer.
3163 * Otherwise the device would get an incorrect idea of the exit
3164 * latency for the link state, and could start a device-initiated
3165 * U1/U2 when the exit latencies are too high.
3166 */
3167 if ((state == USB3_LPM_U1 &&
3168 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3169 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3170 (state == USB3_LPM_U2 &&
3171 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3172 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3173 dev_dbg(&udev->dev, "Device-initiated %s disabled due "
3174 "to long SEL %llu ms or PEL %llu ms\n",
3175 usb3_lpm_names[state], u1_sel, u1_pel);
3176 return -EINVAL;
3177 }
3178
3179 /*
3180 * If we're enabling device-initiated LPM for one link state,
3181 * but the other link state has a too high SEL or PEL value,
3182 * just set those values to the max in the Set SEL request.
3183 */
3184 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3185 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3186
3187 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3188 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3189
3190 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3191 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3192
3193 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3194 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3195
3196 /*
3197 * usb_enable_lpm() can be called as part of a failed device reset,
3198 * which may be initiated by an error path of a mass storage driver.
3199 * Therefore, use GFP_NOIO.
3200 */
3201 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3202 if (!sel_values)
3203 return -ENOMEM;
3204
3205 sel_values->u1_sel = u1_sel;
3206 sel_values->u1_pel = u1_pel;
3207 sel_values->u2_sel = cpu_to_le16(u2_sel);
3208 sel_values->u2_pel = cpu_to_le16(u2_pel);
3209
3210 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3211 USB_REQ_SET_SEL,
3212 USB_RECIP_DEVICE,
3213 0, 0,
3214 sel_values, sizeof *(sel_values),
3215 USB_CTRL_SET_TIMEOUT);
3216 kfree(sel_values);
3217 return ret;
3218}
3219
3220/*
3221 * Enable or disable device-initiated U1 or U2 transitions.
3222 */
3223static int usb_set_device_initiated_lpm(struct usb_device *udev,
3224 enum usb3_link_state state, bool enable)
3225{
3226 int ret;
3227 int feature;
3228
3229 switch (state) {
3230 case USB3_LPM_U1:
3231 feature = USB_DEVICE_U1_ENABLE;
3232 break;
3233 case USB3_LPM_U2:
3234 feature = USB_DEVICE_U2_ENABLE;
3235 break;
3236 default:
3237 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3238 __func__, enable ? "enable" : "disable");
3239 return -EINVAL;
3240 }
3241
3242 if (udev->state != USB_STATE_CONFIGURED) {
3243 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3244 "for unconfigured device.\n",
3245 __func__, enable ? "enable" : "disable",
3246 usb3_lpm_names[state]);
3247 return 0;
3248 }
3249
3250 if (enable) {
3251 /*
3252 * First, let the device know about the exit latencies
3253 * associated with the link state we're about to enable.
3254 */
3255 ret = usb_req_set_sel(udev, state);
3256 if (ret < 0) {
3257 dev_warn(&udev->dev, "Set SEL for device-initiated "
3258 "%s failed.\n", usb3_lpm_names[state]);
3259 return -EBUSY;
3260 }
3261 /*
3262 * Now send the control transfer to enable device-initiated LPM
3263 * for either U1 or U2.
3264 */
3265 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3266 USB_REQ_SET_FEATURE,
3267 USB_RECIP_DEVICE,
3268 feature,
3269 0, NULL, 0,
3270 USB_CTRL_SET_TIMEOUT);
3271 } else {
3272 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3273 USB_REQ_CLEAR_FEATURE,
3274 USB_RECIP_DEVICE,
3275 feature,
3276 0, NULL, 0,
3277 USB_CTRL_SET_TIMEOUT);
3278 }
3279 if (ret < 0) {
3280 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3281 enable ? "Enable" : "Disable",
3282 usb3_lpm_names[state]);
3283 return -EBUSY;
3284 }
3285 return 0;
3286}
3287
3288static int usb_set_lpm_timeout(struct usb_device *udev,
3289 enum usb3_link_state state, int timeout)
3290{
3291 int ret;
3292 int feature;
3293
3294 switch (state) {
3295 case USB3_LPM_U1:
3296 feature = USB_PORT_FEAT_U1_TIMEOUT;
3297 break;
3298 case USB3_LPM_U2:
3299 feature = USB_PORT_FEAT_U2_TIMEOUT;
3300 break;
3301 default:
3302 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3303 __func__);
3304 return -EINVAL;
3305 }
3306
3307 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3308 timeout != USB3_LPM_DEVICE_INITIATED) {
3309 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3310 "which is a reserved value.\n",
3311 usb3_lpm_names[state], timeout);
3312 return -EINVAL;
3313 }
3314
3315 ret = set_port_feature(udev->parent,
3316 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3317 feature);
3318 if (ret < 0) {
3319 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3320 "error code %i\n", usb3_lpm_names[state],
3321 timeout, ret);
3322 return -EBUSY;
3323 }
3324 if (state == USB3_LPM_U1)
3325 udev->u1_params.timeout = timeout;
3326 else
3327 udev->u2_params.timeout = timeout;
3328 return 0;
3329}
3330
3331/*
3332 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3333 * U1/U2 entry.
3334 *
3335 * We will attempt to enable U1 or U2, but there are no guarantees that the
3336 * control transfers to set the hub timeout or enable device-initiated U1/U2
3337 * will be successful.
3338 *
3339 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3340 * driver know about it. If that call fails, it should be harmless, and just
3341 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3342 */
3343static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3344 enum usb3_link_state state)
3345{
3346 int timeout;
3347
3348 /* We allow the host controller to set the U1/U2 timeout internally
3349 * first, so that it can change its schedule to account for the
3350 * additional latency to send data to a device in a lower power
3351 * link state.
3352 */
3353 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3354
3355 /* xHCI host controller doesn't want to enable this LPM state. */
3356 if (timeout == 0)
3357 return;
3358
3359 if (timeout < 0) {
3360 dev_warn(&udev->dev, "Could not enable %s link state, "
3361 "xHCI error %i.\n", usb3_lpm_names[state],
3362 timeout);
3363 return;
3364 }
3365
3366 if (usb_set_lpm_timeout(udev, state, timeout))
3367 /* If we can't set the parent hub U1/U2 timeout,
3368 * device-initiated LPM won't be allowed either, so let the xHCI
3369 * host know that this link state won't be enabled.
3370 */
3371 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3372
3373 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3374 else if (udev->actconfig)
3375 usb_set_device_initiated_lpm(udev, state, true);
3376
3377}
3378
3379/*
3380 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3381 * U1/U2 entry.
3382 *
3383 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3384 * If zero is returned, the parent will not allow the link to go into U1/U2.
3385 *
3386 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3387 * it won't have an effect on the bus link state because the parent hub will
3388 * still disallow device-initiated U1/U2 entry.
3389 *
3390 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3391 * possible. The result will be slightly more bus bandwidth will be taken up
3392 * (to account for U1/U2 exit latency), but it should be harmless.
3393 */
3394static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3395 enum usb3_link_state state)
3396{
3397 int feature;
3398
3399 switch (state) {
3400 case USB3_LPM_U1:
3401 feature = USB_PORT_FEAT_U1_TIMEOUT;
3402 break;
3403 case USB3_LPM_U2:
3404 feature = USB_PORT_FEAT_U2_TIMEOUT;
3405 break;
3406 default:
3407 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3408 __func__);
3409 return -EINVAL;
3410 }
3411
3412 if (usb_set_lpm_timeout(udev, state, 0))
3413 return -EBUSY;
3414
3415 usb_set_device_initiated_lpm(udev, state, false);
3416
3417 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3418 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3419 "bus schedule bandwidth may be impacted.\n",
3420 usb3_lpm_names[state]);
3421 return 0;
3422}
3423
3424/*
3425 * Disable hub-initiated and device-initiated U1 and U2 entry.
3426 * Caller must own the bandwidth_mutex.
3427 *
3428 * This will call usb_enable_lpm() on failure, which will decrement
3429 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3430 */
3431int usb_disable_lpm(struct usb_device *udev)
3432{
3433 struct usb_hcd *hcd;
3434
3435 if (!udev || !udev->parent ||
3436 udev->speed != USB_SPEED_SUPER ||
3437 !udev->lpm_capable)
3438 return 0;
3439
3440 hcd = bus_to_hcd(udev->bus);
3441 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3442 return 0;
3443
3444 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003445 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003446 return 0;
3447
3448 /* If LPM is enabled, attempt to disable it. */
3449 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3450 goto enable_lpm;
3451 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3452 goto enable_lpm;
3453
3454 return 0;
3455
3456enable_lpm:
3457 usb_enable_lpm(udev);
3458 return -EBUSY;
3459}
3460EXPORT_SYMBOL_GPL(usb_disable_lpm);
3461
3462/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3463int usb_unlocked_disable_lpm(struct usb_device *udev)
3464{
3465 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3466 int ret;
3467
3468 if (!hcd)
3469 return -EINVAL;
3470
3471 mutex_lock(hcd->bandwidth_mutex);
3472 ret = usb_disable_lpm(udev);
3473 mutex_unlock(hcd->bandwidth_mutex);
3474
3475 return ret;
3476}
3477EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3478
3479/*
3480 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3481 * xHCI host policy may prevent U1 or U2 from being enabled.
3482 *
3483 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3484 * until the lpm_disable_count drops to zero. Caller must own the
3485 * bandwidth_mutex.
3486 */
3487void usb_enable_lpm(struct usb_device *udev)
3488{
3489 struct usb_hcd *hcd;
3490
3491 if (!udev || !udev->parent ||
3492 udev->speed != USB_SPEED_SUPER ||
3493 !udev->lpm_capable)
3494 return;
3495
3496 udev->lpm_disable_count--;
3497 hcd = bus_to_hcd(udev->bus);
3498 /* Double check that we can both enable and disable LPM.
3499 * Device must be configured to accept set feature U1/U2 timeout.
3500 */
3501 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3502 !hcd->driver->disable_usb3_lpm_timeout)
3503 return;
3504
3505 if (udev->lpm_disable_count > 0)
3506 return;
3507
3508 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3509 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3510}
3511EXPORT_SYMBOL_GPL(usb_enable_lpm);
3512
3513/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3514void usb_unlocked_enable_lpm(struct usb_device *udev)
3515{
3516 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3517
3518 if (!hcd)
3519 return;
3520
3521 mutex_lock(hcd->bandwidth_mutex);
3522 usb_enable_lpm(udev);
3523 mutex_unlock(hcd->bandwidth_mutex);
3524}
3525EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3526
3527
Alan Sternd388dab2006-07-01 22:14:24 -04003528#else /* CONFIG_PM */
3529
Alan Sternf07600c2007-05-30 15:38:16 -04003530#define hub_suspend NULL
3531#define hub_resume NULL
3532#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003533
3534int usb_disable_lpm(struct usb_device *udev)
3535{
3536 return 0;
3537}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003538EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003539
3540void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003541EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003542
3543int usb_unlocked_disable_lpm(struct usb_device *udev)
3544{
3545 return 0;
3546}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003547EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003548
3549void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003550EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003551
3552int usb_disable_ltm(struct usb_device *udev)
3553{
3554 return 0;
3555}
3556EXPORT_SYMBOL_GPL(usb_disable_ltm);
3557
3558void usb_enable_ltm(struct usb_device *udev) { }
3559EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003560#endif
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562
3563/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3564 *
3565 * Between connect detection and reset signaling there must be a delay
3566 * of 100ms at least for debounce and power-settling. The corresponding
3567 * timer shall restart whenever the downstream port detects a disconnect.
3568 *
3569 * Apparently there are some bluetooth and irda-dongles and a number of
3570 * low-speed devices for which this debounce period may last over a second.
3571 * Not covered by the spec - but easy to deal with.
3572 *
3573 * This implementation uses a 1500ms total debounce timeout; if the
3574 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3575 * every 25ms for transient disconnects. When the port status has been
3576 * unchanged for 100ms it returns the port status.
3577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578static int hub_port_debounce(struct usb_hub *hub, int port1)
3579{
3580 int ret;
3581 int total_time, stable_time = 0;
3582 u16 portchange, portstatus;
3583 unsigned connection = 0xffff;
3584
3585 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3586 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3587 if (ret < 0)
3588 return ret;
3589
3590 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3591 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3592 stable_time += HUB_DEBOUNCE_STEP;
3593 if (stable_time >= HUB_DEBOUNCE_STABLE)
3594 break;
3595 } else {
3596 stable_time = 0;
3597 connection = portstatus & USB_PORT_STAT_CONNECTION;
3598 }
3599
3600 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3601 clear_port_feature(hub->hdev, port1,
3602 USB_PORT_FEAT_C_CONNECTION);
3603 }
3604
3605 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3606 break;
3607 msleep(HUB_DEBOUNCE_STEP);
3608 }
3609
3610 dev_dbg (hub->intfdev,
3611 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3612 port1, total_time, stable_time, portstatus);
3613
3614 if (stable_time < HUB_DEBOUNCE_STABLE)
3615 return -ETIMEDOUT;
3616 return portstatus;
3617}
3618
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003619void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
Alan Sternddeac4e2009-01-15 17:03:33 -05003621 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3622 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003623 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003625EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
3627#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3628#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3629
Alan Stern4326ed02007-07-30 17:08:43 -04003630static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
3632 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003633 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
Sarah Sharpc6515272009-04-27 19:57:26 -07003635 /*
3636 * The host controller will choose the device address,
3637 * instead of the core having chosen it earlier
3638 */
3639 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 return -EINVAL;
3641 if (udev->state == USB_STATE_ADDRESS)
3642 return 0;
3643 if (udev->state != USB_STATE_DEFAULT)
3644 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003645 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003646 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003647 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003648 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3649 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3650 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003652 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003653 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003655 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 }
3657 return retval;
3658}
3659
3660/* Reset device, (re)assign address, get device descriptor.
3661 * Device connection must be stable, no more debouncing needed.
3662 * Returns device in USB_STATE_ADDRESS, except on error.
3663 *
3664 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003665 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 * newly detected device that is not accessible through any global
3667 * pointers, it's not necessary to lock the device.
3668 */
3669static int
3670hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3671 int retry_counter)
3672{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003673 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
3675 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003676 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 int i, j, retval;
3678 unsigned delay = HUB_SHORT_RESET_TIME;
3679 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003680 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003681 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
3683 /* root hub ports have a slightly longer reset period
3684 * (from USB 2.0 spec, section 7.1.7.5)
3685 */
3686 if (!hdev->parent) {
3687 delay = HUB_ROOT_RESET_TIME;
3688 if (port1 == hdev->bus->otg_port)
3689 hdev->bus->b_hnp_enable = 0;
3690 }
3691
3692 /* Some low speed devices have problems with the quick delay, so */
3693 /* be a bit pessimistic with those devices. RHbug #23670 */
3694 if (oldspeed == USB_SPEED_LOW)
3695 delay = HUB_LONG_RESET_TIME;
3696
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003697 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
Luben Tuikov07194ab2011-02-11 11:33:10 -08003699 /* Reset the device; full speed may morph to high speed */
3700 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003701 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003702 if (retval < 0) /* error or disconnect */
3703 goto fail;
3704 /* success, speed is known */
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 retval = -ENODEV;
3707
3708 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3709 dev_dbg(&udev->dev, "device reset changed speed!\n");
3710 goto fail;
3711 }
3712 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3715 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003716 * For Wireless USB devices, ep0 max packet is always 512 (tho
3717 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 */
3719 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003720 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003721 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003722 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003725 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 break;
3727 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3728 /* to determine the ep0 maxpacket size, try to read
3729 * the device descriptor to get bMaxPacketSize0 and
3730 * then correct our initial guess.
3731 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003732 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 break;
3734 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003735 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 break;
3737 default:
3738 goto fail;
3739 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003740
3741 if (udev->speed == USB_SPEED_WIRELESS)
3742 speed = "variable speed Wireless";
3743 else
3744 speed = usb_speed_string(udev->speed);
3745
Sarah Sharpc6515272009-04-27 19:57:26 -07003746 if (udev->speed != USB_SPEED_SUPER)
3747 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003748 "%s %s USB device number %d using %s\n",
3749 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003750 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 /* Set up TT records, if needed */
3753 if (hdev->tt) {
3754 udev->tt = hdev->tt;
3755 udev->ttport = hdev->ttport;
3756 } else if (udev->speed != USB_SPEED_HIGH
3757 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003758 if (!hub->tt.hub) {
3759 dev_err(&udev->dev, "parent hub has no TT\n");
3760 retval = -EINVAL;
3761 goto fail;
3762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 udev->tt = &hub->tt;
3764 udev->ttport = port1;
3765 }
3766
3767 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3768 * Because device hardware and firmware is sometimes buggy in
3769 * this area, and this is how Linux has done it for ages.
3770 * Change it cautiously.
3771 *
3772 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3773 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3774 * so it may help with some non-standards-compliant devices.
3775 * Otherwise we start with SET_ADDRESS and then try to read the
3776 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3777 * value.
3778 */
3779 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003780 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 struct usb_device_descriptor *buf;
3782 int r = 0;
3783
3784#define GET_DESCRIPTOR_BUFSIZE 64
3785 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3786 if (!buf) {
3787 retval = -ENOMEM;
3788 continue;
3789 }
3790
Alan Sternb89ee192007-05-11 10:19:04 -04003791 /* Retry on all errors; some devices are flakey.
3792 * 255 is for WUSB devices, we actually need to use
3793 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 */
3795 for (j = 0; j < 3; ++j) {
3796 buf->bMaxPacketSize0 = 0;
3797 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3798 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3799 USB_DT_DEVICE << 8, 0,
3800 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003801 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003803 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 if (buf->bDescriptorType ==
3805 USB_DT_DEVICE) {
3806 r = 0;
3807 break;
3808 }
3809 /* FALL THROUGH */
3810 default:
3811 if (r == 0)
3812 r = -EPROTO;
3813 break;
3814 }
3815 if (r == 0)
3816 break;
3817 }
3818 udev->descriptor.bMaxPacketSize0 =
3819 buf->bMaxPacketSize0;
3820 kfree(buf);
3821
Andiry Xu75d7cf72011-09-13 16:41:11 -07003822 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 if (retval < 0) /* error or disconnect */
3824 goto fail;
3825 if (oldspeed != udev->speed) {
3826 dev_dbg(&udev->dev,
3827 "device reset changed speed!\n");
3828 retval = -ENODEV;
3829 goto fail;
3830 }
3831 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003832 dev_err(&udev->dev,
3833 "device descriptor read/64, error %d\n",
3834 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 retval = -EMSGSIZE;
3836 continue;
3837 }
3838#undef GET_DESCRIPTOR_BUFSIZE
3839 }
3840
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003841 /*
3842 * If device is WUSB, we already assigned an
3843 * unauthorized address in the Connect Ack sequence;
3844 * authorization will assign the final address.
3845 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003846 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003847 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3848 retval = hub_set_address(udev, devnum);
3849 if (retval >= 0)
3850 break;
3851 msleep(200);
3852 }
3853 if (retval < 0) {
3854 dev_err(&udev->dev,
3855 "device not accepting address %d, error %d\n",
3856 devnum, retval);
3857 goto fail;
3858 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003859 if (udev->speed == USB_SPEED_SUPER) {
3860 devnum = udev->devnum;
3861 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003862 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003863 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003864 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003865 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003866
3867 /* cope with hardware quirkiness:
3868 * - let SET_ADDRESS settle, some device hardware wants it
3869 * - read ep0 maxpacket even for high and low speed,
3870 */
3871 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003872 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
3876 retval = usb_get_device_descriptor(udev, 8);
3877 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003878 dev_err(&udev->dev,
3879 "device descriptor read/8, error %d\n",
3880 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 if (retval >= 0)
3882 retval = -EMSGSIZE;
3883 } else {
3884 retval = 0;
3885 break;
3886 }
3887 }
3888 if (retval)
3889 goto fail;
3890
Elric Fud8aec3d2012-03-26 21:16:02 +08003891 /*
3892 * Some superspeed devices have finished the link training process
3893 * and attached to a superspeed hub port, but the device descriptor
3894 * got from those devices show they aren't superspeed devices. Warm
3895 * reset the port attached by the devices can fix them.
3896 */
3897 if ((udev->speed == USB_SPEED_SUPER) &&
3898 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3899 dev_err(&udev->dev, "got a wrong device descriptor, "
3900 "warm reset device\n");
3901 hub_port_reset(hub, port1, udev,
3902 HUB_BH_RESET_TIME, true);
3903 retval = -EINVAL;
3904 goto fail;
3905 }
3906
Sarah Sharp6b403b02009-04-27 19:54:10 -07003907 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3908 udev->speed == USB_SPEED_SUPER)
3909 i = 512;
3910 else
3911 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003912 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003913 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003915 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 retval = -EMSGSIZE;
3917 goto fail;
3918 }
Alan Stern56626a72010-10-14 15:25:21 -04003919 if (udev->speed == USB_SPEED_FULL)
3920 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3921 else
3922 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003924 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
3926
3927 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3928 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003929 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3930 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (retval >= 0)
3932 retval = -ENOMSG;
3933 goto fail;
3934 }
3935
Andiry Xu1ff4df52011-09-23 14:19:48 -07003936 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3937 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003938 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08003939 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003940 usb_set_lpm_parameters(udev);
3941 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07003942 }
Andiry Xu3148bf02011-09-23 14:19:47 -07003943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08003945 /* notify HCD that we have a device connected and addressed */
3946 if (hcd->driver->update_device)
3947 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948fail:
Alan Stern4326ed02007-07-30 17:08:43 -04003949 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05003951 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04003952 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003953 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 return retval;
3955}
3956
3957static void
3958check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
3959{
3960 struct usb_qualifier_descriptor *qual;
3961 int status;
3962
Christoph Lametere94b1762006-12-06 20:33:17 -08003963 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 if (qual == NULL)
3965 return;
3966
3967 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
3968 qual, sizeof *qual);
3969 if (status == sizeof *qual) {
3970 dev_info(&udev->dev, "not running at top speed; "
3971 "connect to a high speed hub\n");
3972 /* hub LEDs are probably harder to miss than syslog */
3973 if (hub->has_indicators) {
3974 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003975 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 }
3977 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003978 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979}
3980
3981static unsigned
3982hub_power_remaining (struct usb_hub *hub)
3983{
3984 struct usb_device *hdev = hub->hdev;
3985 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05003986 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987
Alan Stern55c52712005-11-23 12:03:12 -05003988 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 return 0;
3990
Alan Stern55c52712005-11-23 12:03:12 -05003991 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
3992 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003993 struct usb_device *udev = hdev->children[port1 - 1];
Alan Stern55c52712005-11-23 12:03:12 -05003994 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 if (!udev)
3997 continue;
3998
Alan Stern55c52712005-11-23 12:03:12 -05003999 /* Unconfigured devices may not use more than 100mA,
4000 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004002 delta = udev->actconfig->desc.bMaxPower * 2;
4003 else if (port1 != udev->bus->otg_port || hdev->parent)
4004 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 else
Alan Stern55c52712005-11-23 12:03:12 -05004006 delta = 8;
4007 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004008 dev_warn(&udev->dev,
4009 "%dmA is over %umA budget for port %d!\n",
4010 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 remaining -= delta;
4012 }
4013 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004014 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4015 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 remaining = 0;
4017 }
4018 return remaining;
4019}
4020
4021/* Handle physical or logical connection change events.
4022 * This routine is called when:
4023 * a port connection-change occurs;
4024 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004025 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 * a firmware download)
4027 * caller already locked the hub
4028 */
4029static void hub_port_connect_change(struct usb_hub *hub, int port1,
4030 u16 portstatus, u16 portchange)
4031{
4032 struct usb_device *hdev = hub->hdev;
4033 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304034 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004035 unsigned wHubCharacteristics =
4036 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004037 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 dev_dbg (hub_dev,
4041 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004042 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
4044 if (hub->has_indicators) {
4045 set_port_led(hub, port1, HUB_LED_AUTO);
4046 hub->indicator[port1-1] = INDICATOR_AUTO;
4047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
4049#ifdef CONFIG_USB_OTG
4050 /* during HNP, don't repeat the debounce */
4051 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004052 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4053 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054#endif
4055
Alan Stern8808f002008-04-28 11:06:55 -04004056 /* Try to resuscitate an existing device */
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004057 udev = hdev->children[port1-1];
Alan Stern8808f002008-04-28 11:06:55 -04004058 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4059 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004060 usb_lock_device(udev);
4061 if (portstatus & USB_PORT_STAT_ENABLE) {
4062 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004063
4064#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004065 } else if (udev->state == USB_STATE_SUSPENDED &&
4066 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004067 /* For a suspended device, treat this as a
4068 * remote wakeup event.
4069 */
Alan Stern0534d462010-01-08 12:56:30 -05004070 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004071#endif
4072
4073 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004074 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004075 }
4076 usb_unlock_device(udev);
4077
4078 if (status == 0) {
4079 clear_bit(port1, hub->change_bits);
4080 return;
4081 }
4082 }
4083
Alan Stern24618b02008-04-28 11:06:28 -04004084 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004085 if (udev)
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004086 usb_disconnect(&hdev->children[port1-1]);
Alan Stern24618b02008-04-28 11:06:28 -04004087 clear_bit(port1, hub->change_bits);
4088
Alan Stern253e0572009-10-27 15:20:13 -04004089 /* We can forget about a "removed" device when there's a physical
4090 * disconnect or the connect status changes.
4091 */
4092 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4093 (portchange & USB_PORT_STAT_C_CONNECTION))
4094 clear_bit(port1, hub->removed_bits);
4095
Alan Stern5257d972008-09-22 14:43:08 -04004096 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4097 USB_PORT_STAT_C_ENABLE)) {
4098 status = hub_port_debounce(hub, port1);
4099 if (status < 0) {
4100 if (printk_ratelimit())
4101 dev_err(hub_dev, "connect-debounce failed, "
4102 "port %d disabled\n", port1);
4103 portstatus &= ~USB_PORT_STAT_CONNECTION;
4104 } else {
4105 portstatus = status;
4106 }
4107 }
4108
Richard Zhao925aa462012-07-11 11:09:28 +08004109 if (hcd->phy && !hdev->parent) {
4110 if (portstatus & USB_PORT_STAT_CONNECTION)
4111 usb_phy_notify_connect(hcd->phy, port1);
4112 else
4113 usb_phy_notify_disconnect(hcd->phy, port1);
4114 }
4115
Alan Stern253e0572009-10-27 15:20:13 -04004116 /* Return now if debouncing failed or nothing is connected or
4117 * the device was "removed".
4118 */
4119 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4120 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
4122 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004123 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004124 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 if (portstatus & USB_PORT_STAT_ENABLE)
4128 goto done;
4129 return;
4130 }
4131
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
4134 /* reallocate for each attempt, since references
4135 * to the previous one can escape in various ways
4136 */
4137 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4138 if (!udev) {
4139 dev_err (hub_dev,
4140 "couldn't allocate port %d usb_device\n",
4141 port1);
4142 goto done;
4143 }
4144
4145 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004146 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004147 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004148 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004149
Sarah Sharp131dec32010-12-06 21:00:19 -08004150 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4151 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004152 udev->speed = USB_SPEED_SUPER;
4153 else
4154 udev->speed = USB_SPEED_UNKNOWN;
4155
Alan Stern3b29b682011-02-22 09:53:41 -05004156 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004157 if (udev->devnum <= 0) {
4158 status = -ENOTCONN; /* Don't retry */
4159 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004160 }
4161
Sarah Sharpe7b77172009-04-27 19:54:26 -07004162 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 status = hub_port_init(hub, udev, port1, i);
4164 if (status < 0)
4165 goto loop;
4166
Phil Dibowitz93362a82010-07-22 00:05:01 +02004167 usb_detect_quirks(udev);
4168 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4169 msleep(1000);
4170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 /* consecutive bus-powered hubs aren't reliable; they can
4172 * violate the voltage drop budget. if the new child has
4173 * a "powered" LED, users should notice we didn't enable it
4174 * (without reading syslog), even without per-port LEDs
4175 * on the parent.
4176 */
4177 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004178 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 u16 devstat;
4180
4181 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4182 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004183 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 dev_dbg(&udev->dev, "get status %d ?\n", status);
4185 goto loop_disable;
4186 }
Alan Stern55c52712005-11-23 12:03:12 -05004187 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4189 dev_err(&udev->dev,
4190 "can't connect bus-powered hub "
4191 "to this port\n");
4192 if (hub->has_indicators) {
4193 hub->indicator[port1-1] =
4194 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004195 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 }
4197 status = -ENOTCONN; /* Don't retry */
4198 goto loop_disable;
4199 }
4200 }
4201
4202 /* check for devices running slower than they could */
4203 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4204 && udev->speed == USB_SPEED_FULL
4205 && highspeed_hubs != 0)
4206 check_highspeed (hub, udev, port1);
4207
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004208 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 * udev becomes globally accessible, although presumably
4210 * no one will look at it until hdev is unlocked.
4211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 status = 0;
4213
4214 /* We mustn't add new devices if the parent hub has
4215 * been disconnected; we would race with the
4216 * recursively_mark_NOTATTACHED() routine.
4217 */
4218 spin_lock_irq(&device_state_lock);
4219 if (hdev->state == USB_STATE_NOTATTACHED)
4220 status = -ENOTCONN;
4221 else
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004222 hdev->children[port1-1] = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 spin_unlock_irq(&device_state_lock);
4224
4225 /* Run it through the hoops (find a driver, etc) */
4226 if (!status) {
4227 status = usb_new_device(udev);
4228 if (status) {
4229 spin_lock_irq(&device_state_lock);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004230 hdev->children[port1-1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 spin_unlock_irq(&device_state_lock);
4232 }
4233 }
4234
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 if (status)
4236 goto loop_disable;
4237
4238 status = hub_power_remaining(hub);
4239 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004240 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
4242 return;
4243
4244loop_disable:
4245 hub_port_disable(hub, port1, 1);
4246loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004247 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004248 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004249 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004251 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 break;
4253 }
Alan Stern3a311552008-05-20 16:58:29 -04004254 if (hub->hdev->parent ||
4255 !hcd->driver->port_handed_over ||
4256 !(hcd->driver->port_handed_over)(hcd, port1))
4257 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4258 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
4260done:
4261 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304262 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4263 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264}
4265
Sarah Sharp714b07b2012-01-24 13:53:18 -08004266/* Returns 1 if there was a remote wakeup and a connect status change. */
4267static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004268 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004269{
4270 struct usb_device *hdev;
4271 struct usb_device *udev;
4272 int connect_change = 0;
4273 int ret;
4274
4275 hdev = hub->hdev;
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004276 udev = hdev->children[port-1];
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004277 if (!hub_is_superspeed(hdev)) {
4278 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4279 return 0;
4280 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4281 } else {
4282 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004283 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4284 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004285 return 0;
4286 }
4287
Sarah Sharp714b07b2012-01-24 13:53:18 -08004288 if (udev) {
4289 /* TRSMRCY = 10 msec */
4290 msleep(10);
4291
4292 usb_lock_device(udev);
4293 ret = usb_remote_wakeup(udev);
4294 usb_unlock_device(udev);
4295 if (ret < 0)
4296 connect_change = 1;
4297 } else {
4298 ret = -ENODEV;
4299 hub_port_disable(hub, port, 1);
4300 }
4301 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4302 port, ret);
4303 return connect_change;
4304}
4305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306static void hub_events(void)
4307{
4308 struct list_head *tmp;
4309 struct usb_device *hdev;
4310 struct usb_interface *intf;
4311 struct usb_hub *hub;
4312 struct device *hub_dev;
4313 u16 hubstatus;
4314 u16 hubchange;
4315 u16 portstatus;
4316 u16 portchange;
4317 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004318 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319
4320 /*
4321 * We restart the list every time to avoid a deadlock with
4322 * deleting hubs downstream from this one. This should be
4323 * safe since we delete the hub from the event list.
4324 * Not the most efficient, but avoids deadlocks.
4325 */
4326 while (1) {
4327
4328 /* Grab the first entry at the beginning of the list */
4329 spin_lock_irq(&hub_event_lock);
4330 if (list_empty(&hub_event_list)) {
4331 spin_unlock_irq(&hub_event_lock);
4332 break;
4333 }
4334
4335 tmp = hub_event_list.next;
4336 list_del_init(tmp);
4337
4338 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004339 kref_get(&hub->kref);
4340 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Alan Sterne8054852007-05-04 11:55:11 -04004342 hdev = hub->hdev;
4343 hub_dev = hub->intfdev;
4344 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004345 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 hdev->state, hub->descriptor
4347 ? hub->descriptor->bNbrPorts
4348 : 0,
4349 /* NOTE: expects max 15 ports... */
4350 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004351 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 /* Lock the device, then check to see if we were
4354 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004355 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004356 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004357 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
4359 /* If the hub has died, clean up after it */
4360 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004361 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004362 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 goto loop;
4364 }
4365
Alan Stern40f122f2006-11-09 14:44:33 -05004366 /* Autoresume */
4367 ret = usb_autopm_get_interface(intf);
4368 if (ret) {
4369 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004371 }
4372
4373 /* If this is an inactive hub, do nothing */
4374 if (hub->quiescing)
4375 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
4377 if (hub->error) {
4378 dev_dbg (hub_dev, "resetting for error %d\n",
4379 hub->error);
4380
Ming Lei742120c2008-06-18 22:00:29 +08004381 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 if (ret) {
4383 dev_dbg (hub_dev,
4384 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004385 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 }
4387
4388 hub->nerrors = 0;
4389 hub->error = 0;
4390 }
4391
4392 /* deal with port status changes */
4393 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4394 if (test_bit(i, hub->busy_bits))
4395 continue;
4396 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004397 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004399 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 continue;
4401
4402 ret = hub_port_status(hub, i,
4403 &portstatus, &portchange);
4404 if (ret < 0)
4405 continue;
4406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4408 clear_port_feature(hdev, i,
4409 USB_PORT_FEAT_C_CONNECTION);
4410 connect_change = 1;
4411 }
4412
4413 if (portchange & USB_PORT_STAT_C_ENABLE) {
4414 if (!connect_change)
4415 dev_dbg (hub_dev,
4416 "port %d enable change, "
4417 "status %08x\n",
4418 i, portstatus);
4419 clear_port_feature(hdev, i,
4420 USB_PORT_FEAT_C_ENABLE);
4421
4422 /*
4423 * EM interference sometimes causes badly
4424 * shielded USB devices to be shutdown by
4425 * the hub, this hack enables them again.
4426 * Works at least with mouse driver.
4427 */
4428 if (!(portstatus & USB_PORT_STAT_ENABLE)
4429 && !connect_change
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004430 && hdev->children[i-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 dev_err (hub_dev,
4432 "port %i "
4433 "disabled by hub (EMI?), "
4434 "re-enabling...\n",
4435 i);
4436 connect_change = 1;
4437 }
4438 }
4439
Sarah Sharp72937e12012-01-24 11:46:50 -08004440 if (hub_handle_remote_wakeup(hub, i,
4441 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004442 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004443
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004445 u16 status = 0;
4446 u16 unused;
4447
4448 dev_dbg(hub_dev, "over-current change on port "
4449 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 clear_port_feature(hdev, i,
4451 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004452 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004453 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004454 hub_port_status(hub, i, &status, &unused);
4455 if (status & USB_PORT_STAT_OVERCURRENT)
4456 dev_err(hub_dev, "over-current "
4457 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
4459
4460 if (portchange & USB_PORT_STAT_C_RESET) {
4461 dev_dbg (hub_dev,
4462 "reset change on port %d\n",
4463 i);
4464 clear_port_feature(hdev, i,
4465 USB_PORT_FEAT_C_RESET);
4466 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004467 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4468 hub_is_superspeed(hub->hdev)) {
4469 dev_dbg(hub_dev,
4470 "warm reset change on port %d\n",
4471 i);
4472 clear_port_feature(hdev, i,
4473 USB_PORT_FEAT_C_BH_PORT_RESET);
4474 }
John Youndbe79bb2001-09-17 00:00:00 -07004475 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4476 clear_port_feature(hub->hdev, i,
4477 USB_PORT_FEAT_C_PORT_LINK_STATE);
4478 }
4479 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4480 dev_warn(hub_dev,
4481 "config error on port %d\n",
4482 i);
4483 clear_port_feature(hub->hdev, i,
4484 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Andiry Xu5e467f62011-04-27 18:07:54 +08004487 /* Warm reset a USB3 protocol port if it's in
4488 * SS.Inactive state.
4489 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004490 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004491 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004492 hub_port_reset(hub, i, NULL,
4493 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004494 }
4495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 if (connect_change)
4497 hub_port_connect_change(hub, i,
4498 portstatus, portchange);
4499 } /* end for i */
4500
4501 /* deal with hub status changes */
4502 if (test_and_clear_bit(0, hub->event_bits) == 0)
4503 ; /* do nothing */
4504 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4505 dev_err (hub_dev, "get_hub_status failed\n");
4506 else {
4507 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4508 dev_dbg (hub_dev, "power change\n");
4509 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004510 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4511 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004512 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004513 else
4514 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 }
4516 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004517 u16 status = 0;
4518 u16 unused;
4519
4520 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004522 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004523 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004524 hub_hub_status(hub, &status, &unused);
4525 if (status & HUB_STATUS_OVERCURRENT)
4526 dev_err(hub_dev, "over-current "
4527 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 }
4529 }
4530
Alan Stern8e4ceb32009-12-07 13:01:37 -05004531 loop_autopm:
4532 /* Balance the usb_autopm_get_interface() above */
4533 usb_autopm_put_interface_no_suspend(intf);
4534 loop:
4535 /* Balance the usb_autopm_get_interface_no_resume() in
4536 * kick_khubd() and allow autosuspend.
4537 */
4538 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004539 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004541 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
4543 } /* end while (1) */
4544}
4545
4546static int hub_thread(void *__unused)
4547{
Alan Stern3bb1af52008-03-03 15:15:36 -05004548 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4549 * port handover. Otherwise it might see that a full-speed device
4550 * was gone before the EHCI controller had handed its port over to
4551 * the companion full-speed controller.
4552 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004553 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 do {
4556 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004557 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004558 !list_empty(&hub_event_list) ||
4559 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004560 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004562 pr_debug("%s: khubd exiting\n", usbcore_name);
4563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564}
4565
Németh Márton1e927d92010-01-10 15:34:53 +01004566static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4568 .bDeviceClass = USB_CLASS_HUB},
4569 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4570 .bInterfaceClass = USB_CLASS_HUB},
4571 { } /* Terminating entry */
4572};
4573
4574MODULE_DEVICE_TABLE (usb, hub_id_table);
4575
4576static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 .name = "hub",
4578 .probe = hub_probe,
4579 .disconnect = hub_disconnect,
4580 .suspend = hub_suspend,
4581 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004582 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004583 .pre_reset = hub_pre_reset,
4584 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004585 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004587 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588};
4589
4590int usb_hub_init(void)
4591{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 if (usb_register(&hub_driver) < 0) {
4593 printk(KERN_ERR "%s: can't register hub driver\n",
4594 usbcore_name);
4595 return -1;
4596 }
4597
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004598 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4599 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
4602 /* Fall through if kernel_thread failed */
4603 usb_deregister(&hub_driver);
4604 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4605
4606 return -1;
4607}
4608
4609void usb_hub_cleanup(void)
4610{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004611 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
4613 /*
4614 * Hub resources are freed for us by usb_deregister. It calls
4615 * usb_driver_purge on every device which in turn calls that
4616 * devices disconnect function if it is using this driver.
4617 * The hub_disconnect function takes care of releasing the
4618 * individual hub resources. -greg
4619 */
4620 usb_deregister(&hub_driver);
4621} /* usb_hub_cleanup() */
4622
Alan Sterneb764c42008-03-03 15:16:04 -05004623static int descriptors_changed(struct usb_device *udev,
4624 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
Alan Sterneb764c42008-03-03 15:16:04 -05004626 int changed = 0;
4627 unsigned index;
4628 unsigned serial_len = 0;
4629 unsigned len;
4630 unsigned old_length;
4631 int length;
4632 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Alan Sterneb764c42008-03-03 15:16:04 -05004634 if (memcmp(&udev->descriptor, old_device_descriptor,
4635 sizeof(*old_device_descriptor)) != 0)
4636 return 1;
4637
4638 /* Since the idVendor, idProduct, and bcdDevice values in the
4639 * device descriptor haven't changed, we will assume the
4640 * Manufacturer and Product strings haven't changed either.
4641 * But the SerialNumber string could be different (e.g., a
4642 * different flash card of the same brand).
4643 */
4644 if (udev->serial)
4645 serial_len = strlen(udev->serial) + 1;
4646
4647 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004649 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4650 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 }
Alan Sterneb764c42008-03-03 15:16:04 -05004652
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004653 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 if (buf == NULL) {
4655 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4656 /* assume the worst */
4657 return 1;
4658 }
4659 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004660 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4662 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004663 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 dev_dbg(&udev->dev, "config index %d, error %d\n",
4665 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004666 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 break;
4668 }
4669 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4670 != 0) {
4671 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004672 index,
4673 ((struct usb_config_descriptor *) buf)->
4674 bConfigurationValue);
4675 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 break;
4677 }
4678 }
Alan Sterneb764c42008-03-03 15:16:04 -05004679
4680 if (!changed && serial_len) {
4681 length = usb_string(udev, udev->descriptor.iSerialNumber,
4682 buf, serial_len);
4683 if (length + 1 != serial_len) {
4684 dev_dbg(&udev->dev, "serial string error %d\n",
4685 length);
4686 changed = 1;
4687 } else if (memcmp(buf, udev->serial, length) != 0) {
4688 dev_dbg(&udev->dev, "serial string changed\n");
4689 changed = 1;
4690 }
4691 }
4692
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004694 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
4697/**
Ming Lei742120c2008-06-18 22:00:29 +08004698 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4700 *
Alan Stern79efa092006-06-01 13:33:42 -04004701 * WARNING - don't use this routine to reset a composite device
4702 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004703 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 *
4705 * Do a port reset, reassign the device's address, and establish its
4706 * former operating configuration. If the reset fails, or the device's
4707 * descriptors change from their values before the reset, or the original
4708 * configuration and altsettings cannot be restored, a flag will be set
4709 * telling khubd to pretend the device has been disconnected and then
4710 * re-connected. All drivers will be unbound, and the device will be
4711 * re-enumerated and probed all over again.
4712 *
4713 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4714 * flagged for logical disconnection, or some other negative error code
4715 * if the reset wasn't even attempted.
4716 *
4717 * The caller must own the device lock. For example, it's safe to use
4718 * this from a driver probe() routine after downloading new firmware.
4719 * For calls that might not occur during probe(), drivers should lock
4720 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004721 *
4722 * Locking exception: This routine may also be called from within an
4723 * autoresume handler. Such usage won't conflict with other tasks
4724 * holding the device lock because these tasks should always call
4725 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 */
Ming Lei742120c2008-06-18 22:00:29 +08004727static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728{
4729 struct usb_device *parent_hdev = udev->parent;
4730 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004731 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004733 int i, ret = 0;
4734 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735
4736 if (udev->state == USB_STATE_NOTATTACHED ||
4737 udev->state == USB_STATE_SUSPENDED) {
4738 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4739 udev->state);
4740 return -EINVAL;
4741 }
4742
4743 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004744 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004745 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 return -EISDIR;
4747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 parent_hub = hdev_to_hub(parent_hdev);
4749
Sarah Sharpf74631e2012-06-25 12:08:08 -07004750 /* Disable LPM and LTM while we reset the device and reinstall the alt
4751 * settings. Device-initiated LPM settings, and system exit latency
4752 * settings are cleared when the device is reset, so we have to set
4753 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004754 */
4755 ret = usb_unlocked_disable_lpm(udev);
4756 if (ret) {
4757 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4758 goto re_enumerate;
4759 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004760 ret = usb_disable_ltm(udev);
4761 if (ret) {
4762 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4763 __func__);
4764 goto re_enumerate;
4765 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004766
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 set_bit(port1, parent_hub->busy_bits);
4768 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4769
4770 /* ep0 maxpacket size may change; let the HCD know about it.
4771 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004772 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004774 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 break;
4776 }
4777 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (ret < 0)
4780 goto re_enumerate;
4781
4782 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004783 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 dev_info(&udev->dev, "device firmware changed\n");
4785 udev->descriptor = descriptor; /* for disconnect() calls */
4786 goto re_enumerate;
4787 }
Alan Stern4fe03872009-02-26 10:21:02 -05004788
4789 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 if (!udev->actconfig)
4791 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004792
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004793 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004794 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4795 if (ret < 0) {
4796 dev_warn(&udev->dev,
4797 "Busted HC? Not enough HCD resources for "
4798 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004799 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004800 goto re_enumerate;
4801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4803 USB_REQ_SET_CONFIGURATION, 0,
4804 udev->actconfig->desc.bConfigurationValue, 0,
4805 NULL, 0, USB_CTRL_SET_TIMEOUT);
4806 if (ret < 0) {
4807 dev_err(&udev->dev,
4808 "can't restore configuration #%d (error=%d)\n",
4809 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004810 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 goto re_enumerate;
4812 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004813 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4815
Alan Stern4fe03872009-02-26 10:21:02 -05004816 /* Put interfaces back into the same altsettings as before.
4817 * Don't bother to send the Set-Interface request for interfaces
4818 * that were already in altsetting 0; besides being unnecessary,
4819 * many devices can't handle it. Instead just reset the host-side
4820 * endpoint state.
4821 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004823 struct usb_host_config *config = udev->actconfig;
4824 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 struct usb_interface_descriptor *desc;
4826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004828 if (desc->bAlternateSetting == 0) {
4829 usb_disable_interface(udev, intf, true);
4830 usb_enable_interface(udev, intf, true);
4831 ret = 0;
4832 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004833 /* Let the bandwidth allocation function know that this
4834 * device has been reset, and it will have to use
4835 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004836 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004837 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004838 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4839 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004840 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 if (ret < 0) {
4843 dev_err(&udev->dev, "failed to restore interface %d "
4844 "altsetting %d (error=%d)\n",
4845 desc->bInterfaceNumber,
4846 desc->bAlternateSetting,
4847 ret);
4848 goto re_enumerate;
4849 }
4850 }
4851
4852done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07004853 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04004854 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004855 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 return 0;
4857
4858re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004859 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 hub_port_logical_disconnect(parent_hub, port1);
4861 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862}
4863
4864/**
4865 * usb_reset_device - warn interface drivers and perform a USB port reset
4866 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4867 *
Alan Stern79efa092006-06-01 13:33:42 -04004868 * Warns all drivers bound to registered interfaces (using their pre_reset
4869 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08004870 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04004871 *
Alan Stern79efa092006-06-01 13:33:42 -04004872 * Return value is the same as for usb_reset_and_verify_device().
4873 *
4874 * The caller must own the device lock. For example, it's safe to use
4875 * this from a driver probe() routine after downloading new firmware.
4876 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08004877 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04004878 *
4879 * If an interface is currently being probed or disconnected, we assume
4880 * its driver knows how to handle resets. For all other interfaces,
4881 * if the driver doesn't have pre_reset and post_reset methods then
4882 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004883 */
Ming Lei742120c2008-06-18 22:00:29 +08004884int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004885{
4886 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004887 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004888 struct usb_host_config *config = udev->actconfig;
4889
4890 if (udev->state == USB_STATE_NOTATTACHED ||
4891 udev->state == USB_STATE_SUSPENDED) {
4892 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4893 udev->state);
4894 return -EINVAL;
4895 }
4896
Alan Stern645daaa2006-08-30 15:47:02 -04004897 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004898 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004899
Alan Stern79efa092006-06-01 13:33:42 -04004900 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004901 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004902 struct usb_interface *cintf = config->interface[i];
4903 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004904 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004905
4906 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004907 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004908 if (drv->pre_reset && drv->post_reset)
4909 unbind = (drv->pre_reset)(cintf);
4910 else if (cintf->condition ==
4911 USB_INTERFACE_BOUND)
4912 unbind = 1;
4913 if (unbind)
4914 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004915 }
4916 }
4917 }
4918
Ming Lei742120c2008-06-18 22:00:29 +08004919 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004920
4921 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004922 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004923 struct usb_interface *cintf = config->interface[i];
4924 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004925 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004926
Alan Stern78d9a482008-06-23 16:00:40 -04004927 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004928 drv = to_usb_driver(cintf->dev.driver);
4929 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004930 rebind = (drv->post_reset)(cintf);
4931 else if (cintf->condition ==
4932 USB_INTERFACE_BOUND)
4933 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04004934 }
Alan Stern6c640942008-10-21 15:40:03 -04004935 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04004936 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004937 }
4938 }
4939
Alan Stern94fcda12006-11-20 11:38:46 -05004940 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004941 return ret;
4942}
Ming Lei742120c2008-06-18 22:00:29 +08004943EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08004944
4945
4946/**
4947 * usb_queue_reset_device - Reset a USB device from an atomic context
4948 * @iface: USB interface belonging to the device to reset
4949 *
4950 * This function can be used to reset a USB device from an atomic
4951 * context, where usb_reset_device() won't work (as it blocks).
4952 *
4953 * Doing a reset via this method is functionally equivalent to calling
4954 * usb_reset_device(), except for the fact that it is delayed to a
4955 * workqueue. This means that any drivers bound to other interfaces
4956 * might be unbound, as well as users from usbfs in user space.
4957 *
4958 * Corner cases:
4959 *
4960 * - Scheduling two resets at the same time from two different drivers
4961 * attached to two different interfaces of the same device is
4962 * possible; depending on how the driver attached to each interface
4963 * handles ->pre_reset(), the second reset might happen or not.
4964 *
4965 * - If a driver is unbound and it had a pending reset, the reset will
4966 * be cancelled.
4967 *
4968 * - This function can be called during .probe() or .disconnect()
4969 * times. On return from .disconnect(), any pending resets will be
4970 * cancelled.
4971 *
4972 * There is no no need to lock/unlock the @reset_ws as schedule_work()
4973 * does its own.
4974 *
4975 * NOTE: We don't do any reference count tracking because it is not
4976 * needed. The lifecycle of the work_struct is tied to the
4977 * usb_interface. Before destroying the interface we cancel the
4978 * work_struct, so the fact that work_struct is queued and or
4979 * running means the interface (and thus, the device) exist and
4980 * are referenced.
4981 */
4982void usb_queue_reset_device(struct usb_interface *iface)
4983{
4984 schedule_work(&iface->reset_ws);
4985}
4986EXPORT_SYMBOL_GPL(usb_queue_reset_device);