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