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