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