usb: don't clear FEAT_C_ENABLE on usb_port_runtime_resume failure
[firefly-linux-kernel-4.4.55.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 const struct attribute_group *port_dev_group[];
25
26 static ssize_t connect_type_show(struct device *dev,
27                                  struct device_attribute *attr, char *buf)
28 {
29         struct usb_port *port_dev = to_usb_port(dev);
30         char *result;
31
32         switch (port_dev->connect_type) {
33         case USB_PORT_CONNECT_TYPE_HOT_PLUG:
34                 result = "hotplug";
35                 break;
36         case USB_PORT_CONNECT_TYPE_HARD_WIRED:
37                 result = "hardwired";
38                 break;
39         case USB_PORT_NOT_USED:
40                 result = "not used";
41                 break;
42         default:
43                 result = "unknown";
44                 break;
45         }
46
47         return sprintf(buf, "%s\n", result);
48 }
49 static DEVICE_ATTR_RO(connect_type);
50
51 static struct attribute *port_dev_attrs[] = {
52         &dev_attr_connect_type.attr,
53         NULL,
54 };
55
56 static struct attribute_group port_dev_attr_grp = {
57         .attrs = port_dev_attrs,
58 };
59
60 static const struct attribute_group *port_dev_group[] = {
61         &port_dev_attr_grp,
62         NULL,
63 };
64
65 static void usb_port_device_release(struct device *dev)
66 {
67         struct usb_port *port_dev = to_usb_port(dev);
68
69         kfree(port_dev);
70 }
71
72 #ifdef CONFIG_PM_RUNTIME
73 static int usb_port_runtime_resume(struct device *dev)
74 {
75         struct usb_port *port_dev = to_usb_port(dev);
76         struct usb_device *hdev = to_usb_device(dev->parent->parent);
77         struct usb_interface *intf = to_usb_interface(dev->parent);
78         struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
79         struct usb_port *peer = port_dev->peer;
80         int port1 = port_dev->portnum;
81         int retval;
82
83         if (!hub)
84                 return -EINVAL;
85         if (hub->in_reset) {
86                 set_bit(port1, hub->power_bits);
87                 return 0;
88         }
89
90         /*
91          * Power on our usb3 peer before this usb2 port to prevent a usb3
92          * device from degrading to its usb2 connection
93          */
94         if (!port_dev->is_superspeed && peer)
95                 pm_runtime_get_sync(&peer->dev);
96
97         usb_autopm_get_interface(intf);
98         set_bit(port1, hub->busy_bits);
99
100         retval = usb_hub_set_port_power(hdev, hub, port1, true);
101         msleep(hub_power_on_good_delay(hub));
102         if (port_dev->child && !retval) {
103                 /*
104                  * Attempt to wait for usb hub port to be reconnected in order
105                  * to make the resume procedure successful.  The device may have
106                  * disconnected while the port was powered off, so ignore the
107                  * return status.
108                  */
109                 retval = hub_port_debounce_be_connected(hub, port1);
110                 if (retval < 0)
111                         dev_dbg(&port_dev->dev, "can't get reconnection after setting port  power on, status %d\n",
112                                         retval);
113                 retval = 0;
114         }
115
116         clear_bit(port1, hub->busy_bits);
117         usb_autopm_put_interface(intf);
118
119         return retval;
120 }
121
122 static int usb_port_runtime_suspend(struct device *dev)
123 {
124         struct usb_port *port_dev = to_usb_port(dev);
125         struct usb_device *hdev = to_usb_device(dev->parent->parent);
126         struct usb_interface *intf = to_usb_interface(dev->parent);
127         struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
128         struct usb_port *peer = port_dev->peer;
129         int port1 = port_dev->portnum;
130         int retval;
131
132         if (!hub)
133                 return -EINVAL;
134         if (hub->in_reset)
135                 return -EBUSY;
136
137         if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
138                         == PM_QOS_FLAGS_ALL)
139                 return -EAGAIN;
140
141         usb_autopm_get_interface(intf);
142         set_bit(port1, hub->busy_bits);
143         retval = usb_hub_set_port_power(hdev, hub, port1, false);
144         usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
145         usb_clear_port_feature(hdev, port1,     USB_PORT_FEAT_C_ENABLE);
146         clear_bit(port1, hub->busy_bits);
147         usb_autopm_put_interface(intf);
148
149         /*
150          * Our peer usb3 port may now be able to suspend, so
151          * asynchronously queue a suspend request to observe that this
152          * usb2 port is now off.
153          */
154         if (!port_dev->is_superspeed && peer)
155                 pm_runtime_put(&peer->dev);
156
157         return retval;
158 }
159 #endif
160
161 static const struct dev_pm_ops usb_port_pm_ops = {
162 #ifdef CONFIG_PM_RUNTIME
163         .runtime_suspend =      usb_port_runtime_suspend,
164         .runtime_resume =       usb_port_runtime_resume,
165 #endif
166 };
167
168 struct device_type usb_port_device_type = {
169         .name =         "usb_port",
170         .release =      usb_port_device_release,
171         .pm =           &usb_port_pm_ops,
172 };
173
174 static struct device_driver usb_port_driver = {
175         .name = "usb",
176         .owner = THIS_MODULE,
177 };
178
179 static int link_peers(struct usb_port *left, struct usb_port *right)
180 {
181         struct usb_port *ss_port, *hs_port;
182         int rc;
183
184         if (left->peer == right && right->peer == left)
185                 return 0;
186
187         if (left->peer || right->peer) {
188                 struct usb_port *lpeer = left->peer;
189                 struct usb_port *rpeer = right->peer;
190
191                 WARN(1, "failed to peer %s and %s (%s -> %p) (%s -> %p)\n",
192                         dev_name(&left->dev), dev_name(&right->dev),
193                         dev_name(&left->dev), lpeer,
194                         dev_name(&right->dev), rpeer);
195                 return -EBUSY;
196         }
197
198         rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
199         if (rc)
200                 return rc;
201         rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
202         if (rc) {
203                 sysfs_remove_link(&left->dev.kobj, "peer");
204                 return rc;
205         }
206
207         /*
208          * We need to wake the HiSpeed port to make sure we don't race
209          * setting ->peer with usb_port_runtime_suspend().  Otherwise we
210          * may miss a suspend event for the SuperSpeed port.
211          */
212         if (left->is_superspeed) {
213                 ss_port = left;
214                 WARN_ON(right->is_superspeed);
215                 hs_port = right;
216         } else {
217                 ss_port = right;
218                 WARN_ON(!right->is_superspeed);
219                 hs_port = left;
220         }
221         pm_runtime_get_sync(&hs_port->dev);
222
223         left->peer = right;
224         right->peer = left;
225
226         /*
227          * The SuperSpeed reference is dropped when the HiSpeed port in
228          * this relationship suspends, i.e. when it is safe to allow a
229          * SuperSpeed connection to drop since there is no risk of a
230          * device degrading to its powered-off HiSpeed connection.
231          *
232          * Also, drop the HiSpeed ref taken above.
233          */
234         pm_runtime_get_sync(&ss_port->dev);
235         pm_runtime_put(&hs_port->dev);
236
237         return 0;
238 }
239
240 static void link_peers_report(struct usb_port *left, struct usb_port *right)
241 {
242         int rc;
243
244         rc = link_peers(left, right);
245         if (rc == 0) {
246                 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
247         } else {
248                 dev_warn(&left->dev, "failed to peer to %s (%d)\n",
249                                 dev_name(&right->dev), rc);
250                 pr_warn_once("usb: port power management may be unreliable\n");
251         }
252 }
253
254 static void unlink_peers(struct usb_port *left, struct usb_port *right)
255 {
256         struct usb_port *ss_port, *hs_port;
257
258         WARN(right->peer != left || left->peer != right,
259                         "%s and %s are not peers?\n",
260                         dev_name(&left->dev), dev_name(&right->dev));
261
262         /*
263          * We wake the HiSpeed port to make sure we don't race its
264          * usb_port_runtime_resume() event which takes a SuperSpeed ref
265          * when ->peer is !NULL.
266          */
267         if (left->is_superspeed) {
268                 ss_port = left;
269                 hs_port = right;
270         } else {
271                 ss_port = right;
272                 hs_port = left;
273         }
274
275         pm_runtime_get_sync(&hs_port->dev);
276
277         sysfs_remove_link(&left->dev.kobj, "peer");
278         right->peer = NULL;
279         sysfs_remove_link(&right->dev.kobj, "peer");
280         left->peer = NULL;
281
282         /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
283         pm_runtime_put(&ss_port->dev);
284
285         /* Drop the ref taken above */
286         pm_runtime_put(&hs_port->dev);
287 }
288
289 /*
290  * For each usb hub device in the system check to see if it is in the
291  * peer domain of the given port_dev, and if it is check to see if it
292  * has a port that matches the given port by location
293  */
294 static int match_location(struct usb_device *peer_hdev, void *p)
295 {
296         int port1;
297         struct usb_hcd *hcd, *peer_hcd;
298         struct usb_port *port_dev = p, *peer;
299         struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
300         struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
301
302         if (!peer_hub)
303                 return 0;
304
305         hcd = bus_to_hcd(hdev->bus);
306         peer_hcd = bus_to_hcd(peer_hdev->bus);
307         /* peer_hcd is provisional until we verify it against the known peer */
308         if (peer_hcd != hcd->shared_hcd)
309                 return 0;
310
311         for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
312                 peer = peer_hub->ports[port1 - 1];
313                 if (peer && peer->location == port_dev->location) {
314                         link_peers_report(port_dev, peer);
315                         return 1; /* done */
316                 }
317         }
318
319         return 0;
320 }
321
322 /*
323  * Find the peer port either via explicit platform firmware "location"
324  * data, the peer hcd for root hubs, or the upstream peer relationship
325  * for all other hubs.
326  */
327 static void find_and_link_peer(struct usb_hub *hub, int port1)
328 {
329         struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
330         struct usb_device *hdev = hub->hdev;
331         struct usb_device *peer_hdev;
332         struct usb_hub *peer_hub;
333
334         /*
335          * If location data is available then we can only peer this port
336          * by a location match, not the default peer (lest we create a
337          * situation where we need to go back and undo a default peering
338          * when the port is later peered by location data)
339          */
340         if (port_dev->location) {
341                 /* we link the peer in match_location() if found */
342                 usb_for_each_dev(port_dev, match_location);
343                 return;
344         } else if (!hdev->parent) {
345                 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
346                 struct usb_hcd *peer_hcd = hcd->shared_hcd;
347
348                 if (!peer_hcd)
349                         return;
350
351                 peer_hdev = peer_hcd->self.root_hub;
352         } else {
353                 struct usb_port *upstream;
354                 struct usb_device *parent = hdev->parent;
355                 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
356
357                 if (!parent_hub)
358                         return;
359
360                 upstream = parent_hub->ports[hdev->portnum - 1];
361                 if (!upstream || !upstream->peer)
362                         return;
363
364                 peer_hdev = upstream->peer->child;
365         }
366
367         peer_hub = usb_hub_to_struct_hub(peer_hdev);
368         if (!peer_hub || port1 > peer_hdev->maxchild)
369                 return;
370
371         /*
372          * we found a valid default peer, last check is to make sure it
373          * does not have location data
374          */
375         peer = peer_hub->ports[port1 - 1];
376         if (peer && peer->location == 0)
377                 link_peers_report(port_dev, peer);
378 }
379
380 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
381 {
382         struct usb_port *port_dev;
383         int retval;
384
385         port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
386         if (!port_dev) {
387                 retval = -ENOMEM;
388                 goto exit;
389         }
390
391         hub->ports[port1 - 1] = port_dev;
392         port_dev->portnum = port1;
393         set_bit(port1, hub->power_bits);
394         port_dev->dev.parent = hub->intfdev;
395         port_dev->dev.groups = port_dev_group;
396         port_dev->dev.type = &usb_port_device_type;
397         port_dev->dev.driver = &usb_port_driver;
398         if (hub_is_superspeed(hub->hdev))
399                 port_dev->is_superspeed = 1;
400         dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
401                         port1);
402         retval = device_register(&port_dev->dev);
403         if (retval)
404                 goto error_register;
405
406         find_and_link_peer(hub, port1);
407
408         pm_runtime_set_active(&port_dev->dev);
409
410         /*
411          * Do not enable port runtime pm if the hub does not support
412          * power switching.  Also, userspace must have final say of
413          * whether a port is permitted to power-off.  Do not enable
414          * runtime pm if we fail to expose pm_qos_no_power_off.
415          */
416         if (hub_is_port_power_switchable(hub)
417                         && dev_pm_qos_expose_flags(&port_dev->dev,
418                         PM_QOS_FLAG_NO_POWER_OFF) == 0)
419                 pm_runtime_enable(&port_dev->dev);
420
421         device_enable_async_suspend(&port_dev->dev);
422         return 0;
423
424 error_register:
425         put_device(&port_dev->dev);
426 exit:
427         return retval;
428 }
429
430 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
431 {
432         struct usb_port *port_dev = hub->ports[port1 - 1];
433         struct usb_port *peer;
434
435         peer = port_dev->peer;
436         if (peer)
437                 unlink_peers(port_dev, peer);
438         device_unregister(&port_dev->dev);
439 }