blob: 31a492a4a881195f1e5dfa4cc010edb5c44056b5 [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
Ming Leie6f30de2012-10-24 11:59:24 +080036#define USB_VENDOR_GENESYS_LOGIC 0x05e3
37#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
38
John Youndbe79bb2001-09-17 00:00:00 -070039static inline int hub_is_superspeed(struct usb_device *hdev)
40{
Aman Deep7bf01182011-12-08 12:05:22 +053041 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070042}
Alan Stern1bb5f662006-11-06 11:56:13 -050043
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070044/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050045 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
47static DEFINE_SPINLOCK(device_state_lock);
48
49/* khubd's worklist and its lock */
50static DEFINE_SPINLOCK(hub_event_lock);
51static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
52
53/* Wakes up khubd */
54static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
55
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070056static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dan Williamsd8521af2014-05-20 18:08:28 -070058/* synchronize hub-port add/remove and peering operations */
59DEFINE_MUTEX(usb_port_peer_mutex);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103062static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063module_param (blinkenlights, bool, S_IRUGO);
64MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
65
66/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020067 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
68 * 10 seconds to send reply for the initial 64-byte descriptor request.
69 */
70/* define initial 64-byte descriptor request timeout in milliseconds */
71static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
72module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +080073MODULE_PARM_DESC(initial_descriptor_timeout,
74 "initial 64-byte descriptor request timeout in milliseconds "
75 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +020076
77/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * As of 2.6.10 we introduce a new USB device initialization scheme which
79 * closely resembles the way Windows works. Hopefully it will be compatible
80 * with a wider range of devices than the old scheme. However some previously
81 * working devices may start giving rise to "device not accepting address"
82 * errors; if that happens the user can try the old scheme by adjusting the
83 * following module parameters.
84 *
85 * For maximum flexibility there are two boolean parameters to control the
86 * hub driver's behavior. On the first initialization attempt, if the
87 * "old_scheme_first" parameter is set then the old scheme will be used,
88 * otherwise the new scheme is used. If that fails and "use_both_schemes"
89 * is set, then the driver will make another attempt, using the other scheme.
90 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103091static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
93MODULE_PARM_DESC(old_scheme_first,
94 "start with the old device initialization scheme");
95
Rusty Russell90ab5ee2012-01-13 09:32:20 +103096static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
98MODULE_PARM_DESC(use_both_schemes,
99 "try the other device initialization scheme if the "
100 "first one fails");
101
Alan Stern32fe0192007-10-10 16:27:07 -0400102/* Mutual exclusion for EHCI CF initialization. This interferes with
103 * port reset on some companion controllers.
104 */
105DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
106EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
107
Lan Tianyuad493e52013-01-23 04:26:30 +0800108#define HUB_DEBOUNCE_TIMEOUT 2000
Alan Stern948fea32008-04-28 11:07:07 -0400109#define HUB_DEBOUNCE_STEP 25
110#define HUB_DEBOUNCE_STABLE 100
111
Ming Lei742120c2008-06-18 22:00:29 +0800112static int usb_reset_and_verify_device(struct usb_device *udev);
113
Sarah Sharp131dec32010-12-06 21:00:19 -0800114static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Sarah Sharp131dec32010-12-06 21:00:19 -0800116 if (hub_is_superspeed(hub->hdev))
117 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500118 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Matthias Beyer469271f2013-10-10 23:41:27 +0200119 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500120 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return "1.5 Mb/s";
122 else
123 return "12 Mb/s";
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126/* Note that hdev or one of its children must be locked! */
Lan Tianyuad493e52013-01-23 04:26:30 +0800127struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Lan Tianyuff823c792012-09-05 13:44:32 +0800129 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400130 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return usb_get_intfdata(hdev->actconfig->interface[0]);
132}
133
Sarah Sharp140e3022014-01-22 13:35:02 -0800134static int usb_device_supports_lpm(struct usb_device *udev)
Sarah Sharpd9b20992012-02-20 08:31:26 -0800135{
136 /* USB 2.1 (and greater) devices indicate LPM support through
137 * their USB 2.0 Extended Capabilities BOS descriptor.
138 */
139 if (udev->speed == USB_SPEED_HIGH) {
140 if (udev->bos->ext_cap &&
141 (USB_LPM_SUPPORT &
142 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
143 return 1;
144 return 0;
145 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800146
Sarah Sharp25cd2882014-01-17 14:15:44 -0800147 /*
148 * According to the USB 3.0 spec, all USB 3.0 devices must support LPM.
149 * However, there are some that don't, and they set the U1/U2 exit
150 * latencies to zero.
Sarah Sharp51e0a012012-02-20 12:02:19 -0800151 */
152 if (!udev->bos->ss_cap) {
Sarah Sharp25cd2882014-01-17 14:15:44 -0800153 dev_info(&udev->dev, "No LPM exit latency info found, disabling LPM.\n");
Sarah Sharp51e0a012012-02-20 12:02:19 -0800154 return 0;
155 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800156
Sarah Sharp25cd2882014-01-17 14:15:44 -0800157 if (udev->bos->ss_cap->bU1devExitLat == 0 &&
158 udev->bos->ss_cap->bU2DevExitLat == 0) {
159 if (udev->parent)
160 dev_info(&udev->dev, "LPM exit latency is zeroed, disabling LPM.\n");
161 else
162 dev_info(&udev->dev, "We don't know the algorithms for LPM for this host, disabling LPM.\n");
163 return 0;
164 }
165
166 if (!udev->parent || udev->parent->lpm_capable)
167 return 1;
Sarah Sharpd9b20992012-02-20 08:31:26 -0800168 return 0;
169}
170
Sarah Sharp51e0a012012-02-20 12:02:19 -0800171/*
172 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
173 * either U1 or U2.
174 */
175static void usb_set_lpm_mel(struct usb_device *udev,
176 struct usb3_lpm_parameters *udev_lpm_params,
177 unsigned int udev_exit_latency,
178 struct usb_hub *hub,
179 struct usb3_lpm_parameters *hub_lpm_params,
180 unsigned int hub_exit_latency)
181{
182 unsigned int total_mel;
183 unsigned int device_mel;
184 unsigned int hub_mel;
185
186 /*
187 * Calculate the time it takes to transition all links from the roothub
188 * to the parent hub into U0. The parent hub must then decode the
189 * packet (hub header decode latency) to figure out which port it was
190 * bound for.
191 *
192 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
193 * means 0.1us). Multiply that by 100 to get nanoseconds.
194 */
195 total_mel = hub_lpm_params->mel +
196 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
197
198 /*
199 * How long will it take to transition the downstream hub's port into
200 * U0? The greater of either the hub exit latency or the device exit
201 * latency.
202 *
203 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
204 * Multiply that by 1000 to get nanoseconds.
205 */
206 device_mel = udev_exit_latency * 1000;
207 hub_mel = hub_exit_latency * 1000;
208 if (device_mel > hub_mel)
209 total_mel += device_mel;
210 else
211 total_mel += hub_mel;
212
213 udev_lpm_params->mel = total_mel;
214}
215
216/*
217 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
218 * a transition from either U1 or U2.
219 */
220static void usb_set_lpm_pel(struct usb_device *udev,
221 struct usb3_lpm_parameters *udev_lpm_params,
222 unsigned int udev_exit_latency,
223 struct usb_hub *hub,
224 struct usb3_lpm_parameters *hub_lpm_params,
225 unsigned int hub_exit_latency,
226 unsigned int port_to_port_exit_latency)
227{
228 unsigned int first_link_pel;
229 unsigned int hub_pel;
230
231 /*
232 * First, the device sends an LFPS to transition the link between the
233 * device and the parent hub into U0. The exit latency is the bigger of
234 * the device exit latency or the hub exit latency.
235 */
236 if (udev_exit_latency > hub_exit_latency)
237 first_link_pel = udev_exit_latency * 1000;
238 else
239 first_link_pel = hub_exit_latency * 1000;
240
241 /*
242 * When the hub starts to receive the LFPS, there is a slight delay for
243 * it to figure out that one of the ports is sending an LFPS. Then it
244 * will forward the LFPS to its upstream link. The exit latency is the
245 * delay, plus the PEL that we calculated for this hub.
246 */
247 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
248
249 /*
250 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
251 * is the greater of the two exit latencies.
252 */
253 if (first_link_pel > hub_pel)
254 udev_lpm_params->pel = first_link_pel;
255 else
256 udev_lpm_params->pel = hub_pel;
257}
258
259/*
260 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
261 * when a device initiates a transition to U0, until when it will receive the
262 * first packet from the host controller.
263 *
264 * Section C.1.5.1 describes the four components to this:
265 * - t1: device PEL
266 * - t2: time for the ERDY to make it from the device to the host.
267 * - t3: a host-specific delay to process the ERDY.
268 * - t4: time for the packet to make it from the host to the device.
269 *
270 * t3 is specific to both the xHCI host and the platform the host is integrated
271 * into. The Intel HW folks have said it's negligible, FIXME if a different
272 * vendor says otherwise.
273 */
274static void usb_set_lpm_sel(struct usb_device *udev,
275 struct usb3_lpm_parameters *udev_lpm_params)
276{
277 struct usb_device *parent;
278 unsigned int num_hubs;
279 unsigned int total_sel;
280
281 /* t1 = device PEL */
282 total_sel = udev_lpm_params->pel;
283 /* How many external hubs are in between the device & the root port. */
284 for (parent = udev->parent, num_hubs = 0; parent->parent;
285 parent = parent->parent)
286 num_hubs++;
287 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
288 if (num_hubs > 0)
289 total_sel += 2100 + 250 * (num_hubs - 1);
290
291 /* t4 = 250ns * num_hubs */
292 total_sel += 250 * num_hubs;
293
294 udev_lpm_params->sel = total_sel;
295}
296
297static void usb_set_lpm_parameters(struct usb_device *udev)
298{
299 struct usb_hub *hub;
300 unsigned int port_to_port_delay;
301 unsigned int udev_u1_del;
302 unsigned int udev_u2_del;
303 unsigned int hub_u1_del;
304 unsigned int hub_u2_del;
305
306 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
307 return;
308
Lan Tianyuad493e52013-01-23 04:26:30 +0800309 hub = usb_hub_to_struct_hub(udev->parent);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800310 /* It doesn't take time to transition the roothub into U0, since it
311 * doesn't have an upstream link.
312 */
313 if (!hub)
314 return;
315
316 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300317 udev_u2_del = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800318 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
Xenia Ragiadakou4d967992013-08-31 18:09:13 +0300319 hub_u2_del = le16_to_cpu(udev->parent->bos->ss_cap->bU2DevExitLat);
Sarah Sharp51e0a012012-02-20 12:02:19 -0800320
321 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
322 hub, &udev->parent->u1_params, hub_u1_del);
323
324 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
325 hub, &udev->parent->u2_params, hub_u2_del);
326
327 /*
328 * Appendix C, section C.2.2.2, says that there is a slight delay from
329 * when the parent hub notices the downstream port is trying to
330 * transition to U0 to when the hub initiates a U0 transition on its
331 * upstream port. The section says the delays are tPort2PortU1EL and
332 * tPort2PortU2EL, but it doesn't define what they are.
333 *
334 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
335 * about the same delays. Use the maximum delay calculations from those
336 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
337 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
338 * assume the device exit latencies they are talking about are the hub
339 * exit latencies.
340 *
341 * What do we do if the U2 exit latency is less than the U1 exit
342 * latency? It's possible, although not likely...
343 */
344 port_to_port_delay = 1;
345
346 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
347 hub, &udev->parent->u1_params, hub_u1_del,
348 port_to_port_delay);
349
350 if (hub_u2_del > hub_u1_del)
351 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
352 else
353 port_to_port_delay = 1 + hub_u1_del;
354
355 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
356 hub, &udev->parent->u2_params, hub_u2_del,
357 port_to_port_delay);
358
359 /* Now that we've got PEL, calculate SEL. */
360 usb_set_lpm_sel(udev, &udev->u1_params);
361 usb_set_lpm_sel(udev, &udev->u2_params);
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700365static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
John Youndbe79bb2001-09-17 00:00:00 -0700367 int i, ret, size;
368 unsigned dtype;
369
370 if (hub_is_superspeed(hdev)) {
371 dtype = USB_DT_SS_HUB;
372 size = USB_DT_SS_HUB_SIZE;
373 } else {
374 dtype = USB_DT_HUB;
375 size = sizeof(struct usb_hub_descriptor);
376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 for (i = 0; i < 3; i++) {
379 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
380 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700381 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 USB_CTRL_GET_TIMEOUT);
383 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
384 return ret;
385 }
386 return -EINVAL;
387}
388
389/*
390 * USB 2.0 spec Section 11.24.2.1
391 */
392static int clear_hub_feature(struct usb_device *hdev, int feature)
393{
394 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
395 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
396}
397
398/*
399 * USB 2.0 spec Section 11.24.2.2
400 */
Lan Tianyuad493e52013-01-23 04:26:30 +0800401int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
404 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
405 NULL, 0, 1000);
406}
407
408/*
409 * USB 2.0 spec Section 11.24.2.13
410 */
411static int set_port_feature(struct usb_device *hdev, int port1, int feature)
412{
413 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
414 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
415 NULL, 0, 1000);
416}
417
Dan Williamsd99f6b42014-05-20 18:08:17 -0700418static char *to_led_name(int selector)
419{
420 switch (selector) {
421 case HUB_LED_AMBER:
422 return "amber";
423 case HUB_LED_GREEN:
424 return "green";
425 case HUB_LED_OFF:
426 return "off";
427 case HUB_LED_AUTO:
428 return "auto";
429 default:
430 return "??";
431 }
432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
436 * for info about using port indicators
437 */
Dan Williamsd99f6b42014-05-20 18:08:17 -0700438static void set_port_led(struct usb_hub *hub, int port1, int selector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700440 struct usb_port *port_dev = hub->ports[port1 - 1];
441 int status;
442
443 status = set_port_feature(hub->hdev, (selector << 8) | port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 USB_PORT_FEAT_INDICATOR);
Dan Williamsd99f6b42014-05-20 18:08:17 -0700445 dev_dbg(&port_dev->dev, "indicator %s status %d\n",
446 to_led_name(selector), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449#define LED_CYCLE_PERIOD ((2*HZ)/3)
450
David Howellsc4028952006-11-22 14:57:56 +0000451static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
David Howellsc4028952006-11-22 14:57:56 +0000453 struct usb_hub *hub =
454 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct usb_device *hdev = hub->hdev;
456 unsigned i;
457 unsigned changed = 0;
458 int cursor = -1;
459
460 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
461 return;
462
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200463 for (i = 0; i < hdev->maxchild; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 unsigned selector, mode;
465
466 /* 30%-50% duty cycle */
467
468 switch (hub->indicator[i]) {
469 /* cycle marker */
470 case INDICATOR_CYCLE:
471 cursor = i;
472 selector = HUB_LED_AUTO;
473 mode = INDICATOR_AUTO;
474 break;
475 /* blinking green = sw attention */
476 case INDICATOR_GREEN_BLINK:
477 selector = HUB_LED_GREEN;
478 mode = INDICATOR_GREEN_BLINK_OFF;
479 break;
480 case INDICATOR_GREEN_BLINK_OFF:
481 selector = HUB_LED_OFF;
482 mode = INDICATOR_GREEN_BLINK;
483 break;
484 /* blinking amber = hw attention */
485 case INDICATOR_AMBER_BLINK:
486 selector = HUB_LED_AMBER;
487 mode = INDICATOR_AMBER_BLINK_OFF;
488 break;
489 case INDICATOR_AMBER_BLINK_OFF:
490 selector = HUB_LED_OFF;
491 mode = INDICATOR_AMBER_BLINK;
492 break;
493 /* blink green/amber = reserved */
494 case INDICATOR_ALT_BLINK:
495 selector = HUB_LED_GREEN;
496 mode = INDICATOR_ALT_BLINK_OFF;
497 break;
498 case INDICATOR_ALT_BLINK_OFF:
499 selector = HUB_LED_AMBER;
500 mode = INDICATOR_ALT_BLINK;
501 break;
502 default:
503 continue;
504 }
505 if (selector != HUB_LED_AUTO)
506 changed = 1;
507 set_port_led(hub, i + 1, selector);
508 hub->indicator[i] = mode;
509 }
510 if (!changed && blinkenlights) {
511 cursor++;
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200512 cursor %= hdev->maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
514 hub->indicator[cursor] = INDICATOR_CYCLE;
515 changed++;
516 }
517 if (changed)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -0800518 queue_delayed_work(system_power_efficient_wq,
519 &hub->leds, LED_CYCLE_PERIOD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/* use a short timeout for hub/port status fetches */
523#define USB_STS_TIMEOUT 1000
524#define USB_STS_RETRIES 5
525
526/*
527 * USB 2.0 spec Section 11.24.2.6
528 */
529static int get_hub_status(struct usb_device *hdev,
530 struct usb_hub_status *data)
531{
532 int i, status = -ETIMEDOUT;
533
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200534 for (i = 0; i < USB_STS_RETRIES &&
535 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
537 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
538 data, sizeof(*data), USB_STS_TIMEOUT);
539 }
540 return status;
541}
542
543/*
544 * USB 2.0 spec Section 11.24.2.7
545 */
546static int get_port_status(struct usb_device *hdev, int port1,
547 struct usb_port_status *data)
548{
549 int i, status = -ETIMEDOUT;
550
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200551 for (i = 0; i < USB_STS_RETRIES &&
552 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
554 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
555 data, sizeof(*data), USB_STS_TIMEOUT);
556 }
557 return status;
558}
559
Alan Stern3eb14912008-03-03 15:15:43 -0500560static int hub_port_status(struct usb_hub *hub, int port1,
561 u16 *status, u16 *change)
562{
563 int ret;
564
565 mutex_lock(&hub->status_mutex);
566 ret = get_port_status(hub->hdev, port1, &hub->status->port);
567 if (ret < 4) {
Alan Sterne9e88fb2013-03-27 16:14:01 -0400568 if (ret != -ENODEV)
569 dev_err(hub->intfdev,
570 "%s failed (err = %d)\n", __func__, ret);
Alan Stern3eb14912008-03-03 15:15:43 -0500571 if (ret >= 0)
572 ret = -EIO;
573 } else {
574 *status = le16_to_cpu(hub->status->port.wPortStatus);
575 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700576
Alan Stern3eb14912008-03-03 15:15:43 -0500577 ret = 0;
578 }
579 mutex_unlock(&hub->status_mutex);
580 return ret;
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static void kick_khubd(struct usb_hub *hub)
584{
585 unsigned long flags;
586
587 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200588 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500590
591 /* Suppress autosuspend until khubd runs */
592 usb_autopm_get_interface_no_resume(
593 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 wake_up(&khubd_wait);
595 }
596 spin_unlock_irqrestore(&hub_event_lock, flags);
597}
598
599void usb_kick_khubd(struct usb_device *hdev)
600{
Lan Tianyuad493e52013-01-23 04:26:30 +0800601 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern251180842009-07-22 14:41:18 -0400602
603 if (hub)
604 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
606
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800607/*
608 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
609 * Notification, which indicates it had initiated remote wakeup.
610 *
611 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
612 * device initiates resume, so the USB core will not receive notice of the
613 * resume through the normal hub interrupt URB.
614 */
615void usb_wakeup_notification(struct usb_device *hdev,
616 unsigned int portnum)
617{
618 struct usb_hub *hub;
619
620 if (!hdev)
621 return;
622
Lan Tianyuad493e52013-01-23 04:26:30 +0800623 hub = usb_hub_to_struct_hub(hdev);
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800624 if (hub) {
625 set_bit(portnum, hub->wakeup_bits);
626 kick_khubd(hub);
627 }
628}
629EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100632static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200634 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400635 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100636 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 unsigned long bits;
638
Alan Sterne0152682007-08-24 15:42:52 -0400639 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 case -ENOENT: /* synchronous unlink */
641 case -ECONNRESET: /* async unlink */
642 case -ESHUTDOWN: /* hardware going away */
643 return;
644
645 default: /* presumably an error */
646 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400647 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if ((++hub->nerrors < 10) || hub->error)
649 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400650 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* let khubd handle things */
654 case 0: /* we got data: port status changed */
655 bits = 0;
656 for (i = 0; i < urb->actual_length; ++i)
657 bits |= ((unsigned long) ((*hub->buffer)[i]))
658 << (i*8);
659 hub->event_bits[0] = bits;
660 break;
661 }
662
663 hub->nerrors = 0;
664
665 /* Something happened, let khubd figure it out */
666 kick_khubd(hub);
667
668resubmit:
669 if (hub->quiescing)
670 return;
671
672 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
673 && status != -ENODEV && status != -EPERM)
674 dev_err (hub->intfdev, "resubmit --> %d\n", status);
675}
676
677/* USB 2.0 spec Section 11.24.2.3 */
678static inline int
679hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
680{
William Gulland2c7b8712013-06-27 16:10:20 -0700681 /* Need to clear both directions for control ep */
682 if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) ==
683 USB_ENDPOINT_XFER_CONTROL) {
684 int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
685 HUB_CLEAR_TT_BUFFER, USB_RT_PORT,
686 devinfo ^ 0x8000, tt, NULL, 0, 1000);
687 if (status)
688 return status;
689 }
Alan Sternc2f65952009-11-18 11:37:15 -0500690 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
692 tt, NULL, 0, 1000);
693}
694
695/*
696 * enumeration blocks khubd for a long time. we use keventd instead, since
697 * long blocking there is the exception, not the rule. accordingly, HCDs
698 * talking to TTs must queue control transfers (not just bulk and iso), so
699 * both can talk to the same hub concurrently.
700 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400701static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
David Howellsc4028952006-11-22 14:57:56 +0000703 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400704 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 unsigned long flags;
706
707 spin_lock_irqsave (&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300708 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400709 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 struct usb_tt_clear *clear;
711 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400712 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int status;
714
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400715 next = hub->tt.clear_list.next;
716 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 list_del (&clear->clear_list);
718
719 /* drop lock so HCD can concurrently report other TT errors */
720 spin_unlock_irqrestore (&hub->tt.lock, flags);
721 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400722 if (status && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 dev_err (&hdev->dev,
724 "clear tt %d (%04x) error %d\n",
725 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400726
727 /* Tell the HCD, even if the operation failed */
728 drv = clear->hcd->driver;
729 if (drv->clear_tt_buffer_complete)
730 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
731
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700732 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400733 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735 spin_unlock_irqrestore (&hub->tt.lock, flags);
736}
737
738/**
Lan Tianyu971fcd42013-01-23 04:26:29 +0800739 * usb_hub_set_port_power - control hub port's power state
Mathias Nyman413412612013-06-18 17:28:48 +0300740 * @hdev: USB device belonging to the usb hub
741 * @hub: target hub
Lan Tianyu971fcd42013-01-23 04:26:29 +0800742 * @port1: port index
743 * @set: expected status
744 *
745 * call this function to control port's power via setting or
746 * clearing the port's PORT_POWER feature.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200747 *
748 * Return: 0 if successful. A negative error code otherwise.
Lan Tianyu971fcd42013-01-23 04:26:29 +0800749 */
Mathias Nyman413412612013-06-18 17:28:48 +0300750int usb_hub_set_port_power(struct usb_device *hdev, struct usb_hub *hub,
751 int port1, bool set)
Lan Tianyu971fcd42013-01-23 04:26:29 +0800752{
753 int ret;
754
755 if (set)
756 ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
757 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800758 ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
759
Dan Williamsd5c38342014-05-20 18:08:52 -0700760 if (ret)
761 return ret;
762
763 if (set)
764 set_bit(port1, hub->power_bits);
765 else
766 clear_bit(port1, hub->power_bits);
767 return 0;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800768}
769
770/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400771 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
772 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
774 * High speed HCDs use this to tell the hub driver that some split control or
775 * bulk transaction failed in a way that requires clearing internal state of
776 * a transaction translator. This is normally detected (and reported) from
777 * interrupt context.
778 *
779 * It may not be possible for that hub to handle additional full (or low)
780 * speed transactions until that state is fully cleared out.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200781 *
782 * Return: 0 if successful. A negative error code otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400784int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400786 struct usb_device *udev = urb->dev;
787 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct usb_tt *tt = udev->tt;
789 unsigned long flags;
790 struct usb_tt_clear *clear;
791
792 /* we've got to cope with an arbitrary number of pending TT clears,
793 * since each TT has "at least two" buffers that can need it (and
794 * there can be many TTs per hub). even if they're uncommon.
795 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800796 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
798 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400799 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
802 /* info that CLEAR_TT_BUFFER needs */
803 clear->tt = tt->multi ? udev->ttport : 1;
804 clear->devinfo = usb_pipeendpoint (pipe);
805 clear->devinfo |= udev->devnum << 4;
806 clear->devinfo |= usb_pipecontrol (pipe)
807 ? (USB_ENDPOINT_XFER_CONTROL << 11)
808 : (USB_ENDPOINT_XFER_BULK << 11);
809 if (usb_pipein (pipe))
810 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400811
812 /* info for completion callback */
813 clear->hcd = bus_to_hcd(udev->bus);
814 clear->ep = urb->ep;
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* tell keventd to clear state for this TT */
817 spin_lock_irqsave (&tt->lock, flags);
818 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400819 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400823EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Alan Stern8520f382008-09-22 14:44:26 -0400825/* If do_delay is false, return the number of milliseconds the caller
826 * needs to delay.
827 */
828static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700831 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400832 unsigned delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Alan Stern4489a572006-04-27 15:54:22 -0400834 /* Enable power on each port. Some hubs have reserved values
835 * of LPSM (> 2) in their descriptors, even though they are
836 * USB 2.0 hubs. Some hubs do not implement port-power switching
837 * but only emulate it. In all cases, the ports won't work
838 * unless we send these messages to the hub.
839 */
Dan Williams9262c192014-05-20 18:08:12 -0700840 if (hub_is_port_power_switchable(hub))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400842 else
843 dev_dbg(hub->intfdev, "trying to enable port power on "
844 "non-switchable hub\n");
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +0200845 for (port1 = 1; port1 <= hub->hdev->maxchild; port1++)
Dan Williamsd5c38342014-05-20 18:08:52 -0700846 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +0800847 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
848 else
849 usb_clear_port_feature(hub->hdev, port1,
850 USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
David Brownellb7896962005-08-31 10:41:44 -0700852 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400853 delay = max(pgood_delay, (unsigned) 100);
854 if (do_delay)
855 msleep(delay);
856 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859static int hub_hub_status(struct usb_hub *hub,
860 u16 *status, u16 *change)
861{
862 int ret;
863
Alan Sterndb90e7a2007-02-05 09:56:15 -0500864 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 ret = get_hub_status(hub->hdev, &hub->status->hub);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400866 if (ret < 0) {
867 if (ret != -ENODEV)
868 dev_err(hub->intfdev,
869 "%s failed (err = %d)\n", __func__, ret);
870 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 *status = le16_to_cpu(hub->status->hub.wHubStatus);
Matthias Beyer469271f2013-10-10 23:41:27 +0200872 *change = le16_to_cpu(hub->status->hub.wHubChange);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 ret = 0;
874 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500875 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return ret;
877}
878
Sarah Sharp41e7e052012-11-14 16:42:32 -0800879static int hub_set_port_link_state(struct usb_hub *hub, int port1,
880 unsigned int link_status)
881{
882 return set_port_feature(hub->hdev,
883 port1 | (link_status << 3),
884 USB_PORT_FEAT_LINK_STATE);
885}
886
887/*
888 * If USB 3.0 ports are placed into the Disabled state, they will no longer
889 * detect any device connects or disconnects. This is generally not what the
890 * USB core wants, since it expects a disabled port to produce a port status
891 * change event when a new device connects.
892 *
893 * Instead, set the link state to Disabled, wait for the link to settle into
894 * that state, clear any change bits, and then put the port into the RxDetect
895 * state.
896 */
897static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
898{
899 int ret;
900 int total_time;
901 u16 portchange, portstatus;
902
903 if (!hub_is_superspeed(hub->hdev))
904 return -EINVAL;
905
906 ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
Alan Sterne9e88fb2013-03-27 16:14:01 -0400907 if (ret)
Sarah Sharp41e7e052012-11-14 16:42:32 -0800908 return ret;
Sarah Sharp41e7e052012-11-14 16:42:32 -0800909
910 /* Wait for the link to enter the disabled state. */
911 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
912 ret = hub_port_status(hub, port1, &portstatus, &portchange);
913 if (ret < 0)
914 return ret;
915
916 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
917 USB_SS_PORT_LS_SS_DISABLED)
918 break;
919 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
920 break;
921 msleep(HUB_DEBOUNCE_STEP);
922 }
923 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
Dan Williamsd99f6b42014-05-20 18:08:17 -0700924 dev_warn(&hub->ports[port1 - 1]->dev,
925 "Could not disable after %d ms\n", total_time);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800926
927 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
928}
929
Alan Stern8b28c752005-08-10 17:04:13 -0400930static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
931{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700932 struct usb_port *port_dev = hub->ports[port1 - 1];
Alan Stern8b28c752005-08-10 17:04:13 -0400933 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400934 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400935
Dan Williamsd99f6b42014-05-20 18:08:17 -0700936 if (port_dev->child && set_state)
937 usb_set_device_state(port_dev->child, USB_STATE_NOTATTACHED);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800938 if (!hub->error) {
939 if (hub_is_superspeed(hub->hdev))
940 ret = hub_usb3_port_disable(hub, port1);
941 else
Lan Tianyuad493e52013-01-23 04:26:30 +0800942 ret = usb_clear_port_feature(hdev, port1,
Sarah Sharp41e7e052012-11-14 16:42:32 -0800943 USB_PORT_FEAT_ENABLE);
944 }
Alan Sterne9e88fb2013-03-27 16:14:01 -0400945 if (ret && ret != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -0700946 dev_err(&port_dev->dev, "cannot disable (err = %d)\n", ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400947 return ret;
948}
949
Alan Stern0458d5b2007-05-04 11:52:20 -0400950/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800951 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400952 * time later khubd will disconnect() any existing usb_device on the port
953 * and will re-enumerate if there actually is a device attached.
954 */
955static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
956{
Dan Williamsd99f6b42014-05-20 18:08:17 -0700957 dev_dbg(&hub->ports[port1 - 1]->dev, "logical disconnect\n");
Alan Stern0458d5b2007-05-04 11:52:20 -0400958 hub_port_disable(hub, port1, 1);
959
960 /* FIXME let caller ask to power down the port:
961 * - some devices won't enumerate without a VBUS power cycle
962 * - SRP saves power that way
963 * - ... new call, TBD ...
964 * That's easy if this hub can switch power per-port, and
965 * khubd reactivates the port later (timer, SRP, etc).
966 * Powerdown must be optional, because of reset/DFU.
967 */
968
969 set_bit(port1, hub->change_bits);
Matthias Beyer469271f2013-10-10 23:41:27 +0200970 kick_khubd(hub);
Alan Stern0458d5b2007-05-04 11:52:20 -0400971}
972
Alan Stern253e0572009-10-27 15:20:13 -0400973/**
974 * usb_remove_device - disable a device's port on its parent hub
975 * @udev: device to be disabled and removed
976 * Context: @udev locked, must be able to sleep.
977 *
978 * After @udev's port has been disabled, khubd is notified and it will
979 * see that the device has been disconnected. When the device is
980 * physically unplugged and something is plugged in, the events will
981 * be received and processed normally.
Yacine Belkadi626f0902013-08-02 20:10:04 +0200982 *
983 * Return: 0 if successful. A negative error code otherwise.
Alan Stern253e0572009-10-27 15:20:13 -0400984 */
985int usb_remove_device(struct usb_device *udev)
986{
987 struct usb_hub *hub;
988 struct usb_interface *intf;
989
990 if (!udev->parent) /* Can't remove a root hub */
991 return -EINVAL;
Lan Tianyuad493e52013-01-23 04:26:30 +0800992 hub = usb_hub_to_struct_hub(udev->parent);
Alan Stern253e0572009-10-27 15:20:13 -0400993 intf = to_usb_interface(hub->intfdev);
994
995 usb_autopm_get_interface(intf);
996 set_bit(udev->portnum, hub->removed_bits);
997 hub_port_logical_disconnect(hub, udev->portnum);
998 usb_autopm_put_interface(intf);
999 return 0;
1000}
1001
Alan Stern6ee0b272008-04-28 11:06:42 -04001002enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -05001003 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -04001004 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -04001005};
Alan Stern5e6effa2008-03-03 15:15:51 -05001006
Alan Stern8520f382008-09-22 14:44:26 -04001007static void hub_init_func2(struct work_struct *ws);
1008static void hub_init_func3(struct work_struct *ws);
1009
Alan Sternf2835212008-04-28 11:07:17 -04001010static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -05001011{
1012 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -08001013 struct usb_hcd *hcd;
1014 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -05001015 int port1;
Alan Sternf2835212008-04-28 11:07:17 -04001016 int status;
Alan Stern948fea32008-04-28 11:07:07 -04001017 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -04001018 unsigned delay;
1019
1020 /* Continue a partial initialization */
1021 if (type == HUB_INIT2)
1022 goto init2;
1023 if (type == HUB_INIT3)
1024 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -05001025
Elric Fua45aa3b2012-02-18 13:32:27 +08001026 /* The superspeed hub except for root hub has to use Hub Depth
1027 * value as an offset into the route string to locate the bits
1028 * it uses to determine the downstream port number. So hub driver
1029 * should send a set hub depth request to superspeed hub after
1030 * the superspeed hub is set configuration in initialization or
1031 * reset procedure.
1032 *
1033 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -04001034 * For any other type of activation, turn it on.
1035 */
Alan Stern8520f382008-09-22 14:44:26 -04001036 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +08001037 if (hdev->parent && hub_is_superspeed(hdev)) {
1038 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1039 HUB_SET_DEPTH, USB_RT_HUB,
1040 hdev->level - 1, 0, NULL, 0,
1041 USB_CTRL_SET_TIMEOUT);
1042 if (ret < 0)
1043 dev_err(hub->intfdev,
1044 "set hub depth failed\n");
1045 }
Alan Stern8520f382008-09-22 14:44:26 -04001046
1047 /* Speed up system boot by using a delayed_work for the
1048 * hub's initial power-up delays. This is pretty awkward
1049 * and the implementation looks like a home-brewed sort of
1050 * setjmp/longjmp, but it saves at least 100 ms for each
1051 * root hub (assuming usbcore is compiled into the kernel
1052 * rather than as a module). It adds up.
1053 *
1054 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1055 * because for those activation types the ports have to be
1056 * operational when we return. In theory this could be done
1057 * for HUB_POST_RESET, but it's easier not to.
1058 */
1059 if (type == HUB_INIT) {
1060 delay = hub_power_on(hub, false);
Tejun Heo77fa83c2014-03-07 10:24:48 -05001061 INIT_DELAYED_WORK(&hub->init_work, hub_init_func2);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001062 queue_delayed_work(system_power_efficient_wq,
1063 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001064 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001065
1066 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001067 usb_autopm_get_interface_no_resume(
1068 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001069 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001070 } else if (type == HUB_RESET_RESUME) {
1071 /* The internal host controller state for the hub device
1072 * may be gone after a host power loss on system resume.
1073 * Update the device's info so the HW knows it's a hub.
1074 */
1075 hcd = bus_to_hcd(hdev->bus);
1076 if (hcd->driver->update_hub_device) {
1077 ret = hcd->driver->update_hub_device(hcd, hdev,
1078 &hub->tt, GFP_NOIO);
1079 if (ret < 0) {
1080 dev_err(hub->intfdev, "Host not "
1081 "accepting hub info "
1082 "update.\n");
1083 dev_err(hub->intfdev, "LS/FS devices "
1084 "and hubs may not work "
1085 "under this hub\n.");
1086 }
1087 }
1088 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001089 } else {
1090 hub_power_on(hub, true);
1091 }
1092 }
1093 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001094
Dan Williamsd99f6b42014-05-20 18:08:17 -07001095 /*
1096 * Check each port and set hub->change_bits to let khubd know
Alan Stern6ee0b272008-04-28 11:06:42 -04001097 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001098 */
1099 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07001100 struct usb_port *port_dev = hub->ports[port1 - 1];
1101 struct usb_device *udev = port_dev->child;
Alan Stern5e6effa2008-03-03 15:15:51 -05001102 u16 portstatus, portchange;
1103
Alan Stern6ee0b272008-04-28 11:06:42 -04001104 portstatus = portchange = 0;
1105 status = hub_port_status(hub, port1, &portstatus, &portchange);
1106 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
Dan Williamsd99f6b42014-05-20 18:08:17 -07001107 dev_dbg(&port_dev->dev, "status %04x change %04x\n",
1108 portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001109
Dan Williamsd99f6b42014-05-20 18:08:17 -07001110 /*
1111 * After anything other than HUB_RESUME (i.e., initialization
Alan Stern6ee0b272008-04-28 11:06:42 -04001112 * or any sort of reset), every port should be disabled.
1113 * Unconnected ports should likewise be disabled (paranoia),
1114 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001115 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001116 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1117 type != HUB_RESUME ||
1118 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1119 !udev ||
1120 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001121 /*
1122 * USB3 protocol ports will automatically transition
1123 * to Enabled state when detect an USB3.0 device attach.
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001124 * Do not disable USB3 protocol ports, just pretend
1125 * power was lost
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001126 */
Dan Williamsfd1ac4c2013-10-07 11:58:20 -07001127 portstatus &= ~USB_PORT_STAT_ENABLE;
1128 if (!hub_is_superspeed(hdev))
Lan Tianyuad493e52013-01-23 04:26:30 +08001129 usb_clear_port_feature(hdev, port1,
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001130 USB_PORT_FEAT_ENABLE);
Alan Stern6ee0b272008-04-28 11:06:42 -04001131 }
1132
Alan Stern948fea32008-04-28 11:07:07 -04001133 /* Clear status-change flags; we'll debounce later */
1134 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1135 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001136 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001137 USB_PORT_FEAT_C_CONNECTION);
1138 }
1139 if (portchange & USB_PORT_STAT_C_ENABLE) {
1140 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001141 usb_clear_port_feature(hub->hdev, port1,
Alan Stern948fea32008-04-28 11:07:07 -04001142 USB_PORT_FEAT_C_ENABLE);
1143 }
Julius Wernere92aee32013-10-15 17:45:00 -07001144 if (portchange & USB_PORT_STAT_C_RESET) {
1145 need_debounce_delay = true;
1146 usb_clear_port_feature(hub->hdev, port1,
1147 USB_PORT_FEAT_C_RESET);
1148 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001149 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1150 hub_is_superspeed(hub->hdev)) {
1151 need_debounce_delay = true;
Lan Tianyuad493e52013-01-23 04:26:30 +08001152 usb_clear_port_feature(hub->hdev, port1,
Don Zickus79c3dd82011-11-03 09:07:18 -04001153 USB_PORT_FEAT_C_BH_PORT_RESET);
1154 }
Alan Stern253e0572009-10-27 15:20:13 -04001155 /* We can forget about a "removed" device when there's a
1156 * physical disconnect or the connect status changes.
1157 */
1158 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1159 (portchange & USB_PORT_STAT_C_CONNECTION))
1160 clear_bit(port1, hub->removed_bits);
1161
Alan Stern6ee0b272008-04-28 11:06:42 -04001162 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1163 /* Tell khubd to disconnect the device or
1164 * check for a new connection
1165 */
Shen Guang08d1dec2014-01-08 14:45:42 +08001166 if (udev || (portstatus & USB_PORT_STAT_CONNECTION) ||
1167 (portstatus & USB_PORT_STAT_OVERCURRENT))
Alan Stern6ee0b272008-04-28 11:06:42 -04001168 set_bit(port1, hub->change_bits);
1169
1170 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001171 bool port_resumed = (portstatus &
1172 USB_PORT_STAT_LINK_STATE) ==
1173 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001174 /* The power session apparently survived the resume.
1175 * If there was an overcurrent or suspend change
1176 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001177 * take care of it. Look at the port link state
1178 * for USB 3.0 hubs, since they don't have a suspend
1179 * change bit, and they don't set the port link change
1180 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001181 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001182 if (portchange || (hub_is_superspeed(hub->hdev) &&
1183 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001184 set_bit(port1, hub->change_bits);
1185
1186 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001187#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001188 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001189#endif
Lan Tianyuad493e52013-01-23 04:26:30 +08001190 /* Don't set the change_bits when the device
1191 * was powered off.
1192 */
Dan Williamsd5c38342014-05-20 18:08:52 -07001193 if (test_bit(port1, hub->power_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +08001194 set_bit(port1, hub->change_bits);
Alan Stern8808f002008-04-28 11:06:55 -04001195
Alan Stern6ee0b272008-04-28 11:06:42 -04001196 } else {
1197 /* The power session is gone; tell khubd */
1198 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1199 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001200 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001201 }
1202
Alan Stern948fea32008-04-28 11:07:07 -04001203 /* If no port-status-change flags were set, we don't need any
1204 * debouncing. If flags were set we can try to debounce the
1205 * ports all at once right now, instead of letting khubd do them
1206 * one at a time later on.
1207 *
1208 * If any port-status changes do occur during this delay, khubd
1209 * will see them later and handle them normally.
1210 */
Alan Stern8520f382008-09-22 14:44:26 -04001211 if (need_debounce_delay) {
1212 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001213
Alan Stern8520f382008-09-22 14:44:26 -04001214 /* Don't do a long sleep inside a workqueue routine */
1215 if (type == HUB_INIT2) {
Tejun Heo77fa83c2014-03-07 10:24:48 -05001216 INIT_DELAYED_WORK(&hub->init_work, hub_init_func3);
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001217 queue_delayed_work(system_power_efficient_wq,
1218 &hub->init_work,
Alan Stern8520f382008-09-22 14:44:26 -04001219 msecs_to_jiffies(delay));
1220 return; /* Continues at init3: below */
1221 } else {
1222 msleep(delay);
1223 }
1224 }
1225 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001226 hub->quiescing = 0;
1227
1228 status = usb_submit_urb(hub->urb, GFP_NOIO);
1229 if (status < 0)
1230 dev_err(hub->intfdev, "activate --> %d\n", status);
1231 if (hub->has_indicators && blinkenlights)
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08001232 queue_delayed_work(system_power_efficient_wq,
1233 &hub->leds, LED_CYCLE_PERIOD);
Alan Sternf2835212008-04-28 11:07:17 -04001234
1235 /* Scan all ports that need attention */
1236 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001237
1238 /* Allow autosuspend if it was suppressed */
1239 if (type <= HUB_INIT3)
1240 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001241}
1242
Alan Stern8520f382008-09-22 14:44:26 -04001243/* Implement the continuations for the delays above */
1244static void hub_init_func2(struct work_struct *ws)
1245{
1246 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1247
1248 hub_activate(hub, HUB_INIT2);
1249}
1250
1251static void hub_init_func3(struct work_struct *ws)
1252{
1253 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1254
1255 hub_activate(hub, HUB_INIT3);
1256}
1257
Alan Stern43303542008-04-28 11:07:31 -04001258enum hub_quiescing_type {
1259 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1260};
1261
1262static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1263{
1264 struct usb_device *hdev = hub->hdev;
1265 int i;
1266
Alan Stern8520f382008-09-22 14:44:26 -04001267 cancel_delayed_work_sync(&hub->init_work);
1268
Alan Stern43303542008-04-28 11:07:31 -04001269 /* khubd and related activity won't re-trigger */
1270 hub->quiescing = 1;
1271
1272 if (type != HUB_SUSPEND) {
1273 /* Disconnect all the children */
1274 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001275 if (hub->ports[i]->child)
1276 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001277 }
1278 }
1279
1280 /* Stop khubd and related activity */
1281 usb_kill_urb(hub->urb);
1282 if (hub->has_indicators)
1283 cancel_delayed_work_sync(&hub->leds);
1284 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001285 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001286}
1287
Alan Stern600856c2014-05-20 18:08:07 -07001288static void hub_pm_barrier_for_all_ports(struct usb_hub *hub)
1289{
1290 int i;
1291
1292 for (i = 0; i < hub->hdev->maxchild; ++i)
1293 pm_runtime_barrier(&hub->ports[i]->dev);
1294}
1295
Alan Stern3eb14912008-03-03 15:15:43 -05001296/* caller has locked the hub device */
1297static int hub_pre_reset(struct usb_interface *intf)
1298{
1299 struct usb_hub *hub = usb_get_intfdata(intf);
1300
Alan Stern43303542008-04-28 11:07:31 -04001301 hub_quiesce(hub, HUB_PRE_RESET);
Alan Stern600856c2014-05-20 18:08:07 -07001302 hub->in_reset = 1;
1303 hub_pm_barrier_for_all_ports(hub);
Alan Sternf07600c2007-05-30 15:38:16 -04001304 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001305}
1306
1307/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001308static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001309{
Alan Stern7de18d82006-06-01 13:37:24 -04001310 struct usb_hub *hub = usb_get_intfdata(intf);
1311
Alan Stern600856c2014-05-20 18:08:07 -07001312 hub->in_reset = 0;
1313 hub_pm_barrier_for_all_ports(hub);
Alan Sternf2835212008-04-28 11:07:17 -04001314 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001315 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static int hub_configure(struct usb_hub *hub,
1319 struct usb_endpoint_descriptor *endpoint)
1320{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001321 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 struct usb_device *hdev = hub->hdev;
1323 struct device *hub_dev = hub->intfdev;
1324 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001325 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001327 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001328 char *message = "out of memory";
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001329 unsigned unit_load;
1330 unsigned full_load;
Dan Williamsd8521af2014-05-20 18:08:28 -07001331 unsigned maxchild;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Alan Sternd697cdd2009-10-27 15:18:46 -04001333 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 ret = -ENOMEM;
1336 goto fail;
1337 }
1338
1339 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1340 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 ret = -ENOMEM;
1342 goto fail;
1343 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001344 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1347 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 ret = -ENOMEM;
1349 goto fail;
1350 }
1351
1352 /* Request the entire hub descriptor.
1353 * hub->descriptor can handle USB_MAXCHILDREN ports,
1354 * but the hub can/will return fewer bytes here.
1355 */
John Youndbe79bb2001-09-17 00:00:00 -07001356 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (ret < 0) {
1358 message = "can't read hub descriptor";
1359 goto fail;
1360 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1361 message = "hub has too many ports!";
1362 ret = -ENODEV;
1363 goto fail;
David Linares769d73682013-03-25 10:50:27 +00001364 } else if (hub->descriptor->bNbrPorts == 0) {
1365 message = "hub doesn't have any ports!";
1366 ret = -ENODEV;
1367 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
Dan Williamsd8521af2014-05-20 18:08:28 -07001370 maxchild = hub->descriptor->bNbrPorts;
1371 dev_info(hub_dev, "%d port%s detected\n", maxchild,
1372 (maxchild == 1) ? "" : "s");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Dan Williamsd8521af2014-05-20 18:08:28 -07001374 hub->ports = kzalloc(maxchild * sizeof(struct usb_port *), GFP_KERNEL);
Lan Tianyuff823c792012-09-05 13:44:32 +08001375 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001376 ret = -ENOMEM;
1377 goto fail;
1378 }
1379
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001380 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001381 if (hub_is_superspeed(hdev)) {
1382 unit_load = 150;
1383 full_load = 900;
1384 } else {
1385 unit_load = 100;
1386 full_load = 500;
1387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
John Youndbe79bb2001-09-17 00:00:00 -07001389 /* FIXME for USB 3.0, skip for now */
1390 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1391 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int i;
Matthias Beyer469271f2013-10-10 23:41:27 +02001393 char portstr[USB_MAXCHILDREN + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Dan Williamsd8521af2014-05-20 18:08:28 -07001395 for (i = 0; i < maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001396 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1398 ? 'F' : 'R';
Dan Williamsd8521af2014-05-20 18:08:28 -07001399 portstr[maxchild] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1401 } else
1402 dev_dbg(hub_dev, "standalone hub\n");
1403
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001404 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301405 case HUB_CHAR_COMMON_LPSM:
1406 dev_dbg(hub_dev, "ganged power switching\n");
1407 break;
1408 case HUB_CHAR_INDV_PORT_LPSM:
1409 dev_dbg(hub_dev, "individual port power switching\n");
1410 break;
1411 case HUB_CHAR_NO_LPSM:
1412 case HUB_CHAR_LPSM:
1413 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001417 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301418 case HUB_CHAR_COMMON_OCPM:
1419 dev_dbg(hub_dev, "global over-current protection\n");
1420 break;
1421 case HUB_CHAR_INDV_PORT_OCPM:
1422 dev_dbg(hub_dev, "individual port over-current protection\n");
1423 break;
1424 case HUB_CHAR_NO_OCPM:
1425 case HUB_CHAR_OCPM:
1426 dev_dbg(hub_dev, "no over-current protection\n");
1427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429
1430 spin_lock_init (&hub->tt.lock);
1431 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001432 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301434 case USB_HUB_PR_FS:
1435 break;
1436 case USB_HUB_PR_HS_SINGLE_TT:
1437 dev_dbg(hub_dev, "Single TT\n");
1438 hub->tt.hub = hdev;
1439 break;
1440 case USB_HUB_PR_HS_MULTI_TT:
1441 ret = usb_set_interface(hdev, 0, 1);
1442 if (ret == 0) {
1443 dev_dbg(hub_dev, "TT per port\n");
1444 hub->tt.multi = 1;
1445 } else
1446 dev_err(hub_dev, "Using single TT (err %d)\n",
1447 ret);
1448 hub->tt.hub = hdev;
1449 break;
1450 case USB_HUB_PR_SS:
1451 /* USB 3.0 hubs don't have a TT */
1452 break;
1453 default:
1454 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1455 hdev->descriptor.bDeviceProtocol);
1456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001459 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001460 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
Matthias Beyer469271f2013-10-10 23:41:27 +02001461 case HUB_TTTT_8_BITS:
1462 if (hdev->descriptor.bDeviceProtocol != 0) {
1463 hub->tt.think_time = 666;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001464 dev_dbg(hub_dev, "TT requires at most %d "
1465 "FS bit times (%d ns)\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02001466 8, hub->tt.think_time);
1467 }
1468 break;
1469 case HUB_TTTT_16_BITS:
1470 hub->tt.think_time = 666 * 2;
1471 dev_dbg(hub_dev, "TT requires at most %d "
1472 "FS bit times (%d ns)\n",
1473 16, hub->tt.think_time);
1474 break;
1475 case HUB_TTTT_24_BITS:
1476 hub->tt.think_time = 666 * 3;
1477 dev_dbg(hub_dev, "TT requires at most %d "
1478 "FS bit times (%d ns)\n",
1479 24, hub->tt.think_time);
1480 break;
1481 case HUB_TTTT_32_BITS:
1482 hub->tt.think_time = 666 * 4;
1483 dev_dbg(hub_dev, "TT requires at most %d "
1484 "FS bit times (%d ns)\n",
1485 32, hub->tt.think_time);
1486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
1489 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001490 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 hub->has_indicators = 1;
1492 dev_dbg(hub_dev, "Port indicators are supported\n");
1493 }
1494
1495 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1496 hub->descriptor->bPwrOn2PwrGood * 2);
1497
1498 /* power budgeting mostly matters with bus-powered hubs,
1499 * and battery-powered root hubs (may provide just 8 mA).
1500 */
1501 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern15b73362013-07-30 15:35:40 -04001502 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 message = "can't get hub status";
1504 goto fail;
1505 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001506 hcd = bus_to_hcd(hdev->bus);
Alan Stern7d35b922005-04-25 11:18:32 -04001507 if (hdev == hdev->bus->root_hub) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001508 if (hcd->power_budget > 0)
1509 hdev->bus_mA = hcd->power_budget;
1510 else
Dan Williamsd8521af2014-05-20 18:08:28 -07001511 hdev->bus_mA = full_load * maxchild;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001512 if (hdev->bus_mA >= full_load)
1513 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001514 else {
1515 hub->mA_per_port = hdev->bus_mA;
1516 hub->limited_power = 1;
1517 }
Alan Stern7d35b922005-04-25 11:18:32 -04001518 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001519 int remaining = hdev->bus_mA -
1520 hub->descriptor->bHubContrCurrent;
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1523 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001524 hub->limited_power = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Dan Williamsd8521af2014-05-20 18:08:28 -07001526 if (remaining < maxchild * unit_load)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001527 dev_warn(hub_dev,
Alan Stern55c52712005-11-23 12:03:12 -05001528 "insufficient power available "
1529 "to use all downstream ports\n");
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001530 hub->mA_per_port = unit_load; /* 7.2.1 */
1531
Alan Stern55c52712005-11-23 12:03:12 -05001532 } else { /* Self-powered external hub */
1533 /* FIXME: What about battery-powered external hubs that
1534 * provide less current per port? */
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001535 hub->mA_per_port = full_load;
Alan Stern55c52712005-11-23 12:03:12 -05001536 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01001537 if (hub->mA_per_port < full_load)
Alan Stern55c52712005-11-23 12:03:12 -05001538 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1539 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001541 /* Update the HCD's internal representation of this hub before khubd
1542 * starts getting port status changes for devices under the hub.
1543 */
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001544 if (hcd->driver->update_hub_device) {
1545 ret = hcd->driver->update_hub_device(hcd, hdev,
1546 &hub->tt, GFP_KERNEL);
1547 if (ret < 0) {
1548 message = "can't update HCD hub info";
1549 goto fail;
1550 }
1551 }
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1554 if (ret < 0) {
1555 message = "can't get hub status";
1556 goto fail;
1557 }
1558
1559 /* local power status reports aren't always correct */
1560 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1561 dev_dbg(hub_dev, "local power source is %s\n",
1562 (hubstatus & HUB_STATUS_LOCAL_POWER)
1563 ? "lost (inactive)" : "good");
1564
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001565 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 dev_dbg(hub_dev, "%sover-current condition exists\n",
1567 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1568
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001569 /* set up the interrupt endpoint
1570 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1571 * bytes as USB2.0[11.12.3] says because some hubs are known
1572 * to send more data (and thus cause overflow). For root hubs,
1573 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1574 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1576 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1577
1578 if (maxp > sizeof(*hub->buffer))
1579 maxp = sizeof(*hub->buffer);
1580
1581 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1582 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ret = -ENOMEM;
1584 goto fail;
1585 }
1586
1587 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1588 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 /* maybe cycle the hub leds */
1591 if (hub->has_indicators && blinkenlights)
Matthias Beyer469271f2013-10-10 23:41:27 +02001592 hub->indicator[0] = INDICATOR_CYCLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Dan Williamsd8521af2014-05-20 18:08:28 -07001594 mutex_lock(&usb_port_peer_mutex);
1595 for (i = 0; i < maxchild; i++) {
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001596 ret = usb_hub_create_port_device(hub, i + 1);
1597 if (ret < 0) {
Lan Tianyufa2a9562012-09-05 13:44:31 +08001598 dev_err(hub->intfdev,
1599 "couldn't create port%d device.\n", i + 1);
Dan Williamsd8521af2014-05-20 18:08:28 -07001600 break;
Krzysztof Mazure58547e2013-08-22 14:49:39 +02001601 }
1602 }
Dan Williamsd8521af2014-05-20 18:08:28 -07001603 hdev->maxchild = i;
1604 mutex_unlock(&usb_port_peer_mutex);
1605 if (ret < 0)
1606 goto fail;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001607
Lan Tianyud2123fd2013-01-21 22:18:00 +08001608 usb_hub_adjust_deviceremovable(hdev, hub->descriptor);
1609
Alan Sternf2835212008-04-28 11:07:17 -04001610 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return 0;
1612
1613fail:
1614 dev_err (hub_dev, "config failed, %s (err %d)\n",
1615 message, ret);
1616 /* hub_disconnect() frees urb and descriptor */
1617 return ret;
1618}
1619
Alan Sterne8054852007-05-04 11:55:11 -04001620static void hub_release(struct kref *kref)
1621{
1622 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1623
1624 usb_put_intf(to_usb_interface(hub->intfdev));
1625 kfree(hub);
1626}
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628static unsigned highspeed_hubs;
1629
1630static void hub_disconnect(struct usb_interface *intf)
1631{
Huajun Li88162302012-03-12 21:00:19 +08001632 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001633 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Stern543d7782014-01-07 10:43:02 -05001634 int port1;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001635
Alan Sterne8054852007-05-04 11:55:11 -04001636 /* Take the hub off the event list and don't let it be added again */
1637 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001638 if (!list_empty(&hub->event_list)) {
1639 list_del_init(&hub->event_list);
1640 usb_autopm_put_interface_no_suspend(intf);
1641 }
Alan Sterne8054852007-05-04 11:55:11 -04001642 hub->disconnected = 1;
1643 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Alan Stern7de18d82006-06-01 13:37:24 -04001645 /* Disconnect all children and quiesce the hub */
1646 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001647 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001648
Dan Williamsd8521af2014-05-20 18:08:28 -07001649 mutex_lock(&usb_port_peer_mutex);
1650
Alan Stern543d7782014-01-07 10:43:02 -05001651 /* Avoid races with recursively_mark_NOTATTACHED() */
1652 spin_lock_irq(&device_state_lock);
1653 port1 = hdev->maxchild;
1654 hdev->maxchild = 0;
1655 usb_set_intfdata(intf, NULL);
1656 spin_unlock_irq(&device_state_lock);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001657
Alan Stern543d7782014-01-07 10:43:02 -05001658 for (; port1 > 0; --port1)
1659 usb_hub_remove_port_device(hub, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Dan Williamsd8521af2014-05-20 18:08:28 -07001661 mutex_unlock(&usb_port_peer_mutex);
1662
Alan Sterne8054852007-05-04 11:55:11 -04001663 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 highspeed_hubs--;
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001667 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001668 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001669 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001670 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Lan Tianyu971fcd42013-01-23 04:26:29 +08001672 pm_suspend_ignore_children(&intf->dev, false);
Alan Sterne8054852007-05-04 11:55:11 -04001673 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
1676static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1677{
1678 struct usb_host_interface *desc;
1679 struct usb_endpoint_descriptor *endpoint;
1680 struct usb_device *hdev;
1681 struct usb_hub *hub;
1682
1683 desc = intf->cur_altsetting;
1684 hdev = interface_to_usbdev(intf);
1685
Ming Lei596d7892012-10-24 11:59:25 +08001686 /*
1687 * Set default autosuspend delay as 0 to speedup bus suspend,
1688 * based on the below considerations:
1689 *
1690 * - Unlike other drivers, the hub driver does not rely on the
1691 * autosuspend delay to provide enough time to handle a wakeup
1692 * event, and the submitted status URB is just to check future
1693 * change on hub downstream ports, so it is safe to do it.
1694 *
1695 * - The patch might cause one or more auto supend/resume for
1696 * below very rare devices when they are plugged into hub
1697 * first time:
1698 *
1699 * devices having trouble initializing, and disconnect
1700 * themselves from the bus and then reconnect a second
1701 * or so later
1702 *
1703 * devices just for downloading firmware, and disconnects
1704 * themselves after completing it
1705 *
1706 * For these quite rare devices, their drivers may change the
1707 * autosuspend delay of their parent hub in the probe() to one
1708 * appropriate value to avoid the subtle problem if someone
1709 * does care it.
1710 *
1711 * - The patch may cause one or more auto suspend/resume on
1712 * hub during running 'lsusb', but it is probably too
1713 * infrequent to worry about.
1714 *
1715 * - Change autosuspend delay of hub can avoid unnecessary auto
1716 * suspend timer for hub, also may decrease power consumption
1717 * of USB bus.
1718 */
1719 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1720
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001721 /* Hubs have proper suspend/resume support. */
1722 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001723
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001724 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001725 dev_err(&intf->dev,
1726 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001727 return -E2BIG;
1728 }
1729
David Brownell89ccbdc2006-04-02 10:18:09 -08001730#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1731 if (hdev->parent) {
1732 dev_warn(&intf->dev, "ignoring external hub\n");
1733 return -ENODEV;
1734 }
1735#endif
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /* Some hubs have a subclass of 1, which AFAICT according to the */
1738 /* specs is not defined, but it works */
1739 if ((desc->desc.bInterfaceSubClass != 0) &&
1740 (desc->desc.bInterfaceSubClass != 1)) {
1741descriptor_error:
1742 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1743 return -EIO;
1744 }
1745
1746 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1747 if (desc->desc.bNumEndpoints != 1)
1748 goto descriptor_error;
1749
1750 endpoint = &desc->endpoint[0].desc;
1751
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001752 /* If it's not an interrupt in endpoint, we'd better punt! */
1753 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto descriptor_error;
1755
1756 /* We found a hub */
1757 dev_info (&intf->dev, "USB hub found\n");
1758
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001759 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (!hub) {
1761 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1762 return -ENOMEM;
1763 }
1764
Alan Sterne8054852007-05-04 11:55:11 -04001765 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 INIT_LIST_HEAD(&hub->event_list);
1767 hub->intfdev = &intf->dev;
1768 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001769 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001770 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001771 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001774 intf->needs_remote_wakeup = 1;
Lan Tianyu971fcd42013-01-23 04:26:29 +08001775 pm_suspend_ignore_children(&intf->dev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 if (hdev->speed == USB_SPEED_HIGH)
1778 highspeed_hubs++;
1779
Ming Leie6f30de2012-10-24 11:59:24 +08001780 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1781 hub->quirk_check_port_auto_suspend = 1;
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (hub_configure(hub, endpoint) >= 0)
1784 return 0;
1785
1786 hub_disconnect (intf);
1787 return -ENODEV;
1788}
1789
1790static int
1791hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1792{
1793 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuad493e52013-01-23 04:26:30 +08001794 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /* assert ifno == 0 (part of hub spec) */
1797 switch (code) {
1798 case USBDEVFS_HUB_PORTINFO: {
1799 struct usbdevfs_hub_portinfo *info = user_data;
1800 int i;
1801
1802 spin_lock_irq(&device_state_lock);
1803 if (hdev->devnum <= 0)
1804 info->nports = 0;
1805 else {
1806 info->nports = hdev->maxchild;
1807 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001808 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 info->port[i] = 0;
1810 else
1811 info->port[i] =
Lan Tianyuff823c792012-09-05 13:44:32 +08001812 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814 }
1815 spin_unlock_irq(&device_state_lock);
1816
1817 return info->nports + 1;
1818 }
1819
1820 default:
1821 return -ENOSYS;
1822 }
1823}
1824
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001825/*
1826 * Allow user programs to claim ports on a hub. When a device is attached
1827 * to one of these "claimed" ports, the program will "own" the device.
1828 */
1829static int find_port_owner(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001830 struct usb_dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001831{
Mathias Nyman413412612013-06-18 17:28:48 +03001832 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
1833
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001834 if (hdev->state == USB_STATE_NOTATTACHED)
1835 return -ENODEV;
1836 if (port1 == 0 || port1 > hdev->maxchild)
1837 return -EINVAL;
1838
Mathias Nyman413412612013-06-18 17:28:48 +03001839 /* Devices not managed by the hub driver
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001840 * will always have maxchild equal to 0.
1841 */
Mathias Nyman413412612013-06-18 17:28:48 +03001842 *ppowner = &(hub->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001843 return 0;
1844}
1845
1846/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001847int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001848 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001849{
1850 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001851 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001852
1853 rc = find_port_owner(hdev, port1, &powner);
1854 if (rc)
1855 return rc;
1856 if (*powner)
1857 return -EBUSY;
1858 *powner = owner;
1859 return rc;
1860}
Valentina Manea6080cd02014-03-08 14:53:34 +02001861EXPORT_SYMBOL_GPL(usb_hub_claim_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001862
Lan Tianyu336c5c32012-07-06 14:13:52 +08001863int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001864 struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001865{
1866 int rc;
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001867 struct usb_dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001868
1869 rc = find_port_owner(hdev, port1, &powner);
1870 if (rc)
1871 return rc;
1872 if (*powner != owner)
1873 return -ENOENT;
1874 *powner = NULL;
1875 return rc;
1876}
Valentina Manea6080cd02014-03-08 14:53:34 +02001877EXPORT_SYMBOL_GPL(usb_hub_release_port);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001878
Valentina Manea9b6f0c42014-03-10 10:36:40 +02001879void usb_hub_release_all_ports(struct usb_device *hdev, struct usb_dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001880{
Lan Tianyuad493e52013-01-23 04:26:30 +08001881 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001882 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001883
Lan Tianyufa2a9562012-09-05 13:44:31 +08001884 for (n = 0; n < hdev->maxchild; n++) {
1885 if (hub->ports[n]->port_owner == owner)
1886 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001887 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001888
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001889}
1890
1891/* The caller must hold udev's lock */
1892bool usb_device_is_owned(struct usb_device *udev)
1893{
1894 struct usb_hub *hub;
1895
1896 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1897 return false;
Lan Tianyuad493e52013-01-23 04:26:30 +08001898 hub = usb_hub_to_struct_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001899 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001900}
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1903{
Lan Tianyuad493e52013-01-23 04:26:30 +08001904 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 int i;
1906
1907 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c792012-09-05 13:44:32 +08001908 if (hub->ports[i]->child)
1909 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001911 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001912 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 udev->state = USB_STATE_NOTATTACHED;
1914}
1915
1916/**
1917 * usb_set_device_state - change a device's current state (usbcore, hcds)
1918 * @udev: pointer to device whose state should be changed
1919 * @new_state: new state value to be stored
1920 *
1921 * udev->state is _not_ fully protected by the device lock. Although
1922 * most transitions are made only while holding the lock, the state can
1923 * can change to USB_STATE_NOTATTACHED at almost any time. This
1924 * is so that devices can be marked as disconnected as soon as possible,
1925 * without having to wait for any semaphores to be released. As a result,
1926 * all changes to any device's state must be protected by the
1927 * device_state_lock spinlock.
1928 *
1929 * Once a device has been added to the device tree, all changes to its state
1930 * should be made using this routine. The state should _not_ be set directly.
1931 *
1932 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1933 * Otherwise udev->state is set to new_state, and if new_state is
1934 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1935 * to USB_STATE_NOTATTACHED.
1936 */
1937void usb_set_device_state(struct usb_device *udev,
1938 enum usb_device_state new_state)
1939{
1940 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001941 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 spin_lock_irqsave(&device_state_lock, flags);
1944 if (udev->state == USB_STATE_NOTATTACHED)
1945 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001946 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001947
1948 /* root hub wakeup capabilities are managed out-of-band
1949 * and may involve silicon errata ... ignore them here.
1950 */
1951 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001952 if (udev->state == USB_STATE_SUSPENDED
1953 || new_state == USB_STATE_SUSPENDED)
1954 ; /* No change to wakeup settings */
1955 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001956 wakeup = udev->actconfig->desc.bmAttributes
1957 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001958 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001959 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001960 }
Sarah Sharp15123002007-12-21 16:54:15 -08001961 if (udev->state == USB_STATE_SUSPENDED &&
1962 new_state != USB_STATE_SUSPENDED)
1963 udev->active_duration -= jiffies;
1964 else if (new_state == USB_STATE_SUSPENDED &&
1965 udev->state != USB_STATE_SUSPENDED)
1966 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001967 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001968 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 recursively_mark_NOTATTACHED(udev);
1970 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001971 if (wakeup >= 0)
1972 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
David Vrabel6da9c992009-02-18 14:43:47 +00001974EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001976/*
Alan Stern3b29b682011-02-22 09:53:41 -05001977 * Choose a device number.
1978 *
1979 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1980 * USB-2.0 buses they are also used as device addresses, however on
1981 * USB-3.0 buses the address is assigned by the controller hardware
1982 * and it usually is not the same as the device number.
1983 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001984 * WUSB devices are simple: they have no hubs behind, so the mapping
1985 * device <-> virtual port number becomes 1:1. Why? to simplify the
1986 * life of the device connection logic in
1987 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1988 * handshake we need to assign a temporary address in the unauthorized
1989 * space. For simplicity we use the first virtual port number found to
1990 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1991 * and that becomes it's address [X < 128] or its unauthorized address
1992 * [X | 0x80].
1993 *
1994 * We add 1 as an offset to the one-based USB-stack port number
1995 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1996 * 0 is reserved by USB for default address; (b) Linux's USB stack
1997 * uses always #1 for the root hub of the controller. So USB stack's
1998 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001999 *
2000 * Devices connected under xHCI are not as simple. The host controller
2001 * supports virtualization, so the hardware assigns device addresses and
2002 * the HCD must setup data structures before issuing a set address
2003 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002004 */
Alan Stern3b29b682011-02-22 09:53:41 -05002005static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 int devnum;
2008 struct usb_bus *bus = udev->bus;
2009
2010 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002011 if (udev->wusb) {
2012 devnum = udev->portnum + 1;
2013 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
2014 } else {
2015 /* Try to allocate the next devnum beginning at
2016 * bus->devnum_next. */
2017 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2018 bus->devnum_next);
2019 if (devnum >= 128)
2020 devnum = find_next_zero_bit(bus->devmap.devicemap,
2021 128, 1);
Matthias Beyer469271f2013-10-10 23:41:27 +02002022 bus->devnum_next = (devnum >= 127 ? 1 : devnum + 1);
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (devnum < 128) {
2025 set_bit(devnum, bus->devmap.devicemap);
2026 udev->devnum = devnum;
2027 }
2028}
2029
Alan Stern3b29b682011-02-22 09:53:41 -05002030static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 if (udev->devnum > 0) {
2033 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2034 udev->devnum = -1;
2035 }
2036}
2037
Alan Stern3b29b682011-02-22 09:53:41 -05002038static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07002039{
2040 /* The address for a WUSB device is managed by wusbcore. */
2041 if (!udev->wusb)
2042 udev->devnum = devnum;
2043}
2044
Herbert Xuf7410ce2010-01-10 20:15:03 +11002045static void hub_free_dev(struct usb_device *udev)
2046{
2047 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2048
2049 /* Root hubs aren't real devices, so don't free HCD resources */
2050 if (hcd->driver->free_dev && udev->parent)
2051 hcd->driver->free_dev(hcd, udev);
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054/**
2055 * usb_disconnect - disconnect a device (usbcore-internal)
2056 * @pdev: pointer to device being disconnected
2057 * Context: !in_interrupt ()
2058 *
2059 * Something got disconnected. Get rid of it and all of its children.
2060 *
2061 * If *pdev is a normal device then the parent hub must already be locked.
Bjorn Helgaasdb8f2aa2013-09-13 13:57:34 -06002062 * If *pdev is a root hub then the caller must hold the usb_bus_list_lock,
2063 * which protects the set of root hubs as well as the list of buses.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 *
2065 * Only hub drivers (including virtual root hub drivers for host
2066 * controllers) should ever call this.
2067 *
2068 * This call is synchronous, and may not be used in an interrupt context.
2069 */
2070void usb_disconnect(struct usb_device **pdev)
2071{
2072 struct usb_device *udev = *pdev;
Lan Tianyuad493e52013-01-23 04:26:30 +08002073 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 int i;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /* mark the device as inactive, so any further urb submissions for
2077 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002078 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 */
2080 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002081 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2082 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002084 usb_lock_device(udev);
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08002087 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c792012-09-05 13:44:32 +08002088 if (hub->ports[i]->child)
2089 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091
2092 /* deallocate hcd/hardware state ... nuking all pending urbs and
2093 * cleaning up all state associated with the current configuration
2094 * so that the hardware is now fully quiesced.
2095 */
Alan Stern782da722006-07-01 22:09:35 -04002096 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002098 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Lan Tianyufde26382013-01-20 01:53:33 +08002100 if (udev->parent) {
Dan Williamsd5c38342014-05-20 18:08:52 -07002101 int port1 = udev->portnum;
Lan Tianyuad493e52013-01-23 04:26:30 +08002102 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Dan Williamsd5c38342014-05-20 18:08:52 -07002103 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002104
2105 sysfs_remove_link(&udev->dev.kobj, "port");
2106 sysfs_remove_link(&port_dev->dev.kobj, "device");
Lan Tianyu971fcd42013-01-23 04:26:29 +08002107
Dan Williamsd5c38342014-05-20 18:08:52 -07002108 if (test_and_clear_bit(port1, hub->child_usage_bits))
Lan Tianyuad493e52013-01-23 04:26:30 +08002109 pm_runtime_put(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002110 }
2111
Alan Stern3b23dd62008-12-05 14:10:34 -05002112 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002113 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002114
Alan Stern782da722006-07-01 22:09:35 -04002115 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002116 * for de-configuring the device and invoking the remove-device
2117 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002118 */
2119 device_del(&udev->dev);
2120
2121 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 * (or root_hub) pointer.
2123 */
Alan Stern3b29b682011-02-22 09:53:41 -05002124 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 /* Avoid races with recursively_mark_NOTATTACHED() */
2127 spin_lock_irq(&device_state_lock);
2128 *pdev = NULL;
2129 spin_unlock_irq(&device_state_lock);
2130
Herbert Xuf7410ce2010-01-10 20:15:03 +11002131 hub_free_dev(udev);
2132
Alan Stern782da722006-07-01 22:09:35 -04002133 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002136#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137static void show_string(struct usb_device *udev, char *id, char *string)
2138{
2139 if (!string)
2140 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002141 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002144static void announce_device(struct usb_device *udev)
2145{
2146 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2147 le16_to_cpu(udev->descriptor.idVendor),
2148 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002149 dev_info(&udev->dev,
2150 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002151 udev->descriptor.iManufacturer,
2152 udev->descriptor.iProduct,
2153 udev->descriptor.iSerialNumber);
2154 show_string(udev, "Product", udev->product);
2155 show_string(udev, "Manufacturer", udev->manufacturer);
2156 show_string(udev, "SerialNumber", udev->serial);
2157}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002159static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160#endif
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#ifdef CONFIG_USB_OTG
2163#include "otg_whitelist.h"
2164#endif
2165
Oliver Neukum3ede7602007-01-11 14:35:50 +01002166/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002167 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002168 * @udev: newly addressed device (in ADDRESS state)
2169 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002170 * Finish enumeration for On-The-Go devices
Yacine Belkadi626f0902013-08-02 20:10:04 +02002171 *
2172 * Return: 0 if successful. A negative error code otherwise.
Oliver Neukum3ede7602007-01-11 14:35:50 +01002173 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002174static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002176 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178#ifdef CONFIG_USB_OTG
2179 /*
2180 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2181 * to wake us after we've powered off VBUS; and HNP, switching roles
2182 * "host" to "peripheral". The OTG descriptor helps figure this out.
2183 */
2184 if (!udev->bus->is_b_host
2185 && udev->config
2186 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002187 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 struct usb_bus *bus = udev->bus;
2189
2190 /* descriptor may appear anywhere in config */
2191 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2192 le16_to_cpu(udev->config[0].desc.wTotalLength),
2193 USB_DT_OTG, (void **) &desc) == 0) {
2194 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002195 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 dev_info(&udev->dev,
2198 "Dual-Role OTG device on %sHNP port\n",
2199 (port1 == bus->otg_port)
2200 ? "" : "non-");
2201
2202 /* enable HNP before suspend, it's simpler */
2203 if (port1 == bus->otg_port)
2204 bus->b_hnp_enable = 1;
2205 err = usb_control_msg(udev,
2206 usb_sndctrlpipe(udev, 0),
2207 USB_REQ_SET_FEATURE, 0,
2208 bus->b_hnp_enable
2209 ? USB_DEVICE_B_HNP_ENABLE
2210 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2211 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2212 if (err < 0) {
2213 /* OTG MESSAGE: report errors here,
2214 * customize to match your product.
2215 */
2216 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002217 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 err);
2219 bus->b_hnp_enable = 0;
2220 }
2221 }
2222 }
2223 }
2224
2225 if (!is_targeted(udev)) {
2226
2227 /* Maybe it can talk to us, though we can't talk to it.
2228 * (Includes HNP test device.)
2229 */
2230 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002231 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (err < 0)
2233 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2234 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002235 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 goto fail;
2237 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002238fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002240 return err;
2241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002243
2244/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002245 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002246 * @udev: newly addressed device (in ADDRESS state)
2247 *
2248 * This is only called by usb_new_device() and usb_authorize_device()
2249 * and FIXME -- all comments that apply to them apply here wrt to
2250 * environment.
2251 *
2252 * If the device is WUSB and not authorized, we don't attempt to read
2253 * the string descriptors, as they will be errored out by the device
2254 * until it has been authorized.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002255 *
2256 * Return: 0 if successful. A negative error code otherwise.
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002257 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002258static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002259{
2260 int err;
2261
2262 if (udev->config == NULL) {
2263 err = usb_get_configuration(udev);
2264 if (err < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04002265 if (err != -ENODEV)
2266 dev_err(&udev->dev, "can't read configurations, error %d\n",
2267 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002268 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002269 }
2270 }
Thomas Pugliese83e83ec2013-12-09 13:40:29 -06002271
2272 /* read the standard strings and cache them if present */
2273 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2274 udev->manufacturer = usb_cache_string(udev,
2275 udev->descriptor.iManufacturer);
2276 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2277
Alan Stern8d8558d2009-12-08 15:50:41 -05002278 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002279 if (err < 0)
2280 return err;
2281
2282 usb_detect_interface_quirks(udev);
2283
2284 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002285}
2286
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002287static void set_usb_port_removable(struct usb_device *udev)
2288{
2289 struct usb_device *hdev = udev->parent;
2290 struct usb_hub *hub;
2291 u8 port = udev->portnum;
2292 u16 wHubCharacteristics;
2293 bool removable = true;
2294
2295 if (!hdev)
2296 return;
2297
Lan Tianyuad493e52013-01-23 04:26:30 +08002298 hub = usb_hub_to_struct_hub(udev->parent);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002299
2300 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2301
2302 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2303 return;
2304
2305 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002306 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2307 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002308 removable = false;
2309 } else {
2310 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2311 removable = false;
2312 }
2313
2314 if (removable)
2315 udev->removable = USB_DEVICE_REMOVABLE;
2316 else
2317 udev->removable = USB_DEVICE_FIXED;
Dan Williamsa4204ff2014-05-20 18:08:22 -07002318
2319 /*
2320 * Platform firmware may have populated an alternative value for
2321 * removable. If the parent port has a known connect_type use
2322 * that instead.
2323 */
2324 switch (hub->ports[udev->portnum - 1]->connect_type) {
2325 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
2326 udev->removable = USB_DEVICE_REMOVABLE;
2327 break;
2328 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
2329 udev->removable = USB_DEVICE_FIXED;
2330 break;
2331 default: /* use what was set above */
2332 break;
2333 }
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002334}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002335
2336/**
2337 * usb_new_device - perform initial device setup (usbcore-internal)
2338 * @udev: newly addressed device (in ADDRESS state)
2339 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002340 * This is called with devices which have been detected but not fully
2341 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002342 * for any device configuration. The caller must have locked either
2343 * the parent hub (if udev is a normal device) or else the
2344 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2345 * udev has already been installed, but udev is not yet visible through
2346 * sysfs or other filesystem code.
2347 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002348 * This call is synchronous, and may not be used in an interrupt context.
2349 *
2350 * Only the hub driver or root-hub registrar should ever call this.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002351 *
2352 * Return: Whether the device is configured properly or not. Zero if the
2353 * interface was registered with the driver core; else a negative errno
2354 * value.
2355 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002356 */
2357int usb_new_device(struct usb_device *udev)
2358{
2359 int err;
2360
Dan Streetman16985402010-01-06 09:56:53 -05002361 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002362 /* Initialize non-root-hub device wakeup to disabled;
2363 * device (un)configuration controls wakeup capable
2364 * sysfs power/wakeup controls wakeup enabled/disabled
2365 */
2366 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002367 }
2368
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002369 /* Tell the runtime-PM framework the device is active */
2370 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002371 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002372 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002373 pm_runtime_enable(&udev->dev);
2374
Alan Sternc08512c2010-11-15 15:57:58 -05002375 /* By default, forbid autosuspend for all devices. It will be
2376 * allowed for hubs during binding.
2377 */
2378 usb_disable_autosuspend(udev);
2379
Alan Stern8d8558d2009-12-08 15:50:41 -05002380 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002381 if (err < 0)
2382 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002383 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2384 udev->devnum, udev->bus->busnum,
2385 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002386 /* export the usbdev device-node for libusb */
2387 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2388 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2389
Alan Stern6cd13202008-11-13 15:08:30 -05002390 /* Tell the world! */
2391 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002392
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002393 if (udev->serial)
2394 add_device_randomness(udev->serial, strlen(udev->serial));
2395 if (udev->product)
2396 add_device_randomness(udev->product, strlen(udev->product));
2397 if (udev->manufacturer)
2398 add_device_randomness(udev->manufacturer,
2399 strlen(udev->manufacturer));
2400
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002401 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002402
Dan Williamsa4204ff2014-05-20 18:08:22 -07002403 /* check whether the hub or firmware marks this port as non-removable */
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002404 if (udev->parent)
2405 set_usb_port_removable(udev);
2406
Alan Stern782da722006-07-01 22:09:35 -04002407 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002408 * for configuring the device and invoking the add-device
2409 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002410 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002411 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (err) {
2413 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2414 goto fail;
2415 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002416
Lan Tianyufde26382013-01-20 01:53:33 +08002417 /* Create link files between child device and usb port device. */
2418 if (udev->parent) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002419 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
Dan Williamsd5c38342014-05-20 18:08:52 -07002420 int port1 = udev->portnum;
2421 struct usb_port *port_dev = hub->ports[port1 - 1];
Lan Tianyufde26382013-01-20 01:53:33 +08002422
2423 err = sysfs_create_link(&udev->dev.kobj,
2424 &port_dev->dev.kobj, "port");
2425 if (err)
2426 goto fail;
2427
2428 err = sysfs_create_link(&port_dev->dev.kobj,
2429 &udev->dev.kobj, "device");
2430 if (err) {
2431 sysfs_remove_link(&udev->dev.kobj, "port");
2432 goto fail;
2433 }
Lan Tianyu971fcd42013-01-23 04:26:29 +08002434
Dan Williamsd5c38342014-05-20 18:08:52 -07002435 if (!test_and_set_bit(port1, hub->child_usage_bits))
2436 pm_runtime_get_sync(&port_dev->dev);
Lan Tianyufde26382013-01-20 01:53:33 +08002437 }
2438
Alan Stern3b23dd62008-12-05 14:10:34 -05002439 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002440 usb_mark_last_busy(udev);
2441 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002442 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444fail:
2445 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002446 pm_runtime_disable(&udev->dev);
2447 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002451
2452/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002453 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2454 * @usb_dev: USB device
2455 *
2456 * Move the USB device to a very basic state where interfaces are disabled
2457 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002458 *
2459 * We share a lock (that we have) with device_del(), so we need to
2460 * defer its call.
Yacine Belkadi626f0902013-08-02 20:10:04 +02002461 *
2462 * Return: 0.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002463 */
2464int usb_deauthorize_device(struct usb_device *usb_dev)
2465{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002466 usb_lock_device(usb_dev);
2467 if (usb_dev->authorized == 0)
2468 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002469
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002470 usb_dev->authorized = 0;
2471 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002472
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002473out_unauthorized:
2474 usb_unlock_device(usb_dev);
2475 return 0;
2476}
2477
2478
2479int usb_authorize_device(struct usb_device *usb_dev)
2480{
2481 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002482
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002483 usb_lock_device(usb_dev);
2484 if (usb_dev->authorized == 1)
2485 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002486
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002487 result = usb_autoresume_device(usb_dev);
2488 if (result < 0) {
2489 dev_err(&usb_dev->dev,
2490 "can't autoresume for authorization: %d\n", result);
2491 goto error_autoresume;
2492 }
2493 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2494 if (result < 0) {
2495 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2496 "authorization: %d\n", result);
2497 goto error_device_descriptor;
2498 }
Alan Sternda307122009-12-08 15:54:44 -05002499
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002500 usb_dev->authorized = 1;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002501 /* Choose and set the configuration. This registers the interfaces
2502 * with the driver core and lets interface drivers bind to them.
2503 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002504 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002505 if (c >= 0) {
2506 result = usb_set_configuration(usb_dev, c);
2507 if (result) {
2508 dev_err(&usb_dev->dev,
2509 "can't set config #%d, error %d\n", c, result);
2510 /* This need not be fatal. The user can try to
2511 * set other configurations. */
2512 }
2513 }
2514 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002515
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002516error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002517 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002518error_autoresume:
2519out_authorized:
Matthias Beyer781b2132013-10-10 23:41:29 +02002520 usb_unlock_device(usb_dev); /* complements locktree */
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002521 return result;
2522}
2523
2524
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002525/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2526static unsigned hub_is_wusb(struct usb_hub *hub)
2527{
2528 struct usb_hcd *hcd;
2529 if (hub->hdev->parent != NULL) /* not a root hub? */
2530 return 0;
2531 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2532 return hcd->wireless;
2533}
2534
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536#define PORT_RESET_TRIES 5
2537#define SET_ADDRESS_TRIES 2
2538#define GET_DESCRIPTOR_TRIES 2
2539#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302540#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2543#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002544#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545#define HUB_LONG_RESET_TIME 200
Sarah Sharp77c7f072012-11-14 17:16:52 -08002546#define HUB_RESET_TIMEOUT 800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Dan Williams48fc7db2013-12-05 17:07:27 -08002548/*
2549 * "New scheme" enumeration causes an extra state transition to be
2550 * exposed to an xhci host and causes USB3 devices to receive control
2551 * commands in the default state. This has been seen to cause
2552 * enumeration failures, so disable this enumeration scheme for USB3
2553 * devices.
2554 */
2555static bool use_new_scheme(struct usb_device *udev, int retry)
2556{
2557 if (udev->speed == USB_SPEED_SUPER)
2558 return false;
2559
2560 return USE_NEW_SCHEME(retry);
2561}
2562
Sarah Sharp10d674a2011-09-14 14:24:52 -07002563static int hub_port_reset(struct usb_hub *hub, int port1,
2564 struct usb_device *udev, unsigned int delay, bool warm);
2565
Rahul Bedarkar025d4432014-01-04 11:24:41 +05302566/* Is a USB 3.0 port in the Inactive or Compliance Mode state?
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002567 * Port worm reset is required to recover
2568 */
2569static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002570{
2571 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002572 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2573 USB_SS_PORT_LS_SS_INACTIVE) ||
2574 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2575 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002576}
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002579 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
2581 int delay_time, ret;
2582 u16 portstatus;
2583 u16 portchange;
2584
2585 for (delay_time = 0;
2586 delay_time < HUB_RESET_TIMEOUT;
2587 delay_time += delay) {
2588 /* wait to give the device a chance to reset */
2589 msleep(delay);
2590
2591 /* read and decode port status */
2592 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2593 if (ret < 0)
2594 return ret;
2595
Sarah Sharp4f434472012-11-15 14:58:04 -08002596 /* The port state is unknown until the reset completes. */
Sarah Sharp470f0be2012-12-11 10:14:03 -08002597 if (!(portstatus & USB_PORT_STAT_RESET))
2598 break;
Sarah Sharp4f434472012-11-15 14:58:04 -08002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* switch to the long delay after two short delay failures */
2601 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2602 delay = HUB_LONG_RESET_TIME;
2603
Dan Williamsd99f6b42014-05-20 18:08:17 -07002604 dev_dbg(&hub->ports[port1 - 1]->dev,
2605 "not %sreset yet, waiting %dms\n",
2606 warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
2608
Sarah Sharp470f0be2012-12-11 10:14:03 -08002609 if ((portstatus & USB_PORT_STAT_RESET))
2610 return -EBUSY;
2611
2612 if (hub_port_warm_reset_required(hub, portstatus))
2613 return -ENOTCONN;
2614
2615 /* Device went away? */
2616 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2617 return -ENOTCONN;
2618
2619 /* bomb out completely if the connection bounced. A USB 3.0
2620 * connection may bounce if multiple warm resets were issued,
2621 * but the device may have successfully re-connected. Ignore it.
2622 */
2623 if (!hub_is_superspeed(hub->hdev) &&
2624 (portchange & USB_PORT_STAT_C_CONNECTION))
2625 return -ENOTCONN;
2626
2627 if (!(portstatus & USB_PORT_STAT_ENABLE))
2628 return -EBUSY;
2629
2630 if (!udev)
2631 return 0;
2632
2633 if (hub_is_wusb(hub))
2634 udev->speed = USB_SPEED_WIRELESS;
2635 else if (hub_is_superspeed(hub->hdev))
2636 udev->speed = USB_SPEED_SUPER;
2637 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2638 udev->speed = USB_SPEED_HIGH;
2639 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2640 udev->speed = USB_SPEED_LOW;
2641 else
2642 udev->speed = USB_SPEED_FULL;
2643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Andiry Xu75d7cf72011-09-13 16:41:11 -07002646static void hub_port_finish_reset(struct usb_hub *hub, int port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002647 struct usb_device *udev, int *status)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002648{
2649 switch (*status) {
2650 case 0:
Sarah Sharpa24a6072012-11-01 11:20:44 -07002651 /* TRSTRCY = 10 ms; plus some extra */
2652 msleep(10 + 40);
2653 if (udev) {
2654 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2655
2656 update_devnum(udev, 0);
2657 /* The xHC may think the device is already reset,
2658 * so ignore the status.
2659 */
2660 if (hcd->driver->reset_device)
2661 hcd->driver->reset_device(hcd, udev);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002662 }
2663 /* FALL THROUGH */
2664 case -ENOTCONN:
2665 case -ENODEV:
Lan Tianyuad493e52013-01-23 04:26:30 +08002666 usb_clear_port_feature(hub->hdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002667 port1, USB_PORT_FEAT_C_RESET);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002668 if (hub_is_superspeed(hub->hdev)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08002669 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002670 USB_PORT_FEAT_C_BH_PORT_RESET);
Lan Tianyuad493e52013-01-23 04:26:30 +08002671 usb_clear_port_feature(hub->hdev, port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002672 USB_PORT_FEAT_C_PORT_LINK_STATE);
Lan Tianyuad493e52013-01-23 04:26:30 +08002673 usb_clear_port_feature(hub->hdev, port1,
Sarah Sharpa24a6072012-11-01 11:20:44 -07002674 USB_PORT_FEAT_C_CONNECTION);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002675 }
Sarah Sharpa24a6072012-11-01 11:20:44 -07002676 if (udev)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002677 usb_set_device_state(udev, *status
2678 ? USB_STATE_NOTATTACHED
2679 : USB_STATE_DEFAULT);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002680 break;
2681 }
2682}
2683
2684/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002686 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
2688 int i, status;
Sarah Sharpa24a6072012-11-01 11:20:44 -07002689 u16 portchange, portstatus;
Dan Williamsd99f6b42014-05-20 18:08:17 -07002690 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002692 if (!hub_is_superspeed(hub->hdev)) {
2693 if (warm) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002694 dev_err(hub->intfdev, "only USB3 hub support "
2695 "warm reset\n");
2696 return -EINVAL;
2697 }
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002698 /* Block EHCI CF initialization during the port reset.
2699 * Some companion controllers don't like it when they mix.
2700 */
2701 down_read(&ehci_cf_port_reset_rwsem);
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07002702 } else if (!warm) {
2703 /*
2704 * If the caller hasn't explicitly requested a warm reset,
2705 * double check and see if one is needed.
2706 */
2707 status = hub_port_status(hub, port1,
2708 &portstatus, &portchange);
2709 if (status < 0)
2710 goto done;
2711
2712 if (hub_port_warm_reset_required(hub, portstatus))
2713 warm = true;
Andiry Xu75d7cf72011-09-13 16:41:11 -07002714 }
Alan Stern32fe0192007-10-10 16:27:07 -04002715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 /* Reset the port */
2717 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002718 status = set_port_feature(hub->hdev, port1, (warm ?
2719 USB_PORT_FEAT_BH_PORT_RESET :
2720 USB_PORT_FEAT_RESET));
Alan Sterne9e88fb2013-03-27 16:14:01 -04002721 if (status == -ENODEV) {
2722 ; /* The hub is gone */
2723 } else if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002724 dev_err(&port_dev->dev,
2725 "cannot %sreset (err = %d)\n",
2726 warm ? "warm " : "", status);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002727 } else {
2728 status = hub_port_wait_reset(hub, port1, udev, delay,
2729 warm);
Alan Sterne9e88fb2013-03-27 16:14:01 -04002730 if (status && status != -ENOTCONN && status != -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 dev_dbg(hub->intfdev,
2732 "port_wait_reset: err = %d\n",
2733 status);
2734 }
2735
Sarah Sharpa24a6072012-11-01 11:20:44 -07002736 /* Check for disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002737 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
Sarah Sharpa24a6072012-11-01 11:20:44 -07002738 hub_port_finish_reset(hub, port1, udev, &status);
2739
2740 if (!hub_is_superspeed(hub->hdev))
2741 goto done;
2742
2743 /*
2744 * If a USB 3.0 device migrates from reset to an error
2745 * state, re-issue the warm reset.
2746 */
2747 if (hub_port_status(hub, port1,
2748 &portstatus, &portchange) < 0)
2749 goto done;
2750
2751 if (!hub_port_warm_reset_required(hub, portstatus))
2752 goto done;
2753
2754 /*
2755 * If the port is in SS.Inactive or Compliance Mode, the
2756 * hot or warm reset failed. Try another warm reset.
2757 */
2758 if (!warm) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002759 dev_dbg(&port_dev->dev,
2760 "hot reset failed, warm reset\n");
Sarah Sharpa24a6072012-11-01 11:20:44 -07002761 warm = true;
2762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764
Dan Williamsd99f6b42014-05-20 18:08:17 -07002765 dev_dbg(&port_dev->dev,
2766 "not enabled, trying %sreset again...\n",
2767 warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 delay = HUB_LONG_RESET_TIME;
2769 }
2770
Dan Williamsd99f6b42014-05-20 18:08:17 -07002771 dev_err(&port_dev->dev, "Cannot enable. Maybe the USB cable is bad?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Andiry Xu75d7cf72011-09-13 16:41:11 -07002773done:
Sarah Sharp0fe51aa2012-11-01 11:20:44 -07002774 if (!hub_is_superspeed(hub->hdev))
Andiry Xu75d7cf72011-09-13 16:41:11 -07002775 up_read(&ehci_cf_port_reset_rwsem);
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return status;
2778}
2779
Andiry Xu0ed9a572011-04-27 18:07:43 +08002780/* Check if a port is power on */
2781static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2782{
2783 int ret = 0;
2784
2785 if (hub_is_superspeed(hub->hdev)) {
2786 if (portstatus & USB_SS_PORT_STAT_POWER)
2787 ret = 1;
2788 } else {
2789 if (portstatus & USB_PORT_STAT_POWER)
2790 ret = 1;
2791 }
2792
2793 return ret;
2794}
2795
Alan Sternd388dab2006-07-01 22:14:24 -04002796#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Andiry Xu0ed9a572011-04-27 18:07:43 +08002798/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2799static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2800{
2801 int ret = 0;
2802
2803 if (hub_is_superspeed(hub->hdev)) {
2804 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2805 == USB_SS_PORT_LS_U3)
2806 ret = 1;
2807 } else {
2808 if (portstatus & USB_PORT_STAT_SUSPEND)
2809 ret = 1;
2810 }
2811
2812 return ret;
2813}
Alan Sternb01b03f2008-04-28 11:06:11 -04002814
2815/* Determine whether the device on a port is ready for a normal resume,
2816 * is ready for a reset-resume, or should be disconnected.
2817 */
2818static int check_port_resume_type(struct usb_device *udev,
2819 struct usb_hub *hub, int port1,
2820 int status, unsigned portchange, unsigned portstatus)
2821{
Dan Williamsd99f6b42014-05-20 18:08:17 -07002822 struct usb_port *port_dev = hub->ports[port1 - 1];
2823
Alan Sternb01b03f2008-04-28 11:06:11 -04002824 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002825 if (status || port_is_suspended(hub, portstatus) ||
2826 !port_is_power_on(hub, portstatus) ||
2827 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002828 if (status >= 0)
2829 status = -ENODEV;
2830 }
2831
Alan Stern86c57ed2008-06-30 11:14:43 -04002832 /* Can't do a normal resume if the port isn't enabled,
2833 * so try a reset-resume instead.
2834 */
2835 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2836 if (udev->persist_enabled)
2837 udev->reset_resume = 1;
2838 else
2839 status = -ENODEV;
2840 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002841
2842 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07002843 dev_dbg(&port_dev->dev, "status %04x.%04x after resume, %d\n",
2844 portchange, portstatus, status);
Alan Sternb01b03f2008-04-28 11:06:11 -04002845 } else if (udev->reset_resume) {
2846
2847 /* Late port handoff can set status-change bits */
2848 if (portchange & USB_PORT_STAT_C_CONNECTION)
Lan Tianyuad493e52013-01-23 04:26:30 +08002849 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002850 USB_PORT_FEAT_C_CONNECTION);
2851 if (portchange & USB_PORT_STAT_C_ENABLE)
Lan Tianyuad493e52013-01-23 04:26:30 +08002852 usb_clear_port_feature(hub->hdev, port1,
Alan Sternb01b03f2008-04-28 11:06:11 -04002853 USB_PORT_FEAT_C_ENABLE);
2854 }
2855
2856 return status;
2857}
2858
Sarah Sharpf74631e2012-06-25 12:08:08 -07002859int usb_disable_ltm(struct usb_device *udev)
2860{
2861 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2862
2863 /* Check if the roothub and device supports LTM. */
2864 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2865 !usb_device_supports_ltm(udev))
2866 return 0;
2867
2868 /* Clear Feature LTM Enable can only be sent if the device is
2869 * configured.
2870 */
2871 if (!udev->actconfig)
2872 return 0;
2873
2874 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2875 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2876 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2877 USB_CTRL_SET_TIMEOUT);
2878}
2879EXPORT_SYMBOL_GPL(usb_disable_ltm);
2880
2881void usb_enable_ltm(struct usb_device *udev)
2882{
2883 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2884
2885 /* Check if the roothub and device supports LTM. */
2886 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2887 !usb_device_supports_ltm(udev))
2888 return;
2889
2890 /* Set Feature LTM Enable can only be sent if the device is
2891 * configured.
2892 */
2893 if (!udev->actconfig)
2894 return;
2895
2896 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2897 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2898 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2899 USB_CTRL_SET_TIMEOUT);
2900}
2901EXPORT_SYMBOL_GPL(usb_enable_ltm);
2902
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002903/*
Alan Stern28e861652013-07-30 15:37:33 -04002904 * usb_enable_remote_wakeup - enable remote wakeup for a device
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002905 * @udev: target device
2906 *
Alan Stern28e861652013-07-30 15:37:33 -04002907 * For USB-2 devices: Set the device's remote wakeup feature.
2908 *
2909 * For USB-3 devices: Assume there's only one function on the device and
2910 * enable remote wake for the first interface. FIXME if the interface
2911 * association descriptor shows there's more than one function.
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002912 */
Alan Stern28e861652013-07-30 15:37:33 -04002913static int usb_enable_remote_wakeup(struct usb_device *udev)
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002914{
Alan Stern28e861652013-07-30 15:37:33 -04002915 if (udev->speed < USB_SPEED_SUPER)
2916 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2917 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2918 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
2919 USB_CTRL_SET_TIMEOUT);
2920 else
2921 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2922 USB_REQ_SET_FEATURE, USB_RECIP_INTERFACE,
2923 USB_INTRF_FUNC_SUSPEND,
2924 USB_INTRF_FUNC_SUSPEND_RW |
2925 USB_INTRF_FUNC_SUSPEND_LP,
2926 NULL, 0, USB_CTRL_SET_TIMEOUT);
2927}
2928
2929/*
2930 * usb_disable_remote_wakeup - disable remote wakeup for a device
2931 * @udev: target device
2932 *
2933 * For USB-2 devices: Clear the device's remote wakeup feature.
2934 *
2935 * For USB-3 devices: Assume there's only one function on the device and
2936 * disable remote wake for the first interface. FIXME if the interface
2937 * association descriptor shows there's more than one function.
2938 */
2939static int usb_disable_remote_wakeup(struct usb_device *udev)
2940{
2941 if (udev->speed < USB_SPEED_SUPER)
2942 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2943 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2944 USB_DEVICE_REMOTE_WAKEUP, 0, NULL, 0,
2945 USB_CTRL_SET_TIMEOUT);
2946 else
2947 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Lan Tianyu54a3ac02013-01-24 10:31:28 +08002948 USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE,
2949 USB_INTRF_FUNC_SUSPEND, 0, NULL, 0,
2950 USB_CTRL_SET_TIMEOUT);
2951}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Alan Sterne583d9d2013-07-11 14:58:04 -04002953/* Count of wakeup-enabled devices at or below udev */
2954static unsigned wakeup_enabled_descendants(struct usb_device *udev)
2955{
2956 struct usb_hub *hub = usb_hub_to_struct_hub(udev);
2957
2958 return udev->do_remote_wakeup +
2959 (hub ? hub->wakeup_enabled_descendants : 0);
2960}
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962/*
Alan Stern140d8f62006-07-01 22:07:21 -04002963 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002964 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002965 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 *
2967 * Suspends a USB device that isn't in active use, conserving power.
2968 * Devices may wake out of a suspend, if anything important happens,
2969 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002970 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 * to disconnect devices while they are suspended.
2972 *
David Brownell390a8c32005-09-13 19:57:27 -07002973 * This only affects the USB hardware for a device; its interfaces
2974 * (and, for hubs, child devices) must already have been suspended.
2975 *
Alan Stern624d6c02007-05-30 15:35:16 -04002976 * Selective port suspend reduces power; most suspended devices draw
2977 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2978 * All devices below the suspended port are also suspended.
2979 *
2980 * Devices leave suspend state when the host wakes them up. Some devices
2981 * also support "remote wakeup", where the device can activate the USB
2982 * tree above them to deliver data, such as a keypress or packet. In
2983 * some cases, this wakes the USB host.
2984 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 * Suspending OTG devices may trigger HNP, if that's been enabled
2986 * between a pair of dual-role devices. That will change roles, such
2987 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2988 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002989 * Devices on USB hub ports have only one "suspend" state, corresponding
2990 * to ACPI D2, "may cause the device to lose some context".
2991 * State transitions include:
2992 *
2993 * - suspend, resume ... when the VBUS power link stays live
2994 * - suspend, disconnect ... VBUS lost
2995 *
2996 * Once VBUS drop breaks the circuit, the port it's using has to go through
2997 * normal re-enumeration procedures, starting with enabling VBUS power.
2998 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2999 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
3000 * timer, no SRP, no requests through sysfs.
3001 *
Alan Sterne583d9d2013-07-11 14:58:04 -04003002 * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get
3003 * suspended until their bus goes into global suspend (i.e., the root
Alan Stern0aa28322013-03-27 16:14:19 -04003004 * hub is suspended). Nevertheless, we change @udev->state to
3005 * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual
3006 * upstream port setting is stored in @udev->port_is_suspended.
Alan Stern4956ecc2007-05-30 16:51:28 -04003007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 * Returns 0 on success, else negative errno.
3009 */
Alan Stern65bfd292008-11-25 16:39:18 -05003010int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
Lan Tianyuad493e52013-01-23 04:26:30 +08003012 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3013 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003014 int port1 = udev->portnum;
3015 int status;
Alan Stern0aa28322013-03-27 16:14:19 -04003016 bool really_suspend = true;
Alan Stern4956ecc2007-05-30 16:51:28 -04003017
Alan Stern624d6c02007-05-30 15:35:16 -04003018 /* enable remote wakeup when appropriate; this lets the device
3019 * wake up the upstream hub (including maybe the root hub).
3020 *
3021 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
3022 * we don't explicitly enable it here.
3023 */
3024 if (udev->do_remote_wakeup) {
Alan Stern28e861652013-07-30 15:37:33 -04003025 status = usb_enable_remote_wakeup(udev);
Oliver Neukum0c487202009-10-19 13:19:41 +02003026 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003027 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
3028 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02003029 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003030 if (PMSG_IS_AUTO(msg))
Alan Stern4fae6f02013-07-30 15:39:02 -04003031 goto err_wakeup;
Oliver Neukum0c487202009-10-19 13:19:41 +02003032 }
Alan Stern624d6c02007-05-30 15:35:16 -04003033 }
3034
Andiry Xu65580b432011-09-23 14:19:52 -07003035 /* disable USB2 hardware LPM */
3036 if (udev->usb2_hw_lpm_enabled == 1)
3037 usb_set_usb2_hardware_lpm(udev, 0);
3038
Sarah Sharpf74631e2012-06-25 12:08:08 -07003039 if (usb_disable_ltm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003040 dev_err(&udev->dev, "Failed to disable LTM before suspend\n.");
3041 status = -ENOMEM;
3042 if (PMSG_IS_AUTO(msg))
3043 goto err_ltm;
Sarah Sharpf74631e2012-06-25 12:08:08 -07003044 }
Sarah Sharp83060952012-05-02 14:25:52 -07003045 if (usb_unlocked_disable_lpm(udev)) {
Alan Stern4fae6f02013-07-30 15:39:02 -04003046 dev_err(&udev->dev, "Failed to disable LPM before suspend\n.");
3047 status = -ENOMEM;
3048 if (PMSG_IS_AUTO(msg))
3049 goto err_lpm3;
Sarah Sharp83060952012-05-02 14:25:52 -07003050 }
3051
Alan Stern624d6c02007-05-30 15:35:16 -04003052 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08003053 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003054 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3);
Alan Sterne583d9d2013-07-11 14:58:04 -04003055
Alan Stern0aa28322013-03-27 16:14:19 -04003056 /*
3057 * For system suspend, we do not need to enable the suspend feature
3058 * on individual USB-2 ports. The devices will automatically go
3059 * into suspend a few ms after the root hub stops sending packets.
3060 * The USB 2.0 spec calls this "global suspend".
Alan Sterne583d9d2013-07-11 14:58:04 -04003061 *
3062 * However, many USB hubs have a bug: They don't relay wakeup requests
3063 * from a downstream port if the port's suspend feature isn't on.
3064 * Therefore we will turn on the suspend feature if udev or any of its
3065 * descendants is enabled for remote wakeup.
Alan Stern0aa28322013-03-27 16:14:19 -04003066 */
Alan Sterne583d9d2013-07-11 14:58:04 -04003067 else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0)
3068 status = set_port_feature(hub->hdev, port1,
3069 USB_PORT_FEAT_SUSPEND);
Alan Stern0aa28322013-03-27 16:14:19 -04003070 else {
3071 really_suspend = false;
3072 status = 0;
3073 }
Alan Stern624d6c02007-05-30 15:35:16 -04003074 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003075 dev_dbg(&port_dev->dev, "can't suspend, status %d\n", status);
Alan Sterncbb33002011-06-15 16:29:16 -04003076
Alan Stern4fae6f02013-07-30 15:39:02 -04003077 /* Try to enable USB3 LPM and LTM again */
3078 usb_unlocked_enable_lpm(udev);
3079 err_lpm3:
3080 usb_enable_ltm(udev);
3081 err_ltm:
Andiry Xuc3e751e2012-05-05 00:50:10 +08003082 /* Try to enable USB2 hardware LPM again */
3083 if (udev->usb2_hw_lpm_capable == 1)
3084 usb_set_usb2_hardware_lpm(udev, 1);
3085
Alan Stern4fae6f02013-07-30 15:39:02 -04003086 if (udev->do_remote_wakeup)
3087 (void) usb_disable_remote_wakeup(udev);
3088 err_wakeup:
Sarah Sharp83060952012-05-02 14:25:52 -07003089
Alan Sterncbb33002011-06-15 16:29:16 -04003090 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02003091 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003092 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04003093 } else {
Alan Stern30b1a7a2011-09-27 21:54:22 +02003094 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
3095 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
3096 udev->do_remote_wakeup);
Alan Stern0aa28322013-03-27 16:14:19 -04003097 if (really_suspend) {
3098 udev->port_is_suspended = 1;
Alan Sterne583d9d2013-07-11 14:58:04 -04003099
3100 /* device has up to 10 msec to fully suspend */
Alan Stern0aa28322013-03-27 16:14:19 -04003101 msleep(10);
3102 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003103 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Stern624d6c02007-05-30 15:35:16 -04003104 }
Lan Tianyuad493e52013-01-23 04:26:30 +08003105
Dan Williamsd5c38342014-05-20 18:08:52 -07003106 if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled
3107 && test_and_clear_bit(port1, hub->child_usage_bits))
Lan Tianyu98a4f1f2013-07-03 22:17:54 +08003108 pm_runtime_put_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003109
Alan Sternc08512c2010-11-15 15:57:58 -05003110 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04003111 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
David Brownellf3f32532005-09-22 22:37:29 -07003113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114/*
David Brownell390a8c32005-09-13 19:57:27 -07003115 * If the USB "suspend" state is in use (rather than "global suspend"),
3116 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04003117 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 * hardware resume signaling is finished, either because of selective
3119 * resume (by host) or remote wakeup (by device) ... now see what changed
3120 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04003121 *
3122 * If @udev->reset_resume is set then the device is reset before the
3123 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 */
Alan Stern140d8f62006-07-01 22:07:21 -04003125static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Alan Stern54515fe2007-05-30 15:38:58 -04003127 int status = 0;
Oliver Neukum07e72b92012-11-29 15:05:57 +01003128 u16 devstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003131 dev_dbg(&udev->dev, "%s\n",
3132 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 /* usb ch9 identifies four variants of SUSPENDED, based on what
3135 * state the device resumes to. Linux currently won't see the
3136 * first two on the host side; they'd be inside hub_port_init()
3137 * during many timeouts, but khubd can't suspend until later.
3138 */
3139 usb_set_device_state(udev, udev->actconfig
3140 ? USB_STATE_CONFIGURED
3141 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Alan Stern54515fe2007-05-30 15:38:58 -04003143 /* 10.5.4.5 says not to reset a suspended port if the attached
3144 * device is enabled for remote wakeup. Hence the reset
3145 * operation is carried out here, after the port has been
3146 * resumed.
3147 */
Alan Stern1d102552014-03-18 10:39:05 -04003148 if (udev->reset_resume) {
3149 /*
3150 * If the device morphs or switches modes when it is reset,
3151 * we don't want to perform a reset-resume. We'll fail the
3152 * resume, which will cause a logical disconnect, and then
3153 * the device will be rediscovered.
3154 */
Alan Stern86c57ed2008-06-30 11:14:43 -04003155 retry_reset_resume:
Alan Stern1d102552014-03-18 10:39:05 -04003156 if (udev->quirks & USB_QUIRK_RESET)
3157 status = -ENODEV;
3158 else
3159 status = usb_reset_and_verify_device(udev);
3160 }
Alan Stern54515fe2007-05-30 15:38:58 -04003161
Matthias Beyer469271f2013-10-10 23:41:27 +02003162 /* 10.5.4.5 says be sure devices in the tree are still there.
3163 * For now let's assume the device didn't go crazy on resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 * and device drivers will know about any resume quirks.
3165 */
Alan Stern54515fe2007-05-30 15:38:58 -04003166 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003167 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003168 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
Alan Stern86c57ed2008-06-30 11:14:43 -04003169
3170 /* If a normal resume failed, try doing a reset-resume */
3171 if (status && !udev->reset_resume && udev->persist_enabled) {
3172 dev_dbg(&udev->dev, "retry with reset-resume\n");
3173 udev->reset_resume = 1;
3174 goto retry_reset_resume;
3175 }
Alan Stern54515fe2007-05-30 15:38:58 -04003176 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003177
Alan Stern624d6c02007-05-30 15:35:16 -04003178 if (status) {
3179 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3180 status);
Oliver Neukum07e72b92012-11-29 15:05:57 +01003181 /*
3182 * There are a few quirky devices which violate the standard
3183 * by claiming to have remote wakeup enabled after a reset,
3184 * which crash if the feature is cleared, hence check for
3185 * udev->reset_resume
3186 */
3187 } else if (udev->actconfig && !udev->reset_resume) {
Alan Stern28e861652013-07-30 15:37:33 -04003188 if (udev->speed < USB_SPEED_SUPER) {
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003189 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP))
Alan Stern28e861652013-07-30 15:37:33 -04003190 status = usb_disable_remote_wakeup(udev);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003191 } else {
3192 status = usb_get_status(udev, USB_RECIP_INTERFACE, 0,
3193 &devstatus);
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003194 if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP
3195 | USB_INTRF_STAT_FUNC_RW))
Alan Stern28e861652013-07-30 15:37:33 -04003196 status = usb_disable_remote_wakeup(udev);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003197 }
Lan Tianyu54a3ac02013-01-24 10:31:28 +08003198
3199 if (status)
3200 dev_dbg(&udev->dev,
3201 "disable remote wakeup, status %d\n",
3202 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 }
3205 return status;
3206}
3207
Alan Stern624d6c02007-05-30 15:35:16 -04003208/*
3209 * usb_port_resume - re-activate a suspended usb device's upstream port
3210 * @udev: device to re-activate, not a root hub
3211 * Context: must be able to sleep; device not locked; pm locks held
3212 *
3213 * This will re-activate the suspended device, increasing power usage
3214 * while letting drivers communicate again with its endpoints.
3215 * USB resume explicitly guarantees that the power session between
3216 * the host and the device is the same as it was when the device
3217 * suspended.
3218 *
Alan Sternfeccc302008-03-03 15:15:59 -05003219 * If @udev->reset_resume is set then this routine won't check that the
3220 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003221 * reset @udev. The end result is that a broken power session can be
3222 * recovered and @udev will appear to persist across a loss of VBUS power.
3223 *
3224 * For example, if a host controller doesn't maintain VBUS suspend current
3225 * during a system sleep or is reset when the system wakes up, all the USB
3226 * power sessions below it will be broken. This is especially troublesome
3227 * for mass-storage devices containing mounted filesystems, since the
3228 * device will appear to have disconnected and all the memory mappings
3229 * to it will be lost. Using the USB_PERSIST facility, the device can be
3230 * made to appear as if it had not disconnected.
3231 *
Ming Lei742120c2008-06-18 22:00:29 +08003232 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003233 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003234 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3235 * quite possible for a device to remain unaltered but its media to be
3236 * changed. If the user replaces a flash memory card while the system is
3237 * asleep, he will have only himself to blame when the filesystem on the
3238 * new card is corrupted and the system crashes.
3239 *
Alan Stern624d6c02007-05-30 15:35:16 -04003240 * Returns 0 on success, else negative errno.
3241 */
Alan Stern65bfd292008-11-25 16:39:18 -05003242int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Lan Tianyuad493e52013-01-23 04:26:30 +08003244 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
3245 struct usb_port *port_dev = hub->ports[udev->portnum - 1];
Alan Stern624d6c02007-05-30 15:35:16 -04003246 int port1 = udev->portnum;
3247 int status;
3248 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003249
Dan Williamsd5c38342014-05-20 18:08:52 -07003250 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003251 status = pm_runtime_get_sync(&port_dev->dev);
Lan Tianyuad493e52013-01-23 04:26:30 +08003252 if (status < 0) {
3253 dev_dbg(&udev->dev, "can't resume usb port, status %d\n",
3254 status);
3255 return status;
3256 }
3257 }
3258
Alan Sternd25450c2006-11-20 11:14:30 -05003259 /* Skip the initial Clear-Suspend step for a remote wakeup */
3260 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003261 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003262 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Alan Sternd5cbad42006-08-11 16:52:39 -04003264 set_bit(port1, hub->busy_bits);
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003267 if (hub_is_superspeed(hub->hdev))
Sarah Sharpc2f60db2012-12-10 15:43:08 -08003268 status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0);
Andiry Xua7114232011-04-27 18:07:50 +08003269 else
Lan Tianyuad493e52013-01-23 04:26:30 +08003270 status = usb_clear_port_feature(hub->hdev,
Andiry Xua7114232011-04-27 18:07:50 +08003271 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (status) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003273 dev_dbg(&port_dev->dev, "can't resume, status %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003276 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003277 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 msleep(25);
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3281 * stop resume signaling. Then finish the resume
3282 * sequence.
3283 */
Alan Sternd25450c2006-11-20 11:14:30 -05003284 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003285
Alan Sternb01b03f2008-04-28 11:06:11 -04003286 /* TRSMRCY = 10 msec */
3287 msleep(10);
3288 }
Alan Stern54515fe2007-05-30 15:38:58 -04003289
Alan Sternb01b03f2008-04-28 11:06:11 -04003290 SuspendCleared:
3291 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003292 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003293 if (hub_is_superspeed(hub->hdev)) {
3294 if (portchange & USB_PORT_STAT_C_LINK_STATE)
Lan Tianyuad493e52013-01-23 04:26:30 +08003295 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003296 USB_PORT_FEAT_C_PORT_LINK_STATE);
3297 } else {
3298 if (portchange & USB_PORT_STAT_C_SUSPEND)
Lan Tianyuad493e52013-01-23 04:26:30 +08003299 usb_clear_port_feature(hub->hdev, port1,
Andiry Xua7114232011-04-27 18:07:50 +08003300 USB_PORT_FEAT_C_SUSPEND);
3301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Alan Sternd5cbad42006-08-11 16:52:39 -04003304 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003305
Alan Sternb01b03f2008-04-28 11:06:11 -04003306 status = check_port_resume_type(udev,
3307 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003308 if (status == 0)
3309 status = finish_port_resume(udev);
3310 if (status < 0) {
3311 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3312 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003313 } else {
3314 /* Try to enable USB2 hardware LPM */
3315 if (udev->usb2_hw_lpm_capable == 1)
3316 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003317
Sarah Sharpf74631e2012-06-25 12:08:08 -07003318 /* Try to enable USB3 LTM and LPM */
3319 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003320 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003321 }
Andiry Xu65580b432011-09-23 14:19:52 -07003322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 return status;
3324}
3325
Alan Stern84ebc102013-03-27 16:14:46 -04003326#ifdef CONFIG_PM_RUNTIME
3327
Alan Stern8808f002008-04-28 11:06:55 -04003328/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003329int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
3331 int status = 0;
3332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003334 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003335 status = usb_autoresume_device(udev);
3336 if (status == 0) {
3337 /* Let the drivers do their thing, then... */
3338 usb_autosuspend_device(udev);
3339 }
Alan Sternd25450c2006-11-20 11:14:30 -05003340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 return status;
3342}
3343
Alan Sternd388dab2006-07-01 22:14:24 -04003344#endif
3345
Ming Leie6f30de2012-10-24 11:59:24 +08003346static int check_ports_changed(struct usb_hub *hub)
3347{
3348 int port1;
3349
3350 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3351 u16 portstatus, portchange;
3352 int status;
3353
3354 status = hub_port_status(hub, port1, &portstatus, &portchange);
3355 if (!status && portchange)
3356 return 1;
3357 }
3358 return 0;
3359}
3360
David Brownelldb690872005-09-13 19:56:33 -07003361static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
3363 struct usb_hub *hub = usb_get_intfdata (intf);
3364 struct usb_device *hdev = hub->hdev;
3365 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003366 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367
Alan Sterne583d9d2013-07-11 14:58:04 -04003368 /*
3369 * Warn if children aren't already suspended.
3370 * Also, add up the number of wakeup-enabled descendants.
3371 */
3372 hub->wakeup_enabled_descendants = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003374 struct usb_port *port_dev = hub->ports[port1 - 1];
3375 struct usb_device *udev = port_dev->child;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Alan Stern6840d252007-09-10 11:34:26 -04003377 if (udev && udev->can_submit) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07003378 dev_warn(&port_dev->dev, "not suspended yet\n");
Alan Stern5b1b0b82011-08-19 23:49:48 +02003379 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003380 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003381 }
Alan Sterne583d9d2013-07-11 14:58:04 -04003382 if (udev)
3383 hub->wakeup_enabled_descendants +=
3384 wakeup_enabled_descendants(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
Ming Leie6f30de2012-10-24 11:59:24 +08003386
3387 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3388 /* check if there are changes pending on hub ports */
3389 if (check_ports_changed(hub)) {
3390 if (PMSG_IS_AUTO(msg))
3391 return -EBUSY;
3392 pm_wakeup_event(&hdev->dev, 2000);
3393 }
3394 }
3395
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003396 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3397 /* Enable hub to send remote wakeup for all ports. */
3398 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3399 status = set_port_feature(hdev,
3400 port1 |
3401 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3402 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3403 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3404 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3405 }
3406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
Harvey Harrison441b62c2008-03-03 16:08:34 -08003408 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003409
David Brownellc9f89fa2005-09-13 19:57:04 -07003410 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003411 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
3415static int hub_resume(struct usb_interface *intf)
3416{
Alan Stern5e6effa2008-03-03 15:15:51 -05003417 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
Alan Stern5e6effa2008-03-03 15:15:51 -05003419 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003420 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 return 0;
3422}
3423
Alan Sternb41a60e2007-05-30 15:39:33 -04003424static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003425{
Alan Sternb41a60e2007-05-30 15:39:33 -04003426 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003427
Alan Stern5e6effa2008-03-03 15:15:51 -05003428 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003429 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003430 return 0;
3431}
3432
Alan Stern54515fe2007-05-30 15:38:58 -04003433/**
3434 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3435 * @rhdev: struct usb_device for the root hub
3436 *
3437 * The USB host controller driver calls this function when its root hub
3438 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003439 * has been reset. The routine marks @rhdev as having lost power.
3440 * When the hub driver is resumed it will take notice and carry out
3441 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3442 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003443 */
3444void usb_root_hub_lost_power(struct usb_device *rhdev)
3445{
3446 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3447 rhdev->reset_resume = 1;
3448}
3449EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3450
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003451static const char * const usb3_lpm_names[] = {
3452 "U0",
3453 "U1",
3454 "U2",
3455 "U3",
3456};
3457
3458/*
3459 * Send a Set SEL control transfer to the device, prior to enabling
3460 * device-initiated U1 or U2. This lets the device know the exit latencies from
3461 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3462 * packet from the host.
3463 *
3464 * This function will fail if the SEL or PEL values for udev are greater than
3465 * the maximum allowed values for the link state to be enabled.
3466 */
3467static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3468{
3469 struct usb_set_sel_req *sel_values;
3470 unsigned long long u1_sel;
3471 unsigned long long u1_pel;
3472 unsigned long long u2_sel;
3473 unsigned long long u2_pel;
3474 int ret;
3475
Xenia Ragiadakou38d7f682013-09-04 17:24:45 +03003476 if (udev->state != USB_STATE_CONFIGURED)
3477 return 0;
3478
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003479 /* Convert SEL and PEL stored in ns to us */
3480 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3481 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3482 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3483 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3484
3485 /*
3486 * Make sure that the calculated SEL and PEL values for the link
3487 * state we're enabling aren't bigger than the max SEL/PEL
3488 * value that will fit in the SET SEL control transfer.
3489 * Otherwise the device would get an incorrect idea of the exit
3490 * latency for the link state, and could start a device-initiated
3491 * U1/U2 when the exit latencies are too high.
3492 */
3493 if ((state == USB3_LPM_U1 &&
3494 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3495 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3496 (state == USB3_LPM_U2 &&
3497 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3498 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003499 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 -07003500 usb3_lpm_names[state], u1_sel, u1_pel);
3501 return -EINVAL;
3502 }
3503
3504 /*
3505 * If we're enabling device-initiated LPM for one link state,
3506 * but the other link state has a too high SEL or PEL value,
3507 * just set those values to the max in the Set SEL request.
3508 */
3509 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3510 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3511
3512 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3513 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3514
3515 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3516 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3517
3518 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3519 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3520
3521 /*
3522 * usb_enable_lpm() can be called as part of a failed device reset,
3523 * which may be initiated by an error path of a mass storage driver.
3524 * Therefore, use GFP_NOIO.
3525 */
3526 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3527 if (!sel_values)
3528 return -ENOMEM;
3529
3530 sel_values->u1_sel = u1_sel;
3531 sel_values->u1_pel = u1_pel;
3532 sel_values->u2_sel = cpu_to_le16(u2_sel);
3533 sel_values->u2_pel = cpu_to_le16(u2_pel);
3534
3535 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3536 USB_REQ_SET_SEL,
3537 USB_RECIP_DEVICE,
3538 0, 0,
3539 sel_values, sizeof *(sel_values),
3540 USB_CTRL_SET_TIMEOUT);
3541 kfree(sel_values);
3542 return ret;
3543}
3544
3545/*
3546 * Enable or disable device-initiated U1 or U2 transitions.
3547 */
3548static int usb_set_device_initiated_lpm(struct usb_device *udev,
3549 enum usb3_link_state state, bool enable)
3550{
3551 int ret;
3552 int feature;
3553
3554 switch (state) {
3555 case USB3_LPM_U1:
3556 feature = USB_DEVICE_U1_ENABLE;
3557 break;
3558 case USB3_LPM_U2:
3559 feature = USB_DEVICE_U2_ENABLE;
3560 break;
3561 default:
3562 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3563 __func__, enable ? "enable" : "disable");
3564 return -EINVAL;
3565 }
3566
3567 if (udev->state != USB_STATE_CONFIGURED) {
3568 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3569 "for unconfigured device.\n",
3570 __func__, enable ? "enable" : "disable",
3571 usb3_lpm_names[state]);
3572 return 0;
3573 }
3574
3575 if (enable) {
3576 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003577 * Now send the control transfer to enable device-initiated LPM
3578 * for either U1 or U2.
3579 */
3580 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3581 USB_REQ_SET_FEATURE,
3582 USB_RECIP_DEVICE,
3583 feature,
3584 0, NULL, 0,
3585 USB_CTRL_SET_TIMEOUT);
3586 } else {
3587 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3588 USB_REQ_CLEAR_FEATURE,
3589 USB_RECIP_DEVICE,
3590 feature,
3591 0, NULL, 0,
3592 USB_CTRL_SET_TIMEOUT);
3593 }
3594 if (ret < 0) {
3595 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3596 enable ? "Enable" : "Disable",
3597 usb3_lpm_names[state]);
3598 return -EBUSY;
3599 }
3600 return 0;
3601}
3602
3603static int usb_set_lpm_timeout(struct usb_device *udev,
3604 enum usb3_link_state state, int timeout)
3605{
3606 int ret;
3607 int feature;
3608
3609 switch (state) {
3610 case USB3_LPM_U1:
3611 feature = USB_PORT_FEAT_U1_TIMEOUT;
3612 break;
3613 case USB3_LPM_U2:
3614 feature = USB_PORT_FEAT_U2_TIMEOUT;
3615 break;
3616 default:
3617 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3618 __func__);
3619 return -EINVAL;
3620 }
3621
3622 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3623 timeout != USB3_LPM_DEVICE_INITIATED) {
3624 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3625 "which is a reserved value.\n",
3626 usb3_lpm_names[state], timeout);
3627 return -EINVAL;
3628 }
3629
3630 ret = set_port_feature(udev->parent,
3631 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3632 feature);
3633 if (ret < 0) {
3634 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3635 "error code %i\n", usb3_lpm_names[state],
3636 timeout, ret);
3637 return -EBUSY;
3638 }
3639 if (state == USB3_LPM_U1)
3640 udev->u1_params.timeout = timeout;
3641 else
3642 udev->u2_params.timeout = timeout;
3643 return 0;
3644}
3645
3646/*
3647 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3648 * U1/U2 entry.
3649 *
3650 * We will attempt to enable U1 or U2, but there are no guarantees that the
3651 * control transfers to set the hub timeout or enable device-initiated U1/U2
3652 * will be successful.
3653 *
3654 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3655 * driver know about it. If that call fails, it should be harmless, and just
3656 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3657 */
3658static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3659 enum usb3_link_state state)
3660{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003661 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003662 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3663 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3664
3665 /* If the device says it doesn't have *any* exit latency to come out of
3666 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3667 * state.
3668 */
3669 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3670 (state == USB3_LPM_U2 && u2_mel == 0))
3671 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003672
Sarah Sharp65a95b72012-10-05 10:32:07 -07003673 /*
3674 * First, let the device know about the exit latencies
3675 * associated with the link state we're about to enable.
3676 */
3677 ret = usb_req_set_sel(udev, state);
3678 if (ret < 0) {
3679 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3680 usb3_lpm_names[state]);
3681 return;
3682 }
3683
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003684 /* We allow the host controller to set the U1/U2 timeout internally
3685 * first, so that it can change its schedule to account for the
3686 * additional latency to send data to a device in a lower power
3687 * link state.
3688 */
3689 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3690
3691 /* xHCI host controller doesn't want to enable this LPM state. */
3692 if (timeout == 0)
3693 return;
3694
3695 if (timeout < 0) {
3696 dev_warn(&udev->dev, "Could not enable %s link state, "
3697 "xHCI error %i.\n", usb3_lpm_names[state],
3698 timeout);
3699 return;
3700 }
3701
3702 if (usb_set_lpm_timeout(udev, state, timeout))
3703 /* If we can't set the parent hub U1/U2 timeout,
3704 * device-initiated LPM won't be allowed either, so let the xHCI
3705 * host know that this link state won't be enabled.
3706 */
3707 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3708
3709 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3710 else if (udev->actconfig)
3711 usb_set_device_initiated_lpm(udev, state, true);
3712
3713}
3714
3715/*
3716 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3717 * U1/U2 entry.
3718 *
3719 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3720 * If zero is returned, the parent will not allow the link to go into U1/U2.
3721 *
3722 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3723 * it won't have an effect on the bus link state because the parent hub will
3724 * still disallow device-initiated U1/U2 entry.
3725 *
3726 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3727 * possible. The result will be slightly more bus bandwidth will be taken up
3728 * (to account for U1/U2 exit latency), but it should be harmless.
3729 */
3730static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3731 enum usb3_link_state state)
3732{
3733 int feature;
3734
3735 switch (state) {
3736 case USB3_LPM_U1:
3737 feature = USB_PORT_FEAT_U1_TIMEOUT;
3738 break;
3739 case USB3_LPM_U2:
3740 feature = USB_PORT_FEAT_U2_TIMEOUT;
3741 break;
3742 default:
3743 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3744 __func__);
3745 return -EINVAL;
3746 }
3747
3748 if (usb_set_lpm_timeout(udev, state, 0))
3749 return -EBUSY;
3750
3751 usb_set_device_initiated_lpm(udev, state, false);
3752
3753 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3754 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3755 "bus schedule bandwidth may be impacted.\n",
3756 usb3_lpm_names[state]);
3757 return 0;
3758}
3759
3760/*
3761 * Disable hub-initiated and device-initiated U1 and U2 entry.
3762 * Caller must own the bandwidth_mutex.
3763 *
3764 * This will call usb_enable_lpm() on failure, which will decrement
3765 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3766 */
3767int usb_disable_lpm(struct usb_device *udev)
3768{
3769 struct usb_hcd *hcd;
3770
3771 if (!udev || !udev->parent ||
3772 udev->speed != USB_SPEED_SUPER ||
3773 !udev->lpm_capable)
3774 return 0;
3775
3776 hcd = bus_to_hcd(udev->bus);
3777 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3778 return 0;
3779
3780 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003781 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003782 return 0;
3783
3784 /* If LPM is enabled, attempt to disable it. */
3785 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3786 goto enable_lpm;
3787 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3788 goto enable_lpm;
3789
3790 return 0;
3791
3792enable_lpm:
3793 usb_enable_lpm(udev);
3794 return -EBUSY;
3795}
3796EXPORT_SYMBOL_GPL(usb_disable_lpm);
3797
3798/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3799int usb_unlocked_disable_lpm(struct usb_device *udev)
3800{
3801 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3802 int ret;
3803
3804 if (!hcd)
3805 return -EINVAL;
3806
3807 mutex_lock(hcd->bandwidth_mutex);
3808 ret = usb_disable_lpm(udev);
3809 mutex_unlock(hcd->bandwidth_mutex);
3810
3811 return ret;
3812}
3813EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3814
3815/*
3816 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3817 * xHCI host policy may prevent U1 or U2 from being enabled.
3818 *
3819 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3820 * until the lpm_disable_count drops to zero. Caller must own the
3821 * bandwidth_mutex.
3822 */
3823void usb_enable_lpm(struct usb_device *udev)
3824{
3825 struct usb_hcd *hcd;
3826
3827 if (!udev || !udev->parent ||
3828 udev->speed != USB_SPEED_SUPER ||
3829 !udev->lpm_capable)
3830 return;
3831
3832 udev->lpm_disable_count--;
3833 hcd = bus_to_hcd(udev->bus);
3834 /* Double check that we can both enable and disable LPM.
3835 * Device must be configured to accept set feature U1/U2 timeout.
3836 */
3837 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3838 !hcd->driver->disable_usb3_lpm_timeout)
3839 return;
3840
3841 if (udev->lpm_disable_count > 0)
3842 return;
3843
3844 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3845 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3846}
3847EXPORT_SYMBOL_GPL(usb_enable_lpm);
3848
3849/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3850void usb_unlocked_enable_lpm(struct usb_device *udev)
3851{
3852 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3853
3854 if (!hcd)
3855 return;
3856
3857 mutex_lock(hcd->bandwidth_mutex);
3858 usb_enable_lpm(udev);
3859 mutex_unlock(hcd->bandwidth_mutex);
3860}
3861EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3862
3863
Alan Sternd388dab2006-07-01 22:14:24 -04003864#else /* CONFIG_PM */
3865
Alan Sternf07600c2007-05-30 15:38:16 -04003866#define hub_suspend NULL
3867#define hub_resume NULL
3868#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003869
3870int usb_disable_lpm(struct usb_device *udev)
3871{
3872 return 0;
3873}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003874EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003875
3876void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003877EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003878
3879int usb_unlocked_disable_lpm(struct usb_device *udev)
3880{
3881 return 0;
3882}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003883EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003884
3885void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003886EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003887
3888int usb_disable_ltm(struct usb_device *udev)
3889{
3890 return 0;
3891}
3892EXPORT_SYMBOL_GPL(usb_disable_ltm);
3893
3894void usb_enable_ltm(struct usb_device *udev) { }
3895EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternc4b51a42013-07-11 14:58:23 -04003896
3897#endif /* CONFIG_PM */
Alan Sternd388dab2006-07-01 22:14:24 -04003898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
3900/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3901 *
3902 * Between connect detection and reset signaling there must be a delay
3903 * of 100ms at least for debounce and power-settling. The corresponding
3904 * timer shall restart whenever the downstream port detects a disconnect.
Matthias Beyer469271f2013-10-10 23:41:27 +02003905 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 * Apparently there are some bluetooth and irda-dongles and a number of
3907 * low-speed devices for which this debounce period may last over a second.
3908 * Not covered by the spec - but easy to deal with.
3909 *
3910 * This implementation uses a 1500ms total debounce timeout; if the
3911 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3912 * every 25ms for transient disconnects. When the port status has been
3913 * unchanged for 100ms it returns the port status.
3914 */
Lan Tianyuad493e52013-01-23 04:26:30 +08003915int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
3917 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 u16 portchange, portstatus;
3919 unsigned connection = 0xffff;
Dan Williamsd99f6b42014-05-20 18:08:17 -07003920 int total_time, stable_time = 0;
3921 struct usb_port *port_dev = hub->ports[port1 - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
3923 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3924 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3925 if (ret < 0)
3926 return ret;
3927
3928 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3929 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003930 if (!must_be_connected ||
3931 (connection == USB_PORT_STAT_CONNECTION))
3932 stable_time += HUB_DEBOUNCE_STEP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 if (stable_time >= HUB_DEBOUNCE_STABLE)
3934 break;
3935 } else {
3936 stable_time = 0;
3937 connection = portstatus & USB_PORT_STAT_CONNECTION;
3938 }
3939
3940 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08003941 usb_clear_port_feature(hub->hdev, port1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 USB_PORT_FEAT_C_CONNECTION);
3943 }
3944
3945 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3946 break;
3947 msleep(HUB_DEBOUNCE_STEP);
3948 }
3949
Dan Williamsd99f6b42014-05-20 18:08:17 -07003950 dev_dbg(&port_dev->dev, "debounce total %dms stable %dms status 0x%x\n",
3951 total_time, stable_time, portstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
3953 if (stable_time < HUB_DEBOUNCE_STABLE)
3954 return -ETIMEDOUT;
3955 return portstatus;
3956}
3957
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003958void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959{
Alan Sternddeac4e2009-01-15 17:03:33 -05003960 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3961 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003962 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003964EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
3966#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3967#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3968
Alan Stern4326ed02007-07-30 17:08:43 -04003969static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
3971 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003972 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Sarah Sharpc6515272009-04-27 19:57:26 -07003974 /*
3975 * The host controller will choose the device address,
3976 * instead of the core having chosen it earlier
3977 */
3978 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return -EINVAL;
3980 if (udev->state == USB_STATE_ADDRESS)
3981 return 0;
3982 if (udev->state != USB_STATE_DEFAULT)
3983 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003984 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003985 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003986 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003987 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3988 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3989 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003991 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003992 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003994 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
3996 return retval;
3997}
3998
Mathias Nyman890dae82013-09-30 17:26:31 +03003999/*
4000 * There are reports of USB 3.0 devices that say they support USB 2.0 Link PM
4001 * when they're plugged into a USB 2.0 port, but they don't work when LPM is
4002 * enabled.
4003 *
4004 * Only enable USB 2.0 Link PM if the port is internal (hardwired), or the
4005 * device says it supports the new USB 2.0 Link PM errata by setting the BESL
4006 * support bit in the BOS descriptor.
4007 */
4008static void hub_set_initial_usb2_lpm_policy(struct usb_device *udev)
4009{
Dan Williamsd99f6b42014-05-20 18:08:17 -07004010 struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent);
4011 int connect_type = USB_PORT_CONNECT_TYPE_UNKNOWN;
Mathias Nyman890dae82013-09-30 17:26:31 +03004012
4013 if (!udev->usb2_hw_lpm_capable)
4014 return;
4015
Dan Williamsd99f6b42014-05-20 18:08:17 -07004016 if (hub)
4017 connect_type = hub->ports[udev->portnum - 1]->connect_type;
Mathias Nyman890dae82013-09-30 17:26:31 +03004018
Bjørn Mork23c05822014-02-27 14:23:55 +01004019 if ((udev->bos->ext_cap->bmAttributes & cpu_to_le32(USB_BESL_SUPPORT)) ||
Mathias Nyman890dae82013-09-30 17:26:31 +03004020 connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
4021 udev->usb2_hw_lpm_allowed = 1;
4022 usb_set_usb2_hardware_lpm(udev, 1);
4023 }
4024}
4025
Dan Williams48fc7db2013-12-05 17:07:27 -08004026static int hub_enable_device(struct usb_device *udev)
4027{
4028 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
4029
4030 if (!hcd->driver->enable_device)
4031 return 0;
4032 if (udev->state == USB_STATE_ADDRESS)
4033 return 0;
4034 if (udev->state != USB_STATE_DEFAULT)
4035 return -EINVAL;
4036
4037 return hcd->driver->enable_device(hcd, udev);
4038}
4039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040/* Reset device, (re)assign address, get device descriptor.
4041 * Device connection must be stable, no more debouncing needed.
4042 * Returns device in USB_STATE_ADDRESS, except on error.
4043 *
4044 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08004045 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 * newly detected device that is not accessible through any global
4047 * pointers, it's not necessary to lock the device.
4048 */
4049static int
4050hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
4051 int retry_counter)
4052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07004054 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 int i, j, retval;
4056 unsigned delay = HUB_SHORT_RESET_TIME;
4057 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004058 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004059 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 /* root hub ports have a slightly longer reset period
4062 * (from USB 2.0 spec, section 7.1.7.5)
4063 */
4064 if (!hdev->parent) {
4065 delay = HUB_ROOT_RESET_TIME;
4066 if (port1 == hdev->bus->otg_port)
4067 hdev->bus->b_hnp_enable = 0;
4068 }
4069
4070 /* Some low speed devices have problems with the quick delay, so */
4071 /* be a bit pessimistic with those devices. RHbug #23670 */
4072 if (oldspeed == USB_SPEED_LOW)
4073 delay = HUB_LONG_RESET_TIME;
4074
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004075 mutex_lock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Luben Tuikov07194ab2011-02-11 11:33:10 -08004077 /* Reset the device; full speed may morph to high speed */
4078 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07004079 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08004080 if (retval < 0) /* error or disconnect */
4081 goto fail;
4082 /* success, speed is known */
4083
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 retval = -ENODEV;
4085
4086 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
4087 dev_dbg(&udev->dev, "device reset changed speed!\n");
4088 goto fail;
4089 }
4090 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04004091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
4093 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004094 * For Wireless USB devices, ep0 max packet is always 512 (tho
4095 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 */
4097 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07004098 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08004099 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004100 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004101 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004103 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 break;
4105 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
4106 /* to determine the ep0 maxpacket size, try to read
4107 * the device descriptor to get bMaxPacketSize0 and
4108 * then correct our initial guess.
4109 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004110 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 break;
4112 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08004113 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 break;
4115 default:
4116 goto fail;
4117 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004118
4119 if (udev->speed == USB_SPEED_WIRELESS)
4120 speed = "variable speed Wireless";
4121 else
4122 speed = usb_speed_string(udev->speed);
4123
Sarah Sharpc6515272009-04-27 19:57:26 -07004124 if (udev->speed != USB_SPEED_SUPER)
4125 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02004126 "%s %s USB device number %d using %s\n",
4127 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05004128 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 /* Set up TT records, if needed */
4131 if (hdev->tt) {
4132 udev->tt = hdev->tt;
4133 udev->ttport = hdev->ttport;
4134 } else if (udev->speed != USB_SPEED_HIGH
4135 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05004136 if (!hub->tt.hub) {
4137 dev_err(&udev->dev, "parent hub has no TT\n");
4138 retval = -EINVAL;
4139 goto fail;
4140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 udev->tt = &hub->tt;
4142 udev->ttport = port1;
4143 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
4146 * Because device hardware and firmware is sometimes buggy in
4147 * this area, and this is how Linux has done it for ages.
4148 * Change it cautiously.
4149 *
Dan Williams48fc7db2013-12-05 17:07:27 -08004150 * NOTE: If use_new_scheme() is true we will start by issuing
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
4152 * so it may help with some non-standards-compliant devices.
4153 * Otherwise we start with SET_ADDRESS and then try to read the
4154 * first 8 bytes of the device descriptor to get the ep0 maxpacket
4155 * value.
4156 */
4157 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Dan Williams48fc7db2013-12-05 17:07:27 -08004158 bool did_new_scheme = false;
4159
4160 if (use_new_scheme(udev, retry_counter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 struct usb_device_descriptor *buf;
4162 int r = 0;
4163
Dan Williams48fc7db2013-12-05 17:07:27 -08004164 did_new_scheme = true;
4165 retval = hub_enable_device(udev);
Oliver Neukum938569e2014-02-27 10:57:10 +01004166 if (retval < 0) {
4167 dev_err(&udev->dev,
4168 "hub failed to enable device, error %d\n",
4169 retval);
Dan Williams48fc7db2013-12-05 17:07:27 -08004170 goto fail;
Oliver Neukum938569e2014-02-27 10:57:10 +01004171 }
Dan Williams48fc7db2013-12-05 17:07:27 -08004172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173#define GET_DESCRIPTOR_BUFSIZE 64
4174 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
4175 if (!buf) {
4176 retval = -ENOMEM;
4177 continue;
4178 }
4179
Alan Sternb89ee192007-05-11 10:19:04 -04004180 /* Retry on all errors; some devices are flakey.
4181 * 255 is for WUSB devices, we actually need to use
4182 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 */
4184 for (j = 0; j < 3; ++j) {
4185 buf->bMaxPacketSize0 = 0;
4186 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4187 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4188 USB_DT_DEVICE << 8, 0,
4189 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004190 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004192 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 if (buf->bDescriptorType ==
4194 USB_DT_DEVICE) {
4195 r = 0;
4196 break;
4197 }
4198 /* FALL THROUGH */
4199 default:
4200 if (r == 0)
4201 r = -EPROTO;
4202 break;
4203 }
4204 if (r == 0)
4205 break;
4206 }
4207 udev->descriptor.bMaxPacketSize0 =
4208 buf->bMaxPacketSize0;
4209 kfree(buf);
4210
Andiry Xu75d7cf72011-09-13 16:41:11 -07004211 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 if (retval < 0) /* error or disconnect */
4213 goto fail;
4214 if (oldspeed != udev->speed) {
4215 dev_dbg(&udev->dev,
4216 "device reset changed speed!\n");
4217 retval = -ENODEV;
4218 goto fail;
4219 }
4220 if (r) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004221 if (r != -ENODEV)
4222 dev_err(&udev->dev, "device descriptor read/64, error %d\n",
4223 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 retval = -EMSGSIZE;
4225 continue;
4226 }
4227#undef GET_DESCRIPTOR_BUFSIZE
4228 }
4229
Matthias Beyer469271f2013-10-10 23:41:27 +02004230 /*
4231 * If device is WUSB, we already assigned an
4232 * unauthorized address in the Connect Ack sequence;
4233 * authorization will assign the final address.
4234 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004235 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004236 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4237 retval = hub_set_address(udev, devnum);
4238 if (retval >= 0)
4239 break;
4240 msleep(200);
4241 }
4242 if (retval < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004243 if (retval != -ENODEV)
4244 dev_err(&udev->dev, "device not accepting address %d, error %d\n",
4245 devnum, retval);
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004246 goto fail;
4247 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004248 if (udev->speed == USB_SPEED_SUPER) {
4249 devnum = udev->devnum;
4250 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004251 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004252 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004253 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004254 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004255
4256 /* cope with hardware quirkiness:
4257 * - let SET_ADDRESS settle, some device hardware wants it
4258 * - read ep0 maxpacket even for high and low speed,
4259 */
4260 msleep(10);
Dan Williams48fc7db2013-12-05 17:07:27 -08004261 /* use_new_scheme() checks the speed which may have
4262 * changed since the initial look so we cache the result
4263 * in did_new_scheme
4264 */
4265 if (did_new_scheme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 break;
Matthias Beyer469271f2013-10-10 23:41:27 +02004267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269 retval = usb_get_device_descriptor(udev, 8);
4270 if (retval < 8) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004271 if (retval != -ENODEV)
4272 dev_err(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004273 "device descriptor read/8, error %d\n",
4274 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 if (retval >= 0)
4276 retval = -EMSGSIZE;
4277 } else {
4278 retval = 0;
4279 break;
4280 }
4281 }
4282 if (retval)
4283 goto fail;
4284
Peter Chenb76baa82012-11-09 09:44:43 +08004285 if (hcd->phy && !hdev->parent)
Peter Chenac965112012-11-09 09:44:44 +08004286 usb_phy_notify_connect(hcd->phy, udev->speed);
Peter Chenb76baa82012-11-09 09:44:43 +08004287
Elric Fud8aec3d2012-03-26 21:16:02 +08004288 /*
4289 * Some superspeed devices have finished the link training process
4290 * and attached to a superspeed hub port, but the device descriptor
4291 * got from those devices show they aren't superspeed devices. Warm
4292 * reset the port attached by the devices can fix them.
4293 */
4294 if ((udev->speed == USB_SPEED_SUPER) &&
4295 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4296 dev_err(&udev->dev, "got a wrong device descriptor, "
4297 "warm reset device\n");
4298 hub_port_reset(hub, port1, udev,
4299 HUB_BH_RESET_TIME, true);
4300 retval = -EINVAL;
4301 goto fail;
4302 }
4303
Sarah Sharp6b403b02009-04-27 19:54:10 -07004304 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4305 udev->speed == USB_SPEED_SUPER)
4306 i = 512;
4307 else
4308 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004309 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004310 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004312 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 retval = -EMSGSIZE;
4314 goto fail;
4315 }
Alan Stern56626a72010-10-14 15:25:21 -04004316 if (udev->speed == USB_SPEED_FULL)
4317 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4318 else
4319 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004321 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4325 if (retval < (signed)sizeof(udev->descriptor)) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004326 if (retval != -ENODEV)
4327 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4328 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 if (retval >= 0)
4330 retval = -ENOMSG;
4331 goto fail;
4332 }
4333
Andiry Xu1ff4df52011-09-23 14:19:48 -07004334 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4335 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004336 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004337 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004338 usb_set_lpm_parameters(udev);
4339 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004340 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004343 /* notify HCD that we have a device connected and addressed */
4344 if (hcd->driver->update_device)
4345 hcd->driver->update_device(hcd, udev);
Mathias Nyman890dae82013-09-30 17:26:31 +03004346 hub_set_initial_usb2_lpm_policy(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004348 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004350 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004351 }
Todd E Brandt6fecd4f2014-05-19 10:55:32 -07004352 mutex_unlock(&hdev->bus->usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return retval;
4354}
4355
4356static void
4357check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4358{
4359 struct usb_qualifier_descriptor *qual;
4360 int status;
4361
Christoph Lametere94b1762006-12-06 20:33:17 -08004362 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 if (qual == NULL)
4364 return;
4365
4366 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4367 qual, sizeof *qual);
4368 if (status == sizeof *qual) {
4369 dev_info(&udev->dev, "not running at top speed; "
4370 "connect to a high speed hub\n");
4371 /* hub LEDs are probably harder to miss than syslog */
4372 if (hub->has_indicators) {
4373 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004374 queue_delayed_work(system_power_efficient_wq,
4375 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 }
4377 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004378 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379}
4380
4381static unsigned
4382hub_power_remaining (struct usb_hub *hub)
4383{
4384 struct usb_device *hdev = hub->hdev;
4385 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004386 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Alan Stern55c52712005-11-23 12:03:12 -05004388 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return 0;
4390
Alan Stern55c52712005-11-23 12:03:12 -05004391 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4392 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004393 struct usb_port *port_dev = hub->ports[port1 - 1];
4394 struct usb_device *udev = port_dev->child;
4395 unsigned unit_load;
4396 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397
4398 if (!udev)
4399 continue;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004400 if (hub_is_superspeed(udev))
4401 unit_load = 150;
4402 else
4403 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004405 /*
4406 * Unconfigured devices may not use more than one unit load,
4407 * or 8mA for OTG ports
4408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 if (udev->actconfig)
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +01004410 delta = usb_get_max_power(udev, udev->actconfig);
Alan Stern55c52712005-11-23 12:03:12 -05004411 else if (port1 != udev->bus->otg_port || hdev->parent)
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004412 delta = unit_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 else
Alan Stern55c52712005-11-23 12:03:12 -05004414 delta = 8;
4415 if (delta > hub->mA_per_port)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004416 dev_warn(&port_dev->dev, "%dmA is over %umA budget!\n",
4417 delta, hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 remaining -= delta;
4419 }
4420 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004421 dev_warn(hub->intfdev, "%dmA over power budget!\n",
Matthias Beyer469271f2013-10-10 23:41:27 +02004422 -remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 remaining = 0;
4424 }
4425 return remaining;
4426}
4427
4428/* Handle physical or logical connection change events.
4429 * This routine is called when:
4430 * a port connection-change occurs;
4431 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004432 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 * a firmware download)
4434 * caller already locked the hub
4435 */
4436static void hub_port_connect_change(struct usb_hub *hub, int port1,
4437 u16 portstatus, u16 portchange)
4438{
4439 struct usb_device *hdev = hub->hdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304440 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004441 struct usb_port *port_dev = hub->ports[port1 - 1];
Alan Stern8808f002008-04-28 11:06:55 -04004442 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 int status, i;
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004444 unsigned unit_load;
Alan Stern24618b02008-04-28 11:06:28 -04004445
Dan Williamsd99f6b42014-05-20 18:08:17 -07004446 dev_dbg(&port_dev->dev, "status %04x, change %04x, %s\n",
4447 portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448
4449 if (hub->has_indicators) {
4450 set_port_led(hub, port1, HUB_LED_AUTO);
4451 hub->indicator[port1-1] = INDICATOR_AUTO;
4452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
4454#ifdef CONFIG_USB_OTG
4455 /* during HNP, don't repeat the debounce */
4456 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004457 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4458 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459#endif
4460
Alan Stern8808f002008-04-28 11:06:55 -04004461 /* Try to resuscitate an existing device */
Dan Williamsd99f6b42014-05-20 18:08:17 -07004462 udev = port_dev->child;
Alan Stern8808f002008-04-28 11:06:55 -04004463 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4464 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004465 usb_lock_device(udev);
4466 if (portstatus & USB_PORT_STAT_ENABLE) {
4467 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004468
Alan Stern84ebc102013-03-27 16:14:46 -04004469#ifdef CONFIG_PM_RUNTIME
Alan Stern5257d972008-09-22 14:43:08 -04004470 } else if (udev->state == USB_STATE_SUSPENDED &&
4471 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004472 /* For a suspended device, treat this as a
4473 * remote wakeup event.
4474 */
Alan Stern0534d462010-01-08 12:56:30 -05004475 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004476#endif
4477
4478 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004479 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004480 }
4481 usb_unlock_device(udev);
4482
4483 if (status == 0) {
4484 clear_bit(port1, hub->change_bits);
4485 return;
4486 }
4487 }
4488
Alan Stern24618b02008-04-28 11:06:28 -04004489 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004490 if (udev) {
4491 if (hcd->phy && !hdev->parent &&
4492 !(portstatus & USB_PORT_STAT_CONNECTION))
Peter Chenac965112012-11-09 09:44:44 +08004493 usb_phy_notify_disconnect(hcd->phy, udev->speed);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004494 usb_disconnect(&port_dev->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004495 }
Alan Stern24618b02008-04-28 11:06:28 -04004496 clear_bit(port1, hub->change_bits);
4497
Alan Stern253e0572009-10-27 15:20:13 -04004498 /* We can forget about a "removed" device when there's a physical
4499 * disconnect or the connect status changes.
4500 */
4501 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4502 (portchange & USB_PORT_STAT_C_CONNECTION))
4503 clear_bit(port1, hub->removed_bits);
4504
Alan Stern5257d972008-09-22 14:43:08 -04004505 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4506 USB_PORT_STAT_C_ENABLE)) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004507 status = hub_port_debounce_be_stable(hub, port1);
Alan Stern5257d972008-09-22 14:43:08 -04004508 if (status < 0) {
Alan Sterne9e88fb2013-03-27 16:14:01 -04004509 if (status != -ENODEV && printk_ratelimit())
Dan Williamsd99f6b42014-05-20 18:08:17 -07004510 dev_err(&port_dev->dev,
4511 "connect-debounce failed\n");
Alan Stern5257d972008-09-22 14:43:08 -04004512 portstatus &= ~USB_PORT_STAT_CONNECTION;
4513 } else {
4514 portstatus = status;
4515 }
4516 }
4517
Alan Stern253e0572009-10-27 15:20:13 -04004518 /* Return now if debouncing failed or nothing is connected or
4519 * the device was "removed".
4520 */
4521 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4522 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
4524 /* maybe switch power back on (e.g. root hub was reset) */
Dan Williams9262c192014-05-20 18:08:12 -07004525 if (hub_is_port_power_switchable(hub)
Andiry Xu0ed9a572011-04-27 18:07:43 +08004526 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 if (portstatus & USB_PORT_STAT_ENABLE)
Matthias Beyer469271f2013-10-10 23:41:27 +02004530 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 return;
4532 }
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004533 if (hub_is_superspeed(hub->hdev))
4534 unit_load = 150;
4535 else
4536 unit_load = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Alan Sterne9e88fb2013-03-27 16:14:01 -04004538 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 /* reallocate for each attempt, since references
4542 * to the previous one can escape in various ways
4543 */
4544 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4545 if (!udev) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004546 dev_err(&port_dev->dev,
4547 "couldn't allocate usb_device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 goto done;
4549 }
4550
4551 usb_set_device_state(udev, USB_STATE_POWERED);
Matthias Beyer469271f2013-10-10 23:41:27 +02004552 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004553 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004554 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004555
Sarah Sharp131dec32010-12-06 21:00:19 -08004556 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4557 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004558 udev->speed = USB_SPEED_SUPER;
4559 else
4560 udev->speed = USB_SPEED_UNKNOWN;
4561
Alan Stern3b29b682011-02-22 09:53:41 -05004562 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004563 if (udev->devnum <= 0) {
4564 status = -ENOTCONN; /* Don't retry */
4565 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004566 }
4567
Sarah Sharpe7b77172009-04-27 19:54:26 -07004568 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 status = hub_port_init(hub, udev, port1, i);
4570 if (status < 0)
4571 goto loop;
4572
Phil Dibowitz93362a82010-07-22 00:05:01 +02004573 usb_detect_quirks(udev);
4574 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4575 msleep(1000);
4576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 /* consecutive bus-powered hubs aren't reliable; they can
4578 * violate the voltage drop budget. if the new child has
4579 * a "powered" LED, users should notice we didn't enable it
4580 * (without reading syslog), even without per-port LEDs
4581 * on the parent.
4582 */
4583 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Sebastian Andrzej Siewior430ee582012-12-18 15:25:47 +01004584 && udev->bus_mA <= unit_load) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 u16 devstat;
4586
4587 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4588 &devstat);
Alan Stern15b73362013-07-30 15:35:40 -04004589 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 dev_dbg(&udev->dev, "get status %d ?\n", status);
4591 goto loop_disable;
4592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4594 dev_err(&udev->dev,
4595 "can't connect bus-powered hub "
4596 "to this port\n");
4597 if (hub->has_indicators) {
4598 hub->indicator[port1-1] =
4599 INDICATOR_AMBER_BLINK;
Shaibal Dutta22f6a0f2014-02-01 19:16:46 -08004600 queue_delayed_work(
4601 system_power_efficient_wq,
4602 &hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 }
4604 status = -ENOTCONN; /* Don't retry */
4605 goto loop_disable;
4606 }
4607 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 /* check for devices running slower than they could */
4610 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4611 && udev->speed == USB_SPEED_FULL
4612 && highspeed_hubs != 0)
4613 check_highspeed (hub, udev, port1);
4614
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004615 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 * udev becomes globally accessible, although presumably
4617 * no one will look at it until hdev is unlocked.
4618 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 status = 0;
4620
Dan Williamsd8521af2014-05-20 18:08:28 -07004621 mutex_lock(&usb_port_peer_mutex);
4622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 /* We mustn't add new devices if the parent hub has
4624 * been disconnected; we would race with the
4625 * recursively_mark_NOTATTACHED() routine.
4626 */
4627 spin_lock_irq(&device_state_lock);
4628 if (hdev->state == USB_STATE_NOTATTACHED)
4629 status = -ENOTCONN;
4630 else
Dan Williamsd99f6b42014-05-20 18:08:17 -07004631 port_dev->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004633 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634
4635 /* Run it through the hoops (find a driver, etc) */
4636 if (!status) {
4637 status = usb_new_device(udev);
4638 if (status) {
Dan Williamsd8521af2014-05-20 18:08:28 -07004639 mutex_lock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 spin_lock_irq(&device_state_lock);
Dan Williamsd99f6b42014-05-20 18:08:17 -07004641 port_dev->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 spin_unlock_irq(&device_state_lock);
Dan Williamsd8521af2014-05-20 18:08:28 -07004643 mutex_unlock(&usb_port_peer_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
4645 }
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 if (status)
4648 goto loop_disable;
4649
4650 status = hub_power_remaining(hub);
4651 if (status)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004652 dev_dbg(hub->intfdev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 return;
4655
4656loop_disable:
4657 hub_port_disable(hub, port1, 1);
4658loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004659 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004660 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004661 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004663 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 break;
4665 }
Alan Stern3a311552008-05-20 16:58:29 -04004666 if (hub->hdev->parent ||
4667 !hcd->driver->port_handed_over ||
Alan Sterne9e88fb2013-03-27 16:14:01 -04004668 !(hcd->driver->port_handed_over)(hcd, port1)) {
4669 if (status != -ENOTCONN && status != -ENODEV)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004670 dev_err(&port_dev->dev,
4671 "unable to enumerate USB device\n");
Alan Sterne9e88fb2013-03-27 16:14:01 -04004672 }
Matthias Beyer469271f2013-10-10 23:41:27 +02004673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674done:
4675 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304676 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4677 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678}
4679
Sarah Sharp714b07b2012-01-24 13:53:18 -08004680/* Returns 1 if there was a remote wakeup and a connect status change. */
4681static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004682 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004683{
Dan Williamsd99f6b42014-05-20 18:08:17 -07004684 struct usb_port *port_dev = hub->ports[port - 1];
Sarah Sharp714b07b2012-01-24 13:53:18 -08004685 struct usb_device *hdev;
4686 struct usb_device *udev;
4687 int connect_change = 0;
4688 int ret;
4689
4690 hdev = hub->hdev;
Dan Williamsd99f6b42014-05-20 18:08:17 -07004691 udev = port_dev->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004692 if (!hub_is_superspeed(hdev)) {
4693 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4694 return 0;
Lan Tianyuad493e52013-01-23 04:26:30 +08004695 usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004696 } else {
4697 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004698 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4699 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004700 return 0;
4701 }
4702
Sarah Sharp714b07b2012-01-24 13:53:18 -08004703 if (udev) {
4704 /* TRSMRCY = 10 msec */
4705 msleep(10);
4706
4707 usb_lock_device(udev);
4708 ret = usb_remote_wakeup(udev);
4709 usb_unlock_device(udev);
4710 if (ret < 0)
4711 connect_change = 1;
4712 } else {
4713 ret = -ENODEV;
4714 hub_port_disable(hub, port, 1);
4715 }
Dan Williamsd99f6b42014-05-20 18:08:17 -07004716 dev_dbg(&port_dev->dev, "resume, status %d\n", ret);
Sarah Sharp714b07b2012-01-24 13:53:18 -08004717 return connect_change;
4718}
4719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720static void hub_events(void)
4721{
4722 struct list_head *tmp;
4723 struct usb_device *hdev;
4724 struct usb_interface *intf;
4725 struct usb_hub *hub;
4726 struct device *hub_dev;
4727 u16 hubstatus;
4728 u16 hubchange;
4729 u16 portstatus;
4730 u16 portchange;
4731 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004732 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 /*
4735 * We restart the list every time to avoid a deadlock with
4736 * deleting hubs downstream from this one. This should be
4737 * safe since we delete the hub from the event list.
4738 * Not the most efficient, but avoids deadlocks.
4739 */
4740 while (1) {
4741
4742 /* Grab the first entry at the beginning of the list */
4743 spin_lock_irq(&hub_event_lock);
4744 if (list_empty(&hub_event_list)) {
4745 spin_unlock_irq(&hub_event_lock);
4746 break;
4747 }
4748
4749 tmp = hub_event_list.next;
4750 list_del_init(tmp);
4751
4752 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004753 kref_get(&hub->kref);
4754 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Alan Sterne8054852007-05-04 11:55:11 -04004756 hdev = hub->hdev;
4757 hub_dev = hub->intfdev;
4758 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004759 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +02004760 hdev->state, hdev->maxchild,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 /* NOTE: expects max 15 ports... */
4762 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004763 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 /* Lock the device, then check to see if we were
4766 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004767 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004768 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004769 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 /* If the hub has died, clean up after it */
4772 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004773 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004774 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 goto loop;
4776 }
4777
Alan Stern40f122f2006-11-09 14:44:33 -05004778 /* Autoresume */
4779 ret = usb_autopm_get_interface(intf);
4780 if (ret) {
4781 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004783 }
4784
4785 /* If this is an inactive hub, do nothing */
4786 if (hub->quiescing)
4787 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
4789 if (hub->error) {
4790 dev_dbg (hub_dev, "resetting for error %d\n",
4791 hub->error);
4792
Ming Lei742120c2008-06-18 22:00:29 +08004793 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 if (ret) {
4795 dev_dbg (hub_dev,
4796 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004797 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 }
4799
4800 hub->nerrors = 0;
4801 hub->error = 0;
4802 }
4803
4804 /* deal with port status changes */
Krzysztof Mazur3bbc47d2013-08-22 14:49:40 +02004805 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004806 struct usb_port *port_dev = hub->ports[i - 1];
4807 struct usb_device *udev = port_dev->child;
Hans de Goedea82b76f2013-11-08 13:41:05 +01004808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 if (test_bit(i, hub->busy_bits))
4810 continue;
4811 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004812 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004814 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 continue;
4816
4817 ret = hub_port_status(hub, i,
4818 &portstatus, &portchange);
4819 if (ret < 0)
4820 continue;
4821
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 if (portchange & USB_PORT_STAT_C_CONNECTION) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004823 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 USB_PORT_FEAT_C_CONNECTION);
4825 connect_change = 1;
4826 }
4827
4828 if (portchange & USB_PORT_STAT_C_ENABLE) {
4829 if (!connect_change)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004830 dev_dbg(&port_dev->dev,
4831 "enable change, status %08x\n",
4832 portstatus);
Lan Tianyuad493e52013-01-23 04:26:30 +08004833 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 USB_PORT_FEAT_C_ENABLE);
4835
4836 /*
4837 * EM interference sometimes causes badly
4838 * shielded USB devices to be shutdown by
4839 * the hub, this hack enables them again.
Matthias Beyer469271f2013-10-10 23:41:27 +02004840 * Works at least with mouse driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 */
4842 if (!(portstatus & USB_PORT_STAT_ENABLE)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004843 && !connect_change && udev) {
4844 dev_err(&port_dev->dev,
4845 "disabled by hub (EMI?), re-enabling...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 connect_change = 1;
4847 }
4848 }
4849
Sarah Sharp72937e12012-01-24 11:46:50 -08004850 if (hub_handle_remote_wakeup(hub, i,
4851 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004852 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004855 u16 status = 0;
4856 u16 unused;
4857
Dan Williamsd99f6b42014-05-20 18:08:17 -07004858 dev_dbg(&port_dev->dev, "over-current change\n");
Lan Tianyuad493e52013-01-23 04:26:30 +08004859 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004861 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004862 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004863 hub_port_status(hub, i, &status, &unused);
4864 if (status & USB_PORT_STAT_OVERCURRENT)
Dan Williamsd99f6b42014-05-20 18:08:17 -07004865 dev_err(&port_dev->dev,
4866 "over-current condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 }
4868
4869 if (portchange & USB_PORT_STAT_C_RESET) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004870 dev_dbg(&port_dev->dev, "reset change\n");
Lan Tianyuad493e52013-01-23 04:26:30 +08004871 usb_clear_port_feature(hdev, i,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 USB_PORT_FEAT_C_RESET);
4873 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004874 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4875 hub_is_superspeed(hub->hdev)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004876 dev_dbg(&port_dev->dev, "warm reset change\n");
Lan Tianyuad493e52013-01-23 04:26:30 +08004877 usb_clear_port_feature(hdev, i,
Sarah Sharpc7061572010-12-06 15:08:20 -08004878 USB_PORT_FEAT_C_BH_PORT_RESET);
4879 }
John Youndbe79bb2001-09-17 00:00:00 -07004880 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
Lan Tianyuad493e52013-01-23 04:26:30 +08004881 usb_clear_port_feature(hub->hdev, i,
John Youndbe79bb2001-09-17 00:00:00 -07004882 USB_PORT_FEAT_C_PORT_LINK_STATE);
4883 }
4884 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07004885 dev_warn(&port_dev->dev, "config error\n");
Lan Tianyuad493e52013-01-23 04:26:30 +08004886 usb_clear_port_feature(hub->hdev, i,
John Youndbe79bb2001-09-17 00:00:00 -07004887 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889
Andiry Xu5e467f62011-04-27 18:07:54 +08004890 /* Warm reset a USB3 protocol port if it's in
4891 * SS.Inactive state.
4892 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004893 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp65bdac52012-11-14 17:58:04 -08004894 int status;
4895
Dan Williamsd99f6b42014-05-20 18:08:17 -07004896 dev_dbg(&port_dev->dev, "warm reset\n");
Julius Werner2d51f3c2013-11-07 10:59:14 -08004897 if (!udev ||
4898 !(portstatus & USB_PORT_STAT_CONNECTION) ||
4899 udev->state == USB_STATE_NOTATTACHED) {
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004900 status = hub_port_reset(hub, i,
4901 NULL, HUB_BH_RESET_TIME,
4902 true);
4903 if (status < 0)
4904 hub_port_disable(hub, i, 1);
4905 } else {
4906 usb_lock_device(udev);
4907 status = usb_reset_device(udev);
4908 usb_unlock_device(udev);
Julius Wernerf3e94aa2013-07-30 19:51:20 -07004909 connect_change = 0;
Sarah Sharpd3b9d7a2012-11-01 11:20:44 -07004910 }
Hans de Goedea82b76f2013-11-08 13:41:05 +01004911 /*
4912 * On disconnect USB3 protocol ports transit from U0 to
4913 * SS.Inactive to Rx.Detect. If this happens a warm-
4914 * reset is not needed, but a (re)connect may happen
4915 * before khubd runs and sees the disconnect, and the
4916 * device may be an unknown state.
4917 *
4918 * If the port went through SS.Inactive without khubd
4919 * seeing it the C_LINK_STATE change flag will be set,
4920 * and we reset the dev to put it in a known state.
4921 */
4922 } else if (udev && hub_is_superspeed(hub->hdev) &&
4923 (portchange & USB_PORT_STAT_C_LINK_STATE) &&
4924 (portstatus & USB_PORT_STAT_CONNECTION)) {
4925 usb_lock_device(udev);
4926 usb_reset_device(udev);
4927 usb_unlock_device(udev);
4928 connect_change = 0;
Andiry Xu5e467f62011-04-27 18:07:54 +08004929 }
4930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 if (connect_change)
4932 hub_port_connect_change(hub, i,
4933 portstatus, portchange);
4934 } /* end for i */
4935
4936 /* deal with hub status changes */
4937 if (test_and_clear_bit(0, hub->event_bits) == 0)
4938 ; /* do nothing */
4939 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4940 dev_err (hub_dev, "get_hub_status failed\n");
4941 else {
4942 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4943 dev_dbg (hub_dev, "power change\n");
4944 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004945 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4946 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004947 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004948 else
4949 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 }
4951 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004952 u16 status = 0;
4953 u16 unused;
4954
4955 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004957 msleep(500); /* Cool down */
Matthias Beyer469271f2013-10-10 23:41:27 +02004958 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004959 hub_hub_status(hub, &status, &unused);
4960 if (status & HUB_STATUS_OVERCURRENT)
4961 dev_err(hub_dev, "over-current "
4962 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 }
4964 }
4965
Alan Stern8e4ceb32009-12-07 13:01:37 -05004966 loop_autopm:
4967 /* Balance the usb_autopm_get_interface() above */
4968 usb_autopm_put_interface_no_suspend(intf);
4969 loop:
4970 /* Balance the usb_autopm_get_interface_no_resume() in
4971 * kick_khubd() and allow autosuspend.
4972 */
4973 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004974 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004976 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
Matthias Beyer469271f2013-10-10 23:41:27 +02004978 } /* end while (1) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979}
4980
4981static int hub_thread(void *__unused)
4982{
Rahul Bedarkar025d4432014-01-04 11:24:41 +05304983 /* khubd needs to be freezable to avoid interfering with USB-PERSIST
Alan Stern3bb1af52008-03-03 15:15:36 -05004984 * port handover. Otherwise it might see that a full-speed device
4985 * was gone before the EHCI controller had handed its port over to
4986 * the companion full-speed controller.
4987 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004988 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 do {
4991 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004992 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004993 !list_empty(&hub_event_list) ||
4994 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004995 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004997 pr_debug("%s: khubd exiting\n", usbcore_name);
4998 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999}
5000
Németh Márton1e927d92010-01-10 15:34:53 +01005001static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08005002 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
Matthias Beyer469271f2013-10-10 23:41:27 +02005003 | USB_DEVICE_ID_MATCH_INT_CLASS,
Ming Leie6f30de2012-10-24 11:59:24 +08005004 .idVendor = USB_VENDOR_GENESYS_LOGIC,
5005 .bInterfaceClass = USB_CLASS_HUB,
5006 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
5008 .bDeviceClass = USB_CLASS_HUB},
5009 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
5010 .bInterfaceClass = USB_CLASS_HUB},
5011 { } /* Terminating entry */
5012};
5013
5014MODULE_DEVICE_TABLE (usb, hub_id_table);
5015
5016static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 .name = "hub",
5018 .probe = hub_probe,
5019 .disconnect = hub_disconnect,
5020 .suspend = hub_suspend,
5021 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04005022 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04005023 .pre_reset = hub_pre_reset,
5024 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02005025 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05005027 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028};
5029
5030int usb_hub_init(void)
5031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 if (usb_register(&hub_driver) < 0) {
5033 printk(KERN_ERR "%s: can't register hub driver\n",
5034 usbcore_name);
5035 return -1;
5036 }
5037
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005038 khubd_task = kthread_run(hub_thread, NULL, "khubd");
5039 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
5042 /* Fall through if kernel_thread failed */
5043 usb_deregister(&hub_driver);
5044 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
5045
5046 return -1;
5047}
5048
5049void usb_hub_cleanup(void)
5050{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07005051 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
5053 /*
5054 * Hub resources are freed for us by usb_deregister. It calls
5055 * usb_driver_purge on every device which in turn calls that
5056 * devices disconnect function if it is using this driver.
5057 * The hub_disconnect function takes care of releasing the
5058 * individual hub resources. -greg
5059 */
5060 usb_deregister(&hub_driver);
5061} /* usb_hub_cleanup() */
5062
Alan Sterneb764c42008-03-03 15:16:04 -05005063static int descriptors_changed(struct usb_device *udev,
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005064 struct usb_device_descriptor *old_device_descriptor,
5065 struct usb_host_bos *old_bos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
Alan Sterneb764c42008-03-03 15:16:04 -05005067 int changed = 0;
5068 unsigned index;
5069 unsigned serial_len = 0;
5070 unsigned len;
5071 unsigned old_length;
5072 int length;
5073 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
Alan Sterneb764c42008-03-03 15:16:04 -05005075 if (memcmp(&udev->descriptor, old_device_descriptor,
5076 sizeof(*old_device_descriptor)) != 0)
5077 return 1;
5078
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005079 if ((old_bos && !udev->bos) || (!old_bos && udev->bos))
5080 return 1;
5081 if (udev->bos) {
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005082 len = le16_to_cpu(udev->bos->desc->wTotalLength);
5083 if (len != le16_to_cpu(old_bos->desc->wTotalLength))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005084 return 1;
Xenia Ragiadakoub9a10482013-08-31 04:40:49 +03005085 if (memcmp(udev->bos->desc, old_bos->desc, len))
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005086 return 1;
5087 }
5088
Alan Sterneb764c42008-03-03 15:16:04 -05005089 /* Since the idVendor, idProduct, and bcdDevice values in the
5090 * device descriptor haven't changed, we will assume the
5091 * Manufacturer and Product strings haven't changed either.
5092 * But the SerialNumber string could be different (e.g., a
5093 * different flash card of the same brand).
5094 */
5095 if (udev->serial)
5096 serial_len = strlen(udev->serial) + 1;
5097
5098 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005100 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
5101 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 }
Alan Sterneb764c42008-03-03 15:16:04 -05005103
Oliver Neukum0cc1a512008-01-10 10:31:48 +01005104 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 if (buf == NULL) {
5106 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
5107 /* assume the worst */
5108 return 1;
5109 }
5110 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05005111 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
5113 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05005114 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 dev_dbg(&udev->dev, "config index %d, error %d\n",
5116 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05005117 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 break;
5119 }
5120 if (memcmp (buf, udev->rawdescriptors[index], old_length)
5121 != 0) {
5122 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05005123 index,
5124 ((struct usb_config_descriptor *) buf)->
5125 bConfigurationValue);
5126 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 break;
5128 }
5129 }
Alan Sterneb764c42008-03-03 15:16:04 -05005130
5131 if (!changed && serial_len) {
5132 length = usb_string(udev, udev->descriptor.iSerialNumber,
5133 buf, serial_len);
5134 if (length + 1 != serial_len) {
5135 dev_dbg(&udev->dev, "serial string error %d\n",
5136 length);
5137 changed = 1;
5138 } else if (memcmp(buf, udev->serial, length) != 0) {
5139 dev_dbg(&udev->dev, "serial string changed\n");
5140 changed = 1;
5141 }
5142 }
5143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05005145 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146}
5147
5148/**
Ming Lei742120c2008-06-18 22:00:29 +08005149 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5151 *
Alan Stern79efa092006-06-01 13:33:42 -04005152 * WARNING - don't use this routine to reset a composite device
5153 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08005154 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 *
5156 * Do a port reset, reassign the device's address, and establish its
5157 * former operating configuration. If the reset fails, or the device's
5158 * descriptors change from their values before the reset, or the original
5159 * configuration and altsettings cannot be restored, a flag will be set
5160 * telling khubd to pretend the device has been disconnected and then
5161 * re-connected. All drivers will be unbound, and the device will be
5162 * re-enumerated and probed all over again.
5163 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005164 * Return: 0 if the reset succeeded, -ENODEV if the device has been
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 * flagged for logical disconnection, or some other negative error code
5166 * if the reset wasn't even attempted.
5167 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005168 * Note:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 * The caller must own the device lock. For example, it's safe to use
5170 * this from a driver probe() routine after downloading new firmware.
5171 * For calls that might not occur during probe(), drivers should lock
5172 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04005173 *
5174 * Locking exception: This routine may also be called from within an
5175 * autoresume handler. Such usage won't conflict with other tasks
5176 * holding the device lock because these tasks should always call
5177 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 */
Ming Lei742120c2008-06-18 22:00:29 +08005179static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
5181 struct usb_device *parent_hdev = udev->parent;
5182 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005183 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 struct usb_device_descriptor descriptor = udev->descriptor;
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005185 struct usb_host_bos *bos;
Hans de Goede7a7b5622013-11-08 16:37:26 +01005186 int i, j, ret = 0;
Alan Stern12c3da32005-11-23 12:09:52 -05005187 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
5189 if (udev->state == USB_STATE_NOTATTACHED ||
5190 udev->state == USB_STATE_SUSPENDED) {
5191 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5192 udev->state);
5193 return -EINVAL;
5194 }
5195
5196 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02005197 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08005198 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 return -EISDIR;
5200 }
Lan Tianyuad493e52013-01-23 04:26:30 +08005201 parent_hub = usb_hub_to_struct_hub(parent_hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
Sarah Sharpdcc01c02013-09-30 17:26:29 +03005203 /* Disable USB2 hardware LPM.
5204 * It will be re-enabled by the enumeration process.
5205 */
5206 if (udev->usb2_hw_lpm_enabled == 1)
5207 usb_set_usb2_hardware_lpm(udev, 0);
5208
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005209 bos = udev->bos;
5210 udev->bos = NULL;
5211
Sarah Sharpf74631e2012-06-25 12:08:08 -07005212 /* Disable LPM and LTM while we reset the device and reinstall the alt
5213 * settings. Device-initiated LPM settings, and system exit latency
5214 * settings are cleared when the device is reset, so we have to set
5215 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005216 */
5217 ret = usb_unlocked_disable_lpm(udev);
5218 if (ret) {
5219 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
5220 goto re_enumerate;
5221 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07005222 ret = usb_disable_ltm(udev);
5223 if (ret) {
5224 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
5225 __func__);
5226 goto re_enumerate;
5227 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005228
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 set_bit(port1, parent_hub->busy_bits);
5230 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
5231
5232 /* ep0 maxpacket size may change; let the HCD know about it.
5233 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07005234 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04005236 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 break;
5238 }
5239 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04005240
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 if (ret < 0)
5242 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005243
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 /* Device might have changed firmware (DFU or similar) */
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005245 if (descriptors_changed(udev, &descriptor, bos)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 dev_info(&udev->dev, "device firmware changed\n");
5247 udev->descriptor = descriptor; /* for disconnect() calls */
5248 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005249 }
Alan Stern4fe03872009-02-26 10:21:02 -05005250
5251 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 if (!udev->actconfig)
5253 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005254
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005255 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005256 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5257 if (ret < 0) {
5258 dev_warn(&udev->dev,
5259 "Busted HC? Not enough HCD resources for "
5260 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005261 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005262 goto re_enumerate;
5263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5265 USB_REQ_SET_CONFIGURATION, 0,
5266 udev->actconfig->desc.bConfigurationValue, 0,
5267 NULL, 0, USB_CTRL_SET_TIMEOUT);
5268 if (ret < 0) {
5269 dev_err(&udev->dev,
5270 "can't restore configuration #%d (error=%d)\n",
5271 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005272 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 goto re_enumerate;
Matthias Beyer469271f2013-10-10 23:41:27 +02005274 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005275 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5277
Alan Stern4fe03872009-02-26 10:21:02 -05005278 /* Put interfaces back into the same altsettings as before.
5279 * Don't bother to send the Set-Interface request for interfaces
5280 * that were already in altsetting 0; besides being unnecessary,
5281 * many devices can't handle it. Instead just reset the host-side
5282 * endpoint state.
5283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005285 struct usb_host_config *config = udev->actconfig;
5286 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 struct usb_interface_descriptor *desc;
5288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005290 if (desc->bAlternateSetting == 0) {
5291 usb_disable_interface(udev, intf, true);
5292 usb_enable_interface(udev, intf, true);
5293 ret = 0;
5294 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005295 /* Let the bandwidth allocation function know that this
5296 * device has been reset, and it will have to use
5297 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005298 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005299 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005300 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5301 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005302 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 if (ret < 0) {
5305 dev_err(&udev->dev, "failed to restore interface %d "
5306 "altsetting %d (error=%d)\n",
5307 desc->bInterfaceNumber,
5308 desc->bAlternateSetting,
5309 ret);
5310 goto re_enumerate;
5311 }
Hans de Goede7a7b5622013-11-08 16:37:26 +01005312 /* Resetting also frees any allocated streams */
5313 for (j = 0; j < intf->cur_altsetting->desc.bNumEndpoints; j++)
5314 intf->cur_altsetting->endpoint[j].streams = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 }
5316
5317done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005318 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Sarah Sharpde68bab2013-09-30 17:26:28 +03005319 usb_set_usb2_hardware_lpm(udev, 1);
Alan Stern78d9a482008-06-23 16:00:40 -04005320 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005321 usb_enable_ltm(udev);
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005322 usb_release_bos_descriptor(udev);
5323 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 return 0;
Matthias Beyer469271f2013-10-10 23:41:27 +02005325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005327 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 hub_port_logical_disconnect(parent_hub, port1);
Xenia Ragiadakoue3376d62013-08-29 21:45:48 +03005329 usb_release_bos_descriptor(udev);
5330 udev->bos = bos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332}
5333
5334/**
5335 * usb_reset_device - warn interface drivers and perform a USB port reset
5336 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5337 *
Alan Stern79efa092006-06-01 13:33:42 -04005338 * Warns all drivers bound to registered interfaces (using their pre_reset
5339 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005340 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005341 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005342 * Return: The same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04005343 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005344 * Note:
Alan Stern79efa092006-06-01 13:33:42 -04005345 * The caller must own the device lock. For example, it's safe to use
5346 * this from a driver probe() routine after downloading new firmware.
5347 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005348 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005349 *
5350 * If an interface is currently being probed or disconnected, we assume
5351 * its driver knows how to handle resets. For all other interfaces,
5352 * if the driver doesn't have pre_reset and post_reset methods then
5353 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005354 */
Ming Lei742120c2008-06-18 22:00:29 +08005355int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005356{
5357 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005358 int i;
Ming Lei4d769de2013-02-22 16:34:22 -08005359 unsigned int noio_flag;
Alan Stern79efa092006-06-01 13:33:42 -04005360 struct usb_host_config *config = udev->actconfig;
5361
5362 if (udev->state == USB_STATE_NOTATTACHED ||
5363 udev->state == USB_STATE_SUSPENDED) {
5364 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5365 udev->state);
5366 return -EINVAL;
5367 }
5368
Ming Lei4d769de2013-02-22 16:34:22 -08005369 /*
5370 * Don't allocate memory with GFP_KERNEL in current
5371 * context to avoid possible deadlock if usb mass
5372 * storage interface or usbnet interface(iSCSI case)
5373 * is included in current configuration. The easist
5374 * approach is to do it for every device reset,
5375 * because the device 'memalloc_noio' flag may have
5376 * not been set before reseting the usb device.
5377 */
5378 noio_flag = memalloc_noio_save();
5379
Alan Stern645daaa2006-08-30 15:47:02 -04005380 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005381 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005382
Alan Stern79efa092006-06-01 13:33:42 -04005383 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005384 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005385 struct usb_interface *cintf = config->interface[i];
5386 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005387 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005388
5389 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005390 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005391 if (drv->pre_reset && drv->post_reset)
5392 unbind = (drv->pre_reset)(cintf);
5393 else if (cintf->condition ==
5394 USB_INTERFACE_BOUND)
5395 unbind = 1;
5396 if (unbind)
5397 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005398 }
5399 }
5400 }
5401
Ming Lei742120c2008-06-18 22:00:29 +08005402 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005403
5404 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005405 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005406 struct usb_interface *cintf = config->interface[i];
5407 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005408 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005409
Alan Stern78d9a482008-06-23 16:00:40 -04005410 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005411 drv = to_usb_driver(cintf->dev.driver);
5412 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005413 rebind = (drv->post_reset)(cintf);
5414 else if (cintf->condition ==
5415 USB_INTERFACE_BOUND)
5416 rebind = 1;
Alan Stern6aec0442014-03-12 11:30:38 -04005417 if (rebind)
5418 cintf->needs_binding = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005419 }
Alan Stern79efa092006-06-01 13:33:42 -04005420 }
Alan Stern6aec0442014-03-12 11:30:38 -04005421 usb_unbind_and_rebind_marked_interfaces(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005422 }
5423
Alan Stern94fcda12006-11-20 11:38:46 -05005424 usb_autosuspend_device(udev);
Ming Lei4d769de2013-02-22 16:34:22 -08005425 memalloc_noio_restore(noio_flag);
Alan Stern79efa092006-06-01 13:33:42 -04005426 return ret;
5427}
Ming Lei742120c2008-06-18 22:00:29 +08005428EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005429
5430
5431/**
5432 * usb_queue_reset_device - Reset a USB device from an atomic context
5433 * @iface: USB interface belonging to the device to reset
5434 *
5435 * This function can be used to reset a USB device from an atomic
5436 * context, where usb_reset_device() won't work (as it blocks).
5437 *
5438 * Doing a reset via this method is functionally equivalent to calling
5439 * usb_reset_device(), except for the fact that it is delayed to a
5440 * workqueue. This means that any drivers bound to other interfaces
5441 * might be unbound, as well as users from usbfs in user space.
5442 *
5443 * Corner cases:
5444 *
5445 * - Scheduling two resets at the same time from two different drivers
5446 * attached to two different interfaces of the same device is
5447 * possible; depending on how the driver attached to each interface
5448 * handles ->pre_reset(), the second reset might happen or not.
5449 *
5450 * - If a driver is unbound and it had a pending reset, the reset will
5451 * be cancelled.
5452 *
5453 * - This function can be called during .probe() or .disconnect()
5454 * times. On return from .disconnect(), any pending resets will be
5455 * cancelled.
5456 *
5457 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5458 * does its own.
5459 *
5460 * NOTE: We don't do any reference count tracking because it is not
5461 * needed. The lifecycle of the work_struct is tied to the
5462 * usb_interface. Before destroying the interface we cancel the
5463 * work_struct, so the fact that work_struct is queued and or
5464 * running means the interface (and thus, the device) exist and
5465 * are referenced.
5466 */
5467void usb_queue_reset_device(struct usb_interface *iface)
5468{
5469 schedule_work(&iface->reset_ws);
5470}
5471EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c792012-09-05 13:44:32 +08005472
5473/**
5474 * usb_hub_find_child - Get the pointer of child device
5475 * attached to the port which is specified by @port1.
5476 * @hdev: USB device belonging to the usb hub
5477 * @port1: port num to indicate which port the child device
5478 * is attached to.
5479 *
5480 * USB drivers call this function to get hub's child device
5481 * pointer.
5482 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005483 * Return: %NULL if input param is invalid and
Lan Tianyuff823c792012-09-05 13:44:32 +08005484 * child's usb_device pointer if non-NULL.
5485 */
5486struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5487 int port1)
5488{
Lan Tianyuad493e52013-01-23 04:26:30 +08005489 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyuff823c792012-09-05 13:44:32 +08005490
5491 if (port1 < 1 || port1 > hdev->maxchild)
5492 return NULL;
5493 return hub->ports[port1 - 1]->child;
5494}
5495EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005496
Lan Tianyud2123fd2013-01-21 22:18:00 +08005497void usb_hub_adjust_deviceremovable(struct usb_device *hdev,
5498 struct usb_hub_descriptor *desc)
5499{
Dan Williamsd99f6b42014-05-20 18:08:17 -07005500 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud2123fd2013-01-21 22:18:00 +08005501 enum usb_port_connect_type connect_type;
5502 int i;
5503
Dan Williamsd99f6b42014-05-20 18:08:17 -07005504 if (!hub)
5505 return;
5506
Lan Tianyud2123fd2013-01-21 22:18:00 +08005507 if (!hub_is_superspeed(hdev)) {
5508 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005509 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005510
Dan Williamsd99f6b42014-05-20 18:08:17 -07005511 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005512 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5513 u8 mask = 1 << (i%8);
5514
5515 if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005516 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005517 desc->u.hs.DeviceRemovable[i/8] |= mask;
5518 }
5519 }
5520 }
5521 } else {
5522 u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable);
5523
5524 for (i = 1; i <= hdev->maxchild; i++) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005525 struct usb_port *port_dev = hub->ports[i - 1];
Lan Tianyud2123fd2013-01-21 22:18:00 +08005526
Dan Williamsd99f6b42014-05-20 18:08:17 -07005527 connect_type = port_dev->connect_type;
Lan Tianyud2123fd2013-01-21 22:18:00 +08005528 if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) {
5529 u16 mask = 1 << i;
5530
5531 if (!(port_removable & mask)) {
Dan Williamsd99f6b42014-05-20 18:08:17 -07005532 dev_dbg(&port_dev->dev, "DeviceRemovable is changed to 1 according to platform information.\n");
Lan Tianyud2123fd2013-01-21 22:18:00 +08005533 port_removable |= mask;
5534 }
5535 }
5536 }
5537
5538 desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable);
5539 }
5540}
5541
Lan Tianyud5575422012-09-05 13:44:33 +08005542#ifdef CONFIG_ACPI
5543/**
5544 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5545 * @hdev: USB device belonging to the usb hub
5546 * @port1: port num of the port
5547 *
Yacine Belkadi626f0902013-08-02 20:10:04 +02005548 * Return: Port's acpi handle if successful, %NULL if params are
5549 * invalid.
Lan Tianyud5575422012-09-05 13:44:33 +08005550 */
5551acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5552 int port1)
5553{
Lan Tianyuad493e52013-01-23 04:26:30 +08005554 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Lan Tianyud5575422012-09-05 13:44:33 +08005555
Mathias Nyman413412612013-06-18 17:28:48 +03005556 if (!hub)
5557 return NULL;
5558
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +01005559 return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
Lan Tianyud5575422012-09-05 13:44:33 +08005560}
5561#endif