blob: 968ec37a0f02bf955dfc8f3d92e2321f5125b423 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * USB hub driver.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 * (C) Copyright 1999 Johannes Erdfelt
6 * (C) Copyright 1999 Gregory P. Smith
7 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8 *
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/completion.h>
16#include <linux/sched.h>
17#include <linux/list.h>
18#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/ioctl.h>
20#include <linux/usb.h>
21#include <linux/usbdevice_fs.h>
Eric Lescouet27729aa2010-04-24 23:21:52 +020022#include <linux/usb/hcd.h>
Richard Zhao925aa462012-07-11 11:09:28 +080023#include <linux/usb/otg.h>
Phil Dibowitz93362a82010-07-22 00:05:01 +020024#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070025#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010026#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Theodore Ts'ob04b3152012-07-04 11:22:20 -040028#include <linux/random.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/uaccess.h>
31#include <asm/byteorder.h>
32
33#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080035/* if we are in debug mode, always announce new devices */
36#ifdef DEBUG
37#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
38#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
39#endif
40#endif
41
Ming Leie6f30de2012-10-24 11:59:24 +080042#define USB_VENDOR_GENESYS_LOGIC 0x05e3
43#define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01
44
Lan Tianyufa2a9562012-09-05 13:44:31 +080045struct usb_port {
Lan Tianyuff823c72012-09-05 13:44:32 +080046 struct usb_device *child;
Lan Tianyufa2a9562012-09-05 13:44:31 +080047 struct device dev;
48 struct dev_state *port_owner;
Lan Tianyu05f91682012-09-05 13:44:34 +080049 enum usb_port_connect_type connect_type;
Lan Tianyufa2a9562012-09-05 13:44:31 +080050};
51
Alan Stern1bb5f662006-11-06 11:56:13 -050052struct usb_hub {
53 struct device *intfdev; /* the "interface" device */
54 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040055 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050056 struct urb *urb; /* for interrupt polling pipe */
57
58 /* buffer for urb ... with extra space in case of babble */
59 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050060 union {
61 struct usb_hub_status hub;
62 struct usb_port_status port;
63 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050064 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050065
66 int error; /* last reported error */
67 int nerrors; /* track consecutive errors */
68
69 struct list_head event_list; /* hubs w/data or errs ready */
70 unsigned long event_bits[1]; /* status change bitmask */
71 unsigned long change_bits[1]; /* ports with logical connect
72 status change */
73 unsigned long busy_bits[1]; /* ports being reset or
74 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040075 unsigned long removed_bits[1]; /* ports with a "removed"
76 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080077 unsigned long wakeup_bits[1]; /* ports that have signaled
78 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050079#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
80#error event_bits[] is too short!
81#endif
82
83 struct usb_hub_descriptor *descriptor; /* class descriptor */
84 struct usb_tt tt; /* Transaction Translator */
85
86 unsigned mA_per_port; /* current for each child */
87
88 unsigned limited_power:1;
89 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040090 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050091
Ming Leie6f30de2012-10-24 11:59:24 +080092 unsigned quirk_check_port_auto_suspend:1;
93
Alan Stern1bb5f662006-11-06 11:56:13 -050094 unsigned has_indicators:1;
95 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000096 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -040097 struct delayed_work init_work;
Lan Tianyufa2a9562012-09-05 13:44:31 +080098 struct usb_port **ports;
Alan Stern1bb5f662006-11-06 11:56:13 -050099};
100
John Youndbe79bb2001-09-17 00:00:00 -0700101static inline int hub_is_superspeed(struct usb_device *hdev)
102{
Aman Deep7bf01182011-12-08 12:05:22 +0530103 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -0700104}
Alan Stern1bb5f662006-11-06 11:56:13 -0500105
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700106/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -0500107 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
109static DEFINE_SPINLOCK(device_state_lock);
110
111/* khubd's worklist and its lock */
112static DEFINE_SPINLOCK(hub_event_lock);
113static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
114
115/* Wakes up khubd */
116static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
117
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700118static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030121static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122module_param (blinkenlights, bool, S_IRUGO);
123MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
124
125/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200126 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
127 * 10 seconds to send reply for the initial 64-byte descriptor request.
128 */
129/* define initial 64-byte descriptor request timeout in milliseconds */
130static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
131module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800132MODULE_PARM_DESC(initial_descriptor_timeout,
133 "initial 64-byte descriptor request timeout in milliseconds "
134 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200135
136/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 * As of 2.6.10 we introduce a new USB device initialization scheme which
138 * closely resembles the way Windows works. Hopefully it will be compatible
139 * with a wider range of devices than the old scheme. However some previously
140 * working devices may start giving rise to "device not accepting address"
141 * errors; if that happens the user can try the old scheme by adjusting the
142 * following module parameters.
143 *
144 * For maximum flexibility there are two boolean parameters to control the
145 * hub driver's behavior. On the first initialization attempt, if the
146 * "old_scheme_first" parameter is set then the old scheme will be used,
147 * otherwise the new scheme is used. If that fails and "use_both_schemes"
148 * is set, then the driver will make another attempt, using the other scheme.
149 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030150static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
152MODULE_PARM_DESC(old_scheme_first,
153 "start with the old device initialization scheme");
154
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030155static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
157MODULE_PARM_DESC(use_both_schemes,
158 "try the other device initialization scheme if the "
159 "first one fails");
160
Alan Stern32fe0192007-10-10 16:27:07 -0400161/* Mutual exclusion for EHCI CF initialization. This interferes with
162 * port reset on some companion controllers.
163 */
164DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
165EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
166
Alan Stern948fea32008-04-28 11:07:07 -0400167#define HUB_DEBOUNCE_TIMEOUT 1500
168#define HUB_DEBOUNCE_STEP 25
169#define HUB_DEBOUNCE_STABLE 100
170
Lan Tianyufa2a9562012-09-05 13:44:31 +0800171#define to_usb_port(_dev) \
172 container_of(_dev, struct usb_port, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Ming Lei742120c2008-06-18 22:00:29 +0800174static int usb_reset_and_verify_device(struct usb_device *udev);
175
Sarah Sharp131dec32010-12-06 21:00:19 -0800176static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Sarah Sharp131dec32010-12-06 21:00:19 -0800178 if (hub_is_superspeed(hub->hdev))
179 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500180 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500182 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return "1.5 Mb/s";
184 else
185 return "12 Mb/s";
186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400189static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Lan Tianyuff823c72012-09-05 13:44:32 +0800191 if (!hdev || !hdev->actconfig || !hdev->maxchild)
Alan Stern251180842009-07-22 14:41:18 -0400192 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return usb_get_intfdata(hdev->actconfig->interface[0]);
194}
195
Sarah Sharpd9b20992012-02-20 08:31:26 -0800196static int usb_device_supports_lpm(struct usb_device *udev)
197{
198 /* USB 2.1 (and greater) devices indicate LPM support through
199 * their USB 2.0 Extended Capabilities BOS descriptor.
200 */
201 if (udev->speed == USB_SPEED_HIGH) {
202 if (udev->bos->ext_cap &&
203 (USB_LPM_SUPPORT &
204 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
205 return 1;
206 return 0;
207 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800208
209 /* All USB 3.0 must support LPM, but we need their max exit latency
210 * information from the SuperSpeed Extended Capabilities BOS descriptor.
211 */
212 if (!udev->bos->ss_cap) {
213 dev_warn(&udev->dev, "No LPM exit latency info found. "
214 "Power management will be impacted.\n");
215 return 0;
216 }
217 if (udev->parent->lpm_capable)
218 return 1;
219
220 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
221 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800222 return 0;
223}
224
Sarah Sharp51e0a012012-02-20 12:02:19 -0800225/*
226 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
227 * either U1 or U2.
228 */
229static void usb_set_lpm_mel(struct usb_device *udev,
230 struct usb3_lpm_parameters *udev_lpm_params,
231 unsigned int udev_exit_latency,
232 struct usb_hub *hub,
233 struct usb3_lpm_parameters *hub_lpm_params,
234 unsigned int hub_exit_latency)
235{
236 unsigned int total_mel;
237 unsigned int device_mel;
238 unsigned int hub_mel;
239
240 /*
241 * Calculate the time it takes to transition all links from the roothub
242 * to the parent hub into U0. The parent hub must then decode the
243 * packet (hub header decode latency) to figure out which port it was
244 * bound for.
245 *
246 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
247 * means 0.1us). Multiply that by 100 to get nanoseconds.
248 */
249 total_mel = hub_lpm_params->mel +
250 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
251
252 /*
253 * How long will it take to transition the downstream hub's port into
254 * U0? The greater of either the hub exit latency or the device exit
255 * latency.
256 *
257 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
258 * Multiply that by 1000 to get nanoseconds.
259 */
260 device_mel = udev_exit_latency * 1000;
261 hub_mel = hub_exit_latency * 1000;
262 if (device_mel > hub_mel)
263 total_mel += device_mel;
264 else
265 total_mel += hub_mel;
266
267 udev_lpm_params->mel = total_mel;
268}
269
270/*
271 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
272 * a transition from either U1 or U2.
273 */
274static void usb_set_lpm_pel(struct usb_device *udev,
275 struct usb3_lpm_parameters *udev_lpm_params,
276 unsigned int udev_exit_latency,
277 struct usb_hub *hub,
278 struct usb3_lpm_parameters *hub_lpm_params,
279 unsigned int hub_exit_latency,
280 unsigned int port_to_port_exit_latency)
281{
282 unsigned int first_link_pel;
283 unsigned int hub_pel;
284
285 /*
286 * First, the device sends an LFPS to transition the link between the
287 * device and the parent hub into U0. The exit latency is the bigger of
288 * the device exit latency or the hub exit latency.
289 */
290 if (udev_exit_latency > hub_exit_latency)
291 first_link_pel = udev_exit_latency * 1000;
292 else
293 first_link_pel = hub_exit_latency * 1000;
294
295 /*
296 * When the hub starts to receive the LFPS, there is a slight delay for
297 * it to figure out that one of the ports is sending an LFPS. Then it
298 * will forward the LFPS to its upstream link. The exit latency is the
299 * delay, plus the PEL that we calculated for this hub.
300 */
301 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
302
303 /*
304 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
305 * is the greater of the two exit latencies.
306 */
307 if (first_link_pel > hub_pel)
308 udev_lpm_params->pel = first_link_pel;
309 else
310 udev_lpm_params->pel = hub_pel;
311}
312
313/*
314 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
315 * when a device initiates a transition to U0, until when it will receive the
316 * first packet from the host controller.
317 *
318 * Section C.1.5.1 describes the four components to this:
319 * - t1: device PEL
320 * - t2: time for the ERDY to make it from the device to the host.
321 * - t3: a host-specific delay to process the ERDY.
322 * - t4: time for the packet to make it from the host to the device.
323 *
324 * t3 is specific to both the xHCI host and the platform the host is integrated
325 * into. The Intel HW folks have said it's negligible, FIXME if a different
326 * vendor says otherwise.
327 */
328static void usb_set_lpm_sel(struct usb_device *udev,
329 struct usb3_lpm_parameters *udev_lpm_params)
330{
331 struct usb_device *parent;
332 unsigned int num_hubs;
333 unsigned int total_sel;
334
335 /* t1 = device PEL */
336 total_sel = udev_lpm_params->pel;
337 /* How many external hubs are in between the device & the root port. */
338 for (parent = udev->parent, num_hubs = 0; parent->parent;
339 parent = parent->parent)
340 num_hubs++;
341 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
342 if (num_hubs > 0)
343 total_sel += 2100 + 250 * (num_hubs - 1);
344
345 /* t4 = 250ns * num_hubs */
346 total_sel += 250 * num_hubs;
347
348 udev_lpm_params->sel = total_sel;
349}
350
351static void usb_set_lpm_parameters(struct usb_device *udev)
352{
353 struct usb_hub *hub;
354 unsigned int port_to_port_delay;
355 unsigned int udev_u1_del;
356 unsigned int udev_u2_del;
357 unsigned int hub_u1_del;
358 unsigned int hub_u2_del;
359
360 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
361 return;
362
363 hub = hdev_to_hub(udev->parent);
364 /* It doesn't take time to transition the roothub into U0, since it
365 * doesn't have an upstream link.
366 */
367 if (!hub)
368 return;
369
370 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
371 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
372 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
373 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
374
375 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
376 hub, &udev->parent->u1_params, hub_u1_del);
377
378 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
379 hub, &udev->parent->u2_params, hub_u2_del);
380
381 /*
382 * Appendix C, section C.2.2.2, says that there is a slight delay from
383 * when the parent hub notices the downstream port is trying to
384 * transition to U0 to when the hub initiates a U0 transition on its
385 * upstream port. The section says the delays are tPort2PortU1EL and
386 * tPort2PortU2EL, but it doesn't define what they are.
387 *
388 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
389 * about the same delays. Use the maximum delay calculations from those
390 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
391 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
392 * assume the device exit latencies they are talking about are the hub
393 * exit latencies.
394 *
395 * What do we do if the U2 exit latency is less than the U1 exit
396 * latency? It's possible, although not likely...
397 */
398 port_to_port_delay = 1;
399
400 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
401 hub, &udev->parent->u1_params, hub_u1_del,
402 port_to_port_delay);
403
404 if (hub_u2_del > hub_u1_del)
405 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
406 else
407 port_to_port_delay = 1 + hub_u1_del;
408
409 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
410 hub, &udev->parent->u2_params, hub_u2_del,
411 port_to_port_delay);
412
413 /* Now that we've got PEL, calculate SEL. */
414 usb_set_lpm_sel(udev, &udev->u1_params);
415 usb_set_lpm_sel(udev, &udev->u2_params);
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700419static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
John Youndbe79bb2001-09-17 00:00:00 -0700421 int i, ret, size;
422 unsigned dtype;
423
424 if (hub_is_superspeed(hdev)) {
425 dtype = USB_DT_SS_HUB;
426 size = USB_DT_SS_HUB_SIZE;
427 } else {
428 dtype = USB_DT_HUB;
429 size = sizeof(struct usb_hub_descriptor);
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 for (i = 0; i < 3; i++) {
433 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
434 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700435 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 USB_CTRL_GET_TIMEOUT);
437 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
438 return ret;
439 }
440 return -EINVAL;
441}
442
443/*
444 * USB 2.0 spec Section 11.24.2.1
445 */
446static int clear_hub_feature(struct usb_device *hdev, int feature)
447{
448 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
449 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
450}
451
452/*
453 * USB 2.0 spec Section 11.24.2.2
454 */
455static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
456{
457 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
458 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
459 NULL, 0, 1000);
460}
461
462/*
463 * USB 2.0 spec Section 11.24.2.13
464 */
465static int set_port_feature(struct usb_device *hdev, int port1, int feature)
466{
467 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
468 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
469 NULL, 0, 1000);
470}
471
472/*
473 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
474 * for info about using port indicators
475 */
476static void set_port_led(
477 struct usb_hub *hub,
478 int port1,
479 int selector
480)
481{
482 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
483 USB_PORT_FEAT_INDICATOR);
484 if (status < 0)
485 dev_dbg (hub->intfdev,
486 "port %d indicator %s status %d\n",
487 port1,
488 ({ char *s; switch (selector) {
489 case HUB_LED_AMBER: s = "amber"; break;
490 case HUB_LED_GREEN: s = "green"; break;
491 case HUB_LED_OFF: s = "off"; break;
492 case HUB_LED_AUTO: s = "auto"; break;
493 default: s = "??"; break;
494 }; s; }),
495 status);
496}
497
498#define LED_CYCLE_PERIOD ((2*HZ)/3)
499
David Howellsc4028952006-11-22 14:57:56 +0000500static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
David Howellsc4028952006-11-22 14:57:56 +0000502 struct usb_hub *hub =
503 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct usb_device *hdev = hub->hdev;
505 unsigned i;
506 unsigned changed = 0;
507 int cursor = -1;
508
509 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
510 return;
511
512 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
513 unsigned selector, mode;
514
515 /* 30%-50% duty cycle */
516
517 switch (hub->indicator[i]) {
518 /* cycle marker */
519 case INDICATOR_CYCLE:
520 cursor = i;
521 selector = HUB_LED_AUTO;
522 mode = INDICATOR_AUTO;
523 break;
524 /* blinking green = sw attention */
525 case INDICATOR_GREEN_BLINK:
526 selector = HUB_LED_GREEN;
527 mode = INDICATOR_GREEN_BLINK_OFF;
528 break;
529 case INDICATOR_GREEN_BLINK_OFF:
530 selector = HUB_LED_OFF;
531 mode = INDICATOR_GREEN_BLINK;
532 break;
533 /* blinking amber = hw attention */
534 case INDICATOR_AMBER_BLINK:
535 selector = HUB_LED_AMBER;
536 mode = INDICATOR_AMBER_BLINK_OFF;
537 break;
538 case INDICATOR_AMBER_BLINK_OFF:
539 selector = HUB_LED_OFF;
540 mode = INDICATOR_AMBER_BLINK;
541 break;
542 /* blink green/amber = reserved */
543 case INDICATOR_ALT_BLINK:
544 selector = HUB_LED_GREEN;
545 mode = INDICATOR_ALT_BLINK_OFF;
546 break;
547 case INDICATOR_ALT_BLINK_OFF:
548 selector = HUB_LED_AMBER;
549 mode = INDICATOR_ALT_BLINK;
550 break;
551 default:
552 continue;
553 }
554 if (selector != HUB_LED_AUTO)
555 changed = 1;
556 set_port_led(hub, i + 1, selector);
557 hub->indicator[i] = mode;
558 }
559 if (!changed && blinkenlights) {
560 cursor++;
561 cursor %= hub->descriptor->bNbrPorts;
562 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
563 hub->indicator[cursor] = INDICATOR_CYCLE;
564 changed++;
565 }
566 if (changed)
567 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
568}
569
570/* use a short timeout for hub/port status fetches */
571#define USB_STS_TIMEOUT 1000
572#define USB_STS_RETRIES 5
573
574/*
575 * USB 2.0 spec Section 11.24.2.6
576 */
577static int get_hub_status(struct usb_device *hdev,
578 struct usb_hub_status *data)
579{
580 int i, status = -ETIMEDOUT;
581
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200582 for (i = 0; i < USB_STS_RETRIES &&
583 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
585 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
586 data, sizeof(*data), USB_STS_TIMEOUT);
587 }
588 return status;
589}
590
591/*
592 * USB 2.0 spec Section 11.24.2.7
593 */
594static int get_port_status(struct usb_device *hdev, int port1,
595 struct usb_port_status *data)
596{
597 int i, status = -ETIMEDOUT;
598
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200599 for (i = 0; i < USB_STS_RETRIES &&
600 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
602 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
603 data, sizeof(*data), USB_STS_TIMEOUT);
604 }
605 return status;
606}
607
Alan Stern3eb14912008-03-03 15:15:43 -0500608static int hub_port_status(struct usb_hub *hub, int port1,
609 u16 *status, u16 *change)
610{
611 int ret;
612
613 mutex_lock(&hub->status_mutex);
614 ret = get_port_status(hub->hdev, port1, &hub->status->port);
615 if (ret < 4) {
616 dev_err(hub->intfdev,
617 "%s failed (err = %d)\n", __func__, ret);
618 if (ret >= 0)
619 ret = -EIO;
620 } else {
621 *status = le16_to_cpu(hub->status->port.wPortStatus);
622 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700623
Alan Stern3eb14912008-03-03 15:15:43 -0500624 ret = 0;
625 }
626 mutex_unlock(&hub->status_mutex);
627 return ret;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static void kick_khubd(struct usb_hub *hub)
631{
632 unsigned long flags;
633
634 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200635 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500637
638 /* Suppress autosuspend until khubd runs */
639 usb_autopm_get_interface_no_resume(
640 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 wake_up(&khubd_wait);
642 }
643 spin_unlock_irqrestore(&hub_event_lock, flags);
644}
645
646void usb_kick_khubd(struct usb_device *hdev)
647{
Alan Stern251180842009-07-22 14:41:18 -0400648 struct usb_hub *hub = hdev_to_hub(hdev);
649
650 if (hub)
651 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800654/*
655 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
656 * Notification, which indicates it had initiated remote wakeup.
657 *
658 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
659 * device initiates resume, so the USB core will not receive notice of the
660 * resume through the normal hub interrupt URB.
661 */
662void usb_wakeup_notification(struct usb_device *hdev,
663 unsigned int portnum)
664{
665 struct usb_hub *hub;
666
667 if (!hdev)
668 return;
669
670 hub = hdev_to_hub(hdev);
671 if (hub) {
672 set_bit(portnum, hub->wakeup_bits);
673 kick_khubd(hub);
674 }
675}
676EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100679static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200681 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400682 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100683 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned long bits;
685
Alan Sterne0152682007-08-24 15:42:52 -0400686 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 case -ENOENT: /* synchronous unlink */
688 case -ECONNRESET: /* async unlink */
689 case -ESHUTDOWN: /* hardware going away */
690 return;
691
692 default: /* presumably an error */
693 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400694 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if ((++hub->nerrors < 10) || hub->error)
696 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400697 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* let khubd handle things */
701 case 0: /* we got data: port status changed */
702 bits = 0;
703 for (i = 0; i < urb->actual_length; ++i)
704 bits |= ((unsigned long) ((*hub->buffer)[i]))
705 << (i*8);
706 hub->event_bits[0] = bits;
707 break;
708 }
709
710 hub->nerrors = 0;
711
712 /* Something happened, let khubd figure it out */
713 kick_khubd(hub);
714
715resubmit:
716 if (hub->quiescing)
717 return;
718
719 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
720 && status != -ENODEV && status != -EPERM)
721 dev_err (hub->intfdev, "resubmit --> %d\n", status);
722}
723
724/* USB 2.0 spec Section 11.24.2.3 */
725static inline int
726hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
727{
Alan Sternc2f65952009-11-18 11:37:15 -0500728 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
730 tt, NULL, 0, 1000);
731}
732
733/*
734 * enumeration blocks khubd for a long time. we use keventd instead, since
735 * long blocking there is the exception, not the rule. accordingly, HCDs
736 * talking to TTs must queue control transfers (not just bulk and iso), so
737 * both can talk to the same hub concurrently.
738 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400739static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
David Howellsc4028952006-11-22 14:57:56 +0000741 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400742 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 unsigned long flags;
744
745 spin_lock_irqsave (&hub->tt.lock, flags);
Octavian Purdila3b6054d2012-10-01 22:21:12 +0300746 while (!list_empty(&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400747 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct usb_tt_clear *clear;
749 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400750 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 int status;
752
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400753 next = hub->tt.clear_list.next;
754 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 list_del (&clear->clear_list);
756
757 /* drop lock so HCD can concurrently report other TT errors */
758 spin_unlock_irqrestore (&hub->tt.lock, flags);
759 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (status)
761 dev_err (&hdev->dev,
762 "clear tt %d (%04x) error %d\n",
763 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400764
765 /* Tell the HCD, even if the operation failed */
766 drv = clear->hcd->driver;
767 if (drv->clear_tt_buffer_complete)
768 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
769
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700770 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400771 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 spin_unlock_irqrestore (&hub->tt.lock, flags);
774}
775
776/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400777 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
778 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
780 * High speed HCDs use this to tell the hub driver that some split control or
781 * bulk transaction failed in a way that requires clearing internal state of
782 * a transaction translator. This is normally detected (and reported) from
783 * interrupt context.
784 *
785 * It may not be possible for that hub to handle additional full (or low)
786 * speed transactions until that state is fully cleared out.
787 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400788int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400790 struct usb_device *udev = urb->dev;
791 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct usb_tt *tt = udev->tt;
793 unsigned long flags;
794 struct usb_tt_clear *clear;
795
796 /* we've got to cope with an arbitrary number of pending TT clears,
797 * since each TT has "at least two" buffers that can need it (and
798 * there can be many TTs per hub). even if they're uncommon.
799 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800800 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
802 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400803 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805
806 /* info that CLEAR_TT_BUFFER needs */
807 clear->tt = tt->multi ? udev->ttport : 1;
808 clear->devinfo = usb_pipeendpoint (pipe);
809 clear->devinfo |= udev->devnum << 4;
810 clear->devinfo |= usb_pipecontrol (pipe)
811 ? (USB_ENDPOINT_XFER_CONTROL << 11)
812 : (USB_ENDPOINT_XFER_BULK << 11);
813 if (usb_pipein (pipe))
814 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400815
816 /* info for completion callback */
817 clear->hcd = bus_to_hcd(udev->bus);
818 clear->ep = urb->ep;
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* tell keventd to clear state for this TT */
821 spin_lock_irqsave (&tt->lock, flags);
822 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400823 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400827EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Alan Stern8520f382008-09-22 14:44:26 -0400829/* If do_delay is false, return the number of milliseconds the caller
830 * needs to delay.
831 */
832static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700835 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400836 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400837 u16 wHubCharacteristics =
838 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Alan Stern4489a572006-04-27 15:54:22 -0400840 /* Enable power on each port. Some hubs have reserved values
841 * of LPSM (> 2) in their descriptors, even though they are
842 * USB 2.0 hubs. Some hubs do not implement port-power switching
843 * but only emulate it. In all cases, the ports won't work
844 * unless we send these messages to the hub.
845 */
846 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400848 else
849 dev_dbg(hub->intfdev, "trying to enable port power on "
850 "non-switchable hub\n");
851 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
Greg Kroah-Hartmana0693bd2012-09-24 13:04:02 -0700852 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
David Brownellb7896962005-08-31 10:41:44 -0700854 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400855 delay = max(pgood_delay, (unsigned) 100);
856 if (do_delay)
857 msleep(delay);
858 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861static int hub_hub_status(struct usb_hub *hub,
862 u16 *status, u16 *change)
863{
864 int ret;
865
Alan Sterndb90e7a2007-02-05 09:56:15 -0500866 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 ret = get_hub_status(hub->hdev, &hub->status->hub);
868 if (ret < 0)
869 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800870 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 else {
872 *status = le16_to_cpu(hub->status->hub.wHubStatus);
873 *change = le16_to_cpu(hub->status->hub.wHubChange);
874 ret = 0;
875 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500876 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return ret;
878}
879
Sarah Sharp41e7e052012-11-14 16:42:32 -0800880static int hub_set_port_link_state(struct usb_hub *hub, int port1,
881 unsigned int link_status)
882{
883 return set_port_feature(hub->hdev,
884 port1 | (link_status << 3),
885 USB_PORT_FEAT_LINK_STATE);
886}
887
888/*
889 * If USB 3.0 ports are placed into the Disabled state, they will no longer
890 * detect any device connects or disconnects. This is generally not what the
891 * USB core wants, since it expects a disabled port to produce a port status
892 * change event when a new device connects.
893 *
894 * Instead, set the link state to Disabled, wait for the link to settle into
895 * that state, clear any change bits, and then put the port into the RxDetect
896 * state.
897 */
898static int hub_usb3_port_disable(struct usb_hub *hub, int port1)
899{
900 int ret;
901 int total_time;
902 u16 portchange, portstatus;
903
904 if (!hub_is_superspeed(hub->hdev))
905 return -EINVAL;
906
907 ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED);
908 if (ret) {
909 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
910 port1, ret);
911 return ret;
912 }
913
914 /* Wait for the link to enter the disabled state. */
915 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
916 ret = hub_port_status(hub, port1, &portstatus, &portchange);
917 if (ret < 0)
918 return ret;
919
920 if ((portstatus & USB_PORT_STAT_LINK_STATE) ==
921 USB_SS_PORT_LS_SS_DISABLED)
922 break;
923 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
924 break;
925 msleep(HUB_DEBOUNCE_STEP);
926 }
927 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
928 dev_warn(hub->intfdev, "Could not disable port %d after %d ms\n",
929 port1, total_time);
930
931 return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT);
932}
933
Alan Stern8b28c752005-08-10 17:04:13 -0400934static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
935{
936 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400937 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400938
Lan Tianyuff823c72012-09-05 13:44:32 +0800939 if (hub->ports[port1 - 1]->child && set_state)
940 usb_set_device_state(hub->ports[port1 - 1]->child,
Alan Stern8b28c752005-08-10 17:04:13 -0400941 USB_STATE_NOTATTACHED);
Sarah Sharp41e7e052012-11-14 16:42:32 -0800942 if (!hub->error) {
943 if (hub_is_superspeed(hub->hdev))
944 ret = hub_usb3_port_disable(hub, port1);
945 else
946 ret = clear_port_feature(hdev, port1,
947 USB_PORT_FEAT_ENABLE);
948 }
Alan Stern8b28c752005-08-10 17:04:13 -0400949 if (ret)
950 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400951 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400952 return ret;
953}
954
Alan Stern0458d5b2007-05-04 11:52:20 -0400955/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800956 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400957 * time later khubd will disconnect() any existing usb_device on the port
958 * and will re-enumerate if there actually is a device attached.
959 */
960static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
961{
962 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
963 hub_port_disable(hub, port1, 1);
964
965 /* FIXME let caller ask to power down the port:
966 * - some devices won't enumerate without a VBUS power cycle
967 * - SRP saves power that way
968 * - ... new call, TBD ...
969 * That's easy if this hub can switch power per-port, and
970 * khubd reactivates the port later (timer, SRP, etc).
971 * Powerdown must be optional, because of reset/DFU.
972 */
973
974 set_bit(port1, hub->change_bits);
975 kick_khubd(hub);
976}
977
Alan Stern253e0572009-10-27 15:20:13 -0400978/**
979 * usb_remove_device - disable a device's port on its parent hub
980 * @udev: device to be disabled and removed
981 * Context: @udev locked, must be able to sleep.
982 *
983 * After @udev's port has been disabled, khubd is notified and it will
984 * see that the device has been disconnected. When the device is
985 * physically unplugged and something is plugged in, the events will
986 * be received and processed normally.
987 */
988int usb_remove_device(struct usb_device *udev)
989{
990 struct usb_hub *hub;
991 struct usb_interface *intf;
992
993 if (!udev->parent) /* Can't remove a root hub */
994 return -EINVAL;
995 hub = hdev_to_hub(udev->parent);
996 intf = to_usb_interface(hub->intfdev);
997
998 usb_autopm_get_interface(intf);
999 set_bit(udev->portnum, hub->removed_bits);
1000 hub_port_logical_disconnect(hub, udev->portnum);
1001 usb_autopm_put_interface(intf);
1002 return 0;
1003}
1004
Alan Stern6ee0b272008-04-28 11:06:42 -04001005enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -05001006 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -04001007 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -04001008};
Alan Stern5e6effa2008-03-03 15:15:51 -05001009
Alan Stern8520f382008-09-22 14:44:26 -04001010static void hub_init_func2(struct work_struct *ws);
1011static void hub_init_func3(struct work_struct *ws);
1012
Alan Sternf2835212008-04-28 11:07:17 -04001013static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -05001014{
1015 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -08001016 struct usb_hcd *hcd;
1017 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -05001018 int port1;
Alan Sternf2835212008-04-28 11:07:17 -04001019 int status;
Alan Stern948fea32008-04-28 11:07:07 -04001020 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -04001021 unsigned delay;
1022
1023 /* Continue a partial initialization */
1024 if (type == HUB_INIT2)
1025 goto init2;
1026 if (type == HUB_INIT3)
1027 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -05001028
Elric Fua45aa3b2012-02-18 13:32:27 +08001029 /* The superspeed hub except for root hub has to use Hub Depth
1030 * value as an offset into the route string to locate the bits
1031 * it uses to determine the downstream port number. So hub driver
1032 * should send a set hub depth request to superspeed hub after
1033 * the superspeed hub is set configuration in initialization or
1034 * reset procedure.
1035 *
1036 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -04001037 * For any other type of activation, turn it on.
1038 */
Alan Stern8520f382008-09-22 14:44:26 -04001039 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +08001040 if (hdev->parent && hub_is_superspeed(hdev)) {
1041 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
1042 HUB_SET_DEPTH, USB_RT_HUB,
1043 hdev->level - 1, 0, NULL, 0,
1044 USB_CTRL_SET_TIMEOUT);
1045 if (ret < 0)
1046 dev_err(hub->intfdev,
1047 "set hub depth failed\n");
1048 }
Alan Stern8520f382008-09-22 14:44:26 -04001049
1050 /* Speed up system boot by using a delayed_work for the
1051 * hub's initial power-up delays. This is pretty awkward
1052 * and the implementation looks like a home-brewed sort of
1053 * setjmp/longjmp, but it saves at least 100 ms for each
1054 * root hub (assuming usbcore is compiled into the kernel
1055 * rather than as a module). It adds up.
1056 *
1057 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
1058 * because for those activation types the ports have to be
1059 * operational when we return. In theory this could be done
1060 * for HUB_POST_RESET, but it's easier not to.
1061 */
1062 if (type == HUB_INIT) {
1063 delay = hub_power_on(hub, false);
1064 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
1065 schedule_delayed_work(&hub->init_work,
1066 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -04001067
1068 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -05001069 usb_autopm_get_interface_no_resume(
1070 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -04001071 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001072 } else if (type == HUB_RESET_RESUME) {
1073 /* The internal host controller state for the hub device
1074 * may be gone after a host power loss on system resume.
1075 * Update the device's info so the HW knows it's a hub.
1076 */
1077 hcd = bus_to_hcd(hdev->bus);
1078 if (hcd->driver->update_hub_device) {
1079 ret = hcd->driver->update_hub_device(hcd, hdev,
1080 &hub->tt, GFP_NOIO);
1081 if (ret < 0) {
1082 dev_err(hub->intfdev, "Host not "
1083 "accepting hub info "
1084 "update.\n");
1085 dev_err(hub->intfdev, "LS/FS devices "
1086 "and hubs may not work "
1087 "under this hub\n.");
1088 }
1089 }
1090 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001091 } else {
1092 hub_power_on(hub, true);
1093 }
1094 }
1095 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001096
Alan Stern6ee0b272008-04-28 11:06:42 -04001097 /* Check each port and set hub->change_bits to let khubd know
1098 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001099 */
1100 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001101 struct usb_device *udev = hub->ports[port1 - 1]->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))
1107 dev_dbg(hub->intfdev,
1108 "port %d: status %04x change %04x\n",
1109 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001110
Alan Stern6ee0b272008-04-28 11:06:42 -04001111 /* After anything other than HUB_RESUME (i.e., initialization
1112 * 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.
1124 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001125 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001126 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001127 clear_port_feature(hdev, port1,
1128 USB_PORT_FEAT_ENABLE);
1129 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001130 } else {
1131 /* Pretend that power was lost for USB3 devs */
1132 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001133 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001134 }
1135
Alan Stern948fea32008-04-28 11:07:07 -04001136 /* Clear status-change flags; we'll debounce later */
1137 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1138 need_debounce_delay = true;
1139 clear_port_feature(hub->hdev, port1,
1140 USB_PORT_FEAT_C_CONNECTION);
1141 }
1142 if (portchange & USB_PORT_STAT_C_ENABLE) {
1143 need_debounce_delay = true;
1144 clear_port_feature(hub->hdev, port1,
1145 USB_PORT_FEAT_C_ENABLE);
1146 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001147 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1148 hub_is_superspeed(hub->hdev)) {
1149 need_debounce_delay = true;
1150 clear_port_feature(hub->hdev, port1,
1151 USB_PORT_FEAT_C_BH_PORT_RESET);
1152 }
Alan Stern253e0572009-10-27 15:20:13 -04001153 /* We can forget about a "removed" device when there's a
1154 * physical disconnect or the connect status changes.
1155 */
1156 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1157 (portchange & USB_PORT_STAT_C_CONNECTION))
1158 clear_bit(port1, hub->removed_bits);
1159
Alan Stern6ee0b272008-04-28 11:06:42 -04001160 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1161 /* Tell khubd to disconnect the device or
1162 * check for a new connection
1163 */
1164 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1165 set_bit(port1, hub->change_bits);
1166
1167 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001168 bool port_resumed = (portstatus &
1169 USB_PORT_STAT_LINK_STATE) ==
1170 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001171 /* The power session apparently survived the resume.
1172 * If there was an overcurrent or suspend change
1173 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001174 * take care of it. Look at the port link state
1175 * for USB 3.0 hubs, since they don't have a suspend
1176 * change bit, and they don't set the port link change
1177 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001178 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001179 if (portchange || (hub_is_superspeed(hub->hdev) &&
1180 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001181 set_bit(port1, hub->change_bits);
1182
1183 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001184#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001185 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001186#endif
Alan Stern8808f002008-04-28 11:06:55 -04001187 set_bit(port1, hub->change_bits);
1188
Alan Stern6ee0b272008-04-28 11:06:42 -04001189 } else {
1190 /* The power session is gone; tell khubd */
1191 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1192 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001193 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001194 }
1195
Alan Stern948fea32008-04-28 11:07:07 -04001196 /* If no port-status-change flags were set, we don't need any
1197 * debouncing. If flags were set we can try to debounce the
1198 * ports all at once right now, instead of letting khubd do them
1199 * one at a time later on.
1200 *
1201 * If any port-status changes do occur during this delay, khubd
1202 * will see them later and handle them normally.
1203 */
Alan Stern8520f382008-09-22 14:44:26 -04001204 if (need_debounce_delay) {
1205 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001206
Alan Stern8520f382008-09-22 14:44:26 -04001207 /* Don't do a long sleep inside a workqueue routine */
1208 if (type == HUB_INIT2) {
1209 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1210 schedule_delayed_work(&hub->init_work,
1211 msecs_to_jiffies(delay));
1212 return; /* Continues at init3: below */
1213 } else {
1214 msleep(delay);
1215 }
1216 }
1217 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001218 hub->quiescing = 0;
1219
1220 status = usb_submit_urb(hub->urb, GFP_NOIO);
1221 if (status < 0)
1222 dev_err(hub->intfdev, "activate --> %d\n", status);
1223 if (hub->has_indicators && blinkenlights)
1224 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1225
1226 /* Scan all ports that need attention */
1227 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001228
1229 /* Allow autosuspend if it was suppressed */
1230 if (type <= HUB_INIT3)
1231 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001232}
1233
Alan Stern8520f382008-09-22 14:44:26 -04001234/* Implement the continuations for the delays above */
1235static void hub_init_func2(struct work_struct *ws)
1236{
1237 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1238
1239 hub_activate(hub, HUB_INIT2);
1240}
1241
1242static void hub_init_func3(struct work_struct *ws)
1243{
1244 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1245
1246 hub_activate(hub, HUB_INIT3);
1247}
1248
Alan Stern43303542008-04-28 11:07:31 -04001249enum hub_quiescing_type {
1250 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1251};
1252
1253static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1254{
1255 struct usb_device *hdev = hub->hdev;
1256 int i;
1257
Alan Stern8520f382008-09-22 14:44:26 -04001258 cancel_delayed_work_sync(&hub->init_work);
1259
Alan Stern43303542008-04-28 11:07:31 -04001260 /* khubd and related activity won't re-trigger */
1261 hub->quiescing = 1;
1262
1263 if (type != HUB_SUSPEND) {
1264 /* Disconnect all the children */
1265 for (i = 0; i < hdev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001266 if (hub->ports[i]->child)
1267 usb_disconnect(&hub->ports[i]->child);
Alan Stern43303542008-04-28 11:07:31 -04001268 }
1269 }
1270
1271 /* Stop khubd and related activity */
1272 usb_kill_urb(hub->urb);
1273 if (hub->has_indicators)
1274 cancel_delayed_work_sync(&hub->leds);
1275 if (hub->tt.hub)
Octavian Purdila036546b2012-10-23 11:33:12 +03001276 flush_work(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001277}
1278
Alan Stern3eb14912008-03-03 15:15:43 -05001279/* caller has locked the hub device */
1280static int hub_pre_reset(struct usb_interface *intf)
1281{
1282 struct usb_hub *hub = usb_get_intfdata(intf);
1283
Alan Stern43303542008-04-28 11:07:31 -04001284 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001285 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001286}
1287
1288/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001289static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001290{
Alan Stern7de18d82006-06-01 13:37:24 -04001291 struct usb_hub *hub = usb_get_intfdata(intf);
1292
Alan Sternf2835212008-04-28 11:07:17 -04001293 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001294 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001295}
1296
Lan Tianyufa2a9562012-09-05 13:44:31 +08001297static void usb_port_device_release(struct device *dev)
1298{
1299 struct usb_port *port_dev = to_usb_port(dev);
1300
1301 kfree(port_dev);
1302}
1303
1304static void usb_hub_remove_port_device(struct usb_hub *hub,
1305 int port1)
1306{
1307 device_unregister(&hub->ports[port1 - 1]->dev);
1308}
1309
1310struct device_type usb_port_device_type = {
1311 .name = "usb_port",
1312 .release = usb_port_device_release,
1313};
1314
1315static int usb_hub_create_port_device(struct usb_hub *hub,
1316 int port1)
1317{
1318 struct usb_port *port_dev = NULL;
1319 int retval;
1320
1321 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
1322 if (!port_dev) {
1323 retval = -ENOMEM;
1324 goto exit;
1325 }
1326
1327 hub->ports[port1 - 1] = port_dev;
1328 port_dev->dev.parent = hub->intfdev;
1329 port_dev->dev.type = &usb_port_device_type;
1330 dev_set_name(&port_dev->dev, "port%d", port1);
1331
1332 retval = device_register(&port_dev->dev);
1333 if (retval)
1334 goto error_register;
1335 return 0;
1336
1337error_register:
1338 put_device(&port_dev->dev);
1339exit:
1340 return retval;
1341}
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343static int hub_configure(struct usb_hub *hub,
1344 struct usb_endpoint_descriptor *endpoint)
1345{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001346 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct usb_device *hdev = hub->hdev;
1348 struct device *hub_dev = hub->intfdev;
1349 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001350 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 unsigned int pipe;
Lan Tianyufa2a9562012-09-05 13:44:31 +08001352 int maxp, ret, i;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001353 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Alan Sternd697cdd2009-10-27 15:18:46 -04001355 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 ret = -ENOMEM;
1358 goto fail;
1359 }
1360
1361 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1362 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 ret = -ENOMEM;
1364 goto fail;
1365 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001366 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1369 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 ret = -ENOMEM;
1371 goto fail;
1372 }
1373
1374 /* Request the entire hub descriptor.
1375 * hub->descriptor can handle USB_MAXCHILDREN ports,
1376 * but the hub can/will return fewer bytes here.
1377 */
John Youndbe79bb2001-09-17 00:00:00 -07001378 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (ret < 0) {
1380 message = "can't read hub descriptor";
1381 goto fail;
1382 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1383 message = "hub has too many ports!";
1384 ret = -ENODEV;
1385 goto fail;
1386 }
1387
1388 hdev->maxchild = hub->descriptor->bNbrPorts;
1389 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1390 (hdev->maxchild == 1) ? "" : "s");
1391
Lan Tianyufa2a9562012-09-05 13:44:31 +08001392 hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *),
1393 GFP_KERNEL);
Lan Tianyuff823c72012-09-05 13:44:32 +08001394 if (!hub->ports) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001395 ret = -ENOMEM;
1396 goto fail;
1397 }
1398
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001399 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
John Youndbe79bb2001-09-17 00:00:00 -07001401 /* FIXME for USB 3.0, skip for now */
1402 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1403 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 int i;
1405 char portstr [USB_MAXCHILDREN + 1];
1406
1407 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001408 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1410 ? 'F' : 'R';
1411 portstr[hdev->maxchild] = 0;
1412 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1413 } else
1414 dev_dbg(hub_dev, "standalone hub\n");
1415
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001416 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301417 case HUB_CHAR_COMMON_LPSM:
1418 dev_dbg(hub_dev, "ganged power switching\n");
1419 break;
1420 case HUB_CHAR_INDV_PORT_LPSM:
1421 dev_dbg(hub_dev, "individual port power switching\n");
1422 break;
1423 case HUB_CHAR_NO_LPSM:
1424 case HUB_CHAR_LPSM:
1425 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001429 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301430 case HUB_CHAR_COMMON_OCPM:
1431 dev_dbg(hub_dev, "global over-current protection\n");
1432 break;
1433 case HUB_CHAR_INDV_PORT_OCPM:
1434 dev_dbg(hub_dev, "individual port over-current protection\n");
1435 break;
1436 case HUB_CHAR_NO_OCPM:
1437 case HUB_CHAR_OCPM:
1438 dev_dbg(hub_dev, "no over-current protection\n");
1439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441
1442 spin_lock_init (&hub->tt.lock);
1443 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001444 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301446 case USB_HUB_PR_FS:
1447 break;
1448 case USB_HUB_PR_HS_SINGLE_TT:
1449 dev_dbg(hub_dev, "Single TT\n");
1450 hub->tt.hub = hdev;
1451 break;
1452 case USB_HUB_PR_HS_MULTI_TT:
1453 ret = usb_set_interface(hdev, 0, 1);
1454 if (ret == 0) {
1455 dev_dbg(hub_dev, "TT per port\n");
1456 hub->tt.multi = 1;
1457 } else
1458 dev_err(hub_dev, "Using single TT (err %d)\n",
1459 ret);
1460 hub->tt.hub = hdev;
1461 break;
1462 case USB_HUB_PR_SS:
1463 /* USB 3.0 hubs don't have a TT */
1464 break;
1465 default:
1466 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1467 hdev->descriptor.bDeviceProtocol);
1468 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001471 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001472 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001473 case HUB_TTTT_8_BITS:
1474 if (hdev->descriptor.bDeviceProtocol != 0) {
1475 hub->tt.think_time = 666;
1476 dev_dbg(hub_dev, "TT requires at most %d "
1477 "FS bit times (%d ns)\n",
1478 8, hub->tt.think_time);
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001481 case HUB_TTTT_16_BITS:
1482 hub->tt.think_time = 666 * 2;
1483 dev_dbg(hub_dev, "TT requires at most %d "
1484 "FS bit times (%d ns)\n",
1485 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001487 case HUB_TTTT_24_BITS:
1488 hub->tt.think_time = 666 * 3;
1489 dev_dbg(hub_dev, "TT requires at most %d "
1490 "FS bit times (%d ns)\n",
1491 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001493 case HUB_TTTT_32_BITS:
1494 hub->tt.think_time = 666 * 4;
1495 dev_dbg(hub_dev, "TT requires at most %d "
1496 "FS bit times (%d ns)\n",
1497 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
1499 }
1500
1501 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001502 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 hub->has_indicators = 1;
1504 dev_dbg(hub_dev, "Port indicators are supported\n");
1505 }
1506
1507 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1508 hub->descriptor->bPwrOn2PwrGood * 2);
1509
1510 /* power budgeting mostly matters with bus-powered hubs,
1511 * and battery-powered root hubs (may provide just 8 mA).
1512 */
1513 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001514 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 message = "can't get hub status";
1516 goto fail;
1517 }
Alan Stern7d35b922005-04-25 11:18:32 -04001518 le16_to_cpus(&hubstatus);
1519 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001520 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1521 hub->mA_per_port = 500;
1522 else {
1523 hub->mA_per_port = hdev->bus_mA;
1524 hub->limited_power = 1;
1525 }
Alan Stern7d35b922005-04-25 11:18:32 -04001526 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1528 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001529 hub->limited_power = 1;
1530 if (hdev->maxchild > 0) {
1531 int remaining = hdev->bus_mA -
1532 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Alan Stern55c52712005-11-23 12:03:12 -05001534 if (remaining < hdev->maxchild * 100)
1535 dev_warn(hub_dev,
1536 "insufficient power available "
1537 "to use all downstream ports\n");
1538 hub->mA_per_port = 100; /* 7.2.1.1 */
1539 }
1540 } else { /* Self-powered external hub */
1541 /* FIXME: What about battery-powered external hubs that
1542 * provide less current per port? */
1543 hub->mA_per_port = 500;
1544 }
1545 if (hub->mA_per_port < 500)
1546 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1547 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001549 /* Update the HCD's internal representation of this hub before khubd
1550 * starts getting port status changes for devices under the hub.
1551 */
1552 hcd = bus_to_hcd(hdev->bus);
1553 if (hcd->driver->update_hub_device) {
1554 ret = hcd->driver->update_hub_device(hcd, hdev,
1555 &hub->tt, GFP_KERNEL);
1556 if (ret < 0) {
1557 message = "can't update HCD hub info";
1558 goto fail;
1559 }
1560 }
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1563 if (ret < 0) {
1564 message = "can't get hub status";
1565 goto fail;
1566 }
1567
1568 /* local power status reports aren't always correct */
1569 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1570 dev_dbg(hub_dev, "local power source is %s\n",
1571 (hubstatus & HUB_STATUS_LOCAL_POWER)
1572 ? "lost (inactive)" : "good");
1573
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001574 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 dev_dbg(hub_dev, "%sover-current condition exists\n",
1576 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1577
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001578 /* set up the interrupt endpoint
1579 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1580 * bytes as USB2.0[11.12.3] says because some hubs are known
1581 * to send more data (and thus cause overflow). For root hubs,
1582 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1583 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1585 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1586
1587 if (maxp > sizeof(*hub->buffer))
1588 maxp = sizeof(*hub->buffer);
1589
1590 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1591 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 ret = -ENOMEM;
1593 goto fail;
1594 }
1595
1596 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1597 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 /* maybe cycle the hub leds */
1600 if (hub->has_indicators && blinkenlights)
1601 hub->indicator [0] = INDICATOR_CYCLE;
1602
Lan Tianyufa2a9562012-09-05 13:44:31 +08001603 for (i = 0; i < hdev->maxchild; i++)
1604 if (usb_hub_create_port_device(hub, i + 1) < 0)
1605 dev_err(hub->intfdev,
1606 "couldn't create port%d device.\n", i + 1);
1607
Alan Sternf2835212008-04-28 11:07:17 -04001608 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return 0;
1610
1611fail:
1612 dev_err (hub_dev, "config failed, %s (err %d)\n",
1613 message, ret);
1614 /* hub_disconnect() frees urb and descriptor */
1615 return ret;
1616}
1617
Alan Sterne8054852007-05-04 11:55:11 -04001618static void hub_release(struct kref *kref)
1619{
1620 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1621
1622 usb_put_intf(to_usb_interface(hub->intfdev));
1623 kfree(hub);
1624}
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626static unsigned highspeed_hubs;
1627
1628static void hub_disconnect(struct usb_interface *intf)
1629{
Huajun Li88162302012-03-12 21:00:19 +08001630 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001631 struct usb_device *hdev = interface_to_usbdev(intf);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001632 int i;
1633
Alan Sterne8054852007-05-04 11:55:11 -04001634 /* Take the hub off the event list and don't let it be added again */
1635 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001636 if (!list_empty(&hub->event_list)) {
1637 list_del_init(&hub->event_list);
1638 usb_autopm_put_interface_no_suspend(intf);
1639 }
Alan Sterne8054852007-05-04 11:55:11 -04001640 hub->disconnected = 1;
1641 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Alan Stern7de18d82006-06-01 13:37:24 -04001643 /* Disconnect all children and quiesce the hub */
1644 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001645 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001646
Alan Stern8b28c752005-08-10 17:04:13 -04001647 usb_set_intfdata (intf, NULL);
Alexander Shishkin1f2235b2012-09-12 14:48:31 +03001648
1649 for (i = 0; i < hdev->maxchild; i++)
1650 usb_hub_remove_port_device(hub, i + 1);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001651 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Alan Sterne8054852007-05-04 11:55:11 -04001653 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 highspeed_hubs--;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 usb_free_urb(hub->urb);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001657 kfree(hub->ports);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001658 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001659 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001660 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Alan Sterne8054852007-05-04 11:55:11 -04001662 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
1664
1665static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1666{
1667 struct usb_host_interface *desc;
1668 struct usb_endpoint_descriptor *endpoint;
1669 struct usb_device *hdev;
1670 struct usb_hub *hub;
1671
1672 desc = intf->cur_altsetting;
1673 hdev = interface_to_usbdev(intf);
1674
Ming Lei596d7892012-10-24 11:59:25 +08001675 /*
1676 * Set default autosuspend delay as 0 to speedup bus suspend,
1677 * based on the below considerations:
1678 *
1679 * - Unlike other drivers, the hub driver does not rely on the
1680 * autosuspend delay to provide enough time to handle a wakeup
1681 * event, and the submitted status URB is just to check future
1682 * change on hub downstream ports, so it is safe to do it.
1683 *
1684 * - The patch might cause one or more auto supend/resume for
1685 * below very rare devices when they are plugged into hub
1686 * first time:
1687 *
1688 * devices having trouble initializing, and disconnect
1689 * themselves from the bus and then reconnect a second
1690 * or so later
1691 *
1692 * devices just for downloading firmware, and disconnects
1693 * themselves after completing it
1694 *
1695 * For these quite rare devices, their drivers may change the
1696 * autosuspend delay of their parent hub in the probe() to one
1697 * appropriate value to avoid the subtle problem if someone
1698 * does care it.
1699 *
1700 * - The patch may cause one or more auto suspend/resume on
1701 * hub during running 'lsusb', but it is probably too
1702 * infrequent to worry about.
1703 *
1704 * - Change autosuspend delay of hub can avoid unnecessary auto
1705 * suspend timer for hub, also may decrease power consumption
1706 * of USB bus.
1707 */
1708 pm_runtime_set_autosuspend_delay(&hdev->dev, 0);
1709
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001710 /* Hubs have proper suspend/resume support. */
1711 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001712
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001713 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001714 dev_err(&intf->dev,
1715 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001716 return -E2BIG;
1717 }
1718
David Brownell89ccbdc2006-04-02 10:18:09 -08001719#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1720 if (hdev->parent) {
1721 dev_warn(&intf->dev, "ignoring external hub\n");
1722 return -ENODEV;
1723 }
1724#endif
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /* Some hubs have a subclass of 1, which AFAICT according to the */
1727 /* specs is not defined, but it works */
1728 if ((desc->desc.bInterfaceSubClass != 0) &&
1729 (desc->desc.bInterfaceSubClass != 1)) {
1730descriptor_error:
1731 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1732 return -EIO;
1733 }
1734
1735 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1736 if (desc->desc.bNumEndpoints != 1)
1737 goto descriptor_error;
1738
1739 endpoint = &desc->endpoint[0].desc;
1740
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001741 /* If it's not an interrupt in endpoint, we'd better punt! */
1742 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto descriptor_error;
1744
1745 /* We found a hub */
1746 dev_info (&intf->dev, "USB hub found\n");
1747
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001748 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (!hub) {
1750 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1751 return -ENOMEM;
1752 }
1753
Alan Sterne8054852007-05-04 11:55:11 -04001754 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 INIT_LIST_HEAD(&hub->event_list);
1756 hub->intfdev = &intf->dev;
1757 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001758 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001759 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001760 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001763 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 if (hdev->speed == USB_SPEED_HIGH)
1766 highspeed_hubs++;
1767
Ming Leie6f30de2012-10-24 11:59:24 +08001768 if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND)
1769 hub->quirk_check_port_auto_suspend = 1;
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (hub_configure(hub, endpoint) >= 0)
1772 return 0;
1773
1774 hub_disconnect (intf);
1775 return -ENODEV;
1776}
1777
1778static int
1779hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1780{
1781 struct usb_device *hdev = interface_to_usbdev (intf);
Lan Tianyuff823c72012-09-05 13:44:32 +08001782 struct usb_hub *hub = hdev_to_hub(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /* assert ifno == 0 (part of hub spec) */
1785 switch (code) {
1786 case USBDEVFS_HUB_PORTINFO: {
1787 struct usbdevfs_hub_portinfo *info = user_data;
1788 int i;
1789
1790 spin_lock_irq(&device_state_lock);
1791 if (hdev->devnum <= 0)
1792 info->nports = 0;
1793 else {
1794 info->nports = hdev->maxchild;
1795 for (i = 0; i < info->nports; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001796 if (hub->ports[i]->child == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 info->port[i] = 0;
1798 else
1799 info->port[i] =
Lan Tianyuff823c72012-09-05 13:44:32 +08001800 hub->ports[i]->child->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
1802 }
1803 spin_unlock_irq(&device_state_lock);
1804
1805 return info->nports + 1;
1806 }
1807
1808 default:
1809 return -ENOSYS;
1810 }
1811}
1812
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001813/*
1814 * Allow user programs to claim ports on a hub. When a device is attached
1815 * to one of these "claimed" ports, the program will "own" the device.
1816 */
1817static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001818 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001819{
1820 if (hdev->state == USB_STATE_NOTATTACHED)
1821 return -ENODEV;
1822 if (port1 == 0 || port1 > hdev->maxchild)
1823 return -EINVAL;
1824
1825 /* This assumes that devices not managed by the hub driver
1826 * will always have maxchild equal to 0.
1827 */
Lan Tianyufa2a9562012-09-05 13:44:31 +08001828 *ppowner = &(hdev_to_hub(hdev)->ports[port1 - 1]->port_owner);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001829 return 0;
1830}
1831
1832/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001833int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1834 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001835{
1836 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001837 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001838
1839 rc = find_port_owner(hdev, port1, &powner);
1840 if (rc)
1841 return rc;
1842 if (*powner)
1843 return -EBUSY;
1844 *powner = owner;
1845 return rc;
1846}
1847
Lan Tianyu336c5c32012-07-06 14:13:52 +08001848int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1849 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001850{
1851 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001852 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001853
1854 rc = find_port_owner(hdev, port1, &powner);
1855 if (rc)
1856 return rc;
1857 if (*powner != owner)
1858 return -ENOENT;
1859 *powner = NULL;
1860 return rc;
1861}
1862
Lan Tianyu336c5c32012-07-06 14:13:52 +08001863void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001864{
Lan Tianyufa2a9562012-09-05 13:44:31 +08001865 struct usb_hub *hub = hdev_to_hub(hdev);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001866 int n;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001867
Lan Tianyufa2a9562012-09-05 13:44:31 +08001868 for (n = 0; n < hdev->maxchild; n++) {
1869 if (hub->ports[n]->port_owner == owner)
1870 hub->ports[n]->port_owner = NULL;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001871 }
Lan Tianyufa2a9562012-09-05 13:44:31 +08001872
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001873}
1874
1875/* The caller must hold udev's lock */
1876bool usb_device_is_owned(struct usb_device *udev)
1877{
1878 struct usb_hub *hub;
1879
1880 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1881 return false;
1882 hub = hdev_to_hub(udev->parent);
Lan Tianyufa2a9562012-09-05 13:44:31 +08001883 return !!hub->ports[udev->portnum - 1]->port_owner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001884}
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1887{
Lan Tianyuff823c72012-09-05 13:44:32 +08001888 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 int i;
1890
1891 for (i = 0; i < udev->maxchild; ++i) {
Lan Tianyuff823c72012-09-05 13:44:32 +08001892 if (hub->ports[i]->child)
1893 recursively_mark_NOTATTACHED(hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001895 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001896 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 udev->state = USB_STATE_NOTATTACHED;
1898}
1899
1900/**
1901 * usb_set_device_state - change a device's current state (usbcore, hcds)
1902 * @udev: pointer to device whose state should be changed
1903 * @new_state: new state value to be stored
1904 *
1905 * udev->state is _not_ fully protected by the device lock. Although
1906 * most transitions are made only while holding the lock, the state can
1907 * can change to USB_STATE_NOTATTACHED at almost any time. This
1908 * is so that devices can be marked as disconnected as soon as possible,
1909 * without having to wait for any semaphores to be released. As a result,
1910 * all changes to any device's state must be protected by the
1911 * device_state_lock spinlock.
1912 *
1913 * Once a device has been added to the device tree, all changes to its state
1914 * should be made using this routine. The state should _not_ be set directly.
1915 *
1916 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1917 * Otherwise udev->state is set to new_state, and if new_state is
1918 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1919 * to USB_STATE_NOTATTACHED.
1920 */
1921void usb_set_device_state(struct usb_device *udev,
1922 enum usb_device_state new_state)
1923{
1924 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001925 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 spin_lock_irqsave(&device_state_lock, flags);
1928 if (udev->state == USB_STATE_NOTATTACHED)
1929 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001930 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001931
1932 /* root hub wakeup capabilities are managed out-of-band
1933 * and may involve silicon errata ... ignore them here.
1934 */
1935 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001936 if (udev->state == USB_STATE_SUSPENDED
1937 || new_state == USB_STATE_SUSPENDED)
1938 ; /* No change to wakeup settings */
1939 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001940 wakeup = udev->actconfig->desc.bmAttributes
1941 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001942 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001943 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001944 }
Sarah Sharp15123002007-12-21 16:54:15 -08001945 if (udev->state == USB_STATE_SUSPENDED &&
1946 new_state != USB_STATE_SUSPENDED)
1947 udev->active_duration -= jiffies;
1948 else if (new_state == USB_STATE_SUSPENDED &&
1949 udev->state != USB_STATE_SUSPENDED)
1950 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001951 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001952 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 recursively_mark_NOTATTACHED(udev);
1954 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001955 if (wakeup >= 0)
1956 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
David Vrabel6da9c992009-02-18 14:43:47 +00001958EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001960/*
Alan Stern3b29b682011-02-22 09:53:41 -05001961 * Choose a device number.
1962 *
1963 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1964 * USB-2.0 buses they are also used as device addresses, however on
1965 * USB-3.0 buses the address is assigned by the controller hardware
1966 * and it usually is not the same as the device number.
1967 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001968 * WUSB devices are simple: they have no hubs behind, so the mapping
1969 * device <-> virtual port number becomes 1:1. Why? to simplify the
1970 * life of the device connection logic in
1971 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1972 * handshake we need to assign a temporary address in the unauthorized
1973 * space. For simplicity we use the first virtual port number found to
1974 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1975 * and that becomes it's address [X < 128] or its unauthorized address
1976 * [X | 0x80].
1977 *
1978 * We add 1 as an offset to the one-based USB-stack port number
1979 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1980 * 0 is reserved by USB for default address; (b) Linux's USB stack
1981 * uses always #1 for the root hub of the controller. So USB stack's
1982 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001983 *
1984 * Devices connected under xHCI are not as simple. The host controller
1985 * supports virtualization, so the hardware assigns device addresses and
1986 * the HCD must setup data structures before issuing a set address
1987 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001988 */
Alan Stern3b29b682011-02-22 09:53:41 -05001989static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 int devnum;
1992 struct usb_bus *bus = udev->bus;
1993
1994 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001995 if (udev->wusb) {
1996 devnum = udev->portnum + 1;
1997 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1998 } else {
1999 /* Try to allocate the next devnum beginning at
2000 * bus->devnum_next. */
2001 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
2002 bus->devnum_next);
2003 if (devnum >= 128)
2004 devnum = find_next_zero_bit(bus->devmap.devicemap,
2005 128, 1);
2006 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 if (devnum < 128) {
2009 set_bit(devnum, bus->devmap.devicemap);
2010 udev->devnum = devnum;
2011 }
2012}
2013
Alan Stern3b29b682011-02-22 09:53:41 -05002014static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 if (udev->devnum > 0) {
2017 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
2018 udev->devnum = -1;
2019 }
2020}
2021
Alan Stern3b29b682011-02-22 09:53:41 -05002022static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07002023{
2024 /* The address for a WUSB device is managed by wusbcore. */
2025 if (!udev->wusb)
2026 udev->devnum = devnum;
2027}
2028
Herbert Xuf7410ce2010-01-10 20:15:03 +11002029static void hub_free_dev(struct usb_device *udev)
2030{
2031 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2032
2033 /* Root hubs aren't real devices, so don't free HCD resources */
2034 if (hcd->driver->free_dev && udev->parent)
2035 hcd->driver->free_dev(hcd, udev);
2036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/**
2039 * usb_disconnect - disconnect a device (usbcore-internal)
2040 * @pdev: pointer to device being disconnected
2041 * Context: !in_interrupt ()
2042 *
2043 * Something got disconnected. Get rid of it and all of its children.
2044 *
2045 * If *pdev is a normal device then the parent hub must already be locked.
2046 * If *pdev is a root hub then this routine will acquire the
2047 * usb_bus_list_lock on behalf of the caller.
2048 *
2049 * Only hub drivers (including virtual root hub drivers for host
2050 * controllers) should ever call this.
2051 *
2052 * This call is synchronous, and may not be used in an interrupt context.
2053 */
2054void usb_disconnect(struct usb_device **pdev)
2055{
2056 struct usb_device *udev = *pdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08002057 struct usb_hub *hub = hdev_to_hub(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 int i;
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 /* mark the device as inactive, so any further urb submissions for
2061 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002062 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 */
2064 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05002065 dev_info(&udev->dev, "USB disconnect, device number %d\n",
2066 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002068 usb_lock_device(udev);
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08002071 for (i = 0; i < udev->maxchild; i++) {
Lan Tianyuff823c72012-09-05 13:44:32 +08002072 if (hub->ports[i]->child)
2073 usb_disconnect(&hub->ports[i]->child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075
2076 /* deallocate hcd/hardware state ... nuking all pending urbs and
2077 * cleaning up all state associated with the current configuration
2078 * so that the hardware is now fully quiesced.
2079 */
Alan Stern782da722006-07-01 22:09:35 -04002080 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04002082 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Alan Stern3b23dd62008-12-05 14:10:34 -05002084 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04002085 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07002086
Alan Stern782da722006-07-01 22:09:35 -04002087 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002088 * for de-configuring the device and invoking the remove-device
2089 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002090 */
2091 device_del(&udev->dev);
2092
2093 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 * (or root_hub) pointer.
2095 */
Alan Stern3b29b682011-02-22 09:53:41 -05002096 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 /* Avoid races with recursively_mark_NOTATTACHED() */
2099 spin_lock_irq(&device_state_lock);
2100 *pdev = NULL;
2101 spin_unlock_irq(&device_state_lock);
2102
Herbert Xuf7410ce2010-01-10 20:15:03 +11002103 hub_free_dev(udev);
2104
Alan Stern782da722006-07-01 22:09:35 -04002105 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002108#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109static void show_string(struct usb_device *udev, char *id, char *string)
2110{
2111 if (!string)
2112 return;
Joe Perchesf2ec5222012-10-28 01:05:51 -07002113 dev_info(&udev->dev, "%s: %s\n", id, string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002116static void announce_device(struct usb_device *udev)
2117{
2118 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
2119 le16_to_cpu(udev->descriptor.idVendor),
2120 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002121 dev_info(&udev->dev,
2122 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002123 udev->descriptor.iManufacturer,
2124 udev->descriptor.iProduct,
2125 udev->descriptor.iSerialNumber);
2126 show_string(udev, "Product", udev->product);
2127 show_string(udev, "Manufacturer", udev->manufacturer);
2128 show_string(udev, "SerialNumber", udev->serial);
2129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08002131static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132#endif
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134#ifdef CONFIG_USB_OTG
2135#include "otg_whitelist.h"
2136#endif
2137
Oliver Neukum3ede7602007-01-11 14:35:50 +01002138/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002139 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01002140 * @udev: newly addressed device (in ADDRESS state)
2141 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002142 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01002143 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002144static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002146 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148#ifdef CONFIG_USB_OTG
2149 /*
2150 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
2151 * to wake us after we've powered off VBUS; and HNP, switching roles
2152 * "host" to "peripheral". The OTG descriptor helps figure this out.
2153 */
2154 if (!udev->bus->is_b_host
2155 && udev->config
2156 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02002157 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct usb_bus *bus = udev->bus;
2159
2160 /* descriptor may appear anywhere in config */
2161 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2162 le16_to_cpu(udev->config[0].desc.wTotalLength),
2163 USB_DT_OTG, (void **) &desc) == 0) {
2164 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002165 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 dev_info(&udev->dev,
2168 "Dual-Role OTG device on %sHNP port\n",
2169 (port1 == bus->otg_port)
2170 ? "" : "non-");
2171
2172 /* enable HNP before suspend, it's simpler */
2173 if (port1 == bus->otg_port)
2174 bus->b_hnp_enable = 1;
2175 err = usb_control_msg(udev,
2176 usb_sndctrlpipe(udev, 0),
2177 USB_REQ_SET_FEATURE, 0,
2178 bus->b_hnp_enable
2179 ? USB_DEVICE_B_HNP_ENABLE
2180 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2181 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2182 if (err < 0) {
2183 /* OTG MESSAGE: report errors here,
2184 * customize to match your product.
2185 */
2186 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002187 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 err);
2189 bus->b_hnp_enable = 0;
2190 }
2191 }
2192 }
2193 }
2194
2195 if (!is_targeted(udev)) {
2196
2197 /* Maybe it can talk to us, though we can't talk to it.
2198 * (Includes HNP test device.)
2199 */
2200 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002201 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (err < 0)
2203 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2204 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002205 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 goto fail;
2207 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002208fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002210 return err;
2211}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002213
2214/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002215 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002216 * @udev: newly addressed device (in ADDRESS state)
2217 *
2218 * This is only called by usb_new_device() and usb_authorize_device()
2219 * and FIXME -- all comments that apply to them apply here wrt to
2220 * environment.
2221 *
2222 * If the device is WUSB and not authorized, we don't attempt to read
2223 * the string descriptors, as they will be errored out by the device
2224 * until it has been authorized.
2225 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002226static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002227{
2228 int err;
2229
2230 if (udev->config == NULL) {
2231 err = usb_get_configuration(udev);
2232 if (err < 0) {
2233 dev_err(&udev->dev, "can't read configurations, error %d\n",
2234 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002235 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002236 }
2237 }
2238 if (udev->wusb == 1 && udev->authorized == 0) {
2239 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2240 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2241 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2242 }
2243 else {
2244 /* read the standard strings and cache them if present */
2245 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2246 udev->manufacturer = usb_cache_string(udev,
2247 udev->descriptor.iManufacturer);
2248 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2249 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002250 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002251 if (err < 0)
2252 return err;
2253
2254 usb_detect_interface_quirks(udev);
2255
2256 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002257}
2258
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002259static void set_usb_port_removable(struct usb_device *udev)
2260{
2261 struct usb_device *hdev = udev->parent;
2262 struct usb_hub *hub;
2263 u8 port = udev->portnum;
2264 u16 wHubCharacteristics;
2265 bool removable = true;
2266
2267 if (!hdev)
2268 return;
2269
2270 hub = hdev_to_hub(udev->parent);
2271
2272 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2273
2274 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2275 return;
2276
2277 if (hub_is_superspeed(hdev)) {
Lan Tianyuca3c1532012-09-11 04:22:36 +08002278 if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable)
2279 & (1 << port))
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002280 removable = false;
2281 } else {
2282 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2283 removable = false;
2284 }
2285
2286 if (removable)
2287 udev->removable = USB_DEVICE_REMOVABLE;
2288 else
2289 udev->removable = USB_DEVICE_FIXED;
2290}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002291
2292/**
2293 * usb_new_device - perform initial device setup (usbcore-internal)
2294 * @udev: newly addressed device (in ADDRESS state)
2295 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002296 * This is called with devices which have been detected but not fully
2297 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002298 * for any device configuration. The caller must have locked either
2299 * the parent hub (if udev is a normal device) or else the
2300 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2301 * udev has already been installed, but udev is not yet visible through
2302 * sysfs or other filesystem code.
2303 *
2304 * It will return if the device is configured properly or not. Zero if
2305 * the interface was registered with the driver core; else a negative
2306 * errno value.
2307 *
2308 * This call is synchronous, and may not be used in an interrupt context.
2309 *
2310 * Only the hub driver or root-hub registrar should ever call this.
2311 */
2312int usb_new_device(struct usb_device *udev)
2313{
2314 int err;
2315
Dan Streetman16985402010-01-06 09:56:53 -05002316 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002317 /* Initialize non-root-hub device wakeup to disabled;
2318 * device (un)configuration controls wakeup capable
2319 * sysfs power/wakeup controls wakeup enabled/disabled
2320 */
2321 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002322 }
2323
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002324 /* Tell the runtime-PM framework the device is active */
2325 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002326 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002327 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002328 pm_runtime_enable(&udev->dev);
2329
Alan Sternc08512c2010-11-15 15:57:58 -05002330 /* By default, forbid autosuspend for all devices. It will be
2331 * allowed for hubs during binding.
2332 */
2333 usb_disable_autosuspend(udev);
2334
Alan Stern8d8558d2009-12-08 15:50:41 -05002335 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002336 if (err < 0)
2337 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002338 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2339 udev->devnum, udev->bus->busnum,
2340 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002341 /* export the usbdev device-node for libusb */
2342 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2343 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2344
Alan Stern6cd13202008-11-13 15:08:30 -05002345 /* Tell the world! */
2346 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002347
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002348 if (udev->serial)
2349 add_device_randomness(udev->serial, strlen(udev->serial));
2350 if (udev->product)
2351 add_device_randomness(udev->product, strlen(udev->product));
2352 if (udev->manufacturer)
2353 add_device_randomness(udev->manufacturer,
2354 strlen(udev->manufacturer));
2355
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002356 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002357
2358 /*
2359 * check whether the hub marks this port as non-removable. Do it
2360 * now so that platform-specific data can override it in
2361 * device_add()
2362 */
2363 if (udev->parent)
2364 set_usb_port_removable(udev);
2365
Alan Stern782da722006-07-01 22:09:35 -04002366 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002367 * for configuring the device and invoking the add-device
2368 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002369 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002370 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (err) {
2372 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2373 goto fail;
2374 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002375
Alan Stern3b23dd62008-12-05 14:10:34 -05002376 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002377 usb_mark_last_busy(udev);
2378 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381fail:
2382 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002383 pm_runtime_disable(&udev->dev);
2384 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002385 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002388
2389/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002390 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2391 * @usb_dev: USB device
2392 *
2393 * Move the USB device to a very basic state where interfaces are disabled
2394 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002395 *
2396 * We share a lock (that we have) with device_del(), so we need to
2397 * defer its call.
2398 */
2399int usb_deauthorize_device(struct usb_device *usb_dev)
2400{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002401 usb_lock_device(usb_dev);
2402 if (usb_dev->authorized == 0)
2403 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002404
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002405 usb_dev->authorized = 0;
2406 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002407
2408 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002409 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002410 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002411 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002412 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002413 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002414
2415 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002416 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002417
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002418out_unauthorized:
2419 usb_unlock_device(usb_dev);
2420 return 0;
2421}
2422
2423
2424int usb_authorize_device(struct usb_device *usb_dev)
2425{
2426 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002427
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002428 usb_lock_device(usb_dev);
2429 if (usb_dev->authorized == 1)
2430 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002431
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002432 result = usb_autoresume_device(usb_dev);
2433 if (result < 0) {
2434 dev_err(&usb_dev->dev,
2435 "can't autoresume for authorization: %d\n", result);
2436 goto error_autoresume;
2437 }
2438 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2439 if (result < 0) {
2440 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2441 "authorization: %d\n", result);
2442 goto error_device_descriptor;
2443 }
Alan Sternda307122009-12-08 15:54:44 -05002444
2445 kfree(usb_dev->product);
2446 usb_dev->product = NULL;
2447 kfree(usb_dev->manufacturer);
2448 usb_dev->manufacturer = NULL;
2449 kfree(usb_dev->serial);
2450 usb_dev->serial = NULL;
2451
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002452 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002453 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002454 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002455 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002456 /* Choose and set the configuration. This registers the interfaces
2457 * with the driver core and lets interface drivers bind to them.
2458 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002459 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002460 if (c >= 0) {
2461 result = usb_set_configuration(usb_dev, c);
2462 if (result) {
2463 dev_err(&usb_dev->dev,
2464 "can't set config #%d, error %d\n", c, result);
2465 /* This need not be fatal. The user can try to
2466 * set other configurations. */
2467 }
2468 }
2469 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002470
Alan Stern8d8558d2009-12-08 15:50:41 -05002471error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002472error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002473 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002474error_autoresume:
2475out_authorized:
2476 usb_unlock_device(usb_dev); // complements locktree
2477 return result;
2478}
2479
2480
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002481/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2482static unsigned hub_is_wusb(struct usb_hub *hub)
2483{
2484 struct usb_hcd *hcd;
2485 if (hub->hdev->parent != NULL) /* not a root hub? */
2486 return 0;
2487 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2488 return hcd->wireless;
2489}
2490
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492#define PORT_RESET_TRIES 5
2493#define SET_ADDRESS_TRIES 2
2494#define GET_DESCRIPTOR_TRIES 2
2495#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302496#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2499#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002500#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501#define HUB_LONG_RESET_TIME 200
2502#define HUB_RESET_TIMEOUT 500
2503
Sarah Sharp10d674a2011-09-14 14:24:52 -07002504static int hub_port_reset(struct usb_hub *hub, int port1,
2505 struct usb_device *udev, unsigned int delay, bool warm);
2506
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002507/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2508 * Port worm reset is required to recover
2509 */
2510static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002511{
2512 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002513 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2514 USB_SS_PORT_LS_SS_INACTIVE) ||
2515 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2516 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002520 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 int delay_time, ret;
2523 u16 portstatus;
2524 u16 portchange;
2525
2526 for (delay_time = 0;
2527 delay_time < HUB_RESET_TIMEOUT;
2528 delay_time += delay) {
2529 /* wait to give the device a chance to reset */
2530 msleep(delay);
2531
2532 /* read and decode port status */
2533 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2534 if (ret < 0)
2535 return ret;
2536
Andiry Xu75d7cf72011-09-13 16:41:11 -07002537 /*
2538 * Some buggy devices require a warm reset to be issued even
2539 * when the port appears not to be connected.
2540 */
2541 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002542 /*
2543 * Some buggy devices can cause an NEC host controller
2544 * to transition to the "Error" state after a hot port
2545 * reset. This will show up as the port state in
2546 * "Inactive", and the port may also report a
2547 * disconnect. Forcing a warm port reset seems to make
2548 * the device work.
2549 *
2550 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2551 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002552 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002553 int ret;
2554
2555 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2556 clear_port_feature(hub->hdev, port1,
2557 USB_PORT_FEAT_C_CONNECTION);
2558 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2559 clear_port_feature(hub->hdev, port1,
2560 USB_PORT_FEAT_C_PORT_LINK_STATE);
2561 if (portchange & USB_PORT_STAT_C_RESET)
2562 clear_port_feature(hub->hdev, port1,
2563 USB_PORT_FEAT_C_RESET);
2564 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2565 port1);
2566 ret = hub_port_reset(hub, port1,
2567 udev, HUB_BH_RESET_TIME,
2568 true);
2569 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2570 clear_port_feature(hub->hdev, port1,
2571 USB_PORT_FEAT_C_CONNECTION);
2572 return ret;
2573 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002574 /* Device went away? */
2575 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2576 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Andiry Xu75d7cf72011-09-13 16:41:11 -07002578 /* bomb out completely if the connection bounced */
2579 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2580 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Andiry Xu75d7cf72011-09-13 16:41:11 -07002582 /* if we`ve finished resetting, then break out of
2583 * the loop
2584 */
2585 if (!(portstatus & USB_PORT_STAT_RESET) &&
2586 (portstatus & USB_PORT_STAT_ENABLE)) {
2587 if (hub_is_wusb(hub))
2588 udev->speed = USB_SPEED_WIRELESS;
2589 else if (hub_is_superspeed(hub->hdev))
2590 udev->speed = USB_SPEED_SUPER;
2591 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2592 udev->speed = USB_SPEED_HIGH;
2593 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2594 udev->speed = USB_SPEED_LOW;
2595 else
2596 udev->speed = USB_SPEED_FULL;
2597 return 0;
2598 }
2599 } else {
2600 if (portchange & USB_PORT_STAT_C_BH_RESET)
2601 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603
2604 /* switch to the long delay after two short delay failures */
2605 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2606 delay = HUB_LONG_RESET_TIME;
2607
2608 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002609 "port %d not %sreset yet, waiting %dms\n",
2610 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612
2613 return -EBUSY;
2614}
2615
Andiry Xu75d7cf72011-09-13 16:41:11 -07002616static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2617 struct usb_device *udev, int *status, bool warm)
2618{
2619 switch (*status) {
2620 case 0:
2621 if (!warm) {
2622 struct usb_hcd *hcd;
2623 /* TRSTRCY = 10 ms; plus some extra */
2624 msleep(10 + 40);
2625 update_devnum(udev, 0);
2626 hcd = bus_to_hcd(udev->bus);
Sarah Sharp8b8132b2012-11-14 16:10:49 -08002627 /* The xHC may think the device is already reset,
2628 * so ignore the status.
2629 */
2630 if (hcd->driver->reset_device)
2631 hcd->driver->reset_device(hcd, udev);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002632 }
2633 /* FALL THROUGH */
2634 case -ENOTCONN:
2635 case -ENODEV:
2636 clear_port_feature(hub->hdev,
2637 port1, USB_PORT_FEAT_C_RESET);
2638 /* FIXME need disconnect() for NOTATTACHED device */
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002639 if (hub_is_superspeed(hub->hdev)) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002640 clear_port_feature(hub->hdev, port1,
2641 USB_PORT_FEAT_C_BH_PORT_RESET);
2642 clear_port_feature(hub->hdev, port1,
2643 USB_PORT_FEAT_C_PORT_LINK_STATE);
Sarah Sharp1c7439c62012-11-14 15:58:52 -08002644 }
2645 if (!warm)
Andiry Xu75d7cf72011-09-13 16:41:11 -07002646 usb_set_device_state(udev, *status
2647 ? USB_STATE_NOTATTACHED
2648 : USB_STATE_DEFAULT);
Andiry Xu75d7cf72011-09-13 16:41:11 -07002649 break;
2650 }
2651}
2652
2653/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002655 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 int i, status;
2658
Andiry Xu75d7cf72011-09-13 16:41:11 -07002659 if (!warm) {
2660 /* Block EHCI CF initialization during the port reset.
2661 * Some companion controllers don't like it when they mix.
2662 */
2663 down_read(&ehci_cf_port_reset_rwsem);
2664 } else {
2665 if (!hub_is_superspeed(hub->hdev)) {
2666 dev_err(hub->intfdev, "only USB3 hub support "
2667 "warm reset\n");
2668 return -EINVAL;
2669 }
2670 }
Alan Stern32fe0192007-10-10 16:27:07 -04002671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 /* Reset the port */
2673 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002674 status = set_port_feature(hub->hdev, port1, (warm ?
2675 USB_PORT_FEAT_BH_PORT_RESET :
2676 USB_PORT_FEAT_RESET));
2677 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002679 "cannot %sreset port %d (err = %d)\n",
2680 warm ? "warm " : "", port1, status);
2681 } else {
2682 status = hub_port_wait_reset(hub, port1, udev, delay,
2683 warm);
David Brownelldd165252005-08-31 10:45:25 -07002684 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 dev_dbg(hub->intfdev,
2686 "port_wait_reset: err = %d\n",
2687 status);
2688 }
2689
2690 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002691 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2692 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002693 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
2695
2696 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002697 "port %d not enabled, trying %sreset again...\n",
2698 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 delay = HUB_LONG_RESET_TIME;
2700 }
2701
2702 dev_err (hub->intfdev,
2703 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2704 port1);
2705
Andiry Xu75d7cf72011-09-13 16:41:11 -07002706done:
2707 if (!warm)
2708 up_read(&ehci_cf_port_reset_rwsem);
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return status;
2711}
2712
Andiry Xu0ed9a572011-04-27 18:07:43 +08002713/* Check if a port is power on */
2714static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2715{
2716 int ret = 0;
2717
2718 if (hub_is_superspeed(hub->hdev)) {
2719 if (portstatus & USB_SS_PORT_STAT_POWER)
2720 ret = 1;
2721 } else {
2722 if (portstatus & USB_PORT_STAT_POWER)
2723 ret = 1;
2724 }
2725
2726 return ret;
2727}
2728
Alan Sternd388dab2006-07-01 22:14:24 -04002729#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Andiry Xu0ed9a572011-04-27 18:07:43 +08002731/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2732static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2733{
2734 int ret = 0;
2735
2736 if (hub_is_superspeed(hub->hdev)) {
2737 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2738 == USB_SS_PORT_LS_U3)
2739 ret = 1;
2740 } else {
2741 if (portstatus & USB_PORT_STAT_SUSPEND)
2742 ret = 1;
2743 }
2744
2745 return ret;
2746}
Alan Sternb01b03f2008-04-28 11:06:11 -04002747
2748/* Determine whether the device on a port is ready for a normal resume,
2749 * is ready for a reset-resume, or should be disconnected.
2750 */
2751static int check_port_resume_type(struct usb_device *udev,
2752 struct usb_hub *hub, int port1,
2753 int status, unsigned portchange, unsigned portstatus)
2754{
2755 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002756 if (status || port_is_suspended(hub, portstatus) ||
2757 !port_is_power_on(hub, portstatus) ||
2758 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002759 if (status >= 0)
2760 status = -ENODEV;
2761 }
2762
Alan Stern86c57ed2008-06-30 11:14:43 -04002763 /* Can't do a normal resume if the port isn't enabled,
2764 * so try a reset-resume instead.
2765 */
2766 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2767 if (udev->persist_enabled)
2768 udev->reset_resume = 1;
2769 else
2770 status = -ENODEV;
2771 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002772
2773 if (status) {
2774 dev_dbg(hub->intfdev,
2775 "port %d status %04x.%04x after resume, %d\n",
2776 port1, portchange, portstatus, status);
2777 } else if (udev->reset_resume) {
2778
2779 /* Late port handoff can set status-change bits */
2780 if (portchange & USB_PORT_STAT_C_CONNECTION)
2781 clear_port_feature(hub->hdev, port1,
2782 USB_PORT_FEAT_C_CONNECTION);
2783 if (portchange & USB_PORT_STAT_C_ENABLE)
2784 clear_port_feature(hub->hdev, port1,
2785 USB_PORT_FEAT_C_ENABLE);
2786 }
2787
2788 return status;
2789}
2790
Sarah Sharpf74631e2012-06-25 12:08:08 -07002791int usb_disable_ltm(struct usb_device *udev)
2792{
2793 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2794
2795 /* Check if the roothub and device supports LTM. */
2796 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2797 !usb_device_supports_ltm(udev))
2798 return 0;
2799
2800 /* Clear Feature LTM Enable can only be sent if the device is
2801 * configured.
2802 */
2803 if (!udev->actconfig)
2804 return 0;
2805
2806 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2807 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2808 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2809 USB_CTRL_SET_TIMEOUT);
2810}
2811EXPORT_SYMBOL_GPL(usb_disable_ltm);
2812
2813void usb_enable_ltm(struct usb_device *udev)
2814{
2815 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2816
2817 /* Check if the roothub and device supports LTM. */
2818 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2819 !usb_device_supports_ltm(udev))
2820 return;
2821
2822 /* Set Feature LTM Enable can only be sent if the device is
2823 * configured.
2824 */
2825 if (!udev->actconfig)
2826 return;
2827
2828 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2829 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2830 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2831 USB_CTRL_SET_TIMEOUT);
2832}
2833EXPORT_SYMBOL_GPL(usb_enable_ltm);
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835#ifdef CONFIG_USB_SUSPEND
2836
2837/*
Alan Stern140d8f62006-07-01 22:07:21 -04002838 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002839 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002840 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 *
2842 * Suspends a USB device that isn't in active use, conserving power.
2843 * Devices may wake out of a suspend, if anything important happens,
2844 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002845 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 * to disconnect devices while they are suspended.
2847 *
David Brownell390a8c32005-09-13 19:57:27 -07002848 * This only affects the USB hardware for a device; its interfaces
2849 * (and, for hubs, child devices) must already have been suspended.
2850 *
Alan Stern624d6c02007-05-30 15:35:16 -04002851 * Selective port suspend reduces power; most suspended devices draw
2852 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2853 * All devices below the suspended port are also suspended.
2854 *
2855 * Devices leave suspend state when the host wakes them up. Some devices
2856 * also support "remote wakeup", where the device can activate the USB
2857 * tree above them to deliver data, such as a keypress or packet. In
2858 * some cases, this wakes the USB host.
2859 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 * Suspending OTG devices may trigger HNP, if that's been enabled
2861 * between a pair of dual-role devices. That will change roles, such
2862 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2863 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002864 * Devices on USB hub ports have only one "suspend" state, corresponding
2865 * to ACPI D2, "may cause the device to lose some context".
2866 * State transitions include:
2867 *
2868 * - suspend, resume ... when the VBUS power link stays live
2869 * - suspend, disconnect ... VBUS lost
2870 *
2871 * Once VBUS drop breaks the circuit, the port it's using has to go through
2872 * normal re-enumeration procedures, starting with enabling VBUS power.
2873 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2874 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2875 * timer, no SRP, no requests through sysfs.
2876 *
2877 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2878 * the root hub for their bus goes into global suspend ... so we don't
2879 * (falsely) update the device power state to say it suspended.
2880 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 * Returns 0 on success, else negative errno.
2882 */
Alan Stern65bfd292008-11-25 16:39:18 -05002883int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Alan Stern624d6c02007-05-30 15:35:16 -04002885 struct usb_hub *hub = hdev_to_hub(udev->parent);
2886 int port1 = udev->portnum;
2887 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002888
Alan Stern624d6c02007-05-30 15:35:16 -04002889 /* enable remote wakeup when appropriate; this lets the device
2890 * wake up the upstream hub (including maybe the root hub).
2891 *
2892 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2893 * we don't explicitly enable it here.
2894 */
2895 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002896 if (!hub_is_superspeed(hub->hdev)) {
2897 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2898 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2899 USB_DEVICE_REMOTE_WAKEUP, 0,
2900 NULL, 0,
2901 USB_CTRL_SET_TIMEOUT);
2902 } else {
2903 /* Assume there's only one function on the USB 3.0
2904 * device and enable remote wake for the first
2905 * interface. FIXME if the interface association
2906 * descriptor shows there's more than one function.
2907 */
2908 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2909 USB_REQ_SET_FEATURE,
2910 USB_RECIP_INTERFACE,
2911 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002912 USB_INTRF_FUNC_SUSPEND_RW |
2913 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002914 NULL, 0,
2915 USB_CTRL_SET_TIMEOUT);
2916 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002917 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002918 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2919 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002920 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002921 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002922 return status;
2923 }
Alan Stern624d6c02007-05-30 15:35:16 -04002924 }
2925
Andiry Xu65580b432011-09-23 14:19:52 -07002926 /* disable USB2 hardware LPM */
2927 if (udev->usb2_hw_lpm_enabled == 1)
2928 usb_set_usb2_hardware_lpm(udev, 0);
2929
Sarah Sharpf74631e2012-06-25 12:08:08 -07002930 if (usb_disable_ltm(udev)) {
2931 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2932 __func__);
2933 return -ENOMEM;
2934 }
Sarah Sharp83060952012-05-02 14:25:52 -07002935 if (usb_unlocked_disable_lpm(udev)) {
2936 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2937 __func__);
2938 return -ENOMEM;
2939 }
2940
Alan Stern624d6c02007-05-30 15:35:16 -04002941 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002942 if (hub_is_superspeed(hub->hdev))
2943 status = set_port_feature(hub->hdev,
2944 port1 | (USB_SS_PORT_LS_U3 << 3),
2945 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002946 else
2947 status = set_port_feature(hub->hdev, port1,
2948 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002949 if (status) {
2950 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2951 port1, status);
2952 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002953 if (udev->do_remote_wakeup)
2954 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002955 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2956 USB_DEVICE_REMOTE_WAKEUP, 0,
2957 NULL, 0,
2958 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002959
Andiry Xuc3e751e2012-05-05 00:50:10 +08002960 /* Try to enable USB2 hardware LPM again */
2961 if (udev->usb2_hw_lpm_capable == 1)
2962 usb_set_usb2_hardware_lpm(udev, 1);
2963
Sarah Sharpf74631e2012-06-25 12:08:08 -07002964 /* Try to enable USB3 LTM and LPM again */
2965 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002966 usb_unlocked_enable_lpm(udev);
2967
Alan Sterncbb33002011-06-15 16:29:16 -04002968 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002969 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002970 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002971 } else {
2972 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002973 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2974 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2975 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002976 usb_set_device_state(udev, USB_STATE_SUSPENDED);
Alan Sternbfd1e912012-10-19 11:03:39 -04002977 udev->port_is_suspended = 1;
Alan Stern624d6c02007-05-30 15:35:16 -04002978 msleep(10);
2979 }
Alan Sternc08512c2010-11-15 15:57:58 -05002980 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002981 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
David Brownellf3f32532005-09-22 22:37:29 -07002983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984/*
David Brownell390a8c32005-09-13 19:57:27 -07002985 * If the USB "suspend" state is in use (rather than "global suspend"),
2986 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002987 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 * hardware resume signaling is finished, either because of selective
2989 * resume (by host) or remote wakeup (by device) ... now see what changed
2990 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002991 *
2992 * If @udev->reset_resume is set then the device is reset before the
2993 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 */
Alan Stern140d8f62006-07-01 22:07:21 -04002995static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Alan Stern54515fe2007-05-30 15:38:58 -04002997 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 u16 devstatus;
2999
3000 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003001 dev_dbg(&udev->dev, "%s\n",
3002 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
3004 /* usb ch9 identifies four variants of SUSPENDED, based on what
3005 * state the device resumes to. Linux currently won't see the
3006 * first two on the host side; they'd be inside hub_port_init()
3007 * during many timeouts, but khubd can't suspend until later.
3008 */
3009 usb_set_device_state(udev, udev->actconfig
3010 ? USB_STATE_CONFIGURED
3011 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Alan Stern54515fe2007-05-30 15:38:58 -04003013 /* 10.5.4.5 says not to reset a suspended port if the attached
3014 * device is enabled for remote wakeup. Hence the reset
3015 * operation is carried out here, after the port has been
3016 * resumed.
3017 */
3018 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04003019 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08003020 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 /* 10.5.4.5 says be sure devices in the tree are still there.
3023 * For now let's assume the device didn't go crazy on resume,
3024 * and device drivers will know about any resume quirks.
3025 */
Alan Stern54515fe2007-05-30 15:38:58 -04003026 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04003027 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04003028 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
3029 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04003030 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04003031
3032 /* If a normal resume failed, try doing a reset-resume */
3033 if (status && !udev->reset_resume && udev->persist_enabled) {
3034 dev_dbg(&udev->dev, "retry with reset-resume\n");
3035 udev->reset_resume = 1;
3036 goto retry_reset_resume;
3037 }
Alan Stern54515fe2007-05-30 15:38:58 -04003038 }
Alan Sternb40b7a92006-06-19 15:12:38 -04003039
Alan Stern624d6c02007-05-30 15:35:16 -04003040 if (status) {
3041 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
3042 status);
3043 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04003045 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 status = usb_control_msg(udev,
3047 usb_sndctrlpipe(udev, 0),
3048 USB_REQ_CLEAR_FEATURE,
3049 USB_RECIP_DEVICE,
3050 USB_DEVICE_REMOTE_WAKEUP, 0,
3051 NULL, 0,
3052 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04003053 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003054 dev_dbg(&udev->dev,
3055 "disable remote wakeup, status %d\n",
3056 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05003057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060 return status;
3061}
3062
Alan Stern624d6c02007-05-30 15:35:16 -04003063/*
3064 * usb_port_resume - re-activate a suspended usb device's upstream port
3065 * @udev: device to re-activate, not a root hub
3066 * Context: must be able to sleep; device not locked; pm locks held
3067 *
3068 * This will re-activate the suspended device, increasing power usage
3069 * while letting drivers communicate again with its endpoints.
3070 * USB resume explicitly guarantees that the power session between
3071 * the host and the device is the same as it was when the device
3072 * suspended.
3073 *
Alan Sternfeccc302008-03-03 15:15:59 -05003074 * If @udev->reset_resume is set then this routine won't check that the
3075 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04003076 * reset @udev. The end result is that a broken power session can be
3077 * recovered and @udev will appear to persist across a loss of VBUS power.
3078 *
3079 * For example, if a host controller doesn't maintain VBUS suspend current
3080 * during a system sleep or is reset when the system wakes up, all the USB
3081 * power sessions below it will be broken. This is especially troublesome
3082 * for mass-storage devices containing mounted filesystems, since the
3083 * device will appear to have disconnected and all the memory mappings
3084 * to it will be lost. Using the USB_PERSIST facility, the device can be
3085 * made to appear as if it had not disconnected.
3086 *
Ming Lei742120c2008-06-18 22:00:29 +08003087 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05003088 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04003089 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
3090 * quite possible for a device to remain unaltered but its media to be
3091 * changed. If the user replaces a flash memory card while the system is
3092 * asleep, he will have only himself to blame when the filesystem on the
3093 * new card is corrupted and the system crashes.
3094 *
Alan Stern624d6c02007-05-30 15:35:16 -04003095 * Returns 0 on success, else negative errno.
3096 */
Alan Stern65bfd292008-11-25 16:39:18 -05003097int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Alan Stern624d6c02007-05-30 15:35:16 -04003099 struct usb_hub *hub = hdev_to_hub(udev->parent);
3100 int port1 = udev->portnum;
3101 int status;
3102 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05003103
3104 /* Skip the initial Clear-Suspend step for a remote wakeup */
3105 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08003106 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05003107 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
3109 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
3110
Alan Sternd5cbad42006-08-11 16:52:39 -04003111 set_bit(port1, hub->busy_bits);
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08003114 if (hub_is_superspeed(hub->hdev))
3115 status = set_port_feature(hub->hdev,
3116 port1 | (USB_SS_PORT_LS_U0 << 3),
3117 USB_PORT_FEAT_LINK_STATE);
3118 else
3119 status = clear_port_feature(hub->hdev,
3120 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04003122 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
3123 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04003126 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02003127 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 msleep(25);
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /* Virtual root hubs can trigger on GET_PORT_STATUS to
3131 * stop resume signaling. Then finish the resume
3132 * sequence.
3133 */
Alan Sternd25450c2006-11-20 11:14:30 -05003134 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04003135
Alan Sternb01b03f2008-04-28 11:06:11 -04003136 /* TRSMRCY = 10 msec */
3137 msleep(10);
3138 }
Alan Stern54515fe2007-05-30 15:38:58 -04003139
Alan Sternb01b03f2008-04-28 11:06:11 -04003140 SuspendCleared:
3141 if (status == 0) {
Alan Sternbfd1e912012-10-19 11:03:39 -04003142 udev->port_is_suspended = 0;
Andiry Xua7114232011-04-27 18:07:50 +08003143 if (hub_is_superspeed(hub->hdev)) {
3144 if (portchange & USB_PORT_STAT_C_LINK_STATE)
3145 clear_port_feature(hub->hdev, port1,
3146 USB_PORT_FEAT_C_PORT_LINK_STATE);
3147 } else {
3148 if (portchange & USB_PORT_STAT_C_SUSPEND)
3149 clear_port_feature(hub->hdev, port1,
3150 USB_PORT_FEAT_C_SUSPEND);
3151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Alan Sternd5cbad42006-08-11 16:52:39 -04003154 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04003155
Alan Sternb01b03f2008-04-28 11:06:11 -04003156 status = check_port_resume_type(udev,
3157 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04003158 if (status == 0)
3159 status = finish_port_resume(udev);
3160 if (status < 0) {
3161 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3162 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003163 } else {
3164 /* Try to enable USB2 hardware LPM */
3165 if (udev->usb2_hw_lpm_capable == 1)
3166 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003167
Sarah Sharpf74631e2012-06-25 12:08:08 -07003168 /* Try to enable USB3 LTM and LPM */
3169 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003170 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003171 }
Andiry Xu65580b432011-09-23 14:19:52 -07003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return status;
3174}
3175
Alan Stern8808f002008-04-28 11:06:55 -04003176/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003177int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178{
3179 int status = 0;
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003182 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003183 status = usb_autoresume_device(udev);
3184 if (status == 0) {
3185 /* Let the drivers do their thing, then... */
3186 usb_autosuspend_device(udev);
3187 }
Alan Sternd25450c2006-11-20 11:14:30 -05003188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return status;
3190}
3191
Alan Sternd388dab2006-07-01 22:14:24 -04003192#else /* CONFIG_USB_SUSPEND */
3193
3194/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3195
Alan Stern65bfd292008-11-25 16:39:18 -05003196int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003197{
3198 return 0;
3199}
3200
Alan Sternb01b03f2008-04-28 11:06:11 -04003201/* However we may need to do a reset-resume */
3202
Alan Stern65bfd292008-11-25 16:39:18 -05003203int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003204{
Alan Sternb01b03f2008-04-28 11:06:11 -04003205 struct usb_hub *hub = hdev_to_hub(udev->parent);
3206 int port1 = udev->portnum;
3207 int status;
3208 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003209
Alan Sternb01b03f2008-04-28 11:06:11 -04003210 status = hub_port_status(hub, port1, &portstatus, &portchange);
3211 status = check_port_resume_type(udev,
3212 hub, port1, status, portchange, portstatus);
3213
3214 if (status) {
3215 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3216 hub_port_logical_disconnect(hub, port1);
3217 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003218 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003219 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003220 }
3221 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003222}
3223
Alan Sternd388dab2006-07-01 22:14:24 -04003224#endif
3225
Ming Leie6f30de2012-10-24 11:59:24 +08003226static int check_ports_changed(struct usb_hub *hub)
3227{
3228 int port1;
3229
3230 for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) {
3231 u16 portstatus, portchange;
3232 int status;
3233
3234 status = hub_port_status(hub, port1, &portstatus, &portchange);
3235 if (!status && portchange)
3236 return 1;
3237 }
3238 return 0;
3239}
3240
David Brownelldb690872005-09-13 19:56:33 -07003241static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
3243 struct usb_hub *hub = usb_get_intfdata (intf);
3244 struct usb_device *hdev = hub->hdev;
3245 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003246 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247
Alan Sterncbb33002011-06-15 16:29:16 -04003248 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3250 struct usb_device *udev;
3251
Lan Tianyuff823c72012-09-05 13:44:32 +08003252 udev = hub->ports[port1 - 1]->child;
Alan Stern6840d252007-09-10 11:34:26 -04003253 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003254 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003255 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003256 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 }
Ming Leie6f30de2012-10-24 11:59:24 +08003259
3260 if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) {
3261 /* check if there are changes pending on hub ports */
3262 if (check_ports_changed(hub)) {
3263 if (PMSG_IS_AUTO(msg))
3264 return -EBUSY;
3265 pm_wakeup_event(&hdev->dev, 2000);
3266 }
3267 }
3268
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003269 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3270 /* Enable hub to send remote wakeup for all ports. */
3271 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3272 status = set_port_feature(hdev,
3273 port1 |
3274 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3275 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3276 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3277 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3278 }
3279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280
Harvey Harrison441b62c2008-03-03 16:08:34 -08003281 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003282
David Brownellc9f89fa2005-09-13 19:57:04 -07003283 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003284 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
3288static int hub_resume(struct usb_interface *intf)
3289{
Alan Stern5e6effa2008-03-03 15:15:51 -05003290 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Alan Stern5e6effa2008-03-03 15:15:51 -05003292 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003293 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 return 0;
3295}
3296
Alan Sternb41a60e2007-05-30 15:39:33 -04003297static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003298{
Alan Sternb41a60e2007-05-30 15:39:33 -04003299 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003300
Alan Stern5e6effa2008-03-03 15:15:51 -05003301 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003302 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003303 return 0;
3304}
3305
Alan Stern54515fe2007-05-30 15:38:58 -04003306/**
3307 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3308 * @rhdev: struct usb_device for the root hub
3309 *
3310 * The USB host controller driver calls this function when its root hub
3311 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003312 * has been reset. The routine marks @rhdev as having lost power.
3313 * When the hub driver is resumed it will take notice and carry out
3314 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3315 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003316 */
3317void usb_root_hub_lost_power(struct usb_device *rhdev)
3318{
3319 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3320 rhdev->reset_resume = 1;
3321}
3322EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3323
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003324static const char * const usb3_lpm_names[] = {
3325 "U0",
3326 "U1",
3327 "U2",
3328 "U3",
3329};
3330
3331/*
3332 * Send a Set SEL control transfer to the device, prior to enabling
3333 * device-initiated U1 or U2. This lets the device know the exit latencies from
3334 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3335 * packet from the host.
3336 *
3337 * This function will fail if the SEL or PEL values for udev are greater than
3338 * the maximum allowed values for the link state to be enabled.
3339 */
3340static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3341{
3342 struct usb_set_sel_req *sel_values;
3343 unsigned long long u1_sel;
3344 unsigned long long u1_pel;
3345 unsigned long long u2_sel;
3346 unsigned long long u2_pel;
3347 int ret;
3348
3349 /* Convert SEL and PEL stored in ns to us */
3350 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3351 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3352 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3353 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3354
3355 /*
3356 * Make sure that the calculated SEL and PEL values for the link
3357 * state we're enabling aren't bigger than the max SEL/PEL
3358 * value that will fit in the SET SEL control transfer.
3359 * Otherwise the device would get an incorrect idea of the exit
3360 * latency for the link state, and could start a device-initiated
3361 * U1/U2 when the exit latencies are too high.
3362 */
3363 if ((state == USB3_LPM_U1 &&
3364 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3365 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3366 (state == USB3_LPM_U2 &&
3367 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3368 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
Sarah Sharp1510a1a2012-10-05 10:30:35 -07003369 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 -07003370 usb3_lpm_names[state], u1_sel, u1_pel);
3371 return -EINVAL;
3372 }
3373
3374 /*
3375 * If we're enabling device-initiated LPM for one link state,
3376 * but the other link state has a too high SEL or PEL value,
3377 * just set those values to the max in the Set SEL request.
3378 */
3379 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3380 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3381
3382 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3383 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3384
3385 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3386 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3387
3388 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3389 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3390
3391 /*
3392 * usb_enable_lpm() can be called as part of a failed device reset,
3393 * which may be initiated by an error path of a mass storage driver.
3394 * Therefore, use GFP_NOIO.
3395 */
3396 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3397 if (!sel_values)
3398 return -ENOMEM;
3399
3400 sel_values->u1_sel = u1_sel;
3401 sel_values->u1_pel = u1_pel;
3402 sel_values->u2_sel = cpu_to_le16(u2_sel);
3403 sel_values->u2_pel = cpu_to_le16(u2_pel);
3404
3405 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3406 USB_REQ_SET_SEL,
3407 USB_RECIP_DEVICE,
3408 0, 0,
3409 sel_values, sizeof *(sel_values),
3410 USB_CTRL_SET_TIMEOUT);
3411 kfree(sel_values);
3412 return ret;
3413}
3414
3415/*
3416 * Enable or disable device-initiated U1 or U2 transitions.
3417 */
3418static int usb_set_device_initiated_lpm(struct usb_device *udev,
3419 enum usb3_link_state state, bool enable)
3420{
3421 int ret;
3422 int feature;
3423
3424 switch (state) {
3425 case USB3_LPM_U1:
3426 feature = USB_DEVICE_U1_ENABLE;
3427 break;
3428 case USB3_LPM_U2:
3429 feature = USB_DEVICE_U2_ENABLE;
3430 break;
3431 default:
3432 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3433 __func__, enable ? "enable" : "disable");
3434 return -EINVAL;
3435 }
3436
3437 if (udev->state != USB_STATE_CONFIGURED) {
3438 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3439 "for unconfigured device.\n",
3440 __func__, enable ? "enable" : "disable",
3441 usb3_lpm_names[state]);
3442 return 0;
3443 }
3444
3445 if (enable) {
3446 /*
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003447 * Now send the control transfer to enable device-initiated LPM
3448 * for either U1 or U2.
3449 */
3450 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3451 USB_REQ_SET_FEATURE,
3452 USB_RECIP_DEVICE,
3453 feature,
3454 0, NULL, 0,
3455 USB_CTRL_SET_TIMEOUT);
3456 } else {
3457 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3458 USB_REQ_CLEAR_FEATURE,
3459 USB_RECIP_DEVICE,
3460 feature,
3461 0, NULL, 0,
3462 USB_CTRL_SET_TIMEOUT);
3463 }
3464 if (ret < 0) {
3465 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3466 enable ? "Enable" : "Disable",
3467 usb3_lpm_names[state]);
3468 return -EBUSY;
3469 }
3470 return 0;
3471}
3472
3473static int usb_set_lpm_timeout(struct usb_device *udev,
3474 enum usb3_link_state state, int timeout)
3475{
3476 int ret;
3477 int feature;
3478
3479 switch (state) {
3480 case USB3_LPM_U1:
3481 feature = USB_PORT_FEAT_U1_TIMEOUT;
3482 break;
3483 case USB3_LPM_U2:
3484 feature = USB_PORT_FEAT_U2_TIMEOUT;
3485 break;
3486 default:
3487 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3488 __func__);
3489 return -EINVAL;
3490 }
3491
3492 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3493 timeout != USB3_LPM_DEVICE_INITIATED) {
3494 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3495 "which is a reserved value.\n",
3496 usb3_lpm_names[state], timeout);
3497 return -EINVAL;
3498 }
3499
3500 ret = set_port_feature(udev->parent,
3501 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3502 feature);
3503 if (ret < 0) {
3504 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3505 "error code %i\n", usb3_lpm_names[state],
3506 timeout, ret);
3507 return -EBUSY;
3508 }
3509 if (state == USB3_LPM_U1)
3510 udev->u1_params.timeout = timeout;
3511 else
3512 udev->u2_params.timeout = timeout;
3513 return 0;
3514}
3515
3516/*
3517 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3518 * U1/U2 entry.
3519 *
3520 * We will attempt to enable U1 or U2, but there are no guarantees that the
3521 * control transfers to set the hub timeout or enable device-initiated U1/U2
3522 * will be successful.
3523 *
3524 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3525 * driver know about it. If that call fails, it should be harmless, and just
3526 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3527 */
3528static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3529 enum usb3_link_state state)
3530{
Sarah Sharp65a95b72012-10-05 10:32:07 -07003531 int timeout, ret;
Sarah Sharpae8963a2012-10-03 11:18:05 -07003532 __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat;
3533 __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat;
3534
3535 /* If the device says it doesn't have *any* exit latency to come out of
3536 * U1 or U2, it's probably lying. Assume it doesn't implement that link
3537 * state.
3538 */
3539 if ((state == USB3_LPM_U1 && u1_mel == 0) ||
3540 (state == USB3_LPM_U2 && u2_mel == 0))
3541 return;
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003542
Sarah Sharp65a95b72012-10-05 10:32:07 -07003543 /*
3544 * First, let the device know about the exit latencies
3545 * associated with the link state we're about to enable.
3546 */
3547 ret = usb_req_set_sel(udev, state);
3548 if (ret < 0) {
3549 dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n",
3550 usb3_lpm_names[state]);
3551 return;
3552 }
3553
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003554 /* We allow the host controller to set the U1/U2 timeout internally
3555 * first, so that it can change its schedule to account for the
3556 * additional latency to send data to a device in a lower power
3557 * link state.
3558 */
3559 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3560
3561 /* xHCI host controller doesn't want to enable this LPM state. */
3562 if (timeout == 0)
3563 return;
3564
3565 if (timeout < 0) {
3566 dev_warn(&udev->dev, "Could not enable %s link state, "
3567 "xHCI error %i.\n", usb3_lpm_names[state],
3568 timeout);
3569 return;
3570 }
3571
3572 if (usb_set_lpm_timeout(udev, state, timeout))
3573 /* If we can't set the parent hub U1/U2 timeout,
3574 * device-initiated LPM won't be allowed either, so let the xHCI
3575 * host know that this link state won't be enabled.
3576 */
3577 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3578
3579 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3580 else if (udev->actconfig)
3581 usb_set_device_initiated_lpm(udev, state, true);
3582
3583}
3584
3585/*
3586 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3587 * U1/U2 entry.
3588 *
3589 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3590 * If zero is returned, the parent will not allow the link to go into U1/U2.
3591 *
3592 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3593 * it won't have an effect on the bus link state because the parent hub will
3594 * still disallow device-initiated U1/U2 entry.
3595 *
3596 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3597 * possible. The result will be slightly more bus bandwidth will be taken up
3598 * (to account for U1/U2 exit latency), but it should be harmless.
3599 */
3600static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3601 enum usb3_link_state state)
3602{
3603 int feature;
3604
3605 switch (state) {
3606 case USB3_LPM_U1:
3607 feature = USB_PORT_FEAT_U1_TIMEOUT;
3608 break;
3609 case USB3_LPM_U2:
3610 feature = USB_PORT_FEAT_U2_TIMEOUT;
3611 break;
3612 default:
3613 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3614 __func__);
3615 return -EINVAL;
3616 }
3617
3618 if (usb_set_lpm_timeout(udev, state, 0))
3619 return -EBUSY;
3620
3621 usb_set_device_initiated_lpm(udev, state, false);
3622
3623 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3624 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3625 "bus schedule bandwidth may be impacted.\n",
3626 usb3_lpm_names[state]);
3627 return 0;
3628}
3629
3630/*
3631 * Disable hub-initiated and device-initiated U1 and U2 entry.
3632 * Caller must own the bandwidth_mutex.
3633 *
3634 * This will call usb_enable_lpm() on failure, which will decrement
3635 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3636 */
3637int usb_disable_lpm(struct usb_device *udev)
3638{
3639 struct usb_hcd *hcd;
3640
3641 if (!udev || !udev->parent ||
3642 udev->speed != USB_SPEED_SUPER ||
3643 !udev->lpm_capable)
3644 return 0;
3645
3646 hcd = bus_to_hcd(udev->bus);
3647 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3648 return 0;
3649
3650 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003651 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003652 return 0;
3653
3654 /* If LPM is enabled, attempt to disable it. */
3655 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3656 goto enable_lpm;
3657 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3658 goto enable_lpm;
3659
3660 return 0;
3661
3662enable_lpm:
3663 usb_enable_lpm(udev);
3664 return -EBUSY;
3665}
3666EXPORT_SYMBOL_GPL(usb_disable_lpm);
3667
3668/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3669int usb_unlocked_disable_lpm(struct usb_device *udev)
3670{
3671 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3672 int ret;
3673
3674 if (!hcd)
3675 return -EINVAL;
3676
3677 mutex_lock(hcd->bandwidth_mutex);
3678 ret = usb_disable_lpm(udev);
3679 mutex_unlock(hcd->bandwidth_mutex);
3680
3681 return ret;
3682}
3683EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3684
3685/*
3686 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3687 * xHCI host policy may prevent U1 or U2 from being enabled.
3688 *
3689 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3690 * until the lpm_disable_count drops to zero. Caller must own the
3691 * bandwidth_mutex.
3692 */
3693void usb_enable_lpm(struct usb_device *udev)
3694{
3695 struct usb_hcd *hcd;
3696
3697 if (!udev || !udev->parent ||
3698 udev->speed != USB_SPEED_SUPER ||
3699 !udev->lpm_capable)
3700 return;
3701
3702 udev->lpm_disable_count--;
3703 hcd = bus_to_hcd(udev->bus);
3704 /* Double check that we can both enable and disable LPM.
3705 * Device must be configured to accept set feature U1/U2 timeout.
3706 */
3707 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3708 !hcd->driver->disable_usb3_lpm_timeout)
3709 return;
3710
3711 if (udev->lpm_disable_count > 0)
3712 return;
3713
3714 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3715 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3716}
3717EXPORT_SYMBOL_GPL(usb_enable_lpm);
3718
3719/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3720void usb_unlocked_enable_lpm(struct usb_device *udev)
3721{
3722 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3723
3724 if (!hcd)
3725 return;
3726
3727 mutex_lock(hcd->bandwidth_mutex);
3728 usb_enable_lpm(udev);
3729 mutex_unlock(hcd->bandwidth_mutex);
3730}
3731EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3732
3733
Alan Sternd388dab2006-07-01 22:14:24 -04003734#else /* CONFIG_PM */
3735
Alan Sternf07600c2007-05-30 15:38:16 -04003736#define hub_suspend NULL
3737#define hub_resume NULL
3738#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003739
3740int usb_disable_lpm(struct usb_device *udev)
3741{
3742 return 0;
3743}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003744EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003745
3746void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003747EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003748
3749int usb_unlocked_disable_lpm(struct usb_device *udev)
3750{
3751 return 0;
3752}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003753EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003754
3755void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003756EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003757
3758int usb_disable_ltm(struct usb_device *udev)
3759{
3760 return 0;
3761}
3762EXPORT_SYMBOL_GPL(usb_disable_ltm);
3763
3764void usb_enable_ltm(struct usb_device *udev) { }
3765EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003766#endif
3767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3770 *
3771 * Between connect detection and reset signaling there must be a delay
3772 * of 100ms at least for debounce and power-settling. The corresponding
3773 * timer shall restart whenever the downstream port detects a disconnect.
3774 *
3775 * Apparently there are some bluetooth and irda-dongles and a number of
3776 * low-speed devices for which this debounce period may last over a second.
3777 * Not covered by the spec - but easy to deal with.
3778 *
3779 * This implementation uses a 1500ms total debounce timeout; if the
3780 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3781 * every 25ms for transient disconnects. When the port status has been
3782 * unchanged for 100ms it returns the port status.
3783 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784static int hub_port_debounce(struct usb_hub *hub, int port1)
3785{
3786 int ret;
3787 int total_time, stable_time = 0;
3788 u16 portchange, portstatus;
3789 unsigned connection = 0xffff;
3790
3791 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3792 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3793 if (ret < 0)
3794 return ret;
3795
3796 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3797 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3798 stable_time += HUB_DEBOUNCE_STEP;
3799 if (stable_time >= HUB_DEBOUNCE_STABLE)
3800 break;
3801 } else {
3802 stable_time = 0;
3803 connection = portstatus & USB_PORT_STAT_CONNECTION;
3804 }
3805
3806 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3807 clear_port_feature(hub->hdev, port1,
3808 USB_PORT_FEAT_C_CONNECTION);
3809 }
3810
3811 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3812 break;
3813 msleep(HUB_DEBOUNCE_STEP);
3814 }
3815
3816 dev_dbg (hub->intfdev,
3817 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3818 port1, total_time, stable_time, portstatus);
3819
3820 if (stable_time < HUB_DEBOUNCE_STABLE)
3821 return -ETIMEDOUT;
3822 return portstatus;
3823}
3824
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003825void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
Alan Sternddeac4e2009-01-15 17:03:33 -05003827 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3828 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003829 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003831EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3834#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3835
Alan Stern4326ed02007-07-30 17:08:43 -04003836static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
3838 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003839 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Sarah Sharpc6515272009-04-27 19:57:26 -07003841 /*
3842 * The host controller will choose the device address,
3843 * instead of the core having chosen it earlier
3844 */
3845 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 return -EINVAL;
3847 if (udev->state == USB_STATE_ADDRESS)
3848 return 0;
3849 if (udev->state != USB_STATE_DEFAULT)
3850 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003851 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003852 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003853 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003854 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3855 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3856 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003858 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003859 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003861 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 }
3863 return retval;
3864}
3865
3866/* Reset device, (re)assign address, get device descriptor.
3867 * Device connection must be stable, no more debouncing needed.
3868 * Returns device in USB_STATE_ADDRESS, except on error.
3869 *
3870 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003871 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 * newly detected device that is not accessible through any global
3873 * pointers, it's not necessary to lock the device.
3874 */
3875static int
3876hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3877 int retry_counter)
3878{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003879 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
3881 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003882 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 int i, j, retval;
3884 unsigned delay = HUB_SHORT_RESET_TIME;
3885 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003886 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003887 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
3889 /* root hub ports have a slightly longer reset period
3890 * (from USB 2.0 spec, section 7.1.7.5)
3891 */
3892 if (!hdev->parent) {
3893 delay = HUB_ROOT_RESET_TIME;
3894 if (port1 == hdev->bus->otg_port)
3895 hdev->bus->b_hnp_enable = 0;
3896 }
3897
3898 /* Some low speed devices have problems with the quick delay, so */
3899 /* be a bit pessimistic with those devices. RHbug #23670 */
3900 if (oldspeed == USB_SPEED_LOW)
3901 delay = HUB_LONG_RESET_TIME;
3902
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003903 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
Luben Tuikov07194ab2011-02-11 11:33:10 -08003905 /* Reset the device; full speed may morph to high speed */
3906 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003907 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003908 if (retval < 0) /* error or disconnect */
3909 goto fail;
3910 /* success, speed is known */
3911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 retval = -ENODEV;
3913
3914 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3915 dev_dbg(&udev->dev, "device reset changed speed!\n");
3916 goto fail;
3917 }
3918 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3921 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003922 * For Wireless USB devices, ep0 max packet is always 512 (tho
3923 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 */
3925 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003926 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003927 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003928 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003929 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003931 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 break;
3933 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3934 /* to determine the ep0 maxpacket size, try to read
3935 * the device descriptor to get bMaxPacketSize0 and
3936 * then correct our initial guess.
3937 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003938 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 break;
3940 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003941 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 break;
3943 default:
3944 goto fail;
3945 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003946
3947 if (udev->speed == USB_SPEED_WIRELESS)
3948 speed = "variable speed Wireless";
3949 else
3950 speed = usb_speed_string(udev->speed);
3951
Sarah Sharpc6515272009-04-27 19:57:26 -07003952 if (udev->speed != USB_SPEED_SUPER)
3953 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003954 "%s %s USB device number %d using %s\n",
3955 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003956 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 /* Set up TT records, if needed */
3959 if (hdev->tt) {
3960 udev->tt = hdev->tt;
3961 udev->ttport = hdev->ttport;
3962 } else if (udev->speed != USB_SPEED_HIGH
3963 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003964 if (!hub->tt.hub) {
3965 dev_err(&udev->dev, "parent hub has no TT\n");
3966 retval = -EINVAL;
3967 goto fail;
3968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 udev->tt = &hub->tt;
3970 udev->ttport = port1;
3971 }
3972
3973 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3974 * Because device hardware and firmware is sometimes buggy in
3975 * this area, and this is how Linux has done it for ages.
3976 * Change it cautiously.
3977 *
3978 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3979 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3980 * so it may help with some non-standards-compliant devices.
3981 * Otherwise we start with SET_ADDRESS and then try to read the
3982 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3983 * value.
3984 */
3985 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003986 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 struct usb_device_descriptor *buf;
3988 int r = 0;
3989
3990#define GET_DESCRIPTOR_BUFSIZE 64
3991 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3992 if (!buf) {
3993 retval = -ENOMEM;
3994 continue;
3995 }
3996
Alan Sternb89ee192007-05-11 10:19:04 -04003997 /* Retry on all errors; some devices are flakey.
3998 * 255 is for WUSB devices, we actually need to use
3999 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 */
4001 for (j = 0; j < 3; ++j) {
4002 buf->bMaxPacketSize0 = 0;
4003 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
4004 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
4005 USB_DT_DEVICE << 8, 0,
4006 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02004007 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07004009 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (buf->bDescriptorType ==
4011 USB_DT_DEVICE) {
4012 r = 0;
4013 break;
4014 }
4015 /* FALL THROUGH */
4016 default:
4017 if (r == 0)
4018 r = -EPROTO;
4019 break;
4020 }
4021 if (r == 0)
4022 break;
4023 }
4024 udev->descriptor.bMaxPacketSize0 =
4025 buf->bMaxPacketSize0;
4026 kfree(buf);
4027
Andiry Xu75d7cf72011-09-13 16:41:11 -07004028 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 if (retval < 0) /* error or disconnect */
4030 goto fail;
4031 if (oldspeed != udev->speed) {
4032 dev_dbg(&udev->dev,
4033 "device reset changed speed!\n");
4034 retval = -ENODEV;
4035 goto fail;
4036 }
4037 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004038 dev_err(&udev->dev,
4039 "device descriptor read/64, error %d\n",
4040 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 retval = -EMSGSIZE;
4042 continue;
4043 }
4044#undef GET_DESCRIPTOR_BUFSIZE
4045 }
4046
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004047 /*
4048 * If device is WUSB, we already assigned an
4049 * unauthorized address in the Connect Ack sequence;
4050 * authorization will assign the final address.
4051 */
Sarah Sharpc6515272009-04-27 19:57:26 -07004052 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004053 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
4054 retval = hub_set_address(udev, devnum);
4055 if (retval >= 0)
4056 break;
4057 msleep(200);
4058 }
4059 if (retval < 0) {
4060 dev_err(&udev->dev,
4061 "device not accepting address %d, error %d\n",
4062 devnum, retval);
4063 goto fail;
4064 }
Sarah Sharpc6515272009-04-27 19:57:26 -07004065 if (udev->speed == USB_SPEED_SUPER) {
4066 devnum = udev->devnum;
4067 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05004068 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07004069 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05004070 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07004071 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004072
4073 /* cope with hardware quirkiness:
4074 * - let SET_ADDRESS settle, some device hardware wants it
4075 * - read ep0 maxpacket even for high and low speed,
4076 */
4077 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07004078 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07004080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
4082 retval = usb_get_device_descriptor(udev, 8);
4083 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004084 dev_err(&udev->dev,
4085 "device descriptor read/8, error %d\n",
4086 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (retval >= 0)
4088 retval = -EMSGSIZE;
4089 } else {
4090 retval = 0;
4091 break;
4092 }
4093 }
4094 if (retval)
4095 goto fail;
4096
Peter Chenb76baa82012-11-09 09:44:43 +08004097 if (hcd->phy && !hdev->parent)
Peter Chenac965112012-11-09 09:44:44 +08004098 usb_phy_notify_connect(hcd->phy, udev->speed);
Peter Chenb76baa82012-11-09 09:44:43 +08004099
Elric Fud8aec3d2012-03-26 21:16:02 +08004100 /*
4101 * Some superspeed devices have finished the link training process
4102 * and attached to a superspeed hub port, but the device descriptor
4103 * got from those devices show they aren't superspeed devices. Warm
4104 * reset the port attached by the devices can fix them.
4105 */
4106 if ((udev->speed == USB_SPEED_SUPER) &&
4107 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
4108 dev_err(&udev->dev, "got a wrong device descriptor, "
4109 "warm reset device\n");
4110 hub_port_reset(hub, port1, udev,
4111 HUB_BH_RESET_TIME, true);
4112 retval = -EINVAL;
4113 goto fail;
4114 }
4115
Sarah Sharp6b403b02009-04-27 19:54:10 -07004116 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
4117 udev->speed == USB_SPEED_SUPER)
4118 i = 512;
4119 else
4120 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07004121 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04004122 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04004124 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 retval = -EMSGSIZE;
4126 goto fail;
4127 }
Alan Stern56626a72010-10-14 15:25:21 -04004128 if (udev->speed == USB_SPEED_FULL)
4129 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
4130 else
4131 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004133 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
4135
4136 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
4137 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004138 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
4139 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 if (retval >= 0)
4141 retval = -ENOMSG;
4142 goto fail;
4143 }
4144
Andiry Xu1ff4df52011-09-23 14:19:48 -07004145 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
4146 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004147 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08004148 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08004149 usb_set_lpm_parameters(udev);
4150 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07004151 }
Andiry Xu3148bf02011-09-23 14:19:47 -07004152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08004154 /* notify HCD that we have a device connected and addressed */
4155 if (hcd->driver->update_device)
4156 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157fail:
Alan Stern4326ed02007-07-30 17:08:43 -04004158 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05004160 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04004161 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01004162 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 return retval;
4164}
4165
4166static void
4167check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
4168{
4169 struct usb_qualifier_descriptor *qual;
4170 int status;
4171
Christoph Lametere94b1762006-12-06 20:33:17 -08004172 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 if (qual == NULL)
4174 return;
4175
4176 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
4177 qual, sizeof *qual);
4178 if (status == sizeof *qual) {
4179 dev_info(&udev->dev, "not running at top speed; "
4180 "connect to a high speed hub\n");
4181 /* hub LEDs are probably harder to miss than syslog */
4182 if (hub->has_indicators) {
4183 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004184 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
4186 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07004187 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
4190static unsigned
4191hub_power_remaining (struct usb_hub *hub)
4192{
4193 struct usb_device *hdev = hub->hdev;
4194 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05004195 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Alan Stern55c52712005-11-23 12:03:12 -05004197 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return 0;
4199
Alan Stern55c52712005-11-23 12:03:12 -05004200 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4201 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Lan Tianyuff823c72012-09-05 13:44:32 +08004202 struct usb_device *udev = hub->ports[port1 - 1]->child;
Alan Stern55c52712005-11-23 12:03:12 -05004203 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
4205 if (!udev)
4206 continue;
4207
Alan Stern55c52712005-11-23 12:03:12 -05004208 /* Unconfigured devices may not use more than 100mA,
4209 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004211 delta = udev->actconfig->desc.bMaxPower * 2;
4212 else if (port1 != udev->bus->otg_port || hdev->parent)
4213 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 else
Alan Stern55c52712005-11-23 12:03:12 -05004215 delta = 8;
4216 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004217 dev_warn(&udev->dev,
4218 "%dmA is over %umA budget for port %d!\n",
4219 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 remaining -= delta;
4221 }
4222 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004223 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4224 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 remaining = 0;
4226 }
4227 return remaining;
4228}
4229
4230/* Handle physical or logical connection change events.
4231 * This routine is called when:
4232 * a port connection-change occurs;
4233 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004234 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 * a firmware download)
4236 * caller already locked the hub
4237 */
4238static void hub_port_connect_change(struct usb_hub *hub, int port1,
4239 u16 portstatus, u16 portchange)
4240{
4241 struct usb_device *hdev = hub->hdev;
4242 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304243 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004244 unsigned wHubCharacteristics =
4245 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004246 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 dev_dbg (hub_dev,
4250 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004251 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 if (hub->has_indicators) {
4254 set_port_led(hub, port1, HUB_LED_AUTO);
4255 hub->indicator[port1-1] = INDICATOR_AUTO;
4256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258#ifdef CONFIG_USB_OTG
4259 /* during HNP, don't repeat the debounce */
4260 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004261 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4262 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263#endif
4264
Alan Stern8808f002008-04-28 11:06:55 -04004265 /* Try to resuscitate an existing device */
Lan Tianyuff823c72012-09-05 13:44:32 +08004266 udev = hub->ports[port1 - 1]->child;
Alan Stern8808f002008-04-28 11:06:55 -04004267 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4268 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004269 usb_lock_device(udev);
4270 if (portstatus & USB_PORT_STAT_ENABLE) {
4271 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004272
4273#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004274 } else if (udev->state == USB_STATE_SUSPENDED &&
4275 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004276 /* For a suspended device, treat this as a
4277 * remote wakeup event.
4278 */
Alan Stern0534d462010-01-08 12:56:30 -05004279 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004280#endif
4281
4282 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004283 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004284 }
4285 usb_unlock_device(udev);
4286
4287 if (status == 0) {
4288 clear_bit(port1, hub->change_bits);
4289 return;
4290 }
4291 }
4292
Alan Stern24618b02008-04-28 11:06:28 -04004293 /* Disconnect any existing devices under this port */
Peter Chenb76baa82012-11-09 09:44:43 +08004294 if (udev) {
4295 if (hcd->phy && !hdev->parent &&
4296 !(portstatus & USB_PORT_STAT_CONNECTION))
Peter Chenac965112012-11-09 09:44:44 +08004297 usb_phy_notify_disconnect(hcd->phy, udev->speed);
Lan Tianyuff823c72012-09-05 13:44:32 +08004298 usb_disconnect(&hub->ports[port1 - 1]->child);
Peter Chenb76baa82012-11-09 09:44:43 +08004299 }
Alan Stern24618b02008-04-28 11:06:28 -04004300 clear_bit(port1, hub->change_bits);
4301
Alan Stern253e0572009-10-27 15:20:13 -04004302 /* We can forget about a "removed" device when there's a physical
4303 * disconnect or the connect status changes.
4304 */
4305 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4306 (portchange & USB_PORT_STAT_C_CONNECTION))
4307 clear_bit(port1, hub->removed_bits);
4308
Alan Stern5257d972008-09-22 14:43:08 -04004309 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4310 USB_PORT_STAT_C_ENABLE)) {
4311 status = hub_port_debounce(hub, port1);
4312 if (status < 0) {
4313 if (printk_ratelimit())
4314 dev_err(hub_dev, "connect-debounce failed, "
4315 "port %d disabled\n", port1);
4316 portstatus &= ~USB_PORT_STAT_CONNECTION;
4317 } else {
4318 portstatus = status;
4319 }
4320 }
4321
Alan Stern253e0572009-10-27 15:20:13 -04004322 /* Return now if debouncing failed or nothing is connected or
4323 * the device was "removed".
4324 */
4325 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4326 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004329 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004330 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 if (portstatus & USB_PORT_STAT_ENABLE)
4334 goto done;
4335 return;
4336 }
4337
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 /* reallocate for each attempt, since references
4341 * to the previous one can escape in various ways
4342 */
4343 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4344 if (!udev) {
4345 dev_err (hub_dev,
4346 "couldn't allocate port %d usb_device\n",
4347 port1);
4348 goto done;
4349 }
4350
4351 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004352 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004353 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004354 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004355
Sarah Sharp131dec32010-12-06 21:00:19 -08004356 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4357 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004358 udev->speed = USB_SPEED_SUPER;
4359 else
4360 udev->speed = USB_SPEED_UNKNOWN;
4361
Alan Stern3b29b682011-02-22 09:53:41 -05004362 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004363 if (udev->devnum <= 0) {
4364 status = -ENOTCONN; /* Don't retry */
4365 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004366 }
4367
Sarah Sharpe7b77172009-04-27 19:54:26 -07004368 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 status = hub_port_init(hub, udev, port1, i);
4370 if (status < 0)
4371 goto loop;
4372
Phil Dibowitz93362a82010-07-22 00:05:01 +02004373 usb_detect_quirks(udev);
4374 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4375 msleep(1000);
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 /* consecutive bus-powered hubs aren't reliable; they can
4378 * violate the voltage drop budget. if the new child has
4379 * a "powered" LED, users should notice we didn't enable it
4380 * (without reading syslog), even without per-port LEDs
4381 * on the parent.
4382 */
4383 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004384 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 u16 devstat;
4386
4387 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4388 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004389 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 dev_dbg(&udev->dev, "get status %d ?\n", status);
4391 goto loop_disable;
4392 }
Alan Stern55c52712005-11-23 12:03:12 -05004393 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4395 dev_err(&udev->dev,
4396 "can't connect bus-powered hub "
4397 "to this port\n");
4398 if (hub->has_indicators) {
4399 hub->indicator[port1-1] =
4400 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004401 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 }
4403 status = -ENOTCONN; /* Don't retry */
4404 goto loop_disable;
4405 }
4406 }
4407
4408 /* check for devices running slower than they could */
4409 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4410 && udev->speed == USB_SPEED_FULL
4411 && highspeed_hubs != 0)
4412 check_highspeed (hub, udev, port1);
4413
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004414 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 * udev becomes globally accessible, although presumably
4416 * no one will look at it until hdev is unlocked.
4417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 status = 0;
4419
4420 /* We mustn't add new devices if the parent hub has
4421 * been disconnected; we would race with the
4422 * recursively_mark_NOTATTACHED() routine.
4423 */
4424 spin_lock_irq(&device_state_lock);
4425 if (hdev->state == USB_STATE_NOTATTACHED)
4426 status = -ENOTCONN;
4427 else
Lan Tianyuff823c72012-09-05 13:44:32 +08004428 hub->ports[port1 - 1]->child = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 spin_unlock_irq(&device_state_lock);
4430
4431 /* Run it through the hoops (find a driver, etc) */
4432 if (!status) {
4433 status = usb_new_device(udev);
4434 if (status) {
4435 spin_lock_irq(&device_state_lock);
Lan Tianyuff823c72012-09-05 13:44:32 +08004436 hub->ports[port1 - 1]->child = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 spin_unlock_irq(&device_state_lock);
4438 }
4439 }
4440
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 if (status)
4442 goto loop_disable;
4443
4444 status = hub_power_remaining(hub);
4445 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004446 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448 return;
4449
4450loop_disable:
4451 hub_port_disable(hub, port1, 1);
4452loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004453 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004454 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004455 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004457 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 break;
4459 }
Alan Stern3a311552008-05-20 16:58:29 -04004460 if (hub->hdev->parent ||
4461 !hcd->driver->port_handed_over ||
4462 !(hcd->driver->port_handed_over)(hcd, port1))
4463 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4464 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
4466done:
4467 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304468 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4469 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470}
4471
Sarah Sharp714b07b2012-01-24 13:53:18 -08004472/* Returns 1 if there was a remote wakeup and a connect status change. */
4473static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004474 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004475{
4476 struct usb_device *hdev;
4477 struct usb_device *udev;
4478 int connect_change = 0;
4479 int ret;
4480
4481 hdev = hub->hdev;
Lan Tianyuff823c72012-09-05 13:44:32 +08004482 udev = hub->ports[port - 1]->child;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004483 if (!hub_is_superspeed(hdev)) {
4484 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4485 return 0;
4486 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4487 } else {
4488 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004489 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4490 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004491 return 0;
4492 }
4493
Sarah Sharp714b07b2012-01-24 13:53:18 -08004494 if (udev) {
4495 /* TRSMRCY = 10 msec */
4496 msleep(10);
4497
4498 usb_lock_device(udev);
4499 ret = usb_remote_wakeup(udev);
4500 usb_unlock_device(udev);
4501 if (ret < 0)
4502 connect_change = 1;
4503 } else {
4504 ret = -ENODEV;
4505 hub_port_disable(hub, port, 1);
4506 }
4507 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4508 port, ret);
4509 return connect_change;
4510}
4511
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512static void hub_events(void)
4513{
4514 struct list_head *tmp;
4515 struct usb_device *hdev;
4516 struct usb_interface *intf;
4517 struct usb_hub *hub;
4518 struct device *hub_dev;
4519 u16 hubstatus;
4520 u16 hubchange;
4521 u16 portstatus;
4522 u16 portchange;
4523 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004524 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
4526 /*
4527 * We restart the list every time to avoid a deadlock with
4528 * deleting hubs downstream from this one. This should be
4529 * safe since we delete the hub from the event list.
4530 * Not the most efficient, but avoids deadlocks.
4531 */
4532 while (1) {
4533
4534 /* Grab the first entry at the beginning of the list */
4535 spin_lock_irq(&hub_event_lock);
4536 if (list_empty(&hub_event_list)) {
4537 spin_unlock_irq(&hub_event_lock);
4538 break;
4539 }
4540
4541 tmp = hub_event_list.next;
4542 list_del_init(tmp);
4543
4544 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004545 kref_get(&hub->kref);
4546 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Alan Sterne8054852007-05-04 11:55:11 -04004548 hdev = hub->hdev;
4549 hub_dev = hub->intfdev;
4550 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004551 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 hdev->state, hub->descriptor
4553 ? hub->descriptor->bNbrPorts
4554 : 0,
4555 /* NOTE: expects max 15 ports... */
4556 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004557 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 /* Lock the device, then check to see if we were
4560 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004561 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004562 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004563 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565 /* If the hub has died, clean up after it */
4566 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004567 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004568 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 goto loop;
4570 }
4571
Alan Stern40f122f2006-11-09 14:44:33 -05004572 /* Autoresume */
4573 ret = usb_autopm_get_interface(intf);
4574 if (ret) {
4575 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004577 }
4578
4579 /* If this is an inactive hub, do nothing */
4580 if (hub->quiescing)
4581 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
4583 if (hub->error) {
4584 dev_dbg (hub_dev, "resetting for error %d\n",
4585 hub->error);
4586
Ming Lei742120c2008-06-18 22:00:29 +08004587 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 if (ret) {
4589 dev_dbg (hub_dev,
4590 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004591 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 }
4593
4594 hub->nerrors = 0;
4595 hub->error = 0;
4596 }
4597
4598 /* deal with port status changes */
4599 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4600 if (test_bit(i, hub->busy_bits))
4601 continue;
4602 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004603 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004605 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 continue;
4607
4608 ret = hub_port_status(hub, i,
4609 &portstatus, &portchange);
4610 if (ret < 0)
4611 continue;
4612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4614 clear_port_feature(hdev, i,
4615 USB_PORT_FEAT_C_CONNECTION);
4616 connect_change = 1;
4617 }
4618
4619 if (portchange & USB_PORT_STAT_C_ENABLE) {
4620 if (!connect_change)
4621 dev_dbg (hub_dev,
4622 "port %d enable change, "
4623 "status %08x\n",
4624 i, portstatus);
4625 clear_port_feature(hdev, i,
4626 USB_PORT_FEAT_C_ENABLE);
4627
4628 /*
4629 * EM interference sometimes causes badly
4630 * shielded USB devices to be shutdown by
4631 * the hub, this hack enables them again.
4632 * Works at least with mouse driver.
4633 */
4634 if (!(portstatus & USB_PORT_STAT_ENABLE)
4635 && !connect_change
Lan Tianyuff823c72012-09-05 13:44:32 +08004636 && hub->ports[i - 1]->child) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 dev_err (hub_dev,
4638 "port %i "
4639 "disabled by hub (EMI?), "
4640 "re-enabling...\n",
4641 i);
4642 connect_change = 1;
4643 }
4644 }
4645
Sarah Sharp72937e12012-01-24 11:46:50 -08004646 if (hub_handle_remote_wakeup(hub, i,
4647 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004648 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004651 u16 status = 0;
4652 u16 unused;
4653
4654 dev_dbg(hub_dev, "over-current change on port "
4655 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 clear_port_feature(hdev, i,
4657 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004658 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004659 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004660 hub_port_status(hub, i, &status, &unused);
4661 if (status & USB_PORT_STAT_OVERCURRENT)
4662 dev_err(hub_dev, "over-current "
4663 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
4665
4666 if (portchange & USB_PORT_STAT_C_RESET) {
4667 dev_dbg (hub_dev,
4668 "reset change on port %d\n",
4669 i);
4670 clear_port_feature(hdev, i,
4671 USB_PORT_FEAT_C_RESET);
4672 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004673 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4674 hub_is_superspeed(hub->hdev)) {
4675 dev_dbg(hub_dev,
4676 "warm reset change on port %d\n",
4677 i);
4678 clear_port_feature(hdev, i,
4679 USB_PORT_FEAT_C_BH_PORT_RESET);
4680 }
John Youndbe79bb2001-09-17 00:00:00 -07004681 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4682 clear_port_feature(hub->hdev, i,
4683 USB_PORT_FEAT_C_PORT_LINK_STATE);
4684 }
4685 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4686 dev_warn(hub_dev,
4687 "config error on port %d\n",
4688 i);
4689 clear_port_feature(hub->hdev, i,
4690 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
Andiry Xu5e467f62011-04-27 18:07:54 +08004693 /* Warm reset a USB3 protocol port if it's in
4694 * SS.Inactive state.
4695 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004696 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004697 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004698 hub_port_reset(hub, i, NULL,
4699 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004700 }
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 if (connect_change)
4703 hub_port_connect_change(hub, i,
4704 portstatus, portchange);
4705 } /* end for i */
4706
4707 /* deal with hub status changes */
4708 if (test_and_clear_bit(0, hub->event_bits) == 0)
4709 ; /* do nothing */
4710 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4711 dev_err (hub_dev, "get_hub_status failed\n");
4712 else {
4713 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4714 dev_dbg (hub_dev, "power change\n");
4715 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004716 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4717 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004718 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004719 else
4720 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 }
4722 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004723 u16 status = 0;
4724 u16 unused;
4725
4726 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004728 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004729 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004730 hub_hub_status(hub, &status, &unused);
4731 if (status & HUB_STATUS_OVERCURRENT)
4732 dev_err(hub_dev, "over-current "
4733 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
4735 }
4736
Alan Stern8e4ceb32009-12-07 13:01:37 -05004737 loop_autopm:
4738 /* Balance the usb_autopm_get_interface() above */
4739 usb_autopm_put_interface_no_suspend(intf);
4740 loop:
4741 /* Balance the usb_autopm_get_interface_no_resume() in
4742 * kick_khubd() and allow autosuspend.
4743 */
4744 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004745 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004747 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
4749 } /* end while (1) */
4750}
4751
4752static int hub_thread(void *__unused)
4753{
Alan Stern3bb1af52008-03-03 15:15:36 -05004754 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4755 * port handover. Otherwise it might see that a full-speed device
4756 * was gone before the EHCI controller had handed its port over to
4757 * the companion full-speed controller.
4758 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004759 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 do {
4762 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004763 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004764 !list_empty(&hub_event_list) ||
4765 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004766 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004768 pr_debug("%s: khubd exiting\n", usbcore_name);
4769 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770}
4771
Németh Márton1e927d92010-01-10 15:34:53 +01004772static const struct usb_device_id hub_id_table[] = {
Ming Leie6f30de2012-10-24 11:59:24 +08004773 { .match_flags = USB_DEVICE_ID_MATCH_VENDOR
4774 | USB_DEVICE_ID_MATCH_INT_CLASS,
4775 .idVendor = USB_VENDOR_GENESYS_LOGIC,
4776 .bInterfaceClass = USB_CLASS_HUB,
4777 .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4779 .bDeviceClass = USB_CLASS_HUB},
4780 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4781 .bInterfaceClass = USB_CLASS_HUB},
4782 { } /* Terminating entry */
4783};
4784
4785MODULE_DEVICE_TABLE (usb, hub_id_table);
4786
4787static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 .name = "hub",
4789 .probe = hub_probe,
4790 .disconnect = hub_disconnect,
4791 .suspend = hub_suspend,
4792 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004793 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004794 .pre_reset = hub_pre_reset,
4795 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004796 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004798 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799};
4800
4801int usb_hub_init(void)
4802{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 if (usb_register(&hub_driver) < 0) {
4804 printk(KERN_ERR "%s: can't register hub driver\n",
4805 usbcore_name);
4806 return -1;
4807 }
4808
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004809 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4810 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813 /* Fall through if kernel_thread failed */
4814 usb_deregister(&hub_driver);
4815 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4816
4817 return -1;
4818}
4819
4820void usb_hub_cleanup(void)
4821{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004822 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823
4824 /*
4825 * Hub resources are freed for us by usb_deregister. It calls
4826 * usb_driver_purge on every device which in turn calls that
4827 * devices disconnect function if it is using this driver.
4828 * The hub_disconnect function takes care of releasing the
4829 * individual hub resources. -greg
4830 */
4831 usb_deregister(&hub_driver);
4832} /* usb_hub_cleanup() */
4833
Alan Sterneb764c42008-03-03 15:16:04 -05004834static int descriptors_changed(struct usb_device *udev,
4835 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836{
Alan Sterneb764c42008-03-03 15:16:04 -05004837 int changed = 0;
4838 unsigned index;
4839 unsigned serial_len = 0;
4840 unsigned len;
4841 unsigned old_length;
4842 int length;
4843 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Alan Sterneb764c42008-03-03 15:16:04 -05004845 if (memcmp(&udev->descriptor, old_device_descriptor,
4846 sizeof(*old_device_descriptor)) != 0)
4847 return 1;
4848
4849 /* Since the idVendor, idProduct, and bcdDevice values in the
4850 * device descriptor haven't changed, we will assume the
4851 * Manufacturer and Product strings haven't changed either.
4852 * But the SerialNumber string could be different (e.g., a
4853 * different flash card of the same brand).
4854 */
4855 if (udev->serial)
4856 serial_len = strlen(udev->serial) + 1;
4857
4858 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004860 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4861 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
Alan Sterneb764c42008-03-03 15:16:04 -05004863
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004864 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 if (buf == NULL) {
4866 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4867 /* assume the worst */
4868 return 1;
4869 }
4870 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004871 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4873 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004874 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 dev_dbg(&udev->dev, "config index %d, error %d\n",
4876 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004877 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 break;
4879 }
4880 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4881 != 0) {
4882 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004883 index,
4884 ((struct usb_config_descriptor *) buf)->
4885 bConfigurationValue);
4886 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 break;
4888 }
4889 }
Alan Sterneb764c42008-03-03 15:16:04 -05004890
4891 if (!changed && serial_len) {
4892 length = usb_string(udev, udev->descriptor.iSerialNumber,
4893 buf, serial_len);
4894 if (length + 1 != serial_len) {
4895 dev_dbg(&udev->dev, "serial string error %d\n",
4896 length);
4897 changed = 1;
4898 } else if (memcmp(buf, udev->serial, length) != 0) {
4899 dev_dbg(&udev->dev, "serial string changed\n");
4900 changed = 1;
4901 }
4902 }
4903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004905 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906}
4907
4908/**
Ming Lei742120c2008-06-18 22:00:29 +08004909 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4911 *
Alan Stern79efa092006-06-01 13:33:42 -04004912 * WARNING - don't use this routine to reset a composite device
4913 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004914 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 *
4916 * Do a port reset, reassign the device's address, and establish its
4917 * former operating configuration. If the reset fails, or the device's
4918 * descriptors change from their values before the reset, or the original
4919 * configuration and altsettings cannot be restored, a flag will be set
4920 * telling khubd to pretend the device has been disconnected and then
4921 * re-connected. All drivers will be unbound, and the device will be
4922 * re-enumerated and probed all over again.
4923 *
4924 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4925 * flagged for logical disconnection, or some other negative error code
4926 * if the reset wasn't even attempted.
4927 *
4928 * The caller must own the device lock. For example, it's safe to use
4929 * this from a driver probe() routine after downloading new firmware.
4930 * For calls that might not occur during probe(), drivers should lock
4931 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004932 *
4933 * Locking exception: This routine may also be called from within an
4934 * autoresume handler. Such usage won't conflict with other tasks
4935 * holding the device lock because these tasks should always call
4936 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 */
Ming Lei742120c2008-06-18 22:00:29 +08004938static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939{
4940 struct usb_device *parent_hdev = udev->parent;
4941 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004942 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004944 int i, ret = 0;
4945 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946
4947 if (udev->state == USB_STATE_NOTATTACHED ||
4948 udev->state == USB_STATE_SUSPENDED) {
4949 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4950 udev->state);
4951 return -EINVAL;
4952 }
4953
4954 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004955 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004956 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 return -EISDIR;
4958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 parent_hub = hdev_to_hub(parent_hdev);
4960
Sarah Sharpf74631e2012-06-25 12:08:08 -07004961 /* Disable LPM and LTM while we reset the device and reinstall the alt
4962 * settings. Device-initiated LPM settings, and system exit latency
4963 * settings are cleared when the device is reset, so we have to set
4964 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004965 */
4966 ret = usb_unlocked_disable_lpm(udev);
4967 if (ret) {
4968 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4969 goto re_enumerate;
4970 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004971 ret = usb_disable_ltm(udev);
4972 if (ret) {
4973 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4974 __func__);
4975 goto re_enumerate;
4976 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 set_bit(port1, parent_hub->busy_bits);
4979 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4980
4981 /* ep0 maxpacket size may change; let the HCD know about it.
4982 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004983 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004985 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 break;
4987 }
4988 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 if (ret < 0)
4991 goto re_enumerate;
4992
4993 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004994 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 dev_info(&udev->dev, "device firmware changed\n");
4996 udev->descriptor = descriptor; /* for disconnect() calls */
4997 goto re_enumerate;
4998 }
Alan Stern4fe03872009-02-26 10:21:02 -05004999
5000 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 if (!udev->actconfig)
5002 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005003
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005004 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005005 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
5006 if (ret < 0) {
5007 dev_warn(&udev->dev,
5008 "Busted HC? Not enough HCD resources for "
5009 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005010 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005011 goto re_enumerate;
5012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5014 USB_REQ_SET_CONFIGURATION, 0,
5015 udev->actconfig->desc.bConfigurationValue, 0,
5016 NULL, 0, USB_CTRL_SET_TIMEOUT);
5017 if (ret < 0) {
5018 dev_err(&udev->dev,
5019 "can't restore configuration #%d (error=%d)\n",
5020 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005021 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 goto re_enumerate;
5023 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07005024 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 usb_set_device_state(udev, USB_STATE_CONFIGURED);
5026
Alan Stern4fe03872009-02-26 10:21:02 -05005027 /* Put interfaces back into the same altsettings as before.
5028 * Don't bother to send the Set-Interface request for interfaces
5029 * that were already in altsetting 0; besides being unnecessary,
5030 * many devices can't handle it. Instead just reset the host-side
5031 * endpoint state.
5032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005034 struct usb_host_config *config = udev->actconfig;
5035 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 struct usb_interface_descriptor *desc;
5037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05005039 if (desc->bAlternateSetting == 0) {
5040 usb_disable_interface(udev, intf, true);
5041 usb_enable_interface(udev, intf, true);
5042 ret = 0;
5043 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08005044 /* Let the bandwidth allocation function know that this
5045 * device has been reset, and it will have to use
5046 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08005047 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08005048 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05005049 ret = usb_set_interface(udev, desc->bInterfaceNumber,
5050 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08005051 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05005052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 if (ret < 0) {
5054 dev_err(&udev->dev, "failed to restore interface %d "
5055 "altsetting %d (error=%d)\n",
5056 desc->bInterfaceNumber,
5057 desc->bAlternateSetting,
5058 ret);
5059 goto re_enumerate;
5060 }
5061 }
5062
5063done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07005064 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04005065 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07005066 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 return 0;
5068
5069re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07005070 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 hub_port_logical_disconnect(parent_hub, port1);
5072 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073}
5074
5075/**
5076 * usb_reset_device - warn interface drivers and perform a USB port reset
5077 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
5078 *
Alan Stern79efa092006-06-01 13:33:42 -04005079 * Warns all drivers bound to registered interfaces (using their pre_reset
5080 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08005081 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04005082 *
Alan Stern79efa092006-06-01 13:33:42 -04005083 * Return value is the same as for usb_reset_and_verify_device().
5084 *
5085 * The caller must own the device lock. For example, it's safe to use
5086 * this from a driver probe() routine after downloading new firmware.
5087 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08005088 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04005089 *
5090 * If an interface is currently being probed or disconnected, we assume
5091 * its driver knows how to handle resets. For all other interfaces,
5092 * if the driver doesn't have pre_reset and post_reset methods then
5093 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04005094 */
Ming Lei742120c2008-06-18 22:00:29 +08005095int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04005096{
5097 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05005098 int i;
Alan Stern79efa092006-06-01 13:33:42 -04005099 struct usb_host_config *config = udev->actconfig;
5100
5101 if (udev->state == USB_STATE_NOTATTACHED ||
5102 udev->state == USB_STATE_SUSPENDED) {
5103 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
5104 udev->state);
5105 return -EINVAL;
5106 }
5107
Alan Stern645daaa2006-08-30 15:47:02 -04005108 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05005109 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04005110
Alan Stern79efa092006-06-01 13:33:42 -04005111 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005112 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005113 struct usb_interface *cintf = config->interface[i];
5114 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005115 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05005116
5117 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005118 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04005119 if (drv->pre_reset && drv->post_reset)
5120 unbind = (drv->pre_reset)(cintf);
5121 else if (cintf->condition ==
5122 USB_INTERFACE_BOUND)
5123 unbind = 1;
5124 if (unbind)
5125 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005126 }
5127 }
5128 }
5129
Ming Lei742120c2008-06-18 22:00:29 +08005130 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005131
5132 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04005133 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05005134 struct usb_interface *cintf = config->interface[i];
5135 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04005136 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05005137
Alan Stern78d9a482008-06-23 16:00:40 -04005138 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04005139 drv = to_usb_driver(cintf->dev.driver);
5140 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04005141 rebind = (drv->post_reset)(cintf);
5142 else if (cintf->condition ==
5143 USB_INTERFACE_BOUND)
5144 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04005145 }
Alan Stern6c640942008-10-21 15:40:03 -04005146 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04005147 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04005148 }
5149 }
5150
Alan Stern94fcda12006-11-20 11:38:46 -05005151 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04005152 return ret;
5153}
Ming Lei742120c2008-06-18 22:00:29 +08005154EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08005155
5156
5157/**
5158 * usb_queue_reset_device - Reset a USB device from an atomic context
5159 * @iface: USB interface belonging to the device to reset
5160 *
5161 * This function can be used to reset a USB device from an atomic
5162 * context, where usb_reset_device() won't work (as it blocks).
5163 *
5164 * Doing a reset via this method is functionally equivalent to calling
5165 * usb_reset_device(), except for the fact that it is delayed to a
5166 * workqueue. This means that any drivers bound to other interfaces
5167 * might be unbound, as well as users from usbfs in user space.
5168 *
5169 * Corner cases:
5170 *
5171 * - Scheduling two resets at the same time from two different drivers
5172 * attached to two different interfaces of the same device is
5173 * possible; depending on how the driver attached to each interface
5174 * handles ->pre_reset(), the second reset might happen or not.
5175 *
5176 * - If a driver is unbound and it had a pending reset, the reset will
5177 * be cancelled.
5178 *
5179 * - This function can be called during .probe() or .disconnect()
5180 * times. On return from .disconnect(), any pending resets will be
5181 * cancelled.
5182 *
5183 * There is no no need to lock/unlock the @reset_ws as schedule_work()
5184 * does its own.
5185 *
5186 * NOTE: We don't do any reference count tracking because it is not
5187 * needed. The lifecycle of the work_struct is tied to the
5188 * usb_interface. Before destroying the interface we cancel the
5189 * work_struct, so the fact that work_struct is queued and or
5190 * running means the interface (and thus, the device) exist and
5191 * are referenced.
5192 */
5193void usb_queue_reset_device(struct usb_interface *iface)
5194{
5195 schedule_work(&iface->reset_ws);
5196}
5197EXPORT_SYMBOL_GPL(usb_queue_reset_device);
Lan Tianyuff823c72012-09-05 13:44:32 +08005198
5199/**
5200 * usb_hub_find_child - Get the pointer of child device
5201 * attached to the port which is specified by @port1.
5202 * @hdev: USB device belonging to the usb hub
5203 * @port1: port num to indicate which port the child device
5204 * is attached to.
5205 *
5206 * USB drivers call this function to get hub's child device
5207 * pointer.
5208 *
5209 * Return NULL if input param is invalid and
5210 * child's usb_device pointer if non-NULL.
5211 */
5212struct usb_device *usb_hub_find_child(struct usb_device *hdev,
5213 int port1)
5214{
5215 struct usb_hub *hub = hdev_to_hub(hdev);
5216
5217 if (port1 < 1 || port1 > hdev->maxchild)
5218 return NULL;
5219 return hub->ports[port1 - 1]->child;
5220}
5221EXPORT_SYMBOL_GPL(usb_hub_find_child);
Lan Tianyud5575422012-09-05 13:44:33 +08005222
Lan Tianyu05f91682012-09-05 13:44:34 +08005223/**
5224 * usb_set_hub_port_connect_type - set hub port connect type.
5225 * @hdev: USB device belonging to the usb hub
5226 * @port1: port num of the port
5227 * @type: connect type of the port
5228 */
5229void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1,
5230 enum usb_port_connect_type type)
5231{
5232 struct usb_hub *hub = hdev_to_hub(hdev);
5233
5234 hub->ports[port1 - 1]->connect_type = type;
5235}
5236
5237/**
5238 * usb_get_hub_port_connect_type - Get the port's connect type
5239 * @hdev: USB device belonging to the usb hub
5240 * @port1: port num of the port
5241 *
5242 * Return connect type of the port and if input params are
5243 * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN.
5244 */
5245enum usb_port_connect_type
5246usb_get_hub_port_connect_type(struct usb_device *hdev, int port1)
5247{
5248 struct usb_hub *hub = hdev_to_hub(hdev);
5249
5250 return hub->ports[port1 - 1]->connect_type;
5251}
5252
Lan Tianyud5575422012-09-05 13:44:33 +08005253#ifdef CONFIG_ACPI
5254/**
5255 * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle
5256 * @hdev: USB device belonging to the usb hub
5257 * @port1: port num of the port
5258 *
5259 * Return port's acpi handle if successful, NULL if params are
5260 * invaild.
5261 */
5262acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev,
5263 int port1)
5264{
5265 struct usb_hub *hub = hdev_to_hub(hdev);
5266
5267 return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
5268}
5269#endif