blob: 128a804c42f406b6c6b1f32f57110ff6b0810b41 [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
Alan Stern1bb5f662006-11-06 11:56:13 -050042struct usb_hub {
43 struct device *intfdev; /* the "interface" device */
44 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040045 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050046 struct urb *urb; /* for interrupt polling pipe */
47
48 /* buffer for urb ... with extra space in case of babble */
49 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050050 union {
51 struct usb_hub_status hub;
52 struct usb_port_status port;
53 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050054 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050055
56 int error; /* last reported error */
57 int nerrors; /* track consecutive errors */
58
59 struct list_head event_list; /* hubs w/data or errs ready */
60 unsigned long event_bits[1]; /* status change bitmask */
61 unsigned long change_bits[1]; /* ports with logical connect
62 status change */
63 unsigned long busy_bits[1]; /* ports being reset or
64 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040065 unsigned long removed_bits[1]; /* ports with a "removed"
66 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080067 unsigned long wakeup_bits[1]; /* ports that have signaled
68 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050069#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
70#error event_bits[] is too short!
71#endif
72
73 struct usb_hub_descriptor *descriptor; /* class descriptor */
74 struct usb_tt tt; /* Transaction Translator */
75
76 unsigned mA_per_port; /* current for each child */
77
78 unsigned limited_power:1;
79 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040080 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050081
82 unsigned has_indicators:1;
83 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000084 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -040085 struct delayed_work init_work;
Lan Tianyu336c5c32012-07-06 14:13:52 +080086 struct dev_state **port_owners;
Alan Stern1bb5f662006-11-06 11:56:13 -050087};
88
John Youndbe79bb2001-09-17 00:00:00 -070089static inline int hub_is_superspeed(struct usb_device *hdev)
90{
Aman Deep7bf01182011-12-08 12:05:22 +053091 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -070092}
Alan Stern1bb5f662006-11-06 11:56:13 -050093
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -070094/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050095 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
97static DEFINE_SPINLOCK(device_state_lock);
98
99/* khubd's worklist and its lock */
100static DEFINE_SPINLOCK(hub_event_lock);
101static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
102
103/* Wakes up khubd */
104static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
105
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700106static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030109static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110module_param (blinkenlights, bool, S_IRUGO);
111MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
112
113/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200114 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
115 * 10 seconds to send reply for the initial 64-byte descriptor request.
116 */
117/* define initial 64-byte descriptor request timeout in milliseconds */
118static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
119module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800120MODULE_PARM_DESC(initial_descriptor_timeout,
121 "initial 64-byte descriptor request timeout in milliseconds "
122 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200123
124/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * As of 2.6.10 we introduce a new USB device initialization scheme which
126 * closely resembles the way Windows works. Hopefully it will be compatible
127 * with a wider range of devices than the old scheme. However some previously
128 * working devices may start giving rise to "device not accepting address"
129 * errors; if that happens the user can try the old scheme by adjusting the
130 * following module parameters.
131 *
132 * For maximum flexibility there are two boolean parameters to control the
133 * hub driver's behavior. On the first initialization attempt, if the
134 * "old_scheme_first" parameter is set then the old scheme will be used,
135 * otherwise the new scheme is used. If that fails and "use_both_schemes"
136 * is set, then the driver will make another attempt, using the other scheme.
137 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030138static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
140MODULE_PARM_DESC(old_scheme_first,
141 "start with the old device initialization scheme");
142
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030143static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
145MODULE_PARM_DESC(use_both_schemes,
146 "try the other device initialization scheme if the "
147 "first one fails");
148
Alan Stern32fe0192007-10-10 16:27:07 -0400149/* Mutual exclusion for EHCI CF initialization. This interferes with
150 * port reset on some companion controllers.
151 */
152DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
153EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
154
Alan Stern948fea32008-04-28 11:07:07 -0400155#define HUB_DEBOUNCE_TIMEOUT 1500
156#define HUB_DEBOUNCE_STEP 25
157#define HUB_DEBOUNCE_STABLE 100
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Ming Lei742120c2008-06-18 22:00:29 +0800160static int usb_reset_and_verify_device(struct usb_device *udev);
161
Sarah Sharp131dec32010-12-06 21:00:19 -0800162static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Sarah Sharp131dec32010-12-06 21:00:19 -0800164 if (hub_is_superspeed(hub->hdev))
165 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500166 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500168 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return "1.5 Mb/s";
170 else
171 return "12 Mb/s";
172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400175static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700177 if (!hdev || !hdev->actconfig)
Alan Stern251180842009-07-22 14:41:18 -0400178 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return usb_get_intfdata(hdev->actconfig->interface[0]);
180}
181
Sarah Sharpd9b20992012-02-20 08:31:26 -0800182static int usb_device_supports_lpm(struct usb_device *udev)
183{
184 /* USB 2.1 (and greater) devices indicate LPM support through
185 * their USB 2.0 Extended Capabilities BOS descriptor.
186 */
187 if (udev->speed == USB_SPEED_HIGH) {
188 if (udev->bos->ext_cap &&
189 (USB_LPM_SUPPORT &
190 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
191 return 1;
192 return 0;
193 }
Sarah Sharp51e0a012012-02-20 12:02:19 -0800194
195 /* All USB 3.0 must support LPM, but we need their max exit latency
196 * information from the SuperSpeed Extended Capabilities BOS descriptor.
197 */
198 if (!udev->bos->ss_cap) {
199 dev_warn(&udev->dev, "No LPM exit latency info found. "
200 "Power management will be impacted.\n");
201 return 0;
202 }
203 if (udev->parent->lpm_capable)
204 return 1;
205
206 dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. "
207 "Power management will be impacted.\n");
Sarah Sharpd9b20992012-02-20 08:31:26 -0800208 return 0;
209}
210
Sarah Sharp51e0a012012-02-20 12:02:19 -0800211/*
212 * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from
213 * either U1 or U2.
214 */
215static void usb_set_lpm_mel(struct usb_device *udev,
216 struct usb3_lpm_parameters *udev_lpm_params,
217 unsigned int udev_exit_latency,
218 struct usb_hub *hub,
219 struct usb3_lpm_parameters *hub_lpm_params,
220 unsigned int hub_exit_latency)
221{
222 unsigned int total_mel;
223 unsigned int device_mel;
224 unsigned int hub_mel;
225
226 /*
227 * Calculate the time it takes to transition all links from the roothub
228 * to the parent hub into U0. The parent hub must then decode the
229 * packet (hub header decode latency) to figure out which port it was
230 * bound for.
231 *
232 * The Hub Header decode latency is expressed in 0.1us intervals (0x1
233 * means 0.1us). Multiply that by 100 to get nanoseconds.
234 */
235 total_mel = hub_lpm_params->mel +
236 (hub->descriptor->u.ss.bHubHdrDecLat * 100);
237
238 /*
239 * How long will it take to transition the downstream hub's port into
240 * U0? The greater of either the hub exit latency or the device exit
241 * latency.
242 *
243 * The BOS U1/U2 exit latencies are expressed in 1us intervals.
244 * Multiply that by 1000 to get nanoseconds.
245 */
246 device_mel = udev_exit_latency * 1000;
247 hub_mel = hub_exit_latency * 1000;
248 if (device_mel > hub_mel)
249 total_mel += device_mel;
250 else
251 total_mel += hub_mel;
252
253 udev_lpm_params->mel = total_mel;
254}
255
256/*
257 * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate
258 * a transition from either U1 or U2.
259 */
260static void usb_set_lpm_pel(struct usb_device *udev,
261 struct usb3_lpm_parameters *udev_lpm_params,
262 unsigned int udev_exit_latency,
263 struct usb_hub *hub,
264 struct usb3_lpm_parameters *hub_lpm_params,
265 unsigned int hub_exit_latency,
266 unsigned int port_to_port_exit_latency)
267{
268 unsigned int first_link_pel;
269 unsigned int hub_pel;
270
271 /*
272 * First, the device sends an LFPS to transition the link between the
273 * device and the parent hub into U0. The exit latency is the bigger of
274 * the device exit latency or the hub exit latency.
275 */
276 if (udev_exit_latency > hub_exit_latency)
277 first_link_pel = udev_exit_latency * 1000;
278 else
279 first_link_pel = hub_exit_latency * 1000;
280
281 /*
282 * When the hub starts to receive the LFPS, there is a slight delay for
283 * it to figure out that one of the ports is sending an LFPS. Then it
284 * will forward the LFPS to its upstream link. The exit latency is the
285 * delay, plus the PEL that we calculated for this hub.
286 */
287 hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel;
288
289 /*
290 * According to figure C-7 in the USB 3.0 spec, the PEL for this device
291 * is the greater of the two exit latencies.
292 */
293 if (first_link_pel > hub_pel)
294 udev_lpm_params->pel = first_link_pel;
295 else
296 udev_lpm_params->pel = hub_pel;
297}
298
299/*
300 * Set the System Exit Latency (SEL) to indicate the total worst-case time from
301 * when a device initiates a transition to U0, until when it will receive the
302 * first packet from the host controller.
303 *
304 * Section C.1.5.1 describes the four components to this:
305 * - t1: device PEL
306 * - t2: time for the ERDY to make it from the device to the host.
307 * - t3: a host-specific delay to process the ERDY.
308 * - t4: time for the packet to make it from the host to the device.
309 *
310 * t3 is specific to both the xHCI host and the platform the host is integrated
311 * into. The Intel HW folks have said it's negligible, FIXME if a different
312 * vendor says otherwise.
313 */
314static void usb_set_lpm_sel(struct usb_device *udev,
315 struct usb3_lpm_parameters *udev_lpm_params)
316{
317 struct usb_device *parent;
318 unsigned int num_hubs;
319 unsigned int total_sel;
320
321 /* t1 = device PEL */
322 total_sel = udev_lpm_params->pel;
323 /* How many external hubs are in between the device & the root port. */
324 for (parent = udev->parent, num_hubs = 0; parent->parent;
325 parent = parent->parent)
326 num_hubs++;
327 /* t2 = 2.1us + 250ns * (num_hubs - 1) */
328 if (num_hubs > 0)
329 total_sel += 2100 + 250 * (num_hubs - 1);
330
331 /* t4 = 250ns * num_hubs */
332 total_sel += 250 * num_hubs;
333
334 udev_lpm_params->sel = total_sel;
335}
336
337static void usb_set_lpm_parameters(struct usb_device *udev)
338{
339 struct usb_hub *hub;
340 unsigned int port_to_port_delay;
341 unsigned int udev_u1_del;
342 unsigned int udev_u2_del;
343 unsigned int hub_u1_del;
344 unsigned int hub_u2_del;
345
346 if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER)
347 return;
348
349 hub = hdev_to_hub(udev->parent);
350 /* It doesn't take time to transition the roothub into U0, since it
351 * doesn't have an upstream link.
352 */
353 if (!hub)
354 return;
355
356 udev_u1_del = udev->bos->ss_cap->bU1devExitLat;
357 udev_u2_del = udev->bos->ss_cap->bU2DevExitLat;
358 hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat;
359 hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat;
360
361 usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del,
362 hub, &udev->parent->u1_params, hub_u1_del);
363
364 usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del,
365 hub, &udev->parent->u2_params, hub_u2_del);
366
367 /*
368 * Appendix C, section C.2.2.2, says that there is a slight delay from
369 * when the parent hub notices the downstream port is trying to
370 * transition to U0 to when the hub initiates a U0 transition on its
371 * upstream port. The section says the delays are tPort2PortU1EL and
372 * tPort2PortU2EL, but it doesn't define what they are.
373 *
374 * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking
375 * about the same delays. Use the maximum delay calculations from those
376 * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For
377 * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I
378 * assume the device exit latencies they are talking about are the hub
379 * exit latencies.
380 *
381 * What do we do if the U2 exit latency is less than the U1 exit
382 * latency? It's possible, although not likely...
383 */
384 port_to_port_delay = 1;
385
386 usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del,
387 hub, &udev->parent->u1_params, hub_u1_del,
388 port_to_port_delay);
389
390 if (hub_u2_del > hub_u1_del)
391 port_to_port_delay = 1 + hub_u2_del - hub_u1_del;
392 else
393 port_to_port_delay = 1 + hub_u1_del;
394
395 usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del,
396 hub, &udev->parent->u2_params, hub_u2_del,
397 port_to_port_delay);
398
399 /* Now that we've got PEL, calculate SEL. */
400 usb_set_lpm_sel(udev, &udev->u1_params);
401 usb_set_lpm_sel(udev, &udev->u2_params);
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700405static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
John Youndbe79bb2001-09-17 00:00:00 -0700407 int i, ret, size;
408 unsigned dtype;
409
410 if (hub_is_superspeed(hdev)) {
411 dtype = USB_DT_SS_HUB;
412 size = USB_DT_SS_HUB_SIZE;
413 } else {
414 dtype = USB_DT_HUB;
415 size = sizeof(struct usb_hub_descriptor);
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 for (i = 0; i < 3; i++) {
419 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
420 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700421 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 USB_CTRL_GET_TIMEOUT);
423 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
424 return ret;
425 }
426 return -EINVAL;
427}
428
429/*
430 * USB 2.0 spec Section 11.24.2.1
431 */
432static int clear_hub_feature(struct usb_device *hdev, int feature)
433{
434 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
435 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
436}
437
438/*
439 * USB 2.0 spec Section 11.24.2.2
440 */
441static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
442{
443 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
444 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
445 NULL, 0, 1000);
446}
447
448/*
449 * USB 2.0 spec Section 11.24.2.13
450 */
451static int set_port_feature(struct usb_device *hdev, int port1, int feature)
452{
453 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
454 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
455 NULL, 0, 1000);
456}
457
458/*
459 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
460 * for info about using port indicators
461 */
462static void set_port_led(
463 struct usb_hub *hub,
464 int port1,
465 int selector
466)
467{
468 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
469 USB_PORT_FEAT_INDICATOR);
470 if (status < 0)
471 dev_dbg (hub->intfdev,
472 "port %d indicator %s status %d\n",
473 port1,
474 ({ char *s; switch (selector) {
475 case HUB_LED_AMBER: s = "amber"; break;
476 case HUB_LED_GREEN: s = "green"; break;
477 case HUB_LED_OFF: s = "off"; break;
478 case HUB_LED_AUTO: s = "auto"; break;
479 default: s = "??"; break;
480 }; s; }),
481 status);
482}
483
484#define LED_CYCLE_PERIOD ((2*HZ)/3)
485
David Howellsc4028952006-11-22 14:57:56 +0000486static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
David Howellsc4028952006-11-22 14:57:56 +0000488 struct usb_hub *hub =
489 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct usb_device *hdev = hub->hdev;
491 unsigned i;
492 unsigned changed = 0;
493 int cursor = -1;
494
495 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
496 return;
497
498 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
499 unsigned selector, mode;
500
501 /* 30%-50% duty cycle */
502
503 switch (hub->indicator[i]) {
504 /* cycle marker */
505 case INDICATOR_CYCLE:
506 cursor = i;
507 selector = HUB_LED_AUTO;
508 mode = INDICATOR_AUTO;
509 break;
510 /* blinking green = sw attention */
511 case INDICATOR_GREEN_BLINK:
512 selector = HUB_LED_GREEN;
513 mode = INDICATOR_GREEN_BLINK_OFF;
514 break;
515 case INDICATOR_GREEN_BLINK_OFF:
516 selector = HUB_LED_OFF;
517 mode = INDICATOR_GREEN_BLINK;
518 break;
519 /* blinking amber = hw attention */
520 case INDICATOR_AMBER_BLINK:
521 selector = HUB_LED_AMBER;
522 mode = INDICATOR_AMBER_BLINK_OFF;
523 break;
524 case INDICATOR_AMBER_BLINK_OFF:
525 selector = HUB_LED_OFF;
526 mode = INDICATOR_AMBER_BLINK;
527 break;
528 /* blink green/amber = reserved */
529 case INDICATOR_ALT_BLINK:
530 selector = HUB_LED_GREEN;
531 mode = INDICATOR_ALT_BLINK_OFF;
532 break;
533 case INDICATOR_ALT_BLINK_OFF:
534 selector = HUB_LED_AMBER;
535 mode = INDICATOR_ALT_BLINK;
536 break;
537 default:
538 continue;
539 }
540 if (selector != HUB_LED_AUTO)
541 changed = 1;
542 set_port_led(hub, i + 1, selector);
543 hub->indicator[i] = mode;
544 }
545 if (!changed && blinkenlights) {
546 cursor++;
547 cursor %= hub->descriptor->bNbrPorts;
548 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
549 hub->indicator[cursor] = INDICATOR_CYCLE;
550 changed++;
551 }
552 if (changed)
553 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
554}
555
556/* use a short timeout for hub/port status fetches */
557#define USB_STS_TIMEOUT 1000
558#define USB_STS_RETRIES 5
559
560/*
561 * USB 2.0 spec Section 11.24.2.6
562 */
563static int get_hub_status(struct usb_device *hdev,
564 struct usb_hub_status *data)
565{
566 int i, status = -ETIMEDOUT;
567
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200568 for (i = 0; i < USB_STS_RETRIES &&
569 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
571 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
572 data, sizeof(*data), USB_STS_TIMEOUT);
573 }
574 return status;
575}
576
577/*
578 * USB 2.0 spec Section 11.24.2.7
579 */
580static int get_port_status(struct usb_device *hdev, int port1,
581 struct usb_port_status *data)
582{
583 int i, status = -ETIMEDOUT;
584
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200585 for (i = 0; i < USB_STS_RETRIES &&
586 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
588 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
589 data, sizeof(*data), USB_STS_TIMEOUT);
590 }
591 return status;
592}
593
Alan Stern3eb14912008-03-03 15:15:43 -0500594static int hub_port_status(struct usb_hub *hub, int port1,
595 u16 *status, u16 *change)
596{
597 int ret;
598
599 mutex_lock(&hub->status_mutex);
600 ret = get_port_status(hub->hdev, port1, &hub->status->port);
601 if (ret < 4) {
602 dev_err(hub->intfdev,
603 "%s failed (err = %d)\n", __func__, ret);
604 if (ret >= 0)
605 ret = -EIO;
606 } else {
607 *status = le16_to_cpu(hub->status->port.wPortStatus);
608 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700609
Alan Stern3eb14912008-03-03 15:15:43 -0500610 ret = 0;
611 }
612 mutex_unlock(&hub->status_mutex);
613 return ret;
614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616static void kick_khubd(struct usb_hub *hub)
617{
618 unsigned long flags;
619
620 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200621 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500623
624 /* Suppress autosuspend until khubd runs */
625 usb_autopm_get_interface_no_resume(
626 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 wake_up(&khubd_wait);
628 }
629 spin_unlock_irqrestore(&hub_event_lock, flags);
630}
631
632void usb_kick_khubd(struct usb_device *hdev)
633{
Alan Stern251180842009-07-22 14:41:18 -0400634 struct usb_hub *hub = hdev_to_hub(hdev);
635
636 if (hub)
637 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800640/*
641 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
642 * Notification, which indicates it had initiated remote wakeup.
643 *
644 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
645 * device initiates resume, so the USB core will not receive notice of the
646 * resume through the normal hub interrupt URB.
647 */
648void usb_wakeup_notification(struct usb_device *hdev,
649 unsigned int portnum)
650{
651 struct usb_hub *hub;
652
653 if (!hdev)
654 return;
655
656 hub = hdev_to_hub(hdev);
657 if (hub) {
658 set_bit(portnum, hub->wakeup_bits);
659 kick_khubd(hub);
660 }
661}
662EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100665static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200667 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400668 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100669 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 unsigned long bits;
671
Alan Sterne0152682007-08-24 15:42:52 -0400672 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 case -ENOENT: /* synchronous unlink */
674 case -ECONNRESET: /* async unlink */
675 case -ESHUTDOWN: /* hardware going away */
676 return;
677
678 default: /* presumably an error */
679 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400680 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if ((++hub->nerrors < 10) || hub->error)
682 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400683 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* let khubd handle things */
687 case 0: /* we got data: port status changed */
688 bits = 0;
689 for (i = 0; i < urb->actual_length; ++i)
690 bits |= ((unsigned long) ((*hub->buffer)[i]))
691 << (i*8);
692 hub->event_bits[0] = bits;
693 break;
694 }
695
696 hub->nerrors = 0;
697
698 /* Something happened, let khubd figure it out */
699 kick_khubd(hub);
700
701resubmit:
702 if (hub->quiescing)
703 return;
704
705 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
706 && status != -ENODEV && status != -EPERM)
707 dev_err (hub->intfdev, "resubmit --> %d\n", status);
708}
709
710/* USB 2.0 spec Section 11.24.2.3 */
711static inline int
712hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
713{
Alan Sternc2f65952009-11-18 11:37:15 -0500714 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
716 tt, NULL, 0, 1000);
717}
718
719/*
720 * enumeration blocks khubd for a long time. we use keventd instead, since
721 * long blocking there is the exception, not the rule. accordingly, HCDs
722 * talking to TTs must queue control transfers (not just bulk and iso), so
723 * both can talk to the same hub concurrently.
724 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400725static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
David Howellsc4028952006-11-22 14:57:56 +0000727 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400728 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400730 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 spin_lock_irqsave (&hub->tt.lock, flags);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400733 while (--limit && !list_empty (&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400734 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct usb_tt_clear *clear;
736 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400737 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 int status;
739
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400740 next = hub->tt.clear_list.next;
741 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 list_del (&clear->clear_list);
743
744 /* drop lock so HCD can concurrently report other TT errors */
745 spin_unlock_irqrestore (&hub->tt.lock, flags);
746 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (status)
748 dev_err (&hdev->dev,
749 "clear tt %d (%04x) error %d\n",
750 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400751
752 /* Tell the HCD, even if the operation failed */
753 drv = clear->hcd->driver;
754 if (drv->clear_tt_buffer_complete)
755 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
756
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700757 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400758 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760 spin_unlock_irqrestore (&hub->tt.lock, flags);
761}
762
763/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400764 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
765 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 *
767 * High speed HCDs use this to tell the hub driver that some split control or
768 * bulk transaction failed in a way that requires clearing internal state of
769 * a transaction translator. This is normally detected (and reported) from
770 * interrupt context.
771 *
772 * It may not be possible for that hub to handle additional full (or low)
773 * speed transactions until that state is fully cleared out.
774 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400775int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400777 struct usb_device *udev = urb->dev;
778 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct usb_tt *tt = udev->tt;
780 unsigned long flags;
781 struct usb_tt_clear *clear;
782
783 /* we've got to cope with an arbitrary number of pending TT clears,
784 * since each TT has "at least two" buffers that can need it (and
785 * there can be many TTs per hub). even if they're uncommon.
786 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800787 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
789 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400790 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 /* info that CLEAR_TT_BUFFER needs */
794 clear->tt = tt->multi ? udev->ttport : 1;
795 clear->devinfo = usb_pipeendpoint (pipe);
796 clear->devinfo |= udev->devnum << 4;
797 clear->devinfo |= usb_pipecontrol (pipe)
798 ? (USB_ENDPOINT_XFER_CONTROL << 11)
799 : (USB_ENDPOINT_XFER_BULK << 11);
800 if (usb_pipein (pipe))
801 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400802
803 /* info for completion callback */
804 clear->hcd = bus_to_hcd(udev->bus);
805 clear->ep = urb->ep;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /* tell keventd to clear state for this TT */
808 spin_lock_irqsave (&tt->lock, flags);
809 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400810 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400814EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Alan Stern8520f382008-09-22 14:44:26 -0400816/* If do_delay is false, return the number of milliseconds the caller
817 * needs to delay.
818 */
819static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700822 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400823 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400824 u16 wHubCharacteristics =
825 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Alan Stern4489a572006-04-27 15:54:22 -0400827 /* Enable power on each port. Some hubs have reserved values
828 * of LPSM (> 2) in their descriptors, even though they are
829 * USB 2.0 hubs. Some hubs do not implement port-power switching
830 * but only emulate it. In all cases, the ports won't work
831 * unless we send these messages to the hub.
832 */
833 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400835 else
836 dev_dbg(hub->intfdev, "trying to enable port power on "
837 "non-switchable hub\n");
838 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
839 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
David Brownellb7896962005-08-31 10:41:44 -0700841 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400842 delay = max(pgood_delay, (unsigned) 100);
843 if (do_delay)
844 msleep(delay);
845 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848static int hub_hub_status(struct usb_hub *hub,
849 u16 *status, u16 *change)
850{
851 int ret;
852
Alan Sterndb90e7a2007-02-05 09:56:15 -0500853 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 ret = get_hub_status(hub->hdev, &hub->status->hub);
855 if (ret < 0)
856 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800857 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 else {
859 *status = le16_to_cpu(hub->status->hub.wHubStatus);
860 *change = le16_to_cpu(hub->status->hub.wHubChange);
861 ret = 0;
862 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500863 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return ret;
865}
866
Alan Stern8b28c752005-08-10 17:04:13 -0400867static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
868{
869 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400870 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400871
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -0700872 if (hdev->children[port1-1] && set_state)
873 usb_set_device_state(hdev->children[port1-1],
Alan Stern8b28c752005-08-10 17:04:13 -0400874 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700875 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400876 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400877 if (ret)
878 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400879 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400880 return ret;
881}
882
Alan Stern0458d5b2007-05-04 11:52:20 -0400883/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800884 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400885 * time later khubd will disconnect() any existing usb_device on the port
886 * and will re-enumerate if there actually is a device attached.
887 */
888static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
889{
890 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
891 hub_port_disable(hub, port1, 1);
892
893 /* FIXME let caller ask to power down the port:
894 * - some devices won't enumerate without a VBUS power cycle
895 * - SRP saves power that way
896 * - ... new call, TBD ...
897 * That's easy if this hub can switch power per-port, and
898 * khubd reactivates the port later (timer, SRP, etc).
899 * Powerdown must be optional, because of reset/DFU.
900 */
901
902 set_bit(port1, hub->change_bits);
903 kick_khubd(hub);
904}
905
Alan Stern253e0572009-10-27 15:20:13 -0400906/**
907 * usb_remove_device - disable a device's port on its parent hub
908 * @udev: device to be disabled and removed
909 * Context: @udev locked, must be able to sleep.
910 *
911 * After @udev's port has been disabled, khubd is notified and it will
912 * see that the device has been disconnected. When the device is
913 * physically unplugged and something is plugged in, the events will
914 * be received and processed normally.
915 */
916int usb_remove_device(struct usb_device *udev)
917{
918 struct usb_hub *hub;
919 struct usb_interface *intf;
920
921 if (!udev->parent) /* Can't remove a root hub */
922 return -EINVAL;
923 hub = hdev_to_hub(udev->parent);
924 intf = to_usb_interface(hub->intfdev);
925
926 usb_autopm_get_interface(intf);
927 set_bit(udev->portnum, hub->removed_bits);
928 hub_port_logical_disconnect(hub, udev->portnum);
929 usb_autopm_put_interface(intf);
930 return 0;
931}
932
Alan Stern6ee0b272008-04-28 11:06:42 -0400933enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500934 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400935 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400936};
Alan Stern5e6effa2008-03-03 15:15:51 -0500937
Alan Stern8520f382008-09-22 14:44:26 -0400938static void hub_init_func2(struct work_struct *ws);
939static void hub_init_func3(struct work_struct *ws);
940
Alan Sternf2835212008-04-28 11:07:17 -0400941static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500942{
943 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800944 struct usb_hcd *hcd;
945 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500946 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400947 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400948 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400949 unsigned delay;
950
951 /* Continue a partial initialization */
952 if (type == HUB_INIT2)
953 goto init2;
954 if (type == HUB_INIT3)
955 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500956
Elric Fua45aa3b2012-02-18 13:32:27 +0800957 /* The superspeed hub except for root hub has to use Hub Depth
958 * value as an offset into the route string to locate the bits
959 * it uses to determine the downstream port number. So hub driver
960 * should send a set hub depth request to superspeed hub after
961 * the superspeed hub is set configuration in initialization or
962 * reset procedure.
963 *
964 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400965 * For any other type of activation, turn it on.
966 */
Alan Stern8520f382008-09-22 14:44:26 -0400967 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800968 if (hdev->parent && hub_is_superspeed(hdev)) {
969 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
970 HUB_SET_DEPTH, USB_RT_HUB,
971 hdev->level - 1, 0, NULL, 0,
972 USB_CTRL_SET_TIMEOUT);
973 if (ret < 0)
974 dev_err(hub->intfdev,
975 "set hub depth failed\n");
976 }
Alan Stern8520f382008-09-22 14:44:26 -0400977
978 /* Speed up system boot by using a delayed_work for the
979 * hub's initial power-up delays. This is pretty awkward
980 * and the implementation looks like a home-brewed sort of
981 * setjmp/longjmp, but it saves at least 100 ms for each
982 * root hub (assuming usbcore is compiled into the kernel
983 * rather than as a module). It adds up.
984 *
985 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
986 * because for those activation types the ports have to be
987 * operational when we return. In theory this could be done
988 * for HUB_POST_RESET, but it's easier not to.
989 */
990 if (type == HUB_INIT) {
991 delay = hub_power_on(hub, false);
992 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
993 schedule_delayed_work(&hub->init_work,
994 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -0400995
996 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -0500997 usb_autopm_get_interface_no_resume(
998 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -0400999 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -08001000 } else if (type == HUB_RESET_RESUME) {
1001 /* The internal host controller state for the hub device
1002 * may be gone after a host power loss on system resume.
1003 * Update the device's info so the HW knows it's a hub.
1004 */
1005 hcd = bus_to_hcd(hdev->bus);
1006 if (hcd->driver->update_hub_device) {
1007 ret = hcd->driver->update_hub_device(hcd, hdev,
1008 &hub->tt, GFP_NOIO);
1009 if (ret < 0) {
1010 dev_err(hub->intfdev, "Host not "
1011 "accepting hub info "
1012 "update.\n");
1013 dev_err(hub->intfdev, "LS/FS devices "
1014 "and hubs may not work "
1015 "under this hub\n.");
1016 }
1017 }
1018 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -04001019 } else {
1020 hub_power_on(hub, true);
1021 }
1022 }
1023 init2:
Alan Sternf2835212008-04-28 11:07:17 -04001024
Alan Stern6ee0b272008-04-28 11:06:42 -04001025 /* Check each port and set hub->change_bits to let khubd know
1026 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -05001027 */
1028 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001029 struct usb_device *udev = hdev->children[port1-1];
Alan Stern5e6effa2008-03-03 15:15:51 -05001030 u16 portstatus, portchange;
1031
Alan Stern6ee0b272008-04-28 11:06:42 -04001032 portstatus = portchange = 0;
1033 status = hub_port_status(hub, port1, &portstatus, &portchange);
1034 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1035 dev_dbg(hub->intfdev,
1036 "port %d: status %04x change %04x\n",
1037 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -05001038
Alan Stern6ee0b272008-04-28 11:06:42 -04001039 /* After anything other than HUB_RESUME (i.e., initialization
1040 * or any sort of reset), every port should be disabled.
1041 * Unconnected ports should likewise be disabled (paranoia),
1042 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -05001043 */
Alan Stern6ee0b272008-04-28 11:06:42 -04001044 if ((portstatus & USB_PORT_STAT_ENABLE) && (
1045 type != HUB_RESUME ||
1046 !(portstatus & USB_PORT_STAT_CONNECTION) ||
1047 !udev ||
1048 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001049 /*
1050 * USB3 protocol ports will automatically transition
1051 * to Enabled state when detect an USB3.0 device attach.
1052 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001053 */
Sarah Sharp131dec32010-12-06 21:00:19 -08001054 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001055 clear_port_feature(hdev, port1,
1056 USB_PORT_FEAT_ENABLE);
1057 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -07001058 } else {
1059 /* Pretend that power was lost for USB3 devs */
1060 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +08001061 }
Alan Stern6ee0b272008-04-28 11:06:42 -04001062 }
1063
Alan Stern948fea32008-04-28 11:07:07 -04001064 /* Clear status-change flags; we'll debounce later */
1065 if (portchange & USB_PORT_STAT_C_CONNECTION) {
1066 need_debounce_delay = true;
1067 clear_port_feature(hub->hdev, port1,
1068 USB_PORT_FEAT_C_CONNECTION);
1069 }
1070 if (portchange & USB_PORT_STAT_C_ENABLE) {
1071 need_debounce_delay = true;
1072 clear_port_feature(hub->hdev, port1,
1073 USB_PORT_FEAT_C_ENABLE);
1074 }
Don Zickus79c3dd82011-11-03 09:07:18 -04001075 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
1076 hub_is_superspeed(hub->hdev)) {
1077 need_debounce_delay = true;
1078 clear_port_feature(hub->hdev, port1,
1079 USB_PORT_FEAT_C_BH_PORT_RESET);
1080 }
Alan Stern253e0572009-10-27 15:20:13 -04001081 /* We can forget about a "removed" device when there's a
1082 * physical disconnect or the connect status changes.
1083 */
1084 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
1085 (portchange & USB_PORT_STAT_C_CONNECTION))
1086 clear_bit(port1, hub->removed_bits);
1087
Alan Stern6ee0b272008-04-28 11:06:42 -04001088 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
1089 /* Tell khubd to disconnect the device or
1090 * check for a new connection
1091 */
1092 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
1093 set_bit(port1, hub->change_bits);
1094
1095 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -08001096 bool port_resumed = (portstatus &
1097 USB_PORT_STAT_LINK_STATE) ==
1098 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -04001099 /* The power session apparently survived the resume.
1100 * If there was an overcurrent or suspend change
1101 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -08001102 * take care of it. Look at the port link state
1103 * for USB 3.0 hubs, since they don't have a suspend
1104 * change bit, and they don't set the port link change
1105 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -04001106 */
Sarah Sharp72937e12012-01-24 11:46:50 -08001107 if (portchange || (hub_is_superspeed(hub->hdev) &&
1108 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -04001109 set_bit(port1, hub->change_bits);
1110
1111 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -04001112#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -05001113 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -04001114#endif
Alan Stern8808f002008-04-28 11:06:55 -04001115 set_bit(port1, hub->change_bits);
1116
Alan Stern6ee0b272008-04-28 11:06:42 -04001117 } else {
1118 /* The power session is gone; tell khubd */
1119 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
1120 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -05001121 }
Alan Stern5e6effa2008-03-03 15:15:51 -05001122 }
1123
Alan Stern948fea32008-04-28 11:07:07 -04001124 /* If no port-status-change flags were set, we don't need any
1125 * debouncing. If flags were set we can try to debounce the
1126 * ports all at once right now, instead of letting khubd do them
1127 * one at a time later on.
1128 *
1129 * If any port-status changes do occur during this delay, khubd
1130 * will see them later and handle them normally.
1131 */
Alan Stern8520f382008-09-22 14:44:26 -04001132 if (need_debounce_delay) {
1133 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -04001134
Alan Stern8520f382008-09-22 14:44:26 -04001135 /* Don't do a long sleep inside a workqueue routine */
1136 if (type == HUB_INIT2) {
1137 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
1138 schedule_delayed_work(&hub->init_work,
1139 msecs_to_jiffies(delay));
1140 return; /* Continues at init3: below */
1141 } else {
1142 msleep(delay);
1143 }
1144 }
1145 init3:
Alan Sternf2835212008-04-28 11:07:17 -04001146 hub->quiescing = 0;
1147
1148 status = usb_submit_urb(hub->urb, GFP_NOIO);
1149 if (status < 0)
1150 dev_err(hub->intfdev, "activate --> %d\n", status);
1151 if (hub->has_indicators && blinkenlights)
1152 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
1153
1154 /* Scan all ports that need attention */
1155 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001156
1157 /* Allow autosuspend if it was suppressed */
1158 if (type <= HUB_INIT3)
1159 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -05001160}
1161
Alan Stern8520f382008-09-22 14:44:26 -04001162/* Implement the continuations for the delays above */
1163static void hub_init_func2(struct work_struct *ws)
1164{
1165 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1166
1167 hub_activate(hub, HUB_INIT2);
1168}
1169
1170static void hub_init_func3(struct work_struct *ws)
1171{
1172 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
1173
1174 hub_activate(hub, HUB_INIT3);
1175}
1176
Alan Stern43303542008-04-28 11:07:31 -04001177enum hub_quiescing_type {
1178 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
1179};
1180
1181static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
1182{
1183 struct usb_device *hdev = hub->hdev;
1184 int i;
1185
Alan Stern8520f382008-09-22 14:44:26 -04001186 cancel_delayed_work_sync(&hub->init_work);
1187
Alan Stern43303542008-04-28 11:07:31 -04001188 /* khubd and related activity won't re-trigger */
1189 hub->quiescing = 1;
1190
1191 if (type != HUB_SUSPEND) {
1192 /* Disconnect all the children */
1193 for (i = 0; i < hdev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001194 if (hdev->children[i])
1195 usb_disconnect(&hdev->children[i]);
Alan Stern43303542008-04-28 11:07:31 -04001196 }
1197 }
1198
1199 /* Stop khubd and related activity */
1200 usb_kill_urb(hub->urb);
1201 if (hub->has_indicators)
1202 cancel_delayed_work_sync(&hub->leds);
1203 if (hub->tt.hub)
Alan Sterncb88a1b2009-06-29 10:43:32 -04001204 cancel_work_sync(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001205}
1206
Alan Stern3eb14912008-03-03 15:15:43 -05001207/* caller has locked the hub device */
1208static int hub_pre_reset(struct usb_interface *intf)
1209{
1210 struct usb_hub *hub = usb_get_intfdata(intf);
1211
Alan Stern43303542008-04-28 11:07:31 -04001212 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001213 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001214}
1215
1216/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001217static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001218{
Alan Stern7de18d82006-06-01 13:37:24 -04001219 struct usb_hub *hub = usb_get_intfdata(intf);
1220
Alan Sternf2835212008-04-28 11:07:17 -04001221 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001222 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225static int hub_configure(struct usb_hub *hub,
1226 struct usb_endpoint_descriptor *endpoint)
1227{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001228 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct usb_device *hdev = hub->hdev;
1230 struct device *hub_dev = hub->intfdev;
1231 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001232 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned int pipe;
1234 int maxp, ret;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001235 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Alan Sternd697cdd2009-10-27 15:18:46 -04001237 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 ret = -ENOMEM;
1240 goto fail;
1241 }
1242
1243 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1244 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 ret = -ENOMEM;
1246 goto fail;
1247 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001248 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1251 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 ret = -ENOMEM;
1253 goto fail;
1254 }
1255
1256 /* Request the entire hub descriptor.
1257 * hub->descriptor can handle USB_MAXCHILDREN ports,
1258 * but the hub can/will return fewer bytes here.
1259 */
John Youndbe79bb2001-09-17 00:00:00 -07001260 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (ret < 0) {
1262 message = "can't read hub descriptor";
1263 goto fail;
1264 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1265 message = "hub has too many ports!";
1266 ret = -ENODEV;
1267 goto fail;
1268 }
1269
1270 hdev->maxchild = hub->descriptor->bNbrPorts;
1271 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1272 (hdev->maxchild == 1) ? "" : "s");
1273
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001274 hdev->children = kzalloc(hdev->maxchild *
1275 sizeof(struct usb_device *), GFP_KERNEL);
Lan Tianyu336c5c32012-07-06 14:13:52 +08001276 hub->port_owners = kzalloc(hdev->maxchild * sizeof(struct dev_state *),
1277 GFP_KERNEL);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001278 if (!hdev->children || !hub->port_owners) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001279 ret = -ENOMEM;
1280 goto fail;
1281 }
1282
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001283 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
John Youndbe79bb2001-09-17 00:00:00 -07001285 /* FIXME for USB 3.0, skip for now */
1286 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1287 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 int i;
1289 char portstr [USB_MAXCHILDREN + 1];
1290
1291 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001292 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1294 ? 'F' : 'R';
1295 portstr[hdev->maxchild] = 0;
1296 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1297 } else
1298 dev_dbg(hub_dev, "standalone hub\n");
1299
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001300 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301301 case HUB_CHAR_COMMON_LPSM:
1302 dev_dbg(hub_dev, "ganged power switching\n");
1303 break;
1304 case HUB_CHAR_INDV_PORT_LPSM:
1305 dev_dbg(hub_dev, "individual port power switching\n");
1306 break;
1307 case HUB_CHAR_NO_LPSM:
1308 case HUB_CHAR_LPSM:
1309 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001313 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301314 case HUB_CHAR_COMMON_OCPM:
1315 dev_dbg(hub_dev, "global over-current protection\n");
1316 break;
1317 case HUB_CHAR_INDV_PORT_OCPM:
1318 dev_dbg(hub_dev, "individual port over-current protection\n");
1319 break;
1320 case HUB_CHAR_NO_OCPM:
1321 case HUB_CHAR_OCPM:
1322 dev_dbg(hub_dev, "no over-current protection\n");
1323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325
1326 spin_lock_init (&hub->tt.lock);
1327 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001328 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301330 case USB_HUB_PR_FS:
1331 break;
1332 case USB_HUB_PR_HS_SINGLE_TT:
1333 dev_dbg(hub_dev, "Single TT\n");
1334 hub->tt.hub = hdev;
1335 break;
1336 case USB_HUB_PR_HS_MULTI_TT:
1337 ret = usb_set_interface(hdev, 0, 1);
1338 if (ret == 0) {
1339 dev_dbg(hub_dev, "TT per port\n");
1340 hub->tt.multi = 1;
1341 } else
1342 dev_err(hub_dev, "Using single TT (err %d)\n",
1343 ret);
1344 hub->tt.hub = hdev;
1345 break;
1346 case USB_HUB_PR_SS:
1347 /* USB 3.0 hubs don't have a TT */
1348 break;
1349 default:
1350 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1351 hdev->descriptor.bDeviceProtocol);
1352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
1354
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001355 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001356 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001357 case HUB_TTTT_8_BITS:
1358 if (hdev->descriptor.bDeviceProtocol != 0) {
1359 hub->tt.think_time = 666;
1360 dev_dbg(hub_dev, "TT requires at most %d "
1361 "FS bit times (%d ns)\n",
1362 8, hub->tt.think_time);
1363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001365 case HUB_TTTT_16_BITS:
1366 hub->tt.think_time = 666 * 2;
1367 dev_dbg(hub_dev, "TT requires at most %d "
1368 "FS bit times (%d ns)\n",
1369 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001371 case HUB_TTTT_24_BITS:
1372 hub->tt.think_time = 666 * 3;
1373 dev_dbg(hub_dev, "TT requires at most %d "
1374 "FS bit times (%d ns)\n",
1375 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001377 case HUB_TTTT_32_BITS:
1378 hub->tt.think_time = 666 * 4;
1379 dev_dbg(hub_dev, "TT requires at most %d "
1380 "FS bit times (%d ns)\n",
1381 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
1383 }
1384
1385 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001386 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 hub->has_indicators = 1;
1388 dev_dbg(hub_dev, "Port indicators are supported\n");
1389 }
1390
1391 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1392 hub->descriptor->bPwrOn2PwrGood * 2);
1393
1394 /* power budgeting mostly matters with bus-powered hubs,
1395 * and battery-powered root hubs (may provide just 8 mA).
1396 */
1397 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001398 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 message = "can't get hub status";
1400 goto fail;
1401 }
Alan Stern7d35b922005-04-25 11:18:32 -04001402 le16_to_cpus(&hubstatus);
1403 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001404 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1405 hub->mA_per_port = 500;
1406 else {
1407 hub->mA_per_port = hdev->bus_mA;
1408 hub->limited_power = 1;
1409 }
Alan Stern7d35b922005-04-25 11:18:32 -04001410 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1412 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001413 hub->limited_power = 1;
1414 if (hdev->maxchild > 0) {
1415 int remaining = hdev->bus_mA -
1416 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Alan Stern55c52712005-11-23 12:03:12 -05001418 if (remaining < hdev->maxchild * 100)
1419 dev_warn(hub_dev,
1420 "insufficient power available "
1421 "to use all downstream ports\n");
1422 hub->mA_per_port = 100; /* 7.2.1.1 */
1423 }
1424 } else { /* Self-powered external hub */
1425 /* FIXME: What about battery-powered external hubs that
1426 * provide less current per port? */
1427 hub->mA_per_port = 500;
1428 }
1429 if (hub->mA_per_port < 500)
1430 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1431 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001433 /* Update the HCD's internal representation of this hub before khubd
1434 * starts getting port status changes for devices under the hub.
1435 */
1436 hcd = bus_to_hcd(hdev->bus);
1437 if (hcd->driver->update_hub_device) {
1438 ret = hcd->driver->update_hub_device(hcd, hdev,
1439 &hub->tt, GFP_KERNEL);
1440 if (ret < 0) {
1441 message = "can't update HCD hub info";
1442 goto fail;
1443 }
1444 }
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1447 if (ret < 0) {
1448 message = "can't get hub status";
1449 goto fail;
1450 }
1451
1452 /* local power status reports aren't always correct */
1453 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1454 dev_dbg(hub_dev, "local power source is %s\n",
1455 (hubstatus & HUB_STATUS_LOCAL_POWER)
1456 ? "lost (inactive)" : "good");
1457
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001458 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 dev_dbg(hub_dev, "%sover-current condition exists\n",
1460 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1461
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001462 /* set up the interrupt endpoint
1463 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1464 * bytes as USB2.0[11.12.3] says because some hubs are known
1465 * to send more data (and thus cause overflow). For root hubs,
1466 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1467 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1469 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1470
1471 if (maxp > sizeof(*hub->buffer))
1472 maxp = sizeof(*hub->buffer);
1473
1474 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1475 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 ret = -ENOMEM;
1477 goto fail;
1478 }
1479
1480 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1481 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 /* maybe cycle the hub leds */
1484 if (hub->has_indicators && blinkenlights)
1485 hub->indicator [0] = INDICATOR_CYCLE;
1486
Alan Sternf2835212008-04-28 11:07:17 -04001487 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return 0;
1489
1490fail:
1491 dev_err (hub_dev, "config failed, %s (err %d)\n",
1492 message, ret);
1493 /* hub_disconnect() frees urb and descriptor */
1494 return ret;
1495}
1496
Alan Sterne8054852007-05-04 11:55:11 -04001497static void hub_release(struct kref *kref)
1498{
1499 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1500
1501 usb_put_intf(to_usb_interface(hub->intfdev));
1502 kfree(hub);
1503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505static unsigned highspeed_hubs;
1506
1507static void hub_disconnect(struct usb_interface *intf)
1508{
Huajun Li88162302012-03-12 21:00:19 +08001509 struct usb_hub *hub = usb_get_intfdata(intf);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001510 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Sterne8054852007-05-04 11:55:11 -04001511
1512 /* Take the hub off the event list and don't let it be added again */
1513 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001514 if (!list_empty(&hub->event_list)) {
1515 list_del_init(&hub->event_list);
1516 usb_autopm_put_interface_no_suspend(intf);
1517 }
Alan Sterne8054852007-05-04 11:55:11 -04001518 hub->disconnected = 1;
1519 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Alan Stern7de18d82006-06-01 13:37:24 -04001521 /* Disconnect all children and quiesce the hub */
1522 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001523 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001524
Alan Stern8b28c752005-08-10 17:04:13 -04001525 usb_set_intfdata (intf, NULL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001526 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Alan Sterne8054852007-05-04 11:55:11 -04001528 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 highspeed_hubs--;
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 usb_free_urb(hub->urb);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001532 kfree(hdev->children);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001533 kfree(hub->port_owners);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001534 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001535 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001536 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Alan Sterne8054852007-05-04 11:55:11 -04001538 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
1541static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1542{
1543 struct usb_host_interface *desc;
1544 struct usb_endpoint_descriptor *endpoint;
1545 struct usb_device *hdev;
1546 struct usb_hub *hub;
1547
1548 desc = intf->cur_altsetting;
1549 hdev = interface_to_usbdev(intf);
1550
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001551 /* Hubs have proper suspend/resume support. */
1552 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001553
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001554 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001555 dev_err(&intf->dev,
1556 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001557 return -E2BIG;
1558 }
1559
David Brownell89ccbdc2006-04-02 10:18:09 -08001560#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1561 if (hdev->parent) {
1562 dev_warn(&intf->dev, "ignoring external hub\n");
1563 return -ENODEV;
1564 }
1565#endif
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* Some hubs have a subclass of 1, which AFAICT according to the */
1568 /* specs is not defined, but it works */
1569 if ((desc->desc.bInterfaceSubClass != 0) &&
1570 (desc->desc.bInterfaceSubClass != 1)) {
1571descriptor_error:
1572 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1573 return -EIO;
1574 }
1575
1576 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1577 if (desc->desc.bNumEndpoints != 1)
1578 goto descriptor_error;
1579
1580 endpoint = &desc->endpoint[0].desc;
1581
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001582 /* If it's not an interrupt in endpoint, we'd better punt! */
1583 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 goto descriptor_error;
1585
1586 /* We found a hub */
1587 dev_info (&intf->dev, "USB hub found\n");
1588
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001589 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (!hub) {
1591 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1592 return -ENOMEM;
1593 }
1594
Alan Sterne8054852007-05-04 11:55:11 -04001595 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 INIT_LIST_HEAD(&hub->event_list);
1597 hub->intfdev = &intf->dev;
1598 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001599 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001600 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001601 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001604 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 if (hdev->speed == USB_SPEED_HIGH)
1607 highspeed_hubs++;
1608
1609 if (hub_configure(hub, endpoint) >= 0)
1610 return 0;
1611
1612 hub_disconnect (intf);
1613 return -ENODEV;
1614}
1615
1616static int
1617hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1618{
1619 struct usb_device *hdev = interface_to_usbdev (intf);
1620
1621 /* assert ifno == 0 (part of hub spec) */
1622 switch (code) {
1623 case USBDEVFS_HUB_PORTINFO: {
1624 struct usbdevfs_hub_portinfo *info = user_data;
1625 int i;
1626
1627 spin_lock_irq(&device_state_lock);
1628 if (hdev->devnum <= 0)
1629 info->nports = 0;
1630 else {
1631 info->nports = hdev->maxchild;
1632 for (i = 0; i < info->nports; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001633 if (hdev->children[i] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 info->port[i] = 0;
1635 else
1636 info->port[i] =
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001637 hdev->children[i]->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639 }
1640 spin_unlock_irq(&device_state_lock);
1641
1642 return info->nports + 1;
1643 }
1644
1645 default:
1646 return -ENOSYS;
1647 }
1648}
1649
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001650/*
1651 * Allow user programs to claim ports on a hub. When a device is attached
1652 * to one of these "claimed" ports, the program will "own" the device.
1653 */
1654static int find_port_owner(struct usb_device *hdev, unsigned port1,
Lan Tianyu336c5c32012-07-06 14:13:52 +08001655 struct dev_state ***ppowner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001656{
1657 if (hdev->state == USB_STATE_NOTATTACHED)
1658 return -ENODEV;
1659 if (port1 == 0 || port1 > hdev->maxchild)
1660 return -EINVAL;
1661
1662 /* This assumes that devices not managed by the hub driver
1663 * will always have maxchild equal to 0.
1664 */
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001665 *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001666 return 0;
1667}
1668
1669/* In the following three functions, the caller must hold hdev's lock */
Lan Tianyu336c5c32012-07-06 14:13:52 +08001670int usb_hub_claim_port(struct usb_device *hdev, unsigned port1,
1671 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001672{
1673 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001674 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001675
1676 rc = find_port_owner(hdev, port1, &powner);
1677 if (rc)
1678 return rc;
1679 if (*powner)
1680 return -EBUSY;
1681 *powner = owner;
1682 return rc;
1683}
1684
Lan Tianyu336c5c32012-07-06 14:13:52 +08001685int usb_hub_release_port(struct usb_device *hdev, unsigned port1,
1686 struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001687{
1688 int rc;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001689 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001690
1691 rc = find_port_owner(hdev, port1, &powner);
1692 if (rc)
1693 return rc;
1694 if (*powner != owner)
1695 return -ENOENT;
1696 *powner = NULL;
1697 return rc;
1698}
1699
Lan Tianyu336c5c32012-07-06 14:13:52 +08001700void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner)
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001701{
1702 int n;
Lan Tianyu336c5c32012-07-06 14:13:52 +08001703 struct dev_state **powner;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001704
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001705 n = find_port_owner(hdev, 1, &powner);
1706 if (n == 0) {
1707 for (; n < hdev->maxchild; (++n, ++powner)) {
1708 if (*powner == owner)
1709 *powner = NULL;
1710 }
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001711 }
1712}
1713
1714/* The caller must hold udev's lock */
1715bool usb_device_is_owned(struct usb_device *udev)
1716{
1717 struct usb_hub *hub;
1718
1719 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1720 return false;
1721 hub = hdev_to_hub(udev->parent);
Greg Kroah-Hartman304f0b22012-05-14 09:22:58 -07001722 return !!hub->port_owners[udev->portnum - 1];
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001723}
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1727{
1728 int i;
1729
1730 for (i = 0; i < udev->maxchild; ++i) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001731 if (udev->children[i])
1732 recursively_mark_NOTATTACHED(udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001734 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001735 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 udev->state = USB_STATE_NOTATTACHED;
1737}
1738
1739/**
1740 * usb_set_device_state - change a device's current state (usbcore, hcds)
1741 * @udev: pointer to device whose state should be changed
1742 * @new_state: new state value to be stored
1743 *
1744 * udev->state is _not_ fully protected by the device lock. Although
1745 * most transitions are made only while holding the lock, the state can
1746 * can change to USB_STATE_NOTATTACHED at almost any time. This
1747 * is so that devices can be marked as disconnected as soon as possible,
1748 * without having to wait for any semaphores to be released. As a result,
1749 * all changes to any device's state must be protected by the
1750 * device_state_lock spinlock.
1751 *
1752 * Once a device has been added to the device tree, all changes to its state
1753 * should be made using this routine. The state should _not_ be set directly.
1754 *
1755 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1756 * Otherwise udev->state is set to new_state, and if new_state is
1757 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1758 * to USB_STATE_NOTATTACHED.
1759 */
1760void usb_set_device_state(struct usb_device *udev,
1761 enum usb_device_state new_state)
1762{
1763 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001764 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 spin_lock_irqsave(&device_state_lock, flags);
1767 if (udev->state == USB_STATE_NOTATTACHED)
1768 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001769 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001770
1771 /* root hub wakeup capabilities are managed out-of-band
1772 * and may involve silicon errata ... ignore them here.
1773 */
1774 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001775 if (udev->state == USB_STATE_SUSPENDED
1776 || new_state == USB_STATE_SUSPENDED)
1777 ; /* No change to wakeup settings */
1778 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001779 wakeup = udev->actconfig->desc.bmAttributes
1780 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001781 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001782 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001783 }
Sarah Sharp15123002007-12-21 16:54:15 -08001784 if (udev->state == USB_STATE_SUSPENDED &&
1785 new_state != USB_STATE_SUSPENDED)
1786 udev->active_duration -= jiffies;
1787 else if (new_state == USB_STATE_SUSPENDED &&
1788 udev->state != USB_STATE_SUSPENDED)
1789 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001790 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001791 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 recursively_mark_NOTATTACHED(udev);
1793 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001794 if (wakeup >= 0)
1795 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
David Vrabel6da9c992009-02-18 14:43:47 +00001797EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001799/*
Alan Stern3b29b682011-02-22 09:53:41 -05001800 * Choose a device number.
1801 *
1802 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1803 * USB-2.0 buses they are also used as device addresses, however on
1804 * USB-3.0 buses the address is assigned by the controller hardware
1805 * and it usually is not the same as the device number.
1806 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001807 * WUSB devices are simple: they have no hubs behind, so the mapping
1808 * device <-> virtual port number becomes 1:1. Why? to simplify the
1809 * life of the device connection logic in
1810 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1811 * handshake we need to assign a temporary address in the unauthorized
1812 * space. For simplicity we use the first virtual port number found to
1813 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1814 * and that becomes it's address [X < 128] or its unauthorized address
1815 * [X | 0x80].
1816 *
1817 * We add 1 as an offset to the one-based USB-stack port number
1818 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1819 * 0 is reserved by USB for default address; (b) Linux's USB stack
1820 * uses always #1 for the root hub of the controller. So USB stack's
1821 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001822 *
1823 * Devices connected under xHCI are not as simple. The host controller
1824 * supports virtualization, so the hardware assigns device addresses and
1825 * the HCD must setup data structures before issuing a set address
1826 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001827 */
Alan Stern3b29b682011-02-22 09:53:41 -05001828static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829{
1830 int devnum;
1831 struct usb_bus *bus = udev->bus;
1832
1833 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001834 if (udev->wusb) {
1835 devnum = udev->portnum + 1;
1836 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1837 } else {
1838 /* Try to allocate the next devnum beginning at
1839 * bus->devnum_next. */
1840 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1841 bus->devnum_next);
1842 if (devnum >= 128)
1843 devnum = find_next_zero_bit(bus->devmap.devicemap,
1844 128, 1);
1845 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (devnum < 128) {
1848 set_bit(devnum, bus->devmap.devicemap);
1849 udev->devnum = devnum;
1850 }
1851}
1852
Alan Stern3b29b682011-02-22 09:53:41 -05001853static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
1855 if (udev->devnum > 0) {
1856 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1857 udev->devnum = -1;
1858 }
1859}
1860
Alan Stern3b29b682011-02-22 09:53:41 -05001861static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001862{
1863 /* The address for a WUSB device is managed by wusbcore. */
1864 if (!udev->wusb)
1865 udev->devnum = devnum;
1866}
1867
Herbert Xuf7410ce2010-01-10 20:15:03 +11001868static void hub_free_dev(struct usb_device *udev)
1869{
1870 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1871
1872 /* Root hubs aren't real devices, so don't free HCD resources */
1873 if (hcd->driver->free_dev && udev->parent)
1874 hcd->driver->free_dev(hcd, udev);
1875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877/**
1878 * usb_disconnect - disconnect a device (usbcore-internal)
1879 * @pdev: pointer to device being disconnected
1880 * Context: !in_interrupt ()
1881 *
1882 * Something got disconnected. Get rid of it and all of its children.
1883 *
1884 * If *pdev is a normal device then the parent hub must already be locked.
1885 * If *pdev is a root hub then this routine will acquire the
1886 * usb_bus_list_lock on behalf of the caller.
1887 *
1888 * Only hub drivers (including virtual root hub drivers for host
1889 * controllers) should ever call this.
1890 *
1891 * This call is synchronous, and may not be used in an interrupt context.
1892 */
1893void usb_disconnect(struct usb_device **pdev)
1894{
1895 struct usb_device *udev = *pdev;
1896 int i;
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 /* mark the device as inactive, so any further urb submissions for
1899 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001900 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 */
1902 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001903 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1904 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001906 usb_lock_device(udev);
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001909 for (i = 0; i < udev->maxchild; i++) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07001910 if (udev->children[i])
1911 usb_disconnect(&udev->children[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
1914 /* deallocate hcd/hardware state ... nuking all pending urbs and
1915 * cleaning up all state associated with the current configuration
1916 * so that the hardware is now fully quiesced.
1917 */
Alan Stern782da722006-07-01 22:09:35 -04001918 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001920 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Alan Stern3b23dd62008-12-05 14:10:34 -05001922 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001923 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001924
Alan Stern782da722006-07-01 22:09:35 -04001925 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001926 * for de-configuring the device and invoking the remove-device
1927 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001928 */
1929 device_del(&udev->dev);
1930
1931 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 * (or root_hub) pointer.
1933 */
Alan Stern3b29b682011-02-22 09:53:41 -05001934 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 /* Avoid races with recursively_mark_NOTATTACHED() */
1937 spin_lock_irq(&device_state_lock);
1938 *pdev = NULL;
1939 spin_unlock_irq(&device_state_lock);
1940
Herbert Xuf7410ce2010-01-10 20:15:03 +11001941 hub_free_dev(udev);
1942
Alan Stern782da722006-07-01 22:09:35 -04001943 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001946#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947static void show_string(struct usb_device *udev, char *id, char *string)
1948{
1949 if (!string)
1950 return;
1951 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1952}
1953
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001954static void announce_device(struct usb_device *udev)
1955{
1956 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1957 le16_to_cpu(udev->descriptor.idVendor),
1958 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001959 dev_info(&udev->dev,
1960 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001961 udev->descriptor.iManufacturer,
1962 udev->descriptor.iProduct,
1963 udev->descriptor.iSerialNumber);
1964 show_string(udev, "Product", udev->product);
1965 show_string(udev, "Manufacturer", udev->manufacturer);
1966 show_string(udev, "SerialNumber", udev->serial);
1967}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001969static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970#endif
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972#ifdef CONFIG_USB_OTG
1973#include "otg_whitelist.h"
1974#endif
1975
Oliver Neukum3ede7602007-01-11 14:35:50 +01001976/**
Alan Stern8d8558d2009-12-08 15:50:41 -05001977 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01001978 * @udev: newly addressed device (in ADDRESS state)
1979 *
Alan Stern8d8558d2009-12-08 15:50:41 -05001980 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01001981 */
Alan Stern8d8558d2009-12-08 15:50:41 -05001982static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001984 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986#ifdef CONFIG_USB_OTG
1987 /*
1988 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1989 * to wake us after we've powered off VBUS; and HNP, switching roles
1990 * "host" to "peripheral". The OTG descriptor helps figure this out.
1991 */
1992 if (!udev->bus->is_b_host
1993 && udev->config
1994 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02001995 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 struct usb_bus *bus = udev->bus;
1997
1998 /* descriptor may appear anywhere in config */
1999 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
2000 le16_to_cpu(udev->config[0].desc.wTotalLength),
2001 USB_DT_OTG, (void **) &desc) == 0) {
2002 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05002003 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07002004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 dev_info(&udev->dev,
2006 "Dual-Role OTG device on %sHNP port\n",
2007 (port1 == bus->otg_port)
2008 ? "" : "non-");
2009
2010 /* enable HNP before suspend, it's simpler */
2011 if (port1 == bus->otg_port)
2012 bus->b_hnp_enable = 1;
2013 err = usb_control_msg(udev,
2014 usb_sndctrlpipe(udev, 0),
2015 USB_REQ_SET_FEATURE, 0,
2016 bus->b_hnp_enable
2017 ? USB_DEVICE_B_HNP_ENABLE
2018 : USB_DEVICE_A_ALT_HNP_SUPPORT,
2019 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2020 if (err < 0) {
2021 /* OTG MESSAGE: report errors here,
2022 * customize to match your product.
2023 */
2024 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002025 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 err);
2027 bus->b_hnp_enable = 0;
2028 }
2029 }
2030 }
2031 }
2032
2033 if (!is_targeted(udev)) {
2034
2035 /* Maybe it can talk to us, though we can't talk to it.
2036 * (Includes HNP test device.)
2037 */
2038 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
David Brownell634a84f2009-01-15 13:51:28 -08002039 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (err < 0)
2041 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
2042 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07002043 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 goto fail;
2045 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002046fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002048 return err;
2049}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002051
2052/**
Alan Stern8d8558d2009-12-08 15:50:41 -05002053 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002054 * @udev: newly addressed device (in ADDRESS state)
2055 *
2056 * This is only called by usb_new_device() and usb_authorize_device()
2057 * and FIXME -- all comments that apply to them apply here wrt to
2058 * environment.
2059 *
2060 * If the device is WUSB and not authorized, we don't attempt to read
2061 * the string descriptors, as they will be errored out by the device
2062 * until it has been authorized.
2063 */
Alan Stern8d8558d2009-12-08 15:50:41 -05002064static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002065{
2066 int err;
2067
2068 if (udev->config == NULL) {
2069 err = usb_get_configuration(udev);
2070 if (err < 0) {
2071 dev_err(&udev->dev, "can't read configurations, error %d\n",
2072 err);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002073 return err;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002074 }
2075 }
2076 if (udev->wusb == 1 && udev->authorized == 0) {
2077 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2078 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2079 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
2080 }
2081 else {
2082 /* read the standard strings and cache them if present */
2083 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
2084 udev->manufacturer = usb_cache_string(udev,
2085 udev->descriptor.iManufacturer);
2086 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
2087 }
Alan Stern8d8558d2009-12-08 15:50:41 -05002088 err = usb_enumerate_device_otg(udev);
Laurent Pinchart80da2e02012-07-19 12:39:13 +02002089 if (err < 0)
2090 return err;
2091
2092 usb_detect_interface_quirks(udev);
2093
2094 return 0;
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002095}
2096
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002097static void set_usb_port_removable(struct usb_device *udev)
2098{
2099 struct usb_device *hdev = udev->parent;
2100 struct usb_hub *hub;
2101 u8 port = udev->portnum;
2102 u16 wHubCharacteristics;
2103 bool removable = true;
2104
2105 if (!hdev)
2106 return;
2107
2108 hub = hdev_to_hub(udev->parent);
2109
2110 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
2111
2112 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
2113 return;
2114
2115 if (hub_is_superspeed(hdev)) {
2116 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
2117 removable = false;
2118 } else {
2119 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
2120 removable = false;
2121 }
2122
2123 if (removable)
2124 udev->removable = USB_DEVICE_REMOVABLE;
2125 else
2126 udev->removable = USB_DEVICE_FIXED;
2127}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002128
2129/**
2130 * usb_new_device - perform initial device setup (usbcore-internal)
2131 * @udev: newly addressed device (in ADDRESS state)
2132 *
Alan Stern8d8558d2009-12-08 15:50:41 -05002133 * This is called with devices which have been detected but not fully
2134 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002135 * for any device configuration. The caller must have locked either
2136 * the parent hub (if udev is a normal device) or else the
2137 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
2138 * udev has already been installed, but udev is not yet visible through
2139 * sysfs or other filesystem code.
2140 *
2141 * It will return if the device is configured properly or not. Zero if
2142 * the interface was registered with the driver core; else a negative
2143 * errno value.
2144 *
2145 * This call is synchronous, and may not be used in an interrupt context.
2146 *
2147 * Only the hub driver or root-hub registrar should ever call this.
2148 */
2149int usb_new_device(struct usb_device *udev)
2150{
2151 int err;
2152
Dan Streetman16985402010-01-06 09:56:53 -05002153 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002154 /* Initialize non-root-hub device wakeup to disabled;
2155 * device (un)configuration controls wakeup capable
2156 * sysfs power/wakeup controls wakeup enabled/disabled
2157 */
2158 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002159 }
2160
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002161 /* Tell the runtime-PM framework the device is active */
2162 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002163 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002164 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002165 pm_runtime_enable(&udev->dev);
2166
Alan Sternc08512c2010-11-15 15:57:58 -05002167 /* By default, forbid autosuspend for all devices. It will be
2168 * allowed for hubs during binding.
2169 */
2170 usb_disable_autosuspend(udev);
2171
Alan Stern8d8558d2009-12-08 15:50:41 -05002172 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002173 if (err < 0)
2174 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002175 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2176 udev->devnum, udev->bus->busnum,
2177 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002178 /* export the usbdev device-node for libusb */
2179 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2180 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2181
Alan Stern6cd13202008-11-13 15:08:30 -05002182 /* Tell the world! */
2183 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002184
Theodore Ts'ob04b3152012-07-04 11:22:20 -04002185 if (udev->serial)
2186 add_device_randomness(udev->serial, strlen(udev->serial));
2187 if (udev->product)
2188 add_device_randomness(udev->product, strlen(udev->product));
2189 if (udev->manufacturer)
2190 add_device_randomness(udev->manufacturer,
2191 strlen(udev->manufacturer));
2192
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002193 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002194
2195 /*
2196 * check whether the hub marks this port as non-removable. Do it
2197 * now so that platform-specific data can override it in
2198 * device_add()
2199 */
2200 if (udev->parent)
2201 set_usb_port_removable(udev);
2202
Alan Stern782da722006-07-01 22:09:35 -04002203 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002204 * for configuring the device and invoking the add-device
2205 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002206 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002207 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (err) {
2209 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2210 goto fail;
2211 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002212
Alan Stern3b23dd62008-12-05 14:10:34 -05002213 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002214 usb_mark_last_busy(udev);
2215 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002216 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218fail:
2219 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002220 pm_runtime_disable(&udev->dev);
2221 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002222 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002225
2226/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002227 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2228 * @usb_dev: USB device
2229 *
2230 * Move the USB device to a very basic state where interfaces are disabled
2231 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002232 *
2233 * We share a lock (that we have) with device_del(), so we need to
2234 * defer its call.
2235 */
2236int usb_deauthorize_device(struct usb_device *usb_dev)
2237{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002238 usb_lock_device(usb_dev);
2239 if (usb_dev->authorized == 0)
2240 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002241
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002242 usb_dev->authorized = 0;
2243 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002244
2245 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002246 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002247 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002248 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002249 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002250 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002251
2252 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002253 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002254
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002255out_unauthorized:
2256 usb_unlock_device(usb_dev);
2257 return 0;
2258}
2259
2260
2261int usb_authorize_device(struct usb_device *usb_dev)
2262{
2263 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002264
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002265 usb_lock_device(usb_dev);
2266 if (usb_dev->authorized == 1)
2267 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002268
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002269 result = usb_autoresume_device(usb_dev);
2270 if (result < 0) {
2271 dev_err(&usb_dev->dev,
2272 "can't autoresume for authorization: %d\n", result);
2273 goto error_autoresume;
2274 }
2275 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2276 if (result < 0) {
2277 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2278 "authorization: %d\n", result);
2279 goto error_device_descriptor;
2280 }
Alan Sternda307122009-12-08 15:54:44 -05002281
2282 kfree(usb_dev->product);
2283 usb_dev->product = NULL;
2284 kfree(usb_dev->manufacturer);
2285 usb_dev->manufacturer = NULL;
2286 kfree(usb_dev->serial);
2287 usb_dev->serial = NULL;
2288
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002289 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002290 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002291 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002292 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002293 /* Choose and set the configuration. This registers the interfaces
2294 * with the driver core and lets interface drivers bind to them.
2295 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002296 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002297 if (c >= 0) {
2298 result = usb_set_configuration(usb_dev, c);
2299 if (result) {
2300 dev_err(&usb_dev->dev,
2301 "can't set config #%d, error %d\n", c, result);
2302 /* This need not be fatal. The user can try to
2303 * set other configurations. */
2304 }
2305 }
2306 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002307
Alan Stern8d8558d2009-12-08 15:50:41 -05002308error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002309error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002310 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002311error_autoresume:
2312out_authorized:
2313 usb_unlock_device(usb_dev); // complements locktree
2314 return result;
2315}
2316
2317
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002318/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2319static unsigned hub_is_wusb(struct usb_hub *hub)
2320{
2321 struct usb_hcd *hcd;
2322 if (hub->hdev->parent != NULL) /* not a root hub? */
2323 return 0;
2324 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2325 return hcd->wireless;
2326}
2327
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329#define PORT_RESET_TRIES 5
2330#define SET_ADDRESS_TRIES 2
2331#define GET_DESCRIPTOR_TRIES 2
2332#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302333#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
2335#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2336#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002337#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338#define HUB_LONG_RESET_TIME 200
2339#define HUB_RESET_TIMEOUT 500
2340
Sarah Sharp10d674a2011-09-14 14:24:52 -07002341static int hub_port_reset(struct usb_hub *hub, int port1,
2342 struct usb_device *udev, unsigned int delay, bool warm);
2343
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002344/* Is a USB 3.0 port in the Inactive or Complinance Mode state?
2345 * Port worm reset is required to recover
2346 */
2347static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus)
Sarah Sharp10d674a2011-09-14 14:24:52 -07002348{
2349 return hub_is_superspeed(hub->hdev) &&
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002350 (((portstatus & USB_PORT_STAT_LINK_STATE) ==
2351 USB_SS_PORT_LS_SS_INACTIVE) ||
2352 ((portstatus & USB_PORT_STAT_LINK_STATE) ==
2353 USB_SS_PORT_LS_COMP_MOD)) ;
Sarah Sharp10d674a2011-09-14 14:24:52 -07002354}
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002357 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 int delay_time, ret;
2360 u16 portstatus;
2361 u16 portchange;
2362
2363 for (delay_time = 0;
2364 delay_time < HUB_RESET_TIMEOUT;
2365 delay_time += delay) {
2366 /* wait to give the device a chance to reset */
2367 msleep(delay);
2368
2369 /* read and decode port status */
2370 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2371 if (ret < 0)
2372 return ret;
2373
Andiry Xu75d7cf72011-09-13 16:41:11 -07002374 /*
2375 * Some buggy devices require a warm reset to be issued even
2376 * when the port appears not to be connected.
2377 */
2378 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002379 /*
2380 * Some buggy devices can cause an NEC host controller
2381 * to transition to the "Error" state after a hot port
2382 * reset. This will show up as the port state in
2383 * "Inactive", and the port may also report a
2384 * disconnect. Forcing a warm port reset seems to make
2385 * the device work.
2386 *
2387 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2388 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02002389 if (hub_port_warm_reset_required(hub, portstatus)) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002390 int ret;
2391
2392 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2393 clear_port_feature(hub->hdev, port1,
2394 USB_PORT_FEAT_C_CONNECTION);
2395 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2396 clear_port_feature(hub->hdev, port1,
2397 USB_PORT_FEAT_C_PORT_LINK_STATE);
2398 if (portchange & USB_PORT_STAT_C_RESET)
2399 clear_port_feature(hub->hdev, port1,
2400 USB_PORT_FEAT_C_RESET);
2401 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2402 port1);
2403 ret = hub_port_reset(hub, port1,
2404 udev, HUB_BH_RESET_TIME,
2405 true);
2406 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2407 clear_port_feature(hub->hdev, port1,
2408 USB_PORT_FEAT_C_CONNECTION);
2409 return ret;
2410 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002411 /* Device went away? */
2412 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2413 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Andiry Xu75d7cf72011-09-13 16:41:11 -07002415 /* bomb out completely if the connection bounced */
2416 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2417 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Andiry Xu75d7cf72011-09-13 16:41:11 -07002419 /* if we`ve finished resetting, then break out of
2420 * the loop
2421 */
2422 if (!(portstatus & USB_PORT_STAT_RESET) &&
2423 (portstatus & USB_PORT_STAT_ENABLE)) {
2424 if (hub_is_wusb(hub))
2425 udev->speed = USB_SPEED_WIRELESS;
2426 else if (hub_is_superspeed(hub->hdev))
2427 udev->speed = USB_SPEED_SUPER;
2428 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2429 udev->speed = USB_SPEED_HIGH;
2430 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2431 udev->speed = USB_SPEED_LOW;
2432 else
2433 udev->speed = USB_SPEED_FULL;
2434 return 0;
2435 }
2436 } else {
2437 if (portchange & USB_PORT_STAT_C_BH_RESET)
2438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 }
2440
2441 /* switch to the long delay after two short delay failures */
2442 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2443 delay = HUB_LONG_RESET_TIME;
2444
2445 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002446 "port %d not %sreset yet, waiting %dms\n",
2447 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449
2450 return -EBUSY;
2451}
2452
Andiry Xu75d7cf72011-09-13 16:41:11 -07002453static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2454 struct usb_device *udev, int *status, bool warm)
2455{
2456 switch (*status) {
2457 case 0:
2458 if (!warm) {
2459 struct usb_hcd *hcd;
2460 /* TRSTRCY = 10 ms; plus some extra */
2461 msleep(10 + 40);
2462 update_devnum(udev, 0);
2463 hcd = bus_to_hcd(udev->bus);
2464 if (hcd->driver->reset_device) {
2465 *status = hcd->driver->reset_device(hcd, udev);
2466 if (*status < 0) {
2467 dev_err(&udev->dev, "Cannot reset "
2468 "HCD device state\n");
2469 break;
2470 }
2471 }
2472 }
2473 /* FALL THROUGH */
2474 case -ENOTCONN:
2475 case -ENODEV:
2476 clear_port_feature(hub->hdev,
2477 port1, USB_PORT_FEAT_C_RESET);
2478 /* FIXME need disconnect() for NOTATTACHED device */
2479 if (warm) {
2480 clear_port_feature(hub->hdev, port1,
2481 USB_PORT_FEAT_C_BH_PORT_RESET);
2482 clear_port_feature(hub->hdev, port1,
2483 USB_PORT_FEAT_C_PORT_LINK_STATE);
2484 } else {
2485 usb_set_device_state(udev, *status
2486 ? USB_STATE_NOTATTACHED
2487 : USB_STATE_DEFAULT);
2488 }
2489 break;
2490 }
2491}
2492
2493/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002495 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 int i, status;
2498
Andiry Xu75d7cf72011-09-13 16:41:11 -07002499 if (!warm) {
2500 /* Block EHCI CF initialization during the port reset.
2501 * Some companion controllers don't like it when they mix.
2502 */
2503 down_read(&ehci_cf_port_reset_rwsem);
2504 } else {
2505 if (!hub_is_superspeed(hub->hdev)) {
2506 dev_err(hub->intfdev, "only USB3 hub support "
2507 "warm reset\n");
2508 return -EINVAL;
2509 }
2510 }
Alan Stern32fe0192007-10-10 16:27:07 -04002511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /* Reset the port */
2513 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002514 status = set_port_feature(hub->hdev, port1, (warm ?
2515 USB_PORT_FEAT_BH_PORT_RESET :
2516 USB_PORT_FEAT_RESET));
2517 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002519 "cannot %sreset port %d (err = %d)\n",
2520 warm ? "warm " : "", port1, status);
2521 } else {
2522 status = hub_port_wait_reset(hub, port1, udev, delay,
2523 warm);
David Brownelldd165252005-08-31 10:45:25 -07002524 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 dev_dbg(hub->intfdev,
2526 "port_wait_reset: err = %d\n",
2527 status);
2528 }
2529
2530 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002531 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2532 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002533 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
2536 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002537 "port %d not enabled, trying %sreset again...\n",
2538 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 delay = HUB_LONG_RESET_TIME;
2540 }
2541
2542 dev_err (hub->intfdev,
2543 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2544 port1);
2545
Andiry Xu75d7cf72011-09-13 16:41:11 -07002546done:
2547 if (!warm)
2548 up_read(&ehci_cf_port_reset_rwsem);
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 return status;
2551}
2552
Andiry Xu0ed9a572011-04-27 18:07:43 +08002553/* Check if a port is power on */
2554static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2555{
2556 int ret = 0;
2557
2558 if (hub_is_superspeed(hub->hdev)) {
2559 if (portstatus & USB_SS_PORT_STAT_POWER)
2560 ret = 1;
2561 } else {
2562 if (portstatus & USB_PORT_STAT_POWER)
2563 ret = 1;
2564 }
2565
2566 return ret;
2567}
2568
Alan Sternd388dab2006-07-01 22:14:24 -04002569#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Andiry Xu0ed9a572011-04-27 18:07:43 +08002571/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2572static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2573{
2574 int ret = 0;
2575
2576 if (hub_is_superspeed(hub->hdev)) {
2577 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2578 == USB_SS_PORT_LS_U3)
2579 ret = 1;
2580 } else {
2581 if (portstatus & USB_PORT_STAT_SUSPEND)
2582 ret = 1;
2583 }
2584
2585 return ret;
2586}
Alan Sternb01b03f2008-04-28 11:06:11 -04002587
2588/* Determine whether the device on a port is ready for a normal resume,
2589 * is ready for a reset-resume, or should be disconnected.
2590 */
2591static int check_port_resume_type(struct usb_device *udev,
2592 struct usb_hub *hub, int port1,
2593 int status, unsigned portchange, unsigned portstatus)
2594{
2595 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002596 if (status || port_is_suspended(hub, portstatus) ||
2597 !port_is_power_on(hub, portstatus) ||
2598 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002599 if (status >= 0)
2600 status = -ENODEV;
2601 }
2602
Alan Stern86c57ed2008-06-30 11:14:43 -04002603 /* Can't do a normal resume if the port isn't enabled,
2604 * so try a reset-resume instead.
2605 */
2606 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2607 if (udev->persist_enabled)
2608 udev->reset_resume = 1;
2609 else
2610 status = -ENODEV;
2611 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002612
2613 if (status) {
2614 dev_dbg(hub->intfdev,
2615 "port %d status %04x.%04x after resume, %d\n",
2616 port1, portchange, portstatus, status);
2617 } else if (udev->reset_resume) {
2618
2619 /* Late port handoff can set status-change bits */
2620 if (portchange & USB_PORT_STAT_C_CONNECTION)
2621 clear_port_feature(hub->hdev, port1,
2622 USB_PORT_FEAT_C_CONNECTION);
2623 if (portchange & USB_PORT_STAT_C_ENABLE)
2624 clear_port_feature(hub->hdev, port1,
2625 USB_PORT_FEAT_C_ENABLE);
2626 }
2627
2628 return status;
2629}
2630
Sarah Sharpf74631e2012-06-25 12:08:08 -07002631int usb_disable_ltm(struct usb_device *udev)
2632{
2633 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2634
2635 /* Check if the roothub and device supports LTM. */
2636 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2637 !usb_device_supports_ltm(udev))
2638 return 0;
2639
2640 /* Clear Feature LTM Enable can only be sent if the device is
2641 * configured.
2642 */
2643 if (!udev->actconfig)
2644 return 0;
2645
2646 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2647 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2648 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2649 USB_CTRL_SET_TIMEOUT);
2650}
2651EXPORT_SYMBOL_GPL(usb_disable_ltm);
2652
2653void usb_enable_ltm(struct usb_device *udev)
2654{
2655 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
2656
2657 /* Check if the roothub and device supports LTM. */
2658 if (!usb_device_supports_ltm(hcd->self.root_hub) ||
2659 !usb_device_supports_ltm(udev))
2660 return;
2661
2662 /* Set Feature LTM Enable can only be sent if the device is
2663 * configured.
2664 */
2665 if (!udev->actconfig)
2666 return;
2667
2668 usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2669 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2670 USB_DEVICE_LTM_ENABLE, 0, NULL, 0,
2671 USB_CTRL_SET_TIMEOUT);
2672}
2673EXPORT_SYMBOL_GPL(usb_enable_ltm);
2674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675#ifdef CONFIG_USB_SUSPEND
2676
2677/*
Alan Stern140d8f62006-07-01 22:07:21 -04002678 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002679 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002680 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 *
2682 * Suspends a USB device that isn't in active use, conserving power.
2683 * Devices may wake out of a suspend, if anything important happens,
2684 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002685 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 * to disconnect devices while they are suspended.
2687 *
David Brownell390a8c32005-09-13 19:57:27 -07002688 * This only affects the USB hardware for a device; its interfaces
2689 * (and, for hubs, child devices) must already have been suspended.
2690 *
Alan Stern624d6c02007-05-30 15:35:16 -04002691 * Selective port suspend reduces power; most suspended devices draw
2692 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2693 * All devices below the suspended port are also suspended.
2694 *
2695 * Devices leave suspend state when the host wakes them up. Some devices
2696 * also support "remote wakeup", where the device can activate the USB
2697 * tree above them to deliver data, such as a keypress or packet. In
2698 * some cases, this wakes the USB host.
2699 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 * Suspending OTG devices may trigger HNP, if that's been enabled
2701 * between a pair of dual-role devices. That will change roles, such
2702 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2703 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002704 * Devices on USB hub ports have only one "suspend" state, corresponding
2705 * to ACPI D2, "may cause the device to lose some context".
2706 * State transitions include:
2707 *
2708 * - suspend, resume ... when the VBUS power link stays live
2709 * - suspend, disconnect ... VBUS lost
2710 *
2711 * Once VBUS drop breaks the circuit, the port it's using has to go through
2712 * normal re-enumeration procedures, starting with enabling VBUS power.
2713 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2714 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2715 * timer, no SRP, no requests through sysfs.
2716 *
2717 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2718 * the root hub for their bus goes into global suspend ... so we don't
2719 * (falsely) update the device power state to say it suspended.
2720 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * Returns 0 on success, else negative errno.
2722 */
Alan Stern65bfd292008-11-25 16:39:18 -05002723int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Alan Stern624d6c02007-05-30 15:35:16 -04002725 struct usb_hub *hub = hdev_to_hub(udev->parent);
2726 int port1 = udev->portnum;
2727 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002728
Alan Stern624d6c02007-05-30 15:35:16 -04002729 /* enable remote wakeup when appropriate; this lets the device
2730 * wake up the upstream hub (including maybe the root hub).
2731 *
2732 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2733 * we don't explicitly enable it here.
2734 */
2735 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002736 if (!hub_is_superspeed(hub->hdev)) {
2737 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2738 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2739 USB_DEVICE_REMOTE_WAKEUP, 0,
2740 NULL, 0,
2741 USB_CTRL_SET_TIMEOUT);
2742 } else {
2743 /* Assume there's only one function on the USB 3.0
2744 * device and enable remote wake for the first
2745 * interface. FIXME if the interface association
2746 * descriptor shows there's more than one function.
2747 */
2748 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2749 USB_REQ_SET_FEATURE,
2750 USB_RECIP_INTERFACE,
2751 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002752 USB_INTRF_FUNC_SUSPEND_RW |
2753 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002754 NULL, 0,
2755 USB_CTRL_SET_TIMEOUT);
2756 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002757 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002758 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2759 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002760 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002761 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002762 return status;
2763 }
Alan Stern624d6c02007-05-30 15:35:16 -04002764 }
2765
Andiry Xu65580b432011-09-23 14:19:52 -07002766 /* disable USB2 hardware LPM */
2767 if (udev->usb2_hw_lpm_enabled == 1)
2768 usb_set_usb2_hardware_lpm(udev, 0);
2769
Sarah Sharpf74631e2012-06-25 12:08:08 -07002770 if (usb_disable_ltm(udev)) {
2771 dev_err(&udev->dev, "%s Failed to disable LTM before suspend\n.",
2772 __func__);
2773 return -ENOMEM;
2774 }
Sarah Sharp83060952012-05-02 14:25:52 -07002775 if (usb_unlocked_disable_lpm(udev)) {
2776 dev_err(&udev->dev, "%s Failed to disable LPM before suspend\n.",
2777 __func__);
2778 return -ENOMEM;
2779 }
2780
Alan Stern624d6c02007-05-30 15:35:16 -04002781 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002782 if (hub_is_superspeed(hub->hdev))
2783 status = set_port_feature(hub->hdev,
2784 port1 | (USB_SS_PORT_LS_U3 << 3),
2785 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002786 else
2787 status = set_port_feature(hub->hdev, port1,
2788 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002789 if (status) {
2790 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2791 port1, status);
2792 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002793 if (udev->do_remote_wakeup)
2794 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002795 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2796 USB_DEVICE_REMOTE_WAKEUP, 0,
2797 NULL, 0,
2798 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002799
Andiry Xuc3e751e2012-05-05 00:50:10 +08002800 /* Try to enable USB2 hardware LPM again */
2801 if (udev->usb2_hw_lpm_capable == 1)
2802 usb_set_usb2_hardware_lpm(udev, 1);
2803
Sarah Sharpf74631e2012-06-25 12:08:08 -07002804 /* Try to enable USB3 LTM and LPM again */
2805 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07002806 usb_unlocked_enable_lpm(udev);
2807
Alan Sterncbb33002011-06-15 16:29:16 -04002808 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002809 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002810 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002811 } else {
2812 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002813 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2814 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2815 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002816 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2817 msleep(10);
2818 }
Alan Sternc08512c2010-11-15 15:57:58 -05002819 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002820 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
David Brownellf3f32532005-09-22 22:37:29 -07002822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823/*
David Brownell390a8c32005-09-13 19:57:27 -07002824 * If the USB "suspend" state is in use (rather than "global suspend"),
2825 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002826 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 * hardware resume signaling is finished, either because of selective
2828 * resume (by host) or remote wakeup (by device) ... now see what changed
2829 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002830 *
2831 * If @udev->reset_resume is set then the device is reset before the
2832 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 */
Alan Stern140d8f62006-07-01 22:07:21 -04002834static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Alan Stern54515fe2007-05-30 15:38:58 -04002836 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 u16 devstatus;
2838
2839 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002840 dev_dbg(&udev->dev, "%s\n",
2841 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 /* usb ch9 identifies four variants of SUSPENDED, based on what
2844 * state the device resumes to. Linux currently won't see the
2845 * first two on the host side; they'd be inside hub_port_init()
2846 * during many timeouts, but khubd can't suspend until later.
2847 */
2848 usb_set_device_state(udev, udev->actconfig
2849 ? USB_STATE_CONFIGURED
2850 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Alan Stern54515fe2007-05-30 15:38:58 -04002852 /* 10.5.4.5 says not to reset a suspended port if the attached
2853 * device is enabled for remote wakeup. Hence the reset
2854 * operation is carried out here, after the port has been
2855 * resumed.
2856 */
2857 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002858 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002859 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 /* 10.5.4.5 says be sure devices in the tree are still there.
2862 * For now let's assume the device didn't go crazy on resume,
2863 * and device drivers will know about any resume quirks.
2864 */
Alan Stern54515fe2007-05-30 15:38:58 -04002865 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002866 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002867 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2868 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002869 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002870
2871 /* If a normal resume failed, try doing a reset-resume */
2872 if (status && !udev->reset_resume && udev->persist_enabled) {
2873 dev_dbg(&udev->dev, "retry with reset-resume\n");
2874 udev->reset_resume = 1;
2875 goto retry_reset_resume;
2876 }
Alan Stern54515fe2007-05-30 15:38:58 -04002877 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002878
Alan Stern624d6c02007-05-30 15:35:16 -04002879 if (status) {
2880 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2881 status);
2882 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002884 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 status = usb_control_msg(udev,
2886 usb_sndctrlpipe(udev, 0),
2887 USB_REQ_CLEAR_FEATURE,
2888 USB_RECIP_DEVICE,
2889 USB_DEVICE_REMOTE_WAKEUP, 0,
2890 NULL, 0,
2891 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002892 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002893 dev_dbg(&udev->dev,
2894 "disable remote wakeup, status %d\n",
2895 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
2899 return status;
2900}
2901
Alan Stern624d6c02007-05-30 15:35:16 -04002902/*
2903 * usb_port_resume - re-activate a suspended usb device's upstream port
2904 * @udev: device to re-activate, not a root hub
2905 * Context: must be able to sleep; device not locked; pm locks held
2906 *
2907 * This will re-activate the suspended device, increasing power usage
2908 * while letting drivers communicate again with its endpoints.
2909 * USB resume explicitly guarantees that the power session between
2910 * the host and the device is the same as it was when the device
2911 * suspended.
2912 *
Alan Sternfeccc302008-03-03 15:15:59 -05002913 * If @udev->reset_resume is set then this routine won't check that the
2914 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002915 * reset @udev. The end result is that a broken power session can be
2916 * recovered and @udev will appear to persist across a loss of VBUS power.
2917 *
2918 * For example, if a host controller doesn't maintain VBUS suspend current
2919 * during a system sleep or is reset when the system wakes up, all the USB
2920 * power sessions below it will be broken. This is especially troublesome
2921 * for mass-storage devices containing mounted filesystems, since the
2922 * device will appear to have disconnected and all the memory mappings
2923 * to it will be lost. Using the USB_PERSIST facility, the device can be
2924 * made to appear as if it had not disconnected.
2925 *
Ming Lei742120c2008-06-18 22:00:29 +08002926 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002927 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002928 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2929 * quite possible for a device to remain unaltered but its media to be
2930 * changed. If the user replaces a flash memory card while the system is
2931 * asleep, he will have only himself to blame when the filesystem on the
2932 * new card is corrupted and the system crashes.
2933 *
Alan Stern624d6c02007-05-30 15:35:16 -04002934 * Returns 0 on success, else negative errno.
2935 */
Alan Stern65bfd292008-11-25 16:39:18 -05002936int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
Alan Stern624d6c02007-05-30 15:35:16 -04002938 struct usb_hub *hub = hdev_to_hub(udev->parent);
2939 int port1 = udev->portnum;
2940 int status;
2941 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05002942
2943 /* Skip the initial Clear-Suspend step for a remote wakeup */
2944 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08002945 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05002946 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2949
Alan Sternd5cbad42006-08-11 16:52:39 -04002950 set_bit(port1, hub->busy_bits);
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08002953 if (hub_is_superspeed(hub->hdev))
2954 status = set_port_feature(hub->hdev,
2955 port1 | (USB_SS_PORT_LS_U0 << 3),
2956 USB_PORT_FEAT_LINK_STATE);
2957 else
2958 status = clear_port_feature(hub->hdev,
2959 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002961 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
2962 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04002965 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02002966 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 msleep(25);
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2970 * stop resume signaling. Then finish the resume
2971 * sequence.
2972 */
Alan Sternd25450c2006-11-20 11:14:30 -05002973 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04002974
Alan Sternb01b03f2008-04-28 11:06:11 -04002975 /* TRSMRCY = 10 msec */
2976 msleep(10);
2977 }
Alan Stern54515fe2007-05-30 15:38:58 -04002978
Alan Sternb01b03f2008-04-28 11:06:11 -04002979 SuspendCleared:
2980 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08002981 if (hub_is_superspeed(hub->hdev)) {
2982 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2983 clear_port_feature(hub->hdev, port1,
2984 USB_PORT_FEAT_C_PORT_LINK_STATE);
2985 } else {
2986 if (portchange & USB_PORT_STAT_C_SUSPEND)
2987 clear_port_feature(hub->hdev, port1,
2988 USB_PORT_FEAT_C_SUSPEND);
2989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Alan Sternd5cbad42006-08-11 16:52:39 -04002992 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04002993
Alan Sternb01b03f2008-04-28 11:06:11 -04002994 status = check_port_resume_type(udev,
2995 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04002996 if (status == 0)
2997 status = finish_port_resume(udev);
2998 if (status < 0) {
2999 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3000 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07003001 } else {
3002 /* Try to enable USB2 hardware LPM */
3003 if (udev->usb2_hw_lpm_capable == 1)
3004 usb_set_usb2_hardware_lpm(udev, 1);
Sarah Sharp83060952012-05-02 14:25:52 -07003005
Sarah Sharpf74631e2012-06-25 12:08:08 -07003006 /* Try to enable USB3 LTM and LPM */
3007 usb_enable_ltm(udev);
Sarah Sharp83060952012-05-02 14:25:52 -07003008 usb_unlocked_enable_lpm(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003009 }
Andiry Xu65580b432011-09-23 14:19:52 -07003010
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return status;
3012}
3013
Alan Stern8808f002008-04-28 11:06:55 -04003014/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05003015int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017 int status = 0;
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04003020 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003021 status = usb_autoresume_device(udev);
3022 if (status == 0) {
3023 /* Let the drivers do their thing, then... */
3024 usb_autosuspend_device(udev);
3025 }
Alan Sternd25450c2006-11-20 11:14:30 -05003026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return status;
3028}
3029
Alan Sternd388dab2006-07-01 22:14:24 -04003030#else /* CONFIG_USB_SUSPEND */
3031
3032/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
3033
Alan Stern65bfd292008-11-25 16:39:18 -05003034int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003035{
3036 return 0;
3037}
3038
Alan Sternb01b03f2008-04-28 11:06:11 -04003039/* However we may need to do a reset-resume */
3040
Alan Stern65bfd292008-11-25 16:39:18 -05003041int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04003042{
Alan Sternb01b03f2008-04-28 11:06:11 -04003043 struct usb_hub *hub = hdev_to_hub(udev->parent);
3044 int port1 = udev->portnum;
3045 int status;
3046 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04003047
Alan Sternb01b03f2008-04-28 11:06:11 -04003048 status = hub_port_status(hub, port1, &portstatus, &portchange);
3049 status = check_port_resume_type(udev,
3050 hub, port1, status, portchange, portstatus);
3051
3052 if (status) {
3053 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
3054 hub_port_logical_disconnect(hub, port1);
3055 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04003056 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08003057 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04003058 }
3059 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04003060}
3061
Alan Sternd388dab2006-07-01 22:14:24 -04003062#endif
3063
David Brownelldb690872005-09-13 19:56:33 -07003064static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
3066 struct usb_hub *hub = usb_get_intfdata (intf);
3067 struct usb_device *hdev = hub->hdev;
3068 unsigned port1;
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003069 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Alan Sterncbb33002011-06-15 16:29:16 -04003071 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3073 struct usb_device *udev;
3074
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07003075 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04003076 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04003077 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003078 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04003079 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07003080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
Sarah Sharp4296c70a2012-01-06 10:34:31 -08003082 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
3083 /* Enable hub to send remote wakeup for all ports. */
3084 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
3085 status = set_port_feature(hdev,
3086 port1 |
3087 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
3088 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
3089 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
3090 USB_PORT_FEAT_REMOTE_WAKE_MASK);
3091 }
3092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Harvey Harrison441b62c2008-03-03 16:08:34 -08003094 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05003095
David Brownellc9f89fa2005-09-13 19:57:04 -07003096 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04003097 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f64362007-05-04 11:51:54 -04003098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
3101static int hub_resume(struct usb_interface *intf)
3102{
Alan Stern5e6effa2008-03-03 15:15:51 -05003103 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Alan Stern5e6effa2008-03-03 15:15:51 -05003105 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003106 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return 0;
3108}
3109
Alan Sternb41a60e2007-05-30 15:39:33 -04003110static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04003111{
Alan Sternb41a60e2007-05-30 15:39:33 -04003112 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04003113
Alan Stern5e6effa2008-03-03 15:15:51 -05003114 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04003115 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04003116 return 0;
3117}
3118
Alan Stern54515fe2007-05-30 15:38:58 -04003119/**
3120 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
3121 * @rhdev: struct usb_device for the root hub
3122 *
3123 * The USB host controller driver calls this function when its root hub
3124 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05003125 * has been reset. The routine marks @rhdev as having lost power.
3126 * When the hub driver is resumed it will take notice and carry out
3127 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
3128 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04003129 */
3130void usb_root_hub_lost_power(struct usb_device *rhdev)
3131{
3132 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
3133 rhdev->reset_resume = 1;
3134}
3135EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
3136
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003137static const char * const usb3_lpm_names[] = {
3138 "U0",
3139 "U1",
3140 "U2",
3141 "U3",
3142};
3143
3144/*
3145 * Send a Set SEL control transfer to the device, prior to enabling
3146 * device-initiated U1 or U2. This lets the device know the exit latencies from
3147 * the time the device initiates a U1 or U2 exit, to the time it will receive a
3148 * packet from the host.
3149 *
3150 * This function will fail if the SEL or PEL values for udev are greater than
3151 * the maximum allowed values for the link state to be enabled.
3152 */
3153static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state)
3154{
3155 struct usb_set_sel_req *sel_values;
3156 unsigned long long u1_sel;
3157 unsigned long long u1_pel;
3158 unsigned long long u2_sel;
3159 unsigned long long u2_pel;
3160 int ret;
3161
3162 /* Convert SEL and PEL stored in ns to us */
3163 u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
3164 u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
3165 u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
3166 u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
3167
3168 /*
3169 * Make sure that the calculated SEL and PEL values for the link
3170 * state we're enabling aren't bigger than the max SEL/PEL
3171 * value that will fit in the SET SEL control transfer.
3172 * Otherwise the device would get an incorrect idea of the exit
3173 * latency for the link state, and could start a device-initiated
3174 * U1/U2 when the exit latencies are too high.
3175 */
3176 if ((state == USB3_LPM_U1 &&
3177 (u1_sel > USB3_LPM_MAX_U1_SEL_PEL ||
3178 u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) ||
3179 (state == USB3_LPM_U2 &&
3180 (u2_sel > USB3_LPM_MAX_U2_SEL_PEL ||
3181 u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) {
3182 dev_dbg(&udev->dev, "Device-initiated %s disabled due "
3183 "to long SEL %llu ms or PEL %llu ms\n",
3184 usb3_lpm_names[state], u1_sel, u1_pel);
3185 return -EINVAL;
3186 }
3187
3188 /*
3189 * If we're enabling device-initiated LPM for one link state,
3190 * but the other link state has a too high SEL or PEL value,
3191 * just set those values to the max in the Set SEL request.
3192 */
3193 if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL)
3194 u1_sel = USB3_LPM_MAX_U1_SEL_PEL;
3195
3196 if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL)
3197 u1_pel = USB3_LPM_MAX_U1_SEL_PEL;
3198
3199 if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL)
3200 u2_sel = USB3_LPM_MAX_U2_SEL_PEL;
3201
3202 if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL)
3203 u2_pel = USB3_LPM_MAX_U2_SEL_PEL;
3204
3205 /*
3206 * usb_enable_lpm() can be called as part of a failed device reset,
3207 * which may be initiated by an error path of a mass storage driver.
3208 * Therefore, use GFP_NOIO.
3209 */
3210 sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO);
3211 if (!sel_values)
3212 return -ENOMEM;
3213
3214 sel_values->u1_sel = u1_sel;
3215 sel_values->u1_pel = u1_pel;
3216 sel_values->u2_sel = cpu_to_le16(u2_sel);
3217 sel_values->u2_pel = cpu_to_le16(u2_pel);
3218
3219 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3220 USB_REQ_SET_SEL,
3221 USB_RECIP_DEVICE,
3222 0, 0,
3223 sel_values, sizeof *(sel_values),
3224 USB_CTRL_SET_TIMEOUT);
3225 kfree(sel_values);
3226 return ret;
3227}
3228
3229/*
3230 * Enable or disable device-initiated U1 or U2 transitions.
3231 */
3232static int usb_set_device_initiated_lpm(struct usb_device *udev,
3233 enum usb3_link_state state, bool enable)
3234{
3235 int ret;
3236 int feature;
3237
3238 switch (state) {
3239 case USB3_LPM_U1:
3240 feature = USB_DEVICE_U1_ENABLE;
3241 break;
3242 case USB3_LPM_U2:
3243 feature = USB_DEVICE_U2_ENABLE;
3244 break;
3245 default:
3246 dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n",
3247 __func__, enable ? "enable" : "disable");
3248 return -EINVAL;
3249 }
3250
3251 if (udev->state != USB_STATE_CONFIGURED) {
3252 dev_dbg(&udev->dev, "%s: Can't %s %s state "
3253 "for unconfigured device.\n",
3254 __func__, enable ? "enable" : "disable",
3255 usb3_lpm_names[state]);
3256 return 0;
3257 }
3258
3259 if (enable) {
3260 /*
3261 * First, let the device know about the exit latencies
3262 * associated with the link state we're about to enable.
3263 */
3264 ret = usb_req_set_sel(udev, state);
3265 if (ret < 0) {
3266 dev_warn(&udev->dev, "Set SEL for device-initiated "
3267 "%s failed.\n", usb3_lpm_names[state]);
3268 return -EBUSY;
3269 }
3270 /*
3271 * Now send the control transfer to enable device-initiated LPM
3272 * for either U1 or U2.
3273 */
3274 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3275 USB_REQ_SET_FEATURE,
3276 USB_RECIP_DEVICE,
3277 feature,
3278 0, NULL, 0,
3279 USB_CTRL_SET_TIMEOUT);
3280 } else {
3281 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3282 USB_REQ_CLEAR_FEATURE,
3283 USB_RECIP_DEVICE,
3284 feature,
3285 0, NULL, 0,
3286 USB_CTRL_SET_TIMEOUT);
3287 }
3288 if (ret < 0) {
3289 dev_warn(&udev->dev, "%s of device-initiated %s failed.\n",
3290 enable ? "Enable" : "Disable",
3291 usb3_lpm_names[state]);
3292 return -EBUSY;
3293 }
3294 return 0;
3295}
3296
3297static int usb_set_lpm_timeout(struct usb_device *udev,
3298 enum usb3_link_state state, int timeout)
3299{
3300 int ret;
3301 int feature;
3302
3303 switch (state) {
3304 case USB3_LPM_U1:
3305 feature = USB_PORT_FEAT_U1_TIMEOUT;
3306 break;
3307 case USB3_LPM_U2:
3308 feature = USB_PORT_FEAT_U2_TIMEOUT;
3309 break;
3310 default:
3311 dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n",
3312 __func__);
3313 return -EINVAL;
3314 }
3315
3316 if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT &&
3317 timeout != USB3_LPM_DEVICE_INITIATED) {
3318 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, "
3319 "which is a reserved value.\n",
3320 usb3_lpm_names[state], timeout);
3321 return -EINVAL;
3322 }
3323
3324 ret = set_port_feature(udev->parent,
3325 USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum,
3326 feature);
3327 if (ret < 0) {
3328 dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x,"
3329 "error code %i\n", usb3_lpm_names[state],
3330 timeout, ret);
3331 return -EBUSY;
3332 }
3333 if (state == USB3_LPM_U1)
3334 udev->u1_params.timeout = timeout;
3335 else
3336 udev->u2_params.timeout = timeout;
3337 return 0;
3338}
3339
3340/*
3341 * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated
3342 * U1/U2 entry.
3343 *
3344 * We will attempt to enable U1 or U2, but there are no guarantees that the
3345 * control transfers to set the hub timeout or enable device-initiated U1/U2
3346 * will be successful.
3347 *
3348 * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI
3349 * driver know about it. If that call fails, it should be harmless, and just
3350 * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency.
3351 */
3352static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3353 enum usb3_link_state state)
3354{
3355 int timeout;
3356
3357 /* We allow the host controller to set the U1/U2 timeout internally
3358 * first, so that it can change its schedule to account for the
3359 * additional latency to send data to a device in a lower power
3360 * link state.
3361 */
3362 timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state);
3363
3364 /* xHCI host controller doesn't want to enable this LPM state. */
3365 if (timeout == 0)
3366 return;
3367
3368 if (timeout < 0) {
3369 dev_warn(&udev->dev, "Could not enable %s link state, "
3370 "xHCI error %i.\n", usb3_lpm_names[state],
3371 timeout);
3372 return;
3373 }
3374
3375 if (usb_set_lpm_timeout(udev, state, timeout))
3376 /* If we can't set the parent hub U1/U2 timeout,
3377 * device-initiated LPM won't be allowed either, so let the xHCI
3378 * host know that this link state won't be enabled.
3379 */
3380 hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state);
3381
3382 /* Only a configured device will accept the Set Feature U1/U2_ENABLE */
3383 else if (udev->actconfig)
3384 usb_set_device_initiated_lpm(udev, state, true);
3385
3386}
3387
3388/*
3389 * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated
3390 * U1/U2 entry.
3391 *
3392 * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry.
3393 * If zero is returned, the parent will not allow the link to go into U1/U2.
3394 *
3395 * If zero is returned, device-initiated U1/U2 entry may still be enabled, but
3396 * it won't have an effect on the bus link state because the parent hub will
3397 * still disallow device-initiated U1/U2 entry.
3398 *
3399 * If zero is returned, the xHCI host controller may still think U1/U2 entry is
3400 * possible. The result will be slightly more bus bandwidth will be taken up
3401 * (to account for U1/U2 exit latency), but it should be harmless.
3402 */
3403static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev,
3404 enum usb3_link_state state)
3405{
3406 int feature;
3407
3408 switch (state) {
3409 case USB3_LPM_U1:
3410 feature = USB_PORT_FEAT_U1_TIMEOUT;
3411 break;
3412 case USB3_LPM_U2:
3413 feature = USB_PORT_FEAT_U2_TIMEOUT;
3414 break;
3415 default:
3416 dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n",
3417 __func__);
3418 return -EINVAL;
3419 }
3420
3421 if (usb_set_lpm_timeout(udev, state, 0))
3422 return -EBUSY;
3423
3424 usb_set_device_initiated_lpm(udev, state, false);
3425
3426 if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state))
3427 dev_warn(&udev->dev, "Could not disable xHCI %s timeout, "
3428 "bus schedule bandwidth may be impacted.\n",
3429 usb3_lpm_names[state]);
3430 return 0;
3431}
3432
3433/*
3434 * Disable hub-initiated and device-initiated U1 and U2 entry.
3435 * Caller must own the bandwidth_mutex.
3436 *
3437 * This will call usb_enable_lpm() on failure, which will decrement
3438 * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero.
3439 */
3440int usb_disable_lpm(struct usb_device *udev)
3441{
3442 struct usb_hcd *hcd;
3443
3444 if (!udev || !udev->parent ||
3445 udev->speed != USB_SPEED_SUPER ||
3446 !udev->lpm_capable)
3447 return 0;
3448
3449 hcd = bus_to_hcd(udev->bus);
3450 if (!hcd || !hcd->driver->disable_usb3_lpm_timeout)
3451 return 0;
3452
3453 udev->lpm_disable_count++;
Dan Carpenter55558c32012-05-22 20:54:34 +03003454 if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0))
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003455 return 0;
3456
3457 /* If LPM is enabled, attempt to disable it. */
3458 if (usb_disable_link_state(hcd, udev, USB3_LPM_U1))
3459 goto enable_lpm;
3460 if (usb_disable_link_state(hcd, udev, USB3_LPM_U2))
3461 goto enable_lpm;
3462
3463 return 0;
3464
3465enable_lpm:
3466 usb_enable_lpm(udev);
3467 return -EBUSY;
3468}
3469EXPORT_SYMBOL_GPL(usb_disable_lpm);
3470
3471/* Grab the bandwidth_mutex before calling usb_disable_lpm() */
3472int usb_unlocked_disable_lpm(struct usb_device *udev)
3473{
3474 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3475 int ret;
3476
3477 if (!hcd)
3478 return -EINVAL;
3479
3480 mutex_lock(hcd->bandwidth_mutex);
3481 ret = usb_disable_lpm(udev);
3482 mutex_unlock(hcd->bandwidth_mutex);
3483
3484 return ret;
3485}
3486EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
3487
3488/*
3489 * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The
3490 * xHCI host policy may prevent U1 or U2 from being enabled.
3491 *
3492 * Other callers may have disabled link PM, so U1 and U2 entry will be disabled
3493 * until the lpm_disable_count drops to zero. Caller must own the
3494 * bandwidth_mutex.
3495 */
3496void usb_enable_lpm(struct usb_device *udev)
3497{
3498 struct usb_hcd *hcd;
3499
3500 if (!udev || !udev->parent ||
3501 udev->speed != USB_SPEED_SUPER ||
3502 !udev->lpm_capable)
3503 return;
3504
3505 udev->lpm_disable_count--;
3506 hcd = bus_to_hcd(udev->bus);
3507 /* Double check that we can both enable and disable LPM.
3508 * Device must be configured to accept set feature U1/U2 timeout.
3509 */
3510 if (!hcd || !hcd->driver->enable_usb3_lpm_timeout ||
3511 !hcd->driver->disable_usb3_lpm_timeout)
3512 return;
3513
3514 if (udev->lpm_disable_count > 0)
3515 return;
3516
3517 usb_enable_link_state(hcd, udev, USB3_LPM_U1);
3518 usb_enable_link_state(hcd, udev, USB3_LPM_U2);
3519}
3520EXPORT_SYMBOL_GPL(usb_enable_lpm);
3521
3522/* Grab the bandwidth_mutex before calling usb_enable_lpm() */
3523void usb_unlocked_enable_lpm(struct usb_device *udev)
3524{
3525 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
3526
3527 if (!hcd)
3528 return;
3529
3530 mutex_lock(hcd->bandwidth_mutex);
3531 usb_enable_lpm(udev);
3532 mutex_unlock(hcd->bandwidth_mutex);
3533}
3534EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
3535
3536
Alan Sternd388dab2006-07-01 22:14:24 -04003537#else /* CONFIG_PM */
3538
Alan Sternf07600c2007-05-30 15:38:16 -04003539#define hub_suspend NULL
3540#define hub_resume NULL
3541#define hub_reset_resume NULL
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003542
3543int usb_disable_lpm(struct usb_device *udev)
3544{
3545 return 0;
3546}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003547EXPORT_SYMBOL_GPL(usb_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003548
3549void usb_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003550EXPORT_SYMBOL_GPL(usb_enable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003551
3552int usb_unlocked_disable_lpm(struct usb_device *udev)
3553{
3554 return 0;
3555}
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003556EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm);
Sarah Sharp1ea7e0e2012-04-24 17:21:50 -07003557
3558void usb_unlocked_enable_lpm(struct usb_device *udev) { }
Sarah Sharpe9261fb2012-05-21 08:29:01 -07003559EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm);
Sarah Sharpf74631e2012-06-25 12:08:08 -07003560
3561int usb_disable_ltm(struct usb_device *udev)
3562{
3563 return 0;
3564}
3565EXPORT_SYMBOL_GPL(usb_disable_ltm);
3566
3567void usb_enable_ltm(struct usb_device *udev) { }
3568EXPORT_SYMBOL_GPL(usb_enable_ltm);
Alan Sternd388dab2006-07-01 22:14:24 -04003569#endif
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571
3572/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
3573 *
3574 * Between connect detection and reset signaling there must be a delay
3575 * of 100ms at least for debounce and power-settling. The corresponding
3576 * timer shall restart whenever the downstream port detects a disconnect.
3577 *
3578 * Apparently there are some bluetooth and irda-dongles and a number of
3579 * low-speed devices for which this debounce period may last over a second.
3580 * Not covered by the spec - but easy to deal with.
3581 *
3582 * This implementation uses a 1500ms total debounce timeout; if the
3583 * connection isn't stable by then it returns -ETIMEDOUT. It checks
3584 * every 25ms for transient disconnects. When the port status has been
3585 * unchanged for 100ms it returns the port status.
3586 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587static int hub_port_debounce(struct usb_hub *hub, int port1)
3588{
3589 int ret;
3590 int total_time, stable_time = 0;
3591 u16 portchange, portstatus;
3592 unsigned connection = 0xffff;
3593
3594 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
3595 ret = hub_port_status(hub, port1, &portstatus, &portchange);
3596 if (ret < 0)
3597 return ret;
3598
3599 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
3600 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
3601 stable_time += HUB_DEBOUNCE_STEP;
3602 if (stable_time >= HUB_DEBOUNCE_STABLE)
3603 break;
3604 } else {
3605 stable_time = 0;
3606 connection = portstatus & USB_PORT_STAT_CONNECTION;
3607 }
3608
3609 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3610 clear_port_feature(hub->hdev, port1,
3611 USB_PORT_FEAT_C_CONNECTION);
3612 }
3613
3614 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
3615 break;
3616 msleep(HUB_DEBOUNCE_STEP);
3617 }
3618
3619 dev_dbg (hub->intfdev,
3620 "debounce: port %d: total %dms stable %dms status 0x%x\n",
3621 port1, total_time, stable_time, portstatus);
3622
3623 if (stable_time < HUB_DEBOUNCE_STABLE)
3624 return -ETIMEDOUT;
3625 return portstatus;
3626}
3627
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003628void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629{
Alan Sternddeac4e2009-01-15 17:03:33 -05003630 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3631 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003632 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003634EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
3636#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3637#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3638
Alan Stern4326ed02007-07-30 17:08:43 -04003639static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
3641 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003642 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643
Sarah Sharpc6515272009-04-27 19:57:26 -07003644 /*
3645 * The host controller will choose the device address,
3646 * instead of the core having chosen it earlier
3647 */
3648 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 return -EINVAL;
3650 if (udev->state == USB_STATE_ADDRESS)
3651 return 0;
3652 if (udev->state != USB_STATE_DEFAULT)
3653 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003654 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003655 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003656 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003657 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3658 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3659 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003661 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003662 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003664 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
3666 return retval;
3667}
3668
3669/* Reset device, (re)assign address, get device descriptor.
3670 * Device connection must be stable, no more debouncing needed.
3671 * Returns device in USB_STATE_ADDRESS, except on error.
3672 *
3673 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003674 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 * newly detected device that is not accessible through any global
3676 * pointers, it's not necessary to lock the device.
3677 */
3678static int
3679hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3680 int retry_counter)
3681{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003682 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003685 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 int i, j, retval;
3687 unsigned delay = HUB_SHORT_RESET_TIME;
3688 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003689 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003690 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691
3692 /* root hub ports have a slightly longer reset period
3693 * (from USB 2.0 spec, section 7.1.7.5)
3694 */
3695 if (!hdev->parent) {
3696 delay = HUB_ROOT_RESET_TIME;
3697 if (port1 == hdev->bus->otg_port)
3698 hdev->bus->b_hnp_enable = 0;
3699 }
3700
3701 /* Some low speed devices have problems with the quick delay, so */
3702 /* be a bit pessimistic with those devices. RHbug #23670 */
3703 if (oldspeed == USB_SPEED_LOW)
3704 delay = HUB_LONG_RESET_TIME;
3705
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003706 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Luben Tuikov07194ab2011-02-11 11:33:10 -08003708 /* Reset the device; full speed may morph to high speed */
3709 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003710 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003711 if (retval < 0) /* error or disconnect */
3712 goto fail;
3713 /* success, speed is known */
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 retval = -ENODEV;
3716
3717 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3718 dev_dbg(&udev->dev, "device reset changed speed!\n");
3719 goto fail;
3720 }
3721 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3724 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003725 * For Wireless USB devices, ep0 max packet is always 512 (tho
3726 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 */
3728 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003729 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003730 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003731 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003734 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 break;
3736 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3737 /* to determine the ep0 maxpacket size, try to read
3738 * the device descriptor to get bMaxPacketSize0 and
3739 * then correct our initial guess.
3740 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003741 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 break;
3743 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003744 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 break;
3746 default:
3747 goto fail;
3748 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003749
3750 if (udev->speed == USB_SPEED_WIRELESS)
3751 speed = "variable speed Wireless";
3752 else
3753 speed = usb_speed_string(udev->speed);
3754
Sarah Sharpc6515272009-04-27 19:57:26 -07003755 if (udev->speed != USB_SPEED_SUPER)
3756 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003757 "%s %s USB device number %d using %s\n",
3758 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003759 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
3761 /* Set up TT records, if needed */
3762 if (hdev->tt) {
3763 udev->tt = hdev->tt;
3764 udev->ttport = hdev->ttport;
3765 } else if (udev->speed != USB_SPEED_HIGH
3766 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003767 if (!hub->tt.hub) {
3768 dev_err(&udev->dev, "parent hub has no TT\n");
3769 retval = -EINVAL;
3770 goto fail;
3771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 udev->tt = &hub->tt;
3773 udev->ttport = port1;
3774 }
3775
3776 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3777 * Because device hardware and firmware is sometimes buggy in
3778 * this area, and this is how Linux has done it for ages.
3779 * Change it cautiously.
3780 *
3781 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3782 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3783 * so it may help with some non-standards-compliant devices.
3784 * Otherwise we start with SET_ADDRESS and then try to read the
3785 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3786 * value.
3787 */
3788 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003789 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 struct usb_device_descriptor *buf;
3791 int r = 0;
3792
3793#define GET_DESCRIPTOR_BUFSIZE 64
3794 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3795 if (!buf) {
3796 retval = -ENOMEM;
3797 continue;
3798 }
3799
Alan Sternb89ee192007-05-11 10:19:04 -04003800 /* Retry on all errors; some devices are flakey.
3801 * 255 is for WUSB devices, we actually need to use
3802 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 */
3804 for (j = 0; j < 3; ++j) {
3805 buf->bMaxPacketSize0 = 0;
3806 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3807 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3808 USB_DT_DEVICE << 8, 0,
3809 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003810 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003812 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 if (buf->bDescriptorType ==
3814 USB_DT_DEVICE) {
3815 r = 0;
3816 break;
3817 }
3818 /* FALL THROUGH */
3819 default:
3820 if (r == 0)
3821 r = -EPROTO;
3822 break;
3823 }
3824 if (r == 0)
3825 break;
3826 }
3827 udev->descriptor.bMaxPacketSize0 =
3828 buf->bMaxPacketSize0;
3829 kfree(buf);
3830
Andiry Xu75d7cf72011-09-13 16:41:11 -07003831 retval = hub_port_reset(hub, port1, udev, delay, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 if (retval < 0) /* error or disconnect */
3833 goto fail;
3834 if (oldspeed != udev->speed) {
3835 dev_dbg(&udev->dev,
3836 "device reset changed speed!\n");
3837 retval = -ENODEV;
3838 goto fail;
3839 }
3840 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003841 dev_err(&udev->dev,
3842 "device descriptor read/64, error %d\n",
3843 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 retval = -EMSGSIZE;
3845 continue;
3846 }
3847#undef GET_DESCRIPTOR_BUFSIZE
3848 }
3849
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003850 /*
3851 * If device is WUSB, we already assigned an
3852 * unauthorized address in the Connect Ack sequence;
3853 * authorization will assign the final address.
3854 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003855 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003856 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3857 retval = hub_set_address(udev, devnum);
3858 if (retval >= 0)
3859 break;
3860 msleep(200);
3861 }
3862 if (retval < 0) {
3863 dev_err(&udev->dev,
3864 "device not accepting address %d, error %d\n",
3865 devnum, retval);
3866 goto fail;
3867 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003868 if (udev->speed == USB_SPEED_SUPER) {
3869 devnum = udev->devnum;
3870 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003871 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003872 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003873 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003874 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003875
3876 /* cope with hardware quirkiness:
3877 * - let SET_ADDRESS settle, some device hardware wants it
3878 * - read ep0 maxpacket even for high and low speed,
3879 */
3880 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003881 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
3885 retval = usb_get_device_descriptor(udev, 8);
3886 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003887 dev_err(&udev->dev,
3888 "device descriptor read/8, error %d\n",
3889 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 if (retval >= 0)
3891 retval = -EMSGSIZE;
3892 } else {
3893 retval = 0;
3894 break;
3895 }
3896 }
3897 if (retval)
3898 goto fail;
3899
Elric Fud8aec3d2012-03-26 21:16:02 +08003900 /*
3901 * Some superspeed devices have finished the link training process
3902 * and attached to a superspeed hub port, but the device descriptor
3903 * got from those devices show they aren't superspeed devices. Warm
3904 * reset the port attached by the devices can fix them.
3905 */
3906 if ((udev->speed == USB_SPEED_SUPER) &&
3907 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3908 dev_err(&udev->dev, "got a wrong device descriptor, "
3909 "warm reset device\n");
3910 hub_port_reset(hub, port1, udev,
3911 HUB_BH_RESET_TIME, true);
3912 retval = -EINVAL;
3913 goto fail;
3914 }
3915
Sarah Sharp6b403b02009-04-27 19:54:10 -07003916 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3917 udev->speed == USB_SPEED_SUPER)
3918 i = 512;
3919 else
3920 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003921 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003922 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003924 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 retval = -EMSGSIZE;
3926 goto fail;
3927 }
Alan Stern56626a72010-10-14 15:25:21 -04003928 if (udev->speed == USB_SPEED_FULL)
3929 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3930 else
3931 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003933 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
3935
3936 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3937 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003938 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3939 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 if (retval >= 0)
3941 retval = -ENOMSG;
3942 goto fail;
3943 }
3944
Andiry Xu1ff4df52011-09-23 14:19:48 -07003945 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3946 retval = usb_get_bos_descriptor(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003947 if (!retval) {
Sarah Sharpd9b20992012-02-20 08:31:26 -08003948 udev->lpm_capable = usb_device_supports_lpm(udev);
Sarah Sharp51e0a012012-02-20 12:02:19 -08003949 usb_set_lpm_parameters(udev);
3950 }
Andiry Xu1ff4df52011-09-23 14:19:48 -07003951 }
Andiry Xu3148bf02011-09-23 14:19:47 -07003952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08003954 /* notify HCD that we have a device connected and addressed */
3955 if (hcd->driver->update_device)
3956 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957fail:
Alan Stern4326ed02007-07-30 17:08:43 -04003958 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05003960 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04003961 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003962 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 return retval;
3964}
3965
3966static void
3967check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
3968{
3969 struct usb_qualifier_descriptor *qual;
3970 int status;
3971
Christoph Lametere94b1762006-12-06 20:33:17 -08003972 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 if (qual == NULL)
3974 return;
3975
3976 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
3977 qual, sizeof *qual);
3978 if (status == sizeof *qual) {
3979 dev_info(&udev->dev, "not running at top speed; "
3980 "connect to a high speed hub\n");
3981 /* hub LEDs are probably harder to miss than syslog */
3982 if (hub->has_indicators) {
3983 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003984 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 }
3986 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003987 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988}
3989
3990static unsigned
3991hub_power_remaining (struct usb_hub *hub)
3992{
3993 struct usb_device *hdev = hub->hdev;
3994 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05003995 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Alan Stern55c52712005-11-23 12:03:12 -05003997 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 return 0;
3999
Alan Stern55c52712005-11-23 12:03:12 -05004000 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
4001 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004002 struct usb_device *udev = hdev->children[port1 - 1];
Alan Stern55c52712005-11-23 12:03:12 -05004003 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
4005 if (!udev)
4006 continue;
4007
Alan Stern55c52712005-11-23 12:03:12 -05004008 /* Unconfigured devices may not use more than 100mA,
4009 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05004011 delta = udev->actconfig->desc.bMaxPower * 2;
4012 else if (port1 != udev->bus->otg_port || hdev->parent)
4013 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 else
Alan Stern55c52712005-11-23 12:03:12 -05004015 delta = 8;
4016 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08004017 dev_warn(&udev->dev,
4018 "%dmA is over %umA budget for port %d!\n",
4019 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 remaining -= delta;
4021 }
4022 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05004023 dev_warn(hub->intfdev, "%dmA over power budget!\n",
4024 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 remaining = 0;
4026 }
4027 return remaining;
4028}
4029
4030/* Handle physical or logical connection change events.
4031 * This routine is called when:
4032 * a port connection-change occurs;
4033 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08004034 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 * a firmware download)
4036 * caller already locked the hub
4037 */
4038static void hub_port_connect_change(struct usb_hub *hub, int port1,
4039 u16 portstatus, u16 portchange)
4040{
4041 struct usb_device *hdev = hub->hdev;
4042 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05304043 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04004044 unsigned wHubCharacteristics =
4045 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04004046 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04004048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 dev_dbg (hub_dev,
4050 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08004051 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
4053 if (hub->has_indicators) {
4054 set_port_led(hub, port1, HUB_LED_AUTO);
4055 hub->indicator[port1-1] = INDICATOR_AUTO;
4056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
4058#ifdef CONFIG_USB_OTG
4059 /* during HNP, don't repeat the debounce */
4060 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04004061 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
4062 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063#endif
4064
Alan Stern8808f002008-04-28 11:06:55 -04004065 /* Try to resuscitate an existing device */
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004066 udev = hdev->children[port1-1];
Alan Stern8808f002008-04-28 11:06:55 -04004067 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
4068 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04004069 usb_lock_device(udev);
4070 if (portstatus & USB_PORT_STAT_ENABLE) {
4071 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04004072
4073#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04004074 } else if (udev->state == USB_STATE_SUSPENDED &&
4075 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04004076 /* For a suspended device, treat this as a
4077 * remote wakeup event.
4078 */
Alan Stern0534d462010-01-08 12:56:30 -05004079 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04004080#endif
4081
4082 } else {
Alan Stern5257d972008-09-22 14:43:08 -04004083 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04004084 }
4085 usb_unlock_device(udev);
4086
4087 if (status == 0) {
4088 clear_bit(port1, hub->change_bits);
4089 return;
4090 }
4091 }
4092
Alan Stern24618b02008-04-28 11:06:28 -04004093 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04004094 if (udev)
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004095 usb_disconnect(&hdev->children[port1-1]);
Alan Stern24618b02008-04-28 11:06:28 -04004096 clear_bit(port1, hub->change_bits);
4097
Alan Stern253e0572009-10-27 15:20:13 -04004098 /* We can forget about a "removed" device when there's a physical
4099 * disconnect or the connect status changes.
4100 */
4101 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4102 (portchange & USB_PORT_STAT_C_CONNECTION))
4103 clear_bit(port1, hub->removed_bits);
4104
Alan Stern5257d972008-09-22 14:43:08 -04004105 if (portchange & (USB_PORT_STAT_C_CONNECTION |
4106 USB_PORT_STAT_C_ENABLE)) {
4107 status = hub_port_debounce(hub, port1);
4108 if (status < 0) {
4109 if (printk_ratelimit())
4110 dev_err(hub_dev, "connect-debounce failed, "
4111 "port %d disabled\n", port1);
4112 portstatus &= ~USB_PORT_STAT_CONNECTION;
4113 } else {
4114 portstatus = status;
4115 }
4116 }
4117
Richard Zhao925aa462012-07-11 11:09:28 +08004118 if (hcd->phy && !hdev->parent) {
4119 if (portstatus & USB_PORT_STAT_CONNECTION)
4120 usb_phy_notify_connect(hcd->phy, port1);
4121 else
4122 usb_phy_notify_disconnect(hcd->phy, port1);
4123 }
4124
Alan Stern253e0572009-10-27 15:20:13 -04004125 /* Return now if debouncing failed or nothing is connected or
4126 * the device was "removed".
4127 */
4128 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
4129 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130
4131 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07004132 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08004133 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04004135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 if (portstatus & USB_PORT_STAT_ENABLE)
4137 goto done;
4138 return;
4139 }
4140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
4143 /* reallocate for each attempt, since references
4144 * to the previous one can escape in various ways
4145 */
4146 udev = usb_alloc_dev(hdev, hdev->bus, port1);
4147 if (!udev) {
4148 dev_err (hub_dev,
4149 "couldn't allocate port %d usb_device\n",
4150 port1);
4151 goto done;
4152 }
4153
4154 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05004155 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04004156 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07004157 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05004158
Sarah Sharp131dec32010-12-06 21:00:19 -08004159 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
4160 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07004161 udev->speed = USB_SPEED_SUPER;
4162 else
4163 udev->speed = USB_SPEED_UNKNOWN;
4164
Alan Stern3b29b682011-02-22 09:53:41 -05004165 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07004166 if (udev->devnum <= 0) {
4167 status = -ENOTCONN; /* Don't retry */
4168 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07004169 }
4170
Sarah Sharpe7b77172009-04-27 19:54:26 -07004171 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 status = hub_port_init(hub, udev, port1, i);
4173 if (status < 0)
4174 goto loop;
4175
Phil Dibowitz93362a82010-07-22 00:05:01 +02004176 usb_detect_quirks(udev);
4177 if (udev->quirks & USB_QUIRK_DELAY_INIT)
4178 msleep(1000);
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 /* consecutive bus-powered hubs aren't reliable; they can
4181 * violate the voltage drop budget. if the new child has
4182 * a "powered" LED, users should notice we didn't enable it
4183 * (without reading syslog), even without per-port LEDs
4184 * on the parent.
4185 */
4186 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05004187 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 u16 devstat;
4189
4190 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
4191 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05004192 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 dev_dbg(&udev->dev, "get status %d ?\n", status);
4194 goto loop_disable;
4195 }
Alan Stern55c52712005-11-23 12:03:12 -05004196 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
4198 dev_err(&udev->dev,
4199 "can't connect bus-powered hub "
4200 "to this port\n");
4201 if (hub->has_indicators) {
4202 hub->indicator[port1-1] =
4203 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00004204 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
4206 status = -ENOTCONN; /* Don't retry */
4207 goto loop_disable;
4208 }
4209 }
4210
4211 /* check for devices running slower than they could */
4212 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
4213 && udev->speed == USB_SPEED_FULL
4214 && highspeed_hubs != 0)
4215 check_highspeed (hub, udev, port1);
4216
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004217 /* Store the parent's children[] pointer. At this point
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 * udev becomes globally accessible, although presumably
4219 * no one will look at it until hdev is unlocked.
4220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 status = 0;
4222
4223 /* We mustn't add new devices if the parent hub has
4224 * been disconnected; we would race with the
4225 * recursively_mark_NOTATTACHED() routine.
4226 */
4227 spin_lock_irq(&device_state_lock);
4228 if (hdev->state == USB_STATE_NOTATTACHED)
4229 status = -ENOTCONN;
4230 else
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004231 hdev->children[port1-1] = udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 spin_unlock_irq(&device_state_lock);
4233
4234 /* Run it through the hoops (find a driver, etc) */
4235 if (!status) {
4236 status = usb_new_device(udev);
4237 if (status) {
4238 spin_lock_irq(&device_state_lock);
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004239 hdev->children[port1-1] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 spin_unlock_irq(&device_state_lock);
4241 }
4242 }
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 if (status)
4245 goto loop_disable;
4246
4247 status = hub_power_remaining(hub);
4248 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05004249 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
4251 return;
4252
4253loop_disable:
4254 hub_port_disable(hub, port1, 1);
4255loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004256 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05004257 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11004258 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07004260 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 break;
4262 }
Alan Stern3a311552008-05-20 16:58:29 -04004263 if (hub->hdev->parent ||
4264 !hcd->driver->port_handed_over ||
4265 !(hcd->driver->port_handed_over)(hcd, port1))
4266 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
4267 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
4269done:
4270 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05304271 if (hcd->driver->relinquish_port && !hub->hdev->parent)
4272 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273}
4274
Sarah Sharp714b07b2012-01-24 13:53:18 -08004275/* Returns 1 if there was a remote wakeup and a connect status change. */
4276static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08004277 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08004278{
4279 struct usb_device *hdev;
4280 struct usb_device *udev;
4281 int connect_change = 0;
4282 int ret;
4283
4284 hdev = hub->hdev;
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004285 udev = hdev->children[port-1];
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004286 if (!hub_is_superspeed(hdev)) {
4287 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
4288 return 0;
4289 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
4290 } else {
4291 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08004292 (portstatus & USB_PORT_STAT_LINK_STATE) !=
4293 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004294 return 0;
4295 }
4296
Sarah Sharp714b07b2012-01-24 13:53:18 -08004297 if (udev) {
4298 /* TRSMRCY = 10 msec */
4299 msleep(10);
4300
4301 usb_lock_device(udev);
4302 ret = usb_remote_wakeup(udev);
4303 usb_unlock_device(udev);
4304 if (ret < 0)
4305 connect_change = 1;
4306 } else {
4307 ret = -ENODEV;
4308 hub_port_disable(hub, port, 1);
4309 }
4310 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
4311 port, ret);
4312 return connect_change;
4313}
4314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315static void hub_events(void)
4316{
4317 struct list_head *tmp;
4318 struct usb_device *hdev;
4319 struct usb_interface *intf;
4320 struct usb_hub *hub;
4321 struct device *hub_dev;
4322 u16 hubstatus;
4323 u16 hubchange;
4324 u16 portstatus;
4325 u16 portchange;
4326 int i, ret;
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004327 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329 /*
4330 * We restart the list every time to avoid a deadlock with
4331 * deleting hubs downstream from this one. This should be
4332 * safe since we delete the hub from the event list.
4333 * Not the most efficient, but avoids deadlocks.
4334 */
4335 while (1) {
4336
4337 /* Grab the first entry at the beginning of the list */
4338 spin_lock_irq(&hub_event_lock);
4339 if (list_empty(&hub_event_list)) {
4340 spin_unlock_irq(&hub_event_lock);
4341 break;
4342 }
4343
4344 tmp = hub_event_list.next;
4345 list_del_init(tmp);
4346
4347 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04004348 kref_get(&hub->kref);
4349 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
Alan Sterne8054852007-05-04 11:55:11 -04004351 hdev = hub->hdev;
4352 hub_dev = hub->intfdev;
4353 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05004354 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 hdev->state, hub->descriptor
4356 ? hub->descriptor->bNbrPorts
4357 : 0,
4358 /* NOTE: expects max 15 ports... */
4359 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05004360 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 /* Lock the device, then check to see if we were
4363 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04004364 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004365 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004366 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368 /* If the hub has died, clean up after it */
4369 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04004370 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04004371 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 goto loop;
4373 }
4374
Alan Stern40f122f2006-11-09 14:44:33 -05004375 /* Autoresume */
4376 ret = usb_autopm_get_interface(intf);
4377 if (ret) {
4378 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05004380 }
4381
4382 /* If this is an inactive hub, do nothing */
4383 if (hub->quiescing)
4384 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
4386 if (hub->error) {
4387 dev_dbg (hub_dev, "resetting for error %d\n",
4388 hub->error);
4389
Ming Lei742120c2008-06-18 22:00:29 +08004390 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 if (ret) {
4392 dev_dbg (hub_dev,
4393 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05004394 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 }
4396
4397 hub->nerrors = 0;
4398 hub->error = 0;
4399 }
4400
4401 /* deal with port status changes */
4402 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
4403 if (test_bit(i, hub->busy_bits))
4404 continue;
4405 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08004406 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08004408 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 continue;
4410
4411 ret = hub_port_status(hub, i,
4412 &portstatus, &portchange);
4413 if (ret < 0)
4414 continue;
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 if (portchange & USB_PORT_STAT_C_CONNECTION) {
4417 clear_port_feature(hdev, i,
4418 USB_PORT_FEAT_C_CONNECTION);
4419 connect_change = 1;
4420 }
4421
4422 if (portchange & USB_PORT_STAT_C_ENABLE) {
4423 if (!connect_change)
4424 dev_dbg (hub_dev,
4425 "port %d enable change, "
4426 "status %08x\n",
4427 i, portstatus);
4428 clear_port_feature(hdev, i,
4429 USB_PORT_FEAT_C_ENABLE);
4430
4431 /*
4432 * EM interference sometimes causes badly
4433 * shielded USB devices to be shutdown by
4434 * the hub, this hack enables them again.
4435 * Works at least with mouse driver.
4436 */
4437 if (!(portstatus & USB_PORT_STAT_ENABLE)
4438 && !connect_change
Greg Kroah-Hartmanfa286182012-05-14 09:20:37 -07004439 && hdev->children[i-1]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 dev_err (hub_dev,
4441 "port %i "
4442 "disabled by hub (EMI?), "
4443 "re-enabling...\n",
4444 i);
4445 connect_change = 1;
4446 }
4447 }
4448
Sarah Sharp72937e12012-01-24 11:46:50 -08004449 if (hub_handle_remote_wakeup(hub, i,
4450 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004451 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004454 u16 status = 0;
4455 u16 unused;
4456
4457 dev_dbg(hub_dev, "over-current change on port "
4458 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 clear_port_feature(hdev, i,
4460 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004461 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004462 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004463 hub_port_status(hub, i, &status, &unused);
4464 if (status & USB_PORT_STAT_OVERCURRENT)
4465 dev_err(hub_dev, "over-current "
4466 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 }
4468
4469 if (portchange & USB_PORT_STAT_C_RESET) {
4470 dev_dbg (hub_dev,
4471 "reset change on port %d\n",
4472 i);
4473 clear_port_feature(hdev, i,
4474 USB_PORT_FEAT_C_RESET);
4475 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004476 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4477 hub_is_superspeed(hub->hdev)) {
4478 dev_dbg(hub_dev,
4479 "warm reset change on port %d\n",
4480 i);
4481 clear_port_feature(hdev, i,
4482 USB_PORT_FEAT_C_BH_PORT_RESET);
4483 }
John Youndbe79bb2001-09-17 00:00:00 -07004484 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4485 clear_port_feature(hub->hdev, i,
4486 USB_PORT_FEAT_C_PORT_LINK_STATE);
4487 }
4488 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4489 dev_warn(hub_dev,
4490 "config error on port %d\n",
4491 i);
4492 clear_port_feature(hub->hdev, i,
4493 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
Andiry Xu5e467f62011-04-27 18:07:54 +08004496 /* Warm reset a USB3 protocol port if it's in
4497 * SS.Inactive state.
4498 */
Stanislaw Ledwon8bea2bd2012-06-18 15:20:00 +02004499 if (hub_port_warm_reset_required(hub, portstatus)) {
Andiry Xu5e467f62011-04-27 18:07:54 +08004500 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004501 hub_port_reset(hub, i, NULL,
4502 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004503 }
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 if (connect_change)
4506 hub_port_connect_change(hub, i,
4507 portstatus, portchange);
4508 } /* end for i */
4509
4510 /* deal with hub status changes */
4511 if (test_and_clear_bit(0, hub->event_bits) == 0)
4512 ; /* do nothing */
4513 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4514 dev_err (hub_dev, "get_hub_status failed\n");
4515 else {
4516 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4517 dev_dbg (hub_dev, "power change\n");
4518 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004519 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4520 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004521 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004522 else
4523 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 }
4525 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004526 u16 status = 0;
4527 u16 unused;
4528
4529 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004531 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004532 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004533 hub_hub_status(hub, &status, &unused);
4534 if (status & HUB_STATUS_OVERCURRENT)
4535 dev_err(hub_dev, "over-current "
4536 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 }
4538 }
4539
Alan Stern8e4ceb32009-12-07 13:01:37 -05004540 loop_autopm:
4541 /* Balance the usb_autopm_get_interface() above */
4542 usb_autopm_put_interface_no_suspend(intf);
4543 loop:
4544 /* Balance the usb_autopm_get_interface_no_resume() in
4545 * kick_khubd() and allow autosuspend.
4546 */
4547 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004548 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004550 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
4552 } /* end while (1) */
4553}
4554
4555static int hub_thread(void *__unused)
4556{
Alan Stern3bb1af52008-03-03 15:15:36 -05004557 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4558 * port handover. Otherwise it might see that a full-speed device
4559 * was gone before the EHCI controller had handed its port over to
4560 * the companion full-speed controller.
4561 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004562 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 do {
4565 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004566 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004567 !list_empty(&hub_event_list) ||
4568 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004569 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004571 pr_debug("%s: khubd exiting\n", usbcore_name);
4572 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573}
4574
Németh Márton1e927d92010-01-10 15:34:53 +01004575static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4577 .bDeviceClass = USB_CLASS_HUB},
4578 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4579 .bInterfaceClass = USB_CLASS_HUB},
4580 { } /* Terminating entry */
4581};
4582
4583MODULE_DEVICE_TABLE (usb, hub_id_table);
4584
4585static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 .name = "hub",
4587 .probe = hub_probe,
4588 .disconnect = hub_disconnect,
4589 .suspend = hub_suspend,
4590 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004591 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004592 .pre_reset = hub_pre_reset,
4593 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004594 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004596 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597};
4598
4599int usb_hub_init(void)
4600{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 if (usb_register(&hub_driver) < 0) {
4602 printk(KERN_ERR "%s: can't register hub driver\n",
4603 usbcore_name);
4604 return -1;
4605 }
4606
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004607 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4608 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610
4611 /* Fall through if kernel_thread failed */
4612 usb_deregister(&hub_driver);
4613 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4614
4615 return -1;
4616}
4617
4618void usb_hub_cleanup(void)
4619{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004620 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622 /*
4623 * Hub resources are freed for us by usb_deregister. It calls
4624 * usb_driver_purge on every device which in turn calls that
4625 * devices disconnect function if it is using this driver.
4626 * The hub_disconnect function takes care of releasing the
4627 * individual hub resources. -greg
4628 */
4629 usb_deregister(&hub_driver);
4630} /* usb_hub_cleanup() */
4631
Alan Sterneb764c42008-03-03 15:16:04 -05004632static int descriptors_changed(struct usb_device *udev,
4633 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634{
Alan Sterneb764c42008-03-03 15:16:04 -05004635 int changed = 0;
4636 unsigned index;
4637 unsigned serial_len = 0;
4638 unsigned len;
4639 unsigned old_length;
4640 int length;
4641 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
Alan Sterneb764c42008-03-03 15:16:04 -05004643 if (memcmp(&udev->descriptor, old_device_descriptor,
4644 sizeof(*old_device_descriptor)) != 0)
4645 return 1;
4646
4647 /* Since the idVendor, idProduct, and bcdDevice values in the
4648 * device descriptor haven't changed, we will assume the
4649 * Manufacturer and Product strings haven't changed either.
4650 * But the SerialNumber string could be different (e.g., a
4651 * different flash card of the same brand).
4652 */
4653 if (udev->serial)
4654 serial_len = strlen(udev->serial) + 1;
4655
4656 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004658 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4659 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 }
Alan Sterneb764c42008-03-03 15:16:04 -05004661
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004662 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (buf == NULL) {
4664 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4665 /* assume the worst */
4666 return 1;
4667 }
4668 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004669 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4671 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004672 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 dev_dbg(&udev->dev, "config index %d, error %d\n",
4674 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004675 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 break;
4677 }
4678 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4679 != 0) {
4680 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004681 index,
4682 ((struct usb_config_descriptor *) buf)->
4683 bConfigurationValue);
4684 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 break;
4686 }
4687 }
Alan Sterneb764c42008-03-03 15:16:04 -05004688
4689 if (!changed && serial_len) {
4690 length = usb_string(udev, udev->descriptor.iSerialNumber,
4691 buf, serial_len);
4692 if (length + 1 != serial_len) {
4693 dev_dbg(&udev->dev, "serial string error %d\n",
4694 length);
4695 changed = 1;
4696 } else if (memcmp(buf, udev->serial, length) != 0) {
4697 dev_dbg(&udev->dev, "serial string changed\n");
4698 changed = 1;
4699 }
4700 }
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004703 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
4706/**
Ming Lei742120c2008-06-18 22:00:29 +08004707 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4709 *
Alan Stern79efa092006-06-01 13:33:42 -04004710 * WARNING - don't use this routine to reset a composite device
4711 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004712 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 *
4714 * Do a port reset, reassign the device's address, and establish its
4715 * former operating configuration. If the reset fails, or the device's
4716 * descriptors change from their values before the reset, or the original
4717 * configuration and altsettings cannot be restored, a flag will be set
4718 * telling khubd to pretend the device has been disconnected and then
4719 * re-connected. All drivers will be unbound, and the device will be
4720 * re-enumerated and probed all over again.
4721 *
4722 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4723 * flagged for logical disconnection, or some other negative error code
4724 * if the reset wasn't even attempted.
4725 *
4726 * The caller must own the device lock. For example, it's safe to use
4727 * this from a driver probe() routine after downloading new firmware.
4728 * For calls that might not occur during probe(), drivers should lock
4729 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004730 *
4731 * Locking exception: This routine may also be called from within an
4732 * autoresume handler. Such usage won't conflict with other tasks
4733 * holding the device lock because these tasks should always call
4734 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 */
Ming Lei742120c2008-06-18 22:00:29 +08004736static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737{
4738 struct usb_device *parent_hdev = udev->parent;
4739 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004740 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004742 int i, ret = 0;
4743 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
4745 if (udev->state == USB_STATE_NOTATTACHED ||
4746 udev->state == USB_STATE_SUSPENDED) {
4747 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4748 udev->state);
4749 return -EINVAL;
4750 }
4751
4752 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004753 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004754 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 return -EISDIR;
4756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 parent_hub = hdev_to_hub(parent_hdev);
4758
Sarah Sharpf74631e2012-06-25 12:08:08 -07004759 /* Disable LPM and LTM while we reset the device and reinstall the alt
4760 * settings. Device-initiated LPM settings, and system exit latency
4761 * settings are cleared when the device is reset, so we have to set
4762 * them up again.
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004763 */
4764 ret = usb_unlocked_disable_lpm(udev);
4765 if (ret) {
4766 dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__);
4767 goto re_enumerate;
4768 }
Sarah Sharpf74631e2012-06-25 12:08:08 -07004769 ret = usb_disable_ltm(udev);
4770 if (ret) {
4771 dev_err(&udev->dev, "%s Failed to disable LTM\n.",
4772 __func__);
4773 goto re_enumerate;
4774 }
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 set_bit(port1, parent_hub->busy_bits);
4777 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4778
4779 /* ep0 maxpacket size may change; let the HCD know about it.
4780 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004781 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004783 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 break;
4785 }
4786 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 if (ret < 0)
4789 goto re_enumerate;
4790
4791 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004792 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 dev_info(&udev->dev, "device firmware changed\n");
4794 udev->descriptor = descriptor; /* for disconnect() calls */
4795 goto re_enumerate;
4796 }
Alan Stern4fe03872009-02-26 10:21:02 -05004797
4798 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (!udev->actconfig)
4800 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004801
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004802 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004803 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4804 if (ret < 0) {
4805 dev_warn(&udev->dev,
4806 "Busted HC? Not enough HCD resources for "
4807 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004808 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004809 goto re_enumerate;
4810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4812 USB_REQ_SET_CONFIGURATION, 0,
4813 udev->actconfig->desc.bConfigurationValue, 0,
4814 NULL, 0, USB_CTRL_SET_TIMEOUT);
4815 if (ret < 0) {
4816 dev_err(&udev->dev,
4817 "can't restore configuration #%d (error=%d)\n",
4818 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004819 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 goto re_enumerate;
4821 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004822 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4824
Alan Stern4fe03872009-02-26 10:21:02 -05004825 /* Put interfaces back into the same altsettings as before.
4826 * Don't bother to send the Set-Interface request for interfaces
4827 * that were already in altsetting 0; besides being unnecessary,
4828 * many devices can't handle it. Instead just reset the host-side
4829 * endpoint state.
4830 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004832 struct usb_host_config *config = udev->actconfig;
4833 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 struct usb_interface_descriptor *desc;
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004837 if (desc->bAlternateSetting == 0) {
4838 usb_disable_interface(udev, intf, true);
4839 usb_enable_interface(udev, intf, true);
4840 ret = 0;
4841 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004842 /* Let the bandwidth allocation function know that this
4843 * device has been reset, and it will have to use
4844 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004845 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004846 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004847 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4848 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004849 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 if (ret < 0) {
4852 dev_err(&udev->dev, "failed to restore interface %d "
4853 "altsetting %d (error=%d)\n",
4854 desc->bInterfaceNumber,
4855 desc->bAlternateSetting,
4856 ret);
4857 goto re_enumerate;
4858 }
4859 }
4860
4861done:
Sarah Sharpf74631e2012-06-25 12:08:08 -07004862 /* Now that the alt settings are re-installed, enable LTM and LPM. */
Alan Stern78d9a482008-06-23 16:00:40 -04004863 usb_unlocked_enable_lpm(udev);
Sarah Sharpf74631e2012-06-25 12:08:08 -07004864 usb_enable_ltm(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 return 0;
4866
4867re_enumerate:
Sarah Sharp6d1d0512012-07-03 22:49:04 -07004868 /* LPM state doesn't matter when we're about to destroy the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 hub_port_logical_disconnect(parent_hub, port1);
4870 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871}
4872
4873/**
4874 * usb_reset_device - warn interface drivers and perform a USB port reset
4875 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4876 *
Alan Stern79efa092006-06-01 13:33:42 -04004877 * Warns all drivers bound to registered interfaces (using their pre_reset
4878 * method), performs the port reset, and then lets the drivers know that
Ming Lei742120c2008-06-18 22:00:29 +08004879 * the reset is over (using their post_reset method).
Alan Stern79efa092006-06-01 13:33:42 -04004880 *
Alan Stern79efa092006-06-01 13:33:42 -04004881 * Return value is the same as for usb_reset_and_verify_device().
4882 *
4883 * The caller must own the device lock. For example, it's safe to use
4884 * this from a driver probe() routine after downloading new firmware.
4885 * For calls that might not occur during probe(), drivers should lock
Ming Lei742120c2008-06-18 22:00:29 +08004886 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04004887 *
4888 * If an interface is currently being probed or disconnected, we assume
4889 * its driver knows how to handle resets. For all other interfaces,
4890 * if the driver doesn't have pre_reset and post_reset methods then
4891 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004892 */
Ming Lei742120c2008-06-18 22:00:29 +08004893int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004894{
4895 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004896 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004897 struct usb_host_config *config = udev->actconfig;
4898
4899 if (udev->state == USB_STATE_NOTATTACHED ||
4900 udev->state == USB_STATE_SUSPENDED) {
4901 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4902 udev->state);
4903 return -EINVAL;
4904 }
4905
Alan Stern645daaa2006-08-30 15:47:02 -04004906 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004907 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004908
Alan Stern79efa092006-06-01 13:33:42 -04004909 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004910 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004911 struct usb_interface *cintf = config->interface[i];
4912 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004913 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004914
4915 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004916 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004917 if (drv->pre_reset && drv->post_reset)
4918 unbind = (drv->pre_reset)(cintf);
4919 else if (cintf->condition ==
4920 USB_INTERFACE_BOUND)
4921 unbind = 1;
4922 if (unbind)
4923 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004924 }
4925 }
4926 }
4927
Ming Lei742120c2008-06-18 22:00:29 +08004928 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004929
4930 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004931 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004932 struct usb_interface *cintf = config->interface[i];
4933 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004934 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004935
Alan Stern78d9a482008-06-23 16:00:40 -04004936 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004937 drv = to_usb_driver(cintf->dev.driver);
4938 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004939 rebind = (drv->post_reset)(cintf);
4940 else if (cintf->condition ==
4941 USB_INTERFACE_BOUND)
4942 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04004943 }
Alan Stern6c640942008-10-21 15:40:03 -04004944 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04004945 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004946 }
4947 }
4948
Alan Stern94fcda12006-11-20 11:38:46 -05004949 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004950 return ret;
4951}
Ming Lei742120c2008-06-18 22:00:29 +08004952EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08004953
4954
4955/**
4956 * usb_queue_reset_device - Reset a USB device from an atomic context
4957 * @iface: USB interface belonging to the device to reset
4958 *
4959 * This function can be used to reset a USB device from an atomic
4960 * context, where usb_reset_device() won't work (as it blocks).
4961 *
4962 * Doing a reset via this method is functionally equivalent to calling
4963 * usb_reset_device(), except for the fact that it is delayed to a
4964 * workqueue. This means that any drivers bound to other interfaces
4965 * might be unbound, as well as users from usbfs in user space.
4966 *
4967 * Corner cases:
4968 *
4969 * - Scheduling two resets at the same time from two different drivers
4970 * attached to two different interfaces of the same device is
4971 * possible; depending on how the driver attached to each interface
4972 * handles ->pre_reset(), the second reset might happen or not.
4973 *
4974 * - If a driver is unbound and it had a pending reset, the reset will
4975 * be cancelled.
4976 *
4977 * - This function can be called during .probe() or .disconnect()
4978 * times. On return from .disconnect(), any pending resets will be
4979 * cancelled.
4980 *
4981 * There is no no need to lock/unlock the @reset_ws as schedule_work()
4982 * does its own.
4983 *
4984 * NOTE: We don't do any reference count tracking because it is not
4985 * needed. The lifecycle of the work_struct is tied to the
4986 * usb_interface. Before destroying the interface we cancel the
4987 * work_struct, so the fact that work_struct is queued and or
4988 * running means the interface (and thus, the device) exist and
4989 * are referenced.
4990 */
4991void usb_queue_reset_device(struct usb_interface *iface)
4992{
4993 schedule_work(&iface->reset_ws);
4994}
4995EXPORT_SYMBOL_GPL(usb_queue_reset_device);