dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / usb / core / port.c
blob1a06a4b5fbb14d592b6da8ed2cab8ebd123eb851
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 location_show(struct device *dev,
20 struct device_attribute *attr, char *buf)
22 struct usb_port *port_dev = to_usb_port(dev);
24 return sprintf(buf, "0x%08x\n", port_dev->location);
26 static DEVICE_ATTR_RO(location);
28 static ssize_t connect_type_show(struct device *dev,
29 struct device_attribute *attr, char *buf)
31 struct usb_port *port_dev = to_usb_port(dev);
32 char *result;
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;
49 return sprintf(buf, "%s\n", result);
51 static DEVICE_ATTR_RO(connect_type);
53 static ssize_t over_current_count_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, "%u\n", port_dev->over_current_count);
60 static DEVICE_ATTR_RO(over_current_count);
62 static ssize_t quirks_show(struct device *dev,
63 struct device_attribute *attr, char *buf)
65 struct usb_port *port_dev = to_usb_port(dev);
67 return sprintf(buf, "%08x\n", port_dev->quirks);
70 static ssize_t quirks_store(struct device *dev, struct device_attribute *attr,
71 const char *buf, size_t count)
73 struct usb_port *port_dev = to_usb_port(dev);
74 u32 value;
76 if (kstrtou32(buf, 16, &value))
77 return -EINVAL;
79 port_dev->quirks = value;
80 return count;
82 static DEVICE_ATTR_RW(quirks);
84 static ssize_t usb3_lpm_permit_show(struct device *dev,
85 struct device_attribute *attr, char *buf)
87 struct usb_port *port_dev = to_usb_port(dev);
88 const char *p;
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";
102 return sprintf(buf, "%s\n", p);
105 static ssize_t usb3_lpm_permit_store(struct device *dev,
106 struct device_attribute *attr,
107 const char *buf, size_t count)
109 struct usb_port *port_dev = to_usb_port(dev);
110 struct usb_device *udev = port_dev->child;
111 struct usb_hcd *hcd;
113 if (!strncmp(buf, "u1_u2", 5)) {
114 port_dev->usb3_lpm_u1_permit = 1;
115 port_dev->usb3_lpm_u2_permit = 1;
117 } else if (!strncmp(buf, "u1", 2)) {
118 port_dev->usb3_lpm_u1_permit = 1;
119 port_dev->usb3_lpm_u2_permit = 0;
121 } else if (!strncmp(buf, "u2", 2)) {
122 port_dev->usb3_lpm_u1_permit = 0;
123 port_dev->usb3_lpm_u2_permit = 1;
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;
131 /* If device is connected to the port, disable or enable lpm
132 * to make new u1 u2 setting take effect immediately.
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);
146 return count;
148 static DEVICE_ATTR_RW(usb3_lpm_permit);
150 static struct attribute *port_dev_attrs[] = {
151 &dev_attr_connect_type.attr,
152 &dev_attr_location.attr,
153 &dev_attr_quirks.attr,
154 &dev_attr_over_current_count.attr,
155 NULL,
158 static struct attribute_group port_dev_attr_grp = {
159 .attrs = port_dev_attrs,
162 static const struct attribute_group *port_dev_group[] = {
163 &port_dev_attr_grp,
164 NULL,
167 static struct attribute *port_dev_usb3_attrs[] = {
168 &dev_attr_usb3_lpm_permit.attr,
169 NULL,
172 static struct attribute_group port_dev_usb3_attr_grp = {
173 .attrs = port_dev_usb3_attrs,
176 static const struct attribute_group *port_dev_usb3_group[] = {
177 &port_dev_attr_grp,
178 &port_dev_usb3_attr_grp,
179 NULL,
182 static void usb_port_device_release(struct device *dev)
184 struct usb_port *port_dev = to_usb_port(dev);
186 kfree(port_dev->req);
187 kfree(port_dev);
190 #ifdef CONFIG_PM
191 static int usb_port_runtime_resume(struct device *dev)
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);
196 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
197 struct usb_device *udev = port_dev->child;
198 struct usb_port *peer = port_dev->peer;
199 int port1 = port_dev->portnum;
200 int retval;
202 if (!hub)
203 return -EINVAL;
204 if (hub->in_reset) {
205 set_bit(port1, hub->power_bits);
206 return 0;
210 * Power on our usb3 peer before this usb2 port to prevent a usb3
211 * device from degrading to its usb2 connection
213 if (!port_dev->is_superspeed && peer)
214 pm_runtime_get_sync(&peer->dev);
216 usb_autopm_get_interface(intf);
217 retval = usb_hub_set_port_power(hdev, hub, port1, true);
218 msleep(hub_power_on_good_delay(hub));
219 if (udev && !retval) {
221 * Our preference is to simply wait for the port to reconnect,
222 * as that is the lowest latency method to restart the port.
223 * However, there are cases where toggling port power results in
224 * the host port and the device port getting out of sync causing
225 * a link training live lock. Upon timeout, flag the port as
226 * needing warm reset recovery (to be performed later by
227 * usb_port_resume() as requested via usb_wakeup_notification())
229 if (hub_port_debounce_be_connected(hub, port1) < 0) {
230 dev_dbg(&port_dev->dev, "reconnect timeout\n");
231 if (hub_is_superspeed(hdev))
232 set_bit(port1, hub->warm_reset_bits);
235 /* Force the child awake to revalidate after the power loss. */
236 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
237 pm_runtime_get_noresume(&port_dev->dev);
238 pm_request_resume(&udev->dev);
242 usb_autopm_put_interface(intf);
244 return retval;
247 static int usb_port_runtime_suspend(struct device *dev)
249 struct usb_port *port_dev = to_usb_port(dev);
250 struct usb_device *hdev = to_usb_device(dev->parent->parent);
251 struct usb_interface *intf = to_usb_interface(dev->parent);
252 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
253 struct usb_port *peer = port_dev->peer;
254 int port1 = port_dev->portnum;
255 int retval;
257 if (!hub)
258 return -EINVAL;
259 if (hub->in_reset)
260 return -EBUSY;
262 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
263 == PM_QOS_FLAGS_ALL)
264 return -EAGAIN;
266 if (usb_port_block_power_off)
267 return -EBUSY;
269 usb_autopm_get_interface(intf);
270 retval = usb_hub_set_port_power(hdev, hub, port1, false);
271 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
272 if (!port_dev->is_superspeed)
273 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
274 usb_autopm_put_interface(intf);
277 * Our peer usb3 port may now be able to suspend, so
278 * asynchronously queue a suspend request to observe that this
279 * usb2 port is now off.
281 if (!port_dev->is_superspeed && peer)
282 pm_runtime_put(&peer->dev);
284 return retval;
286 #endif
288 static const struct dev_pm_ops usb_port_pm_ops = {
289 #ifdef CONFIG_PM
290 .runtime_suspend = usb_port_runtime_suspend,
291 .runtime_resume = usb_port_runtime_resume,
292 #endif
295 struct device_type usb_port_device_type = {
296 .name = "usb_port",
297 .release = usb_port_device_release,
298 .pm = &usb_port_pm_ops,
301 static struct device_driver usb_port_driver = {
302 .name = "usb",
303 .owner = THIS_MODULE,
306 static int link_peers(struct usb_port *left, struct usb_port *right)
308 struct usb_port *ss_port, *hs_port;
309 int rc;
311 if (left->peer == right && right->peer == left)
312 return 0;
314 if (left->peer || right->peer) {
315 struct usb_port *lpeer = left->peer;
316 struct usb_port *rpeer = right->peer;
317 char *method;
319 if (left->location && left->location == right->location)
320 method = "location";
321 else
322 method = "default";
324 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
325 dev_name(&left->dev), dev_name(&right->dev), method,
326 dev_name(&left->dev),
327 lpeer ? dev_name(&lpeer->dev) : "none",
328 dev_name(&right->dev),
329 rpeer ? dev_name(&rpeer->dev) : "none");
330 return -EBUSY;
333 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
334 if (rc)
335 return rc;
336 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
337 if (rc) {
338 sysfs_remove_link(&left->dev.kobj, "peer");
339 return rc;
343 * We need to wake the HiSpeed port to make sure we don't race
344 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
345 * may miss a suspend event for the SuperSpeed port.
347 if (left->is_superspeed) {
348 ss_port = left;
349 WARN_ON(right->is_superspeed);
350 hs_port = right;
351 } else {
352 ss_port = right;
353 WARN_ON(!right->is_superspeed);
354 hs_port = left;
356 pm_runtime_get_sync(&hs_port->dev);
358 left->peer = right;
359 right->peer = left;
362 * The SuperSpeed reference is dropped when the HiSpeed port in
363 * this relationship suspends, i.e. when it is safe to allow a
364 * SuperSpeed connection to drop since there is no risk of a
365 * device degrading to its powered-off HiSpeed connection.
367 * Also, drop the HiSpeed ref taken above.
369 pm_runtime_get_sync(&ss_port->dev);
370 pm_runtime_put(&hs_port->dev);
372 return 0;
375 static void link_peers_report(struct usb_port *left, struct usb_port *right)
377 int rc;
379 rc = link_peers(left, right);
380 if (rc == 0) {
381 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
382 } else {
383 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
384 dev_name(&right->dev), rc);
385 pr_warn_once("usb: port power management may be unreliable\n");
386 usb_port_block_power_off = 1;
390 static void unlink_peers(struct usb_port *left, struct usb_port *right)
392 struct usb_port *ss_port, *hs_port;
394 WARN(right->peer != left || left->peer != right,
395 "%s and %s are not peers?\n",
396 dev_name(&left->dev), dev_name(&right->dev));
399 * We wake the HiSpeed port to make sure we don't race its
400 * usb_port_runtime_resume() event which takes a SuperSpeed ref
401 * when ->peer is !NULL.
403 if (left->is_superspeed) {
404 ss_port = left;
405 hs_port = right;
406 } else {
407 ss_port = right;
408 hs_port = left;
411 pm_runtime_get_sync(&hs_port->dev);
413 sysfs_remove_link(&left->dev.kobj, "peer");
414 right->peer = NULL;
415 sysfs_remove_link(&right->dev.kobj, "peer");
416 left->peer = NULL;
418 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
419 pm_runtime_put(&ss_port->dev);
421 /* Drop the ref taken above */
422 pm_runtime_put(&hs_port->dev);
426 * For each usb hub device in the system check to see if it is in the
427 * peer domain of the given port_dev, and if it is check to see if it
428 * has a port that matches the given port by location
430 static int match_location(struct usb_device *peer_hdev, void *p)
432 int port1;
433 struct usb_hcd *hcd, *peer_hcd;
434 struct usb_port *port_dev = p, *peer;
435 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
436 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
438 if (!peer_hub)
439 return 0;
441 hcd = bus_to_hcd(hdev->bus);
442 peer_hcd = bus_to_hcd(peer_hdev->bus);
443 /* peer_hcd is provisional until we verify it against the known peer */
444 if (peer_hcd != hcd->shared_hcd)
445 return 0;
447 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
448 peer = peer_hub->ports[port1 - 1];
449 if (peer && peer->location == port_dev->location) {
450 link_peers_report(port_dev, peer);
451 return 1; /* done */
455 return 0;
459 * Find the peer port either via explicit platform firmware "location"
460 * data, the peer hcd for root hubs, or the upstream peer relationship
461 * for all other hubs.
463 static void find_and_link_peer(struct usb_hub *hub, int port1)
465 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
466 struct usb_device *hdev = hub->hdev;
467 struct usb_device *peer_hdev;
468 struct usb_hub *peer_hub;
471 * If location data is available then we can only peer this port
472 * by a location match, not the default peer (lest we create a
473 * situation where we need to go back and undo a default peering
474 * when the port is later peered by location data)
476 if (port_dev->location) {
477 /* we link the peer in match_location() if found */
478 usb_for_each_dev(port_dev, match_location);
479 return;
480 } else if (!hdev->parent) {
481 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
482 struct usb_hcd *peer_hcd = hcd->shared_hcd;
484 if (!peer_hcd)
485 return;
487 peer_hdev = peer_hcd->self.root_hub;
488 } else {
489 struct usb_port *upstream;
490 struct usb_device *parent = hdev->parent;
491 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
493 if (!parent_hub)
494 return;
496 upstream = parent_hub->ports[hdev->portnum - 1];
497 if (!upstream || !upstream->peer)
498 return;
500 peer_hdev = upstream->peer->child;
503 peer_hub = usb_hub_to_struct_hub(peer_hdev);
504 if (!peer_hub || port1 > peer_hdev->maxchild)
505 return;
508 * we found a valid default peer, last check is to make sure it
509 * does not have location data
511 peer = peer_hub->ports[port1 - 1];
512 if (peer && peer->location == 0)
513 link_peers_report(port_dev, peer);
516 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
518 struct usb_port *port_dev;
519 struct usb_device *hdev = hub->hdev;
520 int retval;
522 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
523 if (!port_dev)
524 return -ENOMEM;
526 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
527 if (!port_dev->req) {
528 kfree(port_dev);
529 return -ENOMEM;
532 hub->ports[port1 - 1] = port_dev;
533 port_dev->portnum = port1;
534 set_bit(port1, hub->power_bits);
535 port_dev->dev.parent = hub->intfdev;
536 if (hub_is_superspeed(hdev)) {
537 port_dev->usb3_lpm_u1_permit = 1;
538 port_dev->usb3_lpm_u2_permit = 1;
539 port_dev->dev.groups = port_dev_usb3_group;
540 } else
541 port_dev->dev.groups = port_dev_group;
542 port_dev->dev.type = &usb_port_device_type;
543 port_dev->dev.driver = &usb_port_driver;
544 if (hub_is_superspeed(hub->hdev))
545 port_dev->is_superspeed = 1;
546 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
547 port1);
548 mutex_init(&port_dev->status_lock);
549 retval = device_register(&port_dev->dev);
550 if (retval) {
551 put_device(&port_dev->dev);
552 return retval;
555 /* Set default policy of port-poweroff disabled. */
556 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
557 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
558 if (retval < 0) {
559 device_unregister(&port_dev->dev);
560 return retval;
563 find_and_link_peer(hub, port1);
566 * Enable runtime pm and hold a refernce that hub_configure()
567 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
568 * and the hub has been fully registered (hdev->maxchild set).
570 pm_runtime_set_active(&port_dev->dev);
571 pm_runtime_get_noresume(&port_dev->dev);
572 pm_runtime_enable(&port_dev->dev);
573 device_enable_async_suspend(&port_dev->dev);
576 * Keep hidden the ability to enable port-poweroff if the hub
577 * does not support power switching.
579 if (!hub_is_port_power_switchable(hub))
580 return 0;
582 /* Attempt to let userspace take over the policy. */
583 retval = dev_pm_qos_expose_flags(&port_dev->dev,
584 PM_QOS_FLAG_NO_POWER_OFF);
585 if (retval < 0) {
586 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
587 return 0;
590 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
591 retval = dev_pm_qos_remove_request(port_dev->req);
592 if (retval >= 0) {
593 kfree(port_dev->req);
594 port_dev->req = NULL;
596 return 0;
599 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
601 struct usb_port *port_dev = hub->ports[port1 - 1];
602 struct usb_port *peer;
604 peer = port_dev->peer;
605 if (peer)
606 unlink_peers(port_dev, peer);
607 device_unregister(&port_dev->dev);