Merge tag 'tegra-for-3.17-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/tegra...
[cascardo/linux.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_RUNTIME
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         usb_autopm_get_interface(intf);
102         retval = usb_hub_set_port_power(hdev, hub, port1, true);
103         msleep(hub_power_on_good_delay(hub));
104         if (udev && !retval) {
105                 /*
106                  * Attempt to wait for usb hub port to be reconnected in order
107                  * to make the resume procedure successful.  The device may have
108                  * disconnected while the port was powered off, so ignore the
109                  * return status.
110                  */
111                 retval = hub_port_debounce_be_connected(hub, port1);
112                 if (retval < 0)
113                         dev_dbg(&port_dev->dev, "can't get reconnection after setting port  power on, status %d\n",
114                                         retval);
115                 retval = 0;
116
117                 /* Force the child awake to revalidate after the power loss. */
118                 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
119                         pm_runtime_get_noresume(&port_dev->dev);
120                         pm_request_resume(&udev->dev);
121                 }
122         }
123
124         usb_autopm_put_interface(intf);
125
126         return retval;
127 }
128
129 static int usb_port_runtime_suspend(struct device *dev)
130 {
131         struct usb_port *port_dev = to_usb_port(dev);
132         struct usb_device *hdev = to_usb_device(dev->parent->parent);
133         struct usb_interface *intf = to_usb_interface(dev->parent);
134         struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
135         struct usb_port *peer = port_dev->peer;
136         int port1 = port_dev->portnum;
137         int retval;
138
139         if (!hub)
140                 return -EINVAL;
141         if (hub->in_reset)
142                 return -EBUSY;
143
144         if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
145                         == PM_QOS_FLAGS_ALL)
146                 return -EAGAIN;
147
148         if (usb_port_block_power_off)
149                 return -EBUSY;
150
151         usb_autopm_get_interface(intf);
152         retval = usb_hub_set_port_power(hdev, hub, port1, false);
153         usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
154         if (!port_dev->is_superspeed)
155                 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
156         usb_autopm_put_interface(intf);
157
158         /*
159          * Our peer usb3 port may now be able to suspend, so
160          * asynchronously queue a suspend request to observe that this
161          * usb2 port is now off.
162          */
163         if (!port_dev->is_superspeed && peer)
164                 pm_runtime_put(&peer->dev);
165
166         return retval;
167 }
168 #endif
169
170 static const struct dev_pm_ops usb_port_pm_ops = {
171 #ifdef CONFIG_PM_RUNTIME
172         .runtime_suspend =      usb_port_runtime_suspend,
173         .runtime_resume =       usb_port_runtime_resume,
174 #endif
175 };
176
177 struct device_type usb_port_device_type = {
178         .name =         "usb_port",
179         .release =      usb_port_device_release,
180         .pm =           &usb_port_pm_ops,
181 };
182
183 static struct device_driver usb_port_driver = {
184         .name = "usb",
185         .owner = THIS_MODULE,
186 };
187
188 static int link_peers(struct usb_port *left, struct usb_port *right)
189 {
190         struct usb_port *ss_port, *hs_port;
191         int rc;
192
193         if (left->peer == right && right->peer == left)
194                 return 0;
195
196         if (left->peer || right->peer) {
197                 struct usb_port *lpeer = left->peer;
198                 struct usb_port *rpeer = right->peer;
199                 char *method;
200
201                 if (left->location && left->location == right->location)
202                         method = "location";
203                 else
204                         method = "default";
205
206                 pr_warn("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
207                         dev_name(&left->dev), dev_name(&right->dev), method,
208                         dev_name(&left->dev),
209                         lpeer ? dev_name(&lpeer->dev) : "none",
210                         dev_name(&right->dev),
211                         rpeer ? dev_name(&rpeer->dev) : "none");
212                 return -EBUSY;
213         }
214
215         rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
216         if (rc)
217                 return rc;
218         rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
219         if (rc) {
220                 sysfs_remove_link(&left->dev.kobj, "peer");
221                 return rc;
222         }
223
224         /*
225          * We need to wake the HiSpeed port to make sure we don't race
226          * setting ->peer with usb_port_runtime_suspend().  Otherwise we
227          * may miss a suspend event for the SuperSpeed port.
228          */
229         if (left->is_superspeed) {
230                 ss_port = left;
231                 WARN_ON(right->is_superspeed);
232                 hs_port = right;
233         } else {
234                 ss_port = right;
235                 WARN_ON(!right->is_superspeed);
236                 hs_port = left;
237         }
238         pm_runtime_get_sync(&hs_port->dev);
239
240         left->peer = right;
241         right->peer = left;
242
243         /*
244          * The SuperSpeed reference is dropped when the HiSpeed port in
245          * this relationship suspends, i.e. when it is safe to allow a
246          * SuperSpeed connection to drop since there is no risk of a
247          * device degrading to its powered-off HiSpeed connection.
248          *
249          * Also, drop the HiSpeed ref taken above.
250          */
251         pm_runtime_get_sync(&ss_port->dev);
252         pm_runtime_put(&hs_port->dev);
253
254         return 0;
255 }
256
257 static void link_peers_report(struct usb_port *left, struct usb_port *right)
258 {
259         int rc;
260
261         rc = link_peers(left, right);
262         if (rc == 0) {
263                 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
264         } else {
265                 dev_warn(&left->dev, "failed to peer to %s (%d)\n",
266                                 dev_name(&right->dev), rc);
267                 pr_warn_once("usb: port power management may be unreliable\n");
268                 usb_port_block_power_off = 1;
269         }
270 }
271
272 static void unlink_peers(struct usb_port *left, struct usb_port *right)
273 {
274         struct usb_port *ss_port, *hs_port;
275
276         WARN(right->peer != left || left->peer != right,
277                         "%s and %s are not peers?\n",
278                         dev_name(&left->dev), dev_name(&right->dev));
279
280         /*
281          * We wake the HiSpeed port to make sure we don't race its
282          * usb_port_runtime_resume() event which takes a SuperSpeed ref
283          * when ->peer is !NULL.
284          */
285         if (left->is_superspeed) {
286                 ss_port = left;
287                 hs_port = right;
288         } else {
289                 ss_port = right;
290                 hs_port = left;
291         }
292
293         pm_runtime_get_sync(&hs_port->dev);
294
295         sysfs_remove_link(&left->dev.kobj, "peer");
296         right->peer = NULL;
297         sysfs_remove_link(&right->dev.kobj, "peer");
298         left->peer = NULL;
299
300         /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
301         pm_runtime_put(&ss_port->dev);
302
303         /* Drop the ref taken above */
304         pm_runtime_put(&hs_port->dev);
305 }
306
307 /*
308  * For each usb hub device in the system check to see if it is in the
309  * peer domain of the given port_dev, and if it is check to see if it
310  * has a port that matches the given port by location
311  */
312 static int match_location(struct usb_device *peer_hdev, void *p)
313 {
314         int port1;
315         struct usb_hcd *hcd, *peer_hcd;
316         struct usb_port *port_dev = p, *peer;
317         struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
318         struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
319
320         if (!peer_hub)
321                 return 0;
322
323         hcd = bus_to_hcd(hdev->bus);
324         peer_hcd = bus_to_hcd(peer_hdev->bus);
325         /* peer_hcd is provisional until we verify it against the known peer */
326         if (peer_hcd != hcd->shared_hcd)
327                 return 0;
328
329         for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
330                 peer = peer_hub->ports[port1 - 1];
331                 if (peer && peer->location == port_dev->location) {
332                         link_peers_report(port_dev, peer);
333                         return 1; /* done */
334                 }
335         }
336
337         return 0;
338 }
339
340 /*
341  * Find the peer port either via explicit platform firmware "location"
342  * data, the peer hcd for root hubs, or the upstream peer relationship
343  * for all other hubs.
344  */
345 static void find_and_link_peer(struct usb_hub *hub, int port1)
346 {
347         struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
348         struct usb_device *hdev = hub->hdev;
349         struct usb_device *peer_hdev;
350         struct usb_hub *peer_hub;
351
352         /*
353          * If location data is available then we can only peer this port
354          * by a location match, not the default peer (lest we create a
355          * situation where we need to go back and undo a default peering
356          * when the port is later peered by location data)
357          */
358         if (port_dev->location) {
359                 /* we link the peer in match_location() if found */
360                 usb_for_each_dev(port_dev, match_location);
361                 return;
362         } else if (!hdev->parent) {
363                 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
364                 struct usb_hcd *peer_hcd = hcd->shared_hcd;
365
366                 if (!peer_hcd)
367                         return;
368
369                 peer_hdev = peer_hcd->self.root_hub;
370         } else {
371                 struct usb_port *upstream;
372                 struct usb_device *parent = hdev->parent;
373                 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
374
375                 if (!parent_hub)
376                         return;
377
378                 upstream = parent_hub->ports[hdev->portnum - 1];
379                 if (!upstream || !upstream->peer)
380                         return;
381
382                 peer_hdev = upstream->peer->child;
383         }
384
385         peer_hub = usb_hub_to_struct_hub(peer_hdev);
386         if (!peer_hub || port1 > peer_hdev->maxchild)
387                 return;
388
389         /*
390          * we found a valid default peer, last check is to make sure it
391          * does not have location data
392          */
393         peer = peer_hub->ports[port1 - 1];
394         if (peer && peer->location == 0)
395                 link_peers_report(port_dev, peer);
396 }
397
398 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
399 {
400         struct usb_port *port_dev;
401         int retval;
402
403         port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
404         if (!port_dev)
405                 return -ENOMEM;
406
407         port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
408         if (!port_dev->req) {
409                 kfree(port_dev);
410                 return -ENOMEM;
411         }
412
413         hub->ports[port1 - 1] = port_dev;
414         port_dev->portnum = port1;
415         set_bit(port1, hub->power_bits);
416         port_dev->dev.parent = hub->intfdev;
417         port_dev->dev.groups = port_dev_group;
418         port_dev->dev.type = &usb_port_device_type;
419         port_dev->dev.driver = &usb_port_driver;
420         if (hub_is_superspeed(hub->hdev))
421                 port_dev->is_superspeed = 1;
422         dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
423                         port1);
424         mutex_init(&port_dev->status_lock);
425         retval = device_register(&port_dev->dev);
426         if (retval) {
427                 put_device(&port_dev->dev);
428                 return retval;
429         }
430
431         /* Set default policy of port-poweroff disabled. */
432         retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
433                         DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
434         if (retval < 0) {
435                 device_unregister(&port_dev->dev);
436                 return retval;
437         }
438
439         find_and_link_peer(hub, port1);
440
441         /*
442          * Enable runtime pm and hold a refernce that hub_configure()
443          * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
444          * and the hub has been fully registered (hdev->maxchild set).
445          */
446         pm_runtime_set_active(&port_dev->dev);
447         pm_runtime_get_noresume(&port_dev->dev);
448         pm_runtime_enable(&port_dev->dev);
449         device_enable_async_suspend(&port_dev->dev);
450
451         /*
452          * Keep hidden the ability to enable port-poweroff if the hub
453          * does not support power switching.
454          */
455         if (!hub_is_port_power_switchable(hub))
456                 return 0;
457
458         /* Attempt to let userspace take over the policy. */
459         retval = dev_pm_qos_expose_flags(&port_dev->dev,
460                         PM_QOS_FLAG_NO_POWER_OFF);
461         if (retval < 0) {
462                 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
463                 return 0;
464         }
465
466         /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
467         retval = dev_pm_qos_remove_request(port_dev->req);
468         if (retval >= 0) {
469                 kfree(port_dev->req);
470                 port_dev->req = NULL;
471         }
472         return 0;
473 }
474
475 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
476 {
477         struct usb_port *port_dev = hub->ports[port1 - 1];
478         struct usb_port *peer;
479
480         peer = port_dev->peer;
481         if (peer)
482                 unlink_peers(port_dev, peer);
483         device_unregister(&port_dev->dev);
484 }