blob: fbaf3c3dbf0751e6a083d8ce27fd91b0bb795aba [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
Ming Leie6f30de2012-10-24 11:59:24 +080042#define USB_VENDOR_GENESYS_LOGIC 0x05e3
43#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
44
Lan Tianyufa2a9562012-09-05 13:44:31 +080045struct usb_port {
Lan Tianyuff823c72012-09-05 13:44:32 +080046 struct usb_device *child;
Lan Tianyufa2a9562012-09-05 13:44:31 +080047 struct device dev;
48 struct dev_state *port_owner;
Lan Tianyu05f91682012-09-05 13:44:34 +080049 enum usb_port_connect_type connect_type;
Lan Tianyufa2a9562012-09-05 13:44:31 +080050};
51
Alan Stern1bb5f662006-11-06 11:56:13 -050052struct usb_hub {
53 struct device *intfdev; /* the "interface" device */
54 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040055 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050056 struct urb *urb; /* for interrupt polling pipe */
57
58 /* buffer for urb ... with extra space in case of babble */
59 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050060 union {
61 struct usb_hub_status hub;
62 struct usb_port_status port;
63 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050064 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050065
66 int error; /* last reported error */
67 int nerrors; /* track consecutive errors */
68
69 struct list_head event_list; /* hubs w/data or errs ready */
70 unsigned long event_bits[1]; /* status change bitmask */
71 unsigned long change_bits[1]; /* ports with logical connect
72 status change */
73 unsigned long busy_bits[1]; /* ports being reset or
74 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040075 unsigned long removed_bits[1]; /* ports with a "removed"
76 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080077 unsigned long wakeup_bits[1]; /* ports that have signaled
78 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050079#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
80#error event_bits[] is too short!
81#endif
82
83 struct usb_hub_descriptor *descriptor; /* class descriptor */
84 struct usb_tt tt; /* Transaction Translator */
85
86 unsigned mA_per_port; /* current for each child */
87
88 unsigned limited_power:1;
89 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040090 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050091
Ming Leie6f30de2012-10-24 11:59:24 +080092 unsigned quirk_check_port_auto_suspend:1;
93
Alan Stern1bb5f662006-11-06 11:56:13 -050094 unsigned has_indicators:1;
95 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000096 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -040097 struct delayed_work init_work;
Lan Tianyufa2a9562012-09-05 13:44:31 +080098 struct usb_port **ports;
Alan Stern1bb5f662006-11-06 11:56:13 -050099};
100
John Youndbe79bb2001-09-17 00:00:00 -0700101static inline int hub_is_superspeed(struct usb_device *hdev)
102{
Aman Deep7bf01182011-12-08 12:05:22 +0530103 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -0700104}
Alan Stern1bb5f662006-11-06 11:56:13 -0500105
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700106/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -0500107 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
109static DEFINE_SPINLOCK(device_state_lock);
110
111/* khubd's worklist and its lock */
112static DEFINE_SPINLOCK(hub_event_lock);
113static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
114
115/* Wakes up khubd */
116static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
117
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700118static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030121static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122module_param (blinkenlights, bool, S_IRUGO);
123MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
124
125/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200126 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
127 * 10 seconds to send reply for the initial 64-byte descriptor request.
128 */
129/* define initial 64-byte descriptor request timeout in milliseconds */
130static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
131module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800132MODULE_PARM_DESC(initial_descriptor_timeout,
133 "initial 64-byte descriptor request timeout in milliseconds "
134 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200135
136/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * As of 2.6.10 we introduce a new USB device initialization scheme which
138 * closely resembles the way Windows works. Hopefully it will be compatible
139 * with a wider range of devices than the old scheme. However some previously
140 * working devices may start giving rise to "device not accepting address"
141 * errors; if that happens the user can try the old scheme by adjusting the
142 * following module parameters.
143 *
144 * For maximum flexibility there are two boolean parameters to control the
145 * hub driver's behavior. On the first initialization attempt, if the
146 * "old_scheme_first" parameter is set then the old scheme will be used,
147 * otherwise the new scheme is used. If that fails and "use_both_schemes"
148 * is set, then the driver will make another attempt, using the other scheme.
149 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030150static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
152MODULE_PARM_DESC(old_scheme_first,
153 "start with the old device initialization scheme");
154
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030155static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
157MODULE_PARM_DESC(use_both_schemes,
158 "try the other device initialization scheme if the "
159 "first one fails");
160
Alan Stern32fe0192007-10-10 16:27:07 -0400161/* Mutual exclusion for EHCI CF initialization. This interferes with
162 * port reset on some companion controllers.
163 */
164DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
165EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
166
Alan Stern948fea32008-04-28 11:07:07 -0400167#define HUB_DEBOUNCE_TIMEOUT 1500
168#define HUB_DEBOUNCE_STEP 25
169#define HUB_DEBOUNCE_STABLE 100
170
Lan Tianyufa2a9562012-09-05 13:44:31 +0800171#define to_usb_port(_dev) \
172 container_of(_dev, struct usb_port, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Ming Lei742120c2008-06-18 22:00:29 +0800174static int usb_reset_and_verify_device(struct usb_device *udev);
175
Sarah Sharp131dec32010-12-06 21:00:19 -0800176static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Sarah Sharp131dec32010-12-06 21:00:19 -0800178 if (hub_is_superspeed(hub->hdev))
179 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500180 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500182 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return "1.5 Mb/s";
184 else
185 return "12 Mb/s";
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400189static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Lan Tianyuff823c72012-09-05 13:44:32 +0800191 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400192 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return usb_get_intfdata(hdev->actconfig->interface[0]);
194}
195
Sarah Sharpd9b20992012-02-20 08:31:26 -0800196static int usb_device_supports_lpm(struct usb_device *udev)
197{
198 /* USB 2.1 (and greater) devices indicate LPM support through
199 * their USB 2.0 Extended Capabilities BOS descriptor.
200 */
201 if (udev->speed == USB_SPEED_HIGH) {
202 if (udev->bos->ext_cap &&
203 (USB_LPM_SUPPORT &
204 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
205 return 1;
206 return 0;
207 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800208
209 /* All USB 3.0 must support LPM, but we need their max exit latency
210 * information from the SuperSpeed Extended Capabilities BOS descriptor.
211 */
212 if (!udev->bos->ss_cap) {
213 dev_warn(&udev->dev, "No LPM exit latency info found. "
214 "Power management will be impacted.\n");
215 return 0;
216 }
217 if (udev->parent->lpm_capable)
218 return 1;
219
220 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
221 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800222 return 0;
223}
224
Sarah Sharp51e0a012012-02-20 12:02:19 -0800225/*
226 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
227 * either U1 or U2.
228 */
229static void usb_set_lpm_mel(struct usb_device *udev,
230 struct usb3_lpm_parameters *udev_lpm_params,
231 unsigned int udev_exit_latency,
232 struct usb_hub *hub,
233 struct usb3_lpm_parameters *hub_lpm_params,
234 unsigned int hub_exit_latency)
235{
236 unsigned int total_mel;
237 unsigned int device_mel;
238 unsigned int hub_mel;
239
240 /*
241 * Calculate the time it takes to transition all links from the roothub
242 * to the parent hub into U0. The parent hub must then decode the
243 * packet (hub header decode latency) to figure out which port it was
244 * bound for.
245 *
246 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
247 * means 0.1us). Multiply that by 100 to get nanoseconds.
248 */
249 total_mel = hub_lpm_params->mel +
250 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
251
252 /*
253 * How long will it take to transition the downstream hub's port into
254 * U0? The greater of either the hub exit latency or the device exit
255 * latency.
256 *
257 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
258 * Multiply that by 1000 to get nanoseconds.
259 */
260 device_mel = udev_exit_latency * 1000;
261 hub_mel = hub_exit_latency * 1000;
262 if (device_mel > hub_mel)
263 total_mel += device_mel;
264 else
265 total_mel += hub_mel;
266
267 udev_lpm_params->mel = total_mel;
268}
269
270/*
271 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
272 * a transition from either U1 or U2.
273 */
274static void usb_set_lpm_pel(struct usb_device *udev,
275 struct usb3_lpm_parameters *udev_lpm_params,
276 unsigned int udev_exit_latency,
277 struct usb_hub *hub,
278 struct usb3_lpm_parameters *hub_lpm_params,
279 unsigned int hub_exit_latency,
280 unsigned int port_to_port_exit_latency)
281{
282 unsigned int first_link_pel;
283 unsigned int hub_pel;
284
285 /*
286 * First, the device sends an LFPS to transition the link between the
287 * device and the parent hub into U0. The exit latency is the bigger of
288 * the device exit latency or the hub exit latency.
289 */
290 if (udev_exit_latency > hub_exit_latency)
291 first_link_pel = udev_exit_latency * 1000;
292 else
293 first_link_pel = hub_exit_latency * 1000;
294
295 /*
296 * When the hub starts to receive the LFPS, there is a slight delay for
297 * it to figure out that one of the ports is sending an LFPS. Then it
298 * will forward the LFPS to its upstream link. The exit latency is the
299 * delay, plus the PEL that we calculated for this hub.
300 */
301 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
302
303 /*
304 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
305 * is the greater of the two exit latencies.
306 */
307 if (first_link_pel > hub_pel)
308 udev_lpm_params->pel = first_link_pel;
309 else
310 udev_lpm_params->pel = hub_pel;
311}
312
313/*
314 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
315 * when a device initiates a transition to U0, until when it will receive the
316 * first packet from the host controller.
317 *
318 * Section C.1.5.1 describes the four components to this:
319 * - t1: device PEL
320 * - t2: time for the ERDY to make it from the device to the host.
321 * - t3: a host-specific delay to process the ERDY.
322 * - t4: time for the packet to make it from the host to the device.
323 *
324 * t3 is specific to both the xHCI host and the platform the host is integrated
325 * into. The Intel HW folks have said it's negligible, FIXME if a different
326 * vendor says otherwise.
327 */
328static void usb_set_lpm_sel(struct usb_device *udev,
329 struct usb3_lpm_parameters *udev_lpm_params)
330{
331 struct usb_device *parent;
332 unsigned int num_hubs;
333 unsigned int total_sel;
334
335 /* t1 = device PEL */
336 total_sel = udev_lpm_params->pel;
337 /* How many external hubs are in between the device & the root port. */
338 for (parent = udev->parent, num_hubs = 0; parent->parent;
339 parent = parent->parent)
340 num_hubs++;
341 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
342 if (num_hubs > 0)
343 total_sel += 2100 + 250 * (num_hubs - 1);
344
345 /* t4 = 250ns * num_hubs */
346 total_sel += 250 * num_hubs;
347
348 udev_lpm_params->sel = total_sel;
349}
350
351static void usb_set_lpm_parameters(struct usb_device *udev)
352{
353 struct usb_hub *hub;
354 unsigned int port_to_port_delay;
355 unsigned int udev_u1_del;
356 unsigned int udev_u2_del;
357 unsigned int hub_u1_del;
358 unsigned int hub_u2_del;
359
360 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
361 return;
362
363 hub = hdev_to_hub(udev->parent);
364 /* It doesn't take time to transition the roothub into U0, since it
365 * doesn't have an upstream link.
366 */
367 if (!hub)
368 return;
369
370 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
371 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
372 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
373 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
374
375 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
376 hub, &udev->parent->u1_params, hub_u1_del);
377
378 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
379 hub, &udev->parent->u2_params, hub_u2_del);
380
381 /*
382 * Appendix C, section C.2.2.2, says that there is a slight delay from
383 * when the parent hub notices the downstream port is trying to
384 * transition to U0 to when the hub initiates a U0 transition on its
385 * upstream port. The section says the delays are tPort2PortU1EL and
386 * tPort2PortU2EL, but it doesn't define what they are.
387 *
388 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
389 * about the same delays. Use the maximum delay calculations from those
390 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
391 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
392 * assume the device exit latencies they are talking about are the hub
393 * exit latencies.
394 *
395 * What do we do if the U2 exit latency is less than the U1 exit
396 * latency? It's possible, although not likely...
397 */
398 port_to_port_delay = 1;
399
400 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
401 hub, &udev->parent->u1_params, hub_u1_del,
402 port_to_port_delay);
403
404 if (hub_u2_del > hub_u1_del)
405 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
406 else
407 port_to_port_delay = 1 + hub_u1_del;
408
409 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
410 hub, &udev->parent->u2_params, hub_u2_del,
411 port_to_port_delay);
412
413 /* Now that we've got PEL, calculate SEL. */
414 usb_set_lpm_sel(udev, &udev->u1_params);
415 usb_set_lpm_sel(udev, &udev->u2_params);
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700419static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
John Youndbe79bb2001-09-17 00:00:00 -0700421 int i, ret, size;
422 unsigned dtype;
423
424 if (hub_is_superspeed(hdev)) {
425 dtype = USB_DT_SS_HUB;
426 size = USB_DT_SS_HUB_SIZE;
427 } else {
428 dtype = USB_DT_HUB;
429 size = sizeof(struct usb_hub_descriptor);
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 for (i = 0; i < 3; i++) {
433 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
434 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700435 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 USB_CTRL_GET_TIMEOUT);
437 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
438 return ret;
439 }
440 return -EINVAL;
441}
442
443/*
444 * USB 2.0 spec Section 11.24.2.1
445 */
446static int clear_hub_feature(struct usb_device *hdev, int feature)
447{
448 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
449 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
450}
451
452/*
453 * USB 2.0 spec Section 11.24.2.2
454 */
455static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
456{
457 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
458 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
459 NULL, 0, 1000);
460}
461
462/*
463 * USB 2.0 spec Section 11.24.2.13
464 */
465static int set_port_feature(struct usb_device *hdev, int port1, int feature)
466{
467 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
468 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
469 NULL, 0, 1000);
470}
471
472/*
473 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
474 * for info about using port indicators
475 */
476static void set_port_led(
477 struct usb_hub *hub,
478 int port1,
479 int selector
480)
481{
482 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
483 USB_PORT_FEAT_INDICATOR);
484 if (status < 0)
485 dev_dbg (hub->intfdev,
486 "port %d indicator %s status %d\n",
487 port1,
488 ({ char *s; switch (selector) {
489 case HUB_LED_AMBER: s = "amber"; break;
490 case HUB_LED_GREEN: s = "green"; break;
491 case HUB_LED_OFF: s = "off"; break;
492 case HUB_LED_AUTO: s = "auto"; break;
493 default: s = "??"; break;
494 }; s; }),
495 status);
496}
497
498#define LED_CYCLE_PERIOD ((2*HZ)/3)
499
David Howellsc4028952006-11-22 14:57:56 +0000500static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
David Howellsc4028952006-11-22 14:57:56 +0000502 struct usb_hub *hub =
503 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct usb_device *hdev = hub->hdev;
505 unsigned i;
506 unsigned changed = 0;
507 int cursor = -1;
508
509 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
510 return;
511
512 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
513 unsigned selector, mode;
514
515 /* 30%-50% duty cycle */
516
517 switch (hub->indicator[i]) {
518 /* cycle marker */
519 case INDICATOR_CYCLE:
520 cursor = i;
521 selector = HUB_LED_AUTO;
522 mode = INDICATOR_AUTO;
523 break;
524 /* blinking green = sw attention */
525 case INDICATOR_GREEN_BLINK:
526 selector = HUB_LED_GREEN;
527 mode = INDICATOR_GREEN_BLINK_OFF;
528 break;
529 case INDICATOR_GREEN_BLINK_OFF:
530 selector = HUB_LED_OFF;
531 mode = INDICATOR_GREEN_BLINK;
532 break;
533 /* blinking amber = hw attention */
534 case INDICATOR_AMBER_BLINK:
535 selector = HUB_LED_AMBER;
536 mode = INDICATOR_AMBER_BLINK_OFF;
537 break;
538 case INDICATOR_AMBER_BLINK_OFF:
539 selector = HUB_LED_OFF;
540 mode = INDICATOR_AMBER_BLINK;
541 break;
542 /* blink green/amber = reserved */
543 case INDICATOR_ALT_BLINK:
544 selector = HUB_LED_GREEN;
545 mode = INDICATOR_ALT_BLINK_OFF;
546 break;
547 case INDICATOR_ALT_BLINK_OFF:
548 selector = HUB_LED_AMBER;
549 mode = INDICATOR_ALT_BLINK;
550 break;
551 default:
552 continue;
553 }
554 if (selector != HUB_LED_AUTO)
555 changed = 1;
556 set_port_led(hub, i + 1, selector);
557 hub->indicator[i] = mode;
558 }
559 if (!changed && blinkenlights) {
560 cursor++;
561 cursor %= hub->descriptor->bNbrPorts;
562 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
563 hub->indicator[cursor] = INDICATOR_CYCLE;
564 changed++;
565 }
566 if (changed)
567 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
568}
569
570/* use a short timeout for hub/port status fetches */
571#define USB_STS_TIMEOUT 1000
572#define USB_STS_RETRIES 5
573
574/*
575 * USB 2.0 spec Section 11.24.2.6
576 */
577static int get_hub_status(struct usb_device *hdev,
578 struct usb_hub_status *data)
579{
580 int i, status = -ETIMEDOUT;
581
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200582 for (i = 0; i < USB_STS_RETRIES &&
583 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
585 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
586 data, sizeof(*data), USB_STS_TIMEOUT);
587 }
588 return status;
589}
590
591/*
592 * USB 2.0 spec Section 11.24.2.7
593 */
594static int get_port_status(struct usb_device *hdev, int port1,
595 struct usb_port_status *data)
596{
597 int i, status = -ETIMEDOUT;
598
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200599 for (i = 0; i < USB_STS_RETRIES &&
600 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
602 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
603 data, sizeof(*data), USB_STS_TIMEOUT);
604 }
605 return status;
606}
607
Alan Stern3eb14912008-03-03 15:15:43 -0500608static int hub_port_status(struct usb_hub *hub, int port1,
609 u16 *status, u16 *change)
610{
611 int ret;
612
613 mutex_lock(&hub->status_mutex);
614 ret = get_port_status(hub->hdev, port1, &hub->status->port);
615 if (ret < 4) {
616 dev_err(hub->intfdev,
617 "%s failed (err = %d)\n", __func__, ret);
618 if (ret >= 0)
619 ret = -EIO;
620 } else {
621 *status = le16_to_cpu(hub->status->port.wPortStatus);
622 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700623
Alan Stern3eb14912008-03-03 15:15:43 -0500624 ret = 0;
625 }
626 mutex_unlock(&hub->status_mutex);
627 return ret;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static void kick_khubd(struct usb_hub *hub)
631{
632 unsigned long flags;
633
634 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200635 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500637
638 /* Suppress autosuspend until khubd runs */
639 usb_autopm_get_interface_no_resume(
640 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 wake_up(&khubd_wait);
642 }
643 spin_unlock_irqrestore(&hub_event_lock, flags);
644}
645
646void usb_kick_khubd(struct usb_device *hdev)
647{
Alan Stern251180842009-07-22 14:41:18 -0400648 struct usb_hub *hub = hdev_to_hub(hdev);
649
650 if (hub)
651 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800654/*
655 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
656 * Notification, which indicates it had initiated remote wakeup.
657 *
658 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
659 * device initiates resume, so the USB core will not receive notice of the
660 * resume through the normal hub interrupt URB.
661 */
662void usb_wakeup_notification(struct usb_device *hdev,
663 unsigned int portnum)
664{
665 struct usb_hub *hub;
666
667 if (!hdev)
668 return;
669
670 hub = hdev_to_hub(hdev);
671 if (hub) {
672 set_bit(portnum, hub->wakeup_bits);
673 kick_khubd(hub);
674 }
675}
676EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100679static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200681 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400682 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100683 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned long bits;
685
Alan Sterne0152682007-08-24 15:42:52 -0400686 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 case -ENOENT: /* synchronous unlink */
688 case -ECONNRESET: /* async unlink */
689 case -ESHUTDOWN: /* hardware going away */
690 return;
691
692 default: /* presumably an error */
693 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400694 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if ((++hub->nerrors < 10) || hub->error)
696 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400697 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* let khubd handle things */
701 case 0: /* we got data: port status changed */
702 bits = 0;
703 for (i = 0; i < urb->actual_length; ++i)
704 bits |= ((unsigned long) ((*hub->buffer)[i]))
705 << (i*8);
706 hub->event_bits[0] = bits;
707 break;
708 }
709
710 hub->nerrors = 0;
711
712 /* Something happened, let khubd figure it out */
713 kick_khubd(hub);
714
715resubmit:
716 if (hub->quiescing)
717 return;
718
719 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
720 && status != -ENODEV && status != -EPERM)
721 dev_err (hub->intfdev, "resubmit --> %d\n", status);
722}
723
724/* USB 2.0 spec Section 11.24.2.3 */
725static inline int
726hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
727{
Alan Sternc2f65952009-11-18 11:37:15 -0500728 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
730 tt, NULL, 0, 1000);
731}
732
733/*
734 * enumeration blocks khubd for a long time. we use keventd instead, since
735 * long blocking there is the exception, not the rule. accordingly, HCDs
736 * talking to TTs must queue control transfers (not just bulk and iso), so
737 * both can talk to the same hub concurrently.
738 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400739static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
David Howellsc4028952006-11-22 14:57:56 +0000741 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400742 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400744 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 spin_lock_irqsave (&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300747 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400748 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct usb_tt_clear *clear;
750 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400751 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 int status;
753
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300754 if (!hub->quiescing && --limit < 0)
755 break;
756
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400757 next = hub->tt.clear_list.next;
758 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 list_del (&clear->clear_list);
760
761 /* drop lock so HCD can concurrently report other TT errors */
762 spin_unlock_irqrestore (&hub->tt.lock, flags);
763 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (status)
765 dev_err (&hdev->dev,
766 "clear tt %d (%04x) error %d\n",
767 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400768
769 /* Tell the HCD, even if the operation failed */
770 drv = clear->hcd->driver;
771 if (drv->clear_tt_buffer_complete)
772 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
773
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700774 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400775 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 spin_unlock_irqrestore (&hub->tt.lock, flags);
778}
779
780/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400781 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
782 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *
784 * High speed HCDs use this to tell the hub driver that some split control or
785 * bulk transaction failed in a way that requires clearing internal state of
786 * a transaction translator. This is normally detected (and reported) from
787 * interrupt context.
788 *
789 * It may not be possible for that hub to handle additional full (or low)
790 * speed transactions until that state is fully cleared out.
791 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400792int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400794 struct usb_device *udev = urb->dev;
795 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 struct usb_tt *tt = udev->tt;
797 unsigned long flags;
798 struct usb_tt_clear *clear;
799
800 /* we've got to cope with an arbitrary number of pending TT clears,
801 * since each TT has "at least two" buffers that can need it (and
802 * there can be many TTs per hub). even if they're uncommon.
803 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800804 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
806 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400807 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 /* info that CLEAR_TT_BUFFER needs */
811 clear->tt = tt->multi ? udev->ttport : 1;
812 clear->devinfo = usb_pipeendpoint (pipe);
813 clear->devinfo |= udev->devnum << 4;
814 clear->devinfo |= usb_pipecontrol (pipe)
815 ? (USB_ENDPOINT_XFER_CONTROL << 11)
816 : (USB_ENDPOINT_XFER_BULK << 11);
817 if (usb_pipein (pipe))
818 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400819
820 /* info for completion callback */
821 clear->hcd = bus_to_hcd(udev->bus);
822 clear->ep = urb->ep;
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /* tell keventd to clear state for this TT */
825 spin_lock_irqsave (&tt->lock, flags);
826 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400827 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400831EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Alan Stern8520f382008-09-22 14:44:26 -0400833/* If do_delay is false, return the number of milliseconds the caller
834 * needs to delay.
835 */
836static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700839 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400840 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400841 u16 wHubCharacteristics =
842 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Alan Stern4489a572006-04-27 15:54:22 -0400844 /* Enable power on each port. Some hubs have reserved values
845 * of LPSM (> 2) in their descriptors, even though they are
846 * USB 2.0 hubs. Some hubs do not implement port-power switching
847 * but only emulate it. In all cases, the ports won't work
848 * unless we send these messages to the hub.
849 */
850 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400852 else
853 dev_dbg(hub->intfdev, "trying to enable port power on "
854 "non-switchable hub\n");
855 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
Greg Kroah-Hartmana0693bd2012-09-24 13:04:02 -0700856 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
David Brownellb7896962005-08-31 10:41:44 -0700858 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400859 delay = max(pgood_delay, (unsigned) 100);
860 if (do_delay)
861 msleep(delay);
862 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865static int hub_hub_status(struct usb_hub *hub,
866 u16 *status, u16 *change)
867{
868 int ret;
869
Alan Sterndb90e7a2007-02-05 09:56:15 -0500870 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 ret = get_hub_status(hub->hdev, &hub->status->hub);
872 if (ret < 0)
873 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800874 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 else {
876 *status = le16_to_cpu(hub->status->hub.wHubStatus);
877 *change = le16_to_cpu(hub->status->hub.wHubChange);
878 ret = 0;
879 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500880 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return ret;
882}
883
Alan Stern8b28c752005-08-10 17:04:13 -0400884static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
885{
886 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400887 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400888
Lan Tianyuff823c72012-09-05 13:44:32 +0800889 if (hub->ports[port1 - 1]->child && set_state)
890 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400891 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700892 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400893 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400894 if (ret)
895 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400896 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400897 return ret;
898}
899
Alan Stern0458d5b2007-05-04 11:52:20 -0400900/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800901 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400902 * time later khubd will disconnect() any existing usb_device on the port
903 * and will re-enumerate if there actually is a device attached.
904 */
905static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
906{
907 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
908 hub_port_disable(hub, port1, 1);
909
910 /* FIXME let caller ask to power down the port:
911 * - some devices won't enumerate without a VBUS power cycle
912 * - SRP saves power that way
913 * - ... new call, TBD ...
914 * That's easy if this hub can switch power per-port, and
915 * khubd reactivates the port later (timer, SRP, etc).
916 * Powerdown must be optional, because of reset/DFU.
917 */
918
919 set_bit(port1, hub->change_bits);
920 kick_khubd(hub);
921}
922
Alan Stern253e0572009-10-27 15:20:13 -0400923/**
924 * usb_remove_device - disable a device's port on its parent hub
925 * @udev: device to be disabled and removed
926 * Context: @udev locked, must be able to sleep.
927 *
928 * After @udev's port has been disabled, khubd is notified and it will
929 * see that the device has been disconnected. When the device is
930 * physically unplugged and something is plugged in, the events will
931 * be received and processed normally.
932 */
933int usb_remove_device(struct usb_device *udev)
934{
935 struct usb_hub *hub;
936 struct usb_interface *intf;
937
938 if (!udev->parent) /* Can't remove a root hub */
939 return -EINVAL;
940 hub = hdev_to_hub(udev->parent);
941 intf = to_usb_interface(hub->intfdev);
942
943 usb_autopm_get_interface(intf);
944 set_bit(udev->portnum, hub->removed_bits);
945 hub_port_logical_disconnect(hub, udev->portnum);
946 usb_autopm_put_interface(intf);
947 return 0;
948}
949
Alan Stern6ee0b272008-04-28 11:06:42 -0400950enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500951 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400952 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400953};
Alan Stern5e6effa2008-03-03 15:15:51 -0500954
Alan Stern8520f382008-09-22 14:44:26 -0400955static void hub_init_func2(struct work_struct *ws);
956static void hub_init_func3(struct work_struct *ws);
957
Alan Sternf2835212008-04-28 11:07:17 -0400958static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500959{
960 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800961 struct usb_hcd *hcd;
962 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500963 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400964 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400965 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400966 unsigned delay;
967
968 /* Continue a partial initialization */
969 if (type == HUB_INIT2)
970 goto init2;
971 if (type == HUB_INIT3)
972 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500973
Elric Fua45aa3b2012-02-18 13:32:27 +0800974 /* The superspeed hub except for root hub has to use Hub Depth
975 * value as an offset into the route string to locate the bits
976 * it uses to determine the downstream port number. So hub driver
977 * should send a set hub depth request to superspeed hub after
978 * the superspeed hub is set configuration in initialization or
979 * reset procedure.
980 *
981 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400982 * For any other type of activation, turn it on.
983 */
Alan Stern8520f382008-09-22 14:44:26 -0400984 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800985 if (hdev->parent && hub_is_superspeed(hdev)) {
986 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
987 HUB_SET_DEPTH, USB_RT_HUB,
988 hdev->level - 1, 0, NULL, 0,
989 USB_CTRL_SET_TIMEOUT);
990 if (ret < 0)
991 dev_err(hub->intfdev,
992 "set hub depth failed\n");
993 }
Alan Stern8520f382008-09-22 14:44:26 -0400994
995 /* Speed up system boot by using a delayed_work for the
996 * hub's initial power-up delays. This is pretty awkward
997 * and the implementation looks like a home-brewed sort of
998 * setjmp/longjmp, but it saves at least 100 ms for each
999 * root hub (assuming usbcore is compiled into the kernel
1000 * rather than as a module). It adds up.
1001 *
1002 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1003 * because for those activation types the ports have to be
1004 * operational when we return. In theory this could be done
1005 * for HUB_POST_RESET, but it's easier not to.
1006 */
1007 if (type == HUB_INIT) {
1008 delay = hub_power_on(hub, false);
1009 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1010 schedule_delayed_work(&hub->init_work,
1011 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001012
1013 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001014 usb_autopm_get_interface_no_resume(
1015 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001016 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001017 } else if (type == HUB_RESET_RESUME) {
1018 /* The internal host controller state for the hub device
1019 * may be gone after a host power loss on system resume.
1020 * Update the device's info so the HW knows it's a hub.
1021 */
1022 hcd = bus_to_hcd(hdev->bus);
1023 if (hcd->driver->update_hub_device) {
1024 ret = hcd->driver->update_hub_device(hcd, hdev,
1025 &hub->tt, GFP_NOIO);
1026 if (ret < 0) {
1027 dev_err(hub->intfdev, "Host not "
1028 "accepting hub info "
1029 "update.\n");
1030 dev_err(hub->intfdev, "LS/FS devices "
1031 "and hubs may not work "
1032 "under this hub\n.");
1033 }
1034 }
1035 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001036 } else {
1037 hub_power_on(hub, true);
1038 }
1039 }
1040 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001041
Alan Stern6ee0b272008-04-28 11:06:42 -04001042 /* Check each port and set hub->change_bits to let khubd know
1043 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001044 */
1045 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001046 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001047 u16 portstatus, portchange;
1048
Alan Stern6ee0b272008-04-28 11:06:42 -04001049 portstatus = portchange = 0;
1050 status = hub_port_status(hub, port1, &portstatus, &portchange);
1051 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1052 dev_dbg(hub->intfdev,
1053 "port %d: status %04x change %04x\n",
1054 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001055
Alan Stern6ee0b272008-04-28 11:06:42 -04001056 /* After anything other than HUB_RESUME (i.e., initialization
1057 * or any sort of reset), every port should be disabled.
1058 * Unconnected ports should likewise be disabled (paranoia),
1059 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001060 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001061 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1062 type != HUB_RESUME ||
1063 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1064 !udev ||
1065 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001066 /*
1067 * USB3 protocol ports will automatically transition
1068 * to Enabled state when detect an USB3.0 device attach.
1069 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001070 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001071 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001072 clear_port_feature(hdev, port1,
1073 USB_PORT_FEAT_ENABLE);
1074 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001075 } else {
1076 /* Pretend that power was lost for USB3 devs */
1077 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001078 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001079 }
1080
Alan Stern948fea32008-04-28 11:07:07 -04001081 /* Clear status-change flags; we'll debounce later */
1082 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1083 need_debounce_delay = true;
1084 clear_port_feature(hub->hdev, port1,
1085 USB_PORT_FEAT_C_CONNECTION);
1086 }
1087 if (portchange & USB_PORT_STAT_C_ENABLE) {
1088 need_debounce_delay = true;
1089 clear_port_feature(hub->hdev, port1,
1090 USB_PORT_FEAT_C_ENABLE);
1091 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001092 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1093 hub_is_superspeed(hub->hdev)) {
1094 need_debounce_delay = true;
1095 clear_port_feature(hub->hdev, port1,
1096 USB_PORT_FEAT_C_BH_PORT_RESET);
1097 }
Alan Stern253e0572009-10-27 15:20:13 -04001098 /* We can forget about a "removed" device when there's a
1099 * physical disconnect or the connect status changes.
1100 */
1101 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1102 (portchange & USB_PORT_STAT_C_CONNECTION))
1103 clear_bit(port1, hub->removed_bits);
1104
Alan Stern6ee0b272008-04-28 11:06:42 -04001105 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1106 /* Tell khubd to disconnect the device or
1107 * check for a new connection
1108 */
1109 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1110 set_bit(port1, hub->change_bits);
1111
1112 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001113 bool port_resumed = (portstatus &
1114 USB_PORT_STAT_LINK_STATE) ==
1115 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001116 /* The power session apparently survived the resume.
1117 * If there was an overcurrent or suspend change
1118 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001119 * take care of it. Look at the port link state
1120 * for USB 3.0 hubs, since they don't have a suspend
1121 * change bit, and they don't set the port link change
1122 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001123 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001124 if (portchange || (hub_is_superspeed(hub->hdev) &&
1125 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001126 set_bit(port1, hub->change_bits);
1127
1128 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001129#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001130 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001131#endif
Alan Stern8808f002008-04-28 11:06:55 -04001132 set_bit(port1, hub->change_bits);
1133
Alan Stern6ee0b272008-04-28 11:06:42 -04001134 } else {
1135 /* The power session is gone; tell khubd */
1136 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1137 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001138 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001139 }
1140
Alan Stern948fea32008-04-28 11:07:07 -04001141 /* If no port-status-change flags were set, we don't need any
1142 * debouncing. If flags were set we can try to debounce the
1143 * ports all at once right now, instead of letting khubd do them
1144 * one at a time later on.
1145 *
1146 * If any port-status changes do occur during this delay, khubd
1147 * will see them later and handle them normally.
1148 */
Alan Stern8520f382008-09-22 14:44:26 -04001149 if (need_debounce_delay) {
1150 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001151
Alan Stern8520f382008-09-22 14:44:26 -04001152 /* Don't do a long sleep inside a workqueue routine */
1153 if (type == HUB_INIT2) {
1154 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1155 schedule_delayed_work(&hub->init_work,
1156 msecs_to_jiffies(delay));
1157 return; /* Continues at init3: below */
1158 } else {
1159 msleep(delay);
1160 }
1161 }
1162 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001163 hub->quiescing = 0;
1164
1165 status = usb_submit_urb(hub->urb, GFP_NOIO);
1166 if (status < 0)
1167 dev_err(hub->intfdev, "activate --> %d\n", status);
1168 if (hub->has_indicators && blinkenlights)
1169 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1170
1171 /* Scan all ports that need attention */
1172 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001173
1174 /* Allow autosuspend if it was suppressed */
1175 if (type <= HUB_INIT3)
1176 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001177}
1178
Alan Stern8520f382008-09-22 14:44:26 -04001179/* Implement the continuations for the delays above */
1180static void hub_init_func2(struct work_struct *ws)
1181{
1182 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1183
1184 hub_activate(hub, HUB_INIT2);
1185}
1186
1187static void hub_init_func3(struct work_struct *ws)
1188{
1189 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1190
1191 hub_activate(hub, HUB_INIT3);
1192}
1193
Alan Stern43303542008-04-28 11:07:31 -04001194enum hub_quiescing_type {
1195 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1196};
1197
1198static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1199{
1200 struct usb_device *hdev = hub->hdev;
1201 int i;
1202
Alan Stern8520f382008-09-22 14:44:26 -04001203 cancel_delayed_work_sync(&hub->init_work);
1204
Alan Stern43303542008-04-28 11:07:31 -04001205 /* khubd and related activity won't re-trigger */
1206 hub->quiescing = 1;
1207
1208 if (type != HUB_SUSPEND) {
1209 /* Disconnect all the children */
1210 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001211 if (hub->ports[i]->child)
1212 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001213 }
1214 }
1215
1216 /* Stop khubd and related activity */
1217 usb_kill_urb(hub->urb);
1218 if (hub->has_indicators)
1219 cancel_delayed_work_sync(&hub->leds);
1220 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001221 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001222}
1223
Alan Stern3eb14912008-03-03 15:15:43 -05001224/* caller has locked the hub device */
1225static int hub_pre_reset(struct usb_interface *intf)
1226{
1227 struct usb_hub *hub = usb_get_intfdata(intf);
1228
Alan Stern43303542008-04-28 11:07:31 -04001229 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001230 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001231}
1232
1233/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001234static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001235{
Alan Stern7de18d82006-06-01 13:37:24 -04001236 struct usb_hub *hub = usb_get_intfdata(intf);
1237
Alan Sternf2835212008-04-28 11:07:17 -04001238 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001239 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001240}
1241
Lan Tianyufa2a9562012-09-05 13:44:31 +08001242static void usb_port_device_release(struct device *dev)
1243{
1244 struct usb_port *port_dev = to_usb_port(dev);
1245
1246 kfree(port_dev);
1247}
1248
1249static void usb_hub_remove_port_device(struct usb_hub *hub,
1250 int port1)
1251{
1252 device_unregister(&hub->ports[port1 - 1]->dev);
1253}
1254
1255struct device_type usb_port_device_type = {
1256 .name = "usb_port",
1257 .release = usb_port_device_release,
1258};
1259
1260static int usb_hub_create_port_device(struct usb_hub *hub,
1261 int port1)
1262{
1263 struct usb_port *port_dev = NULL;
1264 int retval;
1265
1266 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1267 if (!port_dev) {
1268 retval = -ENOMEM;
1269 goto exit;
1270 }
1271
1272 hub->ports[port1 - 1] = port_dev;
1273 port_dev->dev.parent = hub->intfdev;
1274 port_dev->dev.type = &usb_port_device_type;
1275 dev_set_name(&port_dev->dev, "port%d", port1);
1276
1277 retval = device_register(&port_dev->dev);
1278 if (retval)
1279 goto error_register;
1280 return 0;
1281
1282error_register:
1283 put_device(&port_dev->dev);
1284exit:
1285 return retval;
1286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288static int hub_configure(struct usb_hub *hub,
1289 struct usb_endpoint_descriptor *endpoint)
1290{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001291 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct usb_device *hdev = hub->hdev;
1293 struct device *hub_dev = hub->intfdev;
1294 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001295 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001297 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001298 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Alan Sternd697cdd2009-10-27 15:18:46 -04001300 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ret = -ENOMEM;
1303 goto fail;
1304 }
1305
1306 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1307 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ret = -ENOMEM;
1309 goto fail;
1310 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001311 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1314 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ret = -ENOMEM;
1316 goto fail;
1317 }
1318
1319 /* Request the entire hub descriptor.
1320 * hub->descriptor can handle USB_MAXCHILDREN ports,
1321 * but the hub can/will return fewer bytes here.
1322 */
John Youndbe79bb2001-09-17 00:00:00 -07001323 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (ret < 0) {
1325 message = "can't read hub descriptor";
1326 goto fail;
1327 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1328 message = "hub has too many ports!";
1329 ret = -ENODEV;
1330 goto fail;
1331 }
1332
1333 hdev->maxchild = hub->descriptor->bNbrPorts;
1334 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1335 (hdev->maxchild == 1) ? "" : "s");
1336
Lan Tianyufa2a9562012-09-05 13:44:31 +08001337 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1338 GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001339 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001340 ret = -ENOMEM;
1341 goto fail;
1342 }
1343
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001344 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
John Youndbe79bb2001-09-17 00:00:00 -07001346 /* FIXME for USB 3.0, skip for now */
1347 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1348 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int i;
1350 char portstr [USB_MAXCHILDREN + 1];
1351
1352 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001353 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1355 ? 'F' : 'R';
1356 portstr[hdev->maxchild] = 0;
1357 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1358 } else
1359 dev_dbg(hub_dev, "standalone hub\n");
1360
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001361 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301362 case HUB_CHAR_COMMON_LPSM:
1363 dev_dbg(hub_dev, "ganged power switching\n");
1364 break;
1365 case HUB_CHAR_INDV_PORT_LPSM:
1366 dev_dbg(hub_dev, "individual port power switching\n");
1367 break;
1368 case HUB_CHAR_NO_LPSM:
1369 case HUB_CHAR_LPSM:
1370 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001374 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301375 case HUB_CHAR_COMMON_OCPM:
1376 dev_dbg(hub_dev, "global over-current protection\n");
1377 break;
1378 case HUB_CHAR_INDV_PORT_OCPM:
1379 dev_dbg(hub_dev, "individual port over-current protection\n");
1380 break;
1381 case HUB_CHAR_NO_OCPM:
1382 case HUB_CHAR_OCPM:
1383 dev_dbg(hub_dev, "no over-current protection\n");
1384 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
1387 spin_lock_init (&hub->tt.lock);
1388 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001389 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301391 case USB_HUB_PR_FS:
1392 break;
1393 case USB_HUB_PR_HS_SINGLE_TT:
1394 dev_dbg(hub_dev, "Single TT\n");
1395 hub->tt.hub = hdev;
1396 break;
1397 case USB_HUB_PR_HS_MULTI_TT:
1398 ret = usb_set_interface(hdev, 0, 1);
1399 if (ret == 0) {
1400 dev_dbg(hub_dev, "TT per port\n");
1401 hub->tt.multi = 1;
1402 } else
1403 dev_err(hub_dev, "Using single TT (err %d)\n",
1404 ret);
1405 hub->tt.hub = hdev;
1406 break;
1407 case USB_HUB_PR_SS:
1408 /* USB 3.0 hubs don't have a TT */
1409 break;
1410 default:
1411 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1412 hdev->descriptor.bDeviceProtocol);
1413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001416 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001417 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001418 case HUB_TTTT_8_BITS:
1419 if (hdev->descriptor.bDeviceProtocol != 0) {
1420 hub->tt.think_time = 666;
1421 dev_dbg(hub_dev, "TT requires at most %d "
1422 "FS bit times (%d ns)\n",
1423 8, hub->tt.think_time);
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001426 case HUB_TTTT_16_BITS:
1427 hub->tt.think_time = 666 * 2;
1428 dev_dbg(hub_dev, "TT requires at most %d "
1429 "FS bit times (%d ns)\n",
1430 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001432 case HUB_TTTT_24_BITS:
1433 hub->tt.think_time = 666 * 3;
1434 dev_dbg(hub_dev, "TT requires at most %d "
1435 "FS bit times (%d ns)\n",
1436 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001438 case HUB_TTTT_32_BITS:
1439 hub->tt.think_time = 666 * 4;
1440 dev_dbg(hub_dev, "TT requires at most %d "
1441 "FS bit times (%d ns)\n",
1442 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 break;
1444 }
1445
1446 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001447 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 hub->has_indicators = 1;
1449 dev_dbg(hub_dev, "Port indicators are supported\n");
1450 }
1451
1452 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1453 hub->descriptor->bPwrOn2PwrGood * 2);
1454
1455 /* power budgeting mostly matters with bus-powered hubs,
1456 * and battery-powered root hubs (may provide just 8 mA).
1457 */
1458 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001459 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 message = "can't get hub status";
1461 goto fail;
1462 }
Alan Stern7d35b922005-04-25 11:18:32 -04001463 le16_to_cpus(&hubstatus);
1464 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001465 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1466 hub->mA_per_port = 500;
1467 else {
1468 hub->mA_per_port = hdev->bus_mA;
1469 hub->limited_power = 1;
1470 }
Alan Stern7d35b922005-04-25 11:18:32 -04001471 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1473 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001474 hub->limited_power = 1;
1475 if (hdev->maxchild > 0) {
1476 int remaining = hdev->bus_mA -
1477 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Alan Stern55c52712005-11-23 12:03:12 -05001479 if (remaining < hdev->maxchild * 100)
1480 dev_warn(hub_dev,
1481 "insufficient power available "
1482 "to use all downstream ports\n");
1483 hub->mA_per_port = 100; /* 7.2.1.1 */
1484 }
1485 } else { /* Self-powered external hub */
1486 /* FIXME: What about battery-powered external hubs that
1487 * provide less current per port? */
1488 hub->mA_per_port = 500;
1489 }
1490 if (hub->mA_per_port < 500)
1491 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1492 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001494 /* Update the HCD's internal representation of this hub before khubd
1495 * starts getting port status changes for devices under the hub.
1496 */
1497 hcd = bus_to_hcd(hdev->bus);
1498 if (hcd->driver->update_hub_device) {
1499 ret = hcd->driver->update_hub_device(hcd, hdev,
1500 &hub->tt, GFP_KERNEL);
1501 if (ret < 0) {
1502 message = "can't update HCD hub info";
1503 goto fail;
1504 }
1505 }
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1508 if (ret < 0) {
1509 message = "can't get hub status";
1510 goto fail;
1511 }
1512
1513 /* local power status reports aren't always correct */
1514 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1515 dev_dbg(hub_dev, "local power source is %s\n",
1516 (hubstatus & HUB_STATUS_LOCAL_POWER)
1517 ? "lost (inactive)" : "good");
1518
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001519 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 dev_dbg(hub_dev, "%sover-current condition exists\n",
1521 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1522
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001523 /* set up the interrupt endpoint
1524 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1525 * bytes as USB2.0[11.12.3] says because some hubs are known
1526 * to send more data (and thus cause overflow). For root hubs,
1527 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1528 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1530 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1531
1532 if (maxp > sizeof(*hub->buffer))
1533 maxp = sizeof(*hub->buffer);
1534
1535 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1536 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 ret = -ENOMEM;
1538 goto fail;
1539 }
1540
1541 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1542 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 /* maybe cycle the hub leds */
1545 if (hub->has_indicators && blinkenlights)
1546 hub->indicator [0] = INDICATOR_CYCLE;
1547
Lan Tianyufa2a9562012-09-05 13:44:31 +08001548 for (i = 0; i < hdev->maxchild; i++)
1549 if (usb_hub_create_port_device(hub, i + 1) < 0)
1550 dev_err(hub->intfdev,
1551 "couldn't create port%d device.\n", i + 1);
1552
Alan Sternf2835212008-04-28 11:07:17 -04001553 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 return 0;
1555
1556fail:
1557 dev_err (hub_dev, "config failed, %s (err %d)\n",
1558 message, ret);
1559 /* hub_disconnect() frees urb and descriptor */
1560 return ret;
1561}
1562
Alan Sterne8054852007-05-04 11:55:11 -04001563static void hub_release(struct kref *kref)
1564{
1565 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1566
1567 usb_put_intf(to_usb_interface(hub->intfdev));
1568 kfree(hub);
1569}
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571static unsigned highspeed_hubs;
1572
1573static void hub_disconnect(struct usb_interface *intf)
1574{
Huajun Li88162302012-03-12 21:00:19 +08001575 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001576 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001577 int i;
1578
Alan Sterne8054852007-05-04 11:55:11 -04001579 /* Take the hub off the event list and don't let it be added again */
1580 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001581 if (!list_empty(&hub->event_list)) {
1582 list_del_init(&hub->event_list);
1583 usb_autopm_put_interface_no_suspend(intf);
1584 }
Alan Sterne8054852007-05-04 11:55:11 -04001585 hub->disconnected = 1;
1586 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Alan Stern7de18d82006-06-01 13:37:24 -04001588 /* Disconnect all children and quiesce the hub */
1589 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001590 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001591
Alan Stern8b28c752005-08-10 17:04:13 -04001592 usb_set_intfdata (intf, NULL);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001593
1594 for (i = 0; i < hdev->maxchild; i++)
1595 usb_hub_remove_port_device(hub, i + 1);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001596 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Alan Sterne8054852007-05-04 11:55:11 -04001598 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 highspeed_hubs--;
1600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001602 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001603 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001604 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001605 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Alan Sterne8054852007-05-04 11:55:11 -04001607 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1611{
1612 struct usb_host_interface *desc;
1613 struct usb_endpoint_descriptor *endpoint;
1614 struct usb_device *hdev;
1615 struct usb_hub *hub;
1616
1617 desc = intf->cur_altsetting;
1618 hdev = interface_to_usbdev(intf);
1619
Ming Lei596d7892012-10-24 11:59:25 +08001620 /*
1621 * Set default autosuspend delay as 0 to speedup bus suspend,
1622 * based on the below considerations:
1623 *
1624 * - Unlike other drivers, the hub driver does not rely on the
1625 * autosuspend delay to provide enough time to handle a wakeup
1626 * event, and the submitted status URB is just to check future
1627 * change on hub downstream ports, so it is safe to do it.
1628 *
1629 * - The patch might cause one or more auto supend/resume for
1630 * below very rare devices when they are plugged into hub
1631 * first time:
1632 *
1633 * devices having trouble initializing, and disconnect
1634 * themselves from the bus and then reconnect a second
1635 * or so later
1636 *
1637 * devices just for downloading firmware, and disconnects
1638 * themselves after completing it
1639 *
1640 * For these quite rare devices, their drivers may change the
1641 * autosuspend delay of their parent hub in the probe() to one
1642 * appropriate value to avoid the subtle problem if someone
1643 * does care it.
1644 *
1645 * - The patch may cause one or more auto suspend/resume on
1646 * hub during running 'lsusb', but it is probably too
1647 * infrequent to worry about.
1648 *
1649 * - Change autosuspend delay of hub can avoid unnecessary auto
1650 * suspend timer for hub, also may decrease power consumption
1651 * of USB bus.
1652 */
1653 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1654
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001655 /* Hubs have proper suspend/resume support. */
1656 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001657
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001658 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001659 dev_err(&intf->dev,
1660 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001661 return -E2BIG;
1662 }
1663
David Brownell89ccbdc2006-04-02 10:18:09 -08001664#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1665 if (hdev->parent) {
1666 dev_warn(&intf->dev, "ignoring external hub\n");
1667 return -ENODEV;
1668 }
1669#endif
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /* Some hubs have a subclass of 1, which AFAICT according to the */
1672 /* specs is not defined, but it works */
1673 if ((desc->desc.bInterfaceSubClass != 0) &&
1674 (desc->desc.bInterfaceSubClass != 1)) {
1675descriptor_error:
1676 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1677 return -EIO;
1678 }
1679
1680 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1681 if (desc->desc.bNumEndpoints != 1)
1682 goto descriptor_error;
1683
1684 endpoint = &desc->endpoint[0].desc;
1685
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001686 /* If it's not an interrupt in endpoint, we'd better punt! */
1687 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 goto descriptor_error;
1689
1690 /* We found a hub */
1691 dev_info (&intf->dev, "USB hub found\n");
1692
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001693 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (!hub) {
1695 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1696 return -ENOMEM;
1697 }
1698
Alan Sterne8054852007-05-04 11:55:11 -04001699 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 INIT_LIST_HEAD(&hub->event_list);
1701 hub->intfdev = &intf->dev;
1702 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001703 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001704 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001705 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001708 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 if (hdev->speed == USB_SPEED_HIGH)
1711 highspeed_hubs++;
1712
Ming Leie6f30de2012-10-24 11:59:24 +08001713 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1714 hub->quirk_check_port_auto_suspend = 1;
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (hub_configure(hub, endpoint) >= 0)
1717 return 0;
1718
1719 hub_disconnect (intf);
1720 return -ENODEV;
1721}
1722
1723static int
1724hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1725{
1726 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuff823c72012-09-05 13:44:32 +08001727 struct usb_hub *hub = hdev_to_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
1729 /* assert ifno == 0 (part of hub spec) */
1730 switch (code) {
1731 case USBDEVFS_HUB_PORTINFO: {
1732 struct usbdevfs_hub_portinfo *info = user_data;
1733 int i;
1734
1735 spin_lock_irq(&device_state_lock);
1736 if (hdev->devnum <= 0)
1737 info->nports = 0;
1738 else {
1739 info->nports = hdev->maxchild;
1740 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001741 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 info->port[i] = 0;
1743 else
1744 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001745 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 }
1748 spin_unlock_irq(&device_state_lock);
1749
1750 return info->nports + 1;
1751 }
1752
1753 default:
1754 return -ENOSYS;
1755 }
1756}
1757
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001758/*
1759 * Allow user programs to claim ports on a hub. When a device is attached
1760 * to one of these "claimed" ports, the program will "own" the device.
1761 */
1762static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001763 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001764{
1765 if (hdev->state == USB_STATE_NOTATTACHED)
1766 return -ENODEV;
1767 if (port1 == 0 || port1 > hdev->maxchild)
1768 return -EINVAL;
1769
1770 /* This assumes that devices not managed by the hub driver
1771 * will always have maxchild equal to 0.
1772 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001773 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001774 return 0;
1775}
1776
1777/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001778int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1779 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001780{
1781 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001782 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001783
1784 rc = find_port_owner(hdev, port1, &powner);
1785 if (rc)
1786 return rc;
1787 if (*powner)
1788 return -EBUSY;
1789 *powner = owner;
1790 return rc;
1791}
1792
Lan Tianyu336c5c32012-07-06 14:13:52 +08001793int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1794 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001795{
1796 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001797 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001798
1799 rc = find_port_owner(hdev, port1, &powner);
1800 if (rc)
1801 return rc;
1802 if (*powner != owner)
1803 return -ENOENT;
1804 *powner = NULL;
1805 return rc;
1806}
1807
Lan Tianyu336c5c32012-07-06 14:13:52 +08001808void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001809{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001810 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001811 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001812
Lan Tianyufa2a9562012-09-05 13:44:31 +08001813 for (n = 0; n < hdev->maxchild; n++) {
1814 if (hub->ports[n]->port_owner == owner)
1815 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001816 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001817
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001818}
1819
1820/* The caller must hold udev's lock */
1821bool usb_device_is_owned(struct usb_device *udev)
1822{
1823 struct usb_hub *hub;
1824
1825 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1826 return false;
1827 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001828 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1832{
Lan Tianyuff823c72012-09-05 13:44:32 +08001833 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 int i;
1835
1836 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001837 if (hub->ports[i]->child)
1838 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001840 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001841 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 udev->state = USB_STATE_NOTATTACHED;
1843}
1844
1845/**
1846 * usb_set_device_state - change a device's current state (usbcore, hcds)
1847 * @udev: pointer to device whose state should be changed
1848 * @new_state: new state value to be stored
1849 *
1850 * udev->state is _not_ fully protected by the device lock. Although
1851 * most transitions are made only while holding the lock, the state can
1852 * can change to USB_STATE_NOTATTACHED at almost any time. This
1853 * is so that devices can be marked as disconnected as soon as possible,
1854 * without having to wait for any semaphores to be released. As a result,
1855 * all changes to any device's state must be protected by the
1856 * device_state_lock spinlock.
1857 *
1858 * Once a device has been added to the device tree, all changes to its state
1859 * should be made using this routine. The state should _not_ be set directly.
1860 *
1861 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1862 * Otherwise udev->state is set to new_state, and if new_state is
1863 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1864 * to USB_STATE_NOTATTACHED.
1865 */
1866void usb_set_device_state(struct usb_device *udev,
1867 enum usb_device_state new_state)
1868{
1869 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001870 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 spin_lock_irqsave(&device_state_lock, flags);
1873 if (udev->state == USB_STATE_NOTATTACHED)
1874 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001875 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001876
1877 /* root hub wakeup capabilities are managed out-of-band
1878 * and may involve silicon errata ... ignore them here.
1879 */
1880 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001881 if (udev->state == USB_STATE_SUSPENDED
1882 || new_state == USB_STATE_SUSPENDED)
1883 ; /* No change to wakeup settings */
1884 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001885 wakeup = udev->actconfig->desc.bmAttributes
1886 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001887 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001888 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001889 }
Sarah Sharp15123002007-12-21 16:54:15 -08001890 if (udev->state == USB_STATE_SUSPENDED &&
1891 new_state != USB_STATE_SUSPENDED)
1892 udev->active_duration -= jiffies;
1893 else if (new_state == USB_STATE_SUSPENDED &&
1894 udev->state != USB_STATE_SUSPENDED)
1895 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001896 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001897 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 recursively_mark_NOTATTACHED(udev);
1899 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001900 if (wakeup >= 0)
1901 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
David Vrabel6da9c992009-02-18 14:43:47 +00001903EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001905/*
Alan Stern3b29b682011-02-22 09:53:41 -05001906 * Choose a device number.
1907 *
1908 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1909 * USB-2.0 buses they are also used as device addresses, however on
1910 * USB-3.0 buses the address is assigned by the controller hardware
1911 * and it usually is not the same as the device number.
1912 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001913 * WUSB devices are simple: they have no hubs behind, so the mapping
1914 * device <-> virtual port number becomes 1:1. Why? to simplify the
1915 * life of the device connection logic in
1916 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1917 * handshake we need to assign a temporary address in the unauthorized
1918 * space. For simplicity we use the first virtual port number found to
1919 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1920 * and that becomes it's address [X < 128] or its unauthorized address
1921 * [X | 0x80].
1922 *
1923 * We add 1 as an offset to the one-based USB-stack port number
1924 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1925 * 0 is reserved by USB for default address; (b) Linux's USB stack
1926 * uses always #1 for the root hub of the controller. So USB stack's
1927 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001928 *
1929 * Devices connected under xHCI are not as simple. The host controller
1930 * supports virtualization, so the hardware assigns device addresses and
1931 * the HCD must setup data structures before issuing a set address
1932 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001933 */
Alan Stern3b29b682011-02-22 09:53:41 -05001934static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 int devnum;
1937 struct usb_bus *bus = udev->bus;
1938
1939 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001940 if (udev->wusb) {
1941 devnum = udev->portnum + 1;
1942 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1943 } else {
1944 /* Try to allocate the next devnum beginning at
1945 * bus->devnum_next. */
1946 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1947 bus->devnum_next);
1948 if (devnum >= 128)
1949 devnum = find_next_zero_bit(bus->devmap.devicemap,
1950 128, 1);
1951 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (devnum < 128) {
1954 set_bit(devnum, bus->devmap.devicemap);
1955 udev->devnum = devnum;
1956 }
1957}
1958
Alan Stern3b29b682011-02-22 09:53:41 -05001959static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 if (udev->devnum > 0) {
1962 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1963 udev->devnum = -1;
1964 }
1965}
1966
Alan Stern3b29b682011-02-22 09:53:41 -05001967static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001968{
1969 /* The address for a WUSB device is managed by wusbcore. */
1970 if (!udev->wusb)
1971 udev->devnum = devnum;
1972}
1973
Herbert Xuf7410ce2010-01-10 20:15:03 +11001974static void hub_free_dev(struct usb_device *udev)
1975{
1976 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1977
1978 /* Root hubs aren't real devices, so don't free HCD resources */
1979 if (hcd->driver->free_dev && udev->parent)
1980 hcd->driver->free_dev(hcd, udev);
1981}
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983/**
1984 * usb_disconnect - disconnect a device (usbcore-internal)
1985 * @pdev: pointer to device being disconnected
1986 * Context: !in_interrupt ()
1987 *
1988 * Something got disconnected. Get rid of it and all of its children.
1989 *
1990 * If *pdev is a normal device then the parent hub must already be locked.
1991 * If *pdev is a root hub then this routine will acquire the
1992 * usb_bus_list_lock on behalf of the caller.
1993 *
1994 * Only hub drivers (including virtual root hub drivers for host
1995 * controllers) should ever call this.
1996 *
1997 * This call is synchronous, and may not be used in an interrupt context.
1998 */
1999void usb_disconnect(struct usb_device **pdev)
2000{
2001 struct usb_device *udev = *pdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08002002 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 int i;
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /* mark the device as inactive, so any further urb submissions for
2006 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002007 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 */
2009 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002010 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2011 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002013 usb_lock_device(udev);
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08002016 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08002017 if (hub->ports[i]->child)
2018 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020
2021 /* deallocate hcd/hardware state ... nuking all pending urbs and
2022 * cleaning up all state associated with the current configuration
2023 * so that the hardware is now fully quiesced.
2024 */
Alan Stern782da722006-07-01 22:09:35 -04002025 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002027 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Alan Stern3b23dd62008-12-05 14:10:34 -05002029 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002030 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002031
Alan Stern782da722006-07-01 22:09:35 -04002032 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002033 * for de-configuring the device and invoking the remove-device
2034 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002035 */
2036 device_del(&udev->dev);
2037
2038 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * (or root_hub) pointer.
2040 */
Alan Stern3b29b682011-02-22 09:53:41 -05002041 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 /* Avoid races with recursively_mark_NOTATTACHED() */
2044 spin_lock_irq(&device_state_lock);
2045 *pdev = NULL;
2046 spin_unlock_irq(&device_state_lock);
2047
Herbert Xuf7410ce2010-01-10 20:15:03 +11002048 hub_free_dev(udev);
2049
Alan Stern782da722006-07-01 22:09:35 -04002050 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002053#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054static void show_string(struct usb_device *udev, char *id, char *string)
2055{
2056 if (!string)
2057 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002058 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002061static void announce_device(struct usb_device *udev)
2062{
2063 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2064 le16_to_cpu(udev->descriptor.idVendor),
2065 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002066 dev_info(&udev->dev,
2067 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002068 udev->descriptor.iManufacturer,
2069 udev->descriptor.iProduct,
2070 udev->descriptor.iSerialNumber);
2071 show_string(udev, "Product", udev->product);
2072 show_string(udev, "Manufacturer", udev->manufacturer);
2073 show_string(udev, "SerialNumber", udev->serial);
2074}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002076static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077#endif
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079#ifdef CONFIG_USB_OTG
2080#include "otg_whitelist.h"
2081#endif
2082
Oliver Neukum3ede7602007-01-11 14:35:50 +01002083/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002084 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002085 * @udev: newly addressed device (in ADDRESS state)
2086 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002087 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002088 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002089static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002091 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093#ifdef CONFIG_USB_OTG
2094 /*
2095 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2096 * to wake us after we've powered off VBUS; and HNP, switching roles
2097 * "host" to "peripheral". The OTG descriptor helps figure this out.
2098 */
2099 if (!udev->bus->is_b_host
2100 && udev->config
2101 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002102 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct usb_bus *bus = udev->bus;
2104
2105 /* descriptor may appear anywhere in config */
2106 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2107 le16_to_cpu(udev->config[0].desc.wTotalLength),
2108 USB_DT_OTG, (void **) &desc) == 0) {
2109 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002110 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 dev_info(&udev->dev,
2113 "Dual-Role OTG device on %sHNP port\n",
2114 (port1 == bus->otg_port)
2115 ? "" : "non-");
2116
2117 /* enable HNP before suspend, it's simpler */
2118 if (port1 == bus->otg_port)
2119 bus->b_hnp_enable = 1;
2120 err = usb_control_msg(udev,
2121 usb_sndctrlpipe(udev, 0),
2122 USB_REQ_SET_FEATURE, 0,
2123 bus->b_hnp_enable
2124 ? USB_DEVICE_B_HNP_ENABLE
2125 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2126 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2127 if (err < 0) {
2128 /* OTG MESSAGE: report errors here,
2129 * customize to match your product.
2130 */
2131 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002132 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 err);
2134 bus->b_hnp_enable = 0;
2135 }
2136 }
2137 }
2138 }
2139
2140 if (!is_targeted(udev)) {
2141
2142 /* Maybe it can talk to us, though we can't talk to it.
2143 * (Includes HNP test device.)
2144 */
2145 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002146 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (err < 0)
2148 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2149 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002150 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 goto fail;
2152 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002153fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002155 return err;
2156}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002158
2159/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002160 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002161 * @udev: newly addressed device (in ADDRESS state)
2162 *
2163 * This is only called by usb_new_device() and usb_authorize_device()
2164 * and FIXME -- all comments that apply to them apply here wrt to
2165 * environment.
2166 *
2167 * If the device is WUSB and not authorized, we don't attempt to read
2168 * the string descriptors, as they will be errored out by the device
2169 * until it has been authorized.
2170 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002171static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002172{
2173 int err;
2174
2175 if (udev->config == NULL) {
2176 err = usb_get_configuration(udev);
2177 if (err < 0) {
2178 dev_err(&udev->dev, "can't read configurations, error %d\n",
2179 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002180 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002181 }
2182 }
2183 if (udev->wusb == 1 && udev->authorized == 0) {
2184 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2185 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2186 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2187 }
2188 else {
2189 /* read the standard strings and cache them if present */
2190 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2191 udev->manufacturer = usb_cache_string(udev,
2192 udev->descriptor.iManufacturer);
2193 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2194 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002195 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002196 if (err < 0)
2197 return err;
2198
2199 usb_detect_interface_quirks(udev);
2200
2201 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002202}
2203
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002204static void set_usb_port_removable(struct usb_device *udev)
2205{
2206 struct usb_device *hdev = udev->parent;
2207 struct usb_hub *hub;
2208 u8 port = udev->portnum;
2209 u16 wHubCharacteristics;
2210 bool removable = true;
2211
2212 if (!hdev)
2213 return;
2214
2215 hub = hdev_to_hub(udev->parent);
2216
2217 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2218
2219 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2220 return;
2221
2222 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002223 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2224 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002225 removable = false;
2226 } else {
2227 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2228 removable = false;
2229 }
2230
2231 if (removable)
2232 udev->removable = USB_DEVICE_REMOVABLE;
2233 else
2234 udev->removable = USB_DEVICE_FIXED;
2235}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002236
2237/**
2238 * usb_new_device - perform initial device setup (usbcore-internal)
2239 * @udev: newly addressed device (in ADDRESS state)
2240 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002241 * This is called with devices which have been detected but not fully
2242 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002243 * for any device configuration. The caller must have locked either
2244 * the parent hub (if udev is a normal device) or else the
2245 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2246 * udev has already been installed, but udev is not yet visible through
2247 * sysfs or other filesystem code.
2248 *
2249 * It will return if the device is configured properly or not. Zero if
2250 * the interface was registered with the driver core; else a negative
2251 * errno value.
2252 *
2253 * This call is synchronous, and may not be used in an interrupt context.
2254 *
2255 * Only the hub driver or root-hub registrar should ever call this.
2256 */
2257int usb_new_device(struct usb_device *udev)
2258{
2259 int err;
2260
Dan Streetman16985402010-01-06 09:56:53 -05002261 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002262 /* Initialize non-root-hub device wakeup to disabled;
2263 * device (un)configuration controls wakeup capable
2264 * sysfs power/wakeup controls wakeup enabled/disabled
2265 */
2266 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002267 }
2268
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002269 /* Tell the runtime-PM framework the device is active */
2270 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002271 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002272 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002273 pm_runtime_enable(&udev->dev);
2274
Alan Sternc08512c2010-11-15 15:57:58 -05002275 /* By default, forbid autosuspend for all devices. It will be
2276 * allowed for hubs during binding.
2277 */
2278 usb_disable_autosuspend(udev);
2279
Alan Stern8d8558d2009-12-08 15:50:41 -05002280 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002281 if (err < 0)
2282 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002283 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2284 udev->devnum, udev->bus->busnum,
2285 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002286 /* export the usbdev device-node for libusb */
2287 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2288 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2289
Alan Stern6cd13202008-11-13 15:08:30 -05002290 /* Tell the world! */
2291 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002292
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002293 if (udev->serial)
2294 add_device_randomness(udev->serial, strlen(udev->serial));
2295 if (udev->product)
2296 add_device_randomness(udev->product, strlen(udev->product));
2297 if (udev->manufacturer)
2298 add_device_randomness(udev->manufacturer,
2299 strlen(udev->manufacturer));
2300
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002301 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002302
2303 /*
2304 * check whether the hub marks this port as non-removable. Do it
2305 * now so that platform-specific data can override it in
2306 * device_add()
2307 */
2308 if (udev->parent)
2309 set_usb_port_removable(udev);
2310
Alan Stern782da722006-07-01 22:09:35 -04002311 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002312 * for configuring the device and invoking the add-device
2313 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002314 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002315 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (err) {
2317 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2318 goto fail;
2319 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002320
Alan Stern3b23dd62008-12-05 14:10:34 -05002321 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002322 usb_mark_last_busy(udev);
2323 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
2326fail:
2327 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002328 pm_runtime_disable(&udev->dev);
2329 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002330 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002333
2334/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002335 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2336 * @usb_dev: USB device
2337 *
2338 * Move the USB device to a very basic state where interfaces are disabled
2339 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002340 *
2341 * We share a lock (that we have) with device_del(), so we need to
2342 * defer its call.
2343 */
2344int usb_deauthorize_device(struct usb_device *usb_dev)
2345{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002346 usb_lock_device(usb_dev);
2347 if (usb_dev->authorized == 0)
2348 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002349
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002350 usb_dev->authorized = 0;
2351 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002352
2353 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002354 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002355 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002356 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002357 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002358 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002359
2360 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002361 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002362
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002363out_unauthorized:
2364 usb_unlock_device(usb_dev);
2365 return 0;
2366}
2367
2368
2369int usb_authorize_device(struct usb_device *usb_dev)
2370{
2371 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002372
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002373 usb_lock_device(usb_dev);
2374 if (usb_dev->authorized == 1)
2375 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002376
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002377 result = usb_autoresume_device(usb_dev);
2378 if (result < 0) {
2379 dev_err(&usb_dev->dev,
2380 "can't autoresume for authorization: %d\n", result);
2381 goto error_autoresume;
2382 }
2383 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2384 if (result < 0) {
2385 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2386 "authorization: %d\n", result);
2387 goto error_device_descriptor;
2388 }
Alan Sternda307122009-12-08 15:54:44 -05002389
2390 kfree(usb_dev->product);
2391 usb_dev->product = NULL;
2392 kfree(usb_dev->manufacturer);
2393 usb_dev->manufacturer = NULL;
2394 kfree(usb_dev->serial);
2395 usb_dev->serial = NULL;
2396
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002397 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002398 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002399 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002400 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002401 /* Choose and set the configuration. This registers the interfaces
2402 * with the driver core and lets interface drivers bind to them.
2403 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002404 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002405 if (c >= 0) {
2406 result = usb_set_configuration(usb_dev, c);
2407 if (result) {
2408 dev_err(&usb_dev->dev,
2409 "can't set config #%d, error %d\n", c, result);
2410 /* This need not be fatal. The user can try to
2411 * set other configurations. */
2412 }
2413 }
2414 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002415
Alan Stern8d8558d2009-12-08 15:50:41 -05002416error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002417error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002418 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002419error_autoresume:
2420out_authorized:
2421 usb_unlock_device(usb_dev); // complements locktree
2422 return result;
2423}
2424
2425
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002426/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2427static unsigned hub_is_wusb(struct usb_hub *hub)
2428{
2429 struct usb_hcd *hcd;
2430 if (hub->hdev->parent != NULL) /* not a root hub? */
2431 return 0;
2432 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2433 return hcd->wireless;
2434}
2435
2436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437#define PORT_RESET_TRIES 5
2438#define SET_ADDRESS_TRIES 2
2439#define GET_DESCRIPTOR_TRIES 2
2440#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302441#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2444#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002445#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446#define HUB_LONG_RESET_TIME 200
2447#define HUB_RESET_TIMEOUT 500
2448
Sarah Sharp10d674a2011-09-14 14:24:52 -07002449static int hub_port_reset(struct usb_hub *hub, int port1,
2450 struct usb_device *udev, unsigned int delay, bool warm);
2451
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002452/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2453 * Port worm reset is required to recover
2454 */
2455static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002456{
2457 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002458 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2459 USB_SS_PORT_LS_SS_INACTIVE) ||
2460 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2461 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002462}
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002465 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
2467 int delay_time, ret;
2468 u16 portstatus;
2469 u16 portchange;
2470
2471 for (delay_time = 0;
2472 delay_time < HUB_RESET_TIMEOUT;
2473 delay_time += delay) {
2474 /* wait to give the device a chance to reset */
2475 msleep(delay);
2476
2477 /* read and decode port status */
2478 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2479 if (ret < 0)
2480 return ret;
2481
Andiry Xu75d7cf72011-09-13 16:41:11 -07002482 /*
2483 * Some buggy devices require a warm reset to be issued even
2484 * when the port appears not to be connected.
2485 */
2486 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002487 /*
2488 * Some buggy devices can cause an NEC host controller
2489 * to transition to the "Error" state after a hot port
2490 * reset. This will show up as the port state in
2491 * "Inactive", and the port may also report a
2492 * disconnect. Forcing a warm port reset seems to make
2493 * the device work.
2494 *
2495 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2496 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002497 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002498 int ret;
2499
2500 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2501 clear_port_feature(hub->hdev, port1,
2502 USB_PORT_FEAT_C_CONNECTION);
2503 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2504 clear_port_feature(hub->hdev, port1,
2505 USB_PORT_FEAT_C_PORT_LINK_STATE);
2506 if (portchange & USB_PORT_STAT_C_RESET)
2507 clear_port_feature(hub->hdev, port1,
2508 USB_PORT_FEAT_C_RESET);
2509 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2510 port1);
2511 ret = hub_port_reset(hub, port1,
2512 udev, HUB_BH_RESET_TIME,
2513 true);
2514 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2515 clear_port_feature(hub->hdev, port1,
2516 USB_PORT_FEAT_C_CONNECTION);
2517 return ret;
2518 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002519 /* Device went away? */
2520 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2521 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Andiry Xu75d7cf72011-09-13 16:41:11 -07002523 /* bomb out completely if the connection bounced */
2524 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2525 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Andiry Xu75d7cf72011-09-13 16:41:11 -07002527 /* if we`ve finished resetting, then break out of
2528 * the loop
2529 */
2530 if (!(portstatus & USB_PORT_STAT_RESET) &&
2531 (portstatus & USB_PORT_STAT_ENABLE)) {
2532 if (hub_is_wusb(hub))
2533 udev->speed = USB_SPEED_WIRELESS;
2534 else if (hub_is_superspeed(hub->hdev))
2535 udev->speed = USB_SPEED_SUPER;
2536 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2537 udev->speed = USB_SPEED_HIGH;
2538 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2539 udev->speed = USB_SPEED_LOW;
2540 else
2541 udev->speed = USB_SPEED_FULL;
2542 return 0;
2543 }
2544 } else {
2545 if (portchange & USB_PORT_STAT_C_BH_RESET)
2546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548
2549 /* switch to the long delay after two short delay failures */
2550 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2551 delay = HUB_LONG_RESET_TIME;
2552
2553 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002554 "port %d not %sreset yet, waiting %dms\n",
2555 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557
2558 return -EBUSY;
2559}
2560
Andiry Xu75d7cf72011-09-13 16:41:11 -07002561static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2562 struct usb_device *udev, int *status, bool warm)
2563{
2564 switch (*status) {
2565 case 0:
2566 if (!warm) {
2567 struct usb_hcd *hcd;
2568 /* TRSTRCY = 10 ms; plus some extra */
2569 msleep(10 + 40);
2570 update_devnum(udev, 0);
2571 hcd = bus_to_hcd(udev->bus);
2572 if (hcd->driver->reset_device) {
2573 *status = hcd->driver->reset_device(hcd, udev);
2574 if (*status < 0) {
2575 dev_err(&udev->dev, "Cannot reset "
2576 "HCD device state\n");
2577 break;
2578 }
2579 }
2580 }
2581 /* FALL THROUGH */
2582 case -ENOTCONN:
2583 case -ENODEV:
2584 clear_port_feature(hub->hdev,
2585 port1, USB_PORT_FEAT_C_RESET);
2586 /* FIXME need disconnect() for NOTATTACHED device */
2587 if (warm) {
2588 clear_port_feature(hub->hdev, port1,
2589 USB_PORT_FEAT_C_BH_PORT_RESET);
2590 clear_port_feature(hub->hdev, port1,
2591 USB_PORT_FEAT_C_PORT_LINK_STATE);
2592 } else {
2593 usb_set_device_state(udev, *status
2594 ? USB_STATE_NOTATTACHED
2595 : USB_STATE_DEFAULT);
2596 }
2597 break;
2598 }
2599}
2600
2601/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002603 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 int i, status;
2606
Andiry Xu75d7cf72011-09-13 16:41:11 -07002607 if (!warm) {
2608 /* Block EHCI CF initialization during the port reset.
2609 * Some companion controllers don't like it when they mix.
2610 */
2611 down_read(&ehci_cf_port_reset_rwsem);
2612 } else {
2613 if (!hub_is_superspeed(hub->hdev)) {
2614 dev_err(hub->intfdev, "only USB3 hub support "
2615 "warm reset\n");
2616 return -EINVAL;
2617 }
2618 }
Alan Stern32fe0192007-10-10 16:27:07 -04002619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 /* Reset the port */
2621 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002622 status = set_port_feature(hub->hdev, port1, (warm ?
2623 USB_PORT_FEAT_BH_PORT_RESET :
2624 USB_PORT_FEAT_RESET));
2625 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002627 "cannot %sreset port %d (err = %d)\n",
2628 warm ? "warm " : "", port1, status);
2629 } else {
2630 status = hub_port_wait_reset(hub, port1, udev, delay,
2631 warm);
David Brownelldd165252005-08-31 10:45:25 -07002632 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 dev_dbg(hub->intfdev,
2634 "port_wait_reset: err = %d\n",
2635 status);
2636 }
2637
2638 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002639 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2640 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002641 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
2643
2644 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002645 "port %d not enabled, trying %sreset again...\n",
2646 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 delay = HUB_LONG_RESET_TIME;
2648 }
2649
2650 dev_err (hub->intfdev,
2651 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2652 port1);
2653
Andiry Xu75d7cf72011-09-13 16:41:11 -07002654done:
2655 if (!warm)
2656 up_read(&ehci_cf_port_reset_rwsem);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return status;
2659}
2660
Andiry Xu0ed9a572011-04-27 18:07:43 +08002661/* Check if a port is power on */
2662static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2663{
2664 int ret = 0;
2665
2666 if (hub_is_superspeed(hub->hdev)) {
2667 if (portstatus & USB_SS_PORT_STAT_POWER)
2668 ret = 1;
2669 } else {
2670 if (portstatus & USB_PORT_STAT_POWER)
2671 ret = 1;
2672 }
2673
2674 return ret;
2675}
2676
Alan Sternd388dab2006-07-01 22:14:24 -04002677#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Andiry Xu0ed9a572011-04-27 18:07:43 +08002679/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2680static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2681{
2682 int ret = 0;
2683
2684 if (hub_is_superspeed(hub->hdev)) {
2685 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2686 == USB_SS_PORT_LS_U3)
2687 ret = 1;
2688 } else {
2689 if (portstatus & USB_PORT_STAT_SUSPEND)
2690 ret = 1;
2691 }
2692
2693 return ret;
2694}
Alan Sternb01b03f2008-04-28 11:06:11 -04002695
2696/* Determine whether the device on a port is ready for a normal resume,
2697 * is ready for a reset-resume, or should be disconnected.
2698 */
2699static int check_port_resume_type(struct usb_device *udev,
2700 struct usb_hub *hub, int port1,
2701 int status, unsigned portchange, unsigned portstatus)
2702{
2703 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002704 if (status || port_is_suspended(hub, portstatus) ||
2705 !port_is_power_on(hub, portstatus) ||
2706 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002707 if (status >= 0)
2708 status = -ENODEV;
2709 }
2710
Alan Stern86c57ed2008-06-30 11:14:43 -04002711 /* Can't do a normal resume if the port isn't enabled,
2712 * so try a reset-resume instead.
2713 */
2714 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2715 if (udev->persist_enabled)
2716 udev->reset_resume = 1;
2717 else
2718 status = -ENODEV;
2719 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002720
2721 if (status) {
2722 dev_dbg(hub->intfdev,
2723 "port %d status %04x.%04x after resume, %d\n",
2724 port1, portchange, portstatus, status);
2725 } else if (udev->reset_resume) {
2726
2727 /* Late port handoff can set status-change bits */
2728 if (portchange & USB_PORT_STAT_C_CONNECTION)
2729 clear_port_feature(hub->hdev, port1,
2730 USB_PORT_FEAT_C_CONNECTION);
2731 if (portchange & USB_PORT_STAT_C_ENABLE)
2732 clear_port_feature(hub->hdev, port1,
2733 USB_PORT_FEAT_C_ENABLE);
2734 }
2735
2736 return status;
2737}
2738
Sarah Sharpf74631e2012-06-25 12:08:08 -07002739int usb_disable_ltm(struct usb_device *udev)
2740{
2741 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2742
2743 /* Check if the roothub and device supports LTM. */
2744 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2745 !usb_device_supports_ltm(udev))
2746 return 0;
2747
2748 /* Clear Feature LTM Enable can only be sent if the device is
2749 * configured.
2750 */
2751 if (!udev->actconfig)
2752 return 0;
2753
2754 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2755 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2756 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2757 USB_CTRL_SET_TIMEOUT);
2758}
2759EXPORT_SYMBOL_GPL(usb_disable_ltm);
2760
2761void usb_enable_ltm(struct usb_device *udev)
2762{
2763 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2764
2765 /* Check if the roothub and device supports LTM. */
2766 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2767 !usb_device_supports_ltm(udev))
2768 return;
2769
2770 /* Set Feature LTM Enable can only be sent if the device is
2771 * configured.
2772 */
2773 if (!udev->actconfig)
2774 return;
2775
2776 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2777 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2778 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2779 USB_CTRL_SET_TIMEOUT);
2780}
2781EXPORT_SYMBOL_GPL(usb_enable_ltm);
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783#ifdef CONFIG_USB_SUSPEND
2784
2785/*
Alan Stern140d8f62006-07-01 22:07:21 -04002786 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002787 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002788 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 *
2790 * Suspends a USB device that isn't in active use, conserving power.
2791 * Devices may wake out of a suspend, if anything important happens,
2792 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002793 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * to disconnect devices while they are suspended.
2795 *
David Brownell390a8c32005-09-13 19:57:27 -07002796 * This only affects the USB hardware for a device; its interfaces
2797 * (and, for hubs, child devices) must already have been suspended.
2798 *
Alan Stern624d6c02007-05-30 15:35:16 -04002799 * Selective port suspend reduces power; most suspended devices draw
2800 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2801 * All devices below the suspended port are also suspended.
2802 *
2803 * Devices leave suspend state when the host wakes them up. Some devices
2804 * also support "remote wakeup", where the device can activate the USB
2805 * tree above them to deliver data, such as a keypress or packet. In
2806 * some cases, this wakes the USB host.
2807 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 * Suspending OTG devices may trigger HNP, if that's been enabled
2809 * between a pair of dual-role devices. That will change roles, such
2810 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2811 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002812 * Devices on USB hub ports have only one "suspend" state, corresponding
2813 * to ACPI D2, "may cause the device to lose some context".
2814 * State transitions include:
2815 *
2816 * - suspend, resume ... when the VBUS power link stays live
2817 * - suspend, disconnect ... VBUS lost
2818 *
2819 * Once VBUS drop breaks the circuit, the port it's using has to go through
2820 * normal re-enumeration procedures, starting with enabling VBUS power.
2821 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2822 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2823 * timer, no SRP, no requests through sysfs.
2824 *
2825 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2826 * the root hub for their bus goes into global suspend ... so we don't
2827 * (falsely) update the device power state to say it suspended.
2828 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 * Returns 0 on success, else negative errno.
2830 */
Alan Stern65bfd292008-11-25 16:39:18 -05002831int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Alan Stern624d6c02007-05-30 15:35:16 -04002833 struct usb_hub *hub = hdev_to_hub(udev->parent);
2834 int port1 = udev->portnum;
2835 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002836
Alan Stern624d6c02007-05-30 15:35:16 -04002837 /* enable remote wakeup when appropriate; this lets the device
2838 * wake up the upstream hub (including maybe the root hub).
2839 *
2840 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2841 * we don't explicitly enable it here.
2842 */
2843 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002844 if (!hub_is_superspeed(hub->hdev)) {
2845 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2846 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2847 USB_DEVICE_REMOTE_WAKEUP, 0,
2848 NULL, 0,
2849 USB_CTRL_SET_TIMEOUT);
2850 } else {
2851 /* Assume there's only one function on the USB 3.0
2852 * device and enable remote wake for the first
2853 * interface. FIXME if the interface association
2854 * descriptor shows there's more than one function.
2855 */
2856 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2857 USB_REQ_SET_FEATURE,
2858 USB_RECIP_INTERFACE,
2859 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002860 USB_INTRF_FUNC_SUSPEND_RW |
2861 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002862 NULL, 0,
2863 USB_CTRL_SET_TIMEOUT);
2864 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002865 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002866 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2867 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002868 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002869 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002870 return status;
2871 }
Alan Stern624d6c02007-05-30 15:35:16 -04002872 }
2873
Andiry Xu65580b432011-09-23 14:19:52 -07002874 /* disable USB2 hardware LPM */
2875 if (udev->usb2_hw_lpm_enabled == 1)
2876 usb_set_usb2_hardware_lpm(udev, 0);
2877
Sarah Sharpf74631e2012-06-25 12:08:08 -07002878 if (usb_disable_ltm(udev)) {
2879 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2880 __func__);
2881 return -ENOMEM;
2882 }
Sarah Sharp83060952012-05-02 14:25:52 -07002883 if (usb_unlocked_disable_lpm(udev)) {
2884 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2885 __func__);
2886 return -ENOMEM;
2887 }
2888
Alan Stern624d6c02007-05-30 15:35:16 -04002889 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002890 if (hub_is_superspeed(hub->hdev))
2891 status = set_port_feature(hub->hdev,
2892 port1 | (USB_SS_PORT_LS_U3 << 3),
2893 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002894 else
2895 status = set_port_feature(hub->hdev, port1,
2896 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002897 if (status) {
2898 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2899 port1, status);
2900 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002901 if (udev->do_remote_wakeup)
2902 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002903 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2904 USB_DEVICE_REMOTE_WAKEUP, 0,
2905 NULL, 0,
2906 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002907
Andiry Xuc3e751e2012-05-05 00:50:10 +08002908 /* Try to enable USB2 hardware LPM again */
2909 if (udev->usb2_hw_lpm_capable == 1)
2910 usb_set_usb2_hardware_lpm(udev, 1);
2911
Sarah Sharpf74631e2012-06-25 12:08:08 -07002912 /* Try to enable USB3 LTM and LPM again */
2913 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002914 usb_unlocked_enable_lpm(udev);
2915
Alan Sterncbb33002011-06-15 16:29:16 -04002916 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002917 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002918 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002919 } else {
2920 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002921 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2922 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2923 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002924 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Sternbfd1e912012-10-19 11:03:39 -04002925 udev->port_is_suspended = 1;
Alan Stern624d6c02007-05-30 15:35:16 -04002926 msleep(10);
2927 }
Alan Sternc08512c2010-11-15 15:57:58 -05002928 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002929 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
David Brownellf3f32532005-09-22 22:37:29 -07002931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932/*
David Brownell390a8c32005-09-13 19:57:27 -07002933 * If the USB "suspend" state is in use (rather than "global suspend"),
2934 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002935 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 * hardware resume signaling is finished, either because of selective
2937 * resume (by host) or remote wakeup (by device) ... now see what changed
2938 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002939 *
2940 * If @udev->reset_resume is set then the device is reset before the
2941 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 */
Alan Stern140d8f62006-07-01 22:07:21 -04002943static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
Alan Stern54515fe2007-05-30 15:38:58 -04002945 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 u16 devstatus;
2947
2948 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002949 dev_dbg(&udev->dev, "%s\n",
2950 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
2952 /* usb ch9 identifies four variants of SUSPENDED, based on what
2953 * state the device resumes to. Linux currently won't see the
2954 * first two on the host side; they'd be inside hub_port_init()
2955 * during many timeouts, but khubd can't suspend until later.
2956 */
2957 usb_set_device_state(udev, udev->actconfig
2958 ? USB_STATE_CONFIGURED
2959 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Alan Stern54515fe2007-05-30 15:38:58 -04002961 /* 10.5.4.5 says not to reset a suspended port if the attached
2962 * device is enabled for remote wakeup. Hence the reset
2963 * operation is carried out here, after the port has been
2964 * resumed.
2965 */
2966 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002967 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002968 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 /* 10.5.4.5 says be sure devices in the tree are still there.
2971 * For now let's assume the device didn't go crazy on resume,
2972 * and device drivers will know about any resume quirks.
2973 */
Alan Stern54515fe2007-05-30 15:38:58 -04002974 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002975 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002976 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2977 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002978 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002979
2980 /* If a normal resume failed, try doing a reset-resume */
2981 if (status && !udev->reset_resume && udev->persist_enabled) {
2982 dev_dbg(&udev->dev, "retry with reset-resume\n");
2983 udev->reset_resume = 1;
2984 goto retry_reset_resume;
2985 }
Alan Stern54515fe2007-05-30 15:38:58 -04002986 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002987
Alan Stern624d6c02007-05-30 15:35:16 -04002988 if (status) {
2989 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2990 status);
2991 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002993 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 status = usb_control_msg(udev,
2995 usb_sndctrlpipe(udev, 0),
2996 USB_REQ_CLEAR_FEATURE,
2997 USB_RECIP_DEVICE,
2998 USB_DEVICE_REMOTE_WAKEUP, 0,
2999 NULL, 0,
3000 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04003001 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003002 dev_dbg(&udev->dev,
3003 "disable remote wakeup, status %d\n",
3004 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
3008 return status;
3009}
3010
Alan Stern624d6c02007-05-30 15:35:16 -04003011/*
3012 * usb_port_resume - re-activate a suspended usb device's upstream port
3013 * @udev: device to re-activate, not a root hub
3014 * Context: must be able to sleep; device not locked; pm locks held
3015 *
3016 * This will re-activate the suspended device, increasing power usage
3017 * while letting drivers communicate again with its endpoints.
3018 * USB resume explicitly guarantees that the power session between
3019 * the host and the device is the same as it was when the device
3020 * suspended.
3021 *
Alan Sternfeccc302008-03-03 15:15:59 -05003022 * If @udev->reset_resume is set then this routine won't check that the
3023 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003024 * reset @udev. The end result is that a broken power session can be
3025 * recovered and @udev will appear to persist across a loss of VBUS power.
3026 *
3027 * For example, if a host controller doesn't maintain VBUS suspend current
3028 * during a system sleep or is reset when the system wakes up, all the USB
3029 * power sessions below it will be broken. This is especially troublesome
3030 * for mass-storage devices containing mounted filesystems, since the
3031 * device will appear to have disconnected and all the memory mappings
3032 * to it will be lost. Using the USB_PERSIST facility, the device can be
3033 * made to appear as if it had not disconnected.
3034 *
Ming Lei742120c2008-06-18 22:00:29 +08003035 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003036 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003037 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3038 * quite possible for a device to remain unaltered but its media to be
3039 * changed. If the user replaces a flash memory card while the system is
3040 * asleep, he will have only himself to blame when the filesystem on the
3041 * new card is corrupted and the system crashes.
3042 *
Alan Stern624d6c02007-05-30 15:35:16 -04003043 * Returns 0 on success, else negative errno.
3044 */
Alan Stern65bfd292008-11-25 16:39:18 -05003045int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Alan Stern624d6c02007-05-30 15:35:16 -04003047 struct usb_hub *hub = hdev_to_hub(udev->parent);
3048 int port1 = udev->portnum;
3049 int status;
3050 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003051
3052 /* Skip the initial Clear-Suspend step for a remote wakeup */
3053 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003054 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003055 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3058
Alan Sternd5cbad42006-08-11 16:52:39 -04003059 set_bit(port1, hub->busy_bits);
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003062 if (hub_is_superspeed(hub->hdev))
3063 status = set_port_feature(hub->hdev,
3064 port1 | (USB_SS_PORT_LS_U0 << 3),
3065 USB_PORT_FEAT_LINK_STATE);
3066 else
3067 status = clear_port_feature(hub->hdev,
3068 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003070 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3071 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003074 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003075 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 msleep(25);
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3079 * stop resume signaling. Then finish the resume
3080 * sequence.
3081 */
Alan Sternd25450c2006-11-20 11:14:30 -05003082 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003083
Alan Sternb01b03f2008-04-28 11:06:11 -04003084 /* TRSMRCY = 10 msec */
3085 msleep(10);
3086 }
Alan Stern54515fe2007-05-30 15:38:58 -04003087
Alan Sternb01b03f2008-04-28 11:06:11 -04003088 SuspendCleared:
3089 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003090 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003091 if (hub_is_superspeed(hub->hdev)) {
3092 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3093 clear_port_feature(hub->hdev, port1,
3094 USB_PORT_FEAT_C_PORT_LINK_STATE);
3095 } else {
3096 if (portchange & USB_PORT_STAT_C_SUSPEND)
3097 clear_port_feature(hub->hdev, port1,
3098 USB_PORT_FEAT_C_SUSPEND);
3099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Alan Sternd5cbad42006-08-11 16:52:39 -04003102 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003103
Alan Sternb01b03f2008-04-28 11:06:11 -04003104 status = check_port_resume_type(udev,
3105 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003106 if (status == 0)
3107 status = finish_port_resume(udev);
3108 if (status < 0) {
3109 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3110 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003111 } else {
3112 /* Try to enable USB2 hardware LPM */
3113 if (udev->usb2_hw_lpm_capable == 1)
3114 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003115
Sarah Sharpf74631e2012-06-25 12:08:08 -07003116 /* Try to enable USB3 LTM and LPM */
3117 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003118 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003119 }
Andiry Xu65580b432011-09-23 14:19:52 -07003120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 return status;
3122}
3123
Alan Stern8808f002008-04-28 11:06:55 -04003124/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003125int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
3127 int status = 0;
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003130 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003131 status = usb_autoresume_device(udev);
3132 if (status == 0) {
3133 /* Let the drivers do their thing, then... */
3134 usb_autosuspend_device(udev);
3135 }
Alan Sternd25450c2006-11-20 11:14:30 -05003136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return status;
3138}
3139
Alan Sternd388dab2006-07-01 22:14:24 -04003140#else /* CONFIG_USB_SUSPEND */
3141
3142/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3143
Alan Stern65bfd292008-11-25 16:39:18 -05003144int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003145{
3146 return 0;
3147}
3148
Alan Sternb01b03f2008-04-28 11:06:11 -04003149/* However we may need to do a reset-resume */
3150
Alan Stern65bfd292008-11-25 16:39:18 -05003151int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003152{
Alan Sternb01b03f2008-04-28 11:06:11 -04003153 struct usb_hub *hub = hdev_to_hub(udev->parent);
3154 int port1 = udev->portnum;
3155 int status;
3156 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003157
Alan Sternb01b03f2008-04-28 11:06:11 -04003158 status = hub_port_status(hub, port1, &portstatus, &portchange);
3159 status = check_port_resume_type(udev,
3160 hub, port1, status, portchange, portstatus);
3161
3162 if (status) {
3163 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3164 hub_port_logical_disconnect(hub, port1);
3165 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003166 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003167 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003168 }
3169 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003170}
3171
Alan Sternd388dab2006-07-01 22:14:24 -04003172#endif
3173
Ming Leie6f30de2012-10-24 11:59:24 +08003174static int check_ports_changed(struct usb_hub *hub)
3175{
3176 int port1;
3177
3178 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3179 u16 portstatus, portchange;
3180 int status;
3181
3182 status = hub_port_status(hub, port1, &portstatus, &portchange);
3183 if (!status && portchange)
3184 return 1;
3185 }
3186 return 0;
3187}
3188
David Brownelldb690872005-09-13 19:56:33 -07003189static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
3191 struct usb_hub *hub = usb_get_intfdata (intf);
3192 struct usb_device *hdev = hub->hdev;
3193 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003194 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Alan Sterncbb33002011-06-15 16:29:16 -04003196 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3198 struct usb_device *udev;
3199
Lan Tianyuff823c72012-09-05 13:44:32 +08003200 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003201 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003202 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003203 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003204 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 }
Ming Leie6f30de2012-10-24 11:59:24 +08003207
3208 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3209 /* check if there are changes pending on hub ports */
3210 if (check_ports_changed(hub)) {
3211 if (PMSG_IS_AUTO(msg))
3212 return -EBUSY;
3213 pm_wakeup_event(&hdev->dev, 2000);
3214 }
3215 }
3216
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003217 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3218 /* Enable hub to send remote wakeup for all ports. */
3219 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3220 status = set_port_feature(hdev,
3221 port1 |
3222 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3223 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3224 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3225 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3226 }
3227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Harvey Harrison441b62c2008-03-03 16:08:34 -08003229 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003230
David Brownellc9f89fa2005-09-13 19:57:04 -07003231 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003232 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236static int hub_resume(struct usb_interface *intf)
3237{
Alan Stern5e6effa2008-03-03 15:15:51 -05003238 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Alan Stern5e6effa2008-03-03 15:15:51 -05003240 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003241 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return 0;
3243}
3244
Alan Sternb41a60e2007-05-30 15:39:33 -04003245static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003246{
Alan Sternb41a60e2007-05-30 15:39:33 -04003247 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003248
Alan Stern5e6effa2008-03-03 15:15:51 -05003249 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003250 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003251 return 0;
3252}
3253
Alan Stern54515fe2007-05-30 15:38:58 -04003254/**
3255 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3256 * @rhdev: struct usb_device for the root hub
3257 *
3258 * The USB host controller driver calls this function when its root hub
3259 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003260 * has been reset. The routine marks @rhdev as having lost power.
3261 * When the hub driver is resumed it will take notice and carry out
3262 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3263 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003264 */
3265void usb_root_hub_lost_power(struct usb_device *rhdev)
3266{
3267 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3268 rhdev->reset_resume = 1;
3269}
3270EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3271
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003272static const char * const usb3_lpm_names[] = {
3273 "U0",
3274 "U1",
3275 "U2",
3276 "U3",
3277};
3278
3279/*
3280 * Send a Set SEL control transfer to the device, prior to enabling
3281 * device-initiated U1 or U2. This lets the device know the exit latencies from
3282 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3283 * packet from the host.
3284 *
3285 * This function will fail if the SEL or PEL values for udev are greater than
3286 * the maximum allowed values for the link state to be enabled.
3287 */
3288static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3289{
3290 struct usb_set_sel_req *sel_values;
3291 unsigned long long u1_sel;
3292 unsigned long long u1_pel;
3293 unsigned long long u2_sel;
3294 unsigned long long u2_pel;
3295 int ret;
3296
3297 /* Convert SEL and PEL stored in ns to us */
3298 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3299 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3300 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3301 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3302
3303 /*
3304 * Make sure that the calculated SEL and PEL values for the link
3305 * state we're enabling aren't bigger than the max SEL/PEL
3306 * value that will fit in the SET SEL control transfer.
3307 * Otherwise the device would get an incorrect idea of the exit
3308 * latency for the link state, and could start a device-initiated
3309 * U1/U2 when the exit latencies are too high.
3310 */
3311 if ((state == USB3_LPM_U1 &&
3312 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3313 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3314 (state == USB3_LPM_U2 &&
3315 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3316 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003317 dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n",
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003318 usb3_lpm_names[state], u1_sel, u1_pel);
3319 return -EINVAL;
3320 }
3321
3322 /*
3323 * If we're enabling device-initiated LPM for one link state,
3324 * but the other link state has a too high SEL or PEL value,
3325 * just set those values to the max in the Set SEL request.
3326 */
3327 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3328 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3329
3330 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3331 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3332
3333 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3334 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3335
3336 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3337 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3338
3339 /*
3340 * usb_enable_lpm() can be called as part of a failed device reset,
3341 * which may be initiated by an error path of a mass storage driver.
3342 * Therefore, use GFP_NOIO.
3343 */
3344 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3345 if (!sel_values)
3346 return -ENOMEM;
3347
3348 sel_values->u1_sel = u1_sel;
3349 sel_values->u1_pel = u1_pel;
3350 sel_values->u2_sel = cpu_to_le16(u2_sel);
3351 sel_values->u2_pel = cpu_to_le16(u2_pel);
3352
3353 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3354 USB_REQ_SET_SEL,
3355 USB_RECIP_DEVICE,
3356 0, 0,
3357 sel_values, sizeof *(sel_values),
3358 USB_CTRL_SET_TIMEOUT);
3359 kfree(sel_values);
3360 return ret;
3361}
3362
3363/*
3364 * Enable or disable device-initiated U1 or U2 transitions.
3365 */
3366static int usb_set_device_initiated_lpm(struct usb_device *udev,
3367 enum usb3_link_state state, bool enable)
3368{
3369 int ret;
3370 int feature;
3371
3372 switch (state) {
3373 case USB3_LPM_U1:
3374 feature = USB_DEVICE_U1_ENABLE;
3375 break;
3376 case USB3_LPM_U2:
3377 feature = USB_DEVICE_U2_ENABLE;
3378 break;
3379 default:
3380 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3381 __func__, enable ? "enable" : "disable");
3382 return -EINVAL;
3383 }
3384
3385 if (udev->state != USB_STATE_CONFIGURED) {
3386 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3387 "for unconfigured device.\n",
3388 __func__, enable ? "enable" : "disable",
3389 usb3_lpm_names[state]);
3390 return 0;
3391 }
3392
3393 if (enable) {
3394 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003395 * Now send the control transfer to enable device-initiated LPM
3396 * for either U1 or U2.
3397 */
3398 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3399 USB_REQ_SET_FEATURE,
3400 USB_RECIP_DEVICE,
3401 feature,
3402 0, NULL, 0,
3403 USB_CTRL_SET_TIMEOUT);
3404 } else {
3405 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3406 USB_REQ_CLEAR_FEATURE,
3407 USB_RECIP_DEVICE,
3408 feature,
3409 0, NULL, 0,
3410 USB_CTRL_SET_TIMEOUT);
3411 }
3412 if (ret < 0) {
3413 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3414 enable ? "Enable" : "Disable",
3415 usb3_lpm_names[state]);
3416 return -EBUSY;
3417 }
3418 return 0;
3419}
3420
3421static int usb_set_lpm_timeout(struct usb_device *udev,
3422 enum usb3_link_state state, int timeout)
3423{
3424 int ret;
3425 int feature;
3426
3427 switch (state) {
3428 case USB3_LPM_U1:
3429 feature = USB_PORT_FEAT_U1_TIMEOUT;
3430 break;
3431 case USB3_LPM_U2:
3432 feature = USB_PORT_FEAT_U2_TIMEOUT;
3433 break;
3434 default:
3435 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3436 __func__);
3437 return -EINVAL;
3438 }
3439
3440 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3441 timeout != USB3_LPM_DEVICE_INITIATED) {
3442 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3443 "which is a reserved value.\n",
3444 usb3_lpm_names[state], timeout);
3445 return -EINVAL;
3446 }
3447
3448 ret = set_port_feature(udev->parent,
3449 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3450 feature);
3451 if (ret < 0) {
3452 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3453 "error code %i\n", usb3_lpm_names[state],
3454 timeout, ret);
3455 return -EBUSY;
3456 }
3457 if (state == USB3_LPM_U1)
3458 udev->u1_params.timeout = timeout;
3459 else
3460 udev->u2_params.timeout = timeout;
3461 return 0;
3462}
3463
3464/*
3465 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3466 * U1/U2 entry.
3467 *
3468 * We will attempt to enable U1 or U2, but there are no guarantees that the
3469 * control transfers to set the hub timeout or enable device-initiated U1/U2
3470 * will be successful.
3471 *
3472 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3473 * driver know about it. If that call fails, it should be harmless, and just
3474 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3475 */
3476static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3477 enum usb3_link_state state)
3478{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003479 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003480 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3481 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3482
3483 /* If the device says it doesn't have *any* exit latency to come out of
3484 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3485 * state.
3486 */
3487 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3488 (state == USB3_LPM_U2 && u2_mel == 0))
3489 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003490
Sarah Sharp65a95b72012-10-05 10:32:07 -07003491 /*
3492 * First, let the device know about the exit latencies
3493 * associated with the link state we're about to enable.
3494 */
3495 ret = usb_req_set_sel(udev, state);
3496 if (ret < 0) {
3497 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3498 usb3_lpm_names[state]);
3499 return;
3500 }
3501
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003502 /* We allow the host controller to set the U1/U2 timeout internally
3503 * first, so that it can change its schedule to account for the
3504 * additional latency to send data to a device in a lower power
3505 * link state.
3506 */
3507 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3508
3509 /* xHCI host controller doesn't want to enable this LPM state. */
3510 if (timeout == 0)
3511 return;
3512
3513 if (timeout < 0) {
3514 dev_warn(&udev->dev, "Could not enable %s link state, "
3515 "xHCI error %i.\n", usb3_lpm_names[state],
3516 timeout);
3517 return;
3518 }
3519
3520 if (usb_set_lpm_timeout(udev, state, timeout))
3521 /* If we can't set the parent hub U1/U2 timeout,
3522 * device-initiated LPM won't be allowed either, so let the xHCI
3523 * host know that this link state won't be enabled.
3524 */
3525 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3526
3527 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3528 else if (udev->actconfig)
3529 usb_set_device_initiated_lpm(udev, state, true);
3530
3531}
3532
3533/*
3534 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3535 * U1/U2 entry.
3536 *
3537 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3538 * If zero is returned, the parent will not allow the link to go into U1/U2.
3539 *
3540 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3541 * it won't have an effect on the bus link state because the parent hub will
3542 * still disallow device-initiated U1/U2 entry.
3543 *
3544 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3545 * possible. The result will be slightly more bus bandwidth will be taken up
3546 * (to account for U1/U2 exit latency), but it should be harmless.
3547 */
3548static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3549 enum usb3_link_state state)
3550{
3551 int feature;
3552
3553 switch (state) {
3554 case USB3_LPM_U1:
3555 feature = USB_PORT_FEAT_U1_TIMEOUT;
3556 break;
3557 case USB3_LPM_U2:
3558 feature = USB_PORT_FEAT_U2_TIMEOUT;
3559 break;
3560 default:
3561 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3562 __func__);
3563 return -EINVAL;
3564 }
3565
3566 if (usb_set_lpm_timeout(udev, state, 0))
3567 return -EBUSY;
3568
3569 usb_set_device_initiated_lpm(udev, state, false);
3570
3571 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3572 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3573 "bus schedule bandwidth may be impacted.\n",
3574 usb3_lpm_names[state]);
3575 return 0;
3576}
3577
3578/*
3579 * Disable hub-initiated and device-initiated U1 and U2 entry.
3580 * Caller must own the bandwidth_mutex.
3581 *
3582 * This will call usb_enable_lpm() on failure, which will decrement
3583 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3584 */
3585int usb_disable_lpm(struct usb_device *udev)
3586{
3587 struct usb_hcd *hcd;
3588
3589 if (!udev || !udev->parent ||
3590 udev->speed != USB_SPEED_SUPER ||
3591 !udev->lpm_capable)
3592 return 0;
3593
3594 hcd = bus_to_hcd(udev->bus);
3595 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3596 return 0;
3597
3598 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003599 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003600 return 0;
3601
3602 /* If LPM is enabled, attempt to disable it. */
3603 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3604 goto enable_lpm;
3605 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3606 goto enable_lpm;
3607
3608 return 0;
3609
3610enable_lpm:
3611 usb_enable_lpm(udev);
3612 return -EBUSY;
3613}
3614EXPORT_SYMBOL_GPL(usb_disable_lpm);
3615
3616/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3617int usb_unlocked_disable_lpm(struct usb_device *udev)
3618{
3619 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3620 int ret;
3621
3622 if (!hcd)
3623 return -EINVAL;
3624
3625 mutex_lock(hcd->bandwidth_mutex);
3626 ret = usb_disable_lpm(udev);
3627 mutex_unlock(hcd->bandwidth_mutex);
3628
3629 return ret;
3630}
3631EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3632
3633/*
3634 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3635 * xHCI host policy may prevent U1 or U2 from being enabled.
3636 *
3637 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3638 * until the lpm_disable_count drops to zero. Caller must own the
3639 * bandwidth_mutex.
3640 */
3641void usb_enable_lpm(struct usb_device *udev)
3642{
3643 struct usb_hcd *hcd;
3644
3645 if (!udev || !udev->parent ||
3646 udev->speed != USB_SPEED_SUPER ||
3647 !udev->lpm_capable)
3648 return;
3649
3650 udev->lpm_disable_count--;
3651 hcd = bus_to_hcd(udev->bus);
3652 /* Double check that we can both enable and disable LPM.
3653 * Device must be configured to accept set feature U1/U2 timeout.
3654 */
3655 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3656 !hcd->driver->disable_usb3_lpm_timeout)
3657 return;
3658
3659 if (udev->lpm_disable_count > 0)
3660 return;
3661
3662 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3663 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3664}
3665EXPORT_SYMBOL_GPL(usb_enable_lpm);
3666
3667/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3668void usb_unlocked_enable_lpm(struct usb_device *udev)
3669{
3670 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3671
3672 if (!hcd)
3673 return;
3674
3675 mutex_lock(hcd->bandwidth_mutex);
3676 usb_enable_lpm(udev);
3677 mutex_unlock(hcd->bandwidth_mutex);
3678}
3679EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3680
3681
Alan Sternd388dab2006-07-01 22:14:24 -04003682#else /* CONFIG_PM */
3683
Alan Sternf07600c2007-05-30 15:38:16 -04003684#define hub_suspend NULL
3685#define hub_resume NULL
3686#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003687
3688int usb_disable_lpm(struct usb_device *udev)
3689{
3690 return 0;
3691}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003692EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003693
3694void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003695EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003696
3697int usb_unlocked_disable_lpm(struct usb_device *udev)
3698{
3699 return 0;
3700}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003701EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003702
3703void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003704EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003705
3706int usb_disable_ltm(struct usb_device *udev)
3707{
3708 return 0;
3709}
3710EXPORT_SYMBOL_GPL(usb_disable_ltm);
3711
3712void usb_enable_ltm(struct usb_device *udev) { }
3713EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003714#endif
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3718 *
3719 * Between connect detection and reset signaling there must be a delay
3720 * of 100ms at least for debounce and power-settling. The corresponding
3721 * timer shall restart whenever the downstream port detects a disconnect.
3722 *
3723 * Apparently there are some bluetooth and irda-dongles and a number of
3724 * low-speed devices for which this debounce period may last over a second.
3725 * Not covered by the spec - but easy to deal with.
3726 *
3727 * This implementation uses a 1500ms total debounce timeout; if the
3728 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3729 * every 25ms for transient disconnects. When the port status has been
3730 * unchanged for 100ms it returns the port status.
3731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732static int hub_port_debounce(struct usb_hub *hub, int port1)
3733{
3734 int ret;
3735 int total_time, stable_time = 0;
3736 u16 portchange, portstatus;
3737 unsigned connection = 0xffff;
3738
3739 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3740 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3741 if (ret < 0)
3742 return ret;
3743
3744 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3745 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3746 stable_time += HUB_DEBOUNCE_STEP;
3747 if (stable_time >= HUB_DEBOUNCE_STABLE)
3748 break;
3749 } else {
3750 stable_time = 0;
3751 connection = portstatus & USB_PORT_STAT_CONNECTION;
3752 }
3753
3754 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3755 clear_port_feature(hub->hdev, port1,
3756 USB_PORT_FEAT_C_CONNECTION);
3757 }
3758
3759 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3760 break;
3761 msleep(HUB_DEBOUNCE_STEP);
3762 }
3763
3764 dev_dbg (hub->intfdev,
3765 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3766 port1, total_time, stable_time, portstatus);
3767
3768 if (stable_time < HUB_DEBOUNCE_STABLE)
3769 return -ETIMEDOUT;
3770 return portstatus;
3771}
3772
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003773void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
Alan Sternddeac4e72009-01-15 17:03:33 -05003775 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3776 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003777 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003779EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
3781#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3782#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3783
Alan Stern4326ed02007-07-30 17:08:43 -04003784static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
3786 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003787 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
Sarah Sharpc6515272009-04-27 19:57:26 -07003789 /*
3790 * The host controller will choose the device address,
3791 * instead of the core having chosen it earlier
3792 */
3793 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return -EINVAL;
3795 if (udev->state == USB_STATE_ADDRESS)
3796 return 0;
3797 if (udev->state != USB_STATE_DEFAULT)
3798 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003799 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003800 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003801 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003802 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3803 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3804 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003806 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003807 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003809 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 }
3811 return retval;
3812}
3813
3814/* Reset device, (re)assign address, get device descriptor.
3815 * Device connection must be stable, no more debouncing needed.
3816 * Returns device in USB_STATE_ADDRESS, except on error.
3817 *
3818 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003819 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 * newly detected device that is not accessible through any global
3821 * pointers, it's not necessary to lock the device.
3822 */
3823static int
3824hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3825 int retry_counter)
3826{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003827 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
3829 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003830 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 int i, j, retval;
3832 unsigned delay = HUB_SHORT_RESET_TIME;
3833 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003834 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003835 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
3837 /* root hub ports have a slightly longer reset period
3838 * (from USB 2.0 spec, section 7.1.7.5)
3839 */
3840 if (!hdev->parent) {
3841 delay = HUB_ROOT_RESET_TIME;
3842 if (port1 == hdev->bus->otg_port)
3843 hdev->bus->b_hnp_enable = 0;
3844 }
3845
3846 /* Some low speed devices have problems with the quick delay, so */
3847 /* be a bit pessimistic with those devices. RHbug #23670 */
3848 if (oldspeed == USB_SPEED_LOW)
3849 delay = HUB_LONG_RESET_TIME;
3850
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003851 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Luben Tuikov07194ab2011-02-11 11:33:10 -08003853 /* Reset the device; full speed may morph to high speed */
3854 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003855 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003856 if (retval < 0) /* error or disconnect */
3857 goto fail;
3858 /* success, speed is known */
3859
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 retval = -ENODEV;
3861
3862 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3863 dev_dbg(&udev->dev, "device reset changed speed!\n");
3864 goto fail;
3865 }
3866 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3869 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003870 * For Wireless USB devices, ep0 max packet is always 512 (tho
3871 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 */
3873 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003874 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003875 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003876 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003877 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003879 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 break;
3881 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3882 /* to determine the ep0 maxpacket size, try to read
3883 * the device descriptor to get bMaxPacketSize0 and
3884 * then correct our initial guess.
3885 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003886 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 break;
3888 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003889 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 break;
3891 default:
3892 goto fail;
3893 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003894
3895 if (udev->speed == USB_SPEED_WIRELESS)
3896 speed = "variable speed Wireless";
3897 else
3898 speed = usb_speed_string(udev->speed);
3899
Sarah Sharpc6515272009-04-27 19:57:26 -07003900 if (udev->speed != USB_SPEED_SUPER)
3901 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003902 "%s %s USB device number %d using %s\n",
3903 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003904 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
3906 /* Set up TT records, if needed */
3907 if (hdev->tt) {
3908 udev->tt = hdev->tt;
3909 udev->ttport = hdev->ttport;
3910 } else if (udev->speed != USB_SPEED_HIGH
3911 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003912 if (!hub->tt.hub) {
3913 dev_err(&udev->dev, "parent hub has no TT\n");
3914 retval = -EINVAL;
3915 goto fail;
3916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 udev->tt = &hub->tt;
3918 udev->ttport = port1;
3919 }
3920
3921 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3922 * Because device hardware and firmware is sometimes buggy in
3923 * this area, and this is how Linux has done it for ages.
3924 * Change it cautiously.
3925 *
3926 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3927 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3928 * so it may help with some non-standards-compliant devices.
3929 * Otherwise we start with SET_ADDRESS and then try to read the
3930 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3931 * value.
3932 */
3933 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003934 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 struct usb_device_descriptor *buf;
3936 int r = 0;
3937
3938#define GET_DESCRIPTOR_BUFSIZE 64
3939 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3940 if (!buf) {
3941 retval = -ENOMEM;
3942 continue;
3943 }
3944
Alan Sternb89ee192007-05-11 10:19:04 -04003945 /* Retry on all errors; some devices are flakey.
3946 * 255 is for WUSB devices, we actually need to use
3947 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 */
3949 for (j = 0; j < 3; ++j) {
3950 buf->bMaxPacketSize0 = 0;
3951 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3952 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3953 USB_DT_DEVICE << 8, 0,
3954 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003955 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003957 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 if (buf->bDescriptorType ==
3959 USB_DT_DEVICE) {
3960 r = 0;
3961 break;
3962 }
3963 /* FALL THROUGH */
3964 default:
3965 if (r == 0)
3966 r = -EPROTO;
3967 break;
3968 }
3969 if (r == 0)
3970 break;
3971 }
3972 udev->descriptor.bMaxPacketSize0 =
3973 buf->bMaxPacketSize0;
3974 kfree(buf);
3975
Andiry Xu75d7cf72011-09-13 16:41:11 -07003976 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 if (retval < 0) /* error or disconnect */
3978 goto fail;
3979 if (oldspeed != udev->speed) {
3980 dev_dbg(&udev->dev,
3981 "device reset changed speed!\n");
3982 retval = -ENODEV;
3983 goto fail;
3984 }
3985 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003986 dev_err(&udev->dev,
3987 "device descriptor read/64, error %d\n",
3988 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 retval = -EMSGSIZE;
3990 continue;
3991 }
3992#undef GET_DESCRIPTOR_BUFSIZE
3993 }
3994
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003995 /*
3996 * If device is WUSB, we already assigned an
3997 * unauthorized address in the Connect Ack sequence;
3998 * authorization will assign the final address.
3999 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004000 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004001 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4002 retval = hub_set_address(udev, devnum);
4003 if (retval >= 0)
4004 break;
4005 msleep(200);
4006 }
4007 if (retval < 0) {
4008 dev_err(&udev->dev,
4009 "device not accepting address %d, error %d\n",
4010 devnum, retval);
4011 goto fail;
4012 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004013 if (udev->speed == USB_SPEED_SUPER) {
4014 devnum = udev->devnum;
4015 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004016 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004017 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004018 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004019 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004020
4021 /* cope with hardware quirkiness:
4022 * - let SET_ADDRESS settle, some device hardware wants it
4023 * - read ep0 maxpacket even for high and low speed,
4024 */
4025 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07004026 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
4030 retval = usb_get_device_descriptor(udev, 8);
4031 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004032 dev_err(&udev->dev,
4033 "device descriptor read/8, error %d\n",
4034 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 if (retval >= 0)
4036 retval = -EMSGSIZE;
4037 } else {
4038 retval = 0;
4039 break;
4040 }
4041 }
4042 if (retval)
4043 goto fail;
4044
Elric Fud8aec3d2012-03-26 21:16:02 +08004045 /*
4046 * Some superspeed devices have finished the link training process
4047 * and attached to a superspeed hub port, but the device descriptor
4048 * got from those devices show they aren't superspeed devices. Warm
4049 * reset the port attached by the devices can fix them.
4050 */
4051 if ((udev->speed == USB_SPEED_SUPER) &&
4052 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4053 dev_err(&udev->dev, "got a wrong device descriptor, "
4054 "warm reset device\n");
4055 hub_port_reset(hub, port1, udev,
4056 HUB_BH_RESET_TIME, true);
4057 retval = -EINVAL;
4058 goto fail;
4059 }
4060
Sarah Sharp6b403b02009-04-27 19:54:10 -07004061 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4062 udev->speed == USB_SPEED_SUPER)
4063 i = 512;
4064 else
4065 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004066 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004067 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004069 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 retval = -EMSGSIZE;
4071 goto fail;
4072 }
Alan Stern56626a72010-10-14 15:25:21 -04004073 if (udev->speed == USB_SPEED_FULL)
4074 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4075 else
4076 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004078 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 }
4080
4081 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4082 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004083 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4084 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 if (retval >= 0)
4086 retval = -ENOMSG;
4087 goto fail;
4088 }
4089
Andiry Xu1ff4df52011-09-23 14:19:48 -07004090 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4091 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004092 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004093 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004094 usb_set_lpm_parameters(udev);
4095 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004096 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004097
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004099 /* notify HCD that we have a device connected and addressed */
4100 if (hcd->driver->update_device)
4101 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004103 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004105 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004106 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004107 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 return retval;
4109}
4110
4111static void
4112check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4113{
4114 struct usb_qualifier_descriptor *qual;
4115 int status;
4116
Christoph Lametere94b1762006-12-06 20:33:17 -08004117 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (qual == NULL)
4119 return;
4120
4121 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4122 qual, sizeof *qual);
4123 if (status == sizeof *qual) {
4124 dev_info(&udev->dev, "not running at top speed; "
4125 "connect to a high speed hub\n");
4126 /* hub LEDs are probably harder to miss than syslog */
4127 if (hub->has_indicators) {
4128 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004129 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
4131 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004132 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133}
4134
4135static unsigned
4136hub_power_remaining (struct usb_hub *hub)
4137{
4138 struct usb_device *hdev = hub->hdev;
4139 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004140 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Alan Stern55c52712005-11-23 12:03:12 -05004142 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 return 0;
4144
Alan Stern55c52712005-11-23 12:03:12 -05004145 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4146 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08004147 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004148 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
4150 if (!udev)
4151 continue;
4152
Alan Stern55c52712005-11-23 12:03:12 -05004153 /* Unconfigured devices may not use more than 100mA,
4154 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004156 delta = udev->actconfig->desc.bMaxPower * 2;
4157 else if (port1 != udev->bus->otg_port || hdev->parent)
4158 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 else
Alan Stern55c52712005-11-23 12:03:12 -05004160 delta = 8;
4161 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004162 dev_warn(&udev->dev,
4163 "%dmA is over %umA budget for port %d!\n",
4164 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 remaining -= delta;
4166 }
4167 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004168 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4169 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 remaining = 0;
4171 }
4172 return remaining;
4173}
4174
4175/* Handle physical or logical connection change events.
4176 * This routine is called when:
4177 * a port connection-change occurs;
4178 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004179 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 * a firmware download)
4181 * caller already locked the hub
4182 */
4183static void hub_port_connect_change(struct usb_hub *hub, int port1,
4184 u16 portstatus, u16 portchange)
4185{
4186 struct usb_device *hdev = hub->hdev;
4187 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304188 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004189 unsigned wHubCharacteristics =
4190 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004191 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 dev_dbg (hub_dev,
4195 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004196 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
4198 if (hub->has_indicators) {
4199 set_port_led(hub, port1, HUB_LED_AUTO);
4200 hub->indicator[port1-1] = INDICATOR_AUTO;
4201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
4203#ifdef CONFIG_USB_OTG
4204 /* during HNP, don't repeat the debounce */
4205 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004206 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4207 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208#endif
4209
Alan Stern8808f002008-04-28 11:06:55 -04004210 /* Try to resuscitate an existing device */
Lan Tianyuff823c72012-09-05 13:44:32 +08004211 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004212 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4213 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004214 usb_lock_device(udev);
4215 if (portstatus & USB_PORT_STAT_ENABLE) {
4216 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004217
4218#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004219 } else if (udev->state == USB_STATE_SUSPENDED &&
4220 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004221 /* For a suspended device, treat this as a
4222 * remote wakeup event.
4223 */
Alan Stern0534d462010-01-08 12:56:30 -05004224 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004225#endif
4226
4227 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004228 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004229 }
4230 usb_unlock_device(udev);
4231
4232 if (status == 0) {
4233 clear_bit(port1, hub->change_bits);
4234 return;
4235 }
4236 }
4237
Alan Stern24618b02008-04-28 11:06:28 -04004238 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004239 if (udev)
Lan Tianyuff823c72012-09-05 13:44:32 +08004240 usb_disconnect(&hub->ports[port1 - 1]->child);
Alan Stern24618b02008-04-28 11:06:28 -04004241 clear_bit(port1, hub->change_bits);
4242
Alan Stern253e0572009-10-27 15:20:13 -04004243 /* We can forget about a "removed" device when there's a physical
4244 * disconnect or the connect status changes.
4245 */
4246 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4247 (portchange & USB_PORT_STAT_C_CONNECTION))
4248 clear_bit(port1, hub->removed_bits);
4249
Alan Stern5257d972008-09-22 14:43:08 -04004250 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4251 USB_PORT_STAT_C_ENABLE)) {
4252 status = hub_port_debounce(hub, port1);
4253 if (status < 0) {
4254 if (printk_ratelimit())
4255 dev_err(hub_dev, "connect-debounce failed, "
4256 "port %d disabled\n", port1);
4257 portstatus &= ~USB_PORT_STAT_CONNECTION;
4258 } else {
4259 portstatus = status;
4260 }
4261 }
4262
Richard Zhao925aa462012-07-11 11:09:28 +08004263 if (hcd->phy && !hdev->parent) {
4264 if (portstatus & USB_PORT_STAT_CONNECTION)
4265 usb_phy_notify_connect(hcd->phy, port1);
4266 else
4267 usb_phy_notify_disconnect(hcd->phy, port1);
4268 }
4269
Alan Stern253e0572009-10-27 15:20:13 -04004270 /* Return now if debouncing failed or nothing is connected or
4271 * the device was "removed".
4272 */
4273 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4274 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004277 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004278 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004280
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 if (portstatus & USB_PORT_STAT_ENABLE)
4282 goto done;
4283 return;
4284 }
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
4288 /* reallocate for each attempt, since references
4289 * to the previous one can escape in various ways
4290 */
4291 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4292 if (!udev) {
4293 dev_err (hub_dev,
4294 "couldn't allocate port %d usb_device\n",
4295 port1);
4296 goto done;
4297 }
4298
4299 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004300 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004301 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004302 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004303
Sarah Sharp131dec32010-12-06 21:00:19 -08004304 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4305 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004306 udev->speed = USB_SPEED_SUPER;
4307 else
4308 udev->speed = USB_SPEED_UNKNOWN;
4309
Alan Stern3b29b682011-02-22 09:53:41 -05004310 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004311 if (udev->devnum <= 0) {
4312 status = -ENOTCONN; /* Don't retry */
4313 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004314 }
4315
Sarah Sharpe7b77172009-04-27 19:54:26 -07004316 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 status = hub_port_init(hub, udev, port1, i);
4318 if (status < 0)
4319 goto loop;
4320
Phil Dibowitz93362a82010-07-22 00:05:01 +02004321 usb_detect_quirks(udev);
4322 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4323 msleep(1000);
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 /* consecutive bus-powered hubs aren't reliable; they can
4326 * violate the voltage drop budget. if the new child has
4327 * a "powered" LED, users should notice we didn't enable it
4328 * (without reading syslog), even without per-port LEDs
4329 * on the parent.
4330 */
4331 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004332 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 u16 devstat;
4334
4335 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4336 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004337 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 dev_dbg(&udev->dev, "get status %d ?\n", status);
4339 goto loop_disable;
4340 }
Alan Stern55c52712005-11-23 12:03:12 -05004341 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4343 dev_err(&udev->dev,
4344 "can't connect bus-powered hub "
4345 "to this port\n");
4346 if (hub->has_indicators) {
4347 hub->indicator[port1-1] =
4348 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004349 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 }
4351 status = -ENOTCONN; /* Don't retry */
4352 goto loop_disable;
4353 }
4354 }
4355
4356 /* check for devices running slower than they could */
4357 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4358 && udev->speed == USB_SPEED_FULL
4359 && highspeed_hubs != 0)
4360 check_highspeed (hub, udev, port1);
4361
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004362 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 * udev becomes globally accessible, although presumably
4364 * no one will look at it until hdev is unlocked.
4365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 status = 0;
4367
4368 /* We mustn't add new devices if the parent hub has
4369 * been disconnected; we would race with the
4370 * recursively_mark_NOTATTACHED() routine.
4371 */
4372 spin_lock_irq(&device_state_lock);
4373 if (hdev->state == USB_STATE_NOTATTACHED)
4374 status = -ENOTCONN;
4375 else
Lan Tianyuff823c72012-09-05 13:44:32 +08004376 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 spin_unlock_irq(&device_state_lock);
4378
4379 /* Run it through the hoops (find a driver, etc) */
4380 if (!status) {
4381 status = usb_new_device(udev);
4382 if (status) {
4383 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c72012-09-05 13:44:32 +08004384 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 spin_unlock_irq(&device_state_lock);
4386 }
4387 }
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 if (status)
4390 goto loop_disable;
4391
4392 status = hub_power_remaining(hub);
4393 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004394 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
4396 return;
4397
4398loop_disable:
4399 hub_port_disable(hub, port1, 1);
4400loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004401 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004402 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004403 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004405 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 break;
4407 }
Alan Stern3a311552008-05-20 16:58:29 -04004408 if (hub->hdev->parent ||
4409 !hcd->driver->port_handed_over ||
4410 !(hcd->driver->port_handed_over)(hcd, port1))
4411 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4412 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
4414done:
4415 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304416 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4417 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
Sarah Sharp714b07b2012-01-24 13:53:18 -08004420/* Returns 1 if there was a remote wakeup and a connect status change. */
4421static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004422 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004423{
4424 struct usb_device *hdev;
4425 struct usb_device *udev;
4426 int connect_change = 0;
4427 int ret;
4428
4429 hdev = hub->hdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08004430 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004431 if (!hub_is_superspeed(hdev)) {
4432 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4433 return 0;
4434 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4435 } else {
4436 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004437 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4438 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004439 return 0;
4440 }
4441
Sarah Sharp714b07b2012-01-24 13:53:18 -08004442 if (udev) {
4443 /* TRSMRCY = 10 msec */
4444 msleep(10);
4445
4446 usb_lock_device(udev);
4447 ret = usb_remote_wakeup(udev);
4448 usb_unlock_device(udev);
4449 if (ret < 0)
4450 connect_change = 1;
4451 } else {
4452 ret = -ENODEV;
4453 hub_port_disable(hub, port, 1);
4454 }
4455 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4456 port, ret);
4457 return connect_change;
4458}
4459
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460static void hub_events(void)
4461{
4462 struct list_head *tmp;
4463 struct usb_device *hdev;
4464 struct usb_interface *intf;
4465 struct usb_hub *hub;
4466 struct device *hub_dev;
4467 u16 hubstatus;
4468 u16 hubchange;
4469 u16 portstatus;
4470 u16 portchange;
4471 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004472 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
4474 /*
4475 * We restart the list every time to avoid a deadlock with
4476 * deleting hubs downstream from this one. This should be
4477 * safe since we delete the hub from the event list.
4478 * Not the most efficient, but avoids deadlocks.
4479 */
4480 while (1) {
4481
4482 /* Grab the first entry at the beginning of the list */
4483 spin_lock_irq(&hub_event_lock);
4484 if (list_empty(&hub_event_list)) {
4485 spin_unlock_irq(&hub_event_lock);
4486 break;
4487 }
4488
4489 tmp = hub_event_list.next;
4490 list_del_init(tmp);
4491
4492 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004493 kref_get(&hub->kref);
4494 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Alan Sterne8054852007-05-04 11:55:11 -04004496 hdev = hub->hdev;
4497 hub_dev = hub->intfdev;
4498 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004499 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 hdev->state, hub->descriptor
4501 ? hub->descriptor->bNbrPorts
4502 : 0,
4503 /* NOTE: expects max 15 ports... */
4504 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004505 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 /* Lock the device, then check to see if we were
4508 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004509 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004510 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004511 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
4513 /* If the hub has died, clean up after it */
4514 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004515 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004516 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 goto loop;
4518 }
4519
Alan Stern40f122f2006-11-09 14:44:33 -05004520 /* Autoresume */
4521 ret = usb_autopm_get_interface(intf);
4522 if (ret) {
4523 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004525 }
4526
4527 /* If this is an inactive hub, do nothing */
4528 if (hub->quiescing)
4529 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
4531 if (hub->error) {
4532 dev_dbg (hub_dev, "resetting for error %d\n",
4533 hub->error);
4534
Ming Lei742120c2008-06-18 22:00:29 +08004535 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 if (ret) {
4537 dev_dbg (hub_dev,
4538 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004539 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 }
4541
4542 hub->nerrors = 0;
4543 hub->error = 0;
4544 }
4545
4546 /* deal with port status changes */
4547 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4548 if (test_bit(i, hub->busy_bits))
4549 continue;
4550 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004551 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004553 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 continue;
4555
4556 ret = hub_port_status(hub, i,
4557 &portstatus, &portchange);
4558 if (ret < 0)
4559 continue;
4560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4562 clear_port_feature(hdev, i,
4563 USB_PORT_FEAT_C_CONNECTION);
4564 connect_change = 1;
4565 }
4566
4567 if (portchange & USB_PORT_STAT_C_ENABLE) {
4568 if (!connect_change)
4569 dev_dbg (hub_dev,
4570 "port %d enable change, "
4571 "status %08x\n",
4572 i, portstatus);
4573 clear_port_feature(hdev, i,
4574 USB_PORT_FEAT_C_ENABLE);
4575
4576 /*
4577 * EM interference sometimes causes badly
4578 * shielded USB devices to be shutdown by
4579 * the hub, this hack enables them again.
4580 * Works at least with mouse driver.
4581 */
4582 if (!(portstatus & USB_PORT_STAT_ENABLE)
4583 && !connect_change
Lan Tianyuff823c72012-09-05 13:44:32 +08004584 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 dev_err (hub_dev,
4586 "port %i "
4587 "disabled by hub (EMI?), "
4588 "re-enabling...\n",
4589 i);
4590 connect_change = 1;
4591 }
4592 }
4593
Sarah Sharp72937e12012-01-24 11:46:50 -08004594 if (hub_handle_remote_wakeup(hub, i,
4595 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004596 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004599 u16 status = 0;
4600 u16 unused;
4601
4602 dev_dbg(hub_dev, "over-current change on port "
4603 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 clear_port_feature(hdev, i,
4605 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004606 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004607 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004608 hub_port_status(hub, i, &status, &unused);
4609 if (status & USB_PORT_STAT_OVERCURRENT)
4610 dev_err(hub_dev, "over-current "
4611 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 }
4613
4614 if (portchange & USB_PORT_STAT_C_RESET) {
4615 dev_dbg (hub_dev,
4616 "reset change on port %d\n",
4617 i);
4618 clear_port_feature(hdev, i,
4619 USB_PORT_FEAT_C_RESET);
4620 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004621 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4622 hub_is_superspeed(hub->hdev)) {
4623 dev_dbg(hub_dev,
4624 "warm reset change on port %d\n",
4625 i);
4626 clear_port_feature(hdev, i,
4627 USB_PORT_FEAT_C_BH_PORT_RESET);
4628 }
John Youndbe79bb2001-09-17 00:00:00 -07004629 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4630 clear_port_feature(hub->hdev, i,
4631 USB_PORT_FEAT_C_PORT_LINK_STATE);
4632 }
4633 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4634 dev_warn(hub_dev,
4635 "config error on port %d\n",
4636 i);
4637 clear_port_feature(hub->hdev, i,
4638 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Andiry Xu5e467f62011-04-27 18:07:54 +08004641 /* Warm reset a USB3 protocol port if it's in
4642 * SS.Inactive state.
4643 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004644 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004645 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004646 hub_port_reset(hub, i, NULL,
4647 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004648 }
4649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 if (connect_change)
4651 hub_port_connect_change(hub, i,
4652 portstatus, portchange);
4653 } /* end for i */
4654
4655 /* deal with hub status changes */
4656 if (test_and_clear_bit(0, hub->event_bits) == 0)
4657 ; /* do nothing */
4658 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4659 dev_err (hub_dev, "get_hub_status failed\n");
4660 else {
4661 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4662 dev_dbg (hub_dev, "power change\n");
4663 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004664 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4665 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004666 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004667 else
4668 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 }
4670 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004671 u16 status = 0;
4672 u16 unused;
4673
4674 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004676 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004677 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004678 hub_hub_status(hub, &status, &unused);
4679 if (status & HUB_STATUS_OVERCURRENT)
4680 dev_err(hub_dev, "over-current "
4681 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 }
4683 }
4684
Alan Stern8e4ceb32009-12-07 13:01:37 -05004685 loop_autopm:
4686 /* Balance the usb_autopm_get_interface() above */
4687 usb_autopm_put_interface_no_suspend(intf);
4688 loop:
4689 /* Balance the usb_autopm_get_interface_no_resume() in
4690 * kick_khubd() and allow autosuspend.
4691 */
4692 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004693 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004695 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
4697 } /* end while (1) */
4698}
4699
4700static int hub_thread(void *__unused)
4701{
Alan Stern3bb1af52008-03-03 15:15:36 -05004702 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4703 * port handover. Otherwise it might see that a full-speed device
4704 * was gone before the EHCI controller had handed its port over to
4705 * the companion full-speed controller.
4706 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004707 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 do {
4710 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004711 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004712 !list_empty(&hub_event_list) ||
4713 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004714 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004716 pr_debug("%s: khubd exiting\n", usbcore_name);
4717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718}
4719
Németh Márton1e927d92010-01-10 15:34:53 +01004720static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08004721 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4722 | USB_DEVICE_ID_MATCH_INT_CLASS,
4723 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4724 .bInterfaceClass = USB_CLASS_HUB,
4725 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4727 .bDeviceClass = USB_CLASS_HUB},
4728 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4729 .bInterfaceClass = USB_CLASS_HUB},
4730 { } /* Terminating entry */
4731};
4732
4733MODULE_DEVICE_TABLE (usb, hub_id_table);
4734
4735static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 .name = "hub",
4737 .probe = hub_probe,
4738 .disconnect = hub_disconnect,
4739 .suspend = hub_suspend,
4740 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004741 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004742 .pre_reset = hub_pre_reset,
4743 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004744 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004746 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747};
4748
4749int usb_hub_init(void)
4750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 if (usb_register(&hub_driver) < 0) {
4752 printk(KERN_ERR "%s: can't register hub driver\n",
4753 usbcore_name);
4754 return -1;
4755 }
4756
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004757 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4758 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
4761 /* Fall through if kernel_thread failed */
4762 usb_deregister(&hub_driver);
4763 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4764
4765 return -1;
4766}
4767
4768void usb_hub_cleanup(void)
4769{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004770 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
4772 /*
4773 * Hub resources are freed for us by usb_deregister. It calls
4774 * usb_driver_purge on every device which in turn calls that
4775 * devices disconnect function if it is using this driver.
4776 * The hub_disconnect function takes care of releasing the
4777 * individual hub resources. -greg
4778 */
4779 usb_deregister(&hub_driver);
4780} /* usb_hub_cleanup() */
4781
Alan Sterneb764c42008-03-03 15:16:04 -05004782static int descriptors_changed(struct usb_device *udev,
4783 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784{
Alan Sterneb764c42008-03-03 15:16:04 -05004785 int changed = 0;
4786 unsigned index;
4787 unsigned serial_len = 0;
4788 unsigned len;
4789 unsigned old_length;
4790 int length;
4791 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792
Alan Sterneb764c42008-03-03 15:16:04 -05004793 if (memcmp(&udev->descriptor, old_device_descriptor,
4794 sizeof(*old_device_descriptor)) != 0)
4795 return 1;
4796
4797 /* Since the idVendor, idProduct, and bcdDevice values in the
4798 * device descriptor haven't changed, we will assume the
4799 * Manufacturer and Product strings haven't changed either.
4800 * But the SerialNumber string could be different (e.g., a
4801 * different flash card of the same brand).
4802 */
4803 if (udev->serial)
4804 serial_len = strlen(udev->serial) + 1;
4805
4806 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004808 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4809 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 }
Alan Sterneb764c42008-03-03 15:16:04 -05004811
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004812 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 if (buf == NULL) {
4814 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4815 /* assume the worst */
4816 return 1;
4817 }
4818 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004819 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4821 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004822 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 dev_dbg(&udev->dev, "config index %d, error %d\n",
4824 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004825 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 break;
4827 }
4828 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4829 != 0) {
4830 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004831 index,
4832 ((struct usb_config_descriptor *) buf)->
4833 bConfigurationValue);
4834 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 break;
4836 }
4837 }
Alan Sterneb764c42008-03-03 15:16:04 -05004838
4839 if (!changed && serial_len) {
4840 length = usb_string(udev, udev->descriptor.iSerialNumber,
4841 buf, serial_len);
4842 if (length + 1 != serial_len) {
4843 dev_dbg(&udev->dev, "serial string error %d\n",
4844 length);
4845 changed = 1;
4846 } else if (memcmp(buf, udev->serial, length) != 0) {
4847 dev_dbg(&udev->dev, "serial string changed\n");
4848 changed = 1;
4849 }
4850 }
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004853 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
4856/**
Ming Lei742120c2008-06-18 22:00:29 +08004857 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4859 *
Alan Stern79efa092006-06-01 13:33:42 -04004860 * WARNING - don't use this routine to reset a composite device
4861 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004862 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 *
4864 * Do a port reset, reassign the device's address, and establish its
4865 * former operating configuration. If the reset fails, or the device's
4866 * descriptors change from their values before the reset, or the original
4867 * configuration and altsettings cannot be restored, a flag will be set
4868 * telling khubd to pretend the device has been disconnected and then
4869 * re-connected. All drivers will be unbound, and the device will be
4870 * re-enumerated and probed all over again.
4871 *
4872 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4873 * flagged for logical disconnection, or some other negative error code
4874 * if the reset wasn't even attempted.
4875 *
4876 * The caller must own the device lock. For example, it's safe to use
4877 * this from a driver probe() routine after downloading new firmware.
4878 * For calls that might not occur during probe(), drivers should lock
4879 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004880 *
4881 * Locking exception: This routine may also be called from within an
4882 * autoresume handler. Such usage won't conflict with other tasks
4883 * holding the device lock because these tasks should always call
4884 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 */
Ming Lei742120c2008-06-18 22:00:29 +08004886static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887{
4888 struct usb_device *parent_hdev = udev->parent;
4889 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004890 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004892 int i, ret = 0;
4893 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
4895 if (udev->state == USB_STATE_NOTATTACHED ||
4896 udev->state == USB_STATE_SUSPENDED) {
4897 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4898 udev->state);
4899 return -EINVAL;
4900 }
4901
4902 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004903 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004904 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 return -EISDIR;
4906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 parent_hub = hdev_to_hub(parent_hdev);
4908
Sarah Sharpf74631e2012-06-25 12:08:08 -07004909 /* Disable LPM and LTM while we reset the device and reinstall the alt
4910 * settings. Device-initiated LPM settings, and system exit latency
4911 * settings are cleared when the device is reset, so we have to set
4912 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004913 */
4914 ret = usb_unlocked_disable_lpm(udev);
4915 if (ret) {
4916 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4917 goto re_enumerate;
4918 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004919 ret = usb_disable_ltm(udev);
4920 if (ret) {
4921 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4922 __func__);
4923 goto re_enumerate;
4924 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 set_bit(port1, parent_hub->busy_bits);
4927 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4928
4929 /* ep0 maxpacket size may change; let the HCD know about it.
4930 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004931 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004933 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 break;
4935 }
4936 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 if (ret < 0)
4939 goto re_enumerate;
4940
4941 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004942 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 dev_info(&udev->dev, "device firmware changed\n");
4944 udev->descriptor = descriptor; /* for disconnect() calls */
4945 goto re_enumerate;
4946 }
Alan Stern4fe03872009-02-26 10:21:02 -05004947
4948 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 if (!udev->actconfig)
4950 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004951
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004952 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004953 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4954 if (ret < 0) {
4955 dev_warn(&udev->dev,
4956 "Busted HC? Not enough HCD resources for "
4957 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004958 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004959 goto re_enumerate;
4960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4962 USB_REQ_SET_CONFIGURATION, 0,
4963 udev->actconfig->desc.bConfigurationValue, 0,
4964 NULL, 0, USB_CTRL_SET_TIMEOUT);
4965 if (ret < 0) {
4966 dev_err(&udev->dev,
4967 "can't restore configuration #%d (error=%d)\n",
4968 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004969 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 goto re_enumerate;
4971 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004972 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4974
Alan Stern4fe03872009-02-26 10:21:02 -05004975 /* Put interfaces back into the same altsettings as before.
4976 * Don't bother to send the Set-Interface request for interfaces
4977 * that were already in altsetting 0; besides being unnecessary,
4978 * many devices can't handle it. Instead just reset the host-side
4979 * endpoint state.
4980 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004982 struct usb_host_config *config = udev->actconfig;
4983 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 struct usb_interface_descriptor *desc;
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004987 if (desc->bAlternateSetting == 0) {
4988 usb_disable_interface(udev, intf, true);
4989 usb_enable_interface(udev, intf, true);
4990 ret = 0;
4991 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004992 /* Let the bandwidth allocation function know that this
4993 * device has been reset, and it will have to use
4994 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004995 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004996 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004997 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4998 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004999 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 if (ret < 0) {
5002 dev_err(&udev->dev, "failed to restore interface %d "
5003 "altsetting %d (error=%d)\n",
5004 desc->bInterfaceNumber,
5005 desc->bAlternateSetting,
5006 ret);
5007 goto re_enumerate;
5008 }
5009 }
5010
5011done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005012 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04005013 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005014 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 return 0;
5016
5017re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005018 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 hub_port_logical_disconnect(parent_hub, port1);
5020 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021}
5022
5023/**
5024 * usb_reset_device - warn interface drivers and perform a USB port reset
5025 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5026 *
Alan Stern79efa092006-06-01 13:33:42 -04005027 * Warns all drivers bound to registered interfaces (using their pre_reset
5028 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005029 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005030 *
Alan Stern79efa092006-06-01 13:33:42 -04005031 * Return value is the same as for usb_reset_and_verify_device().
5032 *
5033 * The caller must own the device lock. For example, it's safe to use
5034 * this from a driver probe() routine after downloading new firmware.
5035 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005036 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005037 *
5038 * If an interface is currently being probed or disconnected, we assume
5039 * its driver knows how to handle resets. For all other interfaces,
5040 * if the driver doesn't have pre_reset and post_reset methods then
5041 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005042 */
Ming Lei742120c2008-06-18 22:00:29 +08005043int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005044{
5045 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005046 int i;
Alan Stern79efa092006-06-01 13:33:42 -04005047 struct usb_host_config *config = udev->actconfig;
5048
5049 if (udev->state == USB_STATE_NOTATTACHED ||
5050 udev->state == USB_STATE_SUSPENDED) {
5051 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5052 udev->state);
5053 return -EINVAL;
5054 }
5055
Alan Stern645daaa2006-08-30 15:47:02 -04005056 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005057 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005058
Alan Stern79efa092006-06-01 13:33:42 -04005059 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005060 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005061 struct usb_interface *cintf = config->interface[i];
5062 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005063 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005064
5065 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005066 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005067 if (drv->pre_reset && drv->post_reset)
5068 unbind = (drv->pre_reset)(cintf);
5069 else if (cintf->condition ==
5070 USB_INTERFACE_BOUND)
5071 unbind = 1;
5072 if (unbind)
5073 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005074 }
5075 }
5076 }
5077
Ming Lei742120c2008-06-18 22:00:29 +08005078 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005079
5080 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005081 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005082 struct usb_interface *cintf = config->interface[i];
5083 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005084 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005085
Alan Stern78d9a482008-06-23 16:00:40 -04005086 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005087 drv = to_usb_driver(cintf->dev.driver);
5088 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005089 rebind = (drv->post_reset)(cintf);
5090 else if (cintf->condition ==
5091 USB_INTERFACE_BOUND)
5092 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005093 }
Alan Stern6c640942008-10-21 15:40:03 -04005094 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005095 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005096 }
5097 }
5098
Alan Stern94fcda12006-11-20 11:38:46 -05005099 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005100 return ret;
5101}
Ming Lei742120c2008-06-18 22:00:29 +08005102EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005103
5104
5105/**
5106 * usb_queue_reset_device - Reset a USB device from an atomic context
5107 * @iface: USB interface belonging to the device to reset
5108 *
5109 * This function can be used to reset a USB device from an atomic
5110 * context, where usb_reset_device() won't work (as it blocks).
5111 *
5112 * Doing a reset via this method is functionally equivalent to calling
5113 * usb_reset_device(), except for the fact that it is delayed to a
5114 * workqueue. This means that any drivers bound to other interfaces
5115 * might be unbound, as well as users from usbfs in user space.
5116 *
5117 * Corner cases:
5118 *
5119 * - Scheduling two resets at the same time from two different drivers
5120 * attached to two different interfaces of the same device is
5121 * possible; depending on how the driver attached to each interface
5122 * handles ->pre_reset(), the second reset might happen or not.
5123 *
5124 * - If a driver is unbound and it had a pending reset, the reset will
5125 * be cancelled.
5126 *
5127 * - This function can be called during .probe() or .disconnect()
5128 * times. On return from .disconnect(), any pending resets will be
5129 * cancelled.
5130 *
5131 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5132 * does its own.
5133 *
5134 * NOTE: We don't do any reference count tracking because it is not
5135 * needed. The lifecycle of the work_struct is tied to the
5136 * usb_interface. Before destroying the interface we cancel the
5137 * work_struct, so the fact that work_struct is queued and or
5138 * running means the interface (and thus, the device) exist and
5139 * are referenced.
5140 */
5141void usb_queue_reset_device(struct usb_interface *iface)
5142{
5143 schedule_work(&iface->reset_ws);
5144}
5145EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005146
5147/**
5148 * usb_hub_find_child - Get the pointer of child device
5149 * attached to the port which is specified by @port1.
5150 * @hdev: USB device belonging to the usb hub
5151 * @port1: port num to indicate which port the child device
5152 * is attached to.
5153 *
5154 * USB drivers call this function to get hub's child device
5155 * pointer.
5156 *
5157 * Return NULL if input param is invalid and
5158 * child's usb_device pointer if non-NULL.
5159 */
5160struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5161 int port1)
5162{
5163 struct usb_hub *hub = hdev_to_hub(hdev);
5164
5165 if (port1 < 1 || port1 > hdev->maxchild)
5166 return NULL;
5167 return hub->ports[port1 - 1]->child;
5168}
5169EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005170
Lan Tianyu05f91682012-09-05 13:44:34 +08005171/**
5172 * usb_set_hub_port_connect_type - set hub port connect type.
5173 * @hdev: USB device belonging to the usb hub
5174 * @port1: port num of the port
5175 * @type: connect type of the port
5176 */
5177void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5178 enum usb_port_connect_type type)
5179{
5180 struct usb_hub *hub = hdev_to_hub(hdev);
5181
5182 hub->ports[port1 - 1]->connect_type = type;
5183}
5184
5185/**
5186 * usb_get_hub_port_connect_type - Get the port's connect type
5187 * @hdev: USB device belonging to the usb hub
5188 * @port1: port num of the port
5189 *
5190 * Return connect type of the port and if input params are
5191 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5192 */
5193enum usb_port_connect_type
5194usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5195{
5196 struct usb_hub *hub = hdev_to_hub(hdev);
5197
5198 return hub->ports[port1 - 1]->connect_type;
5199}
5200
Lan Tianyud5575422012-09-05 13:44:33 +08005201#ifdef CONFIG_ACPI
5202/**
5203 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5204 * @hdev: USB device belonging to the usb hub
5205 * @port1: port num of the port
5206 *
5207 * Return port's acpi handle if successful, NULL if params are
5208 * invaild.
5209 */
5210acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5211 int port1)
5212{
5213 struct usb_hub *hub = hdev_to_hub(hdev);
5214
5215 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5216}
5217#endif