]>
Commit | Line | Data |
---|---|---|
5fd54ace | 1 | // SPDX-License-Identifier: GPL-2.0 |
6e30d7cb LT |
2 | /* |
3 | * usb port device code | |
4 | * | |
5 | * Copyright (C) 2012 Intel Corp | |
6 | * | |
7 | * Author: Lan Tianyu <tianyu.lan@intel.com> | |
6e30d7cb LT |
8 | */ |
9 | ||
372488c6 | 10 | #include <linux/kstrtox.h> |
9f7344fb | 11 | #include <linux/slab.h> |
971fcd49 | 12 | #include <linux/pm_qos.h> |
8c67d06f | 13 | #include <linux/component.h> |
9f7344fb | 14 | |
6e30d7cb LT |
15 | #include "hub.h" |
16 | ||
6c79fe4a DW |
17 | static int usb_port_block_power_off; |
18 | ||
cef7468c LT |
19 | static const struct attribute_group *port_dev_group[]; |
20 | ||
430d57f5 RC |
21 | static ssize_t early_stop_show(struct device *dev, |
22 | struct device_attribute *attr, char *buf) | |
23 | { | |
24 | struct usb_port *port_dev = to_usb_port(dev); | |
25 | ||
26 | return sysfs_emit(buf, "%s\n", port_dev->early_stop ? "yes" : "no"); | |
27 | } | |
28 | ||
29 | static ssize_t early_stop_store(struct device *dev, struct device_attribute *attr, | |
30 | const char *buf, size_t count) | |
31 | { | |
32 | struct usb_port *port_dev = to_usb_port(dev); | |
33 | bool value; | |
34 | ||
35 | if (kstrtobool(buf, &value)) | |
36 | return -EINVAL; | |
37 | ||
38 | if (value) | |
39 | port_dev->early_stop = 1; | |
40 | else | |
41 | port_dev->early_stop = 0; | |
42 | ||
43 | return count; | |
44 | } | |
45 | static DEVICE_ATTR_RW(early_stop); | |
46 | ||
f061f43d MG |
47 | static ssize_t disable_show(struct device *dev, |
48 | struct device_attribute *attr, char *buf) | |
49 | { | |
50 | struct usb_port *port_dev = to_usb_port(dev); | |
51 | struct usb_device *hdev = to_usb_device(dev->parent->parent); | |
52 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); | |
53 | struct usb_interface *intf = to_usb_interface(hub->intfdev); | |
54 | int port1 = port_dev->portnum; | |
55 | u16 portstatus, unused; | |
56 | bool disabled; | |
57 | int rc; | |
58 | ||
59 | rc = usb_autopm_get_interface(intf); | |
60 | if (rc < 0) | |
61 | return rc; | |
62 | ||
63 | usb_lock_device(hdev); | |
64 | if (hub->disconnected) { | |
65 | rc = -ENODEV; | |
66 | goto out_hdev_lock; | |
67 | } | |
68 | ||
69 | usb_hub_port_status(hub, port1, &portstatus, &unused); | |
70 | disabled = !usb_port_is_power_on(hub, portstatus); | |
71 | ||
72 | out_hdev_lock: | |
73 | usb_unlock_device(hdev); | |
74 | usb_autopm_put_interface(intf); | |
75 | ||
76 | if (rc) | |
77 | return rc; | |
78 | ||
79 | return sysfs_emit(buf, "%s\n", disabled ? "1" : "0"); | |
80 | } | |
81 | ||
82 | static ssize_t disable_store(struct device *dev, struct device_attribute *attr, | |
83 | const char *buf, size_t count) | |
84 | { | |
85 | struct usb_port *port_dev = to_usb_port(dev); | |
86 | struct usb_device *hdev = to_usb_device(dev->parent->parent); | |
87 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); | |
88 | struct usb_interface *intf = to_usb_interface(hub->intfdev); | |
89 | int port1 = port_dev->portnum; | |
90 | bool disabled; | |
91 | int rc; | |
92 | ||
372488c6 | 93 | rc = kstrtobool(buf, &disabled); |
f061f43d MG |
94 | if (rc) |
95 | return rc; | |
96 | ||
97 | rc = usb_autopm_get_interface(intf); | |
98 | if (rc < 0) | |
99 | return rc; | |
100 | ||
101 | usb_lock_device(hdev); | |
102 | if (hub->disconnected) { | |
103 | rc = -ENODEV; | |
104 | goto out_hdev_lock; | |
105 | } | |
106 | ||
107 | if (disabled && port_dev->child) | |
108 | usb_disconnect(&port_dev->child); | |
109 | ||
110 | rc = usb_hub_set_port_power(hdev, hub, port1, !disabled); | |
111 | ||
112 | if (disabled) { | |
113 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); | |
114 | if (!port_dev->is_superspeed) | |
115 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); | |
116 | } | |
117 | ||
118 | if (!rc) | |
119 | rc = count; | |
120 | ||
121 | out_hdev_lock: | |
122 | usb_unlock_device(hdev); | |
123 | usb_autopm_put_interface(intf); | |
124 | ||
125 | return rc; | |
126 | } | |
127 | static DEVICE_ATTR_RW(disable); | |
128 | ||
355c74e5 BM |
129 | static ssize_t location_show(struct device *dev, |
130 | struct device_attribute *attr, char *buf) | |
131 | { | |
132 | struct usb_port *port_dev = to_usb_port(dev); | |
133 | ||
134 | return sprintf(buf, "0x%08x\n", port_dev->location); | |
135 | } | |
136 | static DEVICE_ATTR_RO(location); | |
137 | ||
d03f254f GKH |
138 | static ssize_t connect_type_show(struct device *dev, |
139 | struct device_attribute *attr, char *buf) | |
cef7468c LT |
140 | { |
141 | struct usb_port *port_dev = to_usb_port(dev); | |
142 | char *result; | |
143 | ||
144 | switch (port_dev->connect_type) { | |
145 | case USB_PORT_CONNECT_TYPE_HOT_PLUG: | |
146 | result = "hotplug"; | |
147 | break; | |
148 | case USB_PORT_CONNECT_TYPE_HARD_WIRED: | |
149 | result = "hardwired"; | |
150 | break; | |
151 | case USB_PORT_NOT_USED: | |
152 | result = "not used"; | |
153 | break; | |
154 | default: | |
155 | result = "unknown"; | |
156 | break; | |
157 | } | |
158 | ||
159 | return sprintf(buf, "%s\n", result); | |
160 | } | |
d03f254f | 161 | static DEVICE_ATTR_RO(connect_type); |
cef7468c | 162 | |
83cb2604 RL |
163 | static ssize_t state_show(struct device *dev, |
164 | struct device_attribute *attr, char *buf) | |
165 | { | |
166 | struct usb_port *port_dev = to_usb_port(dev); | |
167 | enum usb_device_state state = READ_ONCE(port_dev->state); | |
168 | ||
169 | return sysfs_emit(buf, "%s\n", usb_state_string(state)); | |
170 | } | |
171 | static DEVICE_ATTR_RO(state); | |
172 | ||
1cbd53c8 RL |
173 | static ssize_t over_current_count_show(struct device *dev, |
174 | struct device_attribute *attr, char *buf) | |
175 | { | |
176 | struct usb_port *port_dev = to_usb_port(dev); | |
177 | ||
178 | return sprintf(buf, "%u\n", port_dev->over_current_count); | |
179 | } | |
180 | static DEVICE_ATTR_RO(over_current_count); | |
181 | ||
25244227 NB |
182 | static ssize_t quirks_show(struct device *dev, |
183 | struct device_attribute *attr, char *buf) | |
184 | { | |
185 | struct usb_port *port_dev = to_usb_port(dev); | |
186 | ||
187 | return sprintf(buf, "%08x\n", port_dev->quirks); | |
188 | } | |
189 | ||
190 | static ssize_t quirks_store(struct device *dev, struct device_attribute *attr, | |
191 | const char *buf, size_t count) | |
192 | { | |
193 | struct usb_port *port_dev = to_usb_port(dev); | |
194 | u32 value; | |
195 | ||
196 | if (kstrtou32(buf, 16, &value)) | |
197 | return -EINVAL; | |
198 | ||
199 | port_dev->quirks = value; | |
200 | return count; | |
201 | } | |
202 | static DEVICE_ATTR_RW(quirks); | |
203 | ||
513072d9 LB |
204 | static ssize_t usb3_lpm_permit_show(struct device *dev, |
205 | struct device_attribute *attr, char *buf) | |
206 | { | |
207 | struct usb_port *port_dev = to_usb_port(dev); | |
208 | const char *p; | |
209 | ||
210 | if (port_dev->usb3_lpm_u1_permit) { | |
211 | if (port_dev->usb3_lpm_u2_permit) | |
212 | p = "u1_u2"; | |
213 | else | |
214 | p = "u1"; | |
215 | } else { | |
216 | if (port_dev->usb3_lpm_u2_permit) | |
217 | p = "u2"; | |
218 | else | |
219 | p = "0"; | |
220 | } | |
221 | ||
222 | return sprintf(buf, "%s\n", p); | |
223 | } | |
224 | ||
225 | static ssize_t usb3_lpm_permit_store(struct device *dev, | |
226 | struct device_attribute *attr, | |
227 | const char *buf, size_t count) | |
228 | { | |
229 | struct usb_port *port_dev = to_usb_port(dev); | |
230 | struct usb_device *udev = port_dev->child; | |
231 | struct usb_hcd *hcd; | |
232 | ||
233 | if (!strncmp(buf, "u1_u2", 5)) { | |
234 | port_dev->usb3_lpm_u1_permit = 1; | |
235 | port_dev->usb3_lpm_u2_permit = 1; | |
236 | ||
237 | } else if (!strncmp(buf, "u1", 2)) { | |
238 | port_dev->usb3_lpm_u1_permit = 1; | |
239 | port_dev->usb3_lpm_u2_permit = 0; | |
240 | ||
241 | } else if (!strncmp(buf, "u2", 2)) { | |
242 | port_dev->usb3_lpm_u1_permit = 0; | |
243 | port_dev->usb3_lpm_u2_permit = 1; | |
244 | ||
245 | } else if (!strncmp(buf, "0", 1)) { | |
246 | port_dev->usb3_lpm_u1_permit = 0; | |
247 | port_dev->usb3_lpm_u2_permit = 0; | |
248 | } else | |
249 | return -EINVAL; | |
250 | ||
251 | /* If device is connected to the port, disable or enable lpm | |
252 | * to make new u1 u2 setting take effect immediately. | |
253 | */ | |
254 | if (udev) { | |
255 | hcd = bus_to_hcd(udev->bus); | |
256 | if (!hcd) | |
257 | return -EINVAL; | |
258 | usb_lock_device(udev); | |
259 | mutex_lock(hcd->bandwidth_mutex); | |
260 | if (!usb_disable_lpm(udev)) | |
261 | usb_enable_lpm(udev); | |
262 | mutex_unlock(hcd->bandwidth_mutex); | |
263 | usb_unlock_device(udev); | |
264 | } | |
265 | ||
266 | return count; | |
267 | } | |
268 | static DEVICE_ATTR_RW(usb3_lpm_permit); | |
269 | ||
cef7468c LT |
270 | static struct attribute *port_dev_attrs[] = { |
271 | &dev_attr_connect_type.attr, | |
83cb2604 | 272 | &dev_attr_state.attr, |
355c74e5 | 273 | &dev_attr_location.attr, |
25244227 | 274 | &dev_attr_quirks.attr, |
1cbd53c8 | 275 | &dev_attr_over_current_count.attr, |
f061f43d | 276 | &dev_attr_disable.attr, |
430d57f5 | 277 | &dev_attr_early_stop.attr, |
cef7468c LT |
278 | NULL, |
279 | }; | |
280 | ||
4154a4f7 | 281 | static const struct attribute_group port_dev_attr_grp = { |
cef7468c LT |
282 | .attrs = port_dev_attrs, |
283 | }; | |
284 | ||
285 | static const struct attribute_group *port_dev_group[] = { | |
286 | &port_dev_attr_grp, | |
287 | NULL, | |
288 | }; | |
289 | ||
513072d9 LB |
290 | static struct attribute *port_dev_usb3_attrs[] = { |
291 | &dev_attr_usb3_lpm_permit.attr, | |
292 | NULL, | |
293 | }; | |
294 | ||
4154a4f7 | 295 | static const struct attribute_group port_dev_usb3_attr_grp = { |
513072d9 LB |
296 | .attrs = port_dev_usb3_attrs, |
297 | }; | |
298 | ||
299 | static const struct attribute_group *port_dev_usb3_group[] = { | |
300 | &port_dev_attr_grp, | |
301 | &port_dev_usb3_attr_grp, | |
302 | NULL, | |
303 | }; | |
304 | ||
6e30d7cb LT |
305 | static void usb_port_device_release(struct device *dev) |
306 | { | |
307 | struct usb_port *port_dev = to_usb_port(dev); | |
308 | ||
e3d10505 | 309 | kfree(port_dev->req); |
6e30d7cb LT |
310 | kfree(port_dev); |
311 | } | |
312 | ||
ceb6c9c8 | 313 | #ifdef CONFIG_PM |
971fcd49 LT |
314 | static int usb_port_runtime_resume(struct device *dev) |
315 | { | |
316 | struct usb_port *port_dev = to_usb_port(dev); | |
317 | struct usb_device *hdev = to_usb_device(dev->parent->parent); | |
318 | struct usb_interface *intf = to_usb_interface(dev->parent); | |
ad493e5e | 319 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
7027df36 | 320 | struct usb_device *udev = port_dev->child; |
7ad3c470 | 321 | struct usb_port *peer = port_dev->peer; |
ad493e5e | 322 | int port1 = port_dev->portnum; |
971fcd49 LT |
323 | int retval; |
324 | ||
ad493e5e LT |
325 | if (!hub) |
326 | return -EINVAL; | |
600856c2 | 327 | if (hub->in_reset) { |
d5c3834e | 328 | set_bit(port1, hub->power_bits); |
600856c2 AS |
329 | return 0; |
330 | } | |
ad493e5e | 331 | |
7ad3c470 DW |
332 | /* |
333 | * Power on our usb3 peer before this usb2 port to prevent a usb3 | |
334 | * device from degrading to its usb2 connection | |
335 | */ | |
336 | if (!port_dev->is_superspeed && peer) | |
337 | pm_runtime_get_sync(&peer->dev); | |
338 | ||
1f8b39bc ER |
339 | retval = usb_autopm_get_interface(intf); |
340 | if (retval < 0) | |
341 | return retval; | |
342 | ||
41341261 | 343 | retval = usb_hub_set_port_power(hdev, hub, port1, true); |
7ad3c470 | 344 | msleep(hub_power_on_good_delay(hub)); |
7027df36 | 345 | if (udev && !retval) { |
ad493e5e | 346 | /* |
3cd12f91 DW |
347 | * Our preference is to simply wait for the port to reconnect, |
348 | * as that is the lowest latency method to restart the port. | |
349 | * However, there are cases where toggling port power results in | |
350 | * the host port and the device port getting out of sync causing | |
351 | * a link training live lock. Upon timeout, flag the port as | |
352 | * needing warm reset recovery (to be performed later by | |
353 | * usb_port_resume() as requested via usb_wakeup_notification()) | |
ad493e5e | 354 | */ |
3cd12f91 DW |
355 | if (hub_port_debounce_be_connected(hub, port1) < 0) { |
356 | dev_dbg(&port_dev->dev, "reconnect timeout\n"); | |
357 | if (hub_is_superspeed(hdev)) | |
358 | set_bit(port1, hub->warm_reset_bits); | |
359 | } | |
7027df36 DW |
360 | |
361 | /* Force the child awake to revalidate after the power loss. */ | |
362 | if (!test_and_set_bit(port1, hub->child_usage_bits)) { | |
363 | pm_runtime_get_noresume(&port_dev->dev); | |
364 | pm_request_resume(&udev->dev); | |
365 | } | |
ad493e5e LT |
366 | } |
367 | ||
971fcd49 | 368 | usb_autopm_put_interface(intf); |
7ad3c470 | 369 | |
971fcd49 LT |
370 | return retval; |
371 | } | |
372 | ||
373 | static int usb_port_runtime_suspend(struct device *dev) | |
374 | { | |
375 | struct usb_port *port_dev = to_usb_port(dev); | |
376 | struct usb_device *hdev = to_usb_device(dev->parent->parent); | |
377 | struct usb_interface *intf = to_usb_interface(dev->parent); | |
ad493e5e | 378 | struct usb_hub *hub = usb_hub_to_struct_hub(hdev); |
7ad3c470 | 379 | struct usb_port *peer = port_dev->peer; |
ad493e5e | 380 | int port1 = port_dev->portnum; |
971fcd49 LT |
381 | int retval; |
382 | ||
ad493e5e LT |
383 | if (!hub) |
384 | return -EINVAL; | |
600856c2 AS |
385 | if (hub->in_reset) |
386 | return -EBUSY; | |
ad493e5e | 387 | |
971fcd49 LT |
388 | if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF) |
389 | == PM_QOS_FLAGS_ALL) | |
390 | return -EAGAIN; | |
391 | ||
6c79fe4a DW |
392 | if (usb_port_block_power_off) |
393 | return -EBUSY; | |
394 | ||
1f8b39bc ER |
395 | retval = usb_autopm_get_interface(intf); |
396 | if (retval < 0) | |
397 | return retval; | |
398 | ||
41341261 | 399 | retval = usb_hub_set_port_power(hdev, hub, port1, false); |
ad493e5e | 400 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION); |
69080584 DW |
401 | if (!port_dev->is_superspeed) |
402 | usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE); | |
971fcd49 | 403 | usb_autopm_put_interface(intf); |
7ad3c470 DW |
404 | |
405 | /* | |
406 | * Our peer usb3 port may now be able to suspend, so | |
407 | * asynchronously queue a suspend request to observe that this | |
408 | * usb2 port is now off. | |
409 | */ | |
410 | if (!port_dev->is_superspeed && peer) | |
411 | pm_runtime_put(&peer->dev); | |
412 | ||
971fcd49 LT |
413 | return retval; |
414 | } | |
415 | #endif | |
416 | ||
582ee9c5 KHF |
417 | static void usb_port_shutdown(struct device *dev) |
418 | { | |
419 | struct usb_port *port_dev = to_usb_port(dev); | |
420 | ||
421 | if (port_dev->child) | |
422 | usb_disable_usb2_hardware_lpm(port_dev->child); | |
423 | } | |
424 | ||
971fcd49 | 425 | static const struct dev_pm_ops usb_port_pm_ops = { |
ceb6c9c8 | 426 | #ifdef CONFIG_PM |
971fcd49 LT |
427 | .runtime_suspend = usb_port_runtime_suspend, |
428 | .runtime_resume = usb_port_runtime_resume, | |
971fcd49 LT |
429 | #endif |
430 | }; | |
431 | ||
6e30d7cb LT |
432 | struct device_type usb_port_device_type = { |
433 | .name = "usb_port", | |
434 | .release = usb_port_device_release, | |
971fcd49 | 435 | .pm = &usb_port_pm_ops, |
6e30d7cb LT |
436 | }; |
437 | ||
d99f6b41 DW |
438 | static struct device_driver usb_port_driver = { |
439 | .name = "usb", | |
440 | .owner = THIS_MODULE, | |
582ee9c5 | 441 | .shutdown = usb_port_shutdown, |
d99f6b41 DW |
442 | }; |
443 | ||
b7e38eac | 444 | static int link_peers(struct usb_port *left, struct usb_port *right) |
d8521afe | 445 | { |
7ad3c470 | 446 | struct usb_port *ss_port, *hs_port; |
b7e38eac DW |
447 | int rc; |
448 | ||
d8521afe | 449 | if (left->peer == right && right->peer == left) |
b7e38eac | 450 | return 0; |
d8521afe DW |
451 | |
452 | if (left->peer || right->peer) { | |
453 | struct usb_port *lpeer = left->peer; | |
454 | struct usb_port *rpeer = right->peer; | |
6c79fe4a DW |
455 | char *method; |
456 | ||
457 | if (left->location && left->location == right->location) | |
458 | method = "location"; | |
459 | else | |
460 | method = "default"; | |
461 | ||
6406eeb3 | 462 | pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n", |
6c79fe4a DW |
463 | dev_name(&left->dev), dev_name(&right->dev), method, |
464 | dev_name(&left->dev), | |
465 | lpeer ? dev_name(&lpeer->dev) : "none", | |
466 | dev_name(&right->dev), | |
467 | rpeer ? dev_name(&rpeer->dev) : "none"); | |
b7e38eac DW |
468 | return -EBUSY; |
469 | } | |
470 | ||
471 | rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer"); | |
472 | if (rc) | |
473 | return rc; | |
474 | rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer"); | |
475 | if (rc) { | |
476 | sysfs_remove_link(&left->dev.kobj, "peer"); | |
477 | return rc; | |
d8521afe DW |
478 | } |
479 | ||
7ad3c470 DW |
480 | /* |
481 | * We need to wake the HiSpeed port to make sure we don't race | |
482 | * setting ->peer with usb_port_runtime_suspend(). Otherwise we | |
483 | * may miss a suspend event for the SuperSpeed port. | |
484 | */ | |
485 | if (left->is_superspeed) { | |
486 | ss_port = left; | |
487 | WARN_ON(right->is_superspeed); | |
488 | hs_port = right; | |
489 | } else { | |
490 | ss_port = right; | |
491 | WARN_ON(!right->is_superspeed); | |
492 | hs_port = left; | |
493 | } | |
494 | pm_runtime_get_sync(&hs_port->dev); | |
495 | ||
d8521afe DW |
496 | left->peer = right; |
497 | right->peer = left; | |
b7e38eac | 498 | |
7ad3c470 DW |
499 | /* |
500 | * The SuperSpeed reference is dropped when the HiSpeed port in | |
501 | * this relationship suspends, i.e. when it is safe to allow a | |
502 | * SuperSpeed connection to drop since there is no risk of a | |
503 | * device degrading to its powered-off HiSpeed connection. | |
504 | * | |
505 | * Also, drop the HiSpeed ref taken above. | |
506 | */ | |
507 | pm_runtime_get_sync(&ss_port->dev); | |
508 | pm_runtime_put(&hs_port->dev); | |
509 | ||
b7e38eac DW |
510 | return 0; |
511 | } | |
512 | ||
513 | static void link_peers_report(struct usb_port *left, struct usb_port *right) | |
514 | { | |
515 | int rc; | |
516 | ||
517 | rc = link_peers(left, right); | |
518 | if (rc == 0) { | |
519 | dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev)); | |
520 | } else { | |
6406eeb3 | 521 | dev_dbg(&left->dev, "failed to peer to %s (%d)\n", |
b7e38eac DW |
522 | dev_name(&right->dev), rc); |
523 | pr_warn_once("usb: port power management may be unreliable\n"); | |
6c79fe4a | 524 | usb_port_block_power_off = 1; |
b7e38eac | 525 | } |
d8521afe DW |
526 | } |
527 | ||
528 | static void unlink_peers(struct usb_port *left, struct usb_port *right) | |
529 | { | |
7ad3c470 DW |
530 | struct usb_port *ss_port, *hs_port; |
531 | ||
d8521afe DW |
532 | WARN(right->peer != left || left->peer != right, |
533 | "%s and %s are not peers?\n", | |
534 | dev_name(&left->dev), dev_name(&right->dev)); | |
535 | ||
7ad3c470 DW |
536 | /* |
537 | * We wake the HiSpeed port to make sure we don't race its | |
538 | * usb_port_runtime_resume() event which takes a SuperSpeed ref | |
539 | * when ->peer is !NULL. | |
540 | */ | |
541 | if (left->is_superspeed) { | |
542 | ss_port = left; | |
543 | hs_port = right; | |
544 | } else { | |
545 | ss_port = right; | |
546 | hs_port = left; | |
547 | } | |
548 | ||
549 | pm_runtime_get_sync(&hs_port->dev); | |
550 | ||
b7e38eac | 551 | sysfs_remove_link(&left->dev.kobj, "peer"); |
d8521afe | 552 | right->peer = NULL; |
b7e38eac | 553 | sysfs_remove_link(&right->dev.kobj, "peer"); |
d8521afe | 554 | left->peer = NULL; |
7ad3c470 DW |
555 | |
556 | /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */ | |
557 | pm_runtime_put(&ss_port->dev); | |
558 | ||
559 | /* Drop the ref taken above */ | |
560 | pm_runtime_put(&hs_port->dev); | |
d8521afe DW |
561 | } |
562 | ||
8b1ba80c | 563 | /* |
3bfd659b DW |
564 | * For each usb hub device in the system check to see if it is in the |
565 | * peer domain of the given port_dev, and if it is check to see if it | |
566 | * has a port that matches the given port by location | |
567 | */ | |
568 | static int match_location(struct usb_device *peer_hdev, void *p) | |
569 | { | |
570 | int port1; | |
571 | struct usb_hcd *hcd, *peer_hcd; | |
572 | struct usb_port *port_dev = p, *peer; | |
573 | struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev); | |
574 | struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent); | |
575 | ||
576 | if (!peer_hub) | |
577 | return 0; | |
578 | ||
579 | hcd = bus_to_hcd(hdev->bus); | |
580 | peer_hcd = bus_to_hcd(peer_hdev->bus); | |
581 | /* peer_hcd is provisional until we verify it against the known peer */ | |
582 | if (peer_hcd != hcd->shared_hcd) | |
583 | return 0; | |
584 | ||
585 | for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) { | |
586 | peer = peer_hub->ports[port1 - 1]; | |
587 | if (peer && peer->location == port_dev->location) { | |
b7e38eac | 588 | link_peers_report(port_dev, peer); |
3bfd659b DW |
589 | return 1; /* done */ |
590 | } | |
591 | } | |
592 | ||
593 | return 0; | |
594 | } | |
595 | ||
596 | /* | |
597 | * Find the peer port either via explicit platform firmware "location" | |
598 | * data, the peer hcd for root hubs, or the upstream peer relationship | |
599 | * for all other hubs. | |
8b1ba80c | 600 | */ |
d8521afe DW |
601 | static void find_and_link_peer(struct usb_hub *hub, int port1) |
602 | { | |
603 | struct usb_port *port_dev = hub->ports[port1 - 1], *peer; | |
604 | struct usb_device *hdev = hub->hdev; | |
8b1ba80c DW |
605 | struct usb_device *peer_hdev; |
606 | struct usb_hub *peer_hub; | |
d8521afe | 607 | |
3bfd659b DW |
608 | /* |
609 | * If location data is available then we can only peer this port | |
610 | * by a location match, not the default peer (lest we create a | |
611 | * situation where we need to go back and undo a default peering | |
612 | * when the port is later peered by location data) | |
613 | */ | |
614 | if (port_dev->location) { | |
615 | /* we link the peer in match_location() if found */ | |
616 | usb_for_each_dev(port_dev, match_location); | |
617 | return; | |
618 | } else if (!hdev->parent) { | |
d8521afe DW |
619 | struct usb_hcd *hcd = bus_to_hcd(hdev->bus); |
620 | struct usb_hcd *peer_hcd = hcd->shared_hcd; | |
621 | ||
622 | if (!peer_hcd) | |
623 | return; | |
624 | ||
625 | peer_hdev = peer_hcd->self.root_hub; | |
8b1ba80c DW |
626 | } else { |
627 | struct usb_port *upstream; | |
628 | struct usb_device *parent = hdev->parent; | |
629 | struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent); | |
630 | ||
631 | if (!parent_hub) | |
d8521afe DW |
632 | return; |
633 | ||
8b1ba80c DW |
634 | upstream = parent_hub->ports[hdev->portnum - 1]; |
635 | if (!upstream || !upstream->peer) | |
636 | return; | |
d8521afe | 637 | |
8b1ba80c | 638 | peer_hdev = upstream->peer->child; |
d8521afe | 639 | } |
8b1ba80c DW |
640 | |
641 | peer_hub = usb_hub_to_struct_hub(peer_hdev); | |
642 | if (!peer_hub || port1 > peer_hdev->maxchild) | |
643 | return; | |
644 | ||
3bfd659b DW |
645 | /* |
646 | * we found a valid default peer, last check is to make sure it | |
647 | * does not have location data | |
648 | */ | |
8b1ba80c | 649 | peer = peer_hub->ports[port1 - 1]; |
3bfd659b | 650 | if (peer && peer->location == 0) |
b7e38eac | 651 | link_peers_report(port_dev, peer); |
d8521afe DW |
652 | } |
653 | ||
8c67d06f HK |
654 | static int connector_bind(struct device *dev, struct device *connector, void *data) |
655 | { | |
11110783 | 656 | struct usb_port *port_dev = to_usb_port(dev); |
8c67d06f HK |
657 | int ret; |
658 | ||
659 | ret = sysfs_create_link(&dev->kobj, &connector->kobj, "connector"); | |
660 | if (ret) | |
661 | return ret; | |
662 | ||
663 | ret = sysfs_create_link(&connector->kobj, &dev->kobj, dev_name(dev)); | |
11110783 | 664 | if (ret) { |
8c67d06f | 665 | sysfs_remove_link(&dev->kobj, "connector"); |
11110783 HK |
666 | return ret; |
667 | } | |
668 | ||
669 | port_dev->connector = data; | |
670 | ||
671 | /* | |
672 | * If there is already USB device connected to the port, letting the | |
673 | * Type-C connector know about it immediately. | |
674 | */ | |
675 | if (port_dev->child) | |
676 | typec_attach(port_dev->connector, &port_dev->child->dev); | |
8c67d06f | 677 | |
11110783 | 678 | return 0; |
8c67d06f HK |
679 | } |
680 | ||
681 | static void connector_unbind(struct device *dev, struct device *connector, void *data) | |
682 | { | |
11110783 HK |
683 | struct usb_port *port_dev = to_usb_port(dev); |
684 | ||
8c67d06f HK |
685 | sysfs_remove_link(&connector->kobj, dev_name(dev)); |
686 | sysfs_remove_link(&dev->kobj, "connector"); | |
11110783 | 687 | port_dev->connector = NULL; |
8c67d06f HK |
688 | } |
689 | ||
690 | static const struct component_ops connector_ops = { | |
691 | .bind = connector_bind, | |
692 | .unbind = connector_unbind, | |
693 | }; | |
694 | ||
6e30d7cb LT |
695 | int usb_hub_create_port_device(struct usb_hub *hub, int port1) |
696 | { | |
d8521afe | 697 | struct usb_port *port_dev; |
513072d9 | 698 | struct usb_device *hdev = hub->hdev; |
6e30d7cb LT |
699 | int retval; |
700 | ||
701 | port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL); | |
e3d10505 DW |
702 | if (!port_dev) |
703 | return -ENOMEM; | |
704 | ||
705 | port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL); | |
706 | if (!port_dev->req) { | |
707 | kfree(port_dev); | |
708 | return -ENOMEM; | |
6e30d7cb LT |
709 | } |
710 | ||
711 | hub->ports[port1 - 1] = port_dev; | |
971fcd49 | 712 | port_dev->portnum = port1; |
d5c3834e | 713 | set_bit(port1, hub->power_bits); |
6e30d7cb | 714 | port_dev->dev.parent = hub->intfdev; |
513072d9 | 715 | if (hub_is_superspeed(hdev)) { |
c9a1d9e7 | 716 | port_dev->is_superspeed = 1; |
513072d9 LB |
717 | port_dev->usb3_lpm_u1_permit = 1; |
718 | port_dev->usb3_lpm_u2_permit = 1; | |
719 | port_dev->dev.groups = port_dev_usb3_group; | |
720 | } else | |
721 | port_dev->dev.groups = port_dev_group; | |
6e30d7cb | 722 | port_dev->dev.type = &usb_port_device_type; |
d99f6b41 DW |
723 | port_dev->dev.driver = &usb_port_driver; |
724 | dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev), | |
725 | port1); | |
5c79a1e3 | 726 | mutex_init(&port_dev->status_lock); |
6e30d7cb | 727 | retval = device_register(&port_dev->dev); |
e3d10505 DW |
728 | if (retval) { |
729 | put_device(&port_dev->dev); | |
730 | return retval; | |
731 | } | |
732 | ||
83cb2604 RL |
733 | port_dev->state_kn = sysfs_get_dirent(port_dev->dev.kobj.sd, "state"); |
734 | if (!port_dev->state_kn) { | |
735 | dev_err(&port_dev->dev, "failed to sysfs_get_dirent 'state'\n"); | |
736 | retval = -ENODEV; | |
737 | goto err_unregister; | |
738 | } | |
739 | ||
e3d10505 DW |
740 | /* Set default policy of port-poweroff disabled. */ |
741 | retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req, | |
742 | DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF); | |
743 | if (retval < 0) { | |
83cb2604 | 744 | goto err_put_kn; |
e3d10505 | 745 | } |
6e30d7cb | 746 | |
8c67d06f | 747 | retval = component_add(&port_dev->dev, &connector_ops); |
c853685d | 748 | if (retval) { |
8c67d06f | 749 | dev_warn(&port_dev->dev, "failed to add component\n"); |
83cb2604 | 750 | goto err_put_kn; |
c853685d FDF |
751 | } |
752 | ||
753 | find_and_link_peer(hub, port1); | |
8c67d06f | 754 | |
e3d10505 DW |
755 | /* |
756 | * Enable runtime pm and hold a refernce that hub_configure() | |
757 | * will drop once the PM_QOS_NO_POWER_OFF flag state has been set | |
758 | * and the hub has been fully registered (hdev->maxchild set). | |
759 | */ | |
971fcd49 | 760 | pm_runtime_set_active(&port_dev->dev); |
e3d10505 DW |
761 | pm_runtime_get_noresume(&port_dev->dev); |
762 | pm_runtime_enable(&port_dev->dev); | |
763 | device_enable_async_suspend(&port_dev->dev); | |
f6cced1a | 764 | |
9262c19d | 765 | /* |
e3d10505 DW |
766 | * Keep hidden the ability to enable port-poweroff if the hub |
767 | * does not support power switching. | |
f6cced1a | 768 | */ |
e3d10505 DW |
769 | if (!hub_is_port_power_switchable(hub)) |
770 | return 0; | |
f6cced1a | 771 | |
e3d10505 DW |
772 | /* Attempt to let userspace take over the policy. */ |
773 | retval = dev_pm_qos_expose_flags(&port_dev->dev, | |
774 | PM_QOS_FLAG_NO_POWER_OFF); | |
775 | if (retval < 0) { | |
776 | dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n"); | |
777 | return 0; | |
778 | } | |
6e30d7cb | 779 | |
e3d10505 DW |
780 | /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */ |
781 | retval = dev_pm_qos_remove_request(port_dev->req); | |
782 | if (retval >= 0) { | |
783 | kfree(port_dev->req); | |
784 | port_dev->req = NULL; | |
785 | } | |
786 | return 0; | |
83cb2604 RL |
787 | |
788 | err_put_kn: | |
789 | sysfs_put(port_dev->state_kn); | |
790 | err_unregister: | |
791 | device_unregister(&port_dev->dev); | |
792 | ||
793 | return retval; | |
6e30d7cb LT |
794 | } |
795 | ||
d8521afe | 796 | void usb_hub_remove_port_device(struct usb_hub *hub, int port1) |
6e30d7cb | 797 | { |
d8521afe DW |
798 | struct usb_port *port_dev = hub->ports[port1 - 1]; |
799 | struct usb_port *peer; | |
6e30d7cb | 800 | |
d8521afe DW |
801 | peer = port_dev->peer; |
802 | if (peer) | |
803 | unlink_peers(port_dev, peer); | |
8c67d06f | 804 | component_del(&port_dev->dev, &connector_ops); |
83cb2604 | 805 | sysfs_put(port_dev->state_kn); |
d8521afe DW |
806 | device_unregister(&port_dev->dev); |
807 | } |