usb: sysfs link peer ports
[linux-2.6-block.git] / drivers / usb / core / port.c
CommitLineData
6e30d7cb
LT
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
9f7344fb 19#include <linux/slab.h>
971fcd49 20#include <linux/pm_qos.h>
9f7344fb 21
6e30d7cb
LT
22#include "hub.h"
23
cef7468c
LT
24static const struct attribute_group *port_dev_group[];
25
d03f254f
GKH
26static ssize_t connect_type_show(struct device *dev,
27 struct device_attribute *attr, char *buf)
cef7468c
LT
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}
d03f254f 49static DEVICE_ATTR_RO(connect_type);
cef7468c
LT
50
51static struct attribute *port_dev_attrs[] = {
52 &dev_attr_connect_type.attr,
53 NULL,
54};
55
56static struct attribute_group port_dev_attr_grp = {
57 .attrs = port_dev_attrs,
58};
59
60static const struct attribute_group *port_dev_group[] = {
61 &port_dev_attr_grp,
62 NULL,
63};
64
6e30d7cb
LT
65static 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
84ebc102 72#ifdef CONFIG_PM_RUNTIME
971fcd49
LT
73static 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);
ad493e5e
LT
78 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
79 int port1 = port_dev->portnum;
971fcd49
LT
80 int retval;
81
ad493e5e
LT
82 if (!hub)
83 return -EINVAL;
600856c2
AS
84 if (hub->in_reset) {
85 port_dev->power_is_on = 1;
86 return 0;
87 }
ad493e5e 88
971fcd49 89 usb_autopm_get_interface(intf);
ad493e5e
LT
90 set_bit(port1, hub->busy_bits);
91
41341261 92 retval = usb_hub_set_port_power(hdev, hub, port1, true);
ad493e5e
LT
93 if (port_dev->child && !retval) {
94 /*
d49dad3e
SS
95 * Attempt to wait for usb hub port to be reconnected in order
96 * to make the resume procedure successful. The device may have
97 * disconnected while the port was powered off, so ignore the
98 * return status.
ad493e5e
LT
99 */
100 retval = hub_port_debounce_be_connected(hub, port1);
d49dad3e 101 if (retval < 0)
ad493e5e
LT
102 dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
103 retval);
ad493e5e 104 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
ad493e5e
LT
105 retval = 0;
106 }
107
ad493e5e 108 clear_bit(port1, hub->busy_bits);
971fcd49
LT
109 usb_autopm_put_interface(intf);
110 return retval;
111}
112
113static int usb_port_runtime_suspend(struct device *dev)
114{
115 struct usb_port *port_dev = to_usb_port(dev);
116 struct usb_device *hdev = to_usb_device(dev->parent->parent);
117 struct usb_interface *intf = to_usb_interface(dev->parent);
ad493e5e
LT
118 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
119 int port1 = port_dev->portnum;
971fcd49
LT
120 int retval;
121
ad493e5e
LT
122 if (!hub)
123 return -EINVAL;
600856c2
AS
124 if (hub->in_reset)
125 return -EBUSY;
ad493e5e 126
971fcd49
LT
127 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
128 == PM_QOS_FLAGS_ALL)
129 return -EAGAIN;
130
131 usb_autopm_get_interface(intf);
ad493e5e 132 set_bit(port1, hub->busy_bits);
41341261 133 retval = usb_hub_set_port_power(hdev, hub, port1, false);
ad493e5e
LT
134 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
135 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
136 clear_bit(port1, hub->busy_bits);
971fcd49
LT
137 usb_autopm_put_interface(intf);
138 return retval;
139}
140#endif
141
142static const struct dev_pm_ops usb_port_pm_ops = {
84ebc102 143#ifdef CONFIG_PM_RUNTIME
971fcd49
LT
144 .runtime_suspend = usb_port_runtime_suspend,
145 .runtime_resume = usb_port_runtime_resume,
971fcd49
LT
146#endif
147};
148
6e30d7cb
LT
149struct device_type usb_port_device_type = {
150 .name = "usb_port",
151 .release = usb_port_device_release,
971fcd49 152 .pm = &usb_port_pm_ops,
6e30d7cb
LT
153};
154
d99f6b41
DW
155static struct device_driver usb_port_driver = {
156 .name = "usb",
157 .owner = THIS_MODULE,
158};
159
b7e38eac 160static int link_peers(struct usb_port *left, struct usb_port *right)
d8521afe 161{
b7e38eac
DW
162 int rc;
163
d8521afe 164 if (left->peer == right && right->peer == left)
b7e38eac 165 return 0;
d8521afe
DW
166
167 if (left->peer || right->peer) {
168 struct usb_port *lpeer = left->peer;
169 struct usb_port *rpeer = right->peer;
170
171 WARN(1, "failed to peer %s and %s (%s -> %p) (%s -> %p)\n",
172 dev_name(&left->dev), dev_name(&right->dev),
173 dev_name(&left->dev), lpeer,
174 dev_name(&right->dev), rpeer);
b7e38eac
DW
175 return -EBUSY;
176 }
177
178 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
179 if (rc)
180 return rc;
181 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
182 if (rc) {
183 sysfs_remove_link(&left->dev.kobj, "peer");
184 return rc;
d8521afe
DW
185 }
186
187 left->peer = right;
188 right->peer = left;
b7e38eac
DW
189
190 return 0;
191}
192
193static void link_peers_report(struct usb_port *left, struct usb_port *right)
194{
195 int rc;
196
197 rc = link_peers(left, right);
198 if (rc == 0) {
199 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
200 } else {
201 dev_warn(&left->dev, "failed to peer to %s (%d)\n",
202 dev_name(&right->dev), rc);
203 pr_warn_once("usb: port power management may be unreliable\n");
204 }
d8521afe
DW
205}
206
207static void unlink_peers(struct usb_port *left, struct usb_port *right)
208{
209 WARN(right->peer != left || left->peer != right,
210 "%s and %s are not peers?\n",
211 dev_name(&left->dev), dev_name(&right->dev));
212
b7e38eac 213 sysfs_remove_link(&left->dev.kobj, "peer");
d8521afe 214 right->peer = NULL;
b7e38eac 215 sysfs_remove_link(&right->dev.kobj, "peer");
d8521afe
DW
216 left->peer = NULL;
217}
218
8b1ba80c 219/*
3bfd659b
DW
220 * For each usb hub device in the system check to see if it is in the
221 * peer domain of the given port_dev, and if it is check to see if it
222 * has a port that matches the given port by location
223 */
224static int match_location(struct usb_device *peer_hdev, void *p)
225{
226 int port1;
227 struct usb_hcd *hcd, *peer_hcd;
228 struct usb_port *port_dev = p, *peer;
229 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
230 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
231
232 if (!peer_hub)
233 return 0;
234
235 hcd = bus_to_hcd(hdev->bus);
236 peer_hcd = bus_to_hcd(peer_hdev->bus);
237 /* peer_hcd is provisional until we verify it against the known peer */
238 if (peer_hcd != hcd->shared_hcd)
239 return 0;
240
241 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
242 peer = peer_hub->ports[port1 - 1];
243 if (peer && peer->location == port_dev->location) {
b7e38eac 244 link_peers_report(port_dev, peer);
3bfd659b
DW
245 return 1; /* done */
246 }
247 }
248
249 return 0;
250}
251
252/*
253 * Find the peer port either via explicit platform firmware "location"
254 * data, the peer hcd for root hubs, or the upstream peer relationship
255 * for all other hubs.
8b1ba80c 256 */
d8521afe
DW
257static void find_and_link_peer(struct usb_hub *hub, int port1)
258{
259 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
260 struct usb_device *hdev = hub->hdev;
8b1ba80c
DW
261 struct usb_device *peer_hdev;
262 struct usb_hub *peer_hub;
d8521afe 263
3bfd659b
DW
264 /*
265 * If location data is available then we can only peer this port
266 * by a location match, not the default peer (lest we create a
267 * situation where we need to go back and undo a default peering
268 * when the port is later peered by location data)
269 */
270 if (port_dev->location) {
271 /* we link the peer in match_location() if found */
272 usb_for_each_dev(port_dev, match_location);
273 return;
274 } else if (!hdev->parent) {
d8521afe
DW
275 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
276 struct usb_hcd *peer_hcd = hcd->shared_hcd;
277
278 if (!peer_hcd)
279 return;
280
281 peer_hdev = peer_hcd->self.root_hub;
8b1ba80c
DW
282 } else {
283 struct usb_port *upstream;
284 struct usb_device *parent = hdev->parent;
285 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
286
287 if (!parent_hub)
d8521afe
DW
288 return;
289
8b1ba80c
DW
290 upstream = parent_hub->ports[hdev->portnum - 1];
291 if (!upstream || !upstream->peer)
292 return;
d8521afe 293
8b1ba80c 294 peer_hdev = upstream->peer->child;
d8521afe 295 }
8b1ba80c
DW
296
297 peer_hub = usb_hub_to_struct_hub(peer_hdev);
298 if (!peer_hub || port1 > peer_hdev->maxchild)
299 return;
300
3bfd659b
DW
301 /*
302 * we found a valid default peer, last check is to make sure it
303 * does not have location data
304 */
8b1ba80c 305 peer = peer_hub->ports[port1 - 1];
3bfd659b 306 if (peer && peer->location == 0)
b7e38eac 307 link_peers_report(port_dev, peer);
d8521afe
DW
308}
309
6e30d7cb
LT
310int usb_hub_create_port_device(struct usb_hub *hub, int port1)
311{
d8521afe 312 struct usb_port *port_dev;
6e30d7cb
LT
313 int retval;
314
315 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
316 if (!port_dev) {
317 retval = -ENOMEM;
318 goto exit;
319 }
320
321 hub->ports[port1 - 1] = port_dev;
971fcd49 322 port_dev->portnum = port1;
ad493e5e 323 port_dev->power_is_on = true;
6e30d7cb 324 port_dev->dev.parent = hub->intfdev;
cef7468c 325 port_dev->dev.groups = port_dev_group;
6e30d7cb 326 port_dev->dev.type = &usb_port_device_type;
d99f6b41
DW
327 port_dev->dev.driver = &usb_port_driver;
328 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
329 port1);
6e30d7cb
LT
330 retval = device_register(&port_dev->dev);
331 if (retval)
332 goto error_register;
333
d8521afe
DW
334 find_and_link_peer(hub, port1);
335
971fcd49 336 pm_runtime_set_active(&port_dev->dev);
f6cced1a 337
9262c19d
DW
338 /*
339 * Do not enable port runtime pm if the hub does not support
340 * power switching. Also, userspace must have final say of
341 * whether a port is permitted to power-off. Do not enable
342 * runtime pm if we fail to expose pm_qos_no_power_off.
f6cced1a 343 */
9262c19d
DW
344 if (hub_is_port_power_switchable(hub)
345 && dev_pm_qos_expose_flags(&port_dev->dev,
346 PM_QOS_FLAG_NO_POWER_OFF) == 0)
f6cced1a
LT
347 pm_runtime_enable(&port_dev->dev);
348
192fef18 349 device_enable_async_suspend(&port_dev->dev);
6e30d7cb
LT
350 return 0;
351
352error_register:
353 put_device(&port_dev->dev);
354exit:
355 return retval;
356}
357
d8521afe 358void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
6e30d7cb 359{
d8521afe
DW
360 struct usb_port *port_dev = hub->ports[port1 - 1];
361 struct usb_port *peer;
6e30d7cb 362
d8521afe
DW
363 peer = port_dev->peer;
364 if (peer)
365 unlink_peers(port_dev, peer);
366 device_unregister(&port_dev->dev);
367}