staging: rtl8188eu: rename HalSetBrateCfg() - style
[linux/fpc-iii.git] / drivers / usb / core / port.c
blob4a21431953953d5b52b467a107a04c5f8198cf19
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * usb port device code
5 * Copyright (C) 2012 Intel Corp
7 * Author: Lan Tianyu <tianyu.lan@intel.com>
8 */
10 #include <linux/slab.h>
11 #include <linux/pm_qos.h>
13 #include "hub.h"
15 static int usb_port_block_power_off;
17 static const struct attribute_group *port_dev_group[];
19 static ssize_t connect_type_show(struct device *dev,
20 struct device_attribute *attr, char *buf)
22 struct usb_port *port_dev = to_usb_port(dev);
23 char *result;
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;
40 return sprintf(buf, "%s\n", result);
42 static DEVICE_ATTR_RO(connect_type);
44 static ssize_t over_current_count_show(struct device *dev,
45 struct device_attribute *attr, char *buf)
47 struct usb_port *port_dev = to_usb_port(dev);
49 return sprintf(buf, "%u\n", port_dev->over_current_count);
51 static DEVICE_ATTR_RO(over_current_count);
53 static ssize_t quirks_show(struct device *dev,
54 struct device_attribute *attr, char *buf)
56 struct usb_port *port_dev = to_usb_port(dev);
58 return sprintf(buf, "%08x\n", port_dev->quirks);
61 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
62 const char *buf, size_t count)
64 struct usb_port *port_dev = to_usb_port(dev);
65 u32 value;
67 if (kstrtou32(buf, 16, &value))
68 return -EINVAL;
70 port_dev->quirks = value;
71 return count;
73 static DEVICE_ATTR_RW(quirks);
75 static ssize_t usb3_lpm_permit_show(struct device *dev,
76 struct device_attribute *attr, char *buf)
78 struct usb_port *port_dev = to_usb_port(dev);
79 const char *p;
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";
93 return sprintf(buf, "%s\n", p);
96 static ssize_t usb3_lpm_permit_store(struct device *dev,
97 struct device_attribute *attr,
98 const char *buf, size_t count)
100 struct usb_port *port_dev = to_usb_port(dev);
101 struct usb_device *udev = port_dev->child;
102 struct usb_hcd *hcd;
104 if (!strncmp(buf, "u1_u2", 5)) {
105 port_dev->usb3_lpm_u1_permit = 1;
106 port_dev->usb3_lpm_u2_permit = 1;
108 } else if (!strncmp(buf, "u1", 2)) {
109 port_dev->usb3_lpm_u1_permit = 1;
110 port_dev->usb3_lpm_u2_permit = 0;
112 } else if (!strncmp(buf, "u2", 2)) {
113 port_dev->usb3_lpm_u1_permit = 0;
114 port_dev->usb3_lpm_u2_permit = 1;
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;
122 /* If device is connected to the port, disable or enable lpm
123 * to make new u1 u2 setting take effect immediately.
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);
137 return count;
139 static DEVICE_ATTR_RW(usb3_lpm_permit);
141 static struct attribute *port_dev_attrs[] = {
142 &dev_attr_connect_type.attr,
143 &dev_attr_quirks.attr,
144 &dev_attr_over_current_count.attr,
145 NULL,
148 static struct attribute_group port_dev_attr_grp = {
149 .attrs = port_dev_attrs,
152 static const struct attribute_group *port_dev_group[] = {
153 &port_dev_attr_grp,
154 NULL,
157 static struct attribute *port_dev_usb3_attrs[] = {
158 &dev_attr_usb3_lpm_permit.attr,
159 NULL,
162 static struct attribute_group port_dev_usb3_attr_grp = {
163 .attrs = port_dev_usb3_attrs,
166 static const struct attribute_group *port_dev_usb3_group[] = {
167 &port_dev_attr_grp,
168 &port_dev_usb3_attr_grp,
169 NULL,
172 static void usb_port_device_release(struct device *dev)
174 struct usb_port *port_dev = to_usb_port(dev);
176 kfree(port_dev->req);
177 kfree(port_dev);
180 #ifdef CONFIG_PM
181 static int usb_port_runtime_resume(struct device *dev)
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);
186 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
187 struct usb_device *udev = port_dev->child;
188 struct usb_port *peer = port_dev->peer;
189 int port1 = port_dev->portnum;
190 int retval;
192 if (!hub)
193 return -EINVAL;
194 if (hub->in_reset) {
195 set_bit(port1, hub->power_bits);
196 return 0;
200 * Power on our usb3 peer before this usb2 port to prevent a usb3
201 * device from degrading to its usb2 connection
203 if (!port_dev->is_superspeed && peer)
204 pm_runtime_get_sync(&peer->dev);
206 usb_autopm_get_interface(intf);
207 retval = usb_hub_set_port_power(hdev, hub, port1, true);
208 msleep(hub_power_on_good_delay(hub));
209 if (udev && !retval) {
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())
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);
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);
232 usb_autopm_put_interface(intf);
234 return retval;
237 static int usb_port_runtime_suspend(struct device *dev)
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);
242 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
243 struct usb_port *peer = port_dev->peer;
244 int port1 = port_dev->portnum;
245 int retval;
247 if (!hub)
248 return -EINVAL;
249 if (hub->in_reset)
250 return -EBUSY;
252 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
253 == PM_QOS_FLAGS_ALL)
254 return -EAGAIN;
256 if (usb_port_block_power_off)
257 return -EBUSY;
259 usb_autopm_get_interface(intf);
260 retval = usb_hub_set_port_power(hdev, hub, port1, false);
261 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
262 if (!port_dev->is_superspeed)
263 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
264 usb_autopm_put_interface(intf);
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.
271 if (!port_dev->is_superspeed && peer)
272 pm_runtime_put(&peer->dev);
274 return retval;
276 #endif
278 static const struct dev_pm_ops usb_port_pm_ops = {
279 #ifdef CONFIG_PM
280 .runtime_suspend = usb_port_runtime_suspend,
281 .runtime_resume = usb_port_runtime_resume,
282 #endif
285 struct device_type usb_port_device_type = {
286 .name = "usb_port",
287 .release = usb_port_device_release,
288 .pm = &usb_port_pm_ops,
291 static struct device_driver usb_port_driver = {
292 .name = "usb",
293 .owner = THIS_MODULE,
296 static int link_peers(struct usb_port *left, struct usb_port *right)
298 struct usb_port *ss_port, *hs_port;
299 int rc;
301 if (left->peer == right && right->peer == left)
302 return 0;
304 if (left->peer || right->peer) {
305 struct usb_port *lpeer = left->peer;
306 struct usb_port *rpeer = right->peer;
307 char *method;
309 if (left->location && left->location == right->location)
310 method = "location";
311 else
312 method = "default";
314 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
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");
320 return -EBUSY;
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;
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.
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;
346 pm_runtime_get_sync(&hs_port->dev);
348 left->peer = right;
349 right->peer = left;
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.
357 * Also, drop the HiSpeed ref taken above.
359 pm_runtime_get_sync(&ss_port->dev);
360 pm_runtime_put(&hs_port->dev);
362 return 0;
365 static void link_peers_report(struct usb_port *left, struct usb_port *right)
367 int rc;
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 {
373 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
374 dev_name(&right->dev), rc);
375 pr_warn_once("usb: port power management may be unreliable\n");
376 usb_port_block_power_off = 1;
380 static void unlink_peers(struct usb_port *left, struct usb_port *right)
382 struct usb_port *ss_port, *hs_port;
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));
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.
393 if (left->is_superspeed) {
394 ss_port = left;
395 hs_port = right;
396 } else {
397 ss_port = right;
398 hs_port = left;
401 pm_runtime_get_sync(&hs_port->dev);
403 sysfs_remove_link(&left->dev.kobj, "peer");
404 right->peer = NULL;
405 sysfs_remove_link(&right->dev.kobj, "peer");
406 left->peer = NULL;
408 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
409 pm_runtime_put(&ss_port->dev);
411 /* Drop the ref taken above */
412 pm_runtime_put(&hs_port->dev);
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
420 static int match_location(struct usb_device *peer_hdev, void *p)
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);
428 if (!peer_hub)
429 return 0;
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;
437 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
438 peer = peer_hub->ports[port1 - 1];
439 if (peer && peer->location == port_dev->location) {
440 link_peers_report(port_dev, peer);
441 return 1; /* done */
445 return 0;
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.
453 static void find_and_link_peer(struct usb_hub *hub, int port1)
455 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
456 struct usb_device *hdev = hub->hdev;
457 struct usb_device *peer_hdev;
458 struct usb_hub *peer_hub;
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)
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) {
471 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
472 struct usb_hcd *peer_hcd = hcd->shared_hcd;
474 if (!peer_hcd)
475 return;
477 peer_hdev = peer_hcd->self.root_hub;
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);
483 if (!parent_hub)
484 return;
486 upstream = parent_hub->ports[hdev->portnum - 1];
487 if (!upstream || !upstream->peer)
488 return;
490 peer_hdev = upstream->peer->child;
493 peer_hub = usb_hub_to_struct_hub(peer_hdev);
494 if (!peer_hub || port1 > peer_hdev->maxchild)
495 return;
498 * we found a valid default peer, last check is to make sure it
499 * does not have location data
501 peer = peer_hub->ports[port1 - 1];
502 if (peer && peer->location == 0)
503 link_peers_report(port_dev, peer);
506 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
508 struct usb_port *port_dev;
509 struct usb_device *hdev = hub->hdev;
510 int retval;
512 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
513 if (!port_dev)
514 return -ENOMEM;
516 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
517 if (!port_dev->req) {
518 kfree(port_dev);
519 return -ENOMEM;
522 hub->ports[port1 - 1] = port_dev;
523 port_dev->portnum = port1;
524 set_bit(port1, hub->power_bits);
525 port_dev->dev.parent = hub->intfdev;
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;
532 port_dev->dev.type = &usb_port_device_type;
533 port_dev->dev.driver = &usb_port_driver;
534 if (hub_is_superspeed(hub->hdev))
535 port_dev->is_superspeed = 1;
536 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
537 port1);
538 mutex_init(&port_dev->status_lock);
539 retval = device_register(&port_dev->dev);
540 if (retval) {
541 put_device(&port_dev->dev);
542 return retval;
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;
553 find_and_link_peer(hub, port1);
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).
560 pm_runtime_set_active(&port_dev->dev);
561 pm_runtime_get_noresume(&port_dev->dev);
562 pm_runtime_enable(&port_dev->dev);
563 device_enable_async_suspend(&port_dev->dev);
566 * Keep hidden the ability to enable port-poweroff if the hub
567 * does not support power switching.
569 if (!hub_is_port_power_switchable(hub))
570 return 0;
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;
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;
586 return 0;
589 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
591 struct usb_port *port_dev = hub->ports[port1 - 1];
592 struct usb_port *peer;
594 peer = port_dev->peer;
595 if (peer)
596 unlink_peers(port_dev, peer);
597 device_unregister(&port_dev->dev);