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