4 * Copyright (C) 2012 Intel Corp
6 * Author: Lan Tianyu <tianyu.lan@intel.com>
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.
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
19 #include <linux/slab.h>
20 #include <linux/pm_qos.h>
24 static int usb_port_block_power_off
;
26 static const struct attribute_group
*port_dev_group
[];
28 static ssize_t
connect_type_show(struct device
*dev
,
29 struct device_attribute
*attr
, char *buf
)
31 struct usb_port
*port_dev
= to_usb_port(dev
);
34 switch (port_dev
->connect_type
) {
35 case USB_PORT_CONNECT_TYPE_HOT_PLUG
:
38 case USB_PORT_CONNECT_TYPE_HARD_WIRED
:
41 case USB_PORT_NOT_USED
:
49 return sprintf(buf
, "%s\n", result
);
51 static DEVICE_ATTR_RO(connect_type
);
53 static struct attribute
*port_dev_attrs
[] = {
54 &dev_attr_connect_type
.attr
,
58 static struct attribute_group port_dev_attr_grp
= {
59 .attrs
= port_dev_attrs
,
62 static const struct attribute_group
*port_dev_group
[] = {
67 static void usb_port_device_release(struct device
*dev
)
69 struct usb_port
*port_dev
= to_usb_port(dev
);
75 #ifdef CONFIG_PM_RUNTIME
76 static int usb_port_runtime_resume(struct device
*dev
)
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
);
81 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
82 struct usb_device
*udev
= port_dev
->child
;
83 struct usb_port
*peer
= port_dev
->peer
;
84 int port1
= port_dev
->portnum
;
90 set_bit(port1
, hub
->power_bits
);
95 * Power on our usb3 peer before this usb2 port to prevent a usb3
96 * device from degrading to its usb2 connection
98 if (!port_dev
->is_superspeed
&& peer
)
99 pm_runtime_get_sync(&peer
->dev
);
101 usb_autopm_get_interface(intf
);
102 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, true);
103 msleep(hub_power_on_good_delay(hub
));
104 if (udev
&& !retval
) {
106 * Attempt to wait for usb hub port to be reconnected in order
107 * to make the resume procedure successful. The device may have
108 * disconnected while the port was powered off, so ignore the
111 retval
= hub_port_debounce_be_connected(hub
, port1
);
113 dev_dbg(&port_dev
->dev
, "can't get reconnection after setting port power on, status %d\n",
117 /* Force the child awake to revalidate after the power loss. */
118 if (!test_and_set_bit(port1
, hub
->child_usage_bits
)) {
119 pm_runtime_get_noresume(&port_dev
->dev
);
120 pm_request_resume(&udev
->dev
);
124 usb_autopm_put_interface(intf
);
129 static int usb_port_runtime_suspend(struct device
*dev
)
131 struct usb_port
*port_dev
= to_usb_port(dev
);
132 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
133 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
134 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
135 struct usb_port
*peer
= port_dev
->peer
;
136 int port1
= port_dev
->portnum
;
144 if (dev_pm_qos_flags(&port_dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
)
148 if (usb_port_block_power_off
)
151 usb_autopm_get_interface(intf
);
152 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, false);
153 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_CONNECTION
);
154 if (!port_dev
->is_superspeed
)
155 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_ENABLE
);
156 usb_autopm_put_interface(intf
);
159 * Our peer usb3 port may now be able to suspend, so
160 * asynchronously queue a suspend request to observe that this
161 * usb2 port is now off.
163 if (!port_dev
->is_superspeed
&& peer
)
164 pm_runtime_put(&peer
->dev
);
170 static const struct dev_pm_ops usb_port_pm_ops
= {
171 #ifdef CONFIG_PM_RUNTIME
172 .runtime_suspend
= usb_port_runtime_suspend
,
173 .runtime_resume
= usb_port_runtime_resume
,
177 struct device_type usb_port_device_type
= {
179 .release
= usb_port_device_release
,
180 .pm
= &usb_port_pm_ops
,
183 static struct device_driver usb_port_driver
= {
185 .owner
= THIS_MODULE
,
188 static int link_peers(struct usb_port
*left
, struct usb_port
*right
)
190 struct usb_port
*ss_port
, *hs_port
;
193 if (left
->peer
== right
&& right
->peer
== left
)
196 if (left
->peer
|| right
->peer
) {
197 struct usb_port
*lpeer
= left
->peer
;
198 struct usb_port
*rpeer
= right
->peer
;
201 if (left
->location
&& left
->location
== right
->location
)
206 pr_warn("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
207 dev_name(&left
->dev
), dev_name(&right
->dev
), method
,
208 dev_name(&left
->dev
),
209 lpeer
? dev_name(&lpeer
->dev
) : "none",
210 dev_name(&right
->dev
),
211 rpeer
? dev_name(&rpeer
->dev
) : "none");
215 rc
= sysfs_create_link(&left
->dev
.kobj
, &right
->dev
.kobj
, "peer");
218 rc
= sysfs_create_link(&right
->dev
.kobj
, &left
->dev
.kobj
, "peer");
220 sysfs_remove_link(&left
->dev
.kobj
, "peer");
225 * We need to wake the HiSpeed port to make sure we don't race
226 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
227 * may miss a suspend event for the SuperSpeed port.
229 if (left
->is_superspeed
) {
231 WARN_ON(right
->is_superspeed
);
235 WARN_ON(!right
->is_superspeed
);
238 pm_runtime_get_sync(&hs_port
->dev
);
244 * The SuperSpeed reference is dropped when the HiSpeed port in
245 * this relationship suspends, i.e. when it is safe to allow a
246 * SuperSpeed connection to drop since there is no risk of a
247 * device degrading to its powered-off HiSpeed connection.
249 * Also, drop the HiSpeed ref taken above.
251 pm_runtime_get_sync(&ss_port
->dev
);
252 pm_runtime_put(&hs_port
->dev
);
257 static void link_peers_report(struct usb_port
*left
, struct usb_port
*right
)
261 rc
= link_peers(left
, right
);
263 dev_dbg(&left
->dev
, "peered to %s\n", dev_name(&right
->dev
));
265 dev_warn(&left
->dev
, "failed to peer to %s (%d)\n",
266 dev_name(&right
->dev
), rc
);
267 pr_warn_once("usb: port power management may be unreliable\n");
268 usb_port_block_power_off
= 1;
272 static void unlink_peers(struct usb_port
*left
, struct usb_port
*right
)
274 struct usb_port
*ss_port
, *hs_port
;
276 WARN(right
->peer
!= left
|| left
->peer
!= right
,
277 "%s and %s are not peers?\n",
278 dev_name(&left
->dev
), dev_name(&right
->dev
));
281 * We wake the HiSpeed port to make sure we don't race its
282 * usb_port_runtime_resume() event which takes a SuperSpeed ref
283 * when ->peer is !NULL.
285 if (left
->is_superspeed
) {
293 pm_runtime_get_sync(&hs_port
->dev
);
295 sysfs_remove_link(&left
->dev
.kobj
, "peer");
297 sysfs_remove_link(&right
->dev
.kobj
, "peer");
300 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
301 pm_runtime_put(&ss_port
->dev
);
303 /* Drop the ref taken above */
304 pm_runtime_put(&hs_port
->dev
);
308 * For each usb hub device in the system check to see if it is in the
309 * peer domain of the given port_dev, and if it is check to see if it
310 * has a port that matches the given port by location
312 static int match_location(struct usb_device
*peer_hdev
, void *p
)
315 struct usb_hcd
*hcd
, *peer_hcd
;
316 struct usb_port
*port_dev
= p
, *peer
;
317 struct usb_hub
*peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
318 struct usb_device
*hdev
= to_usb_device(port_dev
->dev
.parent
->parent
);
323 hcd
= bus_to_hcd(hdev
->bus
);
324 peer_hcd
= bus_to_hcd(peer_hdev
->bus
);
325 /* peer_hcd is provisional until we verify it against the known peer */
326 if (peer_hcd
!= hcd
->shared_hcd
)
329 for (port1
= 1; port1
<= peer_hdev
->maxchild
; port1
++) {
330 peer
= peer_hub
->ports
[port1
- 1];
331 if (peer
&& peer
->location
== port_dev
->location
) {
332 link_peers_report(port_dev
, peer
);
341 * Find the peer port either via explicit platform firmware "location"
342 * data, the peer hcd for root hubs, or the upstream peer relationship
343 * for all other hubs.
345 static void find_and_link_peer(struct usb_hub
*hub
, int port1
)
347 struct usb_port
*port_dev
= hub
->ports
[port1
- 1], *peer
;
348 struct usb_device
*hdev
= hub
->hdev
;
349 struct usb_device
*peer_hdev
;
350 struct usb_hub
*peer_hub
;
353 * If location data is available then we can only peer this port
354 * by a location match, not the default peer (lest we create a
355 * situation where we need to go back and undo a default peering
356 * when the port is later peered by location data)
358 if (port_dev
->location
) {
359 /* we link the peer in match_location() if found */
360 usb_for_each_dev(port_dev
, match_location
);
362 } else if (!hdev
->parent
) {
363 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
364 struct usb_hcd
*peer_hcd
= hcd
->shared_hcd
;
369 peer_hdev
= peer_hcd
->self
.root_hub
;
371 struct usb_port
*upstream
;
372 struct usb_device
*parent
= hdev
->parent
;
373 struct usb_hub
*parent_hub
= usb_hub_to_struct_hub(parent
);
378 upstream
= parent_hub
->ports
[hdev
->portnum
- 1];
379 if (!upstream
|| !upstream
->peer
)
382 peer_hdev
= upstream
->peer
->child
;
385 peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
386 if (!peer_hub
|| port1
> peer_hdev
->maxchild
)
390 * we found a valid default peer, last check is to make sure it
391 * does not have location data
393 peer
= peer_hub
->ports
[port1
- 1];
394 if (peer
&& peer
->location
== 0)
395 link_peers_report(port_dev
, peer
);
398 int usb_hub_create_port_device(struct usb_hub
*hub
, int port1
)
400 struct usb_port
*port_dev
;
403 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
407 port_dev
->req
= kzalloc(sizeof(*(port_dev
->req
)), GFP_KERNEL
);
408 if (!port_dev
->req
) {
413 hub
->ports
[port1
- 1] = port_dev
;
414 port_dev
->portnum
= port1
;
415 set_bit(port1
, hub
->power_bits
);
416 port_dev
->dev
.parent
= hub
->intfdev
;
417 port_dev
->dev
.groups
= port_dev_group
;
418 port_dev
->dev
.type
= &usb_port_device_type
;
419 port_dev
->dev
.driver
= &usb_port_driver
;
420 if (hub_is_superspeed(hub
->hdev
))
421 port_dev
->is_superspeed
= 1;
422 dev_set_name(&port_dev
->dev
, "%s-port%d", dev_name(&hub
->hdev
->dev
),
424 mutex_init(&port_dev
->status_lock
);
425 retval
= device_register(&port_dev
->dev
);
427 put_device(&port_dev
->dev
);
431 /* Set default policy of port-poweroff disabled. */
432 retval
= dev_pm_qos_add_request(&port_dev
->dev
, port_dev
->req
,
433 DEV_PM_QOS_FLAGS
, PM_QOS_FLAG_NO_POWER_OFF
);
435 device_unregister(&port_dev
->dev
);
439 find_and_link_peer(hub
, port1
);
442 * Enable runtime pm and hold a refernce that hub_configure()
443 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
444 * and the hub has been fully registered (hdev->maxchild set).
446 pm_runtime_set_active(&port_dev
->dev
);
447 pm_runtime_get_noresume(&port_dev
->dev
);
448 pm_runtime_enable(&port_dev
->dev
);
449 device_enable_async_suspend(&port_dev
->dev
);
452 * Keep hidden the ability to enable port-poweroff if the hub
453 * does not support power switching.
455 if (!hub_is_port_power_switchable(hub
))
458 /* Attempt to let userspace take over the policy. */
459 retval
= dev_pm_qos_expose_flags(&port_dev
->dev
,
460 PM_QOS_FLAG_NO_POWER_OFF
);
462 dev_warn(&port_dev
->dev
, "failed to expose pm_qos_no_poweroff\n");
466 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
467 retval
= dev_pm_qos_remove_request(port_dev
->req
);
469 kfree(port_dev
->req
);
470 port_dev
->req
= NULL
;
475 void usb_hub_remove_port_device(struct usb_hub
*hub
, int port1
)
477 struct usb_port
*port_dev
= hub
->ports
[port1
- 1];
478 struct usb_port
*peer
;
480 peer
= port_dev
->peer
;
482 unlink_peers(port_dev
, peer
);
483 device_unregister(&port_dev
->dev
);