blob: c8b9fa0e92751ee9cb7d9a59c6711b64febb11df [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>
Lan Tianyuad493e52013-01-23 04:26:30 +080029#include <linux/pm_qos.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/uaccess.h>
32#include <asm/byteorder.h>
33
Lan Tianyu6e30d7c2013-01-11 20:10:38 +080034#include "hub.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080036/* if we are in debug mode, always announce new devices */
37#ifdef DEBUG
38#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
39#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
40#endif
41#endif
42
Ming Leie6f30de2012-10-24 11:59:24 +080043#define USB_VENDOR_GENESYS_LOGIC 0x05e3
44#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
45
John Youndbe79bb2001-09-17 00:00:00 -070046static inline int hub_is_superspeed(struct usb_device *hdev)
47{
Aman Deep7bf01182011-12-08 12:05:22 +053048 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070049}
Alan Stern1bb5f662006-11-06 11:56:13 -050050
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070051/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050052 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
54static DEFINE_SPINLOCK(device_state_lock);
55
56/* khubd's worklist and its lock */
57static DEFINE_SPINLOCK(hub_event_lock);
58static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
59
60/* Wakes up khubd */
61static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
62
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070063static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103066static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067module_param (blinkenlights, bool, S_IRUGO);
68MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
69
70/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020071 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
72 * 10 seconds to send reply for the initial 64-byte descriptor request.
73 */
74/* define initial 64-byte descriptor request timeout in milliseconds */
75static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
76module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +080077MODULE_PARM_DESC(initial_descriptor_timeout,
78 "initial 64-byte descriptor request timeout in milliseconds "
79 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020080
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * As of 2.6.10 we introduce a new USB device initialization scheme which
83 * closely resembles the way Windows works. Hopefully it will be compatible
84 * with a wider range of devices than the old scheme. However some previously
85 * working devices may start giving rise to "device not accepting address"
86 * errors; if that happens the user can try the old scheme by adjusting the
87 * following module parameters.
88 *
89 * For maximum flexibility there are two boolean parameters to control the
90 * hub driver's behavior. On the first initialization attempt, if the
91 * "old_scheme_first" parameter is set then the old scheme will be used,
92 * otherwise the new scheme is used. If that fails and "use_both_schemes"
93 * is set, then the driver will make another attempt, using the other scheme.
94 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103095static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
97MODULE_PARM_DESC(old_scheme_first,
98 "start with the old device initialization scheme");
99
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030100static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
102MODULE_PARM_DESC(use_both_schemes,
103 "try the other device initialization scheme if the "
104 "first one fails");
105
Alan Stern32fe0192007-10-10 16:27:07 -0400106/* Mutual exclusion for EHCI CF initialization. This interferes with
107 * port reset on some companion controllers.
108 */
109DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
110EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
111
Lan Tianyuad493e52013-01-23 04:26:30 +0800112#define HUB_DEBOUNCE_TIMEOUT 2000
Alan Stern948fea32008-04-28 11:07:07 -0400113#define HUB_DEBOUNCE_STEP 25
114#define HUB_DEBOUNCE_STABLE 100
115
Ming Lei742120c2008-06-18 22:00:29 +0800116static int usb_reset_and_verify_device(struct usb_device *udev);
117
Sarah Sharp131dec32010-12-06 21:00:19 -0800118static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Sarah Sharp131dec32010-12-06 21:00:19 -0800120 if (hub_is_superspeed(hub->hdev))
121 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500122 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500124 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return "1.5 Mb/s";
126 else
127 return "12 Mb/s";
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/* Note that hdev or one of its children must be locked! */
Lan Tianyuad493e52013-01-23 04:26:30 +0800131struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Lan Tianyuff823c72012-09-05 13:44:32 +0800133 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400134 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return usb_get_intfdata(hdev->actconfig->interface[0]);
136}
137
Xenia Ragiadakoua5ca6f02013-08-31 18:09:12 +0300138int usb_device_supports_lpm(struct usb_device *udev)
Sarah Sharpd9b20992012-02-20 08:31:26 -0800139{
140 /* USB 2.1 (and greater) devices indicate LPM support through
141 * their USB 2.0 Extended Capabilities BOS descriptor.
142 */
143 if (udev->speed == USB_SPEED_HIGH) {
144 if (udev->bos->ext_cap &&
145 (USB_LPM_SUPPORT &
146 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
147 return 1;
148 return 0;
149 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800150
151 /* All USB 3.0 must support LPM, but we need their max exit latency
152 * information from the SuperSpeed Extended Capabilities BOS descriptor.
153 */
154 if (!udev->bos->ss_cap) {
155 dev_warn(&udev->dev, "No LPM exit latency info found. "
156 "Power management will be impacted.\n");
157 return 0;
158 }
Xenia Ragiadakoua5ca6f02013-08-31 18:09:12 +0300159
160 /* udev is root hub */
161 if (!udev->parent)
162 return 1;
163
Sarah Sharp51e0a012012-02-20 12:02:19 -0800164 if (udev->parent->lpm_capable)
165 return 1;
166
167 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
168 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800169 return 0;
170}
171
Sarah Sharp51e0a012012-02-20 12:02:19 -0800172/*
173 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
174 * either U1 or U2.
175 */
176static void usb_set_lpm_mel(struct usb_device *udev,
177 struct usb3_lpm_parameters *udev_lpm_params,
178 unsigned int udev_exit_latency,
179 struct usb_hub *hub,
180 struct usb3_lpm_parameters *hub_lpm_params,
181 unsigned int hub_exit_latency)
182{
183 unsigned int total_mel;
184 unsigned int device_mel;
185 unsigned int hub_mel;
186
187 /*
188 * Calculate the time it takes to transition all links from the roothub
189 * to the parent hub into U0. The parent hub must then decode the
190 * packet (hub header decode latency) to figure out which port it was
191 * bound for.
192 *
193 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
194 * means 0.1us). Multiply that by 100 to get nanoseconds.
195 */
196 total_mel = hub_lpm_params->mel +
197 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
198
199 /*
200 * How long will it take to transition the downstream hub's port into
201 * U0? The greater of either the hub exit latency or the device exit
202 * latency.
203 *
204 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
205 * Multiply that by 1000 to get nanoseconds.
206 */
207 device_mel = udev_exit_latency * 1000;
208 hub_mel = hub_exit_latency * 1000;
209 if (device_mel > hub_mel)
210 total_mel += device_mel;
211 else
212 total_mel += hub_mel;
213
214 udev_lpm_params->mel = total_mel;
215}
216
217/*
218 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
219 * a transition from either U1 or U2.
220 */
221static void usb_set_lpm_pel(struct usb_device *udev,
222 struct usb3_lpm_parameters *udev_lpm_params,
223 unsigned int udev_exit_latency,
224 struct usb_hub *hub,
225 struct usb3_lpm_parameters *hub_lpm_params,
226 unsigned int hub_exit_latency,
227 unsigned int port_to_port_exit_latency)
228{
229 unsigned int first_link_pel;
230 unsigned int hub_pel;
231
232 /*
233 * First, the device sends an LFPS to transition the link between the
234 * device and the parent hub into U0. The exit latency is the bigger of
235 * the device exit latency or the hub exit latency.
236 */
237 if (udev_exit_latency > hub_exit_latency)
238 first_link_pel = udev_exit_latency * 1000;
239 else
240 first_link_pel = hub_exit_latency * 1000;
241
242 /*
243 * When the hub starts to receive the LFPS, there is a slight delay for
244 * it to figure out that one of the ports is sending an LFPS. Then it
245 * will forward the LFPS to its upstream link. The exit latency is the
246 * delay, plus the PEL that we calculated for this hub.
247 */
248 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
249
250 /*
251 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
252 * is the greater of the two exit latencies.
253 */
254 if (first_link_pel > hub_pel)
255 udev_lpm_params->pel = first_link_pel;
256 else
257 udev_lpm_params->pel = hub_pel;
258}
259
260/*
261 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
262 * when a device initiates a transition to U0, until when it will receive the
263 * first packet from the host controller.
264 *
265 * Section C.1.5.1 describes the four components to this:
266 * - t1: device PEL
267 * - t2: time for the ERDY to make it from the device to the host.
268 * - t3: a host-specific delay to process the ERDY.
269 * - t4: time for the packet to make it from the host to the device.
270 *
271 * t3 is specific to both the xHCI host and the platform the host is integrated
272 * into. The Intel HW folks have said it's negligible, FIXME if a different
273 * vendor says otherwise.
274 */
275static void usb_set_lpm_sel(struct usb_device *udev,
276 struct usb3_lpm_parameters *udev_lpm_params)
277{
278 struct usb_device *parent;
279 unsigned int num_hubs;
280 unsigned int total_sel;
281
282 /* t1 = device PEL */
283 total_sel = udev_lpm_params->pel;
284 /* How many external hubs are in between the device & the root port. */
285 for (parent = udev->parent, num_hubs = 0; parent->parent;
286 parent = parent->parent)
287 num_hubs++;
288 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
289 if (num_hubs > 0)
290 total_sel += 2100 + 250 * (num_hubs - 1);
291
292 /* t4 = 250ns * num_hubs */
293 total_sel += 250 * num_hubs;
294
295 udev_lpm_params->sel = total_sel;
296}
297
298static void usb_set_lpm_parameters(struct usb_device *udev)
299{
300 struct usb_hub *hub;
301 unsigned int port_to_port_delay;
302 unsigned int udev_u1_del;
303 unsigned int udev_u2_del;
304 unsigned int hub_u1_del;
305 unsigned int hub_u2_del;
306
307 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
308 return;
309
Lan Tianyuad493e52013-01-23 04:26:30 +0800310 hub = usb_hub_to_struct_hub(udev->parent);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800311 /* It doesn't take time to transition the roothub into U0, since it
312 * doesn't have an upstream link.
313 */
314 if (!hub)
315 return;
316
317 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
318 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
319 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
320 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
321
322 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
323 hub, &udev->parent->u1_params, hub_u1_del);
324
325 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
326 hub, &udev->parent->u2_params, hub_u2_del);
327
328 /*
329 * Appendix C, section C.2.2.2, says that there is a slight delay from
330 * when the parent hub notices the downstream port is trying to
331 * transition to U0 to when the hub initiates a U0 transition on its
332 * upstream port. The section says the delays are tPort2PortU1EL and
333 * tPort2PortU2EL, but it doesn't define what they are.
334 *
335 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
336 * about the same delays. Use the maximum delay calculations from those
337 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
338 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
339 * assume the device exit latencies they are talking about are the hub
340 * exit latencies.
341 *
342 * What do we do if the U2 exit latency is less than the U1 exit
343 * latency? It's possible, although not likely...
344 */
345 port_to_port_delay = 1;
346
347 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
348 hub, &udev->parent->u1_params, hub_u1_del,
349 port_to_port_delay);
350
351 if (hub_u2_del > hub_u1_del)
352 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
353 else
354 port_to_port_delay = 1 + hub_u1_del;
355
356 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
357 hub, &udev->parent->u2_params, hub_u2_del,
358 port_to_port_delay);
359
360 /* Now that we've got PEL, calculate SEL. */
361 usb_set_lpm_sel(udev, &udev->u1_params);
362 usb_set_lpm_sel(udev, &udev->u2_params);
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700366static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
John Youndbe79bb2001-09-17 00:00:00 -0700368 int i, ret, size;
369 unsigned dtype;
370
371 if (hub_is_superspeed(hdev)) {
372 dtype = USB_DT_SS_HUB;
373 size = USB_DT_SS_HUB_SIZE;
374 } else {
375 dtype = USB_DT_HUB;
376 size = sizeof(struct usb_hub_descriptor);
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 for (i = 0; i < 3; i++) {
380 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
381 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700382 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 USB_CTRL_GET_TIMEOUT);
384 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
385 return ret;
386 }
387 return -EINVAL;
388}
389
390/*
391 * USB 2.0 spec Section 11.24.2.1
392 */
393static int clear_hub_feature(struct usb_device *hdev, int feature)
394{
395 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
396 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
397}
398
399/*
400 * USB 2.0 spec Section 11.24.2.2
401 */
Lan Tianyuad493e52013-01-23 04:26:30 +0800402int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
405 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
406 NULL, 0, 1000);
407}
408
409/*
410 * USB 2.0 spec Section 11.24.2.13
411 */
412static int set_port_feature(struct usb_device *hdev, int port1, int feature)
413{
414 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
415 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
416 NULL, 0, 1000);
417}
418
419/*
420 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
421 * for info about using port indicators
422 */
423static void set_port_led(
424 struct usb_hub *hub,
425 int port1,
426 int selector
427)
428{
429 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
430 USB_PORT_FEAT_INDICATOR);
431 if (status < 0)
432 dev_dbg (hub->intfdev,
433 "port %d indicator %s status %d\n",
434 port1,
435 ({ char *s; switch (selector) {
436 case HUB_LED_AMBER: s = "amber"; break;
437 case HUB_LED_GREEN: s = "green"; break;
438 case HUB_LED_OFF: s = "off"; break;
439 case HUB_LED_AUTO: s = "auto"; break;
440 default: s = "??"; break;
441 }; s; }),
442 status);
443}
444
445#define LED_CYCLE_PERIOD ((2*HZ)/3)
446
David Howellsc4028952006-11-22 14:57:56 +0000447static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
David Howellsc4028952006-11-22 14:57:56 +0000449 struct usb_hub *hub =
450 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct usb_device *hdev = hub->hdev;
452 unsigned i;
453 unsigned changed = 0;
454 int cursor = -1;
455
456 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
457 return;
458
459 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
460 unsigned selector, mode;
461
462 /* 30%-50% duty cycle */
463
464 switch (hub->indicator[i]) {
465 /* cycle marker */
466 case INDICATOR_CYCLE:
467 cursor = i;
468 selector = HUB_LED_AUTO;
469 mode = INDICATOR_AUTO;
470 break;
471 /* blinking green = sw attention */
472 case INDICATOR_GREEN_BLINK:
473 selector = HUB_LED_GREEN;
474 mode = INDICATOR_GREEN_BLINK_OFF;
475 break;
476 case INDICATOR_GREEN_BLINK_OFF:
477 selector = HUB_LED_OFF;
478 mode = INDICATOR_GREEN_BLINK;
479 break;
480 /* blinking amber = hw attention */
481 case INDICATOR_AMBER_BLINK:
482 selector = HUB_LED_AMBER;
483 mode = INDICATOR_AMBER_BLINK_OFF;
484 break;
485 case INDICATOR_AMBER_BLINK_OFF:
486 selector = HUB_LED_OFF;
487 mode = INDICATOR_AMBER_BLINK;
488 break;
489 /* blink green/amber = reserved */
490 case INDICATOR_ALT_BLINK:
491 selector = HUB_LED_GREEN;
492 mode = INDICATOR_ALT_BLINK_OFF;
493 break;
494 case INDICATOR_ALT_BLINK_OFF:
495 selector = HUB_LED_AMBER;
496 mode = INDICATOR_ALT_BLINK;
497 break;
498 default:
499 continue;
500 }
501 if (selector != HUB_LED_AUTO)
502 changed = 1;
503 set_port_led(hub, i + 1, selector);
504 hub->indicator[i] = mode;
505 }
506 if (!changed && blinkenlights) {
507 cursor++;
508 cursor %= hub->descriptor->bNbrPorts;
509 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
510 hub->indicator[cursor] = INDICATOR_CYCLE;
511 changed++;
512 }
513 if (changed)
514 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
515}
516
517/* use a short timeout for hub/port status fetches */
518#define USB_STS_TIMEOUT 1000
519#define USB_STS_RETRIES 5
520
521/*
522 * USB 2.0 spec Section 11.24.2.6
523 */
524static int get_hub_status(struct usb_device *hdev,
525 struct usb_hub_status *data)
526{
527 int i, status = -ETIMEDOUT;
528
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200529 for (i = 0; i < USB_STS_RETRIES &&
530 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
532 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
533 data, sizeof(*data), USB_STS_TIMEOUT);
534 }
535 return status;
536}
537
538/*
539 * USB 2.0 spec Section 11.24.2.7
540 */
541static int get_port_status(struct usb_device *hdev, int port1,
542 struct usb_port_status *data)
543{
544 int i, status = -ETIMEDOUT;
545
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200546 for (i = 0; i < USB_STS_RETRIES &&
547 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
549 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
550 data, sizeof(*data), USB_STS_TIMEOUT);
551 }
552 return status;
553}
554
Alan Stern3eb14912008-03-03 15:15:43 -0500555static int hub_port_status(struct usb_hub *hub, int port1,
556 u16 *status, u16 *change)
557{
558 int ret;
559
560 mutex_lock(&hub->status_mutex);
561 ret = get_port_status(hub->hdev, port1, &hub->status->port);
562 if (ret < 4) {
Alan Sterne9e88fb2013-03-27 16:14:01 -0400563 if (ret != -ENODEV)
564 dev_err(hub->intfdev,
565 "%s failed (err = %d)\n", __func__, ret);
Alan Stern3eb14912008-03-03 15:15:43 -0500566 if (ret >= 0)
567 ret = -EIO;
568 } else {
569 *status = le16_to_cpu(hub->status->port.wPortStatus);
570 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700571
Alan Stern3eb14912008-03-03 15:15:43 -0500572 ret = 0;
573 }
574 mutex_unlock(&hub->status_mutex);
575 return ret;
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578static void kick_khubd(struct usb_hub *hub)
579{
580 unsigned long flags;
581
582 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200583 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500585
586 /* Suppress autosuspend until khubd runs */
587 usb_autopm_get_interface_no_resume(
588 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 wake_up(&khubd_wait);
590 }
591 spin_unlock_irqrestore(&hub_event_lock, flags);
592}
593
594void usb_kick_khubd(struct usb_device *hdev)
595{
Lan Tianyuad493e52013-01-23 04:26:30 +0800596 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern251180842009-07-22 14:41:18 -0400597
598 if (hub)
599 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800602/*
603 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
604 * Notification, which indicates it had initiated remote wakeup.
605 *
606 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
607 * device initiates resume, so the USB core will not receive notice of the
608 * resume through the normal hub interrupt URB.
609 */
610void usb_wakeup_notification(struct usb_device *hdev,
611 unsigned int portnum)
612{
613 struct usb_hub *hub;
614
615 if (!hdev)
616 return;
617
Lan Tianyuad493e52013-01-23 04:26:30 +0800618 hub = usb_hub_to_struct_hub(hdev);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800619 if (hub) {
620 set_bit(portnum, hub->wakeup_bits);
621 kick_khubd(hub);
622 }
623}
624EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100627static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200629 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400630 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100631 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 unsigned long bits;
633
Alan Sterne0152682007-08-24 15:42:52 -0400634 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 case -ENOENT: /* synchronous unlink */
636 case -ECONNRESET: /* async unlink */
637 case -ESHUTDOWN: /* hardware going away */
638 return;
639
640 default: /* presumably an error */
641 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400642 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if ((++hub->nerrors < 10) || hub->error)
644 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400645 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /* let khubd handle things */
649 case 0: /* we got data: port status changed */
650 bits = 0;
651 for (i = 0; i < urb->actual_length; ++i)
652 bits |= ((unsigned long) ((*hub->buffer)[i]))
653 << (i*8);
654 hub->event_bits[0] = bits;
655 break;
656 }
657
658 hub->nerrors = 0;
659
660 /* Something happened, let khubd figure it out */
661 kick_khubd(hub);
662
663resubmit:
664 if (hub->quiescing)
665 return;
666
667 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
668 && status != -ENODEV && status != -EPERM)
669 dev_err (hub->intfdev, "resubmit --> %d\n", status);
670}
671
672/* USB 2.0 spec Section 11.24.2.3 */
673static inline int
674hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
675{
William Gulland889ba7b2013-06-27 16:10:20 -0700676 /* Need to clear both directions for control ep */
677 if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
678 USB_ENDPOINT_XFER_CONTROL) {
679 int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
680 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
681 devinfo ^ 0x8000, tt, NULL, 0, 1000);
682 if (status)
683 return status;
684 }
Alan Sternc2f65952009-11-18 11:37:15 -0500685 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
687 tt, NULL, 0, 1000);
688}
689
690/*
691 * enumeration blocks khubd for a long time. we use keventd instead, since
692 * long blocking there is the exception, not the rule. accordingly, HCDs
693 * talking to TTs must queue control transfers (not just bulk and iso), so
694 * both can talk to the same hub concurrently.
695 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400696static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
David Howellsc4028952006-11-22 14:57:56 +0000698 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400699 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unsigned long flags;
701
702 spin_lock_irqsave (&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300703 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400704 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 struct usb_tt_clear *clear;
706 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400707 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 int status;
709
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400710 next = hub->tt.clear_list.next;
711 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 list_del (&clear->clear_list);
713
714 /* drop lock so HCD can concurrently report other TT errors */
715 spin_unlock_irqrestore (&hub->tt.lock, flags);
716 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400717 if (status && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 dev_err (&hdev->dev,
719 "clear tt %d (%04x) error %d\n",
720 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400721
722 /* Tell the HCD, even if the operation failed */
723 drv = clear->hcd->driver;
724 if (drv->clear_tt_buffer_complete)
725 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
726
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700727 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400728 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730 spin_unlock_irqrestore (&hub->tt.lock, flags);
731}
732
733/**
Lan Tianyu971fcd42013-01-23 04:26:29 +0800734 * usb_hub_set_port_power - control hub port's power state
735 * @hdev: target hub
736 * @port1: port index
737 * @set: expected status
738 *
739 * call this function to control port's power via setting or
740 * clearing the port's PORT_POWER feature.
741 */
742int usb_hub_set_port_power(struct usb_device *hdev, int port1,
743 bool set)
744{
745 int ret;
Lan Tianyuad493e52013-01-23 04:26:30 +0800746 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
747 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyu971fcd42013-01-23 04:26:29 +0800748
749 if (set)
750 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
751 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800752 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
753
754 if (!ret)
755 port_dev->power_is_on = set;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800756 return ret;
757}
758
759/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400760 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
761 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
763 * High speed HCDs use this to tell the hub driver that some split control or
764 * bulk transaction failed in a way that requires clearing internal state of
765 * a transaction translator. This is normally detected (and reported) from
766 * interrupt context.
767 *
768 * It may not be possible for that hub to handle additional full (or low)
769 * speed transactions until that state is fully cleared out.
770 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400771int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400773 struct usb_device *udev = urb->dev;
774 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct usb_tt *tt = udev->tt;
776 unsigned long flags;
777 struct usb_tt_clear *clear;
778
779 /* we've got to cope with an arbitrary number of pending TT clears,
780 * since each TT has "at least two" buffers that can need it (and
781 * there can be many TTs per hub). even if they're uncommon.
782 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800783 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
785 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400786 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788
789 /* info that CLEAR_TT_BUFFER needs */
790 clear->tt = tt->multi ? udev->ttport : 1;
791 clear->devinfo = usb_pipeendpoint (pipe);
792 clear->devinfo |= udev->devnum << 4;
793 clear->devinfo |= usb_pipecontrol (pipe)
794 ? (USB_ENDPOINT_XFER_CONTROL << 11)
795 : (USB_ENDPOINT_XFER_BULK << 11);
796 if (usb_pipein (pipe))
797 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400798
799 /* info for completion callback */
800 clear->hcd = bus_to_hcd(udev->bus);
801 clear->ep = urb->ep;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 /* tell keventd to clear state for this TT */
804 spin_lock_irqsave (&tt->lock, flags);
805 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400806 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400810EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Alan Stern8520f382008-09-22 14:44:26 -0400812/* If do_delay is false, return the number of milliseconds the caller
813 * needs to delay.
814 */
815static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700818 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400819 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400820 u16 wHubCharacteristics =
821 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Alan Stern4489a572006-04-27 15:54:22 -0400823 /* Enable power on each port. Some hubs have reserved values
824 * of LPSM (> 2) in their descriptors, even though they are
825 * USB 2.0 hubs. Some hubs do not implement port-power switching
826 * but only emulate it. In all cases, the ports won't work
827 * unless we send these messages to the hub.
828 */
829 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400831 else
832 dev_dbg(hub->intfdev, "trying to enable port power on "
833 "non-switchable hub\n");
834 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
Lan Tianyuad493e52013-01-23 04:26:30 +0800835 if (hub->ports[port1 - 1]->power_is_on)
836 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
837 else
838 usb_clear_port_feature(hub->hdev, port1,
839 USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
David Brownellb7896962005-08-31 10:41:44 -0700841 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400842 delay = max(pgood_delay, (unsigned) 100);
843 if (do_delay)
844 msleep(delay);
845 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848static int hub_hub_status(struct usb_hub *hub,
849 u16 *status, u16 *change)
850{
851 int ret;
852
Alan Sterndb90e7a2007-02-05 09:56:15 -0500853 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ret = get_hub_status(hub->hdev, &hub->status->hub);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400855 if (ret < 0) {
856 if (ret != -ENODEV)
857 dev_err(hub->intfdev,
858 "%s failed (err = %d)\n", __func__, ret);
859 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 *status = le16_to_cpu(hub->status->hub.wHubStatus);
861 *change = le16_to_cpu(hub->status->hub.wHubChange);
862 ret = 0;
863 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500864 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return ret;
866}
867
Sarah Sharp41e7e052012-11-14 16:42:32 -0800868static int hub_set_port_link_state(struct usb_hub *hub, int port1,
869 unsigned int link_status)
870{
871 return set_port_feature(hub->hdev,
872 port1 | (link_status << 3),
873 USB_PORT_FEAT_LINK_STATE);
874}
875
876/*
877 * If USB 3.0 ports are placed into the Disabled state, they will no longer
878 * detect any device connects or disconnects. This is generally not what the
879 * USB core wants, since it expects a disabled port to produce a port status
880 * change event when a new device connects.
881 *
882 * Instead, set the link state to Disabled, wait for the link to settle into
883 * that state, clear any change bits, and then put the port into the RxDetect
884 * state.
885 */
886static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
887{
888 int ret;
889 int total_time;
890 u16 portchange, portstatus;
891
892 if (!hub_is_superspeed(hub->hdev))
893 return -EINVAL;
894
895 ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400896 if (ret)
Sarah Sharp41e7e052012-11-14 16:42:32 -0800897 return ret;
Sarah Sharp41e7e052012-11-14 16:42:32 -0800898
899 /* Wait for the link to enter the disabled state. */
900 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
901 ret = hub_port_status(hub, port1, &portstatus, &portchange);
902 if (ret < 0)
903 return ret;
904
905 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
906 USB_SS_PORT_LS_SS_DISABLED)
907 break;
908 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
909 break;
910 msleep(HUB_DEBOUNCE_STEP);
911 }
912 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
913 dev_warn(hub->intfdev, "Could not disable port %d after %d ms\n",
914 port1, total_time);
915
916 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
917}
918
Alan Stern8b28c752005-08-10 17:04:13 -0400919static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
920{
921 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400922 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400923
Lan Tianyuff823c72012-09-05 13:44:32 +0800924 if (hub->ports[port1 - 1]->child && set_state)
925 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400926 USB_STATE_NOTATTACHED);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800927 if (!hub->error) {
928 if (hub_is_superspeed(hub->hdev))
929 ret = hub_usb3_port_disable(hub, port1);
930 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800931 ret = usb_clear_port_feature(hdev, port1,
Sarah Sharp41e7e052012-11-14 16:42:32 -0800932 USB_PORT_FEAT_ENABLE);
933 }
Alan Sterne9e88fb2013-03-27 16:14:01 -0400934 if (ret && ret != -ENODEV)
Alan Stern8b28c752005-08-10 17:04:13 -0400935 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400936 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400937 return ret;
938}
939
Alan Stern0458d5b2007-05-04 11:52:20 -0400940/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800941 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400942 * time later khubd will disconnect() any existing usb_device on the port
943 * and will re-enumerate if there actually is a device attached.
944 */
945static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
946{
947 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
948 hub_port_disable(hub, port1, 1);
949
950 /* FIXME let caller ask to power down the port:
951 * - some devices won't enumerate without a VBUS power cycle
952 * - SRP saves power that way
953 * - ... new call, TBD ...
954 * That's easy if this hub can switch power per-port, and
955 * khubd reactivates the port later (timer, SRP, etc).
956 * Powerdown must be optional, because of reset/DFU.
957 */
958
959 set_bit(port1, hub->change_bits);
960 kick_khubd(hub);
961}
962
Alan Stern253e0572009-10-27 15:20:13 -0400963/**
964 * usb_remove_device - disable a device's port on its parent hub
965 * @udev: device to be disabled and removed
966 * Context: @udev locked, must be able to sleep.
967 *
968 * After @udev's port has been disabled, khubd is notified and it will
969 * see that the device has been disconnected. When the device is
970 * physically unplugged and something is plugged in, the events will
971 * be received and processed normally.
972 */
973int usb_remove_device(struct usb_device *udev)
974{
975 struct usb_hub *hub;
976 struct usb_interface *intf;
977
978 if (!udev->parent) /* Can't remove a root hub */
979 return -EINVAL;
Lan Tianyuad493e52013-01-23 04:26:30 +0800980 hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern253e0572009-10-27 15:20:13 -0400981 intf = to_usb_interface(hub->intfdev);
982
983 usb_autopm_get_interface(intf);
984 set_bit(udev->portnum, hub->removed_bits);
985 hub_port_logical_disconnect(hub, udev->portnum);
986 usb_autopm_put_interface(intf);
987 return 0;
988}
989
Alan Stern6ee0b272008-04-28 11:06:42 -0400990enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500991 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400992 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400993};
Alan Stern5e6effa2008-03-03 15:15:51 -0500994
Alan Stern8520f382008-09-22 14:44:26 -0400995static void hub_init_func2(struct work_struct *ws);
996static void hub_init_func3(struct work_struct *ws);
997
Alan Sternf2835212008-04-28 11:07:17 -0400998static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500999{
1000 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -08001001 struct usb_hcd *hcd;
1002 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -05001003 int port1;
Alan Sternf2835212008-04-28 11:07:17 -04001004 int status;
Alan Stern948fea32008-04-28 11:07:07 -04001005 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -04001006 unsigned delay;
1007
1008 /* Continue a partial initialization */
1009 if (type == HUB_INIT2)
1010 goto init2;
1011 if (type == HUB_INIT3)
1012 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -05001013
Elric Fua45aa3b2012-02-18 13:32:27 +08001014 /* The superspeed hub except for root hub has to use Hub Depth
1015 * value as an offset into the route string to locate the bits
1016 * it uses to determine the downstream port number. So hub driver
1017 * should send a set hub depth request to superspeed hub after
1018 * the superspeed hub is set configuration in initialization or
1019 * reset procedure.
1020 *
1021 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -04001022 * For any other type of activation, turn it on.
1023 */
Alan Stern8520f382008-09-22 14:44:26 -04001024 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +08001025 if (hdev->parent && hub_is_superspeed(hdev)) {
1026 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1027 HUB_SET_DEPTH, USB_RT_HUB,
1028 hdev->level - 1, 0, NULL, 0,
1029 USB_CTRL_SET_TIMEOUT);
1030 if (ret < 0)
1031 dev_err(hub->intfdev,
1032 "set hub depth failed\n");
1033 }
Alan Stern8520f382008-09-22 14:44:26 -04001034
1035 /* Speed up system boot by using a delayed_work for the
1036 * hub's initial power-up delays. This is pretty awkward
1037 * and the implementation looks like a home-brewed sort of
1038 * setjmp/longjmp, but it saves at least 100 ms for each
1039 * root hub (assuming usbcore is compiled into the kernel
1040 * rather than as a module). It adds up.
1041 *
1042 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1043 * because for those activation types the ports have to be
1044 * operational when we return. In theory this could be done
1045 * for HUB_POST_RESET, but it's easier not to.
1046 */
1047 if (type == HUB_INIT) {
1048 delay = hub_power_on(hub, false);
1049 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1050 schedule_delayed_work(&hub->init_work,
1051 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001052
1053 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001054 usb_autopm_get_interface_no_resume(
1055 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001056 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001057 } else if (type == HUB_RESET_RESUME) {
1058 /* The internal host controller state for the hub device
1059 * may be gone after a host power loss on system resume.
1060 * Update the device's info so the HW knows it's a hub.
1061 */
1062 hcd = bus_to_hcd(hdev->bus);
1063 if (hcd->driver->update_hub_device) {
1064 ret = hcd->driver->update_hub_device(hcd, hdev,
1065 &hub->tt, GFP_NOIO);
1066 if (ret < 0) {
1067 dev_err(hub->intfdev, "Host not "
1068 "accepting hub info "
1069 "update.\n");
1070 dev_err(hub->intfdev, "LS/FS devices "
1071 "and hubs may not work "
1072 "under this hub\n.");
1073 }
1074 }
1075 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001076 } else {
1077 hub_power_on(hub, true);
1078 }
1079 }
1080 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001081
Alan Stern6ee0b272008-04-28 11:06:42 -04001082 /* Check each port and set hub->change_bits to let khubd know
1083 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001084 */
1085 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001086 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001087 u16 portstatus, portchange;
1088
Alan Stern6ee0b272008-04-28 11:06:42 -04001089 portstatus = portchange = 0;
1090 status = hub_port_status(hub, port1, &portstatus, &portchange);
1091 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1092 dev_dbg(hub->intfdev,
1093 "port %d: status %04x change %04x\n",
1094 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001095
Alan Stern6ee0b272008-04-28 11:06:42 -04001096 /* After anything other than HUB_RESUME (i.e., initialization
1097 * or any sort of reset), every port should be disabled.
1098 * Unconnected ports should likewise be disabled (paranoia),
1099 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001100 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001101 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1102 type != HUB_RESUME ||
1103 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1104 !udev ||
1105 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001106 /*
1107 * USB3 protocol ports will automatically transition
1108 * to Enabled state when detect an USB3.0 device attach.
1109 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001110 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001111 if (!hub_is_superspeed(hdev)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08001112 usb_clear_port_feature(hdev, port1,
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001113 USB_PORT_FEAT_ENABLE);
1114 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001115 } else {
1116 /* Pretend that power was lost for USB3 devs */
1117 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001118 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001119 }
1120
Alan Stern948fea32008-04-28 11:07:07 -04001121 /* Clear status-change flags; we'll debounce later */
1122 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1123 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001124 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001125 USB_PORT_FEAT_C_CONNECTION);
1126 }
1127 if (portchange & USB_PORT_STAT_C_ENABLE) {
1128 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001129 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001130 USB_PORT_FEAT_C_ENABLE);
1131 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001132 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1133 hub_is_superspeed(hub->hdev)) {
1134 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001135 usb_clear_port_feature(hub->hdev, port1,
Don Zickus79c3dd82011-11-03 09:07:18 -04001136 USB_PORT_FEAT_C_BH_PORT_RESET);
1137 }
Alan Stern253e0572009-10-27 15:20:13 -04001138 /* We can forget about a "removed" device when there's a
1139 * physical disconnect or the connect status changes.
1140 */
1141 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1142 (portchange & USB_PORT_STAT_C_CONNECTION))
1143 clear_bit(port1, hub->removed_bits);
1144
Alan Stern6ee0b272008-04-28 11:06:42 -04001145 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1146 /* Tell khubd to disconnect the device or
1147 * check for a new connection
1148 */
1149 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1150 set_bit(port1, hub->change_bits);
1151
1152 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001153 bool port_resumed = (portstatus &
1154 USB_PORT_STAT_LINK_STATE) ==
1155 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001156 /* The power session apparently survived the resume.
1157 * If there was an overcurrent or suspend change
1158 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001159 * take care of it. Look at the port link state
1160 * for USB 3.0 hubs, since they don't have a suspend
1161 * change bit, and they don't set the port link change
1162 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001163 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001164 if (portchange || (hub_is_superspeed(hub->hdev) &&
1165 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001166 set_bit(port1, hub->change_bits);
1167
1168 } else if (udev->persist_enabled) {
Lan Tianyuad493e52013-01-23 04:26:30 +08001169 struct usb_port *port_dev = hub->ports[port1 - 1];
1170
Alan Stern6ee0b272008-04-28 11:06:42 -04001171#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001172 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001173#endif
Lan Tianyuad493e52013-01-23 04:26:30 +08001174 /* Don't set the change_bits when the device
1175 * was powered off.
1176 */
1177 if (port_dev->power_is_on)
1178 set_bit(port1, hub->change_bits);
Alan Stern8808f002008-04-28 11:06:55 -04001179
Alan Stern6ee0b272008-04-28 11:06:42 -04001180 } else {
1181 /* The power session is gone; tell khubd */
1182 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1183 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001184 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001185 }
1186
Alan Stern948fea32008-04-28 11:07:07 -04001187 /* If no port-status-change flags were set, we don't need any
1188 * debouncing. If flags were set we can try to debounce the
1189 * ports all at once right now, instead of letting khubd do them
1190 * one at a time later on.
1191 *
1192 * If any port-status changes do occur during this delay, khubd
1193 * will see them later and handle them normally.
1194 */
Alan Stern8520f382008-09-22 14:44:26 -04001195 if (need_debounce_delay) {
1196 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001197
Alan Stern8520f382008-09-22 14:44:26 -04001198 /* Don't do a long sleep inside a workqueue routine */
1199 if (type == HUB_INIT2) {
1200 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1201 schedule_delayed_work(&hub->init_work,
1202 msecs_to_jiffies(delay));
1203 return; /* Continues at init3: below */
1204 } else {
1205 msleep(delay);
1206 }
1207 }
1208 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001209 hub->quiescing = 0;
1210
1211 status = usb_submit_urb(hub->urb, GFP_NOIO);
1212 if (status < 0)
1213 dev_err(hub->intfdev, "activate --> %d\n", status);
1214 if (hub->has_indicators && blinkenlights)
1215 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1216
1217 /* Scan all ports that need attention */
1218 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001219
1220 /* Allow autosuspend if it was suppressed */
1221 if (type <= HUB_INIT3)
1222 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001223}
1224
Alan Stern8520f382008-09-22 14:44:26 -04001225/* Implement the continuations for the delays above */
1226static void hub_init_func2(struct work_struct *ws)
1227{
1228 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1229
1230 hub_activate(hub, HUB_INIT2);
1231}
1232
1233static void hub_init_func3(struct work_struct *ws)
1234{
1235 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1236
1237 hub_activate(hub, HUB_INIT3);
1238}
1239
Alan Stern43303542008-04-28 11:07:31 -04001240enum hub_quiescing_type {
1241 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1242};
1243
1244static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1245{
1246 struct usb_device *hdev = hub->hdev;
1247 int i;
1248
Alan Stern8520f382008-09-22 14:44:26 -04001249 cancel_delayed_work_sync(&hub->init_work);
1250
Alan Stern43303542008-04-28 11:07:31 -04001251 /* khubd and related activity won't re-trigger */
1252 hub->quiescing = 1;
1253
1254 if (type != HUB_SUSPEND) {
1255 /* Disconnect all the children */
1256 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001257 if (hub->ports[i]->child)
1258 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001259 }
1260 }
1261
1262 /* Stop khubd and related activity */
1263 usb_kill_urb(hub->urb);
1264 if (hub->has_indicators)
1265 cancel_delayed_work_sync(&hub->leds);
1266 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001267 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001268}
1269
Alan Stern3eb14912008-03-03 15:15:43 -05001270/* caller has locked the hub device */
1271static int hub_pre_reset(struct usb_interface *intf)
1272{
1273 struct usb_hub *hub = usb_get_intfdata(intf);
1274
Alan Stern43303542008-04-28 11:07:31 -04001275 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001276 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001277}
1278
1279/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001280static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001281{
Alan Stern7de18d82006-06-01 13:37:24 -04001282 struct usb_hub *hub = usb_get_intfdata(intf);
1283
Alan Sternf2835212008-04-28 11:07:17 -04001284 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001285 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288static int hub_configure(struct usb_hub *hub,
1289 struct usb_endpoint_descriptor *endpoint)
1290{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001291 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct usb_device *hdev = hub->hdev;
1293 struct device *hub_dev = hub->intfdev;
1294 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001295 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001297 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001298 char *message = "out of memory";
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001299 unsigned unit_load;
1300 unsigned full_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Alan Sternd697cdd2009-10-27 15:18:46 -04001302 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 ret = -ENOMEM;
1305 goto fail;
1306 }
1307
1308 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1309 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 ret = -ENOMEM;
1311 goto fail;
1312 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001313 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1316 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 ret = -ENOMEM;
1318 goto fail;
1319 }
1320
1321 /* Request the entire hub descriptor.
1322 * hub->descriptor can handle USB_MAXCHILDREN ports,
1323 * but the hub can/will return fewer bytes here.
1324 */
John Youndbe79bb2001-09-17 00:00:00 -07001325 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (ret < 0) {
1327 message = "can't read hub descriptor";
1328 goto fail;
1329 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1330 message = "hub has too many ports!";
1331 ret = -ENODEV;
1332 goto fail;
David Linares769d73682013-03-25 10:50:27 +00001333 } else if (hub->descriptor->bNbrPorts == 0) {
1334 message = "hub doesn't have any ports!";
1335 ret = -ENODEV;
1336 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338
1339 hdev->maxchild = hub->descriptor->bNbrPorts;
1340 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1341 (hdev->maxchild == 1) ? "" : "s");
1342
Lan Tianyufa2a9562012-09-05 13:44:31 +08001343 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1344 GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001345 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001346 ret = -ENOMEM;
1347 goto fail;
1348 }
1349
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001350 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001351 if (hub_is_superspeed(hdev)) {
1352 unit_load = 150;
1353 full_load = 900;
1354 } else {
1355 unit_load = 100;
1356 full_load = 500;
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
John Youndbe79bb2001-09-17 00:00:00 -07001359 /* FIXME for USB 3.0, skip for now */
1360 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1361 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 int i;
1363 char portstr [USB_MAXCHILDREN + 1];
1364
1365 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001366 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1368 ? 'F' : 'R';
1369 portstr[hdev->maxchild] = 0;
1370 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1371 } else
1372 dev_dbg(hub_dev, "standalone hub\n");
1373
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001374 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301375 case HUB_CHAR_COMMON_LPSM:
1376 dev_dbg(hub_dev, "ganged power switching\n");
1377 break;
1378 case HUB_CHAR_INDV_PORT_LPSM:
1379 dev_dbg(hub_dev, "individual port power switching\n");
1380 break;
1381 case HUB_CHAR_NO_LPSM:
1382 case HUB_CHAR_LPSM:
1383 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1384 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001387 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301388 case HUB_CHAR_COMMON_OCPM:
1389 dev_dbg(hub_dev, "global over-current protection\n");
1390 break;
1391 case HUB_CHAR_INDV_PORT_OCPM:
1392 dev_dbg(hub_dev, "individual port over-current protection\n");
1393 break;
1394 case HUB_CHAR_NO_OCPM:
1395 case HUB_CHAR_OCPM:
1396 dev_dbg(hub_dev, "no over-current protection\n");
1397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
1400 spin_lock_init (&hub->tt.lock);
1401 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001402 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301404 case USB_HUB_PR_FS:
1405 break;
1406 case USB_HUB_PR_HS_SINGLE_TT:
1407 dev_dbg(hub_dev, "Single TT\n");
1408 hub->tt.hub = hdev;
1409 break;
1410 case USB_HUB_PR_HS_MULTI_TT:
1411 ret = usb_set_interface(hdev, 0, 1);
1412 if (ret == 0) {
1413 dev_dbg(hub_dev, "TT per port\n");
1414 hub->tt.multi = 1;
1415 } else
1416 dev_err(hub_dev, "Using single TT (err %d)\n",
1417 ret);
1418 hub->tt.hub = hdev;
1419 break;
1420 case USB_HUB_PR_SS:
1421 /* USB 3.0 hubs don't have a TT */
1422 break;
1423 default:
1424 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1425 hdev->descriptor.bDeviceProtocol);
1426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001429 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001430 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001431 case HUB_TTTT_8_BITS:
1432 if (hdev->descriptor.bDeviceProtocol != 0) {
1433 hub->tt.think_time = 666;
1434 dev_dbg(hub_dev, "TT requires at most %d "
1435 "FS bit times (%d ns)\n",
1436 8, hub->tt.think_time);
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001439 case HUB_TTTT_16_BITS:
1440 hub->tt.think_time = 666 * 2;
1441 dev_dbg(hub_dev, "TT requires at most %d "
1442 "FS bit times (%d ns)\n",
1443 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001445 case HUB_TTTT_24_BITS:
1446 hub->tt.think_time = 666 * 3;
1447 dev_dbg(hub_dev, "TT requires at most %d "
1448 "FS bit times (%d ns)\n",
1449 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001451 case HUB_TTTT_32_BITS:
1452 hub->tt.think_time = 666 * 4;
1453 dev_dbg(hub_dev, "TT requires at most %d "
1454 "FS bit times (%d ns)\n",
1455 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
1457 }
1458
1459 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001460 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 hub->has_indicators = 1;
1462 dev_dbg(hub_dev, "Port indicators are supported\n");
1463 }
1464
1465 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1466 hub->descriptor->bPwrOn2PwrGood * 2);
1467
1468 /* power budgeting mostly matters with bus-powered hubs,
1469 * and battery-powered root hubs (may provide just 8 mA).
1470 */
1471 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001472 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 message = "can't get hub status";
1474 goto fail;
1475 }
Alan Stern7d35b922005-04-25 11:18:32 -04001476 le16_to_cpus(&hubstatus);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001477 hcd = bus_to_hcd(hdev->bus);
Alan Stern7d35b922005-04-25 11:18:32 -04001478 if (hdev == hdev->bus->root_hub) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001479 if (hcd->power_budget > 0)
1480 hdev->bus_mA = hcd->power_budget;
1481 else
1482 hdev->bus_mA = full_load * hdev->maxchild;
1483 if (hdev->bus_mA >= full_load)
1484 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001485 else {
1486 hub->mA_per_port = hdev->bus_mA;
1487 hub->limited_power = 1;
1488 }
Alan Stern7d35b922005-04-25 11:18:32 -04001489 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001490 int remaining = hdev->bus_mA -
1491 hub->descriptor->bHubContrCurrent;
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1494 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001495 hub->limited_power = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001497 if (remaining < hdev->maxchild * unit_load)
1498 dev_warn(hub_dev,
Alan Stern55c52712005-11-23 12:03:12 -05001499 "insufficient power available "
1500 "to use all downstream ports\n");
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001501 hub->mA_per_port = unit_load; /* 7.2.1 */
1502
Alan Stern55c52712005-11-23 12:03:12 -05001503 } else { /* Self-powered external hub */
1504 /* FIXME: What about battery-powered external hubs that
1505 * provide less current per port? */
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001506 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001507 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001508 if (hub->mA_per_port < full_load)
Alan Stern55c52712005-11-23 12:03:12 -05001509 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1510 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001512 /* Update the HCD's internal representation of this hub before khubd
1513 * starts getting port status changes for devices under the hub.
1514 */
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001515 if (hcd->driver->update_hub_device) {
1516 ret = hcd->driver->update_hub_device(hcd, hdev,
1517 &hub->tt, GFP_KERNEL);
1518 if (ret < 0) {
1519 message = "can't update HCD hub info";
1520 goto fail;
1521 }
1522 }
1523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1525 if (ret < 0) {
1526 message = "can't get hub status";
1527 goto fail;
1528 }
1529
1530 /* local power status reports aren't always correct */
1531 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1532 dev_dbg(hub_dev, "local power source is %s\n",
1533 (hubstatus & HUB_STATUS_LOCAL_POWER)
1534 ? "lost (inactive)" : "good");
1535
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001536 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 dev_dbg(hub_dev, "%sover-current condition exists\n",
1538 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1539
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001540 /* set up the interrupt endpoint
1541 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1542 * bytes as USB2.0[11.12.3] says because some hubs are known
1543 * to send more data (and thus cause overflow). For root hubs,
1544 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1545 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1547 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1548
1549 if (maxp > sizeof(*hub->buffer))
1550 maxp = sizeof(*hub->buffer);
1551
1552 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1553 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 ret = -ENOMEM;
1555 goto fail;
1556 }
1557
1558 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1559 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /* maybe cycle the hub leds */
1562 if (hub->has_indicators && blinkenlights)
1563 hub->indicator [0] = INDICATOR_CYCLE;
1564
Krzysztof Mazur386f0002013-08-22 14:49:39 +02001565 for (i = 0; i < hdev->maxchild; i++) {
1566 ret = usb_hub_create_port_device(hub, i + 1);
1567 if (ret < 0) {
Lan Tianyufa2a9562012-09-05 13:44:31 +08001568 dev_err(hub->intfdev,
1569 "couldn't create port%d device.\n", i + 1);
Krzysztof Mazur386f0002013-08-22 14:49:39 +02001570 hdev->maxchild = i;
1571 goto fail_keep_maxchild;
1572 }
1573 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001574
Lan Tianyud2123fd2013-01-21 22:18:00 +08001575 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1576
Alan Sternf2835212008-04-28 11:07:17 -04001577 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579
1580fail:
Krzysztof Mazur73426892013-08-22 14:49:38 +02001581 hdev->maxchild = 0;
Krzysztof Mazur386f0002013-08-22 14:49:39 +02001582fail_keep_maxchild:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 dev_err (hub_dev, "config failed, %s (err %d)\n",
1584 message, ret);
1585 /* hub_disconnect() frees urb and descriptor */
1586 return ret;
1587}
1588
Alan Sterne8054852007-05-04 11:55:11 -04001589static void hub_release(struct kref *kref)
1590{
1591 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1592
1593 usb_put_intf(to_usb_interface(hub->intfdev));
1594 kfree(hub);
1595}
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597static unsigned highspeed_hubs;
1598
1599static void hub_disconnect(struct usb_interface *intf)
1600{
Huajun Li88162302012-03-12 21:00:19 +08001601 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001602 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001603 int i;
1604
Alan Sterne8054852007-05-04 11:55:11 -04001605 /* Take the hub off the event list and don't let it be added again */
1606 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001607 if (!list_empty(&hub->event_list)) {
1608 list_del_init(&hub->event_list);
1609 usb_autopm_put_interface_no_suspend(intf);
1610 }
Alan Sterne8054852007-05-04 11:55:11 -04001611 hub->disconnected = 1;
1612 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Alan Stern7de18d82006-06-01 13:37:24 -04001614 /* Disconnect all children and quiesce the hub */
1615 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001616 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001617
Alan Stern8b28c752005-08-10 17:04:13 -04001618 usb_set_intfdata (intf, NULL);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001619
1620 for (i = 0; i < hdev->maxchild; i++)
1621 usb_hub_remove_port_device(hub, i + 1);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001622 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Alan Sterne8054852007-05-04 11:55:11 -04001624 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 highspeed_hubs--;
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001628 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001629 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001630 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001631 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Lan Tianyu971fcd42013-01-23 04:26:29 +08001633 pm_suspend_ignore_children(&intf->dev, false);
Alan Sterne8054852007-05-04 11:55:11 -04001634 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1638{
1639 struct usb_host_interface *desc;
1640 struct usb_endpoint_descriptor *endpoint;
1641 struct usb_device *hdev;
1642 struct usb_hub *hub;
1643
1644 desc = intf->cur_altsetting;
1645 hdev = interface_to_usbdev(intf);
1646
Ming Lei596d7892012-10-24 11:59:25 +08001647 /*
1648 * Set default autosuspend delay as 0 to speedup bus suspend,
1649 * based on the below considerations:
1650 *
1651 * - Unlike other drivers, the hub driver does not rely on the
1652 * autosuspend delay to provide enough time to handle a wakeup
1653 * event, and the submitted status URB is just to check future
1654 * change on hub downstream ports, so it is safe to do it.
1655 *
1656 * - The patch might cause one or more auto supend/resume for
1657 * below very rare devices when they are plugged into hub
1658 * first time:
1659 *
1660 * devices having trouble initializing, and disconnect
1661 * themselves from the bus and then reconnect a second
1662 * or so later
1663 *
1664 * devices just for downloading firmware, and disconnects
1665 * themselves after completing it
1666 *
1667 * For these quite rare devices, their drivers may change the
1668 * autosuspend delay of their parent hub in the probe() to one
1669 * appropriate value to avoid the subtle problem if someone
1670 * does care it.
1671 *
1672 * - The patch may cause one or more auto suspend/resume on
1673 * hub during running 'lsusb', but it is probably too
1674 * infrequent to worry about.
1675 *
1676 * - Change autosuspend delay of hub can avoid unnecessary auto
1677 * suspend timer for hub, also may decrease power consumption
1678 * of USB bus.
1679 */
1680 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1681
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001682 /* Hubs have proper suspend/resume support. */
1683 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001684
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001685 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001686 dev_err(&intf->dev,
1687 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001688 return -E2BIG;
1689 }
1690
David Brownell89ccbdc2006-04-02 10:18:09 -08001691#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1692 if (hdev->parent) {
1693 dev_warn(&intf->dev, "ignoring external hub\n");
1694 return -ENODEV;
1695 }
1696#endif
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 /* Some hubs have a subclass of 1, which AFAICT according to the */
1699 /* specs is not defined, but it works */
1700 if ((desc->desc.bInterfaceSubClass != 0) &&
1701 (desc->desc.bInterfaceSubClass != 1)) {
1702descriptor_error:
1703 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1704 return -EIO;
1705 }
1706
1707 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1708 if (desc->desc.bNumEndpoints != 1)
1709 goto descriptor_error;
1710
1711 endpoint = &desc->endpoint[0].desc;
1712
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001713 /* If it's not an interrupt in endpoint, we'd better punt! */
1714 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 goto descriptor_error;
1716
1717 /* We found a hub */
1718 dev_info (&intf->dev, "USB hub found\n");
1719
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001720 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (!hub) {
1722 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1723 return -ENOMEM;
1724 }
1725
Alan Sterne8054852007-05-04 11:55:11 -04001726 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 INIT_LIST_HEAD(&hub->event_list);
1728 hub->intfdev = &intf->dev;
1729 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001730 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001731 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001732 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001735 intf->needs_remote_wakeup = 1;
Lan Tianyu971fcd42013-01-23 04:26:29 +08001736 pm_suspend_ignore_children(&intf->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 if (hdev->speed == USB_SPEED_HIGH)
1739 highspeed_hubs++;
1740
Ming Leie6f30de2012-10-24 11:59:24 +08001741 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1742 hub->quirk_check_port_auto_suspend = 1;
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (hub_configure(hub, endpoint) >= 0)
1745 return 0;
1746
1747 hub_disconnect (intf);
1748 return -ENODEV;
1749}
1750
1751static int
1752hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1753{
1754 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuad493e52013-01-23 04:26:30 +08001755 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /* assert ifno == 0 (part of hub spec) */
1758 switch (code) {
1759 case USBDEVFS_HUB_PORTINFO: {
1760 struct usbdevfs_hub_portinfo *info = user_data;
1761 int i;
1762
1763 spin_lock_irq(&device_state_lock);
1764 if (hdev->devnum <= 0)
1765 info->nports = 0;
1766 else {
1767 info->nports = hdev->maxchild;
1768 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001769 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 info->port[i] = 0;
1771 else
1772 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001773 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775 }
1776 spin_unlock_irq(&device_state_lock);
1777
1778 return info->nports + 1;
1779 }
1780
1781 default:
1782 return -ENOSYS;
1783 }
1784}
1785
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001786/*
1787 * Allow user programs to claim ports on a hub. When a device is attached
1788 * to one of these "claimed" ports, the program will "own" the device.
1789 */
1790static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001791 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001792{
1793 if (hdev->state == USB_STATE_NOTATTACHED)
1794 return -ENODEV;
1795 if (port1 == 0 || port1 > hdev->maxchild)
1796 return -EINVAL;
1797
1798 /* This assumes that devices not managed by the hub driver
1799 * will always have maxchild equal to 0.
1800 */
Lan Tianyuad493e52013-01-23 04:26:30 +08001801 *ppowner = &(usb_hub_to_struct_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001802 return 0;
1803}
1804
1805/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001806int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1807 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001808{
1809 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001810 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001811
1812 rc = find_port_owner(hdev, port1, &powner);
1813 if (rc)
1814 return rc;
1815 if (*powner)
1816 return -EBUSY;
1817 *powner = owner;
1818 return rc;
1819}
1820
Lan Tianyu336c5c32012-07-06 14:13:52 +08001821int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1822 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001823{
1824 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001825 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001826
1827 rc = find_port_owner(hdev, port1, &powner);
1828 if (rc)
1829 return rc;
1830 if (*powner != owner)
1831 return -ENOENT;
1832 *powner = NULL;
1833 return rc;
1834}
1835
Lan Tianyu336c5c32012-07-06 14:13:52 +08001836void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001837{
Lan Tianyuad493e52013-01-23 04:26:30 +08001838 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001839 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001840
Lan Tianyufa2a9562012-09-05 13:44:31 +08001841 for (n = 0; n < hdev->maxchild; n++) {
1842 if (hub->ports[n]->port_owner == owner)
1843 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001844 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001845
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001846}
1847
1848/* The caller must hold udev's lock */
1849bool usb_device_is_owned(struct usb_device *udev)
1850{
1851 struct usb_hub *hub;
1852
1853 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1854 return false;
Lan Tianyuad493e52013-01-23 04:26:30 +08001855 hub = usb_hub_to_struct_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001856 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001857}
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1860{
Lan Tianyuad493e52013-01-23 04:26:30 +08001861 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 int i;
1863
1864 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001865 if (hub->ports[i]->child)
1866 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001868 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001869 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 udev->state = USB_STATE_NOTATTACHED;
1871}
1872
1873/**
1874 * usb_set_device_state - change a device's current state (usbcore, hcds)
1875 * @udev: pointer to device whose state should be changed
1876 * @new_state: new state value to be stored
1877 *
1878 * udev->state is _not_ fully protected by the device lock. Although
1879 * most transitions are made only while holding the lock, the state can
1880 * can change to USB_STATE_NOTATTACHED at almost any time. This
1881 * is so that devices can be marked as disconnected as soon as possible,
1882 * without having to wait for any semaphores to be released. As a result,
1883 * all changes to any device's state must be protected by the
1884 * device_state_lock spinlock.
1885 *
1886 * Once a device has been added to the device tree, all changes to its state
1887 * should be made using this routine. The state should _not_ be set directly.
1888 *
1889 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1890 * Otherwise udev->state is set to new_state, and if new_state is
1891 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1892 * to USB_STATE_NOTATTACHED.
1893 */
1894void usb_set_device_state(struct usb_device *udev,
1895 enum usb_device_state new_state)
1896{
1897 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001898 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 spin_lock_irqsave(&device_state_lock, flags);
1901 if (udev->state == USB_STATE_NOTATTACHED)
1902 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001903 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001904
1905 /* root hub wakeup capabilities are managed out-of-band
1906 * and may involve silicon errata ... ignore them here.
1907 */
1908 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001909 if (udev->state == USB_STATE_SUSPENDED
1910 || new_state == USB_STATE_SUSPENDED)
1911 ; /* No change to wakeup settings */
1912 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001913 wakeup = udev->actconfig->desc.bmAttributes
1914 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001915 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001916 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001917 }
Sarah Sharp15123002007-12-21 16:54:15 -08001918 if (udev->state == USB_STATE_SUSPENDED &&
1919 new_state != USB_STATE_SUSPENDED)
1920 udev->active_duration -= jiffies;
1921 else if (new_state == USB_STATE_SUSPENDED &&
1922 udev->state != USB_STATE_SUSPENDED)
1923 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001924 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001925 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 recursively_mark_NOTATTACHED(udev);
1927 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001928 if (wakeup >= 0)
1929 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
David Vrabel6da9c992009-02-18 14:43:47 +00001931EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001933/*
Alan Stern3b29b682011-02-22 09:53:41 -05001934 * Choose a device number.
1935 *
1936 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1937 * USB-2.0 buses they are also used as device addresses, however on
1938 * USB-3.0 buses the address is assigned by the controller hardware
1939 * and it usually is not the same as the device number.
1940 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001941 * WUSB devices are simple: they have no hubs behind, so the mapping
1942 * device <-> virtual port number becomes 1:1. Why? to simplify the
1943 * life of the device connection logic in
1944 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1945 * handshake we need to assign a temporary address in the unauthorized
1946 * space. For simplicity we use the first virtual port number found to
1947 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1948 * and that becomes it's address [X < 128] or its unauthorized address
1949 * [X | 0x80].
1950 *
1951 * We add 1 as an offset to the one-based USB-stack port number
1952 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1953 * 0 is reserved by USB for default address; (b) Linux's USB stack
1954 * uses always #1 for the root hub of the controller. So USB stack's
1955 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001956 *
1957 * Devices connected under xHCI are not as simple. The host controller
1958 * supports virtualization, so the hardware assigns device addresses and
1959 * the HCD must setup data structures before issuing a set address
1960 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001961 */
Alan Stern3b29b682011-02-22 09:53:41 -05001962static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 int devnum;
1965 struct usb_bus *bus = udev->bus;
1966
1967 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001968 if (udev->wusb) {
1969 devnum = udev->portnum + 1;
1970 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1971 } else {
1972 /* Try to allocate the next devnum beginning at
1973 * bus->devnum_next. */
1974 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1975 bus->devnum_next);
1976 if (devnum >= 128)
1977 devnum = find_next_zero_bit(bus->devmap.devicemap,
1978 128, 1);
1979 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (devnum < 128) {
1982 set_bit(devnum, bus->devmap.devicemap);
1983 udev->devnum = devnum;
1984 }
1985}
1986
Alan Stern3b29b682011-02-22 09:53:41 -05001987static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 if (udev->devnum > 0) {
1990 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1991 udev->devnum = -1;
1992 }
1993}
1994
Alan Stern3b29b682011-02-22 09:53:41 -05001995static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001996{
1997 /* The address for a WUSB device is managed by wusbcore. */
1998 if (!udev->wusb)
1999 udev->devnum = devnum;
2000}
2001
Herbert Xuf7410ce2010-01-10 20:15:03 +11002002static void hub_free_dev(struct usb_device *udev)
2003{
2004 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2005
2006 /* Root hubs aren't real devices, so don't free HCD resources */
2007 if (hcd->driver->free_dev && udev->parent)
2008 hcd->driver->free_dev(hcd, udev);
2009}
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011/**
2012 * usb_disconnect - disconnect a device (usbcore-internal)
2013 * @pdev: pointer to device being disconnected
2014 * Context: !in_interrupt ()
2015 *
2016 * Something got disconnected. Get rid of it and all of its children.
2017 *
2018 * If *pdev is a normal device then the parent hub must already be locked.
2019 * If *pdev is a root hub then this routine will acquire the
2020 * usb_bus_list_lock on behalf of the caller.
2021 *
2022 * Only hub drivers (including virtual root hub drivers for host
2023 * controllers) should ever call this.
2024 *
2025 * This call is synchronous, and may not be used in an interrupt context.
2026 */
2027void usb_disconnect(struct usb_device **pdev)
2028{
2029 struct usb_device *udev = *pdev;
Lan Tianyuad493e52013-01-23 04:26:30 +08002030 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int i;
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 /* mark the device as inactive, so any further urb submissions for
2034 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002035 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 */
2037 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002038 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2039 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002041 usb_lock_device(udev);
2042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08002044 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08002045 if (hub->ports[i]->child)
2046 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048
2049 /* deallocate hcd/hardware state ... nuking all pending urbs and
2050 * cleaning up all state associated with the current configuration
2051 * so that the hardware is now fully quiesced.
2052 */
Alan Stern782da722006-07-01 22:09:35 -04002053 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002055 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Lan Tianyufde26382013-01-20 01:53:33 +08002057 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002058 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2059 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002060
2061 sysfs_remove_link(&udev->dev.kobj, "port");
2062 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002063
Lan Tianyuad493e52013-01-23 04:26:30 +08002064 if (!port_dev->did_runtime_put)
2065 pm_runtime_put(&port_dev->dev);
2066 else
2067 port_dev->did_runtime_put = false;
Lan Tianyufde26382013-01-20 01:53:33 +08002068 }
2069
Alan Stern3b23dd62008-12-05 14:10:34 -05002070 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002071 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002072
Alan Stern782da722006-07-01 22:09:35 -04002073 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002074 * for de-configuring the device and invoking the remove-device
2075 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002076 */
2077 device_del(&udev->dev);
2078
2079 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 * (or root_hub) pointer.
2081 */
Alan Stern3b29b682011-02-22 09:53:41 -05002082 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* Avoid races with recursively_mark_NOTATTACHED() */
2085 spin_lock_irq(&device_state_lock);
2086 *pdev = NULL;
2087 spin_unlock_irq(&device_state_lock);
2088
Herbert Xuf7410ce2010-01-10 20:15:03 +11002089 hub_free_dev(udev);
2090
Alan Stern782da722006-07-01 22:09:35 -04002091 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002094#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095static void show_string(struct usb_device *udev, char *id, char *string)
2096{
2097 if (!string)
2098 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002099 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002102static void announce_device(struct usb_device *udev)
2103{
2104 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2105 le16_to_cpu(udev->descriptor.idVendor),
2106 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002107 dev_info(&udev->dev,
2108 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002109 udev->descriptor.iManufacturer,
2110 udev->descriptor.iProduct,
2111 udev->descriptor.iSerialNumber);
2112 show_string(udev, "Product", udev->product);
2113 show_string(udev, "Manufacturer", udev->manufacturer);
2114 show_string(udev, "SerialNumber", udev->serial);
2115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002117static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118#endif
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120#ifdef CONFIG_USB_OTG
2121#include "otg_whitelist.h"
2122#endif
2123
Oliver Neukum3ede7602007-01-11 14:35:50 +01002124/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002125 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002126 * @udev: newly addressed device (in ADDRESS state)
2127 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002128 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002129 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002130static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002132 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134#ifdef CONFIG_USB_OTG
2135 /*
2136 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2137 * to wake us after we've powered off VBUS; and HNP, switching roles
2138 * "host" to "peripheral". The OTG descriptor helps figure this out.
2139 */
2140 if (!udev->bus->is_b_host
2141 && udev->config
2142 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002143 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct usb_bus *bus = udev->bus;
2145
2146 /* descriptor may appear anywhere in config */
2147 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2148 le16_to_cpu(udev->config[0].desc.wTotalLength),
2149 USB_DT_OTG, (void **) &desc) == 0) {
2150 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002151 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 dev_info(&udev->dev,
2154 "Dual-Role OTG device on %sHNP port\n",
2155 (port1 == bus->otg_port)
2156 ? "" : "non-");
2157
2158 /* enable HNP before suspend, it's simpler */
2159 if (port1 == bus->otg_port)
2160 bus->b_hnp_enable = 1;
2161 err = usb_control_msg(udev,
2162 usb_sndctrlpipe(udev, 0),
2163 USB_REQ_SET_FEATURE, 0,
2164 bus->b_hnp_enable
2165 ? USB_DEVICE_B_HNP_ENABLE
2166 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2167 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2168 if (err < 0) {
2169 /* OTG MESSAGE: report errors here,
2170 * customize to match your product.
2171 */
2172 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002173 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 err);
2175 bus->b_hnp_enable = 0;
2176 }
2177 }
2178 }
2179 }
2180
2181 if (!is_targeted(udev)) {
2182
2183 /* Maybe it can talk to us, though we can't talk to it.
2184 * (Includes HNP test device.)
2185 */
2186 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002187 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (err < 0)
2189 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2190 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002191 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 goto fail;
2193 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002194fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002196 return err;
2197}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002199
2200/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002201 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002202 * @udev: newly addressed device (in ADDRESS state)
2203 *
2204 * This is only called by usb_new_device() and usb_authorize_device()
2205 * and FIXME -- all comments that apply to them apply here wrt to
2206 * environment.
2207 *
2208 * If the device is WUSB and not authorized, we don't attempt to read
2209 * the string descriptors, as they will be errored out by the device
2210 * until it has been authorized.
2211 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002212static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002213{
2214 int err;
2215
2216 if (udev->config == NULL) {
2217 err = usb_get_configuration(udev);
2218 if (err < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04002219 if (err != -ENODEV)
2220 dev_err(&udev->dev, "can't read configurations, error %d\n",
2221 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002222 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002223 }
2224 }
2225 if (udev->wusb == 1 && udev->authorized == 0) {
2226 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2227 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2228 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2229 }
2230 else {
2231 /* read the standard strings and cache them if present */
2232 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2233 udev->manufacturer = usb_cache_string(udev,
2234 udev->descriptor.iManufacturer);
2235 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2236 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002237 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002238 if (err < 0)
2239 return err;
2240
2241 usb_detect_interface_quirks(udev);
2242
2243 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002244}
2245
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002246static void set_usb_port_removable(struct usb_device *udev)
2247{
2248 struct usb_device *hdev = udev->parent;
2249 struct usb_hub *hub;
2250 u8 port = udev->portnum;
2251 u16 wHubCharacteristics;
2252 bool removable = true;
2253
2254 if (!hdev)
2255 return;
2256
Lan Tianyuad493e52013-01-23 04:26:30 +08002257 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002258
2259 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2260
2261 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2262 return;
2263
2264 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002265 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2266 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002267 removable = false;
2268 } else {
2269 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2270 removable = false;
2271 }
2272
2273 if (removable)
2274 udev->removable = USB_DEVICE_REMOVABLE;
2275 else
2276 udev->removable = USB_DEVICE_FIXED;
2277}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002278
2279/**
2280 * usb_new_device - perform initial device setup (usbcore-internal)
2281 * @udev: newly addressed device (in ADDRESS state)
2282 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002283 * This is called with devices which have been detected but not fully
2284 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002285 * for any device configuration. The caller must have locked either
2286 * the parent hub (if udev is a normal device) or else the
2287 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2288 * udev has already been installed, but udev is not yet visible through
2289 * sysfs or other filesystem code.
2290 *
2291 * It will return if the device is configured properly or not. Zero if
2292 * the interface was registered with the driver core; else a negative
2293 * errno value.
2294 *
2295 * This call is synchronous, and may not be used in an interrupt context.
2296 *
2297 * Only the hub driver or root-hub registrar should ever call this.
2298 */
2299int usb_new_device(struct usb_device *udev)
2300{
2301 int err;
2302
Dan Streetman16985402010-01-06 09:56:53 -05002303 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002304 /* Initialize non-root-hub device wakeup to disabled;
2305 * device (un)configuration controls wakeup capable
2306 * sysfs power/wakeup controls wakeup enabled/disabled
2307 */
2308 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002309 }
2310
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002311 /* Tell the runtime-PM framework the device is active */
2312 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002313 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002314 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002315 pm_runtime_enable(&udev->dev);
2316
Alan Sternc08512c2010-11-15 15:57:58 -05002317 /* By default, forbid autosuspend for all devices. It will be
2318 * allowed for hubs during binding.
2319 */
2320 usb_disable_autosuspend(udev);
2321
Alan Stern8d8558d2009-12-08 15:50:41 -05002322 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002323 if (err < 0)
2324 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002325 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2326 udev->devnum, udev->bus->busnum,
2327 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002328 /* export the usbdev device-node for libusb */
2329 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2330 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2331
Alan Stern6cd13202008-11-13 15:08:30 -05002332 /* Tell the world! */
2333 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002334
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002335 if (udev->serial)
2336 add_device_randomness(udev->serial, strlen(udev->serial));
2337 if (udev->product)
2338 add_device_randomness(udev->product, strlen(udev->product));
2339 if (udev->manufacturer)
2340 add_device_randomness(udev->manufacturer,
2341 strlen(udev->manufacturer));
2342
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002343 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002344
2345 /*
2346 * check whether the hub marks this port as non-removable. Do it
2347 * now so that platform-specific data can override it in
2348 * device_add()
2349 */
2350 if (udev->parent)
2351 set_usb_port_removable(udev);
2352
Alan Stern782da722006-07-01 22:09:35 -04002353 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002354 * for configuring the device and invoking the add-device
2355 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002356 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002357 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (err) {
2359 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2360 goto fail;
2361 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002362
Lan Tianyufde26382013-01-20 01:53:33 +08002363 /* Create link files between child device and usb port device. */
2364 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002365 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2366 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002367
2368 err = sysfs_create_link(&udev->dev.kobj,
2369 &port_dev->dev.kobj, "port");
2370 if (err)
2371 goto fail;
2372
2373 err = sysfs_create_link(&port_dev->dev.kobj,
2374 &udev->dev.kobj, "device");
2375 if (err) {
2376 sysfs_remove_link(&udev->dev.kobj, "port");
2377 goto fail;
2378 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002379
2380 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002381 }
2382
Alan Stern3b23dd62008-12-05 14:10:34 -05002383 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002384 usb_mark_last_busy(udev);
2385 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388fail:
2389 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002390 pm_runtime_disable(&udev->dev);
2391 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002392 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002395
2396/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002397 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2398 * @usb_dev: USB device
2399 *
2400 * Move the USB device to a very basic state where interfaces are disabled
2401 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002402 *
2403 * We share a lock (that we have) with device_del(), so we need to
2404 * defer its call.
2405 */
2406int usb_deauthorize_device(struct usb_device *usb_dev)
2407{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002408 usb_lock_device(usb_dev);
2409 if (usb_dev->authorized == 0)
2410 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002411
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002412 usb_dev->authorized = 0;
2413 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002414
2415 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002416 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002417 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002418 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002419 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002420 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002421
2422 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002423 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002424
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002425out_unauthorized:
2426 usb_unlock_device(usb_dev);
2427 return 0;
2428}
2429
2430
2431int usb_authorize_device(struct usb_device *usb_dev)
2432{
2433 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002434
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002435 usb_lock_device(usb_dev);
2436 if (usb_dev->authorized == 1)
2437 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002438
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002439 result = usb_autoresume_device(usb_dev);
2440 if (result < 0) {
2441 dev_err(&usb_dev->dev,
2442 "can't autoresume for authorization: %d\n", result);
2443 goto error_autoresume;
2444 }
2445 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2446 if (result < 0) {
2447 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2448 "authorization: %d\n", result);
2449 goto error_device_descriptor;
2450 }
Alan Sternda307122009-12-08 15:54:44 -05002451
2452 kfree(usb_dev->product);
2453 usb_dev->product = NULL;
2454 kfree(usb_dev->manufacturer);
2455 usb_dev->manufacturer = NULL;
2456 kfree(usb_dev->serial);
2457 usb_dev->serial = NULL;
2458
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002459 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002460 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002461 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002462 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002463 /* Choose and set the configuration. This registers the interfaces
2464 * with the driver core and lets interface drivers bind to them.
2465 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002466 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002467 if (c >= 0) {
2468 result = usb_set_configuration(usb_dev, c);
2469 if (result) {
2470 dev_err(&usb_dev->dev,
2471 "can't set config #%d, error %d\n", c, result);
2472 /* This need not be fatal. The user can try to
2473 * set other configurations. */
2474 }
2475 }
2476 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002477
Alan Stern8d8558d2009-12-08 15:50:41 -05002478error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002479error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002480 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002481error_autoresume:
2482out_authorized:
2483 usb_unlock_device(usb_dev); // complements locktree
2484 return result;
2485}
2486
2487
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002488/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2489static unsigned hub_is_wusb(struct usb_hub *hub)
2490{
2491 struct usb_hcd *hcd;
2492 if (hub->hdev->parent != NULL) /* not a root hub? */
2493 return 0;
2494 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2495 return hcd->wireless;
2496}
2497
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499#define PORT_RESET_TRIES 5
2500#define SET_ADDRESS_TRIES 2
2501#define GET_DESCRIPTOR_TRIES 2
2502#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302503#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
2505#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2506#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002507#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002509#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Sarah Sharp10d674a2011-09-14 14:24:52 -07002511static int hub_port_reset(struct usb_hub *hub, int port1,
2512 struct usb_device *udev, unsigned int delay, bool warm);
2513
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002514/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2515 * Port worm reset is required to recover
2516 */
2517static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002518{
2519 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002520 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2521 USB_SS_PORT_LS_SS_INACTIVE) ||
2522 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2523 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002527 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
2529 int delay_time, ret;
2530 u16 portstatus;
2531 u16 portchange;
2532
2533 for (delay_time = 0;
2534 delay_time < HUB_RESET_TIMEOUT;
2535 delay_time += delay) {
2536 /* wait to give the device a chance to reset */
2537 msleep(delay);
2538
2539 /* read and decode port status */
2540 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2541 if (ret < 0)
2542 return ret;
2543
Sarah Sharp4f434472012-11-15 14:58:04 -08002544 /* The port state is unknown until the reset completes. */
Sarah Sharp470f0be2012-12-11 10:14:03 -08002545 if (!(portstatus & USB_PORT_STAT_RESET))
2546 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /* switch to the long delay after two short delay failures */
2549 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2550 delay = HUB_LONG_RESET_TIME;
2551
2552 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002553 "port %d not %sreset yet, waiting %dms\n",
2554 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 }
2556
Sarah Sharp470f0be2012-12-11 10:14:03 -08002557 if ((portstatus & USB_PORT_STAT_RESET))
2558 return -EBUSY;
2559
2560 if (hub_port_warm_reset_required(hub, portstatus))
2561 return -ENOTCONN;
2562
2563 /* Device went away? */
2564 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2565 return -ENOTCONN;
2566
2567 /* bomb out completely if the connection bounced. A USB 3.0
2568 * connection may bounce if multiple warm resets were issued,
2569 * but the device may have successfully re-connected. Ignore it.
2570 */
2571 if (!hub_is_superspeed(hub->hdev) &&
2572 (portchange & USB_PORT_STAT_C_CONNECTION))
2573 return -ENOTCONN;
2574
2575 if (!(portstatus & USB_PORT_STAT_ENABLE))
2576 return -EBUSY;
2577
2578 if (!udev)
2579 return 0;
2580
2581 if (hub_is_wusb(hub))
2582 udev->speed = USB_SPEED_WIRELESS;
2583 else if (hub_is_superspeed(hub->hdev))
2584 udev->speed = USB_SPEED_SUPER;
2585 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2586 udev->speed = USB_SPEED_HIGH;
2587 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2588 udev->speed = USB_SPEED_LOW;
2589 else
2590 udev->speed = USB_SPEED_FULL;
2591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592}
2593
Andiry Xu75d7cf72011-09-13 16:41:11 -07002594static void hub_port_finish_reset(struct usb_hub *hub, int port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002595 struct usb_device *udev, int *status)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002596{
2597 switch (*status) {
2598 case 0:
Sarah Sharpa24a6072012-11-01 11:20:44 -07002599 /* TRSTRCY = 10 ms; plus some extra */
2600 msleep(10 + 40);
2601 if (udev) {
2602 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2603
2604 update_devnum(udev, 0);
2605 /* The xHC may think the device is already reset,
2606 * so ignore the status.
2607 */
2608 if (hcd->driver->reset_device)
2609 hcd->driver->reset_device(hcd, udev);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002610 }
2611 /* FALL THROUGH */
2612 case -ENOTCONN:
2613 case -ENODEV:
Lan Tianyuad493e52013-01-23 04:26:30 +08002614 usb_clear_port_feature(hub->hdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002615 port1, USB_PORT_FEAT_C_RESET);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002616 if (hub_is_superspeed(hub->hdev)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002617 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002618 USB_PORT_FEAT_C_BH_PORT_RESET);
Lan Tianyuad493e52013-01-23 04:26:30 +08002619 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002620 USB_PORT_FEAT_C_PORT_LINK_STATE);
Lan Tianyuad493e52013-01-23 04:26:30 +08002621 usb_clear_port_feature(hub->hdev, port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002622 USB_PORT_FEAT_C_CONNECTION);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002623 }
Sarah Sharpa24a6072012-11-01 11:20:44 -07002624 if (udev)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002625 usb_set_device_state(udev, *status
2626 ? USB_STATE_NOTATTACHED
2627 : USB_STATE_DEFAULT);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002628 break;
2629 }
2630}
2631
2632/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002634 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
2636 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002637 u16 portchange, portstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002639 if (!hub_is_superspeed(hub->hdev)) {
2640 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002641 dev_err(hub->intfdev, "only USB3 hub support "
2642 "warm reset\n");
2643 return -EINVAL;
2644 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002645 /* Block EHCI CF initialization during the port reset.
2646 * Some companion controllers don't like it when they mix.
2647 */
2648 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002649 } else if (!warm) {
2650 /*
2651 * If the caller hasn't explicitly requested a warm reset,
2652 * double check and see if one is needed.
2653 */
2654 status = hub_port_status(hub, port1,
2655 &portstatus, &portchange);
2656 if (status < 0)
2657 goto done;
2658
2659 if (hub_port_warm_reset_required(hub, portstatus))
2660 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002661 }
Alan Stern32fe0192007-10-10 16:27:07 -04002662
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 /* Reset the port */
2664 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002665 status = set_port_feature(hub->hdev, port1, (warm ?
2666 USB_PORT_FEAT_BH_PORT_RESET :
2667 USB_PORT_FEAT_RESET));
Alan Sterne9e88fb2013-03-27 16:14:01 -04002668 if (status == -ENODEV) {
2669 ; /* The hub is gone */
2670 } else if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002672 "cannot %sreset port %d (err = %d)\n",
2673 warm ? "warm " : "", port1, status);
2674 } else {
2675 status = hub_port_wait_reset(hub, port1, udev, delay,
2676 warm);
Alan Sterne9e88fb2013-03-27 16:14:01 -04002677 if (status && status != -ENOTCONN && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 dev_dbg(hub->intfdev,
2679 "port_wait_reset: err = %d\n",
2680 status);
2681 }
2682
Sarah Sharpa24a6072012-11-01 11:20:44 -07002683 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002684 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Sarah Sharpa24a6072012-11-01 11:20:44 -07002685 hub_port_finish_reset(hub, port1, udev, &status);
2686
2687 if (!hub_is_superspeed(hub->hdev))
2688 goto done;
2689
2690 /*
2691 * If a USB 3.0 device migrates from reset to an error
2692 * state, re-issue the warm reset.
2693 */
2694 if (hub_port_status(hub, port1,
2695 &portstatus, &portchange) < 0)
2696 goto done;
2697
2698 if (!hub_port_warm_reset_required(hub, portstatus))
2699 goto done;
2700
2701 /*
2702 * If the port is in SS.Inactive or Compliance Mode, the
2703 * hot or warm reset failed. Try another warm reset.
2704 */
2705 if (!warm) {
2706 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2707 port1);
2708 warm = true;
2709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711
2712 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002713 "port %d not enabled, trying %sreset again...\n",
2714 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 delay = HUB_LONG_RESET_TIME;
2716 }
2717
2718 dev_err (hub->intfdev,
2719 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2720 port1);
2721
Andiry Xu75d7cf72011-09-13 16:41:11 -07002722done:
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002723 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002724 up_read(&ehci_cf_port_reset_rwsem);
2725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return status;
2727}
2728
Andiry Xu0ed9a572011-04-27 18:07:43 +08002729/* Check if a port is power on */
2730static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2731{
2732 int ret = 0;
2733
2734 if (hub_is_superspeed(hub->hdev)) {
2735 if (portstatus & USB_SS_PORT_STAT_POWER)
2736 ret = 1;
2737 } else {
2738 if (portstatus & USB_PORT_STAT_POWER)
2739 ret = 1;
2740 }
2741
2742 return ret;
2743}
2744
Alan Sternd388dab2006-07-01 22:14:24 -04002745#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Andiry Xu0ed9a572011-04-27 18:07:43 +08002747/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2748static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2749{
2750 int ret = 0;
2751
2752 if (hub_is_superspeed(hub->hdev)) {
2753 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2754 == USB_SS_PORT_LS_U3)
2755 ret = 1;
2756 } else {
2757 if (portstatus & USB_PORT_STAT_SUSPEND)
2758 ret = 1;
2759 }
2760
2761 return ret;
2762}
Alan Sternb01b03f2008-04-28 11:06:11 -04002763
2764/* Determine whether the device on a port is ready for a normal resume,
2765 * is ready for a reset-resume, or should be disconnected.
2766 */
2767static int check_port_resume_type(struct usb_device *udev,
2768 struct usb_hub *hub, int port1,
2769 int status, unsigned portchange, unsigned portstatus)
2770{
2771 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002772 if (status || port_is_suspended(hub, portstatus) ||
2773 !port_is_power_on(hub, portstatus) ||
2774 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002775 if (status >= 0)
2776 status = -ENODEV;
2777 }
2778
Alan Stern86c57ed2008-06-30 11:14:43 -04002779 /* Can't do a normal resume if the port isn't enabled,
2780 * so try a reset-resume instead.
2781 */
2782 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2783 if (udev->persist_enabled)
2784 udev->reset_resume = 1;
2785 else
2786 status = -ENODEV;
2787 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002788
2789 if (status) {
2790 dev_dbg(hub->intfdev,
2791 "port %d status %04x.%04x after resume, %d\n",
2792 port1, portchange, portstatus, status);
2793 } else if (udev->reset_resume) {
2794
2795 /* Late port handoff can set status-change bits */
2796 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08002797 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002798 USB_PORT_FEAT_C_CONNECTION);
2799 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08002800 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002801 USB_PORT_FEAT_C_ENABLE);
2802 }
2803
2804 return status;
2805}
2806
Sarah Sharpf74631e2012-06-25 12:08:08 -07002807int usb_disable_ltm(struct usb_device *udev)
2808{
2809 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2810
2811 /* Check if the roothub and device supports LTM. */
2812 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2813 !usb_device_supports_ltm(udev))
2814 return 0;
2815
2816 /* Clear Feature LTM Enable can only be sent if the device is
2817 * configured.
2818 */
2819 if (!udev->actconfig)
2820 return 0;
2821
2822 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2823 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2824 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2825 USB_CTRL_SET_TIMEOUT);
2826}
2827EXPORT_SYMBOL_GPL(usb_disable_ltm);
2828
2829void usb_enable_ltm(struct usb_device *udev)
2830{
2831 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2832
2833 /* Check if the roothub and device supports LTM. */
2834 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2835 !usb_device_supports_ltm(udev))
2836 return;
2837
2838 /* Set Feature LTM Enable can only be sent if the device is
2839 * configured.
2840 */
2841 if (!udev->actconfig)
2842 return;
2843
2844 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2845 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2846 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2847 USB_CTRL_SET_TIMEOUT);
2848}
2849EXPORT_SYMBOL_GPL(usb_enable_ltm);
2850
Alan Stern84ebc102013-03-27 16:14:46 -04002851#ifdef CONFIG_PM
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002852/*
2853 * usb_disable_function_remotewakeup - disable usb3.0
2854 * device's function remote wakeup
2855 * @udev: target device
2856 *
2857 * Assume there's only one function on the USB 3.0
2858 * device and disable remote wake for the first
2859 * interface. FIXME if the interface association
2860 * descriptor shows there's more than one function.
2861 */
2862static int usb_disable_function_remotewakeup(struct usb_device *udev)
2863{
2864 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2865 USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE,
2866 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
2867 USB_CTRL_SET_TIMEOUT);
2868}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Alan Stern5deba4c2013-07-11 14:58:04 -04002870/* Count of wakeup-enabled devices at or below udev */
2871static unsigned wakeup_enabled_descendants(struct usb_device *udev)
2872{
2873 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2874
2875 return udev->do_remote_wakeup +
2876 (hub ? hub->wakeup_enabled_descendants : 0);
2877}
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879/*
Alan Stern140d8f62006-07-01 22:07:21 -04002880 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002881 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002882 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 *
2884 * Suspends a USB device that isn't in active use, conserving power.
2885 * Devices may wake out of a suspend, if anything important happens,
2886 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002887 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 * to disconnect devices while they are suspended.
2889 *
David Brownell390a8c32005-09-13 19:57:27 -07002890 * This only affects the USB hardware for a device; its interfaces
2891 * (and, for hubs, child devices) must already have been suspended.
2892 *
Alan Stern624d6c02007-05-30 15:35:16 -04002893 * Selective port suspend reduces power; most suspended devices draw
2894 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2895 * All devices below the suspended port are also suspended.
2896 *
2897 * Devices leave suspend state when the host wakes them up. Some devices
2898 * also support "remote wakeup", where the device can activate the USB
2899 * tree above them to deliver data, such as a keypress or packet. In
2900 * some cases, this wakes the USB host.
2901 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 * Suspending OTG devices may trigger HNP, if that's been enabled
2903 * between a pair of dual-role devices. That will change roles, such
2904 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2905 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002906 * Devices on USB hub ports have only one "suspend" state, corresponding
2907 * to ACPI D2, "may cause the device to lose some context".
2908 * State transitions include:
2909 *
2910 * - suspend, resume ... when the VBUS power link stays live
2911 * - suspend, disconnect ... VBUS lost
2912 *
2913 * Once VBUS drop breaks the circuit, the port it's using has to go through
2914 * normal re-enumeration procedures, starting with enabling VBUS power.
2915 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2916 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2917 * timer, no SRP, no requests through sysfs.
2918 *
Alan Stern5deba4c2013-07-11 14:58:04 -04002919 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
2920 * suspended until their bus goes into global suspend (i.e., the root
Alan Stern0aa28322013-03-27 16:14:19 -04002921 * hub is suspended). Nevertheless, we change @udev->state to
2922 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
2923 * upstream port setting is stored in @udev->port_is_suspended.
Alan Stern4956ecc2007-05-30 16:51:28 -04002924 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 * Returns 0 on success, else negative errno.
2926 */
Alan Stern65bfd292008-11-25 16:39:18 -05002927int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Lan Tianyuad493e52013-01-23 04:26:30 +08002929 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
2930 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04002931 int port1 = udev->portnum;
2932 int status;
Alan Stern0aa28322013-03-27 16:14:19 -04002933 bool really_suspend = true;
Alan Stern4956ecc2007-05-30 16:51:28 -04002934
Alan Stern624d6c02007-05-30 15:35:16 -04002935 /* enable remote wakeup when appropriate; this lets the device
2936 * wake up the upstream hub (including maybe the root hub).
2937 *
2938 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2939 * we don't explicitly enable it here.
2940 */
2941 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002942 if (!hub_is_superspeed(hub->hdev)) {
2943 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2944 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2945 USB_DEVICE_REMOTE_WAKEUP, 0,
2946 NULL, 0,
2947 USB_CTRL_SET_TIMEOUT);
2948 } else {
2949 /* Assume there's only one function on the USB 3.0
2950 * device and enable remote wake for the first
2951 * interface. FIXME if the interface association
2952 * descriptor shows there's more than one function.
2953 */
2954 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2955 USB_REQ_SET_FEATURE,
2956 USB_RECIP_INTERFACE,
2957 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002958 USB_INTRF_FUNC_SUSPEND_RW |
2959 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002960 NULL, 0,
2961 USB_CTRL_SET_TIMEOUT);
2962 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002963 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002964 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2965 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002966 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002967 if (PMSG_IS_AUTO(msg))
Alan Sternb504b4a12013-07-30 15:39:02 -04002968 goto err_wakeup;
Oliver Neukum0c487202009-10-19 13:19:41 +02002969 }
Alan Stern624d6c02007-05-30 15:35:16 -04002970 }
2971
Andiry Xu65580b432011-09-23 14:19:52 -07002972 /* disable USB2 hardware LPM */
2973 if (udev->usb2_hw_lpm_enabled == 1)
2974 usb_set_usb2_hardware_lpm(udev, 0);
2975
Sarah Sharpf74631e2012-06-25 12:08:08 -07002976 if (usb_disable_ltm(udev)) {
Alan Sternb504b4a12013-07-30 15:39:02 -04002977 dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
2978 status = -ENOMEM;
2979 if (PMSG_IS_AUTO(msg))
2980 goto err_ltm;
Sarah Sharpf74631e2012-06-25 12:08:08 -07002981 }
Sarah Sharp83060952012-05-02 14:25:52 -07002982 if (usb_unlocked_disable_lpm(udev)) {
Alan Sternb504b4a12013-07-30 15:39:02 -04002983 dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
2984 status = -ENOMEM;
2985 if (PMSG_IS_AUTO(msg))
2986 goto err_lpm3;
Sarah Sharp83060952012-05-02 14:25:52 -07002987 }
2988
Alan Stern624d6c02007-05-30 15:35:16 -04002989 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002990 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08002991 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Alan Stern5deba4c2013-07-11 14:58:04 -04002992
Alan Stern0aa28322013-03-27 16:14:19 -04002993 /*
2994 * For system suspend, we do not need to enable the suspend feature
2995 * on individual USB-2 ports. The devices will automatically go
2996 * into suspend a few ms after the root hub stops sending packets.
2997 * The USB 2.0 spec calls this "global suspend".
Alan Stern5deba4c2013-07-11 14:58:04 -04002998 *
2999 * However, many USB hubs have a bug: They don't relay wakeup requests
3000 * from a downstream port if the port's suspend feature isn't on.
3001 * Therefore we will turn on the suspend feature if udev or any of its
3002 * descendants is enabled for remote wakeup.
Alan Stern0aa28322013-03-27 16:14:19 -04003003 */
Alan Stern5deba4c2013-07-11 14:58:04 -04003004 else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3005 status = set_port_feature(hub->hdev, port1,
3006 USB_PORT_FEAT_SUSPEND);
Alan Stern0aa28322013-03-27 16:14:19 -04003007 else {
3008 really_suspend = false;
3009 status = 0;
3010 }
Alan Stern624d6c02007-05-30 15:35:16 -04003011 if (status) {
3012 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
3013 port1, status);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003014
Alan Sternb504b4a12013-07-30 15:39:02 -04003015 /* Try to enable USB3 LPM and LTM again */
3016 usb_unlocked_enable_lpm(udev);
3017 err_lpm3:
3018 usb_enable_ltm(udev);
3019 err_ltm:
Andiry Xuc3e751e2012-05-05 00:50:10 +08003020 /* Try to enable USB2 hardware LPM again */
3021 if (udev->usb2_hw_lpm_capable == 1)
3022 usb_set_usb2_hardware_lpm(udev, 1);
3023
Alan Sternb504b4a12013-07-30 15:39:02 -04003024 if (udev->do_remote_wakeup) {
3025 if (udev->speed < USB_SPEED_SUPER)
3026 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3027 USB_REQ_CLEAR_FEATURE,
3028 USB_RECIP_DEVICE,
3029 USB_DEVICE_REMOTE_WAKEUP, 0,
3030 NULL, 0, USB_CTRL_SET_TIMEOUT);
3031 else
3032 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3033 USB_REQ_CLEAR_FEATURE,
3034 USB_RECIP_INTERFACE,
3035 USB_INTRF_FUNC_SUSPEND, 0,
3036 NULL, 0, USB_CTRL_SET_TIMEOUT);
3037 }
3038 err_wakeup:
Sarah Sharp83060952012-05-02 14:25:52 -07003039
Alan Sterncbb33002011-06-15 16:29:16 -04003040 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003041 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003042 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04003043 } else {
Alan Stern30b1a7a2011-09-27 21:54:22 +02003044 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3045 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3046 udev->do_remote_wakeup);
Alan Stern0aa28322013-03-27 16:14:19 -04003047 if (really_suspend) {
3048 udev->port_is_suspended = 1;
Alan Stern5deba4c2013-07-11 14:58:04 -04003049
3050 /* device has up to 10 msec to fully suspend */
Alan Stern0aa28322013-03-27 16:14:19 -04003051 msleep(10);
3052 }
Alan Stern5deba4c2013-07-11 14:58:04 -04003053 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Stern624d6c02007-05-30 15:35:16 -04003054 }
Lan Tianyuad493e52013-01-23 04:26:30 +08003055
Alan Sternb504b4a12013-07-30 15:39:02 -04003056 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled) {
Lan Tianyue0835052013-07-03 22:17:54 +08003057 pm_runtime_put_sync(&port_dev->dev);
3058 port_dev->did_runtime_put = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08003059 }
3060
Alan Sternc08512c2010-11-15 15:57:58 -05003061 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003062 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
David Brownellf3f32532005-09-22 22:37:29 -07003064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065/*
David Brownell390a8c32005-09-13 19:57:27 -07003066 * If the USB "suspend" state is in use (rather than "global suspend"),
3067 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003068 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 * hardware resume signaling is finished, either because of selective
3070 * resume (by host) or remote wakeup (by device) ... now see what changed
3071 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003072 *
3073 * If @udev->reset_resume is set then the device is reset before the
3074 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 */
Alan Stern140d8f62006-07-01 22:07:21 -04003076static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Alan Stern54515fe2007-05-30 15:38:58 -04003078 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003079 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
3081 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003082 dev_dbg(&udev->dev, "%s\n",
3083 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 /* usb ch9 identifies four variants of SUSPENDED, based on what
3086 * state the device resumes to. Linux currently won't see the
3087 * first two on the host side; they'd be inside hub_port_init()
3088 * during many timeouts, but khubd can't suspend until later.
3089 */
3090 usb_set_device_state(udev, udev->actconfig
3091 ? USB_STATE_CONFIGURED
3092 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Alan Stern54515fe2007-05-30 15:38:58 -04003094 /* 10.5.4.5 says not to reset a suspended port if the attached
3095 * device is enabled for remote wakeup. Hence the reset
3096 * operation is carried out here, after the port has been
3097 * resumed.
3098 */
3099 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04003100 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08003101 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 /* 10.5.4.5 says be sure devices in the tree are still there.
3104 * For now let's assume the device didn't go crazy on resume,
3105 * and device drivers will know about any resume quirks.
3106 */
Alan Stern54515fe2007-05-30 15:38:58 -04003107 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003108 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003109 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
3110 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04003111 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04003112
3113 /* If a normal resume failed, try doing a reset-resume */
3114 if (status && !udev->reset_resume && udev->persist_enabled) {
3115 dev_dbg(&udev->dev, "retry with reset-resume\n");
3116 udev->reset_resume = 1;
3117 goto retry_reset_resume;
3118 }
Alan Stern54515fe2007-05-30 15:38:58 -04003119 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003120
Alan Stern624d6c02007-05-30 15:35:16 -04003121 if (status) {
3122 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3123 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003124 /*
3125 * There are a few quirky devices which violate the standard
3126 * by claiming to have remote wakeup enabled after a reset,
3127 * which crash if the feature is cleared, hence check for
3128 * udev->reset_resume
3129 */
3130 } else if (udev->actconfig && !udev->reset_resume) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003131 if (!hub_is_superspeed(udev->parent)) {
3132 le16_to_cpus(&devstatus);
3133 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
3134 status = usb_control_msg(udev,
3135 usb_sndctrlpipe(udev, 0),
3136 USB_REQ_CLEAR_FEATURE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 USB_RECIP_DEVICE,
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003138 USB_DEVICE_REMOTE_WAKEUP, 0,
3139 NULL, 0,
3140 USB_CTRL_SET_TIMEOUT);
3141 } else {
3142 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3143 &devstatus);
3144 le16_to_cpus(&devstatus);
3145 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3146 | USB_INTRF_STAT_FUNC_RW))
3147 status =
3148 usb_disable_function_remotewakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003149 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003150
3151 if (status)
3152 dev_dbg(&udev->dev,
3153 "disable remote wakeup, status %d\n",
3154 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
3157 return status;
3158}
3159
Alan Stern624d6c02007-05-30 15:35:16 -04003160/*
3161 * usb_port_resume - re-activate a suspended usb device's upstream port
3162 * @udev: device to re-activate, not a root hub
3163 * Context: must be able to sleep; device not locked; pm locks held
3164 *
3165 * This will re-activate the suspended device, increasing power usage
3166 * while letting drivers communicate again with its endpoints.
3167 * USB resume explicitly guarantees that the power session between
3168 * the host and the device is the same as it was when the device
3169 * suspended.
3170 *
Alan Sternfeccc302008-03-03 15:15:59 -05003171 * If @udev->reset_resume is set then this routine won't check that the
3172 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003173 * reset @udev. The end result is that a broken power session can be
3174 * recovered and @udev will appear to persist across a loss of VBUS power.
3175 *
3176 * For example, if a host controller doesn't maintain VBUS suspend current
3177 * during a system sleep or is reset when the system wakes up, all the USB
3178 * power sessions below it will be broken. This is especially troublesome
3179 * for mass-storage devices containing mounted filesystems, since the
3180 * device will appear to have disconnected and all the memory mappings
3181 * to it will be lost. Using the USB_PERSIST facility, the device can be
3182 * made to appear as if it had not disconnected.
3183 *
Ming Lei742120c2008-06-18 22:00:29 +08003184 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003185 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003186 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3187 * quite possible for a device to remain unaltered but its media to be
3188 * changed. If the user replaces a flash memory card while the system is
3189 * asleep, he will have only himself to blame when the filesystem on the
3190 * new card is corrupted and the system crashes.
3191 *
Alan Stern624d6c02007-05-30 15:35:16 -04003192 * Returns 0 on success, else negative errno.
3193 */
Alan Stern65bfd292008-11-25 16:39:18 -05003194int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
Lan Tianyuad493e52013-01-23 04:26:30 +08003196 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3197 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003198 int port1 = udev->portnum;
3199 int status;
3200 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003201
Lan Tianyuad493e52013-01-23 04:26:30 +08003202 if (port_dev->did_runtime_put) {
3203 status = pm_runtime_get_sync(&port_dev->dev);
3204 port_dev->did_runtime_put = false;
3205 if (status < 0) {
3206 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3207 status);
3208 return status;
3209 }
3210 }
3211
Alan Sternd25450c2006-11-20 11:14:30 -05003212 /* Skip the initial Clear-Suspend step for a remote wakeup */
3213 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003214 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003215 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3218
Alan Sternd5cbad42006-08-11 16:52:39 -04003219 set_bit(port1, hub->busy_bits);
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003222 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003223 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003224 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003225 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003226 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003228 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3229 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003232 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003233 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 msleep(25);
3235
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3237 * stop resume signaling. Then finish the resume
3238 * sequence.
3239 */
Alan Sternd25450c2006-11-20 11:14:30 -05003240 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003241
Alan Sternb01b03f2008-04-28 11:06:11 -04003242 /* TRSMRCY = 10 msec */
3243 msleep(10);
3244 }
Alan Stern54515fe2007-05-30 15:38:58 -04003245
Alan Sternb01b03f2008-04-28 11:06:11 -04003246 SuspendCleared:
3247 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003248 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003249 if (hub_is_superspeed(hub->hdev)) {
3250 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003251 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003252 USB_PORT_FEAT_C_PORT_LINK_STATE);
3253 } else {
3254 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003255 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003256 USB_PORT_FEAT_C_SUSPEND);
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Alan Sternd5cbad42006-08-11 16:52:39 -04003260 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003261
Alan Sternb01b03f2008-04-28 11:06:11 -04003262 status = check_port_resume_type(udev,
3263 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003264 if (status == 0)
3265 status = finish_port_resume(udev);
3266 if (status < 0) {
3267 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3268 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003269 } else {
3270 /* Try to enable USB2 hardware LPM */
3271 if (udev->usb2_hw_lpm_capable == 1)
3272 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003273
Sarah Sharpf74631e2012-06-25 12:08:08 -07003274 /* Try to enable USB3 LTM and LPM */
3275 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003276 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003277 }
Andiry Xu65580b432011-09-23 14:19:52 -07003278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return status;
3280}
3281
Alan Stern84ebc102013-03-27 16:14:46 -04003282#endif /* CONFIG_PM */
3283
3284#ifdef CONFIG_PM_RUNTIME
3285
Alan Stern8808f002008-04-28 11:06:55 -04003286/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003287int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
3289 int status = 0;
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003292 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003293 status = usb_autoresume_device(udev);
3294 if (status == 0) {
3295 /* Let the drivers do their thing, then... */
3296 usb_autosuspend_device(udev);
3297 }
Alan Sternd25450c2006-11-20 11:14:30 -05003298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return status;
3300}
3301
Alan Sternd388dab2006-07-01 22:14:24 -04003302#endif
3303
Ming Leie6f30de2012-10-24 11:59:24 +08003304static int check_ports_changed(struct usb_hub *hub)
3305{
3306 int port1;
3307
3308 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3309 u16 portstatus, portchange;
3310 int status;
3311
3312 status = hub_port_status(hub, port1, &portstatus, &portchange);
3313 if (!status && portchange)
3314 return 1;
3315 }
3316 return 0;
3317}
3318
David Brownelldb690872005-09-13 19:56:33 -07003319static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
3321 struct usb_hub *hub = usb_get_intfdata (intf);
3322 struct usb_device *hdev = hub->hdev;
3323 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003324 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Alan Stern5deba4c2013-07-11 14:58:04 -04003326 /*
3327 * Warn if children aren't already suspended.
3328 * Also, add up the number of wakeup-enabled descendants.
3329 */
3330 hub->wakeup_enabled_descendants = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3332 struct usb_device *udev;
3333
Lan Tianyuff823c72012-09-05 13:44:32 +08003334 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003335 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003336 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003337 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003338 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003339 }
Alan Stern5deba4c2013-07-11 14:58:04 -04003340 if (udev)
3341 hub->wakeup_enabled_descendants +=
3342 wakeup_enabled_descendants(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
Ming Leie6f30de2012-10-24 11:59:24 +08003344
3345 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3346 /* check if there are changes pending on hub ports */
3347 if (check_ports_changed(hub)) {
3348 if (PMSG_IS_AUTO(msg))
3349 return -EBUSY;
3350 pm_wakeup_event(&hdev->dev, 2000);
3351 }
3352 }
3353
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003354 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3355 /* Enable hub to send remote wakeup for all ports. */
3356 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3357 status = set_port_feature(hdev,
3358 port1 |
3359 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3360 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3361 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3362 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3363 }
3364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Harvey Harrison441b62c2008-03-03 16:08:34 -08003366 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003367
David Brownellc9f89fa2005-09-13 19:57:04 -07003368 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003369 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371}
3372
3373static int hub_resume(struct usb_interface *intf)
3374{
Alan Stern5e6effa2008-03-03 15:15:51 -05003375 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Alan Stern5e6effa2008-03-03 15:15:51 -05003377 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003378 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return 0;
3380}
3381
Alan Sternb41a60e2007-05-30 15:39:33 -04003382static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003383{
Alan Sternb41a60e2007-05-30 15:39:33 -04003384 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003385
Alan Stern5e6effa2008-03-03 15:15:51 -05003386 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003387 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003388 return 0;
3389}
3390
Alan Stern54515fe2007-05-30 15:38:58 -04003391/**
3392 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3393 * @rhdev: struct usb_device for the root hub
3394 *
3395 * The USB host controller driver calls this function when its root hub
3396 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003397 * has been reset. The routine marks @rhdev as having lost power.
3398 * When the hub driver is resumed it will take notice and carry out
3399 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3400 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003401 */
3402void usb_root_hub_lost_power(struct usb_device *rhdev)
3403{
3404 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3405 rhdev->reset_resume = 1;
3406}
3407EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3408
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003409static const char * const usb3_lpm_names[] = {
3410 "U0",
3411 "U1",
3412 "U2",
3413 "U3",
3414};
3415
3416/*
3417 * Send a Set SEL control transfer to the device, prior to enabling
3418 * device-initiated U1 or U2. This lets the device know the exit latencies from
3419 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3420 * packet from the host.
3421 *
3422 * This function will fail if the SEL or PEL values for udev are greater than
3423 * the maximum allowed values for the link state to be enabled.
3424 */
3425static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3426{
3427 struct usb_set_sel_req *sel_values;
3428 unsigned long long u1_sel;
3429 unsigned long long u1_pel;
3430 unsigned long long u2_sel;
3431 unsigned long long u2_pel;
3432 int ret;
3433
3434 /* Convert SEL and PEL stored in ns to us */
3435 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3436 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3437 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3438 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3439
3440 /*
3441 * Make sure that the calculated SEL and PEL values for the link
3442 * state we're enabling aren't bigger than the max SEL/PEL
3443 * value that will fit in the SET SEL control transfer.
3444 * Otherwise the device would get an incorrect idea of the exit
3445 * latency for the link state, and could start a device-initiated
3446 * U1/U2 when the exit latencies are too high.
3447 */
3448 if ((state == USB3_LPM_U1 &&
3449 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3450 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3451 (state == USB3_LPM_U2 &&
3452 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3453 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003454 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 -07003455 usb3_lpm_names[state], u1_sel, u1_pel);
3456 return -EINVAL;
3457 }
3458
3459 /*
3460 * If we're enabling device-initiated LPM for one link state,
3461 * but the other link state has a too high SEL or PEL value,
3462 * just set those values to the max in the Set SEL request.
3463 */
3464 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3465 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3466
3467 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3468 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3469
3470 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3471 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3472
3473 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3474 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3475
3476 /*
3477 * usb_enable_lpm() can be called as part of a failed device reset,
3478 * which may be initiated by an error path of a mass storage driver.
3479 * Therefore, use GFP_NOIO.
3480 */
3481 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3482 if (!sel_values)
3483 return -ENOMEM;
3484
3485 sel_values->u1_sel = u1_sel;
3486 sel_values->u1_pel = u1_pel;
3487 sel_values->u2_sel = cpu_to_le16(u2_sel);
3488 sel_values->u2_pel = cpu_to_le16(u2_pel);
3489
3490 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3491 USB_REQ_SET_SEL,
3492 USB_RECIP_DEVICE,
3493 0, 0,
3494 sel_values, sizeof *(sel_values),
3495 USB_CTRL_SET_TIMEOUT);
3496 kfree(sel_values);
3497 return ret;
3498}
3499
3500/*
3501 * Enable or disable device-initiated U1 or U2 transitions.
3502 */
3503static int usb_set_device_initiated_lpm(struct usb_device *udev,
3504 enum usb3_link_state state, bool enable)
3505{
3506 int ret;
3507 int feature;
3508
3509 switch (state) {
3510 case USB3_LPM_U1:
3511 feature = USB_DEVICE_U1_ENABLE;
3512 break;
3513 case USB3_LPM_U2:
3514 feature = USB_DEVICE_U2_ENABLE;
3515 break;
3516 default:
3517 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3518 __func__, enable ? "enable" : "disable");
3519 return -EINVAL;
3520 }
3521
3522 if (udev->state != USB_STATE_CONFIGURED) {
3523 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3524 "for unconfigured device.\n",
3525 __func__, enable ? "enable" : "disable",
3526 usb3_lpm_names[state]);
3527 return 0;
3528 }
3529
3530 if (enable) {
3531 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003532 * Now send the control transfer to enable device-initiated LPM
3533 * for either U1 or U2.
3534 */
3535 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3536 USB_REQ_SET_FEATURE,
3537 USB_RECIP_DEVICE,
3538 feature,
3539 0, NULL, 0,
3540 USB_CTRL_SET_TIMEOUT);
3541 } else {
3542 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3543 USB_REQ_CLEAR_FEATURE,
3544 USB_RECIP_DEVICE,
3545 feature,
3546 0, NULL, 0,
3547 USB_CTRL_SET_TIMEOUT);
3548 }
3549 if (ret < 0) {
3550 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3551 enable ? "Enable" : "Disable",
3552 usb3_lpm_names[state]);
3553 return -EBUSY;
3554 }
3555 return 0;
3556}
3557
3558static int usb_set_lpm_timeout(struct usb_device *udev,
3559 enum usb3_link_state state, int timeout)
3560{
3561 int ret;
3562 int feature;
3563
3564 switch (state) {
3565 case USB3_LPM_U1:
3566 feature = USB_PORT_FEAT_U1_TIMEOUT;
3567 break;
3568 case USB3_LPM_U2:
3569 feature = USB_PORT_FEAT_U2_TIMEOUT;
3570 break;
3571 default:
3572 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3573 __func__);
3574 return -EINVAL;
3575 }
3576
3577 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3578 timeout != USB3_LPM_DEVICE_INITIATED) {
3579 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3580 "which is a reserved value.\n",
3581 usb3_lpm_names[state], timeout);
3582 return -EINVAL;
3583 }
3584
3585 ret = set_port_feature(udev->parent,
3586 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3587 feature);
3588 if (ret < 0) {
3589 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3590 "error code %i\n", usb3_lpm_names[state],
3591 timeout, ret);
3592 return -EBUSY;
3593 }
3594 if (state == USB3_LPM_U1)
3595 udev->u1_params.timeout = timeout;
3596 else
3597 udev->u2_params.timeout = timeout;
3598 return 0;
3599}
3600
3601/*
3602 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3603 * U1/U2 entry.
3604 *
3605 * We will attempt to enable U1 or U2, but there are no guarantees that the
3606 * control transfers to set the hub timeout or enable device-initiated U1/U2
3607 * will be successful.
3608 *
3609 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3610 * driver know about it. If that call fails, it should be harmless, and just
3611 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3612 */
3613static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3614 enum usb3_link_state state)
3615{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003616 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003617 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3618 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3619
3620 /* If the device says it doesn't have *any* exit latency to come out of
3621 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3622 * state.
3623 */
3624 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3625 (state == USB3_LPM_U2 && u2_mel == 0))
3626 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003627
Sarah Sharp65a95b72012-10-05 10:32:07 -07003628 /*
3629 * First, let the device know about the exit latencies
3630 * associated with the link state we're about to enable.
3631 */
3632 ret = usb_req_set_sel(udev, state);
3633 if (ret < 0) {
3634 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3635 usb3_lpm_names[state]);
3636 return;
3637 }
3638
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003639 /* We allow the host controller to set the U1/U2 timeout internally
3640 * first, so that it can change its schedule to account for the
3641 * additional latency to send data to a device in a lower power
3642 * link state.
3643 */
3644 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3645
3646 /* xHCI host controller doesn't want to enable this LPM state. */
3647 if (timeout == 0)
3648 return;
3649
3650 if (timeout < 0) {
3651 dev_warn(&udev->dev, "Could not enable %s link state, "
3652 "xHCI error %i.\n", usb3_lpm_names[state],
3653 timeout);
3654 return;
3655 }
3656
3657 if (usb_set_lpm_timeout(udev, state, timeout))
3658 /* If we can't set the parent hub U1/U2 timeout,
3659 * device-initiated LPM won't be allowed either, so let the xHCI
3660 * host know that this link state won't be enabled.
3661 */
3662 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3663
3664 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3665 else if (udev->actconfig)
3666 usb_set_device_initiated_lpm(udev, state, true);
3667
3668}
3669
3670/*
3671 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3672 * U1/U2 entry.
3673 *
3674 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3675 * If zero is returned, the parent will not allow the link to go into U1/U2.
3676 *
3677 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3678 * it won't have an effect on the bus link state because the parent hub will
3679 * still disallow device-initiated U1/U2 entry.
3680 *
3681 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3682 * possible. The result will be slightly more bus bandwidth will be taken up
3683 * (to account for U1/U2 exit latency), but it should be harmless.
3684 */
3685static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3686 enum usb3_link_state state)
3687{
3688 int feature;
3689
3690 switch (state) {
3691 case USB3_LPM_U1:
3692 feature = USB_PORT_FEAT_U1_TIMEOUT;
3693 break;
3694 case USB3_LPM_U2:
3695 feature = USB_PORT_FEAT_U2_TIMEOUT;
3696 break;
3697 default:
3698 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3699 __func__);
3700 return -EINVAL;
3701 }
3702
3703 if (usb_set_lpm_timeout(udev, state, 0))
3704 return -EBUSY;
3705
3706 usb_set_device_initiated_lpm(udev, state, false);
3707
3708 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3709 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3710 "bus schedule bandwidth may be impacted.\n",
3711 usb3_lpm_names[state]);
3712 return 0;
3713}
3714
3715/*
3716 * Disable hub-initiated and device-initiated U1 and U2 entry.
3717 * Caller must own the bandwidth_mutex.
3718 *
3719 * This will call usb_enable_lpm() on failure, which will decrement
3720 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3721 */
3722int usb_disable_lpm(struct usb_device *udev)
3723{
3724 struct usb_hcd *hcd;
3725
3726 if (!udev || !udev->parent ||
3727 udev->speed != USB_SPEED_SUPER ||
3728 !udev->lpm_capable)
3729 return 0;
3730
3731 hcd = bus_to_hcd(udev->bus);
3732 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3733 return 0;
3734
3735 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003736 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003737 return 0;
3738
3739 /* If LPM is enabled, attempt to disable it. */
3740 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3741 goto enable_lpm;
3742 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3743 goto enable_lpm;
3744
3745 return 0;
3746
3747enable_lpm:
3748 usb_enable_lpm(udev);
3749 return -EBUSY;
3750}
3751EXPORT_SYMBOL_GPL(usb_disable_lpm);
3752
3753/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3754int usb_unlocked_disable_lpm(struct usb_device *udev)
3755{
3756 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3757 int ret;
3758
3759 if (!hcd)
3760 return -EINVAL;
3761
3762 mutex_lock(hcd->bandwidth_mutex);
3763 ret = usb_disable_lpm(udev);
3764 mutex_unlock(hcd->bandwidth_mutex);
3765
3766 return ret;
3767}
3768EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3769
3770/*
3771 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3772 * xHCI host policy may prevent U1 or U2 from being enabled.
3773 *
3774 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3775 * until the lpm_disable_count drops to zero. Caller must own the
3776 * bandwidth_mutex.
3777 */
3778void usb_enable_lpm(struct usb_device *udev)
3779{
3780 struct usb_hcd *hcd;
3781
3782 if (!udev || !udev->parent ||
3783 udev->speed != USB_SPEED_SUPER ||
3784 !udev->lpm_capable)
3785 return;
3786
3787 udev->lpm_disable_count--;
3788 hcd = bus_to_hcd(udev->bus);
3789 /* Double check that we can both enable and disable LPM.
3790 * Device must be configured to accept set feature U1/U2 timeout.
3791 */
3792 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3793 !hcd->driver->disable_usb3_lpm_timeout)
3794 return;
3795
3796 if (udev->lpm_disable_count > 0)
3797 return;
3798
3799 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3800 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3801}
3802EXPORT_SYMBOL_GPL(usb_enable_lpm);
3803
3804/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3805void usb_unlocked_enable_lpm(struct usb_device *udev)
3806{
3807 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3808
3809 if (!hcd)
3810 return;
3811
3812 mutex_lock(hcd->bandwidth_mutex);
3813 usb_enable_lpm(udev);
3814 mutex_unlock(hcd->bandwidth_mutex);
3815}
3816EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3817
3818
Alan Sternd388dab2006-07-01 22:14:24 -04003819#else /* CONFIG_PM */
3820
Alan Sternf07600c2007-05-30 15:38:16 -04003821#define hub_suspend NULL
3822#define hub_resume NULL
3823#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003824
3825int usb_disable_lpm(struct usb_device *udev)
3826{
3827 return 0;
3828}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003829EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003830
3831void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003832EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003833
3834int usb_unlocked_disable_lpm(struct usb_device *udev)
3835{
3836 return 0;
3837}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003838EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003839
3840void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003841EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003842
3843int usb_disable_ltm(struct usb_device *udev)
3844{
3845 return 0;
3846}
3847EXPORT_SYMBOL_GPL(usb_disable_ltm);
3848
3849void usb_enable_ltm(struct usb_device *udev) { }
3850EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003851#endif
3852
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
3854/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3855 *
3856 * Between connect detection and reset signaling there must be a delay
3857 * of 100ms at least for debounce and power-settling. The corresponding
3858 * timer shall restart whenever the downstream port detects a disconnect.
3859 *
3860 * Apparently there are some bluetooth and irda-dongles and a number of
3861 * low-speed devices for which this debounce period may last over a second.
3862 * Not covered by the spec - but easy to deal with.
3863 *
3864 * This implementation uses a 1500ms total debounce timeout; if the
3865 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3866 * every 25ms for transient disconnects. When the port status has been
3867 * unchanged for 100ms it returns the port status.
3868 */
Lan Tianyuad493e52013-01-23 04:26:30 +08003869int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
3871 int ret;
3872 int total_time, stable_time = 0;
3873 u16 portchange, portstatus;
3874 unsigned connection = 0xffff;
3875
3876 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3877 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3878 if (ret < 0)
3879 return ret;
3880
3881 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3882 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003883 if (!must_be_connected ||
3884 (connection == USB_PORT_STAT_CONNECTION))
3885 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 if (stable_time >= HUB_DEBOUNCE_STABLE)
3887 break;
3888 } else {
3889 stable_time = 0;
3890 connection = portstatus & USB_PORT_STAT_CONNECTION;
3891 }
3892
3893 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003894 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 USB_PORT_FEAT_C_CONNECTION);
3896 }
3897
3898 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3899 break;
3900 msleep(HUB_DEBOUNCE_STEP);
3901 }
3902
3903 dev_dbg (hub->intfdev,
3904 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3905 port1, total_time, stable_time, portstatus);
3906
3907 if (stable_time < HUB_DEBOUNCE_STABLE)
3908 return -ETIMEDOUT;
3909 return portstatus;
3910}
3911
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003912void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913{
Alan Sternddeac4e72009-01-15 17:03:33 -05003914 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3915 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003916 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003918EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
3920#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3921#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3922
Alan Stern4326ed02007-07-30 17:08:43 -04003923static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
3925 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003926 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
Sarah Sharpc6515272009-04-27 19:57:26 -07003928 /*
3929 * The host controller will choose the device address,
3930 * instead of the core having chosen it earlier
3931 */
3932 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return -EINVAL;
3934 if (udev->state == USB_STATE_ADDRESS)
3935 return 0;
3936 if (udev->state != USB_STATE_DEFAULT)
3937 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003938 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003939 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003940 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003941 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3942 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3943 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003945 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003946 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003948 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 }
3950 return retval;
3951}
3952
3953/* Reset device, (re)assign address, get device descriptor.
3954 * Device connection must be stable, no more debouncing needed.
3955 * Returns device in USB_STATE_ADDRESS, except on error.
3956 *
3957 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003958 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 * newly detected device that is not accessible through any global
3960 * pointers, it's not necessary to lock the device.
3961 */
3962static int
3963hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3964 int retry_counter)
3965{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003966 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
3968 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003969 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 int i, j, retval;
3971 unsigned delay = HUB_SHORT_RESET_TIME;
3972 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003973 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003974 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975
3976 /* root hub ports have a slightly longer reset period
3977 * (from USB 2.0 spec, section 7.1.7.5)
3978 */
3979 if (!hdev->parent) {
3980 delay = HUB_ROOT_RESET_TIME;
3981 if (port1 == hdev->bus->otg_port)
3982 hdev->bus->b_hnp_enable = 0;
3983 }
3984
3985 /* Some low speed devices have problems with the quick delay, so */
3986 /* be a bit pessimistic with those devices. RHbug #23670 */
3987 if (oldspeed == USB_SPEED_LOW)
3988 delay = HUB_LONG_RESET_TIME;
3989
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003990 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Luben Tuikov07194ab2011-02-11 11:33:10 -08003992 /* Reset the device; full speed may morph to high speed */
3993 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003994 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003995 if (retval < 0) /* error or disconnect */
3996 goto fail;
3997 /* success, speed is known */
3998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 retval = -ENODEV;
4000
4001 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
4002 dev_dbg(&udev->dev, "device reset changed speed!\n");
4003 goto fail;
4004 }
4005 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4008 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004009 * For Wireless USB devices, ep0 max packet is always 512 (tho
4010 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 */
4012 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07004013 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08004014 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004015 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004016 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004018 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 break;
4020 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4021 /* to determine the ep0 maxpacket size, try to read
4022 * the device descriptor to get bMaxPacketSize0 and
4023 * then correct our initial guess.
4024 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004025 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 break;
4027 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004028 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 break;
4030 default:
4031 goto fail;
4032 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004033
4034 if (udev->speed == USB_SPEED_WIRELESS)
4035 speed = "variable speed Wireless";
4036 else
4037 speed = usb_speed_string(udev->speed);
4038
Sarah Sharpc6515272009-04-27 19:57:26 -07004039 if (udev->speed != USB_SPEED_SUPER)
4040 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004041 "%s %s USB device number %d using %s\n",
4042 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004043 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045 /* Set up TT records, if needed */
4046 if (hdev->tt) {
4047 udev->tt = hdev->tt;
4048 udev->ttport = hdev->ttport;
4049 } else if (udev->speed != USB_SPEED_HIGH
4050 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004051 if (!hub->tt.hub) {
4052 dev_err(&udev->dev, "parent hub has no TT\n");
4053 retval = -EINVAL;
4054 goto fail;
4055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 udev->tt = &hub->tt;
4057 udev->ttport = port1;
4058 }
4059
4060 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4061 * Because device hardware and firmware is sometimes buggy in
4062 * this area, and this is how Linux has done it for ages.
4063 * Change it cautiously.
4064 *
4065 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
4066 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4067 * so it may help with some non-standards-compliant devices.
4068 * Otherwise we start with SET_ADDRESS and then try to read the
4069 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4070 * value.
4071 */
4072 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07004073 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 struct usb_device_descriptor *buf;
4075 int r = 0;
4076
4077#define GET_DESCRIPTOR_BUFSIZE 64
4078 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4079 if (!buf) {
4080 retval = -ENOMEM;
4081 continue;
4082 }
4083
Alan Sternb89ee192007-05-11 10:19:04 -04004084 /* Retry on all errors; some devices are flakey.
4085 * 255 is for WUSB devices, we actually need to use
4086 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 */
4088 for (j = 0; j < 3; ++j) {
4089 buf->bMaxPacketSize0 = 0;
4090 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4091 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4092 USB_DT_DEVICE << 8, 0,
4093 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004094 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004096 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 if (buf->bDescriptorType ==
4098 USB_DT_DEVICE) {
4099 r = 0;
4100 break;
4101 }
4102 /* FALL THROUGH */
4103 default:
4104 if (r == 0)
4105 r = -EPROTO;
4106 break;
4107 }
4108 if (r == 0)
4109 break;
4110 }
4111 udev->descriptor.bMaxPacketSize0 =
4112 buf->bMaxPacketSize0;
4113 kfree(buf);
4114
Andiry Xu75d7cf72011-09-13 16:41:11 -07004115 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 if (retval < 0) /* error or disconnect */
4117 goto fail;
4118 if (oldspeed != udev->speed) {
4119 dev_dbg(&udev->dev,
4120 "device reset changed speed!\n");
4121 retval = -ENODEV;
4122 goto fail;
4123 }
4124 if (r) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004125 if (r != -ENODEV)
4126 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4127 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 retval = -EMSGSIZE;
4129 continue;
4130 }
4131#undef GET_DESCRIPTOR_BUFSIZE
4132 }
4133
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004134 /*
4135 * If device is WUSB, we already assigned an
4136 * unauthorized address in the Connect Ack sequence;
4137 * authorization will assign the final address.
4138 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004139 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004140 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4141 retval = hub_set_address(udev, devnum);
4142 if (retval >= 0)
4143 break;
4144 msleep(200);
4145 }
4146 if (retval < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004147 if (retval != -ENODEV)
4148 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4149 devnum, retval);
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004150 goto fail;
4151 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004152 if (udev->speed == USB_SPEED_SUPER) {
4153 devnum = udev->devnum;
4154 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004155 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004156 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004157 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004158 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004159
4160 /* cope with hardware quirkiness:
4161 * - let SET_ADDRESS settle, some device hardware wants it
4162 * - read ep0 maxpacket even for high and low speed,
4163 */
4164 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07004165 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 retval = usb_get_device_descriptor(udev, 8);
4170 if (retval < 8) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004171 if (retval != -ENODEV)
4172 dev_err(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004173 "device descriptor read/8, error %d\n",
4174 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 if (retval >= 0)
4176 retval = -EMSGSIZE;
4177 } else {
4178 retval = 0;
4179 break;
4180 }
4181 }
4182 if (retval)
4183 goto fail;
4184
Peter Chenb76baa82012-11-09 09:44:43 +08004185 if (hcd->phy && !hdev->parent)
Peter Chenac965112012-11-09 09:44:44 +08004186 usb_phy_notify_connect(hcd->phy, udev->speed);
Peter Chenb76baa82012-11-09 09:44:43 +08004187
Elric Fud8aec3d2012-03-26 21:16:02 +08004188 /*
4189 * Some superspeed devices have finished the link training process
4190 * and attached to a superspeed hub port, but the device descriptor
4191 * got from those devices show they aren't superspeed devices. Warm
4192 * reset the port attached by the devices can fix them.
4193 */
4194 if ((udev->speed == USB_SPEED_SUPER) &&
4195 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4196 dev_err(&udev->dev, "got a wrong device descriptor, "
4197 "warm reset device\n");
4198 hub_port_reset(hub, port1, udev,
4199 HUB_BH_RESET_TIME, true);
4200 retval = -EINVAL;
4201 goto fail;
4202 }
4203
Sarah Sharp6b403b02009-04-27 19:54:10 -07004204 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4205 udev->speed == USB_SPEED_SUPER)
4206 i = 512;
4207 else
4208 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004209 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004210 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004212 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 retval = -EMSGSIZE;
4214 goto fail;
4215 }
Alan Stern56626a72010-10-14 15:25:21 -04004216 if (udev->speed == USB_SPEED_FULL)
4217 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4218 else
4219 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004221 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 }
4223
4224 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4225 if (retval < (signed)sizeof(udev->descriptor)) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004226 if (retval != -ENODEV)
4227 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4228 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 if (retval >= 0)
4230 retval = -ENOMSG;
4231 goto fail;
4232 }
4233
Andiry Xu1ff4df52011-09-23 14:19:48 -07004234 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4235 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004236 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004237 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004238 usb_set_lpm_parameters(udev);
4239 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004240 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004243 /* notify HCD that we have a device connected and addressed */
4244 if (hcd->driver->update_device)
4245 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004247 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004249 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004250 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004251 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 return retval;
4253}
4254
4255static void
4256check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4257{
4258 struct usb_qualifier_descriptor *qual;
4259 int status;
4260
Christoph Lametere94b1762006-12-06 20:33:17 -08004261 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 if (qual == NULL)
4263 return;
4264
4265 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4266 qual, sizeof *qual);
4267 if (status == sizeof *qual) {
4268 dev_info(&udev->dev, "not running at top speed; "
4269 "connect to a high speed hub\n");
4270 /* hub LEDs are probably harder to miss than syslog */
4271 if (hub->has_indicators) {
4272 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004273 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 }
4275 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004276 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
4279static unsigned
4280hub_power_remaining (struct usb_hub *hub)
4281{
4282 struct usb_device *hdev = hub->hdev;
4283 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004284 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Alan Stern55c52712005-11-23 12:03:12 -05004286 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 return 0;
4288
Alan Stern55c52712005-11-23 12:03:12 -05004289 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4290 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08004291 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004292 int delta;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004293 unsigned unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
4295 if (!udev)
4296 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004297 if (hub_is_superspeed(udev))
4298 unit_load = 150;
4299 else
4300 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004302 /*
4303 * Unconfigured devices may not use more than one unit load,
4304 * or 8mA for OTG ports
4305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004307 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004308 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004309 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 else
Alan Stern55c52712005-11-23 12:03:12 -05004311 delta = 8;
4312 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004313 dev_warn(&udev->dev,
4314 "%dmA is over %umA budget for port %d!\n",
4315 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 remaining -= delta;
4317 }
4318 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004319 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4320 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 remaining = 0;
4322 }
4323 return remaining;
4324}
4325
4326/* Handle physical or logical connection change events.
4327 * This routine is called when:
4328 * a port connection-change occurs;
4329 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004330 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 * a firmware download)
4332 * caller already locked the hub
4333 */
4334static void hub_port_connect_change(struct usb_hub *hub, int port1,
4335 u16 portstatus, u16 portchange)
4336{
4337 struct usb_device *hdev = hub->hdev;
4338 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304339 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004340 unsigned wHubCharacteristics =
4341 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004342 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 int status, i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004344 unsigned unit_load;
Alan Stern24618b02008-04-28 11:06:28 -04004345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 dev_dbg (hub_dev,
4347 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004348 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 if (hub->has_indicators) {
4351 set_port_led(hub, port1, HUB_LED_AUTO);
4352 hub->indicator[port1-1] = INDICATOR_AUTO;
4353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
4355#ifdef CONFIG_USB_OTG
4356 /* during HNP, don't repeat the debounce */
4357 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004358 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4359 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360#endif
4361
Alan Stern8808f002008-04-28 11:06:55 -04004362 /* Try to resuscitate an existing device */
Lan Tianyuff823c72012-09-05 13:44:32 +08004363 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004364 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4365 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004366 usb_lock_device(udev);
4367 if (portstatus & USB_PORT_STAT_ENABLE) {
4368 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004369
Alan Stern84ebc102013-03-27 16:14:46 -04004370#ifdef CONFIG_PM_RUNTIME
Alan Stern5257d972008-09-22 14:43:08 -04004371 } else if (udev->state == USB_STATE_SUSPENDED &&
4372 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004373 /* For a suspended device, treat this as a
4374 * remote wakeup event.
4375 */
Alan Stern0534d462010-01-08 12:56:30 -05004376 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004377#endif
4378
4379 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004380 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004381 }
4382 usb_unlock_device(udev);
4383
4384 if (status == 0) {
4385 clear_bit(port1, hub->change_bits);
4386 return;
4387 }
4388 }
4389
Alan Stern24618b02008-04-28 11:06:28 -04004390 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004391 if (udev) {
4392 if (hcd->phy && !hdev->parent &&
4393 !(portstatus & USB_PORT_STAT_CONNECTION))
Peter Chenac965112012-11-09 09:44:44 +08004394 usb_phy_notify_disconnect(hcd->phy, udev->speed);
Lan Tianyuff823c72012-09-05 13:44:32 +08004395 usb_disconnect(&hub->ports[port1 - 1]->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004396 }
Alan Stern24618b02008-04-28 11:06:28 -04004397 clear_bit(port1, hub->change_bits);
4398
Alan Stern253e0572009-10-27 15:20:13 -04004399 /* We can forget about a "removed" device when there's a physical
4400 * disconnect or the connect status changes.
4401 */
4402 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4403 (portchange & USB_PORT_STAT_C_CONNECTION))
4404 clear_bit(port1, hub->removed_bits);
4405
Alan Stern5257d972008-09-22 14:43:08 -04004406 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4407 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004408 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004409 if (status < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004410 if (status != -ENODEV && printk_ratelimit())
Alan Stern5257d972008-09-22 14:43:08 -04004411 dev_err(hub_dev, "connect-debounce failed, "
4412 "port %d disabled\n", port1);
4413 portstatus &= ~USB_PORT_STAT_CONNECTION;
4414 } else {
4415 portstatus = status;
4416 }
4417 }
4418
Alan Stern253e0572009-10-27 15:20:13 -04004419 /* Return now if debouncing failed or nothing is connected or
4420 * the device was "removed".
4421 */
4422 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4423 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004426 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004427 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (portstatus & USB_PORT_STAT_ENABLE)
4431 goto done;
4432 return;
4433 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004434 if (hub_is_superspeed(hub->hdev))
4435 unit_load = 150;
4436 else
4437 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
Alan Sterne9e88fb2013-03-27 16:14:01 -04004439 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
4442 /* reallocate for each attempt, since references
4443 * to the previous one can escape in various ways
4444 */
4445 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4446 if (!udev) {
4447 dev_err (hub_dev,
4448 "couldn't allocate port %d usb_device\n",
4449 port1);
4450 goto done;
4451 }
4452
4453 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004454 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004455 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004456 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004457
Sarah Sharp131dec32010-12-06 21:00:19 -08004458 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4459 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004460 udev->speed = USB_SPEED_SUPER;
4461 else
4462 udev->speed = USB_SPEED_UNKNOWN;
4463
Alan Stern3b29b682011-02-22 09:53:41 -05004464 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004465 if (udev->devnum <= 0) {
4466 status = -ENOTCONN; /* Don't retry */
4467 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004468 }
4469
Sarah Sharpe7b77172009-04-27 19:54:26 -07004470 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 status = hub_port_init(hub, udev, port1, i);
4472 if (status < 0)
4473 goto loop;
4474
Phil Dibowitz93362a82010-07-22 00:05:01 +02004475 usb_detect_quirks(udev);
4476 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4477 msleep(1000);
4478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 /* consecutive bus-powered hubs aren't reliable; they can
4480 * violate the voltage drop budget. if the new child has
4481 * a "powered" LED, users should notice we didn't enable it
4482 * (without reading syslog), even without per-port LEDs
4483 * on the parent.
4484 */
4485 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004486 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 u16 devstat;
4488
4489 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4490 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004491 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 dev_dbg(&udev->dev, "get status %d ?\n", status);
4493 goto loop_disable;
4494 }
Alan Stern55c52712005-11-23 12:03:12 -05004495 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4497 dev_err(&udev->dev,
4498 "can't connect bus-powered hub "
4499 "to this port\n");
4500 if (hub->has_indicators) {
4501 hub->indicator[port1-1] =
4502 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004503 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 }
4505 status = -ENOTCONN; /* Don't retry */
4506 goto loop_disable;
4507 }
4508 }
4509
4510 /* check for devices running slower than they could */
4511 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4512 && udev->speed == USB_SPEED_FULL
4513 && highspeed_hubs != 0)
4514 check_highspeed (hub, udev, port1);
4515
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004516 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 * udev becomes globally accessible, although presumably
4518 * no one will look at it until hdev is unlocked.
4519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 status = 0;
4521
4522 /* We mustn't add new devices if the parent hub has
4523 * been disconnected; we would race with the
4524 * recursively_mark_NOTATTACHED() routine.
4525 */
4526 spin_lock_irq(&device_state_lock);
4527 if (hdev->state == USB_STATE_NOTATTACHED)
4528 status = -ENOTCONN;
4529 else
Lan Tianyuff823c72012-09-05 13:44:32 +08004530 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 spin_unlock_irq(&device_state_lock);
4532
4533 /* Run it through the hoops (find a driver, etc) */
4534 if (!status) {
4535 status = usb_new_device(udev);
4536 if (status) {
4537 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c72012-09-05 13:44:32 +08004538 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 spin_unlock_irq(&device_state_lock);
4540 }
4541 }
4542
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 if (status)
4544 goto loop_disable;
4545
4546 status = hub_power_remaining(hub);
4547 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004548 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
4550 return;
4551
4552loop_disable:
4553 hub_port_disable(hub, port1, 1);
4554loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004555 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004556 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004557 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004559 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 break;
4561 }
Alan Stern3a311552008-05-20 16:58:29 -04004562 if (hub->hdev->parent ||
4563 !hcd->driver->port_handed_over ||
Alan Sterne9e88fb2013-03-27 16:14:01 -04004564 !(hcd->driver->port_handed_over)(hcd, port1)) {
4565 if (status != -ENOTCONN && status != -ENODEV)
4566 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4567 port1);
4568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
4570done:
4571 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304572 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4573 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574}
4575
Sarah Sharp714b07b2012-01-24 13:53:18 -08004576/* Returns 1 if there was a remote wakeup and a connect status change. */
4577static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004578 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004579{
4580 struct usb_device *hdev;
4581 struct usb_device *udev;
4582 int connect_change = 0;
4583 int ret;
4584
4585 hdev = hub->hdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08004586 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004587 if (!hub_is_superspeed(hdev)) {
4588 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4589 return 0;
Lan Tianyuad493e52013-01-23 04:26:30 +08004590 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004591 } else {
4592 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004593 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4594 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004595 return 0;
4596 }
4597
Sarah Sharp714b07b2012-01-24 13:53:18 -08004598 if (udev) {
4599 /* TRSMRCY = 10 msec */
4600 msleep(10);
4601
4602 usb_lock_device(udev);
4603 ret = usb_remote_wakeup(udev);
4604 usb_unlock_device(udev);
4605 if (ret < 0)
4606 connect_change = 1;
4607 } else {
4608 ret = -ENODEV;
4609 hub_port_disable(hub, port, 1);
4610 }
4611 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4612 port, ret);
4613 return connect_change;
4614}
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616static void hub_events(void)
4617{
4618 struct list_head *tmp;
4619 struct usb_device *hdev;
4620 struct usb_interface *intf;
4621 struct usb_hub *hub;
4622 struct device *hub_dev;
4623 u16 hubstatus;
4624 u16 hubchange;
4625 u16 portstatus;
4626 u16 portchange;
4627 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004628 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
4630 /*
4631 * We restart the list every time to avoid a deadlock with
4632 * deleting hubs downstream from this one. This should be
4633 * safe since we delete the hub from the event list.
4634 * Not the most efficient, but avoids deadlocks.
4635 */
4636 while (1) {
4637
4638 /* Grab the first entry at the beginning of the list */
4639 spin_lock_irq(&hub_event_lock);
4640 if (list_empty(&hub_event_list)) {
4641 spin_unlock_irq(&hub_event_lock);
4642 break;
4643 }
4644
4645 tmp = hub_event_list.next;
4646 list_del_init(tmp);
4647
4648 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004649 kref_get(&hub->kref);
4650 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651
Alan Sterne8054852007-05-04 11:55:11 -04004652 hdev = hub->hdev;
4653 hub_dev = hub->intfdev;
4654 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004655 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 hdev->state, hub->descriptor
4657 ? hub->descriptor->bNbrPorts
4658 : 0,
4659 /* NOTE: expects max 15 ports... */
4660 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004661 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 /* Lock the device, then check to see if we were
4664 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004665 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004666 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004667 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 /* If the hub has died, clean up after it */
4670 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004671 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004672 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 goto loop;
4674 }
4675
Alan Stern40f122f2006-11-09 14:44:33 -05004676 /* Autoresume */
4677 ret = usb_autopm_get_interface(intf);
4678 if (ret) {
4679 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004681 }
4682
4683 /* If this is an inactive hub, do nothing */
4684 if (hub->quiescing)
4685 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
4687 if (hub->error) {
4688 dev_dbg (hub_dev, "resetting for error %d\n",
4689 hub->error);
4690
Ming Lei742120c2008-06-18 22:00:29 +08004691 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 if (ret) {
4693 dev_dbg (hub_dev,
4694 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004695 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 }
4697
4698 hub->nerrors = 0;
4699 hub->error = 0;
4700 }
4701
4702 /* deal with port status changes */
4703 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4704 if (test_bit(i, hub->busy_bits))
4705 continue;
4706 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004707 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004709 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 continue;
4711
4712 ret = hub_port_status(hub, i,
4713 &portstatus, &portchange);
4714 if (ret < 0)
4715 continue;
4716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004718 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 USB_PORT_FEAT_C_CONNECTION);
4720 connect_change = 1;
4721 }
4722
4723 if (portchange & USB_PORT_STAT_C_ENABLE) {
4724 if (!connect_change)
4725 dev_dbg (hub_dev,
4726 "port %d enable change, "
4727 "status %08x\n",
4728 i, portstatus);
Lan Tianyuad493e52013-01-23 04:26:30 +08004729 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 USB_PORT_FEAT_C_ENABLE);
4731
4732 /*
4733 * EM interference sometimes causes badly
4734 * shielded USB devices to be shutdown by
4735 * the hub, this hack enables them again.
4736 * Works at least with mouse driver.
4737 */
4738 if (!(portstatus & USB_PORT_STAT_ENABLE)
4739 && !connect_change
Lan Tianyuff823c72012-09-05 13:44:32 +08004740 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 dev_err (hub_dev,
4742 "port %i "
4743 "disabled by hub (EMI?), "
4744 "re-enabling...\n",
4745 i);
4746 connect_change = 1;
4747 }
4748 }
4749
Sarah Sharp72937e12012-01-24 11:46:50 -08004750 if (hub_handle_remote_wakeup(hub, i,
4751 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004752 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004755 u16 status = 0;
4756 u16 unused;
4757
4758 dev_dbg(hub_dev, "over-current change on port "
4759 "%d\n", i);
Lan Tianyuad493e52013-01-23 04:26:30 +08004760 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004762 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004763 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004764 hub_port_status(hub, i, &status, &unused);
4765 if (status & USB_PORT_STAT_OVERCURRENT)
4766 dev_err(hub_dev, "over-current "
4767 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 }
4769
4770 if (portchange & USB_PORT_STAT_C_RESET) {
4771 dev_dbg (hub_dev,
4772 "reset change on port %d\n",
4773 i);
Lan Tianyuad493e52013-01-23 04:26:30 +08004774 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 USB_PORT_FEAT_C_RESET);
4776 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004777 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4778 hub_is_superspeed(hub->hdev)) {
4779 dev_dbg(hub_dev,
4780 "warm reset change on port %d\n",
4781 i);
Lan Tianyuad493e52013-01-23 04:26:30 +08004782 usb_clear_port_feature(hdev, i,
Sarah Sharpc7061572010-12-06 15:08:20 -08004783 USB_PORT_FEAT_C_BH_PORT_RESET);
4784 }
John Youndbe79bb2001-09-17 00:00:00 -07004785 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004786 usb_clear_port_feature(hub->hdev, i,
John Youndbe79bb2001-09-17 00:00:00 -07004787 USB_PORT_FEAT_C_PORT_LINK_STATE);
4788 }
4789 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4790 dev_warn(hub_dev,
4791 "config error on port %d\n",
4792 i);
Lan Tianyuad493e52013-01-23 04:26:30 +08004793 usb_clear_port_feature(hub->hdev, i,
John Youndbe79bb2001-09-17 00:00:00 -07004794 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Andiry Xu5e467f62011-04-27 18:07:54 +08004797 /* Warm reset a USB3 protocol port if it's in
4798 * SS.Inactive state.
4799 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004800 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp65bdac52012-11-14 17:58:04 -08004801 int status;
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004802 struct usb_device *udev =
4803 hub->ports[i - 1]->child;
Sarah Sharp65bdac52012-11-14 17:58:04 -08004804
Andiry Xu5e467f62011-04-27 18:07:54 +08004805 dev_dbg(hub_dev, "warm reset port %d\n", i);
Julius Werner6b9cb242013-07-30 19:51:20 -07004806 if (!udev || !(portstatus &
4807 USB_PORT_STAT_CONNECTION)) {
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004808 status = hub_port_reset(hub, i,
4809 NULL, HUB_BH_RESET_TIME,
4810 true);
4811 if (status < 0)
4812 hub_port_disable(hub, i, 1);
4813 } else {
4814 usb_lock_device(udev);
4815 status = usb_reset_device(udev);
4816 usb_unlock_device(udev);
Julius Werner6b9cb242013-07-30 19:51:20 -07004817 connect_change = 0;
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004818 }
Andiry Xu5e467f62011-04-27 18:07:54 +08004819 }
4820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 if (connect_change)
4822 hub_port_connect_change(hub, i,
4823 portstatus, portchange);
4824 } /* end for i */
4825
4826 /* deal with hub status changes */
4827 if (test_and_clear_bit(0, hub->event_bits) == 0)
4828 ; /* do nothing */
4829 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4830 dev_err (hub_dev, "get_hub_status failed\n");
4831 else {
4832 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4833 dev_dbg (hub_dev, "power change\n");
4834 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004835 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4836 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004837 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004838 else
4839 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 }
4841 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004842 u16 status = 0;
4843 u16 unused;
4844
4845 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004847 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004848 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004849 hub_hub_status(hub, &status, &unused);
4850 if (status & HUB_STATUS_OVERCURRENT)
4851 dev_err(hub_dev, "over-current "
4852 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 }
4854 }
4855
Alan Stern8e4ceb32009-12-07 13:01:37 -05004856 loop_autopm:
4857 /* Balance the usb_autopm_get_interface() above */
4858 usb_autopm_put_interface_no_suspend(intf);
4859 loop:
4860 /* Balance the usb_autopm_get_interface_no_resume() in
4861 * kick_khubd() and allow autosuspend.
4862 */
4863 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004864 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004866 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
4868 } /* end while (1) */
4869}
4870
4871static int hub_thread(void *__unused)
4872{
Alan Stern3bb1af52008-03-03 15:15:36 -05004873 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4874 * port handover. Otherwise it might see that a full-speed device
4875 * was gone before the EHCI controller had handed its port over to
4876 * the companion full-speed controller.
4877 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004878 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 do {
4881 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004882 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004883 !list_empty(&hub_event_list) ||
4884 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004885 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004887 pr_debug("%s: khubd exiting\n", usbcore_name);
4888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889}
4890
Németh Márton1e927d92010-01-10 15:34:53 +01004891static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08004892 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4893 | USB_DEVICE_ID_MATCH_INT_CLASS,
4894 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4895 .bInterfaceClass = USB_CLASS_HUB,
4896 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4898 .bDeviceClass = USB_CLASS_HUB},
4899 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4900 .bInterfaceClass = USB_CLASS_HUB},
4901 { } /* Terminating entry */
4902};
4903
4904MODULE_DEVICE_TABLE (usb, hub_id_table);
4905
4906static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 .name = "hub",
4908 .probe = hub_probe,
4909 .disconnect = hub_disconnect,
4910 .suspend = hub_suspend,
4911 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004912 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004913 .pre_reset = hub_pre_reset,
4914 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004915 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004917 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918};
4919
4920int usb_hub_init(void)
4921{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 if (usb_register(&hub_driver) < 0) {
4923 printk(KERN_ERR "%s: can't register hub driver\n",
4924 usbcore_name);
4925 return -1;
4926 }
4927
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004928 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4929 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931
4932 /* Fall through if kernel_thread failed */
4933 usb_deregister(&hub_driver);
4934 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4935
4936 return -1;
4937}
4938
4939void usb_hub_cleanup(void)
4940{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004941 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 /*
4944 * Hub resources are freed for us by usb_deregister. It calls
4945 * usb_driver_purge on every device which in turn calls that
4946 * devices disconnect function if it is using this driver.
4947 * The hub_disconnect function takes care of releasing the
4948 * individual hub resources. -greg
4949 */
4950 usb_deregister(&hub_driver);
4951} /* usb_hub_cleanup() */
4952
Alan Sterneb764c42008-03-03 15:16:04 -05004953static int descriptors_changed(struct usb_device *udev,
4954 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955{
Alan Sterneb764c42008-03-03 15:16:04 -05004956 int changed = 0;
4957 unsigned index;
4958 unsigned serial_len = 0;
4959 unsigned len;
4960 unsigned old_length;
4961 int length;
4962 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
Alan Sterneb764c42008-03-03 15:16:04 -05004964 if (memcmp(&udev->descriptor, old_device_descriptor,
4965 sizeof(*old_device_descriptor)) != 0)
4966 return 1;
4967
4968 /* Since the idVendor, idProduct, and bcdDevice values in the
4969 * device descriptor haven't changed, we will assume the
4970 * Manufacturer and Product strings haven't changed either.
4971 * But the SerialNumber string could be different (e.g., a
4972 * different flash card of the same brand).
4973 */
4974 if (udev->serial)
4975 serial_len = strlen(udev->serial) + 1;
4976
4977 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004979 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4980 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
Alan Sterneb764c42008-03-03 15:16:04 -05004982
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004983 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 if (buf == NULL) {
4985 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4986 /* assume the worst */
4987 return 1;
4988 }
4989 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004990 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4992 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004993 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 dev_dbg(&udev->dev, "config index %d, error %d\n",
4995 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004996 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 break;
4998 }
4999 if (memcmp (buf, udev->rawdescriptors[index], old_length)
5000 != 0) {
5001 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05005002 index,
5003 ((struct usb_config_descriptor *) buf)->
5004 bConfigurationValue);
5005 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 break;
5007 }
5008 }
Alan Sterneb764c42008-03-03 15:16:04 -05005009
5010 if (!changed && serial_len) {
5011 length = usb_string(udev, udev->descriptor.iSerialNumber,
5012 buf, serial_len);
5013 if (length + 1 != serial_len) {
5014 dev_dbg(&udev->dev, "serial string error %d\n",
5015 length);
5016 changed = 1;
5017 } else if (memcmp(buf, udev->serial, length) != 0) {
5018 dev_dbg(&udev->dev, "serial string changed\n");
5019 changed = 1;
5020 }
5021 }
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05005024 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025}
5026
5027/**
Ming Lei742120c2008-06-18 22:00:29 +08005028 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5030 *
Alan Stern79efa092006-06-01 13:33:42 -04005031 * WARNING - don't use this routine to reset a composite device
5032 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08005033 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 *
5035 * Do a port reset, reassign the device's address, and establish its
5036 * former operating configuration. If the reset fails, or the device's
5037 * descriptors change from their values before the reset, or the original
5038 * configuration and altsettings cannot be restored, a flag will be set
5039 * telling khubd to pretend the device has been disconnected and then
5040 * re-connected. All drivers will be unbound, and the device will be
5041 * re-enumerated and probed all over again.
5042 *
5043 * Returns 0 if the reset succeeded, -ENODEV if the device has been
5044 * flagged for logical disconnection, or some other negative error code
5045 * if the reset wasn't even attempted.
5046 *
5047 * The caller must own the device lock. For example, it's safe to use
5048 * this from a driver probe() routine after downloading new firmware.
5049 * For calls that might not occur during probe(), drivers should lock
5050 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04005051 *
5052 * Locking exception: This routine may also be called from within an
5053 * autoresume handler. Such usage won't conflict with other tasks
5054 * holding the device lock because these tasks should always call
5055 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 */
Ming Lei742120c2008-06-18 22:00:29 +08005057static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
5059 struct usb_device *parent_hdev = udev->parent;
5060 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005061 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05005063 int i, ret = 0;
5064 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066 if (udev->state == USB_STATE_NOTATTACHED ||
5067 udev->state == USB_STATE_SUSPENDED) {
5068 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5069 udev->state);
5070 return -EINVAL;
5071 }
5072
5073 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02005074 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08005075 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 return -EISDIR;
5077 }
Lan Tianyuad493e52013-01-23 04:26:30 +08005078 parent_hub = usb_hub_to_struct_hub(parent_hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
Sarah Sharpf74631e2012-06-25 12:08:08 -07005080 /* Disable LPM and LTM while we reset the device and reinstall the alt
5081 * settings. Device-initiated LPM settings, and system exit latency
5082 * settings are cleared when the device is reset, so we have to set
5083 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005084 */
5085 ret = usb_unlocked_disable_lpm(udev);
5086 if (ret) {
5087 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
5088 goto re_enumerate;
5089 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07005090 ret = usb_disable_ltm(udev);
5091 if (ret) {
5092 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5093 __func__);
5094 goto re_enumerate;
5095 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 set_bit(port1, parent_hub->busy_bits);
5098 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5099
5100 /* ep0 maxpacket size may change; let the HCD know about it.
5101 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07005102 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04005104 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 break;
5106 }
5107 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04005108
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 if (ret < 0)
5110 goto re_enumerate;
5111
5112 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05005113 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 dev_info(&udev->dev, "device firmware changed\n");
5115 udev->descriptor = descriptor; /* for disconnect() calls */
5116 goto re_enumerate;
5117 }
Alan Stern4fe03872009-02-26 10:21:02 -05005118
5119 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 if (!udev->actconfig)
5121 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005122
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005123 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005124 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5125 if (ret < 0) {
5126 dev_warn(&udev->dev,
5127 "Busted HC? Not enough HCD resources for "
5128 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005129 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005130 goto re_enumerate;
5131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5133 USB_REQ_SET_CONFIGURATION, 0,
5134 udev->actconfig->desc.bConfigurationValue, 0,
5135 NULL, 0, USB_CTRL_SET_TIMEOUT);
5136 if (ret < 0) {
5137 dev_err(&udev->dev,
5138 "can't restore configuration #%d (error=%d)\n",
5139 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005140 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 goto re_enumerate;
5142 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005143 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5145
Alan Stern4fe03872009-02-26 10:21:02 -05005146 /* Put interfaces back into the same altsettings as before.
5147 * Don't bother to send the Set-Interface request for interfaces
5148 * that were already in altsetting 0; besides being unnecessary,
5149 * many devices can't handle it. Instead just reset the host-side
5150 * endpoint state.
5151 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005153 struct usb_host_config *config = udev->actconfig;
5154 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 struct usb_interface_descriptor *desc;
5156
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005158 if (desc->bAlternateSetting == 0) {
5159 usb_disable_interface(udev, intf, true);
5160 usb_enable_interface(udev, intf, true);
5161 ret = 0;
5162 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005163 /* Let the bandwidth allocation function know that this
5164 * device has been reset, and it will have to use
5165 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005166 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005167 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005168 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5169 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005170 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 if (ret < 0) {
5173 dev_err(&udev->dev, "failed to restore interface %d "
5174 "altsetting %d (error=%d)\n",
5175 desc->bInterfaceNumber,
5176 desc->bAlternateSetting,
5177 ret);
5178 goto re_enumerate;
5179 }
5180 }
5181
5182done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005183 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04005184 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005185 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 return 0;
5187
5188re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005189 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 hub_port_logical_disconnect(parent_hub, port1);
5191 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
5194/**
5195 * usb_reset_device - warn interface drivers and perform a USB port reset
5196 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5197 *
Alan Stern79efa092006-06-01 13:33:42 -04005198 * Warns all drivers bound to registered interfaces (using their pre_reset
5199 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005200 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005201 *
Alan Stern79efa092006-06-01 13:33:42 -04005202 * Return value is the same as for usb_reset_and_verify_device().
5203 *
5204 * The caller must own the device lock. For example, it's safe to use
5205 * this from a driver probe() routine after downloading new firmware.
5206 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005207 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005208 *
5209 * If an interface is currently being probed or disconnected, we assume
5210 * its driver knows how to handle resets. For all other interfaces,
5211 * if the driver doesn't have pre_reset and post_reset methods then
5212 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005213 */
Ming Lei742120c2008-06-18 22:00:29 +08005214int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005215{
5216 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005217 int i;
Ming Lei4d769de2013-02-22 16:34:22 -08005218 unsigned int noio_flag;
Alan Stern79efa092006-06-01 13:33:42 -04005219 struct usb_host_config *config = udev->actconfig;
5220
5221 if (udev->state == USB_STATE_NOTATTACHED ||
5222 udev->state == USB_STATE_SUSPENDED) {
5223 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5224 udev->state);
5225 return -EINVAL;
5226 }
5227
Ming Lei4d769de2013-02-22 16:34:22 -08005228 /*
5229 * Don't allocate memory with GFP_KERNEL in current
5230 * context to avoid possible deadlock if usb mass
5231 * storage interface or usbnet interface(iSCSI case)
5232 * is included in current configuration. The easist
5233 * approach is to do it for every device reset,
5234 * because the device 'memalloc_noio' flag may have
5235 * not been set before reseting the usb device.
5236 */
5237 noio_flag = memalloc_noio_save();
5238
Alan Stern645daaa2006-08-30 15:47:02 -04005239 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005240 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005241
Alan Stern79efa092006-06-01 13:33:42 -04005242 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005243 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005244 struct usb_interface *cintf = config->interface[i];
5245 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005246 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005247
5248 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005249 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005250 if (drv->pre_reset && drv->post_reset)
5251 unbind = (drv->pre_reset)(cintf);
5252 else if (cintf->condition ==
5253 USB_INTERFACE_BOUND)
5254 unbind = 1;
5255 if (unbind)
5256 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005257 }
5258 }
5259 }
5260
Ming Lei742120c2008-06-18 22:00:29 +08005261 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005262
5263 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005264 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005265 struct usb_interface *cintf = config->interface[i];
5266 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005267 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005268
Alan Stern78d9a482008-06-23 16:00:40 -04005269 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005270 drv = to_usb_driver(cintf->dev.driver);
5271 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005272 rebind = (drv->post_reset)(cintf);
5273 else if (cintf->condition ==
5274 USB_INTERFACE_BOUND)
5275 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005276 }
Alan Stern6c640942008-10-21 15:40:03 -04005277 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005278 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005279 }
5280 }
5281
Alan Stern94fcda12006-11-20 11:38:46 -05005282 usb_autosuspend_device(udev);
Ming Lei4d769de2013-02-22 16:34:22 -08005283 memalloc_noio_restore(noio_flag);
Alan Stern79efa092006-06-01 13:33:42 -04005284 return ret;
5285}
Ming Lei742120c2008-06-18 22:00:29 +08005286EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005287
5288
5289/**
5290 * usb_queue_reset_device - Reset a USB device from an atomic context
5291 * @iface: USB interface belonging to the device to reset
5292 *
5293 * This function can be used to reset a USB device from an atomic
5294 * context, where usb_reset_device() won't work (as it blocks).
5295 *
5296 * Doing a reset via this method is functionally equivalent to calling
5297 * usb_reset_device(), except for the fact that it is delayed to a
5298 * workqueue. This means that any drivers bound to other interfaces
5299 * might be unbound, as well as users from usbfs in user space.
5300 *
5301 * Corner cases:
5302 *
5303 * - Scheduling two resets at the same time from two different drivers
5304 * attached to two different interfaces of the same device is
5305 * possible; depending on how the driver attached to each interface
5306 * handles ->pre_reset(), the second reset might happen or not.
5307 *
5308 * - If a driver is unbound and it had a pending reset, the reset will
5309 * be cancelled.
5310 *
5311 * - This function can be called during .probe() or .disconnect()
5312 * times. On return from .disconnect(), any pending resets will be
5313 * cancelled.
5314 *
5315 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5316 * does its own.
5317 *
5318 * NOTE: We don't do any reference count tracking because it is not
5319 * needed. The lifecycle of the work_struct is tied to the
5320 * usb_interface. Before destroying the interface we cancel the
5321 * work_struct, so the fact that work_struct is queued and or
5322 * running means the interface (and thus, the device) exist and
5323 * are referenced.
5324 */
5325void usb_queue_reset_device(struct usb_interface *iface)
5326{
5327 schedule_work(&iface->reset_ws);
5328}
5329EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005330
5331/**
5332 * usb_hub_find_child - Get the pointer of child device
5333 * attached to the port which is specified by @port1.
5334 * @hdev: USB device belonging to the usb hub
5335 * @port1: port num to indicate which port the child device
5336 * is attached to.
5337 *
5338 * USB drivers call this function to get hub's child device
5339 * pointer.
5340 *
5341 * Return NULL if input param is invalid and
5342 * child's usb_device pointer if non-NULL.
5343 */
5344struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5345 int port1)
5346{
Lan Tianyuad493e52013-01-23 04:26:30 +08005347 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyuff823c72012-09-05 13:44:32 +08005348
5349 if (port1 < 1 || port1 > hdev->maxchild)
5350 return NULL;
5351 return hub->ports[port1 - 1]->child;
5352}
5353EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005354
Lan Tianyu05f91682012-09-05 13:44:34 +08005355/**
5356 * usb_set_hub_port_connect_type - set hub port connect type.
5357 * @hdev: USB device belonging to the usb hub
5358 * @port1: port num of the port
5359 * @type: connect type of the port
5360 */
5361void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5362 enum usb_port_connect_type type)
5363{
Lan Tianyuad493e52013-01-23 04:26:30 +08005364 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyu05f91682012-09-05 13:44:34 +08005365
5366 hub->ports[port1 - 1]->connect_type = type;
5367}
5368
5369/**
5370 * usb_get_hub_port_connect_type - Get the port's connect type
5371 * @hdev: USB device belonging to the usb hub
5372 * @port1: port num of the port
5373 *
5374 * Return connect type of the port and if input params are
5375 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5376 */
5377enum usb_port_connect_type
5378usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5379{
Lan Tianyuad493e52013-01-23 04:26:30 +08005380 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyu05f91682012-09-05 13:44:34 +08005381
5382 return hub->ports[port1 - 1]->connect_type;
5383}
5384
Lan Tianyud2123fd2013-01-21 22:18:00 +08005385void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5386 struct usb_hub_descriptor *desc)
5387{
5388 enum usb_port_connect_type connect_type;
5389 int i;
5390
5391 if (!hub_is_superspeed(hdev)) {
5392 for (i = 1; i <= hdev->maxchild; i++) {
5393 connect_type = usb_get_hub_port_connect_type(hdev, i);
5394
5395 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5396 u8 mask = 1 << (i%8);
5397
5398 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
5399 dev_dbg(&hdev->dev, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n",
5400 i);
5401 desc->u.hs.DeviceRemovable[i/8] |= mask;
5402 }
5403 }
5404 }
5405 } else {
5406 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5407
5408 for (i = 1; i <= hdev->maxchild; i++) {
5409 connect_type = usb_get_hub_port_connect_type(hdev, i);
5410
5411 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5412 u16 mask = 1 << i;
5413
5414 if (!(port_removable & mask)) {
5415 dev_dbg(&hdev->dev, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n",
5416 i);
5417 port_removable |= mask;
5418 }
5419 }
5420 }
5421
5422 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5423 }
5424}
5425
Lan Tianyud5575422012-09-05 13:44:33 +08005426#ifdef CONFIG_ACPI
5427/**
5428 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5429 * @hdev: USB device belonging to the usb hub
5430 * @port1: port num of the port
5431 *
5432 * Return port's acpi handle if successful, NULL if params are
5433 * invaild.
5434 */
5435acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5436 int port1)
5437{
Lan Tianyuad493e52013-01-23 04:26:30 +08005438 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud5575422012-09-05 13:44:33 +08005439
5440 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5441}
5442#endif