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_port *peer = port_dev->peer;
80 int port1 = port_dev->portnum;
86 set_bit(port1, hub->power_bits);
91 * Power on our usb3 peer before this usb2 port to prevent a usb3
92 * device from degrading to its usb2 connection
94 if (!port_dev->is_superspeed && peer)
95 pm_runtime_get_sync(&peer->dev);
97 usb_autopm_get_interface(intf);
98 set_bit(port1, hub->busy_bits);
100 retval = usb_hub_set_port_power(hdev, hub, port1, true);
101 msleep(hub_power_on_good_delay(hub));
102 if (port_dev->child && !retval) {
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
109 retval = hub_port_debounce_be_connected(hub, port1);
111 dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
116 clear_bit(port1, hub->busy_bits);
117 usb_autopm_put_interface(intf);
122 static int usb_port_runtime_suspend(struct device *dev)
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);
127 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
128 struct usb_port *peer = port_dev->peer;
129 int port1 = port_dev->portnum;
137 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
141 usb_autopm_get_interface(intf);
142 set_bit(port1, hub->busy_bits);
143 retval = usb_hub_set_port_power(hdev, hub, port1, false);
144 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
145 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
146 clear_bit(port1, hub->busy_bits);
147 usb_autopm_put_interface(intf);
150 * Our peer usb3 port may now be able to suspend, so
151 * asynchronously queue a suspend request to observe that this
152 * usb2 port is now off.
154 if (!port_dev->is_superspeed && peer)
155 pm_runtime_put(&peer->dev);
161 static const struct dev_pm_ops usb_port_pm_ops = {
162 #ifdef CONFIG_PM_RUNTIME
163 .runtime_suspend = usb_port_runtime_suspend,
164 .runtime_resume = usb_port_runtime_resume,
168 struct device_type usb_port_device_type = {
170 .release = usb_port_device_release,
171 .pm = &usb_port_pm_ops,
174 static struct device_driver usb_port_driver = {
176 .owner = THIS_MODULE,
179 static int link_peers(struct usb_port *left, struct usb_port *right)
181 struct usb_port *ss_port, *hs_port;
184 if (left->peer == right && right->peer == left)
187 if (left->peer || right->peer) {
188 struct usb_port *lpeer = left->peer;
189 struct usb_port *rpeer = right->peer;
191 WARN(1, "failed to peer %s and %s (%s -> %p) (%s -> %p)\n",
192 dev_name(&left->dev), dev_name(&right->dev),
193 dev_name(&left->dev), lpeer,
194 dev_name(&right->dev), rpeer);
198 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
201 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
203 sysfs_remove_link(&left->dev.kobj, "peer");
208 * We need to wake the HiSpeed port to make sure we don't race
209 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
210 * may miss a suspend event for the SuperSpeed port.
212 if (left->is_superspeed) {
214 WARN_ON(right->is_superspeed);
218 WARN_ON(!right->is_superspeed);
221 pm_runtime_get_sync(&hs_port->dev);
227 * The SuperSpeed reference is dropped when the HiSpeed port in
228 * this relationship suspends, i.e. when it is safe to allow a
229 * SuperSpeed connection to drop since there is no risk of a
230 * device degrading to its powered-off HiSpeed connection.
232 * Also, drop the HiSpeed ref taken above.
234 pm_runtime_get_sync(&ss_port->dev);
235 pm_runtime_put(&hs_port->dev);
240 static void link_peers_report(struct usb_port *left, struct usb_port *right)
244 rc = link_peers(left, right);
246 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
248 dev_warn(&left->dev, "failed to peer to %s (%d)\n",
249 dev_name(&right->dev), rc);
250 pr_warn_once("usb: port power management may be unreliable\n");
254 static void unlink_peers(struct usb_port *left, struct usb_port *right)
256 struct usb_port *ss_port, *hs_port;
258 WARN(right->peer != left || left->peer != right,
259 "%s and %s are not peers?\n",
260 dev_name(&left->dev), dev_name(&right->dev));
263 * We wake the HiSpeed port to make sure we don't race its
264 * usb_port_runtime_resume() event which takes a SuperSpeed ref
265 * when ->peer is !NULL.
267 if (left->is_superspeed) {
275 pm_runtime_get_sync(&hs_port->dev);
277 sysfs_remove_link(&left->dev.kobj, "peer");
279 sysfs_remove_link(&right->dev.kobj, "peer");
282 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
283 pm_runtime_put(&ss_port->dev);
285 /* Drop the ref taken above */
286 pm_runtime_put(&hs_port->dev);
290 * For each usb hub device in the system check to see if it is in the
291 * peer domain of the given port_dev, and if it is check to see if it
292 * has a port that matches the given port by location
294 static int match_location(struct usb_device *peer_hdev, void *p)
297 struct usb_hcd *hcd, *peer_hcd;
298 struct usb_port *port_dev = p, *peer;
299 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
300 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
305 hcd = bus_to_hcd(hdev->bus);
306 peer_hcd = bus_to_hcd(peer_hdev->bus);
307 /* peer_hcd is provisional until we verify it against the known peer */
308 if (peer_hcd != hcd->shared_hcd)
311 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
312 peer = peer_hub->ports[port1 - 1];
313 if (peer && peer->location == port_dev->location) {
314 link_peers_report(port_dev, peer);
323 * Find the peer port either via explicit platform firmware "location"
324 * data, the peer hcd for root hubs, or the upstream peer relationship
325 * for all other hubs.
327 static void find_and_link_peer(struct usb_hub *hub, int port1)
329 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
330 struct usb_device *hdev = hub->hdev;
331 struct usb_device *peer_hdev;
332 struct usb_hub *peer_hub;
335 * If location data is available then we can only peer this port
336 * by a location match, not the default peer (lest we create a
337 * situation where we need to go back and undo a default peering
338 * when the port is later peered by location data)
340 if (port_dev->location) {
341 /* we link the peer in match_location() if found */
342 usb_for_each_dev(port_dev, match_location);
344 } else if (!hdev->parent) {
345 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
346 struct usb_hcd *peer_hcd = hcd->shared_hcd;
351 peer_hdev = peer_hcd->self.root_hub;
353 struct usb_port *upstream;
354 struct usb_device *parent = hdev->parent;
355 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
360 upstream = parent_hub->ports[hdev->portnum - 1];
361 if (!upstream || !upstream->peer)
364 peer_hdev = upstream->peer->child;
367 peer_hub = usb_hub_to_struct_hub(peer_hdev);
368 if (!peer_hub || port1 > peer_hdev->maxchild)
372 * we found a valid default peer, last check is to make sure it
373 * does not have location data
375 peer = peer_hub->ports[port1 - 1];
376 if (peer && peer->location == 0)
377 link_peers_report(port_dev, peer);
380 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
382 struct usb_port *port_dev;
385 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
391 hub->ports[port1 - 1] = port_dev;
392 port_dev->portnum = port1;
393 set_bit(port1, hub->power_bits);
394 port_dev->dev.parent = hub->intfdev;
395 port_dev->dev.groups = port_dev_group;
396 port_dev->dev.type = &usb_port_device_type;
397 port_dev->dev.driver = &usb_port_driver;
398 if (hub_is_superspeed(hub->hdev))
399 port_dev->is_superspeed = 1;
400 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
402 retval = device_register(&port_dev->dev);
406 find_and_link_peer(hub, port1);
408 pm_runtime_set_active(&port_dev->dev);
411 * Do not enable port runtime pm if the hub does not support
412 * power switching. Also, userspace must have final say of
413 * whether a port is permitted to power-off. Do not enable
414 * runtime pm if we fail to expose pm_qos_no_power_off.
416 if (hub_is_port_power_switchable(hub)
417 && dev_pm_qos_expose_flags(&port_dev->dev,
418 PM_QOS_FLAG_NO_POWER_OFF) == 0)
419 pm_runtime_enable(&port_dev->dev);
421 device_enable_async_suspend(&port_dev->dev);
425 put_device(&port_dev->dev);
430 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
432 struct usb_port *port_dev = hub->ports[port1 - 1];
433 struct usb_port *peer;
435 peer = port_dev->peer;
437 unlink_peers(port_dev, peer);
438 device_unregister(&port_dev->dev);