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