blob: ff86355d0dfcc11b292fc34f89d8624f8cab7991 [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 Tianyuff823c792012-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 Tianyuff823c792012-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 Tianyuff823c792012-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 Tianyuff823c792012-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 Tianyuff823c792012-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 Tianyuff823c792012-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
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001617 /* Hubs have proper suspend/resume support. */
1618 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001619
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001620 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001621 dev_err(&intf->dev,
1622 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001623 return -E2BIG;
1624 }
1625
David Brownell89ccbdc2006-04-02 10:18:09 -08001626#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1627 if (hdev->parent) {
1628 dev_warn(&intf->dev, "ignoring external hub\n");
1629 return -ENODEV;
1630 }
1631#endif
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* Some hubs have a subclass of 1, which AFAICT according to the */
1634 /* specs is not defined, but it works */
1635 if ((desc->desc.bInterfaceSubClass != 0) &&
1636 (desc->desc.bInterfaceSubClass != 1)) {
1637descriptor_error:
1638 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1639 return -EIO;
1640 }
1641
1642 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1643 if (desc->desc.bNumEndpoints != 1)
1644 goto descriptor_error;
1645
1646 endpoint = &desc->endpoint[0].desc;
1647
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001648 /* If it's not an interrupt in endpoint, we'd better punt! */
1649 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 goto descriptor_error;
1651
1652 /* We found a hub */
1653 dev_info (&intf->dev, "USB hub found\n");
1654
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001655 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (!hub) {
1657 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1658 return -ENOMEM;
1659 }
1660
Alan Sterne8054852007-05-04 11:55:11 -04001661 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 INIT_LIST_HEAD(&hub->event_list);
1663 hub->intfdev = &intf->dev;
1664 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001665 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001666 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001667 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001670 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 if (hdev->speed == USB_SPEED_HIGH)
1673 highspeed_hubs++;
1674
Ming Leie6f30de2012-10-24 11:59:24 +08001675 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1676 hub->quirk_check_port_auto_suspend = 1;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (hub_configure(hub, endpoint) >= 0)
1679 return 0;
1680
1681 hub_disconnect (intf);
1682 return -ENODEV;
1683}
1684
1685static int
1686hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1687{
1688 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuff823c792012-09-05 13:44:32 +08001689 struct usb_hub *hub = hdev_to_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /* assert ifno == 0 (part of hub spec) */
1692 switch (code) {
1693 case USBDEVFS_HUB_PORTINFO: {
1694 struct usbdevfs_hub_portinfo *info = user_data;
1695 int i;
1696
1697 spin_lock_irq(&device_state_lock);
1698 if (hdev->devnum <= 0)
1699 info->nports = 0;
1700 else {
1701 info->nports = hdev->maxchild;
1702 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001703 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 info->port[i] = 0;
1705 else
1706 info->port[i] =
Lan Tianyuff823c792012-09-05 13:44:32 +08001707 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709 }
1710 spin_unlock_irq(&device_state_lock);
1711
1712 return info->nports + 1;
1713 }
1714
1715 default:
1716 return -ENOSYS;
1717 }
1718}
1719
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001720/*
1721 * Allow user programs to claim ports on a hub. When a device is attached
1722 * to one of these "claimed" ports, the program will "own" the device.
1723 */
1724static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001725 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001726{
1727 if (hdev->state == USB_STATE_NOTATTACHED)
1728 return -ENODEV;
1729 if (port1 == 0 || port1 > hdev->maxchild)
1730 return -EINVAL;
1731
1732 /* This assumes that devices not managed by the hub driver
1733 * will always have maxchild equal to 0.
1734 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001735 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001736 return 0;
1737}
1738
1739/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001740int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1741 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001742{
1743 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001744 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001745
1746 rc = find_port_owner(hdev, port1, &powner);
1747 if (rc)
1748 return rc;
1749 if (*powner)
1750 return -EBUSY;
1751 *powner = owner;
1752 return rc;
1753}
1754
Lan Tianyu336c5c32012-07-06 14:13:52 +08001755int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1756 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001757{
1758 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001759 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001760
1761 rc = find_port_owner(hdev, port1, &powner);
1762 if (rc)
1763 return rc;
1764 if (*powner != owner)
1765 return -ENOENT;
1766 *powner = NULL;
1767 return rc;
1768}
1769
Lan Tianyu336c5c32012-07-06 14:13:52 +08001770void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001771{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001772 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001773 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001774
Lan Tianyufa2a9562012-09-05 13:44:31 +08001775 for (n = 0; n < hdev->maxchild; n++) {
1776 if (hub->ports[n]->port_owner == owner)
1777 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001778 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001779
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001780}
1781
1782/* The caller must hold udev's lock */
1783bool usb_device_is_owned(struct usb_device *udev)
1784{
1785 struct usb_hub *hub;
1786
1787 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1788 return false;
1789 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001790 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001791}
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1794{
Lan Tianyuff823c792012-09-05 13:44:32 +08001795 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 int i;
1797
1798 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001799 if (hub->ports[i]->child)
1800 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001802 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001803 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 udev->state = USB_STATE_NOTATTACHED;
1805}
1806
1807/**
1808 * usb_set_device_state - change a device's current state (usbcore, hcds)
1809 * @udev: pointer to device whose state should be changed
1810 * @new_state: new state value to be stored
1811 *
1812 * udev->state is _not_ fully protected by the device lock. Although
1813 * most transitions are made only while holding the lock, the state can
1814 * can change to USB_STATE_NOTATTACHED at almost any time. This
1815 * is so that devices can be marked as disconnected as soon as possible,
1816 * without having to wait for any semaphores to be released. As a result,
1817 * all changes to any device's state must be protected by the
1818 * device_state_lock spinlock.
1819 *
1820 * Once a device has been added to the device tree, all changes to its state
1821 * should be made using this routine. The state should _not_ be set directly.
1822 *
1823 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1824 * Otherwise udev->state is set to new_state, and if new_state is
1825 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1826 * to USB_STATE_NOTATTACHED.
1827 */
1828void usb_set_device_state(struct usb_device *udev,
1829 enum usb_device_state new_state)
1830{
1831 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001832 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 spin_lock_irqsave(&device_state_lock, flags);
1835 if (udev->state == USB_STATE_NOTATTACHED)
1836 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001837 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001838
1839 /* root hub wakeup capabilities are managed out-of-band
1840 * and may involve silicon errata ... ignore them here.
1841 */
1842 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001843 if (udev->state == USB_STATE_SUSPENDED
1844 || new_state == USB_STATE_SUSPENDED)
1845 ; /* No change to wakeup settings */
1846 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001847 wakeup = udev->actconfig->desc.bmAttributes
1848 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001849 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001850 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001851 }
Sarah Sharp15123002007-12-21 16:54:15 -08001852 if (udev->state == USB_STATE_SUSPENDED &&
1853 new_state != USB_STATE_SUSPENDED)
1854 udev->active_duration -= jiffies;
1855 else if (new_state == USB_STATE_SUSPENDED &&
1856 udev->state != USB_STATE_SUSPENDED)
1857 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001858 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001859 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 recursively_mark_NOTATTACHED(udev);
1861 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001862 if (wakeup >= 0)
1863 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
David Vrabel6da9c992009-02-18 14:43:47 +00001865EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001867/*
Alan Stern3b29b682011-02-22 09:53:41 -05001868 * Choose a device number.
1869 *
1870 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1871 * USB-2.0 buses they are also used as device addresses, however on
1872 * USB-3.0 buses the address is assigned by the controller hardware
1873 * and it usually is not the same as the device number.
1874 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001875 * WUSB devices are simple: they have no hubs behind, so the mapping
1876 * device <-> virtual port number becomes 1:1. Why? to simplify the
1877 * life of the device connection logic in
1878 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1879 * handshake we need to assign a temporary address in the unauthorized
1880 * space. For simplicity we use the first virtual port number found to
1881 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1882 * and that becomes it's address [X < 128] or its unauthorized address
1883 * [X | 0x80].
1884 *
1885 * We add 1 as an offset to the one-based USB-stack port number
1886 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1887 * 0 is reserved by USB for default address; (b) Linux's USB stack
1888 * uses always #1 for the root hub of the controller. So USB stack's
1889 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001890 *
1891 * Devices connected under xHCI are not as simple. The host controller
1892 * supports virtualization, so the hardware assigns device addresses and
1893 * the HCD must setup data structures before issuing a set address
1894 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001895 */
Alan Stern3b29b682011-02-22 09:53:41 -05001896static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 int devnum;
1899 struct usb_bus *bus = udev->bus;
1900
1901 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001902 if (udev->wusb) {
1903 devnum = udev->portnum + 1;
1904 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1905 } else {
1906 /* Try to allocate the next devnum beginning at
1907 * bus->devnum_next. */
1908 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1909 bus->devnum_next);
1910 if (devnum >= 128)
1911 devnum = find_next_zero_bit(bus->devmap.devicemap,
1912 128, 1);
1913 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (devnum < 128) {
1916 set_bit(devnum, bus->devmap.devicemap);
1917 udev->devnum = devnum;
1918 }
1919}
1920
Alan Stern3b29b682011-02-22 09:53:41 -05001921static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 if (udev->devnum > 0) {
1924 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1925 udev->devnum = -1;
1926 }
1927}
1928
Alan Stern3b29b682011-02-22 09:53:41 -05001929static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001930{
1931 /* The address for a WUSB device is managed by wusbcore. */
1932 if (!udev->wusb)
1933 udev->devnum = devnum;
1934}
1935
Herbert Xuf7410ce2010-01-10 20:15:03 +11001936static void hub_free_dev(struct usb_device *udev)
1937{
1938 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1939
1940 /* Root hubs aren't real devices, so don't free HCD resources */
1941 if (hcd->driver->free_dev && udev->parent)
1942 hcd->driver->free_dev(hcd, udev);
1943}
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945/**
1946 * usb_disconnect - disconnect a device (usbcore-internal)
1947 * @pdev: pointer to device being disconnected
1948 * Context: !in_interrupt ()
1949 *
1950 * Something got disconnected. Get rid of it and all of its children.
1951 *
1952 * If *pdev is a normal device then the parent hub must already be locked.
1953 * If *pdev is a root hub then this routine will acquire the
1954 * usb_bus_list_lock on behalf of the caller.
1955 *
1956 * Only hub drivers (including virtual root hub drivers for host
1957 * controllers) should ever call this.
1958 *
1959 * This call is synchronous, and may not be used in an interrupt context.
1960 */
1961void usb_disconnect(struct usb_device **pdev)
1962{
1963 struct usb_device *udev = *pdev;
Lan Tianyuff823c792012-09-05 13:44:32 +08001964 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 int i;
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* mark the device as inactive, so any further urb submissions for
1968 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001969 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 */
1971 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001972 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1973 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001975 usb_lock_device(udev);
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001978 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001979 if (hub->ports[i]->child)
1980 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982
1983 /* deallocate hcd/hardware state ... nuking all pending urbs and
1984 * cleaning up all state associated with the current configuration
1985 * so that the hardware is now fully quiesced.
1986 */
Alan Stern782da722006-07-01 22:09:35 -04001987 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001989 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Alan Stern3b23dd62008-12-05 14:10:34 -05001991 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001992 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001993
Alan Stern782da722006-07-01 22:09:35 -04001994 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001995 * for de-configuring the device and invoking the remove-device
1996 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001997 */
1998 device_del(&udev->dev);
1999
2000 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 * (or root_hub) pointer.
2002 */
Alan Stern3b29b682011-02-22 09:53:41 -05002003 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 /* Avoid races with recursively_mark_NOTATTACHED() */
2006 spin_lock_irq(&device_state_lock);
2007 *pdev = NULL;
2008 spin_unlock_irq(&device_state_lock);
2009
Herbert Xuf7410ce2010-01-10 20:15:03 +11002010 hub_free_dev(udev);
2011
Alan Stern782da722006-07-01 22:09:35 -04002012 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
2014
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002015#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016static void show_string(struct usb_device *udev, char *id, char *string)
2017{
2018 if (!string)
2019 return;
2020 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
2021}
2022
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002023static void announce_device(struct usb_device *udev)
2024{
2025 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2026 le16_to_cpu(udev->descriptor.idVendor),
2027 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002028 dev_info(&udev->dev,
2029 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002030 udev->descriptor.iManufacturer,
2031 udev->descriptor.iProduct,
2032 udev->descriptor.iSerialNumber);
2033 show_string(udev, "Product", udev->product);
2034 show_string(udev, "Manufacturer", udev->manufacturer);
2035 show_string(udev, "SerialNumber", udev->serial);
2036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002038static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039#endif
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041#ifdef CONFIG_USB_OTG
2042#include "otg_whitelist.h"
2043#endif
2044
Oliver Neukum3ede7602007-01-11 14:35:50 +01002045/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002046 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002047 * @udev: newly addressed device (in ADDRESS state)
2048 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002049 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002050 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002051static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002053 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055#ifdef CONFIG_USB_OTG
2056 /*
2057 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2058 * to wake us after we've powered off VBUS; and HNP, switching roles
2059 * "host" to "peripheral". The OTG descriptor helps figure this out.
2060 */
2061 if (!udev->bus->is_b_host
2062 && udev->config
2063 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002064 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 struct usb_bus *bus = udev->bus;
2066
2067 /* descriptor may appear anywhere in config */
2068 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2069 le16_to_cpu(udev->config[0].desc.wTotalLength),
2070 USB_DT_OTG, (void **) &desc) == 0) {
2071 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002072 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 dev_info(&udev->dev,
2075 "Dual-Role OTG device on %sHNP port\n",
2076 (port1 == bus->otg_port)
2077 ? "" : "non-");
2078
2079 /* enable HNP before suspend, it's simpler */
2080 if (port1 == bus->otg_port)
2081 bus->b_hnp_enable = 1;
2082 err = usb_control_msg(udev,
2083 usb_sndctrlpipe(udev, 0),
2084 USB_REQ_SET_FEATURE, 0,
2085 bus->b_hnp_enable
2086 ? USB_DEVICE_B_HNP_ENABLE
2087 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2088 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2089 if (err < 0) {
2090 /* OTG MESSAGE: report errors here,
2091 * customize to match your product.
2092 */
2093 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002094 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 err);
2096 bus->b_hnp_enable = 0;
2097 }
2098 }
2099 }
2100 }
2101
2102 if (!is_targeted(udev)) {
2103
2104 /* Maybe it can talk to us, though we can't talk to it.
2105 * (Includes HNP test device.)
2106 */
2107 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002108 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (err < 0)
2110 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2111 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002112 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 goto fail;
2114 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002115fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002117 return err;
2118}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002120
2121/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002122 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002123 * @udev: newly addressed device (in ADDRESS state)
2124 *
2125 * This is only called by usb_new_device() and usb_authorize_device()
2126 * and FIXME -- all comments that apply to them apply here wrt to
2127 * environment.
2128 *
2129 * If the device is WUSB and not authorized, we don't attempt to read
2130 * the string descriptors, as they will be errored out by the device
2131 * until it has been authorized.
2132 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002133static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002134{
2135 int err;
2136
2137 if (udev->config == NULL) {
2138 err = usb_get_configuration(udev);
2139 if (err < 0) {
2140 dev_err(&udev->dev, "can't read configurations, error %d\n",
2141 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002142 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002143 }
2144 }
2145 if (udev->wusb == 1 && udev->authorized == 0) {
2146 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2147 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2148 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2149 }
2150 else {
2151 /* read the standard strings and cache them if present */
2152 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2153 udev->manufacturer = usb_cache_string(udev,
2154 udev->descriptor.iManufacturer);
2155 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2156 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002157 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002158 if (err < 0)
2159 return err;
2160
2161 usb_detect_interface_quirks(udev);
2162
2163 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002164}
2165
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002166static void set_usb_port_removable(struct usb_device *udev)
2167{
2168 struct usb_device *hdev = udev->parent;
2169 struct usb_hub *hub;
2170 u8 port = udev->portnum;
2171 u16 wHubCharacteristics;
2172 bool removable = true;
2173
2174 if (!hdev)
2175 return;
2176
2177 hub = hdev_to_hub(udev->parent);
2178
2179 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2180
2181 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2182 return;
2183
2184 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002185 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2186 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002187 removable = false;
2188 } else {
2189 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2190 removable = false;
2191 }
2192
2193 if (removable)
2194 udev->removable = USB_DEVICE_REMOVABLE;
2195 else
2196 udev->removable = USB_DEVICE_FIXED;
2197}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002198
2199/**
2200 * usb_new_device - perform initial device setup (usbcore-internal)
2201 * @udev: newly addressed device (in ADDRESS state)
2202 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002203 * This is called with devices which have been detected but not fully
2204 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002205 * for any device configuration. The caller must have locked either
2206 * the parent hub (if udev is a normal device) or else the
2207 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2208 * udev has already been installed, but udev is not yet visible through
2209 * sysfs or other filesystem code.
2210 *
2211 * It will return if the device is configured properly or not. Zero if
2212 * the interface was registered with the driver core; else a negative
2213 * errno value.
2214 *
2215 * This call is synchronous, and may not be used in an interrupt context.
2216 *
2217 * Only the hub driver or root-hub registrar should ever call this.
2218 */
2219int usb_new_device(struct usb_device *udev)
2220{
2221 int err;
2222
Dan Streetman16985402010-01-06 09:56:53 -05002223 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002224 /* Initialize non-root-hub device wakeup to disabled;
2225 * device (un)configuration controls wakeup capable
2226 * sysfs power/wakeup controls wakeup enabled/disabled
2227 */
2228 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002229 }
2230
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002231 /* Tell the runtime-PM framework the device is active */
2232 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002233 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002234 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002235 pm_runtime_enable(&udev->dev);
2236
Alan Sternc08512c2010-11-15 15:57:58 -05002237 /* By default, forbid autosuspend for all devices. It will be
2238 * allowed for hubs during binding.
2239 */
2240 usb_disable_autosuspend(udev);
2241
Alan Stern8d8558d2009-12-08 15:50:41 -05002242 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002243 if (err < 0)
2244 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002245 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2246 udev->devnum, udev->bus->busnum,
2247 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002248 /* export the usbdev device-node for libusb */
2249 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2250 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2251
Alan Stern6cd13202008-11-13 15:08:30 -05002252 /* Tell the world! */
2253 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002254
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002255 if (udev->serial)
2256 add_device_randomness(udev->serial, strlen(udev->serial));
2257 if (udev->product)
2258 add_device_randomness(udev->product, strlen(udev->product));
2259 if (udev->manufacturer)
2260 add_device_randomness(udev->manufacturer,
2261 strlen(udev->manufacturer));
2262
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002263 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002264
2265 /*
2266 * check whether the hub marks this port as non-removable. Do it
2267 * now so that platform-specific data can override it in
2268 * device_add()
2269 */
2270 if (udev->parent)
2271 set_usb_port_removable(udev);
2272
Alan Stern782da722006-07-01 22:09:35 -04002273 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002274 * for configuring the device and invoking the add-device
2275 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002276 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002277 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (err) {
2279 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2280 goto fail;
2281 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002282
Alan Stern3b23dd62008-12-05 14:10:34 -05002283 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002284 usb_mark_last_busy(udev);
2285 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288fail:
2289 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002290 pm_runtime_disable(&udev->dev);
2291 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002295
2296/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002297 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2298 * @usb_dev: USB device
2299 *
2300 * Move the USB device to a very basic state where interfaces are disabled
2301 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002302 *
2303 * We share a lock (that we have) with device_del(), so we need to
2304 * defer its call.
2305 */
2306int usb_deauthorize_device(struct usb_device *usb_dev)
2307{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002308 usb_lock_device(usb_dev);
2309 if (usb_dev->authorized == 0)
2310 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002311
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002312 usb_dev->authorized = 0;
2313 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002314
2315 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002316 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002317 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002318 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002319 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002320 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002321
2322 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002323 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002324
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002325out_unauthorized:
2326 usb_unlock_device(usb_dev);
2327 return 0;
2328}
2329
2330
2331int usb_authorize_device(struct usb_device *usb_dev)
2332{
2333 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002334
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002335 usb_lock_device(usb_dev);
2336 if (usb_dev->authorized == 1)
2337 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002338
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002339 result = usb_autoresume_device(usb_dev);
2340 if (result < 0) {
2341 dev_err(&usb_dev->dev,
2342 "can't autoresume for authorization: %d\n", result);
2343 goto error_autoresume;
2344 }
2345 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2346 if (result < 0) {
2347 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2348 "authorization: %d\n", result);
2349 goto error_device_descriptor;
2350 }
Alan Sternda307122009-12-08 15:54:44 -05002351
2352 kfree(usb_dev->product);
2353 usb_dev->product = NULL;
2354 kfree(usb_dev->manufacturer);
2355 usb_dev->manufacturer = NULL;
2356 kfree(usb_dev->serial);
2357 usb_dev->serial = NULL;
2358
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002359 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002360 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002361 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002362 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002363 /* Choose and set the configuration. This registers the interfaces
2364 * with the driver core and lets interface drivers bind to them.
2365 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002366 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002367 if (c >= 0) {
2368 result = usb_set_configuration(usb_dev, c);
2369 if (result) {
2370 dev_err(&usb_dev->dev,
2371 "can't set config #%d, error %d\n", c, result);
2372 /* This need not be fatal. The user can try to
2373 * set other configurations. */
2374 }
2375 }
2376 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002377
Alan Stern8d8558d2009-12-08 15:50:41 -05002378error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002379error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002380 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002381error_autoresume:
2382out_authorized:
2383 usb_unlock_device(usb_dev); // complements locktree
2384 return result;
2385}
2386
2387
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002388/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2389static unsigned hub_is_wusb(struct usb_hub *hub)
2390{
2391 struct usb_hcd *hcd;
2392 if (hub->hdev->parent != NULL) /* not a root hub? */
2393 return 0;
2394 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2395 return hcd->wireless;
2396}
2397
2398
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399#define PORT_RESET_TRIES 5
2400#define SET_ADDRESS_TRIES 2
2401#define GET_DESCRIPTOR_TRIES 2
2402#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302403#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2406#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002407#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408#define HUB_LONG_RESET_TIME 200
2409#define HUB_RESET_TIMEOUT 500
2410
Sarah Sharp10d674a2011-09-14 14:24:52 -07002411static int hub_port_reset(struct usb_hub *hub, int port1,
2412 struct usb_device *udev, unsigned int delay, bool warm);
2413
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002414/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2415 * Port worm reset is required to recover
2416 */
2417static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002418{
2419 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002420 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2421 USB_SS_PORT_LS_SS_INACTIVE) ||
2422 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2423 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002424}
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002427 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 int delay_time, ret;
2430 u16 portstatus;
2431 u16 portchange;
2432
2433 for (delay_time = 0;
2434 delay_time < HUB_RESET_TIMEOUT;
2435 delay_time += delay) {
2436 /* wait to give the device a chance to reset */
2437 msleep(delay);
2438
2439 /* read and decode port status */
2440 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2441 if (ret < 0)
2442 return ret;
2443
Andiry Xu75d7cf72011-09-13 16:41:11 -07002444 /*
2445 * Some buggy devices require a warm reset to be issued even
2446 * when the port appears not to be connected.
2447 */
2448 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002449 /*
2450 * Some buggy devices can cause an NEC host controller
2451 * to transition to the "Error" state after a hot port
2452 * reset. This will show up as the port state in
2453 * "Inactive", and the port may also report a
2454 * disconnect. Forcing a warm port reset seems to make
2455 * the device work.
2456 *
2457 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2458 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002459 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002460 int ret;
2461
2462 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2463 clear_port_feature(hub->hdev, port1,
2464 USB_PORT_FEAT_C_CONNECTION);
2465 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2466 clear_port_feature(hub->hdev, port1,
2467 USB_PORT_FEAT_C_PORT_LINK_STATE);
2468 if (portchange & USB_PORT_STAT_C_RESET)
2469 clear_port_feature(hub->hdev, port1,
2470 USB_PORT_FEAT_C_RESET);
2471 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2472 port1);
2473 ret = hub_port_reset(hub, port1,
2474 udev, HUB_BH_RESET_TIME,
2475 true);
2476 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2477 clear_port_feature(hub->hdev, port1,
2478 USB_PORT_FEAT_C_CONNECTION);
2479 return ret;
2480 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002481 /* Device went away? */
2482 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2483 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Andiry Xu75d7cf72011-09-13 16:41:11 -07002485 /* bomb out completely if the connection bounced */
2486 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2487 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Andiry Xu75d7cf72011-09-13 16:41:11 -07002489 /* if we`ve finished resetting, then break out of
2490 * the loop
2491 */
2492 if (!(portstatus & USB_PORT_STAT_RESET) &&
2493 (portstatus & USB_PORT_STAT_ENABLE)) {
2494 if (hub_is_wusb(hub))
2495 udev->speed = USB_SPEED_WIRELESS;
2496 else if (hub_is_superspeed(hub->hdev))
2497 udev->speed = USB_SPEED_SUPER;
2498 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2499 udev->speed = USB_SPEED_HIGH;
2500 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2501 udev->speed = USB_SPEED_LOW;
2502 else
2503 udev->speed = USB_SPEED_FULL;
2504 return 0;
2505 }
2506 } else {
2507 if (portchange & USB_PORT_STAT_C_BH_RESET)
2508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
2510
2511 /* switch to the long delay after two short delay failures */
2512 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2513 delay = HUB_LONG_RESET_TIME;
2514
2515 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002516 "port %d not %sreset yet, waiting %dms\n",
2517 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
2519
2520 return -EBUSY;
2521}
2522
Andiry Xu75d7cf72011-09-13 16:41:11 -07002523static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2524 struct usb_device *udev, int *status, bool warm)
2525{
2526 switch (*status) {
2527 case 0:
2528 if (!warm) {
2529 struct usb_hcd *hcd;
2530 /* TRSTRCY = 10 ms; plus some extra */
2531 msleep(10 + 40);
2532 update_devnum(udev, 0);
2533 hcd = bus_to_hcd(udev->bus);
2534 if (hcd->driver->reset_device) {
2535 *status = hcd->driver->reset_device(hcd, udev);
2536 if (*status < 0) {
2537 dev_err(&udev->dev, "Cannot reset "
2538 "HCD device state\n");
2539 break;
2540 }
2541 }
2542 }
2543 /* FALL THROUGH */
2544 case -ENOTCONN:
2545 case -ENODEV:
2546 clear_port_feature(hub->hdev,
2547 port1, USB_PORT_FEAT_C_RESET);
2548 /* FIXME need disconnect() for NOTATTACHED device */
2549 if (warm) {
2550 clear_port_feature(hub->hdev, port1,
2551 USB_PORT_FEAT_C_BH_PORT_RESET);
2552 clear_port_feature(hub->hdev, port1,
2553 USB_PORT_FEAT_C_PORT_LINK_STATE);
2554 } else {
2555 usb_set_device_state(udev, *status
2556 ? USB_STATE_NOTATTACHED
2557 : USB_STATE_DEFAULT);
2558 }
2559 break;
2560 }
2561}
2562
2563/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002565 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
2567 int i, status;
2568
Andiry Xu75d7cf72011-09-13 16:41:11 -07002569 if (!warm) {
2570 /* Block EHCI CF initialization during the port reset.
2571 * Some companion controllers don't like it when they mix.
2572 */
2573 down_read(&ehci_cf_port_reset_rwsem);
2574 } else {
2575 if (!hub_is_superspeed(hub->hdev)) {
2576 dev_err(hub->intfdev, "only USB3 hub support "
2577 "warm reset\n");
2578 return -EINVAL;
2579 }
2580 }
Alan Stern32fe0192007-10-10 16:27:07 -04002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 /* Reset the port */
2583 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002584 status = set_port_feature(hub->hdev, port1, (warm ?
2585 USB_PORT_FEAT_BH_PORT_RESET :
2586 USB_PORT_FEAT_RESET));
2587 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002589 "cannot %sreset port %d (err = %d)\n",
2590 warm ? "warm " : "", port1, status);
2591 } else {
2592 status = hub_port_wait_reset(hub, port1, udev, delay,
2593 warm);
David Brownelldd165252005-08-31 10:45:25 -07002594 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 dev_dbg(hub->intfdev,
2596 "port_wait_reset: err = %d\n",
2597 status);
2598 }
2599
2600 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002601 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2602 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002603 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605
2606 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002607 "port %d not enabled, trying %sreset again...\n",
2608 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 delay = HUB_LONG_RESET_TIME;
2610 }
2611
2612 dev_err (hub->intfdev,
2613 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2614 port1);
2615
Andiry Xu75d7cf72011-09-13 16:41:11 -07002616done:
2617 if (!warm)
2618 up_read(&ehci_cf_port_reset_rwsem);
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 return status;
2621}
2622
Andiry Xu0ed9a572011-04-27 18:07:43 +08002623/* Check if a port is power on */
2624static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2625{
2626 int ret = 0;
2627
2628 if (hub_is_superspeed(hub->hdev)) {
2629 if (portstatus & USB_SS_PORT_STAT_POWER)
2630 ret = 1;
2631 } else {
2632 if (portstatus & USB_PORT_STAT_POWER)
2633 ret = 1;
2634 }
2635
2636 return ret;
2637}
2638
Alan Sternd388dab2006-07-01 22:14:24 -04002639#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Andiry Xu0ed9a572011-04-27 18:07:43 +08002641/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2642static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2643{
2644 int ret = 0;
2645
2646 if (hub_is_superspeed(hub->hdev)) {
2647 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2648 == USB_SS_PORT_LS_U3)
2649 ret = 1;
2650 } else {
2651 if (portstatus & USB_PORT_STAT_SUSPEND)
2652 ret = 1;
2653 }
2654
2655 return ret;
2656}
Alan Sternb01b03f2008-04-28 11:06:11 -04002657
2658/* Determine whether the device on a port is ready for a normal resume,
2659 * is ready for a reset-resume, or should be disconnected.
2660 */
2661static int check_port_resume_type(struct usb_device *udev,
2662 struct usb_hub *hub, int port1,
2663 int status, unsigned portchange, unsigned portstatus)
2664{
2665 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002666 if (status || port_is_suspended(hub, portstatus) ||
2667 !port_is_power_on(hub, portstatus) ||
2668 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002669 if (status >= 0)
2670 status = -ENODEV;
2671 }
2672
Alan Stern86c57ed2008-06-30 11:14:43 -04002673 /* Can't do a normal resume if the port isn't enabled,
2674 * so try a reset-resume instead.
2675 */
2676 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2677 if (udev->persist_enabled)
2678 udev->reset_resume = 1;
2679 else
2680 status = -ENODEV;
2681 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002682
2683 if (status) {
2684 dev_dbg(hub->intfdev,
2685 "port %d status %04x.%04x after resume, %d\n",
2686 port1, portchange, portstatus, status);
2687 } else if (udev->reset_resume) {
2688
2689 /* Late port handoff can set status-change bits */
2690 if (portchange & USB_PORT_STAT_C_CONNECTION)
2691 clear_port_feature(hub->hdev, port1,
2692 USB_PORT_FEAT_C_CONNECTION);
2693 if (portchange & USB_PORT_STAT_C_ENABLE)
2694 clear_port_feature(hub->hdev, port1,
2695 USB_PORT_FEAT_C_ENABLE);
2696 }
2697
2698 return status;
2699}
2700
Sarah Sharpf74631e2012-06-25 12:08:08 -07002701int usb_disable_ltm(struct usb_device *udev)
2702{
2703 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2704
2705 /* Check if the roothub and device supports LTM. */
2706 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2707 !usb_device_supports_ltm(udev))
2708 return 0;
2709
2710 /* Clear Feature LTM Enable can only be sent if the device is
2711 * configured.
2712 */
2713 if (!udev->actconfig)
2714 return 0;
2715
2716 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2717 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2718 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2719 USB_CTRL_SET_TIMEOUT);
2720}
2721EXPORT_SYMBOL_GPL(usb_disable_ltm);
2722
2723void usb_enable_ltm(struct usb_device *udev)
2724{
2725 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2726
2727 /* Check if the roothub and device supports LTM. */
2728 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2729 !usb_device_supports_ltm(udev))
2730 return;
2731
2732 /* Set Feature LTM Enable can only be sent if the device is
2733 * configured.
2734 */
2735 if (!udev->actconfig)
2736 return;
2737
2738 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2739 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2740 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2741 USB_CTRL_SET_TIMEOUT);
2742}
2743EXPORT_SYMBOL_GPL(usb_enable_ltm);
2744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745#ifdef CONFIG_USB_SUSPEND
2746
2747/*
Alan Stern140d8f62006-07-01 22:07:21 -04002748 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002749 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002750 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 *
2752 * Suspends a USB device that isn't in active use, conserving power.
2753 * Devices may wake out of a suspend, if anything important happens,
2754 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002755 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 * to disconnect devices while they are suspended.
2757 *
David Brownell390a8c32005-09-13 19:57:27 -07002758 * This only affects the USB hardware for a device; its interfaces
2759 * (and, for hubs, child devices) must already have been suspended.
2760 *
Alan Stern624d6c02007-05-30 15:35:16 -04002761 * Selective port suspend reduces power; most suspended devices draw
2762 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2763 * All devices below the suspended port are also suspended.
2764 *
2765 * Devices leave suspend state when the host wakes them up. Some devices
2766 * also support "remote wakeup", where the device can activate the USB
2767 * tree above them to deliver data, such as a keypress or packet. In
2768 * some cases, this wakes the USB host.
2769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 * Suspending OTG devices may trigger HNP, if that's been enabled
2771 * between a pair of dual-role devices. That will change roles, such
2772 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2773 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002774 * Devices on USB hub ports have only one "suspend" state, corresponding
2775 * to ACPI D2, "may cause the device to lose some context".
2776 * State transitions include:
2777 *
2778 * - suspend, resume ... when the VBUS power link stays live
2779 * - suspend, disconnect ... VBUS lost
2780 *
2781 * Once VBUS drop breaks the circuit, the port it's using has to go through
2782 * normal re-enumeration procedures, starting with enabling VBUS power.
2783 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2784 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2785 * timer, no SRP, no requests through sysfs.
2786 *
2787 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2788 * the root hub for their bus goes into global suspend ... so we don't
2789 * (falsely) update the device power state to say it suspended.
2790 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 * Returns 0 on success, else negative errno.
2792 */
Alan Stern65bfd292008-11-25 16:39:18 -05002793int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Alan Stern624d6c02007-05-30 15:35:16 -04002795 struct usb_hub *hub = hdev_to_hub(udev->parent);
2796 int port1 = udev->portnum;
2797 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002798
Alan Stern624d6c02007-05-30 15:35:16 -04002799 /* enable remote wakeup when appropriate; this lets the device
2800 * wake up the upstream hub (including maybe the root hub).
2801 *
2802 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2803 * we don't explicitly enable it here.
2804 */
2805 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002806 if (!hub_is_superspeed(hub->hdev)) {
2807 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2808 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2809 USB_DEVICE_REMOTE_WAKEUP, 0,
2810 NULL, 0,
2811 USB_CTRL_SET_TIMEOUT);
2812 } else {
2813 /* Assume there's only one function on the USB 3.0
2814 * device and enable remote wake for the first
2815 * interface. FIXME if the interface association
2816 * descriptor shows there's more than one function.
2817 */
2818 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2819 USB_REQ_SET_FEATURE,
2820 USB_RECIP_INTERFACE,
2821 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002822 USB_INTRF_FUNC_SUSPEND_RW |
2823 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002824 NULL, 0,
2825 USB_CTRL_SET_TIMEOUT);
2826 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002827 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002828 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2829 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002830 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002831 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002832 return status;
2833 }
Alan Stern624d6c02007-05-30 15:35:16 -04002834 }
2835
Andiry Xu65580b432011-09-23 14:19:52 -07002836 /* disable USB2 hardware LPM */
2837 if (udev->usb2_hw_lpm_enabled == 1)
2838 usb_set_usb2_hardware_lpm(udev, 0);
2839
Sarah Sharpf74631e2012-06-25 12:08:08 -07002840 if (usb_disable_ltm(udev)) {
2841 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2842 __func__);
2843 return -ENOMEM;
2844 }
Sarah Sharp83060952012-05-02 14:25:52 -07002845 if (usb_unlocked_disable_lpm(udev)) {
2846 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2847 __func__);
2848 return -ENOMEM;
2849 }
2850
Alan Stern624d6c02007-05-30 15:35:16 -04002851 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002852 if (hub_is_superspeed(hub->hdev))
2853 status = set_port_feature(hub->hdev,
2854 port1 | (USB_SS_PORT_LS_U3 << 3),
2855 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002856 else
2857 status = set_port_feature(hub->hdev, port1,
2858 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002859 if (status) {
2860 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2861 port1, status);
2862 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002863 if (udev->do_remote_wakeup)
2864 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002865 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2866 USB_DEVICE_REMOTE_WAKEUP, 0,
2867 NULL, 0,
2868 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002869
Andiry Xuc3e751e2012-05-05 00:50:10 +08002870 /* Try to enable USB2 hardware LPM again */
2871 if (udev->usb2_hw_lpm_capable == 1)
2872 usb_set_usb2_hardware_lpm(udev, 1);
2873
Sarah Sharpf74631e2012-06-25 12:08:08 -07002874 /* Try to enable USB3 LTM and LPM again */
2875 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002876 usb_unlocked_enable_lpm(udev);
2877
Alan Sterncbb33002011-06-15 16:29:16 -04002878 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002879 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002880 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002881 } else {
2882 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002883 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2884 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2885 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002886 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Sternbfd1e912012-10-19 11:03:39 -04002887 udev->port_is_suspended = 1;
Alan Stern624d6c02007-05-30 15:35:16 -04002888 msleep(10);
2889 }
Alan Sternc08512c2010-11-15 15:57:58 -05002890 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002891 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
David Brownellf3f32532005-09-22 22:37:29 -07002893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894/*
David Brownell390a8c32005-09-13 19:57:27 -07002895 * If the USB "suspend" state is in use (rather than "global suspend"),
2896 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002897 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 * hardware resume signaling is finished, either because of selective
2899 * resume (by host) or remote wakeup (by device) ... now see what changed
2900 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002901 *
2902 * If @udev->reset_resume is set then the device is reset before the
2903 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 */
Alan Stern140d8f62006-07-01 22:07:21 -04002905static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Alan Stern54515fe2007-05-30 15:38:58 -04002907 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 u16 devstatus;
2909
2910 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002911 dev_dbg(&udev->dev, "%s\n",
2912 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 /* usb ch9 identifies four variants of SUSPENDED, based on what
2915 * state the device resumes to. Linux currently won't see the
2916 * first two on the host side; they'd be inside hub_port_init()
2917 * during many timeouts, but khubd can't suspend until later.
2918 */
2919 usb_set_device_state(udev, udev->actconfig
2920 ? USB_STATE_CONFIGURED
2921 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Alan Stern54515fe2007-05-30 15:38:58 -04002923 /* 10.5.4.5 says not to reset a suspended port if the attached
2924 * device is enabled for remote wakeup. Hence the reset
2925 * operation is carried out here, after the port has been
2926 * resumed.
2927 */
2928 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002929 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002930 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 /* 10.5.4.5 says be sure devices in the tree are still there.
2933 * For now let's assume the device didn't go crazy on resume,
2934 * and device drivers will know about any resume quirks.
2935 */
Alan Stern54515fe2007-05-30 15:38:58 -04002936 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002937 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002938 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2939 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002940 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002941
2942 /* If a normal resume failed, try doing a reset-resume */
2943 if (status && !udev->reset_resume && udev->persist_enabled) {
2944 dev_dbg(&udev->dev, "retry with reset-resume\n");
2945 udev->reset_resume = 1;
2946 goto retry_reset_resume;
2947 }
Alan Stern54515fe2007-05-30 15:38:58 -04002948 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002949
Alan Stern624d6c02007-05-30 15:35:16 -04002950 if (status) {
2951 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2952 status);
2953 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002955 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 status = usb_control_msg(udev,
2957 usb_sndctrlpipe(udev, 0),
2958 USB_REQ_CLEAR_FEATURE,
2959 USB_RECIP_DEVICE,
2960 USB_DEVICE_REMOTE_WAKEUP, 0,
2961 NULL, 0,
2962 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002963 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002964 dev_dbg(&udev->dev,
2965 "disable remote wakeup, status %d\n",
2966 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 return status;
2971}
2972
Alan Stern624d6c02007-05-30 15:35:16 -04002973/*
2974 * usb_port_resume - re-activate a suspended usb device's upstream port
2975 * @udev: device to re-activate, not a root hub
2976 * Context: must be able to sleep; device not locked; pm locks held
2977 *
2978 * This will re-activate the suspended device, increasing power usage
2979 * while letting drivers communicate again with its endpoints.
2980 * USB resume explicitly guarantees that the power session between
2981 * the host and the device is the same as it was when the device
2982 * suspended.
2983 *
Alan Sternfeccc302008-03-03 15:15:59 -05002984 * If @udev->reset_resume is set then this routine won't check that the
2985 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002986 * reset @udev. The end result is that a broken power session can be
2987 * recovered and @udev will appear to persist across a loss of VBUS power.
2988 *
2989 * For example, if a host controller doesn't maintain VBUS suspend current
2990 * during a system sleep or is reset when the system wakes up, all the USB
2991 * power sessions below it will be broken. This is especially troublesome
2992 * for mass-storage devices containing mounted filesystems, since the
2993 * device will appear to have disconnected and all the memory mappings
2994 * to it will be lost. Using the USB_PERSIST facility, the device can be
2995 * made to appear as if it had not disconnected.
2996 *
Ming Lei742120c2008-06-18 22:00:29 +08002997 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002998 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002999 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3000 * quite possible for a device to remain unaltered but its media to be
3001 * changed. If the user replaces a flash memory card while the system is
3002 * asleep, he will have only himself to blame when the filesystem on the
3003 * new card is corrupted and the system crashes.
3004 *
Alan Stern624d6c02007-05-30 15:35:16 -04003005 * Returns 0 on success, else negative errno.
3006 */
Alan Stern65bfd292008-11-25 16:39:18 -05003007int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Alan Stern624d6c02007-05-30 15:35:16 -04003009 struct usb_hub *hub = hdev_to_hub(udev->parent);
3010 int port1 = udev->portnum;
3011 int status;
3012 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003013
3014 /* Skip the initial Clear-Suspend step for a remote wakeup */
3015 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003016 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003017 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
3019 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3020
Alan Sternd5cbad42006-08-11 16:52:39 -04003021 set_bit(port1, hub->busy_bits);
3022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003024 if (hub_is_superspeed(hub->hdev))
3025 status = set_port_feature(hub->hdev,
3026 port1 | (USB_SS_PORT_LS_U0 << 3),
3027 USB_PORT_FEAT_LINK_STATE);
3028 else
3029 status = clear_port_feature(hub->hdev,
3030 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003032 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3033 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003036 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003037 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 msleep(25);
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3041 * stop resume signaling. Then finish the resume
3042 * sequence.
3043 */
Alan Sternd25450c2006-11-20 11:14:30 -05003044 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003045
Alan Sternb01b03f2008-04-28 11:06:11 -04003046 /* TRSMRCY = 10 msec */
3047 msleep(10);
3048 }
Alan Stern54515fe2007-05-30 15:38:58 -04003049
Alan Sternb01b03f2008-04-28 11:06:11 -04003050 SuspendCleared:
3051 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003052 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003053 if (hub_is_superspeed(hub->hdev)) {
3054 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3055 clear_port_feature(hub->hdev, port1,
3056 USB_PORT_FEAT_C_PORT_LINK_STATE);
3057 } else {
3058 if (portchange & USB_PORT_STAT_C_SUSPEND)
3059 clear_port_feature(hub->hdev, port1,
3060 USB_PORT_FEAT_C_SUSPEND);
3061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Alan Sternd5cbad42006-08-11 16:52:39 -04003064 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003065
Alan Sternb01b03f2008-04-28 11:06:11 -04003066 status = check_port_resume_type(udev,
3067 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003068 if (status == 0)
3069 status = finish_port_resume(udev);
3070 if (status < 0) {
3071 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3072 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003073 } else {
3074 /* Try to enable USB2 hardware LPM */
3075 if (udev->usb2_hw_lpm_capable == 1)
3076 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003077
Sarah Sharpf74631e2012-06-25 12:08:08 -07003078 /* Try to enable USB3 LTM and LPM */
3079 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003080 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003081 }
Andiry Xu65580b432011-09-23 14:19:52 -07003082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 return status;
3084}
3085
Alan Stern8808f002008-04-28 11:06:55 -04003086/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003087int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
3089 int status = 0;
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003092 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003093 status = usb_autoresume_device(udev);
3094 if (status == 0) {
3095 /* Let the drivers do their thing, then... */
3096 usb_autosuspend_device(udev);
3097 }
Alan Sternd25450c2006-11-20 11:14:30 -05003098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 return status;
3100}
3101
Alan Sternd388dab2006-07-01 22:14:24 -04003102#else /* CONFIG_USB_SUSPEND */
3103
3104/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3105
Alan Stern65bfd292008-11-25 16:39:18 -05003106int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003107{
3108 return 0;
3109}
3110
Alan Sternb01b03f2008-04-28 11:06:11 -04003111/* However we may need to do a reset-resume */
3112
Alan Stern65bfd292008-11-25 16:39:18 -05003113int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003114{
Alan Sternb01b03f2008-04-28 11:06:11 -04003115 struct usb_hub *hub = hdev_to_hub(udev->parent);
3116 int port1 = udev->portnum;
3117 int status;
3118 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003119
Alan Sternb01b03f2008-04-28 11:06:11 -04003120 status = hub_port_status(hub, port1, &portstatus, &portchange);
3121 status = check_port_resume_type(udev,
3122 hub, port1, status, portchange, portstatus);
3123
3124 if (status) {
3125 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3126 hub_port_logical_disconnect(hub, port1);
3127 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003128 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003129 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003130 }
3131 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003132}
3133
Alan Sternd388dab2006-07-01 22:14:24 -04003134#endif
3135
Ming Leie6f30de2012-10-24 11:59:24 +08003136static int check_ports_changed(struct usb_hub *hub)
3137{
3138 int port1;
3139
3140 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3141 u16 portstatus, portchange;
3142 int status;
3143
3144 status = hub_port_status(hub, port1, &portstatus, &portchange);
3145 if (!status && portchange)
3146 return 1;
3147 }
3148 return 0;
3149}
3150
David Brownelldb690872005-09-13 19:56:33 -07003151static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct usb_hub *hub = usb_get_intfdata (intf);
3154 struct usb_device *hdev = hub->hdev;
3155 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003156 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157
Alan Sterncbb33002011-06-15 16:29:16 -04003158 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3160 struct usb_device *udev;
3161
Lan Tianyuff823c792012-09-05 13:44:32 +08003162 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003163 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003164 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003165 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003166 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
Ming Leie6f30de2012-10-24 11:59:24 +08003169
3170 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3171 /* check if there are changes pending on hub ports */
3172 if (check_ports_changed(hub)) {
3173 if (PMSG_IS_AUTO(msg))
3174 return -EBUSY;
3175 pm_wakeup_event(&hdev->dev, 2000);
3176 }
3177 }
3178
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003179 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3180 /* Enable hub to send remote wakeup for all ports. */
3181 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3182 status = set_port_feature(hdev,
3183 port1 |
3184 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3185 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3186 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3187 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3188 }
3189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Harvey Harrison441b62c2008-03-03 16:08:34 -08003191 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003192
David Brownellc9f89fa2005-09-13 19:57:04 -07003193 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003194 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
3198static int hub_resume(struct usb_interface *intf)
3199{
Alan Stern5e6effa2008-03-03 15:15:51 -05003200 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Alan Stern5e6effa2008-03-03 15:15:51 -05003202 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003203 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return 0;
3205}
3206
Alan Sternb41a60e2007-05-30 15:39:33 -04003207static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003208{
Alan Sternb41a60e2007-05-30 15:39:33 -04003209 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003210
Alan Stern5e6effa2008-03-03 15:15:51 -05003211 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003212 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003213 return 0;
3214}
3215
Alan Stern54515fe2007-05-30 15:38:58 -04003216/**
3217 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3218 * @rhdev: struct usb_device for the root hub
3219 *
3220 * The USB host controller driver calls this function when its root hub
3221 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003222 * has been reset. The routine marks @rhdev as having lost power.
3223 * When the hub driver is resumed it will take notice and carry out
3224 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3225 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003226 */
3227void usb_root_hub_lost_power(struct usb_device *rhdev)
3228{
3229 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3230 rhdev->reset_resume = 1;
3231}
3232EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3233
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003234static const char * const usb3_lpm_names[] = {
3235 "U0",
3236 "U1",
3237 "U2",
3238 "U3",
3239};
3240
3241/*
3242 * Send a Set SEL control transfer to the device, prior to enabling
3243 * device-initiated U1 or U2. This lets the device know the exit latencies from
3244 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3245 * packet from the host.
3246 *
3247 * This function will fail if the SEL or PEL values for udev are greater than
3248 * the maximum allowed values for the link state to be enabled.
3249 */
3250static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3251{
3252 struct usb_set_sel_req *sel_values;
3253 unsigned long long u1_sel;
3254 unsigned long long u1_pel;
3255 unsigned long long u2_sel;
3256 unsigned long long u2_pel;
3257 int ret;
3258
3259 /* Convert SEL and PEL stored in ns to us */
3260 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3261 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3262 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3263 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3264
3265 /*
3266 * Make sure that the calculated SEL and PEL values for the link
3267 * state we're enabling aren't bigger than the max SEL/PEL
3268 * value that will fit in the SET SEL control transfer.
3269 * Otherwise the device would get an incorrect idea of the exit
3270 * latency for the link state, and could start a device-initiated
3271 * U1/U2 when the exit latencies are too high.
3272 */
3273 if ((state == USB3_LPM_U1 &&
3274 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3275 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3276 (state == USB3_LPM_U2 &&
3277 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3278 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003279 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 -07003280 usb3_lpm_names[state], u1_sel, u1_pel);
3281 return -EINVAL;
3282 }
3283
3284 /*
3285 * If we're enabling device-initiated LPM for one link state,
3286 * but the other link state has a too high SEL or PEL value,
3287 * just set those values to the max in the Set SEL request.
3288 */
3289 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3290 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3291
3292 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3293 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3294
3295 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3296 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3297
3298 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3299 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3300
3301 /*
3302 * usb_enable_lpm() can be called as part of a failed device reset,
3303 * which may be initiated by an error path of a mass storage driver.
3304 * Therefore, use GFP_NOIO.
3305 */
3306 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3307 if (!sel_values)
3308 return -ENOMEM;
3309
3310 sel_values->u1_sel = u1_sel;
3311 sel_values->u1_pel = u1_pel;
3312 sel_values->u2_sel = cpu_to_le16(u2_sel);
3313 sel_values->u2_pel = cpu_to_le16(u2_pel);
3314
3315 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3316 USB_REQ_SET_SEL,
3317 USB_RECIP_DEVICE,
3318 0, 0,
3319 sel_values, sizeof *(sel_values),
3320 USB_CTRL_SET_TIMEOUT);
3321 kfree(sel_values);
3322 return ret;
3323}
3324
3325/*
3326 * Enable or disable device-initiated U1 or U2 transitions.
3327 */
3328static int usb_set_device_initiated_lpm(struct usb_device *udev,
3329 enum usb3_link_state state, bool enable)
3330{
3331 int ret;
3332 int feature;
3333
3334 switch (state) {
3335 case USB3_LPM_U1:
3336 feature = USB_DEVICE_U1_ENABLE;
3337 break;
3338 case USB3_LPM_U2:
3339 feature = USB_DEVICE_U2_ENABLE;
3340 break;
3341 default:
3342 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3343 __func__, enable ? "enable" : "disable");
3344 return -EINVAL;
3345 }
3346
3347 if (udev->state != USB_STATE_CONFIGURED) {
3348 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3349 "for unconfigured device.\n",
3350 __func__, enable ? "enable" : "disable",
3351 usb3_lpm_names[state]);
3352 return 0;
3353 }
3354
3355 if (enable) {
3356 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003357 * Now send the control transfer to enable device-initiated LPM
3358 * for either U1 or U2.
3359 */
3360 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3361 USB_REQ_SET_FEATURE,
3362 USB_RECIP_DEVICE,
3363 feature,
3364 0, NULL, 0,
3365 USB_CTRL_SET_TIMEOUT);
3366 } else {
3367 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3368 USB_REQ_CLEAR_FEATURE,
3369 USB_RECIP_DEVICE,
3370 feature,
3371 0, NULL, 0,
3372 USB_CTRL_SET_TIMEOUT);
3373 }
3374 if (ret < 0) {
3375 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3376 enable ? "Enable" : "Disable",
3377 usb3_lpm_names[state]);
3378 return -EBUSY;
3379 }
3380 return 0;
3381}
3382
3383static int usb_set_lpm_timeout(struct usb_device *udev,
3384 enum usb3_link_state state, int timeout)
3385{
3386 int ret;
3387 int feature;
3388
3389 switch (state) {
3390 case USB3_LPM_U1:
3391 feature = USB_PORT_FEAT_U1_TIMEOUT;
3392 break;
3393 case USB3_LPM_U2:
3394 feature = USB_PORT_FEAT_U2_TIMEOUT;
3395 break;
3396 default:
3397 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3398 __func__);
3399 return -EINVAL;
3400 }
3401
3402 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3403 timeout != USB3_LPM_DEVICE_INITIATED) {
3404 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3405 "which is a reserved value.\n",
3406 usb3_lpm_names[state], timeout);
3407 return -EINVAL;
3408 }
3409
3410 ret = set_port_feature(udev->parent,
3411 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3412 feature);
3413 if (ret < 0) {
3414 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3415 "error code %i\n", usb3_lpm_names[state],
3416 timeout, ret);
3417 return -EBUSY;
3418 }
3419 if (state == USB3_LPM_U1)
3420 udev->u1_params.timeout = timeout;
3421 else
3422 udev->u2_params.timeout = timeout;
3423 return 0;
3424}
3425
3426/*
3427 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3428 * U1/U2 entry.
3429 *
3430 * We will attempt to enable U1 or U2, but there are no guarantees that the
3431 * control transfers to set the hub timeout or enable device-initiated U1/U2
3432 * will be successful.
3433 *
3434 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3435 * driver know about it. If that call fails, it should be harmless, and just
3436 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3437 */
3438static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3439 enum usb3_link_state state)
3440{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003441 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003442 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3443 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3444
3445 /* If the device says it doesn't have *any* exit latency to come out of
3446 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3447 * state.
3448 */
3449 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3450 (state == USB3_LPM_U2 && u2_mel == 0))
3451 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003452
Sarah Sharp65a95b72012-10-05 10:32:07 -07003453 /*
3454 * First, let the device know about the exit latencies
3455 * associated with the link state we're about to enable.
3456 */
3457 ret = usb_req_set_sel(udev, state);
3458 if (ret < 0) {
3459 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3460 usb3_lpm_names[state]);
3461 return;
3462 }
3463
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003464 /* We allow the host controller to set the U1/U2 timeout internally
3465 * first, so that it can change its schedule to account for the
3466 * additional latency to send data to a device in a lower power
3467 * link state.
3468 */
3469 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3470
3471 /* xHCI host controller doesn't want to enable this LPM state. */
3472 if (timeout == 0)
3473 return;
3474
3475 if (timeout < 0) {
3476 dev_warn(&udev->dev, "Could not enable %s link state, "
3477 "xHCI error %i.\n", usb3_lpm_names[state],
3478 timeout);
3479 return;
3480 }
3481
3482 if (usb_set_lpm_timeout(udev, state, timeout))
3483 /* If we can't set the parent hub U1/U2 timeout,
3484 * device-initiated LPM won't be allowed either, so let the xHCI
3485 * host know that this link state won't be enabled.
3486 */
3487 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3488
3489 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3490 else if (udev->actconfig)
3491 usb_set_device_initiated_lpm(udev, state, true);
3492
3493}
3494
3495/*
3496 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3497 * U1/U2 entry.
3498 *
3499 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3500 * If zero is returned, the parent will not allow the link to go into U1/U2.
3501 *
3502 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3503 * it won't have an effect on the bus link state because the parent hub will
3504 * still disallow device-initiated U1/U2 entry.
3505 *
3506 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3507 * possible. The result will be slightly more bus bandwidth will be taken up
3508 * (to account for U1/U2 exit latency), but it should be harmless.
3509 */
3510static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3511 enum usb3_link_state state)
3512{
3513 int feature;
3514
3515 switch (state) {
3516 case USB3_LPM_U1:
3517 feature = USB_PORT_FEAT_U1_TIMEOUT;
3518 break;
3519 case USB3_LPM_U2:
3520 feature = USB_PORT_FEAT_U2_TIMEOUT;
3521 break;
3522 default:
3523 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3524 __func__);
3525 return -EINVAL;
3526 }
3527
3528 if (usb_set_lpm_timeout(udev, state, 0))
3529 return -EBUSY;
3530
3531 usb_set_device_initiated_lpm(udev, state, false);
3532
3533 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3534 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3535 "bus schedule bandwidth may be impacted.\n",
3536 usb3_lpm_names[state]);
3537 return 0;
3538}
3539
3540/*
3541 * Disable hub-initiated and device-initiated U1 and U2 entry.
3542 * Caller must own the bandwidth_mutex.
3543 *
3544 * This will call usb_enable_lpm() on failure, which will decrement
3545 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3546 */
3547int usb_disable_lpm(struct usb_device *udev)
3548{
3549 struct usb_hcd *hcd;
3550
3551 if (!udev || !udev->parent ||
3552 udev->speed != USB_SPEED_SUPER ||
3553 !udev->lpm_capable)
3554 return 0;
3555
3556 hcd = bus_to_hcd(udev->bus);
3557 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3558 return 0;
3559
3560 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003561 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003562 return 0;
3563
3564 /* If LPM is enabled, attempt to disable it. */
3565 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3566 goto enable_lpm;
3567 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3568 goto enable_lpm;
3569
3570 return 0;
3571
3572enable_lpm:
3573 usb_enable_lpm(udev);
3574 return -EBUSY;
3575}
3576EXPORT_SYMBOL_GPL(usb_disable_lpm);
3577
3578/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3579int usb_unlocked_disable_lpm(struct usb_device *udev)
3580{
3581 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3582 int ret;
3583
3584 if (!hcd)
3585 return -EINVAL;
3586
3587 mutex_lock(hcd->bandwidth_mutex);
3588 ret = usb_disable_lpm(udev);
3589 mutex_unlock(hcd->bandwidth_mutex);
3590
3591 return ret;
3592}
3593EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3594
3595/*
3596 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3597 * xHCI host policy may prevent U1 or U2 from being enabled.
3598 *
3599 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3600 * until the lpm_disable_count drops to zero. Caller must own the
3601 * bandwidth_mutex.
3602 */
3603void usb_enable_lpm(struct usb_device *udev)
3604{
3605 struct usb_hcd *hcd;
3606
3607 if (!udev || !udev->parent ||
3608 udev->speed != USB_SPEED_SUPER ||
3609 !udev->lpm_capable)
3610 return;
3611
3612 udev->lpm_disable_count--;
3613 hcd = bus_to_hcd(udev->bus);
3614 /* Double check that we can both enable and disable LPM.
3615 * Device must be configured to accept set feature U1/U2 timeout.
3616 */
3617 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3618 !hcd->driver->disable_usb3_lpm_timeout)
3619 return;
3620
3621 if (udev->lpm_disable_count > 0)
3622 return;
3623
3624 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3625 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3626}
3627EXPORT_SYMBOL_GPL(usb_enable_lpm);
3628
3629/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3630void usb_unlocked_enable_lpm(struct usb_device *udev)
3631{
3632 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3633
3634 if (!hcd)
3635 return;
3636
3637 mutex_lock(hcd->bandwidth_mutex);
3638 usb_enable_lpm(udev);
3639 mutex_unlock(hcd->bandwidth_mutex);
3640}
3641EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3642
3643
Alan Sternd388dab2006-07-01 22:14:24 -04003644#else /* CONFIG_PM */
3645
Alan Sternf07600c2007-05-30 15:38:16 -04003646#define hub_suspend NULL
3647#define hub_resume NULL
3648#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003649
3650int usb_disable_lpm(struct usb_device *udev)
3651{
3652 return 0;
3653}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003654EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003655
3656void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003657EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003658
3659int usb_unlocked_disable_lpm(struct usb_device *udev)
3660{
3661 return 0;
3662}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003663EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003664
3665void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003666EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003667
3668int usb_disable_ltm(struct usb_device *udev)
3669{
3670 return 0;
3671}
3672EXPORT_SYMBOL_GPL(usb_disable_ltm);
3673
3674void usb_enable_ltm(struct usb_device *udev) { }
3675EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003676#endif
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678
3679/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3680 *
3681 * Between connect detection and reset signaling there must be a delay
3682 * of 100ms at least for debounce and power-settling. The corresponding
3683 * timer shall restart whenever the downstream port detects a disconnect.
3684 *
3685 * Apparently there are some bluetooth and irda-dongles and a number of
3686 * low-speed devices for which this debounce period may last over a second.
3687 * Not covered by the spec - but easy to deal with.
3688 *
3689 * This implementation uses a 1500ms total debounce timeout; if the
3690 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3691 * every 25ms for transient disconnects. When the port status has been
3692 * unchanged for 100ms it returns the port status.
3693 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694static int hub_port_debounce(struct usb_hub *hub, int port1)
3695{
3696 int ret;
3697 int total_time, stable_time = 0;
3698 u16 portchange, portstatus;
3699 unsigned connection = 0xffff;
3700
3701 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3702 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3703 if (ret < 0)
3704 return ret;
3705
3706 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3707 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3708 stable_time += HUB_DEBOUNCE_STEP;
3709 if (stable_time >= HUB_DEBOUNCE_STABLE)
3710 break;
3711 } else {
3712 stable_time = 0;
3713 connection = portstatus & USB_PORT_STAT_CONNECTION;
3714 }
3715
3716 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3717 clear_port_feature(hub->hdev, port1,
3718 USB_PORT_FEAT_C_CONNECTION);
3719 }
3720
3721 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3722 break;
3723 msleep(HUB_DEBOUNCE_STEP);
3724 }
3725
3726 dev_dbg (hub->intfdev,
3727 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3728 port1, total_time, stable_time, portstatus);
3729
3730 if (stable_time < HUB_DEBOUNCE_STABLE)
3731 return -ETIMEDOUT;
3732 return portstatus;
3733}
3734
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003735void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736{
Alan Sternddeac4e2009-01-15 17:03:33 -05003737 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3738 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003739 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003741EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742
3743#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3744#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3745
Alan Stern4326ed02007-07-30 17:08:43 -04003746static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
3748 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003749 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Sarah Sharpc6515272009-04-27 19:57:26 -07003751 /*
3752 * The host controller will choose the device address,
3753 * instead of the core having chosen it earlier
3754 */
3755 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 return -EINVAL;
3757 if (udev->state == USB_STATE_ADDRESS)
3758 return 0;
3759 if (udev->state != USB_STATE_DEFAULT)
3760 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003761 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003762 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003763 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003764 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3765 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3766 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003768 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003769 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003771 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 }
3773 return retval;
3774}
3775
3776/* Reset device, (re)assign address, get device descriptor.
3777 * Device connection must be stable, no more debouncing needed.
3778 * Returns device in USB_STATE_ADDRESS, except on error.
3779 *
3780 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003781 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 * newly detected device that is not accessible through any global
3783 * pointers, it's not necessary to lock the device.
3784 */
3785static int
3786hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3787 int retry_counter)
3788{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003789 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
3791 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003792 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 int i, j, retval;
3794 unsigned delay = HUB_SHORT_RESET_TIME;
3795 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003796 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003797 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 /* root hub ports have a slightly longer reset period
3800 * (from USB 2.0 spec, section 7.1.7.5)
3801 */
3802 if (!hdev->parent) {
3803 delay = HUB_ROOT_RESET_TIME;
3804 if (port1 == hdev->bus->otg_port)
3805 hdev->bus->b_hnp_enable = 0;
3806 }
3807
3808 /* Some low speed devices have problems with the quick delay, so */
3809 /* be a bit pessimistic with those devices. RHbug #23670 */
3810 if (oldspeed == USB_SPEED_LOW)
3811 delay = HUB_LONG_RESET_TIME;
3812
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003813 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814
Luben Tuikov07194ab2011-02-11 11:33:10 -08003815 /* Reset the device; full speed may morph to high speed */
3816 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003817 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003818 if (retval < 0) /* error or disconnect */
3819 goto fail;
3820 /* success, speed is known */
3821
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 retval = -ENODEV;
3823
3824 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3825 dev_dbg(&udev->dev, "device reset changed speed!\n");
3826 goto fail;
3827 }
3828 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3831 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003832 * For Wireless USB devices, ep0 max packet is always 512 (tho
3833 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 */
3835 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003836 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003837 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003838 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003841 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 break;
3843 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3844 /* to determine the ep0 maxpacket size, try to read
3845 * the device descriptor to get bMaxPacketSize0 and
3846 * then correct our initial guess.
3847 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003848 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 break;
3850 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003851 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 break;
3853 default:
3854 goto fail;
3855 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003856
3857 if (udev->speed == USB_SPEED_WIRELESS)
3858 speed = "variable speed Wireless";
3859 else
3860 speed = usb_speed_string(udev->speed);
3861
Sarah Sharpc6515272009-04-27 19:57:26 -07003862 if (udev->speed != USB_SPEED_SUPER)
3863 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003864 "%s %s USB device number %d using %s\n",
3865 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003866 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 /* Set up TT records, if needed */
3869 if (hdev->tt) {
3870 udev->tt = hdev->tt;
3871 udev->ttport = hdev->ttport;
3872 } else if (udev->speed != USB_SPEED_HIGH
3873 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003874 if (!hub->tt.hub) {
3875 dev_err(&udev->dev, "parent hub has no TT\n");
3876 retval = -EINVAL;
3877 goto fail;
3878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 udev->tt = &hub->tt;
3880 udev->ttport = port1;
3881 }
3882
3883 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3884 * Because device hardware and firmware is sometimes buggy in
3885 * this area, and this is how Linux has done it for ages.
3886 * Change it cautiously.
3887 *
3888 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3889 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3890 * so it may help with some non-standards-compliant devices.
3891 * Otherwise we start with SET_ADDRESS and then try to read the
3892 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3893 * value.
3894 */
3895 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003896 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 struct usb_device_descriptor *buf;
3898 int r = 0;
3899
3900#define GET_DESCRIPTOR_BUFSIZE 64
3901 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3902 if (!buf) {
3903 retval = -ENOMEM;
3904 continue;
3905 }
3906
Alan Sternb89ee192007-05-11 10:19:04 -04003907 /* Retry on all errors; some devices are flakey.
3908 * 255 is for WUSB devices, we actually need to use
3909 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 */
3911 for (j = 0; j < 3; ++j) {
3912 buf->bMaxPacketSize0 = 0;
3913 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3914 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3915 USB_DT_DEVICE << 8, 0,
3916 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003917 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003919 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 if (buf->bDescriptorType ==
3921 USB_DT_DEVICE) {
3922 r = 0;
3923 break;
3924 }
3925 /* FALL THROUGH */
3926 default:
3927 if (r == 0)
3928 r = -EPROTO;
3929 break;
3930 }
3931 if (r == 0)
3932 break;
3933 }
3934 udev->descriptor.bMaxPacketSize0 =
3935 buf->bMaxPacketSize0;
3936 kfree(buf);
3937
Andiry Xu75d7cf72011-09-13 16:41:11 -07003938 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 if (retval < 0) /* error or disconnect */
3940 goto fail;
3941 if (oldspeed != udev->speed) {
3942 dev_dbg(&udev->dev,
3943 "device reset changed speed!\n");
3944 retval = -ENODEV;
3945 goto fail;
3946 }
3947 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003948 dev_err(&udev->dev,
3949 "device descriptor read/64, error %d\n",
3950 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 retval = -EMSGSIZE;
3952 continue;
3953 }
3954#undef GET_DESCRIPTOR_BUFSIZE
3955 }
3956
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003957 /*
3958 * If device is WUSB, we already assigned an
3959 * unauthorized address in the Connect Ack sequence;
3960 * authorization will assign the final address.
3961 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003962 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003963 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3964 retval = hub_set_address(udev, devnum);
3965 if (retval >= 0)
3966 break;
3967 msleep(200);
3968 }
3969 if (retval < 0) {
3970 dev_err(&udev->dev,
3971 "device not accepting address %d, error %d\n",
3972 devnum, retval);
3973 goto fail;
3974 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003975 if (udev->speed == USB_SPEED_SUPER) {
3976 devnum = udev->devnum;
3977 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003978 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003979 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003980 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003981 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003982
3983 /* cope with hardware quirkiness:
3984 * - let SET_ADDRESS settle, some device hardware wants it
3985 * - read ep0 maxpacket even for high and low speed,
3986 */
3987 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003988 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 retval = usb_get_device_descriptor(udev, 8);
3993 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003994 dev_err(&udev->dev,
3995 "device descriptor read/8, error %d\n",
3996 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 if (retval >= 0)
3998 retval = -EMSGSIZE;
3999 } else {
4000 retval = 0;
4001 break;
4002 }
4003 }
4004 if (retval)
4005 goto fail;
4006
Elric Fud8aec3d2012-03-26 21:16:02 +08004007 /*
4008 * Some superspeed devices have finished the link training process
4009 * and attached to a superspeed hub port, but the device descriptor
4010 * got from those devices show they aren't superspeed devices. Warm
4011 * reset the port attached by the devices can fix them.
4012 */
4013 if ((udev->speed == USB_SPEED_SUPER) &&
4014 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4015 dev_err(&udev->dev, "got a wrong device descriptor, "
4016 "warm reset device\n");
4017 hub_port_reset(hub, port1, udev,
4018 HUB_BH_RESET_TIME, true);
4019 retval = -EINVAL;
4020 goto fail;
4021 }
4022
Sarah Sharp6b403b02009-04-27 19:54:10 -07004023 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4024 udev->speed == USB_SPEED_SUPER)
4025 i = 512;
4026 else
4027 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004028 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004029 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004031 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 retval = -EMSGSIZE;
4033 goto fail;
4034 }
Alan Stern56626a72010-10-14 15:25:21 -04004035 if (udev->speed == USB_SPEED_FULL)
4036 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4037 else
4038 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004040 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 }
4042
4043 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4044 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004045 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4046 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 if (retval >= 0)
4048 retval = -ENOMSG;
4049 goto fail;
4050 }
4051
Andiry Xu1ff4df52011-09-23 14:19:48 -07004052 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4053 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004054 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004055 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004056 usb_set_lpm_parameters(udev);
4057 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004058 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004059
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004061 /* notify HCD that we have a device connected and addressed */
4062 if (hcd->driver->update_device)
4063 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004065 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004067 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004068 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004069 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 return retval;
4071}
4072
4073static void
4074check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4075{
4076 struct usb_qualifier_descriptor *qual;
4077 int status;
4078
Christoph Lametere94b1762006-12-06 20:33:17 -08004079 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 if (qual == NULL)
4081 return;
4082
4083 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4084 qual, sizeof *qual);
4085 if (status == sizeof *qual) {
4086 dev_info(&udev->dev, "not running at top speed; "
4087 "connect to a high speed hub\n");
4088 /* hub LEDs are probably harder to miss than syslog */
4089 if (hub->has_indicators) {
4090 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004091 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 }
4093 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004094 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095}
4096
4097static unsigned
4098hub_power_remaining (struct usb_hub *hub)
4099{
4100 struct usb_device *hdev = hub->hdev;
4101 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004102 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Alan Stern55c52712005-11-23 12:03:12 -05004104 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return 0;
4106
Alan Stern55c52712005-11-23 12:03:12 -05004107 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4108 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c792012-09-05 13:44:32 +08004109 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004110 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
4112 if (!udev)
4113 continue;
4114
Alan Stern55c52712005-11-23 12:03:12 -05004115 /* Unconfigured devices may not use more than 100mA,
4116 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004118 delta = udev->actconfig->desc.bMaxPower * 2;
4119 else if (port1 != udev->bus->otg_port || hdev->parent)
4120 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 else
Alan Stern55c52712005-11-23 12:03:12 -05004122 delta = 8;
4123 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004124 dev_warn(&udev->dev,
4125 "%dmA is over %umA budget for port %d!\n",
4126 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 remaining -= delta;
4128 }
4129 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004130 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4131 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 remaining = 0;
4133 }
4134 return remaining;
4135}
4136
4137/* Handle physical or logical connection change events.
4138 * This routine is called when:
4139 * a port connection-change occurs;
4140 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004141 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 * a firmware download)
4143 * caller already locked the hub
4144 */
4145static void hub_port_connect_change(struct usb_hub *hub, int port1,
4146 u16 portstatus, u16 portchange)
4147{
4148 struct usb_device *hdev = hub->hdev;
4149 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304150 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004151 unsigned wHubCharacteristics =
4152 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004153 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 dev_dbg (hub_dev,
4157 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004158 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
4160 if (hub->has_indicators) {
4161 set_port_led(hub, port1, HUB_LED_AUTO);
4162 hub->indicator[port1-1] = INDICATOR_AUTO;
4163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
4165#ifdef CONFIG_USB_OTG
4166 /* during HNP, don't repeat the debounce */
4167 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004168 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4169 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170#endif
4171
Alan Stern8808f002008-04-28 11:06:55 -04004172 /* Try to resuscitate an existing device */
Lan Tianyuff823c792012-09-05 13:44:32 +08004173 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004174 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4175 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004176 usb_lock_device(udev);
4177 if (portstatus & USB_PORT_STAT_ENABLE) {
4178 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004179
4180#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004181 } else if (udev->state == USB_STATE_SUSPENDED &&
4182 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004183 /* For a suspended device, treat this as a
4184 * remote wakeup event.
4185 */
Alan Stern0534d462010-01-08 12:56:30 -05004186 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004187#endif
4188
4189 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004190 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004191 }
4192 usb_unlock_device(udev);
4193
4194 if (status == 0) {
4195 clear_bit(port1, hub->change_bits);
4196 return;
4197 }
4198 }
4199
Alan Stern24618b02008-04-28 11:06:28 -04004200 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004201 if (udev)
Lan Tianyuff823c792012-09-05 13:44:32 +08004202 usb_disconnect(&hub->ports[port1 - 1]->child);
Alan Stern24618b02008-04-28 11:06:28 -04004203 clear_bit(port1, hub->change_bits);
4204
Alan Stern253e0572009-10-27 15:20:13 -04004205 /* We can forget about a "removed" device when there's a physical
4206 * disconnect or the connect status changes.
4207 */
4208 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4209 (portchange & USB_PORT_STAT_C_CONNECTION))
4210 clear_bit(port1, hub->removed_bits);
4211
Alan Stern5257d972008-09-22 14:43:08 -04004212 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4213 USB_PORT_STAT_C_ENABLE)) {
4214 status = hub_port_debounce(hub, port1);
4215 if (status < 0) {
4216 if (printk_ratelimit())
4217 dev_err(hub_dev, "connect-debounce failed, "
4218 "port %d disabled\n", port1);
4219 portstatus &= ~USB_PORT_STAT_CONNECTION;
4220 } else {
4221 portstatus = status;
4222 }
4223 }
4224
Richard Zhao925aa462012-07-11 11:09:28 +08004225 if (hcd->phy && !hdev->parent) {
4226 if (portstatus & USB_PORT_STAT_CONNECTION)
4227 usb_phy_notify_connect(hcd->phy, port1);
4228 else
4229 usb_phy_notify_disconnect(hcd->phy, port1);
4230 }
4231
Alan Stern253e0572009-10-27 15:20:13 -04004232 /* Return now if debouncing failed or nothing is connected or
4233 * the device was "removed".
4234 */
4235 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4236 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
4238 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004239 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004240 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004242
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 if (portstatus & USB_PORT_STAT_ENABLE)
4244 goto done;
4245 return;
4246 }
4247
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 /* reallocate for each attempt, since references
4251 * to the previous one can escape in various ways
4252 */
4253 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4254 if (!udev) {
4255 dev_err (hub_dev,
4256 "couldn't allocate port %d usb_device\n",
4257 port1);
4258 goto done;
4259 }
4260
4261 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004262 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004263 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004264 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004265
Sarah Sharp131dec32010-12-06 21:00:19 -08004266 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4267 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004268 udev->speed = USB_SPEED_SUPER;
4269 else
4270 udev->speed = USB_SPEED_UNKNOWN;
4271
Alan Stern3b29b682011-02-22 09:53:41 -05004272 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004273 if (udev->devnum <= 0) {
4274 status = -ENOTCONN; /* Don't retry */
4275 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004276 }
4277
Sarah Sharpe7b77172009-04-27 19:54:26 -07004278 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 status = hub_port_init(hub, udev, port1, i);
4280 if (status < 0)
4281 goto loop;
4282
Phil Dibowitz93362a82010-07-22 00:05:01 +02004283 usb_detect_quirks(udev);
4284 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4285 msleep(1000);
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 /* consecutive bus-powered hubs aren't reliable; they can
4288 * violate the voltage drop budget. if the new child has
4289 * a "powered" LED, users should notice we didn't enable it
4290 * (without reading syslog), even without per-port LEDs
4291 * on the parent.
4292 */
4293 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004294 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 u16 devstat;
4296
4297 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4298 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004299 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 dev_dbg(&udev->dev, "get status %d ?\n", status);
4301 goto loop_disable;
4302 }
Alan Stern55c52712005-11-23 12:03:12 -05004303 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4305 dev_err(&udev->dev,
4306 "can't connect bus-powered hub "
4307 "to this port\n");
4308 if (hub->has_indicators) {
4309 hub->indicator[port1-1] =
4310 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004311 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 }
4313 status = -ENOTCONN; /* Don't retry */
4314 goto loop_disable;
4315 }
4316 }
4317
4318 /* check for devices running slower than they could */
4319 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4320 && udev->speed == USB_SPEED_FULL
4321 && highspeed_hubs != 0)
4322 check_highspeed (hub, udev, port1);
4323
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004324 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 * udev becomes globally accessible, although presumably
4326 * no one will look at it until hdev is unlocked.
4327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 status = 0;
4329
4330 /* We mustn't add new devices if the parent hub has
4331 * been disconnected; we would race with the
4332 * recursively_mark_NOTATTACHED() routine.
4333 */
4334 spin_lock_irq(&device_state_lock);
4335 if (hdev->state == USB_STATE_NOTATTACHED)
4336 status = -ENOTCONN;
4337 else
Lan Tianyuff823c792012-09-05 13:44:32 +08004338 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 spin_unlock_irq(&device_state_lock);
4340
4341 /* Run it through the hoops (find a driver, etc) */
4342 if (!status) {
4343 status = usb_new_device(udev);
4344 if (status) {
4345 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c792012-09-05 13:44:32 +08004346 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 spin_unlock_irq(&device_state_lock);
4348 }
4349 }
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 if (status)
4352 goto loop_disable;
4353
4354 status = hub_power_remaining(hub);
4355 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004356 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
4358 return;
4359
4360loop_disable:
4361 hub_port_disable(hub, port1, 1);
4362loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004363 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004364 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004365 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004367 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 break;
4369 }
Alan Stern3a311552008-05-20 16:58:29 -04004370 if (hub->hdev->parent ||
4371 !hcd->driver->port_handed_over ||
4372 !(hcd->driver->port_handed_over)(hcd, port1))
4373 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4374 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375
4376done:
4377 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304378 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4379 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
Sarah Sharp714b07b2012-01-24 13:53:18 -08004382/* Returns 1 if there was a remote wakeup and a connect status change. */
4383static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004384 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004385{
4386 struct usb_device *hdev;
4387 struct usb_device *udev;
4388 int connect_change = 0;
4389 int ret;
4390
4391 hdev = hub->hdev;
Lan Tianyuff823c792012-09-05 13:44:32 +08004392 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004393 if (!hub_is_superspeed(hdev)) {
4394 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4395 return 0;
4396 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4397 } else {
4398 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004399 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4400 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004401 return 0;
4402 }
4403
Sarah Sharp714b07b2012-01-24 13:53:18 -08004404 if (udev) {
4405 /* TRSMRCY = 10 msec */
4406 msleep(10);
4407
4408 usb_lock_device(udev);
4409 ret = usb_remote_wakeup(udev);
4410 usb_unlock_device(udev);
4411 if (ret < 0)
4412 connect_change = 1;
4413 } else {
4414 ret = -ENODEV;
4415 hub_port_disable(hub, port, 1);
4416 }
4417 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4418 port, ret);
4419 return connect_change;
4420}
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422static void hub_events(void)
4423{
4424 struct list_head *tmp;
4425 struct usb_device *hdev;
4426 struct usb_interface *intf;
4427 struct usb_hub *hub;
4428 struct device *hub_dev;
4429 u16 hubstatus;
4430 u16 hubchange;
4431 u16 portstatus;
4432 u16 portchange;
4433 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004434 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 /*
4437 * We restart the list every time to avoid a deadlock with
4438 * deleting hubs downstream from this one. This should be
4439 * safe since we delete the hub from the event list.
4440 * Not the most efficient, but avoids deadlocks.
4441 */
4442 while (1) {
4443
4444 /* Grab the first entry at the beginning of the list */
4445 spin_lock_irq(&hub_event_lock);
4446 if (list_empty(&hub_event_list)) {
4447 spin_unlock_irq(&hub_event_lock);
4448 break;
4449 }
4450
4451 tmp = hub_event_list.next;
4452 list_del_init(tmp);
4453
4454 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004455 kref_get(&hub->kref);
4456 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Alan Sterne8054852007-05-04 11:55:11 -04004458 hdev = hub->hdev;
4459 hub_dev = hub->intfdev;
4460 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004461 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 hdev->state, hub->descriptor
4463 ? hub->descriptor->bNbrPorts
4464 : 0,
4465 /* NOTE: expects max 15 ports... */
4466 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004467 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 /* Lock the device, then check to see if we were
4470 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004471 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004472 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004473 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
4475 /* If the hub has died, clean up after it */
4476 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004477 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004478 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 goto loop;
4480 }
4481
Alan Stern40f122f2006-11-09 14:44:33 -05004482 /* Autoresume */
4483 ret = usb_autopm_get_interface(intf);
4484 if (ret) {
4485 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004487 }
4488
4489 /* If this is an inactive hub, do nothing */
4490 if (hub->quiescing)
4491 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
4493 if (hub->error) {
4494 dev_dbg (hub_dev, "resetting for error %d\n",
4495 hub->error);
4496
Ming Lei742120c2008-06-18 22:00:29 +08004497 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 if (ret) {
4499 dev_dbg (hub_dev,
4500 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004501 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 }
4503
4504 hub->nerrors = 0;
4505 hub->error = 0;
4506 }
4507
4508 /* deal with port status changes */
4509 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4510 if (test_bit(i, hub->busy_bits))
4511 continue;
4512 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004513 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004515 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 continue;
4517
4518 ret = hub_port_status(hub, i,
4519 &portstatus, &portchange);
4520 if (ret < 0)
4521 continue;
4522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4524 clear_port_feature(hdev, i,
4525 USB_PORT_FEAT_C_CONNECTION);
4526 connect_change = 1;
4527 }
4528
4529 if (portchange & USB_PORT_STAT_C_ENABLE) {
4530 if (!connect_change)
4531 dev_dbg (hub_dev,
4532 "port %d enable change, "
4533 "status %08x\n",
4534 i, portstatus);
4535 clear_port_feature(hdev, i,
4536 USB_PORT_FEAT_C_ENABLE);
4537
4538 /*
4539 * EM interference sometimes causes badly
4540 * shielded USB devices to be shutdown by
4541 * the hub, this hack enables them again.
4542 * Works at least with mouse driver.
4543 */
4544 if (!(portstatus & USB_PORT_STAT_ENABLE)
4545 && !connect_change
Lan Tianyuff823c792012-09-05 13:44:32 +08004546 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 dev_err (hub_dev,
4548 "port %i "
4549 "disabled by hub (EMI?), "
4550 "re-enabling...\n",
4551 i);
4552 connect_change = 1;
4553 }
4554 }
4555
Sarah Sharp72937e12012-01-24 11:46:50 -08004556 if (hub_handle_remote_wakeup(hub, i,
4557 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004558 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004561 u16 status = 0;
4562 u16 unused;
4563
4564 dev_dbg(hub_dev, "over-current change on port "
4565 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 clear_port_feature(hdev, i,
4567 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004568 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004569 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004570 hub_port_status(hub, i, &status, &unused);
4571 if (status & USB_PORT_STAT_OVERCURRENT)
4572 dev_err(hub_dev, "over-current "
4573 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 }
4575
4576 if (portchange & USB_PORT_STAT_C_RESET) {
4577 dev_dbg (hub_dev,
4578 "reset change on port %d\n",
4579 i);
4580 clear_port_feature(hdev, i,
4581 USB_PORT_FEAT_C_RESET);
4582 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004583 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4584 hub_is_superspeed(hub->hdev)) {
4585 dev_dbg(hub_dev,
4586 "warm reset change on port %d\n",
4587 i);
4588 clear_port_feature(hdev, i,
4589 USB_PORT_FEAT_C_BH_PORT_RESET);
4590 }
John Youndbe79bb2001-09-17 00:00:00 -07004591 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4592 clear_port_feature(hub->hdev, i,
4593 USB_PORT_FEAT_C_PORT_LINK_STATE);
4594 }
4595 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4596 dev_warn(hub_dev,
4597 "config error on port %d\n",
4598 i);
4599 clear_port_feature(hub->hdev, i,
4600 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
Andiry Xu5e467f62011-04-27 18:07:54 +08004603 /* Warm reset a USB3 protocol port if it's in
4604 * SS.Inactive state.
4605 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004606 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004607 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004608 hub_port_reset(hub, i, NULL,
4609 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004610 }
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 if (connect_change)
4613 hub_port_connect_change(hub, i,
4614 portstatus, portchange);
4615 } /* end for i */
4616
4617 /* deal with hub status changes */
4618 if (test_and_clear_bit(0, hub->event_bits) == 0)
4619 ; /* do nothing */
4620 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4621 dev_err (hub_dev, "get_hub_status failed\n");
4622 else {
4623 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4624 dev_dbg (hub_dev, "power change\n");
4625 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004626 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4627 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004628 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004629 else
4630 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 }
4632 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004633 u16 status = 0;
4634 u16 unused;
4635
4636 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004638 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004639 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004640 hub_hub_status(hub, &status, &unused);
4641 if (status & HUB_STATUS_OVERCURRENT)
4642 dev_err(hub_dev, "over-current "
4643 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
4645 }
4646
Alan Stern8e4ceb32009-12-07 13:01:37 -05004647 loop_autopm:
4648 /* Balance the usb_autopm_get_interface() above */
4649 usb_autopm_put_interface_no_suspend(intf);
4650 loop:
4651 /* Balance the usb_autopm_get_interface_no_resume() in
4652 * kick_khubd() and allow autosuspend.
4653 */
4654 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004655 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004657 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 } /* end while (1) */
4660}
4661
4662static int hub_thread(void *__unused)
4663{
Alan Stern3bb1af52008-03-03 15:15:36 -05004664 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4665 * port handover. Otherwise it might see that a full-speed device
4666 * was gone before the EHCI controller had handed its port over to
4667 * the companion full-speed controller.
4668 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004669 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 do {
4672 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004673 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004674 !list_empty(&hub_event_list) ||
4675 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004676 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004678 pr_debug("%s: khubd exiting\n", usbcore_name);
4679 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680}
4681
Németh Márton1e927d92010-01-10 15:34:53 +01004682static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08004683 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4684 | USB_DEVICE_ID_MATCH_INT_CLASS,
4685 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4686 .bInterfaceClass = USB_CLASS_HUB,
4687 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4689 .bDeviceClass = USB_CLASS_HUB},
4690 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4691 .bInterfaceClass = USB_CLASS_HUB},
4692 { } /* Terminating entry */
4693};
4694
4695MODULE_DEVICE_TABLE (usb, hub_id_table);
4696
4697static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 .name = "hub",
4699 .probe = hub_probe,
4700 .disconnect = hub_disconnect,
4701 .suspend = hub_suspend,
4702 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004703 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004704 .pre_reset = hub_pre_reset,
4705 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004706 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004708 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709};
4710
4711int usb_hub_init(void)
4712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 if (usb_register(&hub_driver) < 0) {
4714 printk(KERN_ERR "%s: can't register hub driver\n",
4715 usbcore_name);
4716 return -1;
4717 }
4718
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004719 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4720 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
4723 /* Fall through if kernel_thread failed */
4724 usb_deregister(&hub_driver);
4725 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4726
4727 return -1;
4728}
4729
4730void usb_hub_cleanup(void)
4731{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004732 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 /*
4735 * Hub resources are freed for us by usb_deregister. It calls
4736 * usb_driver_purge on every device which in turn calls that
4737 * devices disconnect function if it is using this driver.
4738 * The hub_disconnect function takes care of releasing the
4739 * individual hub resources. -greg
4740 */
4741 usb_deregister(&hub_driver);
4742} /* usb_hub_cleanup() */
4743
Alan Sterneb764c42008-03-03 15:16:04 -05004744static int descriptors_changed(struct usb_device *udev,
4745 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746{
Alan Sterneb764c42008-03-03 15:16:04 -05004747 int changed = 0;
4748 unsigned index;
4749 unsigned serial_len = 0;
4750 unsigned len;
4751 unsigned old_length;
4752 int length;
4753 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
Alan Sterneb764c42008-03-03 15:16:04 -05004755 if (memcmp(&udev->descriptor, old_device_descriptor,
4756 sizeof(*old_device_descriptor)) != 0)
4757 return 1;
4758
4759 /* Since the idVendor, idProduct, and bcdDevice values in the
4760 * device descriptor haven't changed, we will assume the
4761 * Manufacturer and Product strings haven't changed either.
4762 * But the SerialNumber string could be different (e.g., a
4763 * different flash card of the same brand).
4764 */
4765 if (udev->serial)
4766 serial_len = strlen(udev->serial) + 1;
4767
4768 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004770 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4771 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 }
Alan Sterneb764c42008-03-03 15:16:04 -05004773
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004774 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 if (buf == NULL) {
4776 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4777 /* assume the worst */
4778 return 1;
4779 }
4780 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004781 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4783 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004784 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 dev_dbg(&udev->dev, "config index %d, error %d\n",
4786 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004787 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 break;
4789 }
4790 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4791 != 0) {
4792 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004793 index,
4794 ((struct usb_config_descriptor *) buf)->
4795 bConfigurationValue);
4796 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 break;
4798 }
4799 }
Alan Sterneb764c42008-03-03 15:16:04 -05004800
4801 if (!changed && serial_len) {
4802 length = usb_string(udev, udev->descriptor.iSerialNumber,
4803 buf, serial_len);
4804 if (length + 1 != serial_len) {
4805 dev_dbg(&udev->dev, "serial string error %d\n",
4806 length);
4807 changed = 1;
4808 } else if (memcmp(buf, udev->serial, length) != 0) {
4809 dev_dbg(&udev->dev, "serial string changed\n");
4810 changed = 1;
4811 }
4812 }
4813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004815 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816}
4817
4818/**
Ming Lei742120c2008-06-18 22:00:29 +08004819 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4821 *
Alan Stern79efa092006-06-01 13:33:42 -04004822 * WARNING - don't use this routine to reset a composite device
4823 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004824 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 *
4826 * Do a port reset, reassign the device's address, and establish its
4827 * former operating configuration. If the reset fails, or the device's
4828 * descriptors change from their values before the reset, or the original
4829 * configuration and altsettings cannot be restored, a flag will be set
4830 * telling khubd to pretend the device has been disconnected and then
4831 * re-connected. All drivers will be unbound, and the device will be
4832 * re-enumerated and probed all over again.
4833 *
4834 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4835 * flagged for logical disconnection, or some other negative error code
4836 * if the reset wasn't even attempted.
4837 *
4838 * The caller must own the device lock. For example, it's safe to use
4839 * this from a driver probe() routine after downloading new firmware.
4840 * For calls that might not occur during probe(), drivers should lock
4841 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004842 *
4843 * Locking exception: This routine may also be called from within an
4844 * autoresume handler. Such usage won't conflict with other tasks
4845 * holding the device lock because these tasks should always call
4846 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 */
Ming Lei742120c2008-06-18 22:00:29 +08004848static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849{
4850 struct usb_device *parent_hdev = udev->parent;
4851 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004852 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004854 int i, ret = 0;
4855 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
4857 if (udev->state == USB_STATE_NOTATTACHED ||
4858 udev->state == USB_STATE_SUSPENDED) {
4859 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4860 udev->state);
4861 return -EINVAL;
4862 }
4863
4864 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004865 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004866 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 return -EISDIR;
4868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 parent_hub = hdev_to_hub(parent_hdev);
4870
Sarah Sharpf74631e2012-06-25 12:08:08 -07004871 /* Disable LPM and LTM while we reset the device and reinstall the alt
4872 * settings. Device-initiated LPM settings, and system exit latency
4873 * settings are cleared when the device is reset, so we have to set
4874 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004875 */
4876 ret = usb_unlocked_disable_lpm(udev);
4877 if (ret) {
4878 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4879 goto re_enumerate;
4880 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004881 ret = usb_disable_ltm(udev);
4882 if (ret) {
4883 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4884 __func__);
4885 goto re_enumerate;
4886 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 set_bit(port1, parent_hub->busy_bits);
4889 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4890
4891 /* ep0 maxpacket size may change; let the HCD know about it.
4892 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004893 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004895 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 break;
4897 }
4898 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004899
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 if (ret < 0)
4901 goto re_enumerate;
4902
4903 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004904 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 dev_info(&udev->dev, "device firmware changed\n");
4906 udev->descriptor = descriptor; /* for disconnect() calls */
4907 goto re_enumerate;
4908 }
Alan Stern4fe03872009-02-26 10:21:02 -05004909
4910 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 if (!udev->actconfig)
4912 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004913
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004914 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004915 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4916 if (ret < 0) {
4917 dev_warn(&udev->dev,
4918 "Busted HC? Not enough HCD resources for "
4919 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004920 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004921 goto re_enumerate;
4922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4924 USB_REQ_SET_CONFIGURATION, 0,
4925 udev->actconfig->desc.bConfigurationValue, 0,
4926 NULL, 0, USB_CTRL_SET_TIMEOUT);
4927 if (ret < 0) {
4928 dev_err(&udev->dev,
4929 "can't restore configuration #%d (error=%d)\n",
4930 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004931 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 goto re_enumerate;
4933 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004934 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4936
Alan Stern4fe03872009-02-26 10:21:02 -05004937 /* Put interfaces back into the same altsettings as before.
4938 * Don't bother to send the Set-Interface request for interfaces
4939 * that were already in altsetting 0; besides being unnecessary,
4940 * many devices can't handle it. Instead just reset the host-side
4941 * endpoint state.
4942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004944 struct usb_host_config *config = udev->actconfig;
4945 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 struct usb_interface_descriptor *desc;
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004949 if (desc->bAlternateSetting == 0) {
4950 usb_disable_interface(udev, intf, true);
4951 usb_enable_interface(udev, intf, true);
4952 ret = 0;
4953 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004954 /* Let the bandwidth allocation function know that this
4955 * device has been reset, and it will have to use
4956 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004957 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004958 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004959 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4960 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004961 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 if (ret < 0) {
4964 dev_err(&udev->dev, "failed to restore interface %d "
4965 "altsetting %d (error=%d)\n",
4966 desc->bInterfaceNumber,
4967 desc->bAlternateSetting,
4968 ret);
4969 goto re_enumerate;
4970 }
4971 }
4972
4973done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07004974 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04004975 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004976 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 return 0;
4978
4979re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004980 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 hub_port_logical_disconnect(parent_hub, port1);
4982 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
4985/**
4986 * usb_reset_device - warn interface drivers and perform a USB port reset
4987 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4988 *
Alan Stern79efa092006-06-01 13:33:42 -04004989 * Warns all drivers bound to registered interfaces (using their pre_reset
4990 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08004991 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04004992 *
Alan Stern79efa092006-06-01 13:33:42 -04004993 * Return value is the same as for usb_reset_and_verify_device().
4994 *
4995 * The caller must own the device lock. For example, it's safe to use
4996 * this from a driver probe() routine after downloading new firmware.
4997 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08004998 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04004999 *
5000 * If an interface is currently being probed or disconnected, we assume
5001 * its driver knows how to handle resets. For all other interfaces,
5002 * if the driver doesn't have pre_reset and post_reset methods then
5003 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005004 */
Ming Lei742120c2008-06-18 22:00:29 +08005005int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005006{
5007 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005008 int i;
Alan Stern79efa092006-06-01 13:33:42 -04005009 struct usb_host_config *config = udev->actconfig;
5010
5011 if (udev->state == USB_STATE_NOTATTACHED ||
5012 udev->state == USB_STATE_SUSPENDED) {
5013 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5014 udev->state);
5015 return -EINVAL;
5016 }
5017
Alan Stern645daaa2006-08-30 15:47:02 -04005018 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005019 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005020
Alan Stern79efa092006-06-01 13:33:42 -04005021 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005022 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005023 struct usb_interface *cintf = config->interface[i];
5024 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005025 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005026
5027 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005028 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005029 if (drv->pre_reset && drv->post_reset)
5030 unbind = (drv->pre_reset)(cintf);
5031 else if (cintf->condition ==
5032 USB_INTERFACE_BOUND)
5033 unbind = 1;
5034 if (unbind)
5035 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005036 }
5037 }
5038 }
5039
Ming Lei742120c2008-06-18 22:00:29 +08005040 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005041
5042 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005043 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005044 struct usb_interface *cintf = config->interface[i];
5045 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005046 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005047
Alan Stern78d9a482008-06-23 16:00:40 -04005048 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005049 drv = to_usb_driver(cintf->dev.driver);
5050 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005051 rebind = (drv->post_reset)(cintf);
5052 else if (cintf->condition ==
5053 USB_INTERFACE_BOUND)
5054 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005055 }
Alan Stern6c640942008-10-21 15:40:03 -04005056 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005057 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005058 }
5059 }
5060
Alan Stern94fcda12006-11-20 11:38:46 -05005061 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005062 return ret;
5063}
Ming Lei742120c2008-06-18 22:00:29 +08005064EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005065
5066
5067/**
5068 * usb_queue_reset_device - Reset a USB device from an atomic context
5069 * @iface: USB interface belonging to the device to reset
5070 *
5071 * This function can be used to reset a USB device from an atomic
5072 * context, where usb_reset_device() won't work (as it blocks).
5073 *
5074 * Doing a reset via this method is functionally equivalent to calling
5075 * usb_reset_device(), except for the fact that it is delayed to a
5076 * workqueue. This means that any drivers bound to other interfaces
5077 * might be unbound, as well as users from usbfs in user space.
5078 *
5079 * Corner cases:
5080 *
5081 * - Scheduling two resets at the same time from two different drivers
5082 * attached to two different interfaces of the same device is
5083 * possible; depending on how the driver attached to each interface
5084 * handles ->pre_reset(), the second reset might happen or not.
5085 *
5086 * - If a driver is unbound and it had a pending reset, the reset will
5087 * be cancelled.
5088 *
5089 * - This function can be called during .probe() or .disconnect()
5090 * times. On return from .disconnect(), any pending resets will be
5091 * cancelled.
5092 *
5093 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5094 * does its own.
5095 *
5096 * NOTE: We don't do any reference count tracking because it is not
5097 * needed. The lifecycle of the work_struct is tied to the
5098 * usb_interface. Before destroying the interface we cancel the
5099 * work_struct, so the fact that work_struct is queued and or
5100 * running means the interface (and thus, the device) exist and
5101 * are referenced.
5102 */
5103void usb_queue_reset_device(struct usb_interface *iface)
5104{
5105 schedule_work(&iface->reset_ws);
5106}
5107EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c792012-09-05 13:44:32 +08005108
5109/**
5110 * usb_hub_find_child - Get the pointer of child device
5111 * attached to the port which is specified by @port1.
5112 * @hdev: USB device belonging to the usb hub
5113 * @port1: port num to indicate which port the child device
5114 * is attached to.
5115 *
5116 * USB drivers call this function to get hub's child device
5117 * pointer.
5118 *
5119 * Return NULL if input param is invalid and
5120 * child's usb_device pointer if non-NULL.
5121 */
5122struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5123 int port1)
5124{
5125 struct usb_hub *hub = hdev_to_hub(hdev);
5126
5127 if (port1 < 1 || port1 > hdev->maxchild)
5128 return NULL;
5129 return hub->ports[port1 - 1]->child;
5130}
5131EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005132
Lan Tianyu05f91682012-09-05 13:44:34 +08005133/**
5134 * usb_set_hub_port_connect_type - set hub port connect type.
5135 * @hdev: USB device belonging to the usb hub
5136 * @port1: port num of the port
5137 * @type: connect type of the port
5138 */
5139void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5140 enum usb_port_connect_type type)
5141{
5142 struct usb_hub *hub = hdev_to_hub(hdev);
5143
5144 hub->ports[port1 - 1]->connect_type = type;
5145}
5146
5147/**
5148 * usb_get_hub_port_connect_type - Get the port's connect type
5149 * @hdev: USB device belonging to the usb hub
5150 * @port1: port num of the port
5151 *
5152 * Return connect type of the port and if input params are
5153 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5154 */
5155enum usb_port_connect_type
5156usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5157{
5158 struct usb_hub *hub = hdev_to_hub(hdev);
5159
5160 return hub->ports[port1 - 1]->connect_type;
5161}
5162
Lan Tianyud5575422012-09-05 13:44:33 +08005163#ifdef CONFIG_ACPI
5164/**
5165 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5166 * @hdev: USB device belonging to the usb hub
5167 * @port1: port num of the port
5168 *
5169 * Return port's acpi handle if successful, NULL if params are
5170 * invaild.
5171 */
5172acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5173 int port1)
5174{
5175 struct usb_hub *hub = hdev_to_hub(hdev);
5176
5177 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5178}
5179#endif