blob: 4c629a036aeda86d77bb848ba63b59a0ecc210d4 [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>
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070022#include <linux/kthread.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010023#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080024#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/uaccess.h>
27#include <asm/byteorder.h>
28
29#include "usb.h"
30#include "hcd.h"
31#include "hub.h"
32
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -080033/* if we are in debug mode, always announce new devices */
34#ifdef DEBUG
35#ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES
36#define CONFIG_USB_ANNOUNCE_NEW_DEVICES
37#endif
38#endif
39
Alan Stern1bb5f662006-11-06 11:56:13 -050040struct usb_hub {
41 struct device *intfdev; /* the "interface" device */
42 struct usb_device *hdev;
Alan Sterne8054852007-05-04 11:55:11 -040043 struct kref kref;
Alan Stern1bb5f662006-11-06 11:56:13 -050044 struct urb *urb; /* for interrupt polling pipe */
45
46 /* buffer for urb ... with extra space in case of babble */
47 char (*buffer)[8];
48 dma_addr_t buffer_dma; /* DMA address for buffer */
49 union {
50 struct usb_hub_status hub;
51 struct usb_port_status port;
52 } *status; /* buffer for status reports */
Alan Sterndb90e7a2007-02-05 09:56:15 -050053 struct mutex status_mutex; /* for the status buffer */
Alan Stern1bb5f662006-11-06 11:56:13 -050054
55 int error; /* last reported error */
56 int nerrors; /* track consecutive errors */
57
58 struct list_head event_list; /* hubs w/data or errs ready */
59 unsigned long event_bits[1]; /* status change bitmask */
60 unsigned long change_bits[1]; /* ports with logical connect
61 status change */
62 unsigned long busy_bits[1]; /* ports being reset or
63 resumed */
64#if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */
65#error event_bits[] is too short!
66#endif
67
68 struct usb_hub_descriptor *descriptor; /* class descriptor */
69 struct usb_tt tt; /* Transaction Translator */
70
71 unsigned mA_per_port; /* current for each child */
72
73 unsigned limited_power:1;
74 unsigned quiescing:1;
Alan Sterne8054852007-05-04 11:55:11 -040075 unsigned disconnected:1;
Alan Stern1bb5f662006-11-06 11:56:13 -050076
77 unsigned has_indicators:1;
78 u8 indicator[USB_MAXCHILDREN];
David Howells6d5aefb2006-12-05 19:36:26 +000079 struct delayed_work leds;
Alan Stern1bb5f662006-11-06 11:56:13 -050080};
81
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Protect struct usb_device->state and ->children members
Alan Stern9ad3d6c2005-11-17 17:10:32 -050084 * Note: Both are also protected by ->dev.sem, except that ->state can
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */
86static DEFINE_SPINLOCK(device_state_lock);
87
88/* khubd's worklist and its lock */
89static DEFINE_SPINLOCK(hub_event_lock);
90static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
91
92/* Wakes up khubd */
93static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
94
akpm@osdl.org9c8d6172005-06-20 14:29:58 -070095static struct task_struct *khubd_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* cycle leds on hubs that aren't blinking for attention */
98static int blinkenlights = 0;
99module_param (blinkenlights, bool, S_IRUGO);
100MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs");
101
102/*
103 * As of 2.6.10 we introduce a new USB device initialization scheme which
104 * closely resembles the way Windows works. Hopefully it will be compatible
105 * with a wider range of devices than the old scheme. However some previously
106 * working devices may start giving rise to "device not accepting address"
107 * errors; if that happens the user can try the old scheme by adjusting the
108 * following module parameters.
109 *
110 * For maximum flexibility there are two boolean parameters to control the
111 * hub driver's behavior. On the first initialization attempt, if the
112 * "old_scheme_first" parameter is set then the old scheme will be used,
113 * otherwise the new scheme is used. If that fails and "use_both_schemes"
114 * is set, then the driver will make another attempt, using the other scheme.
115 */
116static int old_scheme_first = 0;
117module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR);
118MODULE_PARM_DESC(old_scheme_first,
119 "start with the old device initialization scheme");
120
121static int use_both_schemes = 1;
122module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR);
123MODULE_PARM_DESC(use_both_schemes,
124 "try the other device initialization scheme if the "
125 "first one fails");
126
Alan Stern32fe0192007-10-10 16:27:07 -0400127/* Mutual exclusion for EHCI CF initialization. This interferes with
128 * port reset on some companion controllers.
129 */
130DECLARE_RWSEM(ehci_cf_port_reset_rwsem);
131EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem);
132
Alan Stern948fea32008-04-28 11:07:07 -0400133#define HUB_DEBOUNCE_TIMEOUT 1500
134#define HUB_DEBOUNCE_STEP 25
135#define HUB_DEBOUNCE_STABLE 100
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Dan Williams404d5b12007-04-26 00:12:10 -0700138static inline char *portspeed(int portstatus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
141 return "480 Mb/s";
142 else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
143 return "1.5 Mb/s";
144 else
145 return "12 Mb/s";
146}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* Note that hdev or one of its children must be locked! */
149static inline struct usb_hub *hdev_to_hub(struct usb_device *hdev)
150{
151 return usb_get_intfdata(hdev->actconfig->interface[0]);
152}
153
154/* USB 2.0 spec Section 11.24.4.5 */
155static int get_hub_descriptor(struct usb_device *hdev, void *data, int size)
156{
157 int i, ret;
158
159 for (i = 0; i < 3; i++) {
160 ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
161 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
162 USB_DT_HUB << 8, 0, data, size,
163 USB_CTRL_GET_TIMEOUT);
164 if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2))
165 return ret;
166 }
167 return -EINVAL;
168}
169
170/*
171 * USB 2.0 spec Section 11.24.2.1
172 */
173static int clear_hub_feature(struct usb_device *hdev, int feature)
174{
175 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
176 USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000);
177}
178
179/*
180 * USB 2.0 spec Section 11.24.2.2
181 */
182static int clear_port_feature(struct usb_device *hdev, int port1, int feature)
183{
184 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
185 USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1,
186 NULL, 0, 1000);
187}
188
189/*
190 * USB 2.0 spec Section 11.24.2.13
191 */
192static int set_port_feature(struct usb_device *hdev, int port1, int feature)
193{
194 return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
195 USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1,
196 NULL, 0, 1000);
197}
198
199/*
200 * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7
201 * for info about using port indicators
202 */
203static void set_port_led(
204 struct usb_hub *hub,
205 int port1,
206 int selector
207)
208{
209 int status = set_port_feature(hub->hdev, (selector << 8) | port1,
210 USB_PORT_FEAT_INDICATOR);
211 if (status < 0)
212 dev_dbg (hub->intfdev,
213 "port %d indicator %s status %d\n",
214 port1,
215 ({ char *s; switch (selector) {
216 case HUB_LED_AMBER: s = "amber"; break;
217 case HUB_LED_GREEN: s = "green"; break;
218 case HUB_LED_OFF: s = "off"; break;
219 case HUB_LED_AUTO: s = "auto"; break;
220 default: s = "??"; break;
221 }; s; }),
222 status);
223}
224
225#define LED_CYCLE_PERIOD ((2*HZ)/3)
226
David Howellsc4028952006-11-22 14:57:56 +0000227static void led_work (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
David Howellsc4028952006-11-22 14:57:56 +0000229 struct usb_hub *hub =
230 container_of(work, struct usb_hub, leds.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct usb_device *hdev = hub->hdev;
232 unsigned i;
233 unsigned changed = 0;
234 int cursor = -1;
235
236 if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing)
237 return;
238
239 for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
240 unsigned selector, mode;
241
242 /* 30%-50% duty cycle */
243
244 switch (hub->indicator[i]) {
245 /* cycle marker */
246 case INDICATOR_CYCLE:
247 cursor = i;
248 selector = HUB_LED_AUTO;
249 mode = INDICATOR_AUTO;
250 break;
251 /* blinking green = sw attention */
252 case INDICATOR_GREEN_BLINK:
253 selector = HUB_LED_GREEN;
254 mode = INDICATOR_GREEN_BLINK_OFF;
255 break;
256 case INDICATOR_GREEN_BLINK_OFF:
257 selector = HUB_LED_OFF;
258 mode = INDICATOR_GREEN_BLINK;
259 break;
260 /* blinking amber = hw attention */
261 case INDICATOR_AMBER_BLINK:
262 selector = HUB_LED_AMBER;
263 mode = INDICATOR_AMBER_BLINK_OFF;
264 break;
265 case INDICATOR_AMBER_BLINK_OFF:
266 selector = HUB_LED_OFF;
267 mode = INDICATOR_AMBER_BLINK;
268 break;
269 /* blink green/amber = reserved */
270 case INDICATOR_ALT_BLINK:
271 selector = HUB_LED_GREEN;
272 mode = INDICATOR_ALT_BLINK_OFF;
273 break;
274 case INDICATOR_ALT_BLINK_OFF:
275 selector = HUB_LED_AMBER;
276 mode = INDICATOR_ALT_BLINK;
277 break;
278 default:
279 continue;
280 }
281 if (selector != HUB_LED_AUTO)
282 changed = 1;
283 set_port_led(hub, i + 1, selector);
284 hub->indicator[i] = mode;
285 }
286 if (!changed && blinkenlights) {
287 cursor++;
288 cursor %= hub->descriptor->bNbrPorts;
289 set_port_led(hub, cursor + 1, HUB_LED_GREEN);
290 hub->indicator[cursor] = INDICATOR_CYCLE;
291 changed++;
292 }
293 if (changed)
294 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
295}
296
297/* use a short timeout for hub/port status fetches */
298#define USB_STS_TIMEOUT 1000
299#define USB_STS_RETRIES 5
300
301/*
302 * USB 2.0 spec Section 11.24.2.6
303 */
304static int get_hub_status(struct usb_device *hdev,
305 struct usb_hub_status *data)
306{
307 int i, status = -ETIMEDOUT;
308
309 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
310 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
311 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
312 data, sizeof(*data), USB_STS_TIMEOUT);
313 }
314 return status;
315}
316
317/*
318 * USB 2.0 spec Section 11.24.2.7
319 */
320static int get_port_status(struct usb_device *hdev, int port1,
321 struct usb_port_status *data)
322{
323 int i, status = -ETIMEDOUT;
324
325 for (i = 0; i < USB_STS_RETRIES && status == -ETIMEDOUT; i++) {
326 status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
327 USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1,
328 data, sizeof(*data), USB_STS_TIMEOUT);
329 }
330 return status;
331}
332
Alan Stern3eb14912008-03-03 15:15:43 -0500333static int hub_port_status(struct usb_hub *hub, int port1,
334 u16 *status, u16 *change)
335{
336 int ret;
337
338 mutex_lock(&hub->status_mutex);
339 ret = get_port_status(hub->hdev, port1, &hub->status->port);
340 if (ret < 4) {
341 dev_err(hub->intfdev,
342 "%s failed (err = %d)\n", __func__, ret);
343 if (ret >= 0)
344 ret = -EIO;
345 } else {
346 *status = le16_to_cpu(hub->status->port.wPortStatus);
347 *change = le16_to_cpu(hub->status->port.wPortChange);
348 ret = 0;
349 }
350 mutex_unlock(&hub->status_mutex);
351 return ret;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354static void kick_khubd(struct usb_hub *hub)
355{
356 unsigned long flags;
357
Alan Stern40f122f2006-11-09 14:44:33 -0500358 /* Suppress autosuspend until khubd runs */
359 to_usb_interface(hub->intfdev)->pm_usage_cnt = 1;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_lock_irqsave(&hub_event_lock, flags);
Roel Kluin2e2c5ee2007-10-26 23:54:35 +0200362 if (!hub->disconnected && list_empty(&hub->event_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 list_add_tail(&hub->event_list, &hub_event_list);
364 wake_up(&khubd_wait);
365 }
366 spin_unlock_irqrestore(&hub_event_lock, flags);
367}
368
369void usb_kick_khubd(struct usb_device *hdev)
370{
Alan Sterne8054852007-05-04 11:55:11 -0400371 /* FIXME: What if hdev isn't bound to the hub driver? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 kick_khubd(hdev_to_hub(hdev));
373}
374
375
376/* completion function, fires on port status changes and various faults */
David Howells7d12e782006-10-05 14:55:46 +0100377static void hub_irq(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Tobias Klauserec17cf12006-09-13 21:38:41 +0200379 struct usb_hub *hub = urb->context;
Alan Sterne0152682007-08-24 15:42:52 -0400380 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int i;
382 unsigned long bits;
383
Alan Sterne0152682007-08-24 15:42:52 -0400384 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 case -ENOENT: /* synchronous unlink */
386 case -ECONNRESET: /* async unlink */
387 case -ESHUTDOWN: /* hardware going away */
388 return;
389
390 default: /* presumably an error */
391 /* Cause a hub reset after 10 consecutive errors */
Alan Sterne0152682007-08-24 15:42:52 -0400392 dev_dbg (hub->intfdev, "transfer --> %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if ((++hub->nerrors < 10) || hub->error)
394 goto resubmit;
Alan Sterne0152682007-08-24 15:42:52 -0400395 hub->error = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* FALL THROUGH */
Tobias Klauserec17cf12006-09-13 21:38:41 +0200397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* let khubd handle things */
399 case 0: /* we got data: port status changed */
400 bits = 0;
401 for (i = 0; i < urb->actual_length; ++i)
402 bits |= ((unsigned long) ((*hub->buffer)[i]))
403 << (i*8);
404 hub->event_bits[0] = bits;
405 break;
406 }
407
408 hub->nerrors = 0;
409
410 /* Something happened, let khubd figure it out */
411 kick_khubd(hub);
412
413resubmit:
414 if (hub->quiescing)
415 return;
416
417 if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
418 && status != -ENODEV && status != -EPERM)
419 dev_err (hub->intfdev, "resubmit --> %d\n", status);
420}
421
422/* USB 2.0 spec Section 11.24.2.3 */
423static inline int
424hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt)
425{
426 return usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0),
427 HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo,
428 tt, NULL, 0, 1000);
429}
430
431/*
432 * enumeration blocks khubd for a long time. we use keventd instead, since
433 * long blocking there is the exception, not the rule. accordingly, HCDs
434 * talking to TTs must queue control transfers (not just bulk and iso), so
435 * both can talk to the same hub concurrently.
436 */
David Howellsc4028952006-11-22 14:57:56 +0000437static void hub_tt_kevent (struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
David Howellsc4028952006-11-22 14:57:56 +0000439 struct usb_hub *hub =
440 container_of(work, struct usb_hub, tt.kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned long flags;
Mark Lord55e5fdf2007-05-14 19:48:02 -0400442 int limit = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 spin_lock_irqsave (&hub->tt.lock, flags);
Mark Lord55e5fdf2007-05-14 19:48:02 -0400445 while (--limit && !list_empty (&hub->tt.clear_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct list_head *temp;
447 struct usb_tt_clear *clear;
448 struct usb_device *hdev = hub->hdev;
449 int status;
450
451 temp = hub->tt.clear_list.next;
452 clear = list_entry (temp, struct usb_tt_clear, clear_list);
453 list_del (&clear->clear_list);
454
455 /* drop lock so HCD can concurrently report other TT errors */
456 spin_unlock_irqrestore (&hub->tt.lock, flags);
457 status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt);
458 spin_lock_irqsave (&hub->tt.lock, flags);
459
460 if (status)
461 dev_err (&hdev->dev,
462 "clear tt %d (%04x) error %d\n",
463 clear->tt, clear->devinfo, status);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -0700464 kfree(clear);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466 spin_unlock_irqrestore (&hub->tt.lock, flags);
467}
468
469/**
470 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
471 * @udev: the device whose split transaction failed
472 * @pipe: identifies the endpoint of the failed transaction
473 *
474 * High speed HCDs use this to tell the hub driver that some split control or
475 * bulk transaction failed in a way that requires clearing internal state of
476 * a transaction translator. This is normally detected (and reported) from
477 * interrupt context.
478 *
479 * It may not be possible for that hub to handle additional full (or low)
480 * speed transactions until that state is fully cleared out.
481 */
482void usb_hub_tt_clear_buffer (struct usb_device *udev, int pipe)
483{
484 struct usb_tt *tt = udev->tt;
485 unsigned long flags;
486 struct usb_tt_clear *clear;
487
488 /* we've got to cope with an arbitrary number of pending TT clears,
489 * since each TT has "at least two" buffers that can need it (and
490 * there can be many TTs per hub). even if they're uncommon.
491 */
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800492 if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n");
494 /* FIXME recover somehow ... RESET_TT? */
495 return;
496 }
497
498 /* info that CLEAR_TT_BUFFER needs */
499 clear->tt = tt->multi ? udev->ttport : 1;
500 clear->devinfo = usb_pipeendpoint (pipe);
501 clear->devinfo |= udev->devnum << 4;
502 clear->devinfo |= usb_pipecontrol (pipe)
503 ? (USB_ENDPOINT_XFER_CONTROL << 11)
504 : (USB_ENDPOINT_XFER_BULK << 11);
505 if (usb_pipein (pipe))
506 clear->devinfo |= 1 << 15;
507
508 /* tell keventd to clear state for this TT */
509 spin_lock_irqsave (&tt->lock, flags);
510 list_add_tail (&clear->clear_list, &tt->clear_list);
511 schedule_work (&tt->kevent);
512 spin_unlock_irqrestore (&tt->lock, flags);
513}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -0600514EXPORT_SYMBOL_GPL(usb_hub_tt_clear_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516static void hub_power_on(struct usb_hub *hub)
517{
518 int port1;
David Brownellb7896962005-08-31 10:41:44 -0700519 unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2;
Alan Stern4489a572006-04-27 15:54:22 -0400520 u16 wHubCharacteristics =
521 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Alan Stern4489a572006-04-27 15:54:22 -0400523 /* Enable power on each port. Some hubs have reserved values
524 * of LPSM (> 2) in their descriptors, even though they are
525 * USB 2.0 hubs. Some hubs do not implement port-power switching
526 * but only emulate it. In all cases, the ports won't work
527 * unless we send these messages to the hub.
528 */
529 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 dev_dbg(hub->intfdev, "enabling power on all ports\n");
Alan Stern4489a572006-04-27 15:54:22 -0400531 else
532 dev_dbg(hub->intfdev, "trying to enable port power on "
533 "non-switchable hub\n");
534 for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++)
535 set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
David Brownellb7896962005-08-31 10:41:44 -0700537 /* Wait at least 100 msec for power to become stable */
538 msleep(max(pgood_delay, (unsigned) 100));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Alan Stern02c399e2006-08-30 15:47:11 -0400541static void hub_quiesce(struct usb_hub *hub)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
David Brownell979d5192005-09-22 22:32:24 -0700543 /* (nonblocking) khubd and related activity won't re-trigger */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 hub->quiescing = 1;
David Brownell979d5192005-09-22 22:32:24 -0700545
David Brownell979d5192005-09-22 22:32:24 -0700546 /* (blocking) stop khubd and related activity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 usb_kill_urb(hub->urb);
548 if (hub->has_indicators)
Alan Sternd48bd972007-12-11 16:02:23 -0500549 cancel_delayed_work_sync(&hub->leds);
550 if (hub->tt.hub)
551 cancel_work_sync(&hub->tt.kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static int hub_hub_status(struct usb_hub *hub,
555 u16 *status, u16 *change)
556{
557 int ret;
558
Alan Sterndb90e7a2007-02-05 09:56:15 -0500559 mutex_lock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 ret = get_hub_status(hub->hdev, &hub->status->hub);
561 if (ret < 0)
562 dev_err (hub->intfdev,
Harvey Harrison441b62c2008-03-03 16:08:34 -0800563 "%s failed (err = %d)\n", __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 else {
565 *status = le16_to_cpu(hub->status->hub.wHubStatus);
566 *change = le16_to_cpu(hub->status->hub.wHubChange);
567 ret = 0;
568 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500569 mutex_unlock(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return ret;
571}
572
Alan Stern8b28c752005-08-10 17:04:13 -0400573static int hub_port_disable(struct usb_hub *hub, int port1, int set_state)
574{
575 struct usb_device *hdev = hub->hdev;
Alan Stern0458d5b2007-05-04 11:52:20 -0400576 int ret = 0;
Alan Stern8b28c752005-08-10 17:04:13 -0400577
Alan Stern0458d5b2007-05-04 11:52:20 -0400578 if (hdev->children[port1-1] && set_state)
Alan Stern8b28c752005-08-10 17:04:13 -0400579 usb_set_device_state(hdev->children[port1-1],
580 USB_STATE_NOTATTACHED);
Alan Stern0458d5b2007-05-04 11:52:20 -0400581 if (!hub->error)
582 ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
Alan Stern8b28c752005-08-10 17:04:13 -0400583 if (ret)
584 dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n",
Alan Stern0458d5b2007-05-04 11:52:20 -0400585 port1, ret);
Alan Stern8b28c752005-08-10 17:04:13 -0400586 return ret;
587}
588
Alan Stern0458d5b2007-05-04 11:52:20 -0400589/*
590 * Disable a port and mark a logical connnect-change event, so that some
591 * time later khubd will disconnect() any existing usb_device on the port
592 * and will re-enumerate if there actually is a device attached.
593 */
594static void hub_port_logical_disconnect(struct usb_hub *hub, int port1)
595{
596 dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1);
597 hub_port_disable(hub, port1, 1);
598
599 /* FIXME let caller ask to power down the port:
600 * - some devices won't enumerate without a VBUS power cycle
601 * - SRP saves power that way
602 * - ... new call, TBD ...
603 * That's easy if this hub can switch power per-port, and
604 * khubd reactivates the port later (timer, SRP, etc).
605 * Powerdown must be optional, because of reset/DFU.
606 */
607
608 set_bit(port1, hub->change_bits);
609 kick_khubd(hub);
610}
611
Alan Stern7d069b72005-11-18 12:06:34 -0500612/* caller has locked the hub device */
Alan Stern3eb14912008-03-03 15:15:43 -0500613static void hub_stop(struct usb_hub *hub)
Alan Stern7d069b72005-11-18 12:06:34 -0500614{
Alan Sternb41a60e2007-05-30 15:39:33 -0400615 struct usb_device *hdev = hub->hdev;
616 int i;
Alan Stern7d069b72005-11-18 12:06:34 -0500617
Alan Sternb41a60e2007-05-30 15:39:33 -0400618 /* Disconnect all the children */
619 for (i = 0; i < hdev->maxchild; ++i) {
620 if (hdev->children[i])
621 usb_disconnect(&hdev->children[i]);
622 }
Alan Stern7d069b72005-11-18 12:06:34 -0500623 hub_quiesce(hub);
Alan Stern3eb14912008-03-03 15:15:43 -0500624}
625
Alan Stern6ee0b272008-04-28 11:06:42 -0400626enum hub_activation_type {
627 HUB_INIT, HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME
628};
Alan Stern5e6effa2008-03-03 15:15:51 -0500629
Alan Sternf2835212008-04-28 11:07:17 -0400630static void hub_activate(struct usb_hub *hub, enum hub_activation_type type)
Alan Stern5e6effa2008-03-03 15:15:51 -0500631{
632 struct usb_device *hdev = hub->hdev;
633 int port1;
Alan Sternf2835212008-04-28 11:07:17 -0400634 int status;
Alan Stern948fea32008-04-28 11:07:07 -0400635 bool need_debounce_delay = false;
Alan Stern5e6effa2008-03-03 15:15:51 -0500636
Alan Sternf2835212008-04-28 11:07:17 -0400637 /* After a resume, port power should still be on.
638 * For any other type of activation, turn it on.
639 */
640 if (type != HUB_RESUME)
641 hub_power_on(hub);
642
Alan Stern6ee0b272008-04-28 11:06:42 -0400643 /* Check each port and set hub->change_bits to let khubd know
644 * which ports need attention.
Alan Stern5e6effa2008-03-03 15:15:51 -0500645 */
646 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
647 struct usb_device *udev = hdev->children[port1-1];
Alan Stern5e6effa2008-03-03 15:15:51 -0500648 u16 portstatus, portchange;
649
Alan Stern6ee0b272008-04-28 11:06:42 -0400650 portstatus = portchange = 0;
651 status = hub_port_status(hub, port1, &portstatus, &portchange);
652 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
653 dev_dbg(hub->intfdev,
654 "port %d: status %04x change %04x\n",
655 port1, portstatus, portchange);
Alan Stern5e6effa2008-03-03 15:15:51 -0500656
Alan Stern6ee0b272008-04-28 11:06:42 -0400657 /* After anything other than HUB_RESUME (i.e., initialization
658 * or any sort of reset), every port should be disabled.
659 * Unconnected ports should likewise be disabled (paranoia),
660 * and so should ports for which we have no usb_device.
Alan Stern5e6effa2008-03-03 15:15:51 -0500661 */
Alan Stern6ee0b272008-04-28 11:06:42 -0400662 if ((portstatus & USB_PORT_STAT_ENABLE) && (
663 type != HUB_RESUME ||
664 !(portstatus & USB_PORT_STAT_CONNECTION) ||
665 !udev ||
666 udev->state == USB_STATE_NOTATTACHED)) {
667 clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE);
668 portstatus &= ~USB_PORT_STAT_ENABLE;
669 }
670
Alan Stern948fea32008-04-28 11:07:07 -0400671 /* Clear status-change flags; we'll debounce later */
672 if (portchange & USB_PORT_STAT_C_CONNECTION) {
673 need_debounce_delay = true;
674 clear_port_feature(hub->hdev, port1,
675 USB_PORT_FEAT_C_CONNECTION);
676 }
677 if (portchange & USB_PORT_STAT_C_ENABLE) {
678 need_debounce_delay = true;
679 clear_port_feature(hub->hdev, port1,
680 USB_PORT_FEAT_C_ENABLE);
681 }
682
Alan Stern6ee0b272008-04-28 11:06:42 -0400683 if (!udev || udev->state == USB_STATE_NOTATTACHED) {
684 /* Tell khubd to disconnect the device or
685 * check for a new connection
686 */
687 if (udev || (portstatus & USB_PORT_STAT_CONNECTION))
688 set_bit(port1, hub->change_bits);
689
690 } else if (portstatus & USB_PORT_STAT_ENABLE) {
691 /* The power session apparently survived the resume.
692 * If there was an overcurrent or suspend change
693 * (i.e., remote wakeup request), have khubd
694 * take care of it.
695 */
696 if (portchange)
697 set_bit(port1, hub->change_bits);
698
699 } else if (udev->persist_enabled) {
Alan Stern6ee0b272008-04-28 11:06:42 -0400700#ifdef CONFIG_PM
Alan Stern5e6effa2008-03-03 15:15:51 -0500701 udev->reset_resume = 1;
Alan Stern6ee0b272008-04-28 11:06:42 -0400702#endif
Alan Stern8808f002008-04-28 11:06:55 -0400703 set_bit(port1, hub->change_bits);
704
Alan Stern6ee0b272008-04-28 11:06:42 -0400705 } else {
706 /* The power session is gone; tell khubd */
707 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
708 set_bit(port1, hub->change_bits);
Alan Stern5e6effa2008-03-03 15:15:51 -0500709 }
Alan Stern5e6effa2008-03-03 15:15:51 -0500710 }
711
Alan Stern948fea32008-04-28 11:07:07 -0400712 /* If no port-status-change flags were set, we don't need any
713 * debouncing. If flags were set we can try to debounce the
714 * ports all at once right now, instead of letting khubd do them
715 * one at a time later on.
716 *
717 * If any port-status changes do occur during this delay, khubd
718 * will see them later and handle them normally.
719 */
720 if (need_debounce_delay)
721 msleep(HUB_DEBOUNCE_STABLE);
Alan Sternf2835212008-04-28 11:07:17 -0400722
723 hub->quiescing = 0;
724
725 status = usb_submit_urb(hub->urb, GFP_NOIO);
726 if (status < 0)
727 dev_err(hub->intfdev, "activate --> %d\n", status);
728 if (hub->has_indicators && blinkenlights)
729 schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD);
730
731 /* Scan all ports that need attention */
732 kick_khubd(hub);
Alan Stern5e6effa2008-03-03 15:15:51 -0500733}
734
Alan Stern3eb14912008-03-03 15:15:43 -0500735/* caller has locked the hub device */
736static int hub_pre_reset(struct usb_interface *intf)
737{
738 struct usb_hub *hub = usb_get_intfdata(intf);
739
740 hub_stop(hub);
Alan Sternf07600c2007-05-30 15:38:16 -0400741 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -0500742}
743
744/* caller has locked the hub device */
Alan Sternf07600c2007-05-30 15:38:16 -0400745static int hub_post_reset(struct usb_interface *intf)
Alan Stern7d069b72005-11-18 12:06:34 -0500746{
Alan Stern7de18d82006-06-01 13:37:24 -0400747 struct usb_hub *hub = usb_get_intfdata(intf);
748
Alan Sternf2835212008-04-28 11:07:17 -0400749 hub_activate(hub, HUB_POST_RESET);
Alan Sternf07600c2007-05-30 15:38:16 -0400750 return 0;
Alan Stern7d069b72005-11-18 12:06:34 -0500751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static int hub_configure(struct usb_hub *hub,
754 struct usb_endpoint_descriptor *endpoint)
755{
756 struct usb_device *hdev = hub->hdev;
757 struct device *hub_dev = hub->intfdev;
758 u16 hubstatus, hubchange;
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -0700759 u16 wHubCharacteristics;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 unsigned int pipe;
761 int maxp, ret;
762 char *message;
763
764 hub->buffer = usb_buffer_alloc(hdev, sizeof(*hub->buffer), GFP_KERNEL,
765 &hub->buffer_dma);
766 if (!hub->buffer) {
767 message = "can't allocate hub irq buffer";
768 ret = -ENOMEM;
769 goto fail;
770 }
771
772 hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
773 if (!hub->status) {
774 message = "can't kmalloc hub status buffer";
775 ret = -ENOMEM;
776 goto fail;
777 }
Alan Sterndb90e7a2007-02-05 09:56:15 -0500778 mutex_init(&hub->status_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
781 if (!hub->descriptor) {
782 message = "can't kmalloc hub descriptor";
783 ret = -ENOMEM;
784 goto fail;
785 }
786
787 /* Request the entire hub descriptor.
788 * hub->descriptor can handle USB_MAXCHILDREN ports,
789 * but the hub can/will return fewer bytes here.
790 */
791 ret = get_hub_descriptor(hdev, hub->descriptor,
792 sizeof(*hub->descriptor));
793 if (ret < 0) {
794 message = "can't read hub descriptor";
795 goto fail;
796 } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
797 message = "hub has too many ports!";
798 ret = -ENODEV;
799 goto fail;
800 }
801
802 hdev->maxchild = hub->descriptor->bNbrPorts;
803 dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild,
804 (hdev->maxchild == 1) ? "" : "s");
805
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -0700806 wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -0700808 if (wHubCharacteristics & HUB_CHAR_COMPOUND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int i;
810 char portstr [USB_MAXCHILDREN + 1];
811
812 for (i = 0; i < hdev->maxchild; i++)
813 portstr[i] = hub->descriptor->DeviceRemovable
814 [((i + 1) / 8)] & (1 << ((i + 1) % 8))
815 ? 'F' : 'R';
816 portstr[hdev->maxchild] = 0;
817 dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
818 } else
819 dev_dbg(hub_dev, "standalone hub\n");
820
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -0700821 switch (wHubCharacteristics & HUB_CHAR_LPSM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 case 0x00:
823 dev_dbg(hub_dev, "ganged power switching\n");
824 break;
825 case 0x01:
826 dev_dbg(hub_dev, "individual port power switching\n");
827 break;
828 case 0x02:
829 case 0x03:
830 dev_dbg(hub_dev, "no power switching (usb 1.0)\n");
831 break;
832 }
833
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -0700834 switch (wHubCharacteristics & HUB_CHAR_OCPM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 case 0x00:
836 dev_dbg(hub_dev, "global over-current protection\n");
837 break;
838 case 0x08:
839 dev_dbg(hub_dev, "individual port over-current protection\n");
840 break;
841 case 0x10:
842 case 0x18:
843 dev_dbg(hub_dev, "no over-current protection\n");
844 break;
845 }
846
847 spin_lock_init (&hub->tt.lock);
848 INIT_LIST_HEAD (&hub->tt.clear_list);
David Howellsc4028952006-11-22 14:57:56 +0000849 INIT_WORK (&hub->tt.kevent, hub_tt_kevent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 switch (hdev->descriptor.bDeviceProtocol) {
851 case 0:
852 break;
853 case 1:
854 dev_dbg(hub_dev, "Single TT\n");
855 hub->tt.hub = hdev;
856 break;
857 case 2:
858 ret = usb_set_interface(hdev, 0, 1);
859 if (ret == 0) {
860 dev_dbg(hub_dev, "TT per port\n");
861 hub->tt.multi = 1;
862 } else
863 dev_err(hub_dev, "Using single TT (err %d)\n",
864 ret);
865 hub->tt.hub = hdev;
866 break;
867 default:
868 dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
869 hdev->descriptor.bDeviceProtocol);
870 break;
871 }
872
david-b@pacbell.nete09711a2005-08-13 18:41:04 -0700873 /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -0700874 switch (wHubCharacteristics & HUB_CHAR_TTTT) {
david-b@pacbell.nete09711a2005-08-13 18:41:04 -0700875 case HUB_TTTT_8_BITS:
876 if (hdev->descriptor.bDeviceProtocol != 0) {
877 hub->tt.think_time = 666;
878 dev_dbg(hub_dev, "TT requires at most %d "
879 "FS bit times (%d ns)\n",
880 8, hub->tt.think_time);
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -0700883 case HUB_TTTT_16_BITS:
884 hub->tt.think_time = 666 * 2;
885 dev_dbg(hub_dev, "TT requires at most %d "
886 "FS bit times (%d ns)\n",
887 16, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -0700889 case HUB_TTTT_24_BITS:
890 hub->tt.think_time = 666 * 3;
891 dev_dbg(hub_dev, "TT requires at most %d "
892 "FS bit times (%d ns)\n",
893 24, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 break;
david-b@pacbell.nete09711a2005-08-13 18:41:04 -0700895 case HUB_TTTT_32_BITS:
896 hub->tt.think_time = 666 * 4;
897 dev_dbg(hub_dev, "TT requires at most %d "
898 "FS bit times (%d ns)\n",
899 32, hub->tt.think_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 break;
901 }
902
903 /* probe() zeroes hub->indicator[] */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -0700904 if (wHubCharacteristics & HUB_CHAR_PORTIND) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 hub->has_indicators = 1;
906 dev_dbg(hub_dev, "Port indicators are supported\n");
907 }
908
909 dev_dbg(hub_dev, "power on to power good time: %dms\n",
910 hub->descriptor->bPwrOn2PwrGood * 2);
911
912 /* power budgeting mostly matters with bus-powered hubs,
913 * and battery-powered root hubs (may provide just 8 mA).
914 */
915 ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus);
Alan Stern55c52712005-11-23 12:03:12 -0500916 if (ret < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 message = "can't get hub status";
918 goto fail;
919 }
Alan Stern7d35b922005-04-25 11:18:32 -0400920 le16_to_cpus(&hubstatus);
921 if (hdev == hdev->bus->root_hub) {
Alan Stern55c52712005-11-23 12:03:12 -0500922 if (hdev->bus_mA == 0 || hdev->bus_mA >= 500)
923 hub->mA_per_port = 500;
924 else {
925 hub->mA_per_port = hdev->bus_mA;
926 hub->limited_power = 1;
927 }
Alan Stern7d35b922005-04-25 11:18:32 -0400928 } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
930 hub->descriptor->bHubContrCurrent);
Alan Stern55c52712005-11-23 12:03:12 -0500931 hub->limited_power = 1;
932 if (hdev->maxchild > 0) {
933 int remaining = hdev->bus_mA -
934 hub->descriptor->bHubContrCurrent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Alan Stern55c52712005-11-23 12:03:12 -0500936 if (remaining < hdev->maxchild * 100)
937 dev_warn(hub_dev,
938 "insufficient power available "
939 "to use all downstream ports\n");
940 hub->mA_per_port = 100; /* 7.2.1.1 */
941 }
942 } else { /* Self-powered external hub */
943 /* FIXME: What about battery-powered external hubs that
944 * provide less current per port? */
945 hub->mA_per_port = 500;
946 }
947 if (hub->mA_per_port < 500)
948 dev_dbg(hub_dev, "%umA bus power budget for each child\n",
949 hub->mA_per_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 ret = hub_hub_status(hub, &hubstatus, &hubchange);
952 if (ret < 0) {
953 message = "can't get hub status";
954 goto fail;
955 }
956
957 /* local power status reports aren't always correct */
958 if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER)
959 dev_dbg(hub_dev, "local power source is %s\n",
960 (hubstatus & HUB_STATUS_LOCAL_POWER)
961 ? "lost (inactive)" : "good");
962
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -0700963 if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 dev_dbg(hub_dev, "%sover-current condition exists\n",
965 (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
966
inaky@linux.intel.com88fafff2006-10-11 20:05:59 -0700967 /* set up the interrupt endpoint
968 * We use the EP's maxpacket size instead of (PORTS+1+7)/8
969 * bytes as USB2.0[11.12.3] says because some hubs are known
970 * to send more data (and thus cause overflow). For root hubs,
971 * maxpktsize is defined in hcd.c's fake endpoint descriptors
972 * to be big enough for at least USB_MAXCHILDREN ports. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress);
974 maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe));
975
976 if (maxp > sizeof(*hub->buffer))
977 maxp = sizeof(*hub->buffer);
978
979 hub->urb = usb_alloc_urb(0, GFP_KERNEL);
980 if (!hub->urb) {
981 message = "couldn't allocate interrupt urb";
982 ret = -ENOMEM;
983 goto fail;
984 }
985
986 usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq,
987 hub, endpoint->bInterval);
988 hub->urb->transfer_dma = hub->buffer_dma;
989 hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
990
991 /* maybe cycle the hub leds */
992 if (hub->has_indicators && blinkenlights)
993 hub->indicator [0] = INDICATOR_CYCLE;
994
Alan Sternf2835212008-04-28 11:07:17 -0400995 hub_activate(hub, HUB_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return 0;
997
998fail:
999 dev_err (hub_dev, "config failed, %s (err %d)\n",
1000 message, ret);
1001 /* hub_disconnect() frees urb and descriptor */
1002 return ret;
1003}
1004
Alan Sterne8054852007-05-04 11:55:11 -04001005static void hub_release(struct kref *kref)
1006{
1007 struct usb_hub *hub = container_of(kref, struct usb_hub, kref);
1008
1009 usb_put_intf(to_usb_interface(hub->intfdev));
1010 kfree(hub);
1011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013static unsigned highspeed_hubs;
1014
1015static void hub_disconnect(struct usb_interface *intf)
1016{
1017 struct usb_hub *hub = usb_get_intfdata (intf);
Alan Sterne8054852007-05-04 11:55:11 -04001018
1019 /* Take the hub off the event list and don't let it be added again */
1020 spin_lock_irq(&hub_event_lock);
1021 list_del_init(&hub->event_list);
1022 hub->disconnected = 1;
1023 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Alan Stern7de18d82006-06-01 13:37:24 -04001025 /* Disconnect all children and quiesce the hub */
1026 hub->error = 0;
Alan Stern3eb14912008-03-03 15:15:43 -05001027 hub_stop(hub);
Alan Stern7de18d82006-06-01 13:37:24 -04001028
Alan Stern8b28c752005-08-10 17:04:13 -04001029 usb_set_intfdata (intf, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Alan Sterne8054852007-05-04 11:55:11 -04001031 if (hub->hdev->speed == USB_SPEED_HIGH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 highspeed_hubs--;
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 usb_free_urb(hub->urb);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001035 kfree(hub->descriptor);
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07001036 kfree(hub->status);
Alan Sterne8054852007-05-04 11:55:11 -04001037 usb_buffer_free(hub->hdev, sizeof(*hub->buffer), hub->buffer,
1038 hub->buffer_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Alan Sterne8054852007-05-04 11:55:11 -04001040 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
1044{
1045 struct usb_host_interface *desc;
1046 struct usb_endpoint_descriptor *endpoint;
1047 struct usb_device *hdev;
1048 struct usb_hub *hub;
1049
1050 desc = intf->cur_altsetting;
1051 hdev = interface_to_usbdev(intf);
1052
David Brownell89ccbdc2006-04-02 10:18:09 -08001053#ifdef CONFIG_USB_OTG_BLACKLIST_HUB
1054 if (hdev->parent) {
1055 dev_warn(&intf->dev, "ignoring external hub\n");
1056 return -ENODEV;
1057 }
1058#endif
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /* Some hubs have a subclass of 1, which AFAICT according to the */
1061 /* specs is not defined, but it works */
1062 if ((desc->desc.bInterfaceSubClass != 0) &&
1063 (desc->desc.bInterfaceSubClass != 1)) {
1064descriptor_error:
1065 dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
1066 return -EIO;
1067 }
1068
1069 /* Multiple endpoints? What kind of mutant ninja-hub is this? */
1070 if (desc->desc.bNumEndpoints != 1)
1071 goto descriptor_error;
1072
1073 endpoint = &desc->endpoint[0].desc;
1074
Luiz Fernando N. Capitulinofbf81c22006-09-27 11:58:53 -07001075 /* If it's not an interrupt in endpoint, we'd better punt! */
1076 if (!usb_endpoint_is_int_in(endpoint))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto descriptor_error;
1078
1079 /* We found a hub */
1080 dev_info (&intf->dev, "USB hub found\n");
1081
Alan Stern0a1ef3b2005-10-24 15:38:24 -04001082 hub = kzalloc(sizeof(*hub), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (!hub) {
1084 dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n");
1085 return -ENOMEM;
1086 }
1087
Alan Sterne8054852007-05-04 11:55:11 -04001088 kref_init(&hub->kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 INIT_LIST_HEAD(&hub->event_list);
1090 hub->intfdev = &intf->dev;
1091 hub->hdev = hdev;
David Howellsc4028952006-11-22 14:57:56 +00001092 INIT_DELAYED_WORK(&hub->leds, led_work);
Alan Sterne8054852007-05-04 11:55:11 -04001093 usb_get_intf(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 usb_set_intfdata (intf, hub);
Alan Stern40f122f2006-11-09 14:44:33 -05001096 intf->needs_remote_wakeup = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 if (hdev->speed == USB_SPEED_HIGH)
1099 highspeed_hubs++;
1100
1101 if (hub_configure(hub, endpoint) >= 0)
1102 return 0;
1103
1104 hub_disconnect (intf);
1105 return -ENODEV;
1106}
1107
1108static int
1109hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
1110{
1111 struct usb_device *hdev = interface_to_usbdev (intf);
1112
1113 /* assert ifno == 0 (part of hub spec) */
1114 switch (code) {
1115 case USBDEVFS_HUB_PORTINFO: {
1116 struct usbdevfs_hub_portinfo *info = user_data;
1117 int i;
1118
1119 spin_lock_irq(&device_state_lock);
1120 if (hdev->devnum <= 0)
1121 info->nports = 0;
1122 else {
1123 info->nports = hdev->maxchild;
1124 for (i = 0; i < info->nports; i++) {
1125 if (hdev->children[i] == NULL)
1126 info->port[i] = 0;
1127 else
1128 info->port[i] =
1129 hdev->children[i]->devnum;
1130 }
1131 }
1132 spin_unlock_irq(&device_state_lock);
1133
1134 return info->nports + 1;
1135 }
1136
1137 default:
1138 return -ENOSYS;
1139 }
1140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143static void recursively_mark_NOTATTACHED(struct usb_device *udev)
1144{
1145 int i;
1146
1147 for (i = 0; i < udev->maxchild; ++i) {
1148 if (udev->children[i])
1149 recursively_mark_NOTATTACHED(udev->children[i]);
1150 }
Sarah Sharp15123002007-12-21 16:54:15 -08001151 if (udev->state == USB_STATE_SUSPENDED) {
Alan Sternee49fb52006-11-22 16:55:54 -05001152 udev->discon_suspended = 1;
Sarah Sharp15123002007-12-21 16:54:15 -08001153 udev->active_duration -= jiffies;
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 udev->state = USB_STATE_NOTATTACHED;
1156}
1157
1158/**
1159 * usb_set_device_state - change a device's current state (usbcore, hcds)
1160 * @udev: pointer to device whose state should be changed
1161 * @new_state: new state value to be stored
1162 *
1163 * udev->state is _not_ fully protected by the device lock. Although
1164 * most transitions are made only while holding the lock, the state can
1165 * can change to USB_STATE_NOTATTACHED at almost any time. This
1166 * is so that devices can be marked as disconnected as soon as possible,
1167 * without having to wait for any semaphores to be released. As a result,
1168 * all changes to any device's state must be protected by the
1169 * device_state_lock spinlock.
1170 *
1171 * Once a device has been added to the device tree, all changes to its state
1172 * should be made using this routine. The state should _not_ be set directly.
1173 *
1174 * If udev->state is already USB_STATE_NOTATTACHED then no change is made.
1175 * Otherwise udev->state is set to new_state, and if new_state is
1176 * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set
1177 * to USB_STATE_NOTATTACHED.
1178 */
1179void usb_set_device_state(struct usb_device *udev,
1180 enum usb_device_state new_state)
1181{
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&device_state_lock, flags);
1185 if (udev->state == USB_STATE_NOTATTACHED)
1186 ; /* do nothing */
David Brownellb94dc6b2005-09-12 19:39:39 -07001187 else if (new_state != USB_STATE_NOTATTACHED) {
David Brownellfb669cc2006-01-24 08:40:27 -08001188
1189 /* root hub wakeup capabilities are managed out-of-band
1190 * and may involve silicon errata ... ignore them here.
1191 */
1192 if (udev->parent) {
Alan Stern645daaa2006-08-30 15:47:02 -04001193 if (udev->state == USB_STATE_SUSPENDED
1194 || new_state == USB_STATE_SUSPENDED)
1195 ; /* No change to wakeup settings */
1196 else if (new_state == USB_STATE_CONFIGURED)
David Brownellfb669cc2006-01-24 08:40:27 -08001197 device_init_wakeup(&udev->dev,
1198 (udev->actconfig->desc.bmAttributes
1199 & USB_CONFIG_ATT_WAKEUP));
Alan Stern645daaa2006-08-30 15:47:02 -04001200 else
David Brownellfb669cc2006-01-24 08:40:27 -08001201 device_init_wakeup(&udev->dev, 0);
1202 }
Sarah Sharp15123002007-12-21 16:54:15 -08001203 if (udev->state == USB_STATE_SUSPENDED &&
1204 new_state != USB_STATE_SUSPENDED)
1205 udev->active_duration -= jiffies;
1206 else if (new_state == USB_STATE_SUSPENDED &&
1207 udev->state != USB_STATE_SUSPENDED)
1208 udev->active_duration += jiffies;
Alan Stern645daaa2006-08-30 15:47:02 -04001209 udev->state = new_state;
David Brownellb94dc6b2005-09-12 19:39:39 -07001210 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 recursively_mark_NOTATTACHED(udev);
1212 spin_unlock_irqrestore(&device_state_lock, flags);
1213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001215/*
1216 * WUSB devices are simple: they have no hubs behind, so the mapping
1217 * device <-> virtual port number becomes 1:1. Why? to simplify the
1218 * life of the device connection logic in
1219 * drivers/usb/wusbcore/devconnect.c. When we do the initial secret
1220 * handshake we need to assign a temporary address in the unauthorized
1221 * space. For simplicity we use the first virtual port number found to
1222 * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()]
1223 * and that becomes it's address [X < 128] or its unauthorized address
1224 * [X | 0x80].
1225 *
1226 * We add 1 as an offset to the one-based USB-stack port number
1227 * (zero-based wusb virtual port index) for two reasons: (a) dev addr
1228 * 0 is reserved by USB for default address; (b) Linux's USB stack
1229 * uses always #1 for the root hub of the controller. So USB stack's
1230 * port #1, which is wusb virtual-port #0 has address #2.
1231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232static void choose_address(struct usb_device *udev)
1233{
1234 int devnum;
1235 struct usb_bus *bus = udev->bus;
1236
1237 /* If khubd ever becomes multithreaded, this will need a lock */
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07001238 if (udev->wusb) {
1239 devnum = udev->portnum + 1;
1240 BUG_ON(test_bit(devnum, bus->devmap.devicemap));
1241 } else {
1242 /* Try to allocate the next devnum beginning at
1243 * bus->devnum_next. */
1244 devnum = find_next_zero_bit(bus->devmap.devicemap, 128,
1245 bus->devnum_next);
1246 if (devnum >= 128)
1247 devnum = find_next_zero_bit(bus->devmap.devicemap,
1248 128, 1);
1249 bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1);
1250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (devnum < 128) {
1252 set_bit(devnum, bus->devmap.devicemap);
1253 udev->devnum = devnum;
1254 }
1255}
1256
1257static void release_address(struct usb_device *udev)
1258{
1259 if (udev->devnum > 0) {
1260 clear_bit(udev->devnum, udev->bus->devmap.devicemap);
1261 udev->devnum = -1;
1262 }
1263}
1264
David Vrabel4953d142008-04-08 13:24:46 -07001265static void update_address(struct usb_device *udev, int devnum)
1266{
1267 /* The address for a WUSB device is managed by wusbcore. */
1268 if (!udev->wusb)
1269 udev->devnum = devnum;
1270}
1271
Alan Sternd5d4db72007-05-29 16:34:52 -04001272#ifdef CONFIG_USB_SUSPEND
1273
1274static void usb_stop_pm(struct usb_device *udev)
1275{
1276 /* Synchronize with the ksuspend thread to prevent any more
1277 * autosuspend requests from being submitted, and decrement
1278 * the parent's count of unsuspended children.
1279 */
1280 usb_pm_lock(udev);
1281 if (udev->parent && !udev->discon_suspended)
1282 usb_autosuspend_device(udev->parent);
1283 usb_pm_unlock(udev);
1284
1285 /* Stop any autosuspend requests already submitted */
1286 cancel_rearming_delayed_work(&udev->autosuspend);
1287}
1288
1289#else
1290
1291static inline void usb_stop_pm(struct usb_device *udev)
1292{ }
1293
1294#endif
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/**
1297 * usb_disconnect - disconnect a device (usbcore-internal)
1298 * @pdev: pointer to device being disconnected
1299 * Context: !in_interrupt ()
1300 *
1301 * Something got disconnected. Get rid of it and all of its children.
1302 *
1303 * If *pdev is a normal device then the parent hub must already be locked.
1304 * If *pdev is a root hub then this routine will acquire the
1305 * usb_bus_list_lock on behalf of the caller.
1306 *
1307 * Only hub drivers (including virtual root hub drivers for host
1308 * controllers) should ever call this.
1309 *
1310 * This call is synchronous, and may not be used in an interrupt context.
1311 */
1312void usb_disconnect(struct usb_device **pdev)
1313{
1314 struct usb_device *udev = *pdev;
1315 int i;
1316
1317 if (!udev) {
Harvey Harrison441b62c2008-03-03 16:08:34 -08001318 pr_debug ("%s nodev\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return;
1320 }
1321
1322 /* mark the device as inactive, so any further urb submissions for
1323 * this device (and any of its children) will fail immediately.
1324 * this quiesces everyting except pending urbs.
1325 */
1326 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 dev_info (&udev->dev, "USB disconnect, address %d\n", udev->devnum);
1328
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001329 usb_lock_device(udev);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* Free up all the children before we remove this device */
1332 for (i = 0; i < USB_MAXCHILDREN; i++) {
1333 if (udev->children[i])
1334 usb_disconnect(&udev->children[i]);
1335 }
1336
1337 /* deallocate hcd/hardware state ... nuking all pending urbs and
1338 * cleaning up all state associated with the current configuration
1339 * so that the hardware is now fully quiesced.
1340 */
Alan Stern782da722006-07-01 22:09:35 -04001341 dev_dbg (&udev->dev, "unregistering device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 usb_disable_device(udev, 0);
1343
Alan Stern782da722006-07-01 22:09:35 -04001344 usb_unlock_device(udev);
Greg Kroah-Hartman3099e752005-06-20 21:15:16 -07001345
Alan Sterne16362a2008-05-20 16:37:34 -04001346 /* Remove the device-specific files from sysfs. This must be
1347 * done with udev unlocked, because some of the attribute
1348 * routines try to acquire the device lock.
1349 */
1350 usb_remove_sysfs_dev_files(udev);
1351
Alan Stern782da722006-07-01 22:09:35 -04001352 /* Unregister the device. The device driver is responsible
1353 * for removing the device files from usbfs and sysfs and for
1354 * de-configuring the device.
1355 */
1356 device_del(&udev->dev);
1357
1358 /* Free the device number and delete the parent's children[]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * (or root_hub) pointer.
1360 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 release_address(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 /* Avoid races with recursively_mark_NOTATTACHED() */
1364 spin_lock_irq(&device_state_lock);
1365 *pdev = NULL;
1366 spin_unlock_irq(&device_state_lock);
1367
Alan Sternd5d4db72007-05-29 16:34:52 -04001368 usb_stop_pm(udev);
Alan Sternee49fb52006-11-22 16:55:54 -05001369
Alan Stern782da722006-07-01 22:09:35 -04001370 put_device(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001373#ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374static void show_string(struct usb_device *udev, char *id, char *string)
1375{
1376 if (!string)
1377 return;
1378 dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string);
1379}
1380
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001381static void announce_device(struct usb_device *udev)
1382{
1383 dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n",
1384 le16_to_cpu(udev->descriptor.idVendor),
1385 le16_to_cpu(udev->descriptor.idProduct));
1386 dev_info(&udev->dev, "New USB device strings: Mfr=%d, Product=%d, "
1387 "SerialNumber=%d\n",
1388 udev->descriptor.iManufacturer,
1389 udev->descriptor.iProduct,
1390 udev->descriptor.iSerialNumber);
1391 show_string(udev, "Product", udev->product);
1392 show_string(udev, "Manufacturer", udev->manufacturer);
1393 show_string(udev, "SerialNumber", udev->serial);
1394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395#else
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001396static inline void announce_device(struct usb_device *udev) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397#endif
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399#ifdef CONFIG_USB_OTG
1400#include "otg_whitelist.h"
1401#endif
1402
Oliver Neukum3ede7602007-01-11 14:35:50 +01001403/**
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001404 * usb_configure_device_otg - FIXME (usbcore-internal)
Oliver Neukum3ede7602007-01-11 14:35:50 +01001405 * @udev: newly addressed device (in ADDRESS state)
1406 *
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001407 * Do configuration for On-The-Go devices
Oliver Neukum3ede7602007-01-11 14:35:50 +01001408 */
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001409static int usb_configure_device_otg(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001411 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413#ifdef CONFIG_USB_OTG
1414 /*
1415 * OTG-aware devices on OTG-capable root hubs may be able to use SRP,
1416 * to wake us after we've powered off VBUS; and HNP, switching roles
1417 * "host" to "peripheral". The OTG descriptor helps figure this out.
1418 */
1419 if (!udev->bus->is_b_host
1420 && udev->config
1421 && udev->parent == udev->bus->root_hub) {
1422 struct usb_otg_descriptor *desc = 0;
1423 struct usb_bus *bus = udev->bus;
1424
1425 /* descriptor may appear anywhere in config */
1426 if (__usb_get_extra_descriptor (udev->rawdescriptors[0],
1427 le16_to_cpu(udev->config[0].desc.wTotalLength),
1428 USB_DT_OTG, (void **) &desc) == 0) {
1429 if (desc->bmAttributes & USB_OTG_HNP) {
Alan Stern12c3da32005-11-23 12:09:52 -05001430 unsigned port1 = udev->portnum;
David Brownellfc849b82006-09-18 16:53:26 -07001431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 dev_info(&udev->dev,
1433 "Dual-Role OTG device on %sHNP port\n",
1434 (port1 == bus->otg_port)
1435 ? "" : "non-");
1436
1437 /* enable HNP before suspend, it's simpler */
1438 if (port1 == bus->otg_port)
1439 bus->b_hnp_enable = 1;
1440 err = usb_control_msg(udev,
1441 usb_sndctrlpipe(udev, 0),
1442 USB_REQ_SET_FEATURE, 0,
1443 bus->b_hnp_enable
1444 ? USB_DEVICE_B_HNP_ENABLE
1445 : USB_DEVICE_A_ALT_HNP_SUPPORT,
1446 0, NULL, 0, USB_CTRL_SET_TIMEOUT);
1447 if (err < 0) {
1448 /* OTG MESSAGE: report errors here,
1449 * customize to match your product.
1450 */
1451 dev_info(&udev->dev,
1452 "can't set HNP mode; %d\n",
1453 err);
1454 bus->b_hnp_enable = 0;
1455 }
1456 }
1457 }
1458 }
1459
1460 if (!is_targeted(udev)) {
1461
1462 /* Maybe it can talk to us, though we can't talk to it.
1463 * (Includes HNP test device.)
1464 */
1465 if (udev->bus->b_hnp_enable || udev->bus->is_b_host) {
Alan Stern4956ecc2007-05-30 16:51:28 -04001466 err = usb_port_suspend(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (err < 0)
1468 dev_dbg(&udev->dev, "HNP fail, %d\n", err);
1469 }
Vikram Panditaffcdc182007-05-25 21:31:07 -07001470 err = -ENOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto fail;
1472 }
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001473fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474#endif
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001475 return err;
1476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001478
1479/**
1480 * usb_configure_device - Detect and probe device intfs/otg (usbcore-internal)
1481 * @udev: newly addressed device (in ADDRESS state)
1482 *
1483 * This is only called by usb_new_device() and usb_authorize_device()
1484 * and FIXME -- all comments that apply to them apply here wrt to
1485 * environment.
1486 *
1487 * If the device is WUSB and not authorized, we don't attempt to read
1488 * the string descriptors, as they will be errored out by the device
1489 * until it has been authorized.
1490 */
1491static int usb_configure_device(struct usb_device *udev)
1492{
1493 int err;
1494
1495 if (udev->config == NULL) {
1496 err = usb_get_configuration(udev);
1497 if (err < 0) {
1498 dev_err(&udev->dev, "can't read configurations, error %d\n",
1499 err);
1500 goto fail;
1501 }
1502 }
1503 if (udev->wusb == 1 && udev->authorized == 0) {
1504 udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1505 udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1506 udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1507 }
1508 else {
1509 /* read the standard strings and cache them if present */
1510 udev->product = usb_cache_string(udev, udev->descriptor.iProduct);
1511 udev->manufacturer = usb_cache_string(udev,
1512 udev->descriptor.iManufacturer);
1513 udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber);
1514 }
1515 err = usb_configure_device_otg(udev);
1516fail:
1517 return err;
1518}
1519
1520
1521/**
1522 * usb_new_device - perform initial device setup (usbcore-internal)
1523 * @udev: newly addressed device (in ADDRESS state)
1524 *
1525 * This is called with devices which have been enumerated, but not yet
1526 * configured. The device descriptor is available, but not descriptors
1527 * for any device configuration. The caller must have locked either
1528 * the parent hub (if udev is a normal device) or else the
1529 * usb_bus_list_lock (if udev is a root hub). The parent's pointer to
1530 * udev has already been installed, but udev is not yet visible through
1531 * sysfs or other filesystem code.
1532 *
1533 * It will return if the device is configured properly or not. Zero if
1534 * the interface was registered with the driver core; else a negative
1535 * errno value.
1536 *
1537 * This call is synchronous, and may not be used in an interrupt context.
1538 *
1539 * Only the hub driver or root-hub registrar should ever call this.
1540 */
1541int usb_new_device(struct usb_device *udev)
1542{
1543 int err;
1544
1545 usb_detect_quirks(udev); /* Determine quirks */
1546 err = usb_configure_device(udev); /* detect & probe dev/intfs */
1547 if (err < 0)
1548 goto fail;
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001549 /* export the usbdev device-node for libusb */
1550 udev->dev.devt = MKDEV(USB_DEVICE_MAJOR,
1551 (((udev->bus->busnum-1) * 128) + (udev->devnum-1)));
1552
Alan Stern195af2c2007-07-16 15:28:19 -04001553 /* Increment the parent's count of unsuspended children */
1554 if (udev->parent)
1555 usb_autoresume_device(udev->parent);
1556
Alan Stern782da722006-07-01 22:09:35 -04001557 /* Register the device. The device driver is responsible
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001558 * for adding the device files to sysfs and for configuring
1559 * the device.
Alan Stern782da722006-07-01 22:09:35 -04001560 */
Kay Sievers9f8b17e2007-03-13 15:59:31 +01001561 err = device_add(&udev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 if (err) {
1563 dev_err(&udev->dev, "can't device_add, error %d\n", err);
1564 goto fail;
1565 }
Alan Stern9ad3d6c2005-11-17 17:10:32 -05001566
Alan Sterne16362a2008-05-20 16:37:34 -04001567 /* put device-specific files into sysfs */
1568 usb_create_sysfs_dev_files(udev);
1569
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001570 /* Tell the world! */
Greg Kroah-Hartmanf2a383e2007-11-26 22:11:55 -08001571 announce_device(udev);
Greg Kroah-Hartmanc0664752006-08-11 01:55:12 -07001572 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574fail:
1575 usb_set_device_state(udev, USB_STATE_NOTATTACHED);
Inaky Perez-Gonzalezd9d16e82007-07-31 20:34:05 -07001576 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07001579
1580/**
Randy Dunlapfd39c862007-10-15 17:30:02 -07001581 * usb_deauthorize_device - deauthorize a device (usbcore-internal)
1582 * @usb_dev: USB device
1583 *
1584 * Move the USB device to a very basic state where interfaces are disabled
1585 * and the device is in fact unconfigured and unusable.
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07001586 *
1587 * We share a lock (that we have) with device_del(), so we need to
1588 * defer its call.
1589 */
1590int usb_deauthorize_device(struct usb_device *usb_dev)
1591{
1592 unsigned cnt;
1593 usb_lock_device(usb_dev);
1594 if (usb_dev->authorized == 0)
1595 goto out_unauthorized;
1596 usb_dev->authorized = 0;
1597 usb_set_configuration(usb_dev, -1);
1598 usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1599 usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1600 usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL);
1601 kfree(usb_dev->config);
1602 usb_dev->config = NULL;
1603 for (cnt = 0; cnt < usb_dev->descriptor.bNumConfigurations; cnt++)
1604 kfree(usb_dev->rawdescriptors[cnt]);
1605 usb_dev->descriptor.bNumConfigurations = 0;
1606 kfree(usb_dev->rawdescriptors);
1607out_unauthorized:
1608 usb_unlock_device(usb_dev);
1609 return 0;
1610}
1611
1612
1613int usb_authorize_device(struct usb_device *usb_dev)
1614{
1615 int result = 0, c;
1616 usb_lock_device(usb_dev);
1617 if (usb_dev->authorized == 1)
1618 goto out_authorized;
1619 kfree(usb_dev->product);
1620 usb_dev->product = NULL;
1621 kfree(usb_dev->manufacturer);
1622 usb_dev->manufacturer = NULL;
1623 kfree(usb_dev->serial);
1624 usb_dev->serial = NULL;
1625 result = usb_autoresume_device(usb_dev);
1626 if (result < 0) {
1627 dev_err(&usb_dev->dev,
1628 "can't autoresume for authorization: %d\n", result);
1629 goto error_autoresume;
1630 }
1631 result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor));
1632 if (result < 0) {
1633 dev_err(&usb_dev->dev, "can't re-read device descriptor for "
1634 "authorization: %d\n", result);
1635 goto error_device_descriptor;
1636 }
1637 usb_dev->authorized = 1;
1638 result = usb_configure_device(usb_dev);
1639 if (result < 0)
1640 goto error_configure;
1641 /* Choose and set the configuration. This registers the interfaces
1642 * with the driver core and lets interface drivers bind to them.
1643 */
Greg Kroah-Hartmanb5ea0602007-08-02 22:44:27 -06001644 c = usb_choose_configuration(usb_dev);
Inaky Perez-Gonzalez93993a02007-07-31 20:34:06 -07001645 if (c >= 0) {
1646 result = usb_set_configuration(usb_dev, c);
1647 if (result) {
1648 dev_err(&usb_dev->dev,
1649 "can't set config #%d, error %d\n", c, result);
1650 /* This need not be fatal. The user can try to
1651 * set other configurations. */
1652 }
1653 }
1654 dev_info(&usb_dev->dev, "authorized to connect\n");
1655error_configure:
1656error_device_descriptor:
1657error_autoresume:
1658out_authorized:
1659 usb_unlock_device(usb_dev); // complements locktree
1660 return result;
1661}
1662
1663
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07001664/* Returns 1 if @hub is a WUSB root hub, 0 otherwise */
1665static unsigned hub_is_wusb(struct usb_hub *hub)
1666{
1667 struct usb_hcd *hcd;
1668 if (hub->hdev->parent != NULL) /* not a root hub? */
1669 return 0;
1670 hcd = container_of(hub->hdev->bus, struct usb_hcd, self);
1671 return hcd->wireless;
1672}
1673
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675#define PORT_RESET_TRIES 5
1676#define SET_ADDRESS_TRIES 2
1677#define GET_DESCRIPTOR_TRIES 2
1678#define SET_CONFIG_TRIES (2 * (use_both_schemes + 1))
1679#define USE_NEW_SCHEME(i) ((i) / 2 == old_scheme_first)
1680
1681#define HUB_ROOT_RESET_TIME 50 /* times are in msec */
1682#define HUB_SHORT_RESET_TIME 10
1683#define HUB_LONG_RESET_TIME 200
1684#define HUB_RESET_TIMEOUT 500
1685
1686static int hub_port_wait_reset(struct usb_hub *hub, int port1,
1687 struct usb_device *udev, unsigned int delay)
1688{
1689 int delay_time, ret;
1690 u16 portstatus;
1691 u16 portchange;
1692
1693 for (delay_time = 0;
1694 delay_time < HUB_RESET_TIMEOUT;
1695 delay_time += delay) {
1696 /* wait to give the device a chance to reset */
1697 msleep(delay);
1698
1699 /* read and decode port status */
1700 ret = hub_port_status(hub, port1, &portstatus, &portchange);
1701 if (ret < 0)
1702 return ret;
1703
1704 /* Device went away? */
1705 if (!(portstatus & USB_PORT_STAT_CONNECTION))
1706 return -ENOTCONN;
1707
Alan Sterndd4dd192007-05-04 11:55:54 -04001708 /* bomb out completely if the connection bounced */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if ((portchange & USB_PORT_STAT_C_CONNECTION))
Alan Sterndd4dd192007-05-04 11:55:54 -04001710 return -ENOTCONN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 /* if we`ve finished resetting, then break out of the loop */
1713 if (!(portstatus & USB_PORT_STAT_RESET) &&
1714 (portstatus & USB_PORT_STAT_ENABLE)) {
Inaky Perez-Gonzalez0165de02006-08-25 19:35:29 -07001715 if (hub_is_wusb(hub))
1716 udev->speed = USB_SPEED_VARIABLE;
1717 else if (portstatus & USB_PORT_STAT_HIGH_SPEED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 udev->speed = USB_SPEED_HIGH;
1719 else if (portstatus & USB_PORT_STAT_LOW_SPEED)
1720 udev->speed = USB_SPEED_LOW;
1721 else
1722 udev->speed = USB_SPEED_FULL;
1723 return 0;
1724 }
1725
1726 /* switch to the long delay after two short delay failures */
1727 if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
1728 delay = HUB_LONG_RESET_TIME;
1729
1730 dev_dbg (hub->intfdev,
1731 "port %d not reset yet, waiting %dms\n",
1732 port1, delay);
1733 }
1734
1735 return -EBUSY;
1736}
1737
1738static int hub_port_reset(struct usb_hub *hub, int port1,
1739 struct usb_device *udev, unsigned int delay)
1740{
1741 int i, status;
1742
Alan Stern32fe0192007-10-10 16:27:07 -04001743 /* Block EHCI CF initialization during the port reset.
1744 * Some companion controllers don't like it when they mix.
1745 */
1746 down_read(&ehci_cf_port_reset_rwsem);
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 /* Reset the port */
1749 for (i = 0; i < PORT_RESET_TRIES; i++) {
1750 status = set_port_feature(hub->hdev,
1751 port1, USB_PORT_FEAT_RESET);
1752 if (status)
1753 dev_err(hub->intfdev,
1754 "cannot reset port %d (err = %d)\n",
1755 port1, status);
1756 else {
1757 status = hub_port_wait_reset(hub, port1, udev, delay);
David Brownelldd165252005-08-31 10:45:25 -07001758 if (status && status != -ENOTCONN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 dev_dbg(hub->intfdev,
1760 "port_wait_reset: err = %d\n",
1761 status);
1762 }
1763
1764 /* return on disconnect or reset */
1765 switch (status) {
1766 case 0:
David Brownellb7896962005-08-31 10:41:44 -07001767 /* TRSTRCY = 10 ms; plus some extra */
1768 msleep(10 + 40);
David Vrabel4953d142008-04-08 13:24:46 -07001769 update_address(udev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /* FALL THROUGH */
1771 case -ENOTCONN:
1772 case -ENODEV:
1773 clear_port_feature(hub->hdev,
1774 port1, USB_PORT_FEAT_C_RESET);
1775 /* FIXME need disconnect() for NOTATTACHED device */
1776 usb_set_device_state(udev, status
1777 ? USB_STATE_NOTATTACHED
1778 : USB_STATE_DEFAULT);
Alan Stern32fe0192007-10-10 16:27:07 -04001779 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781
1782 dev_dbg (hub->intfdev,
1783 "port %d not enabled, trying reset again...\n",
1784 port1);
1785 delay = HUB_LONG_RESET_TIME;
1786 }
1787
1788 dev_err (hub->intfdev,
1789 "Cannot enable port %i. Maybe the USB cable is bad?\n",
1790 port1);
1791
Alan Stern32fe0192007-10-10 16:27:07 -04001792 done:
1793 up_read(&ehci_cf_port_reset_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return status;
1795}
1796
Alan Sternd388dab2006-07-01 22:14:24 -04001797#ifdef CONFIG_PM
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Alan Sternb01b03f2008-04-28 11:06:11 -04001799#define MASK_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION | \
1800 USB_PORT_STAT_SUSPEND)
1801#define WANT_BITS (USB_PORT_STAT_POWER | USB_PORT_STAT_CONNECTION)
1802
1803/* Determine whether the device on a port is ready for a normal resume,
1804 * is ready for a reset-resume, or should be disconnected.
1805 */
1806static int check_port_resume_type(struct usb_device *udev,
1807 struct usb_hub *hub, int port1,
1808 int status, unsigned portchange, unsigned portstatus)
1809{
1810 /* Is the device still present? */
1811 if (status || (portstatus & MASK_BITS) != WANT_BITS) {
1812 if (status >= 0)
1813 status = -ENODEV;
1814 }
1815
1816 /* Can't do a normal resume if the port isn't enabled */
1817 else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume)
1818 status = -ENODEV;
1819
1820 if (status) {
1821 dev_dbg(hub->intfdev,
1822 "port %d status %04x.%04x after resume, %d\n",
1823 port1, portchange, portstatus, status);
1824 } else if (udev->reset_resume) {
1825
1826 /* Late port handoff can set status-change bits */
1827 if (portchange & USB_PORT_STAT_C_CONNECTION)
1828 clear_port_feature(hub->hdev, port1,
1829 USB_PORT_FEAT_C_CONNECTION);
1830 if (portchange & USB_PORT_STAT_C_ENABLE)
1831 clear_port_feature(hub->hdev, port1,
1832 USB_PORT_FEAT_C_ENABLE);
1833 }
1834
1835 return status;
1836}
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838#ifdef CONFIG_USB_SUSPEND
1839
1840/*
Alan Stern140d8f62006-07-01 22:07:21 -04001841 * usb_port_suspend - suspend a usb device's upstream port
Alan Stern624d6c02007-05-30 15:35:16 -04001842 * @udev: device that's no longer in active use, not a root hub
David Brownell5edbfb72005-09-22 22:45:26 -07001843 * Context: must be able to sleep; device not locked; pm locks held
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 *
1845 * Suspends a USB device that isn't in active use, conserving power.
1846 * Devices may wake out of a suspend, if anything important happens,
1847 * using the remote wakeup mechanism. They may also be taken out of
Alan Stern140d8f62006-07-01 22:07:21 -04001848 * suspend by the host, using usb_port_resume(). It's also routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 * to disconnect devices while they are suspended.
1850 *
David Brownell390a8c32005-09-13 19:57:27 -07001851 * This only affects the USB hardware for a device; its interfaces
1852 * (and, for hubs, child devices) must already have been suspended.
1853 *
Alan Stern624d6c02007-05-30 15:35:16 -04001854 * Selective port suspend reduces power; most suspended devices draw
1855 * less than 500 uA. It's also used in OTG, along with remote wakeup.
1856 * All devices below the suspended port are also suspended.
1857 *
1858 * Devices leave suspend state when the host wakes them up. Some devices
1859 * also support "remote wakeup", where the device can activate the USB
1860 * tree above them to deliver data, such as a keypress or packet. In
1861 * some cases, this wakes the USB host.
1862 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 * Suspending OTG devices may trigger HNP, if that's been enabled
1864 * between a pair of dual-role devices. That will change roles, such
1865 * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral.
1866 *
Alan Stern4956ecc2007-05-30 16:51:28 -04001867 * Devices on USB hub ports have only one "suspend" state, corresponding
1868 * to ACPI D2, "may cause the device to lose some context".
1869 * State transitions include:
1870 *
1871 * - suspend, resume ... when the VBUS power link stays live
1872 * - suspend, disconnect ... VBUS lost
1873 *
1874 * Once VBUS drop breaks the circuit, the port it's using has to go through
1875 * normal re-enumeration procedures, starting with enabling VBUS power.
1876 * Other than re-initializing the hub (plug/unplug, except for root hubs),
1877 * Linux (2.6) currently has NO mechanisms to initiate that: no khubd
1878 * timer, no SRP, no requests through sysfs.
1879 *
1880 * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when
1881 * the root hub for their bus goes into global suspend ... so we don't
1882 * (falsely) update the device power state to say it suspended.
1883 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 * Returns 0 on success, else negative errno.
1885 */
Alan Stern140d8f62006-07-01 22:07:21 -04001886int usb_port_suspend(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Alan Stern624d6c02007-05-30 15:35:16 -04001888 struct usb_hub *hub = hdev_to_hub(udev->parent);
1889 int port1 = udev->portnum;
1890 int status;
Alan Stern4956ecc2007-05-30 16:51:28 -04001891
Alan Stern624d6c02007-05-30 15:35:16 -04001892 // dev_dbg(hub->intfdev, "suspend port %d\n", port1);
1893
1894 /* enable remote wakeup when appropriate; this lets the device
1895 * wake up the upstream hub (including maybe the root hub).
1896 *
1897 * NOTE: OTG devices may issue remote wakeup (or SRP) even when
1898 * we don't explicitly enable it here.
1899 */
1900 if (udev->do_remote_wakeup) {
1901 status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1902 USB_REQ_SET_FEATURE, USB_RECIP_DEVICE,
1903 USB_DEVICE_REMOTE_WAKEUP, 0,
1904 NULL, 0,
1905 USB_CTRL_SET_TIMEOUT);
1906 if (status)
1907 dev_dbg(&udev->dev, "won't remote wakeup, status %d\n",
1908 status);
1909 }
1910
1911 /* see 7.1.7.6 */
1912 status = set_port_feature(hub->hdev, port1, USB_PORT_FEAT_SUSPEND);
1913 if (status) {
1914 dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n",
1915 port1, status);
1916 /* paranoia: "should not happen" */
1917 (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
1918 USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE,
1919 USB_DEVICE_REMOTE_WAKEUP, 0,
1920 NULL, 0,
1921 USB_CTRL_SET_TIMEOUT);
1922 } else {
1923 /* device has up to 10 msec to fully suspend */
1924 dev_dbg(&udev->dev, "usb %ssuspend\n",
1925 udev->auto_pm ? "auto-" : "");
1926 usb_set_device_state(udev, USB_STATE_SUSPENDED);
1927 msleep(10);
1928 }
Alan Stern4956ecc2007-05-30 16:51:28 -04001929 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
David Brownellf3f32532005-09-22 22:37:29 -07001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
David Brownell390a8c32005-09-13 19:57:27 -07001933 * If the USB "suspend" state is in use (rather than "global suspend"),
1934 * many devices will be individually taken out of suspend state using
Alan Stern54515fe2007-05-30 15:38:58 -04001935 * special "resume" signaling. This routine kicks in shortly after
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * hardware resume signaling is finished, either because of selective
1937 * resume (by host) or remote wakeup (by device) ... now see what changed
1938 * in the tree that's rooted at this device.
Alan Stern54515fe2007-05-30 15:38:58 -04001939 *
1940 * If @udev->reset_resume is set then the device is reset before the
1941 * status check is done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 */
Alan Stern140d8f62006-07-01 22:07:21 -04001943static int finish_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Alan Stern54515fe2007-05-30 15:38:58 -04001945 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 u16 devstatus;
1947
1948 /* caller owns the udev device lock */
Alan Stern54515fe2007-05-30 15:38:58 -04001949 dev_dbg(&udev->dev, "finish %sresume\n",
1950 udev->reset_resume ? "reset-" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 /* usb ch9 identifies four variants of SUSPENDED, based on what
1953 * state the device resumes to. Linux currently won't see the
1954 * first two on the host side; they'd be inside hub_port_init()
1955 * during many timeouts, but khubd can't suspend until later.
1956 */
1957 usb_set_device_state(udev, udev->actconfig
1958 ? USB_STATE_CONFIGURED
1959 : USB_STATE_ADDRESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Alan Stern54515fe2007-05-30 15:38:58 -04001961 /* 10.5.4.5 says not to reset a suspended port if the attached
1962 * device is enabled for remote wakeup. Hence the reset
1963 * operation is carried out here, after the port has been
1964 * resumed.
1965 */
1966 if (udev->reset_resume)
1967 status = usb_reset_device(udev);
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* 10.5.4.5 says be sure devices in the tree are still there.
1970 * For now let's assume the device didn't go crazy on resume,
1971 * and device drivers will know about any resume quirks.
1972 */
Alan Stern54515fe2007-05-30 15:38:58 -04001973 if (status == 0) {
Alan Stern46dede42007-08-14 10:56:10 -04001974 devstatus = 0;
Alan Stern54515fe2007-05-30 15:38:58 -04001975 status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus);
1976 if (status >= 0)
Alan Stern46dede42007-08-14 10:56:10 -04001977 status = (status > 0 ? 0 : -ENODEV);
Alan Stern54515fe2007-05-30 15:38:58 -04001978 }
Alan Sternb40b7a92006-06-19 15:12:38 -04001979
Alan Stern624d6c02007-05-30 15:35:16 -04001980 if (status) {
1981 dev_dbg(&udev->dev, "gone after usb resume? status %d\n",
1982 status);
1983 } else if (udev->actconfig) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 le16_to_cpus(&devstatus);
Alan Stern686314c2007-05-30 15:34:36 -04001985 if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 status = usb_control_msg(udev,
1987 usb_sndctrlpipe(udev, 0),
1988 USB_REQ_CLEAR_FEATURE,
1989 USB_RECIP_DEVICE,
1990 USB_DEVICE_REMOTE_WAKEUP, 0,
1991 NULL, 0,
1992 USB_CTRL_SET_TIMEOUT);
Alan Sterna8e7c562006-07-01 22:11:02 -04001993 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 dev_dbg(&udev->dev, "disable remote "
1995 "wakeup, status %d\n", status);
Alan Stern4bf0ba82005-11-21 11:58:07 -05001996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999 return status;
2000}
2001
Alan Stern624d6c02007-05-30 15:35:16 -04002002/*
2003 * usb_port_resume - re-activate a suspended usb device's upstream port
2004 * @udev: device to re-activate, not a root hub
2005 * Context: must be able to sleep; device not locked; pm locks held
2006 *
2007 * This will re-activate the suspended device, increasing power usage
2008 * while letting drivers communicate again with its endpoints.
2009 * USB resume explicitly guarantees that the power session between
2010 * the host and the device is the same as it was when the device
2011 * suspended.
2012 *
Alan Sternfeccc302008-03-03 15:15:59 -05002013 * If @udev->reset_resume is set then this routine won't check that the
2014 * port is still enabled. Furthermore, finish_port_resume() above will
Alan Stern54515fe2007-05-30 15:38:58 -04002015 * reset @udev. The end result is that a broken power session can be
2016 * recovered and @udev will appear to persist across a loss of VBUS power.
2017 *
2018 * For example, if a host controller doesn't maintain VBUS suspend current
2019 * during a system sleep or is reset when the system wakes up, all the USB
2020 * power sessions below it will be broken. This is especially troublesome
2021 * for mass-storage devices containing mounted filesystems, since the
2022 * device will appear to have disconnected and all the memory mappings
2023 * to it will be lost. Using the USB_PERSIST facility, the device can be
2024 * made to appear as if it had not disconnected.
2025 *
Alan Sternfeccc302008-03-03 15:15:59 -05002026 * This facility can be dangerous. Although usb_reset_device() makes
2027 * every effort to insure that the same device is present after the
Alan Stern54515fe2007-05-30 15:38:58 -04002028 * reset as before, it cannot provide a 100% guarantee. Furthermore it's
2029 * quite possible for a device to remain unaltered but its media to be
2030 * changed. If the user replaces a flash memory card while the system is
2031 * asleep, he will have only himself to blame when the filesystem on the
2032 * new card is corrupted and the system crashes.
2033 *
Alan Stern624d6c02007-05-30 15:35:16 -04002034 * Returns 0 on success, else negative errno.
2035 */
2036int usb_port_resume(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Alan Stern624d6c02007-05-30 15:35:16 -04002038 struct usb_hub *hub = hdev_to_hub(udev->parent);
2039 int port1 = udev->portnum;
2040 int status;
2041 u16 portchange, portstatus;
Alan Sternd25450c2006-11-20 11:14:30 -05002042
2043 /* Skip the initial Clear-Suspend step for a remote wakeup */
2044 status = hub_port_status(hub, port1, &portstatus, &portchange);
2045 if (status == 0 && !(portstatus & USB_PORT_STAT_SUSPEND))
2046 goto SuspendCleared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 // dev_dbg(hub->intfdev, "resume port %d\n", port1);
2049
Alan Sternd5cbad42006-08-11 16:52:39 -04002050 set_bit(port1, hub->busy_bits);
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* see 7.1.7.7; affects power usage, but not budgeting */
2053 status = clear_port_feature(hub->hdev,
2054 port1, USB_PORT_FEAT_SUSPEND);
2055 if (status) {
Alan Stern624d6c02007-05-30 15:35:16 -04002056 dev_dbg(hub->intfdev, "can't resume port %d, status %d\n",
2057 port1, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 /* drive resume for at least 20 msec */
Alan Stern686314c2007-05-30 15:34:36 -04002060 dev_dbg(&udev->dev, "usb %sresume\n",
2061 udev->auto_pm ? "auto-" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 msleep(25);
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 /* Virtual root hubs can trigger on GET_PORT_STATUS to
2065 * stop resume signaling. Then finish the resume
2066 * sequence.
2067 */
Alan Sternd25450c2006-11-20 11:14:30 -05002068 status = hub_port_status(hub, port1, &portstatus, &portchange);
Alan Stern54515fe2007-05-30 15:38:58 -04002069
Alan Sternb01b03f2008-04-28 11:06:11 -04002070 /* TRSMRCY = 10 msec */
2071 msleep(10);
2072 }
Alan Stern54515fe2007-05-30 15:38:58 -04002073
Alan Sternb01b03f2008-04-28 11:06:11 -04002074 SuspendCleared:
2075 if (status == 0) {
2076 if (portchange & USB_PORT_STAT_C_SUSPEND)
2077 clear_port_feature(hub->hdev, port1,
2078 USB_PORT_FEAT_C_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Alan Sternd5cbad42006-08-11 16:52:39 -04002081 clear_bit(port1, hub->busy_bits);
Linus Torvalds09ca8ad2008-07-06 10:27:25 -07002082 if (!hub->hdev->parent && !hub->busy_bits[0])
2083 usb_enable_root_hub_irq(hub->hdev->bus);
Alan Sternd5cbad42006-08-11 16:52:39 -04002084
Alan Sternb01b03f2008-04-28 11:06:11 -04002085 status = check_port_resume_type(udev,
2086 hub, port1, status, portchange, portstatus);
Alan Stern54515fe2007-05-30 15:38:58 -04002087 if (status == 0)
2088 status = finish_port_resume(udev);
2089 if (status < 0) {
2090 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2091 hub_port_logical_disconnect(hub, port1);
2092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return status;
2094}
2095
Alan Stern8808f002008-04-28 11:06:55 -04002096/* caller has locked udev */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097static int remote_wakeup(struct usb_device *udev)
2098{
2099 int status = 0;
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (udev->state == USB_STATE_SUSPENDED) {
Alan Stern645daaa2006-08-30 15:47:02 -04002102 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-");
Alan Stern19410442007-03-27 13:33:59 -04002103 usb_mark_last_busy(udev);
Alan Stern6b157c92007-03-13 16:37:30 -04002104 status = usb_external_resume_device(udev);
Alan Sternd25450c2006-11-20 11:14:30 -05002105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 return status;
2107}
2108
Alan Sternd388dab2006-07-01 22:14:24 -04002109#else /* CONFIG_USB_SUSPEND */
2110
2111/* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */
2112
2113int usb_port_suspend(struct usb_device *udev)
2114{
2115 return 0;
2116}
2117
Alan Sternb01b03f2008-04-28 11:06:11 -04002118/* However we may need to do a reset-resume */
2119
Alan Sternd388dab2006-07-01 22:14:24 -04002120int usb_port_resume(struct usb_device *udev)
2121{
Alan Sternb01b03f2008-04-28 11:06:11 -04002122 struct usb_hub *hub = hdev_to_hub(udev->parent);
2123 int port1 = udev->portnum;
2124 int status;
2125 u16 portchange, portstatus;
Alan Stern54515fe2007-05-30 15:38:58 -04002126
Alan Sternb01b03f2008-04-28 11:06:11 -04002127 status = hub_port_status(hub, port1, &portstatus, &portchange);
2128 status = check_port_resume_type(udev,
2129 hub, port1, status, portchange, portstatus);
2130
2131 if (status) {
2132 dev_dbg(&udev->dev, "can't resume, status %d\n", status);
2133 hub_port_logical_disconnect(hub, port1);
2134 } else if (udev->reset_resume) {
Alan Stern54515fe2007-05-30 15:38:58 -04002135 dev_dbg(&udev->dev, "reset-resume\n");
2136 status = usb_reset_device(udev);
2137 }
2138 return status;
Alan Sternd388dab2006-07-01 22:14:24 -04002139}
2140
2141static inline int remote_wakeup(struct usb_device *udev)
2142{
2143 return 0;
2144}
2145
2146#endif
2147
David Brownelldb690872005-09-13 19:56:33 -07002148static int hub_suspend(struct usb_interface *intf, pm_message_t msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 struct usb_hub *hub = usb_get_intfdata (intf);
2151 struct usb_device *hdev = hub->hdev;
2152 unsigned port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
David Brownellc9f89fa2005-09-13 19:57:04 -07002154 /* fail if children aren't already suspended */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 for (port1 = 1; port1 <= hdev->maxchild; port1++) {
2156 struct usb_device *udev;
2157
2158 udev = hdev->children [port1-1];
Alan Stern6840d252007-09-10 11:34:26 -04002159 if (udev && udev->can_submit) {
Alan Stern645daaa2006-08-30 15:47:02 -04002160 if (!hdev->auto_pm)
2161 dev_dbg(&intf->dev, "port %d nyet suspended\n",
2162 port1);
David Brownellc9f89fa2005-09-13 19:57:04 -07002163 return -EBUSY;
2164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 }
2166
Harvey Harrison441b62c2008-03-03 16:08:34 -08002167 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Stern40f122f2006-11-09 14:44:33 -05002168
David Brownellc9f89fa2005-09-13 19:57:04 -07002169 /* stop khubd and related activity */
2170 hub_quiesce(hub);
Alan Sternb6f64362007-05-04 11:51:54 -04002171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
2174static int hub_resume(struct usb_interface *intf)
2175{
Alan Stern5e6effa2008-03-03 15:15:51 -05002176 struct usb_hub *hub = usb_get_intfdata(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Alan Stern5e6effa2008-03-03 15:15:51 -05002178 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04002179 hub_activate(hub, HUB_RESUME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return 0;
2181}
2182
Alan Sternb41a60e2007-05-30 15:39:33 -04002183static int hub_reset_resume(struct usb_interface *intf)
Alan Sternf07600c2007-05-30 15:38:16 -04002184{
Alan Sternb41a60e2007-05-30 15:39:33 -04002185 struct usb_hub *hub = usb_get_intfdata(intf);
Alan Sternf07600c2007-05-30 15:38:16 -04002186
Alan Stern5e6effa2008-03-03 15:15:51 -05002187 dev_dbg(&intf->dev, "%s\n", __func__);
Alan Sternf2835212008-04-28 11:07:17 -04002188 hub_activate(hub, HUB_RESET_RESUME);
Alan Sternf07600c2007-05-30 15:38:16 -04002189 return 0;
2190}
2191
Alan Stern54515fe2007-05-30 15:38:58 -04002192/**
2193 * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power
2194 * @rhdev: struct usb_device for the root hub
2195 *
2196 * The USB host controller driver calls this function when its root hub
2197 * is resumed and Vbus power has been interrupted or the controller
Alan Sternfeccc302008-03-03 15:15:59 -05002198 * has been reset. The routine marks @rhdev as having lost power.
2199 * When the hub driver is resumed it will take notice and carry out
2200 * power-session recovery for all the "USB-PERSIST"-enabled child devices;
2201 * the others will be disconnected.
Alan Stern54515fe2007-05-30 15:38:58 -04002202 */
2203void usb_root_hub_lost_power(struct usb_device *rhdev)
2204{
2205 dev_warn(&rhdev->dev, "root hub lost power or was reset\n");
2206 rhdev->reset_resume = 1;
2207}
2208EXPORT_SYMBOL_GPL(usb_root_hub_lost_power);
2209
Alan Sternd388dab2006-07-01 22:14:24 -04002210#else /* CONFIG_PM */
2211
2212static inline int remote_wakeup(struct usb_device *udev)
2213{
2214 return 0;
2215}
2216
Alan Sternf07600c2007-05-30 15:38:16 -04002217#define hub_suspend NULL
2218#define hub_resume NULL
2219#define hub_reset_resume NULL
Alan Sternd388dab2006-07-01 22:14:24 -04002220#endif
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
2224 *
2225 * Between connect detection and reset signaling there must be a delay
2226 * of 100ms at least for debounce and power-settling. The corresponding
2227 * timer shall restart whenever the downstream port detects a disconnect.
2228 *
2229 * Apparently there are some bluetooth and irda-dongles and a number of
2230 * low-speed devices for which this debounce period may last over a second.
2231 * Not covered by the spec - but easy to deal with.
2232 *
2233 * This implementation uses a 1500ms total debounce timeout; if the
2234 * connection isn't stable by then it returns -ETIMEDOUT. It checks
2235 * every 25ms for transient disconnects. When the port status has been
2236 * unchanged for 100ms it returns the port status.
2237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238static int hub_port_debounce(struct usb_hub *hub, int port1)
2239{
2240 int ret;
2241 int total_time, stable_time = 0;
2242 u16 portchange, portstatus;
2243 unsigned connection = 0xffff;
2244
2245 for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) {
2246 ret = hub_port_status(hub, port1, &portstatus, &portchange);
2247 if (ret < 0)
2248 return ret;
2249
2250 if (!(portchange & USB_PORT_STAT_C_CONNECTION) &&
2251 (portstatus & USB_PORT_STAT_CONNECTION) == connection) {
2252 stable_time += HUB_DEBOUNCE_STEP;
2253 if (stable_time >= HUB_DEBOUNCE_STABLE)
2254 break;
2255 } else {
2256 stable_time = 0;
2257 connection = portstatus & USB_PORT_STAT_CONNECTION;
2258 }
2259
2260 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2261 clear_port_feature(hub->hdev, port1,
2262 USB_PORT_FEAT_C_CONNECTION);
2263 }
2264
2265 if (total_time >= HUB_DEBOUNCE_TIMEOUT)
2266 break;
2267 msleep(HUB_DEBOUNCE_STEP);
2268 }
2269
2270 dev_dbg (hub->intfdev,
2271 "debounce: port %d: total %dms stable %dms status 0x%x\n",
2272 port1, total_time, stable_time, portstatus);
2273
2274 if (stable_time < HUB_DEBOUNCE_STABLE)
2275 return -ETIMEDOUT;
2276 return portstatus;
2277}
2278
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07002279void usb_ep0_reinit(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
2281 usb_disable_endpoint(udev, 0 + USB_DIR_IN);
2282 usb_disable_endpoint(udev, 0 + USB_DIR_OUT);
Alan Sternbdd016b2007-07-30 17:05:22 -04002283 usb_enable_endpoint(udev, &udev->ep0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07002285EXPORT_SYMBOL_GPL(usb_ep0_reinit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287#define usb_sndaddr0pipe() (PIPE_CONTROL << 30)
2288#define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN)
2289
Alan Stern4326ed02007-07-30 17:08:43 -04002290static int hub_set_address(struct usb_device *udev, int devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 int retval;
2293
Alan Stern4326ed02007-07-30 17:08:43 -04002294 if (devnum <= 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return -EINVAL;
2296 if (udev->state == USB_STATE_ADDRESS)
2297 return 0;
2298 if (udev->state != USB_STATE_DEFAULT)
2299 return -EINVAL;
2300 retval = usb_control_msg(udev, usb_sndaddr0pipe(),
Alan Stern4326ed02007-07-30 17:08:43 -04002301 USB_REQ_SET_ADDRESS, 0, devnum, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 NULL, 0, USB_CTRL_SET_TIMEOUT);
2303 if (retval == 0) {
David Vrabel4953d142008-04-08 13:24:46 -07002304 /* Device now using proper address. */
2305 update_address(udev, devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 usb_set_device_state(udev, USB_STATE_ADDRESS);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07002307 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
2309 return retval;
2310}
2311
2312/* Reset device, (re)assign address, get device descriptor.
2313 * Device connection must be stable, no more debouncing needed.
2314 * Returns device in USB_STATE_ADDRESS, except on error.
2315 *
2316 * If this is called for an already-existing device (as part of
2317 * usb_reset_device), the caller must own the device lock. For a
2318 * newly detected device that is not accessible through any global
2319 * pointers, it's not necessary to lock the device.
2320 */
2321static int
2322hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1,
2323 int retry_counter)
2324{
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002325 static DEFINE_MUTEX(usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 struct usb_device *hdev = hub->hdev;
2328 int i, j, retval;
2329 unsigned delay = HUB_SHORT_RESET_TIME;
2330 enum usb_device_speed oldspeed = udev->speed;
Inaky Perez-Gonzalez83a07192006-08-25 19:35:31 -07002331 char *speed, *type;
Alan Stern4326ed02007-07-30 17:08:43 -04002332 int devnum = udev->devnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 /* root hub ports have a slightly longer reset period
2335 * (from USB 2.0 spec, section 7.1.7.5)
2336 */
2337 if (!hdev->parent) {
2338 delay = HUB_ROOT_RESET_TIME;
2339 if (port1 == hdev->bus->otg_port)
2340 hdev->bus->b_hnp_enable = 0;
2341 }
2342
2343 /* Some low speed devices have problems with the quick delay, so */
2344 /* be a bit pessimistic with those devices. RHbug #23670 */
2345 if (oldspeed == USB_SPEED_LOW)
2346 delay = HUB_LONG_RESET_TIME;
2347
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002348 mutex_lock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* Reset the device; full speed may morph to high speed */
2351 retval = hub_port_reset(hub, port1, udev, delay);
2352 if (retval < 0) /* error or disconnect */
2353 goto fail;
2354 /* success, speed is known */
2355 retval = -ENODEV;
2356
2357 if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) {
2358 dev_dbg(&udev->dev, "device reset changed speed!\n");
2359 goto fail;
2360 }
2361 oldspeed = udev->speed;
Alan Stern4326ed02007-07-30 17:08:43 -04002362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ...
2364 * it's fixed size except for full speed devices.
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07002365 * For Wireless USB devices, ep0 max packet is always 512 (tho
2366 * reported as 0xff in the device descriptor). WUSB1.0[4.8.1].
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 */
2368 switch (udev->speed) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07002369 case USB_SPEED_VARIABLE: /* fixed at 512 */
2370 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(512);
2371 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 case USB_SPEED_HIGH: /* fixed at 64 */
2373 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
2374 break;
2375 case USB_SPEED_FULL: /* 8, 16, 32, or 64 */
2376 /* to determine the ep0 maxpacket size, try to read
2377 * the device descriptor to get bMaxPacketSize0 and
2378 * then correct our initial guess.
2379 */
2380 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
2381 break;
2382 case USB_SPEED_LOW: /* fixed at 8 */
2383 udev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(8);
2384 break;
2385 default:
2386 goto fail;
2387 }
2388
Inaky Perez-Gonzalez83a07192006-08-25 19:35:31 -07002389 type = "";
2390 switch (udev->speed) {
2391 case USB_SPEED_LOW: speed = "low"; break;
2392 case USB_SPEED_FULL: speed = "full"; break;
2393 case USB_SPEED_HIGH: speed = "high"; break;
2394 case USB_SPEED_VARIABLE:
2395 speed = "variable";
2396 type = "Wireless ";
2397 break;
2398 default: speed = "?"; break;
2399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 dev_info (&udev->dev,
Inaky Perez-Gonzalez83a07192006-08-25 19:35:31 -07002401 "%s %s speed %sUSB device using %s and address %d\n",
2402 (udev->config) ? "reset" : "new", speed, type,
Alan Stern4326ed02007-07-30 17:08:43 -04002403 udev->bus->controller->driver->name, devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 /* Set up TT records, if needed */
2406 if (hdev->tt) {
2407 udev->tt = hdev->tt;
2408 udev->ttport = hdev->ttport;
2409 } else if (udev->speed != USB_SPEED_HIGH
2410 && hdev->speed == USB_SPEED_HIGH) {
2411 udev->tt = &hub->tt;
2412 udev->ttport = port1;
2413 }
2414
2415 /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way?
2416 * Because device hardware and firmware is sometimes buggy in
2417 * this area, and this is how Linux has done it for ages.
2418 * Change it cautiously.
2419 *
2420 * NOTE: If USE_NEW_SCHEME() is true we will start by issuing
2421 * a 64-byte GET_DESCRIPTOR request. This is what Windows does,
2422 * so it may help with some non-standards-compliant devices.
2423 * Otherwise we start with SET_ADDRESS and then try to read the
2424 * first 8 bytes of the device descriptor to get the ep0 maxpacket
2425 * value.
2426 */
2427 for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) {
2428 if (USE_NEW_SCHEME(retry_counter)) {
2429 struct usb_device_descriptor *buf;
2430 int r = 0;
2431
2432#define GET_DESCRIPTOR_BUFSIZE 64
2433 buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO);
2434 if (!buf) {
2435 retval = -ENOMEM;
2436 continue;
2437 }
2438
Alan Sternb89ee192007-05-11 10:19:04 -04002439 /* Retry on all errors; some devices are flakey.
2440 * 255 is for WUSB devices, we actually need to use
2441 * 512 (WUSB1.0[4.8.1]).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 */
2443 for (j = 0; j < 3; ++j) {
2444 buf->bMaxPacketSize0 = 0;
2445 r = usb_control_msg(udev, usb_rcvaddr0pipe(),
2446 USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
2447 USB_DT_DEVICE << 8, 0,
2448 buf, GET_DESCRIPTOR_BUFSIZE,
Alan Sternb89ee192007-05-11 10:19:04 -04002449 USB_CTRL_GET_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 switch (buf->bMaxPacketSize0) {
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07002451 case 8: case 16: case 32: case 64: case 255:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (buf->bDescriptorType ==
2453 USB_DT_DEVICE) {
2454 r = 0;
2455 break;
2456 }
2457 /* FALL THROUGH */
2458 default:
2459 if (r == 0)
2460 r = -EPROTO;
2461 break;
2462 }
2463 if (r == 0)
2464 break;
2465 }
2466 udev->descriptor.bMaxPacketSize0 =
2467 buf->bMaxPacketSize0;
2468 kfree(buf);
2469
2470 retval = hub_port_reset(hub, port1, udev, delay);
2471 if (retval < 0) /* error or disconnect */
2472 goto fail;
2473 if (oldspeed != udev->speed) {
2474 dev_dbg(&udev->dev,
2475 "device reset changed speed!\n");
2476 retval = -ENODEV;
2477 goto fail;
2478 }
2479 if (r) {
2480 dev_err(&udev->dev, "device descriptor "
2481 "read/%s, error %d\n",
2482 "64", r);
2483 retval = -EMSGSIZE;
2484 continue;
2485 }
2486#undef GET_DESCRIPTOR_BUFSIZE
2487 }
2488
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07002489 /*
2490 * If device is WUSB, we already assigned an
2491 * unauthorized address in the Connect Ack sequence;
2492 * authorization will assign the final address.
2493 */
2494 if (udev->wusb == 0) {
2495 for (j = 0; j < SET_ADDRESS_TRIES; ++j) {
2496 retval = hub_set_address(udev, devnum);
2497 if (retval >= 0)
2498 break;
2499 msleep(200);
2500 }
2501 if (retval < 0) {
2502 dev_err(&udev->dev,
2503 "device not accepting address %d, error %d\n",
2504 devnum, retval);
2505 goto fail;
2506 }
2507
2508 /* cope with hardware quirkiness:
2509 * - let SET_ADDRESS settle, some device hardware wants it
2510 * - read ep0 maxpacket even for high and low speed,
2511 */
2512 msleep(10);
2513 if (USE_NEW_SCHEME(retry_counter))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 break;
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07002515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 retval = usb_get_device_descriptor(udev, 8);
2518 if (retval < 8) {
2519 dev_err(&udev->dev, "device descriptor "
2520 "read/%s, error %d\n",
2521 "8", retval);
2522 if (retval >= 0)
2523 retval = -EMSGSIZE;
2524 } else {
2525 retval = 0;
2526 break;
2527 }
2528 }
2529 if (retval)
2530 goto fail;
2531
Inaky Perez-Gonzalez6c529cd2008-04-08 13:24:46 -07002532 i = udev->descriptor.bMaxPacketSize0 == 0xff? /* wusb device? */
Inaky Perez-Gonzalez5bb6e0a2006-08-25 19:35:30 -07002533 512 : udev->descriptor.bMaxPacketSize0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (le16_to_cpu(udev->ep0.desc.wMaxPacketSize) != i) {
2535 if (udev->speed != USB_SPEED_FULL ||
2536 !(i == 8 || i == 16 || i == 32 || i == 64)) {
2537 dev_err(&udev->dev, "ep0 maxpacket = %d\n", i);
2538 retval = -EMSGSIZE;
2539 goto fail;
2540 }
2541 dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i);
2542 udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i);
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07002543 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 }
2545
2546 retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE);
2547 if (retval < (signed)sizeof(udev->descriptor)) {
2548 dev_err(&udev->dev, "device descriptor read/%s, error %d\n",
2549 "all", retval);
2550 if (retval >= 0)
2551 retval = -ENOMSG;
2552 goto fail;
2553 }
2554
2555 retval = 0;
2556
2557fail:
Alan Stern4326ed02007-07-30 17:08:43 -04002558 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 hub_port_disable(hub, port1, 0);
David Vrabel4953d142008-04-08 13:24:46 -07002560 update_address(udev, devnum); /* for disconnect processing */
Alan Stern4326ed02007-07-30 17:08:43 -04002561 }
Arjan van de Ven4186ecf2006-01-11 15:55:29 +01002562 mutex_unlock(&usb_address0_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 return retval;
2564}
2565
2566static void
2567check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1)
2568{
2569 struct usb_qualifier_descriptor *qual;
2570 int status;
2571
Christoph Lametere94b1762006-12-06 20:33:17 -08002572 qual = kmalloc (sizeof *qual, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (qual == NULL)
2574 return;
2575
2576 status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0,
2577 qual, sizeof *qual);
2578 if (status == sizeof *qual) {
2579 dev_info(&udev->dev, "not running at top speed; "
2580 "connect to a high speed hub\n");
2581 /* hub LEDs are probably harder to miss than syslog */
2582 if (hub->has_indicators) {
2583 hub->indicator[port1-1] = INDICATOR_GREEN_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00002584 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586 }
Jesper Juhl1bc3c9e2005-04-18 17:39:34 -07002587 kfree(qual);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590static unsigned
2591hub_power_remaining (struct usb_hub *hub)
2592{
2593 struct usb_device *hdev = hub->hdev;
2594 int remaining;
Alan Stern55c52712005-11-23 12:03:12 -05002595 int port1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Alan Stern55c52712005-11-23 12:03:12 -05002597 if (!hub->limited_power)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return 0;
2599
Alan Stern55c52712005-11-23 12:03:12 -05002600 remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent;
2601 for (port1 = 1; port1 <= hdev->maxchild; ++port1) {
2602 struct usb_device *udev = hdev->children[port1 - 1];
2603 int delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 if (!udev)
2606 continue;
2607
Alan Stern55c52712005-11-23 12:03:12 -05002608 /* Unconfigured devices may not use more than 100mA,
2609 * or 8mA for OTG ports */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 if (udev->actconfig)
Alan Stern55c52712005-11-23 12:03:12 -05002611 delta = udev->actconfig->desc.bMaxPower * 2;
2612 else if (port1 != udev->bus->otg_port || hdev->parent)
2613 delta = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 else
Alan Stern55c52712005-11-23 12:03:12 -05002615 delta = 8;
2616 if (delta > hub->mA_per_port)
2617 dev_warn(&udev->dev, "%dmA is over %umA budget "
2618 "for port %d!\n",
2619 delta, hub->mA_per_port, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 remaining -= delta;
2621 }
2622 if (remaining < 0) {
Alan Stern55c52712005-11-23 12:03:12 -05002623 dev_warn(hub->intfdev, "%dmA over power budget!\n",
2624 - remaining);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 remaining = 0;
2626 }
2627 return remaining;
2628}
2629
2630/* Handle physical or logical connection change events.
2631 * This routine is called when:
2632 * a port connection-change occurs;
2633 * a port enable-change occurs (often caused by EMI);
2634 * usb_reset_device() encounters changed descriptors (as from
2635 * a firmware download)
2636 * caller already locked the hub
2637 */
2638static void hub_port_connect_change(struct usb_hub *hub, int port1,
2639 u16 portstatus, u16 portchange)
2640{
2641 struct usb_device *hdev = hub->hdev;
2642 struct device *hub_dev = hub->intfdev;
Balaji Rao90da0962007-11-22 01:58:14 +05302643 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
Alan Stern24618b02008-04-28 11:06:28 -04002644 unsigned wHubCharacteristics =
2645 le16_to_cpu(hub->descriptor->wHubCharacteristics);
Alan Stern8808f002008-04-28 11:06:55 -04002646 struct usb_device *udev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 int status, i;
Alan Stern24618b02008-04-28 11:06:28 -04002648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 dev_dbg (hub_dev,
2650 "port %d, status %04x, change %04x, %s\n",
2651 port1, portstatus, portchange, portspeed (portstatus));
2652
2653 if (hub->has_indicators) {
2654 set_port_led(hub, port1, HUB_LED_AUTO);
2655 hub->indicator[port1-1] = INDICATOR_AUTO;
2656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658#ifdef CONFIG_USB_OTG
2659 /* during HNP, don't repeat the debounce */
2660 if (hdev->bus->is_b_host)
Alan Stern24618b02008-04-28 11:06:28 -04002661 portchange &= ~(USB_PORT_STAT_C_CONNECTION |
2662 USB_PORT_STAT_C_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663#endif
2664
Alan Stern24618b02008-04-28 11:06:28 -04002665 /* Try to use the debounce delay for protection against
2666 * port-enable changes caused, for example, by EMI.
2667 */
2668 if (portchange & (USB_PORT_STAT_C_CONNECTION |
2669 USB_PORT_STAT_C_ENABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 status = hub_port_debounce(hub, port1);
Alan Sternd4b7d8e2007-05-22 11:48:17 -04002671 if (status < 0) {
2672 if (printk_ratelimit())
2673 dev_err (hub_dev, "connect-debounce failed, "
2674 "port %d disabled\n", port1);
Alan Stern24618b02008-04-28 11:06:28 -04002675 portstatus &= ~USB_PORT_STAT_CONNECTION;
2676 } else {
2677 portstatus = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680
Alan Stern8808f002008-04-28 11:06:55 -04002681 /* Try to resuscitate an existing device */
2682 udev = hdev->children[port1-1];
2683 if ((portstatus & USB_PORT_STAT_CONNECTION) && udev &&
2684 udev->state != USB_STATE_NOTATTACHED) {
2685
2686 usb_lock_device(udev);
2687 if (portstatus & USB_PORT_STAT_ENABLE) {
2688 status = 0; /* Nothing to do */
2689 } else if (!udev->persist_enabled) {
2690 status = -ENODEV; /* Mustn't resuscitate */
2691
2692#ifdef CONFIG_USB_SUSPEND
2693 } else if (udev->state == USB_STATE_SUSPENDED) {
2694 /* For a suspended device, treat this as a
2695 * remote wakeup event.
2696 */
2697 if (udev->do_remote_wakeup)
2698 status = remote_wakeup(udev);
2699
2700 /* Otherwise leave it be; devices can't tell the
2701 * difference between suspended and disabled.
2702 */
2703 else
2704 status = 0;
2705#endif
2706
2707 } else {
2708 status = usb_reset_composite_device(udev, NULL);
2709 }
2710 usb_unlock_device(udev);
2711
2712 if (status == 0) {
2713 clear_bit(port1, hub->change_bits);
2714 return;
2715 }
2716 }
2717
Alan Stern24618b02008-04-28 11:06:28 -04002718 /* Disconnect any existing devices under this port */
Alan Stern8808f002008-04-28 11:06:55 -04002719 if (udev)
Alan Stern24618b02008-04-28 11:06:28 -04002720 usb_disconnect(&hdev->children[port1-1]);
2721 clear_bit(port1, hub->change_bits);
2722
2723 /* Return now if debouncing failed or nothing is connected */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
2725
2726 /* maybe switch power back on (e.g. root hub was reset) */
Greg Kroah-Hartman74ad9bd2005-06-20 21:15:16 -07002727 if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 && !(portstatus & (1 << USB_PORT_FEAT_POWER)))
2729 set_port_feature(hdev, port1, USB_PORT_FEAT_POWER);
2730
2731 if (portstatus & USB_PORT_STAT_ENABLE)
2732 goto done;
2733 return;
2734 }
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 for (i = 0; i < SET_CONFIG_TRIES; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 /* reallocate for each attempt, since references
2739 * to the previous one can escape in various ways
2740 */
2741 udev = usb_alloc_dev(hdev, hdev->bus, port1);
2742 if (!udev) {
2743 dev_err (hub_dev,
2744 "couldn't allocate port %d usb_device\n",
2745 port1);
2746 goto done;
2747 }
2748
2749 usb_set_device_state(udev, USB_STATE_POWERED);
2750 udev->speed = USB_SPEED_UNKNOWN;
Alan Stern55c52712005-11-23 12:03:12 -05002751 udev->bus_mA = hub->mA_per_port;
Alan Sternb6956ff2006-08-30 15:46:48 -04002752 udev->level = hdev->level + 1;
Inaky Perez-Gonzalez8af548d2008-04-08 13:24:46 -07002753 udev->wusb = hub_is_wusb(hub);
Alan Stern55c52712005-11-23 12:03:12 -05002754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 /* set the address */
2756 choose_address(udev);
2757 if (udev->devnum <= 0) {
2758 status = -ENOTCONN; /* Don't retry */
2759 goto loop;
2760 }
2761
2762 /* reset and get descriptor */
2763 status = hub_port_init(hub, udev, port1, i);
2764 if (status < 0)
2765 goto loop;
2766
2767 /* consecutive bus-powered hubs aren't reliable; they can
2768 * violate the voltage drop budget. if the new child has
2769 * a "powered" LED, users should notice we didn't enable it
2770 * (without reading syslog), even without per-port LEDs
2771 * on the parent.
2772 */
2773 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB
Alan Stern55c52712005-11-23 12:03:12 -05002774 && udev->bus_mA <= 100) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 u16 devstat;
2776
2777 status = usb_get_status(udev, USB_RECIP_DEVICE, 0,
2778 &devstat);
Alan Stern55c52712005-11-23 12:03:12 -05002779 if (status < 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 dev_dbg(&udev->dev, "get status %d ?\n", status);
2781 goto loop_disable;
2782 }
Alan Stern55c52712005-11-23 12:03:12 -05002783 le16_to_cpus(&devstat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) {
2785 dev_err(&udev->dev,
2786 "can't connect bus-powered hub "
2787 "to this port\n");
2788 if (hub->has_indicators) {
2789 hub->indicator[port1-1] =
2790 INDICATOR_AMBER_BLINK;
David Howellsc4028952006-11-22 14:57:56 +00002791 schedule_delayed_work (&hub->leds, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
2793 status = -ENOTCONN; /* Don't retry */
2794 goto loop_disable;
2795 }
2796 }
2797
2798 /* check for devices running slower than they could */
2799 if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200
2800 && udev->speed == USB_SPEED_FULL
2801 && highspeed_hubs != 0)
2802 check_highspeed (hub, udev, port1);
2803
2804 /* Store the parent's children[] pointer. At this point
2805 * udev becomes globally accessible, although presumably
2806 * no one will look at it until hdev is unlocked.
2807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 status = 0;
2809
2810 /* We mustn't add new devices if the parent hub has
2811 * been disconnected; we would race with the
2812 * recursively_mark_NOTATTACHED() routine.
2813 */
2814 spin_lock_irq(&device_state_lock);
2815 if (hdev->state == USB_STATE_NOTATTACHED)
2816 status = -ENOTCONN;
2817 else
2818 hdev->children[port1-1] = udev;
2819 spin_unlock_irq(&device_state_lock);
2820
2821 /* Run it through the hoops (find a driver, etc) */
2822 if (!status) {
2823 status = usb_new_device(udev);
2824 if (status) {
2825 spin_lock_irq(&device_state_lock);
2826 hdev->children[port1-1] = NULL;
2827 spin_unlock_irq(&device_state_lock);
2828 }
2829 }
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (status)
2832 goto loop_disable;
2833
2834 status = hub_power_remaining(hub);
2835 if (status)
Alan Stern55c52712005-11-23 12:03:12 -05002836 dev_dbg(hub_dev, "%dmA power budget left\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 return;
2839
2840loop_disable:
2841 hub_port_disable(hub, port1, 1);
2842loop:
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07002843 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 release_address(udev);
2845 usb_put_dev(udev);
Vikram Panditaffcdc182007-05-25 21:31:07 -07002846 if ((status == -ENOTCONN) || (status == -ENOTSUPP))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 break;
2848 }
Alan Stern3a311552008-05-20 16:58:29 -04002849 if (hub->hdev->parent ||
2850 !hcd->driver->port_handed_over ||
2851 !(hcd->driver->port_handed_over)(hcd, port1))
2852 dev_err(hub_dev, "unable to enumerate USB device on port %d\n",
2853 port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855done:
2856 hub_port_disable(hub, port1, 1);
Balaji Rao90da0962007-11-22 01:58:14 +05302857 if (hcd->driver->relinquish_port && !hub->hdev->parent)
2858 hcd->driver->relinquish_port(hcd, port1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
2860
2861static void hub_events(void)
2862{
2863 struct list_head *tmp;
2864 struct usb_device *hdev;
2865 struct usb_interface *intf;
2866 struct usb_hub *hub;
2867 struct device *hub_dev;
2868 u16 hubstatus;
2869 u16 hubchange;
2870 u16 portstatus;
2871 u16 portchange;
2872 int i, ret;
2873 int connect_change;
2874
2875 /*
2876 * We restart the list every time to avoid a deadlock with
2877 * deleting hubs downstream from this one. This should be
2878 * safe since we delete the hub from the event list.
2879 * Not the most efficient, but avoids deadlocks.
2880 */
2881 while (1) {
2882
2883 /* Grab the first entry at the beginning of the list */
2884 spin_lock_irq(&hub_event_lock);
2885 if (list_empty(&hub_event_list)) {
2886 spin_unlock_irq(&hub_event_lock);
2887 break;
2888 }
2889
2890 tmp = hub_event_list.next;
2891 list_del_init(tmp);
2892
2893 hub = list_entry(tmp, struct usb_hub, event_list);
Alan Sterne8054852007-05-04 11:55:11 -04002894 kref_get(&hub->kref);
2895 spin_unlock_irq(&hub_event_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Alan Sterne8054852007-05-04 11:55:11 -04002897 hdev = hub->hdev;
2898 hub_dev = hub->intfdev;
2899 intf = to_usb_interface(hub_dev);
Alan Stern40f122f2006-11-09 14:44:33 -05002900 dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 hdev->state, hub->descriptor
2902 ? hub->descriptor->bNbrPorts
2903 : 0,
2904 /* NOTE: expects max 15 ports... */
2905 (u16) hub->change_bits[0],
Alan Stern40f122f2006-11-09 14:44:33 -05002906 (u16) hub->event_bits[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /* Lock the device, then check to see if we were
2909 * disconnected while waiting for the lock to succeed. */
Alan Stern06b84e82007-05-04 11:54:50 -04002910 usb_lock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04002911 if (unlikely(hub->disconnected))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 goto loop;
2913
2914 /* If the hub has died, clean up after it */
2915 if (hdev->state == USB_STATE_NOTATTACHED) {
Alan Stern7de18d82006-06-01 13:37:24 -04002916 hub->error = -ENODEV;
Alan Stern3eb14912008-03-03 15:15:43 -05002917 hub_stop(hub);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 goto loop;
2919 }
2920
Alan Stern40f122f2006-11-09 14:44:33 -05002921 /* Autoresume */
2922 ret = usb_autopm_get_interface(intf);
2923 if (ret) {
2924 dev_dbg(hub_dev, "Can't autoresume: %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 goto loop;
Alan Stern40f122f2006-11-09 14:44:33 -05002926 }
2927
2928 /* If this is an inactive hub, do nothing */
2929 if (hub->quiescing)
2930 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 if (hub->error) {
2933 dev_dbg (hub_dev, "resetting for error %d\n",
2934 hub->error);
2935
Alan Stern7de18d82006-06-01 13:37:24 -04002936 ret = usb_reset_composite_device(hdev, intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (ret) {
2938 dev_dbg (hub_dev,
2939 "error resetting hub: %d\n", ret);
Alan Stern40f122f2006-11-09 14:44:33 -05002940 goto loop_autopm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
2942
2943 hub->nerrors = 0;
2944 hub->error = 0;
2945 }
2946
2947 /* deal with port status changes */
2948 for (i = 1; i <= hub->descriptor->bNbrPorts; i++) {
2949 if (test_bit(i, hub->busy_bits))
2950 continue;
2951 connect_change = test_bit(i, hub->change_bits);
2952 if (!test_and_clear_bit(i, hub->event_bits) &&
Alan Stern6ee0b272008-04-28 11:06:42 -04002953 !connect_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 continue;
2955
2956 ret = hub_port_status(hub, i,
2957 &portstatus, &portchange);
2958 if (ret < 0)
2959 continue;
2960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (portchange & USB_PORT_STAT_C_CONNECTION) {
2962 clear_port_feature(hdev, i,
2963 USB_PORT_FEAT_C_CONNECTION);
2964 connect_change = 1;
2965 }
2966
2967 if (portchange & USB_PORT_STAT_C_ENABLE) {
2968 if (!connect_change)
2969 dev_dbg (hub_dev,
2970 "port %d enable change, "
2971 "status %08x\n",
2972 i, portstatus);
2973 clear_port_feature(hdev, i,
2974 USB_PORT_FEAT_C_ENABLE);
2975
2976 /*
2977 * EM interference sometimes causes badly
2978 * shielded USB devices to be shutdown by
2979 * the hub, this hack enables them again.
2980 * Works at least with mouse driver.
2981 */
2982 if (!(portstatus & USB_PORT_STAT_ENABLE)
2983 && !connect_change
2984 && hdev->children[i-1]) {
2985 dev_err (hub_dev,
2986 "port %i "
2987 "disabled by hub (EMI?), "
2988 "re-enabling...\n",
2989 i);
2990 connect_change = 1;
2991 }
2992 }
2993
2994 if (portchange & USB_PORT_STAT_C_SUSPEND) {
Alan Stern8808f002008-04-28 11:06:55 -04002995 struct usb_device *udev;
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 clear_port_feature(hdev, i,
2998 USB_PORT_FEAT_C_SUSPEND);
Alan Stern8808f002008-04-28 11:06:55 -04002999 udev = hdev->children[i-1];
3000 if (udev) {
3001 usb_lock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 ret = remote_wakeup(hdev->
3003 children[i-1]);
Alan Stern8808f002008-04-28 11:06:55 -04003004 usb_unlock_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (ret < 0)
3006 connect_change = 1;
3007 } else {
3008 ret = -ENODEV;
3009 hub_port_disable(hub, i, 1);
3010 }
3011 dev_dbg (hub_dev,
3012 "resume on port %d, status %d\n",
3013 i, ret);
3014 }
3015
3016 if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
3017 dev_err (hub_dev,
3018 "over-current change on port %d\n",
3019 i);
3020 clear_port_feature(hdev, i,
3021 USB_PORT_FEAT_C_OVER_CURRENT);
3022 hub_power_on(hub);
3023 }
3024
3025 if (portchange & USB_PORT_STAT_C_RESET) {
3026 dev_dbg (hub_dev,
3027 "reset change on port %d\n",
3028 i);
3029 clear_port_feature(hdev, i,
3030 USB_PORT_FEAT_C_RESET);
3031 }
3032
3033 if (connect_change)
3034 hub_port_connect_change(hub, i,
3035 portstatus, portchange);
3036 } /* end for i */
3037
3038 /* deal with hub status changes */
3039 if (test_and_clear_bit(0, hub->event_bits) == 0)
3040 ; /* do nothing */
3041 else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
3042 dev_err (hub_dev, "get_hub_status failed\n");
3043 else {
3044 if (hubchange & HUB_CHANGE_LOCAL_POWER) {
3045 dev_dbg (hub_dev, "power change\n");
3046 clear_hub_feature(hdev, C_HUB_LOCAL_POWER);
Alan Stern55c52712005-11-23 12:03:12 -05003047 if (hubstatus & HUB_STATUS_LOCAL_POWER)
3048 /* FIXME: Is this always true? */
Alan Stern55c52712005-11-23 12:03:12 -05003049 hub->limited_power = 1;
jidong xiao403fae72007-09-14 00:08:51 +08003050 else
3051 hub->limited_power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
3053 if (hubchange & HUB_CHANGE_OVERCURRENT) {
3054 dev_dbg (hub_dev, "overcurrent change\n");
3055 msleep(500); /* Cool down */
3056 clear_hub_feature(hdev, C_HUB_OVER_CURRENT);
3057 hub_power_on(hub);
3058 }
3059 }
3060
Linus Torvalds09ca8ad2008-07-06 10:27:25 -07003061 /* If this is a root hub, tell the HCD it's okay to
3062 * re-enable port-change interrupts now. */
3063 if (!hdev->parent && !hub->busy_bits[0])
3064 usb_enable_root_hub_irq(hdev->bus);
3065
Alan Stern40f122f2006-11-09 14:44:33 -05003066loop_autopm:
3067 /* Allow autosuspend if we're not going to run again */
3068 if (list_empty(&hub->event_list))
3069 usb_autopm_enable(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070loop:
3071 usb_unlock_device(hdev);
Alan Sterne8054852007-05-04 11:55:11 -04003072 kref_put(&hub->kref, hub_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
3074 } /* end while (1) */
3075}
3076
3077static int hub_thread(void *__unused)
3078{
Alan Stern3bb1af52008-03-03 15:15:36 -05003079 /* khubd needs to be freezable to avoid intefering with USB-PERSIST
3080 * port handover. Otherwise it might see that a full-speed device
3081 * was gone before the EHCI controller had handed its port over to
3082 * the companion full-speed controller.
3083 */
Rafael J. Wysocki83144182007-07-17 04:03:35 -07003084 set_freezable();
Alan Stern3bb1af52008-03-03 15:15:36 -05003085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 do {
3087 hub_events();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -07003088 wait_event_freezable(khubd_wait,
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07003089 !list_empty(&hub_event_list) ||
3090 kthread_should_stop());
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07003091 } while (!kthread_should_stop() || !list_empty(&hub_event_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07003093 pr_debug("%s: khubd exiting\n", usbcore_name);
3094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
3097static struct usb_device_id hub_id_table [] = {
3098 { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
3099 .bDeviceClass = USB_CLASS_HUB},
3100 { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
3101 .bInterfaceClass = USB_CLASS_HUB},
3102 { } /* Terminating entry */
3103};
3104
3105MODULE_DEVICE_TABLE (usb, hub_id_table);
3106
3107static struct usb_driver hub_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .name = "hub",
3109 .probe = hub_probe,
3110 .disconnect = hub_disconnect,
3111 .suspend = hub_suspend,
3112 .resume = hub_resume,
Alan Sternf07600c2007-05-30 15:38:16 -04003113 .reset_resume = hub_reset_resume,
Alan Stern7de18d82006-06-01 13:37:24 -04003114 .pre_reset = hub_pre_reset,
3115 .post_reset = hub_post_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 .ioctl = hub_ioctl,
3117 .id_table = hub_id_table,
Alan Stern40f122f2006-11-09 14:44:33 -05003118 .supports_autosuspend = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119};
3120
3121int usb_hub_init(void)
3122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (usb_register(&hub_driver) < 0) {
3124 printk(KERN_ERR "%s: can't register hub driver\n",
3125 usbcore_name);
3126 return -1;
3127 }
3128
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07003129 khubd_task = kthread_run(hub_thread, NULL, "khubd");
3130 if (!IS_ERR(khubd_task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133 /* Fall through if kernel_thread failed */
3134 usb_deregister(&hub_driver);
3135 printk(KERN_ERR "%s: can't start khubd\n", usbcore_name);
3136
3137 return -1;
3138}
3139
3140void usb_hub_cleanup(void)
3141{
akpm@osdl.org9c8d6172005-06-20 14:29:58 -07003142 kthread_stop(khubd_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144 /*
3145 * Hub resources are freed for us by usb_deregister. It calls
3146 * usb_driver_purge on every device which in turn calls that
3147 * devices disconnect function if it is using this driver.
3148 * The hub_disconnect function takes care of releasing the
3149 * individual hub resources. -greg
3150 */
3151 usb_deregister(&hub_driver);
3152} /* usb_hub_cleanup() */
3153
Alan Sterneb764c42008-03-03 15:16:04 -05003154static int descriptors_changed(struct usb_device *udev,
3155 struct usb_device_descriptor *old_device_descriptor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Alan Sterneb764c42008-03-03 15:16:04 -05003157 int changed = 0;
3158 unsigned index;
3159 unsigned serial_len = 0;
3160 unsigned len;
3161 unsigned old_length;
3162 int length;
3163 char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164
Alan Sterneb764c42008-03-03 15:16:04 -05003165 if (memcmp(&udev->descriptor, old_device_descriptor,
3166 sizeof(*old_device_descriptor)) != 0)
3167 return 1;
3168
3169 /* Since the idVendor, idProduct, and bcdDevice values in the
3170 * device descriptor haven't changed, we will assume the
3171 * Manufacturer and Product strings haven't changed either.
3172 * But the SerialNumber string could be different (e.g., a
3173 * different flash card of the same brand).
3174 */
3175 if (udev->serial)
3176 serial_len = strlen(udev->serial) + 1;
3177
3178 len = serial_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05003180 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
3181 len = max(len, old_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
Alan Sterneb764c42008-03-03 15:16:04 -05003183
Oliver Neukum0cc1a512008-01-10 10:31:48 +01003184 buf = kmalloc(len, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 if (buf == NULL) {
3186 dev_err(&udev->dev, "no mem to re-read configs after reset\n");
3187 /* assume the worst */
3188 return 1;
3189 }
3190 for (index = 0; index < udev->descriptor.bNumConfigurations; index++) {
Alan Sterneb764c42008-03-03 15:16:04 -05003191 old_length = le16_to_cpu(udev->config[index].desc.wTotalLength);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf,
3193 old_length);
Alan Sterneb764c42008-03-03 15:16:04 -05003194 if (length != old_length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 dev_dbg(&udev->dev, "config index %d, error %d\n",
3196 index, length);
Alan Sterneb764c42008-03-03 15:16:04 -05003197 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 break;
3199 }
3200 if (memcmp (buf, udev->rawdescriptors[index], old_length)
3201 != 0) {
3202 dev_dbg(&udev->dev, "config index %d changed (#%d)\n",
Alan Sterneb764c42008-03-03 15:16:04 -05003203 index,
3204 ((struct usb_config_descriptor *) buf)->
3205 bConfigurationValue);
3206 changed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 break;
3208 }
3209 }
Alan Sterneb764c42008-03-03 15:16:04 -05003210
3211 if (!changed && serial_len) {
3212 length = usb_string(udev, udev->descriptor.iSerialNumber,
3213 buf, serial_len);
3214 if (length + 1 != serial_len) {
3215 dev_dbg(&udev->dev, "serial string error %d\n",
3216 length);
3217 changed = 1;
3218 } else if (memcmp(buf, udev->serial, length) != 0) {
3219 dev_dbg(&udev->dev, "serial string changed\n");
3220 changed = 1;
3221 }
3222 }
3223
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 kfree(buf);
Alan Sterneb764c42008-03-03 15:16:04 -05003225 return changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
3228/**
3229 * usb_reset_device - perform a USB port reset to reinitialize a device
3230 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
3231 *
Alan Stern79efa092006-06-01 13:33:42 -04003232 * WARNING - don't use this routine to reset a composite device
3233 * (one with multiple interfaces owned by separate drivers)!
3234 * Use usb_reset_composite_device() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 *
3236 * Do a port reset, reassign the device's address, and establish its
3237 * former operating configuration. If the reset fails, or the device's
3238 * descriptors change from their values before the reset, or the original
3239 * configuration and altsettings cannot be restored, a flag will be set
3240 * telling khubd to pretend the device has been disconnected and then
3241 * re-connected. All drivers will be unbound, and the device will be
3242 * re-enumerated and probed all over again.
3243 *
3244 * Returns 0 if the reset succeeded, -ENODEV if the device has been
3245 * flagged for logical disconnection, or some other negative error code
3246 * if the reset wasn't even attempted.
3247 *
3248 * The caller must own the device lock. For example, it's safe to use
3249 * this from a driver probe() routine after downloading new firmware.
3250 * For calls that might not occur during probe(), drivers should lock
3251 * the device using usb_lock_device_for_reset().
Alan Stern6bc6cff2007-05-04 11:53:03 -04003252 *
3253 * Locking exception: This routine may also be called from within an
3254 * autoresume handler. Such usage won't conflict with other tasks
3255 * holding the device lock because these tasks should always call
3256 * usb_autopm_resume_device(), thereby preventing any unwanted autoresume.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 */
3258int usb_reset_device(struct usb_device *udev)
3259{
3260 struct usb_device *parent_hdev = udev->parent;
3261 struct usb_hub *parent_hub;
3262 struct usb_device_descriptor descriptor = udev->descriptor;
Alan Stern12c3da32005-11-23 12:09:52 -05003263 int i, ret = 0;
3264 int port1 = udev->portnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 if (udev->state == USB_STATE_NOTATTACHED ||
3267 udev->state == USB_STATE_SUSPENDED) {
3268 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
3269 udev->state);
3270 return -EINVAL;
3271 }
3272
3273 if (!parent_hdev) {
3274 /* this requires hcd-specific logic; see OHCI hc_restart() */
Harvey Harrison441b62c2008-03-03 16:08:34 -08003275 dev_dbg(&udev->dev, "%s for root hub!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return -EISDIR;
3277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 parent_hub = hdev_to_hub(parent_hdev);
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 set_bit(port1, parent_hub->busy_bits);
3281 for (i = 0; i < SET_CONFIG_TRIES; ++i) {
3282
3283 /* ep0 maxpacket size may change; let the HCD know about it.
3284 * Other endpoints will be handled by re-enumeration. */
Inaky Perez-Gonzalezfc721f52008-04-08 13:24:46 -07003285 usb_ep0_reinit(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 ret = hub_port_init(parent_hub, udev, port1, i);
Alan Sterndd4dd192007-05-04 11:55:54 -04003287 if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 break;
3289 }
3290 clear_bit(port1, parent_hub->busy_bits);
Linus Torvalds09ca8ad2008-07-06 10:27:25 -07003291 if (!parent_hdev->parent && !parent_hub->busy_bits[0])
3292 usb_enable_root_hub_irq(parent_hdev->bus);
Alan Sternd5cbad42006-08-11 16:52:39 -04003293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 if (ret < 0)
3295 goto re_enumerate;
3296
3297 /* Device might have changed firmware (DFU or similar) */
Alan Sterneb764c42008-03-03 15:16:04 -05003298 if (descriptors_changed(udev, &descriptor)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 dev_info(&udev->dev, "device firmware changed\n");
3300 udev->descriptor = descriptor; /* for disconnect() calls */
3301 goto re_enumerate;
3302 }
3303
3304 if (!udev->actconfig)
3305 goto done;
3306
3307 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
3308 USB_REQ_SET_CONFIGURATION, 0,
3309 udev->actconfig->desc.bConfigurationValue, 0,
3310 NULL, 0, USB_CTRL_SET_TIMEOUT);
3311 if (ret < 0) {
3312 dev_err(&udev->dev,
3313 "can't restore configuration #%d (error=%d)\n",
3314 udev->actconfig->desc.bConfigurationValue, ret);
3315 goto re_enumerate;
3316 }
3317 usb_set_device_state(udev, USB_STATE_CONFIGURED);
3318
3319 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
3320 struct usb_interface *intf = udev->actconfig->interface[i];
3321 struct usb_interface_descriptor *desc;
3322
3323 /* set_interface resets host side toggle even
3324 * for altsetting zero. the interface may have no driver.
3325 */
3326 desc = &intf->cur_altsetting->desc;
3327 ret = usb_set_interface(udev, desc->bInterfaceNumber,
3328 desc->bAlternateSetting);
3329 if (ret < 0) {
3330 dev_err(&udev->dev, "failed to restore interface %d "
3331 "altsetting %d (error=%d)\n",
3332 desc->bInterfaceNumber,
3333 desc->bAlternateSetting,
3334 ret);
3335 goto re_enumerate;
3336 }
3337 }
3338
3339done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return 0;
3341
3342re_enumerate:
3343 hub_port_logical_disconnect(parent_hub, port1);
3344 return -ENODEV;
3345}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06003346EXPORT_SYMBOL_GPL(usb_reset_device);
Alan Stern79efa092006-06-01 13:33:42 -04003347
3348/**
3349 * usb_reset_composite_device - warn interface drivers and perform a USB port reset
3350 * @udev: device to reset (not in SUSPENDED or NOTATTACHED state)
3351 * @iface: interface bound to the driver making the request (optional)
3352 *
3353 * Warns all drivers bound to registered interfaces (using their pre_reset
3354 * method), performs the port reset, and then lets the drivers know that
3355 * the reset is over (using their post_reset method).
3356 *
3357 * Return value is the same as for usb_reset_device().
3358 *
3359 * The caller must own the device lock. For example, it's safe to use
3360 * this from a driver probe() routine after downloading new firmware.
3361 * For calls that might not occur during probe(), drivers should lock
3362 * the device using usb_lock_device_for_reset().
Alan Stern79efa092006-06-01 13:33:42 -04003363 */
3364int usb_reset_composite_device(struct usb_device *udev,
3365 struct usb_interface *iface)
3366{
3367 int ret;
Alan Stern852c4b42007-12-03 15:44:29 -05003368 int i;
Alan Stern79efa092006-06-01 13:33:42 -04003369 struct usb_host_config *config = udev->actconfig;
3370
3371 if (udev->state == USB_STATE_NOTATTACHED ||
3372 udev->state == USB_STATE_SUSPENDED) {
3373 dev_dbg(&udev->dev, "device reset not allowed in state %d\n",
3374 udev->state);
3375 return -EINVAL;
3376 }
3377
Alan Stern645daaa2006-08-30 15:47:02 -04003378 /* Prevent autosuspend during the reset */
Alan Stern94fcda12006-11-20 11:38:46 -05003379 usb_autoresume_device(udev);
Alan Stern645daaa2006-08-30 15:47:02 -04003380
Alan Stern79efa092006-06-01 13:33:42 -04003381 if (iface && iface->condition != USB_INTERFACE_BINDING)
3382 iface = NULL;
3383
3384 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04003385 for (i = 0; i < config->desc.bNumInterfaces; ++i) {
Alan Stern852c4b42007-12-03 15:44:29 -05003386 struct usb_interface *cintf = config->interface[i];
3387 struct usb_driver *drv;
3388
3389 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04003390 drv = to_usb_driver(cintf->dev.driver);
3391 if (drv->pre_reset)
3392 (drv->pre_reset)(cintf);
Alan Sternf07600c2007-05-30 15:38:16 -04003393 /* FIXME: Unbind if pre_reset returns an error or isn't defined */
Alan Stern79efa092006-06-01 13:33:42 -04003394 }
3395 }
3396 }
3397
3398 ret = usb_reset_device(udev);
3399
3400 if (config) {
Alan Stern79efa092006-06-01 13:33:42 -04003401 for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) {
Alan Stern852c4b42007-12-03 15:44:29 -05003402 struct usb_interface *cintf = config->interface[i];
3403 struct usb_driver *drv;
3404
3405 if (cintf->dev.driver) {
Alan Stern79efa092006-06-01 13:33:42 -04003406 drv = to_usb_driver(cintf->dev.driver);
3407 if (drv->post_reset)
Alan Sternf07600c2007-05-30 15:38:16 -04003408 (drv->post_reset)(cintf);
3409 /* FIXME: Unbind if post_reset returns an error or isn't defined */
Alan Stern79efa092006-06-01 13:33:42 -04003410 }
Alan Stern79efa092006-06-01 13:33:42 -04003411 }
3412 }
3413
Alan Stern94fcda12006-11-20 11:38:46 -05003414 usb_autosuspend_device(udev);
Alan Stern79efa092006-06-01 13:33:42 -04003415 return ret;
3416}
Greg Kroah-Hartman782e70c2008-01-25 11:12:21 -06003417EXPORT_SYMBOL_GPL(usb_reset_composite_device);