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 show_port_connect_type(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(connect_type, S_IRUGO, show_port_connect_type,
52 static struct attribute *port_dev_attrs[] = {
53 &dev_attr_connect_type.attr,
57 static struct attribute_group port_dev_attr_grp = {
58 .attrs = port_dev_attrs,
61 static const struct attribute_group *port_dev_group[] = {
66 static void usb_port_device_release(struct device *dev)
68 struct usb_port *port_dev = to_usb_port(dev);
73 #ifdef CONFIG_PM_RUNTIME
74 static int usb_port_runtime_resume(struct device *dev)
76 struct usb_port *port_dev = to_usb_port(dev);
77 struct usb_device *hdev = to_usb_device(dev->parent->parent);
78 struct usb_interface *intf = to_usb_interface(dev->parent);
79 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
80 int port1 = port_dev->portnum;
86 usb_autopm_get_interface(intf);
87 set_bit(port1, hub->busy_bits);
89 retval = usb_hub_set_port_power(hdev, port1, true);
90 if (port_dev->child && !retval) {
92 * Attempt to wait for usb hub port to be reconnected in order
93 * to make the resume procedure successful. The device may have
94 * disconnected while the port was powered off, so ignore the
97 retval = hub_port_debounce_be_connected(hub, port1);
99 dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
101 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
105 clear_bit(port1, hub->busy_bits);
106 usb_autopm_put_interface(intf);
110 static int usb_port_runtime_suspend(struct device *dev)
112 struct usb_port *port_dev = to_usb_port(dev);
113 struct usb_device *hdev = to_usb_device(dev->parent->parent);
114 struct usb_interface *intf = to_usb_interface(dev->parent);
115 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
116 int port1 = port_dev->portnum;
122 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
126 usb_autopm_get_interface(intf);
127 set_bit(port1, hub->busy_bits);
128 retval = usb_hub_set_port_power(hdev, port1, false);
129 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
130 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
131 clear_bit(port1, hub->busy_bits);
132 usb_autopm_put_interface(intf);
137 static const struct dev_pm_ops usb_port_pm_ops = {
138 #ifdef CONFIG_PM_RUNTIME
139 .runtime_suspend = usb_port_runtime_suspend,
140 .runtime_resume = usb_port_runtime_resume,
141 .runtime_idle = pm_generic_runtime_idle,
145 struct device_type usb_port_device_type = {
147 .release = usb_port_device_release,
148 .pm = &usb_port_pm_ops,
151 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
153 struct usb_port *port_dev = NULL;
156 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
162 hub->ports[port1 - 1] = port_dev;
163 port_dev->portnum = port1;
164 port_dev->power_is_on = true;
165 port_dev->dev.parent = hub->intfdev;
166 port_dev->dev.groups = port_dev_group;
167 port_dev->dev.type = &usb_port_device_type;
168 dev_set_name(&port_dev->dev, "port%d", port1);
170 retval = device_register(&port_dev->dev);
174 pm_runtime_set_active(&port_dev->dev);
176 /* It would be dangerous if user space couldn't
177 * prevent usb device from being powered off. So don't
178 * enable port runtime pm if failed to expose port's pm qos.
180 if (!dev_pm_qos_expose_flags(&port_dev->dev,
181 PM_QOS_FLAG_NO_POWER_OFF))
182 pm_runtime_enable(&port_dev->dev);
184 device_enable_async_suspend(&port_dev->dev);
188 put_device(&port_dev->dev);
193 void usb_hub_remove_port_device(struct usb_hub *hub,
196 device_unregister(&hub->ports[port1 - 1]->dev);