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 const struct attribute_group
*port_dev_group
[];
26 static ssize_t
connect_type_show(struct device
*dev
,
27 struct device_attribute
*attr
, char *buf
)
29 struct usb_port
*port_dev
= to_usb_port(dev
);
32 switch (port_dev
->connect_type
) {
33 case USB_PORT_CONNECT_TYPE_HOT_PLUG
:
36 case USB_PORT_CONNECT_TYPE_HARD_WIRED
:
39 case USB_PORT_NOT_USED
:
47 return sprintf(buf
, "%s\n", result
);
49 static DEVICE_ATTR_RO(connect_type
);
51 static struct attribute
*port_dev_attrs
[] = {
52 &dev_attr_connect_type
.attr
,
56 static struct attribute_group port_dev_attr_grp
= {
57 .attrs
= port_dev_attrs
,
60 static const struct attribute_group
*port_dev_group
[] = {
65 static void usb_port_device_release(struct device
*dev
)
67 struct usb_port
*port_dev
= to_usb_port(dev
);
72 #ifdef CONFIG_PM_RUNTIME
73 static int usb_port_runtime_resume(struct device
*dev
)
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
);
78 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
79 struct usb_device
*udev
= port_dev
->child
;
80 struct usb_port
*peer
= port_dev
->peer
;
81 int port1
= port_dev
->portnum
;
87 set_bit(port1
, hub
->power_bits
);
92 * Power on our usb3 peer before this usb2 port to prevent a usb3
93 * device from degrading to its usb2 connection
95 if (!port_dev
->is_superspeed
&& peer
)
96 pm_runtime_get_sync(&peer
->dev
);
98 usb_autopm_get_interface(intf
);
99 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, true);
100 msleep(hub_power_on_good_delay(hub
));
101 if (udev
&& !retval
) {
103 * Attempt to wait for usb hub port to be reconnected in order
104 * to make the resume procedure successful. The device may have
105 * disconnected while the port was powered off, so ignore the
108 retval
= hub_port_debounce_be_connected(hub
, port1
);
110 dev_dbg(&port_dev
->dev
, "can't get reconnection after setting port power on, status %d\n",
114 /* Force the child awake to revalidate after the power loss. */
115 if (!test_and_set_bit(port1
, hub
->child_usage_bits
)) {
116 pm_runtime_get_noresume(&port_dev
->dev
);
117 pm_request_resume(&udev
->dev
);
121 usb_autopm_put_interface(intf
);
126 static int usb_port_runtime_suspend(struct device
*dev
)
128 struct usb_port
*port_dev
= to_usb_port(dev
);
129 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
130 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
131 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
132 struct usb_port
*peer
= port_dev
->peer
;
133 int port1
= port_dev
->portnum
;
141 if (dev_pm_qos_flags(&port_dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
)
145 usb_autopm_get_interface(intf
);
146 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, false);
147 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_CONNECTION
);
148 if (!port_dev
->is_superspeed
)
149 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_ENABLE
);
150 usb_autopm_put_interface(intf
);
153 * Our peer usb3 port may now be able to suspend, so
154 * asynchronously queue a suspend request to observe that this
155 * usb2 port is now off.
157 if (!port_dev
->is_superspeed
&& peer
)
158 pm_runtime_put(&peer
->dev
);
164 static const struct dev_pm_ops usb_port_pm_ops
= {
165 #ifdef CONFIG_PM_RUNTIME
166 .runtime_suspend
= usb_port_runtime_suspend
,
167 .runtime_resume
= usb_port_runtime_resume
,
171 struct device_type usb_port_device_type
= {
173 .release
= usb_port_device_release
,
174 .pm
= &usb_port_pm_ops
,
177 static struct device_driver usb_port_driver
= {
179 .owner
= THIS_MODULE
,
182 static int link_peers(struct usb_port
*left
, struct usb_port
*right
)
184 struct usb_port
*ss_port
, *hs_port
;
187 if (left
->peer
== right
&& right
->peer
== left
)
190 if (left
->peer
|| right
->peer
) {
191 struct usb_port
*lpeer
= left
->peer
;
192 struct usb_port
*rpeer
= right
->peer
;
194 WARN(1, "failed to peer %s and %s (%s -> %p) (%s -> %p)\n",
195 dev_name(&left
->dev
), dev_name(&right
->dev
),
196 dev_name(&left
->dev
), lpeer
,
197 dev_name(&right
->dev
), rpeer
);
201 rc
= sysfs_create_link(&left
->dev
.kobj
, &right
->dev
.kobj
, "peer");
204 rc
= sysfs_create_link(&right
->dev
.kobj
, &left
->dev
.kobj
, "peer");
206 sysfs_remove_link(&left
->dev
.kobj
, "peer");
211 * We need to wake the HiSpeed port to make sure we don't race
212 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
213 * may miss a suspend event for the SuperSpeed port.
215 if (left
->is_superspeed
) {
217 WARN_ON(right
->is_superspeed
);
221 WARN_ON(!right
->is_superspeed
);
224 pm_runtime_get_sync(&hs_port
->dev
);
230 * The SuperSpeed reference is dropped when the HiSpeed port in
231 * this relationship suspends, i.e. when it is safe to allow a
232 * SuperSpeed connection to drop since there is no risk of a
233 * device degrading to its powered-off HiSpeed connection.
235 * Also, drop the HiSpeed ref taken above.
237 pm_runtime_get_sync(&ss_port
->dev
);
238 pm_runtime_put(&hs_port
->dev
);
243 static void link_peers_report(struct usb_port
*left
, struct usb_port
*right
)
247 rc
= link_peers(left
, right
);
249 dev_dbg(&left
->dev
, "peered to %s\n", dev_name(&right
->dev
));
251 dev_warn(&left
->dev
, "failed to peer to %s (%d)\n",
252 dev_name(&right
->dev
), rc
);
253 pr_warn_once("usb: port power management may be unreliable\n");
257 static void unlink_peers(struct usb_port
*left
, struct usb_port
*right
)
259 struct usb_port
*ss_port
, *hs_port
;
261 WARN(right
->peer
!= left
|| left
->peer
!= right
,
262 "%s and %s are not peers?\n",
263 dev_name(&left
->dev
), dev_name(&right
->dev
));
266 * We wake the HiSpeed port to make sure we don't race its
267 * usb_port_runtime_resume() event which takes a SuperSpeed ref
268 * when ->peer is !NULL.
270 if (left
->is_superspeed
) {
278 pm_runtime_get_sync(&hs_port
->dev
);
280 sysfs_remove_link(&left
->dev
.kobj
, "peer");
282 sysfs_remove_link(&right
->dev
.kobj
, "peer");
285 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
286 pm_runtime_put(&ss_port
->dev
);
288 /* Drop the ref taken above */
289 pm_runtime_put(&hs_port
->dev
);
293 * For each usb hub device in the system check to see if it is in the
294 * peer domain of the given port_dev, and if it is check to see if it
295 * has a port that matches the given port by location
297 static int match_location(struct usb_device
*peer_hdev
, void *p
)
300 struct usb_hcd
*hcd
, *peer_hcd
;
301 struct usb_port
*port_dev
= p
, *peer
;
302 struct usb_hub
*peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
303 struct usb_device
*hdev
= to_usb_device(port_dev
->dev
.parent
->parent
);
308 hcd
= bus_to_hcd(hdev
->bus
);
309 peer_hcd
= bus_to_hcd(peer_hdev
->bus
);
310 /* peer_hcd is provisional until we verify it against the known peer */
311 if (peer_hcd
!= hcd
->shared_hcd
)
314 for (port1
= 1; port1
<= peer_hdev
->maxchild
; port1
++) {
315 peer
= peer_hub
->ports
[port1
- 1];
316 if (peer
&& peer
->location
== port_dev
->location
) {
317 link_peers_report(port_dev
, peer
);
326 * Find the peer port either via explicit platform firmware "location"
327 * data, the peer hcd for root hubs, or the upstream peer relationship
328 * for all other hubs.
330 static void find_and_link_peer(struct usb_hub
*hub
, int port1
)
332 struct usb_port
*port_dev
= hub
->ports
[port1
- 1], *peer
;
333 struct usb_device
*hdev
= hub
->hdev
;
334 struct usb_device
*peer_hdev
;
335 struct usb_hub
*peer_hub
;
338 * If location data is available then we can only peer this port
339 * by a location match, not the default peer (lest we create a
340 * situation where we need to go back and undo a default peering
341 * when the port is later peered by location data)
343 if (port_dev
->location
) {
344 /* we link the peer in match_location() if found */
345 usb_for_each_dev(port_dev
, match_location
);
347 } else if (!hdev
->parent
) {
348 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
349 struct usb_hcd
*peer_hcd
= hcd
->shared_hcd
;
354 peer_hdev
= peer_hcd
->self
.root_hub
;
356 struct usb_port
*upstream
;
357 struct usb_device
*parent
= hdev
->parent
;
358 struct usb_hub
*parent_hub
= usb_hub_to_struct_hub(parent
);
363 upstream
= parent_hub
->ports
[hdev
->portnum
- 1];
364 if (!upstream
|| !upstream
->peer
)
367 peer_hdev
= upstream
->peer
->child
;
370 peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
371 if (!peer_hub
|| port1
> peer_hdev
->maxchild
)
375 * we found a valid default peer, last check is to make sure it
376 * does not have location data
378 peer
= peer_hub
->ports
[port1
- 1];
379 if (peer
&& peer
->location
== 0)
380 link_peers_report(port_dev
, peer
);
383 int usb_hub_create_port_device(struct usb_hub
*hub
, int port1
)
385 struct usb_port
*port_dev
;
388 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
394 hub
->ports
[port1
- 1] = port_dev
;
395 port_dev
->portnum
= port1
;
396 set_bit(port1
, hub
->power_bits
);
397 port_dev
->dev
.parent
= hub
->intfdev
;
398 port_dev
->dev
.groups
= port_dev_group
;
399 port_dev
->dev
.type
= &usb_port_device_type
;
400 port_dev
->dev
.driver
= &usb_port_driver
;
401 if (hub_is_superspeed(hub
->hdev
))
402 port_dev
->is_superspeed
= 1;
403 dev_set_name(&port_dev
->dev
, "%s-port%d", dev_name(&hub
->hdev
->dev
),
405 mutex_init(&port_dev
->status_lock
);
406 retval
= device_register(&port_dev
->dev
);
410 find_and_link_peer(hub
, port1
);
412 pm_runtime_set_active(&port_dev
->dev
);
415 * Do not enable port runtime pm if the hub does not support
416 * power switching. Also, userspace must have final say of
417 * whether a port is permitted to power-off. Do not enable
418 * runtime pm if we fail to expose pm_qos_no_power_off.
420 if (hub_is_port_power_switchable(hub
)
421 && dev_pm_qos_expose_flags(&port_dev
->dev
,
422 PM_QOS_FLAG_NO_POWER_OFF
) == 0)
423 pm_runtime_enable(&port_dev
->dev
);
425 device_enable_async_suspend(&port_dev
->dev
);
429 put_device(&port_dev
->dev
);
434 void usb_hub_remove_port_device(struct usb_hub
*hub
, int port1
)
436 struct usb_port
*port_dev
= hub
->ports
[port1
- 1];
437 struct usb_port
*peer
;
439 peer
= port_dev
->peer
;
441 unlink_peers(port_dev
, peer
);
442 device_unregister(&port_dev
->dev
);