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 int port1
= port_dev
->portnum
;
85 port_dev
->power_is_on
= 1;
89 usb_autopm_get_interface(intf
);
90 set_bit(port1
, hub
->busy_bits
);
92 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, true);
93 if (port_dev
->child
&& !retval
) {
95 * Attempt to wait for usb hub port to be reconnected in order
96 * to make the resume procedure successful. The device may have
97 * disconnected while the port was powered off, so ignore the
100 retval
= hub_port_debounce_be_connected(hub
, port1
);
102 dev_dbg(&port_dev
->dev
, "can't get reconnection after setting port power on, status %d\n",
104 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_ENABLE
);
108 clear_bit(port1
, hub
->busy_bits
);
109 usb_autopm_put_interface(intf
);
113 static int usb_port_runtime_suspend(struct device
*dev
)
115 struct usb_port
*port_dev
= to_usb_port(dev
);
116 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
117 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
118 struct usb_hub
*hub
= usb_hub_to_struct_hub(hdev
);
119 int port1
= port_dev
->portnum
;
127 if (dev_pm_qos_flags(&port_dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
)
131 usb_autopm_get_interface(intf
);
132 set_bit(port1
, hub
->busy_bits
);
133 retval
= usb_hub_set_port_power(hdev
, hub
, port1
, false);
134 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_CONNECTION
);
135 usb_clear_port_feature(hdev
, port1
, USB_PORT_FEAT_C_ENABLE
);
136 clear_bit(port1
, hub
->busy_bits
);
137 usb_autopm_put_interface(intf
);
142 static const struct dev_pm_ops usb_port_pm_ops
= {
143 #ifdef CONFIG_PM_RUNTIME
144 .runtime_suspend
= usb_port_runtime_suspend
,
145 .runtime_resume
= usb_port_runtime_resume
,
149 struct device_type usb_port_device_type
= {
151 .release
= usb_port_device_release
,
152 .pm
= &usb_port_pm_ops
,
155 static struct device_driver usb_port_driver
= {
157 .owner
= THIS_MODULE
,
160 static void link_peers(struct usb_port
*left
, struct usb_port
*right
)
162 if (left
->peer
== right
&& right
->peer
== left
)
165 if (left
->peer
|| right
->peer
) {
166 struct usb_port
*lpeer
= left
->peer
;
167 struct usb_port
*rpeer
= right
->peer
;
169 WARN(1, "failed to peer %s and %s (%s -> %p) (%s -> %p)\n",
170 dev_name(&left
->dev
), dev_name(&right
->dev
),
171 dev_name(&left
->dev
), lpeer
,
172 dev_name(&right
->dev
), rpeer
);
180 static void unlink_peers(struct usb_port
*left
, struct usb_port
*right
)
182 WARN(right
->peer
!= left
|| left
->peer
!= right
,
183 "%s and %s are not peers?\n",
184 dev_name(&left
->dev
), dev_name(&right
->dev
));
191 * For each usb hub device in the system check to see if it is in the
192 * peer domain of the given port_dev, and if it is check to see if it
193 * has a port that matches the given port by location
195 static int match_location(struct usb_device
*peer_hdev
, void *p
)
198 struct usb_hcd
*hcd
, *peer_hcd
;
199 struct usb_port
*port_dev
= p
, *peer
;
200 struct usb_hub
*peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
201 struct usb_device
*hdev
= to_usb_device(port_dev
->dev
.parent
->parent
);
206 hcd
= bus_to_hcd(hdev
->bus
);
207 peer_hcd
= bus_to_hcd(peer_hdev
->bus
);
208 /* peer_hcd is provisional until we verify it against the known peer */
209 if (peer_hcd
!= hcd
->shared_hcd
)
212 for (port1
= 1; port1
<= peer_hdev
->maxchild
; port1
++) {
213 peer
= peer_hub
->ports
[port1
- 1];
214 if (peer
&& peer
->location
== port_dev
->location
) {
215 link_peers(port_dev
, peer
);
224 * Find the peer port either via explicit platform firmware "location"
225 * data, the peer hcd for root hubs, or the upstream peer relationship
226 * for all other hubs.
228 static void find_and_link_peer(struct usb_hub
*hub
, int port1
)
230 struct usb_port
*port_dev
= hub
->ports
[port1
- 1], *peer
;
231 struct usb_device
*hdev
= hub
->hdev
;
232 struct usb_device
*peer_hdev
;
233 struct usb_hub
*peer_hub
;
236 * If location data is available then we can only peer this port
237 * by a location match, not the default peer (lest we create a
238 * situation where we need to go back and undo a default peering
239 * when the port is later peered by location data)
241 if (port_dev
->location
) {
242 /* we link the peer in match_location() if found */
243 usb_for_each_dev(port_dev
, match_location
);
245 } else if (!hdev
->parent
) {
246 struct usb_hcd
*hcd
= bus_to_hcd(hdev
->bus
);
247 struct usb_hcd
*peer_hcd
= hcd
->shared_hcd
;
252 peer_hdev
= peer_hcd
->self
.root_hub
;
254 struct usb_port
*upstream
;
255 struct usb_device
*parent
= hdev
->parent
;
256 struct usb_hub
*parent_hub
= usb_hub_to_struct_hub(parent
);
261 upstream
= parent_hub
->ports
[hdev
->portnum
- 1];
262 if (!upstream
|| !upstream
->peer
)
265 peer_hdev
= upstream
->peer
->child
;
268 peer_hub
= usb_hub_to_struct_hub(peer_hdev
);
269 if (!peer_hub
|| port1
> peer_hdev
->maxchild
)
273 * we found a valid default peer, last check is to make sure it
274 * does not have location data
276 peer
= peer_hub
->ports
[port1
- 1];
277 if (peer
&& peer
->location
== 0)
278 link_peers(port_dev
, peer
);
281 int usb_hub_create_port_device(struct usb_hub
*hub
, int port1
)
283 struct usb_port
*port_dev
;
286 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
292 hub
->ports
[port1
- 1] = port_dev
;
293 port_dev
->portnum
= port1
;
294 port_dev
->power_is_on
= true;
295 port_dev
->dev
.parent
= hub
->intfdev
;
296 port_dev
->dev
.groups
= port_dev_group
;
297 port_dev
->dev
.type
= &usb_port_device_type
;
298 port_dev
->dev
.driver
= &usb_port_driver
;
299 dev_set_name(&port_dev
->dev
, "%s-port%d", dev_name(&hub
->hdev
->dev
),
301 retval
= device_register(&port_dev
->dev
);
305 find_and_link_peer(hub
, port1
);
307 pm_runtime_set_active(&port_dev
->dev
);
310 * Do not enable port runtime pm if the hub does not support
311 * power switching. Also, userspace must have final say of
312 * whether a port is permitted to power-off. Do not enable
313 * runtime pm if we fail to expose pm_qos_no_power_off.
315 if (hub_is_port_power_switchable(hub
)
316 && dev_pm_qos_expose_flags(&port_dev
->dev
,
317 PM_QOS_FLAG_NO_POWER_OFF
) == 0)
318 pm_runtime_enable(&port_dev
->dev
);
320 device_enable_async_suspend(&port_dev
->dev
);
324 put_device(&port_dev
->dev
);
329 void usb_hub_remove_port_device(struct usb_hub
*hub
, int port1
)
331 struct usb_port
*port_dev
= hub
->ports
[port1
- 1];
332 struct usb_port
*peer
;
334 peer
= port_dev
->peer
;
336 unlink_peers(port_dev
, peer
);
337 device_unregister(&port_dev
->dev
);