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