blob: 43ce1467f8c03fc8d7f6d8c1eaf6240f68985be2 [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);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400747 while (--limit && !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
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400754 next = hub->tt.clear_list.next;
755 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 list_del (&clear->clear_list);
757
758 /* drop lock so HCD can concurrently report other TT errors */
759 spin_unlock_irqrestore (&hub->tt.lock, flags);
760 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (status)
762 dev_err (&hdev->dev,
763 "clear tt %d (%04x) error %d\n",
764 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400765
766 /* Tell the HCD, even if the operation failed */
767 drv = clear->hcd->driver;
768 if (drv->clear_tt_buffer_complete)
769 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
770
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700771 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400772 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 spin_unlock_irqrestore (&hub->tt.lock, flags);
775}
776
777/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400778 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
779 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 *
781 * High speed HCDs use this to tell the hub driver that some split control or
782 * bulk transaction failed in a way that requires clearing internal state of
783 * a transaction translator. This is normally detected (and reported) from
784 * interrupt context.
785 *
786 * It may not be possible for that hub to handle additional full (or low)
787 * speed transactions until that state is fully cleared out.
788 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400789int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400791 struct usb_device *udev = urb->dev;
792 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct usb_tt *tt = udev->tt;
794 unsigned long flags;
795 struct usb_tt_clear *clear;
796
797 /* we've got to cope with an arbitrary number of pending TT clears,
798 * since each TT has "at least two" buffers that can need it (and
799 * there can be many TTs per hub). even if they're uncommon.
800 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800801 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
803 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400804 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
807 /* info that CLEAR_TT_BUFFER needs */
808 clear->tt = tt->multi ? udev->ttport : 1;
809 clear->devinfo = usb_pipeendpoint (pipe);
810 clear->devinfo |= udev->devnum << 4;
811 clear->devinfo |= usb_pipecontrol (pipe)
812 ? (USB_ENDPOINT_XFER_CONTROL << 11)
813 : (USB_ENDPOINT_XFER_BULK << 11);
814 if (usb_pipein (pipe))
815 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400816
817 /* info for completion callback */
818 clear->hcd = bus_to_hcd(udev->bus);
819 clear->ep = urb->ep;
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* tell keventd to clear state for this TT */
822 spin_lock_irqsave (&tt->lock, flags);
823 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400824 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400828EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Alan Stern8520f382008-09-22 14:44:26 -0400830/* If do_delay is false, return the number of milliseconds the caller
831 * needs to delay.
832 */
833static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700836 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400837 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400838 u16 wHubCharacteristics =
839 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Alan Stern4489a572006-04-27 15:54:22 -0400841 /* Enable power on each port. Some hubs have reserved values
842 * of LPSM (> 2) in their descriptors, even though they are
843 * USB 2.0 hubs. Some hubs do not implement port-power switching
844 * but only emulate it. In all cases, the ports won't work
845 * unless we send these messages to the hub.
846 */
847 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400849 else
850 dev_dbg(hub->intfdev, "trying to enable port power on "
851 "non-switchable hub\n");
852 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
Greg Kroah-Hartmana0693bd2012-09-24 13:04:02 -0700853 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
David Brownellb7896962005-08-31 10:41:44 -0700855 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400856 delay = max(pgood_delay, (unsigned) 100);
857 if (do_delay)
858 msleep(delay);
859 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862static int hub_hub_status(struct usb_hub *hub,
863 u16 *status, u16 *change)
864{
865 int ret;
866
Alan Sterndb90e7a2007-02-05 09:56:15 -0500867 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 ret = get_hub_status(hub->hdev, &hub->status->hub);
869 if (ret < 0)
870 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800871 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 else {
873 *status = le16_to_cpu(hub->status->hub.wHubStatus);
874 *change = le16_to_cpu(hub->status->hub.wHubChange);
875 ret = 0;
876 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500877 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return ret;
879}
880
Alan Stern8b28c752005-08-10 17:04:13 -0400881static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
882{
883 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400884 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400885
Lan Tianyuff823c72012-09-05 13:44:32 +0800886 if (hub->ports[port1 - 1]->child && set_state)
887 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400888 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700889 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400890 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400891 if (ret)
892 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400893 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400894 return ret;
895}
896
Alan Stern0458d5b2007-05-04 11:52:20 -0400897/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800898 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400899 * time later khubd will disconnect() any existing usb_device on the port
900 * and will re-enumerate if there actually is a device attached.
901 */
902static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
903{
904 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
905 hub_port_disable(hub, port1, 1);
906
907 /* FIXME let caller ask to power down the port:
908 * - some devices won't enumerate without a VBUS power cycle
909 * - SRP saves power that way
910 * - ... new call, TBD ...
911 * That's easy if this hub can switch power per-port, and
912 * khubd reactivates the port later (timer, SRP, etc).
913 * Powerdown must be optional, because of reset/DFU.
914 */
915
916 set_bit(port1, hub->change_bits);
917 kick_khubd(hub);
918}
919
Alan Stern253e0572009-10-27 15:20:13 -0400920/**
921 * usb_remove_device - disable a device's port on its parent hub
922 * @udev: device to be disabled and removed
923 * Context: @udev locked, must be able to sleep.
924 *
925 * After @udev's port has been disabled, khubd is notified and it will
926 * see that the device has been disconnected. When the device is
927 * physically unplugged and something is plugged in, the events will
928 * be received and processed normally.
929 */
930int usb_remove_device(struct usb_device *udev)
931{
932 struct usb_hub *hub;
933 struct usb_interface *intf;
934
935 if (!udev->parent) /* Can't remove a root hub */
936 return -EINVAL;
937 hub = hdev_to_hub(udev->parent);
938 intf = to_usb_interface(hub->intfdev);
939
940 usb_autopm_get_interface(intf);
941 set_bit(udev->portnum, hub->removed_bits);
942 hub_port_logical_disconnect(hub, udev->portnum);
943 usb_autopm_put_interface(intf);
944 return 0;
945}
946
Alan Stern6ee0b272008-04-28 11:06:42 -0400947enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500948 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400949 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400950};
Alan Stern5e6effa2008-03-03 15:15:51 -0500951
Alan Stern8520f382008-09-22 14:44:26 -0400952static void hub_init_func2(struct work_struct *ws);
953static void hub_init_func3(struct work_struct *ws);
954
Alan Sternf2835212008-04-28 11:07:17 -0400955static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500956{
957 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800958 struct usb_hcd *hcd;
959 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500960 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400961 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400962 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400963 unsigned delay;
964
965 /* Continue a partial initialization */
966 if (type == HUB_INIT2)
967 goto init2;
968 if (type == HUB_INIT3)
969 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500970
Elric Fua45aa3b2012-02-18 13:32:27 +0800971 /* The superspeed hub except for root hub has to use Hub Depth
972 * value as an offset into the route string to locate the bits
973 * it uses to determine the downstream port number. So hub driver
974 * should send a set hub depth request to superspeed hub after
975 * the superspeed hub is set configuration in initialization or
976 * reset procedure.
977 *
978 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400979 * For any other type of activation, turn it on.
980 */
Alan Stern8520f382008-09-22 14:44:26 -0400981 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800982 if (hdev->parent && hub_is_superspeed(hdev)) {
983 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
984 HUB_SET_DEPTH, USB_RT_HUB,
985 hdev->level - 1, 0, NULL, 0,
986 USB_CTRL_SET_TIMEOUT);
987 if (ret < 0)
988 dev_err(hub->intfdev,
989 "set hub depth failed\n");
990 }
Alan Stern8520f382008-09-22 14:44:26 -0400991
992 /* Speed up system boot by using a delayed_work for the
993 * hub's initial power-up delays. This is pretty awkward
994 * and the implementation looks like a home-brewed sort of
995 * setjmp/longjmp, but it saves at least 100 ms for each
996 * root hub (assuming usbcore is compiled into the kernel
997 * rather than as a module). It adds up.
998 *
999 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1000 * because for those activation types the ports have to be
1001 * operational when we return. In theory this could be done
1002 * for HUB_POST_RESET, but it's easier not to.
1003 */
1004 if (type == HUB_INIT) {
1005 delay = hub_power_on(hub, false);
1006 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1007 schedule_delayed_work(&hub->init_work,
1008 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001009
1010 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001011 usb_autopm_get_interface_no_resume(
1012 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001013 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001014 } else if (type == HUB_RESET_RESUME) {
1015 /* The internal host controller state for the hub device
1016 * may be gone after a host power loss on system resume.
1017 * Update the device's info so the HW knows it's a hub.
1018 */
1019 hcd = bus_to_hcd(hdev->bus);
1020 if (hcd->driver->update_hub_device) {
1021 ret = hcd->driver->update_hub_device(hcd, hdev,
1022 &hub->tt, GFP_NOIO);
1023 if (ret < 0) {
1024 dev_err(hub->intfdev, "Host not "
1025 "accepting hub info "
1026 "update.\n");
1027 dev_err(hub->intfdev, "LS/FS devices "
1028 "and hubs may not work "
1029 "under this hub\n.");
1030 }
1031 }
1032 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001033 } else {
1034 hub_power_on(hub, true);
1035 }
1036 }
1037 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001038
Alan Stern6ee0b272008-04-28 11:06:42 -04001039 /* Check each port and set hub->change_bits to let khubd know
1040 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001041 */
1042 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001043 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001044 u16 portstatus, portchange;
1045
Alan Stern6ee0b272008-04-28 11:06:42 -04001046 portstatus = portchange = 0;
1047 status = hub_port_status(hub, port1, &portstatus, &portchange);
1048 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1049 dev_dbg(hub->intfdev,
1050 "port %d: status %04x change %04x\n",
1051 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001052
Alan Stern6ee0b272008-04-28 11:06:42 -04001053 /* After anything other than HUB_RESUME (i.e., initialization
1054 * or any sort of reset), every port should be disabled.
1055 * Unconnected ports should likewise be disabled (paranoia),
1056 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001057 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001058 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1059 type != HUB_RESUME ||
1060 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1061 !udev ||
1062 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001063 /*
1064 * USB3 protocol ports will automatically transition
1065 * to Enabled state when detect an USB3.0 device attach.
1066 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001067 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001068 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001069 clear_port_feature(hdev, port1,
1070 USB_PORT_FEAT_ENABLE);
1071 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001072 } else {
1073 /* Pretend that power was lost for USB3 devs */
1074 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001075 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001076 }
1077
Alan Stern948fea32008-04-28 11:07:07 -04001078 /* Clear status-change flags; we'll debounce later */
1079 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1080 need_debounce_delay = true;
1081 clear_port_feature(hub->hdev, port1,
1082 USB_PORT_FEAT_C_CONNECTION);
1083 }
1084 if (portchange & USB_PORT_STAT_C_ENABLE) {
1085 need_debounce_delay = true;
1086 clear_port_feature(hub->hdev, port1,
1087 USB_PORT_FEAT_C_ENABLE);
1088 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001089 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1090 hub_is_superspeed(hub->hdev)) {
1091 need_debounce_delay = true;
1092 clear_port_feature(hub->hdev, port1,
1093 USB_PORT_FEAT_C_BH_PORT_RESET);
1094 }
Alan Stern253e0572009-10-27 15:20:13 -04001095 /* We can forget about a "removed" device when there's a
1096 * physical disconnect or the connect status changes.
1097 */
1098 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1099 (portchange & USB_PORT_STAT_C_CONNECTION))
1100 clear_bit(port1, hub->removed_bits);
1101
Alan Stern6ee0b272008-04-28 11:06:42 -04001102 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1103 /* Tell khubd to disconnect the device or
1104 * check for a new connection
1105 */
1106 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1107 set_bit(port1, hub->change_bits);
1108
1109 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001110 bool port_resumed = (portstatus &
1111 USB_PORT_STAT_LINK_STATE) ==
1112 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001113 /* The power session apparently survived the resume.
1114 * If there was an overcurrent or suspend change
1115 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001116 * take care of it. Look at the port link state
1117 * for USB 3.0 hubs, since they don't have a suspend
1118 * change bit, and they don't set the port link change
1119 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001120 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001121 if (portchange || (hub_is_superspeed(hub->hdev) &&
1122 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001123 set_bit(port1, hub->change_bits);
1124
1125 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001126#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001127 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001128#endif
Alan Stern8808f002008-04-28 11:06:55 -04001129 set_bit(port1, hub->change_bits);
1130
Alan Stern6ee0b272008-04-28 11:06:42 -04001131 } else {
1132 /* The power session is gone; tell khubd */
1133 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1134 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001135 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001136 }
1137
Alan Stern948fea32008-04-28 11:07:07 -04001138 /* If no port-status-change flags were set, we don't need any
1139 * debouncing. If flags were set we can try to debounce the
1140 * ports all at once right now, instead of letting khubd do them
1141 * one at a time later on.
1142 *
1143 * If any port-status changes do occur during this delay, khubd
1144 * will see them later and handle them normally.
1145 */
Alan Stern8520f382008-09-22 14:44:26 -04001146 if (need_debounce_delay) {
1147 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001148
Alan Stern8520f382008-09-22 14:44:26 -04001149 /* Don't do a long sleep inside a workqueue routine */
1150 if (type == HUB_INIT2) {
1151 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1152 schedule_delayed_work(&hub->init_work,
1153 msecs_to_jiffies(delay));
1154 return; /* Continues at init3: below */
1155 } else {
1156 msleep(delay);
1157 }
1158 }
1159 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001160 hub->quiescing = 0;
1161
1162 status = usb_submit_urb(hub->urb, GFP_NOIO);
1163 if (status < 0)
1164 dev_err(hub->intfdev, "activate --> %d\n", status);
1165 if (hub->has_indicators && blinkenlights)
1166 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1167
1168 /* Scan all ports that need attention */
1169 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001170
1171 /* Allow autosuspend if it was suppressed */
1172 if (type <= HUB_INIT3)
1173 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001174}
1175
Alan Stern8520f382008-09-22 14:44:26 -04001176/* Implement the continuations for the delays above */
1177static void hub_init_func2(struct work_struct *ws)
1178{
1179 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1180
1181 hub_activate(hub, HUB_INIT2);
1182}
1183
1184static void hub_init_func3(struct work_struct *ws)
1185{
1186 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1187
1188 hub_activate(hub, HUB_INIT3);
1189}
1190
Alan Stern43303542008-04-28 11:07:31 -04001191enum hub_quiescing_type {
1192 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1193};
1194
1195static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1196{
1197 struct usb_device *hdev = hub->hdev;
1198 int i;
1199
Alan Stern8520f382008-09-22 14:44:26 -04001200 cancel_delayed_work_sync(&hub->init_work);
1201
Alan Stern43303542008-04-28 11:07:31 -04001202 /* khubd and related activity won't re-trigger */
1203 hub->quiescing = 1;
1204
1205 if (type != HUB_SUSPEND) {
1206 /* Disconnect all the children */
1207 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001208 if (hub->ports[i]->child)
1209 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001210 }
1211 }
1212
1213 /* Stop khubd and related activity */
1214 usb_kill_urb(hub->urb);
1215 if (hub->has_indicators)
1216 cancel_delayed_work_sync(&hub->leds);
1217 if (hub->tt.hub)
Alan Sterncb88a1b2009-06-29 10:43:32 -04001218 cancel_work_sync(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001219}
1220
Alan Stern3eb14912008-03-03 15:15:43 -05001221/* caller has locked the hub device */
1222static int hub_pre_reset(struct usb_interface *intf)
1223{
1224 struct usb_hub *hub = usb_get_intfdata(intf);
1225
Alan Stern43303542008-04-28 11:07:31 -04001226 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001227 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001228}
1229
1230/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001231static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001232{
Alan Stern7de18d82006-06-01 13:37:24 -04001233 struct usb_hub *hub = usb_get_intfdata(intf);
1234
Alan Sternf2835212008-04-28 11:07:17 -04001235 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001236 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001237}
1238
Lan Tianyufa2a9562012-09-05 13:44:31 +08001239static void usb_port_device_release(struct device *dev)
1240{
1241 struct usb_port *port_dev = to_usb_port(dev);
1242
1243 kfree(port_dev);
1244}
1245
1246static void usb_hub_remove_port_device(struct usb_hub *hub,
1247 int port1)
1248{
1249 device_unregister(&hub->ports[port1 - 1]->dev);
1250}
1251
1252struct device_type usb_port_device_type = {
1253 .name = "usb_port",
1254 .release = usb_port_device_release,
1255};
1256
1257static int usb_hub_create_port_device(struct usb_hub *hub,
1258 int port1)
1259{
1260 struct usb_port *port_dev = NULL;
1261 int retval;
1262
1263 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1264 if (!port_dev) {
1265 retval = -ENOMEM;
1266 goto exit;
1267 }
1268
1269 hub->ports[port1 - 1] = port_dev;
1270 port_dev->dev.parent = hub->intfdev;
1271 port_dev->dev.type = &usb_port_device_type;
1272 dev_set_name(&port_dev->dev, "port%d", port1);
1273
1274 retval = device_register(&port_dev->dev);
1275 if (retval)
1276 goto error_register;
1277 return 0;
1278
1279error_register:
1280 put_device(&port_dev->dev);
1281exit:
1282 return retval;
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285static int hub_configure(struct usb_hub *hub,
1286 struct usb_endpoint_descriptor *endpoint)
1287{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001288 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct usb_device *hdev = hub->hdev;
1290 struct device *hub_dev = hub->intfdev;
1291 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001292 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001294 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001295 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Alan Sternd697cdd2009-10-27 15:18:46 -04001297 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 ret = -ENOMEM;
1300 goto fail;
1301 }
1302
1303 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1304 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 ret = -ENOMEM;
1306 goto fail;
1307 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001308 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1311 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 ret = -ENOMEM;
1313 goto fail;
1314 }
1315
1316 /* Request the entire hub descriptor.
1317 * hub->descriptor can handle USB_MAXCHILDREN ports,
1318 * but the hub can/will return fewer bytes here.
1319 */
John Youndbe79bb2001-09-17 00:00:00 -07001320 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (ret < 0) {
1322 message = "can't read hub descriptor";
1323 goto fail;
1324 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1325 message = "hub has too many ports!";
1326 ret = -ENODEV;
1327 goto fail;
1328 }
1329
1330 hdev->maxchild = hub->descriptor->bNbrPorts;
1331 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1332 (hdev->maxchild == 1) ? "" : "s");
1333
Lan Tianyufa2a9562012-09-05 13:44:31 +08001334 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1335 GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001336 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001337 ret = -ENOMEM;
1338 goto fail;
1339 }
1340
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001341 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
John Youndbe79bb2001-09-17 00:00:00 -07001343 /* FIXME for USB 3.0, skip for now */
1344 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1345 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int i;
1347 char portstr [USB_MAXCHILDREN + 1];
1348
1349 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001350 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1352 ? 'F' : 'R';
1353 portstr[hdev->maxchild] = 0;
1354 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1355 } else
1356 dev_dbg(hub_dev, "standalone hub\n");
1357
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001358 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301359 case HUB_CHAR_COMMON_LPSM:
1360 dev_dbg(hub_dev, "ganged power switching\n");
1361 break;
1362 case HUB_CHAR_INDV_PORT_LPSM:
1363 dev_dbg(hub_dev, "individual port power switching\n");
1364 break;
1365 case HUB_CHAR_NO_LPSM:
1366 case HUB_CHAR_LPSM:
1367 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1368 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
1370
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001371 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301372 case HUB_CHAR_COMMON_OCPM:
1373 dev_dbg(hub_dev, "global over-current protection\n");
1374 break;
1375 case HUB_CHAR_INDV_PORT_OCPM:
1376 dev_dbg(hub_dev, "individual port over-current protection\n");
1377 break;
1378 case HUB_CHAR_NO_OCPM:
1379 case HUB_CHAR_OCPM:
1380 dev_dbg(hub_dev, "no over-current protection\n");
1381 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383
1384 spin_lock_init (&hub->tt.lock);
1385 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001386 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301388 case USB_HUB_PR_FS:
1389 break;
1390 case USB_HUB_PR_HS_SINGLE_TT:
1391 dev_dbg(hub_dev, "Single TT\n");
1392 hub->tt.hub = hdev;
1393 break;
1394 case USB_HUB_PR_HS_MULTI_TT:
1395 ret = usb_set_interface(hdev, 0, 1);
1396 if (ret == 0) {
1397 dev_dbg(hub_dev, "TT per port\n");
1398 hub->tt.multi = 1;
1399 } else
1400 dev_err(hub_dev, "Using single TT (err %d)\n",
1401 ret);
1402 hub->tt.hub = hdev;
1403 break;
1404 case USB_HUB_PR_SS:
1405 /* USB 3.0 hubs don't have a TT */
1406 break;
1407 default:
1408 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1409 hdev->descriptor.bDeviceProtocol);
1410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001413 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001414 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001415 case HUB_TTTT_8_BITS:
1416 if (hdev->descriptor.bDeviceProtocol != 0) {
1417 hub->tt.think_time = 666;
1418 dev_dbg(hub_dev, "TT requires at most %d "
1419 "FS bit times (%d ns)\n",
1420 8, hub->tt.think_time);
1421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001423 case HUB_TTTT_16_BITS:
1424 hub->tt.think_time = 666 * 2;
1425 dev_dbg(hub_dev, "TT requires at most %d "
1426 "FS bit times (%d ns)\n",
1427 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001429 case HUB_TTTT_24_BITS:
1430 hub->tt.think_time = 666 * 3;
1431 dev_dbg(hub_dev, "TT requires at most %d "
1432 "FS bit times (%d ns)\n",
1433 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001435 case HUB_TTTT_32_BITS:
1436 hub->tt.think_time = 666 * 4;
1437 dev_dbg(hub_dev, "TT requires at most %d "
1438 "FS bit times (%d ns)\n",
1439 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 break;
1441 }
1442
1443 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001444 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 hub->has_indicators = 1;
1446 dev_dbg(hub_dev, "Port indicators are supported\n");
1447 }
1448
1449 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1450 hub->descriptor->bPwrOn2PwrGood * 2);
1451
1452 /* power budgeting mostly matters with bus-powered hubs,
1453 * and battery-powered root hubs (may provide just 8 mA).
1454 */
1455 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001456 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 message = "can't get hub status";
1458 goto fail;
1459 }
Alan Stern7d35b922005-04-25 11:18:32 -04001460 le16_to_cpus(&hubstatus);
1461 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001462 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1463 hub->mA_per_port = 500;
1464 else {
1465 hub->mA_per_port = hdev->bus_mA;
1466 hub->limited_power = 1;
1467 }
Alan Stern7d35b922005-04-25 11:18:32 -04001468 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1470 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001471 hub->limited_power = 1;
1472 if (hdev->maxchild > 0) {
1473 int remaining = hdev->bus_mA -
1474 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Alan Stern55c52712005-11-23 12:03:12 -05001476 if (remaining < hdev->maxchild * 100)
1477 dev_warn(hub_dev,
1478 "insufficient power available "
1479 "to use all downstream ports\n");
1480 hub->mA_per_port = 100; /* 7.2.1.1 */
1481 }
1482 } else { /* Self-powered external hub */
1483 /* FIXME: What about battery-powered external hubs that
1484 * provide less current per port? */
1485 hub->mA_per_port = 500;
1486 }
1487 if (hub->mA_per_port < 500)
1488 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1489 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001491 /* Update the HCD's internal representation of this hub before khubd
1492 * starts getting port status changes for devices under the hub.
1493 */
1494 hcd = bus_to_hcd(hdev->bus);
1495 if (hcd->driver->update_hub_device) {
1496 ret = hcd->driver->update_hub_device(hcd, hdev,
1497 &hub->tt, GFP_KERNEL);
1498 if (ret < 0) {
1499 message = "can't update HCD hub info";
1500 goto fail;
1501 }
1502 }
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1505 if (ret < 0) {
1506 message = "can't get hub status";
1507 goto fail;
1508 }
1509
1510 /* local power status reports aren't always correct */
1511 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1512 dev_dbg(hub_dev, "local power source is %s\n",
1513 (hubstatus & HUB_STATUS_LOCAL_POWER)
1514 ? "lost (inactive)" : "good");
1515
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001516 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 dev_dbg(hub_dev, "%sover-current condition exists\n",
1518 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1519
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001520 /* set up the interrupt endpoint
1521 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1522 * bytes as USB2.0[11.12.3] says because some hubs are known
1523 * to send more data (and thus cause overflow). For root hubs,
1524 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1525 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1527 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1528
1529 if (maxp > sizeof(*hub->buffer))
1530 maxp = sizeof(*hub->buffer);
1531
1532 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1533 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 ret = -ENOMEM;
1535 goto fail;
1536 }
1537
1538 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1539 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 /* maybe cycle the hub leds */
1542 if (hub->has_indicators && blinkenlights)
1543 hub->indicator [0] = INDICATOR_CYCLE;
1544
Lan Tianyufa2a9562012-09-05 13:44:31 +08001545 for (i = 0; i < hdev->maxchild; i++)
1546 if (usb_hub_create_port_device(hub, i + 1) < 0)
1547 dev_err(hub->intfdev,
1548 "couldn't create port%d device.\n", i + 1);
1549
Alan Sternf2835212008-04-28 11:07:17 -04001550 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return 0;
1552
1553fail:
1554 dev_err (hub_dev, "config failed, %s (err %d)\n",
1555 message, ret);
1556 /* hub_disconnect() frees urb and descriptor */
1557 return ret;
1558}
1559
Alan Sterne8054852007-05-04 11:55:11 -04001560static void hub_release(struct kref *kref)
1561{
1562 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1563
1564 usb_put_intf(to_usb_interface(hub->intfdev));
1565 kfree(hub);
1566}
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568static unsigned highspeed_hubs;
1569
1570static void hub_disconnect(struct usb_interface *intf)
1571{
Huajun Li88162302012-03-12 21:00:19 +08001572 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001573 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001574 int i;
1575
Alan Sterne8054852007-05-04 11:55:11 -04001576 /* Take the hub off the event list and don't let it be added again */
1577 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001578 if (!list_empty(&hub->event_list)) {
1579 list_del_init(&hub->event_list);
1580 usb_autopm_put_interface_no_suspend(intf);
1581 }
Alan Sterne8054852007-05-04 11:55:11 -04001582 hub->disconnected = 1;
1583 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Alan Stern7de18d82006-06-01 13:37:24 -04001585 /* Disconnect all children and quiesce the hub */
1586 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001587 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001588
Alan Stern8b28c752005-08-10 17:04:13 -04001589 usb_set_intfdata (intf, NULL);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001590
1591 for (i = 0; i < hdev->maxchild; i++)
1592 usb_hub_remove_port_device(hub, i + 1);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001593 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Alan Sterne8054852007-05-04 11:55:11 -04001595 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 highspeed_hubs--;
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001599 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001600 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001601 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001602 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Alan Sterne8054852007-05-04 11:55:11 -04001604 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605}
1606
1607static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1608{
1609 struct usb_host_interface *desc;
1610 struct usb_endpoint_descriptor *endpoint;
1611 struct usb_device *hdev;
1612 struct usb_hub *hub;
1613
1614 desc = intf->cur_altsetting;
1615 hdev = interface_to_usbdev(intf);
1616
Ming Lei596d7892012-10-24 11:59:25 +08001617 /*
1618 * Set default autosuspend delay as 0 to speedup bus suspend,
1619 * based on the below considerations:
1620 *
1621 * - Unlike other drivers, the hub driver does not rely on the
1622 * autosuspend delay to provide enough time to handle a wakeup
1623 * event, and the submitted status URB is just to check future
1624 * change on hub downstream ports, so it is safe to do it.
1625 *
1626 * - The patch might cause one or more auto supend/resume for
1627 * below very rare devices when they are plugged into hub
1628 * first time:
1629 *
1630 * devices having trouble initializing, and disconnect
1631 * themselves from the bus and then reconnect a second
1632 * or so later
1633 *
1634 * devices just for downloading firmware, and disconnects
1635 * themselves after completing it
1636 *
1637 * For these quite rare devices, their drivers may change the
1638 * autosuspend delay of their parent hub in the probe() to one
1639 * appropriate value to avoid the subtle problem if someone
1640 * does care it.
1641 *
1642 * - The patch may cause one or more auto suspend/resume on
1643 * hub during running 'lsusb', but it is probably too
1644 * infrequent to worry about.
1645 *
1646 * - Change autosuspend delay of hub can avoid unnecessary auto
1647 * suspend timer for hub, also may decrease power consumption
1648 * of USB bus.
1649 */
1650 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1651
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001652 /* Hubs have proper suspend/resume support. */
1653 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001654
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001655 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001656 dev_err(&intf->dev,
1657 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001658 return -E2BIG;
1659 }
1660
David Brownell89ccbdc2006-04-02 10:18:09 -08001661#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1662 if (hdev->parent) {
1663 dev_warn(&intf->dev, "ignoring external hub\n");
1664 return -ENODEV;
1665 }
1666#endif
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /* Some hubs have a subclass of 1, which AFAICT according to the */
1669 /* specs is not defined, but it works */
1670 if ((desc->desc.bInterfaceSubClass != 0) &&
1671 (desc->desc.bInterfaceSubClass != 1)) {
1672descriptor_error:
1673 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1674 return -EIO;
1675 }
1676
1677 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1678 if (desc->desc.bNumEndpoints != 1)
1679 goto descriptor_error;
1680
1681 endpoint = &desc->endpoint[0].desc;
1682
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001683 /* If it's not an interrupt in endpoint, we'd better punt! */
1684 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 goto descriptor_error;
1686
1687 /* We found a hub */
1688 dev_info (&intf->dev, "USB hub found\n");
1689
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001690 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 if (!hub) {
1692 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1693 return -ENOMEM;
1694 }
1695
Alan Sterne8054852007-05-04 11:55:11 -04001696 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 INIT_LIST_HEAD(&hub->event_list);
1698 hub->intfdev = &intf->dev;
1699 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001700 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001701 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001702 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001705 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 if (hdev->speed == USB_SPEED_HIGH)
1708 highspeed_hubs++;
1709
Ming Leie6f30de2012-10-24 11:59:24 +08001710 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1711 hub->quirk_check_port_auto_suspend = 1;
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 if (hub_configure(hub, endpoint) >= 0)
1714 return 0;
1715
1716 hub_disconnect (intf);
1717 return -ENODEV;
1718}
1719
1720static int
1721hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1722{
1723 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuff823c72012-09-05 13:44:32 +08001724 struct usb_hub *hub = hdev_to_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /* assert ifno == 0 (part of hub spec) */
1727 switch (code) {
1728 case USBDEVFS_HUB_PORTINFO: {
1729 struct usbdevfs_hub_portinfo *info = user_data;
1730 int i;
1731
1732 spin_lock_irq(&device_state_lock);
1733 if (hdev->devnum <= 0)
1734 info->nports = 0;
1735 else {
1736 info->nports = hdev->maxchild;
1737 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001738 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 info->port[i] = 0;
1740 else
1741 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001742 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 }
1744 }
1745 spin_unlock_irq(&device_state_lock);
1746
1747 return info->nports + 1;
1748 }
1749
1750 default:
1751 return -ENOSYS;
1752 }
1753}
1754
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001755/*
1756 * Allow user programs to claim ports on a hub. When a device is attached
1757 * to one of these "claimed" ports, the program will "own" the device.
1758 */
1759static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001760 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001761{
1762 if (hdev->state == USB_STATE_NOTATTACHED)
1763 return -ENODEV;
1764 if (port1 == 0 || port1 > hdev->maxchild)
1765 return -EINVAL;
1766
1767 /* This assumes that devices not managed by the hub driver
1768 * will always have maxchild equal to 0.
1769 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001770 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001771 return 0;
1772}
1773
1774/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001775int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1776 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001777{
1778 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001779 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001780
1781 rc = find_port_owner(hdev, port1, &powner);
1782 if (rc)
1783 return rc;
1784 if (*powner)
1785 return -EBUSY;
1786 *powner = owner;
1787 return rc;
1788}
1789
Lan Tianyu336c5c32012-07-06 14:13:52 +08001790int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1791 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001792{
1793 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001794 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001795
1796 rc = find_port_owner(hdev, port1, &powner);
1797 if (rc)
1798 return rc;
1799 if (*powner != owner)
1800 return -ENOENT;
1801 *powner = NULL;
1802 return rc;
1803}
1804
Lan Tianyu336c5c32012-07-06 14:13:52 +08001805void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001806{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001807 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001808 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001809
Lan Tianyufa2a9562012-09-05 13:44:31 +08001810 for (n = 0; n < hdev->maxchild; n++) {
1811 if (hub->ports[n]->port_owner == owner)
1812 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001813 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001814
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001815}
1816
1817/* The caller must hold udev's lock */
1818bool usb_device_is_owned(struct usb_device *udev)
1819{
1820 struct usb_hub *hub;
1821
1822 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1823 return false;
1824 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001825 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1829{
Lan Tianyuff823c72012-09-05 13:44:32 +08001830 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int i;
1832
1833 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001834 if (hub->ports[i]->child)
1835 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001837 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001838 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 udev->state = USB_STATE_NOTATTACHED;
1840}
1841
1842/**
1843 * usb_set_device_state - change a device's current state (usbcore, hcds)
1844 * @udev: pointer to device whose state should be changed
1845 * @new_state: new state value to be stored
1846 *
1847 * udev->state is _not_ fully protected by the device lock. Although
1848 * most transitions are made only while holding the lock, the state can
1849 * can change to USB_STATE_NOTATTACHED at almost any time. This
1850 * is so that devices can be marked as disconnected as soon as possible,
1851 * without having to wait for any semaphores to be released. As a result,
1852 * all changes to any device's state must be protected by the
1853 * device_state_lock spinlock.
1854 *
1855 * Once a device has been added to the device tree, all changes to its state
1856 * should be made using this routine. The state should _not_ be set directly.
1857 *
1858 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1859 * Otherwise udev->state is set to new_state, and if new_state is
1860 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1861 * to USB_STATE_NOTATTACHED.
1862 */
1863void usb_set_device_state(struct usb_device *udev,
1864 enum usb_device_state new_state)
1865{
1866 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001867 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 spin_lock_irqsave(&device_state_lock, flags);
1870 if (udev->state == USB_STATE_NOTATTACHED)
1871 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001872 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001873
1874 /* root hub wakeup capabilities are managed out-of-band
1875 * and may involve silicon errata ... ignore them here.
1876 */
1877 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001878 if (udev->state == USB_STATE_SUSPENDED
1879 || new_state == USB_STATE_SUSPENDED)
1880 ; /* No change to wakeup settings */
1881 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001882 wakeup = udev->actconfig->desc.bmAttributes
1883 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001884 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001885 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001886 }
Sarah Sharp15123002007-12-21 16:54:15 -08001887 if (udev->state == USB_STATE_SUSPENDED &&
1888 new_state != USB_STATE_SUSPENDED)
1889 udev->active_duration -= jiffies;
1890 else if (new_state == USB_STATE_SUSPENDED &&
1891 udev->state != USB_STATE_SUSPENDED)
1892 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001893 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001894 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 recursively_mark_NOTATTACHED(udev);
1896 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001897 if (wakeup >= 0)
1898 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
David Vrabel6da9c992009-02-18 14:43:47 +00001900EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001902/*
Alan Stern3b29b682011-02-22 09:53:41 -05001903 * Choose a device number.
1904 *
1905 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1906 * USB-2.0 buses they are also used as device addresses, however on
1907 * USB-3.0 buses the address is assigned by the controller hardware
1908 * and it usually is not the same as the device number.
1909 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001910 * WUSB devices are simple: they have no hubs behind, so the mapping
1911 * device <-> virtual port number becomes 1:1. Why? to simplify the
1912 * life of the device connection logic in
1913 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1914 * handshake we need to assign a temporary address in the unauthorized
1915 * space. For simplicity we use the first virtual port number found to
1916 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1917 * and that becomes it's address [X < 128] or its unauthorized address
1918 * [X | 0x80].
1919 *
1920 * We add 1 as an offset to the one-based USB-stack port number
1921 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1922 * 0 is reserved by USB for default address; (b) Linux's USB stack
1923 * uses always #1 for the root hub of the controller. So USB stack's
1924 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001925 *
1926 * Devices connected under xHCI are not as simple. The host controller
1927 * supports virtualization, so the hardware assigns device addresses and
1928 * the HCD must setup data structures before issuing a set address
1929 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001930 */
Alan Stern3b29b682011-02-22 09:53:41 -05001931static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 int devnum;
1934 struct usb_bus *bus = udev->bus;
1935
1936 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001937 if (udev->wusb) {
1938 devnum = udev->portnum + 1;
1939 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1940 } else {
1941 /* Try to allocate the next devnum beginning at
1942 * bus->devnum_next. */
1943 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1944 bus->devnum_next);
1945 if (devnum >= 128)
1946 devnum = find_next_zero_bit(bus->devmap.devicemap,
1947 128, 1);
1948 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (devnum < 128) {
1951 set_bit(devnum, bus->devmap.devicemap);
1952 udev->devnum = devnum;
1953 }
1954}
1955
Alan Stern3b29b682011-02-22 09:53:41 -05001956static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
1958 if (udev->devnum > 0) {
1959 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1960 udev->devnum = -1;
1961 }
1962}
1963
Alan Stern3b29b682011-02-22 09:53:41 -05001964static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001965{
1966 /* The address for a WUSB device is managed by wusbcore. */
1967 if (!udev->wusb)
1968 udev->devnum = devnum;
1969}
1970
Herbert Xuf7410ce2010-01-10 20:15:03 +11001971static void hub_free_dev(struct usb_device *udev)
1972{
1973 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1974
1975 /* Root hubs aren't real devices, so don't free HCD resources */
1976 if (hcd->driver->free_dev && udev->parent)
1977 hcd->driver->free_dev(hcd, udev);
1978}
1979
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980/**
1981 * usb_disconnect - disconnect a device (usbcore-internal)
1982 * @pdev: pointer to device being disconnected
1983 * Context: !in_interrupt ()
1984 *
1985 * Something got disconnected. Get rid of it and all of its children.
1986 *
1987 * If *pdev is a normal device then the parent hub must already be locked.
1988 * If *pdev is a root hub then this routine will acquire the
1989 * usb_bus_list_lock on behalf of the caller.
1990 *
1991 * Only hub drivers (including virtual root hub drivers for host
1992 * controllers) should ever call this.
1993 *
1994 * This call is synchronous, and may not be used in an interrupt context.
1995 */
1996void usb_disconnect(struct usb_device **pdev)
1997{
1998 struct usb_device *udev = *pdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08001999 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 int i;
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* mark the device as inactive, so any further urb submissions for
2003 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002004 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 */
2006 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002007 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2008 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002010 usb_lock_device(udev);
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08002013 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08002014 if (hub->ports[i]->child)
2015 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
2018 /* deallocate hcd/hardware state ... nuking all pending urbs and
2019 * cleaning up all state associated with the current configuration
2020 * so that the hardware is now fully quiesced.
2021 */
Alan Stern782da722006-07-01 22:09:35 -04002022 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002024 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Alan Stern3b23dd62008-12-05 14:10:34 -05002026 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002027 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002028
Alan Stern782da722006-07-01 22:09:35 -04002029 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002030 * for de-configuring the device and invoking the remove-device
2031 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002032 */
2033 device_del(&udev->dev);
2034
2035 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 * (or root_hub) pointer.
2037 */
Alan Stern3b29b682011-02-22 09:53:41 -05002038 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 /* Avoid races with recursively_mark_NOTATTACHED() */
2041 spin_lock_irq(&device_state_lock);
2042 *pdev = NULL;
2043 spin_unlock_irq(&device_state_lock);
2044
Herbert Xuf7410ce2010-01-10 20:15:03 +11002045 hub_free_dev(udev);
2046
Alan Stern782da722006-07-01 22:09:35 -04002047 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002050#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051static void show_string(struct usb_device *udev, char *id, char *string)
2052{
2053 if (!string)
2054 return;
2055 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2056}
2057
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002058static void announce_device(struct usb_device *udev)
2059{
2060 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2061 le16_to_cpu(udev->descriptor.idVendor),
2062 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002063 dev_info(&udev->dev,
2064 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002065 udev->descriptor.iManufacturer,
2066 udev->descriptor.iProduct,
2067 udev->descriptor.iSerialNumber);
2068 show_string(udev, "Product", udev->product);
2069 show_string(udev, "Manufacturer", udev->manufacturer);
2070 show_string(udev, "SerialNumber", udev->serial);
2071}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002073static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074#endif
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076#ifdef CONFIG_USB_OTG
2077#include "otg_whitelist.h"
2078#endif
2079
Oliver Neukum3ede7602007-01-11 14:35:50 +01002080/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002081 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002082 * @udev: newly addressed device (in ADDRESS state)
2083 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002084 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002085 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002086static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002088 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090#ifdef CONFIG_USB_OTG
2091 /*
2092 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2093 * to wake us after we've powered off VBUS; and HNP, switching roles
2094 * "host" to "peripheral". The OTG descriptor helps figure this out.
2095 */
2096 if (!udev->bus->is_b_host
2097 && udev->config
2098 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002099 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct usb_bus *bus = udev->bus;
2101
2102 /* descriptor may appear anywhere in config */
2103 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2104 le16_to_cpu(udev->config[0].desc.wTotalLength),
2105 USB_DT_OTG, (void **) &desc) == 0) {
2106 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002107 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 dev_info(&udev->dev,
2110 "Dual-Role OTG device on %sHNP port\n",
2111 (port1 == bus->otg_port)
2112 ? "" : "non-");
2113
2114 /* enable HNP before suspend, it's simpler */
2115 if (port1 == bus->otg_port)
2116 bus->b_hnp_enable = 1;
2117 err = usb_control_msg(udev,
2118 usb_sndctrlpipe(udev, 0),
2119 USB_REQ_SET_FEATURE, 0,
2120 bus->b_hnp_enable
2121 ? USB_DEVICE_B_HNP_ENABLE
2122 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2123 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2124 if (err < 0) {
2125 /* OTG MESSAGE: report errors here,
2126 * customize to match your product.
2127 */
2128 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002129 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 err);
2131 bus->b_hnp_enable = 0;
2132 }
2133 }
2134 }
2135 }
2136
2137 if (!is_targeted(udev)) {
2138
2139 /* Maybe it can talk to us, though we can't talk to it.
2140 * (Includes HNP test device.)
2141 */
2142 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002143 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (err < 0)
2145 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2146 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002147 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 goto fail;
2149 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002150fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002152 return err;
2153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002155
2156/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002157 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002158 * @udev: newly addressed device (in ADDRESS state)
2159 *
2160 * This is only called by usb_new_device() and usb_authorize_device()
2161 * and FIXME -- all comments that apply to them apply here wrt to
2162 * environment.
2163 *
2164 * If the device is WUSB and not authorized, we don't attempt to read
2165 * the string descriptors, as they will be errored out by the device
2166 * until it has been authorized.
2167 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002168static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002169{
2170 int err;
2171
2172 if (udev->config == NULL) {
2173 err = usb_get_configuration(udev);
2174 if (err < 0) {
2175 dev_err(&udev->dev, "can't read configurations, error %d\n",
2176 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002177 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002178 }
2179 }
2180 if (udev->wusb == 1 && udev->authorized == 0) {
2181 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2182 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2183 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2184 }
2185 else {
2186 /* read the standard strings and cache them if present */
2187 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2188 udev->manufacturer = usb_cache_string(udev,
2189 udev->descriptor.iManufacturer);
2190 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2191 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002192 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002193 if (err < 0)
2194 return err;
2195
2196 usb_detect_interface_quirks(udev);
2197
2198 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002199}
2200
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002201static void set_usb_port_removable(struct usb_device *udev)
2202{
2203 struct usb_device *hdev = udev->parent;
2204 struct usb_hub *hub;
2205 u8 port = udev->portnum;
2206 u16 wHubCharacteristics;
2207 bool removable = true;
2208
2209 if (!hdev)
2210 return;
2211
2212 hub = hdev_to_hub(udev->parent);
2213
2214 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2215
2216 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2217 return;
2218
2219 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002220 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2221 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002222 removable = false;
2223 } else {
2224 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2225 removable = false;
2226 }
2227
2228 if (removable)
2229 udev->removable = USB_DEVICE_REMOVABLE;
2230 else
2231 udev->removable = USB_DEVICE_FIXED;
2232}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002233
2234/**
2235 * usb_new_device - perform initial device setup (usbcore-internal)
2236 * @udev: newly addressed device (in ADDRESS state)
2237 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002238 * This is called with devices which have been detected but not fully
2239 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002240 * for any device configuration. The caller must have locked either
2241 * the parent hub (if udev is a normal device) or else the
2242 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2243 * udev has already been installed, but udev is not yet visible through
2244 * sysfs or other filesystem code.
2245 *
2246 * It will return if the device is configured properly or not. Zero if
2247 * the interface was registered with the driver core; else a negative
2248 * errno value.
2249 *
2250 * This call is synchronous, and may not be used in an interrupt context.
2251 *
2252 * Only the hub driver or root-hub registrar should ever call this.
2253 */
2254int usb_new_device(struct usb_device *udev)
2255{
2256 int err;
2257
Dan Streetman16985402010-01-06 09:56:53 -05002258 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002259 /* Initialize non-root-hub device wakeup to disabled;
2260 * device (un)configuration controls wakeup capable
2261 * sysfs power/wakeup controls wakeup enabled/disabled
2262 */
2263 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002264 }
2265
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002266 /* Tell the runtime-PM framework the device is active */
2267 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002268 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002269 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002270 pm_runtime_enable(&udev->dev);
2271
Alan Sternc08512c2010-11-15 15:57:58 -05002272 /* By default, forbid autosuspend for all devices. It will be
2273 * allowed for hubs during binding.
2274 */
2275 usb_disable_autosuspend(udev);
2276
Alan Stern8d8558d2009-12-08 15:50:41 -05002277 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002278 if (err < 0)
2279 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002280 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2281 udev->devnum, udev->bus->busnum,
2282 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002283 /* export the usbdev device-node for libusb */
2284 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2285 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2286
Alan Stern6cd13202008-11-13 15:08:30 -05002287 /* Tell the world! */
2288 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002289
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002290 if (udev->serial)
2291 add_device_randomness(udev->serial, strlen(udev->serial));
2292 if (udev->product)
2293 add_device_randomness(udev->product, strlen(udev->product));
2294 if (udev->manufacturer)
2295 add_device_randomness(udev->manufacturer,
2296 strlen(udev->manufacturer));
2297
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002298 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002299
2300 /*
2301 * check whether the hub marks this port as non-removable. Do it
2302 * now so that platform-specific data can override it in
2303 * device_add()
2304 */
2305 if (udev->parent)
2306 set_usb_port_removable(udev);
2307
Alan Stern782da722006-07-01 22:09:35 -04002308 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002309 * for configuring the device and invoking the add-device
2310 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002311 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002312 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (err) {
2314 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2315 goto fail;
2316 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002317
Alan Stern3b23dd62008-12-05 14:10:34 -05002318 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002319 usb_mark_last_busy(udev);
2320 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323fail:
2324 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002325 pm_runtime_disable(&udev->dev);
2326 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002327 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002330
2331/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002332 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2333 * @usb_dev: USB device
2334 *
2335 * Move the USB device to a very basic state where interfaces are disabled
2336 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002337 *
2338 * We share a lock (that we have) with device_del(), so we need to
2339 * defer its call.
2340 */
2341int usb_deauthorize_device(struct usb_device *usb_dev)
2342{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002343 usb_lock_device(usb_dev);
2344 if (usb_dev->authorized == 0)
2345 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002346
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002347 usb_dev->authorized = 0;
2348 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002349
2350 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002351 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002352 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002353 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002354 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002355 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002356
2357 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002358 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002359
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002360out_unauthorized:
2361 usb_unlock_device(usb_dev);
2362 return 0;
2363}
2364
2365
2366int usb_authorize_device(struct usb_device *usb_dev)
2367{
2368 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002369
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002370 usb_lock_device(usb_dev);
2371 if (usb_dev->authorized == 1)
2372 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002373
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002374 result = usb_autoresume_device(usb_dev);
2375 if (result < 0) {
2376 dev_err(&usb_dev->dev,
2377 "can't autoresume for authorization: %d\n", result);
2378 goto error_autoresume;
2379 }
2380 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2381 if (result < 0) {
2382 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2383 "authorization: %d\n", result);
2384 goto error_device_descriptor;
2385 }
Alan Sternda307122009-12-08 15:54:44 -05002386
2387 kfree(usb_dev->product);
2388 usb_dev->product = NULL;
2389 kfree(usb_dev->manufacturer);
2390 usb_dev->manufacturer = NULL;
2391 kfree(usb_dev->serial);
2392 usb_dev->serial = NULL;
2393
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002394 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002395 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002396 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002397 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002398 /* Choose and set the configuration. This registers the interfaces
2399 * with the driver core and lets interface drivers bind to them.
2400 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002401 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002402 if (c >= 0) {
2403 result = usb_set_configuration(usb_dev, c);
2404 if (result) {
2405 dev_err(&usb_dev->dev,
2406 "can't set config #%d, error %d\n", c, result);
2407 /* This need not be fatal. The user can try to
2408 * set other configurations. */
2409 }
2410 }
2411 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002412
Alan Stern8d8558d2009-12-08 15:50:41 -05002413error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002414error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002415 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002416error_autoresume:
2417out_authorized:
2418 usb_unlock_device(usb_dev); // complements locktree
2419 return result;
2420}
2421
2422
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002423/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2424static unsigned hub_is_wusb(struct usb_hub *hub)
2425{
2426 struct usb_hcd *hcd;
2427 if (hub->hdev->parent != NULL) /* not a root hub? */
2428 return 0;
2429 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2430 return hcd->wireless;
2431}
2432
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434#define PORT_RESET_TRIES 5
2435#define SET_ADDRESS_TRIES 2
2436#define GET_DESCRIPTOR_TRIES 2
2437#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302438#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2441#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002442#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443#define HUB_LONG_RESET_TIME 200
2444#define HUB_RESET_TIMEOUT 500
2445
Sarah Sharp10d674a2011-09-14 14:24:52 -07002446static int hub_port_reset(struct usb_hub *hub, int port1,
2447 struct usb_device *udev, unsigned int delay, bool warm);
2448
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002449/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2450 * Port worm reset is required to recover
2451 */
2452static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002453{
2454 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002455 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2456 USB_SS_PORT_LS_SS_INACTIVE) ||
2457 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2458 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002459}
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002462 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 int delay_time, ret;
2465 u16 portstatus;
2466 u16 portchange;
2467
2468 for (delay_time = 0;
2469 delay_time < HUB_RESET_TIMEOUT;
2470 delay_time += delay) {
2471 /* wait to give the device a chance to reset */
2472 msleep(delay);
2473
2474 /* read and decode port status */
2475 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2476 if (ret < 0)
2477 return ret;
2478
Andiry Xu75d7cf72011-09-13 16:41:11 -07002479 /*
2480 * Some buggy devices require a warm reset to be issued even
2481 * when the port appears not to be connected.
2482 */
2483 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002484 /*
2485 * Some buggy devices can cause an NEC host controller
2486 * to transition to the "Error" state after a hot port
2487 * reset. This will show up as the port state in
2488 * "Inactive", and the port may also report a
2489 * disconnect. Forcing a warm port reset seems to make
2490 * the device work.
2491 *
2492 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2493 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002494 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002495 int ret;
2496
2497 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2498 clear_port_feature(hub->hdev, port1,
2499 USB_PORT_FEAT_C_CONNECTION);
2500 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2501 clear_port_feature(hub->hdev, port1,
2502 USB_PORT_FEAT_C_PORT_LINK_STATE);
2503 if (portchange & USB_PORT_STAT_C_RESET)
2504 clear_port_feature(hub->hdev, port1,
2505 USB_PORT_FEAT_C_RESET);
2506 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2507 port1);
2508 ret = hub_port_reset(hub, port1,
2509 udev, HUB_BH_RESET_TIME,
2510 true);
2511 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2512 clear_port_feature(hub->hdev, port1,
2513 USB_PORT_FEAT_C_CONNECTION);
2514 return ret;
2515 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002516 /* Device went away? */
2517 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2518 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
Andiry Xu75d7cf72011-09-13 16:41:11 -07002520 /* bomb out completely if the connection bounced */
2521 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2522 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Andiry Xu75d7cf72011-09-13 16:41:11 -07002524 /* if we`ve finished resetting, then break out of
2525 * the loop
2526 */
2527 if (!(portstatus & USB_PORT_STAT_RESET) &&
2528 (portstatus & USB_PORT_STAT_ENABLE)) {
2529 if (hub_is_wusb(hub))
2530 udev->speed = USB_SPEED_WIRELESS;
2531 else if (hub_is_superspeed(hub->hdev))
2532 udev->speed = USB_SPEED_SUPER;
2533 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2534 udev->speed = USB_SPEED_HIGH;
2535 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2536 udev->speed = USB_SPEED_LOW;
2537 else
2538 udev->speed = USB_SPEED_FULL;
2539 return 0;
2540 }
2541 } else {
2542 if (portchange & USB_PORT_STAT_C_BH_RESET)
2543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
2545
2546 /* switch to the long delay after two short delay failures */
2547 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2548 delay = HUB_LONG_RESET_TIME;
2549
2550 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002551 "port %d not %sreset yet, waiting %dms\n",
2552 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
2554
2555 return -EBUSY;
2556}
2557
Andiry Xu75d7cf72011-09-13 16:41:11 -07002558static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2559 struct usb_device *udev, int *status, bool warm)
2560{
2561 switch (*status) {
2562 case 0:
2563 if (!warm) {
2564 struct usb_hcd *hcd;
2565 /* TRSTRCY = 10 ms; plus some extra */
2566 msleep(10 + 40);
2567 update_devnum(udev, 0);
2568 hcd = bus_to_hcd(udev->bus);
2569 if (hcd->driver->reset_device) {
2570 *status = hcd->driver->reset_device(hcd, udev);
2571 if (*status < 0) {
2572 dev_err(&udev->dev, "Cannot reset "
2573 "HCD device state\n");
2574 break;
2575 }
2576 }
2577 }
2578 /* FALL THROUGH */
2579 case -ENOTCONN:
2580 case -ENODEV:
2581 clear_port_feature(hub->hdev,
2582 port1, USB_PORT_FEAT_C_RESET);
2583 /* FIXME need disconnect() for NOTATTACHED device */
2584 if (warm) {
2585 clear_port_feature(hub->hdev, port1,
2586 USB_PORT_FEAT_C_BH_PORT_RESET);
2587 clear_port_feature(hub->hdev, port1,
2588 USB_PORT_FEAT_C_PORT_LINK_STATE);
2589 } else {
2590 usb_set_device_state(udev, *status
2591 ? USB_STATE_NOTATTACHED
2592 : USB_STATE_DEFAULT);
2593 }
2594 break;
2595 }
2596}
2597
2598/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002600 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
2602 int i, status;
2603
Andiry Xu75d7cf72011-09-13 16:41:11 -07002604 if (!warm) {
2605 /* Block EHCI CF initialization during the port reset.
2606 * Some companion controllers don't like it when they mix.
2607 */
2608 down_read(&ehci_cf_port_reset_rwsem);
2609 } else {
2610 if (!hub_is_superspeed(hub->hdev)) {
2611 dev_err(hub->intfdev, "only USB3 hub support "
2612 "warm reset\n");
2613 return -EINVAL;
2614 }
2615 }
Alan Stern32fe0192007-10-10 16:27:07 -04002616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 /* Reset the port */
2618 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002619 status = set_port_feature(hub->hdev, port1, (warm ?
2620 USB_PORT_FEAT_BH_PORT_RESET :
2621 USB_PORT_FEAT_RESET));
2622 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002624 "cannot %sreset port %d (err = %d)\n",
2625 warm ? "warm " : "", port1, status);
2626 } else {
2627 status = hub_port_wait_reset(hub, port1, udev, delay,
2628 warm);
David Brownelldd165252005-08-31 10:45:25 -07002629 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 dev_dbg(hub->intfdev,
2631 "port_wait_reset: err = %d\n",
2632 status);
2633 }
2634
2635 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002636 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2637 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002638 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
2640
2641 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002642 "port %d not enabled, trying %sreset again...\n",
2643 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 delay = HUB_LONG_RESET_TIME;
2645 }
2646
2647 dev_err (hub->intfdev,
2648 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2649 port1);
2650
Andiry Xu75d7cf72011-09-13 16:41:11 -07002651done:
2652 if (!warm)
2653 up_read(&ehci_cf_port_reset_rwsem);
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 return status;
2656}
2657
Andiry Xu0ed9a572011-04-27 18:07:43 +08002658/* Check if a port is power on */
2659static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2660{
2661 int ret = 0;
2662
2663 if (hub_is_superspeed(hub->hdev)) {
2664 if (portstatus & USB_SS_PORT_STAT_POWER)
2665 ret = 1;
2666 } else {
2667 if (portstatus & USB_PORT_STAT_POWER)
2668 ret = 1;
2669 }
2670
2671 return ret;
2672}
2673
Alan Sternd388dab2006-07-01 22:14:24 -04002674#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Andiry Xu0ed9a572011-04-27 18:07:43 +08002676/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2677static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2678{
2679 int ret = 0;
2680
2681 if (hub_is_superspeed(hub->hdev)) {
2682 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2683 == USB_SS_PORT_LS_U3)
2684 ret = 1;
2685 } else {
2686 if (portstatus & USB_PORT_STAT_SUSPEND)
2687 ret = 1;
2688 }
2689
2690 return ret;
2691}
Alan Sternb01b03f2008-04-28 11:06:11 -04002692
2693/* Determine whether the device on a port is ready for a normal resume,
2694 * is ready for a reset-resume, or should be disconnected.
2695 */
2696static int check_port_resume_type(struct usb_device *udev,
2697 struct usb_hub *hub, int port1,
2698 int status, unsigned portchange, unsigned portstatus)
2699{
2700 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002701 if (status || port_is_suspended(hub, portstatus) ||
2702 !port_is_power_on(hub, portstatus) ||
2703 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002704 if (status >= 0)
2705 status = -ENODEV;
2706 }
2707
Alan Stern86c57ed2008-06-30 11:14:43 -04002708 /* Can't do a normal resume if the port isn't enabled,
2709 * so try a reset-resume instead.
2710 */
2711 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2712 if (udev->persist_enabled)
2713 udev->reset_resume = 1;
2714 else
2715 status = -ENODEV;
2716 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002717
2718 if (status) {
2719 dev_dbg(hub->intfdev,
2720 "port %d status %04x.%04x after resume, %d\n",
2721 port1, portchange, portstatus, status);
2722 } else if (udev->reset_resume) {
2723
2724 /* Late port handoff can set status-change bits */
2725 if (portchange & USB_PORT_STAT_C_CONNECTION)
2726 clear_port_feature(hub->hdev, port1,
2727 USB_PORT_FEAT_C_CONNECTION);
2728 if (portchange & USB_PORT_STAT_C_ENABLE)
2729 clear_port_feature(hub->hdev, port1,
2730 USB_PORT_FEAT_C_ENABLE);
2731 }
2732
2733 return status;
2734}
2735
Sarah Sharpf74631e2012-06-25 12:08:08 -07002736int usb_disable_ltm(struct usb_device *udev)
2737{
2738 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2739
2740 /* Check if the roothub and device supports LTM. */
2741 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2742 !usb_device_supports_ltm(udev))
2743 return 0;
2744
2745 /* Clear Feature LTM Enable can only be sent if the device is
2746 * configured.
2747 */
2748 if (!udev->actconfig)
2749 return 0;
2750
2751 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2752 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2753 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2754 USB_CTRL_SET_TIMEOUT);
2755}
2756EXPORT_SYMBOL_GPL(usb_disable_ltm);
2757
2758void usb_enable_ltm(struct usb_device *udev)
2759{
2760 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2761
2762 /* Check if the roothub and device supports LTM. */
2763 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2764 !usb_device_supports_ltm(udev))
2765 return;
2766
2767 /* Set Feature LTM Enable can only be sent if the device is
2768 * configured.
2769 */
2770 if (!udev->actconfig)
2771 return;
2772
2773 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2774 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2775 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2776 USB_CTRL_SET_TIMEOUT);
2777}
2778EXPORT_SYMBOL_GPL(usb_enable_ltm);
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780#ifdef CONFIG_USB_SUSPEND
2781
2782/*
Alan Stern140d8f62006-07-01 22:07:21 -04002783 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002784 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002785 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 *
2787 * Suspends a USB device that isn't in active use, conserving power.
2788 * Devices may wake out of a suspend, if anything important happens,
2789 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002790 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 * to disconnect devices while they are suspended.
2792 *
David Brownell390a8c32005-09-13 19:57:27 -07002793 * This only affects the USB hardware for a device; its interfaces
2794 * (and, for hubs, child devices) must already have been suspended.
2795 *
Alan Stern624d6c02007-05-30 15:35:16 -04002796 * Selective port suspend reduces power; most suspended devices draw
2797 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2798 * All devices below the suspended port are also suspended.
2799 *
2800 * Devices leave suspend state when the host wakes them up. Some devices
2801 * also support "remote wakeup", where the device can activate the USB
2802 * tree above them to deliver data, such as a keypress or packet. In
2803 * some cases, this wakes the USB host.
2804 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 * Suspending OTG devices may trigger HNP, if that's been enabled
2806 * between a pair of dual-role devices. That will change roles, such
2807 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2808 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002809 * Devices on USB hub ports have only one "suspend" state, corresponding
2810 * to ACPI D2, "may cause the device to lose some context".
2811 * State transitions include:
2812 *
2813 * - suspend, resume ... when the VBUS power link stays live
2814 * - suspend, disconnect ... VBUS lost
2815 *
2816 * Once VBUS drop breaks the circuit, the port it's using has to go through
2817 * normal re-enumeration procedures, starting with enabling VBUS power.
2818 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2819 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2820 * timer, no SRP, no requests through sysfs.
2821 *
2822 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2823 * the root hub for their bus goes into global suspend ... so we don't
2824 * (falsely) update the device power state to say it suspended.
2825 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 * Returns 0 on success, else negative errno.
2827 */
Alan Stern65bfd292008-11-25 16:39:18 -05002828int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Alan Stern624d6c02007-05-30 15:35:16 -04002830 struct usb_hub *hub = hdev_to_hub(udev->parent);
2831 int port1 = udev->portnum;
2832 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002833
Alan Stern624d6c02007-05-30 15:35:16 -04002834 /* enable remote wakeup when appropriate; this lets the device
2835 * wake up the upstream hub (including maybe the root hub).
2836 *
2837 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2838 * we don't explicitly enable it here.
2839 */
2840 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002841 if (!hub_is_superspeed(hub->hdev)) {
2842 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2843 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2844 USB_DEVICE_REMOTE_WAKEUP, 0,
2845 NULL, 0,
2846 USB_CTRL_SET_TIMEOUT);
2847 } else {
2848 /* Assume there's only one function on the USB 3.0
2849 * device and enable remote wake for the first
2850 * interface. FIXME if the interface association
2851 * descriptor shows there's more than one function.
2852 */
2853 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2854 USB_REQ_SET_FEATURE,
2855 USB_RECIP_INTERFACE,
2856 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002857 USB_INTRF_FUNC_SUSPEND_RW |
2858 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002859 NULL, 0,
2860 USB_CTRL_SET_TIMEOUT);
2861 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002862 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002863 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2864 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002865 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002866 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002867 return status;
2868 }
Alan Stern624d6c02007-05-30 15:35:16 -04002869 }
2870
Andiry Xu65580b432011-09-23 14:19:52 -07002871 /* disable USB2 hardware LPM */
2872 if (udev->usb2_hw_lpm_enabled == 1)
2873 usb_set_usb2_hardware_lpm(udev, 0);
2874
Sarah Sharpf74631e2012-06-25 12:08:08 -07002875 if (usb_disable_ltm(udev)) {
2876 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2877 __func__);
2878 return -ENOMEM;
2879 }
Sarah Sharp83060952012-05-02 14:25:52 -07002880 if (usb_unlocked_disable_lpm(udev)) {
2881 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2882 __func__);
2883 return -ENOMEM;
2884 }
2885
Alan Stern624d6c02007-05-30 15:35:16 -04002886 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002887 if (hub_is_superspeed(hub->hdev))
2888 status = set_port_feature(hub->hdev,
2889 port1 | (USB_SS_PORT_LS_U3 << 3),
2890 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002891 else
2892 status = set_port_feature(hub->hdev, port1,
2893 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002894 if (status) {
2895 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2896 port1, status);
2897 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002898 if (udev->do_remote_wakeup)
2899 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002900 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2901 USB_DEVICE_REMOTE_WAKEUP, 0,
2902 NULL, 0,
2903 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002904
Andiry Xuc3e751e2012-05-05 00:50:10 +08002905 /* Try to enable USB2 hardware LPM again */
2906 if (udev->usb2_hw_lpm_capable == 1)
2907 usb_set_usb2_hardware_lpm(udev, 1);
2908
Sarah Sharpf74631e2012-06-25 12:08:08 -07002909 /* Try to enable USB3 LTM and LPM again */
2910 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002911 usb_unlocked_enable_lpm(udev);
2912
Alan Sterncbb33002011-06-15 16:29:16 -04002913 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002914 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002915 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002916 } else {
2917 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002918 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2919 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2920 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002921 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Sternbfd1e912012-10-19 11:03:39 -04002922 udev->port_is_suspended = 1;
Alan Stern624d6c02007-05-30 15:35:16 -04002923 msleep(10);
2924 }
Alan Sternc08512c2010-11-15 15:57:58 -05002925 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002926 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
David Brownellf3f32532005-09-22 22:37:29 -07002928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929/*
David Brownell390a8c32005-09-13 19:57:27 -07002930 * If the USB "suspend" state is in use (rather than "global suspend"),
2931 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002932 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 * hardware resume signaling is finished, either because of selective
2934 * resume (by host) or remote wakeup (by device) ... now see what changed
2935 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002936 *
2937 * If @udev->reset_resume is set then the device is reset before the
2938 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 */
Alan Stern140d8f62006-07-01 22:07:21 -04002940static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Alan Stern54515fe2007-05-30 15:38:58 -04002942 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 u16 devstatus;
2944
2945 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002946 dev_dbg(&udev->dev, "%s\n",
2947 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 /* usb ch9 identifies four variants of SUSPENDED, based on what
2950 * state the device resumes to. Linux currently won't see the
2951 * first two on the host side; they'd be inside hub_port_init()
2952 * during many timeouts, but khubd can't suspend until later.
2953 */
2954 usb_set_device_state(udev, udev->actconfig
2955 ? USB_STATE_CONFIGURED
2956 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Alan Stern54515fe2007-05-30 15:38:58 -04002958 /* 10.5.4.5 says not to reset a suspended port if the attached
2959 * device is enabled for remote wakeup. Hence the reset
2960 * operation is carried out here, after the port has been
2961 * resumed.
2962 */
2963 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002964 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002965 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 /* 10.5.4.5 says be sure devices in the tree are still there.
2968 * For now let's assume the device didn't go crazy on resume,
2969 * and device drivers will know about any resume quirks.
2970 */
Alan Stern54515fe2007-05-30 15:38:58 -04002971 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002972 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002973 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2974 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002975 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002976
2977 /* If a normal resume failed, try doing a reset-resume */
2978 if (status && !udev->reset_resume && udev->persist_enabled) {
2979 dev_dbg(&udev->dev, "retry with reset-resume\n");
2980 udev->reset_resume = 1;
2981 goto retry_reset_resume;
2982 }
Alan Stern54515fe2007-05-30 15:38:58 -04002983 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002984
Alan Stern624d6c02007-05-30 15:35:16 -04002985 if (status) {
2986 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2987 status);
2988 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002990 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 status = usb_control_msg(udev,
2992 usb_sndctrlpipe(udev, 0),
2993 USB_REQ_CLEAR_FEATURE,
2994 USB_RECIP_DEVICE,
2995 USB_DEVICE_REMOTE_WAKEUP, 0,
2996 NULL, 0,
2997 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002998 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002999 dev_dbg(&udev->dev,
3000 "disable remote wakeup, status %d\n",
3001 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005 return status;
3006}
3007
Alan Stern624d6c02007-05-30 15:35:16 -04003008/*
3009 * usb_port_resume - re-activate a suspended usb device's upstream port
3010 * @udev: device to re-activate, not a root hub
3011 * Context: must be able to sleep; device not locked; pm locks held
3012 *
3013 * This will re-activate the suspended device, increasing power usage
3014 * while letting drivers communicate again with its endpoints.
3015 * USB resume explicitly guarantees that the power session between
3016 * the host and the device is the same as it was when the device
3017 * suspended.
3018 *
Alan Sternfeccc302008-03-03 15:15:59 -05003019 * If @udev->reset_resume is set then this routine won't check that the
3020 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003021 * reset @udev. The end result is that a broken power session can be
3022 * recovered and @udev will appear to persist across a loss of VBUS power.
3023 *
3024 * For example, if a host controller doesn't maintain VBUS suspend current
3025 * during a system sleep or is reset when the system wakes up, all the USB
3026 * power sessions below it will be broken. This is especially troublesome
3027 * for mass-storage devices containing mounted filesystems, since the
3028 * device will appear to have disconnected and all the memory mappings
3029 * to it will be lost. Using the USB_PERSIST facility, the device can be
3030 * made to appear as if it had not disconnected.
3031 *
Ming Lei742120c2008-06-18 22:00:29 +08003032 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003033 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003034 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3035 * quite possible for a device to remain unaltered but its media to be
3036 * changed. If the user replaces a flash memory card while the system is
3037 * asleep, he will have only himself to blame when the filesystem on the
3038 * new card is corrupted and the system crashes.
3039 *
Alan Stern624d6c02007-05-30 15:35:16 -04003040 * Returns 0 on success, else negative errno.
3041 */
Alan Stern65bfd292008-11-25 16:39:18 -05003042int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Alan Stern624d6c02007-05-30 15:35:16 -04003044 struct usb_hub *hub = hdev_to_hub(udev->parent);
3045 int port1 = udev->portnum;
3046 int status;
3047 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003048
3049 /* Skip the initial Clear-Suspend step for a remote wakeup */
3050 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003051 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003052 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
3054 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3055
Alan Sternd5cbad42006-08-11 16:52:39 -04003056 set_bit(port1, hub->busy_bits);
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003059 if (hub_is_superspeed(hub->hdev))
3060 status = set_port_feature(hub->hdev,
3061 port1 | (USB_SS_PORT_LS_U0 << 3),
3062 USB_PORT_FEAT_LINK_STATE);
3063 else
3064 status = clear_port_feature(hub->hdev,
3065 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003067 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3068 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003071 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003072 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 msleep(25);
3074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3076 * stop resume signaling. Then finish the resume
3077 * sequence.
3078 */
Alan Sternd25450c2006-11-20 11:14:30 -05003079 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003080
Alan Sternb01b03f2008-04-28 11:06:11 -04003081 /* TRSMRCY = 10 msec */
3082 msleep(10);
3083 }
Alan Stern54515fe2007-05-30 15:38:58 -04003084
Alan Sternb01b03f2008-04-28 11:06:11 -04003085 SuspendCleared:
3086 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003087 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003088 if (hub_is_superspeed(hub->hdev)) {
3089 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3090 clear_port_feature(hub->hdev, port1,
3091 USB_PORT_FEAT_C_PORT_LINK_STATE);
3092 } else {
3093 if (portchange & USB_PORT_STAT_C_SUSPEND)
3094 clear_port_feature(hub->hdev, port1,
3095 USB_PORT_FEAT_C_SUSPEND);
3096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Alan Sternd5cbad42006-08-11 16:52:39 -04003099 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003100
Alan Sternb01b03f2008-04-28 11:06:11 -04003101 status = check_port_resume_type(udev,
3102 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003103 if (status == 0)
3104 status = finish_port_resume(udev);
3105 if (status < 0) {
3106 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3107 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003108 } else {
3109 /* Try to enable USB2 hardware LPM */
3110 if (udev->usb2_hw_lpm_capable == 1)
3111 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003112
Sarah Sharpf74631e2012-06-25 12:08:08 -07003113 /* Try to enable USB3 LTM and LPM */
3114 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003115 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003116 }
Andiry Xu65580b432011-09-23 14:19:52 -07003117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 return status;
3119}
3120
Alan Stern8808f002008-04-28 11:06:55 -04003121/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003122int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
3124 int status = 0;
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003127 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003128 status = usb_autoresume_device(udev);
3129 if (status == 0) {
3130 /* Let the drivers do their thing, then... */
3131 usb_autosuspend_device(udev);
3132 }
Alan Sternd25450c2006-11-20 11:14:30 -05003133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return status;
3135}
3136
Alan Sternd388dab2006-07-01 22:14:24 -04003137#else /* CONFIG_USB_SUSPEND */
3138
3139/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3140
Alan Stern65bfd292008-11-25 16:39:18 -05003141int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003142{
3143 return 0;
3144}
3145
Alan Sternb01b03f2008-04-28 11:06:11 -04003146/* However we may need to do a reset-resume */
3147
Alan Stern65bfd292008-11-25 16:39:18 -05003148int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003149{
Alan Sternb01b03f2008-04-28 11:06:11 -04003150 struct usb_hub *hub = hdev_to_hub(udev->parent);
3151 int port1 = udev->portnum;
3152 int status;
3153 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003154
Alan Sternb01b03f2008-04-28 11:06:11 -04003155 status = hub_port_status(hub, port1, &portstatus, &portchange);
3156 status = check_port_resume_type(udev,
3157 hub, port1, status, portchange, portstatus);
3158
3159 if (status) {
3160 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3161 hub_port_logical_disconnect(hub, port1);
3162 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003163 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003164 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003165 }
3166 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003167}
3168
Alan Sternd388dab2006-07-01 22:14:24 -04003169#endif
3170
Ming Leie6f30de2012-10-24 11:59:24 +08003171static int check_ports_changed(struct usb_hub *hub)
3172{
3173 int port1;
3174
3175 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3176 u16 portstatus, portchange;
3177 int status;
3178
3179 status = hub_port_status(hub, port1, &portstatus, &portchange);
3180 if (!status && portchange)
3181 return 1;
3182 }
3183 return 0;
3184}
3185
David Brownelldb690872005-09-13 19:56:33 -07003186static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187{
3188 struct usb_hub *hub = usb_get_intfdata (intf);
3189 struct usb_device *hdev = hub->hdev;
3190 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003191 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Alan Sterncbb33002011-06-15 16:29:16 -04003193 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3195 struct usb_device *udev;
3196
Lan Tianyuff823c72012-09-05 13:44:32 +08003197 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003198 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003199 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003200 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003201 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Ming Leie6f30de2012-10-24 11:59:24 +08003204
3205 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3206 /* check if there are changes pending on hub ports */
3207 if (check_ports_changed(hub)) {
3208 if (PMSG_IS_AUTO(msg))
3209 return -EBUSY;
3210 pm_wakeup_event(&hdev->dev, 2000);
3211 }
3212 }
3213
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003214 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3215 /* Enable hub to send remote wakeup for all ports. */
3216 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3217 status = set_port_feature(hdev,
3218 port1 |
3219 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3220 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3221 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3222 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3223 }
3224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
Harvey Harrison441b62c2008-03-03 16:08:34 -08003226 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003227
David Brownellc9f89fa2005-09-13 19:57:04 -07003228 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003229 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231}
3232
3233static int hub_resume(struct usb_interface *intf)
3234{
Alan Stern5e6effa2008-03-03 15:15:51 -05003235 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
Alan Stern5e6effa2008-03-03 15:15:51 -05003237 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003238 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return 0;
3240}
3241
Alan Sternb41a60e2007-05-30 15:39:33 -04003242static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003243{
Alan Sternb41a60e2007-05-30 15:39:33 -04003244 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003245
Alan Stern5e6effa2008-03-03 15:15:51 -05003246 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003247 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003248 return 0;
3249}
3250
Alan Stern54515fe2007-05-30 15:38:58 -04003251/**
3252 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3253 * @rhdev: struct usb_device for the root hub
3254 *
3255 * The USB host controller driver calls this function when its root hub
3256 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003257 * has been reset. The routine marks @rhdev as having lost power.
3258 * When the hub driver is resumed it will take notice and carry out
3259 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3260 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003261 */
3262void usb_root_hub_lost_power(struct usb_device *rhdev)
3263{
3264 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3265 rhdev->reset_resume = 1;
3266}
3267EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3268
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003269static const char * const usb3_lpm_names[] = {
3270 "U0",
3271 "U1",
3272 "U2",
3273 "U3",
3274};
3275
3276/*
3277 * Send a Set SEL control transfer to the device, prior to enabling
3278 * device-initiated U1 or U2. This lets the device know the exit latencies from
3279 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3280 * packet from the host.
3281 *
3282 * This function will fail if the SEL or PEL values for udev are greater than
3283 * the maximum allowed values for the link state to be enabled.
3284 */
3285static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3286{
3287 struct usb_set_sel_req *sel_values;
3288 unsigned long long u1_sel;
3289 unsigned long long u1_pel;
3290 unsigned long long u2_sel;
3291 unsigned long long u2_pel;
3292 int ret;
3293
3294 /* Convert SEL and PEL stored in ns to us */
3295 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3296 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3297 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3298 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3299
3300 /*
3301 * Make sure that the calculated SEL and PEL values for the link
3302 * state we're enabling aren't bigger than the max SEL/PEL
3303 * value that will fit in the SET SEL control transfer.
3304 * Otherwise the device would get an incorrect idea of the exit
3305 * latency for the link state, and could start a device-initiated
3306 * U1/U2 when the exit latencies are too high.
3307 */
3308 if ((state == USB3_LPM_U1 &&
3309 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3310 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3311 (state == USB3_LPM_U2 &&
3312 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3313 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003314 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 -07003315 usb3_lpm_names[state], u1_sel, u1_pel);
3316 return -EINVAL;
3317 }
3318
3319 /*
3320 * If we're enabling device-initiated LPM for one link state,
3321 * but the other link state has a too high SEL or PEL value,
3322 * just set those values to the max in the Set SEL request.
3323 */
3324 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3325 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3326
3327 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3328 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3329
3330 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3331 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3332
3333 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3334 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3335
3336 /*
3337 * usb_enable_lpm() can be called as part of a failed device reset,
3338 * which may be initiated by an error path of a mass storage driver.
3339 * Therefore, use GFP_NOIO.
3340 */
3341 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3342 if (!sel_values)
3343 return -ENOMEM;
3344
3345 sel_values->u1_sel = u1_sel;
3346 sel_values->u1_pel = u1_pel;
3347 sel_values->u2_sel = cpu_to_le16(u2_sel);
3348 sel_values->u2_pel = cpu_to_le16(u2_pel);
3349
3350 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3351 USB_REQ_SET_SEL,
3352 USB_RECIP_DEVICE,
3353 0, 0,
3354 sel_values, sizeof *(sel_values),
3355 USB_CTRL_SET_TIMEOUT);
3356 kfree(sel_values);
3357 return ret;
3358}
3359
3360/*
3361 * Enable or disable device-initiated U1 or U2 transitions.
3362 */
3363static int usb_set_device_initiated_lpm(struct usb_device *udev,
3364 enum usb3_link_state state, bool enable)
3365{
3366 int ret;
3367 int feature;
3368
3369 switch (state) {
3370 case USB3_LPM_U1:
3371 feature = USB_DEVICE_U1_ENABLE;
3372 break;
3373 case USB3_LPM_U2:
3374 feature = USB_DEVICE_U2_ENABLE;
3375 break;
3376 default:
3377 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3378 __func__, enable ? "enable" : "disable");
3379 return -EINVAL;
3380 }
3381
3382 if (udev->state != USB_STATE_CONFIGURED) {
3383 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3384 "for unconfigured device.\n",
3385 __func__, enable ? "enable" : "disable",
3386 usb3_lpm_names[state]);
3387 return 0;
3388 }
3389
3390 if (enable) {
3391 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003392 * Now send the control transfer to enable device-initiated LPM
3393 * for either U1 or U2.
3394 */
3395 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3396 USB_REQ_SET_FEATURE,
3397 USB_RECIP_DEVICE,
3398 feature,
3399 0, NULL, 0,
3400 USB_CTRL_SET_TIMEOUT);
3401 } else {
3402 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3403 USB_REQ_CLEAR_FEATURE,
3404 USB_RECIP_DEVICE,
3405 feature,
3406 0, NULL, 0,
3407 USB_CTRL_SET_TIMEOUT);
3408 }
3409 if (ret < 0) {
3410 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3411 enable ? "Enable" : "Disable",
3412 usb3_lpm_names[state]);
3413 return -EBUSY;
3414 }
3415 return 0;
3416}
3417
3418static int usb_set_lpm_timeout(struct usb_device *udev,
3419 enum usb3_link_state state, int timeout)
3420{
3421 int ret;
3422 int feature;
3423
3424 switch (state) {
3425 case USB3_LPM_U1:
3426 feature = USB_PORT_FEAT_U1_TIMEOUT;
3427 break;
3428 case USB3_LPM_U2:
3429 feature = USB_PORT_FEAT_U2_TIMEOUT;
3430 break;
3431 default:
3432 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3433 __func__);
3434 return -EINVAL;
3435 }
3436
3437 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3438 timeout != USB3_LPM_DEVICE_INITIATED) {
3439 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3440 "which is a reserved value.\n",
3441 usb3_lpm_names[state], timeout);
3442 return -EINVAL;
3443 }
3444
3445 ret = set_port_feature(udev->parent,
3446 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3447 feature);
3448 if (ret < 0) {
3449 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3450 "error code %i\n", usb3_lpm_names[state],
3451 timeout, ret);
3452 return -EBUSY;
3453 }
3454 if (state == USB3_LPM_U1)
3455 udev->u1_params.timeout = timeout;
3456 else
3457 udev->u2_params.timeout = timeout;
3458 return 0;
3459}
3460
3461/*
3462 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3463 * U1/U2 entry.
3464 *
3465 * We will attempt to enable U1 or U2, but there are no guarantees that the
3466 * control transfers to set the hub timeout or enable device-initiated U1/U2
3467 * will be successful.
3468 *
3469 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3470 * driver know about it. If that call fails, it should be harmless, and just
3471 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3472 */
3473static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3474 enum usb3_link_state state)
3475{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003476 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003477 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3478 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3479
3480 /* If the device says it doesn't have *any* exit latency to come out of
3481 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3482 * state.
3483 */
3484 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3485 (state == USB3_LPM_U2 && u2_mel == 0))
3486 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003487
Sarah Sharp65a95b72012-10-05 10:32:07 -07003488 /*
3489 * First, let the device know about the exit latencies
3490 * associated with the link state we're about to enable.
3491 */
3492 ret = usb_req_set_sel(udev, state);
3493 if (ret < 0) {
3494 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3495 usb3_lpm_names[state]);
3496 return;
3497 }
3498
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003499 /* We allow the host controller to set the U1/U2 timeout internally
3500 * first, so that it can change its schedule to account for the
3501 * additional latency to send data to a device in a lower power
3502 * link state.
3503 */
3504 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3505
3506 /* xHCI host controller doesn't want to enable this LPM state. */
3507 if (timeout == 0)
3508 return;
3509
3510 if (timeout < 0) {
3511 dev_warn(&udev->dev, "Could not enable %s link state, "
3512 "xHCI error %i.\n", usb3_lpm_names[state],
3513 timeout);
3514 return;
3515 }
3516
3517 if (usb_set_lpm_timeout(udev, state, timeout))
3518 /* If we can't set the parent hub U1/U2 timeout,
3519 * device-initiated LPM won't be allowed either, so let the xHCI
3520 * host know that this link state won't be enabled.
3521 */
3522 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3523
3524 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3525 else if (udev->actconfig)
3526 usb_set_device_initiated_lpm(udev, state, true);
3527
3528}
3529
3530/*
3531 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3532 * U1/U2 entry.
3533 *
3534 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3535 * If zero is returned, the parent will not allow the link to go into U1/U2.
3536 *
3537 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3538 * it won't have an effect on the bus link state because the parent hub will
3539 * still disallow device-initiated U1/U2 entry.
3540 *
3541 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3542 * possible. The result will be slightly more bus bandwidth will be taken up
3543 * (to account for U1/U2 exit latency), but it should be harmless.
3544 */
3545static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3546 enum usb3_link_state state)
3547{
3548 int feature;
3549
3550 switch (state) {
3551 case USB3_LPM_U1:
3552 feature = USB_PORT_FEAT_U1_TIMEOUT;
3553 break;
3554 case USB3_LPM_U2:
3555 feature = USB_PORT_FEAT_U2_TIMEOUT;
3556 break;
3557 default:
3558 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3559 __func__);
3560 return -EINVAL;
3561 }
3562
3563 if (usb_set_lpm_timeout(udev, state, 0))
3564 return -EBUSY;
3565
3566 usb_set_device_initiated_lpm(udev, state, false);
3567
3568 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3569 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3570 "bus schedule bandwidth may be impacted.\n",
3571 usb3_lpm_names[state]);
3572 return 0;
3573}
3574
3575/*
3576 * Disable hub-initiated and device-initiated U1 and U2 entry.
3577 * Caller must own the bandwidth_mutex.
3578 *
3579 * This will call usb_enable_lpm() on failure, which will decrement
3580 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3581 */
3582int usb_disable_lpm(struct usb_device *udev)
3583{
3584 struct usb_hcd *hcd;
3585
3586 if (!udev || !udev->parent ||
3587 udev->speed != USB_SPEED_SUPER ||
3588 !udev->lpm_capable)
3589 return 0;
3590
3591 hcd = bus_to_hcd(udev->bus);
3592 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3593 return 0;
3594
3595 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003596 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003597 return 0;
3598
3599 /* If LPM is enabled, attempt to disable it. */
3600 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3601 goto enable_lpm;
3602 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3603 goto enable_lpm;
3604
3605 return 0;
3606
3607enable_lpm:
3608 usb_enable_lpm(udev);
3609 return -EBUSY;
3610}
3611EXPORT_SYMBOL_GPL(usb_disable_lpm);
3612
3613/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3614int usb_unlocked_disable_lpm(struct usb_device *udev)
3615{
3616 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3617 int ret;
3618
3619 if (!hcd)
3620 return -EINVAL;
3621
3622 mutex_lock(hcd->bandwidth_mutex);
3623 ret = usb_disable_lpm(udev);
3624 mutex_unlock(hcd->bandwidth_mutex);
3625
3626 return ret;
3627}
3628EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3629
3630/*
3631 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3632 * xHCI host policy may prevent U1 or U2 from being enabled.
3633 *
3634 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3635 * until the lpm_disable_count drops to zero. Caller must own the
3636 * bandwidth_mutex.
3637 */
3638void usb_enable_lpm(struct usb_device *udev)
3639{
3640 struct usb_hcd *hcd;
3641
3642 if (!udev || !udev->parent ||
3643 udev->speed != USB_SPEED_SUPER ||
3644 !udev->lpm_capable)
3645 return;
3646
3647 udev->lpm_disable_count--;
3648 hcd = bus_to_hcd(udev->bus);
3649 /* Double check that we can both enable and disable LPM.
3650 * Device must be configured to accept set feature U1/U2 timeout.
3651 */
3652 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3653 !hcd->driver->disable_usb3_lpm_timeout)
3654 return;
3655
3656 if (udev->lpm_disable_count > 0)
3657 return;
3658
3659 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3660 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3661}
3662EXPORT_SYMBOL_GPL(usb_enable_lpm);
3663
3664/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3665void usb_unlocked_enable_lpm(struct usb_device *udev)
3666{
3667 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3668
3669 if (!hcd)
3670 return;
3671
3672 mutex_lock(hcd->bandwidth_mutex);
3673 usb_enable_lpm(udev);
3674 mutex_unlock(hcd->bandwidth_mutex);
3675}
3676EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3677
3678
Alan Sternd388dab2006-07-01 22:14:24 -04003679#else /* CONFIG_PM */
3680
Alan Sternf07600c2007-05-30 15:38:16 -04003681#define hub_suspend NULL
3682#define hub_resume NULL
3683#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003684
3685int usb_disable_lpm(struct usb_device *udev)
3686{
3687 return 0;
3688}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003689EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003690
3691void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003692EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003693
3694int usb_unlocked_disable_lpm(struct usb_device *udev)
3695{
3696 return 0;
3697}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003698EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003699
3700void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003701EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003702
3703int usb_disable_ltm(struct usb_device *udev)
3704{
3705 return 0;
3706}
3707EXPORT_SYMBOL_GPL(usb_disable_ltm);
3708
3709void usb_enable_ltm(struct usb_device *udev) { }
3710EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003711#endif
3712
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
3714/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3715 *
3716 * Between connect detection and reset signaling there must be a delay
3717 * of 100ms at least for debounce and power-settling. The corresponding
3718 * timer shall restart whenever the downstream port detects a disconnect.
3719 *
3720 * Apparently there are some bluetooth and irda-dongles and a number of
3721 * low-speed devices for which this debounce period may last over a second.
3722 * Not covered by the spec - but easy to deal with.
3723 *
3724 * This implementation uses a 1500ms total debounce timeout; if the
3725 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3726 * every 25ms for transient disconnects. When the port status has been
3727 * unchanged for 100ms it returns the port status.
3728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729static int hub_port_debounce(struct usb_hub *hub, int port1)
3730{
3731 int ret;
3732 int total_time, stable_time = 0;
3733 u16 portchange, portstatus;
3734 unsigned connection = 0xffff;
3735
3736 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3737 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3738 if (ret < 0)
3739 return ret;
3740
3741 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3742 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3743 stable_time += HUB_DEBOUNCE_STEP;
3744 if (stable_time >= HUB_DEBOUNCE_STABLE)
3745 break;
3746 } else {
3747 stable_time = 0;
3748 connection = portstatus & USB_PORT_STAT_CONNECTION;
3749 }
3750
3751 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3752 clear_port_feature(hub->hdev, port1,
3753 USB_PORT_FEAT_C_CONNECTION);
3754 }
3755
3756 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3757 break;
3758 msleep(HUB_DEBOUNCE_STEP);
3759 }
3760
3761 dev_dbg (hub->intfdev,
3762 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3763 port1, total_time, stable_time, portstatus);
3764
3765 if (stable_time < HUB_DEBOUNCE_STABLE)
3766 return -ETIMEDOUT;
3767 return portstatus;
3768}
3769
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003770void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771{
Alan Sternddeac4e72009-01-15 17:03:33 -05003772 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3773 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003774 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003776EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3779#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3780
Alan Stern4326ed02007-07-30 17:08:43 -04003781static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782{
3783 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003784 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
Sarah Sharpc6515272009-04-27 19:57:26 -07003786 /*
3787 * The host controller will choose the device address,
3788 * instead of the core having chosen it earlier
3789 */
3790 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return -EINVAL;
3792 if (udev->state == USB_STATE_ADDRESS)
3793 return 0;
3794 if (udev->state != USB_STATE_DEFAULT)
3795 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003796 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003797 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003798 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003799 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3800 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3801 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003803 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003804 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003806 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
3808 return retval;
3809}
3810
3811/* Reset device, (re)assign address, get device descriptor.
3812 * Device connection must be stable, no more debouncing needed.
3813 * Returns device in USB_STATE_ADDRESS, except on error.
3814 *
3815 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003816 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 * newly detected device that is not accessible through any global
3818 * pointers, it's not necessary to lock the device.
3819 */
3820static int
3821hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3822 int retry_counter)
3823{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003824 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
3826 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003827 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 int i, j, retval;
3829 unsigned delay = HUB_SHORT_RESET_TIME;
3830 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003831 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003832 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
3834 /* root hub ports have a slightly longer reset period
3835 * (from USB 2.0 spec, section 7.1.7.5)
3836 */
3837 if (!hdev->parent) {
3838 delay = HUB_ROOT_RESET_TIME;
3839 if (port1 == hdev->bus->otg_port)
3840 hdev->bus->b_hnp_enable = 0;
3841 }
3842
3843 /* Some low speed devices have problems with the quick delay, so */
3844 /* be a bit pessimistic with those devices. RHbug #23670 */
3845 if (oldspeed == USB_SPEED_LOW)
3846 delay = HUB_LONG_RESET_TIME;
3847
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003848 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Luben Tuikov07194ab2011-02-11 11:33:10 -08003850 /* Reset the device; full speed may morph to high speed */
3851 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003852 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003853 if (retval < 0) /* error or disconnect */
3854 goto fail;
3855 /* success, speed is known */
3856
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 retval = -ENODEV;
3858
3859 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3860 dev_dbg(&udev->dev, "device reset changed speed!\n");
3861 goto fail;
3862 }
3863 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3866 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003867 * For Wireless USB devices, ep0 max packet is always 512 (tho
3868 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 */
3870 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003871 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003872 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003873 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003874 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003876 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 break;
3878 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3879 /* to determine the ep0 maxpacket size, try to read
3880 * the device descriptor to get bMaxPacketSize0 and
3881 * then correct our initial guess.
3882 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003883 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 break;
3885 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003886 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 break;
3888 default:
3889 goto fail;
3890 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003891
3892 if (udev->speed == USB_SPEED_WIRELESS)
3893 speed = "variable speed Wireless";
3894 else
3895 speed = usb_speed_string(udev->speed);
3896
Sarah Sharpc6515272009-04-27 19:57:26 -07003897 if (udev->speed != USB_SPEED_SUPER)
3898 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003899 "%s %s USB device number %d using %s\n",
3900 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003901 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
3903 /* Set up TT records, if needed */
3904 if (hdev->tt) {
3905 udev->tt = hdev->tt;
3906 udev->ttport = hdev->ttport;
3907 } else if (udev->speed != USB_SPEED_HIGH
3908 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003909 if (!hub->tt.hub) {
3910 dev_err(&udev->dev, "parent hub has no TT\n");
3911 retval = -EINVAL;
3912 goto fail;
3913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 udev->tt = &hub->tt;
3915 udev->ttport = port1;
3916 }
3917
3918 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3919 * Because device hardware and firmware is sometimes buggy in
3920 * this area, and this is how Linux has done it for ages.
3921 * Change it cautiously.
3922 *
3923 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3924 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3925 * so it may help with some non-standards-compliant devices.
3926 * Otherwise we start with SET_ADDRESS and then try to read the
3927 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3928 * value.
3929 */
3930 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003931 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 struct usb_device_descriptor *buf;
3933 int r = 0;
3934
3935#define GET_DESCRIPTOR_BUFSIZE 64
3936 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3937 if (!buf) {
3938 retval = -ENOMEM;
3939 continue;
3940 }
3941
Alan Sternb89ee192007-05-11 10:19:04 -04003942 /* Retry on all errors; some devices are flakey.
3943 * 255 is for WUSB devices, we actually need to use
3944 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 */
3946 for (j = 0; j < 3; ++j) {
3947 buf->bMaxPacketSize0 = 0;
3948 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3949 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3950 USB_DT_DEVICE << 8, 0,
3951 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003952 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003954 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (buf->bDescriptorType ==
3956 USB_DT_DEVICE) {
3957 r = 0;
3958 break;
3959 }
3960 /* FALL THROUGH */
3961 default:
3962 if (r == 0)
3963 r = -EPROTO;
3964 break;
3965 }
3966 if (r == 0)
3967 break;
3968 }
3969 udev->descriptor.bMaxPacketSize0 =
3970 buf->bMaxPacketSize0;
3971 kfree(buf);
3972
Andiry Xu75d7cf72011-09-13 16:41:11 -07003973 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 if (retval < 0) /* error or disconnect */
3975 goto fail;
3976 if (oldspeed != udev->speed) {
3977 dev_dbg(&udev->dev,
3978 "device reset changed speed!\n");
3979 retval = -ENODEV;
3980 goto fail;
3981 }
3982 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003983 dev_err(&udev->dev,
3984 "device descriptor read/64, error %d\n",
3985 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 retval = -EMSGSIZE;
3987 continue;
3988 }
3989#undef GET_DESCRIPTOR_BUFSIZE
3990 }
3991
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003992 /*
3993 * If device is WUSB, we already assigned an
3994 * unauthorized address in the Connect Ack sequence;
3995 * authorization will assign the final address.
3996 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003997 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003998 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3999 retval = hub_set_address(udev, devnum);
4000 if (retval >= 0)
4001 break;
4002 msleep(200);
4003 }
4004 if (retval < 0) {
4005 dev_err(&udev->dev,
4006 "device not accepting address %d, error %d\n",
4007 devnum, retval);
4008 goto fail;
4009 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004010 if (udev->speed == USB_SPEED_SUPER) {
4011 devnum = udev->devnum;
4012 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004013 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004014 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004015 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004016 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004017
4018 /* cope with hardware quirkiness:
4019 * - let SET_ADDRESS settle, some device hardware wants it
4020 * - read ep0 maxpacket even for high and low speed,
4021 */
4022 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07004023 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
4027 retval = usb_get_device_descriptor(udev, 8);
4028 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004029 dev_err(&udev->dev,
4030 "device descriptor read/8, error %d\n",
4031 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (retval >= 0)
4033 retval = -EMSGSIZE;
4034 } else {
4035 retval = 0;
4036 break;
4037 }
4038 }
4039 if (retval)
4040 goto fail;
4041
Elric Fud8aec3d2012-03-26 21:16:02 +08004042 /*
4043 * Some superspeed devices have finished the link training process
4044 * and attached to a superspeed hub port, but the device descriptor
4045 * got from those devices show they aren't superspeed devices. Warm
4046 * reset the port attached by the devices can fix them.
4047 */
4048 if ((udev->speed == USB_SPEED_SUPER) &&
4049 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4050 dev_err(&udev->dev, "got a wrong device descriptor, "
4051 "warm reset device\n");
4052 hub_port_reset(hub, port1, udev,
4053 HUB_BH_RESET_TIME, true);
4054 retval = -EINVAL;
4055 goto fail;
4056 }
4057
Sarah Sharp6b403b02009-04-27 19:54:10 -07004058 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4059 udev->speed == USB_SPEED_SUPER)
4060 i = 512;
4061 else
4062 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004063 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004064 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004066 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 retval = -EMSGSIZE;
4068 goto fail;
4069 }
Alan Stern56626a72010-10-14 15:25:21 -04004070 if (udev->speed == USB_SPEED_FULL)
4071 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4072 else
4073 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004075 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 }
4077
4078 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4079 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004080 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4081 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 if (retval >= 0)
4083 retval = -ENOMSG;
4084 goto fail;
4085 }
4086
Andiry Xu1ff4df52011-09-23 14:19:48 -07004087 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4088 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004089 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004090 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004091 usb_set_lpm_parameters(udev);
4092 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004093 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004096 /* notify HCD that we have a device connected and addressed */
4097 if (hcd->driver->update_device)
4098 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004100 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004102 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004103 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004104 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return retval;
4106}
4107
4108static void
4109check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4110{
4111 struct usb_qualifier_descriptor *qual;
4112 int status;
4113
Christoph Lametere94b1762006-12-06 20:33:17 -08004114 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 if (qual == NULL)
4116 return;
4117
4118 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4119 qual, sizeof *qual);
4120 if (status == sizeof *qual) {
4121 dev_info(&udev->dev, "not running at top speed; "
4122 "connect to a high speed hub\n");
4123 /* hub LEDs are probably harder to miss than syslog */
4124 if (hub->has_indicators) {
4125 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004126 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 }
4128 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004129 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
4132static unsigned
4133hub_power_remaining (struct usb_hub *hub)
4134{
4135 struct usb_device *hdev = hub->hdev;
4136 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004137 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
Alan Stern55c52712005-11-23 12:03:12 -05004139 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return 0;
4141
Alan Stern55c52712005-11-23 12:03:12 -05004142 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4143 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08004144 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004145 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
4147 if (!udev)
4148 continue;
4149
Alan Stern55c52712005-11-23 12:03:12 -05004150 /* Unconfigured devices may not use more than 100mA,
4151 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004153 delta = udev->actconfig->desc.bMaxPower * 2;
4154 else if (port1 != udev->bus->otg_port || hdev->parent)
4155 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 else
Alan Stern55c52712005-11-23 12:03:12 -05004157 delta = 8;
4158 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004159 dev_warn(&udev->dev,
4160 "%dmA is over %umA budget for port %d!\n",
4161 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 remaining -= delta;
4163 }
4164 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004165 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4166 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 remaining = 0;
4168 }
4169 return remaining;
4170}
4171
4172/* Handle physical or logical connection change events.
4173 * This routine is called when:
4174 * a port connection-change occurs;
4175 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004176 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 * a firmware download)
4178 * caller already locked the hub
4179 */
4180static void hub_port_connect_change(struct usb_hub *hub, int port1,
4181 u16 portstatus, u16 portchange)
4182{
4183 struct usb_device *hdev = hub->hdev;
4184 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304185 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004186 unsigned wHubCharacteristics =
4187 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004188 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 dev_dbg (hub_dev,
4192 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004193 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
4195 if (hub->has_indicators) {
4196 set_port_led(hub, port1, HUB_LED_AUTO);
4197 hub->indicator[port1-1] = INDICATOR_AUTO;
4198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200#ifdef CONFIG_USB_OTG
4201 /* during HNP, don't repeat the debounce */
4202 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004203 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4204 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205#endif
4206
Alan Stern8808f002008-04-28 11:06:55 -04004207 /* Try to resuscitate an existing device */
Lan Tianyuff823c72012-09-05 13:44:32 +08004208 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004209 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4210 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004211 usb_lock_device(udev);
4212 if (portstatus & USB_PORT_STAT_ENABLE) {
4213 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004214
4215#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004216 } else if (udev->state == USB_STATE_SUSPENDED &&
4217 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004218 /* For a suspended device, treat this as a
4219 * remote wakeup event.
4220 */
Alan Stern0534d462010-01-08 12:56:30 -05004221 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004222#endif
4223
4224 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004225 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004226 }
4227 usb_unlock_device(udev);
4228
4229 if (status == 0) {
4230 clear_bit(port1, hub->change_bits);
4231 return;
4232 }
4233 }
4234
Alan Stern24618b02008-04-28 11:06:28 -04004235 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004236 if (udev)
Lan Tianyuff823c72012-09-05 13:44:32 +08004237 usb_disconnect(&hub->ports[port1 - 1]->child);
Alan Stern24618b02008-04-28 11:06:28 -04004238 clear_bit(port1, hub->change_bits);
4239
Alan Stern253e0572009-10-27 15:20:13 -04004240 /* We can forget about a "removed" device when there's a physical
4241 * disconnect or the connect status changes.
4242 */
4243 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4244 (portchange & USB_PORT_STAT_C_CONNECTION))
4245 clear_bit(port1, hub->removed_bits);
4246
Alan Stern5257d972008-09-22 14:43:08 -04004247 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4248 USB_PORT_STAT_C_ENABLE)) {
4249 status = hub_port_debounce(hub, port1);
4250 if (status < 0) {
4251 if (printk_ratelimit())
4252 dev_err(hub_dev, "connect-debounce failed, "
4253 "port %d disabled\n", port1);
4254 portstatus &= ~USB_PORT_STAT_CONNECTION;
4255 } else {
4256 portstatus = status;
4257 }
4258 }
4259
Richard Zhao925aa462012-07-11 11:09:28 +08004260 if (hcd->phy && !hdev->parent) {
4261 if (portstatus & USB_PORT_STAT_CONNECTION)
4262 usb_phy_notify_connect(hcd->phy, port1);
4263 else
4264 usb_phy_notify_disconnect(hcd->phy, port1);
4265 }
4266
Alan Stern253e0572009-10-27 15:20:13 -04004267 /* Return now if debouncing failed or nothing is connected or
4268 * the device was "removed".
4269 */
4270 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4271 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
4273 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004274 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004275 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 if (portstatus & USB_PORT_STAT_ENABLE)
4279 goto done;
4280 return;
4281 }
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 /* reallocate for each attempt, since references
4286 * to the previous one can escape in various ways
4287 */
4288 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4289 if (!udev) {
4290 dev_err (hub_dev,
4291 "couldn't allocate port %d usb_device\n",
4292 port1);
4293 goto done;
4294 }
4295
4296 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004297 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004298 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004299 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004300
Sarah Sharp131dec32010-12-06 21:00:19 -08004301 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4302 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004303 udev->speed = USB_SPEED_SUPER;
4304 else
4305 udev->speed = USB_SPEED_UNKNOWN;
4306
Alan Stern3b29b682011-02-22 09:53:41 -05004307 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004308 if (udev->devnum <= 0) {
4309 status = -ENOTCONN; /* Don't retry */
4310 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004311 }
4312
Sarah Sharpe7b77172009-04-27 19:54:26 -07004313 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 status = hub_port_init(hub, udev, port1, i);
4315 if (status < 0)
4316 goto loop;
4317
Phil Dibowitz93362a82010-07-22 00:05:01 +02004318 usb_detect_quirks(udev);
4319 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4320 msleep(1000);
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 /* consecutive bus-powered hubs aren't reliable; they can
4323 * violate the voltage drop budget. if the new child has
4324 * a "powered" LED, users should notice we didn't enable it
4325 * (without reading syslog), even without per-port LEDs
4326 * on the parent.
4327 */
4328 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004329 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 u16 devstat;
4331
4332 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4333 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004334 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 dev_dbg(&udev->dev, "get status %d ?\n", status);
4336 goto loop_disable;
4337 }
Alan Stern55c52712005-11-23 12:03:12 -05004338 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4340 dev_err(&udev->dev,
4341 "can't connect bus-powered hub "
4342 "to this port\n");
4343 if (hub->has_indicators) {
4344 hub->indicator[port1-1] =
4345 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004346 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 }
4348 status = -ENOTCONN; /* Don't retry */
4349 goto loop_disable;
4350 }
4351 }
4352
4353 /* check for devices running slower than they could */
4354 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4355 && udev->speed == USB_SPEED_FULL
4356 && highspeed_hubs != 0)
4357 check_highspeed (hub, udev, port1);
4358
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004359 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 * udev becomes globally accessible, although presumably
4361 * no one will look at it until hdev is unlocked.
4362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 status = 0;
4364
4365 /* We mustn't add new devices if the parent hub has
4366 * been disconnected; we would race with the
4367 * recursively_mark_NOTATTACHED() routine.
4368 */
4369 spin_lock_irq(&device_state_lock);
4370 if (hdev->state == USB_STATE_NOTATTACHED)
4371 status = -ENOTCONN;
4372 else
Lan Tianyuff823c72012-09-05 13:44:32 +08004373 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 spin_unlock_irq(&device_state_lock);
4375
4376 /* Run it through the hoops (find a driver, etc) */
4377 if (!status) {
4378 status = usb_new_device(udev);
4379 if (status) {
4380 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c72012-09-05 13:44:32 +08004381 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 spin_unlock_irq(&device_state_lock);
4383 }
4384 }
4385
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 if (status)
4387 goto loop_disable;
4388
4389 status = hub_power_remaining(hub);
4390 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004391 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
4393 return;
4394
4395loop_disable:
4396 hub_port_disable(hub, port1, 1);
4397loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004398 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004399 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004400 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004402 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 break;
4404 }
Alan Stern3a311552008-05-20 16:58:29 -04004405 if (hub->hdev->parent ||
4406 !hcd->driver->port_handed_over ||
4407 !(hcd->driver->port_handed_over)(hcd, port1))
4408 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4409 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410
4411done:
4412 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304413 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4414 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415}
4416
Sarah Sharp714b07b2012-01-24 13:53:18 -08004417/* Returns 1 if there was a remote wakeup and a connect status change. */
4418static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004419 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004420{
4421 struct usb_device *hdev;
4422 struct usb_device *udev;
4423 int connect_change = 0;
4424 int ret;
4425
4426 hdev = hub->hdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08004427 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004428 if (!hub_is_superspeed(hdev)) {
4429 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4430 return 0;
4431 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4432 } else {
4433 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004434 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4435 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004436 return 0;
4437 }
4438
Sarah Sharp714b07b2012-01-24 13:53:18 -08004439 if (udev) {
4440 /* TRSMRCY = 10 msec */
4441 msleep(10);
4442
4443 usb_lock_device(udev);
4444 ret = usb_remote_wakeup(udev);
4445 usb_unlock_device(udev);
4446 if (ret < 0)
4447 connect_change = 1;
4448 } else {
4449 ret = -ENODEV;
4450 hub_port_disable(hub, port, 1);
4451 }
4452 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4453 port, ret);
4454 return connect_change;
4455}
4456
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457static void hub_events(void)
4458{
4459 struct list_head *tmp;
4460 struct usb_device *hdev;
4461 struct usb_interface *intf;
4462 struct usb_hub *hub;
4463 struct device *hub_dev;
4464 u16 hubstatus;
4465 u16 hubchange;
4466 u16 portstatus;
4467 u16 portchange;
4468 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004469 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471 /*
4472 * We restart the list every time to avoid a deadlock with
4473 * deleting hubs downstream from this one. This should be
4474 * safe since we delete the hub from the event list.
4475 * Not the most efficient, but avoids deadlocks.
4476 */
4477 while (1) {
4478
4479 /* Grab the first entry at the beginning of the list */
4480 spin_lock_irq(&hub_event_lock);
4481 if (list_empty(&hub_event_list)) {
4482 spin_unlock_irq(&hub_event_lock);
4483 break;
4484 }
4485
4486 tmp = hub_event_list.next;
4487 list_del_init(tmp);
4488
4489 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004490 kref_get(&hub->kref);
4491 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
Alan Sterne8054852007-05-04 11:55:11 -04004493 hdev = hub->hdev;
4494 hub_dev = hub->intfdev;
4495 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004496 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 hdev->state, hub->descriptor
4498 ? hub->descriptor->bNbrPorts
4499 : 0,
4500 /* NOTE: expects max 15 ports... */
4501 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004502 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 /* Lock the device, then check to see if we were
4505 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004506 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004507 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004508 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509
4510 /* If the hub has died, clean up after it */
4511 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004512 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004513 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 goto loop;
4515 }
4516
Alan Stern40f122f2006-11-09 14:44:33 -05004517 /* Autoresume */
4518 ret = usb_autopm_get_interface(intf);
4519 if (ret) {
4520 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004522 }
4523
4524 /* If this is an inactive hub, do nothing */
4525 if (hub->quiescing)
4526 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
4528 if (hub->error) {
4529 dev_dbg (hub_dev, "resetting for error %d\n",
4530 hub->error);
4531
Ming Lei742120c2008-06-18 22:00:29 +08004532 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 if (ret) {
4534 dev_dbg (hub_dev,
4535 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004536 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 }
4538
4539 hub->nerrors = 0;
4540 hub->error = 0;
4541 }
4542
4543 /* deal with port status changes */
4544 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4545 if (test_bit(i, hub->busy_bits))
4546 continue;
4547 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004548 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004550 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 continue;
4552
4553 ret = hub_port_status(hub, i,
4554 &portstatus, &portchange);
4555 if (ret < 0)
4556 continue;
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4559 clear_port_feature(hdev, i,
4560 USB_PORT_FEAT_C_CONNECTION);
4561 connect_change = 1;
4562 }
4563
4564 if (portchange & USB_PORT_STAT_C_ENABLE) {
4565 if (!connect_change)
4566 dev_dbg (hub_dev,
4567 "port %d enable change, "
4568 "status %08x\n",
4569 i, portstatus);
4570 clear_port_feature(hdev, i,
4571 USB_PORT_FEAT_C_ENABLE);
4572
4573 /*
4574 * EM interference sometimes causes badly
4575 * shielded USB devices to be shutdown by
4576 * the hub, this hack enables them again.
4577 * Works at least with mouse driver.
4578 */
4579 if (!(portstatus & USB_PORT_STAT_ENABLE)
4580 && !connect_change
Lan Tianyuff823c72012-09-05 13:44:32 +08004581 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 dev_err (hub_dev,
4583 "port %i "
4584 "disabled by hub (EMI?), "
4585 "re-enabling...\n",
4586 i);
4587 connect_change = 1;
4588 }
4589 }
4590
Sarah Sharp72937e12012-01-24 11:46:50 -08004591 if (hub_handle_remote_wakeup(hub, i,
4592 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004593 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004594
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004596 u16 status = 0;
4597 u16 unused;
4598
4599 dev_dbg(hub_dev, "over-current change on port "
4600 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 clear_port_feature(hdev, i,
4602 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004603 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004604 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004605 hub_port_status(hub, i, &status, &unused);
4606 if (status & USB_PORT_STAT_OVERCURRENT)
4607 dev_err(hub_dev, "over-current "
4608 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
4610
4611 if (portchange & USB_PORT_STAT_C_RESET) {
4612 dev_dbg (hub_dev,
4613 "reset change on port %d\n",
4614 i);
4615 clear_port_feature(hdev, i,
4616 USB_PORT_FEAT_C_RESET);
4617 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004618 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4619 hub_is_superspeed(hub->hdev)) {
4620 dev_dbg(hub_dev,
4621 "warm reset change on port %d\n",
4622 i);
4623 clear_port_feature(hdev, i,
4624 USB_PORT_FEAT_C_BH_PORT_RESET);
4625 }
John Youndbe79bb2001-09-17 00:00:00 -07004626 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4627 clear_port_feature(hub->hdev, i,
4628 USB_PORT_FEAT_C_PORT_LINK_STATE);
4629 }
4630 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4631 dev_warn(hub_dev,
4632 "config error on port %d\n",
4633 i);
4634 clear_port_feature(hub->hdev, i,
4635 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Andiry Xu5e467f62011-04-27 18:07:54 +08004638 /* Warm reset a USB3 protocol port if it's in
4639 * SS.Inactive state.
4640 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004641 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004642 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004643 hub_port_reset(hub, i, NULL,
4644 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004645 }
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 if (connect_change)
4648 hub_port_connect_change(hub, i,
4649 portstatus, portchange);
4650 } /* end for i */
4651
4652 /* deal with hub status changes */
4653 if (test_and_clear_bit(0, hub->event_bits) == 0)
4654 ; /* do nothing */
4655 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4656 dev_err (hub_dev, "get_hub_status failed\n");
4657 else {
4658 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4659 dev_dbg (hub_dev, "power change\n");
4660 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004661 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4662 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004663 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004664 else
4665 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
4667 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004668 u16 status = 0;
4669 u16 unused;
4670
4671 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004673 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004674 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004675 hub_hub_status(hub, &status, &unused);
4676 if (status & HUB_STATUS_OVERCURRENT)
4677 dev_err(hub_dev, "over-current "
4678 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 }
4680 }
4681
Alan Stern8e4ceb32009-12-07 13:01:37 -05004682 loop_autopm:
4683 /* Balance the usb_autopm_get_interface() above */
4684 usb_autopm_put_interface_no_suspend(intf);
4685 loop:
4686 /* Balance the usb_autopm_get_interface_no_resume() in
4687 * kick_khubd() and allow autosuspend.
4688 */
4689 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004690 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004692 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
4694 } /* end while (1) */
4695}
4696
4697static int hub_thread(void *__unused)
4698{
Alan Stern3bb1af52008-03-03 15:15:36 -05004699 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4700 * port handover. Otherwise it might see that a full-speed device
4701 * was gone before the EHCI controller had handed its port over to
4702 * the companion full-speed controller.
4703 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004704 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004705
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 do {
4707 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004708 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004709 !list_empty(&hub_event_list) ||
4710 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004711 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004713 pr_debug("%s: khubd exiting\n", usbcore_name);
4714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715}
4716
Németh Márton1e927d92010-01-10 15:34:53 +01004717static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08004718 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4719 | USB_DEVICE_ID_MATCH_INT_CLASS,
4720 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4721 .bInterfaceClass = USB_CLASS_HUB,
4722 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4724 .bDeviceClass = USB_CLASS_HUB},
4725 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4726 .bInterfaceClass = USB_CLASS_HUB},
4727 { } /* Terminating entry */
4728};
4729
4730MODULE_DEVICE_TABLE (usb, hub_id_table);
4731
4732static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 .name = "hub",
4734 .probe = hub_probe,
4735 .disconnect = hub_disconnect,
4736 .suspend = hub_suspend,
4737 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004738 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004739 .pre_reset = hub_pre_reset,
4740 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004741 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004743 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744};
4745
4746int usb_hub_init(void)
4747{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 if (usb_register(&hub_driver) < 0) {
4749 printk(KERN_ERR "%s: can't register hub driver\n",
4750 usbcore_name);
4751 return -1;
4752 }
4753
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004754 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4755 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 /* Fall through if kernel_thread failed */
4759 usb_deregister(&hub_driver);
4760 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4761
4762 return -1;
4763}
4764
4765void usb_hub_cleanup(void)
4766{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004767 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
4769 /*
4770 * Hub resources are freed for us by usb_deregister. It calls
4771 * usb_driver_purge on every device which in turn calls that
4772 * devices disconnect function if it is using this driver.
4773 * The hub_disconnect function takes care of releasing the
4774 * individual hub resources. -greg
4775 */
4776 usb_deregister(&hub_driver);
4777} /* usb_hub_cleanup() */
4778
Alan Sterneb764c42008-03-03 15:16:04 -05004779static int descriptors_changed(struct usb_device *udev,
4780 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Alan Sterneb764c42008-03-03 15:16:04 -05004782 int changed = 0;
4783 unsigned index;
4784 unsigned serial_len = 0;
4785 unsigned len;
4786 unsigned old_length;
4787 int length;
4788 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Alan Sterneb764c42008-03-03 15:16:04 -05004790 if (memcmp(&udev->descriptor, old_device_descriptor,
4791 sizeof(*old_device_descriptor)) != 0)
4792 return 1;
4793
4794 /* Since the idVendor, idProduct, and bcdDevice values in the
4795 * device descriptor haven't changed, we will assume the
4796 * Manufacturer and Product strings haven't changed either.
4797 * But the SerialNumber string could be different (e.g., a
4798 * different flash card of the same brand).
4799 */
4800 if (udev->serial)
4801 serial_len = strlen(udev->serial) + 1;
4802
4803 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004805 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4806 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 }
Alan Sterneb764c42008-03-03 15:16:04 -05004808
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004809 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 if (buf == NULL) {
4811 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4812 /* assume the worst */
4813 return 1;
4814 }
4815 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004816 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4818 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004819 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 dev_dbg(&udev->dev, "config index %d, error %d\n",
4821 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004822 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 break;
4824 }
4825 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4826 != 0) {
4827 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004828 index,
4829 ((struct usb_config_descriptor *) buf)->
4830 bConfigurationValue);
4831 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 break;
4833 }
4834 }
Alan Sterneb764c42008-03-03 15:16:04 -05004835
4836 if (!changed && serial_len) {
4837 length = usb_string(udev, udev->descriptor.iSerialNumber,
4838 buf, serial_len);
4839 if (length + 1 != serial_len) {
4840 dev_dbg(&udev->dev, "serial string error %d\n",
4841 length);
4842 changed = 1;
4843 } else if (memcmp(buf, udev->serial, length) != 0) {
4844 dev_dbg(&udev->dev, "serial string changed\n");
4845 changed = 1;
4846 }
4847 }
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004850 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
4853/**
Ming Lei742120c2008-06-18 22:00:29 +08004854 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4856 *
Alan Stern79efa092006-06-01 13:33:42 -04004857 * WARNING - don't use this routine to reset a composite device
4858 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004859 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 *
4861 * Do a port reset, reassign the device's address, and establish its
4862 * former operating configuration. If the reset fails, or the device's
4863 * descriptors change from their values before the reset, or the original
4864 * configuration and altsettings cannot be restored, a flag will be set
4865 * telling khubd to pretend the device has been disconnected and then
4866 * re-connected. All drivers will be unbound, and the device will be
4867 * re-enumerated and probed all over again.
4868 *
4869 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4870 * flagged for logical disconnection, or some other negative error code
4871 * if the reset wasn't even attempted.
4872 *
4873 * The caller must own the device lock. For example, it's safe to use
4874 * this from a driver probe() routine after downloading new firmware.
4875 * For calls that might not occur during probe(), drivers should lock
4876 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004877 *
4878 * Locking exception: This routine may also be called from within an
4879 * autoresume handler. Such usage won't conflict with other tasks
4880 * holding the device lock because these tasks should always call
4881 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 */
Ming Lei742120c2008-06-18 22:00:29 +08004883static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
4885 struct usb_device *parent_hdev = udev->parent;
4886 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004887 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004889 int i, ret = 0;
4890 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
4892 if (udev->state == USB_STATE_NOTATTACHED ||
4893 udev->state == USB_STATE_SUSPENDED) {
4894 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4895 udev->state);
4896 return -EINVAL;
4897 }
4898
4899 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004900 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004901 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 return -EISDIR;
4903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 parent_hub = hdev_to_hub(parent_hdev);
4905
Sarah Sharpf74631e2012-06-25 12:08:08 -07004906 /* Disable LPM and LTM while we reset the device and reinstall the alt
4907 * settings. Device-initiated LPM settings, and system exit latency
4908 * settings are cleared when the device is reset, so we have to set
4909 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004910 */
4911 ret = usb_unlocked_disable_lpm(udev);
4912 if (ret) {
4913 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4914 goto re_enumerate;
4915 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004916 ret = usb_disable_ltm(udev);
4917 if (ret) {
4918 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4919 __func__);
4920 goto re_enumerate;
4921 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 set_bit(port1, parent_hub->busy_bits);
4924 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4925
4926 /* ep0 maxpacket size may change; let the HCD know about it.
4927 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004928 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004930 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 break;
4932 }
4933 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004934
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 if (ret < 0)
4936 goto re_enumerate;
4937
4938 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004939 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 dev_info(&udev->dev, "device firmware changed\n");
4941 udev->descriptor = descriptor; /* for disconnect() calls */
4942 goto re_enumerate;
4943 }
Alan Stern4fe03872009-02-26 10:21:02 -05004944
4945 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 if (!udev->actconfig)
4947 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004948
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004949 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004950 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4951 if (ret < 0) {
4952 dev_warn(&udev->dev,
4953 "Busted HC? Not enough HCD resources for "
4954 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004955 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004956 goto re_enumerate;
4957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4959 USB_REQ_SET_CONFIGURATION, 0,
4960 udev->actconfig->desc.bConfigurationValue, 0,
4961 NULL, 0, USB_CTRL_SET_TIMEOUT);
4962 if (ret < 0) {
4963 dev_err(&udev->dev,
4964 "can't restore configuration #%d (error=%d)\n",
4965 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004966 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 goto re_enumerate;
4968 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004969 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4971
Alan Stern4fe03872009-02-26 10:21:02 -05004972 /* Put interfaces back into the same altsettings as before.
4973 * Don't bother to send the Set-Interface request for interfaces
4974 * that were already in altsetting 0; besides being unnecessary,
4975 * many devices can't handle it. Instead just reset the host-side
4976 * endpoint state.
4977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004979 struct usb_host_config *config = udev->actconfig;
4980 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 struct usb_interface_descriptor *desc;
4982
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004984 if (desc->bAlternateSetting == 0) {
4985 usb_disable_interface(udev, intf, true);
4986 usb_enable_interface(udev, intf, true);
4987 ret = 0;
4988 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004989 /* Let the bandwidth allocation function know that this
4990 * device has been reset, and it will have to use
4991 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004992 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004993 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004994 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4995 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004996 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 if (ret < 0) {
4999 dev_err(&udev->dev, "failed to restore interface %d "
5000 "altsetting %d (error=%d)\n",
5001 desc->bInterfaceNumber,
5002 desc->bAlternateSetting,
5003 ret);
5004 goto re_enumerate;
5005 }
5006 }
5007
5008done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005009 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04005010 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005011 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 return 0;
5013
5014re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005015 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 hub_port_logical_disconnect(parent_hub, port1);
5017 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018}
5019
5020/**
5021 * usb_reset_device - warn interface drivers and perform a USB port reset
5022 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5023 *
Alan Stern79efa092006-06-01 13:33:42 -04005024 * Warns all drivers bound to registered interfaces (using their pre_reset
5025 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005026 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005027 *
Alan Stern79efa092006-06-01 13:33:42 -04005028 * Return value is the same as for usb_reset_and_verify_device().
5029 *
5030 * The caller must own the device lock. For example, it's safe to use
5031 * this from a driver probe() routine after downloading new firmware.
5032 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005033 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005034 *
5035 * If an interface is currently being probed or disconnected, we assume
5036 * its driver knows how to handle resets. For all other interfaces,
5037 * if the driver doesn't have pre_reset and post_reset methods then
5038 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005039 */
Ming Lei742120c2008-06-18 22:00:29 +08005040int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005041{
5042 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005043 int i;
Alan Stern79efa092006-06-01 13:33:42 -04005044 struct usb_host_config *config = udev->actconfig;
5045
5046 if (udev->state == USB_STATE_NOTATTACHED ||
5047 udev->state == USB_STATE_SUSPENDED) {
5048 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5049 udev->state);
5050 return -EINVAL;
5051 }
5052
Alan Stern645daaa2006-08-30 15:47:02 -04005053 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005054 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005055
Alan Stern79efa092006-06-01 13:33:42 -04005056 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005057 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005058 struct usb_interface *cintf = config->interface[i];
5059 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005060 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005061
5062 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005063 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005064 if (drv->pre_reset && drv->post_reset)
5065 unbind = (drv->pre_reset)(cintf);
5066 else if (cintf->condition ==
5067 USB_INTERFACE_BOUND)
5068 unbind = 1;
5069 if (unbind)
5070 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005071 }
5072 }
5073 }
5074
Ming Lei742120c2008-06-18 22:00:29 +08005075 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005076
5077 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005078 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005079 struct usb_interface *cintf = config->interface[i];
5080 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005081 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005082
Alan Stern78d9a482008-06-23 16:00:40 -04005083 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005084 drv = to_usb_driver(cintf->dev.driver);
5085 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005086 rebind = (drv->post_reset)(cintf);
5087 else if (cintf->condition ==
5088 USB_INTERFACE_BOUND)
5089 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005090 }
Alan Stern6c640942008-10-21 15:40:03 -04005091 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005092 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005093 }
5094 }
5095
Alan Stern94fcda12006-11-20 11:38:46 -05005096 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005097 return ret;
5098}
Ming Lei742120c2008-06-18 22:00:29 +08005099EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005100
5101
5102/**
5103 * usb_queue_reset_device - Reset a USB device from an atomic context
5104 * @iface: USB interface belonging to the device to reset
5105 *
5106 * This function can be used to reset a USB device from an atomic
5107 * context, where usb_reset_device() won't work (as it blocks).
5108 *
5109 * Doing a reset via this method is functionally equivalent to calling
5110 * usb_reset_device(), except for the fact that it is delayed to a
5111 * workqueue. This means that any drivers bound to other interfaces
5112 * might be unbound, as well as users from usbfs in user space.
5113 *
5114 * Corner cases:
5115 *
5116 * - Scheduling two resets at the same time from two different drivers
5117 * attached to two different interfaces of the same device is
5118 * possible; depending on how the driver attached to each interface
5119 * handles ->pre_reset(), the second reset might happen or not.
5120 *
5121 * - If a driver is unbound and it had a pending reset, the reset will
5122 * be cancelled.
5123 *
5124 * - This function can be called during .probe() or .disconnect()
5125 * times. On return from .disconnect(), any pending resets will be
5126 * cancelled.
5127 *
5128 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5129 * does its own.
5130 *
5131 * NOTE: We don't do any reference count tracking because it is not
5132 * needed. The lifecycle of the work_struct is tied to the
5133 * usb_interface. Before destroying the interface we cancel the
5134 * work_struct, so the fact that work_struct is queued and or
5135 * running means the interface (and thus, the device) exist and
5136 * are referenced.
5137 */
5138void usb_queue_reset_device(struct usb_interface *iface)
5139{
5140 schedule_work(&iface->reset_ws);
5141}
5142EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005143
5144/**
5145 * usb_hub_find_child - Get the pointer of child device
5146 * attached to the port which is specified by @port1.
5147 * @hdev: USB device belonging to the usb hub
5148 * @port1: port num to indicate which port the child device
5149 * is attached to.
5150 *
5151 * USB drivers call this function to get hub's child device
5152 * pointer.
5153 *
5154 * Return NULL if input param is invalid and
5155 * child's usb_device pointer if non-NULL.
5156 */
5157struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5158 int port1)
5159{
5160 struct usb_hub *hub = hdev_to_hub(hdev);
5161
5162 if (port1 < 1 || port1 > hdev->maxchild)
5163 return NULL;
5164 return hub->ports[port1 - 1]->child;
5165}
5166EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005167
Lan Tianyu05f91682012-09-05 13:44:34 +08005168/**
5169 * usb_set_hub_port_connect_type - set hub port connect type.
5170 * @hdev: USB device belonging to the usb hub
5171 * @port1: port num of the port
5172 * @type: connect type of the port
5173 */
5174void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5175 enum usb_port_connect_type type)
5176{
5177 struct usb_hub *hub = hdev_to_hub(hdev);
5178
5179 hub->ports[port1 - 1]->connect_type = type;
5180}
5181
5182/**
5183 * usb_get_hub_port_connect_type - Get the port's connect type
5184 * @hdev: USB device belonging to the usb hub
5185 * @port1: port num of the port
5186 *
5187 * Return connect type of the port and if input params are
5188 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5189 */
5190enum usb_port_connect_type
5191usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5192{
5193 struct usb_hub *hub = hdev_to_hub(hdev);
5194
5195 return hub->ports[port1 - 1]->connect_type;
5196}
5197
Lan Tianyud5575422012-09-05 13:44:33 +08005198#ifdef CONFIG_ACPI
5199/**
5200 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5201 * @hdev: USB device belonging to the usb hub
5202 * @port1: port num of the port
5203 *
5204 * Return port's acpi handle if successful, NULL if params are
5205 * invaild.
5206 */
5207acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5208 int port1)
5209{
5210 struct usb_hub *hub = hdev_to_hub(hdev);
5211
5212 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5213}
5214#endif