blob: 5dceb41ead3675c9e93833958731c297aac04bde [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;
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04002813 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern9bbdf1e2010-01-08 12:57:28 -05002814 status = usb_autoresume_device(udev);
2815 if (status == 0) {
2816 /* Let the drivers do their thing, then... */
2817 usb_autosuspend_device(udev);
2818 }
Alan Sternd25450c2006-11-20 11:14:30 -05002819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return status;
2821}
2822
Alan Sternd388dab2006-07-01 22:14:24 -04002823#else /* CONFIG_USB_SUSPEND */
2824
2825/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2826
Alan Stern65bfd292008-11-25 16:39:18 -05002827int usb_port_suspend(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04002828{
2829 return 0;
2830}
2831
Alan Sternb01b03f2008-04-28 11:06:11 -04002832/* However we may need to do a reset-resume */
2833
Alan Stern65bfd292008-11-25 16:39:18 -05002834int usb_port_resume(struct usb_device *udev, pm_message_t msg)
Alan Sternd388dab2006-07-01 22:14:24 -04002835{
Alan Sternb01b03f2008-04-28 11:06:11 -04002836 struct usb_hub *hub = hdev_to_hub(udev->parent);
2837 int port1 = udev->portnum;
2838 int status;
2839 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04002840
Alan Sternb01b03f2008-04-28 11:06:11 -04002841 status = hub_port_status(hub, port1, &portstatus, &portchange);
2842 status = check_port_resume_type(udev,
2843 hub, port1, status, portchange, portstatus);
2844
2845 if (status) {
2846 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2847 hub_port_logical_disconnect(hub, port1);
2848 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04002849 dev_dbg(&udev->dev, "reset-resume\n");
Ming Lei742120c2008-06-18 22:00:29 +08002850 status = usb_reset_and_verify_device(udev);
Alan Stern54515fe2007-05-30 15:38:58 -04002851 }
2852 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04002853}
2854
Alan Sternd388dab2006-07-01 22:14:24 -04002855#endif
2856
David Brownelldb690872005-09-13 19:56:33 -07002857static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 struct usb_hub *hub = usb_get_intfdata (intf);
2860 struct usb_device *hdev = hub->hdev;
2861 unsigned port1;
Sarah Sharp4296c702012-01-06 10:34:31 -08002862 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Alan Sterncbb33002011-06-15 16:29:16 -04002864 /* Warn if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
2866 struct usb_device *udev;
2867
2868 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04002869 if (udev && udev->can_submit) {
Alan Sterncbb33002011-06-15 16:29:16 -04002870 dev_warn(&intf->dev, "port %d nyet suspended\n", port1);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002871 if (PMSG_IS_AUTO(msg))
Alan Sterncbb33002011-06-15 16:29:16 -04002872 return -EBUSY;
David Brownellc9f89fa2005-09-13 19:57:04 -07002873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
Sarah Sharp4296c702012-01-06 10:34:31 -08002875 if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) {
2876 /* Enable hub to send remote wakeup for all ports. */
2877 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
2878 status = set_port_feature(hdev,
2879 port1 |
2880 USB_PORT_FEAT_REMOTE_WAKE_CONNECT |
2881 USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT |
2882 USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT,
2883 USB_PORT_FEAT_REMOTE_WAKE_MASK);
2884 }
2885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Harvey Harrison441b62c2008-03-03 16:08:34 -08002887 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05002888
David Brownellc9f89fa2005-09-13 19:57:04 -07002889 /* stop khubd and related activity */
Alan Stern43303542008-04-28 11:07:31 -04002890 hub_quiesce(hub, HUB_SUSPEND);
Alan Sternb6f6436d2007-05-04 11:51:54 -04002891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892}
2893
2894static int hub_resume(struct usb_interface *intf)
2895{
Alan Stern5e6effa2008-03-03 15:15:51 -05002896 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Alan Stern5e6effa2008-03-03 15:15:51 -05002898 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04002899 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return 0;
2901}
2902
Alan Sternb41a60e2007-05-30 15:39:33 -04002903static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04002904{
Alan Sternb41a60e2007-05-30 15:39:33 -04002905 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04002906
Alan Stern5e6effa2008-03-03 15:15:51 -05002907 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04002908 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04002909 return 0;
2910}
2911
Alan Stern54515fe2007-05-30 15:38:58 -04002912/**
2913 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
2914 * @rhdev: struct usb_device for the root hub
2915 *
2916 * The USB host controller driver calls this function when its root hub
2917 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05002918 * has been reset. The routine marks @rhdev as having lost power.
2919 * When the hub driver is resumed it will take notice and carry out
2920 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2921 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04002922 */
2923void usb_root_hub_lost_power(struct usb_device *rhdev)
2924{
2925 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
2926 rhdev->reset_resume = 1;
2927}
2928EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
2929
Alan Sternd388dab2006-07-01 22:14:24 -04002930#else /* CONFIG_PM */
2931
Alan Sternf07600c2007-05-30 15:38:16 -04002932#define hub_suspend NULL
2933#define hub_resume NULL
2934#define hub_reset_resume NULL
Alan Sternd388dab2006-07-01 22:14:24 -04002935#endif
2936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
2938/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
2939 *
2940 * Between connect detection and reset signaling there must be a delay
2941 * of 100ms at least for debounce and power-settling. The corresponding
2942 * timer shall restart whenever the downstream port detects a disconnect.
2943 *
2944 * Apparently there are some bluetooth and irda-dongles and a number of
2945 * low-speed devices for which this debounce period may last over a second.
2946 * Not covered by the spec - but easy to deal with.
2947 *
2948 * This implementation uses a 1500ms total debounce timeout; if the
2949 * connection isn't stable by then it returns -ETIMEDOUT. It checks
2950 * every 25ms for transient disconnects. When the port status has been
2951 * unchanged for 100ms it returns the port status.
2952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953static int hub_port_debounce(struct usb_hub *hub, int port1)
2954{
2955 int ret;
2956 int total_time, stable_time = 0;
2957 u16 portchange, portstatus;
2958 unsigned connection = 0xffff;
2959
2960 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
2961 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2962 if (ret < 0)
2963 return ret;
2964
2965 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
2966 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
2967 stable_time += HUB_DEBOUNCE_STEP;
2968 if (stable_time >= HUB_DEBOUNCE_STABLE)
2969 break;
2970 } else {
2971 stable_time = 0;
2972 connection = portstatus & USB_PORT_STAT_CONNECTION;
2973 }
2974
2975 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2976 clear_port_feature(hub->hdev, port1,
2977 USB_PORT_FEAT_C_CONNECTION);
2978 }
2979
2980 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
2981 break;
2982 msleep(HUB_DEBOUNCE_STEP);
2983 }
2984
2985 dev_dbg (hub->intfdev,
2986 "debounce: port %d: total %dms stable %dms status 0x%x\n",
2987 port1, total_time, stable_time, portstatus);
2988
2989 if (stable_time < HUB_DEBOUNCE_STABLE)
2990 return -ETIMEDOUT;
2991 return portstatus;
2992}
2993
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07002994void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Alan Sternddeac4e2009-01-15 17:03:33 -05002996 usb_disable_endpoint(udev, 0 + USB_DIR_IN, true);
2997 usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true);
Alan Stern2caf7fc2008-12-31 11:31:33 -05002998 usb_enable_endpoint(udev, &udev->ep0, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003000EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
3003#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
3004
Alan Stern4326ed02007-07-30 17:08:43 -04003005static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 int retval;
Sarah Sharpc6515272009-04-27 19:57:26 -07003008 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Sarah Sharpc6515272009-04-27 19:57:26 -07003010 /*
3011 * The host controller will choose the device address,
3012 * instead of the core having chosen it earlier
3013 */
3014 if (!hcd->driver->address_device && devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return -EINVAL;
3016 if (udev->state == USB_STATE_ADDRESS)
3017 return 0;
3018 if (udev->state != USB_STATE_DEFAULT)
3019 return -EINVAL;
Andiry Xuc8d4af82010-10-14 07:22:51 -07003020 if (hcd->driver->address_device)
Sarah Sharpc6515272009-04-27 19:57:26 -07003021 retval = hcd->driver->address_device(hcd, udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003022 else
Sarah Sharpc6515272009-04-27 19:57:26 -07003023 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
3024 USB_REQ_SET_ADDRESS, 0, devnum, 0,
3025 NULL, 0, USB_CTRL_SET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (retval == 0) {
Alan Stern3b29b682011-02-22 09:53:41 -05003027 update_devnum(udev, devnum);
David Vrabel4953d142008-04-08 13:24:46 -07003028 /* Device now using proper address. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003030 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 }
3032 return retval;
3033}
3034
3035/* Reset device, (re)assign address, get device descriptor.
3036 * Device connection must be stable, no more debouncing needed.
3037 * Returns device in USB_STATE_ADDRESS, except on error.
3038 *
3039 * If this is called for an already-existing device (as part of
Ming Lei742120c2008-06-18 22:00:29 +08003040 * usb_reset_and_verify_device), the caller must own the device lock. For a
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 * newly detected device that is not accessible through any global
3042 * pointers, it's not necessary to lock the device.
3043 */
3044static int
3045hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
3046 int retry_counter)
3047{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003048 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
3050 struct usb_device *hdev = hub->hdev;
Sarah Sharpe7b77172009-04-27 19:54:26 -07003051 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 int i, j, retval;
3053 unsigned delay = HUB_SHORT_RESET_TIME;
3054 enum usb_device_speed oldspeed = udev->speed;
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003055 const char *speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003056 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 /* root hub ports have a slightly longer reset period
3059 * (from USB 2.0 spec, section 7.1.7.5)
3060 */
3061 if (!hdev->parent) {
3062 delay = HUB_ROOT_RESET_TIME;
3063 if (port1 == hdev->bus->otg_port)
3064 hdev->bus->b_hnp_enable = 0;
3065 }
3066
3067 /* Some low speed devices have problems with the quick delay, so */
3068 /* be a bit pessimistic with those devices. RHbug #23670 */
3069 if (oldspeed == USB_SPEED_LOW)
3070 delay = HUB_LONG_RESET_TIME;
3071
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003072 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Luben Tuikov07194ab2011-02-11 11:33:10 -08003074 /* Reset the device; full speed may morph to high speed */
3075 /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */
Andiry Xu75d7cf72011-09-13 16:41:11 -07003076 retval = hub_port_reset(hub, port1, udev, delay, false);
Luben Tuikov07194ab2011-02-11 11:33:10 -08003077 if (retval < 0) /* error or disconnect */
3078 goto fail;
3079 /* success, speed is known */
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 retval = -ENODEV;
3082
3083 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
3084 dev_dbg(&udev->dev, "device reset changed speed!\n");
3085 goto fail;
3086 }
3087 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04003088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
3090 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003091 * For Wireless USB devices, ep0 max packet is always 512 (tho
3092 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 */
3094 switch (udev->speed) {
Sarah Sharp6b403b02009-04-27 19:54:10 -07003095 case USB_SPEED_SUPER:
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -08003096 case USB_SPEED_WIRELESS: /* fixed at 512 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003097 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512);
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 case USB_SPEED_HIGH: /* fixed at 64 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003100 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 break;
3102 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
3103 /* to determine the ep0 maxpacket size, try to read
3104 * the device descriptor to get bMaxPacketSize0 and
3105 * then correct our initial guess.
3106 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003107 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 break;
3109 case USB_SPEED_LOW: /* fixed at 8 */
Harvey Harrison551509d2009-02-11 14:11:36 -08003110 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 break;
3112 default:
3113 goto fail;
3114 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003115
3116 if (udev->speed == USB_SPEED_WIRELESS)
3117 speed = "variable speed Wireless";
3118 else
3119 speed = usb_speed_string(udev->speed);
3120
Sarah Sharpc6515272009-04-27 19:57:26 -07003121 if (udev->speed != USB_SPEED_SUPER)
3122 dev_info(&udev->dev,
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003123 "%s %s USB device number %d using %s\n",
3124 (udev->config) ? "reset" : "new", speed,
Alan Stern3b29b682011-02-22 09:53:41 -05003125 devnum, udev->bus->controller->driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 /* Set up TT records, if needed */
3128 if (hdev->tt) {
3129 udev->tt = hdev->tt;
3130 udev->ttport = hdev->ttport;
3131 } else if (udev->speed != USB_SPEED_HIGH
3132 && hdev->speed == USB_SPEED_HIGH) {
Alan Sternd199c962011-01-31 10:56:37 -05003133 if (!hub->tt.hub) {
3134 dev_err(&udev->dev, "parent hub has no TT\n");
3135 retval = -EINVAL;
3136 goto fail;
3137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 udev->tt = &hub->tt;
3139 udev->ttport = port1;
3140 }
3141
3142 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
3143 * Because device hardware and firmware is sometimes buggy in
3144 * this area, and this is how Linux has done it for ages.
3145 * Change it cautiously.
3146 *
3147 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
3148 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
3149 * so it may help with some non-standards-compliant devices.
3150 * Otherwise we start with SET_ADDRESS and then try to read the
3151 * first 8 bytes of the device descriptor to get the ep0 maxpacket
3152 * value.
3153 */
3154 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
Sarah Sharpc6515272009-04-27 19:57:26 -07003155 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct usb_device_descriptor *buf;
3157 int r = 0;
3158
3159#define GET_DESCRIPTOR_BUFSIZE 64
3160 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
3161 if (!buf) {
3162 retval = -ENOMEM;
3163 continue;
3164 }
3165
Alan Sternb89ee192007-05-11 10:19:04 -04003166 /* Retry on all errors; some devices are flakey.
3167 * 255 is for WUSB devices, we actually need to use
3168 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 */
3170 for (j = 0; j < 3; ++j) {
3171 buf->bMaxPacketSize0 = 0;
3172 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
3173 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
3174 USB_DT_DEVICE << 8, 0,
3175 buf, GET_DESCRIPTOR_BUFSIZE,
Jaroslav Kyselafd7c5192008-10-10 16:24:45 +02003176 initial_descriptor_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07003178 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (buf->bDescriptorType ==
3180 USB_DT_DEVICE) {
3181 r = 0;
3182 break;
3183 }
3184 /* FALL THROUGH */
3185 default:
3186 if (r == 0)
3187 r = -EPROTO;
3188 break;
3189 }
3190 if (r == 0)
3191 break;
3192 }
3193 udev->descriptor.bMaxPacketSize0 =
3194 buf->bMaxPacketSize0;
3195 kfree(buf);
3196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003197 /*
3198 * If it is a HSET Test device, we don't issue a
3199 * second reset which results in failure due to
3200 * speed change.
3201 */
3202 if (le16_to_cpu(buf->idVendor) != 0x1a0a) {
3203 retval = hub_port_reset(hub, port1, udev,
Steve Mucklef132c6c2012-06-06 18:30:57 -07003204 delay, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003205 if (retval < 0) /* error or disconnect */
3206 goto fail;
3207 if (oldspeed != udev->speed) {
3208 dev_dbg(&udev->dev,
3209 "device reset changed speed!\n");
3210 retval = -ENODEV;
3211 goto fail;
3212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 }
3214 if (r) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003215 dev_err(&udev->dev,
3216 "device descriptor read/64, error %d\n",
3217 r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 retval = -EMSGSIZE;
3219 continue;
3220 }
3221#undef GET_DESCRIPTOR_BUFSIZE
3222 }
3223
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003224 /*
3225 * If device is WUSB, we already assigned an
3226 * unauthorized address in the Connect Ack sequence;
3227 * authorization will assign the final address.
3228 */
Sarah Sharpc6515272009-04-27 19:57:26 -07003229 if (udev->wusb == 0) {
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003230 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
3231 retval = hub_set_address(udev, devnum);
3232 if (retval >= 0)
3233 break;
3234 msleep(200);
3235 }
3236 if (retval < 0) {
3237 dev_err(&udev->dev,
3238 "device not accepting address %d, error %d\n",
3239 devnum, retval);
3240 goto fail;
3241 }
Sarah Sharpc6515272009-04-27 19:57:26 -07003242 if (udev->speed == USB_SPEED_SUPER) {
3243 devnum = udev->devnum;
3244 dev_info(&udev->dev,
Alan Stern3b29b682011-02-22 09:53:41 -05003245 "%s SuperSpeed USB device number %d using %s\n",
Sarah Sharpc6515272009-04-27 19:57:26 -07003246 (udev->config) ? "reset" : "new",
Alan Stern3b29b682011-02-22 09:53:41 -05003247 devnum, udev->bus->controller->driver->name);
Sarah Sharpc6515272009-04-27 19:57:26 -07003248 }
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003249
3250 /* cope with hardware quirkiness:
3251 * - let SET_ADDRESS settle, some device hardware wants it
3252 * - read ep0 maxpacket even for high and low speed,
3253 */
3254 msleep(10);
Sarah Sharpc6515272009-04-27 19:57:26 -07003255 if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07003257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
3259 retval = usb_get_device_descriptor(udev, 8);
3260 if (retval < 8) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003261 dev_err(&udev->dev,
3262 "device descriptor read/8, error %d\n",
3263 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (retval >= 0)
3265 retval = -EMSGSIZE;
3266 } else {
3267 retval = 0;
3268 break;
3269 }
3270 }
3271 if (retval)
3272 goto fail;
3273
Elric Fud8aec3d2012-03-26 21:16:02 +08003274 /*
3275 * Some superspeed devices have finished the link training process
3276 * and attached to a superspeed hub port, but the device descriptor
3277 * got from those devices show they aren't superspeed devices. Warm
3278 * reset the port attached by the devices can fix them.
3279 */
3280 if ((udev->speed == USB_SPEED_SUPER) &&
3281 (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) {
3282 dev_err(&udev->dev, "got a wrong device descriptor, "
3283 "warm reset device\n");
3284 hub_port_reset(hub, port1, udev,
3285 HUB_BH_RESET_TIME, true);
3286 retval = -EINVAL;
3287 goto fail;
3288 }
3289
Sarah Sharp6b403b02009-04-27 19:54:10 -07003290 if (udev->descriptor.bMaxPacketSize0 == 0xff ||
3291 udev->speed == USB_SPEED_SUPER)
3292 i = 512;
3293 else
3294 i = udev->descriptor.bMaxPacketSize0;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003295 if (usb_endpoint_maxp(&udev->ep0.desc) != i) {
Alan Stern56626a72010-10-14 15:25:21 -04003296 if (udev->speed == USB_SPEED_LOW ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 !(i == 8 || i == 16 || i == 32 || i == 64)) {
Alan Stern56626a72010-10-14 15:25:21 -04003298 dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 retval = -EMSGSIZE;
3300 goto fail;
3301 }
Alan Stern56626a72010-10-14 15:25:21 -04003302 if (udev->speed == USB_SPEED_FULL)
3303 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
3304 else
3305 dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003307 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 }
3309
3310 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
3311 if (retval < (signed)sizeof(udev->descriptor)) {
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003312 dev_err(&udev->dev, "device descriptor read/all, error %d\n",
3313 retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 if (retval >= 0)
3315 retval = -ENOMSG;
3316 goto fail;
3317 }
3318
Andiry Xu1ff4df52011-09-23 14:19:48 -07003319 if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) {
3320 retval = usb_get_bos_descriptor(udev);
3321 if (!retval) {
3322 if (udev->bos->ext_cap && (USB_LPM_SUPPORT &
3323 le32_to_cpu(udev->bos->ext_cap->bmAttributes)))
3324 udev->lpm_capable = 1;
3325 }
3326 }
Andiry Xu3148bf02011-09-23 14:19:47 -07003327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 retval = 0;
Alek Du48f24972010-06-04 15:47:55 +08003329 /* notify HCD that we have a device connected and addressed */
3330 if (hcd->driver->update_device)
3331 hcd->driver->update_device(hcd, udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332fail:
Alan Stern4326ed02007-07-30 17:08:43 -04003333 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 hub_port_disable(hub, port1, 0);
Alan Stern3b29b682011-02-22 09:53:41 -05003335 update_devnum(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04003336 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01003337 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 return retval;
3339}
3340
3341static void
3342check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
3343{
3344 struct usb_qualifier_descriptor *qual;
3345 int status;
3346
Christoph Lametere94b1762006-12-06 20:33:17 -08003347 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (qual == NULL)
3349 return;
3350
3351 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
3352 qual, sizeof *qual);
3353 if (status == sizeof *qual) {
3354 dev_info(&udev->dev, "not running at top speed; "
3355 "connect to a high speed hub\n");
3356 /* hub LEDs are probably harder to miss than syslog */
3357 if (hub->has_indicators) {
3358 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003359 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07003362 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
3365static unsigned
3366hub_power_remaining (struct usb_hub *hub)
3367{
3368 struct usb_device *hdev = hub->hdev;
3369 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05003370 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Alan Stern55c52712005-11-23 12:03:12 -05003372 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 return 0;
3374
Alan Stern55c52712005-11-23 12:03:12 -05003375 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
3376 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
3377 struct usb_device *udev = hdev->children[port1 - 1];
3378 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
3380 if (!udev)
3381 continue;
3382
Alan Stern55c52712005-11-23 12:03:12 -05003383 /* Unconfigured devices may not use more than 100mA,
3384 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05003386 delta = udev->actconfig->desc.bMaxPower * 2;
3387 else if (port1 != udev->bus->otg_port || hdev->parent)
3388 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 else
Alan Stern55c52712005-11-23 12:03:12 -05003390 delta = 8;
3391 if (delta > hub->mA_per_port)
Wu Fengguangb9cef6c2008-12-15 15:32:01 +08003392 dev_warn(&udev->dev,
3393 "%dmA is over %umA budget for port %d!\n",
3394 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 remaining -= delta;
3396 }
3397 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05003398 dev_warn(hub->intfdev, "%dmA over power budget!\n",
3399 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 remaining = 0;
3401 }
3402 return remaining;
3403}
3404
3405/* Handle physical or logical connection change events.
3406 * This routine is called when:
3407 * a port connection-change occurs;
3408 * a port enable-change occurs (often caused by EMI);
Ming Lei742120c2008-06-18 22:00:29 +08003409 * usb_reset_and_verify_device() encounters changed descriptors (as from
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 * a firmware download)
3411 * caller already locked the hub
3412 */
3413static void hub_port_connect_change(struct usb_hub *hub, int port1,
3414 u16 portstatus, u16 portchange)
3415{
3416 struct usb_device *hdev = hub->hdev;
3417 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05303418 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04003419 unsigned wHubCharacteristics =
3420 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04003421 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04003423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 dev_dbg (hub_dev,
3425 "port %d, status %04x, change %04x, %s\n",
Sarah Sharp131dec32010-12-06 21:00:19 -08003426 port1, portstatus, portchange, portspeed(hub, portstatus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
3428 if (hub->has_indicators) {
3429 set_port_led(hub, port1, HUB_LED_AUTO);
3430 hub->indicator[port1-1] = INDICATOR_AUTO;
3431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
3433#ifdef CONFIG_USB_OTG
3434 /* during HNP, don't repeat the debounce */
3435 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04003436 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
3437 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438#endif
3439
Alan Stern8808f002008-04-28 11:06:55 -04003440 /* Try to resuscitate an existing device */
3441 udev = hdev->children[port1-1];
3442 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
3443 udev->state != USB_STATE_NOTATTACHED) {
Alan Stern8808f002008-04-28 11:06:55 -04003444 usb_lock_device(udev);
3445 if (portstatus & USB_PORT_STAT_ENABLE) {
3446 status = 0; /* Nothing to do */
Alan Stern8808f002008-04-28 11:06:55 -04003447
3448#ifdef CONFIG_USB_SUSPEND
Alan Stern5257d972008-09-22 14:43:08 -04003449 } else if (udev->state == USB_STATE_SUSPENDED &&
3450 udev->persist_enabled) {
Alan Stern8808f002008-04-28 11:06:55 -04003451 /* For a suspended device, treat this as a
3452 * remote wakeup event.
3453 */
Alan Stern0534d462010-01-08 12:56:30 -05003454 status = usb_remote_wakeup(udev);
Alan Stern8808f002008-04-28 11:06:55 -04003455#endif
3456
3457 } else {
Alan Stern5257d972008-09-22 14:43:08 -04003458 status = -ENODEV; /* Don't resuscitate */
Alan Stern8808f002008-04-28 11:06:55 -04003459 }
3460 usb_unlock_device(udev);
3461
3462 if (status == 0) {
3463 clear_bit(port1, hub->change_bits);
3464 return;
3465 }
3466 }
3467
Alan Stern24618b02008-04-28 11:06:28 -04003468 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04003469 if (udev)
Alan Stern24618b02008-04-28 11:06:28 -04003470 usb_disconnect(&hdev->children[port1-1]);
3471 clear_bit(port1, hub->change_bits);
3472
Alan Stern253e0572009-10-27 15:20:13 -04003473 /* We can forget about a "removed" device when there's a physical
3474 * disconnect or the connect status changes.
3475 */
3476 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3477 (portchange & USB_PORT_STAT_C_CONNECTION))
3478 clear_bit(port1, hub->removed_bits);
3479
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003480#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
3481 if (Unwanted_SecondReset == 0) /*stericsson*/
3482#endif
Alan Stern5257d972008-09-22 14:43:08 -04003483 if (portchange & (USB_PORT_STAT_C_CONNECTION |
3484 USB_PORT_STAT_C_ENABLE)) {
3485 status = hub_port_debounce(hub, port1);
3486 if (status < 0) {
3487 if (printk_ratelimit())
3488 dev_err(hub_dev, "connect-debounce failed, "
3489 "port %d disabled\n", port1);
3490 portstatus &= ~USB_PORT_STAT_CONNECTION;
3491 } else {
3492 portstatus = status;
3493 }
3494 }
3495
Alan Stern253e0572009-10-27 15:20:13 -04003496 /* Return now if debouncing failed or nothing is connected or
3497 * the device was "removed".
3498 */
3499 if (!(portstatus & USB_PORT_STAT_CONNECTION) ||
3500 test_bit(port1, hub->removed_bits)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07003503 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Andiry Xu0ed9a572011-04-27 18:07:43 +08003504 && !port_is_power_on(hub, portstatus))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
Alan Stern5257d972008-09-22 14:43:08 -04003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 if (portstatus & USB_PORT_STAT_ENABLE)
3508 goto done;
3509 return;
3510 }
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 /* reallocate for each attempt, since references
3515 * to the previous one can escape in various ways
3516 */
3517 udev = usb_alloc_dev(hdev, hdev->bus, port1);
3518 if (!udev) {
3519 dev_err (hub_dev,
3520 "couldn't allocate port %d usb_device\n",
3521 port1);
3522 goto done;
3523 }
3524
3525 usb_set_device_state(udev, USB_STATE_POWERED);
Alan Stern55c52712005-11-23 12:03:12 -05003526 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04003527 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07003528 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05003529
Sarah Sharp131dec32010-12-06 21:00:19 -08003530 /* Only USB 3.0 devices are connected to SuperSpeed hubs. */
3531 if (hub_is_superspeed(hub->hdev))
Sarah Sharpe7b77172009-04-27 19:54:26 -07003532 udev->speed = USB_SPEED_SUPER;
3533 else
3534 udev->speed = USB_SPEED_UNKNOWN;
3535
Alan Stern3b29b682011-02-22 09:53:41 -05003536 choose_devnum(udev);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003537 if (udev->devnum <= 0) {
3538 status = -ENOTCONN; /* Don't retry */
3539 goto loop;
Sarah Sharpc6515272009-04-27 19:57:26 -07003540 }
3541
Sarah Sharpe7b77172009-04-27 19:54:26 -07003542 /* reset (non-USB 3.0 devices) and get descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 status = hub_port_init(hub, udev, port1, i);
3544 if (status < 0)
3545 goto loop;
3546
Phil Dibowitz93362a82010-07-22 00:05:01 +02003547 usb_detect_quirks(udev);
3548 if (udev->quirks & USB_QUIRK_DELAY_INIT)
3549 msleep(1000);
3550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 /* consecutive bus-powered hubs aren't reliable; they can
3552 * violate the voltage drop budget. if the new child has
3553 * a "powered" LED, users should notice we didn't enable it
3554 * (without reading syslog), even without per-port LEDs
3555 * on the parent.
3556 */
3557 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05003558 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 u16 devstat;
3560
3561 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
3562 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05003563 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 dev_dbg(&udev->dev, "get status %d ?\n", status);
3565 goto loop_disable;
3566 }
Alan Stern55c52712005-11-23 12:03:12 -05003567 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
3569 dev_err(&udev->dev,
3570 "can't connect bus-powered hub "
3571 "to this port\n");
3572 if (hub->has_indicators) {
3573 hub->indicator[port1-1] =
3574 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00003575 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 }
3577 status = -ENOTCONN; /* Don't retry */
3578 goto loop_disable;
3579 }
3580 }
3581
3582 /* check for devices running slower than they could */
3583 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
3584 && udev->speed == USB_SPEED_FULL
3585 && highspeed_hubs != 0)
3586 check_highspeed (hub, udev, port1);
3587
3588 /* Store the parent's children[] pointer. At this point
3589 * udev becomes globally accessible, although presumably
3590 * no one will look at it until hdev is unlocked.
3591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 status = 0;
3593
3594 /* We mustn't add new devices if the parent hub has
3595 * been disconnected; we would race with the
3596 * recursively_mark_NOTATTACHED() routine.
3597 */
3598 spin_lock_irq(&device_state_lock);
3599 if (hdev->state == USB_STATE_NOTATTACHED)
3600 status = -ENOTCONN;
3601 else
3602 hdev->children[port1-1] = udev;
3603 spin_unlock_irq(&device_state_lock);
3604
3605 /* Run it through the hoops (find a driver, etc) */
3606 if (!status) {
3607 status = usb_new_device(udev);
3608 if (status) {
3609 spin_lock_irq(&device_state_lock);
3610 hdev->children[port1-1] = NULL;
3611 spin_unlock_irq(&device_state_lock);
3612 }
3613 }
3614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 if (status)
3616 goto loop_disable;
3617
3618 status = hub_power_remaining(hub);
3619 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05003620 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003621#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
3622 if (HostComplianceTest == 1 && udev->devnum > 1) {
3623 if (HostTest == 7) { /*SINGLE_STEP_GET_DEV_DESC */
3624 dev_info(hub_dev, "Testing "
3625 "SINGLE_STEP_GET_DEV_DESC\n");
3626 /* Test the Single Step Get Device Descriptor ,
3627 * take care it should not get status phase
3628 */
3629 No_Data_Phase = 1;
3630 No_Status_Phase = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003632 usb_get_device_descriptor(udev, 8);
3633 No_Data_Phase = 0;
3634 No_Status_Phase = 0;
3635 }
3636
3637 if (HostTest == 8) {
3638 dev_info(hub_dev, "Testing "
3639 "SINGLE_STEP_SET_FEATURE\n");
3640 /* Test Single Step Set Feature */
3641 No_Status_Phase = 1;
3642 usb_get_device_descriptor(udev, 8);
3643 No_Status_Phase = 0;
3644 }
3645 }
3646#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 return;
3648
3649loop_disable:
3650 hub_port_disable(hub, port1, 1);
3651loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003652 usb_ep0_reinit(udev);
Alan Stern3b29b682011-02-22 09:53:41 -05003653 release_devnum(udev);
Herbert Xuf7410ce2010-01-10 20:15:03 +11003654 hub_free_dev(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07003656 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 break;
3658 }
Alan Stern3a311552008-05-20 16:58:29 -04003659 if (hub->hdev->parent ||
3660 !hcd->driver->port_handed_over ||
3661 !(hcd->driver->port_handed_over)(hcd, port1))
3662 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
3663 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665done:
3666 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05303667 if (hcd->driver->relinquish_port && !hub->hdev->parent)
3668 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
3670
Sarah Sharp714b07b2012-01-24 13:53:18 -08003671/* Returns 1 if there was a remote wakeup and a connect status change. */
3672static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port,
Sarah Sharp72937e12012-01-24 11:46:50 -08003673 u16 portstatus, u16 portchange)
Sarah Sharp714b07b2012-01-24 13:53:18 -08003674{
3675 struct usb_device *hdev;
3676 struct usb_device *udev;
3677 int connect_change = 0;
3678 int ret;
3679
3680 hdev = hub->hdev;
Sarah Sharp714b07b2012-01-24 13:53:18 -08003681 udev = hdev->children[port-1];
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003682 if (!hub_is_superspeed(hdev)) {
3683 if (!(portchange & USB_PORT_STAT_C_SUSPEND))
3684 return 0;
3685 clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND);
3686 } else {
3687 if (!udev || udev->state != USB_STATE_SUSPENDED ||
Sarah Sharp72937e12012-01-24 11:46:50 -08003688 (portstatus & USB_PORT_STAT_LINK_STATE) !=
3689 USB_SS_PORT_LS_U0)
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003690 return 0;
3691 }
3692
Sarah Sharp714b07b2012-01-24 13:53:18 -08003693 if (udev) {
3694 /* TRSMRCY = 10 msec */
3695 msleep(10);
3696
3697 usb_lock_device(udev);
3698 ret = usb_remote_wakeup(udev);
3699 usb_unlock_device(udev);
3700 if (ret < 0)
3701 connect_change = 1;
3702 } else {
3703 ret = -ENODEV;
3704 hub_port_disable(hub, port, 1);
3705 }
3706 dev_dbg(hub->intfdev, "resume on port %d, status %d\n",
3707 port, ret);
3708 return connect_change;
3709}
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711static void hub_events(void)
3712{
3713 struct list_head *tmp;
3714 struct usb_device *hdev;
3715 struct usb_interface *intf;
3716 struct usb_hub *hub;
3717 struct device *hub_dev;
3718 u16 hubstatus;
3719 u16 hubchange;
3720 u16 portstatus;
3721 u16 portchange;
3722 int i, ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003723#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
3724 int j;
3725 int otgport = 0;
3726 struct usb_port_status port_status;
3727#endif
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003728 int connect_change, wakeup_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 /*
3731 * We restart the list every time to avoid a deadlock with
3732 * deleting hubs downstream from this one. This should be
3733 * safe since we delete the hub from the event list.
3734 * Not the most efficient, but avoids deadlocks.
3735 */
3736 while (1) {
3737
3738 /* Grab the first entry at the beginning of the list */
3739 spin_lock_irq(&hub_event_lock);
3740 if (list_empty(&hub_event_list)) {
3741 spin_unlock_irq(&hub_event_lock);
3742 break;
3743 }
3744
3745 tmp = hub_event_list.next;
3746 list_del_init(tmp);
3747
3748 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04003749 kref_get(&hub->kref);
3750 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
Alan Sterne8054852007-05-04 11:55:11 -04003752 hdev = hub->hdev;
3753 hub_dev = hub->intfdev;
3754 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05003755 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 hdev->state, hub->descriptor
3757 ? hub->descriptor->bNbrPorts
3758 : 0,
3759 /* NOTE: expects max 15 ports... */
3760 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05003761 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 /* Lock the device, then check to see if we were
3764 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04003765 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04003766 if (unlikely(hub->disconnected))
Alan Stern9bbdf1e2010-01-08 12:57:28 -05003767 goto loop_disconnected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
3769 /* If the hub has died, clean up after it */
3770 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04003771 hub->error = -ENODEV;
Alan Stern43303542008-04-28 11:07:31 -04003772 hub_quiesce(hub, HUB_DISCONNECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 goto loop;
3774 }
3775
Alan Stern40f122f2006-11-09 14:44:33 -05003776 /* Autoresume */
3777 ret = usb_autopm_get_interface(intf);
3778 if (ret) {
3779 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05003781 }
3782
3783 /* If this is an inactive hub, do nothing */
3784 if (hub->quiescing)
3785 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787 if (hub->error) {
3788 dev_dbg (hub_dev, "resetting for error %d\n",
3789 hub->error);
3790
Ming Lei742120c2008-06-18 22:00:29 +08003791 ret = usb_reset_device(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 if (ret) {
3793 dev_dbg (hub_dev,
3794 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05003795 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
3797
3798 hub->nerrors = 0;
3799 hub->error = 0;
3800 }
3801
3802 /* deal with port status changes */
3803 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003804#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
3805 struct usb_port_status portsts;
3806
3807 /*if we have something to do on
3808 * otg port
3809 * */
3810 if ((hdev->otgstate & USB_OTG_SUSPEND) ||
3811 (hdev->otgstate & USB_OTG_ENUMERATE) ||
3812 (hdev->otgstate & USB_OTG_DISCONNECT) ||
3813 (hdev->otgstate & USB_OTG_RESUME)) {
3814 otgport = 1;
3815 }
3816
3817
3818 if (hdev->otgstate & USB_OTG_RESUME) {
3819 ret = clear_port_feature(hdev, i,
3820 USB_PORT_FEAT_SUSPEND);
3821 if (ret < 0) {
3822 dev_err(hub_dev, "usb otg port Resume"
3823 " fails, %d\n", ret);
3824 }
3825 hdev->otgstate &= ~USB_OTG_RESUME;
3826 }
3827 if ((hdev->otgstate & USB_OTG_SUSPEND)
3828 && (hdev->children[0])) {
3829 hdev->otgstate &= ~USB_OTG_SUSPEND;
3830
3831 ret = set_port_feature(hdev, 1,
3832 USB_PORT_FEAT_SUSPEND);
3833 if (ret < 0) {
3834 dev_err(hub_dev, "usb otg port suspend"
3835 " fails, %d\n", ret);
3836 break;
3837 }
3838 msleep(1);
3839 ret = get_port_status(hdev, i, &portsts);
3840 if (ret < 0) {
3841 dev_err(hub_dev, "usb otg get port"
3842 " status fails, %d\n", ret);
3843 break;
3844 }
3845 portchange = le16_to_cpu(portsts.wPortChange);
3846 if (portchange & USB_PORT_STAT_C_SUSPEND) {
3847 clear_port_feature(hdev, i,
3848 USB_PORT_FEAT_C_SUSPEND);
3849 }
3850 break;
3851 }
3852
3853 if (hdev->otgstate & USB_OTG_REMOTEWAKEUP) {
3854
3855 for (j = 1; j <= hub->descriptor->bNbrPorts;
3856 j++) {
3857 if (hdev->children[j - 1]) {
3858 dev_dbg(hub_dev, "child"
3859 " found at port %d\n", j);
3860 ret = usb_control_msg(hdev->
3861 children[j - 1],
3862 usb_sndctrlpipe(hdev->
3863 children[j - 1],
3864 0),
3865 USB_REQ_SET_FEATURE,
3866 USB_RECIP_DEVICE,
3867 USB_DEVICE_REMOTE_WAKEUP,
3868 0, NULL,
3869 0,
3870 USB_CTRL_SET_TIMEOUT);
3871 if (ret < 0) {
3872 dev_err(hub_dev, "Port"
3873 " %d doesn't support"
3874 "remote wakeup\n", j);
3875 } else {
3876 dev_dbg(hub_dev, "Port"
3877 " %d supports"
3878 "remote wakeup\n", j);
3879 }
3880 ret = set_port_feature(hdev, j,
3881 USB_PORT_FEAT_SUSPEND);
3882 if (ret < 0) {
3883 dev_err(hub_dev, "Port"
3884 " %d NOT ABLE TO"
3885 " SUSPEND\n", j);
3886 } else {
3887 dev_dbg(hub_dev, "Port"
3888 " %d is ABLE TO"
3889 " SUSPEND\n", j);
3890 }
3891 }
3892 }
3893 ret = usb_control_msg(hdev,
3894 usb_sndctrlpipe(hdev, 0),
3895 USB_REQ_SET_FEATURE,
3896 USB_RECIP_DEVICE,
3897 USB_DEVICE_REMOTE_WAKEUP,
3898 0, NULL, 0,
3899 USB_CTRL_SET_TIMEOUT);
3900 if (ret < 0) {
3901 dev_err(hub_dev, "HUB doesn't support"
3902 " REMOTE WAKEUP\n");
3903 } else {
3904 dev_dbg(hub_dev, "HUB supports"
3905 " REMOTE WAKEUP\n");
3906 }
3907 ret = 0;
3908 msleep(10);
3909 if (hdev->parent == hdev->bus->root_hub) {
3910 if (hdev->hcd_suspend &&
3911 hdev->hcd_priv) {
3912 dev_dbg(hub_dev, "calling"
3913 " suspend after remote wakeup"
3914 " command is issued\n");
3915 hdev->hcd_suspend(hdev->
3916 hcd_priv);
3917 }
3918 if (hdev->otg_notif)
3919 hdev->otg_notif(hdev->otgpriv,
3920 PDC_POWERMANAGEMENT, 10);
3921 }
3922 }
3923
3924 if (hdev->otgstate & USB_OTG_WAKEUP_ALL) {
3925 (void) usb_control_msg(hdev,
3926 usb_sndctrlpipe(hdev, 0),
3927 USB_REQ_CLEAR_FEATURE,
3928 USB_RECIP_DEVICE,
3929 USB_DEVICE_REMOTE_WAKEUP,
3930 0, NULL, 0,
3931 USB_CTRL_SET_TIMEOUT);
3932 dev_dbg(hub_dev, "Hub CLEARED REMOTE WAKEUP\n");
3933 for (j = 1; j <= hub->descriptor->bNbrPorts;
3934 j++) {
3935 if (hdev->children[j - 1]) {
3936 dev_dbg(hub_dev, "PORT %d"
3937 " SUSPEND IS CLEARD\n", j);
3938 clear_port_feature(hdev, j,
3939 USB_PORT_FEAT_C_SUSPEND);
3940 msleep(50);
3941 (void) usb_control_msg(hdev->
3942 children[j - 1],
3943 usb_sndctrlpipe(
3944 hdev->children[j - 1],
3945 0),
3946 USB_REQ_CLEAR_FEATURE,
3947 USB_RECIP_DEVICE,
3948 USB_DEVICE_REMOTE_WAKEUP,
3949 0, NULL,
3950 0,
3951 USB_CTRL_SET_TIMEOUT);
3952 dev_dbg(hub_dev, "PORT %d "
3953 "REMOTE WAKEUP IS "
3954 "CLEARD\n", j);
3955 msleep(10);
3956 }
3957 }
3958
3959
3960 }
3961
3962
3963 /*
3964 * reset the state of otg device,
3965 * regardless of otg device
3966 */
3967 hdev->otgstate = 0;
3968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 if (test_bit(i, hub->busy_bits))
3970 continue;
3971 connect_change = test_bit(i, hub->change_bits);
Sarah Sharp72937e12012-01-24 11:46:50 -08003972 wakeup_change = test_and_clear_bit(i, hub->wakeup_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 if (!test_and_clear_bit(i, hub->event_bits) &&
Sarah Sharp4ee823b2011-11-14 18:00:01 -08003974 !connect_change && !wakeup_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 continue;
3976
3977 ret = hub_port_status(hub, i,
3978 &portstatus, &portchange);
3979 if (ret < 0)
3980 continue;
3981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 if (portchange & USB_PORT_STAT_C_CONNECTION) {
3983 clear_port_feature(hdev, i,
3984 USB_PORT_FEAT_C_CONNECTION);
3985 connect_change = 1;
3986 }
3987
3988 if (portchange & USB_PORT_STAT_C_ENABLE) {
3989 if (!connect_change)
3990 dev_dbg (hub_dev,
3991 "port %d enable change, "
3992 "status %08x\n",
3993 i, portstatus);
3994 clear_port_feature(hdev, i,
3995 USB_PORT_FEAT_C_ENABLE);
3996
3997 /*
3998 * EM interference sometimes causes badly
3999 * shielded USB devices to be shutdown by
4000 * the hub, this hack enables them again.
4001 * Works at least with mouse driver.
4002 */
4003 if (!(portstatus & USB_PORT_STAT_ENABLE)
4004 && !connect_change
4005 && hdev->children[i-1]) {
4006 dev_err (hub_dev,
4007 "port %i "
4008 "disabled by hub (EMI?), "
4009 "re-enabling...\n",
4010 i);
4011 connect_change = 1;
4012 }
4013 }
4014
Sarah Sharp72937e12012-01-24 11:46:50 -08004015 if (hub_handle_remote_wakeup(hub, i,
4016 portstatus, portchange))
Sarah Sharp714b07b2012-01-24 13:53:18 -08004017 connect_change = 1;
Alan Stern8808f002008-04-28 11:06:55 -04004018
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004020 u16 status = 0;
4021 u16 unused;
4022
4023 dev_dbg(hub_dev, "over-current change on port "
4024 "%d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 clear_port_feature(hdev, i,
4026 USB_PORT_FEAT_C_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004027 msleep(100); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004028 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004029 hub_port_status(hub, i, &status, &unused);
4030 if (status & USB_PORT_STAT_OVERCURRENT)
4031 dev_err(hub_dev, "over-current "
4032 "condition on port %d\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 }
4034
4035 if (portchange & USB_PORT_STAT_C_RESET) {
4036 dev_dbg (hub_dev,
4037 "reset change on port %d\n",
4038 i);
4039 clear_port_feature(hdev, i,
4040 USB_PORT_FEAT_C_RESET);
4041 }
Sarah Sharpc7061572010-12-06 15:08:20 -08004042 if ((portchange & USB_PORT_STAT_C_BH_RESET) &&
4043 hub_is_superspeed(hub->hdev)) {
4044 dev_dbg(hub_dev,
4045 "warm reset change on port %d\n",
4046 i);
4047 clear_port_feature(hdev, i,
4048 USB_PORT_FEAT_C_BH_PORT_RESET);
4049 }
John Youndbe79bb2001-09-17 00:00:00 -07004050 if (portchange & USB_PORT_STAT_C_LINK_STATE) {
4051 clear_port_feature(hub->hdev, i,
4052 USB_PORT_FEAT_C_PORT_LINK_STATE);
4053 }
4054 if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) {
4055 dev_warn(hub_dev,
4056 "config error on port %d\n",
4057 i);
4058 clear_port_feature(hub->hdev, i,
4059 USB_PORT_FEAT_C_PORT_CONFIG_ERROR);
4060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Andiry Xu5e467f62011-04-27 18:07:54 +08004062 /* Warm reset a USB3 protocol port if it's in
4063 * SS.Inactive state.
4064 */
4065 if (hub_is_superspeed(hub->hdev) &&
4066 (portstatus & USB_PORT_STAT_LINK_STATE)
4067 == USB_SS_PORT_LS_SS_INACTIVE) {
4068 dev_dbg(hub_dev, "warm reset port %d\n", i);
Andiry Xu75d7cf72011-09-13 16:41:11 -07004069 hub_port_reset(hub, i, NULL,
4070 HUB_BH_RESET_TIME, true);
Andiry Xu5e467f62011-04-27 18:07:54 +08004071 }
4072
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004073 if (connect_change) {
4074#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
4075 if (hdev->parent == hdev->bus->root_hub)
4076 if (hdev->otg_notif
4077 && (HostComplianceTest == 0))
4078 hdev->otg_notif(hdev->otgpriv,
4079 PDC_HOST_NOTIFY,
4080 5);
4081 portno = i;
4082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 hub_port_connect_change(hub, i,
4084 portstatus, portchange);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 } /* end for i */
4087
4088 /* deal with hub status changes */
4089 if (test_and_clear_bit(0, hub->event_bits) == 0)
4090 ; /* do nothing */
4091 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
4092 dev_err (hub_dev, "get_hub_status failed\n");
4093 else {
4094 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
4095 dev_dbg (hub_dev, "power change\n");
4096 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05004097 if (hubstatus & HUB_STATUS_LOCAL_POWER)
4098 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05004099 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08004100 else
4101 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 }
4103 if (hubchange & HUB_CHANGE_OVERCURRENT) {
Paul Bolle752d57a2011-03-11 18:03:52 +01004104 u16 status = 0;
4105 u16 unused;
4106
4107 dev_dbg(hub_dev, "over-current change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
Paul Bolle752d57a2011-03-11 18:03:52 +01004109 msleep(500); /* Cool down */
Alan Stern8520f382008-09-22 14:44:26 -04004110 hub_power_on(hub, true);
Paul Bolle752d57a2011-03-11 18:03:52 +01004111 hub_hub_status(hub, &status, &unused);
4112 if (status & HUB_STATUS_OVERCURRENT)
4113 dev_err(hub_dev, "over-current "
4114 "condition\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 }
4116 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004117#if defined(CONFIG_USB_PEHCI_HCD) || defined(CONFIG_USB_PEHCI_HCD_MODULE)
4118 /* if we have something on otg */
4119 if (otgport) {
4120 otgport = 0;
4121 /* notify otg controller about it */
4122 if (hdev->parent == hdev->bus->root_hub)
4123 if (hdev->otg_notif)
4124 hdev->otg_notif(hdev->otgpriv,
4125 PDC_HOST_NOTIFY, 0);
4126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004128 if (HostComplianceTest && hdev->devnum > 1) {
4129 /* TEST_SE0_NAK */
4130 if (HostTest == 1) {
4131 dev_info(hub_dev, "Testing for TEST_SE0_NAK\n");
4132 ret = clear_port_feature(hdev, portno,
4133 USB_PORT_FEAT_C_CONNECTION);
4134 ret = set_port_feature(hdev, portno,
4135 USB_PORT_FEAT_SUSPEND);
4136 ret = set_port_feature(hdev, portno | 0x300,
4137 USB_PORT_FEAT_TEST);
4138 ret = get_port_status(hdev, portno,
4139 &port_status);
4140 }
4141 /*TEST_J*/
4142 if (HostTest == 2) {
4143 dev_info(hub_dev, "Testing TEST_J\n");
4144 ret = clear_port_feature(hdev, portno,
4145 USB_PORT_FEAT_C_CONNECTION);
4146 ret = set_port_feature(hdev, portno,
4147 USB_PORT_FEAT_SUSPEND);
4148 ret = set_port_feature(hdev, portno | 0x100,
4149 USB_PORT_FEAT_TEST);
4150 ret = get_port_status(hdev, portno,
4151 &port_status);
4152 }
4153 if (HostTest == 3) {
4154 dev_info(hub_dev, "Testing TEST_K\n");
4155 ret = clear_port_feature(hdev, portno,
4156 USB_PORT_FEAT_C_CONNECTION);
4157 ret = set_port_feature(hdev, portno,
4158 USB_PORT_FEAT_SUSPEND);
4159 ret = set_port_feature(hdev, portno | 0x200,
4160 USB_PORT_FEAT_TEST);
4161 ret = get_port_status(hdev, portno,
4162 &port_status);
4163 }
4164 if (HostTest == 4) {
4165 dev_info(hub_dev, "Testing TEST_PACKET at Port"
4166 " %d\n", portno);
4167 ret = clear_port_feature(hdev, portno,
4168 USB_PORT_FEAT_C_CONNECTION);
4169 if (ret < 0)
4170 dev_err(hub_dev, "Clear port feature"
4171 " C_CONNECTION failed\n");
4172
4173 ret = set_port_feature(hdev, portno,
4174 USB_PORT_FEAT_SUSPEND);
4175 if (ret < 0)
4176 dev_err(hub_dev, "Clear port feature"
4177 " SUSPEND failed\n");
4178
4179 ret = set_port_feature(hdev, portno | 0x400,
4180 USB_PORT_FEAT_TEST);
4181 if (ret < 0)
4182 dev_err(hub_dev, "Clear port feature"
4183 " TEST failed\n");
4184
4185 ret = get_port_status(hdev, portno,
4186 &port_status);
4187 if (ret < 0)
4188 dev_err(hub_dev, "Get port status"
4189 " failed\n");
4190 }
4191 if (HostTest == 5) {
4192 dev_info(hub_dev, "Testing TEST_FORCE_ENBLE\n");
4193 ret = clear_port_feature(hdev, portno,
4194 USB_PORT_FEAT_C_CONNECTION);
4195 ret = set_port_feature(hdev, portno,
4196 USB_PORT_FEAT_SUSPEND);
4197 ret = set_port_feature(hdev, portno | 0x500,
4198 USB_PORT_FEAT_TEST);
4199 ret = get_port_status(hdev, portno,
4200 &port_status);
4201 }
4202 if (HostTest == 6) {
4203 dev_info(hub_dev, "Testing "
4204 "HS_HOST_PORT_SUSPEND_RESUME\n");
4205 ret = clear_port_feature(hdev, portno,
4206 USB_PORT_FEAT_C_CONNECTION);
4207 ret = set_port_feature(hdev, portno,
4208 USB_PORT_FEAT_SUSPEND);
4209 msleep(3000);
4210 ret = clear_port_feature(hdev, portno,
4211 USB_PORT_FEAT_SUSPEND);
4212 HostTest = 0;
4213 }
4214 }
4215#endif
Alan Stern8e4ceb32009-12-07 13:01:37 -05004216 loop_autopm:
4217 /* Balance the usb_autopm_get_interface() above */
4218 usb_autopm_put_interface_no_suspend(intf);
4219 loop:
4220 /* Balance the usb_autopm_get_interface_no_resume() in
4221 * kick_khubd() and allow autosuspend.
4222 */
4223 usb_autopm_put_interface(intf);
Alan Stern9bbdf1e2010-01-08 12:57:28 -05004224 loop_disconnected:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04004226 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
4228 } /* end while (1) */
4229}
4230
4231static int hub_thread(void *__unused)
4232{
Alan Stern3bb1af52008-03-03 15:15:36 -05004233 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
4234 * port handover. Otherwise it might see that a full-speed device
4235 * was gone before the EHCI controller had handed its port over to
4236 * the companion full-speed controller.
4237 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07004238 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05004239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 do {
4241 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07004242 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004243 !list_empty(&hub_event_list) ||
4244 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004245 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004247 pr_debug("%s: khubd exiting\n", usbcore_name);
4248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Németh Márton1e927d92010-01-10 15:34:53 +01004251static const struct usb_device_id hub_id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
4253 .bDeviceClass = USB_CLASS_HUB},
4254 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
4255 .bInterfaceClass = USB_CLASS_HUB},
4256 { } /* Terminating entry */
4257};
4258
4259MODULE_DEVICE_TABLE (usb, hub_id_table);
4260
4261static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 .name = "hub",
4263 .probe = hub_probe,
4264 .disconnect = hub_disconnect,
4265 .suspend = hub_suspend,
4266 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04004267 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04004268 .pre_reset = hub_pre_reset,
4269 .post_reset = hub_post_reset,
Andi Kleenc532b292010-06-01 23:04:41 +02004270 .unlocked_ioctl = hub_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05004272 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273};
4274
4275int usb_hub_init(void)
4276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 if (usb_register(&hub_driver) < 0) {
4278 printk(KERN_ERR "%s: can't register hub driver\n",
4279 usbcore_name);
4280 return -1;
4281 }
4282
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004283 khubd_task = kthread_run(hub_thread, NULL, "khubd");
4284 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
4287 /* Fall through if kernel_thread failed */
4288 usb_deregister(&hub_driver);
4289 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
4290
4291 return -1;
4292}
4293
4294void usb_hub_cleanup(void)
4295{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07004296 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
4298 /*
4299 * Hub resources are freed for us by usb_deregister. It calls
4300 * usb_driver_purge on every device which in turn calls that
4301 * devices disconnect function if it is using this driver.
4302 * The hub_disconnect function takes care of releasing the
4303 * individual hub resources. -greg
4304 */
4305 usb_deregister(&hub_driver);
4306} /* usb_hub_cleanup() */
4307
Alan Sterneb764c42008-03-03 15:16:04 -05004308static int descriptors_changed(struct usb_device *udev,
4309 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310{
Alan Sterneb764c42008-03-03 15:16:04 -05004311 int changed = 0;
4312 unsigned index;
4313 unsigned serial_len = 0;
4314 unsigned len;
4315 unsigned old_length;
4316 int length;
4317 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Alan Sterneb764c42008-03-03 15:16:04 -05004319 if (memcmp(&udev->descriptor, old_device_descriptor,
4320 sizeof(*old_device_descriptor)) != 0)
4321 return 1;
4322
4323 /* Since the idVendor, idProduct, and bcdDevice values in the
4324 * device descriptor haven't changed, we will assume the
4325 * Manufacturer and Product strings haven't changed either.
4326 * But the SerialNumber string could be different (e.g., a
4327 * different flash card of the same brand).
4328 */
4329 if (udev->serial)
4330 serial_len = strlen(udev->serial) + 1;
4331
4332 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004334 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
4335 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 }
Alan Sterneb764c42008-03-03 15:16:04 -05004337
Oliver Neukum0cc1a512008-01-10 10:31:48 +01004338 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 if (buf == NULL) {
4340 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
4341 /* assume the worst */
4342 return 1;
4343 }
4344 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05004345 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
4347 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05004348 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 dev_dbg(&udev->dev, "config index %d, error %d\n",
4350 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05004351 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 break;
4353 }
4354 if (memcmp (buf, udev->rawdescriptors[index], old_length)
4355 != 0) {
4356 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05004357 index,
4358 ((struct usb_config_descriptor *) buf)->
4359 bConfigurationValue);
4360 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 break;
4362 }
4363 }
Alan Sterneb764c42008-03-03 15:16:04 -05004364
4365 if (!changed && serial_len) {
4366 length = usb_string(udev, udev->descriptor.iSerialNumber,
4367 buf, serial_len);
4368 if (length + 1 != serial_len) {
4369 dev_dbg(&udev->dev, "serial string error %d\n",
4370 length);
4371 changed = 1;
4372 } else if (memcmp(buf, udev->serial, length) != 0) {
4373 dev_dbg(&udev->dev, "serial string changed\n");
4374 changed = 1;
4375 }
4376 }
4377
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05004379 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380}
4381
4382/**
Ming Lei742120c2008-06-18 22:00:29 +08004383 * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
4385 *
Alan Stern79efa092006-06-01 13:33:42 -04004386 * WARNING - don't use this routine to reset a composite device
4387 * (one with multiple interfaces owned by separate drivers)!
Ming Lei742120c2008-06-18 22:00:29 +08004388 * Use usb_reset_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 *
4390 * Do a port reset, reassign the device's address, and establish its
4391 * former operating configuration. If the reset fails, or the device's
4392 * descriptors change from their values before the reset, or the original
4393 * configuration and altsettings cannot be restored, a flag will be set
4394 * telling khubd to pretend the device has been disconnected and then
4395 * re-connected. All drivers will be unbound, and the device will be
4396 * re-enumerated and probed all over again.
4397 *
4398 * Returns 0 if the reset succeeded, -ENODEV if the device has been
4399 * flagged for logical disconnection, or some other negative error code
4400 * if the reset wasn't even attempted.
4401 *
4402 * The caller must own the device lock. For example, it's safe to use
4403 * this from a driver probe() routine after downloading new firmware.
4404 * For calls that might not occur during probe(), drivers should lock
4405 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04004406 *
4407 * Locking exception: This routine may also be called from within an
4408 * autoresume handler. Such usage won't conflict with other tasks
4409 * holding the device lock because these tasks should always call
4410 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 */
Ming Lei742120c2008-06-18 22:00:29 +08004412static int usb_reset_and_verify_device(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
4414 struct usb_device *parent_hdev = udev->parent;
4415 struct usb_hub *parent_hub;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004416 struct usb_hcd *hcd = bus_to_hcd(udev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05004418 int i, ret = 0;
4419 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421 if (udev->state == USB_STATE_NOTATTACHED ||
4422 udev->state == USB_STATE_SUSPENDED) {
4423 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4424 udev->state);
4425 return -EINVAL;
4426 }
4427
4428 if (!parent_hdev) {
Wolfram Sang4bec9912010-08-29 18:17:14 +02004429 /* this requires hcd-specific logic; see ohci_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08004430 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 return -EISDIR;
4432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 parent_hub = hdev_to_hub(parent_hdev);
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 set_bit(port1, parent_hub->busy_bits);
4436 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
4437
4438 /* ep0 maxpacket size may change; let the HCD know about it.
4439 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07004440 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04004442 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 break;
4444 }
4445 clear_bit(port1, parent_hub->busy_bits);
Alan Sternd5cbad42006-08-11 16:52:39 -04004446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 if (ret < 0)
4448 goto re_enumerate;
4449
4450 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05004451 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 dev_info(&udev->dev, "device firmware changed\n");
4453 udev->descriptor = descriptor; /* for disconnect() calls */
4454 goto re_enumerate;
4455 }
Alan Stern4fe03872009-02-26 10:21:02 -05004456
4457 /* Restore the device's previous configuration */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 if (!udev->actconfig)
4459 goto done;
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004460
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004461 mutex_lock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004462 ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL);
4463 if (ret < 0) {
4464 dev_warn(&udev->dev,
4465 "Busted HC? Not enough HCD resources for "
4466 "old configuration.\n");
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004467 mutex_unlock(hcd->bandwidth_mutex);
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004468 goto re_enumerate;
4469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
4471 USB_REQ_SET_CONFIGURATION, 0,
4472 udev->actconfig->desc.bConfigurationValue, 0,
4473 NULL, 0, USB_CTRL_SET_TIMEOUT);
4474 if (ret < 0) {
4475 dev_err(&udev->dev,
4476 "can't restore configuration #%d (error=%d)\n",
4477 udev->actconfig->desc.bConfigurationValue, ret);
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004478 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 goto re_enumerate;
4480 }
Sarah Sharpd673bfc2010-10-15 08:55:24 -07004481 mutex_unlock(hcd->bandwidth_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 usb_set_device_state(udev, USB_STATE_CONFIGURED);
4483
Alan Stern4fe03872009-02-26 10:21:02 -05004484 /* Put interfaces back into the same altsettings as before.
4485 * Don't bother to send the Set-Interface request for interfaces
4486 * that were already in altsetting 0; besides being unnecessary,
4487 * many devices can't handle it. Instead just reset the host-side
4488 * endpoint state.
4489 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004491 struct usb_host_config *config = udev->actconfig;
4492 struct usb_interface *intf = config->interface[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 struct usb_interface_descriptor *desc;
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 desc = &intf->cur_altsetting->desc;
Alan Stern4fe03872009-02-26 10:21:02 -05004496 if (desc->bAlternateSetting == 0) {
4497 usb_disable_interface(udev, intf, true);
4498 usb_enable_interface(udev, intf, true);
4499 ret = 0;
4500 } else {
Sarah Sharp04a723e2010-01-06 10:16:51 -08004501 /* Let the bandwidth allocation function know that this
4502 * device has been reset, and it will have to use
4503 * alternate setting 0 as the current alternate setting.
Sarah Sharp3f0479e2009-12-03 09:44:36 -08004504 */
Sarah Sharp04a723e2010-01-06 10:16:51 -08004505 intf->resetting_device = 1;
Alan Stern4fe03872009-02-26 10:21:02 -05004506 ret = usb_set_interface(udev, desc->bInterfaceNumber,
4507 desc->bAlternateSetting);
Sarah Sharp04a723e2010-01-06 10:16:51 -08004508 intf->resetting_device = 0;
Alan Stern4fe03872009-02-26 10:21:02 -05004509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 if (ret < 0) {
4511 dev_err(&udev->dev, "failed to restore interface %d "
4512 "altsetting %d (error=%d)\n",
4513 desc->bInterfaceNumber,
4514 desc->bAlternateSetting,
4515 ret);
4516 goto re_enumerate;
4517 }
4518 }
4519
4520done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 return 0;
4522
4523re_enumerate:
4524 hub_port_logical_disconnect(parent_hub, port1);
4525 return -ENODEV;
4526}
Alan Stern79efa092006-06-01 13:33:42 -04004527
4528/**
Ming Lei742120c2008-06-18 22:00:29 +08004529 * usb_reset_device - warn interface drivers and perform a USB port reset
Alan Stern79efa092006-06-01 13:33:42 -04004530 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
Alan Stern79efa092006-06-01 13:33:42 -04004531 *
4532 * Warns all drivers bound to registered interfaces (using their pre_reset
4533 * method), performs the port reset, and then lets the drivers know that
4534 * the reset is over (using their post_reset method).
4535 *
Ming Lei742120c2008-06-18 22:00:29 +08004536 * Return value is the same as for usb_reset_and_verify_device().
Alan Stern79efa092006-06-01 13:33:42 -04004537 *
4538 * The caller must own the device lock. For example, it's safe to use
4539 * this from a driver probe() routine after downloading new firmware.
4540 * For calls that might not occur during probe(), drivers should lock
4541 * the device using usb_lock_device_for_reset().
Alan Stern78d9a482008-06-23 16:00:40 -04004542 *
4543 * If an interface is currently being probed or disconnected, we assume
4544 * its driver knows how to handle resets. For all other interfaces,
4545 * if the driver doesn't have pre_reset and post_reset methods then
4546 * we attempt to unbind it and rebind afterward.
Alan Stern79efa092006-06-01 13:33:42 -04004547 */
Ming Lei742120c2008-06-18 22:00:29 +08004548int usb_reset_device(struct usb_device *udev)
Alan Stern79efa092006-06-01 13:33:42 -04004549{
4550 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05004551 int i;
Alan Stern79efa092006-06-01 13:33:42 -04004552 struct usb_host_config *config = udev->actconfig;
4553
4554 if (udev->state == USB_STATE_NOTATTACHED ||
4555 udev->state == USB_STATE_SUSPENDED) {
4556 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
4557 udev->state);
4558 return -EINVAL;
4559 }
4560
Alan Stern645daaa2006-08-30 15:47:02 -04004561 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05004562 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04004563
Alan Stern79efa092006-06-01 13:33:42 -04004564 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004565 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004566 struct usb_interface *cintf = config->interface[i];
4567 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004568 int unbind = 0;
Alan Stern852c4b42007-12-03 15:44:29 -05004569
4570 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004571 drv = to_usb_driver(cintf->dev.driver);
Alan Stern78d9a482008-06-23 16:00:40 -04004572 if (drv->pre_reset && drv->post_reset)
4573 unbind = (drv->pre_reset)(cintf);
4574 else if (cintf->condition ==
4575 USB_INTERFACE_BOUND)
4576 unbind = 1;
4577 if (unbind)
4578 usb_forced_unbind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004579 }
4580 }
4581 }
4582
Ming Lei742120c2008-06-18 22:00:29 +08004583 ret = usb_reset_and_verify_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004584
4585 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04004586 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05004587 struct usb_interface *cintf = config->interface[i];
4588 struct usb_driver *drv;
Alan Stern78d9a482008-06-23 16:00:40 -04004589 int rebind = cintf->needs_binding;
Alan Stern852c4b42007-12-03 15:44:29 -05004590
Alan Stern78d9a482008-06-23 16:00:40 -04004591 if (!rebind && cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04004592 drv = to_usb_driver(cintf->dev.driver);
4593 if (drv->post_reset)
Alan Stern78d9a482008-06-23 16:00:40 -04004594 rebind = (drv->post_reset)(cintf);
4595 else if (cintf->condition ==
4596 USB_INTERFACE_BOUND)
4597 rebind = 1;
Alan Stern79efa092006-06-01 13:33:42 -04004598 }
Alan Stern6c640942008-10-21 15:40:03 -04004599 if (ret == 0 && rebind)
Alan Stern78d9a482008-06-23 16:00:40 -04004600 usb_rebind_intf(cintf);
Alan Stern79efa092006-06-01 13:33:42 -04004601 }
4602 }
4603
Alan Stern94fcda12006-11-20 11:38:46 -05004604 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04004605 return ret;
4606}
Ming Lei742120c2008-06-18 22:00:29 +08004607EXPORT_SYMBOL_GPL(usb_reset_device);
Inaky Perez-Gonzalezdc023dc2008-11-13 10:31:35 -08004608
4609
4610/**
4611 * usb_queue_reset_device - Reset a USB device from an atomic context
4612 * @iface: USB interface belonging to the device to reset
4613 *
4614 * This function can be used to reset a USB device from an atomic
4615 * context, where usb_reset_device() won't work (as it blocks).
4616 *
4617 * Doing a reset via this method is functionally equivalent to calling
4618 * usb_reset_device(), except for the fact that it is delayed to a
4619 * workqueue. This means that any drivers bound to other interfaces
4620 * might be unbound, as well as users from usbfs in user space.
4621 *
4622 * Corner cases:
4623 *
4624 * - Scheduling two resets at the same time from two different drivers
4625 * attached to two different interfaces of the same device is
4626 * possible; depending on how the driver attached to each interface
4627 * handles ->pre_reset(), the second reset might happen or not.
4628 *
4629 * - If a driver is unbound and it had a pending reset, the reset will
4630 * be cancelled.
4631 *
4632 * - This function can be called during .probe() or .disconnect()
4633 * times. On return from .disconnect(), any pending resets will be
4634 * cancelled.
4635 *
4636 * There is no no need to lock/unlock the @reset_ws as schedule_work()
4637 * does its own.
4638 *
4639 * NOTE: We don't do any reference count tracking because it is not
4640 * needed. The lifecycle of the work_struct is tied to the
4641 * usb_interface. Before destroying the interface we cancel the
4642 * work_struct, so the fact that work_struct is queued and or
4643 * running means the interface (and thus, the device) exist and
4644 * are referenced.
4645 */
4646void usb_queue_reset_device(struct usb_interface *iface)
4647{
4648 schedule_work(&iface->reset_ws);
4649}
4650EXPORT_SYMBOL_GPL(usb_queue_reset_device);