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