GNU Linux-libre 4.4.283-gnu1
[releases.git] / drivers / usb / core / port.c
1 /*
2  * usb port device code
3  *
4  * Copyright (C) 2012 Intel Corp
5  *
6  * Author: Lan Tianyu <tianyu.lan@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * for more details.
16  *
17  */
18
19 #include <linux/slab.h>
20 #include <linux/pm_qos.h>
21
22 #include "hub.h"
23
24 static int usb_port_block_power_off;
25
26 static const struct attribute_group *port_dev_group[];
27
28 static ssize_t connect_type_show(struct device *dev,
29                                  struct device_attribute *attr, char *buf)
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 }
51 static DEVICE_ATTR_RO(connect_type);
52
53 static struct attribute *port_dev_attrs[] = {
54         &dev_attr_connect_type.attr,
55         NULL,
56 };
57
58 static struct attribute_group port_dev_attr_grp = {
59         .attrs = port_dev_attrs,
60 };
61
62 static const struct attribute_group *port_dev_group[] = {
63         &port_dev_attr_grp,
64         NULL,
65 };
66
67 static void usb_port_device_release(struct device *dev)
68 {
69         struct usb_port *port_dev = to_usb_port(dev);
70
71         kfree(port_dev->req);
72         kfree(port_dev);
73 }
74
75 #ifdef CONFIG_PM
76 static int usb_port_runtime_resume(struct device *dev)
77 {
78         struct usb_port *port_dev = to_usb_port(dev);
79         struct usb_device *hdev = to_usb_device(dev->parent->parent);
80         struct usb_interface *intf = to_usb_interface(dev->parent);
81         struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
82         struct usb_device *udev = port_dev->child;
83         struct usb_port *peer = port_dev->peer;
84         int port1 = port_dev->portnum;
85         int retval;
86
87         if (!hub)
88                 return -EINVAL;
89         if (hub->in_reset) {
90                 set_bit(port1, hub->power_bits);
91                 return 0;
92         }
93
94         /*
95          * Power on our usb3 peer before this usb2 port to prevent a usb3
96          * device from degrading to its usb2 connection
97          */
98         if (!port_dev->is_superspeed && peer)
99                 pm_runtime_get_sync(&peer->dev);
100
101         retval = usb_autopm_get_interface(intf);
102         if (retval < 0)
103                 return retval;
104
105         retval = usb_hub_set_port_power(hdev, hub, port1, true);
106         msleep(hub_power_on_good_delay(hub));
107         if (udev && !retval) {
108                 /*
109                  * Our preference is to simply wait for the port to reconnect,
110                  * as that is the lowest latency method to restart the port.
111                  * However, there are cases where toggling port power results in
112                  * the host port and the device port getting out of sync causing
113                  * a link training live lock.  Upon timeout, flag the port as
114                  * needing warm reset recovery (to be performed later by
115                  * usb_port_resume() as requested via usb_wakeup_notification())
116                  */
117                 if (hub_port_debounce_be_connected(hub, port1) < 0) {
118                         dev_dbg(&port_dev->dev, "reconnect timeout\n");
119                         if (hub_is_superspeed(hdev))
120                                 set_bit(port1, hub->warm_reset_bits);
121                 }
122
123                 /* Force the child awake to revalidate after the power loss. */
124                 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
125                         pm_runtime_get_noresume(&port_dev->dev);
126                         pm_request_resume(&udev->dev);
127                 }
128         }
129
130         usb_autopm_put_interface(intf);
131
132         return retval;
133 }
134
135 static int usb_port_runtime_suspend(struct device *dev)
136 {
137         struct usb_port *port_dev = to_usb_port(dev);
138         struct usb_device *hdev = to_usb_device(dev->parent->parent);
139         struct usb_interface *intf = to_usb_interface(dev->parent);
140         struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
141         struct usb_port *peer = port_dev->peer;
142         int port1 = port_dev->portnum;
143         int retval;
144
145         if (!hub)
146                 return -EINVAL;
147         if (hub->in_reset)
148                 return -EBUSY;
149
150         if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
151                         == PM_QOS_FLAGS_ALL)
152                 return -EAGAIN;
153
154         if (usb_port_block_power_off)
155                 return -EBUSY;
156
157         retval = usb_autopm_get_interface(intf);
158         if (retval < 0)
159                 return retval;
160
161         retval = usb_hub_set_port_power(hdev, hub, port1, false);
162         usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
163         if (!port_dev->is_superspeed)
164                 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
165         usb_autopm_put_interface(intf);
166
167         /*
168          * Our peer usb3 port may now be able to suspend, so
169          * asynchronously queue a suspend request to observe that this
170          * usb2 port is now off.
171          */
172         if (!port_dev->is_superspeed && peer)
173                 pm_runtime_put(&peer->dev);
174
175         return retval;
176 }
177 #endif
178
179 static const struct dev_pm_ops usb_port_pm_ops = {
180 #ifdef CONFIG_PM
181         .runtime_suspend =      usb_port_runtime_suspend,
182         .runtime_resume =       usb_port_runtime_resume,
183 #endif
184 };
185
186 struct device_type usb_port_device_type = {
187         .name =         "usb_port",
188         .release =      usb_port_device_release,
189         .pm =           &usb_port_pm_ops,
190 };
191
192 static struct device_driver usb_port_driver = {
193         .name = "usb",
194         .owner = THIS_MODULE,
195 };
196
197 static int link_peers(struct usb_port *left, struct usb_port *right)
198 {
199         struct usb_port *ss_port, *hs_port;
200         int rc;
201
202         if (left->peer == right && right->peer == left)
203                 return 0;
204
205         if (left->peer || right->peer) {
206                 struct usb_port *lpeer = left->peer;
207                 struct usb_port *rpeer = right->peer;
208                 char *method;
209
210                 if (left->location && left->location == right->location)
211                         method = "location";
212                 else
213                         method = "default";
214
215                 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
216                         dev_name(&left->dev), dev_name(&right->dev), method,
217                         dev_name(&left->dev),
218                         lpeer ? dev_name(&lpeer->dev) : "none",
219                         dev_name(&right->dev),
220                         rpeer ? dev_name(&rpeer->dev) : "none");
221                 return -EBUSY;
222         }
223
224         rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
225         if (rc)
226                 return rc;
227         rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
228         if (rc) {
229                 sysfs_remove_link(&left->dev.kobj, "peer");
230                 return rc;
231         }
232
233         /*
234          * We need to wake the HiSpeed port to make sure we don't race
235          * setting ->peer with usb_port_runtime_suspend().  Otherwise we
236          * may miss a suspend event for the SuperSpeed port.
237          */
238         if (left->is_superspeed) {
239                 ss_port = left;
240                 WARN_ON(right->is_superspeed);
241                 hs_port = right;
242         } else {
243                 ss_port = right;
244                 WARN_ON(!right->is_superspeed);
245                 hs_port = left;
246         }
247         pm_runtime_get_sync(&hs_port->dev);
248
249         left->peer = right;
250         right->peer = left;
251
252         /*
253          * The SuperSpeed reference is dropped when the HiSpeed port in
254          * this relationship suspends, i.e. when it is safe to allow a
255          * SuperSpeed connection to drop since there is no risk of a
256          * device degrading to its powered-off HiSpeed connection.
257          *
258          * Also, drop the HiSpeed ref taken above.
259          */
260         pm_runtime_get_sync(&ss_port->dev);
261         pm_runtime_put(&hs_port->dev);
262
263         return 0;
264 }
265
266 static void link_peers_report(struct usb_port *left, struct usb_port *right)
267 {
268         int rc;
269
270         rc = link_peers(left, right);
271         if (rc == 0) {
272                 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
273         } else {
274                 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
275                                 dev_name(&right->dev), rc);
276                 pr_warn_once("usb: port power management may be unreliable\n");
277                 usb_port_block_power_off = 1;
278         }
279 }
280
281 static void unlink_peers(struct usb_port *left, struct usb_port *right)
282 {
283         struct usb_port *ss_port, *hs_port;
284
285         WARN(right->peer != left || left->peer != right,
286                         "%s and %s are not peers?\n",
287                         dev_name(&left->dev), dev_name(&right->dev));
288
289         /*
290          * We wake the HiSpeed port to make sure we don't race its
291          * usb_port_runtime_resume() event which takes a SuperSpeed ref
292          * when ->peer is !NULL.
293          */
294         if (left->is_superspeed) {
295                 ss_port = left;
296                 hs_port = right;
297         } else {
298                 ss_port = right;
299                 hs_port = left;
300         }
301
302         pm_runtime_get_sync(&hs_port->dev);
303
304         sysfs_remove_link(&left->dev.kobj, "peer");
305         right->peer = NULL;
306         sysfs_remove_link(&right->dev.kobj, "peer");
307         left->peer = NULL;
308
309         /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
310         pm_runtime_put(&ss_port->dev);
311
312         /* Drop the ref taken above */
313         pm_runtime_put(&hs_port->dev);
314 }
315
316 /*
317  * For each usb hub device in the system check to see if it is in the
318  * peer domain of the given port_dev, and if it is check to see if it
319  * has a port that matches the given port by location
320  */
321 static int match_location(struct usb_device *peer_hdev, void *p)
322 {
323         int port1;
324         struct usb_hcd *hcd, *peer_hcd;
325         struct usb_port *port_dev = p, *peer;
326         struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
327         struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
328
329         if (!peer_hub)
330                 return 0;
331
332         hcd = bus_to_hcd(hdev->bus);
333         peer_hcd = bus_to_hcd(peer_hdev->bus);
334         /* peer_hcd is provisional until we verify it against the known peer */
335         if (peer_hcd != hcd->shared_hcd)
336                 return 0;
337
338         for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
339                 peer = peer_hub->ports[port1 - 1];
340                 if (peer && peer->location == port_dev->location) {
341                         link_peers_report(port_dev, peer);
342                         return 1; /* done */
343                 }
344         }
345
346         return 0;
347 }
348
349 /*
350  * Find the peer port either via explicit platform firmware "location"
351  * data, the peer hcd for root hubs, or the upstream peer relationship
352  * for all other hubs.
353  */
354 static void find_and_link_peer(struct usb_hub *hub, int port1)
355 {
356         struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
357         struct usb_device *hdev = hub->hdev;
358         struct usb_device *peer_hdev;
359         struct usb_hub *peer_hub;
360
361         /*
362          * If location data is available then we can only peer this port
363          * by a location match, not the default peer (lest we create a
364          * situation where we need to go back and undo a default peering
365          * when the port is later peered by location data)
366          */
367         if (port_dev->location) {
368                 /* we link the peer in match_location() if found */
369                 usb_for_each_dev(port_dev, match_location);
370                 return;
371         } else if (!hdev->parent) {
372                 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
373                 struct usb_hcd *peer_hcd = hcd->shared_hcd;
374
375                 if (!peer_hcd)
376                         return;
377
378                 peer_hdev = peer_hcd->self.root_hub;
379         } else {
380                 struct usb_port *upstream;
381                 struct usb_device *parent = hdev->parent;
382                 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
383
384                 if (!parent_hub)
385                         return;
386
387                 upstream = parent_hub->ports[hdev->portnum - 1];
388                 if (!upstream || !upstream->peer)
389                         return;
390
391                 peer_hdev = upstream->peer->child;
392         }
393
394         peer_hub = usb_hub_to_struct_hub(peer_hdev);
395         if (!peer_hub || port1 > peer_hdev->maxchild)
396                 return;
397
398         /*
399          * we found a valid default peer, last check is to make sure it
400          * does not have location data
401          */
402         peer = peer_hub->ports[port1 - 1];
403         if (peer && peer->location == 0)
404                 link_peers_report(port_dev, peer);
405 }
406
407 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
408 {
409         struct usb_port *port_dev;
410         int retval;
411
412         port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
413         if (!port_dev)
414                 return -ENOMEM;
415
416         port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
417         if (!port_dev->req) {
418                 kfree(port_dev);
419                 return -ENOMEM;
420         }
421
422         hub->ports[port1 - 1] = port_dev;
423         port_dev->portnum = port1;
424         set_bit(port1, hub->power_bits);
425         port_dev->dev.parent = hub->intfdev;
426         port_dev->dev.groups = port_dev_group;
427         port_dev->dev.type = &usb_port_device_type;
428         port_dev->dev.driver = &usb_port_driver;
429         if (hub_is_superspeed(hub->hdev))
430                 port_dev->is_superspeed = 1;
431         dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
432                         port1);
433         mutex_init(&port_dev->status_lock);
434         retval = device_register(&port_dev->dev);
435         if (retval) {
436                 put_device(&port_dev->dev);
437                 return retval;
438         }
439
440         /* Set default policy of port-poweroff disabled. */
441         retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
442                         DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
443         if (retval < 0) {
444                 device_unregister(&port_dev->dev);
445                 return retval;
446         }
447
448         find_and_link_peer(hub, port1);
449
450         /*
451          * Enable runtime pm and hold a refernce that hub_configure()
452          * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
453          * and the hub has been fully registered (hdev->maxchild set).
454          */
455         pm_runtime_set_active(&port_dev->dev);
456         pm_runtime_get_noresume(&port_dev->dev);
457         pm_runtime_enable(&port_dev->dev);
458         device_enable_async_suspend(&port_dev->dev);
459
460         /*
461          * Keep hidden the ability to enable port-poweroff if the hub
462          * does not support power switching.
463          */
464         if (!hub_is_port_power_switchable(hub))
465                 return 0;
466
467         /* Attempt to let userspace take over the policy. */
468         retval = dev_pm_qos_expose_flags(&port_dev->dev,
469                         PM_QOS_FLAG_NO_POWER_OFF);
470         if (retval < 0) {
471                 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
472                 return 0;
473         }
474
475         /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
476         retval = dev_pm_qos_remove_request(port_dev->req);
477         if (retval >= 0) {
478                 kfree(port_dev->req);
479                 port_dev->req = NULL;
480         }
481         return 0;
482 }
483
484 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
485 {
486         struct usb_port *port_dev = hub->ports[port1 - 1];
487         struct usb_port *peer;
488
489         peer = port_dev->peer;
490         if (peer)
491                 unlink_peers(port_dev, peer);
492         device_unregister(&port_dev->dev);
493 }