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