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