blob: 7f380ff1f78645a3d727978e5b9a89bcec317207 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB hub driver.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/completion.h>
16#include <linux/sched.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020022#include <linux/usb/hcd.h>
Phil Dibowitz93362a82010-07-22 00:05:01 +020023#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070024#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010025#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Theodore Ts'ob04b3152012-07-04 11:22:20 -040027#include <linux/random.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;
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -070085 void **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);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001275 hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
1276 if (!hdev->children || !hub->port_owners) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001277 ret = -ENOMEM;
1278 goto fail;
1279 }
1280
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001281 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
John Youndbe79bb2001-09-17 00:00:00 -07001283 /* FIXME for USB 3.0, skip for now */
1284 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1285 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 int i;
1287 char portstr [USB_MAXCHILDREN + 1];
1288
1289 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001290 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1292 ? 'F' : 'R';
1293 portstr[hdev->maxchild] = 0;
1294 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1295 } else
1296 dev_dbg(hub_dev, "standalone hub\n");
1297
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001298 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301299 case HUB_CHAR_COMMON_LPSM:
1300 dev_dbg(hub_dev, "ganged power switching\n");
1301 break;
1302 case HUB_CHAR_INDV_PORT_LPSM:
1303 dev_dbg(hub_dev, "individual port power switching\n");
1304 break;
1305 case HUB_CHAR_NO_LPSM:
1306 case HUB_CHAR_LPSM:
1307 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1308 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001311 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301312 case HUB_CHAR_COMMON_OCPM:
1313 dev_dbg(hub_dev, "global over-current protection\n");
1314 break;
1315 case HUB_CHAR_INDV_PORT_OCPM:
1316 dev_dbg(hub_dev, "individual port over-current protection\n");
1317 break;
1318 case HUB_CHAR_NO_OCPM:
1319 case HUB_CHAR_OCPM:
1320 dev_dbg(hub_dev, "no over-current protection\n");
1321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 }
1323
1324 spin_lock_init (&hub->tt.lock);
1325 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001326 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301328 case USB_HUB_PR_FS:
1329 break;
1330 case USB_HUB_PR_HS_SINGLE_TT:
1331 dev_dbg(hub_dev, "Single TT\n");
1332 hub->tt.hub = hdev;
1333 break;
1334 case USB_HUB_PR_HS_MULTI_TT:
1335 ret = usb_set_interface(hdev, 0, 1);
1336 if (ret == 0) {
1337 dev_dbg(hub_dev, "TT per port\n");
1338 hub->tt.multi = 1;
1339 } else
1340 dev_err(hub_dev, "Using single TT (err %d)\n",
1341 ret);
1342 hub->tt.hub = hdev;
1343 break;
1344 case USB_HUB_PR_SS:
1345 /* USB 3.0 hubs don't have a TT */
1346 break;
1347 default:
1348 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1349 hdev->descriptor.bDeviceProtocol);
1350 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001353 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001354 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001355 case HUB_TTTT_8_BITS:
1356 if (hdev->descriptor.bDeviceProtocol != 0) {
1357 hub->tt.think_time = 666;
1358 dev_dbg(hub_dev, "TT requires at most %d "
1359 "FS bit times (%d ns)\n",
1360 8, hub->tt.think_time);
1361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001363 case HUB_TTTT_16_BITS:
1364 hub->tt.think_time = 666 * 2;
1365 dev_dbg(hub_dev, "TT requires at most %d "
1366 "FS bit times (%d ns)\n",
1367 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001369 case HUB_TTTT_24_BITS:
1370 hub->tt.think_time = 666 * 3;
1371 dev_dbg(hub_dev, "TT requires at most %d "
1372 "FS bit times (%d ns)\n",
1373 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001375 case HUB_TTTT_32_BITS:
1376 hub->tt.think_time = 666 * 4;
1377 dev_dbg(hub_dev, "TT requires at most %d "
1378 "FS bit times (%d ns)\n",
1379 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
1381 }
1382
1383 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001384 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 hub->has_indicators = 1;
1386 dev_dbg(hub_dev, "Port indicators are supported\n");
1387 }
1388
1389 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1390 hub->descriptor->bPwrOn2PwrGood * 2);
1391
1392 /* power budgeting mostly matters with bus-powered hubs,
1393 * and battery-powered root hubs (may provide just 8 mA).
1394 */
1395 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001396 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 message = "can't get hub status";
1398 goto fail;
1399 }
Alan Stern7d35b922005-04-25 11:18:32 -04001400 le16_to_cpus(&hubstatus);
1401 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001402 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1403 hub->mA_per_port = 500;
1404 else {
1405 hub->mA_per_port = hdev->bus_mA;
1406 hub->limited_power = 1;
1407 }
Alan Stern7d35b922005-04-25 11:18:32 -04001408 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1410 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001411 hub->limited_power = 1;
1412 if (hdev->maxchild > 0) {
1413 int remaining = hdev->bus_mA -
1414 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Alan Stern55c52712005-11-23 12:03:12 -05001416 if (remaining < hdev->maxchild * 100)
1417 dev_warn(hub_dev,
1418 "insufficient power available "
1419 "to use all downstream ports\n");
1420 hub->mA_per_port = 100; /* 7.2.1.1 */
1421 }
1422 } else { /* Self-powered external hub */
1423 /* FIXME: What about battery-powered external hubs that
1424 * provide less current per port? */
1425 hub->mA_per_port = 500;
1426 }
1427 if (hub->mA_per_port < 500)
1428 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1429 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001431 /* Update the HCD's internal representation of this hub before khubd
1432 * starts getting port status changes for devices under the hub.
1433 */
1434 hcd = bus_to_hcd(hdev->bus);
1435 if (hcd->driver->update_hub_device) {
1436 ret = hcd->driver->update_hub_device(hcd, hdev,
1437 &hub->tt, GFP_KERNEL);
1438 if (ret < 0) {
1439 message = "can't update HCD hub info";
1440 goto fail;
1441 }
1442 }
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1445 if (ret < 0) {
1446 message = "can't get hub status";
1447 goto fail;
1448 }
1449
1450 /* local power status reports aren't always correct */
1451 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1452 dev_dbg(hub_dev, "local power source is %s\n",
1453 (hubstatus & HUB_STATUS_LOCAL_POWER)
1454 ? "lost (inactive)" : "good");
1455
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001456 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 dev_dbg(hub_dev, "%sover-current condition exists\n",
1458 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1459
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001460 /* set up the interrupt endpoint
1461 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1462 * bytes as USB2.0[11.12.3] says because some hubs are known
1463 * to send more data (and thus cause overflow). For root hubs,
1464 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1465 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1467 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1468
1469 if (maxp > sizeof(*hub->buffer))
1470 maxp = sizeof(*hub->buffer);
1471
1472 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1473 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 ret = -ENOMEM;
1475 goto fail;
1476 }
1477
1478 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1479 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 /* maybe cycle the hub leds */
1482 if (hub->has_indicators && blinkenlights)
1483 hub->indicator [0] = INDICATOR_CYCLE;
1484
Alan Sternf2835212008-04-28 11:07:17 -04001485 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return 0;
1487
1488fail:
1489 dev_err (hub_dev, "config failed, %s (err %d)\n",
1490 message, ret);
1491 /* hub_disconnect() frees urb and descriptor */
1492 return ret;
1493}
1494
Alan Sterne8054852007-05-04 11:55:11 -04001495static void hub_release(struct kref *kref)
1496{
1497 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1498
1499 usb_put_intf(to_usb_interface(hub->intfdev));
1500 kfree(hub);
1501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503static unsigned highspeed_hubs;
1504
1505static void hub_disconnect(struct usb_interface *intf)
1506{
Huajun Li88162302012-03-12 21:00:19 +08001507 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001508 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Sterne8054852007-05-04 11:55:11 -04001509
1510 /* Take the hub off the event list and don't let it be added again */
1511 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001512 if (!list_empty(&hub->event_list)) {
1513 list_del_init(&hub->event_list);
1514 usb_autopm_put_interface_no_suspend(intf);
1515 }
Alan Sterne8054852007-05-04 11:55:11 -04001516 hub->disconnected = 1;
1517 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Alan Stern7de18d82006-06-01 13:37:24 -04001519 /* Disconnect all children and quiesce the hub */
1520 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001521 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001522
Alan Stern8b28c752005-08-10 17:04:13 -04001523 usb_set_intfdata (intf, NULL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001524 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Alan Sterne8054852007-05-04 11:55:11 -04001526 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 highspeed_hubs--;
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 usb_free_urb(hub->urb);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001530 kfree(hdev->children);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001531 kfree(hub->port_owners);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001532 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001533 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001534 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Alan Sterne8054852007-05-04 11:55:11 -04001536 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
1538
1539static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1540{
1541 struct usb_host_interface *desc;
1542 struct usb_endpoint_descriptor *endpoint;
1543 struct usb_device *hdev;
1544 struct usb_hub *hub;
1545
1546 desc = intf->cur_altsetting;
1547 hdev = interface_to_usbdev(intf);
1548
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001549 /* Hubs have proper suspend/resume support. */
1550 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001551
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001552 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001553 dev_err(&intf->dev,
1554 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001555 return -E2BIG;
1556 }
1557
David Brownell89ccbdc2006-04-02 10:18:09 -08001558#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1559 if (hdev->parent) {
1560 dev_warn(&intf->dev, "ignoring external hub\n");
1561 return -ENODEV;
1562 }
1563#endif
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* Some hubs have a subclass of 1, which AFAICT according to the */
1566 /* specs is not defined, but it works */
1567 if ((desc->desc.bInterfaceSubClass != 0) &&
1568 (desc->desc.bInterfaceSubClass != 1)) {
1569descriptor_error:
1570 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1571 return -EIO;
1572 }
1573
1574 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1575 if (desc->desc.bNumEndpoints != 1)
1576 goto descriptor_error;
1577
1578 endpoint = &desc->endpoint[0].desc;
1579
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001580 /* If it's not an interrupt in endpoint, we'd better punt! */
1581 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 goto descriptor_error;
1583
1584 /* We found a hub */
1585 dev_info (&intf->dev, "USB hub found\n");
1586
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001587 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (!hub) {
1589 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1590 return -ENOMEM;
1591 }
1592
Alan Sterne8054852007-05-04 11:55:11 -04001593 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 INIT_LIST_HEAD(&hub->event_list);
1595 hub->intfdev = &intf->dev;
1596 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001597 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001598 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001599 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001602 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 if (hdev->speed == USB_SPEED_HIGH)
1605 highspeed_hubs++;
1606
1607 if (hub_configure(hub, endpoint) >= 0)
1608 return 0;
1609
1610 hub_disconnect (intf);
1611 return -ENODEV;
1612}
1613
1614static int
1615hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1616{
1617 struct usb_device *hdev = interface_to_usbdev (intf);
1618
1619 /* assert ifno == 0 (part of hub spec) */
1620 switch (code) {
1621 case USBDEVFS_HUB_PORTINFO: {
1622 struct usbdevfs_hub_portinfo *info = user_data;
1623 int i;
1624
1625 spin_lock_irq(&device_state_lock);
1626 if (hdev->devnum <= 0)
1627 info->nports = 0;
1628 else {
1629 info->nports = hdev->maxchild;
1630 for (i = 0; i < info->nports; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001631 if (hdev->children[i] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 info->port[i] = 0;
1633 else
1634 info->port[i] =
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001635 hdev->children[i]->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 }
1638 spin_unlock_irq(&device_state_lock);
1639
1640 return info->nports + 1;
1641 }
1642
1643 default:
1644 return -ENOSYS;
1645 }
1646}
1647
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001648/*
1649 * Allow user programs to claim ports on a hub. When a device is attached
1650 * to one of these "claimed" ports, the program will "own" the device.
1651 */
1652static int find_port_owner(struct usb_device *hdev, unsigned port1,
1653 void ***ppowner)
1654{
1655 if (hdev->state == USB_STATE_NOTATTACHED)
1656 return -ENODEV;
1657 if (port1 == 0 || port1 > hdev->maxchild)
1658 return -EINVAL;
1659
1660 /* This assumes that devices not managed by the hub driver
1661 * will always have maxchild equal to 0.
1662 */
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001663 *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001664 return 0;
1665}
1666
1667/* In the following three functions, the caller must hold hdev's lock */
1668int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
1669{
1670 int rc;
1671 void **powner;
1672
1673 rc = find_port_owner(hdev, port1, &powner);
1674 if (rc)
1675 return rc;
1676 if (*powner)
1677 return -EBUSY;
1678 *powner = owner;
1679 return rc;
1680}
1681
1682int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
1683{
1684 int rc;
1685 void **powner;
1686
1687 rc = find_port_owner(hdev, port1, &powner);
1688 if (rc)
1689 return rc;
1690 if (*powner != owner)
1691 return -ENOENT;
1692 *powner = NULL;
1693 return rc;
1694}
1695
1696void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
1697{
1698 int n;
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001699 void **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001700
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001701 n = find_port_owner(hdev, 1, &powner);
1702 if (n == 0) {
1703 for (; n < hdev->maxchild; (++n, ++powner)) {
1704 if (*powner == owner)
1705 *powner = NULL;
1706 }
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001707 }
1708}
1709
1710/* The caller must hold udev's lock */
1711bool usb_device_is_owned(struct usb_device *udev)
1712{
1713 struct usb_hub *hub;
1714
1715 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1716 return false;
1717 hub = hdev_to_hub(udev->parent);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001718 return !!hub->port_owners[udev->portnum - 1];
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1723{
1724 int i;
1725
1726 for (i = 0; i < udev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001727 if (udev->children[i])
1728 recursively_mark_NOTATTACHED(udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001730 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001731 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 udev->state = USB_STATE_NOTATTACHED;
1733}
1734
1735/**
1736 * usb_set_device_state - change a device's current state (usbcore, hcds)
1737 * @udev: pointer to device whose state should be changed
1738 * @new_state: new state value to be stored
1739 *
1740 * udev->state is _not_ fully protected by the device lock. Although
1741 * most transitions are made only while holding the lock, the state can
1742 * can change to USB_STATE_NOTATTACHED at almost any time. This
1743 * is so that devices can be marked as disconnected as soon as possible,
1744 * without having to wait for any semaphores to be released. As a result,
1745 * all changes to any device's state must be protected by the
1746 * device_state_lock spinlock.
1747 *
1748 * Once a device has been added to the device tree, all changes to its state
1749 * should be made using this routine. The state should _not_ be set directly.
1750 *
1751 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1752 * Otherwise udev->state is set to new_state, and if new_state is
1753 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1754 * to USB_STATE_NOTATTACHED.
1755 */
1756void usb_set_device_state(struct usb_device *udev,
1757 enum usb_device_state new_state)
1758{
1759 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001760 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 spin_lock_irqsave(&device_state_lock, flags);
1763 if (udev->state == USB_STATE_NOTATTACHED)
1764 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001765 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001766
1767 /* root hub wakeup capabilities are managed out-of-band
1768 * and may involve silicon errata ... ignore them here.
1769 */
1770 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001771 if (udev->state == USB_STATE_SUSPENDED
1772 || new_state == USB_STATE_SUSPENDED)
1773 ; /* No change to wakeup settings */
1774 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001775 wakeup = udev->actconfig->desc.bmAttributes
1776 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001777 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001778 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001779 }
Sarah Sharp15123002007-12-21 16:54:15 -08001780 if (udev->state == USB_STATE_SUSPENDED &&
1781 new_state != USB_STATE_SUSPENDED)
1782 udev->active_duration -= jiffies;
1783 else if (new_state == USB_STATE_SUSPENDED &&
1784 udev->state != USB_STATE_SUSPENDED)
1785 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001786 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001787 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 recursively_mark_NOTATTACHED(udev);
1789 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001790 if (wakeup >= 0)
1791 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
David Vrabel6da9c992009-02-18 14:43:47 +00001793EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001795/*
Alan Stern3b29b682011-02-22 09:53:41 -05001796 * Choose a device number.
1797 *
1798 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1799 * USB-2.0 buses they are also used as device addresses, however on
1800 * USB-3.0 buses the address is assigned by the controller hardware
1801 * and it usually is not the same as the device number.
1802 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001803 * WUSB devices are simple: they have no hubs behind, so the mapping
1804 * device <-> virtual port number becomes 1:1. Why? to simplify the
1805 * life of the device connection logic in
1806 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1807 * handshake we need to assign a temporary address in the unauthorized
1808 * space. For simplicity we use the first virtual port number found to
1809 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1810 * and that becomes it's address [X < 128] or its unauthorized address
1811 * [X | 0x80].
1812 *
1813 * We add 1 as an offset to the one-based USB-stack port number
1814 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1815 * 0 is reserved by USB for default address; (b) Linux's USB stack
1816 * uses always #1 for the root hub of the controller. So USB stack's
1817 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001818 *
1819 * Devices connected under xHCI are not as simple. The host controller
1820 * supports virtualization, so the hardware assigns device addresses and
1821 * the HCD must setup data structures before issuing a set address
1822 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001823 */
Alan Stern3b29b682011-02-22 09:53:41 -05001824static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 int devnum;
1827 struct usb_bus *bus = udev->bus;
1828
1829 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001830 if (udev->wusb) {
1831 devnum = udev->portnum + 1;
1832 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1833 } else {
1834 /* Try to allocate the next devnum beginning at
1835 * bus->devnum_next. */
1836 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1837 bus->devnum_next);
1838 if (devnum >= 128)
1839 devnum = find_next_zero_bit(bus->devmap.devicemap,
1840 128, 1);
1841 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (devnum < 128) {
1844 set_bit(devnum, bus->devmap.devicemap);
1845 udev->devnum = devnum;
1846 }
1847}
1848
Alan Stern3b29b682011-02-22 09:53:41 -05001849static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 if (udev->devnum > 0) {
1852 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1853 udev->devnum = -1;
1854 }
1855}
1856
Alan Stern3b29b682011-02-22 09:53:41 -05001857static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001858{
1859 /* The address for a WUSB device is managed by wusbcore. */
1860 if (!udev->wusb)
1861 udev->devnum = devnum;
1862}
1863
Herbert Xuf7410ce2010-01-10 20:15:03 +11001864static void hub_free_dev(struct usb_device *udev)
1865{
1866 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1867
1868 /* Root hubs aren't real devices, so don't free HCD resources */
1869 if (hcd->driver->free_dev && udev->parent)
1870 hcd->driver->free_dev(hcd, udev);
1871}
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/**
1874 * usb_disconnect - disconnect a device (usbcore-internal)
1875 * @pdev: pointer to device being disconnected
1876 * Context: !in_interrupt ()
1877 *
1878 * Something got disconnected. Get rid of it and all of its children.
1879 *
1880 * If *pdev is a normal device then the parent hub must already be locked.
1881 * If *pdev is a root hub then this routine will acquire the
1882 * usb_bus_list_lock on behalf of the caller.
1883 *
1884 * Only hub drivers (including virtual root hub drivers for host
1885 * controllers) should ever call this.
1886 *
1887 * This call is synchronous, and may not be used in an interrupt context.
1888 */
1889void usb_disconnect(struct usb_device **pdev)
1890{
1891 struct usb_device *udev = *pdev;
1892 int i;
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /* mark the device as inactive, so any further urb submissions for
1895 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001896 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 */
1898 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001899 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1900 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001902 usb_lock_device(udev);
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001905 for (i = 0; i < udev->maxchild; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001906 if (udev->children[i])
1907 usb_disconnect(&udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
1910 /* deallocate hcd/hardware state ... nuking all pending urbs and
1911 * cleaning up all state associated with the current configuration
1912 * so that the hardware is now fully quiesced.
1913 */
Alan Stern782da722006-07-01 22:09:35 -04001914 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001916 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Alan Stern3b23dd62008-12-05 14:10:34 -05001918 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001919 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001920
Alan Stern782da722006-07-01 22:09:35 -04001921 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001922 * for de-configuring the device and invoking the remove-device
1923 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001924 */
1925 device_del(&udev->dev);
1926
1927 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 * (or root_hub) pointer.
1929 */
Alan Stern3b29b682011-02-22 09:53:41 -05001930 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 /* Avoid races with recursively_mark_NOTATTACHED() */
1933 spin_lock_irq(&device_state_lock);
1934 *pdev = NULL;
1935 spin_unlock_irq(&device_state_lock);
1936
Herbert Xuf7410ce2010-01-10 20:15:03 +11001937 hub_free_dev(udev);
1938
Alan Stern782da722006-07-01 22:09:35 -04001939 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001942#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943static void show_string(struct usb_device *udev, char *id, char *string)
1944{
1945 if (!string)
1946 return;
1947 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1948}
1949
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001950static void announce_device(struct usb_device *udev)
1951{
1952 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1953 le16_to_cpu(udev->descriptor.idVendor),
1954 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001955 dev_info(&udev->dev,
1956 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001957 udev->descriptor.iManufacturer,
1958 udev->descriptor.iProduct,
1959 udev->descriptor.iSerialNumber);
1960 show_string(udev, "Product", udev->product);
1961 show_string(udev, "Manufacturer", udev->manufacturer);
1962 show_string(udev, "SerialNumber", udev->serial);
1963}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001965static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966#endif
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968#ifdef CONFIG_USB_OTG
1969#include "otg_whitelist.h"
1970#endif
1971
Oliver Neukum3ede7602007-01-11 14:35:50 +01001972/**
Alan Stern8d8558d2009-12-08 15:50:41 -05001973 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01001974 * @udev: newly addressed device (in ADDRESS state)
1975 *
Alan Stern8d8558d2009-12-08 15:50:41 -05001976 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01001977 */
Alan Stern8d8558d2009-12-08 15:50:41 -05001978static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001980 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982#ifdef CONFIG_USB_OTG
1983 /*
1984 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1985 * to wake us after we've powered off VBUS; and HNP, switching roles
1986 * "host" to "peripheral". The OTG descriptor helps figure this out.
1987 */
1988 if (!udev->bus->is_b_host
1989 && udev->config
1990 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02001991 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 struct usb_bus *bus = udev->bus;
1993
1994 /* descriptor may appear anywhere in config */
1995 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1996 le16_to_cpu(udev->config[0].desc.wTotalLength),
1997 USB_DT_OTG, (void **) &desc) == 0) {
1998 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05001999 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 dev_info(&udev->dev,
2002 "Dual-Role OTG device on %sHNP port\n",
2003 (port1 == bus->otg_port)
2004 ? "" : "non-");
2005
2006 /* enable HNP before suspend, it's simpler */
2007 if (port1 == bus->otg_port)
2008 bus->b_hnp_enable = 1;
2009 err = usb_control_msg(udev,
2010 usb_sndctrlpipe(udev, 0),
2011 USB_REQ_SET_FEATURE, 0,
2012 bus->b_hnp_enable
2013 ? USB_DEVICE_B_HNP_ENABLE
2014 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2015 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2016 if (err < 0) {
2017 /* OTG MESSAGE: report errors here,
2018 * customize to match your product.
2019 */
2020 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002021 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 err);
2023 bus->b_hnp_enable = 0;
2024 }
2025 }
2026 }
2027 }
2028
2029 if (!is_targeted(udev)) {
2030
2031 /* Maybe it can talk to us, though we can't talk to it.
2032 * (Includes HNP test device.)
2033 */
2034 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002035 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (err < 0)
2037 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2038 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002039 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto fail;
2041 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002042fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002044 return err;
2045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002047
2048/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002049 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002050 * @udev: newly addressed device (in ADDRESS state)
2051 *
2052 * This is only called by usb_new_device() and usb_authorize_device()
2053 * and FIXME -- all comments that apply to them apply here wrt to
2054 * environment.
2055 *
2056 * If the device is WUSB and not authorized, we don't attempt to read
2057 * the string descriptors, as they will be errored out by the device
2058 * until it has been authorized.
2059 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002060static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002061{
2062 int err;
2063
2064 if (udev->config == NULL) {
2065 err = usb_get_configuration(udev);
2066 if (err < 0) {
2067 dev_err(&udev->dev, "can't read configurations, error %d\n",
2068 err);
2069 goto fail;
2070 }
2071 }
2072 if (udev->wusb == 1 && udev->authorized == 0) {
2073 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2074 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2075 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2076 }
2077 else {
2078 /* read the standard strings and cache them if present */
2079 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2080 udev->manufacturer = usb_cache_string(udev,
2081 udev->descriptor.iManufacturer);
2082 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2083 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002084 err = usb_enumerate_device_otg(udev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002085fail:
2086 return err;
2087}
2088
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002089static void set_usb_port_removable(struct usb_device *udev)
2090{
2091 struct usb_device *hdev = udev->parent;
2092 struct usb_hub *hub;
2093 u8 port = udev->portnum;
2094 u16 wHubCharacteristics;
2095 bool removable = true;
2096
2097 if (!hdev)
2098 return;
2099
2100 hub = hdev_to_hub(udev->parent);
2101
2102 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2103
2104 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2105 return;
2106
2107 if (hub_is_superspeed(hdev)) {
2108 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2109 removable = false;
2110 } else {
2111 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2112 removable = false;
2113 }
2114
2115 if (removable)
2116 udev->removable = USB_DEVICE_REMOVABLE;
2117 else
2118 udev->removable = USB_DEVICE_FIXED;
2119}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002120
2121/**
2122 * usb_new_device - perform initial device setup (usbcore-internal)
2123 * @udev: newly addressed device (in ADDRESS state)
2124 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002125 * This is called with devices which have been detected but not fully
2126 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002127 * for any device configuration. The caller must have locked either
2128 * the parent hub (if udev is a normal device) or else the
2129 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2130 * udev has already been installed, but udev is not yet visible through
2131 * sysfs or other filesystem code.
2132 *
2133 * It will return if the device is configured properly or not. Zero if
2134 * the interface was registered with the driver core; else a negative
2135 * errno value.
2136 *
2137 * This call is synchronous, and may not be used in an interrupt context.
2138 *
2139 * Only the hub driver or root-hub registrar should ever call this.
2140 */
2141int usb_new_device(struct usb_device *udev)
2142{
2143 int err;
2144
Dan Streetman16985402010-01-06 09:56:53 -05002145 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002146 /* Initialize non-root-hub device wakeup to disabled;
2147 * device (un)configuration controls wakeup capable
2148 * sysfs power/wakeup controls wakeup enabled/disabled
2149 */
2150 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002151 }
2152
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002153 /* Tell the runtime-PM framework the device is active */
2154 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002155 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002156 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002157 pm_runtime_enable(&udev->dev);
2158
Alan Sternc08512c2010-11-15 15:57:58 -05002159 /* By default, forbid autosuspend for all devices. It will be
2160 * allowed for hubs during binding.
2161 */
2162 usb_disable_autosuspend(udev);
2163
Alan Stern8d8558d2009-12-08 15:50:41 -05002164 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002165 if (err < 0)
2166 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002167 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2168 udev->devnum, udev->bus->busnum,
2169 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002170 /* export the usbdev device-node for libusb */
2171 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2172 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2173
Alan Stern6cd13202008-11-13 15:08:30 -05002174 /* Tell the world! */
2175 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002176
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002177 if (udev->serial)
2178 add_device_randomness(udev->serial, strlen(udev->serial));
2179 if (udev->product)
2180 add_device_randomness(udev->product, strlen(udev->product));
2181 if (udev->manufacturer)
2182 add_device_randomness(udev->manufacturer,
2183 strlen(udev->manufacturer));
2184
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002185 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002186
2187 /*
2188 * check whether the hub marks this port as non-removable. Do it
2189 * now so that platform-specific data can override it in
2190 * device_add()
2191 */
2192 if (udev->parent)
2193 set_usb_port_removable(udev);
2194
Alan Stern782da722006-07-01 22:09:35 -04002195 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002196 * for configuring the device and invoking the add-device
2197 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002198 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002199 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (err) {
2201 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2202 goto fail;
2203 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002204
Alan Stern3b23dd62008-12-05 14:10:34 -05002205 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002206 usb_mark_last_busy(udev);
2207 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002208 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210fail:
2211 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002212 pm_runtime_disable(&udev->dev);
2213 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002214 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002217
2218/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002219 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2220 * @usb_dev: USB device
2221 *
2222 * Move the USB device to a very basic state where interfaces are disabled
2223 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002224 *
2225 * We share a lock (that we have) with device_del(), so we need to
2226 * defer its call.
2227 */
2228int usb_deauthorize_device(struct usb_device *usb_dev)
2229{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002230 usb_lock_device(usb_dev);
2231 if (usb_dev->authorized == 0)
2232 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002233
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002234 usb_dev->authorized = 0;
2235 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002236
2237 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002238 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002239 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002240 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002241 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002242 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002243
2244 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002245 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002246
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002247out_unauthorized:
2248 usb_unlock_device(usb_dev);
2249 return 0;
2250}
2251
2252
2253int usb_authorize_device(struct usb_device *usb_dev)
2254{
2255 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002256
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002257 usb_lock_device(usb_dev);
2258 if (usb_dev->authorized == 1)
2259 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002260
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002261 result = usb_autoresume_device(usb_dev);
2262 if (result < 0) {
2263 dev_err(&usb_dev->dev,
2264 "can't autoresume for authorization: %d\n", result);
2265 goto error_autoresume;
2266 }
2267 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2268 if (result < 0) {
2269 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2270 "authorization: %d\n", result);
2271 goto error_device_descriptor;
2272 }
Alan Sternda307122009-12-08 15:54:44 -05002273
2274 kfree(usb_dev->product);
2275 usb_dev->product = NULL;
2276 kfree(usb_dev->manufacturer);
2277 usb_dev->manufacturer = NULL;
2278 kfree(usb_dev->serial);
2279 usb_dev->serial = NULL;
2280
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002281 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002282 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002283 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002284 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002285 /* Choose and set the configuration. This registers the interfaces
2286 * with the driver core and lets interface drivers bind to them.
2287 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002288 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002289 if (c >= 0) {
2290 result = usb_set_configuration(usb_dev, c);
2291 if (result) {
2292 dev_err(&usb_dev->dev,
2293 "can't set config #%d, error %d\n", c, result);
2294 /* This need not be fatal. The user can try to
2295 * set other configurations. */
2296 }
2297 }
2298 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002299
Alan Stern8d8558d2009-12-08 15:50:41 -05002300error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002301error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002302 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002303error_autoresume:
2304out_authorized:
2305 usb_unlock_device(usb_dev); // complements locktree
2306 return result;
2307}
2308
2309
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002310/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2311static unsigned hub_is_wusb(struct usb_hub *hub)
2312{
2313 struct usb_hcd *hcd;
2314 if (hub->hdev->parent != NULL) /* not a root hub? */
2315 return 0;
2316 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2317 return hcd->wireless;
2318}
2319
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321#define PORT_RESET_TRIES 5
2322#define SET_ADDRESS_TRIES 2
2323#define GET_DESCRIPTOR_TRIES 2
2324#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302325#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2328#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002329#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330#define HUB_LONG_RESET_TIME 200
2331#define HUB_RESET_TIMEOUT 500
2332
Sarah Sharp10d674a2011-09-14 14:24:52 -07002333static int hub_port_reset(struct usb_hub *hub, int port1,
2334 struct usb_device *udev, unsigned int delay, bool warm);
2335
2336/* Is a USB 3.0 port in the Inactive state? */
2337static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus)
2338{
2339 return hub_is_superspeed(hub->hdev) &&
2340 (portstatus & USB_PORT_STAT_LINK_STATE) ==
2341 USB_SS_PORT_LS_SS_INACTIVE;
2342}
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002345 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 int delay_time, ret;
2348 u16 portstatus;
2349 u16 portchange;
2350
2351 for (delay_time = 0;
2352 delay_time < HUB_RESET_TIMEOUT;
2353 delay_time += delay) {
2354 /* wait to give the device a chance to reset */
2355 msleep(delay);
2356
2357 /* read and decode port status */
2358 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2359 if (ret < 0)
2360 return ret;
2361
Andiry Xu75d7cf72011-09-13 16:41:11 -07002362 /*
2363 * Some buggy devices require a warm reset to be issued even
2364 * when the port appears not to be connected.
2365 */
2366 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002367 /*
2368 * Some buggy devices can cause an NEC host controller
2369 * to transition to the "Error" state after a hot port
2370 * reset. This will show up as the port state in
2371 * "Inactive", and the port may also report a
2372 * disconnect. Forcing a warm port reset seems to make
2373 * the device work.
2374 *
2375 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2376 */
2377 if (hub_port_inactive(hub, portstatus)) {
2378 int ret;
2379
2380 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2381 clear_port_feature(hub->hdev, port1,
2382 USB_PORT_FEAT_C_CONNECTION);
2383 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2384 clear_port_feature(hub->hdev, port1,
2385 USB_PORT_FEAT_C_PORT_LINK_STATE);
2386 if (portchange & USB_PORT_STAT_C_RESET)
2387 clear_port_feature(hub->hdev, port1,
2388 USB_PORT_FEAT_C_RESET);
2389 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2390 port1);
2391 ret = hub_port_reset(hub, port1,
2392 udev, HUB_BH_RESET_TIME,
2393 true);
2394 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2395 clear_port_feature(hub->hdev, port1,
2396 USB_PORT_FEAT_C_CONNECTION);
2397 return ret;
2398 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002399 /* Device went away? */
2400 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2401 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Andiry Xu75d7cf72011-09-13 16:41:11 -07002403 /* bomb out completely if the connection bounced */
2404 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2405 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Andiry Xu75d7cf72011-09-13 16:41:11 -07002407 /* if we`ve finished resetting, then break out of
2408 * the loop
2409 */
2410 if (!(portstatus & USB_PORT_STAT_RESET) &&
2411 (portstatus & USB_PORT_STAT_ENABLE)) {
2412 if (hub_is_wusb(hub))
2413 udev->speed = USB_SPEED_WIRELESS;
2414 else if (hub_is_superspeed(hub->hdev))
2415 udev->speed = USB_SPEED_SUPER;
2416 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2417 udev->speed = USB_SPEED_HIGH;
2418 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2419 udev->speed = USB_SPEED_LOW;
2420 else
2421 udev->speed = USB_SPEED_FULL;
2422 return 0;
2423 }
2424 } else {
2425 if (portchange & USB_PORT_STAT_C_BH_RESET)
2426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428
2429 /* switch to the long delay after two short delay failures */
2430 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2431 delay = HUB_LONG_RESET_TIME;
2432
2433 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002434 "port %d not %sreset yet, waiting %dms\n",
2435 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437
2438 return -EBUSY;
2439}
2440
Andiry Xu75d7cf72011-09-13 16:41:11 -07002441static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2442 struct usb_device *udev, int *status, bool warm)
2443{
2444 switch (*status) {
2445 case 0:
2446 if (!warm) {
2447 struct usb_hcd *hcd;
2448 /* TRSTRCY = 10 ms; plus some extra */
2449 msleep(10 + 40);
2450 update_devnum(udev, 0);
2451 hcd = bus_to_hcd(udev->bus);
2452 if (hcd->driver->reset_device) {
2453 *status = hcd->driver->reset_device(hcd, udev);
2454 if (*status < 0) {
2455 dev_err(&udev->dev, "Cannot reset "
2456 "HCD device state\n");
2457 break;
2458 }
2459 }
2460 }
2461 /* FALL THROUGH */
2462 case -ENOTCONN:
2463 case -ENODEV:
2464 clear_port_feature(hub->hdev,
2465 port1, USB_PORT_FEAT_C_RESET);
2466 /* FIXME need disconnect() for NOTATTACHED device */
2467 if (warm) {
2468 clear_port_feature(hub->hdev, port1,
2469 USB_PORT_FEAT_C_BH_PORT_RESET);
2470 clear_port_feature(hub->hdev, port1,
2471 USB_PORT_FEAT_C_PORT_LINK_STATE);
2472 } else {
2473 usb_set_device_state(udev, *status
2474 ? USB_STATE_NOTATTACHED
2475 : USB_STATE_DEFAULT);
2476 }
2477 break;
2478 }
2479}
2480
2481/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002483 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
2485 int i, status;
2486
Andiry Xu75d7cf72011-09-13 16:41:11 -07002487 if (!warm) {
2488 /* Block EHCI CF initialization during the port reset.
2489 * Some companion controllers don't like it when they mix.
2490 */
2491 down_read(&ehci_cf_port_reset_rwsem);
2492 } else {
2493 if (!hub_is_superspeed(hub->hdev)) {
2494 dev_err(hub->intfdev, "only USB3 hub support "
2495 "warm reset\n");
2496 return -EINVAL;
2497 }
2498 }
Alan Stern32fe0192007-10-10 16:27:07 -04002499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 /* Reset the port */
2501 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002502 status = set_port_feature(hub->hdev, port1, (warm ?
2503 USB_PORT_FEAT_BH_PORT_RESET :
2504 USB_PORT_FEAT_RESET));
2505 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002507 "cannot %sreset port %d (err = %d)\n",
2508 warm ? "warm " : "", port1, status);
2509 } else {
2510 status = hub_port_wait_reset(hub, port1, udev, delay,
2511 warm);
David Brownelldd165252005-08-31 10:45:25 -07002512 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 dev_dbg(hub->intfdev,
2514 "port_wait_reset: err = %d\n",
2515 status);
2516 }
2517
2518 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002519 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2520 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002521 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
2523
2524 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002525 "port %d not enabled, trying %sreset again...\n",
2526 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 delay = HUB_LONG_RESET_TIME;
2528 }
2529
2530 dev_err (hub->intfdev,
2531 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2532 port1);
2533
Andiry Xu75d7cf72011-09-13 16:41:11 -07002534done:
2535 if (!warm)
2536 up_read(&ehci_cf_port_reset_rwsem);
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return status;
2539}
2540
Andiry Xu0ed9a572011-04-27 18:07:43 +08002541/* Check if a port is power on */
2542static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2543{
2544 int ret = 0;
2545
2546 if (hub_is_superspeed(hub->hdev)) {
2547 if (portstatus & USB_SS_PORT_STAT_POWER)
2548 ret = 1;
2549 } else {
2550 if (portstatus & USB_PORT_STAT_POWER)
2551 ret = 1;
2552 }
2553
2554 return ret;
2555}
2556
Alan Sternd388dab2006-07-01 22:14:24 -04002557#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Andiry Xu0ed9a572011-04-27 18:07:43 +08002559/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2560static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2561{
2562 int ret = 0;
2563
2564 if (hub_is_superspeed(hub->hdev)) {
2565 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2566 == USB_SS_PORT_LS_U3)
2567 ret = 1;
2568 } else {
2569 if (portstatus & USB_PORT_STAT_SUSPEND)
2570 ret = 1;
2571 }
2572
2573 return ret;
2574}
Alan Sternb01b03f2008-04-28 11:06:11 -04002575
2576/* Determine whether the device on a port is ready for a normal resume,
2577 * is ready for a reset-resume, or should be disconnected.
2578 */
2579static int check_port_resume_type(struct usb_device *udev,
2580 struct usb_hub *hub, int port1,
2581 int status, unsigned portchange, unsigned portstatus)
2582{
2583 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002584 if (status || port_is_suspended(hub, portstatus) ||
2585 !port_is_power_on(hub, portstatus) ||
2586 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002587 if (status >= 0)
2588 status = -ENODEV;
2589 }
2590
Alan Stern86c57ed2008-06-30 11:14:43 -04002591 /* Can't do a normal resume if the port isn't enabled,
2592 * so try a reset-resume instead.
2593 */
2594 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2595 if (udev->persist_enabled)
2596 udev->reset_resume = 1;
2597 else
2598 status = -ENODEV;
2599 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002600
2601 if (status) {
2602 dev_dbg(hub->intfdev,
2603 "port %d status %04x.%04x after resume, %d\n",
2604 port1, portchange, portstatus, status);
2605 } else if (udev->reset_resume) {
2606
2607 /* Late port handoff can set status-change bits */
2608 if (portchange & USB_PORT_STAT_C_CONNECTION)
2609 clear_port_feature(hub->hdev, port1,
2610 USB_PORT_FEAT_C_CONNECTION);
2611 if (portchange & USB_PORT_STAT_C_ENABLE)
2612 clear_port_feature(hub->hdev, port1,
2613 USB_PORT_FEAT_C_ENABLE);
2614 }
2615
2616 return status;
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619#ifdef CONFIG_USB_SUSPEND
2620
2621/*
Alan Stern140d8f62006-07-01 22:07:21 -04002622 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002623 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002624 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 *
2626 * Suspends a USB device that isn't in active use, conserving power.
2627 * Devices may wake out of a suspend, if anything important happens,
2628 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002629 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 * to disconnect devices while they are suspended.
2631 *
David Brownell390a8c32005-09-13 19:57:27 -07002632 * This only affects the USB hardware for a device; its interfaces
2633 * (and, for hubs, child devices) must already have been suspended.
2634 *
Alan Stern624d6c02007-05-30 15:35:16 -04002635 * Selective port suspend reduces power; most suspended devices draw
2636 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2637 * All devices below the suspended port are also suspended.
2638 *
2639 * Devices leave suspend state when the host wakes them up. Some devices
2640 * also support "remote wakeup", where the device can activate the USB
2641 * tree above them to deliver data, such as a keypress or packet. In
2642 * some cases, this wakes the USB host.
2643 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 * Suspending OTG devices may trigger HNP, if that's been enabled
2645 * between a pair of dual-role devices. That will change roles, such
2646 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2647 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002648 * Devices on USB hub ports have only one "suspend" state, corresponding
2649 * to ACPI D2, "may cause the device to lose some context".
2650 * State transitions include:
2651 *
2652 * - suspend, resume ... when the VBUS power link stays live
2653 * - suspend, disconnect ... VBUS lost
2654 *
2655 * Once VBUS drop breaks the circuit, the port it's using has to go through
2656 * normal re-enumeration procedures, starting with enabling VBUS power.
2657 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2658 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2659 * timer, no SRP, no requests through sysfs.
2660 *
2661 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2662 * the root hub for their bus goes into global suspend ... so we don't
2663 * (falsely) update the device power state to say it suspended.
2664 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 * Returns 0 on success, else negative errno.
2666 */
Alan Stern65bfd292008-11-25 16:39:18 -05002667int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Alan Stern624d6c02007-05-30 15:35:16 -04002669 struct usb_hub *hub = hdev_to_hub(udev->parent);
2670 int port1 = udev->portnum;
2671 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002672
Alan Stern624d6c02007-05-30 15:35:16 -04002673 /* enable remote wakeup when appropriate; this lets the device
2674 * wake up the upstream hub (including maybe the root hub).
2675 *
2676 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2677 * we don't explicitly enable it here.
2678 */
2679 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002680 if (!hub_is_superspeed(hub->hdev)) {
2681 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2682 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2683 USB_DEVICE_REMOTE_WAKEUP, 0,
2684 NULL, 0,
2685 USB_CTRL_SET_TIMEOUT);
2686 } else {
2687 /* Assume there's only one function on the USB 3.0
2688 * device and enable remote wake for the first
2689 * interface. FIXME if the interface association
2690 * descriptor shows there's more than one function.
2691 */
2692 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2693 USB_REQ_SET_FEATURE,
2694 USB_RECIP_INTERFACE,
2695 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002696 USB_INTRF_FUNC_SUSPEND_RW |
2697 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002698 NULL, 0,
2699 USB_CTRL_SET_TIMEOUT);
2700 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002701 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002702 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2703 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002704 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002705 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002706 return status;
2707 }
Alan Stern624d6c02007-05-30 15:35:16 -04002708 }
2709
Andiry Xu65580b432011-09-23 14:19:52 -07002710 /* disable USB2 hardware LPM */
2711 if (udev->usb2_hw_lpm_enabled == 1)
2712 usb_set_usb2_hardware_lpm(udev, 0);
2713
Sarah Sharp83060952012-05-02 14:25:52 -07002714 if (usb_unlocked_disable_lpm(udev)) {
2715 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2716 __func__);
2717 return -ENOMEM;
2718 }
2719
Alan Stern624d6c02007-05-30 15:35:16 -04002720 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002721 if (hub_is_superspeed(hub->hdev))
2722 status = set_port_feature(hub->hdev,
2723 port1 | (USB_SS_PORT_LS_U3 << 3),
2724 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002725 else
2726 status = set_port_feature(hub->hdev, port1,
2727 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002728 if (status) {
2729 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2730 port1, status);
2731 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002732 if (udev->do_remote_wakeup)
2733 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002734 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2735 USB_DEVICE_REMOTE_WAKEUP, 0,
2736 NULL, 0,
2737 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002738
Andiry Xuc3e751e2012-05-05 00:50:10 +08002739 /* Try to enable USB2 hardware LPM again */
2740 if (udev->usb2_hw_lpm_capable == 1)
2741 usb_set_usb2_hardware_lpm(udev, 1);
2742
Sarah Sharp83060952012-05-02 14:25:52 -07002743 /* Try to enable USB3 LPM again */
2744 usb_unlocked_enable_lpm(udev);
2745
Alan Sterncbb33002011-06-15 16:29:16 -04002746 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002747 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002748 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002749 } else {
2750 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002751 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2752 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2753 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002754 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2755 msleep(10);
2756 }
Alan Sternc08512c2010-11-15 15:57:58 -05002757 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002758 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
David Brownellf3f32532005-09-22 22:37:29 -07002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761/*
David Brownell390a8c32005-09-13 19:57:27 -07002762 * If the USB "suspend" state is in use (rather than "global suspend"),
2763 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002764 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 * hardware resume signaling is finished, either because of selective
2766 * resume (by host) or remote wakeup (by device) ... now see what changed
2767 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002768 *
2769 * If @udev->reset_resume is set then the device is reset before the
2770 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 */
Alan Stern140d8f62006-07-01 22:07:21 -04002772static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Alan Stern54515fe2007-05-30 15:38:58 -04002774 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 u16 devstatus;
2776
2777 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002778 dev_dbg(&udev->dev, "%s\n",
2779 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 /* usb ch9 identifies four variants of SUSPENDED, based on what
2782 * state the device resumes to. Linux currently won't see the
2783 * first two on the host side; they'd be inside hub_port_init()
2784 * during many timeouts, but khubd can't suspend until later.
2785 */
2786 usb_set_device_state(udev, udev->actconfig
2787 ? USB_STATE_CONFIGURED
2788 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Alan Stern54515fe2007-05-30 15:38:58 -04002790 /* 10.5.4.5 says not to reset a suspended port if the attached
2791 * device is enabled for remote wakeup. Hence the reset
2792 * operation is carried out here, after the port has been
2793 * resumed.
2794 */
2795 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002796 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002797 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /* 10.5.4.5 says be sure devices in the tree are still there.
2800 * For now let's assume the device didn't go crazy on resume,
2801 * and device drivers will know about any resume quirks.
2802 */
Alan Stern54515fe2007-05-30 15:38:58 -04002803 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002804 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002805 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2806 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002807 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002808
2809 /* If a normal resume failed, try doing a reset-resume */
2810 if (status && !udev->reset_resume && udev->persist_enabled) {
2811 dev_dbg(&udev->dev, "retry with reset-resume\n");
2812 udev->reset_resume = 1;
2813 goto retry_reset_resume;
2814 }
Alan Stern54515fe2007-05-30 15:38:58 -04002815 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002816
Alan Stern624d6c02007-05-30 15:35:16 -04002817 if (status) {
2818 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2819 status);
2820 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002822 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 status = usb_control_msg(udev,
2824 usb_sndctrlpipe(udev, 0),
2825 USB_REQ_CLEAR_FEATURE,
2826 USB_RECIP_DEVICE,
2827 USB_DEVICE_REMOTE_WAKEUP, 0,
2828 NULL, 0,
2829 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002830 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002831 dev_dbg(&udev->dev,
2832 "disable remote wakeup, status %d\n",
2833 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
2837 return status;
2838}
2839
Alan Stern624d6c02007-05-30 15:35:16 -04002840/*
2841 * usb_port_resume - re-activate a suspended usb device's upstream port
2842 * @udev: device to re-activate, not a root hub
2843 * Context: must be able to sleep; device not locked; pm locks held
2844 *
2845 * This will re-activate the suspended device, increasing power usage
2846 * while letting drivers communicate again with its endpoints.
2847 * USB resume explicitly guarantees that the power session between
2848 * the host and the device is the same as it was when the device
2849 * suspended.
2850 *
Alan Sternfeccc302008-03-03 15:15:59 -05002851 * If @udev->reset_resume is set then this routine won't check that the
2852 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002853 * reset @udev. The end result is that a broken power session can be
2854 * recovered and @udev will appear to persist across a loss of VBUS power.
2855 *
2856 * For example, if a host controller doesn't maintain VBUS suspend current
2857 * during a system sleep or is reset when the system wakes up, all the USB
2858 * power sessions below it will be broken. This is especially troublesome
2859 * for mass-storage devices containing mounted filesystems, since the
2860 * device will appear to have disconnected and all the memory mappings
2861 * to it will be lost. Using the USB_PERSIST facility, the device can be
2862 * made to appear as if it had not disconnected.
2863 *
Ming Lei742120c2008-06-18 22:00:29 +08002864 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002865 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002866 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2867 * quite possible for a device to remain unaltered but its media to be
2868 * changed. If the user replaces a flash memory card while the system is
2869 * asleep, he will have only himself to blame when the filesystem on the
2870 * new card is corrupted and the system crashes.
2871 *
Alan Stern624d6c02007-05-30 15:35:16 -04002872 * Returns 0 on success, else negative errno.
2873 */
Alan Stern65bfd292008-11-25 16:39:18 -05002874int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Alan Stern624d6c02007-05-30 15:35:16 -04002876 struct usb_hub *hub = hdev_to_hub(udev->parent);
2877 int port1 = udev->portnum;
2878 int status;
2879 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05002880
2881 /* Skip the initial Clear-Suspend step for a remote wakeup */
2882 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08002883 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05002884 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2887
Alan Sternd5cbad42006-08-11 16:52:39 -04002888 set_bit(port1, hub->busy_bits);
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08002891 if (hub_is_superspeed(hub->hdev))
2892 status = set_port_feature(hub->hdev,
2893 port1 | (USB_SS_PORT_LS_U0 << 3),
2894 USB_PORT_FEAT_LINK_STATE);
2895 else
2896 status = clear_port_feature(hub->hdev,
2897 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002899 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
2900 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04002903 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02002904 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 msleep(25);
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2908 * stop resume signaling. Then finish the resume
2909 * sequence.
2910 */
Alan Sternd25450c2006-11-20 11:14:30 -05002911 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04002912
Alan Sternb01b03f2008-04-28 11:06:11 -04002913 /* TRSMRCY = 10 msec */
2914 msleep(10);
2915 }
Alan Stern54515fe2007-05-30 15:38:58 -04002916
Alan Sternb01b03f2008-04-28 11:06:11 -04002917 SuspendCleared:
2918 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08002919 if (hub_is_superspeed(hub->hdev)) {
2920 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2921 clear_port_feature(hub->hdev, port1,
2922 USB_PORT_FEAT_C_PORT_LINK_STATE);
2923 } else {
2924 if (portchange & USB_PORT_STAT_C_SUSPEND)
2925 clear_port_feature(hub->hdev, port1,
2926 USB_PORT_FEAT_C_SUSPEND);
2927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Alan Sternd5cbad42006-08-11 16:52:39 -04002930 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04002931
Alan Sternb01b03f2008-04-28 11:06:11 -04002932 status = check_port_resume_type(udev,
2933 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04002934 if (status == 0)
2935 status = finish_port_resume(udev);
2936 if (status < 0) {
2937 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2938 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07002939 } else {
2940 /* Try to enable USB2 hardware LPM */
2941 if (udev->usb2_hw_lpm_capable == 1)
2942 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07002943
2944 /* Try to enable USB3 LPM */
2945 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002946 }
Andiry Xu65580b432011-09-23 14:19:52 -07002947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return status;
2949}
2950
Alan Stern8808f002008-04-28 11:06:55 -04002951/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05002952int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 int status = 0;
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04002957 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002958 status = usb_autoresume_device(udev);
2959 if (status == 0) {
2960 /* Let the drivers do their thing, then... */
2961 usb_autosuspend_device(udev);
2962 }
Alan Sternd25450c2006-11-20 11:14:30 -05002963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return status;
2965}
2966
Alan Sternd388dab2006-07-01 22:14:24 -04002967#else /* CONFIG_USB_SUSPEND */
2968
2969/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2970
Alan Stern65bfd292008-11-25 16:39:18 -05002971int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04002972{
2973 return 0;
2974}
2975
Alan Sternb01b03f2008-04-28 11:06:11 -04002976/* However we may need to do a reset-resume */
2977
Alan Stern65bfd292008-11-25 16:39:18 -05002978int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04002979{
Alan Sternb01b03f2008-04-28 11:06:11 -04002980 struct usb_hub *hub = hdev_to_hub(udev->parent);
2981 int port1 = udev->portnum;
2982 int status;
2983 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04002984
Alan Sternb01b03f2008-04-28 11:06:11 -04002985 status = hub_port_status(hub, port1, &portstatus, &portchange);
2986 status = check_port_resume_type(udev,
2987 hub, port1, status, portchange, portstatus);
2988
2989 if (status) {
2990 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2991 hub_port_logical_disconnect(hub, port1);
2992 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04002993 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08002994 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002995 }
2996 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04002997}
2998
Alan Sternd388dab2006-07-01 22:14:24 -04002999#endif
3000
David Brownelldb690872005-09-13 19:56:33 -07003001static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002{
3003 struct usb_hub *hub = usb_get_intfdata (intf);
3004 struct usb_device *hdev = hub->hdev;
3005 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003006 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
Alan Sterncbb33002011-06-15 16:29:16 -04003008 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3010 struct usb_device *udev;
3011
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003012 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04003013 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003014 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003015 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003016 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003019 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3020 /* Enable hub to send remote wakeup for all ports. */
3021 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3022 status = set_port_feature(hdev,
3023 port1 |
3024 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3025 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3026 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3027 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3028 }
3029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Harvey Harrison441b62c2008-03-03 16:08:34 -08003031 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003032
David Brownellc9f89fa2005-09-13 19:57:04 -07003033 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003034 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003035 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
3038static int hub_resume(struct usb_interface *intf)
3039{
Alan Stern5e6effa2008-03-03 15:15:51 -05003040 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Alan Stern5e6effa2008-03-03 15:15:51 -05003042 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003043 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return 0;
3045}
3046
Alan Sternb41a60e2007-05-30 15:39:33 -04003047static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003048{
Alan Sternb41a60e2007-05-30 15:39:33 -04003049 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003050
Alan Stern5e6effa2008-03-03 15:15:51 -05003051 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003052 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003053 return 0;
3054}
3055
Alan Stern54515fe2007-05-30 15:38:58 -04003056/**
3057 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3058 * @rhdev: struct usb_device for the root hub
3059 *
3060 * The USB host controller driver calls this function when its root hub
3061 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003062 * has been reset. The routine marks @rhdev as having lost power.
3063 * When the hub driver is resumed it will take notice and carry out
3064 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3065 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003066 */
3067void usb_root_hub_lost_power(struct usb_device *rhdev)
3068{
3069 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3070 rhdev->reset_resume = 1;
3071}
3072EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3073
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003074static const char * const usb3_lpm_names[] = {
3075 "U0",
3076 "U1",
3077 "U2",
3078 "U3",
3079};
3080
3081/*
3082 * Send a Set SEL control transfer to the device, prior to enabling
3083 * device-initiated U1 or U2. This lets the device know the exit latencies from
3084 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3085 * packet from the host.
3086 *
3087 * This function will fail if the SEL or PEL values for udev are greater than
3088 * the maximum allowed values for the link state to be enabled.
3089 */
3090static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3091{
3092 struct usb_set_sel_req *sel_values;
3093 unsigned long long u1_sel;
3094 unsigned long long u1_pel;
3095 unsigned long long u2_sel;
3096 unsigned long long u2_pel;
3097 int ret;
3098
3099 /* Convert SEL and PEL stored in ns to us */
3100 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3101 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3102 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3103 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3104
3105 /*
3106 * Make sure that the calculated SEL and PEL values for the link
3107 * state we're enabling aren't bigger than the max SEL/PEL
3108 * value that will fit in the SET SEL control transfer.
3109 * Otherwise the device would get an incorrect idea of the exit
3110 * latency for the link state, and could start a device-initiated
3111 * U1/U2 when the exit latencies are too high.
3112 */
3113 if ((state == USB3_LPM_U1 &&
3114 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3115 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3116 (state == USB3_LPM_U2 &&
3117 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3118 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3119 dev_dbg(&udev->dev, "Device-initiated %s disabled due "
3120 "to long SEL %llu ms or PEL %llu ms\n",
3121 usb3_lpm_names[state], u1_sel, u1_pel);
3122 return -EINVAL;
3123 }
3124
3125 /*
3126 * If we're enabling device-initiated LPM for one link state,
3127 * but the other link state has a too high SEL or PEL value,
3128 * just set those values to the max in the Set SEL request.
3129 */
3130 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3131 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3132
3133 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3134 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3135
3136 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3137 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3138
3139 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3140 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3141
3142 /*
3143 * usb_enable_lpm() can be called as part of a failed device reset,
3144 * which may be initiated by an error path of a mass storage driver.
3145 * Therefore, use GFP_NOIO.
3146 */
3147 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3148 if (!sel_values)
3149 return -ENOMEM;
3150
3151 sel_values->u1_sel = u1_sel;
3152 sel_values->u1_pel = u1_pel;
3153 sel_values->u2_sel = cpu_to_le16(u2_sel);
3154 sel_values->u2_pel = cpu_to_le16(u2_pel);
3155
3156 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3157 USB_REQ_SET_SEL,
3158 USB_RECIP_DEVICE,
3159 0, 0,
3160 sel_values, sizeof *(sel_values),
3161 USB_CTRL_SET_TIMEOUT);
3162 kfree(sel_values);
3163 return ret;
3164}
3165
3166/*
3167 * Enable or disable device-initiated U1 or U2 transitions.
3168 */
3169static int usb_set_device_initiated_lpm(struct usb_device *udev,
3170 enum usb3_link_state state, bool enable)
3171{
3172 int ret;
3173 int feature;
3174
3175 switch (state) {
3176 case USB3_LPM_U1:
3177 feature = USB_DEVICE_U1_ENABLE;
3178 break;
3179 case USB3_LPM_U2:
3180 feature = USB_DEVICE_U2_ENABLE;
3181 break;
3182 default:
3183 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3184 __func__, enable ? "enable" : "disable");
3185 return -EINVAL;
3186 }
3187
3188 if (udev->state != USB_STATE_CONFIGURED) {
3189 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3190 "for unconfigured device.\n",
3191 __func__, enable ? "enable" : "disable",
3192 usb3_lpm_names[state]);
3193 return 0;
3194 }
3195
3196 if (enable) {
3197 /*
3198 * First, let the device know about the exit latencies
3199 * associated with the link state we're about to enable.
3200 */
3201 ret = usb_req_set_sel(udev, state);
3202 if (ret < 0) {
3203 dev_warn(&udev->dev, "Set SEL for device-initiated "
3204 "%s failed.\n", usb3_lpm_names[state]);
3205 return -EBUSY;
3206 }
3207 /*
3208 * Now send the control transfer to enable device-initiated LPM
3209 * for either U1 or U2.
3210 */
3211 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3212 USB_REQ_SET_FEATURE,
3213 USB_RECIP_DEVICE,
3214 feature,
3215 0, NULL, 0,
3216 USB_CTRL_SET_TIMEOUT);
3217 } else {
3218 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3219 USB_REQ_CLEAR_FEATURE,
3220 USB_RECIP_DEVICE,
3221 feature,
3222 0, NULL, 0,
3223 USB_CTRL_SET_TIMEOUT);
3224 }
3225 if (ret < 0) {
3226 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3227 enable ? "Enable" : "Disable",
3228 usb3_lpm_names[state]);
3229 return -EBUSY;
3230 }
3231 return 0;
3232}
3233
3234static int usb_set_lpm_timeout(struct usb_device *udev,
3235 enum usb3_link_state state, int timeout)
3236{
3237 int ret;
3238 int feature;
3239
3240 switch (state) {
3241 case USB3_LPM_U1:
3242 feature = USB_PORT_FEAT_U1_TIMEOUT;
3243 break;
3244 case USB3_LPM_U2:
3245 feature = USB_PORT_FEAT_U2_TIMEOUT;
3246 break;
3247 default:
3248 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3249 __func__);
3250 return -EINVAL;
3251 }
3252
3253 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3254 timeout != USB3_LPM_DEVICE_INITIATED) {
3255 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3256 "which is a reserved value.\n",
3257 usb3_lpm_names[state], timeout);
3258 return -EINVAL;
3259 }
3260
3261 ret = set_port_feature(udev->parent,
3262 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3263 feature);
3264 if (ret < 0) {
3265 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3266 "error code %i\n", usb3_lpm_names[state],
3267 timeout, ret);
3268 return -EBUSY;
3269 }
3270 if (state == USB3_LPM_U1)
3271 udev->u1_params.timeout = timeout;
3272 else
3273 udev->u2_params.timeout = timeout;
3274 return 0;
3275}
3276
3277/*
3278 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3279 * U1/U2 entry.
3280 *
3281 * We will attempt to enable U1 or U2, but there are no guarantees that the
3282 * control transfers to set the hub timeout or enable device-initiated U1/U2
3283 * will be successful.
3284 *
3285 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3286 * driver know about it. If that call fails, it should be harmless, and just
3287 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3288 */
3289static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3290 enum usb3_link_state state)
3291{
3292 int timeout;
3293
3294 /* We allow the host controller to set the U1/U2 timeout internally
3295 * first, so that it can change its schedule to account for the
3296 * additional latency to send data to a device in a lower power
3297 * link state.
3298 */
3299 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3300
3301 /* xHCI host controller doesn't want to enable this LPM state. */
3302 if (timeout == 0)
3303 return;
3304
3305 if (timeout < 0) {
3306 dev_warn(&udev->dev, "Could not enable %s link state, "
3307 "xHCI error %i.\n", usb3_lpm_names[state],
3308 timeout);
3309 return;
3310 }
3311
3312 if (usb_set_lpm_timeout(udev, state, timeout))
3313 /* If we can't set the parent hub U1/U2 timeout,
3314 * device-initiated LPM won't be allowed either, so let the xHCI
3315 * host know that this link state won't be enabled.
3316 */
3317 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3318
3319 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3320 else if (udev->actconfig)
3321 usb_set_device_initiated_lpm(udev, state, true);
3322
3323}
3324
3325/*
3326 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3327 * U1/U2 entry.
3328 *
3329 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3330 * If zero is returned, the parent will not allow the link to go into U1/U2.
3331 *
3332 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3333 * it won't have an effect on the bus link state because the parent hub will
3334 * still disallow device-initiated U1/U2 entry.
3335 *
3336 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3337 * possible. The result will be slightly more bus bandwidth will be taken up
3338 * (to account for U1/U2 exit latency), but it should be harmless.
3339 */
3340static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3341 enum usb3_link_state state)
3342{
3343 int feature;
3344
3345 switch (state) {
3346 case USB3_LPM_U1:
3347 feature = USB_PORT_FEAT_U1_TIMEOUT;
3348 break;
3349 case USB3_LPM_U2:
3350 feature = USB_PORT_FEAT_U2_TIMEOUT;
3351 break;
3352 default:
3353 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3354 __func__);
3355 return -EINVAL;
3356 }
3357
3358 if (usb_set_lpm_timeout(udev, state, 0))
3359 return -EBUSY;
3360
3361 usb_set_device_initiated_lpm(udev, state, false);
3362
3363 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3364 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3365 "bus schedule bandwidth may be impacted.\n",
3366 usb3_lpm_names[state]);
3367 return 0;
3368}
3369
3370/*
3371 * Disable hub-initiated and device-initiated U1 and U2 entry.
3372 * Caller must own the bandwidth_mutex.
3373 *
3374 * This will call usb_enable_lpm() on failure, which will decrement
3375 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3376 */
3377int usb_disable_lpm(struct usb_device *udev)
3378{
3379 struct usb_hcd *hcd;
3380
3381 if (!udev || !udev->parent ||
3382 udev->speed != USB_SPEED_SUPER ||
3383 !udev->lpm_capable)
3384 return 0;
3385
3386 hcd = bus_to_hcd(udev->bus);
3387 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3388 return 0;
3389
3390 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003391 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003392 return 0;
3393
3394 /* If LPM is enabled, attempt to disable it. */
3395 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3396 goto enable_lpm;
3397 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3398 goto enable_lpm;
3399
3400 return 0;
3401
3402enable_lpm:
3403 usb_enable_lpm(udev);
3404 return -EBUSY;
3405}
3406EXPORT_SYMBOL_GPL(usb_disable_lpm);
3407
3408/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3409int usb_unlocked_disable_lpm(struct usb_device *udev)
3410{
3411 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3412 int ret;
3413
3414 if (!hcd)
3415 return -EINVAL;
3416
3417 mutex_lock(hcd->bandwidth_mutex);
3418 ret = usb_disable_lpm(udev);
3419 mutex_unlock(hcd->bandwidth_mutex);
3420
3421 return ret;
3422}
3423EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3424
3425/*
3426 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3427 * xHCI host policy may prevent U1 or U2 from being enabled.
3428 *
3429 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3430 * until the lpm_disable_count drops to zero. Caller must own the
3431 * bandwidth_mutex.
3432 */
3433void usb_enable_lpm(struct usb_device *udev)
3434{
3435 struct usb_hcd *hcd;
3436
3437 if (!udev || !udev->parent ||
3438 udev->speed != USB_SPEED_SUPER ||
3439 !udev->lpm_capable)
3440 return;
3441
3442 udev->lpm_disable_count--;
3443 hcd = bus_to_hcd(udev->bus);
3444 /* Double check that we can both enable and disable LPM.
3445 * Device must be configured to accept set feature U1/U2 timeout.
3446 */
3447 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3448 !hcd->driver->disable_usb3_lpm_timeout)
3449 return;
3450
3451 if (udev->lpm_disable_count > 0)
3452 return;
3453
3454 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3455 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3456}
3457EXPORT_SYMBOL_GPL(usb_enable_lpm);
3458
3459/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3460void usb_unlocked_enable_lpm(struct usb_device *udev)
3461{
3462 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3463
3464 if (!hcd)
3465 return;
3466
3467 mutex_lock(hcd->bandwidth_mutex);
3468 usb_enable_lpm(udev);
3469 mutex_unlock(hcd->bandwidth_mutex);
3470}
3471EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3472
3473
Alan Sternd388dab2006-07-01 22:14:24 -04003474#else /* CONFIG_PM */
3475
Alan Sternf07600c2007-05-30 15:38:16 -04003476#define hub_suspend NULL
3477#define hub_resume NULL
3478#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003479
3480int usb_disable_lpm(struct usb_device *udev)
3481{
3482 return 0;
3483}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003484EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003485
3486void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003487EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003488
3489int usb_unlocked_disable_lpm(struct usb_device *udev)
3490{
3491 return 0;
3492}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003493EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003494
3495void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003496EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Alan Sternd388dab2006-07-01 22:14:24 -04003497#endif
3498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3501 *
3502 * Between connect detection and reset signaling there must be a delay
3503 * of 100ms at least for debounce and power-settling. The corresponding
3504 * timer shall restart whenever the downstream port detects a disconnect.
3505 *
3506 * Apparently there are some bluetooth and irda-dongles and a number of
3507 * low-speed devices for which this debounce period may last over a second.
3508 * Not covered by the spec - but easy to deal with.
3509 *
3510 * This implementation uses a 1500ms total debounce timeout; if the
3511 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3512 * every 25ms for transient disconnects. When the port status has been
3513 * unchanged for 100ms it returns the port status.
3514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515static int hub_port_debounce(struct usb_hub *hub, int port1)
3516{
3517 int ret;
3518 int total_time, stable_time = 0;
3519 u16 portchange, portstatus;
3520 unsigned connection = 0xffff;
3521
3522 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3523 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3524 if (ret < 0)
3525 return ret;
3526
3527 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3528 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3529 stable_time += HUB_DEBOUNCE_STEP;
3530 if (stable_time >= HUB_DEBOUNCE_STABLE)
3531 break;
3532 } else {
3533 stable_time = 0;
3534 connection = portstatus & USB_PORT_STAT_CONNECTION;
3535 }
3536
3537 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3538 clear_port_feature(hub->hdev, port1,
3539 USB_PORT_FEAT_C_CONNECTION);
3540 }
3541
3542 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3543 break;
3544 msleep(HUB_DEBOUNCE_STEP);
3545 }
3546
3547 dev_dbg (hub->intfdev,
3548 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3549 port1, total_time, stable_time, portstatus);
3550
3551 if (stable_time < HUB_DEBOUNCE_STABLE)
3552 return -ETIMEDOUT;
3553 return portstatus;
3554}
3555
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003556void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
Alan Sternddeac4e2009-01-15 17:03:33 -05003558 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3559 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003560 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003562EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563
3564#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3565#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3566
Alan Stern4326ed02007-07-30 17:08:43 -04003567static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
3569 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003570 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
Sarah Sharpc6515272009-04-27 19:57:26 -07003572 /*
3573 * The host controller will choose the device address,
3574 * instead of the core having chosen it earlier
3575 */
3576 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 return -EINVAL;
3578 if (udev->state == USB_STATE_ADDRESS)
3579 return 0;
3580 if (udev->state != USB_STATE_DEFAULT)
3581 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003582 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003583 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003584 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003585 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3586 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3587 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003589 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003590 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003592 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 }
3594 return retval;
3595}
3596
3597/* Reset device, (re)assign address, get device descriptor.
3598 * Device connection must be stable, no more debouncing needed.
3599 * Returns device in USB_STATE_ADDRESS, except on error.
3600 *
3601 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003602 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 * newly detected device that is not accessible through any global
3604 * pointers, it's not necessary to lock the device.
3605 */
3606static int
3607hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3608 int retry_counter)
3609{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003610 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
3612 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003613 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 int i, j, retval;
3615 unsigned delay = HUB_SHORT_RESET_TIME;
3616 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003617 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003618 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
3620 /* root hub ports have a slightly longer reset period
3621 * (from USB 2.0 spec, section 7.1.7.5)
3622 */
3623 if (!hdev->parent) {
3624 delay = HUB_ROOT_RESET_TIME;
3625 if (port1 == hdev->bus->otg_port)
3626 hdev->bus->b_hnp_enable = 0;
3627 }
3628
3629 /* Some low speed devices have problems with the quick delay, so */
3630 /* be a bit pessimistic with those devices. RHbug #23670 */
3631 if (oldspeed == USB_SPEED_LOW)
3632 delay = HUB_LONG_RESET_TIME;
3633
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003634 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Luben Tuikov07194ab2011-02-11 11:33:10 -08003636 /* Reset the device; full speed may morph to high speed */
3637 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003638 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003639 if (retval < 0) /* error or disconnect */
3640 goto fail;
3641 /* success, speed is known */
3642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 retval = -ENODEV;
3644
3645 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3646 dev_dbg(&udev->dev, "device reset changed speed!\n");
3647 goto fail;
3648 }
3649 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3652 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003653 * For Wireless USB devices, ep0 max packet is always 512 (tho
3654 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 */
3656 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003657 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003658 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003659 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003662 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 break;
3664 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3665 /* to determine the ep0 maxpacket size, try to read
3666 * the device descriptor to get bMaxPacketSize0 and
3667 * then correct our initial guess.
3668 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003669 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 break;
3671 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003672 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 break;
3674 default:
3675 goto fail;
3676 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003677
3678 if (udev->speed == USB_SPEED_WIRELESS)
3679 speed = "variable speed Wireless";
3680 else
3681 speed = usb_speed_string(udev->speed);
3682
Sarah Sharpc6515272009-04-27 19:57:26 -07003683 if (udev->speed != USB_SPEED_SUPER)
3684 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003685 "%s %s USB device number %d using %s\n",
3686 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003687 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 /* Set up TT records, if needed */
3690 if (hdev->tt) {
3691 udev->tt = hdev->tt;
3692 udev->ttport = hdev->ttport;
3693 } else if (udev->speed != USB_SPEED_HIGH
3694 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003695 if (!hub->tt.hub) {
3696 dev_err(&udev->dev, "parent hub has no TT\n");
3697 retval = -EINVAL;
3698 goto fail;
3699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 udev->tt = &hub->tt;
3701 udev->ttport = port1;
3702 }
3703
3704 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3705 * Because device hardware and firmware is sometimes buggy in
3706 * this area, and this is how Linux has done it for ages.
3707 * Change it cautiously.
3708 *
3709 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3710 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3711 * so it may help with some non-standards-compliant devices.
3712 * Otherwise we start with SET_ADDRESS and then try to read the
3713 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3714 * value.
3715 */
3716 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003717 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 struct usb_device_descriptor *buf;
3719 int r = 0;
3720
3721#define GET_DESCRIPTOR_BUFSIZE 64
3722 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3723 if (!buf) {
3724 retval = -ENOMEM;
3725 continue;
3726 }
3727
Alan Sternb89ee192007-05-11 10:19:04 -04003728 /* Retry on all errors; some devices are flakey.
3729 * 255 is for WUSB devices, we actually need to use
3730 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 */
3732 for (j = 0; j < 3; ++j) {
3733 buf->bMaxPacketSize0 = 0;
3734 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3735 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3736 USB_DT_DEVICE << 8, 0,
3737 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003738 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003740 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 if (buf->bDescriptorType ==
3742 USB_DT_DEVICE) {
3743 r = 0;
3744 break;
3745 }
3746 /* FALL THROUGH */
3747 default:
3748 if (r == 0)
3749 r = -EPROTO;
3750 break;
3751 }
3752 if (r == 0)
3753 break;
3754 }
3755 udev->descriptor.bMaxPacketSize0 =
3756 buf->bMaxPacketSize0;
3757 kfree(buf);
3758
Andiry Xu75d7cf72011-09-13 16:41:11 -07003759 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 if (retval < 0) /* error or disconnect */
3761 goto fail;
3762 if (oldspeed != udev->speed) {
3763 dev_dbg(&udev->dev,
3764 "device reset changed speed!\n");
3765 retval = -ENODEV;
3766 goto fail;
3767 }
3768 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003769 dev_err(&udev->dev,
3770 "device descriptor read/64, error %d\n",
3771 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 retval = -EMSGSIZE;
3773 continue;
3774 }
3775#undef GET_DESCRIPTOR_BUFSIZE
3776 }
3777
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003778 /*
3779 * If device is WUSB, we already assigned an
3780 * unauthorized address in the Connect Ack sequence;
3781 * authorization will assign the final address.
3782 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003783 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003784 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3785 retval = hub_set_address(udev, devnum);
3786 if (retval >= 0)
3787 break;
3788 msleep(200);
3789 }
3790 if (retval < 0) {
3791 dev_err(&udev->dev,
3792 "device not accepting address %d, error %d\n",
3793 devnum, retval);
3794 goto fail;
3795 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003796 if (udev->speed == USB_SPEED_SUPER) {
3797 devnum = udev->devnum;
3798 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003799 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003800 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003801 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003802 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003803
3804 /* cope with hardware quirkiness:
3805 * - let SET_ADDRESS settle, some device hardware wants it
3806 * - read ep0 maxpacket even for high and low speed,
3807 */
3808 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003809 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813 retval = usb_get_device_descriptor(udev, 8);
3814 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003815 dev_err(&udev->dev,
3816 "device descriptor read/8, error %d\n",
3817 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 if (retval >= 0)
3819 retval = -EMSGSIZE;
3820 } else {
3821 retval = 0;
3822 break;
3823 }
3824 }
3825 if (retval)
3826 goto fail;
3827
Elric Fud8aec3d2012-03-26 21:16:02 +08003828 /*
3829 * Some superspeed devices have finished the link training process
3830 * and attached to a superspeed hub port, but the device descriptor
3831 * got from those devices show they aren't superspeed devices. Warm
3832 * reset the port attached by the devices can fix them.
3833 */
3834 if ((udev->speed == USB_SPEED_SUPER) &&
3835 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3836 dev_err(&udev->dev, "got a wrong device descriptor, "
3837 "warm reset device\n");
3838 hub_port_reset(hub, port1, udev,
3839 HUB_BH_RESET_TIME, true);
3840 retval = -EINVAL;
3841 goto fail;
3842 }
3843
Sarah Sharp6b403b02009-04-27 19:54:10 -07003844 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3845 udev->speed == USB_SPEED_SUPER)
3846 i = 512;
3847 else
3848 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003849 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003850 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003852 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 retval = -EMSGSIZE;
3854 goto fail;
3855 }
Alan Stern56626a72010-10-14 15:25:21 -04003856 if (udev->speed == USB_SPEED_FULL)
3857 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3858 else
3859 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003861 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 }
3863
3864 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3865 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003866 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3867 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 if (retval >= 0)
3869 retval = -ENOMSG;
3870 goto fail;
3871 }
3872
Andiry Xu1ff4df52011-09-23 14:19:48 -07003873 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3874 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003875 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08003876 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003877 usb_set_lpm_parameters(udev);
3878 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07003879 }
Andiry Xu3148bf02011-09-23 14:19:47 -07003880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08003882 /* notify HCD that we have a device connected and addressed */
3883 if (hcd->driver->update_device)
3884 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885fail:
Alan Stern4326ed02007-07-30 17:08:43 -04003886 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05003888 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04003889 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003890 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 return retval;
3892}
3893
3894static void
3895check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
3896{
3897 struct usb_qualifier_descriptor *qual;
3898 int status;
3899
Christoph Lametere94b1762006-12-06 20:33:17 -08003900 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 if (qual == NULL)
3902 return;
3903
3904 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
3905 qual, sizeof *qual);
3906 if (status == sizeof *qual) {
3907 dev_info(&udev->dev, "not running at top speed; "
3908 "connect to a high speed hub\n");
3909 /* hub LEDs are probably harder to miss than syslog */
3910 if (hub->has_indicators) {
3911 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003912 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
3914 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003915 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916}
3917
3918static unsigned
3919hub_power_remaining (struct usb_hub *hub)
3920{
3921 struct usb_device *hdev = hub->hdev;
3922 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05003923 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
Alan Stern55c52712005-11-23 12:03:12 -05003925 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 return 0;
3927
Alan Stern55c52712005-11-23 12:03:12 -05003928 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
3929 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003930 struct usb_device *udev = hdev->children[port1 - 1];
Alan Stern55c52712005-11-23 12:03:12 -05003931 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
3933 if (!udev)
3934 continue;
3935
Alan Stern55c52712005-11-23 12:03:12 -05003936 /* Unconfigured devices may not use more than 100mA,
3937 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05003939 delta = udev->actconfig->desc.bMaxPower * 2;
3940 else if (port1 != udev->bus->otg_port || hdev->parent)
3941 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 else
Alan Stern55c52712005-11-23 12:03:12 -05003943 delta = 8;
3944 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003945 dev_warn(&udev->dev,
3946 "%dmA is over %umA budget for port %d!\n",
3947 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 remaining -= delta;
3949 }
3950 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05003951 dev_warn(hub->intfdev, "%dmA over power budget!\n",
3952 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 remaining = 0;
3954 }
3955 return remaining;
3956}
3957
3958/* Handle physical or logical connection change events.
3959 * This routine is called when:
3960 * a port connection-change occurs;
3961 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08003962 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 * a firmware download)
3964 * caller already locked the hub
3965 */
3966static void hub_port_connect_change(struct usb_hub *hub, int port1,
3967 u16 portstatus, u16 portchange)
3968{
3969 struct usb_device *hdev = hub->hdev;
3970 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05303971 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04003972 unsigned wHubCharacteristics =
3973 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04003974 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04003976
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 dev_dbg (hub_dev,
3978 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08003979 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
3981 if (hub->has_indicators) {
3982 set_port_led(hub, port1, HUB_LED_AUTO);
3983 hub->indicator[port1-1] = INDICATOR_AUTO;
3984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986#ifdef CONFIG_USB_OTG
3987 /* during HNP, don't repeat the debounce */
3988 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04003989 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
3990 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991#endif
3992
Alan Stern8808f002008-04-28 11:06:55 -04003993 /* Try to resuscitate an existing device */
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003994 udev = hdev->children[port1-1];
Alan Stern8808f002008-04-28 11:06:55 -04003995 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
3996 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04003997 usb_lock_device(udev);
3998 if (portstatus & USB_PORT_STAT_ENABLE) {
3999 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004000
4001#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004002 } else if (udev->state == USB_STATE_SUSPENDED &&
4003 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004004 /* For a suspended device, treat this as a
4005 * remote wakeup event.
4006 */
Alan Stern0534d462010-01-08 12:56:30 -05004007 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004008#endif
4009
4010 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004011 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004012 }
4013 usb_unlock_device(udev);
4014
4015 if (status == 0) {
4016 clear_bit(port1, hub->change_bits);
4017 return;
4018 }
4019 }
4020
Alan Stern24618b02008-04-28 11:06:28 -04004021 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004022 if (udev)
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004023 usb_disconnect(&hdev->children[port1-1]);
Alan Stern24618b02008-04-28 11:06:28 -04004024 clear_bit(port1, hub->change_bits);
4025
Alan Stern253e0572009-10-27 15:20:13 -04004026 /* We can forget about a "removed" device when there's a physical
4027 * disconnect or the connect status changes.
4028 */
4029 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4030 (portchange & USB_PORT_STAT_C_CONNECTION))
4031 clear_bit(port1, hub->removed_bits);
4032
Alan Stern5257d972008-09-22 14:43:08 -04004033 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4034 USB_PORT_STAT_C_ENABLE)) {
4035 status = hub_port_debounce(hub, port1);
4036 if (status < 0) {
4037 if (printk_ratelimit())
4038 dev_err(hub_dev, "connect-debounce failed, "
4039 "port %d disabled\n", port1);
4040 portstatus &= ~USB_PORT_STAT_CONNECTION;
4041 } else {
4042 portstatus = status;
4043 }
4044 }
4045
Alan Stern253e0572009-10-27 15:20:13 -04004046 /* Return now if debouncing failed or nothing is connected or
4047 * the device was "removed".
4048 */
4049 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4050 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004053 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004054 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004056
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 if (portstatus & USB_PORT_STAT_ENABLE)
4058 goto done;
4059 return;
4060 }
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 /* reallocate for each attempt, since references
4065 * to the previous one can escape in various ways
4066 */
4067 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4068 if (!udev) {
4069 dev_err (hub_dev,
4070 "couldn't allocate port %d usb_device\n",
4071 port1);
4072 goto done;
4073 }
4074
4075 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004076 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004077 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004078 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004079
Sarah Sharp131dec32010-12-06 21:00:19 -08004080 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4081 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004082 udev->speed = USB_SPEED_SUPER;
4083 else
4084 udev->speed = USB_SPEED_UNKNOWN;
4085
Alan Stern3b29b682011-02-22 09:53:41 -05004086 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004087 if (udev->devnum <= 0) {
4088 status = -ENOTCONN; /* Don't retry */
4089 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004090 }
4091
Sarah Sharpe7b77172009-04-27 19:54:26 -07004092 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 status = hub_port_init(hub, udev, port1, i);
4094 if (status < 0)
4095 goto loop;
4096
Phil Dibowitz93362a82010-07-22 00:05:01 +02004097 usb_detect_quirks(udev);
4098 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4099 msleep(1000);
4100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 /* consecutive bus-powered hubs aren't reliable; they can
4102 * violate the voltage drop budget. if the new child has
4103 * a "powered" LED, users should notice we didn't enable it
4104 * (without reading syslog), even without per-port LEDs
4105 * on the parent.
4106 */
4107 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004108 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 u16 devstat;
4110
4111 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4112 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004113 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 dev_dbg(&udev->dev, "get status %d ?\n", status);
4115 goto loop_disable;
4116 }
Alan Stern55c52712005-11-23 12:03:12 -05004117 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4119 dev_err(&udev->dev,
4120 "can't connect bus-powered hub "
4121 "to this port\n");
4122 if (hub->has_indicators) {
4123 hub->indicator[port1-1] =
4124 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004125 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 }
4127 status = -ENOTCONN; /* Don't retry */
4128 goto loop_disable;
4129 }
4130 }
4131
4132 /* check for devices running slower than they could */
4133 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4134 && udev->speed == USB_SPEED_FULL
4135 && highspeed_hubs != 0)
4136 check_highspeed (hub, udev, port1);
4137
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004138 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 * udev becomes globally accessible, although presumably
4140 * no one will look at it until hdev is unlocked.
4141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 status = 0;
4143
4144 /* We mustn't add new devices if the parent hub has
4145 * been disconnected; we would race with the
4146 * recursively_mark_NOTATTACHED() routine.
4147 */
4148 spin_lock_irq(&device_state_lock);
4149 if (hdev->state == USB_STATE_NOTATTACHED)
4150 status = -ENOTCONN;
4151 else
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004152 hdev->children[port1-1] = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 spin_unlock_irq(&device_state_lock);
4154
4155 /* Run it through the hoops (find a driver, etc) */
4156 if (!status) {
4157 status = usb_new_device(udev);
4158 if (status) {
4159 spin_lock_irq(&device_state_lock);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004160 hdev->children[port1-1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 spin_unlock_irq(&device_state_lock);
4162 }
4163 }
4164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 if (status)
4166 goto loop_disable;
4167
4168 status = hub_power_remaining(hub);
4169 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004170 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 return;
4173
4174loop_disable:
4175 hub_port_disable(hub, port1, 1);
4176loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004177 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004178 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004179 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004181 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 break;
4183 }
Alan Stern3a311552008-05-20 16:58:29 -04004184 if (hub->hdev->parent ||
4185 !hcd->driver->port_handed_over ||
4186 !(hcd->driver->port_handed_over)(hcd, port1))
4187 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4188 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
4190done:
4191 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304192 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4193 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194}
4195
Sarah Sharp714b07b2012-01-24 13:53:18 -08004196/* Returns 1 if there was a remote wakeup and a connect status change. */
4197static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004198 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004199{
4200 struct usb_device *hdev;
4201 struct usb_device *udev;
4202 int connect_change = 0;
4203 int ret;
4204
4205 hdev = hub->hdev;
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004206 udev = hdev->children[port-1];
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004207 if (!hub_is_superspeed(hdev)) {
4208 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4209 return 0;
4210 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4211 } else {
4212 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004213 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4214 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004215 return 0;
4216 }
4217
Sarah Sharp714b07b2012-01-24 13:53:18 -08004218 if (udev) {
4219 /* TRSMRCY = 10 msec */
4220 msleep(10);
4221
4222 usb_lock_device(udev);
4223 ret = usb_remote_wakeup(udev);
4224 usb_unlock_device(udev);
4225 if (ret < 0)
4226 connect_change = 1;
4227 } else {
4228 ret = -ENODEV;
4229 hub_port_disable(hub, port, 1);
4230 }
4231 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4232 port, ret);
4233 return connect_change;
4234}
4235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236static void hub_events(void)
4237{
4238 struct list_head *tmp;
4239 struct usb_device *hdev;
4240 struct usb_interface *intf;
4241 struct usb_hub *hub;
4242 struct device *hub_dev;
4243 u16 hubstatus;
4244 u16 hubchange;
4245 u16 portstatus;
4246 u16 portchange;
4247 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004248 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 /*
4251 * We restart the list every time to avoid a deadlock with
4252 * deleting hubs downstream from this one. This should be
4253 * safe since we delete the hub from the event list.
4254 * Not the most efficient, but avoids deadlocks.
4255 */
4256 while (1) {
4257
4258 /* Grab the first entry at the beginning of the list */
4259 spin_lock_irq(&hub_event_lock);
4260 if (list_empty(&hub_event_list)) {
4261 spin_unlock_irq(&hub_event_lock);
4262 break;
4263 }
4264
4265 tmp = hub_event_list.next;
4266 list_del_init(tmp);
4267
4268 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004269 kref_get(&hub->kref);
4270 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Alan Sterne8054852007-05-04 11:55:11 -04004272 hdev = hub->hdev;
4273 hub_dev = hub->intfdev;
4274 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004275 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 hdev->state, hub->descriptor
4277 ? hub->descriptor->bNbrPorts
4278 : 0,
4279 /* NOTE: expects max 15 ports... */
4280 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004281 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 /* Lock the device, then check to see if we were
4284 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004285 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004286 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004287 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289 /* If the hub has died, clean up after it */
4290 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004291 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004292 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 goto loop;
4294 }
4295
Alan Stern40f122f2006-11-09 14:44:33 -05004296 /* Autoresume */
4297 ret = usb_autopm_get_interface(intf);
4298 if (ret) {
4299 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004301 }
4302
4303 /* If this is an inactive hub, do nothing */
4304 if (hub->quiescing)
4305 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
4307 if (hub->error) {
4308 dev_dbg (hub_dev, "resetting for error %d\n",
4309 hub->error);
4310
Ming Lei742120c2008-06-18 22:00:29 +08004311 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 if (ret) {
4313 dev_dbg (hub_dev,
4314 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004315 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 }
4317
4318 hub->nerrors = 0;
4319 hub->error = 0;
4320 }
4321
4322 /* deal with port status changes */
4323 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4324 if (test_bit(i, hub->busy_bits))
4325 continue;
4326 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004327 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004329 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 continue;
4331
4332 ret = hub_port_status(hub, i,
4333 &portstatus, &portchange);
4334 if (ret < 0)
4335 continue;
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4338 clear_port_feature(hdev, i,
4339 USB_PORT_FEAT_C_CONNECTION);
4340 connect_change = 1;
4341 }
4342
4343 if (portchange & USB_PORT_STAT_C_ENABLE) {
4344 if (!connect_change)
4345 dev_dbg (hub_dev,
4346 "port %d enable change, "
4347 "status %08x\n",
4348 i, portstatus);
4349 clear_port_feature(hdev, i,
4350 USB_PORT_FEAT_C_ENABLE);
4351
4352 /*
4353 * EM interference sometimes causes badly
4354 * shielded USB devices to be shutdown by
4355 * the hub, this hack enables them again.
4356 * Works at least with mouse driver.
4357 */
4358 if (!(portstatus & USB_PORT_STAT_ENABLE)
4359 && !connect_change
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004360 && hdev->children[i-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 dev_err (hub_dev,
4362 "port %i "
4363 "disabled by hub (EMI?), "
4364 "re-enabling...\n",
4365 i);
4366 connect_change = 1;
4367 }
4368 }
4369
Sarah Sharp72937e12012-01-24 11:46:50 -08004370 if (hub_handle_remote_wakeup(hub, i,
4371 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004372 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004375 u16 status = 0;
4376 u16 unused;
4377
4378 dev_dbg(hub_dev, "over-current change on port "
4379 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 clear_port_feature(hdev, i,
4381 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004382 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004383 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004384 hub_port_status(hub, i, &status, &unused);
4385 if (status & USB_PORT_STAT_OVERCURRENT)
4386 dev_err(hub_dev, "over-current "
4387 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 }
4389
4390 if (portchange & USB_PORT_STAT_C_RESET) {
4391 dev_dbg (hub_dev,
4392 "reset change on port %d\n",
4393 i);
4394 clear_port_feature(hdev, i,
4395 USB_PORT_FEAT_C_RESET);
4396 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004397 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4398 hub_is_superspeed(hub->hdev)) {
4399 dev_dbg(hub_dev,
4400 "warm reset change on port %d\n",
4401 i);
4402 clear_port_feature(hdev, i,
4403 USB_PORT_FEAT_C_BH_PORT_RESET);
4404 }
John Youndbe79bb2001-09-17 00:00:00 -07004405 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4406 clear_port_feature(hub->hdev, i,
4407 USB_PORT_FEAT_C_PORT_LINK_STATE);
4408 }
4409 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4410 dev_warn(hub_dev,
4411 "config error on port %d\n",
4412 i);
4413 clear_port_feature(hub->hdev, i,
4414 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Andiry Xu5e467f62011-04-27 18:07:54 +08004417 /* Warm reset a USB3 protocol port if it's in
4418 * SS.Inactive state.
4419 */
4420 if (hub_is_superspeed(hub->hdev) &&
4421 (portstatus & USB_PORT_STAT_LINK_STATE)
4422 == USB_SS_PORT_LS_SS_INACTIVE) {
4423 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004424 hub_port_reset(hub, i, NULL,
4425 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004426 }
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 if (connect_change)
4429 hub_port_connect_change(hub, i,
4430 portstatus, portchange);
4431 } /* end for i */
4432
4433 /* deal with hub status changes */
4434 if (test_and_clear_bit(0, hub->event_bits) == 0)
4435 ; /* do nothing */
4436 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4437 dev_err (hub_dev, "get_hub_status failed\n");
4438 else {
4439 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4440 dev_dbg (hub_dev, "power change\n");
4441 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004442 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4443 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004444 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004445 else
4446 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 }
4448 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004449 u16 status = 0;
4450 u16 unused;
4451
4452 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004454 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004455 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004456 hub_hub_status(hub, &status, &unused);
4457 if (status & HUB_STATUS_OVERCURRENT)
4458 dev_err(hub_dev, "over-current "
4459 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 }
4461 }
4462
Alan Stern8e4ceb32009-12-07 13:01:37 -05004463 loop_autopm:
4464 /* Balance the usb_autopm_get_interface() above */
4465 usb_autopm_put_interface_no_suspend(intf);
4466 loop:
4467 /* Balance the usb_autopm_get_interface_no_resume() in
4468 * kick_khubd() and allow autosuspend.
4469 */
4470 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004471 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004473 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
4475 } /* end while (1) */
4476}
4477
4478static int hub_thread(void *__unused)
4479{
Alan Stern3bb1af52008-03-03 15:15:36 -05004480 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4481 * port handover. Otherwise it might see that a full-speed device
4482 * was gone before the EHCI controller had handed its port over to
4483 * the companion full-speed controller.
4484 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004485 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 do {
4488 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004489 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004490 !list_empty(&hub_event_list) ||
4491 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004492 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004494 pr_debug("%s: khubd exiting\n", usbcore_name);
4495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496}
4497
Németh Márton1e927d92010-01-10 15:34:53 +01004498static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4500 .bDeviceClass = USB_CLASS_HUB},
4501 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4502 .bInterfaceClass = USB_CLASS_HUB},
4503 { } /* Terminating entry */
4504};
4505
4506MODULE_DEVICE_TABLE (usb, hub_id_table);
4507
4508static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 .name = "hub",
4510 .probe = hub_probe,
4511 .disconnect = hub_disconnect,
4512 .suspend = hub_suspend,
4513 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004514 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004515 .pre_reset = hub_pre_reset,
4516 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004517 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004519 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520};
4521
4522int usb_hub_init(void)
4523{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 if (usb_register(&hub_driver) < 0) {
4525 printk(KERN_ERR "%s: can't register hub driver\n",
4526 usbcore_name);
4527 return -1;
4528 }
4529
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004530 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4531 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533
4534 /* Fall through if kernel_thread failed */
4535 usb_deregister(&hub_driver);
4536 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4537
4538 return -1;
4539}
4540
4541void usb_hub_cleanup(void)
4542{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004543 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545 /*
4546 * Hub resources are freed for us by usb_deregister. It calls
4547 * usb_driver_purge on every device which in turn calls that
4548 * devices disconnect function if it is using this driver.
4549 * The hub_disconnect function takes care of releasing the
4550 * individual hub resources. -greg
4551 */
4552 usb_deregister(&hub_driver);
4553} /* usb_hub_cleanup() */
4554
Alan Sterneb764c42008-03-03 15:16:04 -05004555static int descriptors_changed(struct usb_device *udev,
4556 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
Alan Sterneb764c42008-03-03 15:16:04 -05004558 int changed = 0;
4559 unsigned index;
4560 unsigned serial_len = 0;
4561 unsigned len;
4562 unsigned old_length;
4563 int length;
4564 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
Alan Sterneb764c42008-03-03 15:16:04 -05004566 if (memcmp(&udev->descriptor, old_device_descriptor,
4567 sizeof(*old_device_descriptor)) != 0)
4568 return 1;
4569
4570 /* Since the idVendor, idProduct, and bcdDevice values in the
4571 * device descriptor haven't changed, we will assume the
4572 * Manufacturer and Product strings haven't changed either.
4573 * But the SerialNumber string could be different (e.g., a
4574 * different flash card of the same brand).
4575 */
4576 if (udev->serial)
4577 serial_len = strlen(udev->serial) + 1;
4578
4579 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004581 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4582 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 }
Alan Sterneb764c42008-03-03 15:16:04 -05004584
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004585 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 if (buf == NULL) {
4587 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4588 /* assume the worst */
4589 return 1;
4590 }
4591 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004592 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4594 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004595 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 dev_dbg(&udev->dev, "config index %d, error %d\n",
4597 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004598 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 break;
4600 }
4601 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4602 != 0) {
4603 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004604 index,
4605 ((struct usb_config_descriptor *) buf)->
4606 bConfigurationValue);
4607 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 break;
4609 }
4610 }
Alan Sterneb764c42008-03-03 15:16:04 -05004611
4612 if (!changed && serial_len) {
4613 length = usb_string(udev, udev->descriptor.iSerialNumber,
4614 buf, serial_len);
4615 if (length + 1 != serial_len) {
4616 dev_dbg(&udev->dev, "serial string error %d\n",
4617 length);
4618 changed = 1;
4619 } else if (memcmp(buf, udev->serial, length) != 0) {
4620 dev_dbg(&udev->dev, "serial string changed\n");
4621 changed = 1;
4622 }
4623 }
4624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004626 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627}
4628
4629/**
Ming Lei742120c2008-06-18 22:00:29 +08004630 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4632 *
Alan Stern79efa092006-06-01 13:33:42 -04004633 * WARNING - don't use this routine to reset a composite device
4634 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004635 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 *
4637 * Do a port reset, reassign the device's address, and establish its
4638 * former operating configuration. If the reset fails, or the device's
4639 * descriptors change from their values before the reset, or the original
4640 * configuration and altsettings cannot be restored, a flag will be set
4641 * telling khubd to pretend the device has been disconnected and then
4642 * re-connected. All drivers will be unbound, and the device will be
4643 * re-enumerated and probed all over again.
4644 *
4645 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4646 * flagged for logical disconnection, or some other negative error code
4647 * if the reset wasn't even attempted.
4648 *
4649 * The caller must own the device lock. For example, it's safe to use
4650 * this from a driver probe() routine after downloading new firmware.
4651 * For calls that might not occur during probe(), drivers should lock
4652 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004653 *
4654 * Locking exception: This routine may also be called from within an
4655 * autoresume handler. Such usage won't conflict with other tasks
4656 * holding the device lock because these tasks should always call
4657 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 */
Ming Lei742120c2008-06-18 22:00:29 +08004659static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660{
4661 struct usb_device *parent_hdev = udev->parent;
4662 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004663 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004665 int i, ret = 0;
4666 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
4668 if (udev->state == USB_STATE_NOTATTACHED ||
4669 udev->state == USB_STATE_SUSPENDED) {
4670 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4671 udev->state);
4672 return -EINVAL;
4673 }
4674
4675 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004676 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004677 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 return -EISDIR;
4679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 parent_hub = hdev_to_hub(parent_hdev);
4681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 set_bit(port1, parent_hub->busy_bits);
4683 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4684
4685 /* ep0 maxpacket size may change; let the HCD know about it.
4686 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004687 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004689 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 break;
4691 }
4692 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 if (ret < 0)
4695 goto re_enumerate;
4696
4697 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004698 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 dev_info(&udev->dev, "device firmware changed\n");
4700 udev->descriptor = descriptor; /* for disconnect() calls */
4701 goto re_enumerate;
4702 }
Alan Stern4fe03872009-02-26 10:21:02 -05004703
4704 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 if (!udev->actconfig)
4706 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004707
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004708 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp83060952012-05-02 14:25:52 -07004709 /* Disable LPM while we reset the device and reinstall the alt settings.
4710 * Device-initiated LPM settings, and system exit latency settings are
4711 * cleared when the device is reset, so we have to set them up again.
4712 */
4713 ret = usb_disable_lpm(udev);
4714 if (ret) {
4715 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4716 mutex_unlock(hcd->bandwidth_mutex);
4717 goto done;
4718 }
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004719 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4720 if (ret < 0) {
4721 dev_warn(&udev->dev,
4722 "Busted HC? Not enough HCD resources for "
4723 "old configuration.\n");
Sarah Sharp83060952012-05-02 14:25:52 -07004724 usb_enable_lpm(udev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004725 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004726 goto re_enumerate;
4727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4729 USB_REQ_SET_CONFIGURATION, 0,
4730 udev->actconfig->desc.bConfigurationValue, 0,
4731 NULL, 0, USB_CTRL_SET_TIMEOUT);
4732 if (ret < 0) {
4733 dev_err(&udev->dev,
4734 "can't restore configuration #%d (error=%d)\n",
4735 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharp83060952012-05-02 14:25:52 -07004736 usb_enable_lpm(udev);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004737 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 goto re_enumerate;
4739 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004740 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4742
Alan Stern4fe03872009-02-26 10:21:02 -05004743 /* Put interfaces back into the same altsettings as before.
4744 * Don't bother to send the Set-Interface request for interfaces
4745 * that were already in altsetting 0; besides being unnecessary,
4746 * many devices can't handle it. Instead just reset the host-side
4747 * endpoint state.
4748 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004750 struct usb_host_config *config = udev->actconfig;
4751 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 struct usb_interface_descriptor *desc;
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004755 if (desc->bAlternateSetting == 0) {
4756 usb_disable_interface(udev, intf, true);
4757 usb_enable_interface(udev, intf, true);
4758 ret = 0;
4759 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004760 /* Let the bandwidth allocation function know that this
4761 * device has been reset, and it will have to use
4762 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004763 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004764 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004765 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4766 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004767 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 if (ret < 0) {
4770 dev_err(&udev->dev, "failed to restore interface %d "
4771 "altsetting %d (error=%d)\n",
4772 desc->bInterfaceNumber,
4773 desc->bAlternateSetting,
4774 ret);
Sarah Sharp83060952012-05-02 14:25:52 -07004775 usb_unlocked_enable_lpm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 goto re_enumerate;
4777 }
4778 }
4779
Sarah Sharp83060952012-05-02 14:25:52 -07004780 /* Now that the alt settings are re-installed, enable LPM. */
4781 usb_unlocked_enable_lpm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 return 0;
4784
4785re_enumerate:
4786 hub_port_logical_disconnect(parent_hub, port1);
4787 return -ENODEV;
4788}
Alan Stern79efa092006-06-01 13:33:42 -04004789
4790/**
Ming Lei742120c2008-06-18 22:00:29 +08004791 * usb_reset_device - warn interface drivers and perform a USB port reset
Alan Stern79efa092006-06-01 13:33:42 -04004792 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
Alan Stern79efa092006-06-01 13:33:42 -04004793 *
4794 * Warns all drivers bound to registered interfaces (using their pre_reset
4795 * method), performs the port reset, and then lets the drivers know that
4796 * the reset is over (using their post_reset method).
4797 *
Ming Lei742120c2008-06-18 22:00:29 +08004798 * Return value is the same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04004799 *
4800 * The caller must own the device lock. For example, it's safe to use
4801 * this from a driver probe() routine after downloading new firmware.
4802 * For calls that might not occur during probe(), drivers should lock
4803 * the device using usb_lock_device_for_reset().
Alan Stern78d9a482008-06-23 16:00:40 -04004804 *
4805 * If an interface is currently being probed or disconnected, we assume
4806 * its driver knows how to handle resets. For all other interfaces,
4807 * if the driver doesn't have pre_reset and post_reset methods then
4808 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004809 */
Ming Lei742120c2008-06-18 22:00:29 +08004810int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004811{
4812 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004813 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004814 struct usb_host_config *config = udev->actconfig;
4815
4816 if (udev->state == USB_STATE_NOTATTACHED ||
4817 udev->state == USB_STATE_SUSPENDED) {
4818 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4819 udev->state);
4820 return -EINVAL;
4821 }
4822
Alan Stern645daaa2006-08-30 15:47:02 -04004823 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004824 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004825
Alan Stern79efa092006-06-01 13:33:42 -04004826 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004827 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004828 struct usb_interface *cintf = config->interface[i];
4829 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004830 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004831
4832 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004833 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004834 if (drv->pre_reset && drv->post_reset)
4835 unbind = (drv->pre_reset)(cintf);
4836 else if (cintf->condition ==
4837 USB_INTERFACE_BOUND)
4838 unbind = 1;
4839 if (unbind)
4840 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004841 }
4842 }
4843 }
4844
Ming Lei742120c2008-06-18 22:00:29 +08004845 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004846
4847 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004848 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004849 struct usb_interface *cintf = config->interface[i];
4850 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004851 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004852
Alan Stern78d9a482008-06-23 16:00:40 -04004853 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004854 drv = to_usb_driver(cintf->dev.driver);
4855 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004856 rebind = (drv->post_reset)(cintf);
4857 else if (cintf->condition ==
4858 USB_INTERFACE_BOUND)
4859 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04004860 }
Alan Stern6c640942008-10-21 15:40:03 -04004861 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04004862 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004863 }
4864 }
4865
Alan Stern94fcda12006-11-20 11:38:46 -05004866 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004867 return ret;
4868}
Ming Lei742120c2008-06-18 22:00:29 +08004869EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08004870
4871
4872/**
4873 * usb_queue_reset_device - Reset a USB device from an atomic context
4874 * @iface: USB interface belonging to the device to reset
4875 *
4876 * This function can be used to reset a USB device from an atomic
4877 * context, where usb_reset_device() won't work (as it blocks).
4878 *
4879 * Doing a reset via this method is functionally equivalent to calling
4880 * usb_reset_device(), except for the fact that it is delayed to a
4881 * workqueue. This means that any drivers bound to other interfaces
4882 * might be unbound, as well as users from usbfs in user space.
4883 *
4884 * Corner cases:
4885 *
4886 * - Scheduling two resets at the same time from two different drivers
4887 * attached to two different interfaces of the same device is
4888 * possible; depending on how the driver attached to each interface
4889 * handles ->pre_reset(), the second reset might happen or not.
4890 *
4891 * - If a driver is unbound and it had a pending reset, the reset will
4892 * be cancelled.
4893 *
4894 * - This function can be called during .probe() or .disconnect()
4895 * times. On return from .disconnect(), any pending resets will be
4896 * cancelled.
4897 *
4898 * There is no no need to lock/unlock the @reset_ws as schedule_work()
4899 * does its own.
4900 *
4901 * NOTE: We don't do any reference count tracking because it is not
4902 * needed. The lifecycle of the work_struct is tied to the
4903 * usb_interface. Before destroying the interface we cancel the
4904 * work_struct, so the fact that work_struct is queued and or
4905 * running means the interface (and thus, the device) exist and
4906 * are referenced.
4907 */
4908void usb_queue_reset_device(struct usb_interface *iface)
4909{
4910 schedule_work(&iface->reset_ws);
4911}
4912EXPORT_SYMBOL_GPL(usb_queue_reset_device);