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