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