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
);
74 #ifdef CONFIG_PM_RUNTIME
75 static int usb_port_runtime_resume(struct device
*dev
)
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
;
89 set_bit(port1
, hub
->power_bits
);
94 * Power on our usb3 peer before this usb2 port to prevent a usb3
95 * device from degrading to its usb2 connection
97 if (!port_dev
->is_superspeed
&& peer
)
98 pm_runtime_get_sync(&peer
->dev
);
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
) {
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
110 retval
= hub_port_debounce_be_connected(hub
, port1
);
112 dev_dbg(&port_dev
->dev
, "can't get reconnection after setting port power on, status %d\n",
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
);
123 usb_autopm_put_interface(intf
);
128 static int usb_port_runtime_suspend(struct device
*dev
)
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
;
143 if (dev_pm_qos_flags(&port_dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
)
147 if (usb_port_block_power_off
)
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
);
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.
162 if (!port_dev
->is_superspeed
&& peer
)
163 pm_runtime_put(&peer
->dev
);
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
,
176 struct device_type usb_port_device_type
= {
178 .release
= usb_port_device_release
,
179 .pm
= &usb_port_pm_ops
,
182 static struct device_driver usb_port_driver
= {
184 .owner
= THIS_MODULE
,
187 static int link_peers(struct usb_port
*left
, struct usb_port
*right
)
189 struct usb_port
*ss_port
, *hs_port
;
192 if (left
->peer
== right
&& right
->peer
== left
)
195 if (left
->peer
|| right
->peer
) {
196 struct usb_port
*lpeer
= left
->peer
;
197 struct usb_port
*rpeer
= right
->peer
;
200 if (left
->location
&& left
->location
== right
->location
)
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");
214 rc
= sysfs_create_link(&left
->dev
.kobj
, &right
->dev
.kobj
, "peer");
217 rc
= sysfs_create_link(&right
->dev
.kobj
, &left
->dev
.kobj
, "peer");
219 sysfs_remove_link(&left
->dev
.kobj
, "peer");
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.
228 if (left
->is_superspeed
) {
230 WARN_ON(right
->is_superspeed
);
234 WARN_ON(!right
->is_superspeed
);
237 pm_runtime_get_sync(&hs_port
->dev
);
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.
248 * Also, drop the HiSpeed ref taken above.
250 pm_runtime_get_sync(&ss_port
->dev
);
251 pm_runtime_put(&hs_port
->dev
);
256 static void link_peers_report(struct usb_port
*left
, struct usb_port
*right
)
260 rc
= link_peers(left
, right
);
262 dev_dbg(&left
->dev
, "peered to %s\n", dev_name(&right
->dev
));
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;
271 static void unlink_peers(struct usb_port
*left
, struct usb_port
*right
)
273 struct usb_port
*ss_port
, *hs_port
;
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
));
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.
284 if (left
->is_superspeed
) {
292 pm_runtime_get_sync(&hs_port
->dev
);
294 sysfs_remove_link(&left
->dev
.kobj
, "peer");
296 sysfs_remove_link(&right
->dev
.kobj
, "peer");
299 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
300 pm_runtime_put(&ss_port
->dev
);
302 /* Drop the ref taken above */
303 pm_runtime_put(&hs_port
->dev
);
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
311 static int match_location(struct usb_device
*peer_hdev
, void *p
)
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
);
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
)
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
);
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.
344 static void find_and_link_peer(struct usb_hub
*hub
, int port1
)
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
;
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)
357 if (port_dev
->location
) {
358 /* we link the peer in match_location() if found */
359 usb_for_each_dev(port_dev
, match_location
);
361 } else if (!hdev
->parent
) {
362 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
363 struct usb_hcd
*peer_hcd
= hcd
->shared_hcd
;
368 peer_hdev
= peer_hcd
->self
.root_hub
;
370 struct usb_port
*upstream
;
371 struct usb_device
*parent
= hdev
->parent
;
372 struct usb_hub
*parent_hub
= usb_hub_to_struct_hub(parent
);
377 upstream
= parent_hub
->ports
[hdev
->portnum
- 1];
378 if (!upstream
|| !upstream
->peer
)
381 peer_hdev
= upstream
->peer
->child
;
384 peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
385 if (!peer_hub
|| port1
> peer_hdev
->maxchild
)
389 * we found a valid default peer, last check is to make sure it
390 * does not have location data
392 peer
= peer_hub
->ports
[port1
- 1];
393 if (peer
&& peer
->location
== 0)
394 link_peers_report(port_dev
, peer
);
397 int usb_hub_create_port_device(struct usb_hub
*hub
, int port1
)
399 struct usb_port
*port_dev
;
402 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
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
),
419 mutex_init(&port_dev
->status_lock
);
420 retval
= device_register(&port_dev
->dev
);
424 find_and_link_peer(hub
, port1
);
426 pm_runtime_set_active(&port_dev
->dev
);
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.
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
);
439 device_enable_async_suspend(&port_dev
->dev
);
443 put_device(&port_dev
->dev
);
448 void usb_hub_remove_port_device(struct usb_hub
*hub
, int port1
)
450 struct usb_port
*port_dev
= hub
->ports
[port1
- 1];
451 struct usb_port
*peer
;
453 peer
= port_dev
->peer
;
455 unlink_peers(port_dev
, peer
);
456 device_unregister(&port_dev
->dev
);