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