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