]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
1da177e4 LT |
11 | #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> | |
1da177e4 LT |
19 | #include <linux/ioctl.h> |
20 | #include <linux/usb.h> | |
21 | #include <linux/usbdevice_fs.h> | |
27729aad | 22 | #include <linux/usb/hcd.h> |
93362a87 | 23 | #include <linux/usb/quirks.h> |
9c8d6178 | 24 | #include <linux/kthread.h> |
4186ecf8 | 25 | #include <linux/mutex.h> |
7dfb7103 | 26 | #include <linux/freezer.h> |
1da177e4 | 27 | |
1da177e4 LT |
28 | #include <asm/uaccess.h> |
29 | #include <asm/byteorder.h> | |
30 | ||
31 | #include "usb.h" | |
1da177e4 | 32 | |
f2a383e4 GKH |
33 | /* 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 | ||
1bb5f66b AS |
40 | struct usb_hub { |
41 | struct device *intfdev; /* the "interface" device */ | |
42 | struct usb_device *hdev; | |
e8054854 | 43 | struct kref kref; |
1bb5f66b AS |
44 | struct urb *urb; /* for interrupt polling pipe */ |
45 | ||
46 | /* buffer for urb ... with extra space in case of babble */ | |
47 | char (*buffer)[8]; | |
1bb5f66b AS |
48 | union { |
49 | struct usb_hub_status hub; | |
50 | struct usb_port_status port; | |
51 | } *status; /* buffer for status reports */ | |
db90e7a1 | 52 | struct mutex status_mutex; /* for the status buffer */ |
1bb5f66b AS |
53 | |
54 | int error; /* last reported error */ | |
55 | int nerrors; /* track consecutive errors */ | |
56 | ||
57 | struct list_head event_list; /* hubs w/data or errs ready */ | |
58 | unsigned long event_bits[1]; /* status change bitmask */ | |
59 | unsigned long change_bits[1]; /* ports with logical connect | |
60 | status change */ | |
61 | unsigned long busy_bits[1]; /* ports being reset or | |
62 | resumed */ | |
253e0572 AS |
63 | unsigned long removed_bits[1]; /* ports with a "removed" |
64 | device present */ | |
4ee823b8 SS |
65 | unsigned long wakeup_bits[1]; /* ports that have signaled |
66 | remote wakeup */ | |
1bb5f66b AS |
67 | #if USB_MAXCHILDREN > 31 /* 8*sizeof(unsigned long) - 1 */ |
68 | #error event_bits[] is too short! | |
69 | #endif | |
70 | ||
71 | struct usb_hub_descriptor *descriptor; /* class descriptor */ | |
72 | struct usb_tt tt; /* Transaction Translator */ | |
73 | ||
74 | unsigned mA_per_port; /* current for each child */ | |
75 | ||
76 | unsigned limited_power:1; | |
77 | unsigned quiescing:1; | |
e8054854 | 78 | unsigned disconnected:1; |
1bb5f66b AS |
79 | |
80 | unsigned has_indicators:1; | |
81 | u8 indicator[USB_MAXCHILDREN]; | |
6d5aefb8 | 82 | struct delayed_work leds; |
8520f380 | 83 | struct delayed_work init_work; |
304f0b24 | 84 | void **port_owners; |
1bb5f66b AS |
85 | }; |
86 | ||
dbe79bbe JY |
87 | static inline int hub_is_superspeed(struct usb_device *hdev) |
88 | { | |
7bf01185 | 89 | return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS); |
dbe79bbe | 90 | } |
1bb5f66b | 91 | |
fa286188 | 92 | /* Protect struct usb_device->state and ->children members |
9ad3d6cc | 93 | * Note: Both are also protected by ->dev.sem, except that ->state can |
1da177e4 LT |
94 | * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ |
95 | static DEFINE_SPINLOCK(device_state_lock); | |
96 | ||
97 | /* khubd's worklist and its lock */ | |
98 | static DEFINE_SPINLOCK(hub_event_lock); | |
99 | static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ | |
100 | ||
101 | /* Wakes up khubd */ | |
102 | static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); | |
103 | ||
9c8d6178 | 104 | static struct task_struct *khubd_task; |
1da177e4 LT |
105 | |
106 | /* cycle leds on hubs that aren't blinking for attention */ | |
90ab5ee9 | 107 | static bool blinkenlights = 0; |
1da177e4 LT |
108 | module_param (blinkenlights, bool, S_IRUGO); |
109 | MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); | |
110 | ||
fd7c519d JK |
111 | /* |
112 | * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about | |
113 | * 10 seconds to send reply for the initial 64-byte descriptor request. | |
114 | */ | |
115 | /* define initial 64-byte descriptor request timeout in milliseconds */ | |
116 | static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; | |
117 | module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); | |
b9cef6c3 WF |
118 | MODULE_PARM_DESC(initial_descriptor_timeout, |
119 | "initial 64-byte descriptor request timeout in milliseconds " | |
120 | "(default 5000 - 5.0 seconds)"); | |
fd7c519d | 121 | |
1da177e4 LT |
122 | /* |
123 | * As of 2.6.10 we introduce a new USB device initialization scheme which | |
124 | * closely resembles the way Windows works. Hopefully it will be compatible | |
125 | * with a wider range of devices than the old scheme. However some previously | |
126 | * working devices may start giving rise to "device not accepting address" | |
127 | * errors; if that happens the user can try the old scheme by adjusting the | |
128 | * following module parameters. | |
129 | * | |
130 | * For maximum flexibility there are two boolean parameters to control the | |
131 | * hub driver's behavior. On the first initialization attempt, if the | |
132 | * "old_scheme_first" parameter is set then the old scheme will be used, | |
133 | * otherwise the new scheme is used. If that fails and "use_both_schemes" | |
134 | * is set, then the driver will make another attempt, using the other scheme. | |
135 | */ | |
90ab5ee9 | 136 | static bool old_scheme_first = 0; |
1da177e4 LT |
137 | module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); |
138 | MODULE_PARM_DESC(old_scheme_first, | |
139 | "start with the old device initialization scheme"); | |
140 | ||
90ab5ee9 | 141 | static bool use_both_schemes = 1; |
1da177e4 LT |
142 | module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); |
143 | MODULE_PARM_DESC(use_both_schemes, | |
144 | "try the other device initialization scheme if the " | |
145 | "first one fails"); | |
146 | ||
32fe0198 AS |
147 | /* Mutual exclusion for EHCI CF initialization. This interferes with |
148 | * port reset on some companion controllers. | |
149 | */ | |
150 | DECLARE_RWSEM(ehci_cf_port_reset_rwsem); | |
151 | EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); | |
152 | ||
948fea37 AS |
153 | #define HUB_DEBOUNCE_TIMEOUT 1500 |
154 | #define HUB_DEBOUNCE_STEP 25 | |
155 | #define HUB_DEBOUNCE_STABLE 100 | |
156 | ||
1da177e4 | 157 | |
742120c6 ML |
158 | static int usb_reset_and_verify_device(struct usb_device *udev); |
159 | ||
131dec34 | 160 | static inline char *portspeed(struct usb_hub *hub, int portstatus) |
1da177e4 | 161 | { |
131dec34 SS |
162 | if (hub_is_superspeed(hub->hdev)) |
163 | return "5.0 Gb/s"; | |
288ead45 | 164 | if (portstatus & USB_PORT_STAT_HIGH_SPEED) |
1da177e4 | 165 | return "480 Mb/s"; |
288ead45 | 166 | else if (portstatus & USB_PORT_STAT_LOW_SPEED) |
1da177e4 LT |
167 | return "1.5 Mb/s"; |
168 | else | |
169 | return "12 Mb/s"; | |
170 | } | |
1da177e4 LT |
171 | |
172 | /* Note that hdev or one of its children must be locked! */ | |
25118084 | 173 | static struct usb_hub *hdev_to_hub(struct usb_device *hdev) |
1da177e4 | 174 | { |
fa286188 | 175 | if (!hdev || !hdev->actconfig) |
25118084 | 176 | return NULL; |
1da177e4 LT |
177 | return usb_get_intfdata(hdev->actconfig->interface[0]); |
178 | } | |
179 | ||
d9b2099c SS |
180 | static int usb_device_supports_lpm(struct usb_device *udev) |
181 | { | |
182 | /* USB 2.1 (and greater) devices indicate LPM support through | |
183 | * their USB 2.0 Extended Capabilities BOS descriptor. | |
184 | */ | |
185 | if (udev->speed == USB_SPEED_HIGH) { | |
186 | if (udev->bos->ext_cap && | |
187 | (USB_LPM_SUPPORT & | |
188 | le32_to_cpu(udev->bos->ext_cap->bmAttributes))) | |
189 | return 1; | |
190 | return 0; | |
191 | } | |
192 | return 0; | |
193 | } | |
194 | ||
1da177e4 | 195 | /* USB 2.0 spec Section 11.24.4.5 */ |
dbe79bbe | 196 | static int get_hub_descriptor(struct usb_device *hdev, void *data) |
1da177e4 | 197 | { |
dbe79bbe JY |
198 | int i, ret, size; |
199 | unsigned dtype; | |
200 | ||
201 | if (hub_is_superspeed(hdev)) { | |
202 | dtype = USB_DT_SS_HUB; | |
203 | size = USB_DT_SS_HUB_SIZE; | |
204 | } else { | |
205 | dtype = USB_DT_HUB; | |
206 | size = sizeof(struct usb_hub_descriptor); | |
207 | } | |
1da177e4 LT |
208 | |
209 | for (i = 0; i < 3; i++) { | |
210 | ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), | |
211 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, | |
dbe79bbe | 212 | dtype << 8, 0, data, size, |
1da177e4 LT |
213 | USB_CTRL_GET_TIMEOUT); |
214 | if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) | |
215 | return ret; | |
216 | } | |
217 | return -EINVAL; | |
218 | } | |
219 | ||
220 | /* | |
221 | * USB 2.0 spec Section 11.24.2.1 | |
222 | */ | |
223 | static int clear_hub_feature(struct usb_device *hdev, int feature) | |
224 | { | |
225 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
226 | USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); | |
227 | } | |
228 | ||
229 | /* | |
230 | * USB 2.0 spec Section 11.24.2.2 | |
231 | */ | |
232 | static int clear_port_feature(struct usb_device *hdev, int port1, int feature) | |
233 | { | |
234 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
235 | USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, | |
236 | NULL, 0, 1000); | |
237 | } | |
238 | ||
239 | /* | |
240 | * USB 2.0 spec Section 11.24.2.13 | |
241 | */ | |
242 | static int set_port_feature(struct usb_device *hdev, int port1, int feature) | |
243 | { | |
244 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
245 | USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, | |
246 | NULL, 0, 1000); | |
247 | } | |
248 | ||
249 | /* | |
250 | * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 | |
251 | * for info about using port indicators | |
252 | */ | |
253 | static void set_port_led( | |
254 | struct usb_hub *hub, | |
255 | int port1, | |
256 | int selector | |
257 | ) | |
258 | { | |
259 | int status = set_port_feature(hub->hdev, (selector << 8) | port1, | |
260 | USB_PORT_FEAT_INDICATOR); | |
261 | if (status < 0) | |
262 | dev_dbg (hub->intfdev, | |
263 | "port %d indicator %s status %d\n", | |
264 | port1, | |
265 | ({ char *s; switch (selector) { | |
266 | case HUB_LED_AMBER: s = "amber"; break; | |
267 | case HUB_LED_GREEN: s = "green"; break; | |
268 | case HUB_LED_OFF: s = "off"; break; | |
269 | case HUB_LED_AUTO: s = "auto"; break; | |
270 | default: s = "??"; break; | |
271 | }; s; }), | |
272 | status); | |
273 | } | |
274 | ||
275 | #define LED_CYCLE_PERIOD ((2*HZ)/3) | |
276 | ||
c4028958 | 277 | static void led_work (struct work_struct *work) |
1da177e4 | 278 | { |
c4028958 DH |
279 | struct usb_hub *hub = |
280 | container_of(work, struct usb_hub, leds.work); | |
1da177e4 LT |
281 | struct usb_device *hdev = hub->hdev; |
282 | unsigned i; | |
283 | unsigned changed = 0; | |
284 | int cursor = -1; | |
285 | ||
286 | if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) | |
287 | return; | |
288 | ||
289 | for (i = 0; i < hub->descriptor->bNbrPorts; i++) { | |
290 | unsigned selector, mode; | |
291 | ||
292 | /* 30%-50% duty cycle */ | |
293 | ||
294 | switch (hub->indicator[i]) { | |
295 | /* cycle marker */ | |
296 | case INDICATOR_CYCLE: | |
297 | cursor = i; | |
298 | selector = HUB_LED_AUTO; | |
299 | mode = INDICATOR_AUTO; | |
300 | break; | |
301 | /* blinking green = sw attention */ | |
302 | case INDICATOR_GREEN_BLINK: | |
303 | selector = HUB_LED_GREEN; | |
304 | mode = INDICATOR_GREEN_BLINK_OFF; | |
305 | break; | |
306 | case INDICATOR_GREEN_BLINK_OFF: | |
307 | selector = HUB_LED_OFF; | |
308 | mode = INDICATOR_GREEN_BLINK; | |
309 | break; | |
310 | /* blinking amber = hw attention */ | |
311 | case INDICATOR_AMBER_BLINK: | |
312 | selector = HUB_LED_AMBER; | |
313 | mode = INDICATOR_AMBER_BLINK_OFF; | |
314 | break; | |
315 | case INDICATOR_AMBER_BLINK_OFF: | |
316 | selector = HUB_LED_OFF; | |
317 | mode = INDICATOR_AMBER_BLINK; | |
318 | break; | |
319 | /* blink green/amber = reserved */ | |
320 | case INDICATOR_ALT_BLINK: | |
321 | selector = HUB_LED_GREEN; | |
322 | mode = INDICATOR_ALT_BLINK_OFF; | |
323 | break; | |
324 | case INDICATOR_ALT_BLINK_OFF: | |
325 | selector = HUB_LED_AMBER; | |
326 | mode = INDICATOR_ALT_BLINK; | |
327 | break; | |
328 | default: | |
329 | continue; | |
330 | } | |
331 | if (selector != HUB_LED_AUTO) | |
332 | changed = 1; | |
333 | set_port_led(hub, i + 1, selector); | |
334 | hub->indicator[i] = mode; | |
335 | } | |
336 | if (!changed && blinkenlights) { | |
337 | cursor++; | |
338 | cursor %= hub->descriptor->bNbrPorts; | |
339 | set_port_led(hub, cursor + 1, HUB_LED_GREEN); | |
340 | hub->indicator[cursor] = INDICATOR_CYCLE; | |
341 | changed++; | |
342 | } | |
343 | if (changed) | |
344 | schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); | |
345 | } | |
346 | ||
347 | /* use a short timeout for hub/port status fetches */ | |
348 | #define USB_STS_TIMEOUT 1000 | |
349 | #define USB_STS_RETRIES 5 | |
350 | ||
351 | /* | |
352 | * USB 2.0 spec Section 11.24.2.6 | |
353 | */ | |
354 | static int get_hub_status(struct usb_device *hdev, | |
355 | struct usb_hub_status *data) | |
356 | { | |
357 | int i, status = -ETIMEDOUT; | |
358 | ||
3824c1dd LP |
359 | for (i = 0; i < USB_STS_RETRIES && |
360 | (status == -ETIMEDOUT || status == -EPIPE); i++) { | |
1da177e4 LT |
361 | status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
362 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, | |
363 | data, sizeof(*data), USB_STS_TIMEOUT); | |
364 | } | |
365 | return status; | |
366 | } | |
367 | ||
368 | /* | |
369 | * USB 2.0 spec Section 11.24.2.7 | |
370 | */ | |
371 | static int get_port_status(struct usb_device *hdev, int port1, | |
372 | struct usb_port_status *data) | |
373 | { | |
374 | int i, status = -ETIMEDOUT; | |
375 | ||
3824c1dd LP |
376 | for (i = 0; i < USB_STS_RETRIES && |
377 | (status == -ETIMEDOUT || status == -EPIPE); i++) { | |
1da177e4 LT |
378 | status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
379 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, | |
380 | data, sizeof(*data), USB_STS_TIMEOUT); | |
381 | } | |
382 | return status; | |
383 | } | |
384 | ||
3eb14915 AS |
385 | static int hub_port_status(struct usb_hub *hub, int port1, |
386 | u16 *status, u16 *change) | |
387 | { | |
388 | int ret; | |
389 | ||
390 | mutex_lock(&hub->status_mutex); | |
391 | ret = get_port_status(hub->hdev, port1, &hub->status->port); | |
392 | if (ret < 4) { | |
393 | dev_err(hub->intfdev, | |
394 | "%s failed (err = %d)\n", __func__, ret); | |
395 | if (ret >= 0) | |
396 | ret = -EIO; | |
397 | } else { | |
398 | *status = le16_to_cpu(hub->status->port.wPortStatus); | |
399 | *change = le16_to_cpu(hub->status->port.wPortChange); | |
dbe79bbe | 400 | |
3eb14915 AS |
401 | ret = 0; |
402 | } | |
403 | mutex_unlock(&hub->status_mutex); | |
404 | return ret; | |
405 | } | |
406 | ||
1da177e4 LT |
407 | static void kick_khubd(struct usb_hub *hub) |
408 | { | |
409 | unsigned long flags; | |
410 | ||
411 | spin_lock_irqsave(&hub_event_lock, flags); | |
2e2c5eea | 412 | if (!hub->disconnected && list_empty(&hub->event_list)) { |
1da177e4 | 413 | list_add_tail(&hub->event_list, &hub_event_list); |
8e4ceb38 AS |
414 | |
415 | /* Suppress autosuspend until khubd runs */ | |
416 | usb_autopm_get_interface_no_resume( | |
417 | to_usb_interface(hub->intfdev)); | |
1da177e4 LT |
418 | wake_up(&khubd_wait); |
419 | } | |
420 | spin_unlock_irqrestore(&hub_event_lock, flags); | |
421 | } | |
422 | ||
423 | void usb_kick_khubd(struct usb_device *hdev) | |
424 | { | |
25118084 AS |
425 | struct usb_hub *hub = hdev_to_hub(hdev); |
426 | ||
427 | if (hub) | |
428 | kick_khubd(hub); | |
1da177e4 LT |
429 | } |
430 | ||
4ee823b8 SS |
431 | /* |
432 | * Let the USB core know that a USB 3.0 device has sent a Function Wake Device | |
433 | * Notification, which indicates it had initiated remote wakeup. | |
434 | * | |
435 | * USB 3.0 hubs do not report the port link state change from U3 to U0 when the | |
436 | * device initiates resume, so the USB core will not receive notice of the | |
437 | * resume through the normal hub interrupt URB. | |
438 | */ | |
439 | void usb_wakeup_notification(struct usb_device *hdev, | |
440 | unsigned int portnum) | |
441 | { | |
442 | struct usb_hub *hub; | |
443 | ||
444 | if (!hdev) | |
445 | return; | |
446 | ||
447 | hub = hdev_to_hub(hdev); | |
448 | if (hub) { | |
449 | set_bit(portnum, hub->wakeup_bits); | |
450 | kick_khubd(hub); | |
451 | } | |
452 | } | |
453 | EXPORT_SYMBOL_GPL(usb_wakeup_notification); | |
1da177e4 LT |
454 | |
455 | /* completion function, fires on port status changes and various faults */ | |
7d12e780 | 456 | static void hub_irq(struct urb *urb) |
1da177e4 | 457 | { |
ec17cf1c | 458 | struct usb_hub *hub = urb->context; |
e015268d | 459 | int status = urb->status; |
71d2718f | 460 | unsigned i; |
1da177e4 LT |
461 | unsigned long bits; |
462 | ||
e015268d | 463 | switch (status) { |
1da177e4 LT |
464 | case -ENOENT: /* synchronous unlink */ |
465 | case -ECONNRESET: /* async unlink */ | |
466 | case -ESHUTDOWN: /* hardware going away */ | |
467 | return; | |
468 | ||
469 | default: /* presumably an error */ | |
470 | /* Cause a hub reset after 10 consecutive errors */ | |
e015268d | 471 | dev_dbg (hub->intfdev, "transfer --> %d\n", status); |
1da177e4 LT |
472 | if ((++hub->nerrors < 10) || hub->error) |
473 | goto resubmit; | |
e015268d | 474 | hub->error = status; |
1da177e4 | 475 | /* FALL THROUGH */ |
ec17cf1c | 476 | |
1da177e4 LT |
477 | /* let khubd handle things */ |
478 | case 0: /* we got data: port status changed */ | |
479 | bits = 0; | |
480 | for (i = 0; i < urb->actual_length; ++i) | |
481 | bits |= ((unsigned long) ((*hub->buffer)[i])) | |
482 | << (i*8); | |
483 | hub->event_bits[0] = bits; | |
484 | break; | |
485 | } | |
486 | ||
487 | hub->nerrors = 0; | |
488 | ||
489 | /* Something happened, let khubd figure it out */ | |
490 | kick_khubd(hub); | |
491 | ||
492 | resubmit: | |
493 | if (hub->quiescing) | |
494 | return; | |
495 | ||
496 | if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 | |
497 | && status != -ENODEV && status != -EPERM) | |
498 | dev_err (hub->intfdev, "resubmit --> %d\n", status); | |
499 | } | |
500 | ||
501 | /* USB 2.0 spec Section 11.24.2.3 */ | |
502 | static inline int | |
503 | hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) | |
504 | { | |
c2f6595f | 505 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
1da177e4 LT |
506 | HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, |
507 | tt, NULL, 0, 1000); | |
508 | } | |
509 | ||
510 | /* | |
511 | * enumeration blocks khubd for a long time. we use keventd instead, since | |
512 | * long blocking there is the exception, not the rule. accordingly, HCDs | |
513 | * talking to TTs must queue control transfers (not just bulk and iso), so | |
514 | * both can talk to the same hub concurrently. | |
515 | */ | |
cb88a1b8 | 516 | static void hub_tt_work(struct work_struct *work) |
1da177e4 | 517 | { |
c4028958 | 518 | struct usb_hub *hub = |
cb88a1b8 | 519 | container_of(work, struct usb_hub, tt.clear_work); |
1da177e4 | 520 | unsigned long flags; |
55e5fdfa | 521 | int limit = 100; |
1da177e4 LT |
522 | |
523 | spin_lock_irqsave (&hub->tt.lock, flags); | |
55e5fdfa | 524 | while (--limit && !list_empty (&hub->tt.clear_list)) { |
d0f830d3 | 525 | struct list_head *next; |
1da177e4 LT |
526 | struct usb_tt_clear *clear; |
527 | struct usb_device *hdev = hub->hdev; | |
cb88a1b8 | 528 | const struct hc_driver *drv; |
1da177e4 LT |
529 | int status; |
530 | ||
d0f830d3 HS |
531 | next = hub->tt.clear_list.next; |
532 | clear = list_entry (next, struct usb_tt_clear, clear_list); | |
1da177e4 LT |
533 | list_del (&clear->clear_list); |
534 | ||
535 | /* drop lock so HCD can concurrently report other TT errors */ | |
536 | spin_unlock_irqrestore (&hub->tt.lock, flags); | |
537 | status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); | |
1da177e4 LT |
538 | if (status) |
539 | dev_err (&hdev->dev, | |
540 | "clear tt %d (%04x) error %d\n", | |
541 | clear->tt, clear->devinfo, status); | |
cb88a1b8 AS |
542 | |
543 | /* Tell the HCD, even if the operation failed */ | |
544 | drv = clear->hcd->driver; | |
545 | if (drv->clear_tt_buffer_complete) | |
546 | (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); | |
547 | ||
1bc3c9e1 | 548 | kfree(clear); |
cb88a1b8 | 549 | spin_lock_irqsave(&hub->tt.lock, flags); |
1da177e4 LT |
550 | } |
551 | spin_unlock_irqrestore (&hub->tt.lock, flags); | |
552 | } | |
553 | ||
554 | /** | |
cb88a1b8 AS |
555 | * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub |
556 | * @urb: an URB associated with the failed or incomplete split transaction | |
1da177e4 LT |
557 | * |
558 | * High speed HCDs use this to tell the hub driver that some split control or | |
559 | * bulk transaction failed in a way that requires clearing internal state of | |
560 | * a transaction translator. This is normally detected (and reported) from | |
561 | * interrupt context. | |
562 | * | |
563 | * It may not be possible for that hub to handle additional full (or low) | |
564 | * speed transactions until that state is fully cleared out. | |
565 | */ | |
cb88a1b8 | 566 | int usb_hub_clear_tt_buffer(struct urb *urb) |
1da177e4 | 567 | { |
cb88a1b8 AS |
568 | struct usb_device *udev = urb->dev; |
569 | int pipe = urb->pipe; | |
1da177e4 LT |
570 | struct usb_tt *tt = udev->tt; |
571 | unsigned long flags; | |
572 | struct usb_tt_clear *clear; | |
573 | ||
574 | /* we've got to cope with an arbitrary number of pending TT clears, | |
575 | * since each TT has "at least two" buffers that can need it (and | |
576 | * there can be many TTs per hub). even if they're uncommon. | |
577 | */ | |
54e6ecb2 | 578 | if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) { |
1da177e4 LT |
579 | dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); |
580 | /* FIXME recover somehow ... RESET_TT? */ | |
cb88a1b8 | 581 | return -ENOMEM; |
1da177e4 LT |
582 | } |
583 | ||
584 | /* info that CLEAR_TT_BUFFER needs */ | |
585 | clear->tt = tt->multi ? udev->ttport : 1; | |
586 | clear->devinfo = usb_pipeendpoint (pipe); | |
587 | clear->devinfo |= udev->devnum << 4; | |
588 | clear->devinfo |= usb_pipecontrol (pipe) | |
589 | ? (USB_ENDPOINT_XFER_CONTROL << 11) | |
590 | : (USB_ENDPOINT_XFER_BULK << 11); | |
591 | if (usb_pipein (pipe)) | |
592 | clear->devinfo |= 1 << 15; | |
cb88a1b8 AS |
593 | |
594 | /* info for completion callback */ | |
595 | clear->hcd = bus_to_hcd(udev->bus); | |
596 | clear->ep = urb->ep; | |
597 | ||
1da177e4 LT |
598 | /* tell keventd to clear state for this TT */ |
599 | spin_lock_irqsave (&tt->lock, flags); | |
600 | list_add_tail (&clear->clear_list, &tt->clear_list); | |
cb88a1b8 | 601 | schedule_work(&tt->clear_work); |
1da177e4 | 602 | spin_unlock_irqrestore (&tt->lock, flags); |
cb88a1b8 | 603 | return 0; |
1da177e4 | 604 | } |
cb88a1b8 | 605 | EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); |
1da177e4 | 606 | |
8520f380 AS |
607 | /* If do_delay is false, return the number of milliseconds the caller |
608 | * needs to delay. | |
609 | */ | |
610 | static unsigned hub_power_on(struct usb_hub *hub, bool do_delay) | |
1da177e4 LT |
611 | { |
612 | int port1; | |
b789696a | 613 | unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2; |
8520f380 | 614 | unsigned delay; |
4489a571 AS |
615 | u16 wHubCharacteristics = |
616 | le16_to_cpu(hub->descriptor->wHubCharacteristics); | |
617 | ||
618 | /* Enable power on each port. Some hubs have reserved values | |
619 | * of LPSM (> 2) in their descriptors, even though they are | |
620 | * USB 2.0 hubs. Some hubs do not implement port-power switching | |
621 | * but only emulate it. In all cases, the ports won't work | |
622 | * unless we send these messages to the hub. | |
623 | */ | |
624 | if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) | |
1da177e4 | 625 | dev_dbg(hub->intfdev, "enabling power on all ports\n"); |
4489a571 AS |
626 | else |
627 | dev_dbg(hub->intfdev, "trying to enable port power on " | |
628 | "non-switchable hub\n"); | |
629 | for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) | |
630 | set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); | |
1da177e4 | 631 | |
b789696a | 632 | /* Wait at least 100 msec for power to become stable */ |
8520f380 AS |
633 | delay = max(pgood_delay, (unsigned) 100); |
634 | if (do_delay) | |
635 | msleep(delay); | |
636 | return delay; | |
1da177e4 LT |
637 | } |
638 | ||
1da177e4 LT |
639 | static int hub_hub_status(struct usb_hub *hub, |
640 | u16 *status, u16 *change) | |
641 | { | |
642 | int ret; | |
643 | ||
db90e7a1 | 644 | mutex_lock(&hub->status_mutex); |
1da177e4 LT |
645 | ret = get_hub_status(hub->hdev, &hub->status->hub); |
646 | if (ret < 0) | |
647 | dev_err (hub->intfdev, | |
441b62c1 | 648 | "%s failed (err = %d)\n", __func__, ret); |
1da177e4 LT |
649 | else { |
650 | *status = le16_to_cpu(hub->status->hub.wHubStatus); | |
651 | *change = le16_to_cpu(hub->status->hub.wHubChange); | |
652 | ret = 0; | |
653 | } | |
db90e7a1 | 654 | mutex_unlock(&hub->status_mutex); |
1da177e4 LT |
655 | return ret; |
656 | } | |
657 | ||
8b28c752 AS |
658 | static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) |
659 | { | |
660 | struct usb_device *hdev = hub->hdev; | |
0458d5b4 | 661 | int ret = 0; |
8b28c752 | 662 | |
fa286188 GKH |
663 | if (hdev->children[port1-1] && set_state) |
664 | usb_set_device_state(hdev->children[port1-1], | |
8b28c752 | 665 | USB_STATE_NOTATTACHED); |
dbe79bbe | 666 | if (!hub->error && !hub_is_superspeed(hub->hdev)) |
0458d5b4 | 667 | ret = clear_port_feature(hdev, port1, USB_PORT_FEAT_ENABLE); |
8b28c752 AS |
668 | if (ret) |
669 | dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", | |
0458d5b4 | 670 | port1, ret); |
8b28c752 AS |
671 | return ret; |
672 | } | |
673 | ||
0458d5b4 | 674 | /* |
6d42fcdb | 675 | * Disable a port and mark a logical connect-change event, so that some |
0458d5b4 AS |
676 | * time later khubd will disconnect() any existing usb_device on the port |
677 | * and will re-enumerate if there actually is a device attached. | |
678 | */ | |
679 | static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) | |
680 | { | |
681 | dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); | |
682 | hub_port_disable(hub, port1, 1); | |
7d069b7d | 683 | |
0458d5b4 AS |
684 | /* FIXME let caller ask to power down the port: |
685 | * - some devices won't enumerate without a VBUS power cycle | |
686 | * - SRP saves power that way | |
687 | * - ... new call, TBD ... | |
688 | * That's easy if this hub can switch power per-port, and | |
689 | * khubd reactivates the port later (timer, SRP, etc). | |
690 | * Powerdown must be optional, because of reset/DFU. | |
691 | */ | |
692 | ||
693 | set_bit(port1, hub->change_bits); | |
694 | kick_khubd(hub); | |
695 | } | |
696 | ||
253e0572 AS |
697 | /** |
698 | * usb_remove_device - disable a device's port on its parent hub | |
699 | * @udev: device to be disabled and removed | |
700 | * Context: @udev locked, must be able to sleep. | |
701 | * | |
702 | * After @udev's port has been disabled, khubd is notified and it will | |
703 | * see that the device has been disconnected. When the device is | |
704 | * physically unplugged and something is plugged in, the events will | |
705 | * be received and processed normally. | |
706 | */ | |
707 | int usb_remove_device(struct usb_device *udev) | |
708 | { | |
709 | struct usb_hub *hub; | |
710 | struct usb_interface *intf; | |
711 | ||
712 | if (!udev->parent) /* Can't remove a root hub */ | |
713 | return -EINVAL; | |
714 | hub = hdev_to_hub(udev->parent); | |
715 | intf = to_usb_interface(hub->intfdev); | |
716 | ||
717 | usb_autopm_get_interface(intf); | |
718 | set_bit(udev->portnum, hub->removed_bits); | |
719 | hub_port_logical_disconnect(hub, udev->portnum); | |
720 | usb_autopm_put_interface(intf); | |
721 | return 0; | |
722 | } | |
723 | ||
6ee0b270 | 724 | enum hub_activation_type { |
8e4ceb38 | 725 | HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ |
8520f380 | 726 | HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, |
6ee0b270 | 727 | }; |
5e6effae | 728 | |
8520f380 AS |
729 | static void hub_init_func2(struct work_struct *ws); |
730 | static void hub_init_func3(struct work_struct *ws); | |
731 | ||
f2835219 | 732 | static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) |
5e6effae AS |
733 | { |
734 | struct usb_device *hdev = hub->hdev; | |
653a39d1 SS |
735 | struct usb_hcd *hcd; |
736 | int ret; | |
5e6effae | 737 | int port1; |
f2835219 | 738 | int status; |
948fea37 | 739 | bool need_debounce_delay = false; |
8520f380 AS |
740 | unsigned delay; |
741 | ||
742 | /* Continue a partial initialization */ | |
743 | if (type == HUB_INIT2) | |
744 | goto init2; | |
745 | if (type == HUB_INIT3) | |
746 | goto init3; | |
5e6effae | 747 | |
a45aa3b3 EF |
748 | /* The superspeed hub except for root hub has to use Hub Depth |
749 | * value as an offset into the route string to locate the bits | |
750 | * it uses to determine the downstream port number. So hub driver | |
751 | * should send a set hub depth request to superspeed hub after | |
752 | * the superspeed hub is set configuration in initialization or | |
753 | * reset procedure. | |
754 | * | |
755 | * After a resume, port power should still be on. | |
f2835219 AS |
756 | * For any other type of activation, turn it on. |
757 | */ | |
8520f380 | 758 | if (type != HUB_RESUME) { |
a45aa3b3 EF |
759 | if (hdev->parent && hub_is_superspeed(hdev)) { |
760 | ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
761 | HUB_SET_DEPTH, USB_RT_HUB, | |
762 | hdev->level - 1, 0, NULL, 0, | |
763 | USB_CTRL_SET_TIMEOUT); | |
764 | if (ret < 0) | |
765 | dev_err(hub->intfdev, | |
766 | "set hub depth failed\n"); | |
767 | } | |
8520f380 AS |
768 | |
769 | /* Speed up system boot by using a delayed_work for the | |
770 | * hub's initial power-up delays. This is pretty awkward | |
771 | * and the implementation looks like a home-brewed sort of | |
772 | * setjmp/longjmp, but it saves at least 100 ms for each | |
773 | * root hub (assuming usbcore is compiled into the kernel | |
774 | * rather than as a module). It adds up. | |
775 | * | |
776 | * This can't be done for HUB_RESUME or HUB_RESET_RESUME | |
777 | * because for those activation types the ports have to be | |
778 | * operational when we return. In theory this could be done | |
779 | * for HUB_POST_RESET, but it's easier not to. | |
780 | */ | |
781 | if (type == HUB_INIT) { | |
782 | delay = hub_power_on(hub, false); | |
783 | PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2); | |
784 | schedule_delayed_work(&hub->init_work, | |
785 | msecs_to_jiffies(delay)); | |
61fbeba1 AS |
786 | |
787 | /* Suppress autosuspend until init is done */ | |
8e4ceb38 AS |
788 | usb_autopm_get_interface_no_resume( |
789 | to_usb_interface(hub->intfdev)); | |
8520f380 | 790 | return; /* Continues at init2: below */ |
653a39d1 SS |
791 | } else if (type == HUB_RESET_RESUME) { |
792 | /* The internal host controller state for the hub device | |
793 | * may be gone after a host power loss on system resume. | |
794 | * Update the device's info so the HW knows it's a hub. | |
795 | */ | |
796 | hcd = bus_to_hcd(hdev->bus); | |
797 | if (hcd->driver->update_hub_device) { | |
798 | ret = hcd->driver->update_hub_device(hcd, hdev, | |
799 | &hub->tt, GFP_NOIO); | |
800 | if (ret < 0) { | |
801 | dev_err(hub->intfdev, "Host not " | |
802 | "accepting hub info " | |
803 | "update.\n"); | |
804 | dev_err(hub->intfdev, "LS/FS devices " | |
805 | "and hubs may not work " | |
806 | "under this hub\n."); | |
807 | } | |
808 | } | |
809 | hub_power_on(hub, true); | |
8520f380 AS |
810 | } else { |
811 | hub_power_on(hub, true); | |
812 | } | |
813 | } | |
814 | init2: | |
f2835219 | 815 | |
6ee0b270 AS |
816 | /* Check each port and set hub->change_bits to let khubd know |
817 | * which ports need attention. | |
5e6effae AS |
818 | */ |
819 | for (port1 = 1; port1 <= hdev->maxchild; ++port1) { | |
fa286188 | 820 | struct usb_device *udev = hdev->children[port1-1]; |
5e6effae AS |
821 | u16 portstatus, portchange; |
822 | ||
6ee0b270 AS |
823 | portstatus = portchange = 0; |
824 | status = hub_port_status(hub, port1, &portstatus, &portchange); | |
825 | if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) | |
826 | dev_dbg(hub->intfdev, | |
827 | "port %d: status %04x change %04x\n", | |
828 | port1, portstatus, portchange); | |
829 | ||
830 | /* After anything other than HUB_RESUME (i.e., initialization | |
831 | * or any sort of reset), every port should be disabled. | |
832 | * Unconnected ports should likewise be disabled (paranoia), | |
833 | * and so should ports for which we have no usb_device. | |
834 | */ | |
835 | if ((portstatus & USB_PORT_STAT_ENABLE) && ( | |
836 | type != HUB_RESUME || | |
837 | !(portstatus & USB_PORT_STAT_CONNECTION) || | |
838 | !udev || | |
839 | udev->state == USB_STATE_NOTATTACHED)) { | |
9f0a6cd3 AX |
840 | /* |
841 | * USB3 protocol ports will automatically transition | |
842 | * to Enabled state when detect an USB3.0 device attach. | |
843 | * Do not disable USB3 protocol ports. | |
9f0a6cd3 | 844 | */ |
131dec34 | 845 | if (!hub_is_superspeed(hdev)) { |
9f0a6cd3 AX |
846 | clear_port_feature(hdev, port1, |
847 | USB_PORT_FEAT_ENABLE); | |
848 | portstatus &= ~USB_PORT_STAT_ENABLE; | |
85f0ff46 SS |
849 | } else { |
850 | /* Pretend that power was lost for USB3 devs */ | |
851 | portstatus &= ~USB_PORT_STAT_ENABLE; | |
9f0a6cd3 | 852 | } |
5e6effae AS |
853 | } |
854 | ||
948fea37 AS |
855 | /* Clear status-change flags; we'll debounce later */ |
856 | if (portchange & USB_PORT_STAT_C_CONNECTION) { | |
857 | need_debounce_delay = true; | |
858 | clear_port_feature(hub->hdev, port1, | |
859 | USB_PORT_FEAT_C_CONNECTION); | |
860 | } | |
861 | if (portchange & USB_PORT_STAT_C_ENABLE) { | |
862 | need_debounce_delay = true; | |
863 | clear_port_feature(hub->hdev, port1, | |
864 | USB_PORT_FEAT_C_ENABLE); | |
865 | } | |
79c3dd81 DZ |
866 | if ((portchange & USB_PORT_STAT_C_BH_RESET) && |
867 | hub_is_superspeed(hub->hdev)) { | |
868 | need_debounce_delay = true; | |
869 | clear_port_feature(hub->hdev, port1, | |
870 | USB_PORT_FEAT_C_BH_PORT_RESET); | |
871 | } | |
253e0572 AS |
872 | /* We can forget about a "removed" device when there's a |
873 | * physical disconnect or the connect status changes. | |
874 | */ | |
875 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || | |
876 | (portchange & USB_PORT_STAT_C_CONNECTION)) | |
877 | clear_bit(port1, hub->removed_bits); | |
878 | ||
6ee0b270 AS |
879 | if (!udev || udev->state == USB_STATE_NOTATTACHED) { |
880 | /* Tell khubd to disconnect the device or | |
881 | * check for a new connection | |
882 | */ | |
883 | if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) | |
884 | set_bit(port1, hub->change_bits); | |
885 | ||
886 | } else if (portstatus & USB_PORT_STAT_ENABLE) { | |
72937e1e SS |
887 | bool port_resumed = (portstatus & |
888 | USB_PORT_STAT_LINK_STATE) == | |
889 | USB_SS_PORT_LS_U0; | |
6ee0b270 AS |
890 | /* The power session apparently survived the resume. |
891 | * If there was an overcurrent or suspend change | |
892 | * (i.e., remote wakeup request), have khubd | |
72937e1e SS |
893 | * take care of it. Look at the port link state |
894 | * for USB 3.0 hubs, since they don't have a suspend | |
895 | * change bit, and they don't set the port link change | |
896 | * bit on device-initiated resume. | |
6ee0b270 | 897 | */ |
72937e1e SS |
898 | if (portchange || (hub_is_superspeed(hub->hdev) && |
899 | port_resumed)) | |
6ee0b270 | 900 | set_bit(port1, hub->change_bits); |
5e6effae | 901 | |
6ee0b270 | 902 | } else if (udev->persist_enabled) { |
6ee0b270 | 903 | #ifdef CONFIG_PM |
5e6effae | 904 | udev->reset_resume = 1; |
6ee0b270 | 905 | #endif |
8808f00c AS |
906 | set_bit(port1, hub->change_bits); |
907 | ||
6ee0b270 AS |
908 | } else { |
909 | /* The power session is gone; tell khubd */ | |
910 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); | |
911 | set_bit(port1, hub->change_bits); | |
5e6effae | 912 | } |
5e6effae AS |
913 | } |
914 | ||
948fea37 AS |
915 | /* If no port-status-change flags were set, we don't need any |
916 | * debouncing. If flags were set we can try to debounce the | |
917 | * ports all at once right now, instead of letting khubd do them | |
918 | * one at a time later on. | |
919 | * | |
920 | * If any port-status changes do occur during this delay, khubd | |
921 | * will see them later and handle them normally. | |
922 | */ | |
8520f380 AS |
923 | if (need_debounce_delay) { |
924 | delay = HUB_DEBOUNCE_STABLE; | |
925 | ||
926 | /* Don't do a long sleep inside a workqueue routine */ | |
927 | if (type == HUB_INIT2) { | |
928 | PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3); | |
929 | schedule_delayed_work(&hub->init_work, | |
930 | msecs_to_jiffies(delay)); | |
931 | return; /* Continues at init3: below */ | |
932 | } else { | |
933 | msleep(delay); | |
934 | } | |
935 | } | |
936 | init3: | |
f2835219 AS |
937 | hub->quiescing = 0; |
938 | ||
939 | status = usb_submit_urb(hub->urb, GFP_NOIO); | |
940 | if (status < 0) | |
941 | dev_err(hub->intfdev, "activate --> %d\n", status); | |
942 | if (hub->has_indicators && blinkenlights) | |
943 | schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); | |
944 | ||
945 | /* Scan all ports that need attention */ | |
946 | kick_khubd(hub); | |
8e4ceb38 AS |
947 | |
948 | /* Allow autosuspend if it was suppressed */ | |
949 | if (type <= HUB_INIT3) | |
950 | usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); | |
5e6effae AS |
951 | } |
952 | ||
8520f380 AS |
953 | /* Implement the continuations for the delays above */ |
954 | static void hub_init_func2(struct work_struct *ws) | |
955 | { | |
956 | struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); | |
957 | ||
958 | hub_activate(hub, HUB_INIT2); | |
959 | } | |
960 | ||
961 | static void hub_init_func3(struct work_struct *ws) | |
962 | { | |
963 | struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); | |
964 | ||
965 | hub_activate(hub, HUB_INIT3); | |
966 | } | |
967 | ||
4330354f AS |
968 | enum hub_quiescing_type { |
969 | HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND | |
970 | }; | |
971 | ||
972 | static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) | |
973 | { | |
974 | struct usb_device *hdev = hub->hdev; | |
975 | int i; | |
976 | ||
8520f380 AS |
977 | cancel_delayed_work_sync(&hub->init_work); |
978 | ||
4330354f AS |
979 | /* khubd and related activity won't re-trigger */ |
980 | hub->quiescing = 1; | |
981 | ||
982 | if (type != HUB_SUSPEND) { | |
983 | /* Disconnect all the children */ | |
984 | for (i = 0; i < hdev->maxchild; ++i) { | |
fa286188 GKH |
985 | if (hdev->children[i]) |
986 | usb_disconnect(&hdev->children[i]); | |
4330354f AS |
987 | } |
988 | } | |
989 | ||
990 | /* Stop khubd and related activity */ | |
991 | usb_kill_urb(hub->urb); | |
992 | if (hub->has_indicators) | |
993 | cancel_delayed_work_sync(&hub->leds); | |
994 | if (hub->tt.hub) | |
cb88a1b8 | 995 | cancel_work_sync(&hub->tt.clear_work); |
4330354f AS |
996 | } |
997 | ||
3eb14915 AS |
998 | /* caller has locked the hub device */ |
999 | static int hub_pre_reset(struct usb_interface *intf) | |
1000 | { | |
1001 | struct usb_hub *hub = usb_get_intfdata(intf); | |
1002 | ||
4330354f | 1003 | hub_quiesce(hub, HUB_PRE_RESET); |
f07600cf | 1004 | return 0; |
7d069b7d AS |
1005 | } |
1006 | ||
1007 | /* caller has locked the hub device */ | |
f07600cf | 1008 | static int hub_post_reset(struct usb_interface *intf) |
7d069b7d | 1009 | { |
7de18d8b AS |
1010 | struct usb_hub *hub = usb_get_intfdata(intf); |
1011 | ||
f2835219 | 1012 | hub_activate(hub, HUB_POST_RESET); |
f07600cf | 1013 | return 0; |
7d069b7d AS |
1014 | } |
1015 | ||
1da177e4 LT |
1016 | static int hub_configure(struct usb_hub *hub, |
1017 | struct usb_endpoint_descriptor *endpoint) | |
1018 | { | |
b356b7c7 | 1019 | struct usb_hcd *hcd; |
1da177e4 LT |
1020 | struct usb_device *hdev = hub->hdev; |
1021 | struct device *hub_dev = hub->intfdev; | |
1022 | u16 hubstatus, hubchange; | |
74ad9bd2 | 1023 | u16 wHubCharacteristics; |
1da177e4 LT |
1024 | unsigned int pipe; |
1025 | int maxp, ret; | |
7cbe5dca | 1026 | char *message = "out of memory"; |
1da177e4 | 1027 | |
d697cdda | 1028 | hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); |
1da177e4 | 1029 | if (!hub->buffer) { |
1da177e4 LT |
1030 | ret = -ENOMEM; |
1031 | goto fail; | |
1032 | } | |
1033 | ||
1034 | hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); | |
1035 | if (!hub->status) { | |
1da177e4 LT |
1036 | ret = -ENOMEM; |
1037 | goto fail; | |
1038 | } | |
db90e7a1 | 1039 | mutex_init(&hub->status_mutex); |
1da177e4 LT |
1040 | |
1041 | hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); | |
1042 | if (!hub->descriptor) { | |
1da177e4 LT |
1043 | ret = -ENOMEM; |
1044 | goto fail; | |
1045 | } | |
1046 | ||
1047 | /* Request the entire hub descriptor. | |
1048 | * hub->descriptor can handle USB_MAXCHILDREN ports, | |
1049 | * but the hub can/will return fewer bytes here. | |
1050 | */ | |
dbe79bbe | 1051 | ret = get_hub_descriptor(hdev, hub->descriptor); |
1da177e4 LT |
1052 | if (ret < 0) { |
1053 | message = "can't read hub descriptor"; | |
1054 | goto fail; | |
1055 | } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { | |
1056 | message = "hub has too many ports!"; | |
1057 | ret = -ENODEV; | |
1058 | goto fail; | |
1059 | } | |
1060 | ||
1061 | hdev->maxchild = hub->descriptor->bNbrPorts; | |
1062 | dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, | |
1063 | (hdev->maxchild == 1) ? "" : "s"); | |
1064 | ||
fa286188 GKH |
1065 | hdev->children = kzalloc(hdev->maxchild * |
1066 | sizeof(struct usb_device *), GFP_KERNEL); | |
304f0b24 GKH |
1067 | hub->port_owners = kzalloc(hdev->maxchild * sizeof(void *), GFP_KERNEL); |
1068 | if (!hdev->children || !hub->port_owners) { | |
7cbe5dca AS |
1069 | ret = -ENOMEM; |
1070 | goto fail; | |
1071 | } | |
1072 | ||
74ad9bd2 | 1073 | wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); |
1da177e4 | 1074 | |
dbe79bbe JY |
1075 | /* FIXME for USB 3.0, skip for now */ |
1076 | if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && | |
1077 | !(hub_is_superspeed(hdev))) { | |
1da177e4 LT |
1078 | int i; |
1079 | char portstr [USB_MAXCHILDREN + 1]; | |
1080 | ||
1081 | for (i = 0; i < hdev->maxchild; i++) | |
dbe79bbe | 1082 | portstr[i] = hub->descriptor->u.hs.DeviceRemovable |
1da177e4 LT |
1083 | [((i + 1) / 8)] & (1 << ((i + 1) % 8)) |
1084 | ? 'F' : 'R'; | |
1085 | portstr[hdev->maxchild] = 0; | |
1086 | dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); | |
1087 | } else | |
1088 | dev_dbg(hub_dev, "standalone hub\n"); | |
1089 | ||
74ad9bd2 | 1090 | switch (wHubCharacteristics & HUB_CHAR_LPSM) { |
7bf01185 AD |
1091 | case HUB_CHAR_COMMON_LPSM: |
1092 | dev_dbg(hub_dev, "ganged power switching\n"); | |
1093 | break; | |
1094 | case HUB_CHAR_INDV_PORT_LPSM: | |
1095 | dev_dbg(hub_dev, "individual port power switching\n"); | |
1096 | break; | |
1097 | case HUB_CHAR_NO_LPSM: | |
1098 | case HUB_CHAR_LPSM: | |
1099 | dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); | |
1100 | break; | |
1da177e4 LT |
1101 | } |
1102 | ||
74ad9bd2 | 1103 | switch (wHubCharacteristics & HUB_CHAR_OCPM) { |
7bf01185 AD |
1104 | case HUB_CHAR_COMMON_OCPM: |
1105 | dev_dbg(hub_dev, "global over-current protection\n"); | |
1106 | break; | |
1107 | case HUB_CHAR_INDV_PORT_OCPM: | |
1108 | dev_dbg(hub_dev, "individual port over-current protection\n"); | |
1109 | break; | |
1110 | case HUB_CHAR_NO_OCPM: | |
1111 | case HUB_CHAR_OCPM: | |
1112 | dev_dbg(hub_dev, "no over-current protection\n"); | |
1113 | break; | |
1da177e4 LT |
1114 | } |
1115 | ||
1116 | spin_lock_init (&hub->tt.lock); | |
1117 | INIT_LIST_HEAD (&hub->tt.clear_list); | |
cb88a1b8 | 1118 | INIT_WORK(&hub->tt.clear_work, hub_tt_work); |
1da177e4 | 1119 | switch (hdev->descriptor.bDeviceProtocol) { |
7bf01185 AD |
1120 | case USB_HUB_PR_FS: |
1121 | break; | |
1122 | case USB_HUB_PR_HS_SINGLE_TT: | |
1123 | dev_dbg(hub_dev, "Single TT\n"); | |
1124 | hub->tt.hub = hdev; | |
1125 | break; | |
1126 | case USB_HUB_PR_HS_MULTI_TT: | |
1127 | ret = usb_set_interface(hdev, 0, 1); | |
1128 | if (ret == 0) { | |
1129 | dev_dbg(hub_dev, "TT per port\n"); | |
1130 | hub->tt.multi = 1; | |
1131 | } else | |
1132 | dev_err(hub_dev, "Using single TT (err %d)\n", | |
1133 | ret); | |
1134 | hub->tt.hub = hdev; | |
1135 | break; | |
1136 | case USB_HUB_PR_SS: | |
1137 | /* USB 3.0 hubs don't have a TT */ | |
1138 | break; | |
1139 | default: | |
1140 | dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", | |
1141 | hdev->descriptor.bDeviceProtocol); | |
1142 | break; | |
1da177e4 LT |
1143 | } |
1144 | ||
e09711ae | 1145 | /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ |
74ad9bd2 | 1146 | switch (wHubCharacteristics & HUB_CHAR_TTTT) { |
e09711ae DB |
1147 | case HUB_TTTT_8_BITS: |
1148 | if (hdev->descriptor.bDeviceProtocol != 0) { | |
1149 | hub->tt.think_time = 666; | |
1150 | dev_dbg(hub_dev, "TT requires at most %d " | |
1151 | "FS bit times (%d ns)\n", | |
1152 | 8, hub->tt.think_time); | |
1153 | } | |
1da177e4 | 1154 | break; |
e09711ae DB |
1155 | case HUB_TTTT_16_BITS: |
1156 | hub->tt.think_time = 666 * 2; | |
1157 | dev_dbg(hub_dev, "TT requires at most %d " | |
1158 | "FS bit times (%d ns)\n", | |
1159 | 16, hub->tt.think_time); | |
1da177e4 | 1160 | break; |
e09711ae DB |
1161 | case HUB_TTTT_24_BITS: |
1162 | hub->tt.think_time = 666 * 3; | |
1163 | dev_dbg(hub_dev, "TT requires at most %d " | |
1164 | "FS bit times (%d ns)\n", | |
1165 | 24, hub->tt.think_time); | |
1da177e4 | 1166 | break; |
e09711ae DB |
1167 | case HUB_TTTT_32_BITS: |
1168 | hub->tt.think_time = 666 * 4; | |
1169 | dev_dbg(hub_dev, "TT requires at most %d " | |
1170 | "FS bit times (%d ns)\n", | |
1171 | 32, hub->tt.think_time); | |
1da177e4 LT |
1172 | break; |
1173 | } | |
1174 | ||
1175 | /* probe() zeroes hub->indicator[] */ | |
74ad9bd2 | 1176 | if (wHubCharacteristics & HUB_CHAR_PORTIND) { |
1da177e4 LT |
1177 | hub->has_indicators = 1; |
1178 | dev_dbg(hub_dev, "Port indicators are supported\n"); | |
1179 | } | |
1180 | ||
1181 | dev_dbg(hub_dev, "power on to power good time: %dms\n", | |
1182 | hub->descriptor->bPwrOn2PwrGood * 2); | |
1183 | ||
1184 | /* power budgeting mostly matters with bus-powered hubs, | |
1185 | * and battery-powered root hubs (may provide just 8 mA). | |
1186 | */ | |
1187 | ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); | |
55c52718 | 1188 | if (ret < 2) { |
1da177e4 LT |
1189 | message = "can't get hub status"; |
1190 | goto fail; | |
1191 | } | |
7d35b929 AS |
1192 | le16_to_cpus(&hubstatus); |
1193 | if (hdev == hdev->bus->root_hub) { | |
55c52718 AS |
1194 | if (hdev->bus_mA == 0 || hdev->bus_mA >= 500) |
1195 | hub->mA_per_port = 500; | |
1196 | else { | |
1197 | hub->mA_per_port = hdev->bus_mA; | |
1198 | hub->limited_power = 1; | |
1199 | } | |
7d35b929 | 1200 | } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { |
1da177e4 LT |
1201 | dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", |
1202 | hub->descriptor->bHubContrCurrent); | |
55c52718 AS |
1203 | hub->limited_power = 1; |
1204 | if (hdev->maxchild > 0) { | |
1205 | int remaining = hdev->bus_mA - | |
1206 | hub->descriptor->bHubContrCurrent; | |
1207 | ||
1208 | if (remaining < hdev->maxchild * 100) | |
1209 | dev_warn(hub_dev, | |
1210 | "insufficient power available " | |
1211 | "to use all downstream ports\n"); | |
1212 | hub->mA_per_port = 100; /* 7.2.1.1 */ | |
1213 | } | |
1214 | } else { /* Self-powered external hub */ | |
1215 | /* FIXME: What about battery-powered external hubs that | |
1216 | * provide less current per port? */ | |
1217 | hub->mA_per_port = 500; | |
7d35b929 | 1218 | } |
55c52718 AS |
1219 | if (hub->mA_per_port < 500) |
1220 | dev_dbg(hub_dev, "%umA bus power budget for each child\n", | |
1221 | hub->mA_per_port); | |
1da177e4 | 1222 | |
b356b7c7 SS |
1223 | /* Update the HCD's internal representation of this hub before khubd |
1224 | * starts getting port status changes for devices under the hub. | |
1225 | */ | |
1226 | hcd = bus_to_hcd(hdev->bus); | |
1227 | if (hcd->driver->update_hub_device) { | |
1228 | ret = hcd->driver->update_hub_device(hcd, hdev, | |
1229 | &hub->tt, GFP_KERNEL); | |
1230 | if (ret < 0) { | |
1231 | message = "can't update HCD hub info"; | |
1232 | goto fail; | |
1233 | } | |
1234 | } | |
1235 | ||
1da177e4 LT |
1236 | ret = hub_hub_status(hub, &hubstatus, &hubchange); |
1237 | if (ret < 0) { | |
1238 | message = "can't get hub status"; | |
1239 | goto fail; | |
1240 | } | |
1241 | ||
1242 | /* local power status reports aren't always correct */ | |
1243 | if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) | |
1244 | dev_dbg(hub_dev, "local power source is %s\n", | |
1245 | (hubstatus & HUB_STATUS_LOCAL_POWER) | |
1246 | ? "lost (inactive)" : "good"); | |
1247 | ||
74ad9bd2 | 1248 | if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) |
1da177e4 LT |
1249 | dev_dbg(hub_dev, "%sover-current condition exists\n", |
1250 | (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); | |
1251 | ||
88fafff9 | 1252 | /* set up the interrupt endpoint |
1253 | * We use the EP's maxpacket size instead of (PORTS+1+7)/8 | |
1254 | * bytes as USB2.0[11.12.3] says because some hubs are known | |
1255 | * to send more data (and thus cause overflow). For root hubs, | |
1256 | * maxpktsize is defined in hcd.c's fake endpoint descriptors | |
1257 | * to be big enough for at least USB_MAXCHILDREN ports. */ | |
1da177e4 LT |
1258 | pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); |
1259 | maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); | |
1260 | ||
1261 | if (maxp > sizeof(*hub->buffer)) | |
1262 | maxp = sizeof(*hub->buffer); | |
1263 | ||
1264 | hub->urb = usb_alloc_urb(0, GFP_KERNEL); | |
1265 | if (!hub->urb) { | |
1da177e4 LT |
1266 | ret = -ENOMEM; |
1267 | goto fail; | |
1268 | } | |
1269 | ||
1270 | usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, | |
1271 | hub, endpoint->bInterval); | |
1da177e4 LT |
1272 | |
1273 | /* maybe cycle the hub leds */ | |
1274 | if (hub->has_indicators && blinkenlights) | |
1275 | hub->indicator [0] = INDICATOR_CYCLE; | |
1276 | ||
f2835219 | 1277 | hub_activate(hub, HUB_INIT); |
1da177e4 LT |
1278 | return 0; |
1279 | ||
1280 | fail: | |
1281 | dev_err (hub_dev, "config failed, %s (err %d)\n", | |
1282 | message, ret); | |
1283 | /* hub_disconnect() frees urb and descriptor */ | |
1284 | return ret; | |
1285 | } | |
1286 | ||
e8054854 AS |
1287 | static void hub_release(struct kref *kref) |
1288 | { | |
1289 | struct usb_hub *hub = container_of(kref, struct usb_hub, kref); | |
1290 | ||
1291 | usb_put_intf(to_usb_interface(hub->intfdev)); | |
1292 | kfree(hub); | |
1293 | } | |
1294 | ||
1da177e4 LT |
1295 | static unsigned highspeed_hubs; |
1296 | ||
1297 | static void hub_disconnect(struct usb_interface *intf) | |
1298 | { | |
8816230e | 1299 | struct usb_hub *hub = usb_get_intfdata(intf); |
fa286188 | 1300 | struct usb_device *hdev = interface_to_usbdev(intf); |
e8054854 AS |
1301 | |
1302 | /* Take the hub off the event list and don't let it be added again */ | |
1303 | spin_lock_irq(&hub_event_lock); | |
8e4ceb38 AS |
1304 | if (!list_empty(&hub->event_list)) { |
1305 | list_del_init(&hub->event_list); | |
1306 | usb_autopm_put_interface_no_suspend(intf); | |
1307 | } | |
e8054854 AS |
1308 | hub->disconnected = 1; |
1309 | spin_unlock_irq(&hub_event_lock); | |
1da177e4 | 1310 | |
7de18d8b AS |
1311 | /* Disconnect all children and quiesce the hub */ |
1312 | hub->error = 0; | |
4330354f | 1313 | hub_quiesce(hub, HUB_DISCONNECT); |
7de18d8b | 1314 | |
8b28c752 | 1315 | usb_set_intfdata (intf, NULL); |
7cbe5dca | 1316 | hub->hdev->maxchild = 0; |
1da177e4 | 1317 | |
e8054854 | 1318 | if (hub->hdev->speed == USB_SPEED_HIGH) |
1da177e4 LT |
1319 | highspeed_hubs--; |
1320 | ||
1da177e4 | 1321 | usb_free_urb(hub->urb); |
fa286188 | 1322 | kfree(hdev->children); |
304f0b24 | 1323 | kfree(hub->port_owners); |
1bc3c9e1 | 1324 | kfree(hub->descriptor); |
1bc3c9e1 | 1325 | kfree(hub->status); |
d697cdda | 1326 | kfree(hub->buffer); |
1da177e4 | 1327 | |
e8054854 | 1328 | kref_put(&hub->kref, hub_release); |
1da177e4 LT |
1329 | } |
1330 | ||
1331 | static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
1332 | { | |
1333 | struct usb_host_interface *desc; | |
1334 | struct usb_endpoint_descriptor *endpoint; | |
1335 | struct usb_device *hdev; | |
1336 | struct usb_hub *hub; | |
1337 | ||
1338 | desc = intf->cur_altsetting; | |
1339 | hdev = interface_to_usbdev(intf); | |
1340 | ||
2839f5bc SS |
1341 | /* Hubs have proper suspend/resume support. */ |
1342 | usb_enable_autosuspend(hdev); | |
088f7fec | 1343 | |
38f3ad5e | 1344 | if (hdev->level == MAX_TOPO_LEVEL) { |
b9cef6c3 WF |
1345 | dev_err(&intf->dev, |
1346 | "Unsupported bus topology: hub nested too deep\n"); | |
38f3ad5e FB |
1347 | return -E2BIG; |
1348 | } | |
1349 | ||
89ccbdc9 DB |
1350 | #ifdef CONFIG_USB_OTG_BLACKLIST_HUB |
1351 | if (hdev->parent) { | |
1352 | dev_warn(&intf->dev, "ignoring external hub\n"); | |
1353 | return -ENODEV; | |
1354 | } | |
1355 | #endif | |
1356 | ||
1da177e4 LT |
1357 | /* Some hubs have a subclass of 1, which AFAICT according to the */ |
1358 | /* specs is not defined, but it works */ | |
1359 | if ((desc->desc.bInterfaceSubClass != 0) && | |
1360 | (desc->desc.bInterfaceSubClass != 1)) { | |
1361 | descriptor_error: | |
1362 | dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); | |
1363 | return -EIO; | |
1364 | } | |
1365 | ||
1366 | /* Multiple endpoints? What kind of mutant ninja-hub is this? */ | |
1367 | if (desc->desc.bNumEndpoints != 1) | |
1368 | goto descriptor_error; | |
1369 | ||
1370 | endpoint = &desc->endpoint[0].desc; | |
1371 | ||
fbf81c29 LFC |
1372 | /* If it's not an interrupt in endpoint, we'd better punt! */ |
1373 | if (!usb_endpoint_is_int_in(endpoint)) | |
1da177e4 LT |
1374 | goto descriptor_error; |
1375 | ||
1376 | /* We found a hub */ | |
1377 | dev_info (&intf->dev, "USB hub found\n"); | |
1378 | ||
0a1ef3b5 | 1379 | hub = kzalloc(sizeof(*hub), GFP_KERNEL); |
1da177e4 LT |
1380 | if (!hub) { |
1381 | dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); | |
1382 | return -ENOMEM; | |
1383 | } | |
1384 | ||
e8054854 | 1385 | kref_init(&hub->kref); |
1da177e4 LT |
1386 | INIT_LIST_HEAD(&hub->event_list); |
1387 | hub->intfdev = &intf->dev; | |
1388 | hub->hdev = hdev; | |
c4028958 | 1389 | INIT_DELAYED_WORK(&hub->leds, led_work); |
8520f380 | 1390 | INIT_DELAYED_WORK(&hub->init_work, NULL); |
e8054854 | 1391 | usb_get_intf(intf); |
1da177e4 LT |
1392 | |
1393 | usb_set_intfdata (intf, hub); | |
40f122f3 | 1394 | intf->needs_remote_wakeup = 1; |
1da177e4 LT |
1395 | |
1396 | if (hdev->speed == USB_SPEED_HIGH) | |
1397 | highspeed_hubs++; | |
1398 | ||
1399 | if (hub_configure(hub, endpoint) >= 0) | |
1400 | return 0; | |
1401 | ||
1402 | hub_disconnect (intf); | |
1403 | return -ENODEV; | |
1404 | } | |
1405 | ||
1406 | static int | |
1407 | hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) | |
1408 | { | |
1409 | struct usb_device *hdev = interface_to_usbdev (intf); | |
1410 | ||
1411 | /* assert ifno == 0 (part of hub spec) */ | |
1412 | switch (code) { | |
1413 | case USBDEVFS_HUB_PORTINFO: { | |
1414 | struct usbdevfs_hub_portinfo *info = user_data; | |
1415 | int i; | |
1416 | ||
1417 | spin_lock_irq(&device_state_lock); | |
1418 | if (hdev->devnum <= 0) | |
1419 | info->nports = 0; | |
1420 | else { | |
1421 | info->nports = hdev->maxchild; | |
1422 | for (i = 0; i < info->nports; i++) { | |
fa286188 | 1423 | if (hdev->children[i] == NULL) |
1da177e4 LT |
1424 | info->port[i] = 0; |
1425 | else | |
1426 | info->port[i] = | |
fa286188 | 1427 | hdev->children[i]->devnum; |
1da177e4 LT |
1428 | } |
1429 | } | |
1430 | spin_unlock_irq(&device_state_lock); | |
1431 | ||
1432 | return info->nports + 1; | |
1433 | } | |
1434 | ||
1435 | default: | |
1436 | return -ENOSYS; | |
1437 | } | |
1438 | } | |
1439 | ||
7cbe5dca AS |
1440 | /* |
1441 | * Allow user programs to claim ports on a hub. When a device is attached | |
1442 | * to one of these "claimed" ports, the program will "own" the device. | |
1443 | */ | |
1444 | static int find_port_owner(struct usb_device *hdev, unsigned port1, | |
1445 | void ***ppowner) | |
1446 | { | |
1447 | if (hdev->state == USB_STATE_NOTATTACHED) | |
1448 | return -ENODEV; | |
1449 | if (port1 == 0 || port1 > hdev->maxchild) | |
1450 | return -EINVAL; | |
1451 | ||
1452 | /* This assumes that devices not managed by the hub driver | |
1453 | * will always have maxchild equal to 0. | |
1454 | */ | |
304f0b24 | 1455 | *ppowner = &(hdev_to_hub(hdev)->port_owners[port1 - 1]); |
7cbe5dca AS |
1456 | return 0; |
1457 | } | |
1458 | ||
1459 | /* In the following three functions, the caller must hold hdev's lock */ | |
1460 | int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, void *owner) | |
1461 | { | |
1462 | int rc; | |
1463 | void **powner; | |
1464 | ||
1465 | rc = find_port_owner(hdev, port1, &powner); | |
1466 | if (rc) | |
1467 | return rc; | |
1468 | if (*powner) | |
1469 | return -EBUSY; | |
1470 | *powner = owner; | |
1471 | return rc; | |
1472 | } | |
1473 | ||
1474 | int usb_hub_release_port(struct usb_device *hdev, unsigned port1, void *owner) | |
1475 | { | |
1476 | int rc; | |
1477 | void **powner; | |
1478 | ||
1479 | rc = find_port_owner(hdev, port1, &powner); | |
1480 | if (rc) | |
1481 | return rc; | |
1482 | if (*powner != owner) | |
1483 | return -ENOENT; | |
1484 | *powner = NULL; | |
1485 | return rc; | |
1486 | } | |
1487 | ||
1488 | void usb_hub_release_all_ports(struct usb_device *hdev, void *owner) | |
1489 | { | |
1490 | int n; | |
304f0b24 | 1491 | void **powner; |
7cbe5dca | 1492 | |
304f0b24 GKH |
1493 | n = find_port_owner(hdev, 1, &powner); |
1494 | if (n == 0) { | |
1495 | for (; n < hdev->maxchild; (++n, ++powner)) { | |
1496 | if (*powner == owner) | |
1497 | *powner = NULL; | |
1498 | } | |
7cbe5dca AS |
1499 | } |
1500 | } | |
1501 | ||
1502 | /* The caller must hold udev's lock */ | |
1503 | bool usb_device_is_owned(struct usb_device *udev) | |
1504 | { | |
1505 | struct usb_hub *hub; | |
1506 | ||
1507 | if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) | |
1508 | return false; | |
1509 | hub = hdev_to_hub(udev->parent); | |
304f0b24 | 1510 | return !!hub->port_owners[udev->portnum - 1]; |
7cbe5dca AS |
1511 | } |
1512 | ||
1da177e4 | 1513 | |
1da177e4 LT |
1514 | static void recursively_mark_NOTATTACHED(struct usb_device *udev) |
1515 | { | |
1516 | int i; | |
1517 | ||
1518 | for (i = 0; i < udev->maxchild; ++i) { | |
fa286188 GKH |
1519 | if (udev->children[i]) |
1520 | recursively_mark_NOTATTACHED(udev->children[i]); | |
1da177e4 | 1521 | } |
9bbdf1e0 | 1522 | if (udev->state == USB_STATE_SUSPENDED) |
15123006 | 1523 | udev->active_duration -= jiffies; |
1da177e4 LT |
1524 | udev->state = USB_STATE_NOTATTACHED; |
1525 | } | |
1526 | ||
1527 | /** | |
1528 | * usb_set_device_state - change a device's current state (usbcore, hcds) | |
1529 | * @udev: pointer to device whose state should be changed | |
1530 | * @new_state: new state value to be stored | |
1531 | * | |
1532 | * udev->state is _not_ fully protected by the device lock. Although | |
1533 | * most transitions are made only while holding the lock, the state can | |
1534 | * can change to USB_STATE_NOTATTACHED at almost any time. This | |
1535 | * is so that devices can be marked as disconnected as soon as possible, | |
1536 | * without having to wait for any semaphores to be released. As a result, | |
1537 | * all changes to any device's state must be protected by the | |
1538 | * device_state_lock spinlock. | |
1539 | * | |
1540 | * Once a device has been added to the device tree, all changes to its state | |
1541 | * should be made using this routine. The state should _not_ be set directly. | |
1542 | * | |
1543 | * If udev->state is already USB_STATE_NOTATTACHED then no change is made. | |
1544 | * Otherwise udev->state is set to new_state, and if new_state is | |
1545 | * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set | |
1546 | * to USB_STATE_NOTATTACHED. | |
1547 | */ | |
1548 | void usb_set_device_state(struct usb_device *udev, | |
1549 | enum usb_device_state new_state) | |
1550 | { | |
1551 | unsigned long flags; | |
4681b171 | 1552 | int wakeup = -1; |
1da177e4 LT |
1553 | |
1554 | spin_lock_irqsave(&device_state_lock, flags); | |
1555 | if (udev->state == USB_STATE_NOTATTACHED) | |
1556 | ; /* do nothing */ | |
b94dc6b5 | 1557 | else if (new_state != USB_STATE_NOTATTACHED) { |
fb669cc0 DB |
1558 | |
1559 | /* root hub wakeup capabilities are managed out-of-band | |
1560 | * and may involve silicon errata ... ignore them here. | |
1561 | */ | |
1562 | if (udev->parent) { | |
645daaab AS |
1563 | if (udev->state == USB_STATE_SUSPENDED |
1564 | || new_state == USB_STATE_SUSPENDED) | |
1565 | ; /* No change to wakeup settings */ | |
1566 | else if (new_state == USB_STATE_CONFIGURED) | |
4681b171 RW |
1567 | wakeup = udev->actconfig->desc.bmAttributes |
1568 | & USB_CONFIG_ATT_WAKEUP; | |
645daaab | 1569 | else |
4681b171 | 1570 | wakeup = 0; |
fb669cc0 | 1571 | } |
15123006 SS |
1572 | if (udev->state == USB_STATE_SUSPENDED && |
1573 | new_state != USB_STATE_SUSPENDED) | |
1574 | udev->active_duration -= jiffies; | |
1575 | else if (new_state == USB_STATE_SUSPENDED && | |
1576 | udev->state != USB_STATE_SUSPENDED) | |
1577 | udev->active_duration += jiffies; | |
645daaab | 1578 | udev->state = new_state; |
b94dc6b5 | 1579 | } else |
1da177e4 LT |
1580 | recursively_mark_NOTATTACHED(udev); |
1581 | spin_unlock_irqrestore(&device_state_lock, flags); | |
4681b171 RW |
1582 | if (wakeup >= 0) |
1583 | device_set_wakeup_capable(&udev->dev, wakeup); | |
1da177e4 | 1584 | } |
6da9c990 | 1585 | EXPORT_SYMBOL_GPL(usb_set_device_state); |
1da177e4 | 1586 | |
8af548dc | 1587 | /* |
3b29b68b AS |
1588 | * Choose a device number. |
1589 | * | |
1590 | * Device numbers are used as filenames in usbfs. On USB-1.1 and | |
1591 | * USB-2.0 buses they are also used as device addresses, however on | |
1592 | * USB-3.0 buses the address is assigned by the controller hardware | |
1593 | * and it usually is not the same as the device number. | |
1594 | * | |
8af548dc IPG |
1595 | * WUSB devices are simple: they have no hubs behind, so the mapping |
1596 | * device <-> virtual port number becomes 1:1. Why? to simplify the | |
1597 | * life of the device connection logic in | |
1598 | * drivers/usb/wusbcore/devconnect.c. When we do the initial secret | |
1599 | * handshake we need to assign a temporary address in the unauthorized | |
1600 | * space. For simplicity we use the first virtual port number found to | |
1601 | * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()] | |
1602 | * and that becomes it's address [X < 128] or its unauthorized address | |
1603 | * [X | 0x80]. | |
1604 | * | |
1605 | * We add 1 as an offset to the one-based USB-stack port number | |
1606 | * (zero-based wusb virtual port index) for two reasons: (a) dev addr | |
1607 | * 0 is reserved by USB for default address; (b) Linux's USB stack | |
1608 | * uses always #1 for the root hub of the controller. So USB stack's | |
1609 | * port #1, which is wusb virtual-port #0 has address #2. | |
c6515272 SS |
1610 | * |
1611 | * Devices connected under xHCI are not as simple. The host controller | |
1612 | * supports virtualization, so the hardware assigns device addresses and | |
1613 | * the HCD must setup data structures before issuing a set address | |
1614 | * command to the hardware. | |
8af548dc | 1615 | */ |
3b29b68b | 1616 | static void choose_devnum(struct usb_device *udev) |
1da177e4 LT |
1617 | { |
1618 | int devnum; | |
1619 | struct usb_bus *bus = udev->bus; | |
1620 | ||
1621 | /* If khubd ever becomes multithreaded, this will need a lock */ | |
8af548dc IPG |
1622 | if (udev->wusb) { |
1623 | devnum = udev->portnum + 1; | |
1624 | BUG_ON(test_bit(devnum, bus->devmap.devicemap)); | |
1625 | } else { | |
1626 | /* Try to allocate the next devnum beginning at | |
1627 | * bus->devnum_next. */ | |
1628 | devnum = find_next_zero_bit(bus->devmap.devicemap, 128, | |
1629 | bus->devnum_next); | |
1630 | if (devnum >= 128) | |
1631 | devnum = find_next_zero_bit(bus->devmap.devicemap, | |
1632 | 128, 1); | |
1633 | bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1); | |
1634 | } | |
1da177e4 LT |
1635 | if (devnum < 128) { |
1636 | set_bit(devnum, bus->devmap.devicemap); | |
1637 | udev->devnum = devnum; | |
1638 | } | |
1639 | } | |
1640 | ||
3b29b68b | 1641 | static void release_devnum(struct usb_device *udev) |
1da177e4 LT |
1642 | { |
1643 | if (udev->devnum > 0) { | |
1644 | clear_bit(udev->devnum, udev->bus->devmap.devicemap); | |
1645 | udev->devnum = -1; | |
1646 | } | |
1647 | } | |
1648 | ||
3b29b68b | 1649 | static void update_devnum(struct usb_device *udev, int devnum) |
4953d141 DV |
1650 | { |
1651 | /* The address for a WUSB device is managed by wusbcore. */ | |
1652 | if (!udev->wusb) | |
1653 | udev->devnum = devnum; | |
1654 | } | |
1655 | ||
f7410ced HX |
1656 | static void hub_free_dev(struct usb_device *udev) |
1657 | { | |
1658 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
1659 | ||
1660 | /* Root hubs aren't real devices, so don't free HCD resources */ | |
1661 | if (hcd->driver->free_dev && udev->parent) | |
1662 | hcd->driver->free_dev(hcd, udev); | |
1663 | } | |
1664 | ||
1da177e4 LT |
1665 | /** |
1666 | * usb_disconnect - disconnect a device (usbcore-internal) | |
1667 | * @pdev: pointer to device being disconnected | |
1668 | * Context: !in_interrupt () | |
1669 | * | |
1670 | * Something got disconnected. Get rid of it and all of its children. | |
1671 | * | |
1672 | * If *pdev is a normal device then the parent hub must already be locked. | |
1673 | * If *pdev is a root hub then this routine will acquire the | |
1674 | * usb_bus_list_lock on behalf of the caller. | |
1675 | * | |
1676 | * Only hub drivers (including virtual root hub drivers for host | |
1677 | * controllers) should ever call this. | |
1678 | * | |
1679 | * This call is synchronous, and may not be used in an interrupt context. | |
1680 | */ | |
1681 | void usb_disconnect(struct usb_device **pdev) | |
1682 | { | |
1683 | struct usb_device *udev = *pdev; | |
1684 | int i; | |
1685 | ||
1da177e4 LT |
1686 | /* mark the device as inactive, so any further urb submissions for |
1687 | * this device (and any of its children) will fail immediately. | |
25985edc | 1688 | * this quiesces everything except pending urbs. |
1da177e4 LT |
1689 | */ |
1690 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); | |
3b29b68b AS |
1691 | dev_info(&udev->dev, "USB disconnect, device number %d\n", |
1692 | udev->devnum); | |
1da177e4 | 1693 | |
9ad3d6cc AS |
1694 | usb_lock_device(udev); |
1695 | ||
1da177e4 | 1696 | /* Free up all the children before we remove this device */ |
8816230e | 1697 | for (i = 0; i < udev->maxchild; i++) { |
fa286188 GKH |
1698 | if (udev->children[i]) |
1699 | usb_disconnect(&udev->children[i]); | |
1da177e4 LT |
1700 | } |
1701 | ||
1702 | /* deallocate hcd/hardware state ... nuking all pending urbs and | |
1703 | * cleaning up all state associated with the current configuration | |
1704 | * so that the hardware is now fully quiesced. | |
1705 | */ | |
782da727 | 1706 | dev_dbg (&udev->dev, "unregistering device\n"); |
1da177e4 | 1707 | usb_disable_device(udev, 0); |
cde217a5 | 1708 | usb_hcd_synchronize_unlinks(udev); |
1da177e4 | 1709 | |
3b23dd6f | 1710 | usb_remove_ep_devs(&udev->ep0); |
782da727 AS |
1711 | usb_unlock_device(udev); |
1712 | ||
1713 | /* Unregister the device. The device driver is responsible | |
3b23dd6f AS |
1714 | * for de-configuring the device and invoking the remove-device |
1715 | * notifier chain (used by usbfs and possibly others). | |
782da727 AS |
1716 | */ |
1717 | device_del(&udev->dev); | |
3099e75a | 1718 | |
782da727 | 1719 | /* Free the device number and delete the parent's children[] |
1da177e4 LT |
1720 | * (or root_hub) pointer. |
1721 | */ | |
3b29b68b | 1722 | release_devnum(udev); |
1da177e4 LT |
1723 | |
1724 | /* Avoid races with recursively_mark_NOTATTACHED() */ | |
1725 | spin_lock_irq(&device_state_lock); | |
1726 | *pdev = NULL; | |
1727 | spin_unlock_irq(&device_state_lock); | |
1728 | ||
f7410ced HX |
1729 | hub_free_dev(udev); |
1730 | ||
782da727 | 1731 | put_device(&udev->dev); |
1da177e4 LT |
1732 | } |
1733 | ||
f2a383e4 | 1734 | #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES |
1da177e4 LT |
1735 | static void show_string(struct usb_device *udev, char *id, char *string) |
1736 | { | |
1737 | if (!string) | |
1738 | return; | |
1739 | dev_printk(KERN_INFO, &udev->dev, "%s: %s\n", id, string); | |
1740 | } | |
1741 | ||
f2a383e4 GKH |
1742 | static void announce_device(struct usb_device *udev) |
1743 | { | |
1744 | dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n", | |
1745 | le16_to_cpu(udev->descriptor.idVendor), | |
1746 | le16_to_cpu(udev->descriptor.idProduct)); | |
b9cef6c3 WF |
1747 | dev_info(&udev->dev, |
1748 | "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", | |
f2a383e4 GKH |
1749 | udev->descriptor.iManufacturer, |
1750 | udev->descriptor.iProduct, | |
1751 | udev->descriptor.iSerialNumber); | |
1752 | show_string(udev, "Product", udev->product); | |
1753 | show_string(udev, "Manufacturer", udev->manufacturer); | |
1754 | show_string(udev, "SerialNumber", udev->serial); | |
1755 | } | |
1da177e4 | 1756 | #else |
f2a383e4 | 1757 | static inline void announce_device(struct usb_device *udev) { } |
1da177e4 LT |
1758 | #endif |
1759 | ||
1da177e4 LT |
1760 | #ifdef CONFIG_USB_OTG |
1761 | #include "otg_whitelist.h" | |
1762 | #endif | |
1763 | ||
3ede760f | 1764 | /** |
8d8558d1 | 1765 | * usb_enumerate_device_otg - FIXME (usbcore-internal) |
3ede760f ON |
1766 | * @udev: newly addressed device (in ADDRESS state) |
1767 | * | |
8d8558d1 | 1768 | * Finish enumeration for On-The-Go devices |
3ede760f | 1769 | */ |
8d8558d1 | 1770 | static int usb_enumerate_device_otg(struct usb_device *udev) |
1da177e4 | 1771 | { |
d9d16e8a | 1772 | int err = 0; |
1da177e4 LT |
1773 | |
1774 | #ifdef CONFIG_USB_OTG | |
1775 | /* | |
1776 | * OTG-aware devices on OTG-capable root hubs may be able to use SRP, | |
1777 | * to wake us after we've powered off VBUS; and HNP, switching roles | |
1778 | * "host" to "peripheral". The OTG descriptor helps figure this out. | |
1779 | */ | |
1780 | if (!udev->bus->is_b_host | |
1781 | && udev->config | |
1782 | && udev->parent == udev->bus->root_hub) { | |
2eb5052e | 1783 | struct usb_otg_descriptor *desc = NULL; |
1da177e4 LT |
1784 | struct usb_bus *bus = udev->bus; |
1785 | ||
1786 | /* descriptor may appear anywhere in config */ | |
1787 | if (__usb_get_extra_descriptor (udev->rawdescriptors[0], | |
1788 | le16_to_cpu(udev->config[0].desc.wTotalLength), | |
1789 | USB_DT_OTG, (void **) &desc) == 0) { | |
1790 | if (desc->bmAttributes & USB_OTG_HNP) { | |
12c3da34 | 1791 | unsigned port1 = udev->portnum; |
fc849b85 | 1792 | |
1da177e4 LT |
1793 | dev_info(&udev->dev, |
1794 | "Dual-Role OTG device on %sHNP port\n", | |
1795 | (port1 == bus->otg_port) | |
1796 | ? "" : "non-"); | |
1797 | ||
1798 | /* enable HNP before suspend, it's simpler */ | |
1799 | if (port1 == bus->otg_port) | |
1800 | bus->b_hnp_enable = 1; | |
1801 | err = usb_control_msg(udev, | |
1802 | usb_sndctrlpipe(udev, 0), | |
1803 | USB_REQ_SET_FEATURE, 0, | |
1804 | bus->b_hnp_enable | |
1805 | ? USB_DEVICE_B_HNP_ENABLE | |
1806 | : USB_DEVICE_A_ALT_HNP_SUPPORT, | |
1807 | 0, NULL, 0, USB_CTRL_SET_TIMEOUT); | |
1808 | if (err < 0) { | |
1809 | /* OTG MESSAGE: report errors here, | |
1810 | * customize to match your product. | |
1811 | */ | |
1812 | dev_info(&udev->dev, | |
b9cef6c3 | 1813 | "can't set HNP mode: %d\n", |
1da177e4 LT |
1814 | err); |
1815 | bus->b_hnp_enable = 0; | |
1816 | } | |
1817 | } | |
1818 | } | |
1819 | } | |
1820 | ||
1821 | if (!is_targeted(udev)) { | |
1822 | ||
1823 | /* Maybe it can talk to us, though we can't talk to it. | |
1824 | * (Includes HNP test device.) | |
1825 | */ | |
1826 | if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { | |
634a84f8 | 1827 | err = usb_port_suspend(udev, PMSG_SUSPEND); |
1da177e4 LT |
1828 | if (err < 0) |
1829 | dev_dbg(&udev->dev, "HNP fail, %d\n", err); | |
1830 | } | |
ffcdc18d | 1831 | err = -ENOTSUPP; |
1da177e4 LT |
1832 | goto fail; |
1833 | } | |
d9d16e8a | 1834 | fail: |
1da177e4 | 1835 | #endif |
d9d16e8a IPG |
1836 | return err; |
1837 | } | |
1838 | ||
1839 | ||
1840 | /** | |
8d8558d1 | 1841 | * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) |
d9d16e8a IPG |
1842 | * @udev: newly addressed device (in ADDRESS state) |
1843 | * | |
1844 | * This is only called by usb_new_device() and usb_authorize_device() | |
1845 | * and FIXME -- all comments that apply to them apply here wrt to | |
1846 | * environment. | |
1847 | * | |
1848 | * If the device is WUSB and not authorized, we don't attempt to read | |
1849 | * the string descriptors, as they will be errored out by the device | |
1850 | * until it has been authorized. | |
1851 | */ | |
8d8558d1 | 1852 | static int usb_enumerate_device(struct usb_device *udev) |
d9d16e8a IPG |
1853 | { |
1854 | int err; | |
1da177e4 | 1855 | |
d9d16e8a IPG |
1856 | if (udev->config == NULL) { |
1857 | err = usb_get_configuration(udev); | |
1858 | if (err < 0) { | |
1859 | dev_err(&udev->dev, "can't read configurations, error %d\n", | |
1860 | err); | |
1861 | goto fail; | |
1862 | } | |
1863 | } | |
1864 | if (udev->wusb == 1 && udev->authorized == 0) { | |
1865 | udev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); | |
1866 | udev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); | |
1867 | udev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); | |
1868 | } | |
1869 | else { | |
1870 | /* read the standard strings and cache them if present */ | |
1871 | udev->product = usb_cache_string(udev, udev->descriptor.iProduct); | |
1872 | udev->manufacturer = usb_cache_string(udev, | |
1873 | udev->descriptor.iManufacturer); | |
1874 | udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); | |
1875 | } | |
8d8558d1 | 1876 | err = usb_enumerate_device_otg(udev); |
d9d16e8a IPG |
1877 | fail: |
1878 | return err; | |
1879 | } | |
1880 | ||
d35e70d5 MG |
1881 | static void set_usb_port_removable(struct usb_device *udev) |
1882 | { | |
1883 | struct usb_device *hdev = udev->parent; | |
1884 | struct usb_hub *hub; | |
1885 | u8 port = udev->portnum; | |
1886 | u16 wHubCharacteristics; | |
1887 | bool removable = true; | |
1888 | ||
1889 | if (!hdev) | |
1890 | return; | |
1891 | ||
1892 | hub = hdev_to_hub(udev->parent); | |
1893 | ||
1894 | wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); | |
1895 | ||
1896 | if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) | |
1897 | return; | |
1898 | ||
1899 | if (hub_is_superspeed(hdev)) { | |
1900 | if (hub->descriptor->u.ss.DeviceRemovable & (1 << port)) | |
1901 | removable = false; | |
1902 | } else { | |
1903 | if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) | |
1904 | removable = false; | |
1905 | } | |
1906 | ||
1907 | if (removable) | |
1908 | udev->removable = USB_DEVICE_REMOVABLE; | |
1909 | else | |
1910 | udev->removable = USB_DEVICE_FIXED; | |
1911 | } | |
d9d16e8a IPG |
1912 | |
1913 | /** | |
1914 | * usb_new_device - perform initial device setup (usbcore-internal) | |
1915 | * @udev: newly addressed device (in ADDRESS state) | |
1916 | * | |
8d8558d1 AS |
1917 | * This is called with devices which have been detected but not fully |
1918 | * enumerated. The device descriptor is available, but not descriptors | |
d9d16e8a IPG |
1919 | * for any device configuration. The caller must have locked either |
1920 | * the parent hub (if udev is a normal device) or else the | |
1921 | * usb_bus_list_lock (if udev is a root hub). The parent's pointer to | |
1922 | * udev has already been installed, but udev is not yet visible through | |
1923 | * sysfs or other filesystem code. | |
1924 | * | |
1925 | * It will return if the device is configured properly or not. Zero if | |
1926 | * the interface was registered with the driver core; else a negative | |
1927 | * errno value. | |
1928 | * | |
1929 | * This call is synchronous, and may not be used in an interrupt context. | |
1930 | * | |
1931 | * Only the hub driver or root-hub registrar should ever call this. | |
1932 | */ | |
1933 | int usb_new_device(struct usb_device *udev) | |
1934 | { | |
1935 | int err; | |
1936 | ||
16985408 | 1937 | if (udev->parent) { |
16985408 DS |
1938 | /* Initialize non-root-hub device wakeup to disabled; |
1939 | * device (un)configuration controls wakeup capable | |
1940 | * sysfs power/wakeup controls wakeup enabled/disabled | |
1941 | */ | |
1942 | device_init_wakeup(&udev->dev, 0); | |
16985408 DS |
1943 | } |
1944 | ||
9bbdf1e0 AS |
1945 | /* Tell the runtime-PM framework the device is active */ |
1946 | pm_runtime_set_active(&udev->dev); | |
c08512c7 | 1947 | pm_runtime_get_noresume(&udev->dev); |
fcc4a01e | 1948 | pm_runtime_use_autosuspend(&udev->dev); |
9bbdf1e0 AS |
1949 | pm_runtime_enable(&udev->dev); |
1950 | ||
c08512c7 AS |
1951 | /* By default, forbid autosuspend for all devices. It will be |
1952 | * allowed for hubs during binding. | |
1953 | */ | |
1954 | usb_disable_autosuspend(udev); | |
1955 | ||
8d8558d1 | 1956 | err = usb_enumerate_device(udev); /* Read descriptors */ |
d9d16e8a IPG |
1957 | if (err < 0) |
1958 | goto fail; | |
c6515272 SS |
1959 | dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", |
1960 | udev->devnum, udev->bus->busnum, | |
1961 | (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); | |
9f8b17e6 KS |
1962 | /* export the usbdev device-node for libusb */ |
1963 | udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, | |
1964 | (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); | |
1965 | ||
6cd13201 AS |
1966 | /* Tell the world! */ |
1967 | announce_device(udev); | |
195af2cc | 1968 | |
927bc916 | 1969 | device_enable_async_suspend(&udev->dev); |
d35e70d5 MG |
1970 | |
1971 | /* | |
1972 | * check whether the hub marks this port as non-removable. Do it | |
1973 | * now so that platform-specific data can override it in | |
1974 | * device_add() | |
1975 | */ | |
1976 | if (udev->parent) | |
1977 | set_usb_port_removable(udev); | |
1978 | ||
782da727 | 1979 | /* Register the device. The device driver is responsible |
3b23dd6f AS |
1980 | * for configuring the device and invoking the add-device |
1981 | * notifier chain (used by usbfs and possibly others). | |
782da727 | 1982 | */ |
9f8b17e6 | 1983 | err = device_add(&udev->dev); |
1da177e4 LT |
1984 | if (err) { |
1985 | dev_err(&udev->dev, "can't device_add, error %d\n", err); | |
1986 | goto fail; | |
1987 | } | |
9ad3d6cc | 1988 | |
3b23dd6f | 1989 | (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); |
c08512c7 AS |
1990 | usb_mark_last_busy(udev); |
1991 | pm_runtime_put_sync_autosuspend(&udev->dev); | |
c066475e | 1992 | return err; |
1da177e4 LT |
1993 | |
1994 | fail: | |
1995 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); | |
9bbdf1e0 AS |
1996 | pm_runtime_disable(&udev->dev); |
1997 | pm_runtime_set_suspended(&udev->dev); | |
d9d16e8a | 1998 | return err; |
1da177e4 LT |
1999 | } |
2000 | ||
93993a0a IPG |
2001 | |
2002 | /** | |
fd39c86b RD |
2003 | * usb_deauthorize_device - deauthorize a device (usbcore-internal) |
2004 | * @usb_dev: USB device | |
2005 | * | |
2006 | * Move the USB device to a very basic state where interfaces are disabled | |
2007 | * and the device is in fact unconfigured and unusable. | |
93993a0a IPG |
2008 | * |
2009 | * We share a lock (that we have) with device_del(), so we need to | |
2010 | * defer its call. | |
2011 | */ | |
2012 | int usb_deauthorize_device(struct usb_device *usb_dev) | |
2013 | { | |
93993a0a IPG |
2014 | usb_lock_device(usb_dev); |
2015 | if (usb_dev->authorized == 0) | |
2016 | goto out_unauthorized; | |
da307123 | 2017 | |
93993a0a IPG |
2018 | usb_dev->authorized = 0; |
2019 | usb_set_configuration(usb_dev, -1); | |
da307123 AS |
2020 | |
2021 | kfree(usb_dev->product); | |
93993a0a | 2022 | usb_dev->product = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
da307123 | 2023 | kfree(usb_dev->manufacturer); |
93993a0a | 2024 | usb_dev->manufacturer = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
da307123 | 2025 | kfree(usb_dev->serial); |
93993a0a | 2026 | usb_dev->serial = kstrdup("n/a (unauthorized)", GFP_KERNEL); |
da307123 AS |
2027 | |
2028 | usb_destroy_configuration(usb_dev); | |
93993a0a | 2029 | usb_dev->descriptor.bNumConfigurations = 0; |
da307123 | 2030 | |
93993a0a IPG |
2031 | out_unauthorized: |
2032 | usb_unlock_device(usb_dev); | |
2033 | return 0; | |
2034 | } | |
2035 | ||
2036 | ||
2037 | int usb_authorize_device(struct usb_device *usb_dev) | |
2038 | { | |
2039 | int result = 0, c; | |
da307123 | 2040 | |
93993a0a IPG |
2041 | usb_lock_device(usb_dev); |
2042 | if (usb_dev->authorized == 1) | |
2043 | goto out_authorized; | |
da307123 | 2044 | |
93993a0a IPG |
2045 | result = usb_autoresume_device(usb_dev); |
2046 | if (result < 0) { | |
2047 | dev_err(&usb_dev->dev, | |
2048 | "can't autoresume for authorization: %d\n", result); | |
2049 | goto error_autoresume; | |
2050 | } | |
2051 | result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); | |
2052 | if (result < 0) { | |
2053 | dev_err(&usb_dev->dev, "can't re-read device descriptor for " | |
2054 | "authorization: %d\n", result); | |
2055 | goto error_device_descriptor; | |
2056 | } | |
da307123 AS |
2057 | |
2058 | kfree(usb_dev->product); | |
2059 | usb_dev->product = NULL; | |
2060 | kfree(usb_dev->manufacturer); | |
2061 | usb_dev->manufacturer = NULL; | |
2062 | kfree(usb_dev->serial); | |
2063 | usb_dev->serial = NULL; | |
2064 | ||
93993a0a | 2065 | usb_dev->authorized = 1; |
8d8558d1 | 2066 | result = usb_enumerate_device(usb_dev); |
93993a0a | 2067 | if (result < 0) |
8d8558d1 | 2068 | goto error_enumerate; |
93993a0a IPG |
2069 | /* Choose and set the configuration. This registers the interfaces |
2070 | * with the driver core and lets interface drivers bind to them. | |
2071 | */ | |
b5ea060f | 2072 | c = usb_choose_configuration(usb_dev); |
93993a0a IPG |
2073 | if (c >= 0) { |
2074 | result = usb_set_configuration(usb_dev, c); | |
2075 | if (result) { | |
2076 | dev_err(&usb_dev->dev, | |
2077 | "can't set config #%d, error %d\n", c, result); | |
2078 | /* This need not be fatal. The user can try to | |
2079 | * set other configurations. */ | |
2080 | } | |
2081 | } | |
2082 | dev_info(&usb_dev->dev, "authorized to connect\n"); | |
da307123 | 2083 | |
8d8558d1 | 2084 | error_enumerate: |
93993a0a | 2085 | error_device_descriptor: |
da307123 | 2086 | usb_autosuspend_device(usb_dev); |
93993a0a IPG |
2087 | error_autoresume: |
2088 | out_authorized: | |
2089 | usb_unlock_device(usb_dev); // complements locktree | |
2090 | return result; | |
2091 | } | |
2092 | ||
2093 | ||
0165de09 IPG |
2094 | /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ |
2095 | static unsigned hub_is_wusb(struct usb_hub *hub) | |
2096 | { | |
2097 | struct usb_hcd *hcd; | |
2098 | if (hub->hdev->parent != NULL) /* not a root hub? */ | |
2099 | return 0; | |
2100 | hcd = container_of(hub->hdev->bus, struct usb_hcd, self); | |
2101 | return hcd->wireless; | |
2102 | } | |
2103 | ||
2104 | ||
1da177e4 LT |
2105 | #define PORT_RESET_TRIES 5 |
2106 | #define SET_ADDRESS_TRIES 2 | |
2107 | #define GET_DESCRIPTOR_TRIES 2 | |
2108 | #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) | |
90ab5ee9 | 2109 | #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first) |
1da177e4 LT |
2110 | |
2111 | #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ | |
2112 | #define HUB_SHORT_RESET_TIME 10 | |
75d7cf72 | 2113 | #define HUB_BH_RESET_TIME 50 |
1da177e4 LT |
2114 | #define HUB_LONG_RESET_TIME 200 |
2115 | #define HUB_RESET_TIMEOUT 500 | |
2116 | ||
10d674a8 SS |
2117 | static int hub_port_reset(struct usb_hub *hub, int port1, |
2118 | struct usb_device *udev, unsigned int delay, bool warm); | |
2119 | ||
2120 | /* Is a USB 3.0 port in the Inactive state? */ | |
2121 | static bool hub_port_inactive(struct usb_hub *hub, u16 portstatus) | |
2122 | { | |
2123 | return hub_is_superspeed(hub->hdev) && | |
2124 | (portstatus & USB_PORT_STAT_LINK_STATE) == | |
2125 | USB_SS_PORT_LS_SS_INACTIVE; | |
2126 | } | |
2127 | ||
1da177e4 | 2128 | static int hub_port_wait_reset(struct usb_hub *hub, int port1, |
75d7cf72 | 2129 | struct usb_device *udev, unsigned int delay, bool warm) |
1da177e4 LT |
2130 | { |
2131 | int delay_time, ret; | |
2132 | u16 portstatus; | |
2133 | u16 portchange; | |
2134 | ||
2135 | for (delay_time = 0; | |
2136 | delay_time < HUB_RESET_TIMEOUT; | |
2137 | delay_time += delay) { | |
2138 | /* wait to give the device a chance to reset */ | |
2139 | msleep(delay); | |
2140 | ||
2141 | /* read and decode port status */ | |
2142 | ret = hub_port_status(hub, port1, &portstatus, &portchange); | |
2143 | if (ret < 0) | |
2144 | return ret; | |
2145 | ||
75d7cf72 AX |
2146 | /* |
2147 | * Some buggy devices require a warm reset to be issued even | |
2148 | * when the port appears not to be connected. | |
2149 | */ | |
2150 | if (!warm) { | |
10d674a8 SS |
2151 | /* |
2152 | * Some buggy devices can cause an NEC host controller | |
2153 | * to transition to the "Error" state after a hot port | |
2154 | * reset. This will show up as the port state in | |
2155 | * "Inactive", and the port may also report a | |
2156 | * disconnect. Forcing a warm port reset seems to make | |
2157 | * the device work. | |
2158 | * | |
2159 | * See https://bugzilla.kernel.org/show_bug.cgi?id=41752 | |
2160 | */ | |
2161 | if (hub_port_inactive(hub, portstatus)) { | |
2162 | int ret; | |
2163 | ||
2164 | if ((portchange & USB_PORT_STAT_C_CONNECTION)) | |
2165 | clear_port_feature(hub->hdev, port1, | |
2166 | USB_PORT_FEAT_C_CONNECTION); | |
2167 | if (portchange & USB_PORT_STAT_C_LINK_STATE) | |
2168 | clear_port_feature(hub->hdev, port1, | |
2169 | USB_PORT_FEAT_C_PORT_LINK_STATE); | |
2170 | if (portchange & USB_PORT_STAT_C_RESET) | |
2171 | clear_port_feature(hub->hdev, port1, | |
2172 | USB_PORT_FEAT_C_RESET); | |
2173 | dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n", | |
2174 | port1); | |
2175 | ret = hub_port_reset(hub, port1, | |
2176 | udev, HUB_BH_RESET_TIME, | |
2177 | true); | |
2178 | if ((portchange & USB_PORT_STAT_C_CONNECTION)) | |
2179 | clear_port_feature(hub->hdev, port1, | |
2180 | USB_PORT_FEAT_C_CONNECTION); | |
2181 | return ret; | |
2182 | } | |
75d7cf72 AX |
2183 | /* Device went away? */ |
2184 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) | |
2185 | return -ENOTCONN; | |
2186 | ||
2187 | /* bomb out completely if the connection bounced */ | |
2188 | if ((portchange & USB_PORT_STAT_C_CONNECTION)) | |
2189 | return -ENOTCONN; | |
2190 | ||
2191 | /* if we`ve finished resetting, then break out of | |
2192 | * the loop | |
2193 | */ | |
2194 | if (!(portstatus & USB_PORT_STAT_RESET) && | |
2195 | (portstatus & USB_PORT_STAT_ENABLE)) { | |
2196 | if (hub_is_wusb(hub)) | |
2197 | udev->speed = USB_SPEED_WIRELESS; | |
2198 | else if (hub_is_superspeed(hub->hdev)) | |
2199 | udev->speed = USB_SPEED_SUPER; | |
2200 | else if (portstatus & USB_PORT_STAT_HIGH_SPEED) | |
2201 | udev->speed = USB_SPEED_HIGH; | |
2202 | else if (portstatus & USB_PORT_STAT_LOW_SPEED) | |
2203 | udev->speed = USB_SPEED_LOW; | |
2204 | else | |
2205 | udev->speed = USB_SPEED_FULL; | |
2206 | return 0; | |
2207 | } | |
2208 | } else { | |
2209 | if (portchange & USB_PORT_STAT_C_BH_RESET) | |
2210 | return 0; | |
1da177e4 LT |
2211 | } |
2212 | ||
2213 | /* switch to the long delay after two short delay failures */ | |
2214 | if (delay_time >= 2 * HUB_SHORT_RESET_TIME) | |
2215 | delay = HUB_LONG_RESET_TIME; | |
2216 | ||
2217 | dev_dbg (hub->intfdev, | |
75d7cf72 AX |
2218 | "port %d not %sreset yet, waiting %dms\n", |
2219 | port1, warm ? "warm " : "", delay); | |
1da177e4 LT |
2220 | } |
2221 | ||
2222 | return -EBUSY; | |
2223 | } | |
2224 | ||
75d7cf72 AX |
2225 | static void hub_port_finish_reset(struct usb_hub *hub, int port1, |
2226 | struct usb_device *udev, int *status, bool warm) | |
2227 | { | |
2228 | switch (*status) { | |
2229 | case 0: | |
2230 | if (!warm) { | |
2231 | struct usb_hcd *hcd; | |
2232 | /* TRSTRCY = 10 ms; plus some extra */ | |
2233 | msleep(10 + 40); | |
2234 | update_devnum(udev, 0); | |
2235 | hcd = bus_to_hcd(udev->bus); | |
2236 | if (hcd->driver->reset_device) { | |
2237 | *status = hcd->driver->reset_device(hcd, udev); | |
2238 | if (*status < 0) { | |
2239 | dev_err(&udev->dev, "Cannot reset " | |
2240 | "HCD device state\n"); | |
2241 | break; | |
2242 | } | |
2243 | } | |
2244 | } | |
2245 | /* FALL THROUGH */ | |
2246 | case -ENOTCONN: | |
2247 | case -ENODEV: | |
2248 | clear_port_feature(hub->hdev, | |
2249 | port1, USB_PORT_FEAT_C_RESET); | |
2250 | /* FIXME need disconnect() for NOTATTACHED device */ | |
2251 | if (warm) { | |
2252 | clear_port_feature(hub->hdev, port1, | |
2253 | USB_PORT_FEAT_C_BH_PORT_RESET); | |
2254 | clear_port_feature(hub->hdev, port1, | |
2255 | USB_PORT_FEAT_C_PORT_LINK_STATE); | |
2256 | } else { | |
2257 | usb_set_device_state(udev, *status | |
2258 | ? USB_STATE_NOTATTACHED | |
2259 | : USB_STATE_DEFAULT); | |
2260 | } | |
2261 | break; | |
2262 | } | |
2263 | } | |
2264 | ||
2265 | /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ | |
1da177e4 | 2266 | static int hub_port_reset(struct usb_hub *hub, int port1, |
75d7cf72 | 2267 | struct usb_device *udev, unsigned int delay, bool warm) |
1da177e4 LT |
2268 | { |
2269 | int i, status; | |
2270 | ||
75d7cf72 AX |
2271 | if (!warm) { |
2272 | /* Block EHCI CF initialization during the port reset. | |
2273 | * Some companion controllers don't like it when they mix. | |
2274 | */ | |
2275 | down_read(&ehci_cf_port_reset_rwsem); | |
2276 | } else { | |
2277 | if (!hub_is_superspeed(hub->hdev)) { | |
2278 | dev_err(hub->intfdev, "only USB3 hub support " | |
2279 | "warm reset\n"); | |
2280 | return -EINVAL; | |
2281 | } | |
2282 | } | |
32fe0198 | 2283 | |
1da177e4 LT |
2284 | /* Reset the port */ |
2285 | for (i = 0; i < PORT_RESET_TRIES; i++) { | |
75d7cf72 AX |
2286 | status = set_port_feature(hub->hdev, port1, (warm ? |
2287 | USB_PORT_FEAT_BH_PORT_RESET : | |
2288 | USB_PORT_FEAT_RESET)); | |
2289 | if (status) { | |
1da177e4 | 2290 | dev_err(hub->intfdev, |
75d7cf72 AX |
2291 | "cannot %sreset port %d (err = %d)\n", |
2292 | warm ? "warm " : "", port1, status); | |
2293 | } else { | |
2294 | status = hub_port_wait_reset(hub, port1, udev, delay, | |
2295 | warm); | |
dd16525b | 2296 | if (status && status != -ENOTCONN) |
1da177e4 LT |
2297 | dev_dbg(hub->intfdev, |
2298 | "port_wait_reset: err = %d\n", | |
2299 | status); | |
2300 | } | |
2301 | ||
2302 | /* return on disconnect or reset */ | |
75d7cf72 AX |
2303 | if (status == 0 || status == -ENOTCONN || status == -ENODEV) { |
2304 | hub_port_finish_reset(hub, port1, udev, &status, warm); | |
32fe0198 | 2305 | goto done; |
1da177e4 LT |
2306 | } |
2307 | ||
2308 | dev_dbg (hub->intfdev, | |
75d7cf72 AX |
2309 | "port %d not enabled, trying %sreset again...\n", |
2310 | port1, warm ? "warm " : ""); | |
1da177e4 LT |
2311 | delay = HUB_LONG_RESET_TIME; |
2312 | } | |
2313 | ||
2314 | dev_err (hub->intfdev, | |
2315 | "Cannot enable port %i. Maybe the USB cable is bad?\n", | |
2316 | port1); | |
2317 | ||
75d7cf72 AX |
2318 | done: |
2319 | if (!warm) | |
2320 | up_read(&ehci_cf_port_reset_rwsem); | |
5e467f6e | 2321 | |
75d7cf72 | 2322 | return status; |
5e467f6e AX |
2323 | } |
2324 | ||
0ed9a57e AX |
2325 | /* Check if a port is power on */ |
2326 | static int port_is_power_on(struct usb_hub *hub, unsigned portstatus) | |
2327 | { | |
2328 | int ret = 0; | |
2329 | ||
2330 | if (hub_is_superspeed(hub->hdev)) { | |
2331 | if (portstatus & USB_SS_PORT_STAT_POWER) | |
2332 | ret = 1; | |
2333 | } else { | |
2334 | if (portstatus & USB_PORT_STAT_POWER) | |
2335 | ret = 1; | |
2336 | } | |
2337 | ||
2338 | return ret; | |
2339 | } | |
2340 | ||
d388dab7 | 2341 | #ifdef CONFIG_PM |
1da177e4 | 2342 | |
0ed9a57e AX |
2343 | /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ |
2344 | static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) | |
2345 | { | |
2346 | int ret = 0; | |
2347 | ||
2348 | if (hub_is_superspeed(hub->hdev)) { | |
2349 | if ((portstatus & USB_PORT_STAT_LINK_STATE) | |
2350 | == USB_SS_PORT_LS_U3) | |
2351 | ret = 1; | |
2352 | } else { | |
2353 | if (portstatus & USB_PORT_STAT_SUSPEND) | |
2354 | ret = 1; | |
2355 | } | |
2356 | ||
2357 | return ret; | |
2358 | } | |
b01b03f3 AS |
2359 | |
2360 | /* Determine whether the device on a port is ready for a normal resume, | |
2361 | * is ready for a reset-resume, or should be disconnected. | |
2362 | */ | |
2363 | static int check_port_resume_type(struct usb_device *udev, | |
2364 | struct usb_hub *hub, int port1, | |
2365 | int status, unsigned portchange, unsigned portstatus) | |
2366 | { | |
2367 | /* Is the device still present? */ | |
0ed9a57e AX |
2368 | if (status || port_is_suspended(hub, portstatus) || |
2369 | !port_is_power_on(hub, portstatus) || | |
2370 | !(portstatus & USB_PORT_STAT_CONNECTION)) { | |
b01b03f3 AS |
2371 | if (status >= 0) |
2372 | status = -ENODEV; | |
2373 | } | |
2374 | ||
86c57edf AS |
2375 | /* Can't do a normal resume if the port isn't enabled, |
2376 | * so try a reset-resume instead. | |
2377 | */ | |
2378 | else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { | |
2379 | if (udev->persist_enabled) | |
2380 | udev->reset_resume = 1; | |
2381 | else | |
2382 | status = -ENODEV; | |
2383 | } | |
b01b03f3 AS |
2384 | |
2385 | if (status) { | |
2386 | dev_dbg(hub->intfdev, | |
2387 | "port %d status %04x.%04x after resume, %d\n", | |
2388 | port1, portchange, portstatus, status); | |
2389 | } else if (udev->reset_resume) { | |
2390 | ||
2391 | /* Late port handoff can set status-change bits */ | |
2392 | if (portchange & USB_PORT_STAT_C_CONNECTION) | |
2393 | clear_port_feature(hub->hdev, port1, | |
2394 | USB_PORT_FEAT_C_CONNECTION); | |
2395 | if (portchange & USB_PORT_STAT_C_ENABLE) | |
2396 | clear_port_feature(hub->hdev, port1, | |
2397 | USB_PORT_FEAT_C_ENABLE); | |
2398 | } | |
2399 | ||
2400 | return status; | |
2401 | } | |
2402 | ||
1da177e4 LT |
2403 | #ifdef CONFIG_USB_SUSPEND |
2404 | ||
2405 | /* | |
624d6c07 AS |
2406 | * usb_port_suspend - suspend a usb device's upstream port |
2407 | * @udev: device that's no longer in active use, not a root hub | |
2408 | * Context: must be able to sleep; device not locked; pm locks held | |
2409 | * | |
2410 | * Suspends a USB device that isn't in active use, conserving power. | |
2411 | * Devices may wake out of a suspend, if anything important happens, | |
2412 | * using the remote wakeup mechanism. They may also be taken out of | |
2413 | * suspend by the host, using usb_port_resume(). It's also routine | |
2414 | * to disconnect devices while they are suspended. | |
2415 | * | |
2416 | * This only affects the USB hardware for a device; its interfaces | |
2417 | * (and, for hubs, child devices) must already have been suspended. | |
2418 | * | |
1da177e4 LT |
2419 | * Selective port suspend reduces power; most suspended devices draw |
2420 | * less than 500 uA. It's also used in OTG, along with remote wakeup. | |
2421 | * All devices below the suspended port are also suspended. | |
2422 | * | |
2423 | * Devices leave suspend state when the host wakes them up. Some devices | |
2424 | * also support "remote wakeup", where the device can activate the USB | |
2425 | * tree above them to deliver data, such as a keypress or packet. In | |
2426 | * some cases, this wakes the USB host. | |
624d6c07 AS |
2427 | * |
2428 | * Suspending OTG devices may trigger HNP, if that's been enabled | |
2429 | * between a pair of dual-role devices. That will change roles, such | |
2430 | * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. | |
2431 | * | |
2432 | * Devices on USB hub ports have only one "suspend" state, corresponding | |
2433 | * to ACPI D2, "may cause the device to lose some context". | |
2434 | * State transitions include: | |
2435 | * | |
2436 | * - suspend, resume ... when the VBUS power link stays live | |
2437 | * - suspend, disconnect ... VBUS lost | |
2438 | * | |
2439 | * Once VBUS drop breaks the circuit, the port it's using has to go through | |
2440 | * normal re-enumeration procedures, starting with enabling VBUS power. | |
2441 | * Other than re-initializing the hub (plug/unplug, except for root hubs), | |
2442 | * Linux (2.6) currently has NO mechanisms to initiate that: no khubd | |
2443 | * timer, no SRP, no requests through sysfs. | |
2444 | * | |
2445 | * If CONFIG_USB_SUSPEND isn't enabled, devices only really suspend when | |
2446 | * the root hub for their bus goes into global suspend ... so we don't | |
2447 | * (falsely) update the device power state to say it suspended. | |
2448 | * | |
2449 | * Returns 0 on success, else negative errno. | |
1da177e4 | 2450 | */ |
65bfd296 | 2451 | int usb_port_suspend(struct usb_device *udev, pm_message_t msg) |
1da177e4 | 2452 | { |
624d6c07 AS |
2453 | struct usb_hub *hub = hdev_to_hub(udev->parent); |
2454 | int port1 = udev->portnum; | |
2455 | int status; | |
1da177e4 | 2456 | |
1da177e4 LT |
2457 | /* enable remote wakeup when appropriate; this lets the device |
2458 | * wake up the upstream hub (including maybe the root hub). | |
2459 | * | |
2460 | * NOTE: OTG devices may issue remote wakeup (or SRP) even when | |
2461 | * we don't explicitly enable it here. | |
2462 | */ | |
645daaab | 2463 | if (udev->do_remote_wakeup) { |
623bef9e SS |
2464 | if (!hub_is_superspeed(hub->hdev)) { |
2465 | status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
2466 | USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, | |
2467 | USB_DEVICE_REMOTE_WAKEUP, 0, | |
2468 | NULL, 0, | |
2469 | USB_CTRL_SET_TIMEOUT); | |
2470 | } else { | |
2471 | /* Assume there's only one function on the USB 3.0 | |
2472 | * device and enable remote wake for the first | |
2473 | * interface. FIXME if the interface association | |
2474 | * descriptor shows there's more than one function. | |
2475 | */ | |
2476 | status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
2477 | USB_REQ_SET_FEATURE, | |
2478 | USB_RECIP_INTERFACE, | |
2479 | USB_INTRF_FUNC_SUSPEND, | |
3b9b6acd SS |
2480 | USB_INTRF_FUNC_SUSPEND_RW | |
2481 | USB_INTRF_FUNC_SUSPEND_LP, | |
623bef9e SS |
2482 | NULL, 0, |
2483 | USB_CTRL_SET_TIMEOUT); | |
2484 | } | |
0c487206 | 2485 | if (status) { |
624d6c07 AS |
2486 | dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", |
2487 | status); | |
0c487206 | 2488 | /* bail if autosuspend is requested */ |
5b1b0b81 | 2489 | if (PMSG_IS_AUTO(msg)) |
0c487206 ON |
2490 | return status; |
2491 | } | |
1da177e4 LT |
2492 | } |
2493 | ||
65580b43 AX |
2494 | /* disable USB2 hardware LPM */ |
2495 | if (udev->usb2_hw_lpm_enabled == 1) | |
2496 | usb_set_usb2_hardware_lpm(udev, 0); | |
2497 | ||
1da177e4 | 2498 | /* see 7.1.7.6 */ |
a7114230 AX |
2499 | if (hub_is_superspeed(hub->hdev)) |
2500 | status = set_port_feature(hub->hdev, | |
2501 | port1 | (USB_SS_PORT_LS_U3 << 3), | |
2502 | USB_PORT_FEAT_LINK_STATE); | |
a8f08d86 AX |
2503 | else |
2504 | status = set_port_feature(hub->hdev, port1, | |
2505 | USB_PORT_FEAT_SUSPEND); | |
1da177e4 | 2506 | if (status) { |
624d6c07 AS |
2507 | dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n", |
2508 | port1, status); | |
1da177e4 | 2509 | /* paranoia: "should not happen" */ |
0c487206 ON |
2510 | if (udev->do_remote_wakeup) |
2511 | (void) usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
1da177e4 LT |
2512 | USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, |
2513 | USB_DEVICE_REMOTE_WAKEUP, 0, | |
2514 | NULL, 0, | |
2515 | USB_CTRL_SET_TIMEOUT); | |
cbb33004 | 2516 | |
c3e751e4 AX |
2517 | /* Try to enable USB2 hardware LPM again */ |
2518 | if (udev->usb2_hw_lpm_capable == 1) | |
2519 | usb_set_usb2_hardware_lpm(udev, 1); | |
2520 | ||
cbb33004 | 2521 | /* System sleep transitions should never fail */ |
5b1b0b81 | 2522 | if (!PMSG_IS_AUTO(msg)) |
cbb33004 | 2523 | status = 0; |
1da177e4 LT |
2524 | } else { |
2525 | /* device has up to 10 msec to fully suspend */ | |
30b1a7a3 AS |
2526 | dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", |
2527 | (PMSG_IS_AUTO(msg) ? "auto-" : ""), | |
2528 | udev->do_remote_wakeup); | |
1da177e4 LT |
2529 | usb_set_device_state(udev, USB_STATE_SUSPENDED); |
2530 | msleep(10); | |
2531 | } | |
c08512c7 | 2532 | usb_mark_last_busy(hub->hdev); |
1da177e4 LT |
2533 | return status; |
2534 | } | |
2535 | ||
1da177e4 | 2536 | /* |
390a8c34 DB |
2537 | * If the USB "suspend" state is in use (rather than "global suspend"), |
2538 | * many devices will be individually taken out of suspend state using | |
54515fe5 | 2539 | * special "resume" signaling. This routine kicks in shortly after |
1da177e4 LT |
2540 | * hardware resume signaling is finished, either because of selective |
2541 | * resume (by host) or remote wakeup (by device) ... now see what changed | |
2542 | * in the tree that's rooted at this device. | |
54515fe5 AS |
2543 | * |
2544 | * If @udev->reset_resume is set then the device is reset before the | |
2545 | * status check is done. | |
1da177e4 | 2546 | */ |
140d8f68 | 2547 | static int finish_port_resume(struct usb_device *udev) |
1da177e4 | 2548 | { |
54515fe5 | 2549 | int status = 0; |
1da177e4 LT |
2550 | u16 devstatus; |
2551 | ||
2552 | /* caller owns the udev device lock */ | |
b9cef6c3 WF |
2553 | dev_dbg(&udev->dev, "%s\n", |
2554 | udev->reset_resume ? "finish reset-resume" : "finish resume"); | |
1da177e4 LT |
2555 | |
2556 | /* usb ch9 identifies four variants of SUSPENDED, based on what | |
2557 | * state the device resumes to. Linux currently won't see the | |
2558 | * first two on the host side; they'd be inside hub_port_init() | |
2559 | * during many timeouts, but khubd can't suspend until later. | |
2560 | */ | |
2561 | usb_set_device_state(udev, udev->actconfig | |
2562 | ? USB_STATE_CONFIGURED | |
2563 | : USB_STATE_ADDRESS); | |
1da177e4 | 2564 | |
54515fe5 AS |
2565 | /* 10.5.4.5 says not to reset a suspended port if the attached |
2566 | * device is enabled for remote wakeup. Hence the reset | |
2567 | * operation is carried out here, after the port has been | |
2568 | * resumed. | |
2569 | */ | |
2570 | if (udev->reset_resume) | |
86c57edf | 2571 | retry_reset_resume: |
742120c6 | 2572 | status = usb_reset_and_verify_device(udev); |
54515fe5 | 2573 | |
1da177e4 LT |
2574 | /* 10.5.4.5 says be sure devices in the tree are still there. |
2575 | * For now let's assume the device didn't go crazy on resume, | |
2576 | * and device drivers will know about any resume quirks. | |
2577 | */ | |
54515fe5 | 2578 | if (status == 0) { |
46dede46 | 2579 | devstatus = 0; |
54515fe5 AS |
2580 | status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); |
2581 | if (status >= 0) | |
46dede46 | 2582 | status = (status > 0 ? 0 : -ENODEV); |
86c57edf AS |
2583 | |
2584 | /* If a normal resume failed, try doing a reset-resume */ | |
2585 | if (status && !udev->reset_resume && udev->persist_enabled) { | |
2586 | dev_dbg(&udev->dev, "retry with reset-resume\n"); | |
2587 | udev->reset_resume = 1; | |
2588 | goto retry_reset_resume; | |
2589 | } | |
54515fe5 | 2590 | } |
b40b7a90 | 2591 | |
624d6c07 AS |
2592 | if (status) { |
2593 | dev_dbg(&udev->dev, "gone after usb resume? status %d\n", | |
2594 | status); | |
2595 | } else if (udev->actconfig) { | |
1da177e4 | 2596 | le16_to_cpus(&devstatus); |
686314cf | 2597 | if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) { |
1da177e4 LT |
2598 | status = usb_control_msg(udev, |
2599 | usb_sndctrlpipe(udev, 0), | |
2600 | USB_REQ_CLEAR_FEATURE, | |
2601 | USB_RECIP_DEVICE, | |
2602 | USB_DEVICE_REMOTE_WAKEUP, 0, | |
2603 | NULL, 0, | |
2604 | USB_CTRL_SET_TIMEOUT); | |
a8e7c565 | 2605 | if (status) |
b9cef6c3 WF |
2606 | dev_dbg(&udev->dev, |
2607 | "disable remote wakeup, status %d\n", | |
2608 | status); | |
4bf0ba86 | 2609 | } |
1da177e4 | 2610 | status = 0; |
1da177e4 LT |
2611 | } |
2612 | return status; | |
2613 | } | |
2614 | ||
624d6c07 AS |
2615 | /* |
2616 | * usb_port_resume - re-activate a suspended usb device's upstream port | |
2617 | * @udev: device to re-activate, not a root hub | |
2618 | * Context: must be able to sleep; device not locked; pm locks held | |
2619 | * | |
2620 | * This will re-activate the suspended device, increasing power usage | |
2621 | * while letting drivers communicate again with its endpoints. | |
2622 | * USB resume explicitly guarantees that the power session between | |
2623 | * the host and the device is the same as it was when the device | |
2624 | * suspended. | |
2625 | * | |
feccc30d AS |
2626 | * If @udev->reset_resume is set then this routine won't check that the |
2627 | * port is still enabled. Furthermore, finish_port_resume() above will | |
54515fe5 AS |
2628 | * reset @udev. The end result is that a broken power session can be |
2629 | * recovered and @udev will appear to persist across a loss of VBUS power. | |
2630 | * | |
2631 | * For example, if a host controller doesn't maintain VBUS suspend current | |
2632 | * during a system sleep or is reset when the system wakes up, all the USB | |
2633 | * power sessions below it will be broken. This is especially troublesome | |
2634 | * for mass-storage devices containing mounted filesystems, since the | |
2635 | * device will appear to have disconnected and all the memory mappings | |
2636 | * to it will be lost. Using the USB_PERSIST facility, the device can be | |
2637 | * made to appear as if it had not disconnected. | |
2638 | * | |
742120c6 | 2639 | * This facility can be dangerous. Although usb_reset_and_verify_device() makes |
feccc30d | 2640 | * every effort to insure that the same device is present after the |
54515fe5 AS |
2641 | * reset as before, it cannot provide a 100% guarantee. Furthermore it's |
2642 | * quite possible for a device to remain unaltered but its media to be | |
2643 | * changed. If the user replaces a flash memory card while the system is | |
2644 | * asleep, he will have only himself to blame when the filesystem on the | |
2645 | * new card is corrupted and the system crashes. | |
2646 | * | |
624d6c07 AS |
2647 | * Returns 0 on success, else negative errno. |
2648 | */ | |
65bfd296 | 2649 | int usb_port_resume(struct usb_device *udev, pm_message_t msg) |
1da177e4 | 2650 | { |
624d6c07 AS |
2651 | struct usb_hub *hub = hdev_to_hub(udev->parent); |
2652 | int port1 = udev->portnum; | |
2653 | int status; | |
2654 | u16 portchange, portstatus; | |
d25450c6 AS |
2655 | |
2656 | /* Skip the initial Clear-Suspend step for a remote wakeup */ | |
2657 | status = hub_port_status(hub, port1, &portstatus, &portchange); | |
0ed9a57e | 2658 | if (status == 0 && !port_is_suspended(hub, portstatus)) |
d25450c6 | 2659 | goto SuspendCleared; |
1da177e4 LT |
2660 | |
2661 | // dev_dbg(hub->intfdev, "resume port %d\n", port1); | |
2662 | ||
d5cbad4b AS |
2663 | set_bit(port1, hub->busy_bits); |
2664 | ||
1da177e4 | 2665 | /* see 7.1.7.7; affects power usage, but not budgeting */ |
a7114230 AX |
2666 | if (hub_is_superspeed(hub->hdev)) |
2667 | status = set_port_feature(hub->hdev, | |
2668 | port1 | (USB_SS_PORT_LS_U0 << 3), | |
2669 | USB_PORT_FEAT_LINK_STATE); | |
2670 | else | |
2671 | status = clear_port_feature(hub->hdev, | |
2672 | port1, USB_PORT_FEAT_SUSPEND); | |
1da177e4 | 2673 | if (status) { |
624d6c07 AS |
2674 | dev_dbg(hub->intfdev, "can't resume port %d, status %d\n", |
2675 | port1, status); | |
1da177e4 | 2676 | } else { |
1da177e4 | 2677 | /* drive resume for at least 20 msec */ |
686314cf | 2678 | dev_dbg(&udev->dev, "usb %sresume\n", |
5b1b0b81 | 2679 | (PMSG_IS_AUTO(msg) ? "auto-" : "")); |
1da177e4 LT |
2680 | msleep(25); |
2681 | ||
1da177e4 LT |
2682 | /* Virtual root hubs can trigger on GET_PORT_STATUS to |
2683 | * stop resume signaling. Then finish the resume | |
2684 | * sequence. | |
2685 | */ | |
d25450c6 | 2686 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
54515fe5 | 2687 | |
b01b03f3 AS |
2688 | /* TRSMRCY = 10 msec */ |
2689 | msleep(10); | |
2690 | } | |
2691 | ||
54515fe5 | 2692 | SuspendCleared: |
b01b03f3 | 2693 | if (status == 0) { |
a7114230 AX |
2694 | if (hub_is_superspeed(hub->hdev)) { |
2695 | if (portchange & USB_PORT_STAT_C_LINK_STATE) | |
2696 | clear_port_feature(hub->hdev, port1, | |
2697 | USB_PORT_FEAT_C_PORT_LINK_STATE); | |
2698 | } else { | |
2699 | if (portchange & USB_PORT_STAT_C_SUSPEND) | |
2700 | clear_port_feature(hub->hdev, port1, | |
2701 | USB_PORT_FEAT_C_SUSPEND); | |
2702 | } | |
1da177e4 | 2703 | } |
1da177e4 | 2704 | |
d5cbad4b | 2705 | clear_bit(port1, hub->busy_bits); |
d5cbad4b | 2706 | |
b01b03f3 AS |
2707 | status = check_port_resume_type(udev, |
2708 | hub, port1, status, portchange, portstatus); | |
54515fe5 AS |
2709 | if (status == 0) |
2710 | status = finish_port_resume(udev); | |
2711 | if (status < 0) { | |
2712 | dev_dbg(&udev->dev, "can't resume, status %d\n", status); | |
2713 | hub_port_logical_disconnect(hub, port1); | |
65580b43 AX |
2714 | } else { |
2715 | /* Try to enable USB2 hardware LPM */ | |
2716 | if (udev->usb2_hw_lpm_capable == 1) | |
2717 | usb_set_usb2_hardware_lpm(udev, 1); | |
54515fe5 | 2718 | } |
65580b43 | 2719 | |
1da177e4 LT |
2720 | return status; |
2721 | } | |
2722 | ||
8808f00c | 2723 | /* caller has locked udev */ |
0534d468 | 2724 | int usb_remote_wakeup(struct usb_device *udev) |
1da177e4 LT |
2725 | { |
2726 | int status = 0; | |
2727 | ||
1da177e4 | 2728 | if (udev->state == USB_STATE_SUSPENDED) { |
645daaab | 2729 | dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); |
9bbdf1e0 AS |
2730 | status = usb_autoresume_device(udev); |
2731 | if (status == 0) { | |
2732 | /* Let the drivers do their thing, then... */ | |
2733 | usb_autosuspend_device(udev); | |
2734 | } | |
1da177e4 | 2735 | } |
1da177e4 LT |
2736 | return status; |
2737 | } | |
2738 | ||
d388dab7 AS |
2739 | #else /* CONFIG_USB_SUSPEND */ |
2740 | ||
2741 | /* When CONFIG_USB_SUSPEND isn't set, we never suspend or resume any ports. */ | |
2742 | ||
65bfd296 | 2743 | int usb_port_suspend(struct usb_device *udev, pm_message_t msg) |
d388dab7 AS |
2744 | { |
2745 | return 0; | |
2746 | } | |
2747 | ||
b01b03f3 AS |
2748 | /* However we may need to do a reset-resume */ |
2749 | ||
65bfd296 | 2750 | int usb_port_resume(struct usb_device *udev, pm_message_t msg) |
d388dab7 | 2751 | { |
b01b03f3 AS |
2752 | struct usb_hub *hub = hdev_to_hub(udev->parent); |
2753 | int port1 = udev->portnum; | |
2754 | int status; | |
2755 | u16 portchange, portstatus; | |
2756 | ||
2757 | status = hub_port_status(hub, port1, &portstatus, &portchange); | |
2758 | status = check_port_resume_type(udev, | |
2759 | hub, port1, status, portchange, portstatus); | |
54515fe5 | 2760 | |
b01b03f3 AS |
2761 | if (status) { |
2762 | dev_dbg(&udev->dev, "can't resume, status %d\n", status); | |
2763 | hub_port_logical_disconnect(hub, port1); | |
2764 | } else if (udev->reset_resume) { | |
54515fe5 | 2765 | dev_dbg(&udev->dev, "reset-resume\n"); |
742120c6 | 2766 | status = usb_reset_and_verify_device(udev); |
54515fe5 AS |
2767 | } |
2768 | return status; | |
d388dab7 AS |
2769 | } |
2770 | ||
d388dab7 AS |
2771 | #endif |
2772 | ||
db690874 | 2773 | static int hub_suspend(struct usb_interface *intf, pm_message_t msg) |
1da177e4 LT |
2774 | { |
2775 | struct usb_hub *hub = usb_get_intfdata (intf); | |
2776 | struct usb_device *hdev = hub->hdev; | |
2777 | unsigned port1; | |
4296c70a | 2778 | int status; |
1da177e4 | 2779 | |
cbb33004 | 2780 | /* Warn if children aren't already suspended */ |
1da177e4 LT |
2781 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { |
2782 | struct usb_device *udev; | |
2783 | ||
fa286188 | 2784 | udev = hdev->children [port1-1]; |
6840d255 | 2785 | if (udev && udev->can_submit) { |
cbb33004 | 2786 | dev_warn(&intf->dev, "port %d nyet suspended\n", port1); |
5b1b0b81 | 2787 | if (PMSG_IS_AUTO(msg)) |
cbb33004 | 2788 | return -EBUSY; |
c9f89fa4 | 2789 | } |
1da177e4 | 2790 | } |
4296c70a SS |
2791 | if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { |
2792 | /* Enable hub to send remote wakeup for all ports. */ | |
2793 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { | |
2794 | status = set_port_feature(hdev, | |
2795 | port1 | | |
2796 | USB_PORT_FEAT_REMOTE_WAKE_CONNECT | | |
2797 | USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | | |
2798 | USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, | |
2799 | USB_PORT_FEAT_REMOTE_WAKE_MASK); | |
2800 | } | |
2801 | } | |
1da177e4 | 2802 | |
441b62c1 | 2803 | dev_dbg(&intf->dev, "%s\n", __func__); |
40f122f3 | 2804 | |
12f1ff83 | 2805 | /* stop khubd and related activity */ |
4330354f | 2806 | hub_quiesce(hub, HUB_SUSPEND); |
b6f6436d | 2807 | return 0; |
1da177e4 LT |
2808 | } |
2809 | ||
2810 | static int hub_resume(struct usb_interface *intf) | |
2811 | { | |
5e6effae | 2812 | struct usb_hub *hub = usb_get_intfdata(intf); |
40f122f3 | 2813 | |
5e6effae | 2814 | dev_dbg(&intf->dev, "%s\n", __func__); |
f2835219 | 2815 | hub_activate(hub, HUB_RESUME); |
1da177e4 LT |
2816 | return 0; |
2817 | } | |
2818 | ||
b41a60ec | 2819 | static int hub_reset_resume(struct usb_interface *intf) |
f07600cf | 2820 | { |
b41a60ec | 2821 | struct usb_hub *hub = usb_get_intfdata(intf); |
f07600cf | 2822 | |
5e6effae | 2823 | dev_dbg(&intf->dev, "%s\n", __func__); |
f2835219 | 2824 | hub_activate(hub, HUB_RESET_RESUME); |
f07600cf AS |
2825 | return 0; |
2826 | } | |
2827 | ||
54515fe5 AS |
2828 | /** |
2829 | * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power | |
2830 | * @rhdev: struct usb_device for the root hub | |
2831 | * | |
2832 | * The USB host controller driver calls this function when its root hub | |
2833 | * is resumed and Vbus power has been interrupted or the controller | |
feccc30d AS |
2834 | * has been reset. The routine marks @rhdev as having lost power. |
2835 | * When the hub driver is resumed it will take notice and carry out | |
2836 | * power-session recovery for all the "USB-PERSIST"-enabled child devices; | |
2837 | * the others will be disconnected. | |
54515fe5 AS |
2838 | */ |
2839 | void usb_root_hub_lost_power(struct usb_device *rhdev) | |
2840 | { | |
2841 | dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); | |
2842 | rhdev->reset_resume = 1; | |
2843 | } | |
2844 | EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); | |
2845 | ||
d388dab7 AS |
2846 | #else /* CONFIG_PM */ |
2847 | ||
f07600cf AS |
2848 | #define hub_suspend NULL |
2849 | #define hub_resume NULL | |
2850 | #define hub_reset_resume NULL | |
d388dab7 AS |
2851 | #endif |
2852 | ||
1da177e4 LT |
2853 | |
2854 | /* USB 2.0 spec, 7.1.7.3 / fig 7-29: | |
2855 | * | |
2856 | * Between connect detection and reset signaling there must be a delay | |
2857 | * of 100ms at least for debounce and power-settling. The corresponding | |
2858 | * timer shall restart whenever the downstream port detects a disconnect. | |
2859 | * | |
2860 | * Apparently there are some bluetooth and irda-dongles and a number of | |
2861 | * low-speed devices for which this debounce period may last over a second. | |
2862 | * Not covered by the spec - but easy to deal with. | |
2863 | * | |
2864 | * This implementation uses a 1500ms total debounce timeout; if the | |
2865 | * connection isn't stable by then it returns -ETIMEDOUT. It checks | |
2866 | * every 25ms for transient disconnects. When the port status has been | |
2867 | * unchanged for 100ms it returns the port status. | |
2868 | */ | |
1da177e4 LT |
2869 | static int hub_port_debounce(struct usb_hub *hub, int port1) |
2870 | { | |
2871 | int ret; | |
2872 | int total_time, stable_time = 0; | |
2873 | u16 portchange, portstatus; | |
2874 | unsigned connection = 0xffff; | |
2875 | ||
2876 | for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { | |
2877 | ret = hub_port_status(hub, port1, &portstatus, &portchange); | |
2878 | if (ret < 0) | |
2879 | return ret; | |
2880 | ||
2881 | if (!(portchange & USB_PORT_STAT_C_CONNECTION) && | |
2882 | (portstatus & USB_PORT_STAT_CONNECTION) == connection) { | |
2883 | stable_time += HUB_DEBOUNCE_STEP; | |
2884 | if (stable_time >= HUB_DEBOUNCE_STABLE) | |
2885 | break; | |
2886 | } else { | |
2887 | stable_time = 0; | |
2888 | connection = portstatus & USB_PORT_STAT_CONNECTION; | |
2889 | } | |
2890 | ||
2891 | if (portchange & USB_PORT_STAT_C_CONNECTION) { | |
2892 | clear_port_feature(hub->hdev, port1, | |
2893 | USB_PORT_FEAT_C_CONNECTION); | |
2894 | } | |
2895 | ||
2896 | if (total_time >= HUB_DEBOUNCE_TIMEOUT) | |
2897 | break; | |
2898 | msleep(HUB_DEBOUNCE_STEP); | |
2899 | } | |
2900 | ||
2901 | dev_dbg (hub->intfdev, | |
2902 | "debounce: port %d: total %dms stable %dms status 0x%x\n", | |
2903 | port1, total_time, stable_time, portstatus); | |
2904 | ||
2905 | if (stable_time < HUB_DEBOUNCE_STABLE) | |
2906 | return -ETIMEDOUT; | |
2907 | return portstatus; | |
2908 | } | |
2909 | ||
fc721f51 | 2910 | void usb_ep0_reinit(struct usb_device *udev) |
1da177e4 | 2911 | { |
ddeac4e7 AS |
2912 | usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); |
2913 | usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); | |
2caf7fcd | 2914 | usb_enable_endpoint(udev, &udev->ep0, true); |
1da177e4 | 2915 | } |
fc721f51 | 2916 | EXPORT_SYMBOL_GPL(usb_ep0_reinit); |
1da177e4 LT |
2917 | |
2918 | #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) | |
2919 | #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) | |
2920 | ||
4326ed0b | 2921 | static int hub_set_address(struct usb_device *udev, int devnum) |
1da177e4 LT |
2922 | { |
2923 | int retval; | |
c6515272 | 2924 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
1da177e4 | 2925 | |
c6515272 SS |
2926 | /* |
2927 | * The host controller will choose the device address, | |
2928 | * instead of the core having chosen it earlier | |
2929 | */ | |
2930 | if (!hcd->driver->address_device && devnum <= 1) | |
1da177e4 LT |
2931 | return -EINVAL; |
2932 | if (udev->state == USB_STATE_ADDRESS) | |
2933 | return 0; | |
2934 | if (udev->state != USB_STATE_DEFAULT) | |
2935 | return -EINVAL; | |
c8d4af8e | 2936 | if (hcd->driver->address_device) |
c6515272 | 2937 | retval = hcd->driver->address_device(hcd, udev); |
c8d4af8e | 2938 | else |
c6515272 SS |
2939 | retval = usb_control_msg(udev, usb_sndaddr0pipe(), |
2940 | USB_REQ_SET_ADDRESS, 0, devnum, 0, | |
2941 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
1da177e4 | 2942 | if (retval == 0) { |
3b29b68b | 2943 | update_devnum(udev, devnum); |
4953d141 | 2944 | /* Device now using proper address. */ |
1da177e4 | 2945 | usb_set_device_state(udev, USB_STATE_ADDRESS); |
fc721f51 | 2946 | usb_ep0_reinit(udev); |
1da177e4 LT |
2947 | } |
2948 | return retval; | |
2949 | } | |
2950 | ||
2951 | /* Reset device, (re)assign address, get device descriptor. | |
2952 | * Device connection must be stable, no more debouncing needed. | |
2953 | * Returns device in USB_STATE_ADDRESS, except on error. | |
2954 | * | |
2955 | * If this is called for an already-existing device (as part of | |
742120c6 | 2956 | * usb_reset_and_verify_device), the caller must own the device lock. For a |
1da177e4 LT |
2957 | * newly detected device that is not accessible through any global |
2958 | * pointers, it's not necessary to lock the device. | |
2959 | */ | |
2960 | static int | |
2961 | hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, | |
2962 | int retry_counter) | |
2963 | { | |
4186ecf8 | 2964 | static DEFINE_MUTEX(usb_address0_mutex); |
1da177e4 LT |
2965 | |
2966 | struct usb_device *hdev = hub->hdev; | |
e7b77172 | 2967 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
1da177e4 LT |
2968 | int i, j, retval; |
2969 | unsigned delay = HUB_SHORT_RESET_TIME; | |
2970 | enum usb_device_speed oldspeed = udev->speed; | |
e538dfda | 2971 | const char *speed; |
4326ed0b | 2972 | int devnum = udev->devnum; |
1da177e4 LT |
2973 | |
2974 | /* root hub ports have a slightly longer reset period | |
2975 | * (from USB 2.0 spec, section 7.1.7.5) | |
2976 | */ | |
2977 | if (!hdev->parent) { | |
2978 | delay = HUB_ROOT_RESET_TIME; | |
2979 | if (port1 == hdev->bus->otg_port) | |
2980 | hdev->bus->b_hnp_enable = 0; | |
2981 | } | |
2982 | ||
2983 | /* Some low speed devices have problems with the quick delay, so */ | |
2984 | /* be a bit pessimistic with those devices. RHbug #23670 */ | |
2985 | if (oldspeed == USB_SPEED_LOW) | |
2986 | delay = HUB_LONG_RESET_TIME; | |
2987 | ||
4186ecf8 | 2988 | mutex_lock(&usb_address0_mutex); |
1da177e4 | 2989 | |
07194ab7 LT |
2990 | /* Reset the device; full speed may morph to high speed */ |
2991 | /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ | |
75d7cf72 | 2992 | retval = hub_port_reset(hub, port1, udev, delay, false); |
07194ab7 LT |
2993 | if (retval < 0) /* error or disconnect */ |
2994 | goto fail; | |
2995 | /* success, speed is known */ | |
2996 | ||
1da177e4 LT |
2997 | retval = -ENODEV; |
2998 | ||
2999 | if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { | |
3000 | dev_dbg(&udev->dev, "device reset changed speed!\n"); | |
3001 | goto fail; | |
3002 | } | |
3003 | oldspeed = udev->speed; | |
4326ed0b | 3004 | |
1da177e4 LT |
3005 | /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... |
3006 | * it's fixed size except for full speed devices. | |
5bb6e0ae IPG |
3007 | * For Wireless USB devices, ep0 max packet is always 512 (tho |
3008 | * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. | |
1da177e4 LT |
3009 | */ |
3010 | switch (udev->speed) { | |
6b403b02 | 3011 | case USB_SPEED_SUPER: |
551cdbbe | 3012 | case USB_SPEED_WIRELESS: /* fixed at 512 */ |
551509d2 | 3013 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); |
5bb6e0ae | 3014 | break; |
1da177e4 | 3015 | case USB_SPEED_HIGH: /* fixed at 64 */ |
551509d2 | 3016 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
1da177e4 LT |
3017 | break; |
3018 | case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ | |
3019 | /* to determine the ep0 maxpacket size, try to read | |
3020 | * the device descriptor to get bMaxPacketSize0 and | |
3021 | * then correct our initial guess. | |
3022 | */ | |
551509d2 | 3023 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
1da177e4 LT |
3024 | break; |
3025 | case USB_SPEED_LOW: /* fixed at 8 */ | |
551509d2 | 3026 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); |
1da177e4 LT |
3027 | break; |
3028 | default: | |
3029 | goto fail; | |
3030 | } | |
e538dfda MN |
3031 | |
3032 | if (udev->speed == USB_SPEED_WIRELESS) | |
3033 | speed = "variable speed Wireless"; | |
3034 | else | |
3035 | speed = usb_speed_string(udev->speed); | |
3036 | ||
c6515272 SS |
3037 | if (udev->speed != USB_SPEED_SUPER) |
3038 | dev_info(&udev->dev, | |
e538dfda MN |
3039 | "%s %s USB device number %d using %s\n", |
3040 | (udev->config) ? "reset" : "new", speed, | |
3b29b68b | 3041 | devnum, udev->bus->controller->driver->name); |
1da177e4 LT |
3042 | |
3043 | /* Set up TT records, if needed */ | |
3044 | if (hdev->tt) { | |
3045 | udev->tt = hdev->tt; | |
3046 | udev->ttport = hdev->ttport; | |
3047 | } else if (udev->speed != USB_SPEED_HIGH | |
3048 | && hdev->speed == USB_SPEED_HIGH) { | |
d199c96d AS |
3049 | if (!hub->tt.hub) { |
3050 | dev_err(&udev->dev, "parent hub has no TT\n"); | |
3051 | retval = -EINVAL; | |
3052 | goto fail; | |
3053 | } | |
1da177e4 LT |
3054 | udev->tt = &hub->tt; |
3055 | udev->ttport = port1; | |
3056 | } | |
3057 | ||
3058 | /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? | |
3059 | * Because device hardware and firmware is sometimes buggy in | |
3060 | * this area, and this is how Linux has done it for ages. | |
3061 | * Change it cautiously. | |
3062 | * | |
3063 | * NOTE: If USE_NEW_SCHEME() is true we will start by issuing | |
3064 | * a 64-byte GET_DESCRIPTOR request. This is what Windows does, | |
3065 | * so it may help with some non-standards-compliant devices. | |
3066 | * Otherwise we start with SET_ADDRESS and then try to read the | |
3067 | * first 8 bytes of the device descriptor to get the ep0 maxpacket | |
3068 | * value. | |
3069 | */ | |
3070 | for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { | |
c6515272 | 3071 | if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) { |
1da177e4 LT |
3072 | struct usb_device_descriptor *buf; |
3073 | int r = 0; | |
3074 | ||
3075 | #define GET_DESCRIPTOR_BUFSIZE 64 | |
3076 | buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); | |
3077 | if (!buf) { | |
3078 | retval = -ENOMEM; | |
3079 | continue; | |
3080 | } | |
3081 | ||
b89ee19a AS |
3082 | /* Retry on all errors; some devices are flakey. |
3083 | * 255 is for WUSB devices, we actually need to use | |
3084 | * 512 (WUSB1.0[4.8.1]). | |
1da177e4 LT |
3085 | */ |
3086 | for (j = 0; j < 3; ++j) { | |
3087 | buf->bMaxPacketSize0 = 0; | |
3088 | r = usb_control_msg(udev, usb_rcvaddr0pipe(), | |
3089 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, | |
3090 | USB_DT_DEVICE << 8, 0, | |
3091 | buf, GET_DESCRIPTOR_BUFSIZE, | |
fd7c519d | 3092 | initial_descriptor_timeout); |
1da177e4 | 3093 | switch (buf->bMaxPacketSize0) { |
5bb6e0ae | 3094 | case 8: case 16: case 32: case 64: case 255: |
1da177e4 LT |
3095 | if (buf->bDescriptorType == |
3096 | USB_DT_DEVICE) { | |
3097 | r = 0; | |
3098 | break; | |
3099 | } | |
3100 | /* FALL THROUGH */ | |
3101 | default: | |
3102 | if (r == 0) | |
3103 | r = -EPROTO; | |
3104 | break; | |
3105 | } | |
3106 | if (r == 0) | |
3107 | break; | |
3108 | } | |
3109 | udev->descriptor.bMaxPacketSize0 = | |
3110 | buf->bMaxPacketSize0; | |
3111 | kfree(buf); | |
3112 | ||
75d7cf72 | 3113 | retval = hub_port_reset(hub, port1, udev, delay, false); |
1da177e4 LT |
3114 | if (retval < 0) /* error or disconnect */ |
3115 | goto fail; | |
3116 | if (oldspeed != udev->speed) { | |
3117 | dev_dbg(&udev->dev, | |
3118 | "device reset changed speed!\n"); | |
3119 | retval = -ENODEV; | |
3120 | goto fail; | |
3121 | } | |
3122 | if (r) { | |
b9cef6c3 WF |
3123 | dev_err(&udev->dev, |
3124 | "device descriptor read/64, error %d\n", | |
3125 | r); | |
1da177e4 LT |
3126 | retval = -EMSGSIZE; |
3127 | continue; | |
3128 | } | |
3129 | #undef GET_DESCRIPTOR_BUFSIZE | |
3130 | } | |
3131 | ||
6c529cdc IPG |
3132 | /* |
3133 | * If device is WUSB, we already assigned an | |
3134 | * unauthorized address in the Connect Ack sequence; | |
3135 | * authorization will assign the final address. | |
3136 | */ | |
c6515272 | 3137 | if (udev->wusb == 0) { |
6c529cdc IPG |
3138 | for (j = 0; j < SET_ADDRESS_TRIES; ++j) { |
3139 | retval = hub_set_address(udev, devnum); | |
3140 | if (retval >= 0) | |
3141 | break; | |
3142 | msleep(200); | |
3143 | } | |
3144 | if (retval < 0) { | |
3145 | dev_err(&udev->dev, | |
3146 | "device not accepting address %d, error %d\n", | |
3147 | devnum, retval); | |
3148 | goto fail; | |
3149 | } | |
c6515272 SS |
3150 | if (udev->speed == USB_SPEED_SUPER) { |
3151 | devnum = udev->devnum; | |
3152 | dev_info(&udev->dev, | |
3b29b68b | 3153 | "%s SuperSpeed USB device number %d using %s\n", |
c6515272 | 3154 | (udev->config) ? "reset" : "new", |
3b29b68b | 3155 | devnum, udev->bus->controller->driver->name); |
c6515272 | 3156 | } |
6c529cdc IPG |
3157 | |
3158 | /* cope with hardware quirkiness: | |
3159 | * - let SET_ADDRESS settle, some device hardware wants it | |
3160 | * - read ep0 maxpacket even for high and low speed, | |
3161 | */ | |
3162 | msleep(10); | |
c6515272 | 3163 | if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) |
1da177e4 | 3164 | break; |
6c529cdc | 3165 | } |
1da177e4 LT |
3166 | |
3167 | retval = usb_get_device_descriptor(udev, 8); | |
3168 | if (retval < 8) { | |
b9cef6c3 WF |
3169 | dev_err(&udev->dev, |
3170 | "device descriptor read/8, error %d\n", | |
3171 | retval); | |
1da177e4 LT |
3172 | if (retval >= 0) |
3173 | retval = -EMSGSIZE; | |
3174 | } else { | |
3175 | retval = 0; | |
3176 | break; | |
3177 | } | |
3178 | } | |
3179 | if (retval) | |
3180 | goto fail; | |
3181 | ||
d8aec3db EF |
3182 | /* |
3183 | * Some superspeed devices have finished the link training process | |
3184 | * and attached to a superspeed hub port, but the device descriptor | |
3185 | * got from those devices show they aren't superspeed devices. Warm | |
3186 | * reset the port attached by the devices can fix them. | |
3187 | */ | |
3188 | if ((udev->speed == USB_SPEED_SUPER) && | |
3189 | (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { | |
3190 | dev_err(&udev->dev, "got a wrong device descriptor, " | |
3191 | "warm reset device\n"); | |
3192 | hub_port_reset(hub, port1, udev, | |
3193 | HUB_BH_RESET_TIME, true); | |
3194 | retval = -EINVAL; | |
3195 | goto fail; | |
3196 | } | |
3197 | ||
6b403b02 SS |
3198 | if (udev->descriptor.bMaxPacketSize0 == 0xff || |
3199 | udev->speed == USB_SPEED_SUPER) | |
3200 | i = 512; | |
3201 | else | |
3202 | i = udev->descriptor.bMaxPacketSize0; | |
29cc8897 | 3203 | if (usb_endpoint_maxp(&udev->ep0.desc) != i) { |
56626a72 | 3204 | if (udev->speed == USB_SPEED_LOW || |
1da177e4 | 3205 | !(i == 8 || i == 16 || i == 32 || i == 64)) { |
56626a72 | 3206 | dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); |
1da177e4 LT |
3207 | retval = -EMSGSIZE; |
3208 | goto fail; | |
3209 | } | |
56626a72 AS |
3210 | if (udev->speed == USB_SPEED_FULL) |
3211 | dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); | |
3212 | else | |
3213 | dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); | |
1da177e4 | 3214 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); |
fc721f51 | 3215 | usb_ep0_reinit(udev); |
1da177e4 LT |
3216 | } |
3217 | ||
3218 | retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); | |
3219 | if (retval < (signed)sizeof(udev->descriptor)) { | |
b9cef6c3 WF |
3220 | dev_err(&udev->dev, "device descriptor read/all, error %d\n", |
3221 | retval); | |
1da177e4 LT |
3222 | if (retval >= 0) |
3223 | retval = -ENOMSG; | |
3224 | goto fail; | |
3225 | } | |
3226 | ||
1ff4df56 AX |
3227 | if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { |
3228 | retval = usb_get_bos_descriptor(udev); | |
d9b2099c SS |
3229 | if (!retval) |
3230 | udev->lpm_capable = usb_device_supports_lpm(udev); | |
1ff4df56 | 3231 | } |
3148bf04 | 3232 | |
1da177e4 | 3233 | retval = 0; |
48f24970 AD |
3234 | /* notify HCD that we have a device connected and addressed */ |
3235 | if (hcd->driver->update_device) | |
3236 | hcd->driver->update_device(hcd, udev); | |
1da177e4 | 3237 | fail: |
4326ed0b | 3238 | if (retval) { |
1da177e4 | 3239 | hub_port_disable(hub, port1, 0); |
3b29b68b | 3240 | update_devnum(udev, devnum); /* for disconnect processing */ |
4326ed0b | 3241 | } |
4186ecf8 | 3242 | mutex_unlock(&usb_address0_mutex); |
1da177e4 LT |
3243 | return retval; |
3244 | } | |
3245 | ||
3246 | static void | |
3247 | check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) | |
3248 | { | |
3249 | struct usb_qualifier_descriptor *qual; | |
3250 | int status; | |
3251 | ||
e94b1766 | 3252 | qual = kmalloc (sizeof *qual, GFP_KERNEL); |
1da177e4 LT |
3253 | if (qual == NULL) |
3254 | return; | |
3255 | ||
3256 | status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, | |
3257 | qual, sizeof *qual); | |
3258 | if (status == sizeof *qual) { | |
3259 | dev_info(&udev->dev, "not running at top speed; " | |
3260 | "connect to a high speed hub\n"); | |
3261 | /* hub LEDs are probably harder to miss than syslog */ | |
3262 | if (hub->has_indicators) { | |
3263 | hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; | |
c4028958 | 3264 | schedule_delayed_work (&hub->leds, 0); |
1da177e4 LT |
3265 | } |
3266 | } | |
1bc3c9e1 | 3267 | kfree(qual); |
1da177e4 LT |
3268 | } |
3269 | ||
3270 | static unsigned | |
3271 | hub_power_remaining (struct usb_hub *hub) | |
3272 | { | |
3273 | struct usb_device *hdev = hub->hdev; | |
3274 | int remaining; | |
55c52718 | 3275 | int port1; |
1da177e4 | 3276 | |
55c52718 | 3277 | if (!hub->limited_power) |
1da177e4 LT |
3278 | return 0; |
3279 | ||
55c52718 AS |
3280 | remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; |
3281 | for (port1 = 1; port1 <= hdev->maxchild; ++port1) { | |
fa286188 | 3282 | struct usb_device *udev = hdev->children[port1 - 1]; |
55c52718 | 3283 | int delta; |
1da177e4 LT |
3284 | |
3285 | if (!udev) | |
3286 | continue; | |
3287 | ||
55c52718 AS |
3288 | /* Unconfigured devices may not use more than 100mA, |
3289 | * or 8mA for OTG ports */ | |
1da177e4 | 3290 | if (udev->actconfig) |
55c52718 AS |
3291 | delta = udev->actconfig->desc.bMaxPower * 2; |
3292 | else if (port1 != udev->bus->otg_port || hdev->parent) | |
3293 | delta = 100; | |
1da177e4 | 3294 | else |
55c52718 AS |
3295 | delta = 8; |
3296 | if (delta > hub->mA_per_port) | |
b9cef6c3 WF |
3297 | dev_warn(&udev->dev, |
3298 | "%dmA is over %umA budget for port %d!\n", | |
3299 | delta, hub->mA_per_port, port1); | |
1da177e4 LT |
3300 | remaining -= delta; |
3301 | } | |
3302 | if (remaining < 0) { | |
55c52718 AS |
3303 | dev_warn(hub->intfdev, "%dmA over power budget!\n", |
3304 | - remaining); | |
1da177e4 LT |
3305 | remaining = 0; |
3306 | } | |
3307 | return remaining; | |
3308 | } | |
3309 | ||
3310 | /* Handle physical or logical connection change events. | |
3311 | * This routine is called when: | |
3312 | * a port connection-change occurs; | |
3313 | * a port enable-change occurs (often caused by EMI); | |
742120c6 | 3314 | * usb_reset_and_verify_device() encounters changed descriptors (as from |
1da177e4 LT |
3315 | * a firmware download) |
3316 | * caller already locked the hub | |
3317 | */ | |
3318 | static void hub_port_connect_change(struct usb_hub *hub, int port1, | |
3319 | u16 portstatus, u16 portchange) | |
3320 | { | |
3321 | struct usb_device *hdev = hub->hdev; | |
3322 | struct device *hub_dev = hub->intfdev; | |
90da096e | 3323 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
24618b0c AS |
3324 | unsigned wHubCharacteristics = |
3325 | le16_to_cpu(hub->descriptor->wHubCharacteristics); | |
8808f00c | 3326 | struct usb_device *udev; |
1da177e4 | 3327 | int status, i; |
24618b0c | 3328 | |
1da177e4 LT |
3329 | dev_dbg (hub_dev, |
3330 | "port %d, status %04x, change %04x, %s\n", | |
131dec34 | 3331 | port1, portstatus, portchange, portspeed(hub, portstatus)); |
1da177e4 LT |
3332 | |
3333 | if (hub->has_indicators) { | |
3334 | set_port_led(hub, port1, HUB_LED_AUTO); | |
3335 | hub->indicator[port1-1] = INDICATOR_AUTO; | |
3336 | } | |
1da177e4 LT |
3337 | |
3338 | #ifdef CONFIG_USB_OTG | |
3339 | /* during HNP, don't repeat the debounce */ | |
3340 | if (hdev->bus->is_b_host) | |
24618b0c AS |
3341 | portchange &= ~(USB_PORT_STAT_C_CONNECTION | |
3342 | USB_PORT_STAT_C_ENABLE); | |
1da177e4 LT |
3343 | #endif |
3344 | ||
8808f00c | 3345 | /* Try to resuscitate an existing device */ |
fa286188 | 3346 | udev = hdev->children[port1-1]; |
8808f00c AS |
3347 | if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && |
3348 | udev->state != USB_STATE_NOTATTACHED) { | |
8808f00c AS |
3349 | usb_lock_device(udev); |
3350 | if (portstatus & USB_PORT_STAT_ENABLE) { | |
3351 | status = 0; /* Nothing to do */ | |
8808f00c AS |
3352 | |
3353 | #ifdef CONFIG_USB_SUSPEND | |
5257d97a AS |
3354 | } else if (udev->state == USB_STATE_SUSPENDED && |
3355 | udev->persist_enabled) { | |
8808f00c AS |
3356 | /* For a suspended device, treat this as a |
3357 | * remote wakeup event. | |
3358 | */ | |
0534d468 | 3359 | status = usb_remote_wakeup(udev); |
8808f00c AS |
3360 | #endif |
3361 | ||
3362 | } else { | |
5257d97a | 3363 | status = -ENODEV; /* Don't resuscitate */ |
8808f00c AS |
3364 | } |
3365 | usb_unlock_device(udev); | |
3366 | ||
3367 | if (status == 0) { | |
3368 | clear_bit(port1, hub->change_bits); | |
3369 | return; | |
3370 | } | |
3371 | } | |
3372 | ||
24618b0c | 3373 | /* Disconnect any existing devices under this port */ |
8808f00c | 3374 | if (udev) |
fa286188 | 3375 | usb_disconnect(&hdev->children[port1-1]); |
24618b0c AS |
3376 | clear_bit(port1, hub->change_bits); |
3377 | ||
253e0572 AS |
3378 | /* We can forget about a "removed" device when there's a physical |
3379 | * disconnect or the connect status changes. | |
3380 | */ | |
3381 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || | |
3382 | (portchange & USB_PORT_STAT_C_CONNECTION)) | |
3383 | clear_bit(port1, hub->removed_bits); | |
3384 | ||
5257d97a AS |
3385 | if (portchange & (USB_PORT_STAT_C_CONNECTION | |
3386 | USB_PORT_STAT_C_ENABLE)) { | |
3387 | status = hub_port_debounce(hub, port1); | |
3388 | if (status < 0) { | |
3389 | if (printk_ratelimit()) | |
3390 | dev_err(hub_dev, "connect-debounce failed, " | |
3391 | "port %d disabled\n", port1); | |
3392 | portstatus &= ~USB_PORT_STAT_CONNECTION; | |
3393 | } else { | |
3394 | portstatus = status; | |
3395 | } | |
3396 | } | |
3397 | ||
253e0572 AS |
3398 | /* Return now if debouncing failed or nothing is connected or |
3399 | * the device was "removed". | |
3400 | */ | |
3401 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || | |
3402 | test_bit(port1, hub->removed_bits)) { | |
1da177e4 LT |
3403 | |
3404 | /* maybe switch power back on (e.g. root hub was reset) */ | |
74ad9bd2 | 3405 | if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 |
0ed9a57e | 3406 | && !port_is_power_on(hub, portstatus)) |
1da177e4 | 3407 | set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); |
5257d97a | 3408 | |
1da177e4 LT |
3409 | if (portstatus & USB_PORT_STAT_ENABLE) |
3410 | goto done; | |
3411 | return; | |
3412 | } | |
3413 | ||
1da177e4 | 3414 | for (i = 0; i < SET_CONFIG_TRIES; i++) { |
1da177e4 LT |
3415 | |
3416 | /* reallocate for each attempt, since references | |
3417 | * to the previous one can escape in various ways | |
3418 | */ | |
3419 | udev = usb_alloc_dev(hdev, hdev->bus, port1); | |
3420 | if (!udev) { | |
3421 | dev_err (hub_dev, | |
3422 | "couldn't allocate port %d usb_device\n", | |
3423 | port1); | |
3424 | goto done; | |
3425 | } | |
3426 | ||
3427 | usb_set_device_state(udev, USB_STATE_POWERED); | |
55c52718 | 3428 | udev->bus_mA = hub->mA_per_port; |
b6956ffa | 3429 | udev->level = hdev->level + 1; |
8af548dc | 3430 | udev->wusb = hub_is_wusb(hub); |
55c52718 | 3431 | |
131dec34 SS |
3432 | /* Only USB 3.0 devices are connected to SuperSpeed hubs. */ |
3433 | if (hub_is_superspeed(hub->hdev)) | |
e7b77172 SS |
3434 | udev->speed = USB_SPEED_SUPER; |
3435 | else | |
3436 | udev->speed = USB_SPEED_UNKNOWN; | |
3437 | ||
3b29b68b | 3438 | choose_devnum(udev); |
c8d4af8e AX |
3439 | if (udev->devnum <= 0) { |
3440 | status = -ENOTCONN; /* Don't retry */ | |
3441 | goto loop; | |
c6515272 SS |
3442 | } |
3443 | ||
e7b77172 | 3444 | /* reset (non-USB 3.0 devices) and get descriptor */ |
1da177e4 LT |
3445 | status = hub_port_init(hub, udev, port1, i); |
3446 | if (status < 0) | |
3447 | goto loop; | |
3448 | ||
93362a87 PD |
3449 | usb_detect_quirks(udev); |
3450 | if (udev->quirks & USB_QUIRK_DELAY_INIT) | |
3451 | msleep(1000); | |
3452 | ||
1da177e4 LT |
3453 | /* consecutive bus-powered hubs aren't reliable; they can |
3454 | * violate the voltage drop budget. if the new child has | |
3455 | * a "powered" LED, users should notice we didn't enable it | |
3456 | * (without reading syslog), even without per-port LEDs | |
3457 | * on the parent. | |
3458 | */ | |
3459 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB | |
55c52718 | 3460 | && udev->bus_mA <= 100) { |
1da177e4 LT |
3461 | u16 devstat; |
3462 | ||
3463 | status = usb_get_status(udev, USB_RECIP_DEVICE, 0, | |
3464 | &devstat); | |
55c52718 | 3465 | if (status < 2) { |
1da177e4 LT |
3466 | dev_dbg(&udev->dev, "get status %d ?\n", status); |
3467 | goto loop_disable; | |
3468 | } | |
55c52718 | 3469 | le16_to_cpus(&devstat); |
1da177e4 LT |
3470 | if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { |
3471 | dev_err(&udev->dev, | |
3472 | "can't connect bus-powered hub " | |
3473 | "to this port\n"); | |
3474 | if (hub->has_indicators) { | |
3475 | hub->indicator[port1-1] = | |
3476 | INDICATOR_AMBER_BLINK; | |
c4028958 | 3477 | schedule_delayed_work (&hub->leds, 0); |
1da177e4 LT |
3478 | } |
3479 | status = -ENOTCONN; /* Don't retry */ | |
3480 | goto loop_disable; | |
3481 | } | |
3482 | } | |
3483 | ||
3484 | /* check for devices running slower than they could */ | |
3485 | if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 | |
3486 | && udev->speed == USB_SPEED_FULL | |
3487 | && highspeed_hubs != 0) | |
3488 | check_highspeed (hub, udev, port1); | |
3489 | ||
fa286188 | 3490 | /* Store the parent's children[] pointer. At this point |
1da177e4 LT |
3491 | * udev becomes globally accessible, although presumably |
3492 | * no one will look at it until hdev is unlocked. | |
3493 | */ | |
1da177e4 LT |
3494 | status = 0; |
3495 | ||
3496 | /* We mustn't add new devices if the parent hub has | |
3497 | * been disconnected; we would race with the | |
3498 | * recursively_mark_NOTATTACHED() routine. | |
3499 | */ | |
3500 | spin_lock_irq(&device_state_lock); | |
3501 | if (hdev->state == USB_STATE_NOTATTACHED) | |
3502 | status = -ENOTCONN; | |
3503 | else | |
fa286188 | 3504 | hdev->children[port1-1] = udev; |
1da177e4 LT |
3505 | spin_unlock_irq(&device_state_lock); |
3506 | ||
3507 | /* Run it through the hoops (find a driver, etc) */ | |
3508 | if (!status) { | |
3509 | status = usb_new_device(udev); | |
3510 | if (status) { | |
3511 | spin_lock_irq(&device_state_lock); | |
fa286188 | 3512 | hdev->children[port1-1] = NULL; |
1da177e4 LT |
3513 | spin_unlock_irq(&device_state_lock); |
3514 | } | |
3515 | } | |
3516 | ||
1da177e4 LT |
3517 | if (status) |
3518 | goto loop_disable; | |
3519 | ||
3520 | status = hub_power_remaining(hub); | |
3521 | if (status) | |
55c52718 | 3522 | dev_dbg(hub_dev, "%dmA power budget left\n", status); |
1da177e4 LT |
3523 | |
3524 | return; | |
3525 | ||
3526 | loop_disable: | |
3527 | hub_port_disable(hub, port1, 1); | |
3528 | loop: | |
fc721f51 | 3529 | usb_ep0_reinit(udev); |
3b29b68b | 3530 | release_devnum(udev); |
f7410ced | 3531 | hub_free_dev(udev); |
1da177e4 | 3532 | usb_put_dev(udev); |
ffcdc18d | 3533 | if ((status == -ENOTCONN) || (status == -ENOTSUPP)) |
1da177e4 LT |
3534 | break; |
3535 | } | |
3a31155c AS |
3536 | if (hub->hdev->parent || |
3537 | !hcd->driver->port_handed_over || | |
3538 | !(hcd->driver->port_handed_over)(hcd, port1)) | |
3539 | dev_err(hub_dev, "unable to enumerate USB device on port %d\n", | |
3540 | port1); | |
1da177e4 LT |
3541 | |
3542 | done: | |
3543 | hub_port_disable(hub, port1, 1); | |
90da096e BR |
3544 | if (hcd->driver->relinquish_port && !hub->hdev->parent) |
3545 | hcd->driver->relinquish_port(hcd, port1); | |
1da177e4 LT |
3546 | } |
3547 | ||
714b07be SS |
3548 | /* Returns 1 if there was a remote wakeup and a connect status change. */ |
3549 | static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, | |
72937e1e | 3550 | u16 portstatus, u16 portchange) |
714b07be SS |
3551 | { |
3552 | struct usb_device *hdev; | |
3553 | struct usb_device *udev; | |
3554 | int connect_change = 0; | |
3555 | int ret; | |
3556 | ||
3557 | hdev = hub->hdev; | |
fa286188 | 3558 | udev = hdev->children[port-1]; |
4ee823b8 SS |
3559 | if (!hub_is_superspeed(hdev)) { |
3560 | if (!(portchange & USB_PORT_STAT_C_SUSPEND)) | |
3561 | return 0; | |
3562 | clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); | |
3563 | } else { | |
3564 | if (!udev || udev->state != USB_STATE_SUSPENDED || | |
72937e1e SS |
3565 | (portstatus & USB_PORT_STAT_LINK_STATE) != |
3566 | USB_SS_PORT_LS_U0) | |
4ee823b8 SS |
3567 | return 0; |
3568 | } | |
3569 | ||
714b07be SS |
3570 | if (udev) { |
3571 | /* TRSMRCY = 10 msec */ | |
3572 | msleep(10); | |
3573 | ||
3574 | usb_lock_device(udev); | |
3575 | ret = usb_remote_wakeup(udev); | |
3576 | usb_unlock_device(udev); | |
3577 | if (ret < 0) | |
3578 | connect_change = 1; | |
3579 | } else { | |
3580 | ret = -ENODEV; | |
3581 | hub_port_disable(hub, port, 1); | |
3582 | } | |
3583 | dev_dbg(hub->intfdev, "resume on port %d, status %d\n", | |
3584 | port, ret); | |
3585 | return connect_change; | |
3586 | } | |
3587 | ||
1da177e4 LT |
3588 | static void hub_events(void) |
3589 | { | |
3590 | struct list_head *tmp; | |
3591 | struct usb_device *hdev; | |
3592 | struct usb_interface *intf; | |
3593 | struct usb_hub *hub; | |
3594 | struct device *hub_dev; | |
3595 | u16 hubstatus; | |
3596 | u16 hubchange; | |
3597 | u16 portstatus; | |
3598 | u16 portchange; | |
3599 | int i, ret; | |
4ee823b8 | 3600 | int connect_change, wakeup_change; |
1da177e4 LT |
3601 | |
3602 | /* | |
3603 | * We restart the list every time to avoid a deadlock with | |
3604 | * deleting hubs downstream from this one. This should be | |
3605 | * safe since we delete the hub from the event list. | |
3606 | * Not the most efficient, but avoids deadlocks. | |
3607 | */ | |
3608 | while (1) { | |
3609 | ||
3610 | /* Grab the first entry at the beginning of the list */ | |
3611 | spin_lock_irq(&hub_event_lock); | |
3612 | if (list_empty(&hub_event_list)) { | |
3613 | spin_unlock_irq(&hub_event_lock); | |
3614 | break; | |
3615 | } | |
3616 | ||
3617 | tmp = hub_event_list.next; | |
3618 | list_del_init(tmp); | |
3619 | ||
3620 | hub = list_entry(tmp, struct usb_hub, event_list); | |
e8054854 AS |
3621 | kref_get(&hub->kref); |
3622 | spin_unlock_irq(&hub_event_lock); | |
1da177e4 | 3623 | |
e8054854 AS |
3624 | hdev = hub->hdev; |
3625 | hub_dev = hub->intfdev; | |
3626 | intf = to_usb_interface(hub_dev); | |
40f122f3 | 3627 | dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", |
1da177e4 LT |
3628 | hdev->state, hub->descriptor |
3629 | ? hub->descriptor->bNbrPorts | |
3630 | : 0, | |
3631 | /* NOTE: expects max 15 ports... */ | |
3632 | (u16) hub->change_bits[0], | |
40f122f3 | 3633 | (u16) hub->event_bits[0]); |
1da177e4 | 3634 | |
1da177e4 LT |
3635 | /* Lock the device, then check to see if we were |
3636 | * disconnected while waiting for the lock to succeed. */ | |
06b84e8a | 3637 | usb_lock_device(hdev); |
e8054854 | 3638 | if (unlikely(hub->disconnected)) |
9bbdf1e0 | 3639 | goto loop_disconnected; |
1da177e4 LT |
3640 | |
3641 | /* If the hub has died, clean up after it */ | |
3642 | if (hdev->state == USB_STATE_NOTATTACHED) { | |
7de18d8b | 3643 | hub->error = -ENODEV; |
4330354f | 3644 | hub_quiesce(hub, HUB_DISCONNECT); |
1da177e4 LT |
3645 | goto loop; |
3646 | } | |
3647 | ||
40f122f3 AS |
3648 | /* Autoresume */ |
3649 | ret = usb_autopm_get_interface(intf); | |
3650 | if (ret) { | |
3651 | dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); | |
3652 | goto loop; | |
3653 | } | |
a8e7c565 | 3654 | |
40f122f3 | 3655 | /* If this is an inactive hub, do nothing */ |
1da177e4 | 3656 | if (hub->quiescing) |
40f122f3 | 3657 | goto loop_autopm; |
1da177e4 LT |
3658 | |
3659 | if (hub->error) { | |
3660 | dev_dbg (hub_dev, "resetting for error %d\n", | |
3661 | hub->error); | |
3662 | ||
742120c6 | 3663 | ret = usb_reset_device(hdev); |
1da177e4 LT |
3664 | if (ret) { |
3665 | dev_dbg (hub_dev, | |
3666 | "error resetting hub: %d\n", ret); | |
40f122f3 | 3667 | goto loop_autopm; |
1da177e4 LT |
3668 | } |
3669 | ||
3670 | hub->nerrors = 0; | |
3671 | hub->error = 0; | |
3672 | } | |
3673 | ||
3674 | /* deal with port status changes */ | |
3675 | for (i = 1; i <= hub->descriptor->bNbrPorts; i++) { | |
3676 | if (test_bit(i, hub->busy_bits)) | |
3677 | continue; | |
3678 | connect_change = test_bit(i, hub->change_bits); | |
72937e1e | 3679 | wakeup_change = test_and_clear_bit(i, hub->wakeup_bits); |
1da177e4 | 3680 | if (!test_and_clear_bit(i, hub->event_bits) && |
4ee823b8 | 3681 | !connect_change && !wakeup_change) |
1da177e4 LT |
3682 | continue; |
3683 | ||
3684 | ret = hub_port_status(hub, i, | |
3685 | &portstatus, &portchange); | |
3686 | if (ret < 0) | |
3687 | continue; | |
3688 | ||
1da177e4 LT |
3689 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
3690 | clear_port_feature(hdev, i, | |
3691 | USB_PORT_FEAT_C_CONNECTION); | |
3692 | connect_change = 1; | |
3693 | } | |
3694 | ||
3695 | if (portchange & USB_PORT_STAT_C_ENABLE) { | |
3696 | if (!connect_change) | |
3697 | dev_dbg (hub_dev, | |
3698 | "port %d enable change, " | |
3699 | "status %08x\n", | |
3700 | i, portstatus); | |
3701 | clear_port_feature(hdev, i, | |
3702 | USB_PORT_FEAT_C_ENABLE); | |
3703 | ||
3704 | /* | |
3705 | * EM interference sometimes causes badly | |
3706 | * shielded USB devices to be shutdown by | |
3707 | * the hub, this hack enables them again. | |
3708 | * Works at least with mouse driver. | |
3709 | */ | |
3710 | if (!(portstatus & USB_PORT_STAT_ENABLE) | |
3711 | && !connect_change | |
fa286188 | 3712 | && hdev->children[i-1]) { |
1da177e4 LT |
3713 | dev_err (hub_dev, |
3714 | "port %i " | |
3715 | "disabled by hub (EMI?), " | |
3716 | "re-enabling...\n", | |
3717 | i); | |
3718 | connect_change = 1; | |
3719 | } | |
3720 | } | |
3721 | ||
72937e1e SS |
3722 | if (hub_handle_remote_wakeup(hub, i, |
3723 | portstatus, portchange)) | |
714b07be | 3724 | connect_change = 1; |
8808f00c | 3725 | |
1da177e4 | 3726 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { |
752d57a8 PB |
3727 | u16 status = 0; |
3728 | u16 unused; | |
3729 | ||
3730 | dev_dbg(hub_dev, "over-current change on port " | |
3731 | "%d\n", i); | |
1da177e4 LT |
3732 | clear_port_feature(hdev, i, |
3733 | USB_PORT_FEAT_C_OVER_CURRENT); | |
752d57a8 | 3734 | msleep(100); /* Cool down */ |
8520f380 | 3735 | hub_power_on(hub, true); |
752d57a8 PB |
3736 | hub_port_status(hub, i, &status, &unused); |
3737 | if (status & USB_PORT_STAT_OVERCURRENT) | |
3738 | dev_err(hub_dev, "over-current " | |
3739 | "condition on port %d\n", i); | |
1da177e4 LT |
3740 | } |
3741 | ||
3742 | if (portchange & USB_PORT_STAT_C_RESET) { | |
3743 | dev_dbg (hub_dev, | |
3744 | "reset change on port %d\n", | |
3745 | i); | |
3746 | clear_port_feature(hdev, i, | |
3747 | USB_PORT_FEAT_C_RESET); | |
3748 | } | |
c7061574 SS |
3749 | if ((portchange & USB_PORT_STAT_C_BH_RESET) && |
3750 | hub_is_superspeed(hub->hdev)) { | |
3751 | dev_dbg(hub_dev, | |
3752 | "warm reset change on port %d\n", | |
3753 | i); | |
3754 | clear_port_feature(hdev, i, | |
3755 | USB_PORT_FEAT_C_BH_PORT_RESET); | |
3756 | } | |
dbe79bbe JY |
3757 | if (portchange & USB_PORT_STAT_C_LINK_STATE) { |
3758 | clear_port_feature(hub->hdev, i, | |
3759 | USB_PORT_FEAT_C_PORT_LINK_STATE); | |
3760 | } | |
3761 | if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { | |
3762 | dev_warn(hub_dev, | |
3763 | "config error on port %d\n", | |
3764 | i); | |
3765 | clear_port_feature(hub->hdev, i, | |
3766 | USB_PORT_FEAT_C_PORT_CONFIG_ERROR); | |
3767 | } | |
1da177e4 | 3768 | |
5e467f6e AX |
3769 | /* Warm reset a USB3 protocol port if it's in |
3770 | * SS.Inactive state. | |
3771 | */ | |
3772 | if (hub_is_superspeed(hub->hdev) && | |
3773 | (portstatus & USB_PORT_STAT_LINK_STATE) | |
3774 | == USB_SS_PORT_LS_SS_INACTIVE) { | |
3775 | dev_dbg(hub_dev, "warm reset port %d\n", i); | |
75d7cf72 AX |
3776 | hub_port_reset(hub, i, NULL, |
3777 | HUB_BH_RESET_TIME, true); | |
5e467f6e AX |
3778 | } |
3779 | ||
1da177e4 LT |
3780 | if (connect_change) |
3781 | hub_port_connect_change(hub, i, | |
3782 | portstatus, portchange); | |
3783 | } /* end for i */ | |
3784 | ||
3785 | /* deal with hub status changes */ | |
3786 | if (test_and_clear_bit(0, hub->event_bits) == 0) | |
3787 | ; /* do nothing */ | |
3788 | else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) | |
3789 | dev_err (hub_dev, "get_hub_status failed\n"); | |
3790 | else { | |
3791 | if (hubchange & HUB_CHANGE_LOCAL_POWER) { | |
3792 | dev_dbg (hub_dev, "power change\n"); | |
3793 | clear_hub_feature(hdev, C_HUB_LOCAL_POWER); | |
55c52718 AS |
3794 | if (hubstatus & HUB_STATUS_LOCAL_POWER) |
3795 | /* FIXME: Is this always true? */ | |
55c52718 | 3796 | hub->limited_power = 1; |
403fae78 | 3797 | else |
3798 | hub->limited_power = 0; | |
1da177e4 LT |
3799 | } |
3800 | if (hubchange & HUB_CHANGE_OVERCURRENT) { | |
752d57a8 PB |
3801 | u16 status = 0; |
3802 | u16 unused; | |
3803 | ||
3804 | dev_dbg(hub_dev, "over-current change\n"); | |
1da177e4 | 3805 | clear_hub_feature(hdev, C_HUB_OVER_CURRENT); |
752d57a8 | 3806 | msleep(500); /* Cool down */ |
8520f380 | 3807 | hub_power_on(hub, true); |
752d57a8 PB |
3808 | hub_hub_status(hub, &status, &unused); |
3809 | if (status & HUB_STATUS_OVERCURRENT) | |
3810 | dev_err(hub_dev, "over-current " | |
3811 | "condition\n"); | |
1da177e4 LT |
3812 | } |
3813 | } | |
3814 | ||
8e4ceb38 AS |
3815 | loop_autopm: |
3816 | /* Balance the usb_autopm_get_interface() above */ | |
3817 | usb_autopm_put_interface_no_suspend(intf); | |
3818 | loop: | |
3819 | /* Balance the usb_autopm_get_interface_no_resume() in | |
3820 | * kick_khubd() and allow autosuspend. | |
3821 | */ | |
3822 | usb_autopm_put_interface(intf); | |
9bbdf1e0 | 3823 | loop_disconnected: |
1da177e4 | 3824 | usb_unlock_device(hdev); |
e8054854 | 3825 | kref_put(&hub->kref, hub_release); |
1da177e4 LT |
3826 | |
3827 | } /* end while (1) */ | |
3828 | } | |
3829 | ||
3830 | static int hub_thread(void *__unused) | |
3831 | { | |
3bb1af52 AS |
3832 | /* khubd needs to be freezable to avoid intefering with USB-PERSIST |
3833 | * port handover. Otherwise it might see that a full-speed device | |
3834 | * was gone before the EHCI controller had handed its port over to | |
3835 | * the companion full-speed controller. | |
3836 | */ | |
83144186 | 3837 | set_freezable(); |
3bb1af52 | 3838 | |
1da177e4 LT |
3839 | do { |
3840 | hub_events(); | |
e42837bc | 3841 | wait_event_freezable(khubd_wait, |
9c8d6178 | 3842 | !list_empty(&hub_event_list) || |
3843 | kthread_should_stop()); | |
9c8d6178 | 3844 | } while (!kthread_should_stop() || !list_empty(&hub_event_list)); |
1da177e4 | 3845 | |
9c8d6178 | 3846 | pr_debug("%s: khubd exiting\n", usbcore_name); |
3847 | return 0; | |
1da177e4 LT |
3848 | } |
3849 | ||
1e927d96 | 3850 | static const struct usb_device_id hub_id_table[] = { |
1da177e4 LT |
3851 | { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, |
3852 | .bDeviceClass = USB_CLASS_HUB}, | |
3853 | { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, | |
3854 | .bInterfaceClass = USB_CLASS_HUB}, | |
3855 | { } /* Terminating entry */ | |
3856 | }; | |
3857 | ||
3858 | MODULE_DEVICE_TABLE (usb, hub_id_table); | |
3859 | ||
3860 | static struct usb_driver hub_driver = { | |
1da177e4 LT |
3861 | .name = "hub", |
3862 | .probe = hub_probe, | |
3863 | .disconnect = hub_disconnect, | |
3864 | .suspend = hub_suspend, | |
3865 | .resume = hub_resume, | |
f07600cf | 3866 | .reset_resume = hub_reset_resume, |
7de18d8b AS |
3867 | .pre_reset = hub_pre_reset, |
3868 | .post_reset = hub_post_reset, | |
c532b29a | 3869 | .unlocked_ioctl = hub_ioctl, |
1da177e4 | 3870 | .id_table = hub_id_table, |
40f122f3 | 3871 | .supports_autosuspend = 1, |
1da177e4 LT |
3872 | }; |
3873 | ||
3874 | int usb_hub_init(void) | |
3875 | { | |
1da177e4 LT |
3876 | if (usb_register(&hub_driver) < 0) { |
3877 | printk(KERN_ERR "%s: can't register hub driver\n", | |
3878 | usbcore_name); | |
3879 | return -1; | |
3880 | } | |
3881 | ||
9c8d6178 | 3882 | khubd_task = kthread_run(hub_thread, NULL, "khubd"); |
3883 | if (!IS_ERR(khubd_task)) | |
1da177e4 | 3884 | return 0; |
1da177e4 LT |
3885 | |
3886 | /* Fall through if kernel_thread failed */ | |
3887 | usb_deregister(&hub_driver); | |
3888 | printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); | |
3889 | ||
3890 | return -1; | |
3891 | } | |
3892 | ||
3893 | void usb_hub_cleanup(void) | |
3894 | { | |
9c8d6178 | 3895 | kthread_stop(khubd_task); |
1da177e4 LT |
3896 | |
3897 | /* | |
3898 | * Hub resources are freed for us by usb_deregister. It calls | |
3899 | * usb_driver_purge on every device which in turn calls that | |
3900 | * devices disconnect function if it is using this driver. | |
3901 | * The hub_disconnect function takes care of releasing the | |
3902 | * individual hub resources. -greg | |
3903 | */ | |
3904 | usb_deregister(&hub_driver); | |
3905 | } /* usb_hub_cleanup() */ | |
3906 | ||
eb764c4b AS |
3907 | static int descriptors_changed(struct usb_device *udev, |
3908 | struct usb_device_descriptor *old_device_descriptor) | |
3909 | { | |
3910 | int changed = 0; | |
3911 | unsigned index; | |
3912 | unsigned serial_len = 0; | |
3913 | unsigned len; | |
3914 | unsigned old_length; | |
3915 | int length; | |
3916 | char *buf; | |
3917 | ||
3918 | if (memcmp(&udev->descriptor, old_device_descriptor, | |
3919 | sizeof(*old_device_descriptor)) != 0) | |
3920 | return 1; | |
3921 | ||
3922 | /* Since the idVendor, idProduct, and bcdDevice values in the | |
3923 | * device descriptor haven't changed, we will assume the | |
3924 | * Manufacturer and Product strings haven't changed either. | |
3925 | * But the SerialNumber string could be different (e.g., a | |
3926 | * different flash card of the same brand). | |
3927 | */ | |
3928 | if (udev->serial) | |
3929 | serial_len = strlen(udev->serial) + 1; | |
1da177e4 | 3930 | |
eb764c4b | 3931 | len = serial_len; |
1da177e4 | 3932 | for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
eb764c4b AS |
3933 | old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
3934 | len = max(len, old_length); | |
1da177e4 | 3935 | } |
eb764c4b | 3936 | |
0cc1a51f | 3937 | buf = kmalloc(len, GFP_NOIO); |
1da177e4 LT |
3938 | if (buf == NULL) { |
3939 | dev_err(&udev->dev, "no mem to re-read configs after reset\n"); | |
3940 | /* assume the worst */ | |
3941 | return 1; | |
3942 | } | |
3943 | for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { | |
eb764c4b | 3944 | old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
1da177e4 LT |
3945 | length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, |
3946 | old_length); | |
eb764c4b | 3947 | if (length != old_length) { |
1da177e4 LT |
3948 | dev_dbg(&udev->dev, "config index %d, error %d\n", |
3949 | index, length); | |
eb764c4b | 3950 | changed = 1; |
1da177e4 LT |
3951 | break; |
3952 | } | |
3953 | if (memcmp (buf, udev->rawdescriptors[index], old_length) | |
3954 | != 0) { | |
3955 | dev_dbg(&udev->dev, "config index %d changed (#%d)\n", | |
eb764c4b AS |
3956 | index, |
3957 | ((struct usb_config_descriptor *) buf)-> | |
3958 | bConfigurationValue); | |
3959 | changed = 1; | |
1da177e4 LT |
3960 | break; |
3961 | } | |
3962 | } | |
eb764c4b AS |
3963 | |
3964 | if (!changed && serial_len) { | |
3965 | length = usb_string(udev, udev->descriptor.iSerialNumber, | |
3966 | buf, serial_len); | |
3967 | if (length + 1 != serial_len) { | |
3968 | dev_dbg(&udev->dev, "serial string error %d\n", | |
3969 | length); | |
3970 | changed = 1; | |
3971 | } else if (memcmp(buf, udev->serial, length) != 0) { | |
3972 | dev_dbg(&udev->dev, "serial string changed\n"); | |
3973 | changed = 1; | |
3974 | } | |
3975 | } | |
3976 | ||
1da177e4 | 3977 | kfree(buf); |
eb764c4b | 3978 | return changed; |
1da177e4 LT |
3979 | } |
3980 | ||
3981 | /** | |
742120c6 | 3982 | * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device |
1da177e4 LT |
3983 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
3984 | * | |
79efa097 AS |
3985 | * WARNING - don't use this routine to reset a composite device |
3986 | * (one with multiple interfaces owned by separate drivers)! | |
742120c6 | 3987 | * Use usb_reset_device() instead. |
1da177e4 LT |
3988 | * |
3989 | * Do a port reset, reassign the device's address, and establish its | |
3990 | * former operating configuration. If the reset fails, or the device's | |
3991 | * descriptors change from their values before the reset, or the original | |
3992 | * configuration and altsettings cannot be restored, a flag will be set | |
3993 | * telling khubd to pretend the device has been disconnected and then | |
3994 | * re-connected. All drivers will be unbound, and the device will be | |
3995 | * re-enumerated and probed all over again. | |
3996 | * | |
3997 | * Returns 0 if the reset succeeded, -ENODEV if the device has been | |
3998 | * flagged for logical disconnection, or some other negative error code | |
3999 | * if the reset wasn't even attempted. | |
4000 | * | |
4001 | * The caller must own the device lock. For example, it's safe to use | |
4002 | * this from a driver probe() routine after downloading new firmware. | |
4003 | * For calls that might not occur during probe(), drivers should lock | |
4004 | * the device using usb_lock_device_for_reset(). | |
6bc6cff5 AS |
4005 | * |
4006 | * Locking exception: This routine may also be called from within an | |
4007 | * autoresume handler. Such usage won't conflict with other tasks | |
4008 | * holding the device lock because these tasks should always call | |
4009 | * usb_autopm_resume_device(), thereby preventing any unwanted autoresume. | |
1da177e4 | 4010 | */ |
742120c6 | 4011 | static int usb_reset_and_verify_device(struct usb_device *udev) |
1da177e4 LT |
4012 | { |
4013 | struct usb_device *parent_hdev = udev->parent; | |
4014 | struct usb_hub *parent_hub; | |
3f0479e0 | 4015 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
1da177e4 | 4016 | struct usb_device_descriptor descriptor = udev->descriptor; |
12c3da34 AS |
4017 | int i, ret = 0; |
4018 | int port1 = udev->portnum; | |
1da177e4 LT |
4019 | |
4020 | if (udev->state == USB_STATE_NOTATTACHED || | |
4021 | udev->state == USB_STATE_SUSPENDED) { | |
4022 | dev_dbg(&udev->dev, "device reset not allowed in state %d\n", | |
4023 | udev->state); | |
4024 | return -EINVAL; | |
4025 | } | |
4026 | ||
4027 | if (!parent_hdev) { | |
4bec9917 | 4028 | /* this requires hcd-specific logic; see ohci_restart() */ |
441b62c1 | 4029 | dev_dbg(&udev->dev, "%s for root hub!\n", __func__); |
1da177e4 LT |
4030 | return -EISDIR; |
4031 | } | |
1da177e4 LT |
4032 | parent_hub = hdev_to_hub(parent_hdev); |
4033 | ||
1da177e4 LT |
4034 | set_bit(port1, parent_hub->busy_bits); |
4035 | for (i = 0; i < SET_CONFIG_TRIES; ++i) { | |
4036 | ||
4037 | /* ep0 maxpacket size may change; let the HCD know about it. | |
4038 | * Other endpoints will be handled by re-enumeration. */ | |
fc721f51 | 4039 | usb_ep0_reinit(udev); |
1da177e4 | 4040 | ret = hub_port_init(parent_hub, udev, port1, i); |
dd4dd19e | 4041 | if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) |
1da177e4 LT |
4042 | break; |
4043 | } | |
4044 | clear_bit(port1, parent_hub->busy_bits); | |
d5cbad4b | 4045 | |
1da177e4 LT |
4046 | if (ret < 0) |
4047 | goto re_enumerate; | |
4048 | ||
4049 | /* Device might have changed firmware (DFU or similar) */ | |
eb764c4b | 4050 | if (descriptors_changed(udev, &descriptor)) { |
1da177e4 LT |
4051 | dev_info(&udev->dev, "device firmware changed\n"); |
4052 | udev->descriptor = descriptor; /* for disconnect() calls */ | |
4053 | goto re_enumerate; | |
4054 | } | |
4fe0387a AS |
4055 | |
4056 | /* Restore the device's previous configuration */ | |
1da177e4 LT |
4057 | if (!udev->actconfig) |
4058 | goto done; | |
3f0479e0 | 4059 | |
d673bfcb | 4060 | mutex_lock(hcd->bandwidth_mutex); |
3f0479e0 SS |
4061 | ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); |
4062 | if (ret < 0) { | |
4063 | dev_warn(&udev->dev, | |
4064 | "Busted HC? Not enough HCD resources for " | |
4065 | "old configuration.\n"); | |
d673bfcb | 4066 | mutex_unlock(hcd->bandwidth_mutex); |
3f0479e0 SS |
4067 | goto re_enumerate; |
4068 | } | |
1da177e4 LT |
4069 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
4070 | USB_REQ_SET_CONFIGURATION, 0, | |
4071 | udev->actconfig->desc.bConfigurationValue, 0, | |
4072 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
4073 | if (ret < 0) { | |
4074 | dev_err(&udev->dev, | |
4075 | "can't restore configuration #%d (error=%d)\n", | |
4076 | udev->actconfig->desc.bConfigurationValue, ret); | |
d673bfcb | 4077 | mutex_unlock(hcd->bandwidth_mutex); |
1da177e4 LT |
4078 | goto re_enumerate; |
4079 | } | |
d673bfcb | 4080 | mutex_unlock(hcd->bandwidth_mutex); |
1da177e4 LT |
4081 | usb_set_device_state(udev, USB_STATE_CONFIGURED); |
4082 | ||
4fe0387a AS |
4083 | /* Put interfaces back into the same altsettings as before. |
4084 | * Don't bother to send the Set-Interface request for interfaces | |
4085 | * that were already in altsetting 0; besides being unnecessary, | |
4086 | * many devices can't handle it. Instead just reset the host-side | |
4087 | * endpoint state. | |
4088 | */ | |
1da177e4 | 4089 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
3f0479e0 SS |
4090 | struct usb_host_config *config = udev->actconfig; |
4091 | struct usb_interface *intf = config->interface[i]; | |
1da177e4 LT |
4092 | struct usb_interface_descriptor *desc; |
4093 | ||
1da177e4 | 4094 | desc = &intf->cur_altsetting->desc; |
4fe0387a AS |
4095 | if (desc->bAlternateSetting == 0) { |
4096 | usb_disable_interface(udev, intf, true); | |
4097 | usb_enable_interface(udev, intf, true); | |
4098 | ret = 0; | |
4099 | } else { | |
04a723ea SS |
4100 | /* Let the bandwidth allocation function know that this |
4101 | * device has been reset, and it will have to use | |
4102 | * alternate setting 0 as the current alternate setting. | |
3f0479e0 | 4103 | */ |
04a723ea | 4104 | intf->resetting_device = 1; |
4fe0387a AS |
4105 | ret = usb_set_interface(udev, desc->bInterfaceNumber, |
4106 | desc->bAlternateSetting); | |
04a723ea | 4107 | intf->resetting_device = 0; |
4fe0387a | 4108 | } |
1da177e4 LT |
4109 | if (ret < 0) { |
4110 | dev_err(&udev->dev, "failed to restore interface %d " | |
4111 | "altsetting %d (error=%d)\n", | |
4112 | desc->bInterfaceNumber, | |
4113 | desc->bAlternateSetting, | |
4114 | ret); | |
4115 | goto re_enumerate; | |
4116 | } | |
4117 | } | |
4118 | ||
4119 | done: | |
1da177e4 LT |
4120 | return 0; |
4121 | ||
4122 | re_enumerate: | |
4123 | hub_port_logical_disconnect(parent_hub, port1); | |
4124 | return -ENODEV; | |
4125 | } | |
79efa097 AS |
4126 | |
4127 | /** | |
742120c6 | 4128 | * usb_reset_device - warn interface drivers and perform a USB port reset |
79efa097 | 4129 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
79efa097 AS |
4130 | * |
4131 | * Warns all drivers bound to registered interfaces (using their pre_reset | |
4132 | * method), performs the port reset, and then lets the drivers know that | |
4133 | * the reset is over (using their post_reset method). | |
4134 | * | |
742120c6 | 4135 | * Return value is the same as for usb_reset_and_verify_device(). |
79efa097 AS |
4136 | * |
4137 | * The caller must own the device lock. For example, it's safe to use | |
4138 | * this from a driver probe() routine after downloading new firmware. | |
4139 | * For calls that might not occur during probe(), drivers should lock | |
4140 | * the device using usb_lock_device_for_reset(). | |
78d9a487 AS |
4141 | * |
4142 | * If an interface is currently being probed or disconnected, we assume | |
4143 | * its driver knows how to handle resets. For all other interfaces, | |
4144 | * if the driver doesn't have pre_reset and post_reset methods then | |
4145 | * we attempt to unbind it and rebind afterward. | |
79efa097 | 4146 | */ |
742120c6 | 4147 | int usb_reset_device(struct usb_device *udev) |
79efa097 AS |
4148 | { |
4149 | int ret; | |
852c4b43 | 4150 | int i; |
79efa097 AS |
4151 | struct usb_host_config *config = udev->actconfig; |
4152 | ||
4153 | if (udev->state == USB_STATE_NOTATTACHED || | |
4154 | udev->state == USB_STATE_SUSPENDED) { | |
4155 | dev_dbg(&udev->dev, "device reset not allowed in state %d\n", | |
4156 | udev->state); | |
4157 | return -EINVAL; | |
4158 | } | |
4159 | ||
645daaab | 4160 | /* Prevent autosuspend during the reset */ |
94fcda1f | 4161 | usb_autoresume_device(udev); |
645daaab | 4162 | |
79efa097 | 4163 | if (config) { |
79efa097 | 4164 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
852c4b43 AS |
4165 | struct usb_interface *cintf = config->interface[i]; |
4166 | struct usb_driver *drv; | |
78d9a487 | 4167 | int unbind = 0; |
852c4b43 AS |
4168 | |
4169 | if (cintf->dev.driver) { | |
79efa097 | 4170 | drv = to_usb_driver(cintf->dev.driver); |
78d9a487 AS |
4171 | if (drv->pre_reset && drv->post_reset) |
4172 | unbind = (drv->pre_reset)(cintf); | |
4173 | else if (cintf->condition == | |
4174 | USB_INTERFACE_BOUND) | |
4175 | unbind = 1; | |
4176 | if (unbind) | |
4177 | usb_forced_unbind_intf(cintf); | |
79efa097 AS |
4178 | } |
4179 | } | |
4180 | } | |
4181 | ||
742120c6 | 4182 | ret = usb_reset_and_verify_device(udev); |
79efa097 AS |
4183 | |
4184 | if (config) { | |
79efa097 | 4185 | for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { |
852c4b43 AS |
4186 | struct usb_interface *cintf = config->interface[i]; |
4187 | struct usb_driver *drv; | |
78d9a487 | 4188 | int rebind = cintf->needs_binding; |
852c4b43 | 4189 | |
78d9a487 | 4190 | if (!rebind && cintf->dev.driver) { |
79efa097 AS |
4191 | drv = to_usb_driver(cintf->dev.driver); |
4192 | if (drv->post_reset) | |
78d9a487 AS |
4193 | rebind = (drv->post_reset)(cintf); |
4194 | else if (cintf->condition == | |
4195 | USB_INTERFACE_BOUND) | |
4196 | rebind = 1; | |
79efa097 | 4197 | } |
6c640945 | 4198 | if (ret == 0 && rebind) |
78d9a487 | 4199 | usb_rebind_intf(cintf); |
79efa097 AS |
4200 | } |
4201 | } | |
4202 | ||
94fcda1f | 4203 | usb_autosuspend_device(udev); |
79efa097 AS |
4204 | return ret; |
4205 | } | |
742120c6 | 4206 | EXPORT_SYMBOL_GPL(usb_reset_device); |
dc023dce IPG |
4207 | |
4208 | ||
4209 | /** | |
4210 | * usb_queue_reset_device - Reset a USB device from an atomic context | |
4211 | * @iface: USB interface belonging to the device to reset | |
4212 | * | |
4213 | * This function can be used to reset a USB device from an atomic | |
4214 | * context, where usb_reset_device() won't work (as it blocks). | |
4215 | * | |
4216 | * Doing a reset via this method is functionally equivalent to calling | |
4217 | * usb_reset_device(), except for the fact that it is delayed to a | |
4218 | * workqueue. This means that any drivers bound to other interfaces | |
4219 | * might be unbound, as well as users from usbfs in user space. | |
4220 | * | |
4221 | * Corner cases: | |
4222 | * | |
4223 | * - Scheduling two resets at the same time from two different drivers | |
4224 | * attached to two different interfaces of the same device is | |
4225 | * possible; depending on how the driver attached to each interface | |
4226 | * handles ->pre_reset(), the second reset might happen or not. | |
4227 | * | |
4228 | * - If a driver is unbound and it had a pending reset, the reset will | |
4229 | * be cancelled. | |
4230 | * | |
4231 | * - This function can be called during .probe() or .disconnect() | |
4232 | * times. On return from .disconnect(), any pending resets will be | |
4233 | * cancelled. | |
4234 | * | |
4235 | * There is no no need to lock/unlock the @reset_ws as schedule_work() | |
4236 | * does its own. | |
4237 | * | |
4238 | * NOTE: We don't do any reference count tracking because it is not | |
4239 | * needed. The lifecycle of the work_struct is tied to the | |
4240 | * usb_interface. Before destroying the interface we cancel the | |
4241 | * work_struct, so the fact that work_struct is queued and or | |
4242 | * running means the interface (and thus, the device) exist and | |
4243 | * are referenced. | |
4244 | */ | |
4245 | void usb_queue_reset_device(struct usb_interface *iface) | |
4246 | { | |
4247 | schedule_work(&iface->reset_ws); | |
4248 | } | |
4249 | EXPORT_SYMBOL_GPL(usb_queue_reset_device); |