blob: 87df22eef491d5158fae1f3bf683ce562b0cc404 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB hub driver.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/completion.h>
16#include <linux/sched.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020022#include <linux/usb/hcd.h>
Richard Zhao925aa462012-07-11 11:09:28 +080023#include <linux/usb/otg.h>
Phil Dibowitz93362a82010-07-22 00:05:01 +020024#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070025#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010026#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Theodore Ts'ob04b3152012-07-04 11:22:20 -040028#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/byteorder.h>
32
33#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080035/* if we are in debug mode, always announce new devices */
36#ifdef DEBUG
37#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
39#endif
40#endif
41
Lan Tianyufa2a9562012-09-05 13:44:31 +080042struct usb_port {
43 struct device dev;
44 struct dev_state *port_owner;
45};
46
Alan Stern1bb5f662006-11-06 11:56:13 -050047struct usb_hub {
48 struct device *intfdev; /* the "interface" device */
49 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040050 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050051 struct urb *urb; /* for interrupt polling pipe */
52
53 /* buffer for urb ... with extra space in case of babble */
54 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050055 union {
56 struct usb_hub_status hub;
57 struct usb_port_status port;
58 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050059 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050060
61 int error; /* last reported error */
62 int nerrors; /* track consecutive errors */
63
64 struct list_head event_list; /* hubs w/data or errs ready */
65 unsigned long event_bits[1]; /* status change bitmask */
66 unsigned long change_bits[1]; /* ports with logical connect
67 status change */
68 unsigned long busy_bits[1]; /* ports being reset or
69 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040070 unsigned long removed_bits[1]; /* ports with a "removed"
71 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080072 unsigned long wakeup_bits[1]; /* ports that have signaled
73 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050074#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
75#error event_bits[] is too short!
76#endif
77
78 struct usb_hub_descriptor *descriptor; /* class descriptor */
79 struct usb_tt tt; /* Transaction Translator */
80
81 unsigned mA_per_port; /* current for each child */
82
83 unsigned limited_power:1;
84 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040085 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050086
87 unsigned has_indicators:1;
88 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000089 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -040090 struct delayed_work init_work;
Lan Tianyufa2a9562012-09-05 13:44:31 +080091 struct usb_port **ports;
Alan Stern1bb5f662006-11-06 11:56:13 -050092};
93
John Youndbe79bb2001-09-17 00:00:00 -070094static inline int hub_is_superspeed(struct usb_device *hdev)
95{
Aman Deep7bf01182011-12-08 12:05:22 +053096 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070097}
Alan Stern1bb5f662006-11-06 11:56:13 -050098
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070099/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -0500100 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
102static DEFINE_SPINLOCK(device_state_lock);
103
104/* khubd's worklist and its lock */
105static DEFINE_SPINLOCK(hub_event_lock);
106static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
107
108/* Wakes up khubd */
109static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
110
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700111static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030114static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115module_param (blinkenlights, bool, S_IRUGO);
116MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
117
118/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200119 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
120 * 10 seconds to send reply for the initial 64-byte descriptor request.
121 */
122/* define initial 64-byte descriptor request timeout in milliseconds */
123static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
124module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800125MODULE_PARM_DESC(initial_descriptor_timeout,
126 "initial 64-byte descriptor request timeout in milliseconds "
127 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200128
129/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 * As of 2.6.10 we introduce a new USB device initialization scheme which
131 * closely resembles the way Windows works. Hopefully it will be compatible
132 * with a wider range of devices than the old scheme. However some previously
133 * working devices may start giving rise to "device not accepting address"
134 * errors; if that happens the user can try the old scheme by adjusting the
135 * following module parameters.
136 *
137 * For maximum flexibility there are two boolean parameters to control the
138 * hub driver's behavior. On the first initialization attempt, if the
139 * "old_scheme_first" parameter is set then the old scheme will be used,
140 * otherwise the new scheme is used. If that fails and "use_both_schemes"
141 * is set, then the driver will make another attempt, using the other scheme.
142 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030143static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
145MODULE_PARM_DESC(old_scheme_first,
146 "start with the old device initialization scheme");
147
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030148static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
150MODULE_PARM_DESC(use_both_schemes,
151 "try the other device initialization scheme if the "
152 "first one fails");
153
Alan Stern32fe0192007-10-10 16:27:07 -0400154/* Mutual exclusion for EHCI CF initialization. This interferes with
155 * port reset on some companion controllers.
156 */
157DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
158EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
159
Alan Stern948fea32008-04-28 11:07:07 -0400160#define HUB_DEBOUNCE_TIMEOUT 1500
161#define HUB_DEBOUNCE_STEP 25
162#define HUB_DEBOUNCE_STABLE 100
163
Lan Tianyufa2a9562012-09-05 13:44:31 +0800164#define to_usb_port(_dev) \
165 container_of(_dev, struct usb_port, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Ming Lei742120c2008-06-18 22:00:29 +0800167static int usb_reset_and_verify_device(struct usb_device *udev);
168
Sarah Sharp131dec32010-12-06 21:00:19 -0800169static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
Sarah Sharp131dec32010-12-06 21:00:19 -0800171 if (hub_is_superspeed(hub->hdev))
172 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500173 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500175 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return "1.5 Mb/s";
177 else
178 return "12 Mb/s";
179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400182static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700184 if (!hdev || !hdev->actconfig)
Alan Stern251180842009-07-22 14:41:18 -0400185 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return usb_get_intfdata(hdev->actconfig->interface[0]);
187}
188
Sarah Sharpd9b20992012-02-20 08:31:26 -0800189static int usb_device_supports_lpm(struct usb_device *udev)
190{
191 /* USB 2.1 (and greater) devices indicate LPM support through
192 * their USB 2.0 Extended Capabilities BOS descriptor.
193 */
194 if (udev->speed == USB_SPEED_HIGH) {
195 if (udev->bos->ext_cap &&
196 (USB_LPM_SUPPORT &
197 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
198 return 1;
199 return 0;
200 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800201
202 /* All USB 3.0 must support LPM, but we need their max exit latency
203 * information from the SuperSpeed Extended Capabilities BOS descriptor.
204 */
205 if (!udev->bos->ss_cap) {
206 dev_warn(&udev->dev, "No LPM exit latency info found. "
207 "Power management will be impacted.\n");
208 return 0;
209 }
210 if (udev->parent->lpm_capable)
211 return 1;
212
213 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
214 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800215 return 0;
216}
217
Sarah Sharp51e0a012012-02-20 12:02:19 -0800218/*
219 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
220 * either U1 or U2.
221 */
222static void usb_set_lpm_mel(struct usb_device *udev,
223 struct usb3_lpm_parameters *udev_lpm_params,
224 unsigned int udev_exit_latency,
225 struct usb_hub *hub,
226 struct usb3_lpm_parameters *hub_lpm_params,
227 unsigned int hub_exit_latency)
228{
229 unsigned int total_mel;
230 unsigned int device_mel;
231 unsigned int hub_mel;
232
233 /*
234 * Calculate the time it takes to transition all links from the roothub
235 * to the parent hub into U0. The parent hub must then decode the
236 * packet (hub header decode latency) to figure out which port it was
237 * bound for.
238 *
239 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
240 * means 0.1us). Multiply that by 100 to get nanoseconds.
241 */
242 total_mel = hub_lpm_params->mel +
243 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
244
245 /*
246 * How long will it take to transition the downstream hub's port into
247 * U0? The greater of either the hub exit latency or the device exit
248 * latency.
249 *
250 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
251 * Multiply that by 1000 to get nanoseconds.
252 */
253 device_mel = udev_exit_latency * 1000;
254 hub_mel = hub_exit_latency * 1000;
255 if (device_mel > hub_mel)
256 total_mel += device_mel;
257 else
258 total_mel += hub_mel;
259
260 udev_lpm_params->mel = total_mel;
261}
262
263/*
264 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
265 * a transition from either U1 or U2.
266 */
267static void usb_set_lpm_pel(struct usb_device *udev,
268 struct usb3_lpm_parameters *udev_lpm_params,
269 unsigned int udev_exit_latency,
270 struct usb_hub *hub,
271 struct usb3_lpm_parameters *hub_lpm_params,
272 unsigned int hub_exit_latency,
273 unsigned int port_to_port_exit_latency)
274{
275 unsigned int first_link_pel;
276 unsigned int hub_pel;
277
278 /*
279 * First, the device sends an LFPS to transition the link between the
280 * device and the parent hub into U0. The exit latency is the bigger of
281 * the device exit latency or the hub exit latency.
282 */
283 if (udev_exit_latency > hub_exit_latency)
284 first_link_pel = udev_exit_latency * 1000;
285 else
286 first_link_pel = hub_exit_latency * 1000;
287
288 /*
289 * When the hub starts to receive the LFPS, there is a slight delay for
290 * it to figure out that one of the ports is sending an LFPS. Then it
291 * will forward the LFPS to its upstream link. The exit latency is the
292 * delay, plus the PEL that we calculated for this hub.
293 */
294 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
295
296 /*
297 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
298 * is the greater of the two exit latencies.
299 */
300 if (first_link_pel > hub_pel)
301 udev_lpm_params->pel = first_link_pel;
302 else
303 udev_lpm_params->pel = hub_pel;
304}
305
306/*
307 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
308 * when a device initiates a transition to U0, until when it will receive the
309 * first packet from the host controller.
310 *
311 * Section C.1.5.1 describes the four components to this:
312 * - t1: device PEL
313 * - t2: time for the ERDY to make it from the device to the host.
314 * - t3: a host-specific delay to process the ERDY.
315 * - t4: time for the packet to make it from the host to the device.
316 *
317 * t3 is specific to both the xHCI host and the platform the host is integrated
318 * into. The Intel HW folks have said it's negligible, FIXME if a different
319 * vendor says otherwise.
320 */
321static void usb_set_lpm_sel(struct usb_device *udev,
322 struct usb3_lpm_parameters *udev_lpm_params)
323{
324 struct usb_device *parent;
325 unsigned int num_hubs;
326 unsigned int total_sel;
327
328 /* t1 = device PEL */
329 total_sel = udev_lpm_params->pel;
330 /* How many external hubs are in between the device & the root port. */
331 for (parent = udev->parent, num_hubs = 0; parent->parent;
332 parent = parent->parent)
333 num_hubs++;
334 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
335 if (num_hubs > 0)
336 total_sel += 2100 + 250 * (num_hubs - 1);
337
338 /* t4 = 250ns * num_hubs */
339 total_sel += 250 * num_hubs;
340
341 udev_lpm_params->sel = total_sel;
342}
343
344static void usb_set_lpm_parameters(struct usb_device *udev)
345{
346 struct usb_hub *hub;
347 unsigned int port_to_port_delay;
348 unsigned int udev_u1_del;
349 unsigned int udev_u2_del;
350 unsigned int hub_u1_del;
351 unsigned int hub_u2_del;
352
353 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
354 return;
355
356 hub = hdev_to_hub(udev->parent);
357 /* It doesn't take time to transition the roothub into U0, since it
358 * doesn't have an upstream link.
359 */
360 if (!hub)
361 return;
362
363 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
364 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
365 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
366 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
367
368 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
369 hub, &udev->parent->u1_params, hub_u1_del);
370
371 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
372 hub, &udev->parent->u2_params, hub_u2_del);
373
374 /*
375 * Appendix C, section C.2.2.2, says that there is a slight delay from
376 * when the parent hub notices the downstream port is trying to
377 * transition to U0 to when the hub initiates a U0 transition on its
378 * upstream port. The section says the delays are tPort2PortU1EL and
379 * tPort2PortU2EL, but it doesn't define what they are.
380 *
381 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
382 * about the same delays. Use the maximum delay calculations from those
383 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
384 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
385 * assume the device exit latencies they are talking about are the hub
386 * exit latencies.
387 *
388 * What do we do if the U2 exit latency is less than the U1 exit
389 * latency? It's possible, although not likely...
390 */
391 port_to_port_delay = 1;
392
393 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
394 hub, &udev->parent->u1_params, hub_u1_del,
395 port_to_port_delay);
396
397 if (hub_u2_del > hub_u1_del)
398 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
399 else
400 port_to_port_delay = 1 + hub_u1_del;
401
402 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
403 hub, &udev->parent->u2_params, hub_u2_del,
404 port_to_port_delay);
405
406 /* Now that we've got PEL, calculate SEL. */
407 usb_set_lpm_sel(udev, &udev->u1_params);
408 usb_set_lpm_sel(udev, &udev->u2_params);
409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700412static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
John Youndbe79bb2001-09-17 00:00:00 -0700414 int i, ret, size;
415 unsigned dtype;
416
417 if (hub_is_superspeed(hdev)) {
418 dtype = USB_DT_SS_HUB;
419 size = USB_DT_SS_HUB_SIZE;
420 } else {
421 dtype = USB_DT_HUB;
422 size = sizeof(struct usb_hub_descriptor);
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 for (i = 0; i < 3; i++) {
426 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
427 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700428 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 USB_CTRL_GET_TIMEOUT);
430 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
431 return ret;
432 }
433 return -EINVAL;
434}
435
436/*
437 * USB 2.0 spec Section 11.24.2.1
438 */
439static int clear_hub_feature(struct usb_device *hdev, int feature)
440{
441 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
442 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
443}
444
445/*
446 * USB 2.0 spec Section 11.24.2.2
447 */
448static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
449{
450 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
451 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
452 NULL, 0, 1000);
453}
454
455/*
456 * USB 2.0 spec Section 11.24.2.13
457 */
458static int set_port_feature(struct usb_device *hdev, int port1, int feature)
459{
460 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
461 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
462 NULL, 0, 1000);
463}
464
465/*
466 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
467 * for info about using port indicators
468 */
469static void set_port_led(
470 struct usb_hub *hub,
471 int port1,
472 int selector
473)
474{
475 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
476 USB_PORT_FEAT_INDICATOR);
477 if (status < 0)
478 dev_dbg (hub->intfdev,
479 "port %d indicator %s status %d\n",
480 port1,
481 ({ char *s; switch (selector) {
482 case HUB_LED_AMBER: s = "amber"; break;
483 case HUB_LED_GREEN: s = "green"; break;
484 case HUB_LED_OFF: s = "off"; break;
485 case HUB_LED_AUTO: s = "auto"; break;
486 default: s = "??"; break;
487 }; s; }),
488 status);
489}
490
491#define LED_CYCLE_PERIOD ((2*HZ)/3)
492
David Howellsc4028952006-11-22 14:57:56 +0000493static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
David Howellsc4028952006-11-22 14:57:56 +0000495 struct usb_hub *hub =
496 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct usb_device *hdev = hub->hdev;
498 unsigned i;
499 unsigned changed = 0;
500 int cursor = -1;
501
502 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
503 return;
504
505 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
506 unsigned selector, mode;
507
508 /* 30%-50% duty cycle */
509
510 switch (hub->indicator[i]) {
511 /* cycle marker */
512 case INDICATOR_CYCLE:
513 cursor = i;
514 selector = HUB_LED_AUTO;
515 mode = INDICATOR_AUTO;
516 break;
517 /* blinking green = sw attention */
518 case INDICATOR_GREEN_BLINK:
519 selector = HUB_LED_GREEN;
520 mode = INDICATOR_GREEN_BLINK_OFF;
521 break;
522 case INDICATOR_GREEN_BLINK_OFF:
523 selector = HUB_LED_OFF;
524 mode = INDICATOR_GREEN_BLINK;
525 break;
526 /* blinking amber = hw attention */
527 case INDICATOR_AMBER_BLINK:
528 selector = HUB_LED_AMBER;
529 mode = INDICATOR_AMBER_BLINK_OFF;
530 break;
531 case INDICATOR_AMBER_BLINK_OFF:
532 selector = HUB_LED_OFF;
533 mode = INDICATOR_AMBER_BLINK;
534 break;
535 /* blink green/amber = reserved */
536 case INDICATOR_ALT_BLINK:
537 selector = HUB_LED_GREEN;
538 mode = INDICATOR_ALT_BLINK_OFF;
539 break;
540 case INDICATOR_ALT_BLINK_OFF:
541 selector = HUB_LED_AMBER;
542 mode = INDICATOR_ALT_BLINK;
543 break;
544 default:
545 continue;
546 }
547 if (selector != HUB_LED_AUTO)
548 changed = 1;
549 set_port_led(hub, i + 1, selector);
550 hub->indicator[i] = mode;
551 }
552 if (!changed && blinkenlights) {
553 cursor++;
554 cursor %= hub->descriptor->bNbrPorts;
555 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
556 hub->indicator[cursor] = INDICATOR_CYCLE;
557 changed++;
558 }
559 if (changed)
560 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
561}
562
563/* use a short timeout for hub/port status fetches */
564#define USB_STS_TIMEOUT 1000
565#define USB_STS_RETRIES 5
566
567/*
568 * USB 2.0 spec Section 11.24.2.6
569 */
570static int get_hub_status(struct usb_device *hdev,
571 struct usb_hub_status *data)
572{
573 int i, status = -ETIMEDOUT;
574
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200575 for (i = 0; i < USB_STS_RETRIES &&
576 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
578 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
579 data, sizeof(*data), USB_STS_TIMEOUT);
580 }
581 return status;
582}
583
584/*
585 * USB 2.0 spec Section 11.24.2.7
586 */
587static int get_port_status(struct usb_device *hdev, int port1,
588 struct usb_port_status *data)
589{
590 int i, status = -ETIMEDOUT;
591
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200592 for (i = 0; i < USB_STS_RETRIES &&
593 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
595 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
596 data, sizeof(*data), USB_STS_TIMEOUT);
597 }
598 return status;
599}
600
Alan Stern3eb14912008-03-03 15:15:43 -0500601static int hub_port_status(struct usb_hub *hub, int port1,
602 u16 *status, u16 *change)
603{
604 int ret;
605
606 mutex_lock(&hub->status_mutex);
607 ret = get_port_status(hub->hdev, port1, &hub->status->port);
608 if (ret < 4) {
609 dev_err(hub->intfdev,
610 "%s failed (err = %d)\n", __func__, ret);
611 if (ret >= 0)
612 ret = -EIO;
613 } else {
614 *status = le16_to_cpu(hub->status->port.wPortStatus);
615 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700616
Alan Stern3eb14912008-03-03 15:15:43 -0500617 ret = 0;
618 }
619 mutex_unlock(&hub->status_mutex);
620 return ret;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623static void kick_khubd(struct usb_hub *hub)
624{
625 unsigned long flags;
626
627 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200628 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500630
631 /* Suppress autosuspend until khubd runs */
632 usb_autopm_get_interface_no_resume(
633 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 wake_up(&khubd_wait);
635 }
636 spin_unlock_irqrestore(&hub_event_lock, flags);
637}
638
639void usb_kick_khubd(struct usb_device *hdev)
640{
Alan Stern251180842009-07-22 14:41:18 -0400641 struct usb_hub *hub = hdev_to_hub(hdev);
642
643 if (hub)
644 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800647/*
648 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
649 * Notification, which indicates it had initiated remote wakeup.
650 *
651 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
652 * device initiates resume, so the USB core will not receive notice of the
653 * resume through the normal hub interrupt URB.
654 */
655void usb_wakeup_notification(struct usb_device *hdev,
656 unsigned int portnum)
657{
658 struct usb_hub *hub;
659
660 if (!hdev)
661 return;
662
663 hub = hdev_to_hub(hdev);
664 if (hub) {
665 set_bit(portnum, hub->wakeup_bits);
666 kick_khubd(hub);
667 }
668}
669EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100672static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200674 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400675 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100676 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned long bits;
678
Alan Sterne0152682007-08-24 15:42:52 -0400679 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 case -ENOENT: /* synchronous unlink */
681 case -ECONNRESET: /* async unlink */
682 case -ESHUTDOWN: /* hardware going away */
683 return;
684
685 default: /* presumably an error */
686 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400687 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if ((++hub->nerrors < 10) || hub->error)
689 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400690 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* let khubd handle things */
694 case 0: /* we got data: port status changed */
695 bits = 0;
696 for (i = 0; i < urb->actual_length; ++i)
697 bits |= ((unsigned long) ((*hub->buffer)[i]))
698 << (i*8);
699 hub->event_bits[0] = bits;
700 break;
701 }
702
703 hub->nerrors = 0;
704
705 /* Something happened, let khubd figure it out */
706 kick_khubd(hub);
707
708resubmit:
709 if (hub->quiescing)
710 return;
711
712 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
713 && status != -ENODEV && status != -EPERM)
714 dev_err (hub->intfdev, "resubmit --> %d\n", status);
715}
716
717/* USB 2.0 spec Section 11.24.2.3 */
718static inline int
719hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
720{
Alan Sternc2f65952009-11-18 11:37:15 -0500721 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
723 tt, NULL, 0, 1000);
724}
725
726/*
727 * enumeration blocks khubd for a long time. we use keventd instead, since
728 * long blocking there is the exception, not the rule. accordingly, HCDs
729 * talking to TTs must queue control transfers (not just bulk and iso), so
730 * both can talk to the same hub concurrently.
731 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400732static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
David Howellsc4028952006-11-22 14:57:56 +0000734 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400735 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400737 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 spin_lock_irqsave (&hub->tt.lock, flags);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400740 while (--limit && !list_empty (&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400741 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct usb_tt_clear *clear;
743 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400744 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 int status;
746
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400747 next = hub->tt.clear_list.next;
748 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 list_del (&clear->clear_list);
750
751 /* drop lock so HCD can concurrently report other TT errors */
752 spin_unlock_irqrestore (&hub->tt.lock, flags);
753 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (status)
755 dev_err (&hdev->dev,
756 "clear tt %d (%04x) error %d\n",
757 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400758
759 /* Tell the HCD, even if the operation failed */
760 drv = clear->hcd->driver;
761 if (drv->clear_tt_buffer_complete)
762 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
763
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700764 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400765 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 spin_unlock_irqrestore (&hub->tt.lock, flags);
768}
769
770/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400771 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
772 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
774 * High speed HCDs use this to tell the hub driver that some split control or
775 * bulk transaction failed in a way that requires clearing internal state of
776 * a transaction translator. This is normally detected (and reported) from
777 * interrupt context.
778 *
779 * It may not be possible for that hub to handle additional full (or low)
780 * speed transactions until that state is fully cleared out.
781 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400782int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400784 struct usb_device *udev = urb->dev;
785 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct usb_tt *tt = udev->tt;
787 unsigned long flags;
788 struct usb_tt_clear *clear;
789
790 /* we've got to cope with an arbitrary number of pending TT clears,
791 * since each TT has "at least two" buffers that can need it (and
792 * there can be many TTs per hub). even if they're uncommon.
793 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800794 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
796 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400797 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
800 /* info that CLEAR_TT_BUFFER needs */
801 clear->tt = tt->multi ? udev->ttport : 1;
802 clear->devinfo = usb_pipeendpoint (pipe);
803 clear->devinfo |= udev->devnum << 4;
804 clear->devinfo |= usb_pipecontrol (pipe)
805 ? (USB_ENDPOINT_XFER_CONTROL << 11)
806 : (USB_ENDPOINT_XFER_BULK << 11);
807 if (usb_pipein (pipe))
808 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400809
810 /* info for completion callback */
811 clear->hcd = bus_to_hcd(udev->bus);
812 clear->ep = urb->ep;
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /* tell keventd to clear state for this TT */
815 spin_lock_irqsave (&tt->lock, flags);
816 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400817 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400819 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400821EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Alan Stern8520f382008-09-22 14:44:26 -0400823/* If do_delay is false, return the number of milliseconds the caller
824 * needs to delay.
825 */
826static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700829 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400830 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400831 u16 wHubCharacteristics =
832 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Alan Stern4489a572006-04-27 15:54:22 -0400834 /* Enable power on each port. Some hubs have reserved values
835 * of LPSM (> 2) in their descriptors, even though they are
836 * USB 2.0 hubs. Some hubs do not implement port-power switching
837 * but only emulate it. In all cases, the ports won't work
838 * unless we send these messages to the hub.
839 */
840 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400842 else
843 dev_dbg(hub->intfdev, "trying to enable port power on "
844 "non-switchable hub\n");
845 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
846 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
David Brownellb7896962005-08-31 10:41:44 -0700848 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400849 delay = max(pgood_delay, (unsigned) 100);
850 if (do_delay)
851 msleep(delay);
852 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855static int hub_hub_status(struct usb_hub *hub,
856 u16 *status, u16 *change)
857{
858 int ret;
859
Alan Sterndb90e7a2007-02-05 09:56:15 -0500860 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ret = get_hub_status(hub->hdev, &hub->status->hub);
862 if (ret < 0)
863 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800864 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 else {
866 *status = le16_to_cpu(hub->status->hub.wHubStatus);
867 *change = le16_to_cpu(hub->status->hub.wHubChange);
868 ret = 0;
869 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500870 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return ret;
872}
873
Alan Stern8b28c752005-08-10 17:04:13 -0400874static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
875{
876 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400877 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400878
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700879 if (hdev->children[port1-1] && set_state)
880 usb_set_device_state(hdev->children[port1-1],
Alan Stern8b28c752005-08-10 17:04:13 -0400881 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700882 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400883 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400884 if (ret)
885 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400886 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400887 return ret;
888}
889
Alan Stern0458d5b2007-05-04 11:52:20 -0400890/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800891 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400892 * time later khubd will disconnect() any existing usb_device on the port
893 * and will re-enumerate if there actually is a device attached.
894 */
895static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
896{
897 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
898 hub_port_disable(hub, port1, 1);
899
900 /* FIXME let caller ask to power down the port:
901 * - some devices won't enumerate without a VBUS power cycle
902 * - SRP saves power that way
903 * - ... new call, TBD ...
904 * That's easy if this hub can switch power per-port, and
905 * khubd reactivates the port later (timer, SRP, etc).
906 * Powerdown must be optional, because of reset/DFU.
907 */
908
909 set_bit(port1, hub->change_bits);
910 kick_khubd(hub);
911}
912
Alan Stern253e0572009-10-27 15:20:13 -0400913/**
914 * usb_remove_device - disable a device's port on its parent hub
915 * @udev: device to be disabled and removed
916 * Context: @udev locked, must be able to sleep.
917 *
918 * After @udev's port has been disabled, khubd is notified and it will
919 * see that the device has been disconnected. When the device is
920 * physically unplugged and something is plugged in, the events will
921 * be received and processed normally.
922 */
923int usb_remove_device(struct usb_device *udev)
924{
925 struct usb_hub *hub;
926 struct usb_interface *intf;
927
928 if (!udev->parent) /* Can't remove a root hub */
929 return -EINVAL;
930 hub = hdev_to_hub(udev->parent);
931 intf = to_usb_interface(hub->intfdev);
932
933 usb_autopm_get_interface(intf);
934 set_bit(udev->portnum, hub->removed_bits);
935 hub_port_logical_disconnect(hub, udev->portnum);
936 usb_autopm_put_interface(intf);
937 return 0;
938}
939
Alan Stern6ee0b272008-04-28 11:06:42 -0400940enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500941 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400942 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400943};
Alan Stern5e6effa2008-03-03 15:15:51 -0500944
Alan Stern8520f382008-09-22 14:44:26 -0400945static void hub_init_func2(struct work_struct *ws);
946static void hub_init_func3(struct work_struct *ws);
947
Alan Sternf2835212008-04-28 11:07:17 -0400948static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500949{
950 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800951 struct usb_hcd *hcd;
952 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500953 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400954 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400955 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400956 unsigned delay;
957
958 /* Continue a partial initialization */
959 if (type == HUB_INIT2)
960 goto init2;
961 if (type == HUB_INIT3)
962 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500963
Elric Fua45aa3b2012-02-18 13:32:27 +0800964 /* The superspeed hub except for root hub has to use Hub Depth
965 * value as an offset into the route string to locate the bits
966 * it uses to determine the downstream port number. So hub driver
967 * should send a set hub depth request to superspeed hub after
968 * the superspeed hub is set configuration in initialization or
969 * reset procedure.
970 *
971 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400972 * For any other type of activation, turn it on.
973 */
Alan Stern8520f382008-09-22 14:44:26 -0400974 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800975 if (hdev->parent && hub_is_superspeed(hdev)) {
976 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
977 HUB_SET_DEPTH, USB_RT_HUB,
978 hdev->level - 1, 0, NULL, 0,
979 USB_CTRL_SET_TIMEOUT);
980 if (ret < 0)
981 dev_err(hub->intfdev,
982 "set hub depth failed\n");
983 }
Alan Stern8520f382008-09-22 14:44:26 -0400984
985 /* Speed up system boot by using a delayed_work for the
986 * hub's initial power-up delays. This is pretty awkward
987 * and the implementation looks like a home-brewed sort of
988 * setjmp/longjmp, but it saves at least 100 ms for each
989 * root hub (assuming usbcore is compiled into the kernel
990 * rather than as a module). It adds up.
991 *
992 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
993 * because for those activation types the ports have to be
994 * operational when we return. In theory this could be done
995 * for HUB_POST_RESET, but it's easier not to.
996 */
997 if (type == HUB_INIT) {
998 delay = hub_power_on(hub, false);
999 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1000 schedule_delayed_work(&hub->init_work,
1001 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001002
1003 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001004 usb_autopm_get_interface_no_resume(
1005 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001006 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001007 } else if (type == HUB_RESET_RESUME) {
1008 /* The internal host controller state for the hub device
1009 * may be gone after a host power loss on system resume.
1010 * Update the device's info so the HW knows it's a hub.
1011 */
1012 hcd = bus_to_hcd(hdev->bus);
1013 if (hcd->driver->update_hub_device) {
1014 ret = hcd->driver->update_hub_device(hcd, hdev,
1015 &hub->tt, GFP_NOIO);
1016 if (ret < 0) {
1017 dev_err(hub->intfdev, "Host not "
1018 "accepting hub info "
1019 "update.\n");
1020 dev_err(hub->intfdev, "LS/FS devices "
1021 "and hubs may not work "
1022 "under this hub\n.");
1023 }
1024 }
1025 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001026 } else {
1027 hub_power_on(hub, true);
1028 }
1029 }
1030 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001031
Alan Stern6ee0b272008-04-28 11:06:42 -04001032 /* Check each port and set hub->change_bits to let khubd know
1033 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001034 */
1035 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001036 struct usb_device *udev = hdev->children[port1-1];
Alan Stern5e6effa2008-03-03 15:15:51 -05001037 u16 portstatus, portchange;
1038
Alan Stern6ee0b272008-04-28 11:06:42 -04001039 portstatus = portchange = 0;
1040 status = hub_port_status(hub, port1, &portstatus, &portchange);
1041 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1042 dev_dbg(hub->intfdev,
1043 "port %d: status %04x change %04x\n",
1044 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001045
Alan Stern6ee0b272008-04-28 11:06:42 -04001046 /* After anything other than HUB_RESUME (i.e., initialization
1047 * or any sort of reset), every port should be disabled.
1048 * Unconnected ports should likewise be disabled (paranoia),
1049 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001050 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001051 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1052 type != HUB_RESUME ||
1053 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1054 !udev ||
1055 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001056 /*
1057 * USB3 protocol ports will automatically transition
1058 * to Enabled state when detect an USB3.0 device attach.
1059 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001060 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001061 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001062 clear_port_feature(hdev, port1,
1063 USB_PORT_FEAT_ENABLE);
1064 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001065 } else {
1066 /* Pretend that power was lost for USB3 devs */
1067 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001068 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001069 }
1070
Alan Stern948fea32008-04-28 11:07:07 -04001071 /* Clear status-change flags; we'll debounce later */
1072 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1073 need_debounce_delay = true;
1074 clear_port_feature(hub->hdev, port1,
1075 USB_PORT_FEAT_C_CONNECTION);
1076 }
1077 if (portchange & USB_PORT_STAT_C_ENABLE) {
1078 need_debounce_delay = true;
1079 clear_port_feature(hub->hdev, port1,
1080 USB_PORT_FEAT_C_ENABLE);
1081 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001082 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1083 hub_is_superspeed(hub->hdev)) {
1084 need_debounce_delay = true;
1085 clear_port_feature(hub->hdev, port1,
1086 USB_PORT_FEAT_C_BH_PORT_RESET);
1087 }
Alan Stern253e0572009-10-27 15:20:13 -04001088 /* We can forget about a "removed" device when there's a
1089 * physical disconnect or the connect status changes.
1090 */
1091 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1092 (portchange & USB_PORT_STAT_C_CONNECTION))
1093 clear_bit(port1, hub->removed_bits);
1094
Alan Stern6ee0b272008-04-28 11:06:42 -04001095 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1096 /* Tell khubd to disconnect the device or
1097 * check for a new connection
1098 */
1099 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1100 set_bit(port1, hub->change_bits);
1101
1102 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001103 bool port_resumed = (portstatus &
1104 USB_PORT_STAT_LINK_STATE) ==
1105 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001106 /* The power session apparently survived the resume.
1107 * If there was an overcurrent or suspend change
1108 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001109 * take care of it. Look at the port link state
1110 * for USB 3.0 hubs, since they don't have a suspend
1111 * change bit, and they don't set the port link change
1112 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001113 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001114 if (portchange || (hub_is_superspeed(hub->hdev) &&
1115 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001116 set_bit(port1, hub->change_bits);
1117
1118 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001119#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001120 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001121#endif
Alan Stern8808f002008-04-28 11:06:55 -04001122 set_bit(port1, hub->change_bits);
1123
Alan Stern6ee0b272008-04-28 11:06:42 -04001124 } else {
1125 /* The power session is gone; tell khubd */
1126 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1127 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001128 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001129 }
1130
Alan Stern948fea32008-04-28 11:07:07 -04001131 /* If no port-status-change flags were set, we don't need any
1132 * debouncing. If flags were set we can try to debounce the
1133 * ports all at once right now, instead of letting khubd do them
1134 * one at a time later on.
1135 *
1136 * If any port-status changes do occur during this delay, khubd
1137 * will see them later and handle them normally.
1138 */
Alan Stern8520f382008-09-22 14:44:26 -04001139 if (need_debounce_delay) {
1140 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001141
Alan Stern8520f382008-09-22 14:44:26 -04001142 /* Don't do a long sleep inside a workqueue routine */
1143 if (type == HUB_INIT2) {
1144 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1145 schedule_delayed_work(&hub->init_work,
1146 msecs_to_jiffies(delay));
1147 return; /* Continues at init3: below */
1148 } else {
1149 msleep(delay);
1150 }
1151 }
1152 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001153 hub->quiescing = 0;
1154
1155 status = usb_submit_urb(hub->urb, GFP_NOIO);
1156 if (status < 0)
1157 dev_err(hub->intfdev, "activate --> %d\n", status);
1158 if (hub->has_indicators && blinkenlights)
1159 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1160
1161 /* Scan all ports that need attention */
1162 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001163
1164 /* Allow autosuspend if it was suppressed */
1165 if (type <= HUB_INIT3)
1166 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001167}
1168
Alan Stern8520f382008-09-22 14:44:26 -04001169/* Implement the continuations for the delays above */
1170static void hub_init_func2(struct work_struct *ws)
1171{
1172 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1173
1174 hub_activate(hub, HUB_INIT2);
1175}
1176
1177static void hub_init_func3(struct work_struct *ws)
1178{
1179 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1180
1181 hub_activate(hub, HUB_INIT3);
1182}
1183
Alan Stern43303542008-04-28 11:07:31 -04001184enum hub_quiescing_type {
1185 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1186};
1187
1188static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1189{
1190 struct usb_device *hdev = hub->hdev;
1191 int i;
1192
Alan Stern8520f382008-09-22 14:44:26 -04001193 cancel_delayed_work_sync(&hub->init_work);
1194
Alan Stern43303542008-04-28 11:07:31 -04001195 /* khubd and related activity won't re-trigger */
1196 hub->quiescing = 1;
1197
1198 if (type != HUB_SUSPEND) {
1199 /* Disconnect all the children */
1200 for (i = 0; i < hdev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001201 if (hdev->children[i])
1202 usb_disconnect(&hdev->children[i]);
Alan Stern43303542008-04-28 11:07:31 -04001203 }
1204 }
1205
1206 /* Stop khubd and related activity */
1207 usb_kill_urb(hub->urb);
1208 if (hub->has_indicators)
1209 cancel_delayed_work_sync(&hub->leds);
1210 if (hub->tt.hub)
Alan Sterncb88a1b2009-06-29 10:43:32 -04001211 cancel_work_sync(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001212}
1213
Alan Stern3eb14912008-03-03 15:15:43 -05001214/* caller has locked the hub device */
1215static int hub_pre_reset(struct usb_interface *intf)
1216{
1217 struct usb_hub *hub = usb_get_intfdata(intf);
1218
Alan Stern43303542008-04-28 11:07:31 -04001219 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001220 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001221}
1222
1223/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001224static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001225{
Alan Stern7de18d82006-06-01 13:37:24 -04001226 struct usb_hub *hub = usb_get_intfdata(intf);
1227
Alan Sternf2835212008-04-28 11:07:17 -04001228 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001229 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001230}
1231
Lan Tianyufa2a9562012-09-05 13:44:31 +08001232static void usb_port_device_release(struct device *dev)
1233{
1234 struct usb_port *port_dev = to_usb_port(dev);
1235
1236 kfree(port_dev);
1237}
1238
1239static void usb_hub_remove_port_device(struct usb_hub *hub,
1240 int port1)
1241{
1242 device_unregister(&hub->ports[port1 - 1]->dev);
1243}
1244
1245struct device_type usb_port_device_type = {
1246 .name = "usb_port",
1247 .release = usb_port_device_release,
1248};
1249
1250static int usb_hub_create_port_device(struct usb_hub *hub,
1251 int port1)
1252{
1253 struct usb_port *port_dev = NULL;
1254 int retval;
1255
1256 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1257 if (!port_dev) {
1258 retval = -ENOMEM;
1259 goto exit;
1260 }
1261
1262 hub->ports[port1 - 1] = port_dev;
1263 port_dev->dev.parent = hub->intfdev;
1264 port_dev->dev.type = &usb_port_device_type;
1265 dev_set_name(&port_dev->dev, "port%d", port1);
1266
1267 retval = device_register(&port_dev->dev);
1268 if (retval)
1269 goto error_register;
1270 return 0;
1271
1272error_register:
1273 put_device(&port_dev->dev);
1274exit:
1275 return retval;
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278static int hub_configure(struct usb_hub *hub,
1279 struct usb_endpoint_descriptor *endpoint)
1280{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001281 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 struct usb_device *hdev = hub->hdev;
1283 struct device *hub_dev = hub->intfdev;
1284 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001285 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001287 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001288 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Alan Sternd697cdd2009-10-27 15:18:46 -04001290 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 ret = -ENOMEM;
1293 goto fail;
1294 }
1295
1296 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1297 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ret = -ENOMEM;
1299 goto fail;
1300 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001301 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1304 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 ret = -ENOMEM;
1306 goto fail;
1307 }
1308
1309 /* Request the entire hub descriptor.
1310 * hub->descriptor can handle USB_MAXCHILDREN ports,
1311 * but the hub can/will return fewer bytes here.
1312 */
John Youndbe79bb2001-09-17 00:00:00 -07001313 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (ret < 0) {
1315 message = "can't read hub descriptor";
1316 goto fail;
1317 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1318 message = "hub has too many ports!";
1319 ret = -ENODEV;
1320 goto fail;
1321 }
1322
1323 hdev->maxchild = hub->descriptor->bNbrPorts;
1324 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1325 (hdev->maxchild == 1) ? "" : "s");
1326
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001327 hdev->children = kzalloc(hdev->maxchild *
1328 sizeof(struct usb_device *), GFP_KERNEL);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001329 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1330 GFP_KERNEL);
1331 if (!hdev->children || !hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001332 ret = -ENOMEM;
1333 goto fail;
1334 }
1335
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001336 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
John Youndbe79bb2001-09-17 00:00:00 -07001338 /* FIXME for USB 3.0, skip for now */
1339 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1340 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int i;
1342 char portstr [USB_MAXCHILDREN + 1];
1343
1344 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001345 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1347 ? 'F' : 'R';
1348 portstr[hdev->maxchild] = 0;
1349 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1350 } else
1351 dev_dbg(hub_dev, "standalone hub\n");
1352
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001353 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301354 case HUB_CHAR_COMMON_LPSM:
1355 dev_dbg(hub_dev, "ganged power switching\n");
1356 break;
1357 case HUB_CHAR_INDV_PORT_LPSM:
1358 dev_dbg(hub_dev, "individual port power switching\n");
1359 break;
1360 case HUB_CHAR_NO_LPSM:
1361 case HUB_CHAR_LPSM:
1362 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001366 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301367 case HUB_CHAR_COMMON_OCPM:
1368 dev_dbg(hub_dev, "global over-current protection\n");
1369 break;
1370 case HUB_CHAR_INDV_PORT_OCPM:
1371 dev_dbg(hub_dev, "individual port over-current protection\n");
1372 break;
1373 case HUB_CHAR_NO_OCPM:
1374 case HUB_CHAR_OCPM:
1375 dev_dbg(hub_dev, "no over-current protection\n");
1376 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
1379 spin_lock_init (&hub->tt.lock);
1380 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001381 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301383 case USB_HUB_PR_FS:
1384 break;
1385 case USB_HUB_PR_HS_SINGLE_TT:
1386 dev_dbg(hub_dev, "Single TT\n");
1387 hub->tt.hub = hdev;
1388 break;
1389 case USB_HUB_PR_HS_MULTI_TT:
1390 ret = usb_set_interface(hdev, 0, 1);
1391 if (ret == 0) {
1392 dev_dbg(hub_dev, "TT per port\n");
1393 hub->tt.multi = 1;
1394 } else
1395 dev_err(hub_dev, "Using single TT (err %d)\n",
1396 ret);
1397 hub->tt.hub = hdev;
1398 break;
1399 case USB_HUB_PR_SS:
1400 /* USB 3.0 hubs don't have a TT */
1401 break;
1402 default:
1403 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1404 hdev->descriptor.bDeviceProtocol);
1405 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001408 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001409 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001410 case HUB_TTTT_8_BITS:
1411 if (hdev->descriptor.bDeviceProtocol != 0) {
1412 hub->tt.think_time = 666;
1413 dev_dbg(hub_dev, "TT requires at most %d "
1414 "FS bit times (%d ns)\n",
1415 8, hub->tt.think_time);
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001418 case HUB_TTTT_16_BITS:
1419 hub->tt.think_time = 666 * 2;
1420 dev_dbg(hub_dev, "TT requires at most %d "
1421 "FS bit times (%d ns)\n",
1422 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001424 case HUB_TTTT_24_BITS:
1425 hub->tt.think_time = 666 * 3;
1426 dev_dbg(hub_dev, "TT requires at most %d "
1427 "FS bit times (%d ns)\n",
1428 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001430 case HUB_TTTT_32_BITS:
1431 hub->tt.think_time = 666 * 4;
1432 dev_dbg(hub_dev, "TT requires at most %d "
1433 "FS bit times (%d ns)\n",
1434 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436 }
1437
1438 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001439 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 hub->has_indicators = 1;
1441 dev_dbg(hub_dev, "Port indicators are supported\n");
1442 }
1443
1444 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1445 hub->descriptor->bPwrOn2PwrGood * 2);
1446
1447 /* power budgeting mostly matters with bus-powered hubs,
1448 * and battery-powered root hubs (may provide just 8 mA).
1449 */
1450 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001451 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 message = "can't get hub status";
1453 goto fail;
1454 }
Alan Stern7d35b922005-04-25 11:18:32 -04001455 le16_to_cpus(&hubstatus);
1456 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001457 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1458 hub->mA_per_port = 500;
1459 else {
1460 hub->mA_per_port = hdev->bus_mA;
1461 hub->limited_power = 1;
1462 }
Alan Stern7d35b922005-04-25 11:18:32 -04001463 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1465 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001466 hub->limited_power = 1;
1467 if (hdev->maxchild > 0) {
1468 int remaining = hdev->bus_mA -
1469 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Alan Stern55c52712005-11-23 12:03:12 -05001471 if (remaining < hdev->maxchild * 100)
1472 dev_warn(hub_dev,
1473 "insufficient power available "
1474 "to use all downstream ports\n");
1475 hub->mA_per_port = 100; /* 7.2.1.1 */
1476 }
1477 } else { /* Self-powered external hub */
1478 /* FIXME: What about battery-powered external hubs that
1479 * provide less current per port? */
1480 hub->mA_per_port = 500;
1481 }
1482 if (hub->mA_per_port < 500)
1483 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1484 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001486 /* Update the HCD's internal representation of this hub before khubd
1487 * starts getting port status changes for devices under the hub.
1488 */
1489 hcd = bus_to_hcd(hdev->bus);
1490 if (hcd->driver->update_hub_device) {
1491 ret = hcd->driver->update_hub_device(hcd, hdev,
1492 &hub->tt, GFP_KERNEL);
1493 if (ret < 0) {
1494 message = "can't update HCD hub info";
1495 goto fail;
1496 }
1497 }
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1500 if (ret < 0) {
1501 message = "can't get hub status";
1502 goto fail;
1503 }
1504
1505 /* local power status reports aren't always correct */
1506 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1507 dev_dbg(hub_dev, "local power source is %s\n",
1508 (hubstatus & HUB_STATUS_LOCAL_POWER)
1509 ? "lost (inactive)" : "good");
1510
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001511 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 dev_dbg(hub_dev, "%sover-current condition exists\n",
1513 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1514
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001515 /* set up the interrupt endpoint
1516 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1517 * bytes as USB2.0[11.12.3] says because some hubs are known
1518 * to send more data (and thus cause overflow). For root hubs,
1519 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1520 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1522 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1523
1524 if (maxp > sizeof(*hub->buffer))
1525 maxp = sizeof(*hub->buffer);
1526
1527 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1528 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 ret = -ENOMEM;
1530 goto fail;
1531 }
1532
1533 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1534 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 /* maybe cycle the hub leds */
1537 if (hub->has_indicators && blinkenlights)
1538 hub->indicator [0] = INDICATOR_CYCLE;
1539
Lan Tianyufa2a9562012-09-05 13:44:31 +08001540 for (i = 0; i < hdev->maxchild; i++)
1541 if (usb_hub_create_port_device(hub, i + 1) < 0)
1542 dev_err(hub->intfdev,
1543 "couldn't create port%d device.\n", i + 1);
1544
Alan Sternf2835212008-04-28 11:07:17 -04001545 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 return 0;
1547
1548fail:
1549 dev_err (hub_dev, "config failed, %s (err %d)\n",
1550 message, ret);
1551 /* hub_disconnect() frees urb and descriptor */
1552 return ret;
1553}
1554
Alan Sterne8054852007-05-04 11:55:11 -04001555static void hub_release(struct kref *kref)
1556{
1557 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1558
1559 usb_put_intf(to_usb_interface(hub->intfdev));
1560 kfree(hub);
1561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563static unsigned highspeed_hubs;
1564
1565static void hub_disconnect(struct usb_interface *intf)
1566{
Huajun Li88162302012-03-12 21:00:19 +08001567 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001568 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001569 int i;
1570
1571 for (i = 0; i < hdev->maxchild; i++)
1572 usb_hub_remove_port_device(hub, i + 1);
Alan Sterne8054852007-05-04 11:55:11 -04001573
1574 /* Take the hub off the event list and don't let it be added again */
1575 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001576 if (!list_empty(&hub->event_list)) {
1577 list_del_init(&hub->event_list);
1578 usb_autopm_put_interface_no_suspend(intf);
1579 }
Alan Sterne8054852007-05-04 11:55:11 -04001580 hub->disconnected = 1;
1581 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Alan Stern7de18d82006-06-01 13:37:24 -04001583 /* Disconnect all children and quiesce the hub */
1584 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001585 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001586
Alan Stern8b28c752005-08-10 17:04:13 -04001587 usb_set_intfdata (intf, NULL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001588 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Alan Sterne8054852007-05-04 11:55:11 -04001590 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 highspeed_hubs--;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 usb_free_urb(hub->urb);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001594 kfree(hdev->children);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001595 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001596 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001597 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001598 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Alan Sterne8054852007-05-04 11:55:11 -04001600 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1604{
1605 struct usb_host_interface *desc;
1606 struct usb_endpoint_descriptor *endpoint;
1607 struct usb_device *hdev;
1608 struct usb_hub *hub;
1609
1610 desc = intf->cur_altsetting;
1611 hdev = interface_to_usbdev(intf);
1612
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001613 /* Hubs have proper suspend/resume support. */
1614 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001615
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001616 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001617 dev_err(&intf->dev,
1618 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001619 return -E2BIG;
1620 }
1621
David Brownell89ccbdc2006-04-02 10:18:09 -08001622#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1623 if (hdev->parent) {
1624 dev_warn(&intf->dev, "ignoring external hub\n");
1625 return -ENODEV;
1626 }
1627#endif
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 /* Some hubs have a subclass of 1, which AFAICT according to the */
1630 /* specs is not defined, but it works */
1631 if ((desc->desc.bInterfaceSubClass != 0) &&
1632 (desc->desc.bInterfaceSubClass != 1)) {
1633descriptor_error:
1634 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1635 return -EIO;
1636 }
1637
1638 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1639 if (desc->desc.bNumEndpoints != 1)
1640 goto descriptor_error;
1641
1642 endpoint = &desc->endpoint[0].desc;
1643
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001644 /* If it's not an interrupt in endpoint, we'd better punt! */
1645 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 goto descriptor_error;
1647
1648 /* We found a hub */
1649 dev_info (&intf->dev, "USB hub found\n");
1650
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001651 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (!hub) {
1653 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1654 return -ENOMEM;
1655 }
1656
Alan Sterne8054852007-05-04 11:55:11 -04001657 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 INIT_LIST_HEAD(&hub->event_list);
1659 hub->intfdev = &intf->dev;
1660 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001661 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001662 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001663 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001666 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (hdev->speed == USB_SPEED_HIGH)
1669 highspeed_hubs++;
1670
1671 if (hub_configure(hub, endpoint) >= 0)
1672 return 0;
1673
1674 hub_disconnect (intf);
1675 return -ENODEV;
1676}
1677
1678static int
1679hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1680{
1681 struct usb_device *hdev = interface_to_usbdev (intf);
1682
1683 /* assert ifno == 0 (part of hub spec) */
1684 switch (code) {
1685 case USBDEVFS_HUB_PORTINFO: {
1686 struct usbdevfs_hub_portinfo *info = user_data;
1687 int i;
1688
1689 spin_lock_irq(&device_state_lock);
1690 if (hdev->devnum <= 0)
1691 info->nports = 0;
1692 else {
1693 info->nports = hdev->maxchild;
1694 for (i = 0; i < info->nports; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001695 if (hdev->children[i] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 info->port[i] = 0;
1697 else
1698 info->port[i] =
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001699 hdev->children[i]->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701 }
1702 spin_unlock_irq(&device_state_lock);
1703
1704 return info->nports + 1;
1705 }
1706
1707 default:
1708 return -ENOSYS;
1709 }
1710}
1711
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001712/*
1713 * Allow user programs to claim ports on a hub. When a device is attached
1714 * to one of these "claimed" ports, the program will "own" the device.
1715 */
1716static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001717 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001718{
1719 if (hdev->state == USB_STATE_NOTATTACHED)
1720 return -ENODEV;
1721 if (port1 == 0 || port1 > hdev->maxchild)
1722 return -EINVAL;
1723
1724 /* This assumes that devices not managed by the hub driver
1725 * will always have maxchild equal to 0.
1726 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001727 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001728 return 0;
1729}
1730
1731/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001732int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1733 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001734{
1735 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001736 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001737
1738 rc = find_port_owner(hdev, port1, &powner);
1739 if (rc)
1740 return rc;
1741 if (*powner)
1742 return -EBUSY;
1743 *powner = owner;
1744 return rc;
1745}
1746
Lan Tianyu336c5c32012-07-06 14:13:52 +08001747int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1748 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001749{
1750 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001751 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001752
1753 rc = find_port_owner(hdev, port1, &powner);
1754 if (rc)
1755 return rc;
1756 if (*powner != owner)
1757 return -ENOENT;
1758 *powner = NULL;
1759 return rc;
1760}
1761
Lan Tianyu336c5c32012-07-06 14:13:52 +08001762void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001763{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001764 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001765 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001766
Lan Tianyufa2a9562012-09-05 13:44:31 +08001767 for (n = 0; n < hdev->maxchild; n++) {
1768 if (hub->ports[n]->port_owner == owner)
1769 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001770 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001771
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001772}
1773
1774/* The caller must hold udev's lock */
1775bool usb_device_is_owned(struct usb_device *udev)
1776{
1777 struct usb_hub *hub;
1778
1779 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1780 return false;
1781 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001782 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1786{
1787 int i;
1788
1789 for (i = 0; i < udev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001790 if (udev->children[i])
1791 recursively_mark_NOTATTACHED(udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001793 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001794 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 udev->state = USB_STATE_NOTATTACHED;
1796}
1797
1798/**
1799 * usb_set_device_state - change a device's current state (usbcore, hcds)
1800 * @udev: pointer to device whose state should be changed
1801 * @new_state: new state value to be stored
1802 *
1803 * udev->state is _not_ fully protected by the device lock. Although
1804 * most transitions are made only while holding the lock, the state can
1805 * can change to USB_STATE_NOTATTACHED at almost any time. This
1806 * is so that devices can be marked as disconnected as soon as possible,
1807 * without having to wait for any semaphores to be released. As a result,
1808 * all changes to any device's state must be protected by the
1809 * device_state_lock spinlock.
1810 *
1811 * Once a device has been added to the device tree, all changes to its state
1812 * should be made using this routine. The state should _not_ be set directly.
1813 *
1814 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1815 * Otherwise udev->state is set to new_state, and if new_state is
1816 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1817 * to USB_STATE_NOTATTACHED.
1818 */
1819void usb_set_device_state(struct usb_device *udev,
1820 enum usb_device_state new_state)
1821{
1822 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001823 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 spin_lock_irqsave(&device_state_lock, flags);
1826 if (udev->state == USB_STATE_NOTATTACHED)
1827 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001828 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001829
1830 /* root hub wakeup capabilities are managed out-of-band
1831 * and may involve silicon errata ... ignore them here.
1832 */
1833 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001834 if (udev->state == USB_STATE_SUSPENDED
1835 || new_state == USB_STATE_SUSPENDED)
1836 ; /* No change to wakeup settings */
1837 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001838 wakeup = udev->actconfig->desc.bmAttributes
1839 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001840 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001841 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001842 }
Sarah Sharp15123002007-12-21 16:54:15 -08001843 if (udev->state == USB_STATE_SUSPENDED &&
1844 new_state != USB_STATE_SUSPENDED)
1845 udev->active_duration -= jiffies;
1846 else if (new_state == USB_STATE_SUSPENDED &&
1847 udev->state != USB_STATE_SUSPENDED)
1848 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001849 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001850 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 recursively_mark_NOTATTACHED(udev);
1852 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001853 if (wakeup >= 0)
1854 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
David Vrabel6da9c992009-02-18 14:43:47 +00001856EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001858/*
Alan Stern3b29b682011-02-22 09:53:41 -05001859 * Choose a device number.
1860 *
1861 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1862 * USB-2.0 buses they are also used as device addresses, however on
1863 * USB-3.0 buses the address is assigned by the controller hardware
1864 * and it usually is not the same as the device number.
1865 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001866 * WUSB devices are simple: they have no hubs behind, so the mapping
1867 * device <-> virtual port number becomes 1:1. Why? to simplify the
1868 * life of the device connection logic in
1869 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1870 * handshake we need to assign a temporary address in the unauthorized
1871 * space. For simplicity we use the first virtual port number found to
1872 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1873 * and that becomes it's address [X < 128] or its unauthorized address
1874 * [X | 0x80].
1875 *
1876 * We add 1 as an offset to the one-based USB-stack port number
1877 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1878 * 0 is reserved by USB for default address; (b) Linux's USB stack
1879 * uses always #1 for the root hub of the controller. So USB stack's
1880 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001881 *
1882 * Devices connected under xHCI are not as simple. The host controller
1883 * supports virtualization, so the hardware assigns device addresses and
1884 * the HCD must setup data structures before issuing a set address
1885 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001886 */
Alan Stern3b29b682011-02-22 09:53:41 -05001887static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
1889 int devnum;
1890 struct usb_bus *bus = udev->bus;
1891
1892 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001893 if (udev->wusb) {
1894 devnum = udev->portnum + 1;
1895 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1896 } else {
1897 /* Try to allocate the next devnum beginning at
1898 * bus->devnum_next. */
1899 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1900 bus->devnum_next);
1901 if (devnum >= 128)
1902 devnum = find_next_zero_bit(bus->devmap.devicemap,
1903 128, 1);
1904 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (devnum < 128) {
1907 set_bit(devnum, bus->devmap.devicemap);
1908 udev->devnum = devnum;
1909 }
1910}
1911
Alan Stern3b29b682011-02-22 09:53:41 -05001912static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 if (udev->devnum > 0) {
1915 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1916 udev->devnum = -1;
1917 }
1918}
1919
Alan Stern3b29b682011-02-22 09:53:41 -05001920static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001921{
1922 /* The address for a WUSB device is managed by wusbcore. */
1923 if (!udev->wusb)
1924 udev->devnum = devnum;
1925}
1926
Herbert Xuf7410ce2010-01-10 20:15:03 +11001927static void hub_free_dev(struct usb_device *udev)
1928{
1929 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1930
1931 /* Root hubs aren't real devices, so don't free HCD resources */
1932 if (hcd->driver->free_dev && udev->parent)
1933 hcd->driver->free_dev(hcd, udev);
1934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/**
1937 * usb_disconnect - disconnect a device (usbcore-internal)
1938 * @pdev: pointer to device being disconnected
1939 * Context: !in_interrupt ()
1940 *
1941 * Something got disconnected. Get rid of it and all of its children.
1942 *
1943 * If *pdev is a normal device then the parent hub must already be locked.
1944 * If *pdev is a root hub then this routine will acquire the
1945 * usb_bus_list_lock on behalf of the caller.
1946 *
1947 * Only hub drivers (including virtual root hub drivers for host
1948 * controllers) should ever call this.
1949 *
1950 * This call is synchronous, and may not be used in an interrupt context.
1951 */
1952void usb_disconnect(struct usb_device **pdev)
1953{
1954 struct usb_device *udev = *pdev;
1955 int i;
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /* mark the device as inactive, so any further urb submissions for
1958 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001959 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
1961 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001962 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1963 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001965 usb_lock_device(udev);
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001968 for (i = 0; i < udev->maxchild; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001969 if (udev->children[i])
1970 usb_disconnect(&udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972
1973 /* deallocate hcd/hardware state ... nuking all pending urbs and
1974 * cleaning up all state associated with the current configuration
1975 * so that the hardware is now fully quiesced.
1976 */
Alan Stern782da722006-07-01 22:09:35 -04001977 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001979 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Alan Stern3b23dd62008-12-05 14:10:34 -05001981 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001982 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001983
Alan Stern782da722006-07-01 22:09:35 -04001984 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001985 * for de-configuring the device and invoking the remove-device
1986 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001987 */
1988 device_del(&udev->dev);
1989
1990 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 * (or root_hub) pointer.
1992 */
Alan Stern3b29b682011-02-22 09:53:41 -05001993 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /* Avoid races with recursively_mark_NOTATTACHED() */
1996 spin_lock_irq(&device_state_lock);
1997 *pdev = NULL;
1998 spin_unlock_irq(&device_state_lock);
1999
Herbert Xuf7410ce2010-01-10 20:15:03 +11002000 hub_free_dev(udev);
2001
Alan Stern782da722006-07-01 22:09:35 -04002002 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003}
2004
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002005#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006static void show_string(struct usb_device *udev, char *id, char *string)
2007{
2008 if (!string)
2009 return;
2010 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2011}
2012
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002013static void announce_device(struct usb_device *udev)
2014{
2015 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2016 le16_to_cpu(udev->descriptor.idVendor),
2017 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002018 dev_info(&udev->dev,
2019 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002020 udev->descriptor.iManufacturer,
2021 udev->descriptor.iProduct,
2022 udev->descriptor.iSerialNumber);
2023 show_string(udev, "Product", udev->product);
2024 show_string(udev, "Manufacturer", udev->manufacturer);
2025 show_string(udev, "SerialNumber", udev->serial);
2026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002028static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029#endif
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031#ifdef CONFIG_USB_OTG
2032#include "otg_whitelist.h"
2033#endif
2034
Oliver Neukum3ede7602007-01-11 14:35:50 +01002035/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002036 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002037 * @udev: newly addressed device (in ADDRESS state)
2038 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002039 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002040 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002041static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002043 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045#ifdef CONFIG_USB_OTG
2046 /*
2047 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2048 * to wake us after we've powered off VBUS; and HNP, switching roles
2049 * "host" to "peripheral". The OTG descriptor helps figure this out.
2050 */
2051 if (!udev->bus->is_b_host
2052 && udev->config
2053 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002054 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct usb_bus *bus = udev->bus;
2056
2057 /* descriptor may appear anywhere in config */
2058 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2059 le16_to_cpu(udev->config[0].desc.wTotalLength),
2060 USB_DT_OTG, (void **) &desc) == 0) {
2061 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002062 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 dev_info(&udev->dev,
2065 "Dual-Role OTG device on %sHNP port\n",
2066 (port1 == bus->otg_port)
2067 ? "" : "non-");
2068
2069 /* enable HNP before suspend, it's simpler */
2070 if (port1 == bus->otg_port)
2071 bus->b_hnp_enable = 1;
2072 err = usb_control_msg(udev,
2073 usb_sndctrlpipe(udev, 0),
2074 USB_REQ_SET_FEATURE, 0,
2075 bus->b_hnp_enable
2076 ? USB_DEVICE_B_HNP_ENABLE
2077 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2078 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2079 if (err < 0) {
2080 /* OTG MESSAGE: report errors here,
2081 * customize to match your product.
2082 */
2083 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002084 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 err);
2086 bus->b_hnp_enable = 0;
2087 }
2088 }
2089 }
2090 }
2091
2092 if (!is_targeted(udev)) {
2093
2094 /* Maybe it can talk to us, though we can't talk to it.
2095 * (Includes HNP test device.)
2096 */
2097 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002098 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (err < 0)
2100 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2101 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002102 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 goto fail;
2104 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002105fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002107 return err;
2108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002110
2111/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002112 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002113 * @udev: newly addressed device (in ADDRESS state)
2114 *
2115 * This is only called by usb_new_device() and usb_authorize_device()
2116 * and FIXME -- all comments that apply to them apply here wrt to
2117 * environment.
2118 *
2119 * If the device is WUSB and not authorized, we don't attempt to read
2120 * the string descriptors, as they will be errored out by the device
2121 * until it has been authorized.
2122 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002123static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002124{
2125 int err;
2126
2127 if (udev->config == NULL) {
2128 err = usb_get_configuration(udev);
2129 if (err < 0) {
2130 dev_err(&udev->dev, "can't read configurations, error %d\n",
2131 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002132 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002133 }
2134 }
2135 if (udev->wusb == 1 && udev->authorized == 0) {
2136 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2137 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2138 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2139 }
2140 else {
2141 /* read the standard strings and cache them if present */
2142 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2143 udev->manufacturer = usb_cache_string(udev,
2144 udev->descriptor.iManufacturer);
2145 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2146 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002147 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002148 if (err < 0)
2149 return err;
2150
2151 usb_detect_interface_quirks(udev);
2152
2153 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002154}
2155
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002156static void set_usb_port_removable(struct usb_device *udev)
2157{
2158 struct usb_device *hdev = udev->parent;
2159 struct usb_hub *hub;
2160 u8 port = udev->portnum;
2161 u16 wHubCharacteristics;
2162 bool removable = true;
2163
2164 if (!hdev)
2165 return;
2166
2167 hub = hdev_to_hub(udev->parent);
2168
2169 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2170
2171 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2172 return;
2173
2174 if (hub_is_superspeed(hdev)) {
2175 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2176 removable = false;
2177 } else {
2178 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2179 removable = false;
2180 }
2181
2182 if (removable)
2183 udev->removable = USB_DEVICE_REMOVABLE;
2184 else
2185 udev->removable = USB_DEVICE_FIXED;
2186}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002187
2188/**
2189 * usb_new_device - perform initial device setup (usbcore-internal)
2190 * @udev: newly addressed device (in ADDRESS state)
2191 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002192 * This is called with devices which have been detected but not fully
2193 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002194 * for any device configuration. The caller must have locked either
2195 * the parent hub (if udev is a normal device) or else the
2196 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2197 * udev has already been installed, but udev is not yet visible through
2198 * sysfs or other filesystem code.
2199 *
2200 * It will return if the device is configured properly or not. Zero if
2201 * the interface was registered with the driver core; else a negative
2202 * errno value.
2203 *
2204 * This call is synchronous, and may not be used in an interrupt context.
2205 *
2206 * Only the hub driver or root-hub registrar should ever call this.
2207 */
2208int usb_new_device(struct usb_device *udev)
2209{
2210 int err;
2211
Dan Streetman16985402010-01-06 09:56:53 -05002212 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002213 /* Initialize non-root-hub device wakeup to disabled;
2214 * device (un)configuration controls wakeup capable
2215 * sysfs power/wakeup controls wakeup enabled/disabled
2216 */
2217 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002218 }
2219
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002220 /* Tell the runtime-PM framework the device is active */
2221 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002222 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002223 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002224 pm_runtime_enable(&udev->dev);
2225
Alan Sternc08512c2010-11-15 15:57:58 -05002226 /* By default, forbid autosuspend for all devices. It will be
2227 * allowed for hubs during binding.
2228 */
2229 usb_disable_autosuspend(udev);
2230
Alan Stern8d8558d2009-12-08 15:50:41 -05002231 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002232 if (err < 0)
2233 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002234 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2235 udev->devnum, udev->bus->busnum,
2236 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002237 /* export the usbdev device-node for libusb */
2238 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2239 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2240
Alan Stern6cd13202008-11-13 15:08:30 -05002241 /* Tell the world! */
2242 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002243
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002244 if (udev->serial)
2245 add_device_randomness(udev->serial, strlen(udev->serial));
2246 if (udev->product)
2247 add_device_randomness(udev->product, strlen(udev->product));
2248 if (udev->manufacturer)
2249 add_device_randomness(udev->manufacturer,
2250 strlen(udev->manufacturer));
2251
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002252 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002253
2254 /*
2255 * check whether the hub marks this port as non-removable. Do it
2256 * now so that platform-specific data can override it in
2257 * device_add()
2258 */
2259 if (udev->parent)
2260 set_usb_port_removable(udev);
2261
Alan Stern782da722006-07-01 22:09:35 -04002262 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002263 * for configuring the device and invoking the add-device
2264 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002265 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002266 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (err) {
2268 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2269 goto fail;
2270 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002271
Alan Stern3b23dd62008-12-05 14:10:34 -05002272 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002273 usb_mark_last_busy(udev);
2274 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002275 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277fail:
2278 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002279 pm_runtime_disable(&udev->dev);
2280 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002281 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002284
2285/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002286 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2287 * @usb_dev: USB device
2288 *
2289 * Move the USB device to a very basic state where interfaces are disabled
2290 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002291 *
2292 * We share a lock (that we have) with device_del(), so we need to
2293 * defer its call.
2294 */
2295int usb_deauthorize_device(struct usb_device *usb_dev)
2296{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002297 usb_lock_device(usb_dev);
2298 if (usb_dev->authorized == 0)
2299 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002300
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002301 usb_dev->authorized = 0;
2302 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002303
2304 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002305 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002306 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002307 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002308 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002309 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002310
2311 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002312 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002313
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002314out_unauthorized:
2315 usb_unlock_device(usb_dev);
2316 return 0;
2317}
2318
2319
2320int usb_authorize_device(struct usb_device *usb_dev)
2321{
2322 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002323
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002324 usb_lock_device(usb_dev);
2325 if (usb_dev->authorized == 1)
2326 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002327
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002328 result = usb_autoresume_device(usb_dev);
2329 if (result < 0) {
2330 dev_err(&usb_dev->dev,
2331 "can't autoresume for authorization: %d\n", result);
2332 goto error_autoresume;
2333 }
2334 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2335 if (result < 0) {
2336 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2337 "authorization: %d\n", result);
2338 goto error_device_descriptor;
2339 }
Alan Sternda307122009-12-08 15:54:44 -05002340
2341 kfree(usb_dev->product);
2342 usb_dev->product = NULL;
2343 kfree(usb_dev->manufacturer);
2344 usb_dev->manufacturer = NULL;
2345 kfree(usb_dev->serial);
2346 usb_dev->serial = NULL;
2347
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002348 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002349 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002350 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002351 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002352 /* Choose and set the configuration. This registers the interfaces
2353 * with the driver core and lets interface drivers bind to them.
2354 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002355 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002356 if (c >= 0) {
2357 result = usb_set_configuration(usb_dev, c);
2358 if (result) {
2359 dev_err(&usb_dev->dev,
2360 "can't set config #%d, error %d\n", c, result);
2361 /* This need not be fatal. The user can try to
2362 * set other configurations. */
2363 }
2364 }
2365 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002366
Alan Stern8d8558d2009-12-08 15:50:41 -05002367error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002368error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002369 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002370error_autoresume:
2371out_authorized:
2372 usb_unlock_device(usb_dev); // complements locktree
2373 return result;
2374}
2375
2376
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002377/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2378static unsigned hub_is_wusb(struct usb_hub *hub)
2379{
2380 struct usb_hcd *hcd;
2381 if (hub->hdev->parent != NULL) /* not a root hub? */
2382 return 0;
2383 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2384 return hcd->wireless;
2385}
2386
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388#define PORT_RESET_TRIES 5
2389#define SET_ADDRESS_TRIES 2
2390#define GET_DESCRIPTOR_TRIES 2
2391#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302392#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
2394#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2395#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002396#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397#define HUB_LONG_RESET_TIME 200
2398#define HUB_RESET_TIMEOUT 500
2399
Sarah Sharp10d674a2011-09-14 14:24:52 -07002400static int hub_port_reset(struct usb_hub *hub, int port1,
2401 struct usb_device *udev, unsigned int delay, bool warm);
2402
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002403/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2404 * Port worm reset is required to recover
2405 */
2406static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002407{
2408 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002409 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2410 USB_SS_PORT_LS_SS_INACTIVE) ||
2411 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2412 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002413}
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002416 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
2418 int delay_time, ret;
2419 u16 portstatus;
2420 u16 portchange;
2421
2422 for (delay_time = 0;
2423 delay_time < HUB_RESET_TIMEOUT;
2424 delay_time += delay) {
2425 /* wait to give the device a chance to reset */
2426 msleep(delay);
2427
2428 /* read and decode port status */
2429 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2430 if (ret < 0)
2431 return ret;
2432
Andiry Xu75d7cf72011-09-13 16:41:11 -07002433 /*
2434 * Some buggy devices require a warm reset to be issued even
2435 * when the port appears not to be connected.
2436 */
2437 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002438 /*
2439 * Some buggy devices can cause an NEC host controller
2440 * to transition to the "Error" state after a hot port
2441 * reset. This will show up as the port state in
2442 * "Inactive", and the port may also report a
2443 * disconnect. Forcing a warm port reset seems to make
2444 * the device work.
2445 *
2446 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2447 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002448 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002449 int ret;
2450
2451 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2452 clear_port_feature(hub->hdev, port1,
2453 USB_PORT_FEAT_C_CONNECTION);
2454 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2455 clear_port_feature(hub->hdev, port1,
2456 USB_PORT_FEAT_C_PORT_LINK_STATE);
2457 if (portchange & USB_PORT_STAT_C_RESET)
2458 clear_port_feature(hub->hdev, port1,
2459 USB_PORT_FEAT_C_RESET);
2460 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2461 port1);
2462 ret = hub_port_reset(hub, port1,
2463 udev, HUB_BH_RESET_TIME,
2464 true);
2465 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2466 clear_port_feature(hub->hdev, port1,
2467 USB_PORT_FEAT_C_CONNECTION);
2468 return ret;
2469 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002470 /* Device went away? */
2471 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2472 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Andiry Xu75d7cf72011-09-13 16:41:11 -07002474 /* bomb out completely if the connection bounced */
2475 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2476 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Andiry Xu75d7cf72011-09-13 16:41:11 -07002478 /* if we`ve finished resetting, then break out of
2479 * the loop
2480 */
2481 if (!(portstatus & USB_PORT_STAT_RESET) &&
2482 (portstatus & USB_PORT_STAT_ENABLE)) {
2483 if (hub_is_wusb(hub))
2484 udev->speed = USB_SPEED_WIRELESS;
2485 else if (hub_is_superspeed(hub->hdev))
2486 udev->speed = USB_SPEED_SUPER;
2487 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2488 udev->speed = USB_SPEED_HIGH;
2489 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2490 udev->speed = USB_SPEED_LOW;
2491 else
2492 udev->speed = USB_SPEED_FULL;
2493 return 0;
2494 }
2495 } else {
2496 if (portchange & USB_PORT_STAT_C_BH_RESET)
2497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
2499
2500 /* switch to the long delay after two short delay failures */
2501 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2502 delay = HUB_LONG_RESET_TIME;
2503
2504 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002505 "port %d not %sreset yet, waiting %dms\n",
2506 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
2508
2509 return -EBUSY;
2510}
2511
Andiry Xu75d7cf72011-09-13 16:41:11 -07002512static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2513 struct usb_device *udev, int *status, bool warm)
2514{
2515 switch (*status) {
2516 case 0:
2517 if (!warm) {
2518 struct usb_hcd *hcd;
2519 /* TRSTRCY = 10 ms; plus some extra */
2520 msleep(10 + 40);
2521 update_devnum(udev, 0);
2522 hcd = bus_to_hcd(udev->bus);
2523 if (hcd->driver->reset_device) {
2524 *status = hcd->driver->reset_device(hcd, udev);
2525 if (*status < 0) {
2526 dev_err(&udev->dev, "Cannot reset "
2527 "HCD device state\n");
2528 break;
2529 }
2530 }
2531 }
2532 /* FALL THROUGH */
2533 case -ENOTCONN:
2534 case -ENODEV:
2535 clear_port_feature(hub->hdev,
2536 port1, USB_PORT_FEAT_C_RESET);
2537 /* FIXME need disconnect() for NOTATTACHED device */
2538 if (warm) {
2539 clear_port_feature(hub->hdev, port1,
2540 USB_PORT_FEAT_C_BH_PORT_RESET);
2541 clear_port_feature(hub->hdev, port1,
2542 USB_PORT_FEAT_C_PORT_LINK_STATE);
2543 } else {
2544 usb_set_device_state(udev, *status
2545 ? USB_STATE_NOTATTACHED
2546 : USB_STATE_DEFAULT);
2547 }
2548 break;
2549 }
2550}
2551
2552/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002554 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 int i, status;
2557
Andiry Xu75d7cf72011-09-13 16:41:11 -07002558 if (!warm) {
2559 /* Block EHCI CF initialization during the port reset.
2560 * Some companion controllers don't like it when they mix.
2561 */
2562 down_read(&ehci_cf_port_reset_rwsem);
2563 } else {
2564 if (!hub_is_superspeed(hub->hdev)) {
2565 dev_err(hub->intfdev, "only USB3 hub support "
2566 "warm reset\n");
2567 return -EINVAL;
2568 }
2569 }
Alan Stern32fe0192007-10-10 16:27:07 -04002570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 /* Reset the port */
2572 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002573 status = set_port_feature(hub->hdev, port1, (warm ?
2574 USB_PORT_FEAT_BH_PORT_RESET :
2575 USB_PORT_FEAT_RESET));
2576 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002578 "cannot %sreset port %d (err = %d)\n",
2579 warm ? "warm " : "", port1, status);
2580 } else {
2581 status = hub_port_wait_reset(hub, port1, udev, delay,
2582 warm);
David Brownelldd165252005-08-31 10:45:25 -07002583 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 dev_dbg(hub->intfdev,
2585 "port_wait_reset: err = %d\n",
2586 status);
2587 }
2588
2589 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002590 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2591 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002592 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594
2595 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002596 "port %d not enabled, trying %sreset again...\n",
2597 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 delay = HUB_LONG_RESET_TIME;
2599 }
2600
2601 dev_err (hub->intfdev,
2602 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2603 port1);
2604
Andiry Xu75d7cf72011-09-13 16:41:11 -07002605done:
2606 if (!warm)
2607 up_read(&ehci_cf_port_reset_rwsem);
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 return status;
2610}
2611
Andiry Xu0ed9a572011-04-27 18:07:43 +08002612/* Check if a port is power on */
2613static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2614{
2615 int ret = 0;
2616
2617 if (hub_is_superspeed(hub->hdev)) {
2618 if (portstatus & USB_SS_PORT_STAT_POWER)
2619 ret = 1;
2620 } else {
2621 if (portstatus & USB_PORT_STAT_POWER)
2622 ret = 1;
2623 }
2624
2625 return ret;
2626}
2627
Alan Sternd388dab2006-07-01 22:14:24 -04002628#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Andiry Xu0ed9a572011-04-27 18:07:43 +08002630/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2631static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2632{
2633 int ret = 0;
2634
2635 if (hub_is_superspeed(hub->hdev)) {
2636 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2637 == USB_SS_PORT_LS_U3)
2638 ret = 1;
2639 } else {
2640 if (portstatus & USB_PORT_STAT_SUSPEND)
2641 ret = 1;
2642 }
2643
2644 return ret;
2645}
Alan Sternb01b03f2008-04-28 11:06:11 -04002646
2647/* Determine whether the device on a port is ready for a normal resume,
2648 * is ready for a reset-resume, or should be disconnected.
2649 */
2650static int check_port_resume_type(struct usb_device *udev,
2651 struct usb_hub *hub, int port1,
2652 int status, unsigned portchange, unsigned portstatus)
2653{
2654 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002655 if (status || port_is_suspended(hub, portstatus) ||
2656 !port_is_power_on(hub, portstatus) ||
2657 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002658 if (status >= 0)
2659 status = -ENODEV;
2660 }
2661
Alan Stern86c57ed2008-06-30 11:14:43 -04002662 /* Can't do a normal resume if the port isn't enabled,
2663 * so try a reset-resume instead.
2664 */
2665 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2666 if (udev->persist_enabled)
2667 udev->reset_resume = 1;
2668 else
2669 status = -ENODEV;
2670 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002671
2672 if (status) {
2673 dev_dbg(hub->intfdev,
2674 "port %d status %04x.%04x after resume, %d\n",
2675 port1, portchange, portstatus, status);
2676 } else if (udev->reset_resume) {
2677
2678 /* Late port handoff can set status-change bits */
2679 if (portchange & USB_PORT_STAT_C_CONNECTION)
2680 clear_port_feature(hub->hdev, port1,
2681 USB_PORT_FEAT_C_CONNECTION);
2682 if (portchange & USB_PORT_STAT_C_ENABLE)
2683 clear_port_feature(hub->hdev, port1,
2684 USB_PORT_FEAT_C_ENABLE);
2685 }
2686
2687 return status;
2688}
2689
Sarah Sharpf74631e2012-06-25 12:08:08 -07002690int usb_disable_ltm(struct usb_device *udev)
2691{
2692 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2693
2694 /* Check if the roothub and device supports LTM. */
2695 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2696 !usb_device_supports_ltm(udev))
2697 return 0;
2698
2699 /* Clear Feature LTM Enable can only be sent if the device is
2700 * configured.
2701 */
2702 if (!udev->actconfig)
2703 return 0;
2704
2705 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2706 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2707 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2708 USB_CTRL_SET_TIMEOUT);
2709}
2710EXPORT_SYMBOL_GPL(usb_disable_ltm);
2711
2712void usb_enable_ltm(struct usb_device *udev)
2713{
2714 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2715
2716 /* Check if the roothub and device supports LTM. */
2717 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2718 !usb_device_supports_ltm(udev))
2719 return;
2720
2721 /* Set Feature LTM Enable can only be sent if the device is
2722 * configured.
2723 */
2724 if (!udev->actconfig)
2725 return;
2726
2727 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2728 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2729 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2730 USB_CTRL_SET_TIMEOUT);
2731}
2732EXPORT_SYMBOL_GPL(usb_enable_ltm);
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734#ifdef CONFIG_USB_SUSPEND
2735
2736/*
Alan Stern140d8f62006-07-01 22:07:21 -04002737 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002738 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002739 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 *
2741 * Suspends a USB device that isn't in active use, conserving power.
2742 * Devices may wake out of a suspend, if anything important happens,
2743 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002744 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 * to disconnect devices while they are suspended.
2746 *
David Brownell390a8c32005-09-13 19:57:27 -07002747 * This only affects the USB hardware for a device; its interfaces
2748 * (and, for hubs, child devices) must already have been suspended.
2749 *
Alan Stern624d6c02007-05-30 15:35:16 -04002750 * Selective port suspend reduces power; most suspended devices draw
2751 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2752 * All devices below the suspended port are also suspended.
2753 *
2754 * Devices leave suspend state when the host wakes them up. Some devices
2755 * also support "remote wakeup", where the device can activate the USB
2756 * tree above them to deliver data, such as a keypress or packet. In
2757 * some cases, this wakes the USB host.
2758 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 * Suspending OTG devices may trigger HNP, if that's been enabled
2760 * between a pair of dual-role devices. That will change roles, such
2761 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2762 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002763 * Devices on USB hub ports have only one "suspend" state, corresponding
2764 * to ACPI D2, "may cause the device to lose some context".
2765 * State transitions include:
2766 *
2767 * - suspend, resume ... when the VBUS power link stays live
2768 * - suspend, disconnect ... VBUS lost
2769 *
2770 * Once VBUS drop breaks the circuit, the port it's using has to go through
2771 * normal re-enumeration procedures, starting with enabling VBUS power.
2772 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2773 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2774 * timer, no SRP, no requests through sysfs.
2775 *
2776 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2777 * the root hub for their bus goes into global suspend ... so we don't
2778 * (falsely) update the device power state to say it suspended.
2779 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 * Returns 0 on success, else negative errno.
2781 */
Alan Stern65bfd292008-11-25 16:39:18 -05002782int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
Alan Stern624d6c02007-05-30 15:35:16 -04002784 struct usb_hub *hub = hdev_to_hub(udev->parent);
2785 int port1 = udev->portnum;
2786 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002787
Alan Stern624d6c02007-05-30 15:35:16 -04002788 /* enable remote wakeup when appropriate; this lets the device
2789 * wake up the upstream hub (including maybe the root hub).
2790 *
2791 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2792 * we don't explicitly enable it here.
2793 */
2794 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002795 if (!hub_is_superspeed(hub->hdev)) {
2796 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2797 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2798 USB_DEVICE_REMOTE_WAKEUP, 0,
2799 NULL, 0,
2800 USB_CTRL_SET_TIMEOUT);
2801 } else {
2802 /* Assume there's only one function on the USB 3.0
2803 * device and enable remote wake for the first
2804 * interface. FIXME if the interface association
2805 * descriptor shows there's more than one function.
2806 */
2807 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2808 USB_REQ_SET_FEATURE,
2809 USB_RECIP_INTERFACE,
2810 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002811 USB_INTRF_FUNC_SUSPEND_RW |
2812 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002813 NULL, 0,
2814 USB_CTRL_SET_TIMEOUT);
2815 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002816 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002817 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2818 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002819 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002820 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002821 return status;
2822 }
Alan Stern624d6c02007-05-30 15:35:16 -04002823 }
2824
Andiry Xu65580b432011-09-23 14:19:52 -07002825 /* disable USB2 hardware LPM */
2826 if (udev->usb2_hw_lpm_enabled == 1)
2827 usb_set_usb2_hardware_lpm(udev, 0);
2828
Sarah Sharpf74631e2012-06-25 12:08:08 -07002829 if (usb_disable_ltm(udev)) {
2830 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2831 __func__);
2832 return -ENOMEM;
2833 }
Sarah Sharp83060952012-05-02 14:25:52 -07002834 if (usb_unlocked_disable_lpm(udev)) {
2835 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2836 __func__);
2837 return -ENOMEM;
2838 }
2839
Alan Stern624d6c02007-05-30 15:35:16 -04002840 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002841 if (hub_is_superspeed(hub->hdev))
2842 status = set_port_feature(hub->hdev,
2843 port1 | (USB_SS_PORT_LS_U3 << 3),
2844 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002845 else
2846 status = set_port_feature(hub->hdev, port1,
2847 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002848 if (status) {
2849 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2850 port1, status);
2851 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002852 if (udev->do_remote_wakeup)
2853 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002854 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2855 USB_DEVICE_REMOTE_WAKEUP, 0,
2856 NULL, 0,
2857 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002858
Andiry Xuc3e751e2012-05-05 00:50:10 +08002859 /* Try to enable USB2 hardware LPM again */
2860 if (udev->usb2_hw_lpm_capable == 1)
2861 usb_set_usb2_hardware_lpm(udev, 1);
2862
Sarah Sharpf74631e2012-06-25 12:08:08 -07002863 /* Try to enable USB3 LTM and LPM again */
2864 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002865 usb_unlocked_enable_lpm(udev);
2866
Alan Sterncbb33002011-06-15 16:29:16 -04002867 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002868 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002869 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002870 } else {
2871 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002872 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2873 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2874 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002875 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2876 msleep(10);
2877 }
Alan Sternc08512c2010-11-15 15:57:58 -05002878 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002879 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880}
David Brownellf3f32532005-09-22 22:37:29 -07002881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882/*
David Brownell390a8c32005-09-13 19:57:27 -07002883 * If the USB "suspend" state is in use (rather than "global suspend"),
2884 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002885 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 * hardware resume signaling is finished, either because of selective
2887 * resume (by host) or remote wakeup (by device) ... now see what changed
2888 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002889 *
2890 * If @udev->reset_resume is set then the device is reset before the
2891 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 */
Alan Stern140d8f62006-07-01 22:07:21 -04002893static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Alan Stern54515fe2007-05-30 15:38:58 -04002895 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 u16 devstatus;
2897
2898 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002899 dev_dbg(&udev->dev, "%s\n",
2900 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
2902 /* usb ch9 identifies four variants of SUSPENDED, based on what
2903 * state the device resumes to. Linux currently won't see the
2904 * first two on the host side; they'd be inside hub_port_init()
2905 * during many timeouts, but khubd can't suspend until later.
2906 */
2907 usb_set_device_state(udev, udev->actconfig
2908 ? USB_STATE_CONFIGURED
2909 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Alan Stern54515fe2007-05-30 15:38:58 -04002911 /* 10.5.4.5 says not to reset a suspended port if the attached
2912 * device is enabled for remote wakeup. Hence the reset
2913 * operation is carried out here, after the port has been
2914 * resumed.
2915 */
2916 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002917 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002918 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 /* 10.5.4.5 says be sure devices in the tree are still there.
2921 * For now let's assume the device didn't go crazy on resume,
2922 * and device drivers will know about any resume quirks.
2923 */
Alan Stern54515fe2007-05-30 15:38:58 -04002924 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002925 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002926 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2927 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002928 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002929
2930 /* If a normal resume failed, try doing a reset-resume */
2931 if (status && !udev->reset_resume && udev->persist_enabled) {
2932 dev_dbg(&udev->dev, "retry with reset-resume\n");
2933 udev->reset_resume = 1;
2934 goto retry_reset_resume;
2935 }
Alan Stern54515fe2007-05-30 15:38:58 -04002936 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002937
Alan Stern624d6c02007-05-30 15:35:16 -04002938 if (status) {
2939 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2940 status);
2941 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002943 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 status = usb_control_msg(udev,
2945 usb_sndctrlpipe(udev, 0),
2946 USB_REQ_CLEAR_FEATURE,
2947 USB_RECIP_DEVICE,
2948 USB_DEVICE_REMOTE_WAKEUP, 0,
2949 NULL, 0,
2950 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002951 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002952 dev_dbg(&udev->dev,
2953 "disable remote wakeup, status %d\n",
2954 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
2958 return status;
2959}
2960
Alan Stern624d6c02007-05-30 15:35:16 -04002961/*
2962 * usb_port_resume - re-activate a suspended usb device's upstream port
2963 * @udev: device to re-activate, not a root hub
2964 * Context: must be able to sleep; device not locked; pm locks held
2965 *
2966 * This will re-activate the suspended device, increasing power usage
2967 * while letting drivers communicate again with its endpoints.
2968 * USB resume explicitly guarantees that the power session between
2969 * the host and the device is the same as it was when the device
2970 * suspended.
2971 *
Alan Sternfeccc302008-03-03 15:15:59 -05002972 * If @udev->reset_resume is set then this routine won't check that the
2973 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002974 * reset @udev. The end result is that a broken power session can be
2975 * recovered and @udev will appear to persist across a loss of VBUS power.
2976 *
2977 * For example, if a host controller doesn't maintain VBUS suspend current
2978 * during a system sleep or is reset when the system wakes up, all the USB
2979 * power sessions below it will be broken. This is especially troublesome
2980 * for mass-storage devices containing mounted filesystems, since the
2981 * device will appear to have disconnected and all the memory mappings
2982 * to it will be lost. Using the USB_PERSIST facility, the device can be
2983 * made to appear as if it had not disconnected.
2984 *
Ming Lei742120c2008-06-18 22:00:29 +08002985 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002986 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002987 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2988 * quite possible for a device to remain unaltered but its media to be
2989 * changed. If the user replaces a flash memory card while the system is
2990 * asleep, he will have only himself to blame when the filesystem on the
2991 * new card is corrupted and the system crashes.
2992 *
Alan Stern624d6c02007-05-30 15:35:16 -04002993 * Returns 0 on success, else negative errno.
2994 */
Alan Stern65bfd292008-11-25 16:39:18 -05002995int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Alan Stern624d6c02007-05-30 15:35:16 -04002997 struct usb_hub *hub = hdev_to_hub(udev->parent);
2998 int port1 = udev->portnum;
2999 int status;
3000 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003001
3002 /* Skip the initial Clear-Suspend step for a remote wakeup */
3003 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003004 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003005 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3008
Alan Sternd5cbad42006-08-11 16:52:39 -04003009 set_bit(port1, hub->busy_bits);
3010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003012 if (hub_is_superspeed(hub->hdev))
3013 status = set_port_feature(hub->hdev,
3014 port1 | (USB_SS_PORT_LS_U0 << 3),
3015 USB_PORT_FEAT_LINK_STATE);
3016 else
3017 status = clear_port_feature(hub->hdev,
3018 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003020 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3021 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003024 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003025 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 msleep(25);
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3029 * stop resume signaling. Then finish the resume
3030 * sequence.
3031 */
Alan Sternd25450c2006-11-20 11:14:30 -05003032 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003033
Alan Sternb01b03f2008-04-28 11:06:11 -04003034 /* TRSMRCY = 10 msec */
3035 msleep(10);
3036 }
Alan Stern54515fe2007-05-30 15:38:58 -04003037
Alan Sternb01b03f2008-04-28 11:06:11 -04003038 SuspendCleared:
3039 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08003040 if (hub_is_superspeed(hub->hdev)) {
3041 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3042 clear_port_feature(hub->hdev, port1,
3043 USB_PORT_FEAT_C_PORT_LINK_STATE);
3044 } else {
3045 if (portchange & USB_PORT_STAT_C_SUSPEND)
3046 clear_port_feature(hub->hdev, port1,
3047 USB_PORT_FEAT_C_SUSPEND);
3048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Alan Sternd5cbad42006-08-11 16:52:39 -04003051 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003052
Alan Sternb01b03f2008-04-28 11:06:11 -04003053 status = check_port_resume_type(udev,
3054 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003055 if (status == 0)
3056 status = finish_port_resume(udev);
3057 if (status < 0) {
3058 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3059 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003060 } else {
3061 /* Try to enable USB2 hardware LPM */
3062 if (udev->usb2_hw_lpm_capable == 1)
3063 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003064
Sarah Sharpf74631e2012-06-25 12:08:08 -07003065 /* Try to enable USB3 LTM and LPM */
3066 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003067 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003068 }
Andiry Xu65580b432011-09-23 14:19:52 -07003069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return status;
3071}
3072
Alan Stern8808f002008-04-28 11:06:55 -04003073/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003074int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
3076 int status = 0;
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003079 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003080 status = usb_autoresume_device(udev);
3081 if (status == 0) {
3082 /* Let the drivers do their thing, then... */
3083 usb_autosuspend_device(udev);
3084 }
Alan Sternd25450c2006-11-20 11:14:30 -05003085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return status;
3087}
3088
Alan Sternd388dab2006-07-01 22:14:24 -04003089#else /* CONFIG_USB_SUSPEND */
3090
3091/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3092
Alan Stern65bfd292008-11-25 16:39:18 -05003093int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003094{
3095 return 0;
3096}
3097
Alan Sternb01b03f2008-04-28 11:06:11 -04003098/* However we may need to do a reset-resume */
3099
Alan Stern65bfd292008-11-25 16:39:18 -05003100int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003101{
Alan Sternb01b03f2008-04-28 11:06:11 -04003102 struct usb_hub *hub = hdev_to_hub(udev->parent);
3103 int port1 = udev->portnum;
3104 int status;
3105 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003106
Alan Sternb01b03f2008-04-28 11:06:11 -04003107 status = hub_port_status(hub, port1, &portstatus, &portchange);
3108 status = check_port_resume_type(udev,
3109 hub, port1, status, portchange, portstatus);
3110
3111 if (status) {
3112 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3113 hub_port_logical_disconnect(hub, port1);
3114 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003115 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003116 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003117 }
3118 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003119}
3120
Alan Sternd388dab2006-07-01 22:14:24 -04003121#endif
3122
David Brownelldb690872005-09-13 19:56:33 -07003123static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
3125 struct usb_hub *hub = usb_get_intfdata (intf);
3126 struct usb_device *hdev = hub->hdev;
3127 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003128 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
Alan Sterncbb33002011-06-15 16:29:16 -04003130 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3132 struct usb_device *udev;
3133
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003134 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04003135 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003136 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003137 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003138 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003141 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3142 /* Enable hub to send remote wakeup for all ports. */
3143 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3144 status = set_port_feature(hdev,
3145 port1 |
3146 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3147 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3148 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3149 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3150 }
3151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Harvey Harrison441b62c2008-03-03 16:08:34 -08003153 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003154
David Brownellc9f89fa2005-09-13 19:57:04 -07003155 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003156 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
3160static int hub_resume(struct usb_interface *intf)
3161{
Alan Stern5e6effa2008-03-03 15:15:51 -05003162 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Alan Stern5e6effa2008-03-03 15:15:51 -05003164 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003165 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return 0;
3167}
3168
Alan Sternb41a60e2007-05-30 15:39:33 -04003169static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003170{
Alan Sternb41a60e2007-05-30 15:39:33 -04003171 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003172
Alan Stern5e6effa2008-03-03 15:15:51 -05003173 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003174 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003175 return 0;
3176}
3177
Alan Stern54515fe2007-05-30 15:38:58 -04003178/**
3179 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3180 * @rhdev: struct usb_device for the root hub
3181 *
3182 * The USB host controller driver calls this function when its root hub
3183 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003184 * has been reset. The routine marks @rhdev as having lost power.
3185 * When the hub driver is resumed it will take notice and carry out
3186 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3187 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003188 */
3189void usb_root_hub_lost_power(struct usb_device *rhdev)
3190{
3191 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3192 rhdev->reset_resume = 1;
3193}
3194EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3195
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003196static const char * const usb3_lpm_names[] = {
3197 "U0",
3198 "U1",
3199 "U2",
3200 "U3",
3201};
3202
3203/*
3204 * Send a Set SEL control transfer to the device, prior to enabling
3205 * device-initiated U1 or U2. This lets the device know the exit latencies from
3206 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3207 * packet from the host.
3208 *
3209 * This function will fail if the SEL or PEL values for udev are greater than
3210 * the maximum allowed values for the link state to be enabled.
3211 */
3212static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3213{
3214 struct usb_set_sel_req *sel_values;
3215 unsigned long long u1_sel;
3216 unsigned long long u1_pel;
3217 unsigned long long u2_sel;
3218 unsigned long long u2_pel;
3219 int ret;
3220
3221 /* Convert SEL and PEL stored in ns to us */
3222 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3223 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3224 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3225 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3226
3227 /*
3228 * Make sure that the calculated SEL and PEL values for the link
3229 * state we're enabling aren't bigger than the max SEL/PEL
3230 * value that will fit in the SET SEL control transfer.
3231 * Otherwise the device would get an incorrect idea of the exit
3232 * latency for the link state, and could start a device-initiated
3233 * U1/U2 when the exit latencies are too high.
3234 */
3235 if ((state == USB3_LPM_U1 &&
3236 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3237 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3238 (state == USB3_LPM_U2 &&
3239 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3240 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3241 dev_dbg(&udev->dev, "Device-initiated %s disabled due "
3242 "to long SEL %llu ms or PEL %llu ms\n",
3243 usb3_lpm_names[state], u1_sel, u1_pel);
3244 return -EINVAL;
3245 }
3246
3247 /*
3248 * If we're enabling device-initiated LPM for one link state,
3249 * but the other link state has a too high SEL or PEL value,
3250 * just set those values to the max in the Set SEL request.
3251 */
3252 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3253 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3254
3255 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3256 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3257
3258 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3259 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3260
3261 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3262 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3263
3264 /*
3265 * usb_enable_lpm() can be called as part of a failed device reset,
3266 * which may be initiated by an error path of a mass storage driver.
3267 * Therefore, use GFP_NOIO.
3268 */
3269 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3270 if (!sel_values)
3271 return -ENOMEM;
3272
3273 sel_values->u1_sel = u1_sel;
3274 sel_values->u1_pel = u1_pel;
3275 sel_values->u2_sel = cpu_to_le16(u2_sel);
3276 sel_values->u2_pel = cpu_to_le16(u2_pel);
3277
3278 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3279 USB_REQ_SET_SEL,
3280 USB_RECIP_DEVICE,
3281 0, 0,
3282 sel_values, sizeof *(sel_values),
3283 USB_CTRL_SET_TIMEOUT);
3284 kfree(sel_values);
3285 return ret;
3286}
3287
3288/*
3289 * Enable or disable device-initiated U1 or U2 transitions.
3290 */
3291static int usb_set_device_initiated_lpm(struct usb_device *udev,
3292 enum usb3_link_state state, bool enable)
3293{
3294 int ret;
3295 int feature;
3296
3297 switch (state) {
3298 case USB3_LPM_U1:
3299 feature = USB_DEVICE_U1_ENABLE;
3300 break;
3301 case USB3_LPM_U2:
3302 feature = USB_DEVICE_U2_ENABLE;
3303 break;
3304 default:
3305 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3306 __func__, enable ? "enable" : "disable");
3307 return -EINVAL;
3308 }
3309
3310 if (udev->state != USB_STATE_CONFIGURED) {
3311 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3312 "for unconfigured device.\n",
3313 __func__, enable ? "enable" : "disable",
3314 usb3_lpm_names[state]);
3315 return 0;
3316 }
3317
3318 if (enable) {
3319 /*
3320 * First, let the device know about the exit latencies
3321 * associated with the link state we're about to enable.
3322 */
3323 ret = usb_req_set_sel(udev, state);
3324 if (ret < 0) {
3325 dev_warn(&udev->dev, "Set SEL for device-initiated "
3326 "%s failed.\n", usb3_lpm_names[state]);
3327 return -EBUSY;
3328 }
3329 /*
3330 * Now send the control transfer to enable device-initiated LPM
3331 * for either U1 or U2.
3332 */
3333 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3334 USB_REQ_SET_FEATURE,
3335 USB_RECIP_DEVICE,
3336 feature,
3337 0, NULL, 0,
3338 USB_CTRL_SET_TIMEOUT);
3339 } else {
3340 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3341 USB_REQ_CLEAR_FEATURE,
3342 USB_RECIP_DEVICE,
3343 feature,
3344 0, NULL, 0,
3345 USB_CTRL_SET_TIMEOUT);
3346 }
3347 if (ret < 0) {
3348 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3349 enable ? "Enable" : "Disable",
3350 usb3_lpm_names[state]);
3351 return -EBUSY;
3352 }
3353 return 0;
3354}
3355
3356static int usb_set_lpm_timeout(struct usb_device *udev,
3357 enum usb3_link_state state, int timeout)
3358{
3359 int ret;
3360 int feature;
3361
3362 switch (state) {
3363 case USB3_LPM_U1:
3364 feature = USB_PORT_FEAT_U1_TIMEOUT;
3365 break;
3366 case USB3_LPM_U2:
3367 feature = USB_PORT_FEAT_U2_TIMEOUT;
3368 break;
3369 default:
3370 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3371 __func__);
3372 return -EINVAL;
3373 }
3374
3375 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3376 timeout != USB3_LPM_DEVICE_INITIATED) {
3377 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3378 "which is a reserved value.\n",
3379 usb3_lpm_names[state], timeout);
3380 return -EINVAL;
3381 }
3382
3383 ret = set_port_feature(udev->parent,
3384 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3385 feature);
3386 if (ret < 0) {
3387 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3388 "error code %i\n", usb3_lpm_names[state],
3389 timeout, ret);
3390 return -EBUSY;
3391 }
3392 if (state == USB3_LPM_U1)
3393 udev->u1_params.timeout = timeout;
3394 else
3395 udev->u2_params.timeout = timeout;
3396 return 0;
3397}
3398
3399/*
3400 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3401 * U1/U2 entry.
3402 *
3403 * We will attempt to enable U1 or U2, but there are no guarantees that the
3404 * control transfers to set the hub timeout or enable device-initiated U1/U2
3405 * will be successful.
3406 *
3407 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3408 * driver know about it. If that call fails, it should be harmless, and just
3409 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3410 */
3411static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3412 enum usb3_link_state state)
3413{
3414 int timeout;
3415
3416 /* We allow the host controller to set the U1/U2 timeout internally
3417 * first, so that it can change its schedule to account for the
3418 * additional latency to send data to a device in a lower power
3419 * link state.
3420 */
3421 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3422
3423 /* xHCI host controller doesn't want to enable this LPM state. */
3424 if (timeout == 0)
3425 return;
3426
3427 if (timeout < 0) {
3428 dev_warn(&udev->dev, "Could not enable %s link state, "
3429 "xHCI error %i.\n", usb3_lpm_names[state],
3430 timeout);
3431 return;
3432 }
3433
3434 if (usb_set_lpm_timeout(udev, state, timeout))
3435 /* If we can't set the parent hub U1/U2 timeout,
3436 * device-initiated LPM won't be allowed either, so let the xHCI
3437 * host know that this link state won't be enabled.
3438 */
3439 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3440
3441 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3442 else if (udev->actconfig)
3443 usb_set_device_initiated_lpm(udev, state, true);
3444
3445}
3446
3447/*
3448 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3449 * U1/U2 entry.
3450 *
3451 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3452 * If zero is returned, the parent will not allow the link to go into U1/U2.
3453 *
3454 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3455 * it won't have an effect on the bus link state because the parent hub will
3456 * still disallow device-initiated U1/U2 entry.
3457 *
3458 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3459 * possible. The result will be slightly more bus bandwidth will be taken up
3460 * (to account for U1/U2 exit latency), but it should be harmless.
3461 */
3462static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3463 enum usb3_link_state state)
3464{
3465 int feature;
3466
3467 switch (state) {
3468 case USB3_LPM_U1:
3469 feature = USB_PORT_FEAT_U1_TIMEOUT;
3470 break;
3471 case USB3_LPM_U2:
3472 feature = USB_PORT_FEAT_U2_TIMEOUT;
3473 break;
3474 default:
3475 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3476 __func__);
3477 return -EINVAL;
3478 }
3479
3480 if (usb_set_lpm_timeout(udev, state, 0))
3481 return -EBUSY;
3482
3483 usb_set_device_initiated_lpm(udev, state, false);
3484
3485 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3486 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3487 "bus schedule bandwidth may be impacted.\n",
3488 usb3_lpm_names[state]);
3489 return 0;
3490}
3491
3492/*
3493 * Disable hub-initiated and device-initiated U1 and U2 entry.
3494 * Caller must own the bandwidth_mutex.
3495 *
3496 * This will call usb_enable_lpm() on failure, which will decrement
3497 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3498 */
3499int usb_disable_lpm(struct usb_device *udev)
3500{
3501 struct usb_hcd *hcd;
3502
3503 if (!udev || !udev->parent ||
3504 udev->speed != USB_SPEED_SUPER ||
3505 !udev->lpm_capable)
3506 return 0;
3507
3508 hcd = bus_to_hcd(udev->bus);
3509 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3510 return 0;
3511
3512 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003513 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003514 return 0;
3515
3516 /* If LPM is enabled, attempt to disable it. */
3517 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3518 goto enable_lpm;
3519 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3520 goto enable_lpm;
3521
3522 return 0;
3523
3524enable_lpm:
3525 usb_enable_lpm(udev);
3526 return -EBUSY;
3527}
3528EXPORT_SYMBOL_GPL(usb_disable_lpm);
3529
3530/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3531int usb_unlocked_disable_lpm(struct usb_device *udev)
3532{
3533 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3534 int ret;
3535
3536 if (!hcd)
3537 return -EINVAL;
3538
3539 mutex_lock(hcd->bandwidth_mutex);
3540 ret = usb_disable_lpm(udev);
3541 mutex_unlock(hcd->bandwidth_mutex);
3542
3543 return ret;
3544}
3545EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3546
3547/*
3548 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3549 * xHCI host policy may prevent U1 or U2 from being enabled.
3550 *
3551 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3552 * until the lpm_disable_count drops to zero. Caller must own the
3553 * bandwidth_mutex.
3554 */
3555void usb_enable_lpm(struct usb_device *udev)
3556{
3557 struct usb_hcd *hcd;
3558
3559 if (!udev || !udev->parent ||
3560 udev->speed != USB_SPEED_SUPER ||
3561 !udev->lpm_capable)
3562 return;
3563
3564 udev->lpm_disable_count--;
3565 hcd = bus_to_hcd(udev->bus);
3566 /* Double check that we can both enable and disable LPM.
3567 * Device must be configured to accept set feature U1/U2 timeout.
3568 */
3569 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3570 !hcd->driver->disable_usb3_lpm_timeout)
3571 return;
3572
3573 if (udev->lpm_disable_count > 0)
3574 return;
3575
3576 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3577 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3578}
3579EXPORT_SYMBOL_GPL(usb_enable_lpm);
3580
3581/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3582void usb_unlocked_enable_lpm(struct usb_device *udev)
3583{
3584 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3585
3586 if (!hcd)
3587 return;
3588
3589 mutex_lock(hcd->bandwidth_mutex);
3590 usb_enable_lpm(udev);
3591 mutex_unlock(hcd->bandwidth_mutex);
3592}
3593EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3594
3595
Alan Sternd388dab2006-07-01 22:14:24 -04003596#else /* CONFIG_PM */
3597
Alan Sternf07600c2007-05-30 15:38:16 -04003598#define hub_suspend NULL
3599#define hub_resume NULL
3600#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003601
3602int usb_disable_lpm(struct usb_device *udev)
3603{
3604 return 0;
3605}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003606EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003607
3608void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003609EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003610
3611int usb_unlocked_disable_lpm(struct usb_device *udev)
3612{
3613 return 0;
3614}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003615EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003616
3617void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003618EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003619
3620int usb_disable_ltm(struct usb_device *udev)
3621{
3622 return 0;
3623}
3624EXPORT_SYMBOL_GPL(usb_disable_ltm);
3625
3626void usb_enable_ltm(struct usb_device *udev) { }
3627EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003628#endif
3629
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
3631/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3632 *
3633 * Between connect detection and reset signaling there must be a delay
3634 * of 100ms at least for debounce and power-settling. The corresponding
3635 * timer shall restart whenever the downstream port detects a disconnect.
3636 *
3637 * Apparently there are some bluetooth and irda-dongles and a number of
3638 * low-speed devices for which this debounce period may last over a second.
3639 * Not covered by the spec - but easy to deal with.
3640 *
3641 * This implementation uses a 1500ms total debounce timeout; if the
3642 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3643 * every 25ms for transient disconnects. When the port status has been
3644 * unchanged for 100ms it returns the port status.
3645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646static int hub_port_debounce(struct usb_hub *hub, int port1)
3647{
3648 int ret;
3649 int total_time, stable_time = 0;
3650 u16 portchange, portstatus;
3651 unsigned connection = 0xffff;
3652
3653 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3654 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3655 if (ret < 0)
3656 return ret;
3657
3658 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3659 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3660 stable_time += HUB_DEBOUNCE_STEP;
3661 if (stable_time >= HUB_DEBOUNCE_STABLE)
3662 break;
3663 } else {
3664 stable_time = 0;
3665 connection = portstatus & USB_PORT_STAT_CONNECTION;
3666 }
3667
3668 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3669 clear_port_feature(hub->hdev, port1,
3670 USB_PORT_FEAT_C_CONNECTION);
3671 }
3672
3673 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3674 break;
3675 msleep(HUB_DEBOUNCE_STEP);
3676 }
3677
3678 dev_dbg (hub->intfdev,
3679 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3680 port1, total_time, stable_time, portstatus);
3681
3682 if (stable_time < HUB_DEBOUNCE_STABLE)
3683 return -ETIMEDOUT;
3684 return portstatus;
3685}
3686
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003687void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688{
Alan Sternddeac4e2009-01-15 17:03:33 -05003689 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3690 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003691 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003693EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3696#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3697
Alan Stern4326ed02007-07-30 17:08:43 -04003698static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003701 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Sarah Sharpc6515272009-04-27 19:57:26 -07003703 /*
3704 * The host controller will choose the device address,
3705 * instead of the core having chosen it earlier
3706 */
3707 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return -EINVAL;
3709 if (udev->state == USB_STATE_ADDRESS)
3710 return 0;
3711 if (udev->state != USB_STATE_DEFAULT)
3712 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003713 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003714 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003715 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003716 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3717 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3718 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003720 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003721 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003723 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 }
3725 return retval;
3726}
3727
3728/* Reset device, (re)assign address, get device descriptor.
3729 * Device connection must be stable, no more debouncing needed.
3730 * Returns device in USB_STATE_ADDRESS, except on error.
3731 *
3732 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003733 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 * newly detected device that is not accessible through any global
3735 * pointers, it's not necessary to lock the device.
3736 */
3737static int
3738hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3739 int retry_counter)
3740{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003741 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003744 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 int i, j, retval;
3746 unsigned delay = HUB_SHORT_RESET_TIME;
3747 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003748 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003749 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
3751 /* root hub ports have a slightly longer reset period
3752 * (from USB 2.0 spec, section 7.1.7.5)
3753 */
3754 if (!hdev->parent) {
3755 delay = HUB_ROOT_RESET_TIME;
3756 if (port1 == hdev->bus->otg_port)
3757 hdev->bus->b_hnp_enable = 0;
3758 }
3759
3760 /* Some low speed devices have problems with the quick delay, so */
3761 /* be a bit pessimistic with those devices. RHbug #23670 */
3762 if (oldspeed == USB_SPEED_LOW)
3763 delay = HUB_LONG_RESET_TIME;
3764
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003765 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Luben Tuikov07194ab2011-02-11 11:33:10 -08003767 /* Reset the device; full speed may morph to high speed */
3768 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003769 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003770 if (retval < 0) /* error or disconnect */
3771 goto fail;
3772 /* success, speed is known */
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 retval = -ENODEV;
3775
3776 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3777 dev_dbg(&udev->dev, "device reset changed speed!\n");
3778 goto fail;
3779 }
3780 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3783 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003784 * For Wireless USB devices, ep0 max packet is always 512 (tho
3785 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 */
3787 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003788 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003789 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003790 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003793 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 break;
3795 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3796 /* to determine the ep0 maxpacket size, try to read
3797 * the device descriptor to get bMaxPacketSize0 and
3798 * then correct our initial guess.
3799 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003800 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 break;
3802 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003803 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 break;
3805 default:
3806 goto fail;
3807 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003808
3809 if (udev->speed == USB_SPEED_WIRELESS)
3810 speed = "variable speed Wireless";
3811 else
3812 speed = usb_speed_string(udev->speed);
3813
Sarah Sharpc6515272009-04-27 19:57:26 -07003814 if (udev->speed != USB_SPEED_SUPER)
3815 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003816 "%s %s USB device number %d using %s\n",
3817 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003818 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
3820 /* Set up TT records, if needed */
3821 if (hdev->tt) {
3822 udev->tt = hdev->tt;
3823 udev->ttport = hdev->ttport;
3824 } else if (udev->speed != USB_SPEED_HIGH
3825 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003826 if (!hub->tt.hub) {
3827 dev_err(&udev->dev, "parent hub has no TT\n");
3828 retval = -EINVAL;
3829 goto fail;
3830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 udev->tt = &hub->tt;
3832 udev->ttport = port1;
3833 }
3834
3835 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3836 * Because device hardware and firmware is sometimes buggy in
3837 * this area, and this is how Linux has done it for ages.
3838 * Change it cautiously.
3839 *
3840 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3841 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3842 * so it may help with some non-standards-compliant devices.
3843 * Otherwise we start with SET_ADDRESS and then try to read the
3844 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3845 * value.
3846 */
3847 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003848 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 struct usb_device_descriptor *buf;
3850 int r = 0;
3851
3852#define GET_DESCRIPTOR_BUFSIZE 64
3853 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3854 if (!buf) {
3855 retval = -ENOMEM;
3856 continue;
3857 }
3858
Alan Sternb89ee192007-05-11 10:19:04 -04003859 /* Retry on all errors; some devices are flakey.
3860 * 255 is for WUSB devices, we actually need to use
3861 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 */
3863 for (j = 0; j < 3; ++j) {
3864 buf->bMaxPacketSize0 = 0;
3865 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3866 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3867 USB_DT_DEVICE << 8, 0,
3868 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003869 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003871 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 if (buf->bDescriptorType ==
3873 USB_DT_DEVICE) {
3874 r = 0;
3875 break;
3876 }
3877 /* FALL THROUGH */
3878 default:
3879 if (r == 0)
3880 r = -EPROTO;
3881 break;
3882 }
3883 if (r == 0)
3884 break;
3885 }
3886 udev->descriptor.bMaxPacketSize0 =
3887 buf->bMaxPacketSize0;
3888 kfree(buf);
3889
Andiry Xu75d7cf72011-09-13 16:41:11 -07003890 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 if (retval < 0) /* error or disconnect */
3892 goto fail;
3893 if (oldspeed != udev->speed) {
3894 dev_dbg(&udev->dev,
3895 "device reset changed speed!\n");
3896 retval = -ENODEV;
3897 goto fail;
3898 }
3899 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003900 dev_err(&udev->dev,
3901 "device descriptor read/64, error %d\n",
3902 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 retval = -EMSGSIZE;
3904 continue;
3905 }
3906#undef GET_DESCRIPTOR_BUFSIZE
3907 }
3908
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003909 /*
3910 * If device is WUSB, we already assigned an
3911 * unauthorized address in the Connect Ack sequence;
3912 * authorization will assign the final address.
3913 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003914 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003915 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3916 retval = hub_set_address(udev, devnum);
3917 if (retval >= 0)
3918 break;
3919 msleep(200);
3920 }
3921 if (retval < 0) {
3922 dev_err(&udev->dev,
3923 "device not accepting address %d, error %d\n",
3924 devnum, retval);
3925 goto fail;
3926 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003927 if (udev->speed == USB_SPEED_SUPER) {
3928 devnum = udev->devnum;
3929 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003930 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003931 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003932 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003933 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003934
3935 /* cope with hardware quirkiness:
3936 * - let SET_ADDRESS settle, some device hardware wants it
3937 * - read ep0 maxpacket even for high and low speed,
3938 */
3939 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003940 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
3944 retval = usb_get_device_descriptor(udev, 8);
3945 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003946 dev_err(&udev->dev,
3947 "device descriptor read/8, error %d\n",
3948 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 if (retval >= 0)
3950 retval = -EMSGSIZE;
3951 } else {
3952 retval = 0;
3953 break;
3954 }
3955 }
3956 if (retval)
3957 goto fail;
3958
Elric Fud8aec3d2012-03-26 21:16:02 +08003959 /*
3960 * Some superspeed devices have finished the link training process
3961 * and attached to a superspeed hub port, but the device descriptor
3962 * got from those devices show they aren't superspeed devices. Warm
3963 * reset the port attached by the devices can fix them.
3964 */
3965 if ((udev->speed == USB_SPEED_SUPER) &&
3966 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3967 dev_err(&udev->dev, "got a wrong device descriptor, "
3968 "warm reset device\n");
3969 hub_port_reset(hub, port1, udev,
3970 HUB_BH_RESET_TIME, true);
3971 retval = -EINVAL;
3972 goto fail;
3973 }
3974
Sarah Sharp6b403b02009-04-27 19:54:10 -07003975 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3976 udev->speed == USB_SPEED_SUPER)
3977 i = 512;
3978 else
3979 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003980 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003981 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003983 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 retval = -EMSGSIZE;
3985 goto fail;
3986 }
Alan Stern56626a72010-10-14 15:25:21 -04003987 if (udev->speed == USB_SPEED_FULL)
3988 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3989 else
3990 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003992 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 }
3994
3995 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3996 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003997 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3998 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 if (retval >= 0)
4000 retval = -ENOMSG;
4001 goto fail;
4002 }
4003
Andiry Xu1ff4df52011-09-23 14:19:48 -07004004 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4005 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004006 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004007 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004008 usb_set_lpm_parameters(udev);
4009 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004010 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004013 /* notify HCD that we have a device connected and addressed */
4014 if (hcd->driver->update_device)
4015 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004017 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004019 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004020 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004021 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 return retval;
4023}
4024
4025static void
4026check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4027{
4028 struct usb_qualifier_descriptor *qual;
4029 int status;
4030
Christoph Lametere94b1762006-12-06 20:33:17 -08004031 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (qual == NULL)
4033 return;
4034
4035 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4036 qual, sizeof *qual);
4037 if (status == sizeof *qual) {
4038 dev_info(&udev->dev, "not running at top speed; "
4039 "connect to a high speed hub\n");
4040 /* hub LEDs are probably harder to miss than syslog */
4041 if (hub->has_indicators) {
4042 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004043 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
4045 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004046 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047}
4048
4049static unsigned
4050hub_power_remaining (struct usb_hub *hub)
4051{
4052 struct usb_device *hdev = hub->hdev;
4053 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004054 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Alan Stern55c52712005-11-23 12:03:12 -05004056 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 return 0;
4058
Alan Stern55c52712005-11-23 12:03:12 -05004059 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4060 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004061 struct usb_device *udev = hdev->children[port1 - 1];
Alan Stern55c52712005-11-23 12:03:12 -05004062 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
4064 if (!udev)
4065 continue;
4066
Alan Stern55c52712005-11-23 12:03:12 -05004067 /* Unconfigured devices may not use more than 100mA,
4068 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004070 delta = udev->actconfig->desc.bMaxPower * 2;
4071 else if (port1 != udev->bus->otg_port || hdev->parent)
4072 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 else
Alan Stern55c52712005-11-23 12:03:12 -05004074 delta = 8;
4075 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004076 dev_warn(&udev->dev,
4077 "%dmA is over %umA budget for port %d!\n",
4078 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 remaining -= delta;
4080 }
4081 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004082 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4083 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 remaining = 0;
4085 }
4086 return remaining;
4087}
4088
4089/* Handle physical or logical connection change events.
4090 * This routine is called when:
4091 * a port connection-change occurs;
4092 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004093 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 * a firmware download)
4095 * caller already locked the hub
4096 */
4097static void hub_port_connect_change(struct usb_hub *hub, int port1,
4098 u16 portstatus, u16 portchange)
4099{
4100 struct usb_device *hdev = hub->hdev;
4101 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304102 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004103 unsigned wHubCharacteristics =
4104 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004105 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004107
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 dev_dbg (hub_dev,
4109 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004110 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
4112 if (hub->has_indicators) {
4113 set_port_led(hub, port1, HUB_LED_AUTO);
4114 hub->indicator[port1-1] = INDICATOR_AUTO;
4115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
4117#ifdef CONFIG_USB_OTG
4118 /* during HNP, don't repeat the debounce */
4119 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004120 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4121 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122#endif
4123
Alan Stern8808f002008-04-28 11:06:55 -04004124 /* Try to resuscitate an existing device */
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004125 udev = hdev->children[port1-1];
Alan Stern8808f002008-04-28 11:06:55 -04004126 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4127 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004128 usb_lock_device(udev);
4129 if (portstatus & USB_PORT_STAT_ENABLE) {
4130 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004131
4132#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004133 } else if (udev->state == USB_STATE_SUSPENDED &&
4134 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004135 /* For a suspended device, treat this as a
4136 * remote wakeup event.
4137 */
Alan Stern0534d462010-01-08 12:56:30 -05004138 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004139#endif
4140
4141 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004142 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004143 }
4144 usb_unlock_device(udev);
4145
4146 if (status == 0) {
4147 clear_bit(port1, hub->change_bits);
4148 return;
4149 }
4150 }
4151
Alan Stern24618b02008-04-28 11:06:28 -04004152 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004153 if (udev)
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004154 usb_disconnect(&hdev->children[port1-1]);
Alan Stern24618b02008-04-28 11:06:28 -04004155 clear_bit(port1, hub->change_bits);
4156
Alan Stern253e0572009-10-27 15:20:13 -04004157 /* We can forget about a "removed" device when there's a physical
4158 * disconnect or the connect status changes.
4159 */
4160 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4161 (portchange & USB_PORT_STAT_C_CONNECTION))
4162 clear_bit(port1, hub->removed_bits);
4163
Alan Stern5257d972008-09-22 14:43:08 -04004164 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4165 USB_PORT_STAT_C_ENABLE)) {
4166 status = hub_port_debounce(hub, port1);
4167 if (status < 0) {
4168 if (printk_ratelimit())
4169 dev_err(hub_dev, "connect-debounce failed, "
4170 "port %d disabled\n", port1);
4171 portstatus &= ~USB_PORT_STAT_CONNECTION;
4172 } else {
4173 portstatus = status;
4174 }
4175 }
4176
Richard Zhao925aa462012-07-11 11:09:28 +08004177 if (hcd->phy && !hdev->parent) {
4178 if (portstatus & USB_PORT_STAT_CONNECTION)
4179 usb_phy_notify_connect(hcd->phy, port1);
4180 else
4181 usb_phy_notify_disconnect(hcd->phy, port1);
4182 }
4183
Alan Stern253e0572009-10-27 15:20:13 -04004184 /* Return now if debouncing failed or nothing is connected or
4185 * the device was "removed".
4186 */
4187 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4188 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
4190 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004191 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004192 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004194
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 if (portstatus & USB_PORT_STAT_ENABLE)
4196 goto done;
4197 return;
4198 }
4199
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
4202 /* reallocate for each attempt, since references
4203 * to the previous one can escape in various ways
4204 */
4205 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4206 if (!udev) {
4207 dev_err (hub_dev,
4208 "couldn't allocate port %d usb_device\n",
4209 port1);
4210 goto done;
4211 }
4212
4213 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004214 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004215 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004216 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004217
Sarah Sharp131dec32010-12-06 21:00:19 -08004218 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4219 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004220 udev->speed = USB_SPEED_SUPER;
4221 else
4222 udev->speed = USB_SPEED_UNKNOWN;
4223
Alan Stern3b29b682011-02-22 09:53:41 -05004224 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004225 if (udev->devnum <= 0) {
4226 status = -ENOTCONN; /* Don't retry */
4227 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004228 }
4229
Sarah Sharpe7b77172009-04-27 19:54:26 -07004230 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 status = hub_port_init(hub, udev, port1, i);
4232 if (status < 0)
4233 goto loop;
4234
Phil Dibowitz93362a82010-07-22 00:05:01 +02004235 usb_detect_quirks(udev);
4236 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4237 msleep(1000);
4238
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 /* consecutive bus-powered hubs aren't reliable; they can
4240 * violate the voltage drop budget. if the new child has
4241 * a "powered" LED, users should notice we didn't enable it
4242 * (without reading syslog), even without per-port LEDs
4243 * on the parent.
4244 */
4245 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004246 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 u16 devstat;
4248
4249 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4250 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004251 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 dev_dbg(&udev->dev, "get status %d ?\n", status);
4253 goto loop_disable;
4254 }
Alan Stern55c52712005-11-23 12:03:12 -05004255 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4257 dev_err(&udev->dev,
4258 "can't connect bus-powered hub "
4259 "to this port\n");
4260 if (hub->has_indicators) {
4261 hub->indicator[port1-1] =
4262 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004263 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 }
4265 status = -ENOTCONN; /* Don't retry */
4266 goto loop_disable;
4267 }
4268 }
4269
4270 /* check for devices running slower than they could */
4271 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4272 && udev->speed == USB_SPEED_FULL
4273 && highspeed_hubs != 0)
4274 check_highspeed (hub, udev, port1);
4275
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004276 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 * udev becomes globally accessible, although presumably
4278 * no one will look at it until hdev is unlocked.
4279 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 status = 0;
4281
4282 /* We mustn't add new devices if the parent hub has
4283 * been disconnected; we would race with the
4284 * recursively_mark_NOTATTACHED() routine.
4285 */
4286 spin_lock_irq(&device_state_lock);
4287 if (hdev->state == USB_STATE_NOTATTACHED)
4288 status = -ENOTCONN;
4289 else
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004290 hdev->children[port1-1] = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 spin_unlock_irq(&device_state_lock);
4292
4293 /* Run it through the hoops (find a driver, etc) */
4294 if (!status) {
4295 status = usb_new_device(udev);
4296 if (status) {
4297 spin_lock_irq(&device_state_lock);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004298 hdev->children[port1-1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 spin_unlock_irq(&device_state_lock);
4300 }
4301 }
4302
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 if (status)
4304 goto loop_disable;
4305
4306 status = hub_power_remaining(hub);
4307 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004308 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
4310 return;
4311
4312loop_disable:
4313 hub_port_disable(hub, port1, 1);
4314loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004315 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004316 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004317 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004319 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 break;
4321 }
Alan Stern3a311552008-05-20 16:58:29 -04004322 if (hub->hdev->parent ||
4323 !hcd->driver->port_handed_over ||
4324 !(hcd->driver->port_handed_over)(hcd, port1))
4325 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4326 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328done:
4329 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304330 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4331 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332}
4333
Sarah Sharp714b07b2012-01-24 13:53:18 -08004334/* Returns 1 if there was a remote wakeup and a connect status change. */
4335static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004336 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004337{
4338 struct usb_device *hdev;
4339 struct usb_device *udev;
4340 int connect_change = 0;
4341 int ret;
4342
4343 hdev = hub->hdev;
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004344 udev = hdev->children[port-1];
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004345 if (!hub_is_superspeed(hdev)) {
4346 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4347 return 0;
4348 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4349 } else {
4350 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004351 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4352 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004353 return 0;
4354 }
4355
Sarah Sharp714b07b2012-01-24 13:53:18 -08004356 if (udev) {
4357 /* TRSMRCY = 10 msec */
4358 msleep(10);
4359
4360 usb_lock_device(udev);
4361 ret = usb_remote_wakeup(udev);
4362 usb_unlock_device(udev);
4363 if (ret < 0)
4364 connect_change = 1;
4365 } else {
4366 ret = -ENODEV;
4367 hub_port_disable(hub, port, 1);
4368 }
4369 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4370 port, ret);
4371 return connect_change;
4372}
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374static void hub_events(void)
4375{
4376 struct list_head *tmp;
4377 struct usb_device *hdev;
4378 struct usb_interface *intf;
4379 struct usb_hub *hub;
4380 struct device *hub_dev;
4381 u16 hubstatus;
4382 u16 hubchange;
4383 u16 portstatus;
4384 u16 portchange;
4385 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004386 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 /*
4389 * We restart the list every time to avoid a deadlock with
4390 * deleting hubs downstream from this one. This should be
4391 * safe since we delete the hub from the event list.
4392 * Not the most efficient, but avoids deadlocks.
4393 */
4394 while (1) {
4395
4396 /* Grab the first entry at the beginning of the list */
4397 spin_lock_irq(&hub_event_lock);
4398 if (list_empty(&hub_event_list)) {
4399 spin_unlock_irq(&hub_event_lock);
4400 break;
4401 }
4402
4403 tmp = hub_event_list.next;
4404 list_del_init(tmp);
4405
4406 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004407 kref_get(&hub->kref);
4408 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
Alan Sterne8054852007-05-04 11:55:11 -04004410 hdev = hub->hdev;
4411 hub_dev = hub->intfdev;
4412 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004413 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 hdev->state, hub->descriptor
4415 ? hub->descriptor->bNbrPorts
4416 : 0,
4417 /* NOTE: expects max 15 ports... */
4418 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004419 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 /* Lock the device, then check to see if we were
4422 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004423 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004424 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004425 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426
4427 /* If the hub has died, clean up after it */
4428 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004429 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004430 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 goto loop;
4432 }
4433
Alan Stern40f122f2006-11-09 14:44:33 -05004434 /* Autoresume */
4435 ret = usb_autopm_get_interface(intf);
4436 if (ret) {
4437 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004439 }
4440
4441 /* If this is an inactive hub, do nothing */
4442 if (hub->quiescing)
4443 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
4445 if (hub->error) {
4446 dev_dbg (hub_dev, "resetting for error %d\n",
4447 hub->error);
4448
Ming Lei742120c2008-06-18 22:00:29 +08004449 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 if (ret) {
4451 dev_dbg (hub_dev,
4452 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004453 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
4455
4456 hub->nerrors = 0;
4457 hub->error = 0;
4458 }
4459
4460 /* deal with port status changes */
4461 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4462 if (test_bit(i, hub->busy_bits))
4463 continue;
4464 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004465 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004467 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 continue;
4469
4470 ret = hub_port_status(hub, i,
4471 &portstatus, &portchange);
4472 if (ret < 0)
4473 continue;
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4476 clear_port_feature(hdev, i,
4477 USB_PORT_FEAT_C_CONNECTION);
4478 connect_change = 1;
4479 }
4480
4481 if (portchange & USB_PORT_STAT_C_ENABLE) {
4482 if (!connect_change)
4483 dev_dbg (hub_dev,
4484 "port %d enable change, "
4485 "status %08x\n",
4486 i, portstatus);
4487 clear_port_feature(hdev, i,
4488 USB_PORT_FEAT_C_ENABLE);
4489
4490 /*
4491 * EM interference sometimes causes badly
4492 * shielded USB devices to be shutdown by
4493 * the hub, this hack enables them again.
4494 * Works at least with mouse driver.
4495 */
4496 if (!(portstatus & USB_PORT_STAT_ENABLE)
4497 && !connect_change
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004498 && hdev->children[i-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 dev_err (hub_dev,
4500 "port %i "
4501 "disabled by hub (EMI?), "
4502 "re-enabling...\n",
4503 i);
4504 connect_change = 1;
4505 }
4506 }
4507
Sarah Sharp72937e12012-01-24 11:46:50 -08004508 if (hub_handle_remote_wakeup(hub, i,
4509 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004510 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004513 u16 status = 0;
4514 u16 unused;
4515
4516 dev_dbg(hub_dev, "over-current change on port "
4517 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 clear_port_feature(hdev, i,
4519 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004520 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004521 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004522 hub_port_status(hub, i, &status, &unused);
4523 if (status & USB_PORT_STAT_OVERCURRENT)
4524 dev_err(hub_dev, "over-current "
4525 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 }
4527
4528 if (portchange & USB_PORT_STAT_C_RESET) {
4529 dev_dbg (hub_dev,
4530 "reset change on port %d\n",
4531 i);
4532 clear_port_feature(hdev, i,
4533 USB_PORT_FEAT_C_RESET);
4534 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004535 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4536 hub_is_superspeed(hub->hdev)) {
4537 dev_dbg(hub_dev,
4538 "warm reset change on port %d\n",
4539 i);
4540 clear_port_feature(hdev, i,
4541 USB_PORT_FEAT_C_BH_PORT_RESET);
4542 }
John Youndbe79bb2001-09-17 00:00:00 -07004543 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4544 clear_port_feature(hub->hdev, i,
4545 USB_PORT_FEAT_C_PORT_LINK_STATE);
4546 }
4547 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4548 dev_warn(hub_dev,
4549 "config error on port %d\n",
4550 i);
4551 clear_port_feature(hub->hdev, i,
4552 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Andiry Xu5e467f62011-04-27 18:07:54 +08004555 /* Warm reset a USB3 protocol port if it's in
4556 * SS.Inactive state.
4557 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004558 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004559 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004560 hub_port_reset(hub, i, NULL,
4561 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004562 }
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (connect_change)
4565 hub_port_connect_change(hub, i,
4566 portstatus, portchange);
4567 } /* end for i */
4568
4569 /* deal with hub status changes */
4570 if (test_and_clear_bit(0, hub->event_bits) == 0)
4571 ; /* do nothing */
4572 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4573 dev_err (hub_dev, "get_hub_status failed\n");
4574 else {
4575 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4576 dev_dbg (hub_dev, "power change\n");
4577 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004578 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4579 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004580 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004581 else
4582 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 }
4584 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004585 u16 status = 0;
4586 u16 unused;
4587
4588 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004590 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004591 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004592 hub_hub_status(hub, &status, &unused);
4593 if (status & HUB_STATUS_OVERCURRENT)
4594 dev_err(hub_dev, "over-current "
4595 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 }
4597 }
4598
Alan Stern8e4ceb32009-12-07 13:01:37 -05004599 loop_autopm:
4600 /* Balance the usb_autopm_get_interface() above */
4601 usb_autopm_put_interface_no_suspend(intf);
4602 loop:
4603 /* Balance the usb_autopm_get_interface_no_resume() in
4604 * kick_khubd() and allow autosuspend.
4605 */
4606 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004607 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004609 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 } /* end while (1) */
4612}
4613
4614static int hub_thread(void *__unused)
4615{
Alan Stern3bb1af52008-03-03 15:15:36 -05004616 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4617 * port handover. Otherwise it might see that a full-speed device
4618 * was gone before the EHCI controller had handed its port over to
4619 * the companion full-speed controller.
4620 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004621 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 do {
4624 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004625 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004626 !list_empty(&hub_event_list) ||
4627 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004628 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004630 pr_debug("%s: khubd exiting\n", usbcore_name);
4631 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632}
4633
Németh Márton1e927d92010-01-10 15:34:53 +01004634static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4636 .bDeviceClass = USB_CLASS_HUB},
4637 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4638 .bInterfaceClass = USB_CLASS_HUB},
4639 { } /* Terminating entry */
4640};
4641
4642MODULE_DEVICE_TABLE (usb, hub_id_table);
4643
4644static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 .name = "hub",
4646 .probe = hub_probe,
4647 .disconnect = hub_disconnect,
4648 .suspend = hub_suspend,
4649 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004650 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004651 .pre_reset = hub_pre_reset,
4652 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004653 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004655 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656};
4657
4658int usb_hub_init(void)
4659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 if (usb_register(&hub_driver) < 0) {
4661 printk(KERN_ERR "%s: can't register hub driver\n",
4662 usbcore_name);
4663 return -1;
4664 }
4665
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004666 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4667 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
4670 /* Fall through if kernel_thread failed */
4671 usb_deregister(&hub_driver);
4672 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4673
4674 return -1;
4675}
4676
4677void usb_hub_cleanup(void)
4678{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004679 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 /*
4682 * Hub resources are freed for us by usb_deregister. It calls
4683 * usb_driver_purge on every device which in turn calls that
4684 * devices disconnect function if it is using this driver.
4685 * The hub_disconnect function takes care of releasing the
4686 * individual hub resources. -greg
4687 */
4688 usb_deregister(&hub_driver);
4689} /* usb_hub_cleanup() */
4690
Alan Sterneb764c42008-03-03 15:16:04 -05004691static int descriptors_changed(struct usb_device *udev,
4692 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
Alan Sterneb764c42008-03-03 15:16:04 -05004694 int changed = 0;
4695 unsigned index;
4696 unsigned serial_len = 0;
4697 unsigned len;
4698 unsigned old_length;
4699 int length;
4700 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Alan Sterneb764c42008-03-03 15:16:04 -05004702 if (memcmp(&udev->descriptor, old_device_descriptor,
4703 sizeof(*old_device_descriptor)) != 0)
4704 return 1;
4705
4706 /* Since the idVendor, idProduct, and bcdDevice values in the
4707 * device descriptor haven't changed, we will assume the
4708 * Manufacturer and Product strings haven't changed either.
4709 * But the SerialNumber string could be different (e.g., a
4710 * different flash card of the same brand).
4711 */
4712 if (udev->serial)
4713 serial_len = strlen(udev->serial) + 1;
4714
4715 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004717 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4718 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
Alan Sterneb764c42008-03-03 15:16:04 -05004720
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004721 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 if (buf == NULL) {
4723 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4724 /* assume the worst */
4725 return 1;
4726 }
4727 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004728 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4730 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004731 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 dev_dbg(&udev->dev, "config index %d, error %d\n",
4733 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004734 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 break;
4736 }
4737 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4738 != 0) {
4739 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004740 index,
4741 ((struct usb_config_descriptor *) buf)->
4742 bConfigurationValue);
4743 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 break;
4745 }
4746 }
Alan Sterneb764c42008-03-03 15:16:04 -05004747
4748 if (!changed && serial_len) {
4749 length = usb_string(udev, udev->descriptor.iSerialNumber,
4750 buf, serial_len);
4751 if (length + 1 != serial_len) {
4752 dev_dbg(&udev->dev, "serial string error %d\n",
4753 length);
4754 changed = 1;
4755 } else if (memcmp(buf, udev->serial, length) != 0) {
4756 dev_dbg(&udev->dev, "serial string changed\n");
4757 changed = 1;
4758 }
4759 }
4760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004762 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763}
4764
4765/**
Ming Lei742120c2008-06-18 22:00:29 +08004766 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4768 *
Alan Stern79efa092006-06-01 13:33:42 -04004769 * WARNING - don't use this routine to reset a composite device
4770 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004771 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 *
4773 * Do a port reset, reassign the device's address, and establish its
4774 * former operating configuration. If the reset fails, or the device's
4775 * descriptors change from their values before the reset, or the original
4776 * configuration and altsettings cannot be restored, a flag will be set
4777 * telling khubd to pretend the device has been disconnected and then
4778 * re-connected. All drivers will be unbound, and the device will be
4779 * re-enumerated and probed all over again.
4780 *
4781 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4782 * flagged for logical disconnection, or some other negative error code
4783 * if the reset wasn't even attempted.
4784 *
4785 * The caller must own the device lock. For example, it's safe to use
4786 * this from a driver probe() routine after downloading new firmware.
4787 * For calls that might not occur during probe(), drivers should lock
4788 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004789 *
4790 * Locking exception: This routine may also be called from within an
4791 * autoresume handler. Such usage won't conflict with other tasks
4792 * holding the device lock because these tasks should always call
4793 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 */
Ming Lei742120c2008-06-18 22:00:29 +08004795static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
4797 struct usb_device *parent_hdev = udev->parent;
4798 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004799 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004801 int i, ret = 0;
4802 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
4804 if (udev->state == USB_STATE_NOTATTACHED ||
4805 udev->state == USB_STATE_SUSPENDED) {
4806 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4807 udev->state);
4808 return -EINVAL;
4809 }
4810
4811 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004812 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004813 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 return -EISDIR;
4815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 parent_hub = hdev_to_hub(parent_hdev);
4817
Sarah Sharpf74631e2012-06-25 12:08:08 -07004818 /* Disable LPM and LTM while we reset the device and reinstall the alt
4819 * settings. Device-initiated LPM settings, and system exit latency
4820 * settings are cleared when the device is reset, so we have to set
4821 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004822 */
4823 ret = usb_unlocked_disable_lpm(udev);
4824 if (ret) {
4825 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4826 goto re_enumerate;
4827 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004828 ret = usb_disable_ltm(udev);
4829 if (ret) {
4830 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4831 __func__);
4832 goto re_enumerate;
4833 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 set_bit(port1, parent_hub->busy_bits);
4836 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4837
4838 /* ep0 maxpacket size may change; let the HCD know about it.
4839 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004840 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004842 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 break;
4844 }
4845 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004846
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 if (ret < 0)
4848 goto re_enumerate;
4849
4850 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004851 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 dev_info(&udev->dev, "device firmware changed\n");
4853 udev->descriptor = descriptor; /* for disconnect() calls */
4854 goto re_enumerate;
4855 }
Alan Stern4fe03872009-02-26 10:21:02 -05004856
4857 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 if (!udev->actconfig)
4859 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004860
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004861 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004862 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4863 if (ret < 0) {
4864 dev_warn(&udev->dev,
4865 "Busted HC? Not enough HCD resources for "
4866 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004867 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004868 goto re_enumerate;
4869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4871 USB_REQ_SET_CONFIGURATION, 0,
4872 udev->actconfig->desc.bConfigurationValue, 0,
4873 NULL, 0, USB_CTRL_SET_TIMEOUT);
4874 if (ret < 0) {
4875 dev_err(&udev->dev,
4876 "can't restore configuration #%d (error=%d)\n",
4877 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004878 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 goto re_enumerate;
4880 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004881 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4883
Alan Stern4fe03872009-02-26 10:21:02 -05004884 /* Put interfaces back into the same altsettings as before.
4885 * Don't bother to send the Set-Interface request for interfaces
4886 * that were already in altsetting 0; besides being unnecessary,
4887 * many devices can't handle it. Instead just reset the host-side
4888 * endpoint state.
4889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004891 struct usb_host_config *config = udev->actconfig;
4892 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 struct usb_interface_descriptor *desc;
4894
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004896 if (desc->bAlternateSetting == 0) {
4897 usb_disable_interface(udev, intf, true);
4898 usb_enable_interface(udev, intf, true);
4899 ret = 0;
4900 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004901 /* Let the bandwidth allocation function know that this
4902 * device has been reset, and it will have to use
4903 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004904 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004905 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004906 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4907 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004908 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 if (ret < 0) {
4911 dev_err(&udev->dev, "failed to restore interface %d "
4912 "altsetting %d (error=%d)\n",
4913 desc->bInterfaceNumber,
4914 desc->bAlternateSetting,
4915 ret);
4916 goto re_enumerate;
4917 }
4918 }
4919
4920done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07004921 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04004922 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004923 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return 0;
4925
4926re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004927 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 hub_port_logical_disconnect(parent_hub, port1);
4929 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930}
4931
4932/**
4933 * usb_reset_device - warn interface drivers and perform a USB port reset
4934 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4935 *
Alan Stern79efa092006-06-01 13:33:42 -04004936 * Warns all drivers bound to registered interfaces (using their pre_reset
4937 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08004938 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04004939 *
Alan Stern79efa092006-06-01 13:33:42 -04004940 * Return value is the same as for usb_reset_and_verify_device().
4941 *
4942 * The caller must own the device lock. For example, it's safe to use
4943 * this from a driver probe() routine after downloading new firmware.
4944 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08004945 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04004946 *
4947 * If an interface is currently being probed or disconnected, we assume
4948 * its driver knows how to handle resets. For all other interfaces,
4949 * if the driver doesn't have pre_reset and post_reset methods then
4950 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004951 */
Ming Lei742120c2008-06-18 22:00:29 +08004952int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004953{
4954 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004955 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004956 struct usb_host_config *config = udev->actconfig;
4957
4958 if (udev->state == USB_STATE_NOTATTACHED ||
4959 udev->state == USB_STATE_SUSPENDED) {
4960 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4961 udev->state);
4962 return -EINVAL;
4963 }
4964
Alan Stern645daaa2006-08-30 15:47:02 -04004965 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004966 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004967
Alan Stern79efa092006-06-01 13:33:42 -04004968 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004969 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004970 struct usb_interface *cintf = config->interface[i];
4971 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004972 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004973
4974 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004975 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004976 if (drv->pre_reset && drv->post_reset)
4977 unbind = (drv->pre_reset)(cintf);
4978 else if (cintf->condition ==
4979 USB_INTERFACE_BOUND)
4980 unbind = 1;
4981 if (unbind)
4982 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004983 }
4984 }
4985 }
4986
Ming Lei742120c2008-06-18 22:00:29 +08004987 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004988
4989 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004990 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004991 struct usb_interface *cintf = config->interface[i];
4992 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004993 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004994
Alan Stern78d9a482008-06-23 16:00:40 -04004995 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004996 drv = to_usb_driver(cintf->dev.driver);
4997 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004998 rebind = (drv->post_reset)(cintf);
4999 else if (cintf->condition ==
5000 USB_INTERFACE_BOUND)
5001 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005002 }
Alan Stern6c640942008-10-21 15:40:03 -04005003 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005004 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005005 }
5006 }
5007
Alan Stern94fcda12006-11-20 11:38:46 -05005008 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005009 return ret;
5010}
Ming Lei742120c2008-06-18 22:00:29 +08005011EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005012
5013
5014/**
5015 * usb_queue_reset_device - Reset a USB device from an atomic context
5016 * @iface: USB interface belonging to the device to reset
5017 *
5018 * This function can be used to reset a USB device from an atomic
5019 * context, where usb_reset_device() won't work (as it blocks).
5020 *
5021 * Doing a reset via this method is functionally equivalent to calling
5022 * usb_reset_device(), except for the fact that it is delayed to a
5023 * workqueue. This means that any drivers bound to other interfaces
5024 * might be unbound, as well as users from usbfs in user space.
5025 *
5026 * Corner cases:
5027 *
5028 * - Scheduling two resets at the same time from two different drivers
5029 * attached to two different interfaces of the same device is
5030 * possible; depending on how the driver attached to each interface
5031 * handles ->pre_reset(), the second reset might happen or not.
5032 *
5033 * - If a driver is unbound and it had a pending reset, the reset will
5034 * be cancelled.
5035 *
5036 * - This function can be called during .probe() or .disconnect()
5037 * times. On return from .disconnect(), any pending resets will be
5038 * cancelled.
5039 *
5040 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5041 * does its own.
5042 *
5043 * NOTE: We don't do any reference count tracking because it is not
5044 * needed. The lifecycle of the work_struct is tied to the
5045 * usb_interface. Before destroying the interface we cancel the
5046 * work_struct, so the fact that work_struct is queued and or
5047 * running means the interface (and thus, the device) exist and
5048 * are referenced.
5049 */
5050void usb_queue_reset_device(struct usb_interface *iface)
5051{
5052 schedule_work(&iface->reset_ws);
5053}
5054EXPORT_SYMBOL_GPL(usb_queue_reset_device);