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