blob: d97d5480d9309fd05265e601d3ae10f40b3a2daa [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>
Phil Dibowitz93362a82010-07-22 00:05:01 +020023#include <linux/usb/quirks.h>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070024#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010025#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <asm/byteorder.h>
31
32#include "usb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070034#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
35#include <linux/usb/hcd.h>
36#include <linux/usb/ch11.h>
37
38int portno;
39int No_Data_Phase;
40EXPORT_SYMBOL(No_Data_Phase);
41int No_Status_Phase;
42EXPORT_SYMBOL(No_Status_Phase);
43unsigned char hub_tier;
44
45#define PDC_HOST_NOTIFY 0x8001 /*completion from core */
46#define UNSUPPORTED_DEVICE 0x8099
47#define UNWANTED_SUSPEND 0x8098
48#define PDC_POWERMANAGEMENT 0x8097
49
50int Unwanted_SecondReset;
51EXPORT_SYMBOL(Unwanted_SecondReset);
52int HostComplianceTest;
53EXPORT_SYMBOL(HostComplianceTest);
54int HostTest;
55EXPORT_SYMBOL(HostTest);
56#endif
57
58
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080059/* if we are in debug mode, always announce new devices */
60#ifdef DEBUG
61#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
62#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
63#endif
64#endif
65
Alan Stern1bb5f662006-11-06 11:56:13 -050066struct usb_hub {
67 struct device *intfdev; /* the "interface" device */
68 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040069 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050070 struct urb *urb; /* for interrupt polling pipe */
71
72 /* buffer for urb ... with extra space in case of babble */
73 char (*buffer)[8];
Alan Stern1bb5f662006-11-06 11:56:13 -050074 union {
75 struct usb_hub_status hub;
76 struct usb_port_status port;
77 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050078 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050079
80 int error; /* last reported error */
81 int nerrors; /* track consecutive errors */
82
83 struct list_head event_list; /* hubs w/data or errs ready */
84 unsigned long event_bits[1]; /* status change bitmask */
85 unsigned long change_bits[1]; /* ports with logical connect
86 status change */
87 unsigned long busy_bits[1]; /* ports being reset or
88 resumed */
Alan Stern253e0572009-10-27 15:20:13 -040089 unsigned long removed_bits[1]; /* ports with a "removed"
90 device present */
Sarah Sharp4ee823b2011-11-14 18:00:01 -080091 unsigned long wakeup_bits[1]; /* ports that have signaled
92 remote wakeup */
Alan Stern1bb5f662006-11-06 11:56:13 -050093#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
94#error event_bits[] is too short!
95#endif
96
97 struct usb_hub_descriptor *descriptor; /* class descriptor */
98 struct usb_tt tt; /* Transaction Translator */
99
100 unsigned mA_per_port; /* current for each child */
101
102 unsigned limited_power:1;
103 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -0400104 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -0500105
106 unsigned has_indicators:1;
107 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +0000108 struct delayed_work leds;
Alan Stern8520f382008-09-22 14:44:26 -0400109 struct delayed_work init_work;
Alan Stern7cbe5dc2009-06-29 10:56:54 -0400110 void **port_owners;
Alan Stern1bb5f662006-11-06 11:56:13 -0500111};
112
John Youndbe79bb2001-09-17 00:00:00 -0700113static inline int hub_is_superspeed(struct usb_device *hdev)
114{
Aman Deep7bf01182011-12-08 12:05:22 +0530115 return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS);
John Youndbe79bb2001-09-17 00:00:00 -0700116}
Alan Stern1bb5f662006-11-06 11:56:13 -0500117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -0500119 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
121static DEFINE_SPINLOCK(device_state_lock);
122
123/* khubd's worklist and its lock */
124static DEFINE_SPINLOCK(hub_event_lock);
125static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
126
127/* Wakes up khubd */
128static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
129
akpm@osdl.org9c8d6172005-06-20 14:29:58 -0700130static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132/* cycle leds on hubs that aren't blinking for attention */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030133static bool blinkenlights = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134module_param (blinkenlights, bool, S_IRUGO);
135MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
136
137/*
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200138 * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about
139 * 10 seconds to send reply for the initial 64-byte descriptor request.
140 */
141/* define initial 64-byte descriptor request timeout in milliseconds */
142static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT;
143module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR);
Wu Fengguangb9cef6c2008-12-15 15:32:01 +0800144MODULE_PARM_DESC(initial_descriptor_timeout,
145 "initial 64-byte descriptor request timeout in milliseconds "
146 "(default 5000 - 5.0 seconds)");
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +0200147
148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 * As of 2.6.10 we introduce a new USB device initialization scheme which
150 * closely resembles the way Windows works. Hopefully it will be compatible
151 * with a wider range of devices than the old scheme. However some previously
152 * working devices may start giving rise to "device not accepting address"
153 * errors; if that happens the user can try the old scheme by adjusting the
154 * following module parameters.
155 *
156 * For maximum flexibility there are two boolean parameters to control the
157 * hub driver's behavior. On the first initialization attempt, if the
158 * "old_scheme_first" parameter is set then the old scheme will be used,
159 * otherwise the new scheme is used. If that fails and "use_both_schemes"
160 * is set, then the driver will make another attempt, using the other scheme.
161 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030162static bool old_scheme_first = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
164MODULE_PARM_DESC(old_scheme_first,
165 "start with the old device initialization scheme");
166
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030167static bool use_both_schemes = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
169MODULE_PARM_DESC(use_both_schemes,
170 "try the other device initialization scheme if the "
171 "first one fails");
172
Alan Stern32fe0192007-10-10 16:27:07 -0400173/* Mutual exclusion for EHCI CF initialization. This interferes with
174 * port reset on some companion controllers.
175 */
176DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
177EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
178
Alan Stern948fea32008-04-28 11:07:07 -0400179#define HUB_DEBOUNCE_TIMEOUT 1500
180#define HUB_DEBOUNCE_STEP 25
181#define HUB_DEBOUNCE_STABLE 100
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Ming Lei742120c2008-06-18 22:00:29 +0800184static int usb_reset_and_verify_device(struct usb_device *udev);
185
Sarah Sharp131dec32010-12-06 21:00:19 -0800186static inline char *portspeed(struct usb_hub *hub, int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Sarah Sharp131dec32010-12-06 21:00:19 -0800188 if (hub_is_superspeed(hub->hdev))
189 return "5.0 Gb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500190 if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return "480 Mb/s";
Alan Stern288ead42010-03-04 11:32:30 -0500192 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return "1.5 Mb/s";
194 else
195 return "12 Mb/s";
196}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/* Note that hdev or one of its children must be locked! */
Alan Stern251180842009-07-22 14:41:18 -0400199static struct usb_hub *hdev_to_hub(struct usb_device *hdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Alan Stern251180842009-07-22 14:41:18 -0400201 if (!hdev || !hdev->actconfig)
202 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return usb_get_intfdata(hdev->actconfig->interface[0]);
204}
205
206/* USB 2.0 spec Section 11.24.4.5 */
John Youndbe79bb2001-09-17 00:00:00 -0700207static int get_hub_descriptor(struct usb_device *hdev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
John Youndbe79bb2001-09-17 00:00:00 -0700209 int i, ret, size;
210 unsigned dtype;
211
212 if (hub_is_superspeed(hdev)) {
213 dtype = USB_DT_SS_HUB;
214 size = USB_DT_SS_HUB_SIZE;
215 } else {
216 dtype = USB_DT_HUB;
217 size = sizeof(struct usb_hub_descriptor);
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 for (i = 0; i < 3; i++) {
221 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
222 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
John Youndbe79bb2001-09-17 00:00:00 -0700223 dtype << 8, 0, data, size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 USB_CTRL_GET_TIMEOUT);
225 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
226 return ret;
227 }
228 return -EINVAL;
229}
230
231/*
232 * USB 2.0 spec Section 11.24.2.1
233 */
234static int clear_hub_feature(struct usb_device *hdev, int feature)
235{
236 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
237 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
238}
239
240/*
241 * USB 2.0 spec Section 11.24.2.2
242 */
243static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
244{
245 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
246 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
247 NULL, 0, 1000);
248}
249
250/*
251 * USB 2.0 spec Section 11.24.2.13
252 */
253static int set_port_feature(struct usb_device *hdev, int port1, int feature)
254{
255 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
256 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
257 NULL, 0, 1000);
258}
259
260/*
261 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
262 * for info about using port indicators
263 */
264static void set_port_led(
265 struct usb_hub *hub,
266 int port1,
267 int selector
268)
269{
270 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
271 USB_PORT_FEAT_INDICATOR);
272 if (status < 0)
273 dev_dbg (hub->intfdev,
274 "port %d indicator %s status %d\n",
275 port1,
276 ({ char *s; switch (selector) {
277 case HUB_LED_AMBER: s = "amber"; break;
278 case HUB_LED_GREEN: s = "green"; break;
279 case HUB_LED_OFF: s = "off"; break;
280 case HUB_LED_AUTO: s = "auto"; break;
281 default: s = "??"; break;
282 }; s; }),
283 status);
284}
285
286#define LED_CYCLE_PERIOD ((2*HZ)/3)
287
David Howellsc4028952006-11-22 14:57:56 +0000288static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
David Howellsc4028952006-11-22 14:57:56 +0000290 struct usb_hub *hub =
291 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 struct usb_device *hdev = hub->hdev;
293 unsigned i;
294 unsigned changed = 0;
295 int cursor = -1;
296
297 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
298 return;
299
300 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
301 unsigned selector, mode;
302
303 /* 30%-50% duty cycle */
304
305 switch (hub->indicator[i]) {
306 /* cycle marker */
307 case INDICATOR_CYCLE:
308 cursor = i;
309 selector = HUB_LED_AUTO;
310 mode = INDICATOR_AUTO;
311 break;
312 /* blinking green = sw attention */
313 case INDICATOR_GREEN_BLINK:
314 selector = HUB_LED_GREEN;
315 mode = INDICATOR_GREEN_BLINK_OFF;
316 break;
317 case INDICATOR_GREEN_BLINK_OFF:
318 selector = HUB_LED_OFF;
319 mode = INDICATOR_GREEN_BLINK;
320 break;
321 /* blinking amber = hw attention */
322 case INDICATOR_AMBER_BLINK:
323 selector = HUB_LED_AMBER;
324 mode = INDICATOR_AMBER_BLINK_OFF;
325 break;
326 case INDICATOR_AMBER_BLINK_OFF:
327 selector = HUB_LED_OFF;
328 mode = INDICATOR_AMBER_BLINK;
329 break;
330 /* blink green/amber = reserved */
331 case INDICATOR_ALT_BLINK:
332 selector = HUB_LED_GREEN;
333 mode = INDICATOR_ALT_BLINK_OFF;
334 break;
335 case INDICATOR_ALT_BLINK_OFF:
336 selector = HUB_LED_AMBER;
337 mode = INDICATOR_ALT_BLINK;
338 break;
339 default:
340 continue;
341 }
342 if (selector != HUB_LED_AUTO)
343 changed = 1;
344 set_port_led(hub, i + 1, selector);
345 hub->indicator[i] = mode;
346 }
347 if (!changed && blinkenlights) {
348 cursor++;
349 cursor %= hub->descriptor->bNbrPorts;
350 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
351 hub->indicator[cursor] = INDICATOR_CYCLE;
352 changed++;
353 }
354 if (changed)
355 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
356}
357
358/* use a short timeout for hub/port status fetches */
359#define USB_STS_TIMEOUT 1000
360#define USB_STS_RETRIES 5
361
362/*
363 * USB 2.0 spec Section 11.24.2.6
364 */
365static int get_hub_status(struct usb_device *hdev,
366 struct usb_hub_status *data)
367{
368 int i, status = -ETIMEDOUT;
369
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200370 for (i = 0; i < USB_STS_RETRIES &&
371 (status == -ETIMEDOUT || status == -EPIPE); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
373 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
374 data, sizeof(*data), USB_STS_TIMEOUT);
375 }
376 return status;
377}
378
379/*
380 * USB 2.0 spec Section 11.24.2.7
381 */
382static int get_port_status(struct usb_device *hdev, int port1,
383 struct usb_port_status *data)
384{
385 int i, status = -ETIMEDOUT;
386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387 /* ISP1763A HUB sometimes returns 2 bytes instead of 4 bytes, retry
388 * if this happens
389 */
Libor Pechacek3824c1d2011-05-20 14:53:25 +0200390 for (i = 0; i < USB_STS_RETRIES &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391 (status == -ETIMEDOUT || status == -EPIPE || status == 2); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
393 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
394 data, sizeof(*data), USB_STS_TIMEOUT);
395 }
396 return status;
397}
398
Alan Stern3eb14912008-03-03 15:15:43 -0500399static int hub_port_status(struct usb_hub *hub, int port1,
400 u16 *status, u16 *change)
401{
402 int ret;
403
404 mutex_lock(&hub->status_mutex);
405 ret = get_port_status(hub->hdev, port1, &hub->status->port);
406 if (ret < 4) {
407 dev_err(hub->intfdev,
408 "%s failed (err = %d)\n", __func__, ret);
409 if (ret >= 0)
410 ret = -EIO;
411 } else {
412 *status = le16_to_cpu(hub->status->port.wPortStatus);
413 *change = le16_to_cpu(hub->status->port.wPortChange);
John Youndbe79bb2001-09-17 00:00:00 -0700414
Alan Stern3eb14912008-03-03 15:15:43 -0500415 ret = 0;
416 }
417 mutex_unlock(&hub->status_mutex);
418 return ret;
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static void kick_khubd(struct usb_hub *hub)
422{
423 unsigned long flags;
424
425 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200426 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 list_add_tail(&hub->event_list, &hub_event_list);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500428
429 /* Suppress autosuspend until khubd runs */
430 usb_autopm_get_interface_no_resume(
431 to_usb_interface(hub->intfdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 wake_up(&khubd_wait);
433 }
434 spin_unlock_irqrestore(&hub_event_lock, flags);
435}
436
437void usb_kick_khubd(struct usb_device *hdev)
438{
Alan Stern251180842009-07-22 14:41:18 -0400439 struct usb_hub *hub = hdev_to_hub(hdev);
440
441 if (hub)
442 kick_khubd(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Sarah Sharp4ee823b2011-11-14 18:00:01 -0800445/*
446 * Let the USB core know that a USB 3.0 device has sent a Function Wake Device
447 * Notification, which indicates it had initiated remote wakeup.
448 *
449 * USB 3.0 hubs do not report the port link state change from U3 to U0 when the
450 * device initiates resume, so the USB core will not receive notice of the
451 * resume through the normal hub interrupt URB.
452 */
453void usb_wakeup_notification(struct usb_device *hdev,
454 unsigned int portnum)
455{
456 struct usb_hub *hub;
457
458 if (!hdev)
459 return;
460
461 hub = hdev_to_hub(hdev);
462 if (hub) {
463 set_bit(portnum, hub->wakeup_bits);
464 kick_khubd(hub);
465 }
466}
467EXPORT_SYMBOL_GPL(usb_wakeup_notification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100470static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200472 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400473 int status = urb->status;
Roel Kluin71d27182009-03-13 12:19:18 +0100474 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 unsigned long bits;
476
Alan Sterne0152682007-08-24 15:42:52 -0400477 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 case -ENOENT: /* synchronous unlink */
479 case -ECONNRESET: /* async unlink */
480 case -ESHUTDOWN: /* hardware going away */
481 return;
482
483 default: /* presumably an error */
484 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400485 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if ((++hub->nerrors < 10) || hub->error)
487 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400488 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* let khubd handle things */
492 case 0: /* we got data: port status changed */
493 bits = 0;
494 for (i = 0; i < urb->actual_length; ++i)
495 bits |= ((unsigned long) ((*hub->buffer)[i]))
496 << (i*8);
497 hub->event_bits[0] = bits;
498 break;
499 }
500
501 hub->nerrors = 0;
502
503 /* Something happened, let khubd figure it out */
504 kick_khubd(hub);
505
506resubmit:
507 if (hub->quiescing)
508 return;
509
510 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
511 && status != -ENODEV && status != -EPERM)
512 dev_err (hub->intfdev, "resubmit --> %d\n", status);
513}
514
515/* USB 2.0 spec Section 11.24.2.3 */
516static inline int
517hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
518{
Alan Sternc2f65952009-11-18 11:37:15 -0500519 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
521 tt, NULL, 0, 1000);
522}
523
524/*
525 * enumeration blocks khubd for a long time. we use keventd instead, since
526 * long blocking there is the exception, not the rule. accordingly, HCDs
527 * talking to TTs must queue control transfers (not just bulk and iso), so
528 * both can talk to the same hub concurrently.
529 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400530static void hub_tt_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
David Howellsc4028952006-11-22 14:57:56 +0000532 struct usb_hub *hub =
Alan Sterncb88a1b2009-06-29 10:43:32 -0400533 container_of(work, struct usb_hub, tt.clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400535 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 spin_lock_irqsave (&hub->tt.lock, flags);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400538 while (--limit && !list_empty (&hub->tt.clear_list)) {
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400539 struct list_head *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct usb_tt_clear *clear;
541 struct usb_device *hdev = hub->hdev;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400542 const struct hc_driver *drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int status;
544
H Hartley Sweetend0f830d2009-04-22 16:03:05 -0400545 next = hub->tt.clear_list.next;
546 clear = list_entry (next, struct usb_tt_clear, clear_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 list_del (&clear->clear_list);
548
549 /* drop lock so HCD can concurrently report other TT errors */
550 spin_unlock_irqrestore (&hub->tt.lock, flags);
551 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (status)
553 dev_err (&hdev->dev,
554 "clear tt %d (%04x) error %d\n",
555 clear->tt, clear->devinfo, status);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400556
557 /* Tell the HCD, even if the operation failed */
558 drv = clear->hcd->driver;
559 if (drv->clear_tt_buffer_complete)
560 (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep);
561
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700562 kfree(clear);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400563 spin_lock_irqsave(&hub->tt.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565 spin_unlock_irqrestore (&hub->tt.lock, flags);
566}
567
568/**
Alan Sterncb88a1b2009-06-29 10:43:32 -0400569 * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub
570 * @urb: an URB associated with the failed or incomplete split transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 *
572 * High speed HCDs use this to tell the hub driver that some split control or
573 * bulk transaction failed in a way that requires clearing internal state of
574 * a transaction translator. This is normally detected (and reported) from
575 * interrupt context.
576 *
577 * It may not be possible for that hub to handle additional full (or low)
578 * speed transactions until that state is fully cleared out.
579 */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400580int usb_hub_clear_tt_buffer(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Alan Sterncb88a1b2009-06-29 10:43:32 -0400582 struct usb_device *udev = urb->dev;
583 int pipe = urb->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct usb_tt *tt = udev->tt;
585 unsigned long flags;
586 struct usb_tt_clear *clear;
587
588 /* we've got to cope with an arbitrary number of pending TT clears,
589 * since each TT has "at least two" buffers that can need it (and
590 * there can be many TTs per hub). even if they're uncommon.
591 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800592 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
594 /* FIXME recover somehow ... RESET_TT? */
Alan Sterncb88a1b2009-06-29 10:43:32 -0400595 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
598 /* info that CLEAR_TT_BUFFER needs */
599 clear->tt = tt->multi ? udev->ttport : 1;
600 clear->devinfo = usb_pipeendpoint (pipe);
601 clear->devinfo |= udev->devnum << 4;
602 clear->devinfo |= usb_pipecontrol (pipe)
603 ? (USB_ENDPOINT_XFER_CONTROL << 11)
604 : (USB_ENDPOINT_XFER_BULK << 11);
605 if (usb_pipein (pipe))
606 clear->devinfo |= 1 << 15;
Alan Sterncb88a1b2009-06-29 10:43:32 -0400607
608 /* info for completion callback */
609 clear->hcd = bus_to_hcd(udev->bus);
610 clear->ep = urb->ep;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* tell keventd to clear state for this TT */
613 spin_lock_irqsave (&tt->lock, flags);
614 list_add_tail (&clear->clear_list, &tt->clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400615 schedule_work(&tt->clear_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 spin_unlock_irqrestore (&tt->lock, flags);
Alan Sterncb88a1b2009-06-29 10:43:32 -0400617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
Alan Sterncb88a1b2009-06-29 10:43:32 -0400619EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Alan Stern8520f382008-09-22 14:44:26 -0400621/* If do_delay is false, return the number of milliseconds the caller
622 * needs to delay.
623 */
624static unsigned hub_power_on(struct usb_hub *hub, bool do_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700627 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern8520f382008-09-22 14:44:26 -0400628 unsigned delay;
Alan Stern4489a572006-04-27 15:54:22 -0400629 u16 wHubCharacteristics =
630 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Alan Stern4489a572006-04-27 15:54:22 -0400632 /* Enable power on each port. Some hubs have reserved values
633 * of LPSM (> 2) in their descriptors, even though they are
634 * USB 2.0 hubs. Some hubs do not implement port-power switching
635 * but only emulate it. In all cases, the ports won't work
636 * unless we send these messages to the hub.
637 */
638 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400640 else
641 dev_dbg(hub->intfdev, "trying to enable port power on "
642 "non-switchable hub\n");
643 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
644 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
David Brownellb7896962005-08-31 10:41:44 -0700646 /* Wait at least 100 msec for power to become stable */
Alan Stern8520f382008-09-22 14:44:26 -0400647 delay = max(pgood_delay, (unsigned) 100);
648 if (do_delay)
649 msleep(delay);
650 return delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653static int hub_hub_status(struct usb_hub *hub,
654 u16 *status, u16 *change)
655{
656 int ret;
657
Alan Sterndb90e7a2007-02-05 09:56:15 -0500658 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ret = get_hub_status(hub->hdev, &hub->status->hub);
660 if (ret < 0)
661 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800662 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 else {
664 *status = le16_to_cpu(hub->status->hub.wHubStatus);
665 *change = le16_to_cpu(hub->status->hub.wHubChange);
666 ret = 0;
667 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500668 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return ret;
670}
671
Alan Stern8b28c752005-08-10 17:04:13 -0400672static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
673{
674 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400675 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400676
Alan Stern0458d5b2007-05-04 11:52:20 -0400677 if (hdev->children[port1-1] && set_state)
Alan Stern8b28c752005-08-10 17:04:13 -0400678 usb_set_device_state(hdev->children[port1-1],
679 USB_STATE_NOTATTACHED);
John Youndbe79bb2001-09-17 00:00:00 -0700680 if (!hub->error && !hub_is_superspeed(hub->hdev))
Alan Stern0458d5b2007-05-04 11:52:20 -0400681 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400682 if (ret)
683 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400684 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400685 return ret;
686}
687
Alan Stern0458d5b2007-05-04 11:52:20 -0400688/*
Justin P. Mattock6d42fcd2011-02-26 20:33:56 -0800689 * Disable a port and mark a logical connect-change event, so that some
Alan Stern0458d5b2007-05-04 11:52:20 -0400690 * time later khubd will disconnect() any existing usb_device on the port
691 * and will re-enumerate if there actually is a device attached.
692 */
693static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
694{
695 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
696 hub_port_disable(hub, port1, 1);
697
698 /* FIXME let caller ask to power down the port:
699 * - some devices won't enumerate without a VBUS power cycle
700 * - SRP saves power that way
701 * - ... new call, TBD ...
702 * That's easy if this hub can switch power per-port, and
703 * khubd reactivates the port later (timer, SRP, etc).
704 * Powerdown must be optional, because of reset/DFU.
705 */
706
707 set_bit(port1, hub->change_bits);
708 kick_khubd(hub);
709}
710
Alan Stern253e0572009-10-27 15:20:13 -0400711/**
712 * usb_remove_device - disable a device's port on its parent hub
713 * @udev: device to be disabled and removed
714 * Context: @udev locked, must be able to sleep.
715 *
716 * After @udev's port has been disabled, khubd is notified and it will
717 * see that the device has been disconnected. When the device is
718 * physically unplugged and something is plugged in, the events will
719 * be received and processed normally.
720 */
721int usb_remove_device(struct usb_device *udev)
722{
723 struct usb_hub *hub;
724 struct usb_interface *intf;
725
726 if (!udev->parent) /* Can't remove a root hub */
727 return -EINVAL;
728 hub = hdev_to_hub(udev->parent);
729 intf = to_usb_interface(hub->intfdev);
730
731 usb_autopm_get_interface(intf);
732 set_bit(udev->portnum, hub->removed_bits);
733 hub_port_logical_disconnect(hub, udev->portnum);
734 usb_autopm_put_interface(intf);
735 return 0;
736}
737
Alan Stern6ee0b272008-04-28 11:06:42 -0400738enum hub_activation_type {
Alan Stern8e4ceb32009-12-07 13:01:37 -0500739 HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */
Alan Stern8520f382008-09-22 14:44:26 -0400740 HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME,
Alan Stern6ee0b272008-04-28 11:06:42 -0400741};
Alan Stern5e6effa2008-03-03 15:15:51 -0500742
Alan Stern8520f382008-09-22 14:44:26 -0400743static void hub_init_func2(struct work_struct *ws);
744static void hub_init_func3(struct work_struct *ws);
745
Alan Sternf2835212008-04-28 11:07:17 -0400746static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500747{
748 struct usb_device *hdev = hub->hdev;
Sarah Sharp653a39d2010-12-23 11:12:42 -0800749 struct usb_hcd *hcd;
750 int ret;
Alan Stern5e6effa2008-03-03 15:15:51 -0500751 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400752 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400753 bool need_debounce_delay = false;
Alan Stern8520f382008-09-22 14:44:26 -0400754 unsigned delay;
755
756 /* Continue a partial initialization */
757 if (type == HUB_INIT2)
758 goto init2;
759 if (type == HUB_INIT3)
760 goto init3;
Alan Stern5e6effa2008-03-03 15:15:51 -0500761
Elric Fua45aa3b2012-02-18 13:32:27 +0800762 /* The superspeed hub except for root hub has to use Hub Depth
763 * value as an offset into the route string to locate the bits
764 * it uses to determine the downstream port number. So hub driver
765 * should send a set hub depth request to superspeed hub after
766 * the superspeed hub is set configuration in initialization or
767 * reset procedure.
768 *
769 * After a resume, port power should still be on.
Alan Sternf2835212008-04-28 11:07:17 -0400770 * For any other type of activation, turn it on.
771 */
Alan Stern8520f382008-09-22 14:44:26 -0400772 if (type != HUB_RESUME) {
Elric Fua45aa3b2012-02-18 13:32:27 +0800773 if (hdev->parent && hub_is_superspeed(hdev)) {
774 ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
775 HUB_SET_DEPTH, USB_RT_HUB,
776 hdev->level - 1, 0, NULL, 0,
777 USB_CTRL_SET_TIMEOUT);
778 if (ret < 0)
779 dev_err(hub->intfdev,
780 "set hub depth failed\n");
781 }
Alan Stern8520f382008-09-22 14:44:26 -0400782
783 /* Speed up system boot by using a delayed_work for the
784 * hub's initial power-up delays. This is pretty awkward
785 * and the implementation looks like a home-brewed sort of
786 * setjmp/longjmp, but it saves at least 100 ms for each
787 * root hub (assuming usbcore is compiled into the kernel
788 * rather than as a module). It adds up.
789 *
790 * This can't be done for HUB_RESUME or HUB_RESET_RESUME
791 * because for those activation types the ports have to be
792 * operational when we return. In theory this could be done
793 * for HUB_POST_RESET, but it's easier not to.
794 */
795 if (type == HUB_INIT) {
796 delay = hub_power_on(hub, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700797#ifdef CONFIG_USB_OTG
798 if (hdev->bus->is_b_host)
799 goto init2;
800#endif
Alan Stern8520f382008-09-22 14:44:26 -0400801 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2);
802 schedule_delayed_work(&hub->init_work,
803 msecs_to_jiffies(delay));
Alan Stern61fbeba2008-10-27 12:07:44 -0400804
805 /* Suppress autosuspend until init is done */
Alan Stern8e4ceb32009-12-07 13:01:37 -0500806 usb_autopm_get_interface_no_resume(
807 to_usb_interface(hub->intfdev));
Alan Stern8520f382008-09-22 14:44:26 -0400808 return; /* Continues at init2: below */
Sarah Sharp653a39d2010-12-23 11:12:42 -0800809 } else if (type == HUB_RESET_RESUME) {
810 /* The internal host controller state for the hub device
811 * may be gone after a host power loss on system resume.
812 * Update the device's info so the HW knows it's a hub.
813 */
814 hcd = bus_to_hcd(hdev->bus);
815 if (hcd->driver->update_hub_device) {
816 ret = hcd->driver->update_hub_device(hcd, hdev,
817 &hub->tt, GFP_NOIO);
818 if (ret < 0) {
819 dev_err(hub->intfdev, "Host not "
820 "accepting hub info "
821 "update.\n");
822 dev_err(hub->intfdev, "LS/FS devices "
823 "and hubs may not work "
824 "under this hub\n.");
825 }
826 }
827 hub_power_on(hub, true);
Alan Stern8520f382008-09-22 14:44:26 -0400828 } else {
829 hub_power_on(hub, true);
830 }
831 }
832 init2:
Alan Sternf2835212008-04-28 11:07:17 -0400833
Alan Stern6ee0b272008-04-28 11:06:42 -0400834 /* Check each port and set hub->change_bits to let khubd know
835 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -0500836 */
837 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
838 struct usb_device *udev = hdev->children[port1-1];
Alan Stern5e6effa2008-03-03 15:15:51 -0500839 u16 portstatus, portchange;
840
Alan Stern6ee0b272008-04-28 11:06:42 -0400841 portstatus = portchange = 0;
842 status = hub_port_status(hub, port1, &portstatus, &portchange);
843 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
844 dev_dbg(hub->intfdev,
845 "port %d: status %04x change %04x\n",
846 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -0500847
Alan Stern6ee0b272008-04-28 11:06:42 -0400848 /* After anything other than HUB_RESUME (i.e., initialization
849 * or any sort of reset), every port should be disabled.
850 * Unconnected ports should likewise be disabled (paranoia),
851 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -0500852 */
Alan Stern6ee0b272008-04-28 11:06:42 -0400853 if ((portstatus & USB_PORT_STAT_ENABLE) && (
854 type != HUB_RESUME ||
855 !(portstatus & USB_PORT_STAT_CONNECTION) ||
856 !udev ||
857 udev->state == USB_STATE_NOTATTACHED)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +0800858 /*
859 * USB3 protocol ports will automatically transition
860 * to Enabled state when detect an USB3.0 device attach.
861 * Do not disable USB3 protocol ports.
Andiry Xu9f0a6cd2010-05-07 18:09:27 +0800862 */
Sarah Sharp131dec32010-12-06 21:00:19 -0800863 if (!hub_is_superspeed(hdev)) {
Andiry Xu9f0a6cd2010-05-07 18:09:27 +0800864 clear_port_feature(hdev, port1,
865 USB_PORT_FEAT_ENABLE);
866 portstatus &= ~USB_PORT_STAT_ENABLE;
Sarah Sharp85f0ff42010-10-14 07:22:54 -0700867 } else {
868 /* Pretend that power was lost for USB3 devs */
869 portstatus &= ~USB_PORT_STAT_ENABLE;
Andiry Xu9f0a6cd2010-05-07 18:09:27 +0800870 }
Alan Stern6ee0b272008-04-28 11:06:42 -0400871 }
872
Alan Stern948fea32008-04-28 11:07:07 -0400873 /* Clear status-change flags; we'll debounce later */
874 if (portchange & USB_PORT_STAT_C_CONNECTION) {
875 need_debounce_delay = true;
876 clear_port_feature(hub->hdev, port1,
877 USB_PORT_FEAT_C_CONNECTION);
878 }
879 if (portchange & USB_PORT_STAT_C_ENABLE) {
880 need_debounce_delay = true;
881 clear_port_feature(hub->hdev, port1,
882 USB_PORT_FEAT_C_ENABLE);
883 }
Don Zickus79c3dd82011-11-03 09:07:18 -0400884 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
885 hub_is_superspeed(hub->hdev)) {
886 need_debounce_delay = true;
887 clear_port_feature(hub->hdev, port1,
888 USB_PORT_FEAT_C_BH_PORT_RESET);
889 }
Alan Stern253e0572009-10-27 15:20:13 -0400890 /* We can forget about a "removed" device when there's a
891 * physical disconnect or the connect status changes.
892 */
893 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
894 (portchange & USB_PORT_STAT_C_CONNECTION))
895 clear_bit(port1, hub->removed_bits);
896
Alan Stern6ee0b272008-04-28 11:06:42 -0400897 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
898 /* Tell khubd to disconnect the device or
899 * check for a new connection
900 */
901 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
902 set_bit(port1, hub->change_bits);
903
904 } else if (portstatus & USB_PORT_STAT_ENABLE) {
Sarah Sharp72937e12012-01-24 11:46:50 -0800905 bool port_resumed = (portstatus &
906 USB_PORT_STAT_LINK_STATE) ==
907 USB_SS_PORT_LS_U0;
Alan Stern6ee0b272008-04-28 11:06:42 -0400908 /* The power session apparently survived the resume.
909 * If there was an overcurrent or suspend change
910 * (i.e., remote wakeup request), have khubd
Sarah Sharp72937e12012-01-24 11:46:50 -0800911 * take care of it. Look at the port link state
912 * for USB 3.0 hubs, since they don't have a suspend
913 * change bit, and they don't set the port link change
914 * bit on device-initiated resume.
Alan Stern6ee0b272008-04-28 11:06:42 -0400915 */
Sarah Sharp72937e12012-01-24 11:46:50 -0800916 if (portchange || (hub_is_superspeed(hub->hdev) &&
917 port_resumed))
Alan Stern6ee0b272008-04-28 11:06:42 -0400918 set_bit(port1, hub->change_bits);
919
920 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -0400921#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -0500922 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -0400923#endif
Alan Stern8808f002008-04-28 11:06:55 -0400924 set_bit(port1, hub->change_bits);
925
Alan Stern6ee0b272008-04-28 11:06:42 -0400926 } else {
927 /* The power session is gone; tell khubd */
928 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
929 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -0500930 }
Alan Stern5e6effa2008-03-03 15:15:51 -0500931 }
932
Alan Stern948fea32008-04-28 11:07:07 -0400933 /* If no port-status-change flags were set, we don't need any
934 * debouncing. If flags were set we can try to debounce the
935 * ports all at once right now, instead of letting khubd do them
936 * one at a time later on.
937 *
938 * If any port-status changes do occur during this delay, khubd
939 * will see them later and handle them normally.
940 */
Alan Stern8520f382008-09-22 14:44:26 -0400941 if (need_debounce_delay) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942#ifdef CONFIG_USB_OTG
943 if (hdev->bus->is_b_host && type == HUB_INIT)
944 goto init3;
945#endif
946
Alan Stern8520f382008-09-22 14:44:26 -0400947 delay = HUB_DEBOUNCE_STABLE;
Alan Sternf2835212008-04-28 11:07:17 -0400948
Alan Stern8520f382008-09-22 14:44:26 -0400949 /* Don't do a long sleep inside a workqueue routine */
950 if (type == HUB_INIT2) {
951 PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3);
952 schedule_delayed_work(&hub->init_work,
953 msecs_to_jiffies(delay));
954 return; /* Continues at init3: below */
955 } else {
956 msleep(delay);
957 }
958 }
959 init3:
Alan Sternf2835212008-04-28 11:07:17 -0400960 hub->quiescing = 0;
961
962 status = usb_submit_urb(hub->urb, GFP_NOIO);
963 if (status < 0)
964 dev_err(hub->intfdev, "activate --> %d\n", status);
965 if (hub->has_indicators && blinkenlights)
966 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
967
968 /* Scan all ports that need attention */
969 kick_khubd(hub);
Alan Stern8e4ceb32009-12-07 13:01:37 -0500970
971 /* Allow autosuspend if it was suppressed */
972 if (type <= HUB_INIT3)
973 usb_autopm_put_interface_async(to_usb_interface(hub->intfdev));
Alan Stern5e6effa2008-03-03 15:15:51 -0500974}
975
Alan Stern8520f382008-09-22 14:44:26 -0400976/* Implement the continuations for the delays above */
977static void hub_init_func2(struct work_struct *ws)
978{
979 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
980
981 hub_activate(hub, HUB_INIT2);
982}
983
984static void hub_init_func3(struct work_struct *ws)
985{
986 struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work);
987
988 hub_activate(hub, HUB_INIT3);
989}
990
Alan Stern43303542008-04-28 11:07:31 -0400991enum hub_quiescing_type {
992 HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND
993};
994
995static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type)
996{
997 struct usb_device *hdev = hub->hdev;
998 int i;
999
Alan Stern8520f382008-09-22 14:44:26 -04001000 cancel_delayed_work_sync(&hub->init_work);
1001
Alan Stern43303542008-04-28 11:07:31 -04001002 /* khubd and related activity won't re-trigger */
1003 hub->quiescing = 1;
1004
1005 if (type != HUB_SUSPEND) {
1006 /* Disconnect all the children */
1007 for (i = 0; i < hdev->maxchild; ++i) {
1008 if (hdev->children[i])
1009 usb_disconnect(&hdev->children[i]);
1010 }
1011 }
1012
1013 /* Stop khubd and related activity */
1014 usb_kill_urb(hub->urb);
1015 if (hub->has_indicators)
1016 cancel_delayed_work_sync(&hub->leds);
1017 if (hub->tt.hub)
Alan Sterncb88a1b2009-06-29 10:43:32 -04001018 cancel_work_sync(&hub->tt.clear_work);
Alan Stern43303542008-04-28 11:07:31 -04001019}
1020
Alan Stern3eb14912008-03-03 15:15:43 -05001021/* caller has locked the hub device */
1022static int hub_pre_reset(struct usb_interface *intf)
1023{
1024 struct usb_hub *hub = usb_get_intfdata(intf);
1025
Alan Stern43303542008-04-28 11:07:31 -04001026 hub_quiesce(hub, HUB_PRE_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001027 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001028}
1029
1030/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -04001031static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -05001032{
Alan Stern7de18d82006-06-01 13:37:24 -04001033 struct usb_hub *hub = usb_get_intfdata(intf);
1034
Alan Sternf2835212008-04-28 11:07:17 -04001035 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -04001036 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -05001037}
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039static int hub_configure(struct usb_hub *hub,
1040 struct usb_endpoint_descriptor *endpoint)
1041{
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001042 struct usb_hcd *hcd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 struct usb_device *hdev = hub->hdev;
1044 struct device *hub_dev = hub->intfdev;
1045 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001046 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 unsigned int pipe;
1048 int maxp, ret;
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001049 char *message = "out of memory";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Alan Sternd697cdd2009-10-27 15:18:46 -04001051 hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (!hub->buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 ret = -ENOMEM;
1054 goto fail;
1055 }
1056
1057 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
1058 if (!hub->status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ret = -ENOMEM;
1060 goto fail;
1061 }
Alan Sterndb90e7a2007-02-05 09:56:15 -05001062 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
1065 if (!hub->descriptor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 ret = -ENOMEM;
1067 goto fail;
1068 }
1069
1070 /* Request the entire hub descriptor.
1071 * hub->descriptor can handle USB_MAXCHILDREN ports,
1072 * but the hub can/will return fewer bytes here.
1073 */
John Youndbe79bb2001-09-17 00:00:00 -07001074 ret = get_hub_descriptor(hdev, hub->descriptor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (ret < 0) {
1076 message = "can't read hub descriptor";
1077 goto fail;
1078 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
1079 message = "hub has too many ports!";
1080 ret = -ENODEV;
1081 goto fail;
1082 }
1083
1084 hdev->maxchild = hub->descriptor->bNbrPorts;
1085 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
1086 (hdev->maxchild == 1) ? "" : "s");
1087
Huajun Li88162302012-03-12 21:00:19 +08001088 hdev->children = kzalloc(hdev->maxchild *
1089 sizeof(struct usb_device *), GFP_KERNEL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001090 hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL);
Huajun Li88162302012-03-12 21:00:19 +08001091 if (!hdev->children || !hub->port_owners) {
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001092 ret = -ENOMEM;
1093 goto fail;
1094 }
1095
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001096 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
John Youndbe79bb2001-09-17 00:00:00 -07001098 /* FIXME for USB 3.0, skip for now */
1099 if ((wHubCharacteristics & HUB_CHAR_COMPOUND) &&
1100 !(hub_is_superspeed(hdev))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int i;
1102 char portstr [USB_MAXCHILDREN + 1];
1103
1104 for (i = 0; i < hdev->maxchild; i++)
John Youndbe79bb2001-09-17 00:00:00 -07001105 portstr[i] = hub->descriptor->u.hs.DeviceRemovable
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
1107 ? 'F' : 'R';
1108 portstr[hdev->maxchild] = 0;
1109 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
1110 } else
1111 dev_dbg(hub_dev, "standalone hub\n");
1112
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001113 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301114 case HUB_CHAR_COMMON_LPSM:
1115 dev_dbg(hub_dev, "ganged power switching\n");
1116 break;
1117 case HUB_CHAR_INDV_PORT_LPSM:
1118 dev_dbg(hub_dev, "individual port power switching\n");
1119 break;
1120 case HUB_CHAR_NO_LPSM:
1121 case HUB_CHAR_LPSM:
1122 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
1123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001126 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Aman Deep7bf01182011-12-08 12:05:22 +05301127 case HUB_CHAR_COMMON_OCPM:
1128 dev_dbg(hub_dev, "global over-current protection\n");
1129 break;
1130 case HUB_CHAR_INDV_PORT_OCPM:
1131 dev_dbg(hub_dev, "individual port over-current protection\n");
1132 break;
1133 case HUB_CHAR_NO_OCPM:
1134 case HUB_CHAR_OCPM:
1135 dev_dbg(hub_dev, "no over-current protection\n");
1136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
1139 spin_lock_init (&hub->tt.lock);
1140 INIT_LIST_HEAD (&hub->tt.clear_list);
Alan Sterncb88a1b2009-06-29 10:43:32 -04001141 INIT_WORK(&hub->tt.clear_work, hub_tt_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 switch (hdev->descriptor.bDeviceProtocol) {
Aman Deep7bf01182011-12-08 12:05:22 +05301143 case USB_HUB_PR_FS:
1144 break;
1145 case USB_HUB_PR_HS_SINGLE_TT:
1146 dev_dbg(hub_dev, "Single TT\n");
1147 hub->tt.hub = hdev;
1148 break;
1149 case USB_HUB_PR_HS_MULTI_TT:
1150 ret = usb_set_interface(hdev, 0, 1);
1151 if (ret == 0) {
1152 dev_dbg(hub_dev, "TT per port\n");
1153 hub->tt.multi = 1;
1154 } else
1155 dev_err(hub_dev, "Using single TT (err %d)\n",
1156 ret);
1157 hub->tt.hub = hdev;
1158 break;
1159 case USB_HUB_PR_SS:
1160 /* USB 3.0 hubs don't have a TT */
1161 break;
1162 default:
1163 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
1164 hdev->descriptor.bDeviceProtocol);
1165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001168 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001169 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001170 case HUB_TTTT_8_BITS:
1171 if (hdev->descriptor.bDeviceProtocol != 0) {
1172 hub->tt.think_time = 666;
1173 dev_dbg(hub_dev, "TT requires at most %d "
1174 "FS bit times (%d ns)\n",
1175 8, hub->tt.think_time);
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001178 case HUB_TTTT_16_BITS:
1179 hub->tt.think_time = 666 * 2;
1180 dev_dbg(hub_dev, "TT requires at most %d "
1181 "FS bit times (%d ns)\n",
1182 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001184 case HUB_TTTT_24_BITS:
1185 hub->tt.think_time = 666 * 3;
1186 dev_dbg(hub_dev, "TT requires at most %d "
1187 "FS bit times (%d ns)\n",
1188 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -07001190 case HUB_TTTT_32_BITS:
1191 hub->tt.think_time = 666 * 4;
1192 dev_dbg(hub_dev, "TT requires at most %d "
1193 "FS bit times (%d ns)\n",
1194 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
1196 }
1197
1198 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001199 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 hub->has_indicators = 1;
1201 dev_dbg(hub_dev, "Port indicators are supported\n");
1202 }
1203
1204 dev_dbg(hub_dev, "power on to power good time: %dms\n",
1205 hub->descriptor->bPwrOn2PwrGood * 2);
1206
1207 /* power budgeting mostly matters with bus-powered hubs,
1208 * and battery-powered root hubs (may provide just 8 mA).
1209 */
1210 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -05001211 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 message = "can't get hub status";
1213 goto fail;
1214 }
Alan Stern7d35b922005-04-25 11:18:32 -04001215 le16_to_cpus(&hubstatus);
1216 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -05001217 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
1218 hub->mA_per_port = 500;
1219 else {
1220 hub->mA_per_port = hdev->bus_mA;
1221 hub->limited_power = 1;
1222 }
Alan Stern7d35b922005-04-25 11:18:32 -04001223 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
1225 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -05001226 hub->limited_power = 1;
1227 if (hdev->maxchild > 0) {
1228 int remaining = hdev->bus_mA -
1229 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Alan Stern55c52712005-11-23 12:03:12 -05001231 if (remaining < hdev->maxchild * 100)
1232 dev_warn(hub_dev,
1233 "insufficient power available "
1234 "to use all downstream ports\n");
1235 hub->mA_per_port = 100; /* 7.2.1.1 */
1236 }
1237 } else { /* Self-powered external hub */
1238 /* FIXME: What about battery-powered external hubs that
1239 * provide less current per port? */
1240 hub->mA_per_port = 500;
1241 }
1242 if (hub->mA_per_port < 500)
1243 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
1244 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Sarah Sharpb356b7c2009-09-04 10:53:24 -07001246 /* Update the HCD's internal representation of this hub before khubd
1247 * starts getting port status changes for devices under the hub.
1248 */
1249 hcd = bus_to_hcd(hdev->bus);
1250 if (hcd->driver->update_hub_device) {
1251 ret = hcd->driver->update_hub_device(hcd, hdev,
1252 &hub->tt, GFP_KERNEL);
1253 if (ret < 0) {
1254 message = "can't update HCD hub info";
1255 goto fail;
1256 }
1257 }
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 ret = hub_hub_status(hub, &hubstatus, &hubchange);
1260 if (ret < 0) {
1261 message = "can't get hub status";
1262 goto fail;
1263 }
1264
1265 /* local power status reports aren't always correct */
1266 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
1267 dev_dbg(hub_dev, "local power source is %s\n",
1268 (hubstatus & HUB_STATUS_LOCAL_POWER)
1269 ? "lost (inactive)" : "good");
1270
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07001271 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 dev_dbg(hub_dev, "%sover-current condition exists\n",
1273 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
1274
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -07001275 /* set up the interrupt endpoint
1276 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
1277 * bytes as USB2.0[11.12.3] says because some hubs are known
1278 * to send more data (and thus cause overflow). For root hubs,
1279 * maxpktsize is defined in hcd.c's fake endpoint descriptors
1280 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
1282 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
1283
1284 if (maxp > sizeof(*hub->buffer))
1285 maxp = sizeof(*hub->buffer);
1286
1287 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
1288 if (!hub->urb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 ret = -ENOMEM;
1290 goto fail;
1291 }
1292
1293 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
1294 hub, endpoint->bInterval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /* maybe cycle the hub leds */
1297 if (hub->has_indicators && blinkenlights)
1298 hub->indicator [0] = INDICATOR_CYCLE;
1299
Alan Sternf2835212008-04-28 11:07:17 -04001300 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return 0;
1302
1303fail:
1304 dev_err (hub_dev, "config failed, %s (err %d)\n",
1305 message, ret);
1306 /* hub_disconnect() frees urb and descriptor */
1307 return ret;
1308}
1309
Alan Sterne8054852007-05-04 11:55:11 -04001310static void hub_release(struct kref *kref)
1311{
1312 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1313
1314 usb_put_intf(to_usb_interface(hub->intfdev));
1315 kfree(hub);
1316}
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318static unsigned highspeed_hubs;
1319
1320static void hub_disconnect(struct usb_interface *intf)
1321{
Huajun Li88162302012-03-12 21:00:19 +08001322 struct usb_hub *hub = usb_get_intfdata(intf);
1323 struct usb_device *hdev = interface_to_usbdev(intf);
Alan Sterne8054852007-05-04 11:55:11 -04001324
1325 /* Take the hub off the event list and don't let it be added again */
1326 spin_lock_irq(&hub_event_lock);
Alan Stern8e4ceb32009-12-07 13:01:37 -05001327 if (!list_empty(&hub->event_list)) {
1328 list_del_init(&hub->event_list);
1329 usb_autopm_put_interface_no_suspend(intf);
1330 }
Alan Sterne8054852007-05-04 11:55:11 -04001331 hub->disconnected = 1;
1332 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Alan Stern7de18d82006-06-01 13:37:24 -04001334 /* Disconnect all children and quiesce the hub */
1335 hub->error = 0;
Alan Stern43303542008-04-28 11:07:31 -04001336 hub_quiesce(hub, HUB_DISCONNECT);
Alan Stern7de18d82006-06-01 13:37:24 -04001337
Alan Stern8b28c752005-08-10 17:04:13 -04001338 usb_set_intfdata (intf, NULL);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001339 hub->hdev->maxchild = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Alan Sterne8054852007-05-04 11:55:11 -04001341 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 highspeed_hubs--;
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 usb_free_urb(hub->urb);
Huajun Li88162302012-03-12 21:00:19 +08001345 kfree(hdev->children);
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001346 kfree(hub->port_owners);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001347 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001348 kfree(hub->status);
Alan Sternd697cdd2009-10-27 15:18:46 -04001349 kfree(hub->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Alan Sterne8054852007-05-04 11:55:11 -04001351 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352}
1353
1354static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1355{
1356 struct usb_host_interface *desc;
1357 struct usb_endpoint_descriptor *endpoint;
1358 struct usb_device *hdev;
1359 struct usb_hub *hub;
1360
1361 desc = intf->cur_altsetting;
1362 hdev = interface_to_usbdev(intf);
1363
Sarah Sharp2839f5b2012-01-06 16:27:25 -08001364 /* Hubs have proper suspend/resume support. */
1365 usb_enable_autosuspend(hdev);
Alan Stern088f7fe2010-01-08 12:56:54 -05001366
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001367 if (hdev->level == MAX_TOPO_LEVEL) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001368 dev_err(&intf->dev,
1369 "Unsupported bus topology: hub nested too deep\n");
Felipe Balbi38f3ad52008-06-12 10:49:47 +03001370 return -E2BIG;
1371 }
1372
David Brownell89ccbdc2006-04-02 10:18:09 -08001373#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1374 if (hdev->parent) {
1375 dev_warn(&intf->dev, "ignoring external hub\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001376 otg_send_event(OTG_EVENT_HUB_NOT_SUPPORTED);
David Brownell89ccbdc2006-04-02 10:18:09 -08001377 return -ENODEV;
1378 }
1379#endif
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 /* Some hubs have a subclass of 1, which AFAICT according to the */
1382 /* specs is not defined, but it works */
1383 if ((desc->desc.bInterfaceSubClass != 0) &&
1384 (desc->desc.bInterfaceSubClass != 1)) {
1385descriptor_error:
1386 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1387 return -EIO;
1388 }
1389
1390 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1391 if (desc->desc.bNumEndpoints != 1)
1392 goto descriptor_error;
1393
1394 endpoint = &desc->endpoint[0].desc;
1395
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001396 /* If it's not an interrupt in endpoint, we'd better punt! */
1397 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 goto descriptor_error;
1399
1400 /* We found a hub */
1401 dev_info (&intf->dev, "USB hub found\n");
1402
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001403 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (!hub) {
1405 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1406 return -ENOMEM;
1407 }
1408
Alan Sterne8054852007-05-04 11:55:11 -04001409 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 INIT_LIST_HEAD(&hub->event_list);
1411 hub->intfdev = &intf->dev;
1412 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001413 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Stern8520f382008-09-22 14:44:26 -04001414 INIT_DELAYED_WORK(&hub->init_work, NULL);
Alan Sterne8054852007-05-04 11:55:11 -04001415 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001418 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 if (hdev->speed == USB_SPEED_HIGH)
1421 highspeed_hubs++;
1422
1423 if (hub_configure(hub, endpoint) >= 0)
1424 return 0;
1425
1426 hub_disconnect (intf);
1427 return -ENODEV;
1428}
1429
1430static int
1431hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1432{
1433 struct usb_device *hdev = interface_to_usbdev (intf);
1434
1435 /* assert ifno == 0 (part of hub spec) */
1436 switch (code) {
1437 case USBDEVFS_HUB_PORTINFO: {
1438 struct usbdevfs_hub_portinfo *info = user_data;
1439 int i;
1440
1441 spin_lock_irq(&device_state_lock);
1442 if (hdev->devnum <= 0)
1443 info->nports = 0;
1444 else {
1445 info->nports = hdev->maxchild;
1446 for (i = 0; i < info->nports; i++) {
1447 if (hdev->children[i] == NULL)
1448 info->port[i] = 0;
1449 else
1450 info->port[i] =
1451 hdev->children[i]->devnum;
1452 }
1453 }
1454 spin_unlock_irq(&device_state_lock);
1455
1456 return info->nports + 1;
1457 }
1458
1459 default:
1460 return -ENOSYS;
1461 }
1462}
1463
Alan Stern7cbe5dc2009-06-29 10:56:54 -04001464/*
1465 * Allow user programs to claim ports on a hub. When a device is attached
1466 * to one of these "claimed" ports, the program will "own" the device.
1467 */
1468static int find_port_owner(struct usb_device *hdev, unsigned port1,
1469 void ***ppowner)
1470{
1471 if (hdev->state == USB_STATE_NOTATTACHED)
1472 return -ENODEV;
1473 if (port1 == 0 || port1 > hdev->maxchild)
1474 return -EINVAL;
1475
1476 /* This assumes that devices not managed by the hub driver
1477 * will always have maxchild equal to 0.
1478 */
1479 *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]);
1480 return 0;
1481}
1482
1483/* In the following three functions, the caller must hold hdev's lock */
1484int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner)
1485{
1486 int rc;
1487 void **powner;
1488
1489 rc = find_port_owner(hdev, port1, &powner);
1490 if (rc)
1491 return rc;
1492 if (*powner)
1493 return -EBUSY;
1494 *powner = owner;
1495 return rc;
1496}
1497
1498int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner)
1499{
1500 int rc;
1501 void **powner;
1502
1503 rc = find_port_owner(hdev, port1, &powner);
1504 if (rc)
1505 return rc;
1506 if (*powner != owner)
1507 return -ENOENT;
1508 *powner = NULL;
1509 return rc;
1510}
1511
1512void usb_hub_release_all_ports(struct usb_device *hdev, void *owner)
1513{
1514 int n;
1515 void **powner;
1516
1517 n = find_port_owner(hdev, 1, &powner);
1518 if (n == 0) {
1519 for (; n < hdev->maxchild; (++n, ++powner)) {
1520 if (*powner == owner)
1521 *powner = NULL;
1522 }
1523 }
1524}
1525
1526/* The caller must hold udev's lock */
1527bool usb_device_is_owned(struct usb_device *udev)
1528{
1529 struct usb_hub *hub;
1530
1531 if (udev->state == USB_STATE_NOTATTACHED || !udev->parent)
1532 return false;
1533 hub = hdev_to_hub(udev->parent);
1534 return !!hub->port_owners[udev->portnum - 1];
1535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1539{
1540 int i;
1541
1542 for (i = 0; i < udev->maxchild; ++i) {
1543 if (udev->children[i])
1544 recursively_mark_NOTATTACHED(udev->children[i]);
1545 }
Alan Stern9bbdf1e2010-01-08 12:57:28 -05001546 if (udev->state == USB_STATE_SUSPENDED)
Sarah Sharp15123002007-12-21 16:54:15 -08001547 udev->active_duration -= jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 udev->state = USB_STATE_NOTATTACHED;
1549}
1550
1551/**
1552 * usb_set_device_state - change a device's current state (usbcore, hcds)
1553 * @udev: pointer to device whose state should be changed
1554 * @new_state: new state value to be stored
1555 *
1556 * udev->state is _not_ fully protected by the device lock. Although
1557 * most transitions are made only while holding the lock, the state can
1558 * can change to USB_STATE_NOTATTACHED at almost any time. This
1559 * is so that devices can be marked as disconnected as soon as possible,
1560 * without having to wait for any semaphores to be released. As a result,
1561 * all changes to any device's state must be protected by the
1562 * device_state_lock spinlock.
1563 *
1564 * Once a device has been added to the device tree, all changes to its state
1565 * should be made using this routine. The state should _not_ be set directly.
1566 *
1567 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1568 * Otherwise udev->state is set to new_state, and if new_state is
1569 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1570 * to USB_STATE_NOTATTACHED.
1571 */
1572void usb_set_device_state(struct usb_device *udev,
1573 enum usb_device_state new_state)
1574{
1575 unsigned long flags;
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001576 int wakeup = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 spin_lock_irqsave(&device_state_lock, flags);
1579 if (udev->state == USB_STATE_NOTATTACHED)
1580 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001581 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001582
1583 /* root hub wakeup capabilities are managed out-of-band
1584 * and may involve silicon errata ... ignore them here.
1585 */
1586 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001587 if (udev->state == USB_STATE_SUSPENDED
1588 || new_state == USB_STATE_SUSPENDED)
1589 ; /* No change to wakeup settings */
1590 else if (new_state == USB_STATE_CONFIGURED)
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001591 wakeup = udev->actconfig->desc.bmAttributes
1592 & USB_CONFIG_ATT_WAKEUP;
Alan Stern645daaa2006-08-30 15:47:02 -04001593 else
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001594 wakeup = 0;
David Brownellfb669cc2006-01-24 08:40:27 -08001595 }
Sarah Sharp15123002007-12-21 16:54:15 -08001596 if (udev->state == USB_STATE_SUSPENDED &&
1597 new_state != USB_STATE_SUSPENDED)
1598 udev->active_duration -= jiffies;
1599 else if (new_state == USB_STATE_SUSPENDED &&
1600 udev->state != USB_STATE_SUSPENDED)
1601 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001602 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001603 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 recursively_mark_NOTATTACHED(udev);
1605 spin_unlock_irqrestore(&device_state_lock, flags);
Rafael J. Wysocki4681b172011-02-08 23:25:48 +01001606 if (wakeup >= 0)
1607 device_set_wakeup_capable(&udev->dev, wakeup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
David Vrabel6da9c992009-02-18 14:43:47 +00001609EXPORT_SYMBOL_GPL(usb_set_device_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001611/*
Alan Stern3b29b682011-02-22 09:53:41 -05001612 * Choose a device number.
1613 *
1614 * Device numbers are used as filenames in usbfs. On USB-1.1 and
1615 * USB-2.0 buses they are also used as device addresses, however on
1616 * USB-3.0 buses the address is assigned by the controller hardware
1617 * and it usually is not the same as the device number.
1618 *
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001619 * WUSB devices are simple: they have no hubs behind, so the mapping
1620 * device <-> virtual port number becomes 1:1. Why? to simplify the
1621 * life of the device connection logic in
1622 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1623 * handshake we need to assign a temporary address in the unauthorized
1624 * space. For simplicity we use the first virtual port number found to
1625 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1626 * and that becomes it's address [X < 128] or its unauthorized address
1627 * [X | 0x80].
1628 *
1629 * We add 1 as an offset to the one-based USB-stack port number
1630 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1631 * 0 is reserved by USB for default address; (b) Linux's USB stack
1632 * uses always #1 for the root hub of the controller. So USB stack's
1633 * port #1, which is wusb virtual-port #0 has address #2.
Sarah Sharpc6515272009-04-27 19:57:26 -07001634 *
1635 * Devices connected under xHCI are not as simple. The host controller
1636 * supports virtualization, so the hardware assigns device addresses and
1637 * the HCD must setup data structures before issuing a set address
1638 * command to the hardware.
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001639 */
Alan Stern3b29b682011-02-22 09:53:41 -05001640static void choose_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 int devnum;
1643 struct usb_bus *bus = udev->bus;
1644
1645 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001646 if (udev->wusb) {
1647 devnum = udev->portnum + 1;
1648 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1649 } else {
1650 /* Try to allocate the next devnum beginning at
1651 * bus->devnum_next. */
1652 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1653 bus->devnum_next);
1654 if (devnum >= 128)
1655 devnum = find_next_zero_bit(bus->devmap.devicemap,
1656 128, 1);
1657 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (devnum < 128) {
1660 set_bit(devnum, bus->devmap.devicemap);
1661 udev->devnum = devnum;
1662 }
1663}
1664
Alan Stern3b29b682011-02-22 09:53:41 -05001665static void release_devnum(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 if (udev->devnum > 0) {
1668 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1669 udev->devnum = -1;
1670 }
1671}
1672
Alan Stern3b29b682011-02-22 09:53:41 -05001673static void update_devnum(struct usb_device *udev, int devnum)
David Vrabel4953d142008-04-08 13:24:46 -07001674{
1675 /* The address for a WUSB device is managed by wusbcore. */
1676 if (!udev->wusb)
1677 udev->devnum = devnum;
1678}
1679
Herbert Xuf7410ce2010-01-10 20:15:03 +11001680static void hub_free_dev(struct usb_device *udev)
1681{
1682 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
1683
1684 /* Root hubs aren't real devices, so don't free HCD resources */
1685 if (hcd->driver->free_dev && udev->parent)
1686 hcd->driver->free_dev(hcd, udev);
1687}
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689/**
1690 * usb_disconnect - disconnect a device (usbcore-internal)
1691 * @pdev: pointer to device being disconnected
1692 * Context: !in_interrupt ()
1693 *
1694 * Something got disconnected. Get rid of it and all of its children.
1695 *
1696 * If *pdev is a normal device then the parent hub must already be locked.
1697 * If *pdev is a root hub then this routine will acquire the
1698 * usb_bus_list_lock on behalf of the caller.
1699 *
1700 * Only hub drivers (including virtual root hub drivers for host
1701 * controllers) should ever call this.
1702 *
1703 * This call is synchronous, and may not be used in an interrupt context.
1704 */
1705void usb_disconnect(struct usb_device **pdev)
1706{
1707 struct usb_device *udev = *pdev;
1708 int i;
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 /* mark the device as inactive, so any further urb submissions for
1711 * this device (and any of its children) will fail immediately.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001712 * this quiesces everything except pending urbs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 */
1714 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern3b29b682011-02-22 09:53:41 -05001715 dev_info(&udev->dev, "USB disconnect, device number %d\n",
1716 udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718#ifdef CONFIG_USB_OTG
1719 if (udev->bus->hnp_support && udev->portnum == udev->bus->otg_port) {
Vijayavardhan Vennapusa81bda9d2012-01-18 13:18:22 +05301720 cancel_delayed_work_sync(&udev->bus->hnp_polling);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001721 udev->bus->hnp_support = 0;
1722 }
1723#endif
1724
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001725 usb_lock_device(udev);
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* Free up all the children before we remove this device */
Huajun Li88162302012-03-12 21:00:19 +08001728 for (i = 0; i < udev->maxchild; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (udev->children[i])
1730 usb_disconnect(&udev->children[i]);
1731 }
1732
1733 /* deallocate hcd/hardware state ... nuking all pending urbs and
1734 * cleaning up all state associated with the current configuration
1735 * so that the hardware is now fully quiesced.
1736 */
Alan Stern782da722006-07-01 22:09:35 -04001737 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 usb_disable_device(udev, 0);
Alan Sterncde217a2008-10-21 15:28:46 -04001739 usb_hcd_synchronize_unlinks(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Alan Stern3b23dd62008-12-05 14:10:34 -05001741 usb_remove_ep_devs(&udev->ep0);
Alan Stern782da722006-07-01 22:09:35 -04001742 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001743
Alan Stern782da722006-07-01 22:09:35 -04001744 /* Unregister the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05001745 * for de-configuring the device and invoking the remove-device
1746 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04001747 */
1748 device_del(&udev->dev);
1749
1750 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 * (or root_hub) pointer.
1752 */
Alan Stern3b29b682011-02-22 09:53:41 -05001753 release_devnum(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 /* Avoid races with recursively_mark_NOTATTACHED() */
1756 spin_lock_irq(&device_state_lock);
1757 *pdev = NULL;
1758 spin_unlock_irq(&device_state_lock);
1759
Herbert Xuf7410ce2010-01-10 20:15:03 +11001760 hub_free_dev(udev);
1761
Alan Stern782da722006-07-01 22:09:35 -04001762 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001765#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766static void show_string(struct usb_device *udev, char *id, char *string)
1767{
1768 if (!string)
1769 return;
1770 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1771}
1772
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001773static void announce_device(struct usb_device *udev)
1774{
1775 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1776 le16_to_cpu(udev->descriptor.idVendor),
1777 le16_to_cpu(udev->descriptor.idProduct));
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001778 dev_info(&udev->dev,
1779 "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n",
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001780 udev->descriptor.iManufacturer,
1781 udev->descriptor.iProduct,
1782 udev->descriptor.iSerialNumber);
1783 show_string(udev, "Product", udev->product);
1784 show_string(udev, "Manufacturer", udev->manufacturer);
1785 show_string(udev, "SerialNumber", udev->serial);
1786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001788static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789#endif
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791#ifdef CONFIG_USB_OTG
1792#include "otg_whitelist.h"
1793#endif
1794
Oliver Neukum3ede7602007-01-11 14:35:50 +01001795/**
Alan Stern8d8558d2009-12-08 15:50:41 -05001796 * usb_enumerate_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01001797 * @udev: newly addressed device (in ADDRESS state)
1798 *
Alan Stern8d8558d2009-12-08 15:50:41 -05001799 * Finish enumeration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01001800 */
Alan Stern8d8558d2009-12-08 15:50:41 -05001801static int usb_enumerate_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001803 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805#ifdef CONFIG_USB_OTG
Vijayavardhan Vennapusa1720db842012-01-18 14:16:55 +05301806 bool old_otg = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /*
1808 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1809 * to wake us after we've powered off VBUS; and HNP, switching roles
1810 * "host" to "peripheral". The OTG descriptor helps figure this out.
1811 */
1812 if (!udev->bus->is_b_host
1813 && udev->config
1814 && udev->parent == udev->bus->root_hub) {
Felipe Balbi2eb50522009-12-04 15:47:43 +02001815 struct usb_otg_descriptor *desc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct usb_bus *bus = udev->bus;
1817
1818 /* descriptor may appear anywhere in config */
1819 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1820 le16_to_cpu(udev->config[0].desc.wTotalLength),
1821 USB_DT_OTG, (void **) &desc) == 0) {
1822 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05001823 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07001824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 dev_info(&udev->dev,
1826 "Dual-Role OTG device on %sHNP port\n",
1827 (port1 == bus->otg_port)
1828 ? "" : "non-");
1829
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001830 /* a_alt_hnp_support is obsoleted */
1831 if (port1 != bus->otg_port)
1832 goto out;
1833
1834 bus->hnp_support = 1;
1835
1836 /* a_hnp_support is not required for devices
1837 * compliant to revision 2.0 or subsequent
1838 * versions.
1839 */
Vijayavardhan Vennapusa81bda9d2012-01-18 13:18:22 +05301840
1841 if ((le16_to_cpu(desc->bLength) ==
1842 USB_DT_OTG_SIZE) &&
1843 le16_to_cpu(desc->bcdOTG) >= 0x0200)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001844 goto out;
1845
1846 /* Legacy B-device i.e compliant to spec
1847 * revision 1.3 expect A-device to set
1848 * a_hnp_support or b_hnp_enable before
1849 * selecting configuration.
1850 */
Vijayavardhan Vennapusa1720db842012-01-18 14:16:55 +05301851 old_otg = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /* enable HNP before suspend, it's simpler */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 err = usb_control_msg(udev,
1855 usb_sndctrlpipe(udev, 0),
1856 USB_REQ_SET_FEATURE, 0,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 USB_DEVICE_A_HNP_SUPPORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1859 if (err < 0) {
1860 /* OTG MESSAGE: report errors here,
1861 * customize to match your product.
1862 */
1863 dev_info(&udev->dev,
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08001864 "can't set HNP mode: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 err);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001866 bus->hnp_support = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868 }
1869 }
1870 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871out:
Vijayavardhan Vennapusa1720db842012-01-18 14:16:55 +05301872 if ((udev->quirks & USB_QUIRK_OTG_PET)) {
1873 if (le16_to_cpu(udev->descriptor.bcdDevice) &
1874 OTG_TTST_VBUS_OFF)
1875 udev->bus->otg_vbus_off = 1;
1876 if (udev->bus->is_b_host || old_otg)
1877 udev->bus->quick_hnp = 1;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 if (!is_targeted(udev)) {
1881
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 otg_send_event(OTG_EVENT_DEV_NOT_SUPPORTED);
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* Maybe it can talk to us, though we can't talk to it.
1885 * (Includes HNP test device.)
1886 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001887 if (udev->bus->hnp_support) {
David Brownell634a84f2009-01-15 13:51:28 -08001888 err = usb_port_suspend(udev, PMSG_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (err < 0)
1890 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1891 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07001892 err = -ENOTSUPP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 } else if (udev->bus->hnp_support &&
1894 udev->portnum == udev->bus->otg_port) {
1895 /* HNP polling is introduced in OTG supplement Rev 2.0
1896 * and older devices may not support. Work is not
1897 * re-armed if device returns STALL. B-Host also perform
1898 * HNP polling.
1899 */
Vijayavardhan Vennapusa1720db842012-01-18 14:16:55 +05301900 if (udev->bus->quick_hnp)
1901 schedule_delayed_work(&udev->bus->hnp_polling,
1902 msecs_to_jiffies(OTG_TTST_SUSP));
1903 else
1904 schedule_delayed_work(&udev->bus->hnp_polling,
1905 msecs_to_jiffies(THOST_REQ_POLL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001908 return err;
1909}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001911
1912/**
Alan Stern8d8558d2009-12-08 15:50:41 -05001913 * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001914 * @udev: newly addressed device (in ADDRESS state)
1915 *
1916 * This is only called by usb_new_device() and usb_authorize_device()
1917 * and FIXME -- all comments that apply to them apply here wrt to
1918 * environment.
1919 *
1920 * If the device is WUSB and not authorized, we don't attempt to read
1921 * the string descriptors, as they will be errored out by the device
1922 * until it has been authorized.
1923 */
Alan Stern8d8558d2009-12-08 15:50:41 -05001924static int usb_enumerate_device(struct usb_device *udev)
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001925{
1926 int err;
1927
1928 if (udev->config == NULL) {
1929 err = usb_get_configuration(udev);
1930 if (err < 0) {
1931 dev_err(&udev->dev, "can't read configurations, error %d\n",
1932 err);
1933 goto fail;
1934 }
1935 }
1936 if (udev->wusb == 1 && udev->authorized == 0) {
1937 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1938 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1939 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1940 }
1941 else {
1942 /* read the standard strings and cache them if present */
1943 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1944 udev->manufacturer = usb_cache_string(udev,
1945 udev->descriptor.iManufacturer);
1946 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1947 }
Alan Stern8d8558d2009-12-08 15:50:41 -05001948 err = usb_enumerate_device_otg(udev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001949fail:
1950 return err;
1951}
1952
Matthew Garrettd35e70d2012-02-03 17:11:55 -05001953static void set_usb_port_removable(struct usb_device *udev)
1954{
1955 struct usb_device *hdev = udev->parent;
1956 struct usb_hub *hub;
1957 u8 port = udev->portnum;
1958 u16 wHubCharacteristics;
1959 bool removable = true;
1960
1961 if (!hdev)
1962 return;
1963
1964 hub = hdev_to_hub(udev->parent);
1965
1966 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
1967
1968 if (!(wHubCharacteristics & HUB_CHAR_COMPOUND))
1969 return;
1970
1971 if (hub_is_superspeed(hdev)) {
1972 if (hub->descriptor->u.ss.DeviceRemovable & (1 << port))
1973 removable = false;
1974 } else {
1975 if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8)))
1976 removable = false;
1977 }
1978
1979 if (removable)
1980 udev->removable = USB_DEVICE_REMOVABLE;
1981 else
1982 udev->removable = USB_DEVICE_FIXED;
1983}
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001984
1985/**
1986 * usb_new_device - perform initial device setup (usbcore-internal)
1987 * @udev: newly addressed device (in ADDRESS state)
1988 *
Alan Stern8d8558d2009-12-08 15:50:41 -05001989 * This is called with devices which have been detected but not fully
1990 * enumerated. The device descriptor is available, but not descriptors
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001991 * for any device configuration. The caller must have locked either
1992 * the parent hub (if udev is a normal device) or else the
1993 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
1994 * udev has already been installed, but udev is not yet visible through
1995 * sysfs or other filesystem code.
1996 *
1997 * It will return if the device is configured properly or not. Zero if
1998 * the interface was registered with the driver core; else a negative
1999 * errno value.
2000 *
2001 * This call is synchronous, and may not be used in an interrupt context.
2002 *
2003 * Only the hub driver or root-hub registrar should ever call this.
2004 */
2005int usb_new_device(struct usb_device *udev)
2006{
2007 int err;
2008
Dan Streetman16985402010-01-06 09:56:53 -05002009 if (udev->parent) {
Dan Streetman16985402010-01-06 09:56:53 -05002010 /* Initialize non-root-hub device wakeup to disabled;
2011 * device (un)configuration controls wakeup capable
2012 * sysfs power/wakeup controls wakeup enabled/disabled
2013 */
2014 device_init_wakeup(&udev->dev, 0);
Dan Streetman16985402010-01-06 09:56:53 -05002015 }
2016
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002017 /* Tell the runtime-PM framework the device is active */
2018 pm_runtime_set_active(&udev->dev);
Alan Sternc08512c2010-11-15 15:57:58 -05002019 pm_runtime_get_noresume(&udev->dev);
Alan Sternfcc4a012010-11-15 15:57:51 -05002020 pm_runtime_use_autosuspend(&udev->dev);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002021 pm_runtime_enable(&udev->dev);
2022
Alan Sternc08512c2010-11-15 15:57:58 -05002023 /* By default, forbid autosuspend for all devices. It will be
2024 * allowed for hubs during binding.
2025 */
2026 usb_disable_autosuspend(udev);
2027
Alan Stern8d8558d2009-12-08 15:50:41 -05002028 err = usb_enumerate_device(udev); /* Read descriptors */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002029 if (err < 0)
2030 goto fail;
Sarah Sharpc6515272009-04-27 19:57:26 -07002031 dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n",
2032 udev->devnum, udev->bus->busnum,
2033 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002034 /* export the usbdev device-node for libusb */
2035 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
2036 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
2037
Alan Stern6cd13202008-11-13 15:08:30 -05002038 /* Tell the world! */
2039 announce_device(udev);
Alan Stern195af2c2007-07-16 15:28:19 -04002040
Rafael J. Wysocki927bc912010-02-08 19:18:16 +01002041 device_enable_async_suspend(&udev->dev);
Matthew Garrettd35e70d2012-02-03 17:11:55 -05002042
2043 /*
2044 * check whether the hub marks this port as non-removable. Do it
2045 * now so that platform-specific data can override it in
2046 * device_add()
2047 */
2048 if (udev->parent)
2049 set_usb_port_removable(udev);
2050
Alan Stern782da722006-07-01 22:09:35 -04002051 /* Register the device. The device driver is responsible
Alan Stern3b23dd62008-12-05 14:10:34 -05002052 * for configuring the device and invoking the add-device
2053 * notifier chain (used by usbfs and possibly others).
Alan Stern782da722006-07-01 22:09:35 -04002054 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01002055 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (err) {
2057 dev_err(&udev->dev, "can't device_add, error %d\n", err);
2058 goto fail;
2059 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05002060
Alan Stern3b23dd62008-12-05 14:10:34 -05002061 (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev);
Alan Sternc08512c2010-11-15 15:57:58 -05002062 usb_mark_last_busy(udev);
2063 pm_runtime_put_sync_autosuspend(&udev->dev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07002064 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
2066fail:
2067 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002068 pm_runtime_disable(&udev->dev);
2069 pm_runtime_set_suspended(&udev->dev);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07002070 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002073
2074/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07002075 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
2076 * @usb_dev: USB device
2077 *
2078 * Move the USB device to a very basic state where interfaces are disabled
2079 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002080 *
2081 * We share a lock (that we have) with device_del(), so we need to
2082 * defer its call.
2083 */
2084int usb_deauthorize_device(struct usb_device *usb_dev)
2085{
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002086 usb_lock_device(usb_dev);
2087 if (usb_dev->authorized == 0)
2088 goto out_unauthorized;
Alan Sternda307122009-12-08 15:54:44 -05002089
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002090 usb_dev->authorized = 0;
2091 usb_set_configuration(usb_dev, -1);
Alan Sternda307122009-12-08 15:54:44 -05002092
2093 kfree(usb_dev->product);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002094 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002095 kfree(usb_dev->manufacturer);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002096 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002097 kfree(usb_dev->serial);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002098 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
Alan Sternda307122009-12-08 15:54:44 -05002099
2100 usb_destroy_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002101 usb_dev->descriptor.bNumConfigurations = 0;
Alan Sternda307122009-12-08 15:54:44 -05002102
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002103out_unauthorized:
2104 usb_unlock_device(usb_dev);
2105 return 0;
2106}
2107
2108
2109int usb_authorize_device(struct usb_device *usb_dev)
2110{
2111 int result = 0, c;
Alan Sternda307122009-12-08 15:54:44 -05002112
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002113 usb_lock_device(usb_dev);
2114 if (usb_dev->authorized == 1)
2115 goto out_authorized;
Alan Sternda307122009-12-08 15:54:44 -05002116
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002117 result = usb_autoresume_device(usb_dev);
2118 if (result < 0) {
2119 dev_err(&usb_dev->dev,
2120 "can't autoresume for authorization: %d\n", result);
2121 goto error_autoresume;
2122 }
2123 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
2124 if (result < 0) {
2125 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
2126 "authorization: %d\n", result);
2127 goto error_device_descriptor;
2128 }
Alan Sternda307122009-12-08 15:54:44 -05002129
2130 kfree(usb_dev->product);
2131 usb_dev->product = NULL;
2132 kfree(usb_dev->manufacturer);
2133 usb_dev->manufacturer = NULL;
2134 kfree(usb_dev->serial);
2135 usb_dev->serial = NULL;
2136
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002137 usb_dev->authorized = 1;
Alan Stern8d8558d2009-12-08 15:50:41 -05002138 result = usb_enumerate_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002139 if (result < 0)
Alan Stern8d8558d2009-12-08 15:50:41 -05002140 goto error_enumerate;
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002141 /* Choose and set the configuration. This registers the interfaces
2142 * with the driver core and lets interface drivers bind to them.
2143 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06002144 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002145 if (c >= 0) {
2146 result = usb_set_configuration(usb_dev, c);
2147 if (result) {
2148 dev_err(&usb_dev->dev,
2149 "can't set config #%d, error %d\n", c, result);
2150 /* This need not be fatal. The user can try to
2151 * set other configurations. */
2152 }
2153 }
2154 dev_info(&usb_dev->dev, "authorized to connect\n");
Alan Sternda307122009-12-08 15:54:44 -05002155
Alan Stern8d8558d2009-12-08 15:50:41 -05002156error_enumerate:
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002157error_device_descriptor:
Alan Sternda307122009-12-08 15:54:44 -05002158 usb_autosuspend_device(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07002159error_autoresume:
2160out_authorized:
2161 usb_unlock_device(usb_dev); // complements locktree
2162 return result;
2163}
2164
2165
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07002166/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
2167static unsigned hub_is_wusb(struct usb_hub *hub)
2168{
2169 struct usb_hcd *hcd;
2170 if (hub->hdev->parent != NULL) /* not a root hub? */
2171 return 0;
2172 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
2173 return hcd->wireless;
2174}
2175
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177#define PORT_RESET_TRIES 5
2178#define SET_ADDRESS_TRIES 2
2179#define GET_DESCRIPTOR_TRIES 2
2180#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
Rusty Russell90ab5ee2012-01-13 09:32:20 +10302181#define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
2184#define HUB_SHORT_RESET_TIME 10
Andiry Xu75d7cf72011-09-13 16:41:11 -07002185#define HUB_BH_RESET_TIME 50
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#define HUB_LONG_RESET_TIME 200
2187#define HUB_RESET_TIMEOUT 500
2188
Sarah Sharp10d674a2011-09-14 14:24:52 -07002189static int hub_port_reset(struct usb_hub *hub, int port1,
2190 struct usb_device *udev, unsigned int delay, bool warm);
2191
2192/* Is a USB 3.0 port in the Inactive state? */
2193static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus)
2194{
2195 return hub_is_superspeed(hub->hdev) &&
2196 (portstatus & USB_PORT_STAT_LINK_STATE) ==
2197 USB_SS_PORT_LS_SS_INACTIVE;
2198}
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200static int hub_port_wait_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002201 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 int delay_time, ret;
2204 u16 portstatus;
2205 u16 portchange;
2206
2207 for (delay_time = 0;
2208 delay_time < HUB_RESET_TIMEOUT;
2209 delay_time += delay) {
2210 /* wait to give the device a chance to reset */
2211 msleep(delay);
2212
2213 /* read and decode port status */
2214 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2215 if (ret < 0)
2216 return ret;
2217
Andiry Xu75d7cf72011-09-13 16:41:11 -07002218 /*
2219 * Some buggy devices require a warm reset to be issued even
2220 * when the port appears not to be connected.
2221 */
2222 if (!warm) {
Sarah Sharp10d674a2011-09-14 14:24:52 -07002223 /*
2224 * Some buggy devices can cause an NEC host controller
2225 * to transition to the "Error" state after a hot port
2226 * reset. This will show up as the port state in
2227 * "Inactive", and the port may also report a
2228 * disconnect. Forcing a warm port reset seems to make
2229 * the device work.
2230 *
2231 * See https://bugzilla.kernel.org/show_bug.cgi?id=41752
2232 */
2233 if (hub_port_inactive(hub, portstatus)) {
2234 int ret;
2235
2236 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2237 clear_port_feature(hub->hdev, port1,
2238 USB_PORT_FEAT_C_CONNECTION);
2239 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2240 clear_port_feature(hub->hdev, port1,
2241 USB_PORT_FEAT_C_PORT_LINK_STATE);
2242 if (portchange & USB_PORT_STAT_C_RESET)
2243 clear_port_feature(hub->hdev, port1,
2244 USB_PORT_FEAT_C_RESET);
2245 dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n",
2246 port1);
2247 ret = hub_port_reset(hub, port1,
2248 udev, HUB_BH_RESET_TIME,
2249 true);
2250 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2251 clear_port_feature(hub->hdev, port1,
2252 USB_PORT_FEAT_C_CONNECTION);
2253 return ret;
2254 }
Andiry Xu75d7cf72011-09-13 16:41:11 -07002255 /* Device went away? */
2256 if (!(portstatus & USB_PORT_STAT_CONNECTION))
2257 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Andiry Xu75d7cf72011-09-13 16:41:11 -07002259 /* bomb out completely if the connection bounced */
2260 if ((portchange & USB_PORT_STAT_C_CONNECTION))
2261 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Andiry Xu75d7cf72011-09-13 16:41:11 -07002263 /* if we`ve finished resetting, then break out of
2264 * the loop
2265 */
2266 if (!(portstatus & USB_PORT_STAT_RESET) &&
2267 (portstatus & USB_PORT_STAT_ENABLE)) {
2268 if (hub_is_wusb(hub))
2269 udev->speed = USB_SPEED_WIRELESS;
2270 else if (hub_is_superspeed(hub->hdev))
2271 udev->speed = USB_SPEED_SUPER;
2272 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
2273 udev->speed = USB_SPEED_HIGH;
2274 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
2275 udev->speed = USB_SPEED_LOW;
2276 else
2277 udev->speed = USB_SPEED_FULL;
2278 return 0;
2279 }
2280 } else {
2281 if (portchange & USB_PORT_STAT_C_BH_RESET)
2282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284
2285 /* switch to the long delay after two short delay failures */
2286 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
2287 delay = HUB_LONG_RESET_TIME;
2288
2289 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002290 "port %d not %sreset yet, waiting %dms\n",
2291 port1, warm ? "warm " : "", delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
2293
2294 return -EBUSY;
2295}
2296
Andiry Xu75d7cf72011-09-13 16:41:11 -07002297static void hub_port_finish_reset(struct usb_hub *hub, int port1,
2298 struct usb_device *udev, int *status, bool warm)
2299{
2300 switch (*status) {
2301 case 0:
2302 if (!warm) {
2303 struct usb_hcd *hcd;
2304 /* TRSTRCY = 10 ms; plus some extra */
2305 msleep(10 + 40);
2306 update_devnum(udev, 0);
2307 hcd = bus_to_hcd(udev->bus);
2308 if (hcd->driver->reset_device) {
2309 *status = hcd->driver->reset_device(hcd, udev);
2310 if (*status < 0) {
2311 dev_err(&udev->dev, "Cannot reset "
2312 "HCD device state\n");
2313 break;
2314 }
2315 }
2316 }
2317 /* FALL THROUGH */
2318 case -ENOTCONN:
2319 case -ENODEV:
2320 clear_port_feature(hub->hdev,
2321 port1, USB_PORT_FEAT_C_RESET);
2322 /* FIXME need disconnect() for NOTATTACHED device */
2323 if (warm) {
2324 clear_port_feature(hub->hdev, port1,
2325 USB_PORT_FEAT_C_BH_PORT_RESET);
2326 clear_port_feature(hub->hdev, port1,
2327 USB_PORT_FEAT_C_PORT_LINK_STATE);
2328 } else {
2329 usb_set_device_state(udev, *status
2330 ? USB_STATE_NOTATTACHED
2331 : USB_STATE_DEFAULT);
2332 }
2333 break;
2334 }
2335}
2336
2337/* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338static int hub_port_reset(struct usb_hub *hub, int port1,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002339 struct usb_device *udev, unsigned int delay, bool warm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340{
2341 int i, status;
2342
Andiry Xu75d7cf72011-09-13 16:41:11 -07002343 if (!warm) {
2344 /* Block EHCI CF initialization during the port reset.
2345 * Some companion controllers don't like it when they mix.
2346 */
2347 down_read(&ehci_cf_port_reset_rwsem);
2348 } else {
2349 if (!hub_is_superspeed(hub->hdev)) {
2350 dev_err(hub->intfdev, "only USB3 hub support "
2351 "warm reset\n");
2352 return -EINVAL;
2353 }
2354 }
Alan Stern32fe0192007-10-10 16:27:07 -04002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /* Reset the port */
2357 for (i = 0; i < PORT_RESET_TRIES; i++) {
Andiry Xu75d7cf72011-09-13 16:41:11 -07002358 status = set_port_feature(hub->hdev, port1, (warm ?
2359 USB_PORT_FEAT_BH_PORT_RESET :
2360 USB_PORT_FEAT_RESET));
2361 if (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 dev_err(hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002363 "cannot %sreset port %d (err = %d)\n",
2364 warm ? "warm " : "", port1, status);
2365 } else {
2366 status = hub_port_wait_reset(hub, port1, udev, delay,
2367 warm);
David Brownelldd165252005-08-31 10:45:25 -07002368 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 dev_dbg(hub->intfdev,
2370 "port_wait_reset: err = %d\n",
2371 status);
2372 }
2373
2374 /* return on disconnect or reset */
Andiry Xu75d7cf72011-09-13 16:41:11 -07002375 if (status == 0 || status == -ENOTCONN || status == -ENODEV) {
2376 hub_port_finish_reset(hub, port1, udev, &status, warm);
Alan Stern32fe0192007-10-10 16:27:07 -04002377 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379
2380 dev_dbg (hub->intfdev,
Andiry Xu75d7cf72011-09-13 16:41:11 -07002381 "port %d not enabled, trying %sreset again...\n",
2382 port1, warm ? "warm " : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 delay = HUB_LONG_RESET_TIME;
2384 }
2385
2386 dev_err (hub->intfdev,
2387 "Cannot enable port %i. Maybe the USB cable is bad?\n",
2388 port1);
2389
Andiry Xu75d7cf72011-09-13 16:41:11 -07002390done:
2391 if (!warm)
2392 up_read(&ehci_cf_port_reset_rwsem);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return status;
2395}
2396
Andiry Xu0ed9a572011-04-27 18:07:43 +08002397/* Check if a port is power on */
2398static int port_is_power_on(struct usb_hub *hub, unsigned portstatus)
2399{
2400 int ret = 0;
2401
2402 if (hub_is_superspeed(hub->hdev)) {
2403 if (portstatus & USB_SS_PORT_STAT_POWER)
2404 ret = 1;
2405 } else {
2406 if (portstatus & USB_PORT_STAT_POWER)
2407 ret = 1;
2408 }
2409
2410 return ret;
2411}
2412
Alan Sternd388dab2006-07-01 22:14:24 -04002413#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Andiry Xu0ed9a572011-04-27 18:07:43 +08002415/* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */
2416static int port_is_suspended(struct usb_hub *hub, unsigned portstatus)
2417{
2418 int ret = 0;
2419
2420 if (hub_is_superspeed(hub->hdev)) {
2421 if ((portstatus & USB_PORT_STAT_LINK_STATE)
2422 == USB_SS_PORT_LS_U3)
2423 ret = 1;
2424 } else {
2425 if (portstatus & USB_PORT_STAT_SUSPEND)
2426 ret = 1;
2427 }
2428
2429 return ret;
2430}
Alan Sternb01b03f2008-04-28 11:06:11 -04002431
2432/* Determine whether the device on a port is ready for a normal resume,
2433 * is ready for a reset-resume, or should be disconnected.
2434 */
2435static int check_port_resume_type(struct usb_device *udev,
2436 struct usb_hub *hub, int port1,
2437 int status, unsigned portchange, unsigned portstatus)
2438{
2439 /* Is the device still present? */
Andiry Xu0ed9a572011-04-27 18:07:43 +08002440 if (status || port_is_suspended(hub, portstatus) ||
2441 !port_is_power_on(hub, portstatus) ||
2442 !(portstatus & USB_PORT_STAT_CONNECTION)) {
Alan Sternb01b03f2008-04-28 11:06:11 -04002443 if (status >= 0)
2444 status = -ENODEV;
2445 }
2446
Alan Stern86c57ed2008-06-30 11:14:43 -04002447 /* Can't do a normal resume if the port isn't enabled,
2448 * so try a reset-resume instead.
2449 */
2450 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) {
2451 if (udev->persist_enabled)
2452 udev->reset_resume = 1;
2453 else
2454 status = -ENODEV;
2455 }
Alan Sternb01b03f2008-04-28 11:06:11 -04002456
2457 if (status) {
2458 dev_dbg(hub->intfdev,
2459 "port %d status %04x.%04x after resume, %d\n",
2460 port1, portchange, portstatus, status);
2461 } else if (udev->reset_resume) {
2462
2463 /* Late port handoff can set status-change bits */
2464 if (portchange & USB_PORT_STAT_C_CONNECTION)
2465 clear_port_feature(hub->hdev, port1,
2466 USB_PORT_FEAT_C_CONNECTION);
2467 if (portchange & USB_PORT_STAT_C_ENABLE)
2468 clear_port_feature(hub->hdev, port1,
2469 USB_PORT_FEAT_C_ENABLE);
2470 }
2471
2472 return status;
2473}
2474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475#ifdef CONFIG_USB_SUSPEND
2476
2477/*
Alan Stern140d8f62006-07-01 22:07:21 -04002478 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04002479 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07002480 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 *
2482 * Suspends a USB device that isn't in active use, conserving power.
2483 * Devices may wake out of a suspend, if anything important happens,
2484 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04002485 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * to disconnect devices while they are suspended.
2487 *
David Brownell390a8c32005-09-13 19:57:27 -07002488 * This only affects the USB hardware for a device; its interfaces
2489 * (and, for hubs, child devices) must already have been suspended.
2490 *
Alan Stern624d6c02007-05-30 15:35:16 -04002491 * Selective port suspend reduces power; most suspended devices draw
2492 * less than 500 uA. It's also used in OTG, along with remote wakeup.
2493 * All devices below the suspended port are also suspended.
2494 *
2495 * Devices leave suspend state when the host wakes them up. Some devices
2496 * also support "remote wakeup", where the device can activate the USB
2497 * tree above them to deliver data, such as a keypress or packet. In
2498 * some cases, this wakes the USB host.
2499 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 * Suspending OTG devices may trigger HNP, if that's been enabled
2501 * between a pair of dual-role devices. That will change roles, such
2502 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
2503 *
Alan Stern4956ecc2007-05-30 16:51:28 -04002504 * Devices on USB hub ports have only one "suspend" state, corresponding
2505 * to ACPI D2, "may cause the device to lose some context".
2506 * State transitions include:
2507 *
2508 * - suspend, resume ... when the VBUS power link stays live
2509 * - suspend, disconnect ... VBUS lost
2510 *
2511 * Once VBUS drop breaks the circuit, the port it's using has to go through
2512 * normal re-enumeration procedures, starting with enabling VBUS power.
2513 * Other than re-initializing the hub (plug/unplug, except for root hubs),
2514 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
2515 * timer, no SRP, no requests through sysfs.
2516 *
2517 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
2518 * the root hub for their bus goes into global suspend ... so we don't
2519 * (falsely) update the device power state to say it suspended.
2520 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 * Returns 0 on success, else negative errno.
2522 */
Alan Stern65bfd292008-11-25 16:39:18 -05002523int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
Alan Stern624d6c02007-05-30 15:35:16 -04002525 struct usb_hub *hub = hdev_to_hub(udev->parent);
2526 int port1 = udev->portnum;
2527 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04002528
Alan Stern624d6c02007-05-30 15:35:16 -04002529 /* enable remote wakeup when appropriate; this lets the device
2530 * wake up the upstream hub (including maybe the root hub).
2531 *
2532 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
2533 * we don't explicitly enable it here.
2534 */
2535 if (udev->do_remote_wakeup) {
Sarah Sharp623bef92011-11-11 14:57:33 -08002536 if (!hub_is_superspeed(hub->hdev)) {
2537 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2538 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
2539 USB_DEVICE_REMOTE_WAKEUP, 0,
2540 NULL, 0,
2541 USB_CTRL_SET_TIMEOUT);
2542 } else {
2543 /* Assume there's only one function on the USB 3.0
2544 * device and enable remote wake for the first
2545 * interface. FIXME if the interface association
2546 * descriptor shows there's more than one function.
2547 */
2548 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2549 USB_REQ_SET_FEATURE,
2550 USB_RECIP_INTERFACE,
2551 USB_INTRF_FUNC_SUSPEND,
Sarah Sharp3b9b6ac2012-01-06 15:48:30 -08002552 USB_INTRF_FUNC_SUSPEND_RW |
2553 USB_INTRF_FUNC_SUSPEND_LP,
Sarah Sharp623bef92011-11-11 14:57:33 -08002554 NULL, 0,
2555 USB_CTRL_SET_TIMEOUT);
2556 }
Oliver Neukum0c487202009-10-19 13:19:41 +02002557 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002558 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
2559 status);
Oliver Neukum0c487202009-10-19 13:19:41 +02002560 /* bail if autosuspend is requested */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002561 if (PMSG_IS_AUTO(msg))
Oliver Neukum0c487202009-10-19 13:19:41 +02002562 return status;
2563 }
Alan Stern624d6c02007-05-30 15:35:16 -04002564 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002565#ifdef CONFIG_USB_OTG
2566 if (!udev->bus->is_b_host && udev->bus->hnp_support &&
2567 udev->portnum == udev->bus->otg_port) {
2568 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
2569 USB_REQ_SET_FEATURE, 0,
2570 USB_DEVICE_B_HNP_ENABLE,
2571 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
2572 if (status < 0) {
2573 otg_send_event(OTG_EVENT_NO_RESP_FOR_HNP_ENABLE);
2574 dev_dbg(&udev->dev, "can't enable HNP on port %d, "
2575 "status %d\n", port1, status);
2576 } else {
2577 udev->bus->b_hnp_enable = 1;
2578 }
2579 }
2580#endif
Alan Stern624d6c02007-05-30 15:35:16 -04002581
Andiry Xu65580b432011-09-23 14:19:52 -07002582 /* disable USB2 hardware LPM */
2583 if (udev->usb2_hw_lpm_enabled == 1)
2584 usb_set_usb2_hardware_lpm(udev, 0);
2585
Alan Stern624d6c02007-05-30 15:35:16 -04002586 /* see 7.1.7.6 */
Andiry Xua7114232011-04-27 18:07:50 +08002587 if (hub_is_superspeed(hub->hdev))
2588 status = set_port_feature(hub->hdev,
2589 port1 | (USB_SS_PORT_LS_U3 << 3),
2590 USB_PORT_FEAT_LINK_STATE);
Andiry Xua8f08d82011-03-31 14:56:50 +08002591 else
2592 status = set_port_feature(hub->hdev, port1,
2593 USB_PORT_FEAT_SUSPEND);
Alan Stern624d6c02007-05-30 15:35:16 -04002594 if (status) {
2595 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
2596 port1, status);
2597 /* paranoia: "should not happen" */
Oliver Neukum0c487202009-10-19 13:19:41 +02002598 if (udev->do_remote_wakeup)
2599 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
Alan Stern624d6c02007-05-30 15:35:16 -04002600 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
2601 USB_DEVICE_REMOTE_WAKEUP, 0,
2602 NULL, 0,
2603 USB_CTRL_SET_TIMEOUT);
Alan Sterncbb33002011-06-15 16:29:16 -04002604
2605 /* System sleep transitions should never fail */
Alan Stern5b1b0b82011-08-19 23:49:48 +02002606 if (!PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002607 status = 0;
Alan Stern624d6c02007-05-30 15:35:16 -04002608 } else {
2609 /* device has up to 10 msec to fully suspend */
Alan Stern30b1a7a2011-09-27 21:54:22 +02002610 dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n",
2611 (PMSG_IS_AUTO(msg) ? "auto-" : ""),
2612 udev->do_remote_wakeup);
Alan Stern624d6c02007-05-30 15:35:16 -04002613 usb_set_device_state(udev, USB_STATE_SUSPENDED);
2614 msleep(10);
2615 }
Alan Sternc08512c2010-11-15 15:57:58 -05002616 usb_mark_last_busy(hub->hdev);
Alan Stern4956ecc2007-05-30 16:51:28 -04002617 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
David Brownellf3f32532005-09-22 22:37:29 -07002619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620/*
David Brownell390a8c32005-09-13 19:57:27 -07002621 * If the USB "suspend" state is in use (rather than "global suspend"),
2622 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04002623 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 * hardware resume signaling is finished, either because of selective
2625 * resume (by host) or remote wakeup (by device) ... now see what changed
2626 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04002627 *
2628 * If @udev->reset_resume is set then the device is reset before the
2629 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 */
Alan Stern140d8f62006-07-01 22:07:21 -04002631static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Alan Stern54515fe2007-05-30 15:38:58 -04002633 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 u16 devstatus;
2635
2636 /* caller owns the udev device lock */
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002637 dev_dbg(&udev->dev, "%s\n",
2638 udev->reset_resume ? "finish reset-resume" : "finish resume");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 /* usb ch9 identifies four variants of SUSPENDED, based on what
2641 * state the device resumes to. Linux currently won't see the
2642 * first two on the host side; they'd be inside hub_port_init()
2643 * during many timeouts, but khubd can't suspend until later.
2644 */
2645 usb_set_device_state(udev, udev->actconfig
2646 ? USB_STATE_CONFIGURED
2647 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Alan Stern54515fe2007-05-30 15:38:58 -04002649 /* 10.5.4.5 says not to reset a suspended port if the attached
2650 * device is enabled for remote wakeup. Hence the reset
2651 * operation is carried out here, after the port has been
2652 * resumed.
2653 */
2654 if (udev->reset_resume)
Alan Stern86c57ed2008-06-30 11:14:43 -04002655 retry_reset_resume:
Ming Lei742120c2008-06-18 22:00:29 +08002656 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /* 10.5.4.5 says be sure devices in the tree are still there.
2659 * For now let's assume the device didn't go crazy on resume,
2660 * and device drivers will know about any resume quirks.
2661 */
Alan Stern54515fe2007-05-30 15:38:58 -04002662 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04002663 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04002664 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
2665 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04002666 status = (status > 0 ? 0 : -ENODEV);
Alan Stern86c57ed2008-06-30 11:14:43 -04002667
2668 /* If a normal resume failed, try doing a reset-resume */
2669 if (status && !udev->reset_resume && udev->persist_enabled) {
2670 dev_dbg(&udev->dev, "retry with reset-resume\n");
2671 udev->reset_resume = 1;
2672 goto retry_reset_resume;
2673 }
Alan Stern54515fe2007-05-30 15:38:58 -04002674 }
Alan Sternb40b7a92006-06-19 15:12:38 -04002675
Alan Stern624d6c02007-05-30 15:35:16 -04002676 if (status) {
2677 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
2678 status);
2679 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04002681 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 status = usb_control_msg(udev,
2683 usb_sndctrlpipe(udev, 0),
2684 USB_REQ_CLEAR_FEATURE,
2685 USB_RECIP_DEVICE,
2686 USB_DEVICE_REMOTE_WAKEUP, 0,
2687 NULL, 0,
2688 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04002689 if (status)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08002690 dev_dbg(&udev->dev,
2691 "disable remote wakeup, status %d\n",
2692 status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05002693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
2696 return status;
2697}
2698
Alan Stern624d6c02007-05-30 15:35:16 -04002699/*
2700 * usb_port_resume - re-activate a suspended usb device's upstream port
2701 * @udev: device to re-activate, not a root hub
2702 * Context: must be able to sleep; device not locked; pm locks held
2703 *
2704 * This will re-activate the suspended device, increasing power usage
2705 * while letting drivers communicate again with its endpoints.
2706 * USB resume explicitly guarantees that the power session between
2707 * the host and the device is the same as it was when the device
2708 * suspended.
2709 *
Alan Sternfeccc302008-03-03 15:15:59 -05002710 * If @udev->reset_resume is set then this routine won't check that the
2711 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002712 * reset @udev. The end result is that a broken power session can be
2713 * recovered and @udev will appear to persist across a loss of VBUS power.
2714 *
2715 * For example, if a host controller doesn't maintain VBUS suspend current
2716 * during a system sleep or is reset when the system wakes up, all the USB
2717 * power sessions below it will be broken. This is especially troublesome
2718 * for mass-storage devices containing mounted filesystems, since the
2719 * device will appear to have disconnected and all the memory mappings
2720 * to it will be lost. Using the USB_PERSIST facility, the device can be
2721 * made to appear as if it had not disconnected.
2722 *
Ming Lei742120c2008-06-18 22:00:29 +08002723 * This facility can be dangerous. Although usb_reset_and_verify_device() makes
Alan Sternfeccc302008-03-03 15:15:59 -05002724 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002725 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2726 * quite possible for a device to remain unaltered but its media to be
2727 * changed. If the user replaces a flash memory card while the system is
2728 * asleep, he will have only himself to blame when the filesystem on the
2729 * new card is corrupted and the system crashes.
2730 *
Alan Stern624d6c02007-05-30 15:35:16 -04002731 * Returns 0 on success, else negative errno.
2732 */
Alan Stern65bfd292008-11-25 16:39:18 -05002733int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Alan Stern624d6c02007-05-30 15:35:16 -04002735 struct usb_hub *hub = hdev_to_hub(udev->parent);
2736 int port1 = udev->portnum;
2737 int status;
2738 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05002739
2740 /* Skip the initial Clear-Suspend step for a remote wakeup */
2741 status = hub_port_status(hub, port1, &portstatus, &portchange);
Andiry Xu0ed9a572011-04-27 18:07:43 +08002742 if (status == 0 && !port_is_suspended(hub, portstatus))
Alan Sternd25450c2006-11-20 11:14:30 -05002743 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2746
Alan Sternd5cbad42006-08-11 16:52:39 -04002747 set_bit(port1, hub->busy_bits);
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /* see 7.1.7.7; affects power usage, but not budgeting */
Andiry Xua7114232011-04-27 18:07:50 +08002750 if (hub_is_superspeed(hub->hdev))
2751 status = set_port_feature(hub->hdev,
2752 port1 | (USB_SS_PORT_LS_U0 << 3),
2753 USB_PORT_FEAT_LINK_STATE);
2754 else
2755 status = clear_port_feature(hub->hdev,
2756 port1, USB_PORT_FEAT_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002758 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
2759 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04002762 dev_dbg(&udev->dev, "usb %sresume\n",
Alan Stern5b1b0b82011-08-19 23:49:48 +02002763 (PMSG_IS_AUTO(msg) ? "auto-" : ""));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 msleep(25);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2767 * stop resume signaling. Then finish the resume
2768 * sequence.
2769 */
Alan Sternd25450c2006-11-20 11:14:30 -05002770 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04002771
Alan Sternb01b03f2008-04-28 11:06:11 -04002772 /* TRSMRCY = 10 msec */
2773 msleep(10);
2774 }
Alan Stern54515fe2007-05-30 15:38:58 -04002775
Alan Sternb01b03f2008-04-28 11:06:11 -04002776 SuspendCleared:
2777 if (status == 0) {
Andiry Xua7114232011-04-27 18:07:50 +08002778 if (hub_is_superspeed(hub->hdev)) {
2779 if (portchange & USB_PORT_STAT_C_LINK_STATE)
2780 clear_port_feature(hub->hdev, port1,
2781 USB_PORT_FEAT_C_PORT_LINK_STATE);
2782 } else {
2783 if (portchange & USB_PORT_STAT_C_SUSPEND)
2784 clear_port_feature(hub->hdev, port1,
2785 USB_PORT_FEAT_C_SUSPEND);
2786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Alan Sternd5cbad42006-08-11 16:52:39 -04002789 clear_bit(port1, hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04002790
Alan Sternb01b03f2008-04-28 11:06:11 -04002791 status = check_port_resume_type(udev,
2792 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04002793 if (status == 0)
2794 status = finish_port_resume(udev);
2795 if (status < 0) {
2796 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2797 hub_port_logical_disconnect(hub, port1);
Andiry Xu65580b432011-09-23 14:19:52 -07002798 } else {
2799 /* Try to enable USB2 hardware LPM */
2800 if (udev->usb2_hw_lpm_capable == 1)
2801 usb_set_usb2_hardware_lpm(udev, 1);
Alan Stern54515fe2007-05-30 15:38:58 -04002802 }
Andiry Xu65580b432011-09-23 14:19:52 -07002803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return status;
2805}
2806
Alan Stern8808f002008-04-28 11:06:55 -04002807/* caller has locked udev */
Alan Stern0534d462010-01-08 12:56:30 -05002808int usb_remote_wakeup(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
2810 int status = 0;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07002811 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04002814 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002815 status = usb_autoresume_device(udev);
2816 if (status == 0) {
2817 /* Let the drivers do their thing, then... */
2818 usb_autosuspend_device(udev);
2819 }
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07002820 } else {
2821 dev_dbg(&udev->dev, "usb not suspended\n");
2822 clear_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags);
Alan Sternd25450c2006-11-20 11:14:30 -05002823 }
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return status;
2826}
2827
Alan Sternd388dab2006-07-01 22:14:24 -04002828#else /* CONFIG_USB_SUSPEND */
2829
2830/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2831
Alan Stern65bfd292008-11-25 16:39:18 -05002832int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04002833{
2834 return 0;
2835}
2836
Alan Sternb01b03f2008-04-28 11:06:11 -04002837/* However we may need to do a reset-resume */
2838
Alan Stern65bfd292008-11-25 16:39:18 -05002839int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04002840{
Alan Sternb01b03f2008-04-28 11:06:11 -04002841 struct usb_hub *hub = hdev_to_hub(udev->parent);
2842 int port1 = udev->portnum;
2843 int status;
2844 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04002845
Alan Sternb01b03f2008-04-28 11:06:11 -04002846 status = hub_port_status(hub, port1, &portstatus, &portchange);
2847 status = check_port_resume_type(udev,
2848 hub, port1, status, portchange, portstatus);
2849
2850 if (status) {
2851 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2852 hub_port_logical_disconnect(hub, port1);
2853 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04002854 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08002855 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002856 }
2857 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04002858}
2859
Alan Sternd388dab2006-07-01 22:14:24 -04002860#endif
2861
David Brownelldb690872005-09-13 19:56:33 -07002862static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct usb_hub *hub = usb_get_intfdata (intf);
2865 struct usb_device *hdev = hub->hdev;
2866 unsigned port1;
Sarah Sharp4296c702012-01-06 10:34:31 -08002867 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Alan Sterncbb33002011-06-15 16:29:16 -04002869 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
2871 struct usb_device *udev;
2872
2873 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04002874 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04002875 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002876 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002877 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07002878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
Sarah Sharp4296c702012-01-06 10:34:31 -08002880 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
2881 /* Enable hub to send remote wakeup for all ports. */
2882 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
2883 status = set_port_feature(hdev,
2884 port1 |
2885 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
2886 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
2887 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
2888 USB_PORT_FEAT_REMOTE_WAKE_MASK);
2889 }
2890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Harvey Harrison441b62c2008-03-03 16:08:34 -08002892 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05002893
David Brownellc9f89fa2005-09-13 19:57:04 -07002894 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04002895 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f6436d2007-05-04 11:51:54 -04002896 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
2899static int hub_resume(struct usb_interface *intf)
2900{
Alan Stern5e6effa2008-03-03 15:15:51 -05002901 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Alan Stern5e6effa2008-03-03 15:15:51 -05002903 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04002904 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 return 0;
2906}
2907
Alan Sternb41a60e2007-05-30 15:39:33 -04002908static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04002909{
Alan Sternb41a60e2007-05-30 15:39:33 -04002910 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04002911
Alan Stern5e6effa2008-03-03 15:15:51 -05002912 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04002913 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04002914 return 0;
2915}
2916
Alan Stern54515fe2007-05-30 15:38:58 -04002917/**
2918 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
2919 * @rhdev: struct usb_device for the root hub
2920 *
2921 * The USB host controller driver calls this function when its root hub
2922 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05002923 * has been reset. The routine marks @rhdev as having lost power.
2924 * When the hub driver is resumed it will take notice and carry out
2925 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2926 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04002927 */
2928void usb_root_hub_lost_power(struct usb_device *rhdev)
2929{
2930 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
2931 rhdev->reset_resume = 1;
2932}
2933EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
2934
Alan Sternd388dab2006-07-01 22:14:24 -04002935#else /* CONFIG_PM */
2936
Alan Sternf07600c2007-05-30 15:38:16 -04002937#define hub_suspend NULL
2938#define hub_resume NULL
2939#define hub_reset_resume NULL
Alan Sternd388dab2006-07-01 22:14:24 -04002940#endif
2941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
2944 *
2945 * Between connect detection and reset signaling there must be a delay
2946 * of 100ms at least for debounce and power-settling. The corresponding
2947 * timer shall restart whenever the downstream port detects a disconnect.
2948 *
2949 * Apparently there are some bluetooth and irda-dongles and a number of
2950 * low-speed devices for which this debounce period may last over a second.
2951 * Not covered by the spec - but easy to deal with.
2952 *
2953 * This implementation uses a 1500ms total debounce timeout; if the
2954 * connection isn't stable by then it returns -ETIMEDOUT. It checks
2955 * every 25ms for transient disconnects. When the port status has been
2956 * unchanged for 100ms it returns the port status.
2957 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958static int hub_port_debounce(struct usb_hub *hub, int port1)
2959{
2960 int ret;
2961 int total_time, stable_time = 0;
2962 u16 portchange, portstatus;
2963 unsigned connection = 0xffff;
2964
2965 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
2966 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2967 if (ret < 0)
2968 return ret;
2969
2970 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
2971 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
2972 stable_time += HUB_DEBOUNCE_STEP;
2973 if (stable_time >= HUB_DEBOUNCE_STABLE)
2974 break;
2975 } else {
2976 stable_time = 0;
2977 connection = portstatus & USB_PORT_STAT_CONNECTION;
2978 }
2979
2980 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2981 clear_port_feature(hub->hdev, port1,
2982 USB_PORT_FEAT_C_CONNECTION);
2983 }
2984
2985 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
2986 break;
2987 msleep(HUB_DEBOUNCE_STEP);
2988 }
2989
2990 dev_dbg (hub->intfdev,
2991 "debounce: port %d: total %dms stable %dms status 0x%x\n",
2992 port1, total_time, stable_time, portstatus);
2993
2994 if (stable_time < HUB_DEBOUNCE_STABLE)
2995 return -ETIMEDOUT;
2996 return portstatus;
2997}
2998
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07002999void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
Alan Sternddeac4e2009-01-15 17:03:33 -05003001 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
3002 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05003003 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003005EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3008#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3009
Alan Stern4326ed02007-07-30 17:08:43 -04003010static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
3012 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003013 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
Sarah Sharpc6515272009-04-27 19:57:26 -07003015 /*
3016 * The host controller will choose the device address,
3017 * instead of the core having chosen it earlier
3018 */
3019 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return -EINVAL;
3021 if (udev->state == USB_STATE_ADDRESS)
3022 return 0;
3023 if (udev->state != USB_STATE_DEFAULT)
3024 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003025 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003026 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003027 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003028 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3029 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3030 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003032 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003033 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003035 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
3037 return retval;
3038}
3039
3040/* Reset device, (re)assign address, get device descriptor.
3041 * Device connection must be stable, no more debouncing needed.
3042 * Returns device in USB_STATE_ADDRESS, except on error.
3043 *
3044 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003045 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 * newly detected device that is not accessible through any global
3047 * pointers, it's not necessary to lock the device.
3048 */
3049static int
3050hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3051 int retry_counter)
3052{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003053 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
3055 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003056 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 int i, j, retval;
3058 unsigned delay = HUB_SHORT_RESET_TIME;
3059 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003060 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003061 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063 /* root hub ports have a slightly longer reset period
3064 * (from USB 2.0 spec, section 7.1.7.5)
3065 */
3066 if (!hdev->parent) {
3067 delay = HUB_ROOT_RESET_TIME;
3068 if (port1 == hdev->bus->otg_port)
3069 hdev->bus->b_hnp_enable = 0;
3070 }
3071
3072 /* Some low speed devices have problems with the quick delay, so */
3073 /* be a bit pessimistic with those devices. RHbug #23670 */
3074 if (oldspeed == USB_SPEED_LOW)
3075 delay = HUB_LONG_RESET_TIME;
3076
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003077 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
Luben Tuikov07194ab2011-02-11 11:33:10 -08003079 /* Reset the device; full speed may morph to high speed */
3080 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003081 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003082 if (retval < 0) /* error or disconnect */
3083 goto fail;
3084 /* success, speed is known */
3085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 retval = -ENODEV;
3087
3088 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3089 dev_dbg(&udev->dev, "device reset changed speed!\n");
3090 goto fail;
3091 }
3092 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3095 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003096 * For Wireless USB devices, ep0 max packet is always 512 (tho
3097 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 */
3099 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003100 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003101 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003102 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003105 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 break;
3107 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3108 /* to determine the ep0 maxpacket size, try to read
3109 * the device descriptor to get bMaxPacketSize0 and
3110 * then correct our initial guess.
3111 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003112 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 break;
3114 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003115 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 break;
3117 default:
3118 goto fail;
3119 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003120
3121 if (udev->speed == USB_SPEED_WIRELESS)
3122 speed = "variable speed Wireless";
3123 else
3124 speed = usb_speed_string(udev->speed);
3125
Sarah Sharpc6515272009-04-27 19:57:26 -07003126 if (udev->speed != USB_SPEED_SUPER)
3127 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003128 "%s %s USB device number %d using %s\n",
3129 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003130 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
3132 /* Set up TT records, if needed */
3133 if (hdev->tt) {
3134 udev->tt = hdev->tt;
3135 udev->ttport = hdev->ttport;
3136 } else if (udev->speed != USB_SPEED_HIGH
3137 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003138 if (!hub->tt.hub) {
3139 dev_err(&udev->dev, "parent hub has no TT\n");
3140 retval = -EINVAL;
3141 goto fail;
3142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 udev->tt = &hub->tt;
3144 udev->ttport = port1;
3145 }
3146
3147 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3148 * Because device hardware and firmware is sometimes buggy in
3149 * this area, and this is how Linux has done it for ages.
3150 * Change it cautiously.
3151 *
3152 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3153 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3154 * so it may help with some non-standards-compliant devices.
3155 * Otherwise we start with SET_ADDRESS and then try to read the
3156 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3157 * value.
3158 */
3159 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07003160 if (USE_NEW_SCHEME(retry_counter) &&
3161 !(hcd->driver->flags & HCD_USB3) &&
3162 !(hcd->driver->flags & HCD_OLD_ENUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 struct usb_device_descriptor *buf;
3164 int r = 0;
3165
3166#define GET_DESCRIPTOR_BUFSIZE 64
3167 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3168 if (!buf) {
3169 retval = -ENOMEM;
3170 continue;
3171 }
3172
Alan Sternb89ee192007-05-11 10:19:04 -04003173 /* Retry on all errors; some devices are flakey.
3174 * 255 is for WUSB devices, we actually need to use
3175 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 */
3177 for (j = 0; j < 3; ++j) {
3178 buf->bMaxPacketSize0 = 0;
3179 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3180 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3181 USB_DT_DEVICE << 8, 0,
3182 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003183 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003185 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 if (buf->bDescriptorType ==
3187 USB_DT_DEVICE) {
3188 r = 0;
3189 break;
3190 }
3191 /* FALL THROUGH */
3192 default:
3193 if (r == 0)
3194 r = -EPROTO;
3195 break;
3196 }
3197 if (r == 0)
3198 break;
3199 }
3200 udev->descriptor.bMaxPacketSize0 =
3201 buf->bMaxPacketSize0;
3202 kfree(buf);
3203
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003204 /*
3205 * If it is a HSET Test device, we don't issue a
3206 * second reset which results in failure due to
3207 * speed change.
3208 */
3209 if (le16_to_cpu(buf->idVendor) != 0x1a0a) {
3210 retval = hub_port_reset(hub, port1, udev,
Steve Mucklef132c6c2012-06-06 18:30:57 -07003211 delay, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003212 if (retval < 0) /* error or disconnect */
3213 goto fail;
3214 if (oldspeed != udev->speed) {
3215 dev_dbg(&udev->dev,
3216 "device reset changed speed!\n");
3217 retval = -ENODEV;
3218 goto fail;
3219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 }
3221 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003222 dev_err(&udev->dev,
3223 "device descriptor read/64, error %d\n",
3224 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 retval = -EMSGSIZE;
3226 continue;
3227 }
3228#undef GET_DESCRIPTOR_BUFSIZE
3229 }
3230
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003231 /*
3232 * If device is WUSB, we already assigned an
3233 * unauthorized address in the Connect Ack sequence;
3234 * authorization will assign the final address.
3235 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003236 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003237 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3238 retval = hub_set_address(udev, devnum);
3239 if (retval >= 0)
3240 break;
3241 msleep(200);
3242 }
3243 if (retval < 0) {
3244 dev_err(&udev->dev,
3245 "device not accepting address %d, error %d\n",
3246 devnum, retval);
3247 goto fail;
3248 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003249 if (udev->speed == USB_SPEED_SUPER) {
3250 devnum = udev->devnum;
3251 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003252 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003253 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003254 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003255 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003256
3257 /* cope with hardware quirkiness:
3258 * - let SET_ADDRESS settle, some device hardware wants it
3259 * - read ep0 maxpacket even for high and low speed,
3260 */
3261 msleep(10);
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07003262 if (USE_NEW_SCHEME(retry_counter) &&
3263 !(hcd->driver->flags & HCD_USB3) &&
3264 !(hcd->driver->flags & HCD_OLD_ENUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268 retval = usb_get_device_descriptor(udev, 8);
3269 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003270 dev_err(&udev->dev,
3271 "device descriptor read/8, error %d\n",
3272 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (retval >= 0)
3274 retval = -EMSGSIZE;
3275 } else {
3276 retval = 0;
3277 break;
3278 }
3279 }
3280 if (retval)
3281 goto fail;
3282
Elric Fud8aec3d2012-03-26 21:16:02 +08003283 /*
3284 * Some superspeed devices have finished the link training process
3285 * and attached to a superspeed hub port, but the device descriptor
3286 * got from those devices show they aren't superspeed devices. Warm
3287 * reset the port attached by the devices can fix them.
3288 */
3289 if ((udev->speed == USB_SPEED_SUPER) &&
3290 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3291 dev_err(&udev->dev, "got a wrong device descriptor, "
3292 "warm reset device\n");
3293 hub_port_reset(hub, port1, udev,
3294 HUB_BH_RESET_TIME, true);
3295 retval = -EINVAL;
3296 goto fail;
3297 }
3298
Sarah Sharp6b403b02009-04-27 19:54:10 -07003299 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3300 udev->speed == USB_SPEED_SUPER)
3301 i = 512;
3302 else
3303 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003304 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003305 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003307 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 retval = -EMSGSIZE;
3309 goto fail;
3310 }
Alan Stern56626a72010-10-14 15:25:21 -04003311 if (udev->speed == USB_SPEED_FULL)
3312 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3313 else
3314 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003316 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 }
3318
3319 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3320 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003321 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3322 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (retval >= 0)
3324 retval = -ENOMSG;
3325 goto fail;
3326 }
3327
Andiry Xu1ff4df52011-09-23 14:19:48 -07003328 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3329 retval = usb_get_bos_descriptor(udev);
3330 if (!retval) {
3331 if (udev->bos->ext_cap && (USB_LPM_SUPPORT &
3332 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
3333 udev->lpm_capable = 1;
3334 }
3335 }
Andiry Xu3148bf02011-09-23 14:19:47 -07003336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08003338 /* notify HCD that we have a device connected and addressed */
3339 if (hcd->driver->update_device)
3340 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341fail:
Alan Stern4326ed02007-07-30 17:08:43 -04003342 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05003344 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04003345 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003346 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 return retval;
3348}
3349
3350static void
3351check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
3352{
3353 struct usb_qualifier_descriptor *qual;
3354 int status;
3355
Christoph Lametere94b1762006-12-06 20:33:17 -08003356 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 if (qual == NULL)
3358 return;
3359
3360 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
3361 qual, sizeof *qual);
3362 if (status == sizeof *qual) {
3363 dev_info(&udev->dev, "not running at top speed; "
3364 "connect to a high speed hub\n");
3365 /* hub LEDs are probably harder to miss than syslog */
3366 if (hub->has_indicators) {
3367 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003368 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 }
3370 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003371 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372}
3373
3374static unsigned
3375hub_power_remaining (struct usb_hub *hub)
3376{
3377 struct usb_device *hdev = hub->hdev;
3378 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05003379 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Alan Stern55c52712005-11-23 12:03:12 -05003381 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return 0;
3383
Alan Stern55c52712005-11-23 12:03:12 -05003384 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
3385 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
3386 struct usb_device *udev = hdev->children[port1 - 1];
3387 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389 if (!udev)
3390 continue;
3391
Alan Stern55c52712005-11-23 12:03:12 -05003392 /* Unconfigured devices may not use more than 100mA,
3393 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05003395 delta = udev->actconfig->desc.bMaxPower * 2;
3396 else if (port1 != udev->bus->otg_port || hdev->parent)
3397 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 else
Alan Stern55c52712005-11-23 12:03:12 -05003399 delta = 8;
3400 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003401 dev_warn(&udev->dev,
3402 "%dmA is over %umA budget for port %d!\n",
3403 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 remaining -= delta;
3405 }
3406 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05003407 dev_warn(hub->intfdev, "%dmA over power budget!\n",
3408 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 remaining = 0;
3410 }
3411 return remaining;
3412}
3413
3414/* Handle physical or logical connection change events.
3415 * This routine is called when:
3416 * a port connection-change occurs;
3417 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08003418 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 * a firmware download)
3420 * caller already locked the hub
3421 */
3422static void hub_port_connect_change(struct usb_hub *hub, int port1,
3423 u16 portstatus, u16 portchange)
3424{
3425 struct usb_device *hdev = hub->hdev;
3426 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05303427 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04003428 unsigned wHubCharacteristics =
3429 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04003430 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04003432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 dev_dbg (hub_dev,
3434 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08003435 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 if (hub->has_indicators) {
3438 set_port_led(hub, port1, HUB_LED_AUTO);
3439 hub->indicator[port1-1] = INDICATOR_AUTO;
3440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442#ifdef CONFIG_USB_OTG
3443 /* during HNP, don't repeat the debounce */
3444 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04003445 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
3446 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447#endif
3448
Alan Stern8808f002008-04-28 11:06:55 -04003449 /* Try to resuscitate an existing device */
3450 udev = hdev->children[port1-1];
3451 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
3452 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04003453 usb_lock_device(udev);
3454 if (portstatus & USB_PORT_STAT_ENABLE) {
3455 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04003456
3457#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04003458 } else if (udev->state == USB_STATE_SUSPENDED &&
3459 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04003460 /* For a suspended device, treat this as a
3461 * remote wakeup event.
3462 */
Alan Stern0534d462010-01-08 12:56:30 -05003463 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04003464#endif
3465
3466 } else {
Alan Stern5257d972008-09-22 14:43:08 -04003467 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04003468 }
3469 usb_unlock_device(udev);
3470
3471 if (status == 0) {
3472 clear_bit(port1, hub->change_bits);
3473 return;
3474 }
3475 }
3476
Alan Stern24618b02008-04-28 11:06:28 -04003477 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04003478 if (udev)
Alan Stern24618b02008-04-28 11:06:28 -04003479 usb_disconnect(&hdev->children[port1-1]);
3480 clear_bit(port1, hub->change_bits);
3481
Alan Stern253e0572009-10-27 15:20:13 -04003482 /* We can forget about a "removed" device when there's a physical
3483 * disconnect or the connect status changes.
3484 */
3485 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3486 (portchange & USB_PORT_STAT_C_CONNECTION))
3487 clear_bit(port1, hub->removed_bits);
3488
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003489#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
3490 if (Unwanted_SecondReset == 0) /*stericsson*/
3491#endif
Alan Stern5257d972008-09-22 14:43:08 -04003492 if (portchange & (USB_PORT_STAT_C_CONNECTION |
3493 USB_PORT_STAT_C_ENABLE)) {
3494 status = hub_port_debounce(hub, port1);
3495 if (status < 0) {
3496 if (printk_ratelimit())
3497 dev_err(hub_dev, "connect-debounce failed, "
3498 "port %d disabled\n", port1);
3499 portstatus &= ~USB_PORT_STAT_CONNECTION;
3500 } else {
3501 portstatus = status;
3502 }
3503 }
3504
Alan Stern253e0572009-10-27 15:20:13 -04003505 /* Return now if debouncing failed or nothing is connected or
3506 * the device was "removed".
3507 */
3508 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3509 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
3511 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07003512 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08003513 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04003515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (portstatus & USB_PORT_STAT_ENABLE)
3517 goto done;
3518 return;
3519 }
3520
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
3523 /* reallocate for each attempt, since references
3524 * to the previous one can escape in various ways
3525 */
3526 udev = usb_alloc_dev(hdev, hdev->bus, port1);
3527 if (!udev) {
3528 dev_err (hub_dev,
3529 "couldn't allocate port %d usb_device\n",
3530 port1);
3531 goto done;
3532 }
3533
3534 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05003535 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04003536 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07003537 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05003538
Sarah Sharp131dec32010-12-06 21:00:19 -08003539 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
3540 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07003541 udev->speed = USB_SPEED_SUPER;
3542 else
3543 udev->speed = USB_SPEED_UNKNOWN;
3544
Alan Stern3b29b682011-02-22 09:53:41 -05003545 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003546 if (udev->devnum <= 0) {
3547 status = -ENOTCONN; /* Don't retry */
3548 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07003549 }
3550
Sarah Sharpe7b77172009-04-27 19:54:26 -07003551 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 status = hub_port_init(hub, udev, port1, i);
3553 if (status < 0)
3554 goto loop;
3555
Phil Dibowitz93362a82010-07-22 00:05:01 +02003556 usb_detect_quirks(udev);
3557 if (udev->quirks & USB_QUIRK_DELAY_INIT)
3558 msleep(1000);
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 /* consecutive bus-powered hubs aren't reliable; they can
3561 * violate the voltage drop budget. if the new child has
3562 * a "powered" LED, users should notice we didn't enable it
3563 * (without reading syslog), even without per-port LEDs
3564 * on the parent.
3565 */
3566 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05003567 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 u16 devstat;
3569
3570 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
3571 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05003572 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 dev_dbg(&udev->dev, "get status %d ?\n", status);
3574 goto loop_disable;
3575 }
Alan Stern55c52712005-11-23 12:03:12 -05003576 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
3578 dev_err(&udev->dev,
3579 "can't connect bus-powered hub "
3580 "to this port\n");
3581 if (hub->has_indicators) {
3582 hub->indicator[port1-1] =
3583 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003584 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 }
3586 status = -ENOTCONN; /* Don't retry */
3587 goto loop_disable;
3588 }
3589 }
3590
3591 /* check for devices running slower than they could */
3592 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
3593 && udev->speed == USB_SPEED_FULL
3594 && highspeed_hubs != 0)
3595 check_highspeed (hub, udev, port1);
3596
3597 /* Store the parent's children[] pointer. At this point
3598 * udev becomes globally accessible, although presumably
3599 * no one will look at it until hdev is unlocked.
3600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 status = 0;
3602
3603 /* We mustn't add new devices if the parent hub has
3604 * been disconnected; we would race with the
3605 * recursively_mark_NOTATTACHED() routine.
3606 */
3607 spin_lock_irq(&device_state_lock);
3608 if (hdev->state == USB_STATE_NOTATTACHED)
3609 status = -ENOTCONN;
3610 else
3611 hdev->children[port1-1] = udev;
3612 spin_unlock_irq(&device_state_lock);
3613
3614 /* Run it through the hoops (find a driver, etc) */
3615 if (!status) {
3616 status = usb_new_device(udev);
3617 if (status) {
3618 spin_lock_irq(&device_state_lock);
3619 hdev->children[port1-1] = NULL;
3620 spin_unlock_irq(&device_state_lock);
3621 }
3622 }
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 if (status)
3625 goto loop_disable;
3626
3627 status = hub_power_remaining(hub);
3628 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05003629 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003630#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
3631 if (HostComplianceTest == 1 && udev->devnum > 1) {
3632 if (HostTest == 7) { /*SINGLE_STEP_GET_DEV_DESC */
3633 dev_info(hub_dev, "Testing "
3634 "SINGLE_STEP_GET_DEV_DESC\n");
3635 /* Test the Single Step Get Device Descriptor ,
3636 * take care it should not get status phase
3637 */
3638 No_Data_Phase = 1;
3639 No_Status_Phase = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003641 usb_get_device_descriptor(udev, 8);
3642 No_Data_Phase = 0;
3643 No_Status_Phase = 0;
3644 }
3645
3646 if (HostTest == 8) {
3647 dev_info(hub_dev, "Testing "
3648 "SINGLE_STEP_SET_FEATURE\n");
3649 /* Test Single Step Set Feature */
3650 No_Status_Phase = 1;
3651 usb_get_device_descriptor(udev, 8);
3652 No_Status_Phase = 0;
3653 }
3654 }
3655#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 return;
3657
3658loop_disable:
3659 hub_port_disable(hub, port1, 1);
3660loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003661 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05003662 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11003663 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07003665 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 break;
3667 }
Alan Stern3a311552008-05-20 16:58:29 -04003668 if (hub->hdev->parent ||
3669 !hcd->driver->port_handed_over ||
3670 !(hcd->driver->port_handed_over)(hcd, port1))
3671 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
3672 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
3674done:
3675 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05303676 if (hcd->driver->relinquish_port && !hub->hdev->parent)
3677 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
Sarah Sharp714b07b2012-01-24 13:53:18 -08003680/* Returns 1 if there was a remote wakeup and a connect status change. */
3681static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08003682 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08003683{
3684 struct usb_device *hdev;
3685 struct usb_device *udev;
3686 int connect_change = 0;
3687 int ret;
3688
3689 hdev = hub->hdev;
Sarah Sharp714b07b2012-01-24 13:53:18 -08003690 udev = hdev->children[port-1];
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003691 if (!hub_is_superspeed(hdev)) {
3692 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3693 return 0;
3694 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3695 } else {
3696 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08003697 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3698 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003699 return 0;
3700 }
3701
Sarah Sharp714b07b2012-01-24 13:53:18 -08003702 if (udev) {
3703 /* TRSMRCY = 10 msec */
3704 msleep(10);
3705
3706 usb_lock_device(udev);
3707 ret = usb_remote_wakeup(udev);
3708 usb_unlock_device(udev);
3709 if (ret < 0)
3710 connect_change = 1;
3711 } else {
3712 ret = -ENODEV;
3713 hub_port_disable(hub, port, 1);
3714 }
3715 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
3716 port, ret);
3717 return connect_change;
3718}
3719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720static void hub_events(void)
3721{
3722 struct list_head *tmp;
3723 struct usb_device *hdev;
3724 struct usb_interface *intf;
3725 struct usb_hub *hub;
3726 struct device *hub_dev;
3727 u16 hubstatus;
3728 u16 hubchange;
3729 u16 portstatus;
3730 u16 portchange;
3731 int i, ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003732#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
3733 int j;
3734 int otgport = 0;
3735 struct usb_port_status port_status;
3736#endif
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003737 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
3739 /*
3740 * We restart the list every time to avoid a deadlock with
3741 * deleting hubs downstream from this one. This should be
3742 * safe since we delete the hub from the event list.
3743 * Not the most efficient, but avoids deadlocks.
3744 */
3745 while (1) {
3746
3747 /* Grab the first entry at the beginning of the list */
3748 spin_lock_irq(&hub_event_lock);
3749 if (list_empty(&hub_event_list)) {
3750 spin_unlock_irq(&hub_event_lock);
3751 break;
3752 }
3753
3754 tmp = hub_event_list.next;
3755 list_del_init(tmp);
3756
3757 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04003758 kref_get(&hub->kref);
3759 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Alan Sterne8054852007-05-04 11:55:11 -04003761 hdev = hub->hdev;
3762 hub_dev = hub->intfdev;
3763 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05003764 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 hdev->state, hub->descriptor
3766 ? hub->descriptor->bNbrPorts
3767 : 0,
3768 /* NOTE: expects max 15 ports... */
3769 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05003770 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 /* Lock the device, then check to see if we were
3773 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04003774 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04003775 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003776 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
3778 /* If the hub has died, clean up after it */
3779 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04003780 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04003781 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 goto loop;
3783 }
3784
Alan Stern40f122f2006-11-09 14:44:33 -05003785 /* Autoresume */
3786 ret = usb_autopm_get_interface(intf);
3787 if (ret) {
3788 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05003790 }
3791
3792 /* If this is an inactive hub, do nothing */
3793 if (hub->quiescing)
3794 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796 if (hub->error) {
3797 dev_dbg (hub_dev, "resetting for error %d\n",
3798 hub->error);
3799
Ming Lei742120c2008-06-18 22:00:29 +08003800 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 if (ret) {
3802 dev_dbg (hub_dev,
3803 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05003804 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806
3807 hub->nerrors = 0;
3808 hub->error = 0;
3809 }
3810
3811 /* deal with port status changes */
3812 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003813#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
3814 struct usb_port_status portsts;
3815
3816 /*if we have something to do on
3817 * otg port
3818 * */
3819 if ((hdev->otgstate & USB_OTG_SUSPEND) ||
3820 (hdev->otgstate & USB_OTG_ENUMERATE) ||
3821 (hdev->otgstate & USB_OTG_DISCONNECT) ||
3822 (hdev->otgstate & USB_OTG_RESUME)) {
3823 otgport = 1;
3824 }
3825
3826
3827 if (hdev->otgstate & USB_OTG_RESUME) {
3828 ret = clear_port_feature(hdev, i,
3829 USB_PORT_FEAT_SUSPEND);
3830 if (ret < 0) {
3831 dev_err(hub_dev, "usb otg port Resume"
3832 " fails, %d\n", ret);
3833 }
3834 hdev->otgstate &= ~USB_OTG_RESUME;
3835 }
3836 if ((hdev->otgstate & USB_OTG_SUSPEND)
3837 && (hdev->children[0])) {
3838 hdev->otgstate &= ~USB_OTG_SUSPEND;
3839
3840 ret = set_port_feature(hdev, 1,
3841 USB_PORT_FEAT_SUSPEND);
3842 if (ret < 0) {
3843 dev_err(hub_dev, "usb otg port suspend"
3844 " fails, %d\n", ret);
3845 break;
3846 }
3847 msleep(1);
3848 ret = get_port_status(hdev, i, &portsts);
3849 if (ret < 0) {
3850 dev_err(hub_dev, "usb otg get port"
3851 " status fails, %d\n", ret);
3852 break;
3853 }
3854 portchange = le16_to_cpu(portsts.wPortChange);
3855 if (portchange & USB_PORT_STAT_C_SUSPEND) {
3856 clear_port_feature(hdev, i,
3857 USB_PORT_FEAT_C_SUSPEND);
3858 }
3859 break;
3860 }
3861
3862 if (hdev->otgstate & USB_OTG_REMOTEWAKEUP) {
3863
3864 for (j = 1; j <= hub->descriptor->bNbrPorts;
3865 j++) {
3866 if (hdev->children[j - 1]) {
3867 dev_dbg(hub_dev, "child"
3868 " found at port %d\n", j);
3869 ret = usb_control_msg(hdev->
3870 children[j - 1],
3871 usb_sndctrlpipe(hdev->
3872 children[j - 1],
3873 0),
3874 USB_REQ_SET_FEATURE,
3875 USB_RECIP_DEVICE,
3876 USB_DEVICE_REMOTE_WAKEUP,
3877 0, NULL,
3878 0,
3879 USB_CTRL_SET_TIMEOUT);
3880 if (ret < 0) {
3881 dev_err(hub_dev, "Port"
3882 " %d doesn't support"
3883 "remote wakeup\n", j);
3884 } else {
3885 dev_dbg(hub_dev, "Port"
3886 " %d supports"
3887 "remote wakeup\n", j);
3888 }
3889 ret = set_port_feature(hdev, j,
3890 USB_PORT_FEAT_SUSPEND);
3891 if (ret < 0) {
3892 dev_err(hub_dev, "Port"
3893 " %d NOT ABLE TO"
3894 " SUSPEND\n", j);
3895 } else {
3896 dev_dbg(hub_dev, "Port"
3897 " %d is ABLE TO"
3898 " SUSPEND\n", j);
3899 }
3900 }
3901 }
3902 ret = usb_control_msg(hdev,
3903 usb_sndctrlpipe(hdev, 0),
3904 USB_REQ_SET_FEATURE,
3905 USB_RECIP_DEVICE,
3906 USB_DEVICE_REMOTE_WAKEUP,
3907 0, NULL, 0,
3908 USB_CTRL_SET_TIMEOUT);
3909 if (ret < 0) {
3910 dev_err(hub_dev, "HUB doesn't support"
3911 " REMOTE WAKEUP\n");
3912 } else {
3913 dev_dbg(hub_dev, "HUB supports"
3914 " REMOTE WAKEUP\n");
3915 }
3916 ret = 0;
3917 msleep(10);
3918 if (hdev->parent == hdev->bus->root_hub) {
3919 if (hdev->hcd_suspend &&
3920 hdev->hcd_priv) {
3921 dev_dbg(hub_dev, "calling"
3922 " suspend after remote wakeup"
3923 " command is issued\n");
3924 hdev->hcd_suspend(hdev->
3925 hcd_priv);
3926 }
3927 if (hdev->otg_notif)
3928 hdev->otg_notif(hdev->otgpriv,
3929 PDC_POWERMANAGEMENT, 10);
3930 }
3931 }
3932
3933 if (hdev->otgstate & USB_OTG_WAKEUP_ALL) {
3934 (void) usb_control_msg(hdev,
3935 usb_sndctrlpipe(hdev, 0),
3936 USB_REQ_CLEAR_FEATURE,
3937 USB_RECIP_DEVICE,
3938 USB_DEVICE_REMOTE_WAKEUP,
3939 0, NULL, 0,
3940 USB_CTRL_SET_TIMEOUT);
3941 dev_dbg(hub_dev, "Hub CLEARED REMOTE WAKEUP\n");
3942 for (j = 1; j <= hub->descriptor->bNbrPorts;
3943 j++) {
3944 if (hdev->children[j - 1]) {
3945 dev_dbg(hub_dev, "PORT %d"
3946 " SUSPEND IS CLEARD\n", j);
3947 clear_port_feature(hdev, j,
3948 USB_PORT_FEAT_C_SUSPEND);
3949 msleep(50);
3950 (void) usb_control_msg(hdev->
3951 children[j - 1],
3952 usb_sndctrlpipe(
3953 hdev->children[j - 1],
3954 0),
3955 USB_REQ_CLEAR_FEATURE,
3956 USB_RECIP_DEVICE,
3957 USB_DEVICE_REMOTE_WAKEUP,
3958 0, NULL,
3959 0,
3960 USB_CTRL_SET_TIMEOUT);
3961 dev_dbg(hub_dev, "PORT %d "
3962 "REMOTE WAKEUP IS "
3963 "CLEARD\n", j);
3964 msleep(10);
3965 }
3966 }
3967
3968
3969 }
3970
3971
3972 /*
3973 * reset the state of otg device,
3974 * regardless of otg device
3975 */
3976 hdev->otgstate = 0;
3977#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 if (test_bit(i, hub->busy_bits))
3979 continue;
3980 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08003981 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003983 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 continue;
3985
3986 ret = hub_port_status(hub, i,
3987 &portstatus, &portchange);
3988 if (ret < 0)
3989 continue;
3990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3992 clear_port_feature(hdev, i,
3993 USB_PORT_FEAT_C_CONNECTION);
3994 connect_change = 1;
3995 }
3996
3997 if (portchange & USB_PORT_STAT_C_ENABLE) {
3998 if (!connect_change)
3999 dev_dbg (hub_dev,
4000 "port %d enable change, "
4001 "status %08x\n",
4002 i, portstatus);
4003 clear_port_feature(hdev, i,
4004 USB_PORT_FEAT_C_ENABLE);
4005
4006 /*
4007 * EM interference sometimes causes badly
4008 * shielded USB devices to be shutdown by
4009 * the hub, this hack enables them again.
4010 * Works at least with mouse driver.
4011 */
4012 if (!(portstatus & USB_PORT_STAT_ENABLE)
4013 && !connect_change
4014 && hdev->children[i-1]) {
4015 dev_err (hub_dev,
4016 "port %i "
4017 "disabled by hub (EMI?), "
4018 "re-enabling...\n",
4019 i);
4020 connect_change = 1;
4021 }
4022 }
4023
Sarah Sharp72937e12012-01-24 11:46:50 -08004024 if (hub_handle_remote_wakeup(hub, i,
4025 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004026 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004027
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004029 u16 status = 0;
4030 u16 unused;
4031
4032 dev_dbg(hub_dev, "over-current change on port "
4033 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 clear_port_feature(hdev, i,
4035 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004036 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004037 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004038 hub_port_status(hub, i, &status, &unused);
4039 if (status & USB_PORT_STAT_OVERCURRENT)
4040 dev_err(hub_dev, "over-current "
4041 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 }
4043
4044 if (portchange & USB_PORT_STAT_C_RESET) {
4045 dev_dbg (hub_dev,
4046 "reset change on port %d\n",
4047 i);
4048 clear_port_feature(hdev, i,
4049 USB_PORT_FEAT_C_RESET);
4050 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004051 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4052 hub_is_superspeed(hub->hdev)) {
4053 dev_dbg(hub_dev,
4054 "warm reset change on port %d\n",
4055 i);
4056 clear_port_feature(hdev, i,
4057 USB_PORT_FEAT_C_BH_PORT_RESET);
4058 }
John Youndbe79bb2001-09-17 00:00:00 -07004059 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4060 clear_port_feature(hub->hdev, i,
4061 USB_PORT_FEAT_C_PORT_LINK_STATE);
4062 }
4063 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4064 dev_warn(hub_dev,
4065 "config error on port %d\n",
4066 i);
4067 clear_port_feature(hub->hdev, i,
4068 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Andiry Xu5e467f62011-04-27 18:07:54 +08004071 /* Warm reset a USB3 protocol port if it's in
4072 * SS.Inactive state.
4073 */
4074 if (hub_is_superspeed(hub->hdev) &&
4075 (portstatus & USB_PORT_STAT_LINK_STATE)
4076 == USB_SS_PORT_LS_SS_INACTIVE) {
4077 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004078 hub_port_reset(hub, i, NULL,
4079 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004080 }
4081
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004082 if (connect_change) {
4083#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
4084 if (hdev->parent == hdev->bus->root_hub)
4085 if (hdev->otg_notif
4086 && (HostComplianceTest == 0))
4087 hdev->otg_notif(hdev->otgpriv,
4088 PDC_HOST_NOTIFY,
4089 5);
4090 portno = i;
4091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 hub_port_connect_change(hub, i,
4093 portstatus, portchange);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 } /* end for i */
4096
4097 /* deal with hub status changes */
4098 if (test_and_clear_bit(0, hub->event_bits) == 0)
4099 ; /* do nothing */
4100 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4101 dev_err (hub_dev, "get_hub_status failed\n");
4102 else {
4103 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4104 dev_dbg (hub_dev, "power change\n");
4105 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004106 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4107 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004108 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004109 else
4110 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 }
4112 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004113 u16 status = 0;
4114 u16 unused;
4115
4116 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004118 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004119 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004120 hub_hub_status(hub, &status, &unused);
4121 if (status & HUB_STATUS_OVERCURRENT)
4122 dev_err(hub_dev, "over-current "
4123 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 }
4125 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004126#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
4127 /* if we have something on otg */
4128 if (otgport) {
4129 otgport = 0;
4130 /* notify otg controller about it */
4131 if (hdev->parent == hdev->bus->root_hub)
4132 if (hdev->otg_notif)
4133 hdev->otg_notif(hdev->otgpriv,
4134 PDC_HOST_NOTIFY, 0);
4135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004137 if (HostComplianceTest && hdev->devnum > 1) {
4138 /* TEST_SE0_NAK */
4139 if (HostTest == 1) {
4140 dev_info(hub_dev, "Testing for TEST_SE0_NAK\n");
4141 ret = clear_port_feature(hdev, portno,
4142 USB_PORT_FEAT_C_CONNECTION);
4143 ret = set_port_feature(hdev, portno,
4144 USB_PORT_FEAT_SUSPEND);
4145 ret = set_port_feature(hdev, portno | 0x300,
4146 USB_PORT_FEAT_TEST);
4147 ret = get_port_status(hdev, portno,
4148 &port_status);
4149 }
4150 /*TEST_J*/
4151 if (HostTest == 2) {
4152 dev_info(hub_dev, "Testing TEST_J\n");
4153 ret = clear_port_feature(hdev, portno,
4154 USB_PORT_FEAT_C_CONNECTION);
4155 ret = set_port_feature(hdev, portno,
4156 USB_PORT_FEAT_SUSPEND);
4157 ret = set_port_feature(hdev, portno | 0x100,
4158 USB_PORT_FEAT_TEST);
4159 ret = get_port_status(hdev, portno,
4160 &port_status);
4161 }
4162 if (HostTest == 3) {
4163 dev_info(hub_dev, "Testing TEST_K\n");
4164 ret = clear_port_feature(hdev, portno,
4165 USB_PORT_FEAT_C_CONNECTION);
4166 ret = set_port_feature(hdev, portno,
4167 USB_PORT_FEAT_SUSPEND);
4168 ret = set_port_feature(hdev, portno | 0x200,
4169 USB_PORT_FEAT_TEST);
4170 ret = get_port_status(hdev, portno,
4171 &port_status);
4172 }
4173 if (HostTest == 4) {
4174 dev_info(hub_dev, "Testing TEST_PACKET at Port"
4175 " %d\n", portno);
4176 ret = clear_port_feature(hdev, portno,
4177 USB_PORT_FEAT_C_CONNECTION);
4178 if (ret < 0)
4179 dev_err(hub_dev, "Clear port feature"
4180 " C_CONNECTION failed\n");
4181
4182 ret = set_port_feature(hdev, portno,
4183 USB_PORT_FEAT_SUSPEND);
4184 if (ret < 0)
4185 dev_err(hub_dev, "Clear port feature"
4186 " SUSPEND failed\n");
4187
4188 ret = set_port_feature(hdev, portno | 0x400,
4189 USB_PORT_FEAT_TEST);
4190 if (ret < 0)
4191 dev_err(hub_dev, "Clear port feature"
4192 " TEST failed\n");
4193
4194 ret = get_port_status(hdev, portno,
4195 &port_status);
4196 if (ret < 0)
4197 dev_err(hub_dev, "Get port status"
4198 " failed\n");
4199 }
4200 if (HostTest == 5) {
4201 dev_info(hub_dev, "Testing TEST_FORCE_ENBLE\n");
4202 ret = clear_port_feature(hdev, portno,
4203 USB_PORT_FEAT_C_CONNECTION);
4204 ret = set_port_feature(hdev, portno,
4205 USB_PORT_FEAT_SUSPEND);
4206 ret = set_port_feature(hdev, portno | 0x500,
4207 USB_PORT_FEAT_TEST);
4208 ret = get_port_status(hdev, portno,
4209 &port_status);
4210 }
4211 if (HostTest == 6) {
4212 dev_info(hub_dev, "Testing "
4213 "HS_HOST_PORT_SUSPEND_RESUME\n");
4214 ret = clear_port_feature(hdev, portno,
4215 USB_PORT_FEAT_C_CONNECTION);
4216 ret = set_port_feature(hdev, portno,
4217 USB_PORT_FEAT_SUSPEND);
4218 msleep(3000);
4219 ret = clear_port_feature(hdev, portno,
4220 USB_PORT_FEAT_SUSPEND);
4221 HostTest = 0;
4222 }
4223 }
4224#endif
Alan Stern8e4ceb32009-12-07 13:01:37 -05004225 loop_autopm:
4226 /* Balance the usb_autopm_get_interface() above */
4227 usb_autopm_put_interface_no_suspend(intf);
4228 loop:
4229 /* Balance the usb_autopm_get_interface_no_resume() in
4230 * kick_khubd() and allow autosuspend.
4231 */
4232 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004233 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004235 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 } /* end while (1) */
4238}
4239
4240static int hub_thread(void *__unused)
4241{
Alan Stern3bb1af52008-03-03 15:15:36 -05004242 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4243 * port handover. Otherwise it might see that a full-speed device
4244 * was gone before the EHCI controller had handed its port over to
4245 * the companion full-speed controller.
4246 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004247 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004248
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 do {
4250 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004251 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004252 !list_empty(&hub_event_list) ||
4253 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004254 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004256 pr_debug("%s: khubd exiting\n", usbcore_name);
4257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
Németh Márton1e927d92010-01-10 15:34:53 +01004260static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4262 .bDeviceClass = USB_CLASS_HUB},
4263 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4264 .bInterfaceClass = USB_CLASS_HUB},
4265 { } /* Terminating entry */
4266};
4267
4268MODULE_DEVICE_TABLE (usb, hub_id_table);
4269
4270static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 .name = "hub",
4272 .probe = hub_probe,
4273 .disconnect = hub_disconnect,
4274 .suspend = hub_suspend,
4275 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004276 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004277 .pre_reset = hub_pre_reset,
4278 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004279 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004281 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282};
4283
4284int usb_hub_init(void)
4285{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 if (usb_register(&hub_driver) < 0) {
4287 printk(KERN_ERR "%s: can't register hub driver\n",
4288 usbcore_name);
4289 return -1;
4290 }
4291
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004292 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4293 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
4296 /* Fall through if kernel_thread failed */
4297 usb_deregister(&hub_driver);
4298 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4299
4300 return -1;
4301}
4302
4303void usb_hub_cleanup(void)
4304{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004305 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
4307 /*
4308 * Hub resources are freed for us by usb_deregister. It calls
4309 * usb_driver_purge on every device which in turn calls that
4310 * devices disconnect function if it is using this driver.
4311 * The hub_disconnect function takes care of releasing the
4312 * individual hub resources. -greg
4313 */
4314 usb_deregister(&hub_driver);
4315} /* usb_hub_cleanup() */
4316
Alan Sterneb764c42008-03-03 15:16:04 -05004317static int descriptors_changed(struct usb_device *udev,
4318 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
Alan Sterneb764c42008-03-03 15:16:04 -05004320 int changed = 0;
4321 unsigned index;
4322 unsigned serial_len = 0;
4323 unsigned len;
4324 unsigned old_length;
4325 int length;
4326 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Alan Sterneb764c42008-03-03 15:16:04 -05004328 if (memcmp(&udev->descriptor, old_device_descriptor,
4329 sizeof(*old_device_descriptor)) != 0)
4330 return 1;
4331
4332 /* Since the idVendor, idProduct, and bcdDevice values in the
4333 * device descriptor haven't changed, we will assume the
4334 * Manufacturer and Product strings haven't changed either.
4335 * But the SerialNumber string could be different (e.g., a
4336 * different flash card of the same brand).
4337 */
4338 if (udev->serial)
4339 serial_len = strlen(udev->serial) + 1;
4340
4341 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004343 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4344 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 }
Alan Sterneb764c42008-03-03 15:16:04 -05004346
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004347 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 if (buf == NULL) {
4349 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4350 /* assume the worst */
4351 return 1;
4352 }
4353 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004354 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4356 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004357 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 dev_dbg(&udev->dev, "config index %d, error %d\n",
4359 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004360 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 break;
4362 }
4363 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4364 != 0) {
4365 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004366 index,
4367 ((struct usb_config_descriptor *) buf)->
4368 bConfigurationValue);
4369 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 break;
4371 }
4372 }
Alan Sterneb764c42008-03-03 15:16:04 -05004373
4374 if (!changed && serial_len) {
4375 length = usb_string(udev, udev->descriptor.iSerialNumber,
4376 buf, serial_len);
4377 if (length + 1 != serial_len) {
4378 dev_dbg(&udev->dev, "serial string error %d\n",
4379 length);
4380 changed = 1;
4381 } else if (memcmp(buf, udev->serial, length) != 0) {
4382 dev_dbg(&udev->dev, "serial string changed\n");
4383 changed = 1;
4384 }
4385 }
4386
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004388 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389}
4390
4391/**
Ming Lei742120c2008-06-18 22:00:29 +08004392 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4394 *
Alan Stern79efa092006-06-01 13:33:42 -04004395 * WARNING - don't use this routine to reset a composite device
4396 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004397 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 *
4399 * Do a port reset, reassign the device's address, and establish its
4400 * former operating configuration. If the reset fails, or the device's
4401 * descriptors change from their values before the reset, or the original
4402 * configuration and altsettings cannot be restored, a flag will be set
4403 * telling khubd to pretend the device has been disconnected and then
4404 * re-connected. All drivers will be unbound, and the device will be
4405 * re-enumerated and probed all over again.
4406 *
4407 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4408 * flagged for logical disconnection, or some other negative error code
4409 * if the reset wasn't even attempted.
4410 *
4411 * The caller must own the device lock. For example, it's safe to use
4412 * this from a driver probe() routine after downloading new firmware.
4413 * For calls that might not occur during probe(), drivers should lock
4414 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004415 *
4416 * Locking exception: This routine may also be called from within an
4417 * autoresume handler. Such usage won't conflict with other tasks
4418 * holding the device lock because these tasks should always call
4419 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 */
Ming Lei742120c2008-06-18 22:00:29 +08004421static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422{
4423 struct usb_device *parent_hdev = udev->parent;
4424 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004425 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004427 int i, ret = 0;
4428 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
4430 if (udev->state == USB_STATE_NOTATTACHED ||
4431 udev->state == USB_STATE_SUSPENDED) {
4432 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4433 udev->state);
4434 return -EINVAL;
4435 }
4436
4437 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004438 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004439 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 return -EISDIR;
4441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 parent_hub = hdev_to_hub(parent_hdev);
4443
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 set_bit(port1, parent_hub->busy_bits);
4445 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4446
4447 /* ep0 maxpacket size may change; let the HCD know about it.
4448 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004449 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004451 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 break;
4453 }
4454 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 if (ret < 0)
4457 goto re_enumerate;
4458
4459 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004460 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 dev_info(&udev->dev, "device firmware changed\n");
4462 udev->descriptor = descriptor; /* for disconnect() calls */
4463 goto re_enumerate;
4464 }
Alan Stern4fe03872009-02-26 10:21:02 -05004465
4466 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 if (!udev->actconfig)
4468 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004469
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004470 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004471 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4472 if (ret < 0) {
4473 dev_warn(&udev->dev,
4474 "Busted HC? Not enough HCD resources for "
4475 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004476 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004477 goto re_enumerate;
4478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4480 USB_REQ_SET_CONFIGURATION, 0,
4481 udev->actconfig->desc.bConfigurationValue, 0,
4482 NULL, 0, USB_CTRL_SET_TIMEOUT);
4483 if (ret < 0) {
4484 dev_err(&udev->dev,
4485 "can't restore configuration #%d (error=%d)\n",
4486 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004487 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 goto re_enumerate;
4489 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004490 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4492
Alan Stern4fe03872009-02-26 10:21:02 -05004493 /* Put interfaces back into the same altsettings as before.
4494 * Don't bother to send the Set-Interface request for interfaces
4495 * that were already in altsetting 0; besides being unnecessary,
4496 * many devices can't handle it. Instead just reset the host-side
4497 * endpoint state.
4498 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004500 struct usb_host_config *config = udev->actconfig;
4501 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 struct usb_interface_descriptor *desc;
4503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004505 if (desc->bAlternateSetting == 0) {
4506 usb_disable_interface(udev, intf, true);
4507 usb_enable_interface(udev, intf, true);
4508 ret = 0;
4509 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004510 /* Let the bandwidth allocation function know that this
4511 * device has been reset, and it will have to use
4512 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004513 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004514 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004515 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4516 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004517 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 if (ret < 0) {
4520 dev_err(&udev->dev, "failed to restore interface %d "
4521 "altsetting %d (error=%d)\n",
4522 desc->bInterfaceNumber,
4523 desc->bAlternateSetting,
4524 ret);
4525 goto re_enumerate;
4526 }
4527 }
4528
4529done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return 0;
4531
4532re_enumerate:
4533 hub_port_logical_disconnect(parent_hub, port1);
4534 return -ENODEV;
4535}
Alan Stern79efa092006-06-01 13:33:42 -04004536
4537/**
Ming Lei742120c2008-06-18 22:00:29 +08004538 * usb_reset_device - warn interface drivers and perform a USB port reset
Alan Stern79efa092006-06-01 13:33:42 -04004539 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
Alan Stern79efa092006-06-01 13:33:42 -04004540 *
4541 * Warns all drivers bound to registered interfaces (using their pre_reset
4542 * method), performs the port reset, and then lets the drivers know that
4543 * the reset is over (using their post_reset method).
4544 *
Ming Lei742120c2008-06-18 22:00:29 +08004545 * Return value is the same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04004546 *
4547 * The caller must own the device lock. For example, it's safe to use
4548 * this from a driver probe() routine after downloading new firmware.
4549 * For calls that might not occur during probe(), drivers should lock
4550 * the device using usb_lock_device_for_reset().
Alan Stern78d9a482008-06-23 16:00:40 -04004551 *
4552 * If an interface is currently being probed or disconnected, we assume
4553 * its driver knows how to handle resets. For all other interfaces,
4554 * if the driver doesn't have pre_reset and post_reset methods then
4555 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004556 */
Ming Lei742120c2008-06-18 22:00:29 +08004557int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004558{
4559 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004560 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004561 struct usb_host_config *config = udev->actconfig;
4562
4563 if (udev->state == USB_STATE_NOTATTACHED ||
4564 udev->state == USB_STATE_SUSPENDED) {
4565 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4566 udev->state);
4567 return -EINVAL;
4568 }
4569
Alan Stern645daaa2006-08-30 15:47:02 -04004570 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004571 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004572
Alan Stern79efa092006-06-01 13:33:42 -04004573 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004574 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004575 struct usb_interface *cintf = config->interface[i];
4576 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004577 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004578
4579 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004580 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004581 if (drv->pre_reset && drv->post_reset)
4582 unbind = (drv->pre_reset)(cintf);
4583 else if (cintf->condition ==
4584 USB_INTERFACE_BOUND)
4585 unbind = 1;
4586 if (unbind)
4587 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004588 }
4589 }
4590 }
4591
Ming Lei742120c2008-06-18 22:00:29 +08004592 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004593
4594 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004595 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004596 struct usb_interface *cintf = config->interface[i];
4597 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004598 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004599
Alan Stern78d9a482008-06-23 16:00:40 -04004600 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004601 drv = to_usb_driver(cintf->dev.driver);
4602 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004603 rebind = (drv->post_reset)(cintf);
4604 else if (cintf->condition ==
4605 USB_INTERFACE_BOUND)
4606 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04004607 }
Alan Stern6c640942008-10-21 15:40:03 -04004608 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04004609 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004610 }
4611 }
4612
Alan Stern94fcda12006-11-20 11:38:46 -05004613 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004614 return ret;
4615}
Ming Lei742120c2008-06-18 22:00:29 +08004616EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08004617
4618
4619/**
4620 * usb_queue_reset_device - Reset a USB device from an atomic context
4621 * @iface: USB interface belonging to the device to reset
4622 *
4623 * This function can be used to reset a USB device from an atomic
4624 * context, where usb_reset_device() won't work (as it blocks).
4625 *
4626 * Doing a reset via this method is functionally equivalent to calling
4627 * usb_reset_device(), except for the fact that it is delayed to a
4628 * workqueue. This means that any drivers bound to other interfaces
4629 * might be unbound, as well as users from usbfs in user space.
4630 *
4631 * Corner cases:
4632 *
4633 * - Scheduling two resets at the same time from two different drivers
4634 * attached to two different interfaces of the same device is
4635 * possible; depending on how the driver attached to each interface
4636 * handles ->pre_reset(), the second reset might happen or not.
4637 *
4638 * - If a driver is unbound and it had a pending reset, the reset will
4639 * be cancelled.
4640 *
4641 * - This function can be called during .probe() or .disconnect()
4642 * times. On return from .disconnect(), any pending resets will be
4643 * cancelled.
4644 *
4645 * There is no no need to lock/unlock the @reset_ws as schedule_work()
4646 * does its own.
4647 *
4648 * NOTE: We don't do any reference count tracking because it is not
4649 * needed. The lifecycle of the work_struct is tied to the
4650 * usb_interface. Before destroying the interface we cancel the
4651 * work_struct, so the fact that work_struct is queued and or
4652 * running means the interface (and thus, the device) exist and
4653 * are referenced.
4654 */
4655void usb_queue_reset_device(struct usb_interface *iface)
4656{
4657 schedule_work(&iface->reset_ws);
4658}
4659EXPORT_SYMBOL_GPL(usb_queue_reset_device);