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> |
925aa46b | 23 | #include <linux/usb/otg.h> |
93362a87 | 24 | #include <linux/usb/quirks.h> |
9c8d6178 | 25 | #include <linux/kthread.h> |
4186ecf8 | 26 | #include <linux/mutex.h> |
7dfb7103 | 27 | #include <linux/freezer.h> |
b04b3156 | 28 | #include <linux/random.h> |
ad493e5e | 29 | #include <linux/pm_qos.h> |
1da177e4 | 30 | |
1da177e4 LT |
31 | #include <asm/uaccess.h> |
32 | #include <asm/byteorder.h> | |
33 | ||
6e30d7cb | 34 | #include "hub.h" |
1da177e4 | 35 | |
f2a383e4 GKH |
36 | /* if we are in debug mode, always announce new devices */ |
37 | #ifdef DEBUG | |
38 | #ifndef CONFIG_USB_ANNOUNCE_NEW_DEVICES | |
39 | #define CONFIG_USB_ANNOUNCE_NEW_DEVICES | |
40 | #endif | |
41 | #endif | |
42 | ||
e6f30dea ML |
43 | #define USB_VENDOR_GENESYS_LOGIC 0x05e3 |
44 | #define HUB_QUIRK_CHECK_PORT_AUTOSUSPEND 0x01 | |
45 | ||
dbe79bbe JY |
46 | static inline int hub_is_superspeed(struct usb_device *hdev) |
47 | { | |
7bf01185 | 48 | return (hdev->descriptor.bDeviceProtocol == USB_HUB_PR_SS); |
dbe79bbe | 49 | } |
1bb5f66b | 50 | |
fa286188 | 51 | /* Protect struct usb_device->state and ->children members |
9ad3d6cc | 52 | * Note: Both are also protected by ->dev.sem, except that ->state can |
1da177e4 LT |
53 | * change to USB_STATE_NOTATTACHED even when the semaphore isn't held. */ |
54 | static DEFINE_SPINLOCK(device_state_lock); | |
55 | ||
56 | /* khubd's worklist and its lock */ | |
57 | static DEFINE_SPINLOCK(hub_event_lock); | |
58 | static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */ | |
59 | ||
60 | /* Wakes up khubd */ | |
61 | static DECLARE_WAIT_QUEUE_HEAD(khubd_wait); | |
62 | ||
9c8d6178 | 63 | static struct task_struct *khubd_task; |
1da177e4 LT |
64 | |
65 | /* cycle leds on hubs that aren't blinking for attention */ | |
90ab5ee9 | 66 | static bool blinkenlights = 0; |
1da177e4 LT |
67 | module_param (blinkenlights, bool, S_IRUGO); |
68 | MODULE_PARM_DESC (blinkenlights, "true to cycle leds on hubs"); | |
69 | ||
fd7c519d JK |
70 | /* |
71 | * Device SATA8000 FW1.0 from DATAST0R Technology Corp requires about | |
72 | * 10 seconds to send reply for the initial 64-byte descriptor request. | |
73 | */ | |
74 | /* define initial 64-byte descriptor request timeout in milliseconds */ | |
75 | static int initial_descriptor_timeout = USB_CTRL_GET_TIMEOUT; | |
76 | module_param(initial_descriptor_timeout, int, S_IRUGO|S_IWUSR); | |
b9cef6c3 WF |
77 | MODULE_PARM_DESC(initial_descriptor_timeout, |
78 | "initial 64-byte descriptor request timeout in milliseconds " | |
79 | "(default 5000 - 5.0 seconds)"); | |
fd7c519d | 80 | |
1da177e4 LT |
81 | /* |
82 | * As of 2.6.10 we introduce a new USB device initialization scheme which | |
83 | * closely resembles the way Windows works. Hopefully it will be compatible | |
84 | * with a wider range of devices than the old scheme. However some previously | |
85 | * working devices may start giving rise to "device not accepting address" | |
86 | * errors; if that happens the user can try the old scheme by adjusting the | |
87 | * following module parameters. | |
88 | * | |
89 | * For maximum flexibility there are two boolean parameters to control the | |
90 | * hub driver's behavior. On the first initialization attempt, if the | |
91 | * "old_scheme_first" parameter is set then the old scheme will be used, | |
92 | * otherwise the new scheme is used. If that fails and "use_both_schemes" | |
93 | * is set, then the driver will make another attempt, using the other scheme. | |
94 | */ | |
90ab5ee9 | 95 | static bool old_scheme_first = 0; |
1da177e4 LT |
96 | module_param(old_scheme_first, bool, S_IRUGO | S_IWUSR); |
97 | MODULE_PARM_DESC(old_scheme_first, | |
98 | "start with the old device initialization scheme"); | |
99 | ||
90ab5ee9 | 100 | static bool use_both_schemes = 1; |
1da177e4 LT |
101 | module_param(use_both_schemes, bool, S_IRUGO | S_IWUSR); |
102 | MODULE_PARM_DESC(use_both_schemes, | |
103 | "try the other device initialization scheme if the " | |
104 | "first one fails"); | |
105 | ||
32fe0198 AS |
106 | /* Mutual exclusion for EHCI CF initialization. This interferes with |
107 | * port reset on some companion controllers. | |
108 | */ | |
109 | DECLARE_RWSEM(ehci_cf_port_reset_rwsem); | |
110 | EXPORT_SYMBOL_GPL(ehci_cf_port_reset_rwsem); | |
111 | ||
ad493e5e | 112 | #define HUB_DEBOUNCE_TIMEOUT 2000 |
948fea37 AS |
113 | #define HUB_DEBOUNCE_STEP 25 |
114 | #define HUB_DEBOUNCE_STABLE 100 | |
115 | ||
742120c6 ML |
116 | static int usb_reset_and_verify_device(struct usb_device *udev); |
117 | ||
131dec34 | 118 | static inline char *portspeed(struct usb_hub *hub, int portstatus) |
1da177e4 | 119 | { |
131dec34 SS |
120 | if (hub_is_superspeed(hub->hdev)) |
121 | return "5.0 Gb/s"; | |
288ead45 | 122 | if (portstatus & USB_PORT_STAT_HIGH_SPEED) |
1da177e4 | 123 | return "480 Mb/s"; |
288ead45 | 124 | else if (portstatus & USB_PORT_STAT_LOW_SPEED) |
1da177e4 LT |
125 | return "1.5 Mb/s"; |
126 | else | |
127 | return "12 Mb/s"; | |
128 | } | |
1da177e4 LT |
129 | |
130 | /* Note that hdev or one of its children must be locked! */ | |
ad493e5e | 131 | struct usb_hub *usb_hub_to_struct_hub(struct usb_device *hdev) |
1da177e4 | 132 | { |
ff823c79 | 133 | if (!hdev || !hdev->actconfig || !hdev->maxchild) |
25118084 | 134 | return NULL; |
1da177e4 LT |
135 | return usb_get_intfdata(hdev->actconfig->interface[0]); |
136 | } | |
137 | ||
6cafd971 | 138 | static int usb_device_supports_lpm(struct usb_device *udev) |
d9b2099c SS |
139 | { |
140 | /* USB 2.1 (and greater) devices indicate LPM support through | |
141 | * their USB 2.0 Extended Capabilities BOS descriptor. | |
142 | */ | |
143 | if (udev->speed == USB_SPEED_HIGH) { | |
144 | if (udev->bos->ext_cap && | |
145 | (USB_LPM_SUPPORT & | |
146 | le32_to_cpu(udev->bos->ext_cap->bmAttributes))) | |
147 | return 1; | |
148 | return 0; | |
149 | } | |
51e0a012 SS |
150 | |
151 | /* All USB 3.0 must support LPM, but we need their max exit latency | |
152 | * information from the SuperSpeed Extended Capabilities BOS descriptor. | |
153 | */ | |
154 | if (!udev->bos->ss_cap) { | |
155 | dev_warn(&udev->dev, "No LPM exit latency info found. " | |
156 | "Power management will be impacted.\n"); | |
157 | return 0; | |
158 | } | |
159 | if (udev->parent->lpm_capable) | |
160 | return 1; | |
161 | ||
162 | dev_warn(&udev->dev, "Parent hub missing LPM exit latency info. " | |
163 | "Power management will be impacted.\n"); | |
d9b2099c SS |
164 | return 0; |
165 | } | |
166 | ||
51e0a012 SS |
167 | /* |
168 | * Set the Maximum Exit Latency (MEL) for the host to initiate a transition from | |
169 | * either U1 or U2. | |
170 | */ | |
171 | static void usb_set_lpm_mel(struct usb_device *udev, | |
172 | struct usb3_lpm_parameters *udev_lpm_params, | |
173 | unsigned int udev_exit_latency, | |
174 | struct usb_hub *hub, | |
175 | struct usb3_lpm_parameters *hub_lpm_params, | |
176 | unsigned int hub_exit_latency) | |
177 | { | |
178 | unsigned int total_mel; | |
179 | unsigned int device_mel; | |
180 | unsigned int hub_mel; | |
181 | ||
182 | /* | |
183 | * Calculate the time it takes to transition all links from the roothub | |
184 | * to the parent hub into U0. The parent hub must then decode the | |
185 | * packet (hub header decode latency) to figure out which port it was | |
186 | * bound for. | |
187 | * | |
188 | * The Hub Header decode latency is expressed in 0.1us intervals (0x1 | |
189 | * means 0.1us). Multiply that by 100 to get nanoseconds. | |
190 | */ | |
191 | total_mel = hub_lpm_params->mel + | |
192 | (hub->descriptor->u.ss.bHubHdrDecLat * 100); | |
193 | ||
194 | /* | |
195 | * How long will it take to transition the downstream hub's port into | |
196 | * U0? The greater of either the hub exit latency or the device exit | |
197 | * latency. | |
198 | * | |
199 | * The BOS U1/U2 exit latencies are expressed in 1us intervals. | |
200 | * Multiply that by 1000 to get nanoseconds. | |
201 | */ | |
202 | device_mel = udev_exit_latency * 1000; | |
203 | hub_mel = hub_exit_latency * 1000; | |
204 | if (device_mel > hub_mel) | |
205 | total_mel += device_mel; | |
206 | else | |
207 | total_mel += hub_mel; | |
208 | ||
209 | udev_lpm_params->mel = total_mel; | |
210 | } | |
211 | ||
212 | /* | |
213 | * Set the maximum Device to Host Exit Latency (PEL) for the device to initiate | |
214 | * a transition from either U1 or U2. | |
215 | */ | |
216 | static void usb_set_lpm_pel(struct usb_device *udev, | |
217 | struct usb3_lpm_parameters *udev_lpm_params, | |
218 | unsigned int udev_exit_latency, | |
219 | struct usb_hub *hub, | |
220 | struct usb3_lpm_parameters *hub_lpm_params, | |
221 | unsigned int hub_exit_latency, | |
222 | unsigned int port_to_port_exit_latency) | |
223 | { | |
224 | unsigned int first_link_pel; | |
225 | unsigned int hub_pel; | |
226 | ||
227 | /* | |
228 | * First, the device sends an LFPS to transition the link between the | |
229 | * device and the parent hub into U0. The exit latency is the bigger of | |
230 | * the device exit latency or the hub exit latency. | |
231 | */ | |
232 | if (udev_exit_latency > hub_exit_latency) | |
233 | first_link_pel = udev_exit_latency * 1000; | |
234 | else | |
235 | first_link_pel = hub_exit_latency * 1000; | |
236 | ||
237 | /* | |
238 | * When the hub starts to receive the LFPS, there is a slight delay for | |
239 | * it to figure out that one of the ports is sending an LFPS. Then it | |
240 | * will forward the LFPS to its upstream link. The exit latency is the | |
241 | * delay, plus the PEL that we calculated for this hub. | |
242 | */ | |
243 | hub_pel = port_to_port_exit_latency * 1000 + hub_lpm_params->pel; | |
244 | ||
245 | /* | |
246 | * According to figure C-7 in the USB 3.0 spec, the PEL for this device | |
247 | * is the greater of the two exit latencies. | |
248 | */ | |
249 | if (first_link_pel > hub_pel) | |
250 | udev_lpm_params->pel = first_link_pel; | |
251 | else | |
252 | udev_lpm_params->pel = hub_pel; | |
253 | } | |
254 | ||
255 | /* | |
256 | * Set the System Exit Latency (SEL) to indicate the total worst-case time from | |
257 | * when a device initiates a transition to U0, until when it will receive the | |
258 | * first packet from the host controller. | |
259 | * | |
260 | * Section C.1.5.1 describes the four components to this: | |
261 | * - t1: device PEL | |
262 | * - t2: time for the ERDY to make it from the device to the host. | |
263 | * - t3: a host-specific delay to process the ERDY. | |
264 | * - t4: time for the packet to make it from the host to the device. | |
265 | * | |
266 | * t3 is specific to both the xHCI host and the platform the host is integrated | |
267 | * into. The Intel HW folks have said it's negligible, FIXME if a different | |
268 | * vendor says otherwise. | |
269 | */ | |
270 | static void usb_set_lpm_sel(struct usb_device *udev, | |
271 | struct usb3_lpm_parameters *udev_lpm_params) | |
272 | { | |
273 | struct usb_device *parent; | |
274 | unsigned int num_hubs; | |
275 | unsigned int total_sel; | |
276 | ||
277 | /* t1 = device PEL */ | |
278 | total_sel = udev_lpm_params->pel; | |
279 | /* How many external hubs are in between the device & the root port. */ | |
280 | for (parent = udev->parent, num_hubs = 0; parent->parent; | |
281 | parent = parent->parent) | |
282 | num_hubs++; | |
283 | /* t2 = 2.1us + 250ns * (num_hubs - 1) */ | |
284 | if (num_hubs > 0) | |
285 | total_sel += 2100 + 250 * (num_hubs - 1); | |
286 | ||
287 | /* t4 = 250ns * num_hubs */ | |
288 | total_sel += 250 * num_hubs; | |
289 | ||
290 | udev_lpm_params->sel = total_sel; | |
291 | } | |
292 | ||
293 | static void usb_set_lpm_parameters(struct usb_device *udev) | |
294 | { | |
295 | struct usb_hub *hub; | |
296 | unsigned int port_to_port_delay; | |
297 | unsigned int udev_u1_del; | |
298 | unsigned int udev_u2_del; | |
299 | unsigned int hub_u1_del; | |
300 | unsigned int hub_u2_del; | |
301 | ||
302 | if (!udev->lpm_capable || udev->speed != USB_SPEED_SUPER) | |
303 | return; | |
304 | ||
ad493e5e | 305 | hub = usb_hub_to_struct_hub(udev->parent); |
51e0a012 SS |
306 | /* It doesn't take time to transition the roothub into U0, since it |
307 | * doesn't have an upstream link. | |
308 | */ | |
309 | if (!hub) | |
310 | return; | |
311 | ||
312 | udev_u1_del = udev->bos->ss_cap->bU1devExitLat; | |
313 | udev_u2_del = udev->bos->ss_cap->bU2DevExitLat; | |
314 | hub_u1_del = udev->parent->bos->ss_cap->bU1devExitLat; | |
315 | hub_u2_del = udev->parent->bos->ss_cap->bU2DevExitLat; | |
316 | ||
317 | usb_set_lpm_mel(udev, &udev->u1_params, udev_u1_del, | |
318 | hub, &udev->parent->u1_params, hub_u1_del); | |
319 | ||
320 | usb_set_lpm_mel(udev, &udev->u2_params, udev_u2_del, | |
321 | hub, &udev->parent->u2_params, hub_u2_del); | |
322 | ||
323 | /* | |
324 | * Appendix C, section C.2.2.2, says that there is a slight delay from | |
325 | * when the parent hub notices the downstream port is trying to | |
326 | * transition to U0 to when the hub initiates a U0 transition on its | |
327 | * upstream port. The section says the delays are tPort2PortU1EL and | |
328 | * tPort2PortU2EL, but it doesn't define what they are. | |
329 | * | |
330 | * The hub chapter, sections 10.4.2.4 and 10.4.2.5 seem to be talking | |
331 | * about the same delays. Use the maximum delay calculations from those | |
332 | * sections. For U1, it's tHubPort2PortExitLat, which is 1us max. For | |
333 | * U2, it's tHubPort2PortExitLat + U2DevExitLat - U1DevExitLat. I | |
334 | * assume the device exit latencies they are talking about are the hub | |
335 | * exit latencies. | |
336 | * | |
337 | * What do we do if the U2 exit latency is less than the U1 exit | |
338 | * latency? It's possible, although not likely... | |
339 | */ | |
340 | port_to_port_delay = 1; | |
341 | ||
342 | usb_set_lpm_pel(udev, &udev->u1_params, udev_u1_del, | |
343 | hub, &udev->parent->u1_params, hub_u1_del, | |
344 | port_to_port_delay); | |
345 | ||
346 | if (hub_u2_del > hub_u1_del) | |
347 | port_to_port_delay = 1 + hub_u2_del - hub_u1_del; | |
348 | else | |
349 | port_to_port_delay = 1 + hub_u1_del; | |
350 | ||
351 | usb_set_lpm_pel(udev, &udev->u2_params, udev_u2_del, | |
352 | hub, &udev->parent->u2_params, hub_u2_del, | |
353 | port_to_port_delay); | |
354 | ||
355 | /* Now that we've got PEL, calculate SEL. */ | |
356 | usb_set_lpm_sel(udev, &udev->u1_params); | |
357 | usb_set_lpm_sel(udev, &udev->u2_params); | |
358 | } | |
359 | ||
1da177e4 | 360 | /* USB 2.0 spec Section 11.24.4.5 */ |
dbe79bbe | 361 | static int get_hub_descriptor(struct usb_device *hdev, void *data) |
1da177e4 | 362 | { |
dbe79bbe JY |
363 | int i, ret, size; |
364 | unsigned dtype; | |
365 | ||
366 | if (hub_is_superspeed(hdev)) { | |
367 | dtype = USB_DT_SS_HUB; | |
368 | size = USB_DT_SS_HUB_SIZE; | |
369 | } else { | |
370 | dtype = USB_DT_HUB; | |
371 | size = sizeof(struct usb_hub_descriptor); | |
372 | } | |
1da177e4 LT |
373 | |
374 | for (i = 0; i < 3; i++) { | |
375 | ret = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), | |
376 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB, | |
dbe79bbe | 377 | dtype << 8, 0, data, size, |
1da177e4 LT |
378 | USB_CTRL_GET_TIMEOUT); |
379 | if (ret >= (USB_DT_HUB_NONVAR_SIZE + 2)) | |
380 | return ret; | |
381 | } | |
382 | return -EINVAL; | |
383 | } | |
384 | ||
385 | /* | |
386 | * USB 2.0 spec Section 11.24.2.1 | |
387 | */ | |
388 | static int clear_hub_feature(struct usb_device *hdev, int feature) | |
389 | { | |
390 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
391 | USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, 1000); | |
392 | } | |
393 | ||
394 | /* | |
395 | * USB 2.0 spec Section 11.24.2.2 | |
396 | */ | |
ad493e5e | 397 | int usb_clear_port_feature(struct usb_device *hdev, int port1, int feature) |
1da177e4 LT |
398 | { |
399 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
400 | USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port1, | |
401 | NULL, 0, 1000); | |
402 | } | |
403 | ||
404 | /* | |
405 | * USB 2.0 spec Section 11.24.2.13 | |
406 | */ | |
407 | static int set_port_feature(struct usb_device *hdev, int port1, int feature) | |
408 | { | |
409 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
410 | USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port1, | |
411 | NULL, 0, 1000); | |
412 | } | |
413 | ||
414 | /* | |
415 | * USB 2.0 spec Section 11.24.2.7.1.10 and table 11-7 | |
416 | * for info about using port indicators | |
417 | */ | |
418 | static void set_port_led( | |
419 | struct usb_hub *hub, | |
420 | int port1, | |
421 | int selector | |
422 | ) | |
423 | { | |
424 | int status = set_port_feature(hub->hdev, (selector << 8) | port1, | |
425 | USB_PORT_FEAT_INDICATOR); | |
426 | if (status < 0) | |
427 | dev_dbg (hub->intfdev, | |
428 | "port %d indicator %s status %d\n", | |
429 | port1, | |
430 | ({ char *s; switch (selector) { | |
431 | case HUB_LED_AMBER: s = "amber"; break; | |
432 | case HUB_LED_GREEN: s = "green"; break; | |
433 | case HUB_LED_OFF: s = "off"; break; | |
434 | case HUB_LED_AUTO: s = "auto"; break; | |
435 | default: s = "??"; break; | |
436 | }; s; }), | |
437 | status); | |
438 | } | |
439 | ||
440 | #define LED_CYCLE_PERIOD ((2*HZ)/3) | |
441 | ||
c4028958 | 442 | static void led_work (struct work_struct *work) |
1da177e4 | 443 | { |
c4028958 DH |
444 | struct usb_hub *hub = |
445 | container_of(work, struct usb_hub, leds.work); | |
1da177e4 LT |
446 | struct usb_device *hdev = hub->hdev; |
447 | unsigned i; | |
448 | unsigned changed = 0; | |
449 | int cursor = -1; | |
450 | ||
451 | if (hdev->state != USB_STATE_CONFIGURED || hub->quiescing) | |
452 | return; | |
453 | ||
454 | for (i = 0; i < hub->descriptor->bNbrPorts; i++) { | |
455 | unsigned selector, mode; | |
456 | ||
457 | /* 30%-50% duty cycle */ | |
458 | ||
459 | switch (hub->indicator[i]) { | |
460 | /* cycle marker */ | |
461 | case INDICATOR_CYCLE: | |
462 | cursor = i; | |
463 | selector = HUB_LED_AUTO; | |
464 | mode = INDICATOR_AUTO; | |
465 | break; | |
466 | /* blinking green = sw attention */ | |
467 | case INDICATOR_GREEN_BLINK: | |
468 | selector = HUB_LED_GREEN; | |
469 | mode = INDICATOR_GREEN_BLINK_OFF; | |
470 | break; | |
471 | case INDICATOR_GREEN_BLINK_OFF: | |
472 | selector = HUB_LED_OFF; | |
473 | mode = INDICATOR_GREEN_BLINK; | |
474 | break; | |
475 | /* blinking amber = hw attention */ | |
476 | case INDICATOR_AMBER_BLINK: | |
477 | selector = HUB_LED_AMBER; | |
478 | mode = INDICATOR_AMBER_BLINK_OFF; | |
479 | break; | |
480 | case INDICATOR_AMBER_BLINK_OFF: | |
481 | selector = HUB_LED_OFF; | |
482 | mode = INDICATOR_AMBER_BLINK; | |
483 | break; | |
484 | /* blink green/amber = reserved */ | |
485 | case INDICATOR_ALT_BLINK: | |
486 | selector = HUB_LED_GREEN; | |
487 | mode = INDICATOR_ALT_BLINK_OFF; | |
488 | break; | |
489 | case INDICATOR_ALT_BLINK_OFF: | |
490 | selector = HUB_LED_AMBER; | |
491 | mode = INDICATOR_ALT_BLINK; | |
492 | break; | |
493 | default: | |
494 | continue; | |
495 | } | |
496 | if (selector != HUB_LED_AUTO) | |
497 | changed = 1; | |
498 | set_port_led(hub, i + 1, selector); | |
499 | hub->indicator[i] = mode; | |
500 | } | |
501 | if (!changed && blinkenlights) { | |
502 | cursor++; | |
503 | cursor %= hub->descriptor->bNbrPorts; | |
504 | set_port_led(hub, cursor + 1, HUB_LED_GREEN); | |
505 | hub->indicator[cursor] = INDICATOR_CYCLE; | |
506 | changed++; | |
507 | } | |
508 | if (changed) | |
509 | schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); | |
510 | } | |
511 | ||
512 | /* use a short timeout for hub/port status fetches */ | |
513 | #define USB_STS_TIMEOUT 1000 | |
514 | #define USB_STS_RETRIES 5 | |
515 | ||
516 | /* | |
517 | * USB 2.0 spec Section 11.24.2.6 | |
518 | */ | |
519 | static int get_hub_status(struct usb_device *hdev, | |
520 | struct usb_hub_status *data) | |
521 | { | |
522 | int i, status = -ETIMEDOUT; | |
523 | ||
3824c1dd LP |
524 | for (i = 0; i < USB_STS_RETRIES && |
525 | (status == -ETIMEDOUT || status == -EPIPE); i++) { | |
1da177e4 LT |
526 | status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
527 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0, | |
528 | data, sizeof(*data), USB_STS_TIMEOUT); | |
529 | } | |
530 | return status; | |
531 | } | |
532 | ||
533 | /* | |
534 | * USB 2.0 spec Section 11.24.2.7 | |
535 | */ | |
536 | static int get_port_status(struct usb_device *hdev, int port1, | |
537 | struct usb_port_status *data) | |
538 | { | |
539 | int i, status = -ETIMEDOUT; | |
540 | ||
3824c1dd LP |
541 | for (i = 0; i < USB_STS_RETRIES && |
542 | (status == -ETIMEDOUT || status == -EPIPE); i++) { | |
1da177e4 LT |
543 | status = usb_control_msg(hdev, usb_rcvctrlpipe(hdev, 0), |
544 | USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port1, | |
545 | data, sizeof(*data), USB_STS_TIMEOUT); | |
546 | } | |
547 | return status; | |
548 | } | |
549 | ||
3eb14915 AS |
550 | static int hub_port_status(struct usb_hub *hub, int port1, |
551 | u16 *status, u16 *change) | |
552 | { | |
553 | int ret; | |
554 | ||
555 | mutex_lock(&hub->status_mutex); | |
556 | ret = get_port_status(hub->hdev, port1, &hub->status->port); | |
557 | if (ret < 4) { | |
e9e88fb7 AS |
558 | if (ret != -ENODEV) |
559 | dev_err(hub->intfdev, | |
560 | "%s failed (err = %d)\n", __func__, ret); | |
3eb14915 AS |
561 | if (ret >= 0) |
562 | ret = -EIO; | |
563 | } else { | |
564 | *status = le16_to_cpu(hub->status->port.wPortStatus); | |
565 | *change = le16_to_cpu(hub->status->port.wPortChange); | |
dbe79bbe | 566 | |
3eb14915 AS |
567 | ret = 0; |
568 | } | |
569 | mutex_unlock(&hub->status_mutex); | |
570 | return ret; | |
571 | } | |
572 | ||
1da177e4 LT |
573 | static void kick_khubd(struct usb_hub *hub) |
574 | { | |
575 | unsigned long flags; | |
576 | ||
577 | spin_lock_irqsave(&hub_event_lock, flags); | |
2e2c5eea | 578 | if (!hub->disconnected && list_empty(&hub->event_list)) { |
1da177e4 | 579 | list_add_tail(&hub->event_list, &hub_event_list); |
8e4ceb38 AS |
580 | |
581 | /* Suppress autosuspend until khubd runs */ | |
582 | usb_autopm_get_interface_no_resume( | |
583 | to_usb_interface(hub->intfdev)); | |
1da177e4 LT |
584 | wake_up(&khubd_wait); |
585 | } | |
586 | spin_unlock_irqrestore(&hub_event_lock, flags); | |
587 | } | |
588 | ||
589 | void usb_kick_khubd(struct usb_device *hdev) | |
590 | { | |
ad493e5e | 591 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
25118084 AS |
592 | |
593 | if (hub) | |
594 | kick_khubd(hub); | |
1da177e4 LT |
595 | } |
596 | ||
4ee823b8 SS |
597 | /* |
598 | * Let the USB core know that a USB 3.0 device has sent a Function Wake Device | |
599 | * Notification, which indicates it had initiated remote wakeup. | |
600 | * | |
601 | * USB 3.0 hubs do not report the port link state change from U3 to U0 when the | |
602 | * device initiates resume, so the USB core will not receive notice of the | |
603 | * resume through the normal hub interrupt URB. | |
604 | */ | |
605 | void usb_wakeup_notification(struct usb_device *hdev, | |
606 | unsigned int portnum) | |
607 | { | |
608 | struct usb_hub *hub; | |
609 | ||
610 | if (!hdev) | |
611 | return; | |
612 | ||
ad493e5e | 613 | hub = usb_hub_to_struct_hub(hdev); |
4ee823b8 SS |
614 | if (hub) { |
615 | set_bit(portnum, hub->wakeup_bits); | |
616 | kick_khubd(hub); | |
617 | } | |
618 | } | |
619 | EXPORT_SYMBOL_GPL(usb_wakeup_notification); | |
1da177e4 LT |
620 | |
621 | /* completion function, fires on port status changes and various faults */ | |
7d12e780 | 622 | static void hub_irq(struct urb *urb) |
1da177e4 | 623 | { |
ec17cf1c | 624 | struct usb_hub *hub = urb->context; |
e015268d | 625 | int status = urb->status; |
71d2718f | 626 | unsigned i; |
1da177e4 LT |
627 | unsigned long bits; |
628 | ||
e015268d | 629 | switch (status) { |
1da177e4 LT |
630 | case -ENOENT: /* synchronous unlink */ |
631 | case -ECONNRESET: /* async unlink */ | |
632 | case -ESHUTDOWN: /* hardware going away */ | |
633 | return; | |
634 | ||
635 | default: /* presumably an error */ | |
636 | /* Cause a hub reset after 10 consecutive errors */ | |
e015268d | 637 | dev_dbg (hub->intfdev, "transfer --> %d\n", status); |
1da177e4 LT |
638 | if ((++hub->nerrors < 10) || hub->error) |
639 | goto resubmit; | |
e015268d | 640 | hub->error = status; |
1da177e4 | 641 | /* FALL THROUGH */ |
ec17cf1c | 642 | |
1da177e4 LT |
643 | /* let khubd handle things */ |
644 | case 0: /* we got data: port status changed */ | |
645 | bits = 0; | |
646 | for (i = 0; i < urb->actual_length; ++i) | |
647 | bits |= ((unsigned long) ((*hub->buffer)[i])) | |
648 | << (i*8); | |
649 | hub->event_bits[0] = bits; | |
650 | break; | |
651 | } | |
652 | ||
653 | hub->nerrors = 0; | |
654 | ||
655 | /* Something happened, let khubd figure it out */ | |
656 | kick_khubd(hub); | |
657 | ||
658 | resubmit: | |
659 | if (hub->quiescing) | |
660 | return; | |
661 | ||
662 | if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0 | |
663 | && status != -ENODEV && status != -EPERM) | |
664 | dev_err (hub->intfdev, "resubmit --> %d\n", status); | |
665 | } | |
666 | ||
667 | /* USB 2.0 spec Section 11.24.2.3 */ | |
668 | static inline int | |
669 | hub_clear_tt_buffer (struct usb_device *hdev, u16 devinfo, u16 tt) | |
670 | { | |
889ba7bc WG |
671 | /* Need to clear both directions for control ep */ |
672 | if (((devinfo >> 11) & USB_ENDPOINT_XFERTYPE_MASK) == | |
673 | USB_ENDPOINT_XFER_CONTROL) { | |
674 | int status = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
675 | HUB_CLEAR_TT_BUFFER, USB_RT_PORT, | |
676 | devinfo ^ 0x8000, tt, NULL, 0, 1000); | |
677 | if (status) | |
678 | return status; | |
679 | } | |
c2f6595f | 680 | return usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), |
1da177e4 LT |
681 | HUB_CLEAR_TT_BUFFER, USB_RT_PORT, devinfo, |
682 | tt, NULL, 0, 1000); | |
683 | } | |
684 | ||
685 | /* | |
686 | * enumeration blocks khubd for a long time. we use keventd instead, since | |
687 | * long blocking there is the exception, not the rule. accordingly, HCDs | |
688 | * talking to TTs must queue control transfers (not just bulk and iso), so | |
689 | * both can talk to the same hub concurrently. | |
690 | */ | |
cb88a1b8 | 691 | static void hub_tt_work(struct work_struct *work) |
1da177e4 | 692 | { |
c4028958 | 693 | struct usb_hub *hub = |
cb88a1b8 | 694 | container_of(work, struct usb_hub, tt.clear_work); |
1da177e4 LT |
695 | unsigned long flags; |
696 | ||
697 | spin_lock_irqsave (&hub->tt.lock, flags); | |
3b6054da | 698 | while (!list_empty(&hub->tt.clear_list)) { |
d0f830d3 | 699 | struct list_head *next; |
1da177e4 LT |
700 | struct usb_tt_clear *clear; |
701 | struct usb_device *hdev = hub->hdev; | |
cb88a1b8 | 702 | const struct hc_driver *drv; |
1da177e4 LT |
703 | int status; |
704 | ||
d0f830d3 HS |
705 | next = hub->tt.clear_list.next; |
706 | clear = list_entry (next, struct usb_tt_clear, clear_list); | |
1da177e4 LT |
707 | list_del (&clear->clear_list); |
708 | ||
709 | /* drop lock so HCD can concurrently report other TT errors */ | |
710 | spin_unlock_irqrestore (&hub->tt.lock, flags); | |
711 | status = hub_clear_tt_buffer (hdev, clear->devinfo, clear->tt); | |
e9e88fb7 | 712 | if (status && status != -ENODEV) |
1da177e4 LT |
713 | dev_err (&hdev->dev, |
714 | "clear tt %d (%04x) error %d\n", | |
715 | clear->tt, clear->devinfo, status); | |
cb88a1b8 AS |
716 | |
717 | /* Tell the HCD, even if the operation failed */ | |
718 | drv = clear->hcd->driver; | |
719 | if (drv->clear_tt_buffer_complete) | |
720 | (drv->clear_tt_buffer_complete)(clear->hcd, clear->ep); | |
721 | ||
1bc3c9e1 | 722 | kfree(clear); |
cb88a1b8 | 723 | spin_lock_irqsave(&hub->tt.lock, flags); |
1da177e4 LT |
724 | } |
725 | spin_unlock_irqrestore (&hub->tt.lock, flags); | |
726 | } | |
727 | ||
971fcd49 LT |
728 | /** |
729 | * usb_hub_set_port_power - control hub port's power state | |
730 | * @hdev: target hub | |
731 | * @port1: port index | |
732 | * @set: expected status | |
733 | * | |
734 | * call this function to control port's power via setting or | |
735 | * clearing the port's PORT_POWER feature. | |
736 | */ | |
737 | int usb_hub_set_port_power(struct usb_device *hdev, int port1, | |
738 | bool set) | |
739 | { | |
740 | int ret; | |
ad493e5e LT |
741 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
742 | struct usb_port *port_dev = hub->ports[port1 - 1]; | |
971fcd49 LT |
743 | |
744 | if (set) | |
745 | ret = set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); | |
746 | else | |
ad493e5e LT |
747 | ret = usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_POWER); |
748 | ||
749 | if (!ret) | |
750 | port_dev->power_is_on = set; | |
971fcd49 LT |
751 | return ret; |
752 | } | |
753 | ||
1da177e4 | 754 | /** |
cb88a1b8 AS |
755 | * usb_hub_clear_tt_buffer - clear control/bulk TT state in high speed hub |
756 | * @urb: an URB associated with the failed or incomplete split transaction | |
1da177e4 LT |
757 | * |
758 | * High speed HCDs use this to tell the hub driver that some split control or | |
759 | * bulk transaction failed in a way that requires clearing internal state of | |
760 | * a transaction translator. This is normally detected (and reported) from | |
761 | * interrupt context. | |
762 | * | |
763 | * It may not be possible for that hub to handle additional full (or low) | |
764 | * speed transactions until that state is fully cleared out. | |
765 | */ | |
cb88a1b8 | 766 | int usb_hub_clear_tt_buffer(struct urb *urb) |
1da177e4 | 767 | { |
cb88a1b8 AS |
768 | struct usb_device *udev = urb->dev; |
769 | int pipe = urb->pipe; | |
1da177e4 LT |
770 | struct usb_tt *tt = udev->tt; |
771 | unsigned long flags; | |
772 | struct usb_tt_clear *clear; | |
773 | ||
774 | /* we've got to cope with an arbitrary number of pending TT clears, | |
775 | * since each TT has "at least two" buffers that can need it (and | |
776 | * there can be many TTs per hub). even if they're uncommon. | |
777 | */ | |
54e6ecb2 | 778 | if ((clear = kmalloc (sizeof *clear, GFP_ATOMIC)) == NULL) { |
1da177e4 LT |
779 | dev_err (&udev->dev, "can't save CLEAR_TT_BUFFER state\n"); |
780 | /* FIXME recover somehow ... RESET_TT? */ | |
cb88a1b8 | 781 | return -ENOMEM; |
1da177e4 LT |
782 | } |
783 | ||
784 | /* info that CLEAR_TT_BUFFER needs */ | |
785 | clear->tt = tt->multi ? udev->ttport : 1; | |
786 | clear->devinfo = usb_pipeendpoint (pipe); | |
787 | clear->devinfo |= udev->devnum << 4; | |
788 | clear->devinfo |= usb_pipecontrol (pipe) | |
789 | ? (USB_ENDPOINT_XFER_CONTROL << 11) | |
790 | : (USB_ENDPOINT_XFER_BULK << 11); | |
791 | if (usb_pipein (pipe)) | |
792 | clear->devinfo |= 1 << 15; | |
cb88a1b8 AS |
793 | |
794 | /* info for completion callback */ | |
795 | clear->hcd = bus_to_hcd(udev->bus); | |
796 | clear->ep = urb->ep; | |
797 | ||
1da177e4 LT |
798 | /* tell keventd to clear state for this TT */ |
799 | spin_lock_irqsave (&tt->lock, flags); | |
800 | list_add_tail (&clear->clear_list, &tt->clear_list); | |
cb88a1b8 | 801 | schedule_work(&tt->clear_work); |
1da177e4 | 802 | spin_unlock_irqrestore (&tt->lock, flags); |
cb88a1b8 | 803 | return 0; |
1da177e4 | 804 | } |
cb88a1b8 | 805 | EXPORT_SYMBOL_GPL(usb_hub_clear_tt_buffer); |
1da177e4 | 806 | |
8520f380 AS |
807 | /* If do_delay is false, return the number of milliseconds the caller |
808 | * needs to delay. | |
809 | */ | |
810 | static unsigned hub_power_on(struct usb_hub *hub, bool do_delay) | |
1da177e4 LT |
811 | { |
812 | int port1; | |
b789696a | 813 | unsigned pgood_delay = hub->descriptor->bPwrOn2PwrGood * 2; |
8520f380 | 814 | unsigned delay; |
4489a571 AS |
815 | u16 wHubCharacteristics = |
816 | le16_to_cpu(hub->descriptor->wHubCharacteristics); | |
817 | ||
818 | /* Enable power on each port. Some hubs have reserved values | |
819 | * of LPSM (> 2) in their descriptors, even though they are | |
820 | * USB 2.0 hubs. Some hubs do not implement port-power switching | |
821 | * but only emulate it. In all cases, the ports won't work | |
822 | * unless we send these messages to the hub. | |
823 | */ | |
824 | if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2) | |
1da177e4 | 825 | dev_dbg(hub->intfdev, "enabling power on all ports\n"); |
4489a571 AS |
826 | else |
827 | dev_dbg(hub->intfdev, "trying to enable port power on " | |
828 | "non-switchable hub\n"); | |
829 | for (port1 = 1; port1 <= hub->descriptor->bNbrPorts; port1++) | |
ad493e5e LT |
830 | if (hub->ports[port1 - 1]->power_is_on) |
831 | set_port_feature(hub->hdev, port1, USB_PORT_FEAT_POWER); | |
832 | else | |
833 | usb_clear_port_feature(hub->hdev, port1, | |
834 | USB_PORT_FEAT_POWER); | |
1da177e4 | 835 | |
b789696a | 836 | /* Wait at least 100 msec for power to become stable */ |
8520f380 AS |
837 | delay = max(pgood_delay, (unsigned) 100); |
838 | if (do_delay) | |
839 | msleep(delay); | |
840 | return delay; | |
1da177e4 LT |
841 | } |
842 | ||
1da177e4 LT |
843 | static int hub_hub_status(struct usb_hub *hub, |
844 | u16 *status, u16 *change) | |
845 | { | |
846 | int ret; | |
847 | ||
db90e7a1 | 848 | mutex_lock(&hub->status_mutex); |
1da177e4 | 849 | ret = get_hub_status(hub->hdev, &hub->status->hub); |
e9e88fb7 AS |
850 | if (ret < 0) { |
851 | if (ret != -ENODEV) | |
852 | dev_err(hub->intfdev, | |
853 | "%s failed (err = %d)\n", __func__, ret); | |
854 | } else { | |
1da177e4 LT |
855 | *status = le16_to_cpu(hub->status->hub.wHubStatus); |
856 | *change = le16_to_cpu(hub->status->hub.wHubChange); | |
857 | ret = 0; | |
858 | } | |
db90e7a1 | 859 | mutex_unlock(&hub->status_mutex); |
1da177e4 LT |
860 | return ret; |
861 | } | |
862 | ||
41e7e056 SS |
863 | static int hub_set_port_link_state(struct usb_hub *hub, int port1, |
864 | unsigned int link_status) | |
865 | { | |
866 | return set_port_feature(hub->hdev, | |
867 | port1 | (link_status << 3), | |
868 | USB_PORT_FEAT_LINK_STATE); | |
869 | } | |
870 | ||
871 | /* | |
872 | * If USB 3.0 ports are placed into the Disabled state, they will no longer | |
873 | * detect any device connects or disconnects. This is generally not what the | |
874 | * USB core wants, since it expects a disabled port to produce a port status | |
875 | * change event when a new device connects. | |
876 | * | |
877 | * Instead, set the link state to Disabled, wait for the link to settle into | |
878 | * that state, clear any change bits, and then put the port into the RxDetect | |
879 | * state. | |
880 | */ | |
881 | static int hub_usb3_port_disable(struct usb_hub *hub, int port1) | |
882 | { | |
883 | int ret; | |
884 | int total_time; | |
885 | u16 portchange, portstatus; | |
886 | ||
887 | if (!hub_is_superspeed(hub->hdev)) | |
888 | return -EINVAL; | |
889 | ||
42cfa82f GG |
890 | ret = hub_port_status(hub, port1, &portstatus, &portchange); |
891 | if (ret < 0) | |
892 | return ret; | |
893 | ||
894 | /* | |
895 | * USB controller Advanced Micro Devices, Inc. [AMD] FCH USB XHCI | |
896 | * Controller [1022:7814] will have spurious result making the following | |
897 | * usb 3.0 device hotplugging route to the 2.0 root hub and recognized | |
898 | * as high-speed device if we set the usb 3.0 port link state to | |
899 | * Disabled. Since it's already in USB_SS_PORT_LS_RX_DETECT state, we | |
900 | * check the state here to avoid the bug. | |
901 | */ | |
902 | if ((portstatus & USB_PORT_STAT_LINK_STATE) == | |
903 | USB_SS_PORT_LS_RX_DETECT) { | |
904 | dev_dbg(&hub->ports[port1 - 1]->dev, | |
905 | "Not disabling port; link state is RxDetect\n"); | |
906 | return ret; | |
907 | } | |
908 | ||
41e7e056 | 909 | ret = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_SS_DISABLED); |
e9e88fb7 | 910 | if (ret) |
41e7e056 | 911 | return ret; |
41e7e056 SS |
912 | |
913 | /* Wait for the link to enter the disabled state. */ | |
914 | for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { | |
915 | ret = hub_port_status(hub, port1, &portstatus, &portchange); | |
916 | if (ret < 0) | |
917 | return ret; | |
918 | ||
919 | if ((portstatus & USB_PORT_STAT_LINK_STATE) == | |
920 | USB_SS_PORT_LS_SS_DISABLED) | |
921 | break; | |
922 | if (total_time >= HUB_DEBOUNCE_TIMEOUT) | |
923 | break; | |
924 | msleep(HUB_DEBOUNCE_STEP); | |
925 | } | |
926 | if (total_time >= HUB_DEBOUNCE_TIMEOUT) | |
927 | dev_warn(hub->intfdev, "Could not disable port %d after %d ms\n", | |
928 | port1, total_time); | |
929 | ||
930 | return hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_RX_DETECT); | |
931 | } | |
932 | ||
8b28c752 AS |
933 | static int hub_port_disable(struct usb_hub *hub, int port1, int set_state) |
934 | { | |
935 | struct usb_device *hdev = hub->hdev; | |
0458d5b4 | 936 | int ret = 0; |
8b28c752 | 937 | |
ff823c79 LT |
938 | if (hub->ports[port1 - 1]->child && set_state) |
939 | usb_set_device_state(hub->ports[port1 - 1]->child, | |
8b28c752 | 940 | USB_STATE_NOTATTACHED); |
41e7e056 SS |
941 | if (!hub->error) { |
942 | if (hub_is_superspeed(hub->hdev)) | |
943 | ret = hub_usb3_port_disable(hub, port1); | |
944 | else | |
ad493e5e | 945 | ret = usb_clear_port_feature(hdev, port1, |
41e7e056 SS |
946 | USB_PORT_FEAT_ENABLE); |
947 | } | |
e9e88fb7 | 948 | if (ret && ret != -ENODEV) |
8b28c752 | 949 | dev_err(hub->intfdev, "cannot disable port %d (err = %d)\n", |
0458d5b4 | 950 | port1, ret); |
8b28c752 AS |
951 | return ret; |
952 | } | |
953 | ||
0458d5b4 | 954 | /* |
6d42fcdb | 955 | * Disable a port and mark a logical connect-change event, so that some |
0458d5b4 AS |
956 | * time later khubd will disconnect() any existing usb_device on the port |
957 | * and will re-enumerate if there actually is a device attached. | |
958 | */ | |
959 | static void hub_port_logical_disconnect(struct usb_hub *hub, int port1) | |
960 | { | |
961 | dev_dbg(hub->intfdev, "logical disconnect on port %d\n", port1); | |
962 | hub_port_disable(hub, port1, 1); | |
7d069b7d | 963 | |
0458d5b4 AS |
964 | /* FIXME let caller ask to power down the port: |
965 | * - some devices won't enumerate without a VBUS power cycle | |
966 | * - SRP saves power that way | |
967 | * - ... new call, TBD ... | |
968 | * That's easy if this hub can switch power per-port, and | |
969 | * khubd reactivates the port later (timer, SRP, etc). | |
970 | * Powerdown must be optional, because of reset/DFU. | |
971 | */ | |
972 | ||
973 | set_bit(port1, hub->change_bits); | |
974 | kick_khubd(hub); | |
975 | } | |
976 | ||
253e0572 AS |
977 | /** |
978 | * usb_remove_device - disable a device's port on its parent hub | |
979 | * @udev: device to be disabled and removed | |
980 | * Context: @udev locked, must be able to sleep. | |
981 | * | |
982 | * After @udev's port has been disabled, khubd is notified and it will | |
983 | * see that the device has been disconnected. When the device is | |
984 | * physically unplugged and something is plugged in, the events will | |
985 | * be received and processed normally. | |
986 | */ | |
987 | int usb_remove_device(struct usb_device *udev) | |
988 | { | |
989 | struct usb_hub *hub; | |
990 | struct usb_interface *intf; | |
991 | ||
992 | if (!udev->parent) /* Can't remove a root hub */ | |
993 | return -EINVAL; | |
ad493e5e | 994 | hub = usb_hub_to_struct_hub(udev->parent); |
253e0572 AS |
995 | intf = to_usb_interface(hub->intfdev); |
996 | ||
997 | usb_autopm_get_interface(intf); | |
998 | set_bit(udev->portnum, hub->removed_bits); | |
999 | hub_port_logical_disconnect(hub, udev->portnum); | |
1000 | usb_autopm_put_interface(intf); | |
1001 | return 0; | |
1002 | } | |
1003 | ||
6ee0b270 | 1004 | enum hub_activation_type { |
8e4ceb38 | 1005 | HUB_INIT, HUB_INIT2, HUB_INIT3, /* INITs must come first */ |
8520f380 | 1006 | HUB_POST_RESET, HUB_RESUME, HUB_RESET_RESUME, |
6ee0b270 | 1007 | }; |
5e6effae | 1008 | |
8520f380 AS |
1009 | static void hub_init_func2(struct work_struct *ws); |
1010 | static void hub_init_func3(struct work_struct *ws); | |
1011 | ||
f2835219 | 1012 | static void hub_activate(struct usb_hub *hub, enum hub_activation_type type) |
5e6effae AS |
1013 | { |
1014 | struct usb_device *hdev = hub->hdev; | |
653a39d1 SS |
1015 | struct usb_hcd *hcd; |
1016 | int ret; | |
5e6effae | 1017 | int port1; |
f2835219 | 1018 | int status; |
948fea37 | 1019 | bool need_debounce_delay = false; |
8520f380 AS |
1020 | unsigned delay; |
1021 | ||
1022 | /* Continue a partial initialization */ | |
1023 | if (type == HUB_INIT2) | |
1024 | goto init2; | |
1025 | if (type == HUB_INIT3) | |
1026 | goto init3; | |
5e6effae | 1027 | |
a45aa3b3 EF |
1028 | /* The superspeed hub except for root hub has to use Hub Depth |
1029 | * value as an offset into the route string to locate the bits | |
1030 | * it uses to determine the downstream port number. So hub driver | |
1031 | * should send a set hub depth request to superspeed hub after | |
1032 | * the superspeed hub is set configuration in initialization or | |
1033 | * reset procedure. | |
1034 | * | |
1035 | * After a resume, port power should still be on. | |
f2835219 AS |
1036 | * For any other type of activation, turn it on. |
1037 | */ | |
8520f380 | 1038 | if (type != HUB_RESUME) { |
a45aa3b3 EF |
1039 | if (hdev->parent && hub_is_superspeed(hdev)) { |
1040 | ret = usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0), | |
1041 | HUB_SET_DEPTH, USB_RT_HUB, | |
1042 | hdev->level - 1, 0, NULL, 0, | |
1043 | USB_CTRL_SET_TIMEOUT); | |
1044 | if (ret < 0) | |
1045 | dev_err(hub->intfdev, | |
1046 | "set hub depth failed\n"); | |
1047 | } | |
8520f380 AS |
1048 | |
1049 | /* Speed up system boot by using a delayed_work for the | |
1050 | * hub's initial power-up delays. This is pretty awkward | |
1051 | * and the implementation looks like a home-brewed sort of | |
1052 | * setjmp/longjmp, but it saves at least 100 ms for each | |
1053 | * root hub (assuming usbcore is compiled into the kernel | |
1054 | * rather than as a module). It adds up. | |
1055 | * | |
1056 | * This can't be done for HUB_RESUME or HUB_RESET_RESUME | |
1057 | * because for those activation types the ports have to be | |
1058 | * operational when we return. In theory this could be done | |
1059 | * for HUB_POST_RESET, but it's easier not to. | |
1060 | */ | |
1061 | if (type == HUB_INIT) { | |
1062 | delay = hub_power_on(hub, false); | |
1063 | PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func2); | |
1064 | schedule_delayed_work(&hub->init_work, | |
1065 | msecs_to_jiffies(delay)); | |
61fbeba1 AS |
1066 | |
1067 | /* Suppress autosuspend until init is done */ | |
8e4ceb38 AS |
1068 | usb_autopm_get_interface_no_resume( |
1069 | to_usb_interface(hub->intfdev)); | |
8520f380 | 1070 | return; /* Continues at init2: below */ |
653a39d1 SS |
1071 | } else if (type == HUB_RESET_RESUME) { |
1072 | /* The internal host controller state for the hub device | |
1073 | * may be gone after a host power loss on system resume. | |
1074 | * Update the device's info so the HW knows it's a hub. | |
1075 | */ | |
1076 | hcd = bus_to_hcd(hdev->bus); | |
1077 | if (hcd->driver->update_hub_device) { | |
1078 | ret = hcd->driver->update_hub_device(hcd, hdev, | |
1079 | &hub->tt, GFP_NOIO); | |
1080 | if (ret < 0) { | |
1081 | dev_err(hub->intfdev, "Host not " | |
1082 | "accepting hub info " | |
1083 | "update.\n"); | |
1084 | dev_err(hub->intfdev, "LS/FS devices " | |
1085 | "and hubs may not work " | |
1086 | "under this hub\n."); | |
1087 | } | |
1088 | } | |
1089 | hub_power_on(hub, true); | |
8520f380 AS |
1090 | } else { |
1091 | hub_power_on(hub, true); | |
1092 | } | |
1093 | } | |
1094 | init2: | |
f2835219 | 1095 | |
6ee0b270 AS |
1096 | /* Check each port and set hub->change_bits to let khubd know |
1097 | * which ports need attention. | |
5e6effae AS |
1098 | */ |
1099 | for (port1 = 1; port1 <= hdev->maxchild; ++port1) { | |
ff823c79 | 1100 | struct usb_device *udev = hub->ports[port1 - 1]->child; |
5e6effae AS |
1101 | u16 portstatus, portchange; |
1102 | ||
6ee0b270 AS |
1103 | portstatus = portchange = 0; |
1104 | status = hub_port_status(hub, port1, &portstatus, &portchange); | |
1105 | if (udev || (portstatus & USB_PORT_STAT_CONNECTION)) | |
1106 | dev_dbg(hub->intfdev, | |
1107 | "port %d: status %04x change %04x\n", | |
1108 | port1, portstatus, portchange); | |
1109 | ||
1110 | /* After anything other than HUB_RESUME (i.e., initialization | |
1111 | * or any sort of reset), every port should be disabled. | |
1112 | * Unconnected ports should likewise be disabled (paranoia), | |
1113 | * and so should ports for which we have no usb_device. | |
1114 | */ | |
1115 | if ((portstatus & USB_PORT_STAT_ENABLE) && ( | |
1116 | type != HUB_RESUME || | |
1117 | !(portstatus & USB_PORT_STAT_CONNECTION) || | |
1118 | !udev || | |
1119 | udev->state == USB_STATE_NOTATTACHED)) { | |
9f0a6cd3 AX |
1120 | /* |
1121 | * USB3 protocol ports will automatically transition | |
1122 | * to Enabled state when detect an USB3.0 device attach. | |
1123 | * Do not disable USB3 protocol ports. | |
9f0a6cd3 | 1124 | */ |
131dec34 | 1125 | if (!hub_is_superspeed(hdev)) { |
ad493e5e | 1126 | usb_clear_port_feature(hdev, port1, |
9f0a6cd3 AX |
1127 | USB_PORT_FEAT_ENABLE); |
1128 | portstatus &= ~USB_PORT_STAT_ENABLE; | |
85f0ff46 SS |
1129 | } else { |
1130 | /* Pretend that power was lost for USB3 devs */ | |
1131 | portstatus &= ~USB_PORT_STAT_ENABLE; | |
9f0a6cd3 | 1132 | } |
5e6effae AS |
1133 | } |
1134 | ||
948fea37 AS |
1135 | /* Clear status-change flags; we'll debounce later */ |
1136 | if (portchange & USB_PORT_STAT_C_CONNECTION) { | |
1137 | need_debounce_delay = true; | |
ad493e5e | 1138 | usb_clear_port_feature(hub->hdev, port1, |
948fea37 AS |
1139 | USB_PORT_FEAT_C_CONNECTION); |
1140 | } | |
1141 | if (portchange & USB_PORT_STAT_C_ENABLE) { | |
1142 | need_debounce_delay = true; | |
ad493e5e | 1143 | usb_clear_port_feature(hub->hdev, port1, |
948fea37 AS |
1144 | USB_PORT_FEAT_C_ENABLE); |
1145 | } | |
1316580f JW |
1146 | if (portchange & USB_PORT_STAT_C_RESET) { |
1147 | need_debounce_delay = true; | |
1148 | usb_clear_port_feature(hub->hdev, port1, | |
1149 | USB_PORT_FEAT_C_RESET); | |
1150 | } | |
79c3dd81 DZ |
1151 | if ((portchange & USB_PORT_STAT_C_BH_RESET) && |
1152 | hub_is_superspeed(hub->hdev)) { | |
1153 | need_debounce_delay = true; | |
ad493e5e | 1154 | usb_clear_port_feature(hub->hdev, port1, |
79c3dd81 DZ |
1155 | USB_PORT_FEAT_C_BH_PORT_RESET); |
1156 | } | |
253e0572 AS |
1157 | /* We can forget about a "removed" device when there's a |
1158 | * physical disconnect or the connect status changes. | |
1159 | */ | |
1160 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || | |
1161 | (portchange & USB_PORT_STAT_C_CONNECTION)) | |
1162 | clear_bit(port1, hub->removed_bits); | |
1163 | ||
6ee0b270 AS |
1164 | if (!udev || udev->state == USB_STATE_NOTATTACHED) { |
1165 | /* Tell khubd to disconnect the device or | |
1166 | * check for a new connection | |
1167 | */ | |
47415d54 SG |
1168 | if (udev || (portstatus & USB_PORT_STAT_CONNECTION) || |
1169 | (portstatus & USB_PORT_STAT_OVERCURRENT)) | |
6ee0b270 AS |
1170 | set_bit(port1, hub->change_bits); |
1171 | ||
1172 | } else if (portstatus & USB_PORT_STAT_ENABLE) { | |
72937e1e SS |
1173 | bool port_resumed = (portstatus & |
1174 | USB_PORT_STAT_LINK_STATE) == | |
1175 | USB_SS_PORT_LS_U0; | |
6ee0b270 AS |
1176 | /* The power session apparently survived the resume. |
1177 | * If there was an overcurrent or suspend change | |
1178 | * (i.e., remote wakeup request), have khubd | |
72937e1e SS |
1179 | * take care of it. Look at the port link state |
1180 | * for USB 3.0 hubs, since they don't have a suspend | |
1181 | * change bit, and they don't set the port link change | |
1182 | * bit on device-initiated resume. | |
6ee0b270 | 1183 | */ |
72937e1e SS |
1184 | if (portchange || (hub_is_superspeed(hub->hdev) && |
1185 | port_resumed)) | |
6ee0b270 | 1186 | set_bit(port1, hub->change_bits); |
5e6effae | 1187 | |
6ee0b270 | 1188 | } else if (udev->persist_enabled) { |
ad493e5e LT |
1189 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
1190 | ||
6ee0b270 | 1191 | #ifdef CONFIG_PM |
5e6effae | 1192 | udev->reset_resume = 1; |
6ee0b270 | 1193 | #endif |
ad493e5e LT |
1194 | /* Don't set the change_bits when the device |
1195 | * was powered off. | |
1196 | */ | |
1197 | if (port_dev->power_is_on) | |
1198 | set_bit(port1, hub->change_bits); | |
8808f00c | 1199 | |
6ee0b270 AS |
1200 | } else { |
1201 | /* The power session is gone; tell khubd */ | |
1202 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); | |
1203 | set_bit(port1, hub->change_bits); | |
5e6effae | 1204 | } |
5e6effae AS |
1205 | } |
1206 | ||
948fea37 AS |
1207 | /* If no port-status-change flags were set, we don't need any |
1208 | * debouncing. If flags were set we can try to debounce the | |
1209 | * ports all at once right now, instead of letting khubd do them | |
1210 | * one at a time later on. | |
1211 | * | |
1212 | * If any port-status changes do occur during this delay, khubd | |
1213 | * will see them later and handle them normally. | |
1214 | */ | |
8520f380 AS |
1215 | if (need_debounce_delay) { |
1216 | delay = HUB_DEBOUNCE_STABLE; | |
1217 | ||
1218 | /* Don't do a long sleep inside a workqueue routine */ | |
1219 | if (type == HUB_INIT2) { | |
1220 | PREPARE_DELAYED_WORK(&hub->init_work, hub_init_func3); | |
1221 | schedule_delayed_work(&hub->init_work, | |
1222 | msecs_to_jiffies(delay)); | |
1223 | return; /* Continues at init3: below */ | |
1224 | } else { | |
1225 | msleep(delay); | |
1226 | } | |
1227 | } | |
1228 | init3: | |
f2835219 AS |
1229 | hub->quiescing = 0; |
1230 | ||
1231 | status = usb_submit_urb(hub->urb, GFP_NOIO); | |
1232 | if (status < 0) | |
1233 | dev_err(hub->intfdev, "activate --> %d\n", status); | |
1234 | if (hub->has_indicators && blinkenlights) | |
1235 | schedule_delayed_work(&hub->leds, LED_CYCLE_PERIOD); | |
1236 | ||
1237 | /* Scan all ports that need attention */ | |
1238 | kick_khubd(hub); | |
8e4ceb38 AS |
1239 | |
1240 | /* Allow autosuspend if it was suppressed */ | |
1241 | if (type <= HUB_INIT3) | |
1242 | usb_autopm_put_interface_async(to_usb_interface(hub->intfdev)); | |
5e6effae AS |
1243 | } |
1244 | ||
8520f380 AS |
1245 | /* Implement the continuations for the delays above */ |
1246 | static void hub_init_func2(struct work_struct *ws) | |
1247 | { | |
1248 | struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); | |
1249 | ||
1250 | hub_activate(hub, HUB_INIT2); | |
1251 | } | |
1252 | ||
1253 | static void hub_init_func3(struct work_struct *ws) | |
1254 | { | |
1255 | struct usb_hub *hub = container_of(ws, struct usb_hub, init_work.work); | |
1256 | ||
1257 | hub_activate(hub, HUB_INIT3); | |
1258 | } | |
1259 | ||
4330354f AS |
1260 | enum hub_quiescing_type { |
1261 | HUB_DISCONNECT, HUB_PRE_RESET, HUB_SUSPEND | |
1262 | }; | |
1263 | ||
1264 | static void hub_quiesce(struct usb_hub *hub, enum hub_quiescing_type type) | |
1265 | { | |
1266 | struct usb_device *hdev = hub->hdev; | |
1267 | int i; | |
1268 | ||
8520f380 AS |
1269 | cancel_delayed_work_sync(&hub->init_work); |
1270 | ||
4330354f AS |
1271 | /* khubd and related activity won't re-trigger */ |
1272 | hub->quiescing = 1; | |
1273 | ||
1274 | if (type != HUB_SUSPEND) { | |
1275 | /* Disconnect all the children */ | |
1276 | for (i = 0; i < hdev->maxchild; ++i) { | |
ff823c79 LT |
1277 | if (hub->ports[i]->child) |
1278 | usb_disconnect(&hub->ports[i]->child); | |
4330354f AS |
1279 | } |
1280 | } | |
1281 | ||
1282 | /* Stop khubd and related activity */ | |
1283 | usb_kill_urb(hub->urb); | |
1284 | if (hub->has_indicators) | |
1285 | cancel_delayed_work_sync(&hub->leds); | |
1286 | if (hub->tt.hub) | |
036546bf | 1287 | flush_work(&hub->tt.clear_work); |
4330354f AS |
1288 | } |
1289 | ||
3eb14915 AS |
1290 | /* caller has locked the hub device */ |
1291 | static int hub_pre_reset(struct usb_interface *intf) | |
1292 | { | |
1293 | struct usb_hub *hub = usb_get_intfdata(intf); | |
1294 | ||
4330354f | 1295 | hub_quiesce(hub, HUB_PRE_RESET); |
f07600cf | 1296 | return 0; |
7d069b7d AS |
1297 | } |
1298 | ||
1299 | /* caller has locked the hub device */ | |
f07600cf | 1300 | static int hub_post_reset(struct usb_interface *intf) |
7d069b7d | 1301 | { |
7de18d8b AS |
1302 | struct usb_hub *hub = usb_get_intfdata(intf); |
1303 | ||
f2835219 | 1304 | hub_activate(hub, HUB_POST_RESET); |
f07600cf | 1305 | return 0; |
7d069b7d AS |
1306 | } |
1307 | ||
1da177e4 LT |
1308 | static int hub_configure(struct usb_hub *hub, |
1309 | struct usb_endpoint_descriptor *endpoint) | |
1310 | { | |
b356b7c7 | 1311 | struct usb_hcd *hcd; |
1da177e4 LT |
1312 | struct usb_device *hdev = hub->hdev; |
1313 | struct device *hub_dev = hub->intfdev; | |
1314 | u16 hubstatus, hubchange; | |
74ad9bd2 | 1315 | u16 wHubCharacteristics; |
1da177e4 | 1316 | unsigned int pipe; |
fa2a9566 | 1317 | int maxp, ret, i; |
7cbe5dca | 1318 | char *message = "out of memory"; |
430ee58e SAS |
1319 | unsigned unit_load; |
1320 | unsigned full_load; | |
1da177e4 | 1321 | |
d697cdda | 1322 | hub->buffer = kmalloc(sizeof(*hub->buffer), GFP_KERNEL); |
1da177e4 | 1323 | if (!hub->buffer) { |
1da177e4 LT |
1324 | ret = -ENOMEM; |
1325 | goto fail; | |
1326 | } | |
1327 | ||
1328 | hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL); | |
1329 | if (!hub->status) { | |
1da177e4 LT |
1330 | ret = -ENOMEM; |
1331 | goto fail; | |
1332 | } | |
db90e7a1 | 1333 | mutex_init(&hub->status_mutex); |
1da177e4 LT |
1334 | |
1335 | hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL); | |
1336 | if (!hub->descriptor) { | |
1da177e4 LT |
1337 | ret = -ENOMEM; |
1338 | goto fail; | |
1339 | } | |
1340 | ||
1341 | /* Request the entire hub descriptor. | |
1342 | * hub->descriptor can handle USB_MAXCHILDREN ports, | |
1343 | * but the hub can/will return fewer bytes here. | |
1344 | */ | |
dbe79bbe | 1345 | ret = get_hub_descriptor(hdev, hub->descriptor); |
1da177e4 LT |
1346 | if (ret < 0) { |
1347 | message = "can't read hub descriptor"; | |
1348 | goto fail; | |
1349 | } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) { | |
1350 | message = "hub has too many ports!"; | |
1351 | ret = -ENODEV; | |
1352 | goto fail; | |
769d7368 DL |
1353 | } else if (hub->descriptor->bNbrPorts == 0) { |
1354 | message = "hub doesn't have any ports!"; | |
1355 | ret = -ENODEV; | |
1356 | goto fail; | |
1da177e4 LT |
1357 | } |
1358 | ||
1359 | hdev->maxchild = hub->descriptor->bNbrPorts; | |
1360 | dev_info (hub_dev, "%d port%s detected\n", hdev->maxchild, | |
1361 | (hdev->maxchild == 1) ? "" : "s"); | |
1362 | ||
fa2a9566 LT |
1363 | hub->ports = kzalloc(hdev->maxchild * sizeof(struct usb_port *), |
1364 | GFP_KERNEL); | |
ff823c79 | 1365 | if (!hub->ports) { |
7cbe5dca AS |
1366 | ret = -ENOMEM; |
1367 | goto fail; | |
1368 | } | |
1369 | ||
74ad9bd2 | 1370 | wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); |
430ee58e SAS |
1371 | if (hub_is_superspeed(hdev)) { |
1372 | unit_load = 150; | |
1373 | full_load = 900; | |
1374 | } else { | |
1375 | unit_load = 100; | |
1376 | full_load = 500; | |
1377 | } | |
1da177e4 | 1378 | |
dbe79bbe JY |
1379 | /* FIXME for USB 3.0, skip for now */ |
1380 | if ((wHubCharacteristics & HUB_CHAR_COMPOUND) && | |
1381 | !(hub_is_superspeed(hdev))) { | |
1da177e4 LT |
1382 | int i; |
1383 | char portstr [USB_MAXCHILDREN + 1]; | |
1384 | ||
1385 | for (i = 0; i < hdev->maxchild; i++) | |
dbe79bbe | 1386 | portstr[i] = hub->descriptor->u.hs.DeviceRemovable |
1da177e4 LT |
1387 | [((i + 1) / 8)] & (1 << ((i + 1) % 8)) |
1388 | ? 'F' : 'R'; | |
1389 | portstr[hdev->maxchild] = 0; | |
1390 | dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr); | |
1391 | } else | |
1392 | dev_dbg(hub_dev, "standalone hub\n"); | |
1393 | ||
74ad9bd2 | 1394 | switch (wHubCharacteristics & HUB_CHAR_LPSM) { |
7bf01185 AD |
1395 | case HUB_CHAR_COMMON_LPSM: |
1396 | dev_dbg(hub_dev, "ganged power switching\n"); | |
1397 | break; | |
1398 | case HUB_CHAR_INDV_PORT_LPSM: | |
1399 | dev_dbg(hub_dev, "individual port power switching\n"); | |
1400 | break; | |
1401 | case HUB_CHAR_NO_LPSM: | |
1402 | case HUB_CHAR_LPSM: | |
1403 | dev_dbg(hub_dev, "no power switching (usb 1.0)\n"); | |
1404 | break; | |
1da177e4 LT |
1405 | } |
1406 | ||
74ad9bd2 | 1407 | switch (wHubCharacteristics & HUB_CHAR_OCPM) { |
7bf01185 AD |
1408 | case HUB_CHAR_COMMON_OCPM: |
1409 | dev_dbg(hub_dev, "global over-current protection\n"); | |
1410 | break; | |
1411 | case HUB_CHAR_INDV_PORT_OCPM: | |
1412 | dev_dbg(hub_dev, "individual port over-current protection\n"); | |
1413 | break; | |
1414 | case HUB_CHAR_NO_OCPM: | |
1415 | case HUB_CHAR_OCPM: | |
1416 | dev_dbg(hub_dev, "no over-current protection\n"); | |
1417 | break; | |
1da177e4 LT |
1418 | } |
1419 | ||
1420 | spin_lock_init (&hub->tt.lock); | |
1421 | INIT_LIST_HEAD (&hub->tt.clear_list); | |
cb88a1b8 | 1422 | INIT_WORK(&hub->tt.clear_work, hub_tt_work); |
1da177e4 | 1423 | switch (hdev->descriptor.bDeviceProtocol) { |
7bf01185 AD |
1424 | case USB_HUB_PR_FS: |
1425 | break; | |
1426 | case USB_HUB_PR_HS_SINGLE_TT: | |
1427 | dev_dbg(hub_dev, "Single TT\n"); | |
1428 | hub->tt.hub = hdev; | |
1429 | break; | |
1430 | case USB_HUB_PR_HS_MULTI_TT: | |
1431 | ret = usb_set_interface(hdev, 0, 1); | |
1432 | if (ret == 0) { | |
1433 | dev_dbg(hub_dev, "TT per port\n"); | |
1434 | hub->tt.multi = 1; | |
1435 | } else | |
1436 | dev_err(hub_dev, "Using single TT (err %d)\n", | |
1437 | ret); | |
1438 | hub->tt.hub = hdev; | |
1439 | break; | |
1440 | case USB_HUB_PR_SS: | |
1441 | /* USB 3.0 hubs don't have a TT */ | |
1442 | break; | |
1443 | default: | |
1444 | dev_dbg(hub_dev, "Unrecognized hub protocol %d\n", | |
1445 | hdev->descriptor.bDeviceProtocol); | |
1446 | break; | |
1da177e4 LT |
1447 | } |
1448 | ||
e09711ae | 1449 | /* Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns */ |
74ad9bd2 | 1450 | switch (wHubCharacteristics & HUB_CHAR_TTTT) { |
e09711ae DB |
1451 | case HUB_TTTT_8_BITS: |
1452 | if (hdev->descriptor.bDeviceProtocol != 0) { | |
1453 | hub->tt.think_time = 666; | |
1454 | dev_dbg(hub_dev, "TT requires at most %d " | |
1455 | "FS bit times (%d ns)\n", | |
1456 | 8, hub->tt.think_time); | |
1457 | } | |
1da177e4 | 1458 | break; |
e09711ae DB |
1459 | case HUB_TTTT_16_BITS: |
1460 | hub->tt.think_time = 666 * 2; | |
1461 | dev_dbg(hub_dev, "TT requires at most %d " | |
1462 | "FS bit times (%d ns)\n", | |
1463 | 16, hub->tt.think_time); | |
1da177e4 | 1464 | break; |
e09711ae DB |
1465 | case HUB_TTTT_24_BITS: |
1466 | hub->tt.think_time = 666 * 3; | |
1467 | dev_dbg(hub_dev, "TT requires at most %d " | |
1468 | "FS bit times (%d ns)\n", | |
1469 | 24, hub->tt.think_time); | |
1da177e4 | 1470 | break; |
e09711ae DB |
1471 | case HUB_TTTT_32_BITS: |
1472 | hub->tt.think_time = 666 * 4; | |
1473 | dev_dbg(hub_dev, "TT requires at most %d " | |
1474 | "FS bit times (%d ns)\n", | |
1475 | 32, hub->tt.think_time); | |
1da177e4 LT |
1476 | break; |
1477 | } | |
1478 | ||
1479 | /* probe() zeroes hub->indicator[] */ | |
74ad9bd2 | 1480 | if (wHubCharacteristics & HUB_CHAR_PORTIND) { |
1da177e4 LT |
1481 | hub->has_indicators = 1; |
1482 | dev_dbg(hub_dev, "Port indicators are supported\n"); | |
1483 | } | |
1484 | ||
1485 | dev_dbg(hub_dev, "power on to power good time: %dms\n", | |
1486 | hub->descriptor->bPwrOn2PwrGood * 2); | |
1487 | ||
1488 | /* power budgeting mostly matters with bus-powered hubs, | |
1489 | * and battery-powered root hubs (may provide just 8 mA). | |
1490 | */ | |
1491 | ret = usb_get_status(hdev, USB_RECIP_DEVICE, 0, &hubstatus); | |
55c52718 | 1492 | if (ret < 2) { |
1da177e4 LT |
1493 | message = "can't get hub status"; |
1494 | goto fail; | |
1495 | } | |
7d35b929 | 1496 | le16_to_cpus(&hubstatus); |
430ee58e | 1497 | hcd = bus_to_hcd(hdev->bus); |
7d35b929 | 1498 | if (hdev == hdev->bus->root_hub) { |
430ee58e SAS |
1499 | if (hcd->power_budget > 0) |
1500 | hdev->bus_mA = hcd->power_budget; | |
1501 | else | |
1502 | hdev->bus_mA = full_load * hdev->maxchild; | |
1503 | if (hdev->bus_mA >= full_load) | |
1504 | hub->mA_per_port = full_load; | |
55c52718 AS |
1505 | else { |
1506 | hub->mA_per_port = hdev->bus_mA; | |
1507 | hub->limited_power = 1; | |
1508 | } | |
7d35b929 | 1509 | } else if ((hubstatus & (1 << USB_DEVICE_SELF_POWERED)) == 0) { |
430ee58e SAS |
1510 | int remaining = hdev->bus_mA - |
1511 | hub->descriptor->bHubContrCurrent; | |
1512 | ||
1da177e4 LT |
1513 | dev_dbg(hub_dev, "hub controller current requirement: %dmA\n", |
1514 | hub->descriptor->bHubContrCurrent); | |
55c52718 | 1515 | hub->limited_power = 1; |
55c52718 | 1516 | |
430ee58e SAS |
1517 | if (remaining < hdev->maxchild * unit_load) |
1518 | dev_warn(hub_dev, | |
55c52718 AS |
1519 | "insufficient power available " |
1520 | "to use all downstream ports\n"); | |
430ee58e SAS |
1521 | hub->mA_per_port = unit_load; /* 7.2.1 */ |
1522 | ||
55c52718 AS |
1523 | } else { /* Self-powered external hub */ |
1524 | /* FIXME: What about battery-powered external hubs that | |
1525 | * provide less current per port? */ | |
430ee58e | 1526 | hub->mA_per_port = full_load; |
7d35b929 | 1527 | } |
430ee58e | 1528 | if (hub->mA_per_port < full_load) |
55c52718 AS |
1529 | dev_dbg(hub_dev, "%umA bus power budget for each child\n", |
1530 | hub->mA_per_port); | |
1da177e4 | 1531 | |
b356b7c7 SS |
1532 | /* Update the HCD's internal representation of this hub before khubd |
1533 | * starts getting port status changes for devices under the hub. | |
1534 | */ | |
b356b7c7 SS |
1535 | if (hcd->driver->update_hub_device) { |
1536 | ret = hcd->driver->update_hub_device(hcd, hdev, | |
1537 | &hub->tt, GFP_KERNEL); | |
1538 | if (ret < 0) { | |
1539 | message = "can't update HCD hub info"; | |
1540 | goto fail; | |
1541 | } | |
1542 | } | |
1543 | ||
1da177e4 LT |
1544 | ret = hub_hub_status(hub, &hubstatus, &hubchange); |
1545 | if (ret < 0) { | |
1546 | message = "can't get hub status"; | |
1547 | goto fail; | |
1548 | } | |
1549 | ||
1550 | /* local power status reports aren't always correct */ | |
1551 | if (hdev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_SELFPOWER) | |
1552 | dev_dbg(hub_dev, "local power source is %s\n", | |
1553 | (hubstatus & HUB_STATUS_LOCAL_POWER) | |
1554 | ? "lost (inactive)" : "good"); | |
1555 | ||
74ad9bd2 | 1556 | if ((wHubCharacteristics & HUB_CHAR_OCPM) == 0) |
1da177e4 LT |
1557 | dev_dbg(hub_dev, "%sover-current condition exists\n", |
1558 | (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no "); | |
1559 | ||
88fafff9 | 1560 | /* set up the interrupt endpoint |
1561 | * We use the EP's maxpacket size instead of (PORTS+1+7)/8 | |
1562 | * bytes as USB2.0[11.12.3] says because some hubs are known | |
1563 | * to send more data (and thus cause overflow). For root hubs, | |
1564 | * maxpktsize is defined in hcd.c's fake endpoint descriptors | |
1565 | * to be big enough for at least USB_MAXCHILDREN ports. */ | |
1da177e4 LT |
1566 | pipe = usb_rcvintpipe(hdev, endpoint->bEndpointAddress); |
1567 | maxp = usb_maxpacket(hdev, pipe, usb_pipeout(pipe)); | |
1568 | ||
1569 | if (maxp > sizeof(*hub->buffer)) | |
1570 | maxp = sizeof(*hub->buffer); | |
1571 | ||
1572 | hub->urb = usb_alloc_urb(0, GFP_KERNEL); | |
1573 | if (!hub->urb) { | |
1da177e4 LT |
1574 | ret = -ENOMEM; |
1575 | goto fail; | |
1576 | } | |
1577 | ||
1578 | usb_fill_int_urb(hub->urb, hdev, pipe, *hub->buffer, maxp, hub_irq, | |
1579 | hub, endpoint->bInterval); | |
1da177e4 LT |
1580 | |
1581 | /* maybe cycle the hub leds */ | |
1582 | if (hub->has_indicators && blinkenlights) | |
1583 | hub->indicator [0] = INDICATOR_CYCLE; | |
1584 | ||
386f000a KM |
1585 | for (i = 0; i < hdev->maxchild; i++) { |
1586 | ret = usb_hub_create_port_device(hub, i + 1); | |
1587 | if (ret < 0) { | |
fa2a9566 LT |
1588 | dev_err(hub->intfdev, |
1589 | "couldn't create port%d device.\n", i + 1); | |
386f000a KM |
1590 | hdev->maxchild = i; |
1591 | goto fail_keep_maxchild; | |
1592 | } | |
1593 | } | |
fa2a9566 | 1594 | |
d2123fd9 LT |
1595 | usb_hub_adjust_deviceremovable(hdev, hub->descriptor); |
1596 | ||
f2835219 | 1597 | hub_activate(hub, HUB_INIT); |
1da177e4 LT |
1598 | return 0; |
1599 | ||
1600 | fail: | |
73426899 | 1601 | hdev->maxchild = 0; |
386f000a | 1602 | fail_keep_maxchild: |
1da177e4 LT |
1603 | dev_err (hub_dev, "config failed, %s (err %d)\n", |
1604 | message, ret); | |
1605 | /* hub_disconnect() frees urb and descriptor */ | |
1606 | return ret; | |
1607 | } | |
1608 | ||
e8054854 AS |
1609 | static void hub_release(struct kref *kref) |
1610 | { | |
1611 | struct usb_hub *hub = container_of(kref, struct usb_hub, kref); | |
1612 | ||
1613 | usb_put_intf(to_usb_interface(hub->intfdev)); | |
1614 | kfree(hub); | |
1615 | } | |
1616 | ||
1da177e4 LT |
1617 | static unsigned highspeed_hubs; |
1618 | ||
1619 | static void hub_disconnect(struct usb_interface *intf) | |
1620 | { | |
8816230e | 1621 | struct usb_hub *hub = usb_get_intfdata(intf); |
fa286188 | 1622 | struct usb_device *hdev = interface_to_usbdev(intf); |
356c1be6 | 1623 | int port1; |
fa2a9566 | 1624 | |
e8054854 AS |
1625 | /* Take the hub off the event list and don't let it be added again */ |
1626 | spin_lock_irq(&hub_event_lock); | |
8e4ceb38 AS |
1627 | if (!list_empty(&hub->event_list)) { |
1628 | list_del_init(&hub->event_list); | |
1629 | usb_autopm_put_interface_no_suspend(intf); | |
1630 | } | |
e8054854 AS |
1631 | hub->disconnected = 1; |
1632 | spin_unlock_irq(&hub_event_lock); | |
1da177e4 | 1633 | |
7de18d8b AS |
1634 | /* Disconnect all children and quiesce the hub */ |
1635 | hub->error = 0; | |
4330354f | 1636 | hub_quiesce(hub, HUB_DISCONNECT); |
7de18d8b | 1637 | |
356c1be6 AS |
1638 | /* Avoid races with recursively_mark_NOTATTACHED() */ |
1639 | spin_lock_irq(&device_state_lock); | |
1640 | port1 = hdev->maxchild; | |
1641 | hdev->maxchild = 0; | |
1642 | usb_set_intfdata(intf, NULL); | |
1643 | spin_unlock_irq(&device_state_lock); | |
1f2235b8 | 1644 | |
356c1be6 AS |
1645 | for (; port1 > 0; --port1) |
1646 | usb_hub_remove_port_device(hub, port1); | |
1da177e4 | 1647 | |
e8054854 | 1648 | if (hub->hdev->speed == USB_SPEED_HIGH) |
1da177e4 LT |
1649 | highspeed_hubs--; |
1650 | ||
1da177e4 | 1651 | usb_free_urb(hub->urb); |
fa2a9566 | 1652 | kfree(hub->ports); |
1bc3c9e1 | 1653 | kfree(hub->descriptor); |
1bc3c9e1 | 1654 | kfree(hub->status); |
d697cdda | 1655 | kfree(hub->buffer); |
1da177e4 | 1656 | |
971fcd49 | 1657 | pm_suspend_ignore_children(&intf->dev, false); |
e8054854 | 1658 | kref_put(&hub->kref, hub_release); |
1da177e4 LT |
1659 | } |
1660 | ||
1661 | static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id) | |
1662 | { | |
1663 | struct usb_host_interface *desc; | |
1664 | struct usb_endpoint_descriptor *endpoint; | |
1665 | struct usb_device *hdev; | |
1666 | struct usb_hub *hub; | |
1667 | ||
1668 | desc = intf->cur_altsetting; | |
1669 | hdev = interface_to_usbdev(intf); | |
1670 | ||
596d789a ML |
1671 | /* |
1672 | * Set default autosuspend delay as 0 to speedup bus suspend, | |
1673 | * based on the below considerations: | |
1674 | * | |
1675 | * - Unlike other drivers, the hub driver does not rely on the | |
1676 | * autosuspend delay to provide enough time to handle a wakeup | |
1677 | * event, and the submitted status URB is just to check future | |
1678 | * change on hub downstream ports, so it is safe to do it. | |
1679 | * | |
1680 | * - The patch might cause one or more auto supend/resume for | |
1681 | * below very rare devices when they are plugged into hub | |
1682 | * first time: | |
1683 | * | |
1684 | * devices having trouble initializing, and disconnect | |
1685 | * themselves from the bus and then reconnect a second | |
1686 | * or so later | |
1687 | * | |
1688 | * devices just for downloading firmware, and disconnects | |
1689 | * themselves after completing it | |
1690 | * | |
1691 | * For these quite rare devices, their drivers may change the | |
1692 | * autosuspend delay of their parent hub in the probe() to one | |
1693 | * appropriate value to avoid the subtle problem if someone | |
1694 | * does care it. | |
1695 | * | |
1696 | * - The patch may cause one or more auto suspend/resume on | |
1697 | * hub during running 'lsusb', but it is probably too | |
1698 | * infrequent to worry about. | |
1699 | * | |
1700 | * - Change autosuspend delay of hub can avoid unnecessary auto | |
1701 | * suspend timer for hub, also may decrease power consumption | |
1702 | * of USB bus. | |
6a5335b4 RQ |
1703 | * |
1704 | * - If user has indicated to prevent autosuspend by passing | |
1705 | * usbcore.autosuspend = -1 then keep autosuspend disabled. | |
596d789a | 1706 | */ |
3d81c473 | 1707 | #ifdef CONFIG_PM_RUNTIME |
6a5335b4 RQ |
1708 | if (hdev->dev.power.autosuspend_delay >= 0) |
1709 | pm_runtime_set_autosuspend_delay(&hdev->dev, 0); | |
3d81c473 | 1710 | #endif |
596d789a | 1711 | |
4cca9669 AS |
1712 | /* |
1713 | * Hubs have proper suspend/resume support, except for root hubs | |
1714 | * where the controller driver doesn't have bus_suspend and | |
1715 | * bus_resume methods. | |
1716 | */ | |
1717 | if (hdev->parent) { /* normal device */ | |
1718 | usb_enable_autosuspend(hdev); | |
1719 | } else { /* root hub */ | |
1720 | const struct hc_driver *drv = bus_to_hcd(hdev->bus)->driver; | |
1721 | ||
1722 | if (drv->bus_suspend && drv->bus_resume) | |
1723 | usb_enable_autosuspend(hdev); | |
1724 | } | |
088f7fec | 1725 | |
38f3ad5e | 1726 | if (hdev->level == MAX_TOPO_LEVEL) { |
b9cef6c3 WF |
1727 | dev_err(&intf->dev, |
1728 | "Unsupported bus topology: hub nested too deep\n"); | |
38f3ad5e FB |
1729 | return -E2BIG; |
1730 | } | |
1731 | ||
89ccbdc9 DB |
1732 | #ifdef CONFIG_USB_OTG_BLACKLIST_HUB |
1733 | if (hdev->parent) { | |
1734 | dev_warn(&intf->dev, "ignoring external hub\n"); | |
1735 | return -ENODEV; | |
1736 | } | |
1737 | #endif | |
1738 | ||
1da177e4 LT |
1739 | /* Some hubs have a subclass of 1, which AFAICT according to the */ |
1740 | /* specs is not defined, but it works */ | |
1741 | if ((desc->desc.bInterfaceSubClass != 0) && | |
1742 | (desc->desc.bInterfaceSubClass != 1)) { | |
1743 | descriptor_error: | |
1744 | dev_err (&intf->dev, "bad descriptor, ignoring hub\n"); | |
1745 | return -EIO; | |
1746 | } | |
1747 | ||
1748 | /* Multiple endpoints? What kind of mutant ninja-hub is this? */ | |
1749 | if (desc->desc.bNumEndpoints != 1) | |
1750 | goto descriptor_error; | |
1751 | ||
1752 | endpoint = &desc->endpoint[0].desc; | |
1753 | ||
fbf81c29 LFC |
1754 | /* If it's not an interrupt in endpoint, we'd better punt! */ |
1755 | if (!usb_endpoint_is_int_in(endpoint)) | |
1da177e4 LT |
1756 | goto descriptor_error; |
1757 | ||
1758 | /* We found a hub */ | |
1759 | dev_info (&intf->dev, "USB hub found\n"); | |
1760 | ||
0a1ef3b5 | 1761 | hub = kzalloc(sizeof(*hub), GFP_KERNEL); |
1da177e4 LT |
1762 | if (!hub) { |
1763 | dev_dbg (&intf->dev, "couldn't kmalloc hub struct\n"); | |
1764 | return -ENOMEM; | |
1765 | } | |
1766 | ||
e8054854 | 1767 | kref_init(&hub->kref); |
1da177e4 LT |
1768 | INIT_LIST_HEAD(&hub->event_list); |
1769 | hub->intfdev = &intf->dev; | |
1770 | hub->hdev = hdev; | |
c4028958 | 1771 | INIT_DELAYED_WORK(&hub->leds, led_work); |
8520f380 | 1772 | INIT_DELAYED_WORK(&hub->init_work, NULL); |
e8054854 | 1773 | usb_get_intf(intf); |
1da177e4 LT |
1774 | |
1775 | usb_set_intfdata (intf, hub); | |
40f122f3 | 1776 | intf->needs_remote_wakeup = 1; |
971fcd49 | 1777 | pm_suspend_ignore_children(&intf->dev, true); |
1da177e4 LT |
1778 | |
1779 | if (hdev->speed == USB_SPEED_HIGH) | |
1780 | highspeed_hubs++; | |
1781 | ||
e6f30dea ML |
1782 | if (id->driver_info & HUB_QUIRK_CHECK_PORT_AUTOSUSPEND) |
1783 | hub->quirk_check_port_auto_suspend = 1; | |
1784 | ||
1da177e4 LT |
1785 | if (hub_configure(hub, endpoint) >= 0) |
1786 | return 0; | |
1787 | ||
1788 | hub_disconnect (intf); | |
1789 | return -ENODEV; | |
1790 | } | |
1791 | ||
1792 | static int | |
1793 | hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data) | |
1794 | { | |
1795 | struct usb_device *hdev = interface_to_usbdev (intf); | |
ad493e5e | 1796 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
1da177e4 LT |
1797 | |
1798 | /* assert ifno == 0 (part of hub spec) */ | |
1799 | switch (code) { | |
1800 | case USBDEVFS_HUB_PORTINFO: { | |
1801 | struct usbdevfs_hub_portinfo *info = user_data; | |
1802 | int i; | |
1803 | ||
1804 | spin_lock_irq(&device_state_lock); | |
1805 | if (hdev->devnum <= 0) | |
1806 | info->nports = 0; | |
1807 | else { | |
1808 | info->nports = hdev->maxchild; | |
1809 | for (i = 0; i < info->nports; i++) { | |
ff823c79 | 1810 | if (hub->ports[i]->child == NULL) |
1da177e4 LT |
1811 | info->port[i] = 0; |
1812 | else | |
1813 | info->port[i] = | |
ff823c79 | 1814 | hub->ports[i]->child->devnum; |
1da177e4 LT |
1815 | } |
1816 | } | |
1817 | spin_unlock_irq(&device_state_lock); | |
1818 | ||
1819 | return info->nports + 1; | |
1820 | } | |
1821 | ||
1822 | default: | |
1823 | return -ENOSYS; | |
1824 | } | |
1825 | } | |
1826 | ||
7cbe5dca AS |
1827 | /* |
1828 | * Allow user programs to claim ports on a hub. When a device is attached | |
1829 | * to one of these "claimed" ports, the program will "own" the device. | |
1830 | */ | |
1831 | static int find_port_owner(struct usb_device *hdev, unsigned port1, | |
336c5c31 | 1832 | struct dev_state ***ppowner) |
7cbe5dca AS |
1833 | { |
1834 | if (hdev->state == USB_STATE_NOTATTACHED) | |
1835 | return -ENODEV; | |
1836 | if (port1 == 0 || port1 > hdev->maxchild) | |
1837 | return -EINVAL; | |
1838 | ||
1839 | /* This assumes that devices not managed by the hub driver | |
1840 | * will always have maxchild equal to 0. | |
1841 | */ | |
ad493e5e | 1842 | *ppowner = &(usb_hub_to_struct_hub(hdev)->ports[port1 - 1]->port_owner); |
7cbe5dca AS |
1843 | return 0; |
1844 | } | |
1845 | ||
1846 | /* In the following three functions, the caller must hold hdev's lock */ | |
336c5c31 LT |
1847 | int usb_hub_claim_port(struct usb_device *hdev, unsigned port1, |
1848 | struct dev_state *owner) | |
7cbe5dca AS |
1849 | { |
1850 | int rc; | |
336c5c31 | 1851 | struct dev_state **powner; |
7cbe5dca AS |
1852 | |
1853 | rc = find_port_owner(hdev, port1, &powner); | |
1854 | if (rc) | |
1855 | return rc; | |
1856 | if (*powner) | |
1857 | return -EBUSY; | |
1858 | *powner = owner; | |
1859 | return rc; | |
1860 | } | |
1861 | ||
336c5c31 LT |
1862 | int usb_hub_release_port(struct usb_device *hdev, unsigned port1, |
1863 | struct dev_state *owner) | |
7cbe5dca AS |
1864 | { |
1865 | int rc; | |
336c5c31 | 1866 | struct dev_state **powner; |
7cbe5dca AS |
1867 | |
1868 | rc = find_port_owner(hdev, port1, &powner); | |
1869 | if (rc) | |
1870 | return rc; | |
1871 | if (*powner != owner) | |
1872 | return -ENOENT; | |
1873 | *powner = NULL; | |
1874 | return rc; | |
1875 | } | |
1876 | ||
336c5c31 | 1877 | void usb_hub_release_all_ports(struct usb_device *hdev, struct dev_state *owner) |
7cbe5dca | 1878 | { |
ad493e5e | 1879 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
7cbe5dca | 1880 | int n; |
7cbe5dca | 1881 | |
fa2a9566 LT |
1882 | for (n = 0; n < hdev->maxchild; n++) { |
1883 | if (hub->ports[n]->port_owner == owner) | |
1884 | hub->ports[n]->port_owner = NULL; | |
7cbe5dca | 1885 | } |
fa2a9566 | 1886 | |
7cbe5dca AS |
1887 | } |
1888 | ||
1889 | /* The caller must hold udev's lock */ | |
1890 | bool usb_device_is_owned(struct usb_device *udev) | |
1891 | { | |
1892 | struct usb_hub *hub; | |
1893 | ||
1894 | if (udev->state == USB_STATE_NOTATTACHED || !udev->parent) | |
1895 | return false; | |
ad493e5e | 1896 | hub = usb_hub_to_struct_hub(udev->parent); |
fa2a9566 | 1897 | return !!hub->ports[udev->portnum - 1]->port_owner; |
7cbe5dca AS |
1898 | } |
1899 | ||
1da177e4 LT |
1900 | static void recursively_mark_NOTATTACHED(struct usb_device *udev) |
1901 | { | |
ad493e5e | 1902 | struct usb_hub *hub = usb_hub_to_struct_hub(udev); |
1da177e4 LT |
1903 | int i; |
1904 | ||
1905 | for (i = 0; i < udev->maxchild; ++i) { | |
ff823c79 LT |
1906 | if (hub->ports[i]->child) |
1907 | recursively_mark_NOTATTACHED(hub->ports[i]->child); | |
1da177e4 | 1908 | } |
9bbdf1e0 | 1909 | if (udev->state == USB_STATE_SUSPENDED) |
15123006 | 1910 | udev->active_duration -= jiffies; |
1da177e4 LT |
1911 | udev->state = USB_STATE_NOTATTACHED; |
1912 | } | |
1913 | ||
1914 | /** | |
1915 | * usb_set_device_state - change a device's current state (usbcore, hcds) | |
1916 | * @udev: pointer to device whose state should be changed | |
1917 | * @new_state: new state value to be stored | |
1918 | * | |
1919 | * udev->state is _not_ fully protected by the device lock. Although | |
1920 | * most transitions are made only while holding the lock, the state can | |
1921 | * can change to USB_STATE_NOTATTACHED at almost any time. This | |
1922 | * is so that devices can be marked as disconnected as soon as possible, | |
1923 | * without having to wait for any semaphores to be released. As a result, | |
1924 | * all changes to any device's state must be protected by the | |
1925 | * device_state_lock spinlock. | |
1926 | * | |
1927 | * Once a device has been added to the device tree, all changes to its state | |
1928 | * should be made using this routine. The state should _not_ be set directly. | |
1929 | * | |
1930 | * If udev->state is already USB_STATE_NOTATTACHED then no change is made. | |
1931 | * Otherwise udev->state is set to new_state, and if new_state is | |
1932 | * USB_STATE_NOTATTACHED then all of udev's descendants' states are also set | |
1933 | * to USB_STATE_NOTATTACHED. | |
1934 | */ | |
1935 | void usb_set_device_state(struct usb_device *udev, | |
1936 | enum usb_device_state new_state) | |
1937 | { | |
1938 | unsigned long flags; | |
4681b171 | 1939 | int wakeup = -1; |
1da177e4 LT |
1940 | |
1941 | spin_lock_irqsave(&device_state_lock, flags); | |
1942 | if (udev->state == USB_STATE_NOTATTACHED) | |
1943 | ; /* do nothing */ | |
b94dc6b5 | 1944 | else if (new_state != USB_STATE_NOTATTACHED) { |
fb669cc0 DB |
1945 | |
1946 | /* root hub wakeup capabilities are managed out-of-band | |
1947 | * and may involve silicon errata ... ignore them here. | |
1948 | */ | |
1949 | if (udev->parent) { | |
645daaab AS |
1950 | if (udev->state == USB_STATE_SUSPENDED |
1951 | || new_state == USB_STATE_SUSPENDED) | |
1952 | ; /* No change to wakeup settings */ | |
1953 | else if (new_state == USB_STATE_CONFIGURED) | |
4681b171 RW |
1954 | wakeup = udev->actconfig->desc.bmAttributes |
1955 | & USB_CONFIG_ATT_WAKEUP; | |
645daaab | 1956 | else |
4681b171 | 1957 | wakeup = 0; |
fb669cc0 | 1958 | } |
15123006 SS |
1959 | if (udev->state == USB_STATE_SUSPENDED && |
1960 | new_state != USB_STATE_SUSPENDED) | |
1961 | udev->active_duration -= jiffies; | |
1962 | else if (new_state == USB_STATE_SUSPENDED && | |
1963 | udev->state != USB_STATE_SUSPENDED) | |
1964 | udev->active_duration += jiffies; | |
645daaab | 1965 | udev->state = new_state; |
b94dc6b5 | 1966 | } else |
1da177e4 LT |
1967 | recursively_mark_NOTATTACHED(udev); |
1968 | spin_unlock_irqrestore(&device_state_lock, flags); | |
4681b171 RW |
1969 | if (wakeup >= 0) |
1970 | device_set_wakeup_capable(&udev->dev, wakeup); | |
1da177e4 | 1971 | } |
6da9c990 | 1972 | EXPORT_SYMBOL_GPL(usb_set_device_state); |
1da177e4 | 1973 | |
8af548dc | 1974 | /* |
3b29b68b AS |
1975 | * Choose a device number. |
1976 | * | |
1977 | * Device numbers are used as filenames in usbfs. On USB-1.1 and | |
1978 | * USB-2.0 buses they are also used as device addresses, however on | |
1979 | * USB-3.0 buses the address is assigned by the controller hardware | |
1980 | * and it usually is not the same as the device number. | |
1981 | * | |
8af548dc IPG |
1982 | * WUSB devices are simple: they have no hubs behind, so the mapping |
1983 | * device <-> virtual port number becomes 1:1. Why? to simplify the | |
1984 | * life of the device connection logic in | |
1985 | * drivers/usb/wusbcore/devconnect.c. When we do the initial secret | |
1986 | * handshake we need to assign a temporary address in the unauthorized | |
1987 | * space. For simplicity we use the first virtual port number found to | |
1988 | * be free [drivers/usb/wusbcore/devconnect.c:wusbhc_devconnect_ack()] | |
1989 | * and that becomes it's address [X < 128] or its unauthorized address | |
1990 | * [X | 0x80]. | |
1991 | * | |
1992 | * We add 1 as an offset to the one-based USB-stack port number | |
1993 | * (zero-based wusb virtual port index) for two reasons: (a) dev addr | |
1994 | * 0 is reserved by USB for default address; (b) Linux's USB stack | |
1995 | * uses always #1 for the root hub of the controller. So USB stack's | |
1996 | * port #1, which is wusb virtual-port #0 has address #2. | |
c6515272 SS |
1997 | * |
1998 | * Devices connected under xHCI are not as simple. The host controller | |
1999 | * supports virtualization, so the hardware assigns device addresses and | |
2000 | * the HCD must setup data structures before issuing a set address | |
2001 | * command to the hardware. | |
8af548dc | 2002 | */ |
3b29b68b | 2003 | static void choose_devnum(struct usb_device *udev) |
1da177e4 LT |
2004 | { |
2005 | int devnum; | |
2006 | struct usb_bus *bus = udev->bus; | |
2007 | ||
2008 | /* If khubd ever becomes multithreaded, this will need a lock */ | |
8af548dc IPG |
2009 | if (udev->wusb) { |
2010 | devnum = udev->portnum + 1; | |
2011 | BUG_ON(test_bit(devnum, bus->devmap.devicemap)); | |
2012 | } else { | |
2013 | /* Try to allocate the next devnum beginning at | |
2014 | * bus->devnum_next. */ | |
2015 | devnum = find_next_zero_bit(bus->devmap.devicemap, 128, | |
2016 | bus->devnum_next); | |
2017 | if (devnum >= 128) | |
2018 | devnum = find_next_zero_bit(bus->devmap.devicemap, | |
2019 | 128, 1); | |
2020 | bus->devnum_next = ( devnum >= 127 ? 1 : devnum + 1); | |
2021 | } | |
1da177e4 LT |
2022 | if (devnum < 128) { |
2023 | set_bit(devnum, bus->devmap.devicemap); | |
2024 | udev->devnum = devnum; | |
2025 | } | |
2026 | } | |
2027 | ||
3b29b68b | 2028 | static void release_devnum(struct usb_device *udev) |
1da177e4 LT |
2029 | { |
2030 | if (udev->devnum > 0) { | |
2031 | clear_bit(udev->devnum, udev->bus->devmap.devicemap); | |
2032 | udev->devnum = -1; | |
2033 | } | |
2034 | } | |
2035 | ||
3b29b68b | 2036 | static void update_devnum(struct usb_device *udev, int devnum) |
4953d141 DV |
2037 | { |
2038 | /* The address for a WUSB device is managed by wusbcore. */ | |
2039 | if (!udev->wusb) | |
2040 | udev->devnum = devnum; | |
2041 | } | |
2042 | ||
f7410ced HX |
2043 | static void hub_free_dev(struct usb_device *udev) |
2044 | { | |
2045 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
2046 | ||
2047 | /* Root hubs aren't real devices, so don't free HCD resources */ | |
2048 | if (hcd->driver->free_dev && udev->parent) | |
2049 | hcd->driver->free_dev(hcd, udev); | |
2050 | } | |
2051 | ||
1da177e4 LT |
2052 | /** |
2053 | * usb_disconnect - disconnect a device (usbcore-internal) | |
2054 | * @pdev: pointer to device being disconnected | |
2055 | * Context: !in_interrupt () | |
2056 | * | |
2057 | * Something got disconnected. Get rid of it and all of its children. | |
2058 | * | |
2059 | * If *pdev is a normal device then the parent hub must already be locked. | |
2060 | * If *pdev is a root hub then this routine will acquire the | |
2061 | * usb_bus_list_lock on behalf of the caller. | |
2062 | * | |
2063 | * Only hub drivers (including virtual root hub drivers for host | |
2064 | * controllers) should ever call this. | |
2065 | * | |
2066 | * This call is synchronous, and may not be used in an interrupt context. | |
2067 | */ | |
2068 | void usb_disconnect(struct usb_device **pdev) | |
2069 | { | |
2070 | struct usb_device *udev = *pdev; | |
ad493e5e | 2071 | struct usb_hub *hub = usb_hub_to_struct_hub(udev); |
1da177e4 LT |
2072 | int i; |
2073 | ||
1da177e4 LT |
2074 | /* mark the device as inactive, so any further urb submissions for |
2075 | * this device (and any of its children) will fail immediately. | |
25985edc | 2076 | * this quiesces everything except pending urbs. |
1da177e4 LT |
2077 | */ |
2078 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); | |
3b29b68b AS |
2079 | dev_info(&udev->dev, "USB disconnect, device number %d\n", |
2080 | udev->devnum); | |
1da177e4 | 2081 | |
9ad3d6cc AS |
2082 | usb_lock_device(udev); |
2083 | ||
1da177e4 | 2084 | /* Free up all the children before we remove this device */ |
8816230e | 2085 | for (i = 0; i < udev->maxchild; i++) { |
ff823c79 LT |
2086 | if (hub->ports[i]->child) |
2087 | usb_disconnect(&hub->ports[i]->child); | |
1da177e4 LT |
2088 | } |
2089 | ||
2090 | /* deallocate hcd/hardware state ... nuking all pending urbs and | |
2091 | * cleaning up all state associated with the current configuration | |
2092 | * so that the hardware is now fully quiesced. | |
2093 | */ | |
782da727 | 2094 | dev_dbg (&udev->dev, "unregistering device\n"); |
1da177e4 | 2095 | usb_disable_device(udev, 0); |
cde217a5 | 2096 | usb_hcd_synchronize_unlinks(udev); |
1da177e4 | 2097 | |
fde26380 | 2098 | if (udev->parent) { |
ad493e5e LT |
2099 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
2100 | struct usb_port *port_dev = hub->ports[udev->portnum - 1]; | |
fde26380 LT |
2101 | |
2102 | sysfs_remove_link(&udev->dev.kobj, "port"); | |
2103 | sysfs_remove_link(&port_dev->dev.kobj, "device"); | |
971fcd49 | 2104 | |
ad493e5e LT |
2105 | if (!port_dev->did_runtime_put) |
2106 | pm_runtime_put(&port_dev->dev); | |
2107 | else | |
2108 | port_dev->did_runtime_put = false; | |
fde26380 LT |
2109 | } |
2110 | ||
3b23dd6f | 2111 | usb_remove_ep_devs(&udev->ep0); |
782da727 AS |
2112 | usb_unlock_device(udev); |
2113 | ||
2114 | /* Unregister the device. The device driver is responsible | |
3b23dd6f AS |
2115 | * for de-configuring the device and invoking the remove-device |
2116 | * notifier chain (used by usbfs and possibly others). | |
782da727 AS |
2117 | */ |
2118 | device_del(&udev->dev); | |
3099e75a | 2119 | |
782da727 | 2120 | /* Free the device number and delete the parent's children[] |
1da177e4 LT |
2121 | * (or root_hub) pointer. |
2122 | */ | |
3b29b68b | 2123 | release_devnum(udev); |
1da177e4 LT |
2124 | |
2125 | /* Avoid races with recursively_mark_NOTATTACHED() */ | |
2126 | spin_lock_irq(&device_state_lock); | |
2127 | *pdev = NULL; | |
2128 | spin_unlock_irq(&device_state_lock); | |
2129 | ||
f7410ced HX |
2130 | hub_free_dev(udev); |
2131 | ||
782da727 | 2132 | put_device(&udev->dev); |
1da177e4 LT |
2133 | } |
2134 | ||
f2a383e4 | 2135 | #ifdef CONFIG_USB_ANNOUNCE_NEW_DEVICES |
1da177e4 LT |
2136 | static void show_string(struct usb_device *udev, char *id, char *string) |
2137 | { | |
2138 | if (!string) | |
2139 | return; | |
f2ec522e | 2140 | dev_info(&udev->dev, "%s: %s\n", id, string); |
1da177e4 LT |
2141 | } |
2142 | ||
f2a383e4 GKH |
2143 | static void announce_device(struct usb_device *udev) |
2144 | { | |
2145 | dev_info(&udev->dev, "New USB device found, idVendor=%04x, idProduct=%04x\n", | |
2146 | le16_to_cpu(udev->descriptor.idVendor), | |
2147 | le16_to_cpu(udev->descriptor.idProduct)); | |
b9cef6c3 WF |
2148 | dev_info(&udev->dev, |
2149 | "New USB device strings: Mfr=%d, Product=%d, SerialNumber=%d\n", | |
f2a383e4 GKH |
2150 | udev->descriptor.iManufacturer, |
2151 | udev->descriptor.iProduct, | |
2152 | udev->descriptor.iSerialNumber); | |
2153 | show_string(udev, "Product", udev->product); | |
2154 | show_string(udev, "Manufacturer", udev->manufacturer); | |
2155 | show_string(udev, "SerialNumber", udev->serial); | |
2156 | } | |
1da177e4 | 2157 | #else |
f2a383e4 | 2158 | static inline void announce_device(struct usb_device *udev) { } |
1da177e4 LT |
2159 | #endif |
2160 | ||
1da177e4 LT |
2161 | #ifdef CONFIG_USB_OTG |
2162 | #include "otg_whitelist.h" | |
2163 | #endif | |
2164 | ||
3ede760f | 2165 | /** |
8d8558d1 | 2166 | * usb_enumerate_device_otg - FIXME (usbcore-internal) |
3ede760f ON |
2167 | * @udev: newly addressed device (in ADDRESS state) |
2168 | * | |
8d8558d1 | 2169 | * Finish enumeration for On-The-Go devices |
3ede760f | 2170 | */ |
8d8558d1 | 2171 | static int usb_enumerate_device_otg(struct usb_device *udev) |
1da177e4 | 2172 | { |
d9d16e8a | 2173 | int err = 0; |
1da177e4 LT |
2174 | |
2175 | #ifdef CONFIG_USB_OTG | |
2176 | /* | |
2177 | * OTG-aware devices on OTG-capable root hubs may be able to use SRP, | |
2178 | * to wake us after we've powered off VBUS; and HNP, switching roles | |
2179 | * "host" to "peripheral". The OTG descriptor helps figure this out. | |
2180 | */ | |
2181 | if (!udev->bus->is_b_host | |
2182 | && udev->config | |
2183 | && udev->parent == udev->bus->root_hub) { | |
2eb5052e | 2184 | struct usb_otg_descriptor *desc = NULL; |
1da177e4 LT |
2185 | struct usb_bus *bus = udev->bus; |
2186 | ||
2187 | /* descriptor may appear anywhere in config */ | |
2188 | if (__usb_get_extra_descriptor (udev->rawdescriptors[0], | |
2189 | le16_to_cpu(udev->config[0].desc.wTotalLength), | |
2190 | USB_DT_OTG, (void **) &desc) == 0) { | |
2191 | if (desc->bmAttributes & USB_OTG_HNP) { | |
12c3da34 | 2192 | unsigned port1 = udev->portnum; |
fc849b85 | 2193 | |
1da177e4 LT |
2194 | dev_info(&udev->dev, |
2195 | "Dual-Role OTG device on %sHNP port\n", | |
2196 | (port1 == bus->otg_port) | |
2197 | ? "" : "non-"); | |
2198 | ||
2199 | /* enable HNP before suspend, it's simpler */ | |
2200 | if (port1 == bus->otg_port) | |
2201 | bus->b_hnp_enable = 1; | |
2202 | err = usb_control_msg(udev, | |
2203 | usb_sndctrlpipe(udev, 0), | |
2204 | USB_REQ_SET_FEATURE, 0, | |
2205 | bus->b_hnp_enable | |
2206 | ? USB_DEVICE_B_HNP_ENABLE | |
2207 | : USB_DEVICE_A_ALT_HNP_SUPPORT, | |
2208 | 0, NULL, 0, USB_CTRL_SET_TIMEOUT); | |
2209 | if (err < 0) { | |
2210 | /* OTG MESSAGE: report errors here, | |
2211 | * customize to match your product. | |
2212 | */ | |
2213 | dev_info(&udev->dev, | |
b9cef6c3 | 2214 | "can't set HNP mode: %d\n", |
1da177e4 LT |
2215 | err); |
2216 | bus->b_hnp_enable = 0; | |
2217 | } | |
2218 | } | |
2219 | } | |
2220 | } | |
2221 | ||
2222 | if (!is_targeted(udev)) { | |
2223 | ||
2224 | /* Maybe it can talk to us, though we can't talk to it. | |
2225 | * (Includes HNP test device.) | |
2226 | */ | |
2227 | if (udev->bus->b_hnp_enable || udev->bus->is_b_host) { | |
634a84f8 | 2228 | err = usb_port_suspend(udev, PMSG_SUSPEND); |
1da177e4 LT |
2229 | if (err < 0) |
2230 | dev_dbg(&udev->dev, "HNP fail, %d\n", err); | |
2231 | } | |
ffcdc18d | 2232 | err = -ENOTSUPP; |
1da177e4 LT |
2233 | goto fail; |
2234 | } | |
d9d16e8a | 2235 | fail: |
1da177e4 | 2236 | #endif |
d9d16e8a IPG |
2237 | return err; |
2238 | } | |
2239 | ||
2240 | ||
2241 | /** | |
8d8558d1 | 2242 | * usb_enumerate_device - Read device configs/intfs/otg (usbcore-internal) |
d9d16e8a IPG |
2243 | * @udev: newly addressed device (in ADDRESS state) |
2244 | * | |
2245 | * This is only called by usb_new_device() and usb_authorize_device() | |
2246 | * and FIXME -- all comments that apply to them apply here wrt to | |
2247 | * environment. | |
2248 | * | |
2249 | * If the device is WUSB and not authorized, we don't attempt to read | |
2250 | * the string descriptors, as they will be errored out by the device | |
2251 | * until it has been authorized. | |
2252 | */ | |
8d8558d1 | 2253 | static int usb_enumerate_device(struct usb_device *udev) |
d9d16e8a IPG |
2254 | { |
2255 | int err; | |
1da177e4 | 2256 | |
d9d16e8a IPG |
2257 | if (udev->config == NULL) { |
2258 | err = usb_get_configuration(udev); | |
2259 | if (err < 0) { | |
e9e88fb7 AS |
2260 | if (err != -ENODEV) |
2261 | dev_err(&udev->dev, "can't read configurations, error %d\n", | |
2262 | err); | |
80da2e0d | 2263 | return err; |
d9d16e8a IPG |
2264 | } |
2265 | } | |
6f7c6ef1 TP |
2266 | |
2267 | /* read the standard strings and cache them if present */ | |
2268 | udev->product = usb_cache_string(udev, udev->descriptor.iProduct); | |
2269 | udev->manufacturer = usb_cache_string(udev, | |
2270 | udev->descriptor.iManufacturer); | |
2271 | udev->serial = usb_cache_string(udev, udev->descriptor.iSerialNumber); | |
2272 | ||
8d8558d1 | 2273 | err = usb_enumerate_device_otg(udev); |
80da2e0d LP |
2274 | if (err < 0) |
2275 | return err; | |
2276 | ||
2277 | usb_detect_interface_quirks(udev); | |
2278 | ||
2279 | return 0; | |
d9d16e8a IPG |
2280 | } |
2281 | ||
d35e70d5 MG |
2282 | static void set_usb_port_removable(struct usb_device *udev) |
2283 | { | |
2284 | struct usb_device *hdev = udev->parent; | |
2285 | struct usb_hub *hub; | |
2286 | u8 port = udev->portnum; | |
2287 | u16 wHubCharacteristics; | |
2288 | bool removable = true; | |
2289 | ||
2290 | if (!hdev) | |
2291 | return; | |
2292 | ||
ad493e5e | 2293 | hub = usb_hub_to_struct_hub(udev->parent); |
d35e70d5 MG |
2294 | |
2295 | wHubCharacteristics = le16_to_cpu(hub->descriptor->wHubCharacteristics); | |
2296 | ||
2297 | if (!(wHubCharacteristics & HUB_CHAR_COMPOUND)) | |
2298 | return; | |
2299 | ||
2300 | if (hub_is_superspeed(hdev)) { | |
ca3c1539 LT |
2301 | if (le16_to_cpu(hub->descriptor->u.ss.DeviceRemovable) |
2302 | & (1 << port)) | |
d35e70d5 MG |
2303 | removable = false; |
2304 | } else { | |
2305 | if (hub->descriptor->u.hs.DeviceRemovable[port / 8] & (1 << (port % 8))) | |
2306 | removable = false; | |
2307 | } | |
2308 | ||
2309 | if (removable) | |
2310 | udev->removable = USB_DEVICE_REMOVABLE; | |
2311 | else | |
2312 | udev->removable = USB_DEVICE_FIXED; | |
2313 | } | |
d9d16e8a IPG |
2314 | |
2315 | /** | |
2316 | * usb_new_device - perform initial device setup (usbcore-internal) | |
2317 | * @udev: newly addressed device (in ADDRESS state) | |
2318 | * | |
8d8558d1 AS |
2319 | * This is called with devices which have been detected but not fully |
2320 | * enumerated. The device descriptor is available, but not descriptors | |
d9d16e8a IPG |
2321 | * for any device configuration. The caller must have locked either |
2322 | * the parent hub (if udev is a normal device) or else the | |
2323 | * usb_bus_list_lock (if udev is a root hub). The parent's pointer to | |
2324 | * udev has already been installed, but udev is not yet visible through | |
2325 | * sysfs or other filesystem code. | |
2326 | * | |
2327 | * It will return if the device is configured properly or not. Zero if | |
2328 | * the interface was registered with the driver core; else a negative | |
2329 | * errno value. | |
2330 | * | |
2331 | * This call is synchronous, and may not be used in an interrupt context. | |
2332 | * | |
2333 | * Only the hub driver or root-hub registrar should ever call this. | |
2334 | */ | |
2335 | int usb_new_device(struct usb_device *udev) | |
2336 | { | |
2337 | int err; | |
2338 | ||
16985408 | 2339 | if (udev->parent) { |
16985408 DS |
2340 | /* Initialize non-root-hub device wakeup to disabled; |
2341 | * device (un)configuration controls wakeup capable | |
2342 | * sysfs power/wakeup controls wakeup enabled/disabled | |
2343 | */ | |
2344 | device_init_wakeup(&udev->dev, 0); | |
16985408 DS |
2345 | } |
2346 | ||
9bbdf1e0 AS |
2347 | /* Tell the runtime-PM framework the device is active */ |
2348 | pm_runtime_set_active(&udev->dev); | |
c08512c7 | 2349 | pm_runtime_get_noresume(&udev->dev); |
fcc4a01e | 2350 | pm_runtime_use_autosuspend(&udev->dev); |
9bbdf1e0 AS |
2351 | pm_runtime_enable(&udev->dev); |
2352 | ||
c08512c7 AS |
2353 | /* By default, forbid autosuspend for all devices. It will be |
2354 | * allowed for hubs during binding. | |
2355 | */ | |
2356 | usb_disable_autosuspend(udev); | |
2357 | ||
8d8558d1 | 2358 | err = usb_enumerate_device(udev); /* Read descriptors */ |
d9d16e8a IPG |
2359 | if (err < 0) |
2360 | goto fail; | |
c6515272 SS |
2361 | dev_dbg(&udev->dev, "udev %d, busnum %d, minor = %d\n", |
2362 | udev->devnum, udev->bus->busnum, | |
2363 | (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); | |
9f8b17e6 KS |
2364 | /* export the usbdev device-node for libusb */ |
2365 | udev->dev.devt = MKDEV(USB_DEVICE_MAJOR, | |
2366 | (((udev->bus->busnum-1) * 128) + (udev->devnum-1))); | |
2367 | ||
6cd13201 AS |
2368 | /* Tell the world! */ |
2369 | announce_device(udev); | |
195af2cc | 2370 | |
b04b3156 TT |
2371 | if (udev->serial) |
2372 | add_device_randomness(udev->serial, strlen(udev->serial)); | |
2373 | if (udev->product) | |
2374 | add_device_randomness(udev->product, strlen(udev->product)); | |
2375 | if (udev->manufacturer) | |
2376 | add_device_randomness(udev->manufacturer, | |
2377 | strlen(udev->manufacturer)); | |
2378 | ||
927bc916 | 2379 | device_enable_async_suspend(&udev->dev); |
d35e70d5 MG |
2380 | |
2381 | /* | |
2382 | * check whether the hub marks this port as non-removable. Do it | |
2383 | * now so that platform-specific data can override it in | |
2384 | * device_add() | |
2385 | */ | |
2386 | if (udev->parent) | |
2387 | set_usb_port_removable(udev); | |
2388 | ||
782da727 | 2389 | /* Register the device. The device driver is responsible |
3b23dd6f AS |
2390 | * for configuring the device and invoking the add-device |
2391 | * notifier chain (used by usbfs and possibly others). | |
782da727 | 2392 | */ |
9f8b17e6 | 2393 | err = device_add(&udev->dev); |
1da177e4 LT |
2394 | if (err) { |
2395 | dev_err(&udev->dev, "can't device_add, error %d\n", err); | |
2396 | goto fail; | |
2397 | } | |
9ad3d6cc | 2398 | |
fde26380 LT |
2399 | /* Create link files between child device and usb port device. */ |
2400 | if (udev->parent) { | |
ad493e5e LT |
2401 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
2402 | struct usb_port *port_dev = hub->ports[udev->portnum - 1]; | |
fde26380 LT |
2403 | |
2404 | err = sysfs_create_link(&udev->dev.kobj, | |
2405 | &port_dev->dev.kobj, "port"); | |
2406 | if (err) | |
2407 | goto fail; | |
2408 | ||
2409 | err = sysfs_create_link(&port_dev->dev.kobj, | |
2410 | &udev->dev.kobj, "device"); | |
2411 | if (err) { | |
2412 | sysfs_remove_link(&udev->dev.kobj, "port"); | |
2413 | goto fail; | |
2414 | } | |
971fcd49 LT |
2415 | |
2416 | pm_runtime_get_sync(&port_dev->dev); | |
fde26380 LT |
2417 | } |
2418 | ||
3b23dd6f | 2419 | (void) usb_create_ep_devs(&udev->dev, &udev->ep0, udev); |
c08512c7 AS |
2420 | usb_mark_last_busy(udev); |
2421 | pm_runtime_put_sync_autosuspend(&udev->dev); | |
c066475e | 2422 | return err; |
1da177e4 LT |
2423 | |
2424 | fail: | |
2425 | usb_set_device_state(udev, USB_STATE_NOTATTACHED); | |
9bbdf1e0 AS |
2426 | pm_runtime_disable(&udev->dev); |
2427 | pm_runtime_set_suspended(&udev->dev); | |
d9d16e8a | 2428 | return err; |
1da177e4 LT |
2429 | } |
2430 | ||
93993a0a IPG |
2431 | |
2432 | /** | |
fd39c86b RD |
2433 | * usb_deauthorize_device - deauthorize a device (usbcore-internal) |
2434 | * @usb_dev: USB device | |
2435 | * | |
2436 | * Move the USB device to a very basic state where interfaces are disabled | |
2437 | * and the device is in fact unconfigured and unusable. | |
93993a0a IPG |
2438 | * |
2439 | * We share a lock (that we have) with device_del(), so we need to | |
2440 | * defer its call. | |
2441 | */ | |
2442 | int usb_deauthorize_device(struct usb_device *usb_dev) | |
2443 | { | |
93993a0a IPG |
2444 | usb_lock_device(usb_dev); |
2445 | if (usb_dev->authorized == 0) | |
2446 | goto out_unauthorized; | |
da307123 | 2447 | |
93993a0a IPG |
2448 | usb_dev->authorized = 0; |
2449 | usb_set_configuration(usb_dev, -1); | |
da307123 | 2450 | |
93993a0a IPG |
2451 | out_unauthorized: |
2452 | usb_unlock_device(usb_dev); | |
2453 | return 0; | |
2454 | } | |
2455 | ||
2456 | ||
2457 | int usb_authorize_device(struct usb_device *usb_dev) | |
2458 | { | |
2459 | int result = 0, c; | |
da307123 | 2460 | |
93993a0a IPG |
2461 | usb_lock_device(usb_dev); |
2462 | if (usb_dev->authorized == 1) | |
2463 | goto out_authorized; | |
da307123 | 2464 | |
93993a0a IPG |
2465 | result = usb_autoresume_device(usb_dev); |
2466 | if (result < 0) { | |
2467 | dev_err(&usb_dev->dev, | |
2468 | "can't autoresume for authorization: %d\n", result); | |
2469 | goto error_autoresume; | |
2470 | } | |
2471 | result = usb_get_device_descriptor(usb_dev, sizeof(usb_dev->descriptor)); | |
2472 | if (result < 0) { | |
2473 | dev_err(&usb_dev->dev, "can't re-read device descriptor for " | |
2474 | "authorization: %d\n", result); | |
2475 | goto error_device_descriptor; | |
2476 | } | |
da307123 | 2477 | |
93993a0a | 2478 | usb_dev->authorized = 1; |
93993a0a IPG |
2479 | /* Choose and set the configuration. This registers the interfaces |
2480 | * with the driver core and lets interface drivers bind to them. | |
2481 | */ | |
b5ea060f | 2482 | c = usb_choose_configuration(usb_dev); |
93993a0a IPG |
2483 | if (c >= 0) { |
2484 | result = usb_set_configuration(usb_dev, c); | |
2485 | if (result) { | |
2486 | dev_err(&usb_dev->dev, | |
2487 | "can't set config #%d, error %d\n", c, result); | |
2488 | /* This need not be fatal. The user can try to | |
2489 | * set other configurations. */ | |
2490 | } | |
2491 | } | |
2492 | dev_info(&usb_dev->dev, "authorized to connect\n"); | |
da307123 | 2493 | |
93993a0a | 2494 | error_device_descriptor: |
da307123 | 2495 | usb_autosuspend_device(usb_dev); |
93993a0a IPG |
2496 | error_autoresume: |
2497 | out_authorized: | |
2498 | usb_unlock_device(usb_dev); // complements locktree | |
2499 | return result; | |
2500 | } | |
2501 | ||
2502 | ||
0165de09 IPG |
2503 | /* Returns 1 if @hub is a WUSB root hub, 0 otherwise */ |
2504 | static unsigned hub_is_wusb(struct usb_hub *hub) | |
2505 | { | |
2506 | struct usb_hcd *hcd; | |
2507 | if (hub->hdev->parent != NULL) /* not a root hub? */ | |
2508 | return 0; | |
2509 | hcd = container_of(hub->hdev->bus, struct usb_hcd, self); | |
2510 | return hcd->wireless; | |
2511 | } | |
2512 | ||
2513 | ||
1da177e4 LT |
2514 | #define PORT_RESET_TRIES 5 |
2515 | #define SET_ADDRESS_TRIES 2 | |
2516 | #define GET_DESCRIPTOR_TRIES 2 | |
2517 | #define SET_CONFIG_TRIES (2 * (use_both_schemes + 1)) | |
90ab5ee9 | 2518 | #define USE_NEW_SCHEME(i) ((i) / 2 == (int)old_scheme_first) |
1da177e4 LT |
2519 | |
2520 | #define HUB_ROOT_RESET_TIME 50 /* times are in msec */ | |
2521 | #define HUB_SHORT_RESET_TIME 10 | |
75d7cf72 | 2522 | #define HUB_BH_RESET_TIME 50 |
1da177e4 | 2523 | #define HUB_LONG_RESET_TIME 200 |
77c7f072 | 2524 | #define HUB_RESET_TIMEOUT 800 |
1da177e4 | 2525 | |
10d674a8 SS |
2526 | static int hub_port_reset(struct usb_hub *hub, int port1, |
2527 | struct usb_device *udev, unsigned int delay, bool warm); | |
2528 | ||
8bea2bd3 SL |
2529 | /* Is a USB 3.0 port in the Inactive or Complinance Mode state? |
2530 | * Port worm reset is required to recover | |
2531 | */ | |
2532 | static bool hub_port_warm_reset_required(struct usb_hub *hub, u16 portstatus) | |
10d674a8 SS |
2533 | { |
2534 | return hub_is_superspeed(hub->hdev) && | |
8bea2bd3 SL |
2535 | (((portstatus & USB_PORT_STAT_LINK_STATE) == |
2536 | USB_SS_PORT_LS_SS_INACTIVE) || | |
2537 | ((portstatus & USB_PORT_STAT_LINK_STATE) == | |
2538 | USB_SS_PORT_LS_COMP_MOD)) ; | |
10d674a8 SS |
2539 | } |
2540 | ||
1da177e4 | 2541 | static int hub_port_wait_reset(struct usb_hub *hub, int port1, |
75d7cf72 | 2542 | struct usb_device *udev, unsigned int delay, bool warm) |
1da177e4 LT |
2543 | { |
2544 | int delay_time, ret; | |
2545 | u16 portstatus; | |
2546 | u16 portchange; | |
2547 | ||
2548 | for (delay_time = 0; | |
2549 | delay_time < HUB_RESET_TIMEOUT; | |
2550 | delay_time += delay) { | |
2551 | /* wait to give the device a chance to reset */ | |
2552 | msleep(delay); | |
2553 | ||
2554 | /* read and decode port status */ | |
2555 | ret = hub_port_status(hub, port1, &portstatus, &portchange); | |
2556 | if (ret < 0) | |
2557 | return ret; | |
2558 | ||
4f43447e | 2559 | /* The port state is unknown until the reset completes. */ |
470f0be8 SS |
2560 | if (!(portstatus & USB_PORT_STAT_RESET)) |
2561 | break; | |
1da177e4 LT |
2562 | |
2563 | /* switch to the long delay after two short delay failures */ | |
2564 | if (delay_time >= 2 * HUB_SHORT_RESET_TIME) | |
2565 | delay = HUB_LONG_RESET_TIME; | |
2566 | ||
2567 | dev_dbg (hub->intfdev, | |
75d7cf72 AX |
2568 | "port %d not %sreset yet, waiting %dms\n", |
2569 | port1, warm ? "warm " : "", delay); | |
1da177e4 LT |
2570 | } |
2571 | ||
470f0be8 SS |
2572 | if ((portstatus & USB_PORT_STAT_RESET)) |
2573 | return -EBUSY; | |
2574 | ||
2575 | if (hub_port_warm_reset_required(hub, portstatus)) | |
2576 | return -ENOTCONN; | |
2577 | ||
2578 | /* Device went away? */ | |
2579 | if (!(portstatus & USB_PORT_STAT_CONNECTION)) | |
2580 | return -ENOTCONN; | |
2581 | ||
2582 | /* bomb out completely if the connection bounced. A USB 3.0 | |
2583 | * connection may bounce if multiple warm resets were issued, | |
2584 | * but the device may have successfully re-connected. Ignore it. | |
2585 | */ | |
2586 | if (!hub_is_superspeed(hub->hdev) && | |
2587 | (portchange & USB_PORT_STAT_C_CONNECTION)) | |
2588 | return -ENOTCONN; | |
2589 | ||
2590 | if (!(portstatus & USB_PORT_STAT_ENABLE)) | |
2591 | return -EBUSY; | |
2592 | ||
2593 | if (!udev) | |
2594 | return 0; | |
2595 | ||
2596 | if (hub_is_wusb(hub)) | |
2597 | udev->speed = USB_SPEED_WIRELESS; | |
2598 | else if (hub_is_superspeed(hub->hdev)) | |
2599 | udev->speed = USB_SPEED_SUPER; | |
2600 | else if (portstatus & USB_PORT_STAT_HIGH_SPEED) | |
2601 | udev->speed = USB_SPEED_HIGH; | |
2602 | else if (portstatus & USB_PORT_STAT_LOW_SPEED) | |
2603 | udev->speed = USB_SPEED_LOW; | |
2604 | else | |
2605 | udev->speed = USB_SPEED_FULL; | |
2606 | return 0; | |
1da177e4 LT |
2607 | } |
2608 | ||
75d7cf72 | 2609 | static void hub_port_finish_reset(struct usb_hub *hub, int port1, |
a24a6078 | 2610 | struct usb_device *udev, int *status) |
75d7cf72 AX |
2611 | { |
2612 | switch (*status) { | |
2613 | case 0: | |
a24a6078 SS |
2614 | /* TRSTRCY = 10 ms; plus some extra */ |
2615 | msleep(10 + 40); | |
2616 | if (udev) { | |
2617 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
2618 | ||
2619 | update_devnum(udev, 0); | |
2620 | /* The xHC may think the device is already reset, | |
2621 | * so ignore the status. | |
2622 | */ | |
2623 | if (hcd->driver->reset_device) | |
2624 | hcd->driver->reset_device(hcd, udev); | |
75d7cf72 AX |
2625 | } |
2626 | /* FALL THROUGH */ | |
2627 | case -ENOTCONN: | |
2628 | case -ENODEV: | |
ad493e5e | 2629 | usb_clear_port_feature(hub->hdev, |
75d7cf72 | 2630 | port1, USB_PORT_FEAT_C_RESET); |
1c7439c6 | 2631 | if (hub_is_superspeed(hub->hdev)) { |
ad493e5e | 2632 | usb_clear_port_feature(hub->hdev, port1, |
75d7cf72 | 2633 | USB_PORT_FEAT_C_BH_PORT_RESET); |
ad493e5e | 2634 | usb_clear_port_feature(hub->hdev, port1, |
75d7cf72 | 2635 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
ad493e5e | 2636 | usb_clear_port_feature(hub->hdev, port1, |
a24a6078 | 2637 | USB_PORT_FEAT_C_CONNECTION); |
1c7439c6 | 2638 | } |
a24a6078 | 2639 | if (udev) |
75d7cf72 AX |
2640 | usb_set_device_state(udev, *status |
2641 | ? USB_STATE_NOTATTACHED | |
2642 | : USB_STATE_DEFAULT); | |
75d7cf72 AX |
2643 | break; |
2644 | } | |
2645 | } | |
2646 | ||
2647 | /* Handle port reset and port warm(BH) reset (for USB3 protocol ports) */ | |
1da177e4 | 2648 | static int hub_port_reset(struct usb_hub *hub, int port1, |
75d7cf72 | 2649 | struct usb_device *udev, unsigned int delay, bool warm) |
1da177e4 LT |
2650 | { |
2651 | int i, status; | |
a24a6078 | 2652 | u16 portchange, portstatus; |
1da177e4 | 2653 | |
0fe51aa5 SS |
2654 | if (!hub_is_superspeed(hub->hdev)) { |
2655 | if (warm) { | |
75d7cf72 AX |
2656 | dev_err(hub->intfdev, "only USB3 hub support " |
2657 | "warm reset\n"); | |
2658 | return -EINVAL; | |
2659 | } | |
0fe51aa5 SS |
2660 | /* Block EHCI CF initialization during the port reset. |
2661 | * Some companion controllers don't like it when they mix. | |
2662 | */ | |
2663 | down_read(&ehci_cf_port_reset_rwsem); | |
d3b9d7a9 SS |
2664 | } else if (!warm) { |
2665 | /* | |
2666 | * If the caller hasn't explicitly requested a warm reset, | |
2667 | * double check and see if one is needed. | |
2668 | */ | |
2669 | status = hub_port_status(hub, port1, | |
2670 | &portstatus, &portchange); | |
2671 | if (status < 0) | |
2672 | goto done; | |
2673 | ||
2674 | if (hub_port_warm_reset_required(hub, portstatus)) | |
2675 | warm = true; | |
75d7cf72 | 2676 | } |
32fe0198 | 2677 | |
1da177e4 LT |
2678 | /* Reset the port */ |
2679 | for (i = 0; i < PORT_RESET_TRIES; i++) { | |
75d7cf72 AX |
2680 | status = set_port_feature(hub->hdev, port1, (warm ? |
2681 | USB_PORT_FEAT_BH_PORT_RESET : | |
2682 | USB_PORT_FEAT_RESET)); | |
e9e88fb7 AS |
2683 | if (status == -ENODEV) { |
2684 | ; /* The hub is gone */ | |
2685 | } else if (status) { | |
1da177e4 | 2686 | dev_err(hub->intfdev, |
75d7cf72 AX |
2687 | "cannot %sreset port %d (err = %d)\n", |
2688 | warm ? "warm " : "", port1, status); | |
2689 | } else { | |
2690 | status = hub_port_wait_reset(hub, port1, udev, delay, | |
2691 | warm); | |
e9e88fb7 | 2692 | if (status && status != -ENOTCONN && status != -ENODEV) |
1da177e4 LT |
2693 | dev_dbg(hub->intfdev, |
2694 | "port_wait_reset: err = %d\n", | |
2695 | status); | |
2696 | } | |
2697 | ||
a24a6078 | 2698 | /* Check for disconnect or reset */ |
75d7cf72 | 2699 | if (status == 0 || status == -ENOTCONN || status == -ENODEV) { |
a24a6078 SS |
2700 | hub_port_finish_reset(hub, port1, udev, &status); |
2701 | ||
2702 | if (!hub_is_superspeed(hub->hdev)) | |
2703 | goto done; | |
2704 | ||
2705 | /* | |
2706 | * If a USB 3.0 device migrates from reset to an error | |
2707 | * state, re-issue the warm reset. | |
2708 | */ | |
2709 | if (hub_port_status(hub, port1, | |
2710 | &portstatus, &portchange) < 0) | |
2711 | goto done; | |
2712 | ||
2713 | if (!hub_port_warm_reset_required(hub, portstatus)) | |
2714 | goto done; | |
2715 | ||
2716 | /* | |
2717 | * If the port is in SS.Inactive or Compliance Mode, the | |
2718 | * hot or warm reset failed. Try another warm reset. | |
2719 | */ | |
2720 | if (!warm) { | |
2721 | dev_dbg(hub->intfdev, "hot reset failed, warm reset port %d\n", | |
2722 | port1); | |
2723 | warm = true; | |
2724 | } | |
1da177e4 LT |
2725 | } |
2726 | ||
2727 | dev_dbg (hub->intfdev, | |
75d7cf72 AX |
2728 | "port %d not enabled, trying %sreset again...\n", |
2729 | port1, warm ? "warm " : ""); | |
1da177e4 LT |
2730 | delay = HUB_LONG_RESET_TIME; |
2731 | } | |
2732 | ||
2733 | dev_err (hub->intfdev, | |
2734 | "Cannot enable port %i. Maybe the USB cable is bad?\n", | |
2735 | port1); | |
2736 | ||
75d7cf72 | 2737 | done: |
0fe51aa5 | 2738 | if (!hub_is_superspeed(hub->hdev)) |
75d7cf72 | 2739 | up_read(&ehci_cf_port_reset_rwsem); |
5e467f6e | 2740 | |
75d7cf72 | 2741 | return status; |
5e467f6e AX |
2742 | } |
2743 | ||
0ed9a57e AX |
2744 | /* Check if a port is power on */ |
2745 | static int port_is_power_on(struct usb_hub *hub, unsigned portstatus) | |
2746 | { | |
2747 | int ret = 0; | |
2748 | ||
2749 | if (hub_is_superspeed(hub->hdev)) { | |
2750 | if (portstatus & USB_SS_PORT_STAT_POWER) | |
2751 | ret = 1; | |
2752 | } else { | |
2753 | if (portstatus & USB_PORT_STAT_POWER) | |
2754 | ret = 1; | |
2755 | } | |
2756 | ||
2757 | return ret; | |
2758 | } | |
2759 | ||
d388dab7 | 2760 | #ifdef CONFIG_PM |
1da177e4 | 2761 | |
0ed9a57e AX |
2762 | /* Check if a port is suspended(USB2.0 port) or in U3 state(USB3.0 port) */ |
2763 | static int port_is_suspended(struct usb_hub *hub, unsigned portstatus) | |
2764 | { | |
2765 | int ret = 0; | |
2766 | ||
2767 | if (hub_is_superspeed(hub->hdev)) { | |
2768 | if ((portstatus & USB_PORT_STAT_LINK_STATE) | |
2769 | == USB_SS_PORT_LS_U3) | |
2770 | ret = 1; | |
2771 | } else { | |
2772 | if (portstatus & USB_PORT_STAT_SUSPEND) | |
2773 | ret = 1; | |
2774 | } | |
2775 | ||
2776 | return ret; | |
2777 | } | |
b01b03f3 AS |
2778 | |
2779 | /* Determine whether the device on a port is ready for a normal resume, | |
2780 | * is ready for a reset-resume, or should be disconnected. | |
2781 | */ | |
2782 | static int check_port_resume_type(struct usb_device *udev, | |
2783 | struct usb_hub *hub, int port1, | |
2784 | int status, unsigned portchange, unsigned portstatus) | |
2785 | { | |
2786 | /* Is the device still present? */ | |
0ed9a57e AX |
2787 | if (status || port_is_suspended(hub, portstatus) || |
2788 | !port_is_power_on(hub, portstatus) || | |
2789 | !(portstatus & USB_PORT_STAT_CONNECTION)) { | |
b01b03f3 AS |
2790 | if (status >= 0) |
2791 | status = -ENODEV; | |
2792 | } | |
2793 | ||
86c57edf AS |
2794 | /* Can't do a normal resume if the port isn't enabled, |
2795 | * so try a reset-resume instead. | |
2796 | */ | |
2797 | else if (!(portstatus & USB_PORT_STAT_ENABLE) && !udev->reset_resume) { | |
2798 | if (udev->persist_enabled) | |
2799 | udev->reset_resume = 1; | |
2800 | else | |
2801 | status = -ENODEV; | |
2802 | } | |
b01b03f3 AS |
2803 | |
2804 | if (status) { | |
2805 | dev_dbg(hub->intfdev, | |
2806 | "port %d status %04x.%04x after resume, %d\n", | |
2807 | port1, portchange, portstatus, status); | |
2808 | } else if (udev->reset_resume) { | |
2809 | ||
2810 | /* Late port handoff can set status-change bits */ | |
2811 | if (portchange & USB_PORT_STAT_C_CONNECTION) | |
ad493e5e | 2812 | usb_clear_port_feature(hub->hdev, port1, |
b01b03f3 AS |
2813 | USB_PORT_FEAT_C_CONNECTION); |
2814 | if (portchange & USB_PORT_STAT_C_ENABLE) | |
ad493e5e | 2815 | usb_clear_port_feature(hub->hdev, port1, |
b01b03f3 AS |
2816 | USB_PORT_FEAT_C_ENABLE); |
2817 | } | |
2818 | ||
2819 | return status; | |
2820 | } | |
2821 | ||
f74631e3 SS |
2822 | int usb_disable_ltm(struct usb_device *udev) |
2823 | { | |
2824 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
2825 | ||
2826 | /* Check if the roothub and device supports LTM. */ | |
2827 | if (!usb_device_supports_ltm(hcd->self.root_hub) || | |
2828 | !usb_device_supports_ltm(udev)) | |
2829 | return 0; | |
2830 | ||
2831 | /* Clear Feature LTM Enable can only be sent if the device is | |
2832 | * configured. | |
2833 | */ | |
2834 | if (!udev->actconfig) | |
2835 | return 0; | |
2836 | ||
2837 | return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
2838 | USB_REQ_CLEAR_FEATURE, USB_RECIP_DEVICE, | |
2839 | USB_DEVICE_LTM_ENABLE, 0, NULL, 0, | |
2840 | USB_CTRL_SET_TIMEOUT); | |
2841 | } | |
2842 | EXPORT_SYMBOL_GPL(usb_disable_ltm); | |
2843 | ||
2844 | void usb_enable_ltm(struct usb_device *udev) | |
2845 | { | |
2846 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
2847 | ||
2848 | /* Check if the roothub and device supports LTM. */ | |
2849 | if (!usb_device_supports_ltm(hcd->self.root_hub) || | |
2850 | !usb_device_supports_ltm(udev)) | |
2851 | return; | |
2852 | ||
2853 | /* Set Feature LTM Enable can only be sent if the device is | |
2854 | * configured. | |
2855 | */ | |
2856 | if (!udev->actconfig) | |
2857 | return; | |
2858 | ||
2859 | usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
2860 | USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, | |
2861 | USB_DEVICE_LTM_ENABLE, 0, NULL, 0, | |
2862 | USB_CTRL_SET_TIMEOUT); | |
2863 | } | |
2864 | EXPORT_SYMBOL_GPL(usb_enable_ltm); | |
2865 | ||
84ebc102 | 2866 | #ifdef CONFIG_PM |
54a3ac0c LT |
2867 | /* |
2868 | * usb_disable_function_remotewakeup - disable usb3.0 | |
2869 | * device's function remote wakeup | |
2870 | * @udev: target device | |
2871 | * | |
2872 | * Assume there's only one function on the USB 3.0 | |
2873 | * device and disable remote wake for the first | |
2874 | * interface. FIXME if the interface association | |
2875 | * descriptor shows there's more than one function. | |
2876 | */ | |
2877 | static int usb_disable_function_remotewakeup(struct usb_device *udev) | |
2878 | { | |
2879 | return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
2880 | USB_REQ_CLEAR_FEATURE, USB_RECIP_INTERFACE, | |
2881 | USB_INTRF_FUNC_SUSPEND, 0, NULL, 0, | |
2882 | USB_CTRL_SET_TIMEOUT); | |
2883 | } | |
1da177e4 | 2884 | |
5deba4cf AS |
2885 | /* Count of wakeup-enabled devices at or below udev */ |
2886 | static unsigned wakeup_enabled_descendants(struct usb_device *udev) | |
2887 | { | |
2888 | struct usb_hub *hub = usb_hub_to_struct_hub(udev); | |
2889 | ||
2890 | return udev->do_remote_wakeup + | |
2891 | (hub ? hub->wakeup_enabled_descendants : 0); | |
2892 | } | |
2893 | ||
1da177e4 | 2894 | /* |
624d6c07 AS |
2895 | * usb_port_suspend - suspend a usb device's upstream port |
2896 | * @udev: device that's no longer in active use, not a root hub | |
2897 | * Context: must be able to sleep; device not locked; pm locks held | |
2898 | * | |
2899 | * Suspends a USB device that isn't in active use, conserving power. | |
2900 | * Devices may wake out of a suspend, if anything important happens, | |
2901 | * using the remote wakeup mechanism. They may also be taken out of | |
2902 | * suspend by the host, using usb_port_resume(). It's also routine | |
2903 | * to disconnect devices while they are suspended. | |
2904 | * | |
2905 | * This only affects the USB hardware for a device; its interfaces | |
2906 | * (and, for hubs, child devices) must already have been suspended. | |
2907 | * | |
1da177e4 LT |
2908 | * Selective port suspend reduces power; most suspended devices draw |
2909 | * less than 500 uA. It's also used in OTG, along with remote wakeup. | |
2910 | * All devices below the suspended port are also suspended. | |
2911 | * | |
2912 | * Devices leave suspend state when the host wakes them up. Some devices | |
2913 | * also support "remote wakeup", where the device can activate the USB | |
2914 | * tree above them to deliver data, such as a keypress or packet. In | |
2915 | * some cases, this wakes the USB host. | |
624d6c07 AS |
2916 | * |
2917 | * Suspending OTG devices may trigger HNP, if that's been enabled | |
2918 | * between a pair of dual-role devices. That will change roles, such | |
2919 | * as from A-Host to A-Peripheral or from B-Host back to B-Peripheral. | |
2920 | * | |
2921 | * Devices on USB hub ports have only one "suspend" state, corresponding | |
2922 | * to ACPI D2, "may cause the device to lose some context". | |
2923 | * State transitions include: | |
2924 | * | |
2925 | * - suspend, resume ... when the VBUS power link stays live | |
2926 | * - suspend, disconnect ... VBUS lost | |
2927 | * | |
2928 | * Once VBUS drop breaks the circuit, the port it's using has to go through | |
2929 | * normal re-enumeration procedures, starting with enabling VBUS power. | |
2930 | * Other than re-initializing the hub (plug/unplug, except for root hubs), | |
2931 | * Linux (2.6) currently has NO mechanisms to initiate that: no khubd | |
2932 | * timer, no SRP, no requests through sysfs. | |
2933 | * | |
5deba4cf AS |
2934 | * If Runtime PM isn't enabled or used, non-SuperSpeed devices may not get |
2935 | * suspended until their bus goes into global suspend (i.e., the root | |
0aa2832d AS |
2936 | * hub is suspended). Nevertheless, we change @udev->state to |
2937 | * USB_STATE_SUSPENDED as this is the device's "logical" state. The actual | |
2938 | * upstream port setting is stored in @udev->port_is_suspended. | |
624d6c07 AS |
2939 | * |
2940 | * Returns 0 on success, else negative errno. | |
1da177e4 | 2941 | */ |
65bfd296 | 2942 | int usb_port_suspend(struct usb_device *udev, pm_message_t msg) |
1da177e4 | 2943 | { |
ad493e5e LT |
2944 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
2945 | struct usb_port *port_dev = hub->ports[udev->portnum - 1]; | |
624d6c07 AS |
2946 | int port1 = udev->portnum; |
2947 | int status; | |
0aa2832d | 2948 | bool really_suspend = true; |
1da177e4 | 2949 | |
1da177e4 LT |
2950 | /* enable remote wakeup when appropriate; this lets the device |
2951 | * wake up the upstream hub (including maybe the root hub). | |
2952 | * | |
2953 | * NOTE: OTG devices may issue remote wakeup (or SRP) even when | |
2954 | * we don't explicitly enable it here. | |
2955 | */ | |
645daaab | 2956 | if (udev->do_remote_wakeup) { |
623bef9e SS |
2957 | if (!hub_is_superspeed(hub->hdev)) { |
2958 | status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
2959 | USB_REQ_SET_FEATURE, USB_RECIP_DEVICE, | |
2960 | USB_DEVICE_REMOTE_WAKEUP, 0, | |
2961 | NULL, 0, | |
2962 | USB_CTRL_SET_TIMEOUT); | |
2963 | } else { | |
2964 | /* Assume there's only one function on the USB 3.0 | |
2965 | * device and enable remote wake for the first | |
2966 | * interface. FIXME if the interface association | |
2967 | * descriptor shows there's more than one function. | |
2968 | */ | |
2969 | status = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
2970 | USB_REQ_SET_FEATURE, | |
2971 | USB_RECIP_INTERFACE, | |
2972 | USB_INTRF_FUNC_SUSPEND, | |
3b9b6acd SS |
2973 | USB_INTRF_FUNC_SUSPEND_RW | |
2974 | USB_INTRF_FUNC_SUSPEND_LP, | |
623bef9e SS |
2975 | NULL, 0, |
2976 | USB_CTRL_SET_TIMEOUT); | |
2977 | } | |
0c487206 | 2978 | if (status) { |
624d6c07 AS |
2979 | dev_dbg(&udev->dev, "won't remote wakeup, status %d\n", |
2980 | status); | |
0c487206 | 2981 | /* bail if autosuspend is requested */ |
5b1b0b81 | 2982 | if (PMSG_IS_AUTO(msg)) |
b504b4a1 | 2983 | goto err_wakeup; |
0c487206 | 2984 | } |
1da177e4 LT |
2985 | } |
2986 | ||
65580b43 AX |
2987 | /* disable USB2 hardware LPM */ |
2988 | if (udev->usb2_hw_lpm_enabled == 1) | |
2989 | usb_set_usb2_hardware_lpm(udev, 0); | |
2990 | ||
f74631e3 | 2991 | if (usb_disable_ltm(udev)) { |
b504b4a1 AS |
2992 | dev_err(&udev->dev, "Failed to disable LTM before suspend\n."); |
2993 | status = -ENOMEM; | |
2994 | if (PMSG_IS_AUTO(msg)) | |
2995 | goto err_ltm; | |
f74631e3 | 2996 | } |
8306095f | 2997 | if (usb_unlocked_disable_lpm(udev)) { |
b504b4a1 AS |
2998 | dev_err(&udev->dev, "Failed to disable LPM before suspend\n."); |
2999 | status = -ENOMEM; | |
3000 | if (PMSG_IS_AUTO(msg)) | |
3001 | goto err_lpm3; | |
8306095f SS |
3002 | } |
3003 | ||
1da177e4 | 3004 | /* see 7.1.7.6 */ |
a7114230 | 3005 | if (hub_is_superspeed(hub->hdev)) |
c2f60db7 | 3006 | status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U3); |
5deba4cf | 3007 | |
0aa2832d AS |
3008 | /* |
3009 | * For system suspend, we do not need to enable the suspend feature | |
3010 | * on individual USB-2 ports. The devices will automatically go | |
3011 | * into suspend a few ms after the root hub stops sending packets. | |
3012 | * The USB 2.0 spec calls this "global suspend". | |
5deba4cf AS |
3013 | * |
3014 | * However, many USB hubs have a bug: They don't relay wakeup requests | |
3015 | * from a downstream port if the port's suspend feature isn't on. | |
3016 | * Therefore we will turn on the suspend feature if udev or any of its | |
3017 | * descendants is enabled for remote wakeup. | |
0aa2832d | 3018 | */ |
5deba4cf AS |
3019 | else if (PMSG_IS_AUTO(msg) || wakeup_enabled_descendants(udev) > 0) |
3020 | status = set_port_feature(hub->hdev, port1, | |
3021 | USB_PORT_FEAT_SUSPEND); | |
0aa2832d AS |
3022 | else { |
3023 | really_suspend = false; | |
3024 | status = 0; | |
3025 | } | |
1da177e4 | 3026 | if (status) { |
624d6c07 AS |
3027 | dev_dbg(hub->intfdev, "can't suspend port %d, status %d\n", |
3028 | port1, status); | |
cbb33004 | 3029 | |
b504b4a1 AS |
3030 | /* Try to enable USB3 LPM and LTM again */ |
3031 | usb_unlocked_enable_lpm(udev); | |
3032 | err_lpm3: | |
3033 | usb_enable_ltm(udev); | |
3034 | err_ltm: | |
c3e751e4 AX |
3035 | /* Try to enable USB2 hardware LPM again */ |
3036 | if (udev->usb2_hw_lpm_capable == 1) | |
3037 | usb_set_usb2_hardware_lpm(udev, 1); | |
3038 | ||
b504b4a1 AS |
3039 | if (udev->do_remote_wakeup) { |
3040 | if (udev->speed < USB_SPEED_SUPER) | |
3041 | usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
3042 | USB_REQ_CLEAR_FEATURE, | |
3043 | USB_RECIP_DEVICE, | |
3044 | USB_DEVICE_REMOTE_WAKEUP, 0, | |
3045 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
3046 | else | |
3047 | usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
3048 | USB_REQ_CLEAR_FEATURE, | |
3049 | USB_RECIP_INTERFACE, | |
3050 | USB_INTRF_FUNC_SUSPEND, 0, | |
3051 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
3052 | } | |
3053 | err_wakeup: | |
8306095f | 3054 | |
cbb33004 | 3055 | /* System sleep transitions should never fail */ |
5b1b0b81 | 3056 | if (!PMSG_IS_AUTO(msg)) |
cbb33004 | 3057 | status = 0; |
1da177e4 | 3058 | } else { |
30b1a7a3 AS |
3059 | dev_dbg(&udev->dev, "usb %ssuspend, wakeup %d\n", |
3060 | (PMSG_IS_AUTO(msg) ? "auto-" : ""), | |
3061 | udev->do_remote_wakeup); | |
0aa2832d AS |
3062 | if (really_suspend) { |
3063 | udev->port_is_suspended = 1; | |
5deba4cf AS |
3064 | |
3065 | /* device has up to 10 msec to fully suspend */ | |
0aa2832d AS |
3066 | msleep(10); |
3067 | } | |
5deba4cf | 3068 | usb_set_device_state(udev, USB_STATE_SUSPENDED); |
1da177e4 | 3069 | } |
ad493e5e | 3070 | |
b504b4a1 | 3071 | if (status == 0 && !udev->do_remote_wakeup && udev->persist_enabled) { |
e0835055 LT |
3072 | pm_runtime_put_sync(&port_dev->dev); |
3073 | port_dev->did_runtime_put = true; | |
ad493e5e LT |
3074 | } |
3075 | ||
c08512c7 | 3076 | usb_mark_last_busy(hub->hdev); |
1da177e4 LT |
3077 | return status; |
3078 | } | |
3079 | ||
1da177e4 | 3080 | /* |
390a8c34 DB |
3081 | * If the USB "suspend" state is in use (rather than "global suspend"), |
3082 | * many devices will be individually taken out of suspend state using | |
54515fe5 | 3083 | * special "resume" signaling. This routine kicks in shortly after |
1da177e4 LT |
3084 | * hardware resume signaling is finished, either because of selective |
3085 | * resume (by host) or remote wakeup (by device) ... now see what changed | |
3086 | * in the tree that's rooted at this device. | |
54515fe5 AS |
3087 | * |
3088 | * If @udev->reset_resume is set then the device is reset before the | |
3089 | * status check is done. | |
1da177e4 | 3090 | */ |
140d8f68 | 3091 | static int finish_port_resume(struct usb_device *udev) |
1da177e4 | 3092 | { |
54515fe5 | 3093 | int status = 0; |
07e72b95 | 3094 | u16 devstatus = 0; |
1da177e4 LT |
3095 | |
3096 | /* caller owns the udev device lock */ | |
b9cef6c3 WF |
3097 | dev_dbg(&udev->dev, "%s\n", |
3098 | udev->reset_resume ? "finish reset-resume" : "finish resume"); | |
1da177e4 LT |
3099 | |
3100 | /* usb ch9 identifies four variants of SUSPENDED, based on what | |
3101 | * state the device resumes to. Linux currently won't see the | |
3102 | * first two on the host side; they'd be inside hub_port_init() | |
3103 | * during many timeouts, but khubd can't suspend until later. | |
3104 | */ | |
3105 | usb_set_device_state(udev, udev->actconfig | |
3106 | ? USB_STATE_CONFIGURED | |
3107 | : USB_STATE_ADDRESS); | |
1da177e4 | 3108 | |
54515fe5 AS |
3109 | /* 10.5.4.5 says not to reset a suspended port if the attached |
3110 | * device is enabled for remote wakeup. Hence the reset | |
3111 | * operation is carried out here, after the port has been | |
3112 | * resumed. | |
3113 | */ | |
3114 | if (udev->reset_resume) | |
86c57edf | 3115 | retry_reset_resume: |
742120c6 | 3116 | status = usb_reset_and_verify_device(udev); |
54515fe5 | 3117 | |
1da177e4 LT |
3118 | /* 10.5.4.5 says be sure devices in the tree are still there. |
3119 | * For now let's assume the device didn't go crazy on resume, | |
3120 | * and device drivers will know about any resume quirks. | |
3121 | */ | |
54515fe5 | 3122 | if (status == 0) { |
46dede46 | 3123 | devstatus = 0; |
54515fe5 AS |
3124 | status = usb_get_status(udev, USB_RECIP_DEVICE, 0, &devstatus); |
3125 | if (status >= 0) | |
46dede46 | 3126 | status = (status > 0 ? 0 : -ENODEV); |
86c57edf AS |
3127 | |
3128 | /* If a normal resume failed, try doing a reset-resume */ | |
3129 | if (status && !udev->reset_resume && udev->persist_enabled) { | |
3130 | dev_dbg(&udev->dev, "retry with reset-resume\n"); | |
3131 | udev->reset_resume = 1; | |
3132 | goto retry_reset_resume; | |
3133 | } | |
54515fe5 | 3134 | } |
b40b7a90 | 3135 | |
624d6c07 AS |
3136 | if (status) { |
3137 | dev_dbg(&udev->dev, "gone after usb resume? status %d\n", | |
3138 | status); | |
07e72b95 ON |
3139 | /* |
3140 | * There are a few quirky devices which violate the standard | |
3141 | * by claiming to have remote wakeup enabled after a reset, | |
3142 | * which crash if the feature is cleared, hence check for | |
3143 | * udev->reset_resume | |
3144 | */ | |
3145 | } else if (udev->actconfig && !udev->reset_resume) { | |
54a3ac0c LT |
3146 | if (!hub_is_superspeed(udev->parent)) { |
3147 | le16_to_cpus(&devstatus); | |
3148 | if (devstatus & (1 << USB_DEVICE_REMOTE_WAKEUP)) | |
3149 | status = usb_control_msg(udev, | |
3150 | usb_sndctrlpipe(udev, 0), | |
3151 | USB_REQ_CLEAR_FEATURE, | |
1da177e4 | 3152 | USB_RECIP_DEVICE, |
54a3ac0c LT |
3153 | USB_DEVICE_REMOTE_WAKEUP, 0, |
3154 | NULL, 0, | |
3155 | USB_CTRL_SET_TIMEOUT); | |
3156 | } else { | |
3157 | status = usb_get_status(udev, USB_RECIP_INTERFACE, 0, | |
3158 | &devstatus); | |
3159 | le16_to_cpus(&devstatus); | |
3160 | if (!status && devstatus & (USB_INTRF_STAT_FUNC_RW_CAP | |
3161 | | USB_INTRF_STAT_FUNC_RW)) | |
3162 | status = | |
3163 | usb_disable_function_remotewakeup(udev); | |
4bf0ba86 | 3164 | } |
54a3ac0c LT |
3165 | |
3166 | if (status) | |
3167 | dev_dbg(&udev->dev, | |
3168 | "disable remote wakeup, status %d\n", | |
3169 | status); | |
1da177e4 | 3170 | status = 0; |
1da177e4 LT |
3171 | } |
3172 | return status; | |
3173 | } | |
3174 | ||
eee49a52 PA |
3175 | /* |
3176 | * There are some SS USB devices which take longer time for link training. | |
3177 | * XHCI specs 4.19.4 says that when Link training is successful, port | |
3178 | * sets CSC bit to 1. So if SW reads port status before successful link | |
3179 | * training, then it will not find device to be present. | |
3180 | * USB Analyzer log with such buggy devices show that in some cases | |
3181 | * device switch on the RX termination after long delay of host enabling | |
3182 | * the VBUS. In few other cases it has been seen that device fails to | |
3183 | * negotiate link training in first attempt. It has been | |
3184 | * reported till now that few devices take as long as 2000 ms to train | |
3185 | * the link after host enabling its VBUS and termination. Following | |
3186 | * routine implements a 2000 ms timeout for link training. If in a case | |
3187 | * link trains before timeout, loop will exit earlier. | |
3188 | * | |
3189 | * FIXME: If a device was connected before suspend, but was removed | |
3190 | * while system was asleep, then the loop in the following routine will | |
3191 | * only exit at timeout. | |
3192 | * | |
3193 | * This routine should only be called when persist is enabled for a SS | |
3194 | * device. | |
3195 | */ | |
3196 | static int wait_for_ss_port_enable(struct usb_device *udev, | |
3197 | struct usb_hub *hub, int *port1, | |
3198 | u16 *portchange, u16 *portstatus) | |
3199 | { | |
3200 | int status = 0, delay_ms = 0; | |
3201 | ||
3202 | while (delay_ms < 2000) { | |
3203 | if (status || *portstatus & USB_PORT_STAT_CONNECTION) | |
3204 | break; | |
3205 | msleep(20); | |
3206 | delay_ms += 20; | |
3207 | status = hub_port_status(hub, *port1, portstatus, portchange); | |
3208 | } | |
3209 | return status; | |
3210 | } | |
3211 | ||
624d6c07 AS |
3212 | /* |
3213 | * usb_port_resume - re-activate a suspended usb device's upstream port | |
3214 | * @udev: device to re-activate, not a root hub | |
3215 | * Context: must be able to sleep; device not locked; pm locks held | |
3216 | * | |
3217 | * This will re-activate the suspended device, increasing power usage | |
3218 | * while letting drivers communicate again with its endpoints. | |
3219 | * USB resume explicitly guarantees that the power session between | |
3220 | * the host and the device is the same as it was when the device | |
3221 | * suspended. | |
3222 | * | |
feccc30d AS |
3223 | * If @udev->reset_resume is set then this routine won't check that the |
3224 | * port is still enabled. Furthermore, finish_port_resume() above will | |
54515fe5 AS |
3225 | * reset @udev. The end result is that a broken power session can be |
3226 | * recovered and @udev will appear to persist across a loss of VBUS power. | |
3227 | * | |
3228 | * For example, if a host controller doesn't maintain VBUS suspend current | |
3229 | * during a system sleep or is reset when the system wakes up, all the USB | |
3230 | * power sessions below it will be broken. This is especially troublesome | |
3231 | * for mass-storage devices containing mounted filesystems, since the | |
3232 | * device will appear to have disconnected and all the memory mappings | |
3233 | * to it will be lost. Using the USB_PERSIST facility, the device can be | |
3234 | * made to appear as if it had not disconnected. | |
3235 | * | |
742120c6 | 3236 | * This facility can be dangerous. Although usb_reset_and_verify_device() makes |
feccc30d | 3237 | * every effort to insure that the same device is present after the |
54515fe5 AS |
3238 | * reset as before, it cannot provide a 100% guarantee. Furthermore it's |
3239 | * quite possible for a device to remain unaltered but its media to be | |
3240 | * changed. If the user replaces a flash memory card while the system is | |
3241 | * asleep, he will have only himself to blame when the filesystem on the | |
3242 | * new card is corrupted and the system crashes. | |
3243 | * | |
624d6c07 AS |
3244 | * Returns 0 on success, else negative errno. |
3245 | */ | |
65bfd296 | 3246 | int usb_port_resume(struct usb_device *udev, pm_message_t msg) |
1da177e4 | 3247 | { |
ad493e5e LT |
3248 | struct usb_hub *hub = usb_hub_to_struct_hub(udev->parent); |
3249 | struct usb_port *port_dev = hub->ports[udev->portnum - 1]; | |
624d6c07 AS |
3250 | int port1 = udev->portnum; |
3251 | int status; | |
3252 | u16 portchange, portstatus; | |
d25450c6 | 3253 | |
ad493e5e LT |
3254 | if (port_dev->did_runtime_put) { |
3255 | status = pm_runtime_get_sync(&port_dev->dev); | |
3256 | port_dev->did_runtime_put = false; | |
3257 | if (status < 0) { | |
3258 | dev_dbg(&udev->dev, "can't resume usb port, status %d\n", | |
3259 | status); | |
3260 | return status; | |
3261 | } | |
3262 | } | |
3263 | ||
d25450c6 AS |
3264 | /* Skip the initial Clear-Suspend step for a remote wakeup */ |
3265 | status = hub_port_status(hub, port1, &portstatus, &portchange); | |
0ed9a57e | 3266 | if (status == 0 && !port_is_suspended(hub, portstatus)) |
d25450c6 | 3267 | goto SuspendCleared; |
1da177e4 LT |
3268 | |
3269 | // dev_dbg(hub->intfdev, "resume port %d\n", port1); | |
3270 | ||
d5cbad4b AS |
3271 | set_bit(port1, hub->busy_bits); |
3272 | ||
1da177e4 | 3273 | /* see 7.1.7.7; affects power usage, but not budgeting */ |
a7114230 | 3274 | if (hub_is_superspeed(hub->hdev)) |
c2f60db7 | 3275 | status = hub_set_port_link_state(hub, port1, USB_SS_PORT_LS_U0); |
a7114230 | 3276 | else |
ad493e5e | 3277 | status = usb_clear_port_feature(hub->hdev, |
a7114230 | 3278 | port1, USB_PORT_FEAT_SUSPEND); |
1da177e4 | 3279 | if (status) { |
624d6c07 AS |
3280 | dev_dbg(hub->intfdev, "can't resume port %d, status %d\n", |
3281 | port1, status); | |
1da177e4 | 3282 | } else { |
1da177e4 | 3283 | /* drive resume for at least 20 msec */ |
686314cf | 3284 | dev_dbg(&udev->dev, "usb %sresume\n", |
5b1b0b81 | 3285 | (PMSG_IS_AUTO(msg) ? "auto-" : "")); |
1da177e4 LT |
3286 | msleep(25); |
3287 | ||
1da177e4 LT |
3288 | /* Virtual root hubs can trigger on GET_PORT_STATUS to |
3289 | * stop resume signaling. Then finish the resume | |
3290 | * sequence. | |
3291 | */ | |
d25450c6 | 3292 | status = hub_port_status(hub, port1, &portstatus, &portchange); |
54515fe5 | 3293 | |
b01b03f3 AS |
3294 | /* TRSMRCY = 10 msec */ |
3295 | msleep(10); | |
3296 | } | |
3297 | ||
54515fe5 | 3298 | SuspendCleared: |
b01b03f3 | 3299 | if (status == 0) { |
bfd1e910 | 3300 | udev->port_is_suspended = 0; |
a7114230 AX |
3301 | if (hub_is_superspeed(hub->hdev)) { |
3302 | if (portchange & USB_PORT_STAT_C_LINK_STATE) | |
ad493e5e | 3303 | usb_clear_port_feature(hub->hdev, port1, |
a7114230 AX |
3304 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
3305 | } else { | |
3306 | if (portchange & USB_PORT_STAT_C_SUSPEND) | |
ad493e5e | 3307 | usb_clear_port_feature(hub->hdev, port1, |
a7114230 AX |
3308 | USB_PORT_FEAT_C_SUSPEND); |
3309 | } | |
1da177e4 | 3310 | } |
1da177e4 | 3311 | |
d5cbad4b | 3312 | clear_bit(port1, hub->busy_bits); |
d5cbad4b | 3313 | |
eee49a52 PA |
3314 | if (udev->persist_enabled && hub_is_superspeed(hub->hdev)) |
3315 | status = wait_for_ss_port_enable(udev, hub, &port1, &portchange, | |
3316 | &portstatus); | |
3317 | ||
b01b03f3 AS |
3318 | status = check_port_resume_type(udev, |
3319 | hub, port1, status, portchange, portstatus); | |
54515fe5 AS |
3320 | if (status == 0) |
3321 | status = finish_port_resume(udev); | |
3322 | if (status < 0) { | |
3323 | dev_dbg(&udev->dev, "can't resume, status %d\n", status); | |
3324 | hub_port_logical_disconnect(hub, port1); | |
65580b43 AX |
3325 | } else { |
3326 | /* Try to enable USB2 hardware LPM */ | |
3327 | if (udev->usb2_hw_lpm_capable == 1) | |
3328 | usb_set_usb2_hardware_lpm(udev, 1); | |
8306095f | 3329 | |
f74631e3 SS |
3330 | /* Try to enable USB3 LTM and LPM */ |
3331 | usb_enable_ltm(udev); | |
8306095f | 3332 | usb_unlocked_enable_lpm(udev); |
54515fe5 | 3333 | } |
65580b43 | 3334 | |
1da177e4 LT |
3335 | return status; |
3336 | } | |
3337 | ||
84ebc102 AS |
3338 | #endif /* CONFIG_PM */ |
3339 | ||
3340 | #ifdef CONFIG_PM_RUNTIME | |
3341 | ||
8808f00c | 3342 | /* caller has locked udev */ |
0534d468 | 3343 | int usb_remote_wakeup(struct usb_device *udev) |
1da177e4 LT |
3344 | { |
3345 | int status = 0; | |
3346 | ||
1da177e4 | 3347 | if (udev->state == USB_STATE_SUSPENDED) { |
645daaab | 3348 | dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); |
9bbdf1e0 AS |
3349 | status = usb_autoresume_device(udev); |
3350 | if (status == 0) { | |
3351 | /* Let the drivers do their thing, then... */ | |
3352 | usb_autosuspend_device(udev); | |
3353 | } | |
1da177e4 | 3354 | } |
1da177e4 LT |
3355 | return status; |
3356 | } | |
3357 | ||
d388dab7 AS |
3358 | #endif |
3359 | ||
e6f30dea ML |
3360 | static int check_ports_changed(struct usb_hub *hub) |
3361 | { | |
3362 | int port1; | |
3363 | ||
3364 | for (port1 = 1; port1 <= hub->hdev->maxchild; ++port1) { | |
3365 | u16 portstatus, portchange; | |
3366 | int status; | |
3367 | ||
3368 | status = hub_port_status(hub, port1, &portstatus, &portchange); | |
3369 | if (!status && portchange) | |
3370 | return 1; | |
3371 | } | |
3372 | return 0; | |
3373 | } | |
3374 | ||
db690874 | 3375 | static int hub_suspend(struct usb_interface *intf, pm_message_t msg) |
1da177e4 LT |
3376 | { |
3377 | struct usb_hub *hub = usb_get_intfdata (intf); | |
3378 | struct usb_device *hdev = hub->hdev; | |
3379 | unsigned port1; | |
4296c70a | 3380 | int status; |
1da177e4 | 3381 | |
5deba4cf AS |
3382 | /* |
3383 | * Warn if children aren't already suspended. | |
3384 | * Also, add up the number of wakeup-enabled descendants. | |
3385 | */ | |
3386 | hub->wakeup_enabled_descendants = 0; | |
1da177e4 LT |
3387 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { |
3388 | struct usb_device *udev; | |
3389 | ||
ff823c79 | 3390 | udev = hub->ports[port1 - 1]->child; |
6840d255 | 3391 | if (udev && udev->can_submit) { |
cbb33004 | 3392 | dev_warn(&intf->dev, "port %d nyet suspended\n", port1); |
5b1b0b81 | 3393 | if (PMSG_IS_AUTO(msg)) |
cbb33004 | 3394 | return -EBUSY; |
c9f89fa4 | 3395 | } |
5deba4cf AS |
3396 | if (udev) |
3397 | hub->wakeup_enabled_descendants += | |
3398 | wakeup_enabled_descendants(udev); | |
1da177e4 | 3399 | } |
e6f30dea ML |
3400 | |
3401 | if (hdev->do_remote_wakeup && hub->quirk_check_port_auto_suspend) { | |
3402 | /* check if there are changes pending on hub ports */ | |
3403 | if (check_ports_changed(hub)) { | |
3404 | if (PMSG_IS_AUTO(msg)) | |
3405 | return -EBUSY; | |
3406 | pm_wakeup_event(&hdev->dev, 2000); | |
3407 | } | |
3408 | } | |
3409 | ||
4296c70a SS |
3410 | if (hub_is_superspeed(hdev) && hdev->do_remote_wakeup) { |
3411 | /* Enable hub to send remote wakeup for all ports. */ | |
3412 | for (port1 = 1; port1 <= hdev->maxchild; port1++) { | |
3413 | status = set_port_feature(hdev, | |
3414 | port1 | | |
3415 | USB_PORT_FEAT_REMOTE_WAKE_CONNECT | | |
3416 | USB_PORT_FEAT_REMOTE_WAKE_DISCONNECT | | |
3417 | USB_PORT_FEAT_REMOTE_WAKE_OVER_CURRENT, | |
3418 | USB_PORT_FEAT_REMOTE_WAKE_MASK); | |
3419 | } | |
3420 | } | |
1da177e4 | 3421 | |
441b62c1 | 3422 | dev_dbg(&intf->dev, "%s\n", __func__); |
40f122f3 | 3423 | |
12f1ff83 | 3424 | /* stop khubd and related activity */ |
4330354f | 3425 | hub_quiesce(hub, HUB_SUSPEND); |
b6f6436d | 3426 | return 0; |
1da177e4 LT |
3427 | } |
3428 | ||
3429 | static int hub_resume(struct usb_interface *intf) | |
3430 | { | |
5e6effae | 3431 | struct usb_hub *hub = usb_get_intfdata(intf); |
40f122f3 | 3432 | |
5e6effae | 3433 | dev_dbg(&intf->dev, "%s\n", __func__); |
f2835219 | 3434 | hub_activate(hub, HUB_RESUME); |
1da177e4 LT |
3435 | return 0; |
3436 | } | |
3437 | ||
b41a60ec | 3438 | static int hub_reset_resume(struct usb_interface *intf) |
f07600cf | 3439 | { |
b41a60ec | 3440 | struct usb_hub *hub = usb_get_intfdata(intf); |
f07600cf | 3441 | |
5e6effae | 3442 | dev_dbg(&intf->dev, "%s\n", __func__); |
f2835219 | 3443 | hub_activate(hub, HUB_RESET_RESUME); |
f07600cf AS |
3444 | return 0; |
3445 | } | |
3446 | ||
54515fe5 AS |
3447 | /** |
3448 | * usb_root_hub_lost_power - called by HCD if the root hub lost Vbus power | |
3449 | * @rhdev: struct usb_device for the root hub | |
3450 | * | |
3451 | * The USB host controller driver calls this function when its root hub | |
3452 | * is resumed and Vbus power has been interrupted or the controller | |
feccc30d AS |
3453 | * has been reset. The routine marks @rhdev as having lost power. |
3454 | * When the hub driver is resumed it will take notice and carry out | |
3455 | * power-session recovery for all the "USB-PERSIST"-enabled child devices; | |
3456 | * the others will be disconnected. | |
54515fe5 AS |
3457 | */ |
3458 | void usb_root_hub_lost_power(struct usb_device *rhdev) | |
3459 | { | |
3460 | dev_warn(&rhdev->dev, "root hub lost power or was reset\n"); | |
3461 | rhdev->reset_resume = 1; | |
3462 | } | |
3463 | EXPORT_SYMBOL_GPL(usb_root_hub_lost_power); | |
3464 | ||
1ea7e0e8 SS |
3465 | static const char * const usb3_lpm_names[] = { |
3466 | "U0", | |
3467 | "U1", | |
3468 | "U2", | |
3469 | "U3", | |
3470 | }; | |
3471 | ||
3472 | /* | |
3473 | * Send a Set SEL control transfer to the device, prior to enabling | |
3474 | * device-initiated U1 or U2. This lets the device know the exit latencies from | |
3475 | * the time the device initiates a U1 or U2 exit, to the time it will receive a | |
3476 | * packet from the host. | |
3477 | * | |
3478 | * This function will fail if the SEL or PEL values for udev are greater than | |
3479 | * the maximum allowed values for the link state to be enabled. | |
3480 | */ | |
3481 | static int usb_req_set_sel(struct usb_device *udev, enum usb3_link_state state) | |
3482 | { | |
3483 | struct usb_set_sel_req *sel_values; | |
3484 | unsigned long long u1_sel; | |
3485 | unsigned long long u1_pel; | |
3486 | unsigned long long u2_sel; | |
3487 | unsigned long long u2_pel; | |
3488 | int ret; | |
3489 | ||
3490 | /* Convert SEL and PEL stored in ns to us */ | |
3491 | u1_sel = DIV_ROUND_UP(udev->u1_params.sel, 1000); | |
3492 | u1_pel = DIV_ROUND_UP(udev->u1_params.pel, 1000); | |
3493 | u2_sel = DIV_ROUND_UP(udev->u2_params.sel, 1000); | |
3494 | u2_pel = DIV_ROUND_UP(udev->u2_params.pel, 1000); | |
3495 | ||
3496 | /* | |
3497 | * Make sure that the calculated SEL and PEL values for the link | |
3498 | * state we're enabling aren't bigger than the max SEL/PEL | |
3499 | * value that will fit in the SET SEL control transfer. | |
3500 | * Otherwise the device would get an incorrect idea of the exit | |
3501 | * latency for the link state, and could start a device-initiated | |
3502 | * U1/U2 when the exit latencies are too high. | |
3503 | */ | |
3504 | if ((state == USB3_LPM_U1 && | |
3505 | (u1_sel > USB3_LPM_MAX_U1_SEL_PEL || | |
3506 | u1_pel > USB3_LPM_MAX_U1_SEL_PEL)) || | |
3507 | (state == USB3_LPM_U2 && | |
3508 | (u2_sel > USB3_LPM_MAX_U2_SEL_PEL || | |
3509 | u2_pel > USB3_LPM_MAX_U2_SEL_PEL))) { | |
1510a1a2 | 3510 | dev_dbg(&udev->dev, "Device-initiated %s disabled due to long SEL %llu us or PEL %llu us\n", |
1ea7e0e8 SS |
3511 | usb3_lpm_names[state], u1_sel, u1_pel); |
3512 | return -EINVAL; | |
3513 | } | |
3514 | ||
3515 | /* | |
3516 | * If we're enabling device-initiated LPM for one link state, | |
3517 | * but the other link state has a too high SEL or PEL value, | |
3518 | * just set those values to the max in the Set SEL request. | |
3519 | */ | |
3520 | if (u1_sel > USB3_LPM_MAX_U1_SEL_PEL) | |
3521 | u1_sel = USB3_LPM_MAX_U1_SEL_PEL; | |
3522 | ||
3523 | if (u1_pel > USB3_LPM_MAX_U1_SEL_PEL) | |
3524 | u1_pel = USB3_LPM_MAX_U1_SEL_PEL; | |
3525 | ||
3526 | if (u2_sel > USB3_LPM_MAX_U2_SEL_PEL) | |
3527 | u2_sel = USB3_LPM_MAX_U2_SEL_PEL; | |
3528 | ||
3529 | if (u2_pel > USB3_LPM_MAX_U2_SEL_PEL) | |
3530 | u2_pel = USB3_LPM_MAX_U2_SEL_PEL; | |
3531 | ||
3532 | /* | |
3533 | * usb_enable_lpm() can be called as part of a failed device reset, | |
3534 | * which may be initiated by an error path of a mass storage driver. | |
3535 | * Therefore, use GFP_NOIO. | |
3536 | */ | |
3537 | sel_values = kmalloc(sizeof *(sel_values), GFP_NOIO); | |
3538 | if (!sel_values) | |
3539 | return -ENOMEM; | |
3540 | ||
3541 | sel_values->u1_sel = u1_sel; | |
3542 | sel_values->u1_pel = u1_pel; | |
3543 | sel_values->u2_sel = cpu_to_le16(u2_sel); | |
3544 | sel_values->u2_pel = cpu_to_le16(u2_pel); | |
3545 | ||
3546 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
3547 | USB_REQ_SET_SEL, | |
3548 | USB_RECIP_DEVICE, | |
3549 | 0, 0, | |
3550 | sel_values, sizeof *(sel_values), | |
3551 | USB_CTRL_SET_TIMEOUT); | |
3552 | kfree(sel_values); | |
3553 | return ret; | |
3554 | } | |
3555 | ||
3556 | /* | |
3557 | * Enable or disable device-initiated U1 or U2 transitions. | |
3558 | */ | |
3559 | static int usb_set_device_initiated_lpm(struct usb_device *udev, | |
3560 | enum usb3_link_state state, bool enable) | |
3561 | { | |
3562 | int ret; | |
3563 | int feature; | |
3564 | ||
3565 | switch (state) { | |
3566 | case USB3_LPM_U1: | |
3567 | feature = USB_DEVICE_U1_ENABLE; | |
3568 | break; | |
3569 | case USB3_LPM_U2: | |
3570 | feature = USB_DEVICE_U2_ENABLE; | |
3571 | break; | |
3572 | default: | |
3573 | dev_warn(&udev->dev, "%s: Can't %s non-U1 or U2 state.\n", | |
3574 | __func__, enable ? "enable" : "disable"); | |
3575 | return -EINVAL; | |
3576 | } | |
3577 | ||
3578 | if (udev->state != USB_STATE_CONFIGURED) { | |
3579 | dev_dbg(&udev->dev, "%s: Can't %s %s state " | |
3580 | "for unconfigured device.\n", | |
3581 | __func__, enable ? "enable" : "disable", | |
3582 | usb3_lpm_names[state]); | |
3583 | return 0; | |
3584 | } | |
3585 | ||
3586 | if (enable) { | |
1ea7e0e8 SS |
3587 | /* |
3588 | * Now send the control transfer to enable device-initiated LPM | |
3589 | * for either U1 or U2. | |
3590 | */ | |
3591 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
3592 | USB_REQ_SET_FEATURE, | |
3593 | USB_RECIP_DEVICE, | |
3594 | feature, | |
3595 | 0, NULL, 0, | |
3596 | USB_CTRL_SET_TIMEOUT); | |
3597 | } else { | |
3598 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), | |
3599 | USB_REQ_CLEAR_FEATURE, | |
3600 | USB_RECIP_DEVICE, | |
3601 | feature, | |
3602 | 0, NULL, 0, | |
3603 | USB_CTRL_SET_TIMEOUT); | |
3604 | } | |
3605 | if (ret < 0) { | |
3606 | dev_warn(&udev->dev, "%s of device-initiated %s failed.\n", | |
3607 | enable ? "Enable" : "Disable", | |
3608 | usb3_lpm_names[state]); | |
3609 | return -EBUSY; | |
3610 | } | |
3611 | return 0; | |
3612 | } | |
3613 | ||
3614 | static int usb_set_lpm_timeout(struct usb_device *udev, | |
3615 | enum usb3_link_state state, int timeout) | |
3616 | { | |
3617 | int ret; | |
3618 | int feature; | |
3619 | ||
3620 | switch (state) { | |
3621 | case USB3_LPM_U1: | |
3622 | feature = USB_PORT_FEAT_U1_TIMEOUT; | |
3623 | break; | |
3624 | case USB3_LPM_U2: | |
3625 | feature = USB_PORT_FEAT_U2_TIMEOUT; | |
3626 | break; | |
3627 | default: | |
3628 | dev_warn(&udev->dev, "%s: Can't set timeout for non-U1 or U2 state.\n", | |
3629 | __func__); | |
3630 | return -EINVAL; | |
3631 | } | |
3632 | ||
3633 | if (state == USB3_LPM_U1 && timeout > USB3_LPM_U1_MAX_TIMEOUT && | |
3634 | timeout != USB3_LPM_DEVICE_INITIATED) { | |
3635 | dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x, " | |
3636 | "which is a reserved value.\n", | |
3637 | usb3_lpm_names[state], timeout); | |
3638 | return -EINVAL; | |
3639 | } | |
3640 | ||
3641 | ret = set_port_feature(udev->parent, | |
3642 | USB_PORT_LPM_TIMEOUT(timeout) | udev->portnum, | |
3643 | feature); | |
3644 | if (ret < 0) { | |
3645 | dev_warn(&udev->dev, "Failed to set %s timeout to 0x%x," | |
3646 | "error code %i\n", usb3_lpm_names[state], | |
3647 | timeout, ret); | |
3648 | return -EBUSY; | |
3649 | } | |
3650 | if (state == USB3_LPM_U1) | |
3651 | udev->u1_params.timeout = timeout; | |
3652 | else | |
3653 | udev->u2_params.timeout = timeout; | |
3654 | return 0; | |
3655 | } | |
3656 | ||
3657 | /* | |
3658 | * Enable the hub-initiated U1/U2 idle timeouts, and enable device-initiated | |
3659 | * U1/U2 entry. | |
3660 | * | |
3661 | * We will attempt to enable U1 or U2, but there are no guarantees that the | |
3662 | * control transfers to set the hub timeout or enable device-initiated U1/U2 | |
3663 | * will be successful. | |
3664 | * | |
3665 | * If we cannot set the parent hub U1/U2 timeout, we attempt to let the xHCI | |
3666 | * driver know about it. If that call fails, it should be harmless, and just | |
3667 | * take up more slightly more bus bandwidth for unnecessary U1/U2 exit latency. | |
3668 | */ | |
3669 | static void usb_enable_link_state(struct usb_hcd *hcd, struct usb_device *udev, | |
3670 | enum usb3_link_state state) | |
3671 | { | |
65a95b75 | 3672 | int timeout, ret; |
ae8963ad SS |
3673 | __u8 u1_mel = udev->bos->ss_cap->bU1devExitLat; |
3674 | __le16 u2_mel = udev->bos->ss_cap->bU2DevExitLat; | |
3675 | ||
3676 | /* If the device says it doesn't have *any* exit latency to come out of | |
3677 | * U1 or U2, it's probably lying. Assume it doesn't implement that link | |
3678 | * state. | |
3679 | */ | |
3680 | if ((state == USB3_LPM_U1 && u1_mel == 0) || | |
3681 | (state == USB3_LPM_U2 && u2_mel == 0)) | |
3682 | return; | |
1ea7e0e8 | 3683 | |
65a95b75 SS |
3684 | /* |
3685 | * First, let the device know about the exit latencies | |
3686 | * associated with the link state we're about to enable. | |
3687 | */ | |
3688 | ret = usb_req_set_sel(udev, state); | |
3689 | if (ret < 0) { | |
3690 | dev_warn(&udev->dev, "Set SEL for device-initiated %s failed.\n", | |
3691 | usb3_lpm_names[state]); | |
3692 | return; | |
3693 | } | |
3694 | ||
1ea7e0e8 SS |
3695 | /* We allow the host controller to set the U1/U2 timeout internally |
3696 | * first, so that it can change its schedule to account for the | |
3697 | * additional latency to send data to a device in a lower power | |
3698 | * link state. | |
3699 | */ | |
3700 | timeout = hcd->driver->enable_usb3_lpm_timeout(hcd, udev, state); | |
3701 | ||
3702 | /* xHCI host controller doesn't want to enable this LPM state. */ | |
3703 | if (timeout == 0) | |
3704 | return; | |
3705 | ||
3706 | if (timeout < 0) { | |
3707 | dev_warn(&udev->dev, "Could not enable %s link state, " | |
3708 | "xHCI error %i.\n", usb3_lpm_names[state], | |
3709 | timeout); | |
3710 | return; | |
3711 | } | |
3712 | ||
3713 | if (usb_set_lpm_timeout(udev, state, timeout)) | |
3714 | /* If we can't set the parent hub U1/U2 timeout, | |
3715 | * device-initiated LPM won't be allowed either, so let the xHCI | |
3716 | * host know that this link state won't be enabled. | |
3717 | */ | |
3718 | hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state); | |
3719 | ||
3720 | /* Only a configured device will accept the Set Feature U1/U2_ENABLE */ | |
3721 | else if (udev->actconfig) | |
3722 | usb_set_device_initiated_lpm(udev, state, true); | |
3723 | ||
3724 | } | |
3725 | ||
3726 | /* | |
3727 | * Disable the hub-initiated U1/U2 idle timeouts, and disable device-initiated | |
3728 | * U1/U2 entry. | |
3729 | * | |
3730 | * If this function returns -EBUSY, the parent hub will still allow U1/U2 entry. | |
3731 | * If zero is returned, the parent will not allow the link to go into U1/U2. | |
3732 | * | |
3733 | * If zero is returned, device-initiated U1/U2 entry may still be enabled, but | |
3734 | * it won't have an effect on the bus link state because the parent hub will | |
3735 | * still disallow device-initiated U1/U2 entry. | |
3736 | * | |
3737 | * If zero is returned, the xHCI host controller may still think U1/U2 entry is | |
3738 | * possible. The result will be slightly more bus bandwidth will be taken up | |
3739 | * (to account for U1/U2 exit latency), but it should be harmless. | |
3740 | */ | |
3741 | static int usb_disable_link_state(struct usb_hcd *hcd, struct usb_device *udev, | |
3742 | enum usb3_link_state state) | |
3743 | { | |
3744 | int feature; | |
3745 | ||
3746 | switch (state) { | |
3747 | case USB3_LPM_U1: | |
3748 | feature = USB_PORT_FEAT_U1_TIMEOUT; | |
3749 | break; | |
3750 | case USB3_LPM_U2: | |
3751 | feature = USB_PORT_FEAT_U2_TIMEOUT; | |
3752 | break; | |
3753 | default: | |
3754 | dev_warn(&udev->dev, "%s: Can't disable non-U1 or U2 state.\n", | |
3755 | __func__); | |
3756 | return -EINVAL; | |
3757 | } | |
3758 | ||
3759 | if (usb_set_lpm_timeout(udev, state, 0)) | |
3760 | return -EBUSY; | |
3761 | ||
3762 | usb_set_device_initiated_lpm(udev, state, false); | |
3763 | ||
3764 | if (hcd->driver->disable_usb3_lpm_timeout(hcd, udev, state)) | |
3765 | dev_warn(&udev->dev, "Could not disable xHCI %s timeout, " | |
3766 | "bus schedule bandwidth may be impacted.\n", | |
3767 | usb3_lpm_names[state]); | |
3768 | return 0; | |
3769 | } | |
3770 | ||
3771 | /* | |
3772 | * Disable hub-initiated and device-initiated U1 and U2 entry. | |
3773 | * Caller must own the bandwidth_mutex. | |
3774 | * | |
3775 | * This will call usb_enable_lpm() on failure, which will decrement | |
3776 | * lpm_disable_count, and will re-enable LPM if lpm_disable_count reaches zero. | |
3777 | */ | |
3778 | int usb_disable_lpm(struct usb_device *udev) | |
3779 | { | |
3780 | struct usb_hcd *hcd; | |
3781 | ||
3782 | if (!udev || !udev->parent || | |
3783 | udev->speed != USB_SPEED_SUPER || | |
3784 | !udev->lpm_capable) | |
3785 | return 0; | |
3786 | ||
3787 | hcd = bus_to_hcd(udev->bus); | |
3788 | if (!hcd || !hcd->driver->disable_usb3_lpm_timeout) | |
3789 | return 0; | |
3790 | ||
3791 | udev->lpm_disable_count++; | |
55558c33 | 3792 | if ((udev->u1_params.timeout == 0 && udev->u2_params.timeout == 0)) |
1ea7e0e8 SS |
3793 | return 0; |
3794 | ||
3795 | /* If LPM is enabled, attempt to disable it. */ | |
3796 | if (usb_disable_link_state(hcd, udev, USB3_LPM_U1)) | |
3797 | goto enable_lpm; | |
3798 | if (usb_disable_link_state(hcd, udev, USB3_LPM_U2)) | |
3799 | goto enable_lpm; | |
3800 | ||
3801 | return 0; | |
3802 | ||
3803 | enable_lpm: | |
3804 | usb_enable_lpm(udev); | |
3805 | return -EBUSY; | |
3806 | } | |
3807 | EXPORT_SYMBOL_GPL(usb_disable_lpm); | |
3808 | ||
3809 | /* Grab the bandwidth_mutex before calling usb_disable_lpm() */ | |
3810 | int usb_unlocked_disable_lpm(struct usb_device *udev) | |
3811 | { | |
3812 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
3813 | int ret; | |
3814 | ||
3815 | if (!hcd) | |
3816 | return -EINVAL; | |
3817 | ||
3818 | mutex_lock(hcd->bandwidth_mutex); | |
3819 | ret = usb_disable_lpm(udev); | |
3820 | mutex_unlock(hcd->bandwidth_mutex); | |
3821 | ||
3822 | return ret; | |
3823 | } | |
3824 | EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); | |
3825 | ||
3826 | /* | |
3827 | * Attempt to enable device-initiated and hub-initiated U1 and U2 entry. The | |
3828 | * xHCI host policy may prevent U1 or U2 from being enabled. | |
3829 | * | |
3830 | * Other callers may have disabled link PM, so U1 and U2 entry will be disabled | |
3831 | * until the lpm_disable_count drops to zero. Caller must own the | |
3832 | * bandwidth_mutex. | |
3833 | */ | |
3834 | void usb_enable_lpm(struct usb_device *udev) | |
3835 | { | |
3836 | struct usb_hcd *hcd; | |
3837 | ||
3838 | if (!udev || !udev->parent || | |
3839 | udev->speed != USB_SPEED_SUPER || | |
3840 | !udev->lpm_capable) | |
3841 | return; | |
3842 | ||
3843 | udev->lpm_disable_count--; | |
3844 | hcd = bus_to_hcd(udev->bus); | |
3845 | /* Double check that we can both enable and disable LPM. | |
3846 | * Device must be configured to accept set feature U1/U2 timeout. | |
3847 | */ | |
3848 | if (!hcd || !hcd->driver->enable_usb3_lpm_timeout || | |
3849 | !hcd->driver->disable_usb3_lpm_timeout) | |
3850 | return; | |
3851 | ||
3852 | if (udev->lpm_disable_count > 0) | |
3853 | return; | |
3854 | ||
3855 | usb_enable_link_state(hcd, udev, USB3_LPM_U1); | |
3856 | usb_enable_link_state(hcd, udev, USB3_LPM_U2); | |
3857 | } | |
3858 | EXPORT_SYMBOL_GPL(usb_enable_lpm); | |
3859 | ||
3860 | /* Grab the bandwidth_mutex before calling usb_enable_lpm() */ | |
3861 | void usb_unlocked_enable_lpm(struct usb_device *udev) | |
3862 | { | |
3863 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); | |
3864 | ||
3865 | if (!hcd) | |
3866 | return; | |
3867 | ||
3868 | mutex_lock(hcd->bandwidth_mutex); | |
3869 | usb_enable_lpm(udev); | |
3870 | mutex_unlock(hcd->bandwidth_mutex); | |
3871 | } | |
3872 | EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); | |
3873 | ||
3874 | ||
d388dab7 AS |
3875 | #else /* CONFIG_PM */ |
3876 | ||
f07600cf AS |
3877 | #define hub_suspend NULL |
3878 | #define hub_resume NULL | |
3879 | #define hub_reset_resume NULL | |
1ea7e0e8 SS |
3880 | |
3881 | int usb_disable_lpm(struct usb_device *udev) | |
3882 | { | |
3883 | return 0; | |
3884 | } | |
e9261fb6 | 3885 | EXPORT_SYMBOL_GPL(usb_disable_lpm); |
1ea7e0e8 SS |
3886 | |
3887 | void usb_enable_lpm(struct usb_device *udev) { } | |
e9261fb6 | 3888 | EXPORT_SYMBOL_GPL(usb_enable_lpm); |
1ea7e0e8 SS |
3889 | |
3890 | int usb_unlocked_disable_lpm(struct usb_device *udev) | |
3891 | { | |
3892 | return 0; | |
3893 | } | |
e9261fb6 | 3894 | EXPORT_SYMBOL_GPL(usb_unlocked_disable_lpm); |
1ea7e0e8 SS |
3895 | |
3896 | void usb_unlocked_enable_lpm(struct usb_device *udev) { } | |
e9261fb6 | 3897 | EXPORT_SYMBOL_GPL(usb_unlocked_enable_lpm); |
f74631e3 SS |
3898 | |
3899 | int usb_disable_ltm(struct usb_device *udev) | |
3900 | { | |
3901 | return 0; | |
3902 | } | |
3903 | EXPORT_SYMBOL_GPL(usb_disable_ltm); | |
3904 | ||
3905 | void usb_enable_ltm(struct usb_device *udev) { } | |
3906 | EXPORT_SYMBOL_GPL(usb_enable_ltm); | |
d388dab7 AS |
3907 | #endif |
3908 | ||
1da177e4 LT |
3909 | |
3910 | /* USB 2.0 spec, 7.1.7.3 / fig 7-29: | |
3911 | * | |
3912 | * Between connect detection and reset signaling there must be a delay | |
3913 | * of 100ms at least for debounce and power-settling. The corresponding | |
3914 | * timer shall restart whenever the downstream port detects a disconnect. | |
3915 | * | |
3916 | * Apparently there are some bluetooth and irda-dongles and a number of | |
3917 | * low-speed devices for which this debounce period may last over a second. | |
3918 | * Not covered by the spec - but easy to deal with. | |
3919 | * | |
3920 | * This implementation uses a 1500ms total debounce timeout; if the | |
3921 | * connection isn't stable by then it returns -ETIMEDOUT. It checks | |
3922 | * every 25ms for transient disconnects. When the port status has been | |
3923 | * unchanged for 100ms it returns the port status. | |
3924 | */ | |
ad493e5e | 3925 | int hub_port_debounce(struct usb_hub *hub, int port1, bool must_be_connected) |
1da177e4 LT |
3926 | { |
3927 | int ret; | |
3928 | int total_time, stable_time = 0; | |
3929 | u16 portchange, portstatus; | |
3930 | unsigned connection = 0xffff; | |
3931 | ||
3932 | for (total_time = 0; ; total_time += HUB_DEBOUNCE_STEP) { | |
3933 | ret = hub_port_status(hub, port1, &portstatus, &portchange); | |
3934 | if (ret < 0) | |
3935 | return ret; | |
3936 | ||
3937 | if (!(portchange & USB_PORT_STAT_C_CONNECTION) && | |
3938 | (portstatus & USB_PORT_STAT_CONNECTION) == connection) { | |
ad493e5e LT |
3939 | if (!must_be_connected || |
3940 | (connection == USB_PORT_STAT_CONNECTION)) | |
3941 | stable_time += HUB_DEBOUNCE_STEP; | |
1da177e4 LT |
3942 | if (stable_time >= HUB_DEBOUNCE_STABLE) |
3943 | break; | |
3944 | } else { | |
3945 | stable_time = 0; | |
3946 | connection = portstatus & USB_PORT_STAT_CONNECTION; | |
3947 | } | |
3948 | ||
3949 | if (portchange & USB_PORT_STAT_C_CONNECTION) { | |
ad493e5e | 3950 | usb_clear_port_feature(hub->hdev, port1, |
1da177e4 LT |
3951 | USB_PORT_FEAT_C_CONNECTION); |
3952 | } | |
3953 | ||
3954 | if (total_time >= HUB_DEBOUNCE_TIMEOUT) | |
3955 | break; | |
3956 | msleep(HUB_DEBOUNCE_STEP); | |
3957 | } | |
3958 | ||
3959 | dev_dbg (hub->intfdev, | |
3960 | "debounce: port %d: total %dms stable %dms status 0x%x\n", | |
3961 | port1, total_time, stable_time, portstatus); | |
3962 | ||
3963 | if (stable_time < HUB_DEBOUNCE_STABLE) | |
3964 | return -ETIMEDOUT; | |
3965 | return portstatus; | |
3966 | } | |
3967 | ||
fc721f51 | 3968 | void usb_ep0_reinit(struct usb_device *udev) |
1da177e4 | 3969 | { |
ddeac4e7 AS |
3970 | usb_disable_endpoint(udev, 0 + USB_DIR_IN, true); |
3971 | usb_disable_endpoint(udev, 0 + USB_DIR_OUT, true); | |
2caf7fcd | 3972 | usb_enable_endpoint(udev, &udev->ep0, true); |
1da177e4 | 3973 | } |
fc721f51 | 3974 | EXPORT_SYMBOL_GPL(usb_ep0_reinit); |
1da177e4 LT |
3975 | |
3976 | #define usb_sndaddr0pipe() (PIPE_CONTROL << 30) | |
3977 | #define usb_rcvaddr0pipe() ((PIPE_CONTROL << 30) | USB_DIR_IN) | |
3978 | ||
4326ed0b | 3979 | static int hub_set_address(struct usb_device *udev, int devnum) |
1da177e4 LT |
3980 | { |
3981 | int retval; | |
c6515272 | 3982 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
1da177e4 | 3983 | |
c6515272 SS |
3984 | /* |
3985 | * The host controller will choose the device address, | |
3986 | * instead of the core having chosen it earlier | |
3987 | */ | |
3988 | if (!hcd->driver->address_device && devnum <= 1) | |
1da177e4 LT |
3989 | return -EINVAL; |
3990 | if (udev->state == USB_STATE_ADDRESS) | |
3991 | return 0; | |
3992 | if (udev->state != USB_STATE_DEFAULT) | |
3993 | return -EINVAL; | |
c8d4af8e | 3994 | if (hcd->driver->address_device) |
c6515272 | 3995 | retval = hcd->driver->address_device(hcd, udev); |
c8d4af8e | 3996 | else |
c6515272 SS |
3997 | retval = usb_control_msg(udev, usb_sndaddr0pipe(), |
3998 | USB_REQ_SET_ADDRESS, 0, devnum, 0, | |
3999 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
1da177e4 | 4000 | if (retval == 0) { |
3b29b68b | 4001 | update_devnum(udev, devnum); |
4953d141 | 4002 | /* Device now using proper address. */ |
1da177e4 | 4003 | usb_set_device_state(udev, USB_STATE_ADDRESS); |
fc721f51 | 4004 | usb_ep0_reinit(udev); |
1da177e4 LT |
4005 | } |
4006 | return retval; | |
4007 | } | |
4008 | ||
4009 | /* Reset device, (re)assign address, get device descriptor. | |
4010 | * Device connection must be stable, no more debouncing needed. | |
4011 | * Returns device in USB_STATE_ADDRESS, except on error. | |
4012 | * | |
4013 | * If this is called for an already-existing device (as part of | |
742120c6 | 4014 | * usb_reset_and_verify_device), the caller must own the device lock. For a |
1da177e4 LT |
4015 | * newly detected device that is not accessible through any global |
4016 | * pointers, it's not necessary to lock the device. | |
4017 | */ | |
4018 | static int | |
4019 | hub_port_init (struct usb_hub *hub, struct usb_device *udev, int port1, | |
4020 | int retry_counter) | |
4021 | { | |
4186ecf8 | 4022 | static DEFINE_MUTEX(usb_address0_mutex); |
1da177e4 LT |
4023 | |
4024 | struct usb_device *hdev = hub->hdev; | |
e7b77172 | 4025 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
1da177e4 LT |
4026 | int i, j, retval; |
4027 | unsigned delay = HUB_SHORT_RESET_TIME; | |
4028 | enum usb_device_speed oldspeed = udev->speed; | |
e538dfda | 4029 | const char *speed; |
4326ed0b | 4030 | int devnum = udev->devnum; |
1da177e4 LT |
4031 | |
4032 | /* root hub ports have a slightly longer reset period | |
4033 | * (from USB 2.0 spec, section 7.1.7.5) | |
4034 | */ | |
4035 | if (!hdev->parent) { | |
4036 | delay = HUB_ROOT_RESET_TIME; | |
4037 | if (port1 == hdev->bus->otg_port) | |
4038 | hdev->bus->b_hnp_enable = 0; | |
4039 | } | |
4040 | ||
4041 | /* Some low speed devices have problems with the quick delay, so */ | |
4042 | /* be a bit pessimistic with those devices. RHbug #23670 */ | |
4043 | if (oldspeed == USB_SPEED_LOW) | |
4044 | delay = HUB_LONG_RESET_TIME; | |
4045 | ||
4186ecf8 | 4046 | mutex_lock(&usb_address0_mutex); |
1da177e4 | 4047 | |
07194ab7 LT |
4048 | /* Reset the device; full speed may morph to high speed */ |
4049 | /* FIXME a USB 2.0 device may morph into SuperSpeed on reset. */ | |
75d7cf72 | 4050 | retval = hub_port_reset(hub, port1, udev, delay, false); |
07194ab7 LT |
4051 | if (retval < 0) /* error or disconnect */ |
4052 | goto fail; | |
4053 | /* success, speed is known */ | |
4054 | ||
1da177e4 LT |
4055 | retval = -ENODEV; |
4056 | ||
4057 | if (oldspeed != USB_SPEED_UNKNOWN && oldspeed != udev->speed) { | |
4058 | dev_dbg(&udev->dev, "device reset changed speed!\n"); | |
4059 | goto fail; | |
4060 | } | |
4061 | oldspeed = udev->speed; | |
4326ed0b | 4062 | |
1da177e4 LT |
4063 | /* USB 2.0 section 5.5.3 talks about ep0 maxpacket ... |
4064 | * it's fixed size except for full speed devices. | |
5bb6e0ae IPG |
4065 | * For Wireless USB devices, ep0 max packet is always 512 (tho |
4066 | * reported as 0xff in the device descriptor). WUSB1.0[4.8.1]. | |
1da177e4 LT |
4067 | */ |
4068 | switch (udev->speed) { | |
6b403b02 | 4069 | case USB_SPEED_SUPER: |
551cdbbe | 4070 | case USB_SPEED_WIRELESS: /* fixed at 512 */ |
551509d2 | 4071 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(512); |
5bb6e0ae | 4072 | break; |
1da177e4 | 4073 | case USB_SPEED_HIGH: /* fixed at 64 */ |
551509d2 | 4074 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
1da177e4 LT |
4075 | break; |
4076 | case USB_SPEED_FULL: /* 8, 16, 32, or 64 */ | |
4077 | /* to determine the ep0 maxpacket size, try to read | |
4078 | * the device descriptor to get bMaxPacketSize0 and | |
4079 | * then correct our initial guess. | |
4080 | */ | |
551509d2 | 4081 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(64); |
1da177e4 LT |
4082 | break; |
4083 | case USB_SPEED_LOW: /* fixed at 8 */ | |
551509d2 | 4084 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(8); |
1da177e4 LT |
4085 | break; |
4086 | default: | |
4087 | goto fail; | |
4088 | } | |
e538dfda MN |
4089 | |
4090 | if (udev->speed == USB_SPEED_WIRELESS) | |
4091 | speed = "variable speed Wireless"; | |
4092 | else | |
4093 | speed = usb_speed_string(udev->speed); | |
4094 | ||
c6515272 SS |
4095 | if (udev->speed != USB_SPEED_SUPER) |
4096 | dev_info(&udev->dev, | |
e538dfda MN |
4097 | "%s %s USB device number %d using %s\n", |
4098 | (udev->config) ? "reset" : "new", speed, | |
3b29b68b | 4099 | devnum, udev->bus->controller->driver->name); |
1da177e4 LT |
4100 | |
4101 | /* Set up TT records, if needed */ | |
4102 | if (hdev->tt) { | |
4103 | udev->tt = hdev->tt; | |
4104 | udev->ttport = hdev->ttport; | |
4105 | } else if (udev->speed != USB_SPEED_HIGH | |
4106 | && hdev->speed == USB_SPEED_HIGH) { | |
d199c96d AS |
4107 | if (!hub->tt.hub) { |
4108 | dev_err(&udev->dev, "parent hub has no TT\n"); | |
4109 | retval = -EINVAL; | |
4110 | goto fail; | |
4111 | } | |
1da177e4 LT |
4112 | udev->tt = &hub->tt; |
4113 | udev->ttport = port1; | |
4114 | } | |
4115 | ||
4116 | /* Why interleave GET_DESCRIPTOR and SET_ADDRESS this way? | |
4117 | * Because device hardware and firmware is sometimes buggy in | |
4118 | * this area, and this is how Linux has done it for ages. | |
4119 | * Change it cautiously. | |
4120 | * | |
4121 | * NOTE: If USE_NEW_SCHEME() is true we will start by issuing | |
4122 | * a 64-byte GET_DESCRIPTOR request. This is what Windows does, | |
4123 | * so it may help with some non-standards-compliant devices. | |
4124 | * Otherwise we start with SET_ADDRESS and then try to read the | |
4125 | * first 8 bytes of the device descriptor to get the ep0 maxpacket | |
4126 | * value. | |
4127 | */ | |
4128 | for (i = 0; i < GET_DESCRIPTOR_TRIES; (++i, msleep(100))) { | |
c6515272 | 4129 | if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) { |
1da177e4 LT |
4130 | struct usb_device_descriptor *buf; |
4131 | int r = 0; | |
4132 | ||
4133 | #define GET_DESCRIPTOR_BUFSIZE 64 | |
4134 | buf = kmalloc(GET_DESCRIPTOR_BUFSIZE, GFP_NOIO); | |
4135 | if (!buf) { | |
4136 | retval = -ENOMEM; | |
4137 | continue; | |
4138 | } | |
4139 | ||
b89ee19a AS |
4140 | /* Retry on all errors; some devices are flakey. |
4141 | * 255 is for WUSB devices, we actually need to use | |
4142 | * 512 (WUSB1.0[4.8.1]). | |
1da177e4 LT |
4143 | */ |
4144 | for (j = 0; j < 3; ++j) { | |
4145 | buf->bMaxPacketSize0 = 0; | |
4146 | r = usb_control_msg(udev, usb_rcvaddr0pipe(), | |
4147 | USB_REQ_GET_DESCRIPTOR, USB_DIR_IN, | |
4148 | USB_DT_DEVICE << 8, 0, | |
4149 | buf, GET_DESCRIPTOR_BUFSIZE, | |
fd7c519d | 4150 | initial_descriptor_timeout); |
1da177e4 | 4151 | switch (buf->bMaxPacketSize0) { |
5bb6e0ae | 4152 | case 8: case 16: case 32: case 64: case 255: |
1da177e4 LT |
4153 | if (buf->bDescriptorType == |
4154 | USB_DT_DEVICE) { | |
4155 | r = 0; | |
4156 | break; | |
4157 | } | |
4158 | /* FALL THROUGH */ | |
4159 | default: | |
4160 | if (r == 0) | |
4161 | r = -EPROTO; | |
4162 | break; | |
4163 | } | |
4164 | if (r == 0) | |
4165 | break; | |
4166 | } | |
4167 | udev->descriptor.bMaxPacketSize0 = | |
4168 | buf->bMaxPacketSize0; | |
4169 | kfree(buf); | |
4170 | ||
75d7cf72 | 4171 | retval = hub_port_reset(hub, port1, udev, delay, false); |
1da177e4 LT |
4172 | if (retval < 0) /* error or disconnect */ |
4173 | goto fail; | |
4174 | if (oldspeed != udev->speed) { | |
4175 | dev_dbg(&udev->dev, | |
4176 | "device reset changed speed!\n"); | |
4177 | retval = -ENODEV; | |
4178 | goto fail; | |
4179 | } | |
4180 | if (r) { | |
e9e88fb7 AS |
4181 | if (r != -ENODEV) |
4182 | dev_err(&udev->dev, "device descriptor read/64, error %d\n", | |
4183 | r); | |
1da177e4 LT |
4184 | retval = -EMSGSIZE; |
4185 | continue; | |
4186 | } | |
4187 | #undef GET_DESCRIPTOR_BUFSIZE | |
4188 | } | |
4189 | ||
6c529cdc IPG |
4190 | /* |
4191 | * If device is WUSB, we already assigned an | |
4192 | * unauthorized address in the Connect Ack sequence; | |
4193 | * authorization will assign the final address. | |
4194 | */ | |
c6515272 | 4195 | if (udev->wusb == 0) { |
6c529cdc IPG |
4196 | for (j = 0; j < SET_ADDRESS_TRIES; ++j) { |
4197 | retval = hub_set_address(udev, devnum); | |
4198 | if (retval >= 0) | |
4199 | break; | |
4200 | msleep(200); | |
4201 | } | |
4202 | if (retval < 0) { | |
e9e88fb7 AS |
4203 | if (retval != -ENODEV) |
4204 | dev_err(&udev->dev, "device not accepting address %d, error %d\n", | |
4205 | devnum, retval); | |
6c529cdc IPG |
4206 | goto fail; |
4207 | } | |
c6515272 SS |
4208 | if (udev->speed == USB_SPEED_SUPER) { |
4209 | devnum = udev->devnum; | |
4210 | dev_info(&udev->dev, | |
3b29b68b | 4211 | "%s SuperSpeed USB device number %d using %s\n", |
c6515272 | 4212 | (udev->config) ? "reset" : "new", |
3b29b68b | 4213 | devnum, udev->bus->controller->driver->name); |
c6515272 | 4214 | } |
6c529cdc IPG |
4215 | |
4216 | /* cope with hardware quirkiness: | |
4217 | * - let SET_ADDRESS settle, some device hardware wants it | |
4218 | * - read ep0 maxpacket even for high and low speed, | |
4219 | */ | |
4220 | msleep(10); | |
c6515272 | 4221 | if (USE_NEW_SCHEME(retry_counter) && !(hcd->driver->flags & HCD_USB3)) |
1da177e4 | 4222 | break; |
6c529cdc | 4223 | } |
1da177e4 LT |
4224 | |
4225 | retval = usb_get_device_descriptor(udev, 8); | |
4226 | if (retval < 8) { | |
e9e88fb7 AS |
4227 | if (retval != -ENODEV) |
4228 | dev_err(&udev->dev, | |
b9cef6c3 WF |
4229 | "device descriptor read/8, error %d\n", |
4230 | retval); | |
1da177e4 LT |
4231 | if (retval >= 0) |
4232 | retval = -EMSGSIZE; | |
4233 | } else { | |
4234 | retval = 0; | |
4235 | break; | |
4236 | } | |
4237 | } | |
4238 | if (retval) | |
4239 | goto fail; | |
4240 | ||
b76baa81 | 4241 | if (hcd->phy && !hdev->parent) |
ac96511b | 4242 | usb_phy_notify_connect(hcd->phy, udev->speed); |
b76baa81 | 4243 | |
d8aec3db EF |
4244 | /* |
4245 | * Some superspeed devices have finished the link training process | |
4246 | * and attached to a superspeed hub port, but the device descriptor | |
4247 | * got from those devices show they aren't superspeed devices. Warm | |
4248 | * reset the port attached by the devices can fix them. | |
4249 | */ | |
4250 | if ((udev->speed == USB_SPEED_SUPER) && | |
4251 | (le16_to_cpu(udev->descriptor.bcdUSB) < 0x0300)) { | |
4252 | dev_err(&udev->dev, "got a wrong device descriptor, " | |
4253 | "warm reset device\n"); | |
4254 | hub_port_reset(hub, port1, udev, | |
4255 | HUB_BH_RESET_TIME, true); | |
4256 | retval = -EINVAL; | |
4257 | goto fail; | |
4258 | } | |
4259 | ||
6b403b02 SS |
4260 | if (udev->descriptor.bMaxPacketSize0 == 0xff || |
4261 | udev->speed == USB_SPEED_SUPER) | |
4262 | i = 512; | |
4263 | else | |
4264 | i = udev->descriptor.bMaxPacketSize0; | |
29cc8897 | 4265 | if (usb_endpoint_maxp(&udev->ep0.desc) != i) { |
56626a72 | 4266 | if (udev->speed == USB_SPEED_LOW || |
1da177e4 | 4267 | !(i == 8 || i == 16 || i == 32 || i == 64)) { |
56626a72 | 4268 | dev_err(&udev->dev, "Invalid ep0 maxpacket: %d\n", i); |
1da177e4 LT |
4269 | retval = -EMSGSIZE; |
4270 | goto fail; | |
4271 | } | |
56626a72 AS |
4272 | if (udev->speed == USB_SPEED_FULL) |
4273 | dev_dbg(&udev->dev, "ep0 maxpacket = %d\n", i); | |
4274 | else | |
4275 | dev_warn(&udev->dev, "Using ep0 maxpacket: %d\n", i); | |
1da177e4 | 4276 | udev->ep0.desc.wMaxPacketSize = cpu_to_le16(i); |
fc721f51 | 4277 | usb_ep0_reinit(udev); |
1da177e4 LT |
4278 | } |
4279 | ||
4280 | retval = usb_get_device_descriptor(udev, USB_DT_DEVICE_SIZE); | |
4281 | if (retval < (signed)sizeof(udev->descriptor)) { | |
e9e88fb7 AS |
4282 | if (retval != -ENODEV) |
4283 | dev_err(&udev->dev, "device descriptor read/all, error %d\n", | |
4284 | retval); | |
1da177e4 LT |
4285 | if (retval >= 0) |
4286 | retval = -ENOMSG; | |
4287 | goto fail; | |
4288 | } | |
4289 | ||
1ff4df56 AX |
4290 | if (udev->wusb == 0 && le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0201) { |
4291 | retval = usb_get_bos_descriptor(udev); | |
51e0a012 | 4292 | if (!retval) { |
d9b2099c | 4293 | udev->lpm_capable = usb_device_supports_lpm(udev); |
51e0a012 SS |
4294 | usb_set_lpm_parameters(udev); |
4295 | } | |
1ff4df56 | 4296 | } |
3148bf04 | 4297 | |
1da177e4 | 4298 | retval = 0; |
48f24970 AD |
4299 | /* notify HCD that we have a device connected and addressed */ |
4300 | if (hcd->driver->update_device) | |
4301 | hcd->driver->update_device(hcd, udev); | |
1da177e4 | 4302 | fail: |
4326ed0b | 4303 | if (retval) { |
1da177e4 | 4304 | hub_port_disable(hub, port1, 0); |
3b29b68b | 4305 | update_devnum(udev, devnum); /* for disconnect processing */ |
4326ed0b | 4306 | } |
4186ecf8 | 4307 | mutex_unlock(&usb_address0_mutex); |
1da177e4 LT |
4308 | return retval; |
4309 | } | |
4310 | ||
4311 | static void | |
4312 | check_highspeed (struct usb_hub *hub, struct usb_device *udev, int port1) | |
4313 | { | |
4314 | struct usb_qualifier_descriptor *qual; | |
4315 | int status; | |
4316 | ||
e94b1766 | 4317 | qual = kmalloc (sizeof *qual, GFP_KERNEL); |
1da177e4 LT |
4318 | if (qual == NULL) |
4319 | return; | |
4320 | ||
4321 | status = usb_get_descriptor (udev, USB_DT_DEVICE_QUALIFIER, 0, | |
4322 | qual, sizeof *qual); | |
4323 | if (status == sizeof *qual) { | |
4324 | dev_info(&udev->dev, "not running at top speed; " | |
4325 | "connect to a high speed hub\n"); | |
4326 | /* hub LEDs are probably harder to miss than syslog */ | |
4327 | if (hub->has_indicators) { | |
4328 | hub->indicator[port1-1] = INDICATOR_GREEN_BLINK; | |
c4028958 | 4329 | schedule_delayed_work (&hub->leds, 0); |
1da177e4 LT |
4330 | } |
4331 | } | |
1bc3c9e1 | 4332 | kfree(qual); |
1da177e4 LT |
4333 | } |
4334 | ||
4335 | static unsigned | |
4336 | hub_power_remaining (struct usb_hub *hub) | |
4337 | { | |
4338 | struct usb_device *hdev = hub->hdev; | |
4339 | int remaining; | |
55c52718 | 4340 | int port1; |
1da177e4 | 4341 | |
55c52718 | 4342 | if (!hub->limited_power) |
1da177e4 LT |
4343 | return 0; |
4344 | ||
55c52718 AS |
4345 | remaining = hdev->bus_mA - hub->descriptor->bHubContrCurrent; |
4346 | for (port1 = 1; port1 <= hdev->maxchild; ++port1) { | |
ff823c79 | 4347 | struct usb_device *udev = hub->ports[port1 - 1]->child; |
55c52718 | 4348 | int delta; |
430ee58e | 4349 | unsigned unit_load; |
1da177e4 LT |
4350 | |
4351 | if (!udev) | |
4352 | continue; | |
430ee58e SAS |
4353 | if (hub_is_superspeed(udev)) |
4354 | unit_load = 150; | |
4355 | else | |
4356 | unit_load = 100; | |
1da177e4 | 4357 | |
430ee58e SAS |
4358 | /* |
4359 | * Unconfigured devices may not use more than one unit load, | |
4360 | * or 8mA for OTG ports | |
4361 | */ | |
1da177e4 | 4362 | if (udev->actconfig) |
8d8479db | 4363 | delta = usb_get_max_power(udev, udev->actconfig); |
55c52718 | 4364 | else if (port1 != udev->bus->otg_port || hdev->parent) |
430ee58e | 4365 | delta = unit_load; |
1da177e4 | 4366 | else |
55c52718 AS |
4367 | delta = 8; |
4368 | if (delta > hub->mA_per_port) | |
b9cef6c3 WF |
4369 | dev_warn(&udev->dev, |
4370 | "%dmA is over %umA budget for port %d!\n", | |
4371 | delta, hub->mA_per_port, port1); | |
1da177e4 LT |
4372 | remaining -= delta; |
4373 | } | |
4374 | if (remaining < 0) { | |
55c52718 AS |
4375 | dev_warn(hub->intfdev, "%dmA over power budget!\n", |
4376 | - remaining); | |
1da177e4 LT |
4377 | remaining = 0; |
4378 | } | |
4379 | return remaining; | |
4380 | } | |
4381 | ||
4382 | /* Handle physical or logical connection change events. | |
4383 | * This routine is called when: | |
4384 | * a port connection-change occurs; | |
4385 | * a port enable-change occurs (often caused by EMI); | |
742120c6 | 4386 | * usb_reset_and_verify_device() encounters changed descriptors (as from |
1da177e4 LT |
4387 | * a firmware download) |
4388 | * caller already locked the hub | |
4389 | */ | |
4390 | static void hub_port_connect_change(struct usb_hub *hub, int port1, | |
4391 | u16 portstatus, u16 portchange) | |
4392 | { | |
4393 | struct usb_device *hdev = hub->hdev; | |
4394 | struct device *hub_dev = hub->intfdev; | |
90da096e | 4395 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
24618b0c AS |
4396 | unsigned wHubCharacteristics = |
4397 | le16_to_cpu(hub->descriptor->wHubCharacteristics); | |
8808f00c | 4398 | struct usb_device *udev; |
1da177e4 | 4399 | int status, i; |
430ee58e | 4400 | unsigned unit_load; |
24618b0c | 4401 | |
1da177e4 LT |
4402 | dev_dbg (hub_dev, |
4403 | "port %d, status %04x, change %04x, %s\n", | |
131dec34 | 4404 | port1, portstatus, portchange, portspeed(hub, portstatus)); |
1da177e4 LT |
4405 | |
4406 | if (hub->has_indicators) { | |
4407 | set_port_led(hub, port1, HUB_LED_AUTO); | |
4408 | hub->indicator[port1-1] = INDICATOR_AUTO; | |
4409 | } | |
1da177e4 LT |
4410 | |
4411 | #ifdef CONFIG_USB_OTG | |
4412 | /* during HNP, don't repeat the debounce */ | |
4413 | if (hdev->bus->is_b_host) | |
24618b0c AS |
4414 | portchange &= ~(USB_PORT_STAT_C_CONNECTION | |
4415 | USB_PORT_STAT_C_ENABLE); | |
1da177e4 LT |
4416 | #endif |
4417 | ||
8808f00c | 4418 | /* Try to resuscitate an existing device */ |
ff823c79 | 4419 | udev = hub->ports[port1 - 1]->child; |
8808f00c AS |
4420 | if ((portstatus & USB_PORT_STAT_CONNECTION) && udev && |
4421 | udev->state != USB_STATE_NOTATTACHED) { | |
8808f00c AS |
4422 | usb_lock_device(udev); |
4423 | if (portstatus & USB_PORT_STAT_ENABLE) { | |
4424 | status = 0; /* Nothing to do */ | |
8808f00c | 4425 | |
84ebc102 | 4426 | #ifdef CONFIG_PM_RUNTIME |
5257d97a AS |
4427 | } else if (udev->state == USB_STATE_SUSPENDED && |
4428 | udev->persist_enabled) { | |
8808f00c AS |
4429 | /* For a suspended device, treat this as a |
4430 | * remote wakeup event. | |
4431 | */ | |
0534d468 | 4432 | status = usb_remote_wakeup(udev); |
8808f00c AS |
4433 | #endif |
4434 | ||
4435 | } else { | |
5257d97a | 4436 | status = -ENODEV; /* Don't resuscitate */ |
8808f00c AS |
4437 | } |
4438 | usb_unlock_device(udev); | |
4439 | ||
4440 | if (status == 0) { | |
4441 | clear_bit(port1, hub->change_bits); | |
4442 | return; | |
4443 | } | |
4444 | } | |
4445 | ||
24618b0c | 4446 | /* Disconnect any existing devices under this port */ |
b76baa81 PC |
4447 | if (udev) { |
4448 | if (hcd->phy && !hdev->parent && | |
4449 | !(portstatus & USB_PORT_STAT_CONNECTION)) | |
ac96511b | 4450 | usb_phy_notify_disconnect(hcd->phy, udev->speed); |
ff823c79 | 4451 | usb_disconnect(&hub->ports[port1 - 1]->child); |
b76baa81 | 4452 | } |
24618b0c AS |
4453 | clear_bit(port1, hub->change_bits); |
4454 | ||
253e0572 AS |
4455 | /* We can forget about a "removed" device when there's a physical |
4456 | * disconnect or the connect status changes. | |
4457 | */ | |
4458 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || | |
4459 | (portchange & USB_PORT_STAT_C_CONNECTION)) | |
4460 | clear_bit(port1, hub->removed_bits); | |
4461 | ||
5257d97a AS |
4462 | if (portchange & (USB_PORT_STAT_C_CONNECTION | |
4463 | USB_PORT_STAT_C_ENABLE)) { | |
ad493e5e | 4464 | status = hub_port_debounce_be_stable(hub, port1); |
5257d97a | 4465 | if (status < 0) { |
e9e88fb7 | 4466 | if (status != -ENODEV && printk_ratelimit()) |
5257d97a AS |
4467 | dev_err(hub_dev, "connect-debounce failed, " |
4468 | "port %d disabled\n", port1); | |
4469 | portstatus &= ~USB_PORT_STAT_CONNECTION; | |
4470 | } else { | |
4471 | portstatus = status; | |
4472 | } | |
4473 | } | |
4474 | ||
253e0572 AS |
4475 | /* Return now if debouncing failed or nothing is connected or |
4476 | * the device was "removed". | |
4477 | */ | |
4478 | if (!(portstatus & USB_PORT_STAT_CONNECTION) || | |
4479 | test_bit(port1, hub->removed_bits)) { | |
1da177e4 LT |
4480 | |
4481 | /* maybe switch power back on (e.g. root hub was reset) */ | |
74ad9bd2 | 4482 | if ((wHubCharacteristics & HUB_CHAR_LPSM) < 2 |
0ed9a57e | 4483 | && !port_is_power_on(hub, portstatus)) |
1da177e4 | 4484 | set_port_feature(hdev, port1, USB_PORT_FEAT_POWER); |
5257d97a | 4485 | |
1da177e4 LT |
4486 | if (portstatus & USB_PORT_STAT_ENABLE) |
4487 | goto done; | |
4488 | return; | |
4489 | } | |
430ee58e SAS |
4490 | if (hub_is_superspeed(hub->hdev)) |
4491 | unit_load = 150; | |
4492 | else | |
4493 | unit_load = 100; | |
1da177e4 | 4494 | |
e9e88fb7 | 4495 | status = 0; |
1da177e4 | 4496 | for (i = 0; i < SET_CONFIG_TRIES; i++) { |
1da177e4 LT |
4497 | |
4498 | /* reallocate for each attempt, since references | |
4499 | * to the previous one can escape in various ways | |
4500 | */ | |
4501 | udev = usb_alloc_dev(hdev, hdev->bus, port1); | |
4502 | if (!udev) { | |
4503 | dev_err (hub_dev, | |
4504 | "couldn't allocate port %d usb_device\n", | |
4505 | port1); | |
4506 | goto done; | |
4507 | } | |
4508 | ||
4509 | usb_set_device_state(udev, USB_STATE_POWERED); | |
55c52718 | 4510 | udev->bus_mA = hub->mA_per_port; |
b6956ffa | 4511 | udev->level = hdev->level + 1; |
8af548dc | 4512 | udev->wusb = hub_is_wusb(hub); |
55c52718 | 4513 | |
131dec34 SS |
4514 | /* Only USB 3.0 devices are connected to SuperSpeed hubs. */ |
4515 | if (hub_is_superspeed(hub->hdev)) | |
e7b77172 SS |
4516 | udev->speed = USB_SPEED_SUPER; |
4517 | else | |
4518 | udev->speed = USB_SPEED_UNKNOWN; | |
4519 | ||
3b29b68b | 4520 | choose_devnum(udev); |
c8d4af8e AX |
4521 | if (udev->devnum <= 0) { |
4522 | status = -ENOTCONN; /* Don't retry */ | |
4523 | goto loop; | |
c6515272 SS |
4524 | } |
4525 | ||
e7b77172 | 4526 | /* reset (non-USB 3.0 devices) and get descriptor */ |
1da177e4 LT |
4527 | status = hub_port_init(hub, udev, port1, i); |
4528 | if (status < 0) | |
4529 | goto loop; | |
4530 | ||
93362a87 PD |
4531 | usb_detect_quirks(udev); |
4532 | if (udev->quirks & USB_QUIRK_DELAY_INIT) | |
4533 | msleep(1000); | |
4534 | ||
1da177e4 LT |
4535 | /* consecutive bus-powered hubs aren't reliable; they can |
4536 | * violate the voltage drop budget. if the new child has | |
4537 | * a "powered" LED, users should notice we didn't enable it | |
4538 | * (without reading syslog), even without per-port LEDs | |
4539 | * on the parent. | |
4540 | */ | |
4541 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB | |
430ee58e | 4542 | && udev->bus_mA <= unit_load) { |
1da177e4 LT |
4543 | u16 devstat; |
4544 | ||
4545 | status = usb_get_status(udev, USB_RECIP_DEVICE, 0, | |
4546 | &devstat); | |
55c52718 | 4547 | if (status < 2) { |
1da177e4 LT |
4548 | dev_dbg(&udev->dev, "get status %d ?\n", status); |
4549 | goto loop_disable; | |
4550 | } | |
55c52718 | 4551 | le16_to_cpus(&devstat); |
1da177e4 LT |
4552 | if ((devstat & (1 << USB_DEVICE_SELF_POWERED)) == 0) { |
4553 | dev_err(&udev->dev, | |
4554 | "can't connect bus-powered hub " | |
4555 | "to this port\n"); | |
4556 | if (hub->has_indicators) { | |
4557 | hub->indicator[port1-1] = | |
4558 | INDICATOR_AMBER_BLINK; | |
c4028958 | 4559 | schedule_delayed_work (&hub->leds, 0); |
1da177e4 LT |
4560 | } |
4561 | status = -ENOTCONN; /* Don't retry */ | |
4562 | goto loop_disable; | |
4563 | } | |
4564 | } | |
4565 | ||
4566 | /* check for devices running slower than they could */ | |
4567 | if (le16_to_cpu(udev->descriptor.bcdUSB) >= 0x0200 | |
4568 | && udev->speed == USB_SPEED_FULL | |
4569 | && highspeed_hubs != 0) | |
4570 | check_highspeed (hub, udev, port1); | |
4571 | ||
fa286188 | 4572 | /* Store the parent's children[] pointer. At this point |
1da177e4 LT |
4573 | * udev becomes globally accessible, although presumably |
4574 | * no one will look at it until hdev is unlocked. | |
4575 | */ | |
1da177e4 LT |
4576 | status = 0; |
4577 | ||
4578 | /* We mustn't add new devices if the parent hub has | |
4579 | * been disconnected; we would race with the | |
4580 | * recursively_mark_NOTATTACHED() routine. | |
4581 | */ | |
4582 | spin_lock_irq(&device_state_lock); | |
4583 | if (hdev->state == USB_STATE_NOTATTACHED) | |
4584 | status = -ENOTCONN; | |
4585 | else | |
ff823c79 | 4586 | hub->ports[port1 - 1]->child = udev; |
1da177e4 LT |
4587 | spin_unlock_irq(&device_state_lock); |
4588 | ||
4589 | /* Run it through the hoops (find a driver, etc) */ | |
4590 | if (!status) { | |
4591 | status = usb_new_device(udev); | |
4592 | if (status) { | |
4593 | spin_lock_irq(&device_state_lock); | |
ff823c79 | 4594 | hub->ports[port1 - 1]->child = NULL; |
1da177e4 LT |
4595 | spin_unlock_irq(&device_state_lock); |
4596 | } | |
4597 | } | |
4598 | ||
1da177e4 LT |
4599 | if (status) |
4600 | goto loop_disable; | |
4601 | ||
4602 | status = hub_power_remaining(hub); | |
4603 | if (status) | |
55c52718 | 4604 | dev_dbg(hub_dev, "%dmA power budget left\n", status); |
1da177e4 LT |
4605 | |
4606 | return; | |
4607 | ||
4608 | loop_disable: | |
4609 | hub_port_disable(hub, port1, 1); | |
4610 | loop: | |
fc721f51 | 4611 | usb_ep0_reinit(udev); |
3b29b68b | 4612 | release_devnum(udev); |
f7410ced | 4613 | hub_free_dev(udev); |
1da177e4 | 4614 | usb_put_dev(udev); |
ffcdc18d | 4615 | if ((status == -ENOTCONN) || (status == -ENOTSUPP)) |
1da177e4 LT |
4616 | break; |
4617 | } | |
3a31155c AS |
4618 | if (hub->hdev->parent || |
4619 | !hcd->driver->port_handed_over || | |
e9e88fb7 AS |
4620 | !(hcd->driver->port_handed_over)(hcd, port1)) { |
4621 | if (status != -ENOTCONN && status != -ENODEV) | |
4622 | dev_err(hub_dev, "unable to enumerate USB device on port %d\n", | |
4623 | port1); | |
4624 | } | |
1da177e4 LT |
4625 | |
4626 | done: | |
4627 | hub_port_disable(hub, port1, 1); | |
90da096e BR |
4628 | if (hcd->driver->relinquish_port && !hub->hdev->parent) |
4629 | hcd->driver->relinquish_port(hcd, port1); | |
1da177e4 LT |
4630 | } |
4631 | ||
714b07be SS |
4632 | /* Returns 1 if there was a remote wakeup and a connect status change. */ |
4633 | static int hub_handle_remote_wakeup(struct usb_hub *hub, unsigned int port, | |
72937e1e | 4634 | u16 portstatus, u16 portchange) |
714b07be SS |
4635 | { |
4636 | struct usb_device *hdev; | |
4637 | struct usb_device *udev; | |
4638 | int connect_change = 0; | |
4639 | int ret; | |
4640 | ||
4641 | hdev = hub->hdev; | |
ff823c79 | 4642 | udev = hub->ports[port - 1]->child; |
4ee823b8 SS |
4643 | if (!hub_is_superspeed(hdev)) { |
4644 | if (!(portchange & USB_PORT_STAT_C_SUSPEND)) | |
4645 | return 0; | |
ad493e5e | 4646 | usb_clear_port_feature(hdev, port, USB_PORT_FEAT_C_SUSPEND); |
4ee823b8 SS |
4647 | } else { |
4648 | if (!udev || udev->state != USB_STATE_SUSPENDED || | |
72937e1e SS |
4649 | (portstatus & USB_PORT_STAT_LINK_STATE) != |
4650 | USB_SS_PORT_LS_U0) | |
4ee823b8 SS |
4651 | return 0; |
4652 | } | |
4653 | ||
714b07be SS |
4654 | if (udev) { |
4655 | /* TRSMRCY = 10 msec */ | |
4656 | msleep(10); | |
4657 | ||
4658 | usb_lock_device(udev); | |
4659 | ret = usb_remote_wakeup(udev); | |
4660 | usb_unlock_device(udev); | |
4661 | if (ret < 0) | |
4662 | connect_change = 1; | |
4663 | } else { | |
4664 | ret = -ENODEV; | |
4665 | hub_port_disable(hub, port, 1); | |
4666 | } | |
4667 | dev_dbg(hub->intfdev, "resume on port %d, status %d\n", | |
4668 | port, ret); | |
4669 | return connect_change; | |
4670 | } | |
4671 | ||
1da177e4 LT |
4672 | static void hub_events(void) |
4673 | { | |
4674 | struct list_head *tmp; | |
4675 | struct usb_device *hdev; | |
4676 | struct usb_interface *intf; | |
4677 | struct usb_hub *hub; | |
4678 | struct device *hub_dev; | |
4679 | u16 hubstatus; | |
4680 | u16 hubchange; | |
4681 | u16 portstatus; | |
4682 | u16 portchange; | |
4683 | int i, ret; | |
4ee823b8 | 4684 | int connect_change, wakeup_change; |
1da177e4 LT |
4685 | |
4686 | /* | |
4687 | * We restart the list every time to avoid a deadlock with | |
4688 | * deleting hubs downstream from this one. This should be | |
4689 | * safe since we delete the hub from the event list. | |
4690 | * Not the most efficient, but avoids deadlocks. | |
4691 | */ | |
4692 | while (1) { | |
4693 | ||
4694 | /* Grab the first entry at the beginning of the list */ | |
4695 | spin_lock_irq(&hub_event_lock); | |
4696 | if (list_empty(&hub_event_list)) { | |
4697 | spin_unlock_irq(&hub_event_lock); | |
4698 | break; | |
4699 | } | |
4700 | ||
4701 | tmp = hub_event_list.next; | |
4702 | list_del_init(tmp); | |
4703 | ||
4704 | hub = list_entry(tmp, struct usb_hub, event_list); | |
e8054854 | 4705 | kref_get(&hub->kref); |
14200ead JL |
4706 | hdev = hub->hdev; |
4707 | usb_get_dev(hdev); | |
e8054854 | 4708 | spin_unlock_irq(&hub_event_lock); |
1da177e4 | 4709 | |
e8054854 AS |
4710 | hub_dev = hub->intfdev; |
4711 | intf = to_usb_interface(hub_dev); | |
40f122f3 | 4712 | dev_dbg(hub_dev, "state %d ports %d chg %04x evt %04x\n", |
1da177e4 LT |
4713 | hdev->state, hub->descriptor |
4714 | ? hub->descriptor->bNbrPorts | |
4715 | : 0, | |
4716 | /* NOTE: expects max 15 ports... */ | |
4717 | (u16) hub->change_bits[0], | |
40f122f3 | 4718 | (u16) hub->event_bits[0]); |
1da177e4 | 4719 | |
1da177e4 LT |
4720 | /* Lock the device, then check to see if we were |
4721 | * disconnected while waiting for the lock to succeed. */ | |
06b84e8a | 4722 | usb_lock_device(hdev); |
e8054854 | 4723 | if (unlikely(hub->disconnected)) |
9bbdf1e0 | 4724 | goto loop_disconnected; |
1da177e4 LT |
4725 | |
4726 | /* If the hub has died, clean up after it */ | |
4727 | if (hdev->state == USB_STATE_NOTATTACHED) { | |
7de18d8b | 4728 | hub->error = -ENODEV; |
4330354f | 4729 | hub_quiesce(hub, HUB_DISCONNECT); |
1da177e4 LT |
4730 | goto loop; |
4731 | } | |
4732 | ||
40f122f3 AS |
4733 | /* Autoresume */ |
4734 | ret = usb_autopm_get_interface(intf); | |
4735 | if (ret) { | |
4736 | dev_dbg(hub_dev, "Can't autoresume: %d\n", ret); | |
4737 | goto loop; | |
4738 | } | |
a8e7c565 | 4739 | |
40f122f3 | 4740 | /* If this is an inactive hub, do nothing */ |
1da177e4 | 4741 | if (hub->quiescing) |
40f122f3 | 4742 | goto loop_autopm; |
1da177e4 LT |
4743 | |
4744 | if (hub->error) { | |
4745 | dev_dbg (hub_dev, "resetting for error %d\n", | |
4746 | hub->error); | |
4747 | ||
742120c6 | 4748 | ret = usb_reset_device(hdev); |
1da177e4 LT |
4749 | if (ret) { |
4750 | dev_dbg (hub_dev, | |
4751 | "error resetting hub: %d\n", ret); | |
40f122f3 | 4752 | goto loop_autopm; |
1da177e4 LT |
4753 | } |
4754 | ||
4755 | hub->nerrors = 0; | |
4756 | hub->error = 0; | |
4757 | } | |
4758 | ||
4759 | /* deal with port status changes */ | |
4760 | for (i = 1; i <= hub->descriptor->bNbrPorts; i++) { | |
4761 | if (test_bit(i, hub->busy_bits)) | |
4762 | continue; | |
4763 | connect_change = test_bit(i, hub->change_bits); | |
72937e1e | 4764 | wakeup_change = test_and_clear_bit(i, hub->wakeup_bits); |
1da177e4 | 4765 | if (!test_and_clear_bit(i, hub->event_bits) && |
4ee823b8 | 4766 | !connect_change && !wakeup_change) |
1da177e4 LT |
4767 | continue; |
4768 | ||
4769 | ret = hub_port_status(hub, i, | |
4770 | &portstatus, &portchange); | |
4771 | if (ret < 0) | |
4772 | continue; | |
4773 | ||
1da177e4 | 4774 | if (portchange & USB_PORT_STAT_C_CONNECTION) { |
ad493e5e | 4775 | usb_clear_port_feature(hdev, i, |
1da177e4 LT |
4776 | USB_PORT_FEAT_C_CONNECTION); |
4777 | connect_change = 1; | |
4778 | } | |
4779 | ||
4780 | if (portchange & USB_PORT_STAT_C_ENABLE) { | |
4781 | if (!connect_change) | |
4782 | dev_dbg (hub_dev, | |
4783 | "port %d enable change, " | |
4784 | "status %08x\n", | |
4785 | i, portstatus); | |
ad493e5e | 4786 | usb_clear_port_feature(hdev, i, |
1da177e4 LT |
4787 | USB_PORT_FEAT_C_ENABLE); |
4788 | ||
4789 | /* | |
4790 | * EM interference sometimes causes badly | |
4791 | * shielded USB devices to be shutdown by | |
4792 | * the hub, this hack enables them again. | |
4793 | * Works at least with mouse driver. | |
4794 | */ | |
4795 | if (!(portstatus & USB_PORT_STAT_ENABLE) | |
4796 | && !connect_change | |
ff823c79 | 4797 | && hub->ports[i - 1]->child) { |
1da177e4 LT |
4798 | dev_err (hub_dev, |
4799 | "port %i " | |
4800 | "disabled by hub (EMI?), " | |
4801 | "re-enabling...\n", | |
4802 | i); | |
4803 | connect_change = 1; | |
4804 | } | |
4805 | } | |
4806 | ||
72937e1e SS |
4807 | if (hub_handle_remote_wakeup(hub, i, |
4808 | portstatus, portchange)) | |
714b07be | 4809 | connect_change = 1; |
8808f00c | 4810 | |
1da177e4 | 4811 | if (portchange & USB_PORT_STAT_C_OVERCURRENT) { |
752d57a8 PB |
4812 | u16 status = 0; |
4813 | u16 unused; | |
4814 | ||
4815 | dev_dbg(hub_dev, "over-current change on port " | |
4816 | "%d\n", i); | |
ad493e5e | 4817 | usb_clear_port_feature(hdev, i, |
1da177e4 | 4818 | USB_PORT_FEAT_C_OVER_CURRENT); |
752d57a8 | 4819 | msleep(100); /* Cool down */ |
8520f380 | 4820 | hub_power_on(hub, true); |
752d57a8 PB |
4821 | hub_port_status(hub, i, &status, &unused); |
4822 | if (status & USB_PORT_STAT_OVERCURRENT) | |
4823 | dev_err(hub_dev, "over-current " | |
4824 | "condition on port %d\n", i); | |
1da177e4 LT |
4825 | } |
4826 | ||
4827 | if (portchange & USB_PORT_STAT_C_RESET) { | |
4828 | dev_dbg (hub_dev, | |
4829 | "reset change on port %d\n", | |
4830 | i); | |
ad493e5e | 4831 | usb_clear_port_feature(hdev, i, |
1da177e4 LT |
4832 | USB_PORT_FEAT_C_RESET); |
4833 | } | |
c7061574 SS |
4834 | if ((portchange & USB_PORT_STAT_C_BH_RESET) && |
4835 | hub_is_superspeed(hub->hdev)) { | |
4836 | dev_dbg(hub_dev, | |
4837 | "warm reset change on port %d\n", | |
4838 | i); | |
ad493e5e | 4839 | usb_clear_port_feature(hdev, i, |
c7061574 SS |
4840 | USB_PORT_FEAT_C_BH_PORT_RESET); |
4841 | } | |
dbe79bbe | 4842 | if (portchange & USB_PORT_STAT_C_LINK_STATE) { |
ad493e5e | 4843 | usb_clear_port_feature(hub->hdev, i, |
dbe79bbe JY |
4844 | USB_PORT_FEAT_C_PORT_LINK_STATE); |
4845 | } | |
4846 | if (portchange & USB_PORT_STAT_C_CONFIG_ERROR) { | |
4847 | dev_warn(hub_dev, | |
4848 | "config error on port %d\n", | |
4849 | i); | |
ad493e5e | 4850 | usb_clear_port_feature(hub->hdev, i, |
dbe79bbe JY |
4851 | USB_PORT_FEAT_C_PORT_CONFIG_ERROR); |
4852 | } | |
1da177e4 | 4853 | |
5e467f6e AX |
4854 | /* Warm reset a USB3 protocol port if it's in |
4855 | * SS.Inactive state. | |
4856 | */ | |
8bea2bd3 | 4857 | if (hub_port_warm_reset_required(hub, portstatus)) { |
65bdac5e | 4858 | int status; |
d3b9d7a9 SS |
4859 | struct usb_device *udev = |
4860 | hub->ports[i - 1]->child; | |
65bdac5e | 4861 | |
5e467f6e | 4862 | dev_dbg(hub_dev, "warm reset port %d\n", i); |
9126f1f5 JW |
4863 | if (!udev || |
4864 | !(portstatus & USB_PORT_STAT_CONNECTION) || | |
4865 | udev->state == USB_STATE_NOTATTACHED) { | |
d3b9d7a9 SS |
4866 | status = hub_port_reset(hub, i, |
4867 | NULL, HUB_BH_RESET_TIME, | |
4868 | true); | |
4869 | if (status < 0) | |
4870 | hub_port_disable(hub, i, 1); | |
4871 | } else { | |
4872 | usb_lock_device(udev); | |
4873 | status = usb_reset_device(udev); | |
4874 | usb_unlock_device(udev); | |
6b9cb24d | 4875 | connect_change = 0; |
d3b9d7a9 | 4876 | } |
5e467f6e AX |
4877 | } |
4878 | ||
1da177e4 LT |
4879 | if (connect_change) |
4880 | hub_port_connect_change(hub, i, | |
4881 | portstatus, portchange); | |
4882 | } /* end for i */ | |
4883 | ||
4884 | /* deal with hub status changes */ | |
4885 | if (test_and_clear_bit(0, hub->event_bits) == 0) | |
4886 | ; /* do nothing */ | |
4887 | else if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) | |
4888 | dev_err (hub_dev, "get_hub_status failed\n"); | |
4889 | else { | |
4890 | if (hubchange & HUB_CHANGE_LOCAL_POWER) { | |
4891 | dev_dbg (hub_dev, "power change\n"); | |
4892 | clear_hub_feature(hdev, C_HUB_LOCAL_POWER); | |
55c52718 AS |
4893 | if (hubstatus & HUB_STATUS_LOCAL_POWER) |
4894 | /* FIXME: Is this always true? */ | |
55c52718 | 4895 | hub->limited_power = 1; |
403fae78 | 4896 | else |
4897 | hub->limited_power = 0; | |
1da177e4 LT |
4898 | } |
4899 | if (hubchange & HUB_CHANGE_OVERCURRENT) { | |
752d57a8 PB |
4900 | u16 status = 0; |
4901 | u16 unused; | |
4902 | ||
4903 | dev_dbg(hub_dev, "over-current change\n"); | |
1da177e4 | 4904 | clear_hub_feature(hdev, C_HUB_OVER_CURRENT); |
752d57a8 | 4905 | msleep(500); /* Cool down */ |
8520f380 | 4906 | hub_power_on(hub, true); |
752d57a8 PB |
4907 | hub_hub_status(hub, &status, &unused); |
4908 | if (status & HUB_STATUS_OVERCURRENT) | |
4909 | dev_err(hub_dev, "over-current " | |
4910 | "condition\n"); | |
1da177e4 LT |
4911 | } |
4912 | } | |
4913 | ||
8e4ceb38 AS |
4914 | loop_autopm: |
4915 | /* Balance the usb_autopm_get_interface() above */ | |
4916 | usb_autopm_put_interface_no_suspend(intf); | |
4917 | loop: | |
4918 | /* Balance the usb_autopm_get_interface_no_resume() in | |
4919 | * kick_khubd() and allow autosuspend. | |
4920 | */ | |
4921 | usb_autopm_put_interface(intf); | |
9bbdf1e0 | 4922 | loop_disconnected: |
1da177e4 | 4923 | usb_unlock_device(hdev); |
14200ead | 4924 | usb_put_dev(hdev); |
e8054854 | 4925 | kref_put(&hub->kref, hub_release); |
1da177e4 LT |
4926 | |
4927 | } /* end while (1) */ | |
4928 | } | |
4929 | ||
4930 | static int hub_thread(void *__unused) | |
4931 | { | |
3bb1af52 AS |
4932 | /* khubd needs to be freezable to avoid intefering with USB-PERSIST |
4933 | * port handover. Otherwise it might see that a full-speed device | |
4934 | * was gone before the EHCI controller had handed its port over to | |
4935 | * the companion full-speed controller. | |
4936 | */ | |
83144186 | 4937 | set_freezable(); |
3bb1af52 | 4938 | |
1da177e4 LT |
4939 | do { |
4940 | hub_events(); | |
e42837bc | 4941 | wait_event_freezable(khubd_wait, |
9c8d6178 AM |
4942 | !list_empty(&hub_event_list) || |
4943 | kthread_should_stop()); | |
9c8d6178 | 4944 | } while (!kthread_should_stop() || !list_empty(&hub_event_list)); |
1da177e4 | 4945 | |
9c8d6178 AM |
4946 | pr_debug("%s: khubd exiting\n", usbcore_name); |
4947 | return 0; | |
1da177e4 LT |
4948 | } |
4949 | ||
1e927d96 | 4950 | static const struct usb_device_id hub_id_table[] = { |
e6f30dea ML |
4951 | { .match_flags = USB_DEVICE_ID_MATCH_VENDOR |
4952 | | USB_DEVICE_ID_MATCH_INT_CLASS, | |
4953 | .idVendor = USB_VENDOR_GENESYS_LOGIC, | |
4954 | .bInterfaceClass = USB_CLASS_HUB, | |
4955 | .driver_info = HUB_QUIRK_CHECK_PORT_AUTOSUSPEND}, | |
1da177e4 LT |
4956 | { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS, |
4957 | .bDeviceClass = USB_CLASS_HUB}, | |
4958 | { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS, | |
4959 | .bInterfaceClass = USB_CLASS_HUB}, | |
4960 | { } /* Terminating entry */ | |
4961 | }; | |
4962 | ||
4963 | MODULE_DEVICE_TABLE (usb, hub_id_table); | |
4964 | ||
4965 | static struct usb_driver hub_driver = { | |
1da177e4 LT |
4966 | .name = "hub", |
4967 | .probe = hub_probe, | |
4968 | .disconnect = hub_disconnect, | |
4969 | .suspend = hub_suspend, | |
4970 | .resume = hub_resume, | |
f07600cf | 4971 | .reset_resume = hub_reset_resume, |
7de18d8b AS |
4972 | .pre_reset = hub_pre_reset, |
4973 | .post_reset = hub_post_reset, | |
c532b29a | 4974 | .unlocked_ioctl = hub_ioctl, |
1da177e4 | 4975 | .id_table = hub_id_table, |
40f122f3 | 4976 | .supports_autosuspend = 1, |
1da177e4 LT |
4977 | }; |
4978 | ||
4979 | int usb_hub_init(void) | |
4980 | { | |
1da177e4 LT |
4981 | if (usb_register(&hub_driver) < 0) { |
4982 | printk(KERN_ERR "%s: can't register hub driver\n", | |
4983 | usbcore_name); | |
4984 | return -1; | |
4985 | } | |
4986 | ||
9c8d6178 AM |
4987 | khubd_task = kthread_run(hub_thread, NULL, "khubd"); |
4988 | if (!IS_ERR(khubd_task)) | |
1da177e4 | 4989 | return 0; |
1da177e4 LT |
4990 | |
4991 | /* Fall through if kernel_thread failed */ | |
4992 | usb_deregister(&hub_driver); | |
4993 | printk(KERN_ERR "%s: can't start khubd\n", usbcore_name); | |
4994 | ||
4995 | return -1; | |
4996 | } | |
4997 | ||
4998 | void usb_hub_cleanup(void) | |
4999 | { | |
9c8d6178 | 5000 | kthread_stop(khubd_task); |
1da177e4 LT |
5001 | |
5002 | /* | |
5003 | * Hub resources are freed for us by usb_deregister. It calls | |
5004 | * usb_driver_purge on every device which in turn calls that | |
5005 | * devices disconnect function if it is using this driver. | |
5006 | * The hub_disconnect function takes care of releasing the | |
5007 | * individual hub resources. -greg | |
5008 | */ | |
5009 | usb_deregister(&hub_driver); | |
5010 | } /* usb_hub_cleanup() */ | |
5011 | ||
eb764c4b AS |
5012 | static int descriptors_changed(struct usb_device *udev, |
5013 | struct usb_device_descriptor *old_device_descriptor) | |
5014 | { | |
5015 | int changed = 0; | |
5016 | unsigned index; | |
5017 | unsigned serial_len = 0; | |
5018 | unsigned len; | |
5019 | unsigned old_length; | |
5020 | int length; | |
5021 | char *buf; | |
5022 | ||
5023 | if (memcmp(&udev->descriptor, old_device_descriptor, | |
5024 | sizeof(*old_device_descriptor)) != 0) | |
5025 | return 1; | |
5026 | ||
5027 | /* Since the idVendor, idProduct, and bcdDevice values in the | |
5028 | * device descriptor haven't changed, we will assume the | |
5029 | * Manufacturer and Product strings haven't changed either. | |
5030 | * But the SerialNumber string could be different (e.g., a | |
5031 | * different flash card of the same brand). | |
5032 | */ | |
5033 | if (udev->serial) | |
5034 | serial_len = strlen(udev->serial) + 1; | |
1da177e4 | 5035 | |
eb764c4b | 5036 | len = serial_len; |
1da177e4 | 5037 | for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { |
eb764c4b AS |
5038 | old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
5039 | len = max(len, old_length); | |
1da177e4 | 5040 | } |
eb764c4b | 5041 | |
0cc1a51f | 5042 | buf = kmalloc(len, GFP_NOIO); |
1da177e4 LT |
5043 | if (buf == NULL) { |
5044 | dev_err(&udev->dev, "no mem to re-read configs after reset\n"); | |
5045 | /* assume the worst */ | |
5046 | return 1; | |
5047 | } | |
5048 | for (index = 0; index < udev->descriptor.bNumConfigurations; index++) { | |
eb764c4b | 5049 | old_length = le16_to_cpu(udev->config[index].desc.wTotalLength); |
1da177e4 LT |
5050 | length = usb_get_descriptor(udev, USB_DT_CONFIG, index, buf, |
5051 | old_length); | |
eb764c4b | 5052 | if (length != old_length) { |
1da177e4 LT |
5053 | dev_dbg(&udev->dev, "config index %d, error %d\n", |
5054 | index, length); | |
eb764c4b | 5055 | changed = 1; |
1da177e4 LT |
5056 | break; |
5057 | } | |
5058 | if (memcmp (buf, udev->rawdescriptors[index], old_length) | |
5059 | != 0) { | |
5060 | dev_dbg(&udev->dev, "config index %d changed (#%d)\n", | |
eb764c4b AS |
5061 | index, |
5062 | ((struct usb_config_descriptor *) buf)-> | |
5063 | bConfigurationValue); | |
5064 | changed = 1; | |
1da177e4 LT |
5065 | break; |
5066 | } | |
5067 | } | |
eb764c4b AS |
5068 | |
5069 | if (!changed && serial_len) { | |
5070 | length = usb_string(udev, udev->descriptor.iSerialNumber, | |
5071 | buf, serial_len); | |
5072 | if (length + 1 != serial_len) { | |
5073 | dev_dbg(&udev->dev, "serial string error %d\n", | |
5074 | length); | |
5075 | changed = 1; | |
5076 | } else if (memcmp(buf, udev->serial, length) != 0) { | |
5077 | dev_dbg(&udev->dev, "serial string changed\n"); | |
5078 | changed = 1; | |
5079 | } | |
5080 | } | |
5081 | ||
1da177e4 | 5082 | kfree(buf); |
eb764c4b | 5083 | return changed; |
1da177e4 LT |
5084 | } |
5085 | ||
5086 | /** | |
742120c6 | 5087 | * usb_reset_and_verify_device - perform a USB port reset to reinitialize a device |
1da177e4 LT |
5088 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
5089 | * | |
79efa097 AS |
5090 | * WARNING - don't use this routine to reset a composite device |
5091 | * (one with multiple interfaces owned by separate drivers)! | |
742120c6 | 5092 | * Use usb_reset_device() instead. |
1da177e4 LT |
5093 | * |
5094 | * Do a port reset, reassign the device's address, and establish its | |
5095 | * former operating configuration. If the reset fails, or the device's | |
5096 | * descriptors change from their values before the reset, or the original | |
5097 | * configuration and altsettings cannot be restored, a flag will be set | |
5098 | * telling khubd to pretend the device has been disconnected and then | |
5099 | * re-connected. All drivers will be unbound, and the device will be | |
5100 | * re-enumerated and probed all over again. | |
5101 | * | |
5102 | * Returns 0 if the reset succeeded, -ENODEV if the device has been | |
5103 | * flagged for logical disconnection, or some other negative error code | |
5104 | * if the reset wasn't even attempted. | |
5105 | * | |
5106 | * The caller must own the device lock. For example, it's safe to use | |
5107 | * this from a driver probe() routine after downloading new firmware. | |
5108 | * For calls that might not occur during probe(), drivers should lock | |
5109 | * the device using usb_lock_device_for_reset(). | |
6bc6cff5 AS |
5110 | * |
5111 | * Locking exception: This routine may also be called from within an | |
5112 | * autoresume handler. Such usage won't conflict with other tasks | |
5113 | * holding the device lock because these tasks should always call | |
5114 | * usb_autopm_resume_device(), thereby preventing any unwanted autoresume. | |
1da177e4 | 5115 | */ |
742120c6 | 5116 | static int usb_reset_and_verify_device(struct usb_device *udev) |
1da177e4 LT |
5117 | { |
5118 | struct usb_device *parent_hdev = udev->parent; | |
5119 | struct usb_hub *parent_hub; | |
3f0479e0 | 5120 | struct usb_hcd *hcd = bus_to_hcd(udev->bus); |
1da177e4 | 5121 | struct usb_device_descriptor descriptor = udev->descriptor; |
12c3da34 AS |
5122 | int i, ret = 0; |
5123 | int port1 = udev->portnum; | |
1da177e4 LT |
5124 | |
5125 | if (udev->state == USB_STATE_NOTATTACHED || | |
5126 | udev->state == USB_STATE_SUSPENDED) { | |
5127 | dev_dbg(&udev->dev, "device reset not allowed in state %d\n", | |
5128 | udev->state); | |
5129 | return -EINVAL; | |
5130 | } | |
5131 | ||
5132 | if (!parent_hdev) { | |
4bec9917 | 5133 | /* this requires hcd-specific logic; see ohci_restart() */ |
441b62c1 | 5134 | dev_dbg(&udev->dev, "%s for root hub!\n", __func__); |
1da177e4 LT |
5135 | return -EISDIR; |
5136 | } | |
ad493e5e | 5137 | parent_hub = usb_hub_to_struct_hub(parent_hdev); |
1da177e4 | 5138 | |
f74631e3 SS |
5139 | /* Disable LPM and LTM while we reset the device and reinstall the alt |
5140 | * settings. Device-initiated LPM settings, and system exit latency | |
5141 | * settings are cleared when the device is reset, so we have to set | |
5142 | * them up again. | |
6d1d0513 SS |
5143 | */ |
5144 | ret = usb_unlocked_disable_lpm(udev); | |
5145 | if (ret) { | |
5146 | dev_err(&udev->dev, "%s Failed to disable LPM\n.", __func__); | |
5147 | goto re_enumerate; | |
5148 | } | |
f74631e3 SS |
5149 | ret = usb_disable_ltm(udev); |
5150 | if (ret) { | |
5151 | dev_err(&udev->dev, "%s Failed to disable LTM\n.", | |
5152 | __func__); | |
5153 | goto re_enumerate; | |
5154 | } | |
6d1d0513 | 5155 | |
1da177e4 LT |
5156 | set_bit(port1, parent_hub->busy_bits); |
5157 | for (i = 0; i < SET_CONFIG_TRIES; ++i) { | |
5158 | ||
5159 | /* ep0 maxpacket size may change; let the HCD know about it. | |
5160 | * Other endpoints will be handled by re-enumeration. */ | |
fc721f51 | 5161 | usb_ep0_reinit(udev); |
1da177e4 | 5162 | ret = hub_port_init(parent_hub, udev, port1, i); |
dd4dd19e | 5163 | if (ret >= 0 || ret == -ENOTCONN || ret == -ENODEV) |
1da177e4 LT |
5164 | break; |
5165 | } | |
5166 | clear_bit(port1, parent_hub->busy_bits); | |
d5cbad4b | 5167 | |
1da177e4 LT |
5168 | if (ret < 0) |
5169 | goto re_enumerate; | |
5170 | ||
5171 | /* Device might have changed firmware (DFU or similar) */ | |
eb764c4b | 5172 | if (descriptors_changed(udev, &descriptor)) { |
1da177e4 LT |
5173 | dev_info(&udev->dev, "device firmware changed\n"); |
5174 | udev->descriptor = descriptor; /* for disconnect() calls */ | |
5175 | goto re_enumerate; | |
5176 | } | |
4fe0387a AS |
5177 | |
5178 | /* Restore the device's previous configuration */ | |
1da177e4 LT |
5179 | if (!udev->actconfig) |
5180 | goto done; | |
3f0479e0 | 5181 | |
d673bfcb | 5182 | mutex_lock(hcd->bandwidth_mutex); |
3f0479e0 SS |
5183 | ret = usb_hcd_alloc_bandwidth(udev, udev->actconfig, NULL, NULL); |
5184 | if (ret < 0) { | |
5185 | dev_warn(&udev->dev, | |
5186 | "Busted HC? Not enough HCD resources for " | |
5187 | "old configuration.\n"); | |
d673bfcb | 5188 | mutex_unlock(hcd->bandwidth_mutex); |
3f0479e0 SS |
5189 | goto re_enumerate; |
5190 | } | |
1da177e4 LT |
5191 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
5192 | USB_REQ_SET_CONFIGURATION, 0, | |
5193 | udev->actconfig->desc.bConfigurationValue, 0, | |
5194 | NULL, 0, USB_CTRL_SET_TIMEOUT); | |
5195 | if (ret < 0) { | |
5196 | dev_err(&udev->dev, | |
5197 | "can't restore configuration #%d (error=%d)\n", | |
5198 | udev->actconfig->desc.bConfigurationValue, ret); | |
d673bfcb | 5199 | mutex_unlock(hcd->bandwidth_mutex); |
1da177e4 LT |
5200 | goto re_enumerate; |
5201 | } | |
d673bfcb | 5202 | mutex_unlock(hcd->bandwidth_mutex); |
1da177e4 LT |
5203 | usb_set_device_state(udev, USB_STATE_CONFIGURED); |
5204 | ||
4fe0387a AS |
5205 | /* Put interfaces back into the same altsettings as before. |
5206 | * Don't bother to send the Set-Interface request for interfaces | |
5207 | * that were already in altsetting 0; besides being unnecessary, | |
5208 | * many devices can't handle it. Instead just reset the host-side | |
5209 | * endpoint state. | |
5210 | */ | |
1da177e4 | 5211 | for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { |
3f0479e0 SS |
5212 | struct usb_host_config *config = udev->actconfig; |
5213 | struct usb_interface *intf = config->interface[i]; | |
1da177e4 LT |
5214 | struct usb_interface_descriptor *desc; |
5215 | ||
1da177e4 | 5216 | desc = &intf->cur_altsetting->desc; |
4fe0387a AS |
5217 | if (desc->bAlternateSetting == 0) { |
5218 | usb_disable_interface(udev, intf, true); | |
5219 | usb_enable_interface(udev, intf, true); | |
5220 | ret = 0; | |
5221 | } else { | |
04a723ea SS |
5222 | /* Let the bandwidth allocation function know that this |
5223 | * device has been reset, and it will have to use | |
5224 | * alternate setting 0 as the current alternate setting. | |
3f0479e0 | 5225 | */ |
04a723ea | 5226 | intf->resetting_device = 1; |
4fe0387a AS |
5227 | ret = usb_set_interface(udev, desc->bInterfaceNumber, |
5228 | desc->bAlternateSetting); | |
04a723ea | 5229 | intf->resetting_device = 0; |
4fe0387a | 5230 | } |
1da177e4 LT |
5231 | if (ret < 0) { |
5232 | dev_err(&udev->dev, "failed to restore interface %d " | |
5233 | "altsetting %d (error=%d)\n", | |
5234 | desc->bInterfaceNumber, | |
5235 | desc->bAlternateSetting, | |
5236 | ret); | |
5237 | goto re_enumerate; | |
5238 | } | |
5239 | } | |
5240 | ||
5241 | done: | |
f74631e3 | 5242 | /* Now that the alt settings are re-installed, enable LTM and LPM. */ |
8306095f | 5243 | usb_unlocked_enable_lpm(udev); |
f74631e3 | 5244 | usb_enable_ltm(udev); |
1da177e4 LT |
5245 | return 0; |
5246 | ||
5247 | re_enumerate: | |
6d1d0513 | 5248 | /* LPM state doesn't matter when we're about to destroy the device. */ |
1da177e4 LT |
5249 | hub_port_logical_disconnect(parent_hub, port1); |
5250 | return -ENODEV; | |
5251 | } | |
79efa097 AS |
5252 | |
5253 | /** | |
742120c6 | 5254 | * usb_reset_device - warn interface drivers and perform a USB port reset |
79efa097 | 5255 | * @udev: device to reset (not in SUSPENDED or NOTATTACHED state) |
79efa097 AS |
5256 | * |
5257 | * Warns all drivers bound to registered interfaces (using their pre_reset | |
5258 | * method), performs the port reset, and then lets the drivers know that | |
5259 | * the reset is over (using their post_reset method). | |
5260 | * | |
742120c6 | 5261 | * Return value is the same as for usb_reset_and_verify_device(). |
79efa097 AS |
5262 | * |
5263 | * The caller must own the device lock. For example, it's safe to use | |
5264 | * this from a driver probe() routine after downloading new firmware. | |
5265 | * For calls that might not occur during probe(), drivers should lock | |
5266 | * the device using usb_lock_device_for_reset(). | |
78d9a487 AS |
5267 | * |
5268 | * If an interface is currently being probed or disconnected, we assume | |
5269 | * its driver knows how to handle resets. For all other interfaces, | |
5270 | * if the driver doesn't have pre_reset and post_reset methods then | |
5271 | * we attempt to unbind it and rebind afterward. | |
79efa097 | 5272 | */ |
742120c6 | 5273 | int usb_reset_device(struct usb_device *udev) |
79efa097 AS |
5274 | { |
5275 | int ret; | |
852c4b43 | 5276 | int i; |
4d769def | 5277 | unsigned int noio_flag; |
79efa097 AS |
5278 | struct usb_host_config *config = udev->actconfig; |
5279 | ||
5280 | if (udev->state == USB_STATE_NOTATTACHED || | |
5281 | udev->state == USB_STATE_SUSPENDED) { | |
5282 | dev_dbg(&udev->dev, "device reset not allowed in state %d\n", | |
5283 | udev->state); | |
5284 | return -EINVAL; | |
5285 | } | |
5286 | ||
4d769def ML |
5287 | /* |
5288 | * Don't allocate memory with GFP_KERNEL in current | |
5289 | * context to avoid possible deadlock if usb mass | |
5290 | * storage interface or usbnet interface(iSCSI case) | |
5291 | * is included in current configuration. The easist | |
5292 | * approach is to do it for every device reset, | |
5293 | * because the device 'memalloc_noio' flag may have | |
5294 | * not been set before reseting the usb device. | |
5295 | */ | |
5296 | noio_flag = memalloc_noio_save(); | |
5297 | ||
645daaab | 5298 | /* Prevent autosuspend during the reset */ |
94fcda1f | 5299 | usb_autoresume_device(udev); |
645daaab | 5300 | |
79efa097 | 5301 | if (config) { |
79efa097 | 5302 | for (i = 0; i < config->desc.bNumInterfaces; ++i) { |
852c4b43 AS |
5303 | struct usb_interface *cintf = config->interface[i]; |
5304 | struct usb_driver *drv; | |
78d9a487 | 5305 | int unbind = 0; |
852c4b43 AS |
5306 | |
5307 | if (cintf->dev.driver) { | |
79efa097 | 5308 | drv = to_usb_driver(cintf->dev.driver); |
78d9a487 AS |
5309 | if (drv->pre_reset && drv->post_reset) |
5310 | unbind = (drv->pre_reset)(cintf); | |
5311 | else if (cintf->condition == | |
5312 | USB_INTERFACE_BOUND) | |
5313 | unbind = 1; | |
5314 | if (unbind) | |
5315 | usb_forced_unbind_intf(cintf); | |
79efa097 AS |
5316 | } |
5317 | } | |
5318 | } | |
5319 | ||
742120c6 | 5320 | ret = usb_reset_and_verify_device(udev); |
79efa097 AS |
5321 | |
5322 | if (config) { | |
79efa097 | 5323 | for (i = config->desc.bNumInterfaces - 1; i >= 0; --i) { |
852c4b43 AS |
5324 | struct usb_interface *cintf = config->interface[i]; |
5325 | struct usb_driver *drv; | |
78d9a487 | 5326 | int rebind = cintf->needs_binding; |
852c4b43 | 5327 | |
78d9a487 | 5328 | if (!rebind && cintf->dev.driver) { |
79efa097 AS |
5329 | drv = to_usb_driver(cintf->dev.driver); |
5330 | if (drv->post_reset) | |
78d9a487 AS |
5331 | rebind = (drv->post_reset)(cintf); |
5332 | else if (cintf->condition == | |
5333 | USB_INTERFACE_BOUND) | |
5334 | rebind = 1; | |
59f0103d AS |
5335 | if (rebind) |
5336 | cintf->needs_binding = 1; | |
79efa097 | 5337 | } |
79efa097 | 5338 | } |
59f0103d | 5339 | usb_unbind_and_rebind_marked_interfaces(udev); |
79efa097 AS |
5340 | } |
5341 | ||
94fcda1f | 5342 | usb_autosuspend_device(udev); |
4d769def | 5343 | memalloc_noio_restore(noio_flag); |
79efa097 AS |
5344 | return ret; |
5345 | } | |
742120c6 | 5346 | EXPORT_SYMBOL_GPL(usb_reset_device); |
dc023dce IPG |
5347 | |
5348 | ||
5349 | /** | |
5350 | * usb_queue_reset_device - Reset a USB device from an atomic context | |
5351 | * @iface: USB interface belonging to the device to reset | |
5352 | * | |
5353 | * This function can be used to reset a USB device from an atomic | |
5354 | * context, where usb_reset_device() won't work (as it blocks). | |
5355 | * | |
5356 | * Doing a reset via this method is functionally equivalent to calling | |
5357 | * usb_reset_device(), except for the fact that it is delayed to a | |
5358 | * workqueue. This means that any drivers bound to other interfaces | |
5359 | * might be unbound, as well as users from usbfs in user space. | |
5360 | * | |
5361 | * Corner cases: | |
5362 | * | |
5363 | * - Scheduling two resets at the same time from two different drivers | |
5364 | * attached to two different interfaces of the same device is | |
5365 | * possible; depending on how the driver attached to each interface | |
5366 | * handles ->pre_reset(), the second reset might happen or not. | |
5367 | * | |
5368 | * - If a driver is unbound and it had a pending reset, the reset will | |
5369 | * be cancelled. | |
5370 | * | |
5371 | * - This function can be called during .probe() or .disconnect() | |
5372 | * times. On return from .disconnect(), any pending resets will be | |
5373 | * cancelled. | |
5374 | * | |
5375 | * There is no no need to lock/unlock the @reset_ws as schedule_work() | |
5376 | * does its own. | |
5377 | * | |
5378 | * NOTE: We don't do any reference count tracking because it is not | |
5379 | * needed. The lifecycle of the work_struct is tied to the | |
5380 | * usb_interface. Before destroying the interface we cancel the | |
5381 | * work_struct, so the fact that work_struct is queued and or | |
5382 | * running means the interface (and thus, the device) exist and | |
5383 | * are referenced. | |
5384 | */ | |
5385 | void usb_queue_reset_device(struct usb_interface *iface) | |
5386 | { | |
5387 | schedule_work(&iface->reset_ws); | |
5388 | } | |
5389 | EXPORT_SYMBOL_GPL(usb_queue_reset_device); | |
ff823c79 LT |
5390 | |
5391 | /** | |
5392 | * usb_hub_find_child - Get the pointer of child device | |
5393 | * attached to the port which is specified by @port1. | |
5394 | * @hdev: USB device belonging to the usb hub | |
5395 | * @port1: port num to indicate which port the child device | |
5396 | * is attached to. | |
5397 | * | |
5398 | * USB drivers call this function to get hub's child device | |
5399 | * pointer. | |
5400 | * | |
5401 | * Return NULL if input param is invalid and | |
5402 | * child's usb_device pointer if non-NULL. | |
5403 | */ | |
5404 | struct usb_device *usb_hub_find_child(struct usb_device *hdev, | |
5405 | int port1) | |
5406 | { | |
ad493e5e | 5407 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
ff823c79 LT |
5408 | |
5409 | if (port1 < 1 || port1 > hdev->maxchild) | |
5410 | return NULL; | |
5411 | return hub->ports[port1 - 1]->child; | |
5412 | } | |
5413 | EXPORT_SYMBOL_GPL(usb_hub_find_child); | |
d5575424 | 5414 | |
05f91689 LT |
5415 | /** |
5416 | * usb_set_hub_port_connect_type - set hub port connect type. | |
5417 | * @hdev: USB device belonging to the usb hub | |
5418 | * @port1: port num of the port | |
5419 | * @type: connect type of the port | |
5420 | */ | |
5421 | void usb_set_hub_port_connect_type(struct usb_device *hdev, int port1, | |
5422 | enum usb_port_connect_type type) | |
5423 | { | |
ad493e5e | 5424 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
05f91689 LT |
5425 | |
5426 | hub->ports[port1 - 1]->connect_type = type; | |
5427 | } | |
5428 | ||
5429 | /** | |
5430 | * usb_get_hub_port_connect_type - Get the port's connect type | |
5431 | * @hdev: USB device belonging to the usb hub | |
5432 | * @port1: port num of the port | |
5433 | * | |
5434 | * Return connect type of the port and if input params are | |
5435 | * invalid, return USB_PORT_CONNECT_TYPE_UNKNOWN. | |
5436 | */ | |
5437 | enum usb_port_connect_type | |
5438 | usb_get_hub_port_connect_type(struct usb_device *hdev, int port1) | |
5439 | { | |
ad493e5e | 5440 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
05f91689 LT |
5441 | |
5442 | return hub->ports[port1 - 1]->connect_type; | |
5443 | } | |
5444 | ||
d2123fd9 LT |
5445 | void usb_hub_adjust_deviceremovable(struct usb_device *hdev, |
5446 | struct usb_hub_descriptor *desc) | |
5447 | { | |
5448 | enum usb_port_connect_type connect_type; | |
5449 | int i; | |
5450 | ||
5451 | if (!hub_is_superspeed(hdev)) { | |
5452 | for (i = 1; i <= hdev->maxchild; i++) { | |
5453 | connect_type = usb_get_hub_port_connect_type(hdev, i); | |
5454 | ||
5455 | if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { | |
5456 | u8 mask = 1 << (i%8); | |
5457 | ||
5458 | if (!(desc->u.hs.DeviceRemovable[i/8] & mask)) { | |
5459 | dev_dbg(&hdev->dev, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n", | |
5460 | i); | |
5461 | desc->u.hs.DeviceRemovable[i/8] |= mask; | |
5462 | } | |
5463 | } | |
5464 | } | |
5465 | } else { | |
5466 | u16 port_removable = le16_to_cpu(desc->u.ss.DeviceRemovable); | |
5467 | ||
5468 | for (i = 1; i <= hdev->maxchild; i++) { | |
5469 | connect_type = usb_get_hub_port_connect_type(hdev, i); | |
5470 | ||
5471 | if (connect_type == USB_PORT_CONNECT_TYPE_HARD_WIRED) { | |
5472 | u16 mask = 1 << i; | |
5473 | ||
5474 | if (!(port_removable & mask)) { | |
5475 | dev_dbg(&hdev->dev, "usb port%d's DeviceRemovable is changed to 1 according to platform information.\n", | |
5476 | i); | |
5477 | port_removable |= mask; | |
5478 | } | |
5479 | } | |
5480 | } | |
5481 | ||
5482 | desc->u.ss.DeviceRemovable = cpu_to_le16(port_removable); | |
5483 | } | |
5484 | } | |
5485 | ||
d5575424 LT |
5486 | #ifdef CONFIG_ACPI |
5487 | /** | |
5488 | * usb_get_hub_port_acpi_handle - Get the usb port's acpi handle | |
5489 | * @hdev: USB device belonging to the usb hub | |
5490 | * @port1: port num of the port | |
5491 | * | |
5492 | * Return port's acpi handle if successful, NULL if params are | |
5493 | * invaild. | |
5494 | */ | |
5495 | acpi_handle usb_get_hub_port_acpi_handle(struct usb_device *hdev, | |
5496 | int port1) | |
5497 | { | |
ad493e5e | 5498 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
d5575424 LT |
5499 | |
5500 | return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev); | |
5501 | } | |
5502 | #endif |