GNU Linux-libre 4.19.264-gnu1
[releases.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/slab.h>
11 #include <linux/pm_qos.h>
12
13 #include "hub.h"
14
15 static int usb_port_block_power_off;
16
17 static const struct attribute_group *port_dev_group[];
18
19 static ssize_t connect_type_show(struct device *dev,
20                                  struct device_attribute *attr, char *buf)
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 }
42 static DEVICE_ATTR_RO(connect_type);
43
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
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
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
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,
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
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
172 static void usb_port_device_release(struct device *dev)
173 {
174         struct usb_port *port_dev = to_usb_port(dev);
175
176         kfree(port_dev->req);
177         kfree(port_dev);
178 }
179
180 #ifdef CONFIG_PM
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);
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;
191
192         if (!hub)
193                 return -EINVAL;
194         if (hub->in_reset) {
195                 set_bit(port1, hub->power_bits);
196                 return 0;
197         }
198
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
206         retval = usb_autopm_get_interface(intf);
207         if (retval < 0)
208                 return retval;
209
210         retval = usb_hub_set_port_power(hdev, hub, port1, true);
211         msleep(hub_power_on_good_delay(hub));
212         if (udev && !retval) {
213                 /*
214                  * Our preference is to simply wait for the port to reconnect,
215                  * as that is the lowest latency method to restart the port.
216                  * However, there are cases where toggling port power results in
217                  * the host port and the device port getting out of sync causing
218                  * a link training live lock.  Upon timeout, flag the port as
219                  * needing warm reset recovery (to be performed later by
220                  * usb_port_resume() as requested via usb_wakeup_notification())
221                  */
222                 if (hub_port_debounce_be_connected(hub, port1) < 0) {
223                         dev_dbg(&port_dev->dev, "reconnect timeout\n");
224                         if (hub_is_superspeed(hdev))
225                                 set_bit(port1, hub->warm_reset_bits);
226                 }
227
228                 /* Force the child awake to revalidate after the power loss. */
229                 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
230                         pm_runtime_get_noresume(&port_dev->dev);
231                         pm_request_resume(&udev->dev);
232                 }
233         }
234
235         usb_autopm_put_interface(intf);
236
237         return retval;
238 }
239
240 static int usb_port_runtime_suspend(struct device *dev)
241 {
242         struct usb_port *port_dev = to_usb_port(dev);
243         struct usb_device *hdev = to_usb_device(dev->parent->parent);
244         struct usb_interface *intf = to_usb_interface(dev->parent);
245         struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
246         struct usb_port *peer = port_dev->peer;
247         int port1 = port_dev->portnum;
248         int retval;
249
250         if (!hub)
251                 return -EINVAL;
252         if (hub->in_reset)
253                 return -EBUSY;
254
255         if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
256                         == PM_QOS_FLAGS_ALL)
257                 return -EAGAIN;
258
259         if (usb_port_block_power_off)
260                 return -EBUSY;
261
262         retval = usb_autopm_get_interface(intf);
263         if (retval < 0)
264                 return retval;
265
266         retval = usb_hub_set_port_power(hdev, hub, port1, false);
267         usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
268         if (!port_dev->is_superspeed)
269                 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
270         usb_autopm_put_interface(intf);
271
272         /*
273          * Our peer usb3 port may now be able to suspend, so
274          * asynchronously queue a suspend request to observe that this
275          * usb2 port is now off.
276          */
277         if (!port_dev->is_superspeed && peer)
278                 pm_runtime_put(&peer->dev);
279
280         return retval;
281 }
282 #endif
283
284 static const struct dev_pm_ops usb_port_pm_ops = {
285 #ifdef CONFIG_PM
286         .runtime_suspend =      usb_port_runtime_suspend,
287         .runtime_resume =       usb_port_runtime_resume,
288 #endif
289 };
290
291 struct device_type usb_port_device_type = {
292         .name =         "usb_port",
293         .release =      usb_port_device_release,
294         .pm =           &usb_port_pm_ops,
295 };
296
297 static struct device_driver usb_port_driver = {
298         .name = "usb",
299         .owner = THIS_MODULE,
300 };
301
302 static int link_peers(struct usb_port *left, struct usb_port *right)
303 {
304         struct usb_port *ss_port, *hs_port;
305         int rc;
306
307         if (left->peer == right && right->peer == left)
308                 return 0;
309
310         if (left->peer || right->peer) {
311                 struct usb_port *lpeer = left->peer;
312                 struct usb_port *rpeer = right->peer;
313                 char *method;
314
315                 if (left->location && left->location == right->location)
316                         method = "location";
317                 else
318                         method = "default";
319
320                 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
321                         dev_name(&left->dev), dev_name(&right->dev), method,
322                         dev_name(&left->dev),
323                         lpeer ? dev_name(&lpeer->dev) : "none",
324                         dev_name(&right->dev),
325                         rpeer ? dev_name(&rpeer->dev) : "none");
326                 return -EBUSY;
327         }
328
329         rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
330         if (rc)
331                 return rc;
332         rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
333         if (rc) {
334                 sysfs_remove_link(&left->dev.kobj, "peer");
335                 return rc;
336         }
337
338         /*
339          * We need to wake the HiSpeed port to make sure we don't race
340          * setting ->peer with usb_port_runtime_suspend().  Otherwise we
341          * may miss a suspend event for the SuperSpeed port.
342          */
343         if (left->is_superspeed) {
344                 ss_port = left;
345                 WARN_ON(right->is_superspeed);
346                 hs_port = right;
347         } else {
348                 ss_port = right;
349                 WARN_ON(!right->is_superspeed);
350                 hs_port = left;
351         }
352         pm_runtime_get_sync(&hs_port->dev);
353
354         left->peer = right;
355         right->peer = left;
356
357         /*
358          * The SuperSpeed reference is dropped when the HiSpeed port in
359          * this relationship suspends, i.e. when it is safe to allow a
360          * SuperSpeed connection to drop since there is no risk of a
361          * device degrading to its powered-off HiSpeed connection.
362          *
363          * Also, drop the HiSpeed ref taken above.
364          */
365         pm_runtime_get_sync(&ss_port->dev);
366         pm_runtime_put(&hs_port->dev);
367
368         return 0;
369 }
370
371 static void link_peers_report(struct usb_port *left, struct usb_port *right)
372 {
373         int rc;
374
375         rc = link_peers(left, right);
376         if (rc == 0) {
377                 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
378         } else {
379                 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
380                                 dev_name(&right->dev), rc);
381                 pr_warn_once("usb: port power management may be unreliable\n");
382                 usb_port_block_power_off = 1;
383         }
384 }
385
386 static void unlink_peers(struct usb_port *left, struct usb_port *right)
387 {
388         struct usb_port *ss_port, *hs_port;
389
390         WARN(right->peer != left || left->peer != right,
391                         "%s and %s are not peers?\n",
392                         dev_name(&left->dev), dev_name(&right->dev));
393
394         /*
395          * We wake the HiSpeed port to make sure we don't race its
396          * usb_port_runtime_resume() event which takes a SuperSpeed ref
397          * when ->peer is !NULL.
398          */
399         if (left->is_superspeed) {
400                 ss_port = left;
401                 hs_port = right;
402         } else {
403                 ss_port = right;
404                 hs_port = left;
405         }
406
407         pm_runtime_get_sync(&hs_port->dev);
408
409         sysfs_remove_link(&left->dev.kobj, "peer");
410         right->peer = NULL;
411         sysfs_remove_link(&right->dev.kobj, "peer");
412         left->peer = NULL;
413
414         /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
415         pm_runtime_put(&ss_port->dev);
416
417         /* Drop the ref taken above */
418         pm_runtime_put(&hs_port->dev);
419 }
420
421 /*
422  * For each usb hub device in the system check to see if it is in the
423  * peer domain of the given port_dev, and if it is check to see if it
424  * has a port that matches the given port by location
425  */
426 static int match_location(struct usb_device *peer_hdev, void *p)
427 {
428         int port1;
429         struct usb_hcd *hcd, *peer_hcd;
430         struct usb_port *port_dev = p, *peer;
431         struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
432         struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
433
434         if (!peer_hub)
435                 return 0;
436
437         hcd = bus_to_hcd(hdev->bus);
438         peer_hcd = bus_to_hcd(peer_hdev->bus);
439         /* peer_hcd is provisional until we verify it against the known peer */
440         if (peer_hcd != hcd->shared_hcd)
441                 return 0;
442
443         for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
444                 peer = peer_hub->ports[port1 - 1];
445                 if (peer && peer->location == port_dev->location) {
446                         link_peers_report(port_dev, peer);
447                         return 1; /* done */
448                 }
449         }
450
451         return 0;
452 }
453
454 /*
455  * Find the peer port either via explicit platform firmware "location"
456  * data, the peer hcd for root hubs, or the upstream peer relationship
457  * for all other hubs.
458  */
459 static void find_and_link_peer(struct usb_hub *hub, int port1)
460 {
461         struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
462         struct usb_device *hdev = hub->hdev;
463         struct usb_device *peer_hdev;
464         struct usb_hub *peer_hub;
465
466         /*
467          * If location data is available then we can only peer this port
468          * by a location match, not the default peer (lest we create a
469          * situation where we need to go back and undo a default peering
470          * when the port is later peered by location data)
471          */
472         if (port_dev->location) {
473                 /* we link the peer in match_location() if found */
474                 usb_for_each_dev(port_dev, match_location);
475                 return;
476         } else if (!hdev->parent) {
477                 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
478                 struct usb_hcd *peer_hcd = hcd->shared_hcd;
479
480                 if (!peer_hcd)
481                         return;
482
483                 peer_hdev = peer_hcd->self.root_hub;
484         } else {
485                 struct usb_port *upstream;
486                 struct usb_device *parent = hdev->parent;
487                 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
488
489                 if (!parent_hub)
490                         return;
491
492                 upstream = parent_hub->ports[hdev->portnum - 1];
493                 if (!upstream || !upstream->peer)
494                         return;
495
496                 peer_hdev = upstream->peer->child;
497         }
498
499         peer_hub = usb_hub_to_struct_hub(peer_hdev);
500         if (!peer_hub || port1 > peer_hdev->maxchild)
501                 return;
502
503         /*
504          * we found a valid default peer, last check is to make sure it
505          * does not have location data
506          */
507         peer = peer_hub->ports[port1 - 1];
508         if (peer && peer->location == 0)
509                 link_peers_report(port_dev, peer);
510 }
511
512 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
513 {
514         struct usb_port *port_dev;
515         struct usb_device *hdev = hub->hdev;
516         int retval;
517
518         port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
519         if (!port_dev)
520                 return -ENOMEM;
521
522         port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
523         if (!port_dev->req) {
524                 kfree(port_dev);
525                 return -ENOMEM;
526         }
527
528         hub->ports[port1 - 1] = port_dev;
529         port_dev->portnum = port1;
530         set_bit(port1, hub->power_bits);
531         port_dev->dev.parent = hub->intfdev;
532         if (hub_is_superspeed(hdev)) {
533                 port_dev->usb3_lpm_u1_permit = 1;
534                 port_dev->usb3_lpm_u2_permit = 1;
535                 port_dev->dev.groups = port_dev_usb3_group;
536         } else
537                 port_dev->dev.groups = port_dev_group;
538         port_dev->dev.type = &usb_port_device_type;
539         port_dev->dev.driver = &usb_port_driver;
540         if (hub_is_superspeed(hub->hdev))
541                 port_dev->is_superspeed = 1;
542         dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
543                         port1);
544         mutex_init(&port_dev->status_lock);
545         retval = device_register(&port_dev->dev);
546         if (retval) {
547                 put_device(&port_dev->dev);
548                 return retval;
549         }
550
551         /* Set default policy of port-poweroff disabled. */
552         retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
553                         DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
554         if (retval < 0) {
555                 device_unregister(&port_dev->dev);
556                 return retval;
557         }
558
559         find_and_link_peer(hub, port1);
560
561         /*
562          * Enable runtime pm and hold a refernce that hub_configure()
563          * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
564          * and the hub has been fully registered (hdev->maxchild set).
565          */
566         pm_runtime_set_active(&port_dev->dev);
567         pm_runtime_get_noresume(&port_dev->dev);
568         pm_runtime_enable(&port_dev->dev);
569         device_enable_async_suspend(&port_dev->dev);
570
571         /*
572          * Keep hidden the ability to enable port-poweroff if the hub
573          * does not support power switching.
574          */
575         if (!hub_is_port_power_switchable(hub))
576                 return 0;
577
578         /* Attempt to let userspace take over the policy. */
579         retval = dev_pm_qos_expose_flags(&port_dev->dev,
580                         PM_QOS_FLAG_NO_POWER_OFF);
581         if (retval < 0) {
582                 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
583                 return 0;
584         }
585
586         /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
587         retval = dev_pm_qos_remove_request(port_dev->req);
588         if (retval >= 0) {
589                 kfree(port_dev->req);
590                 port_dev->req = NULL;
591         }
592         return 0;
593 }
594
595 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
596 {
597         struct usb_port *port_dev = hub->ports[port1 - 1];
598         struct usb_port *peer;
599
600         peer = port_dev->peer;
601         if (peer)
602                 unlink_peers(port_dev, peer);
603         device_unregister(&port_dev->dev);
604 }