blob: 797f9d51473298582d166858b30e5e464ee2b18c [file] [log] [blame]
Lan Tianyu6e30d7c2013-01-11 20:10:38 +08001/*
2 * usb port device code
3 *
4 * Copyright (C) 2012 Intel Corp
5 *
6 * Author: Lan Tianyu <tianyu.lan@intel.com>
7 *
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.
11 *
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
15 * for more details.
16 *
17 */
18
Lan Tianyu9f7344f2013-01-19 22:30:19 +080019#include <linux/slab.h>
Lan Tianyu971fcd42013-01-23 04:26:29 +080020#include <linux/pm_qos.h>
Lan Tianyu9f7344f2013-01-19 22:30:19 +080021
Lan Tianyu6e30d7c2013-01-11 20:10:38 +080022#include "hub.h"
23
Lan Tianyucef74682013-01-20 01:53:32 +080024static const struct attribute_group *port_dev_group[];
25
26static ssize_t show_port_connect_type(struct device *dev,
27 struct device_attribute *attr, char *buf)
28{
29 struct usb_port *port_dev = to_usb_port(dev);
30 char *result;
31
32 switch (port_dev->connect_type) {
33 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
34 result = "hotplug";
35 break;
36 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
37 result = "hardwired";
38 break;
39 case USB_PORT_NOT_USED:
40 result = "not used";
41 break;
42 default:
43 result = "unknown";
44 break;
45 }
46
47 return sprintf(buf, "%s\n", result);
48}
49static DEVICE_ATTR(connect_type, S_IRUGO, show_port_connect_type,
50 NULL);
51
52static struct attribute *port_dev_attrs[] = {
53 &dev_attr_connect_type.attr,
54 NULL,
55};
56
57static struct attribute_group port_dev_attr_grp = {
58 .attrs = port_dev_attrs,
59};
60
61static const struct attribute_group *port_dev_group[] = {
62 &port_dev_attr_grp,
63 NULL,
64};
65
Lan Tianyu6e30d7c2013-01-11 20:10:38 +080066static void usb_port_device_release(struct device *dev)
67{
68 struct usb_port *port_dev = to_usb_port(dev);
69
Lan Tianyuf6cced12013-01-23 04:26:31 +080070 dev_pm_qos_hide_flags(dev);
Lan Tianyu6e30d7c2013-01-11 20:10:38 +080071 kfree(port_dev);
72}
73
Lan Tianyu971fcd42013-01-23 04:26:29 +080074#ifdef CONFIG_USB_SUSPEND
75static int usb_port_runtime_resume(struct device *dev)
76{
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);
Lan Tianyuad493e52013-01-23 04:26:30 +080080 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
81 int port1 = port_dev->portnum;
Lan Tianyu971fcd42013-01-23 04:26:29 +080082 int retval;
83
Lan Tianyuad493e52013-01-23 04:26:30 +080084 if (!hub)
85 return -EINVAL;
86
Lan Tianyu971fcd42013-01-23 04:26:29 +080087 usb_autopm_get_interface(intf);
Lan Tianyuad493e52013-01-23 04:26:30 +080088 set_bit(port1, hub->busy_bits);
89
90 retval = usb_hub_set_port_power(hdev, port1, true);
91 if (port_dev->child && !retval) {
92 /*
93 * Wait for usb hub port to be reconnected in order to make
94 * the resume procedure successful.
95 */
96 retval = hub_port_debounce_be_connected(hub, port1);
97 if (retval < 0) {
98 dev_dbg(&port_dev->dev, "can't get reconnection after setting port power on, status %d\n",
99 retval);
100 goto out;
101 }
102 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
103
104 /* Set return value to 0 if debounce successful */
105 retval = 0;
106 }
107
108out:
109 clear_bit(port1, hub->busy_bits);
Lan Tianyu971fcd42013-01-23 04:26:29 +0800110 usb_autopm_put_interface(intf);
111 return retval;
112}
113
114static int usb_port_runtime_suspend(struct device *dev)
115{
116 struct usb_port *port_dev = to_usb_port(dev);
117 struct usb_device *hdev = to_usb_device(dev->parent->parent);
118 struct usb_interface *intf = to_usb_interface(dev->parent);
Lan Tianyuad493e52013-01-23 04:26:30 +0800119 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
120 int port1 = port_dev->portnum;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800121 int retval;
122
Lan Tianyuad493e52013-01-23 04:26:30 +0800123 if (!hub)
124 return -EINVAL;
125
Lan Tianyu971fcd42013-01-23 04:26:29 +0800126 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
127 == PM_QOS_FLAGS_ALL)
128 return -EAGAIN;
129
130 usb_autopm_get_interface(intf);
Lan Tianyuad493e52013-01-23 04:26:30 +0800131 set_bit(port1, hub->busy_bits);
132 retval = usb_hub_set_port_power(hdev, port1, false);
133 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
134 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
135 clear_bit(port1, hub->busy_bits);
Lan Tianyu971fcd42013-01-23 04:26:29 +0800136 usb_autopm_put_interface(intf);
137 return retval;
138}
139#endif
140
141static const struct dev_pm_ops usb_port_pm_ops = {
142#ifdef CONFIG_USB_SUSPEND
143 .runtime_suspend = usb_port_runtime_suspend,
144 .runtime_resume = usb_port_runtime_resume,
145 .runtime_idle = pm_generic_runtime_idle,
146#endif
147};
148
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800149struct device_type usb_port_device_type = {
150 .name = "usb_port",
151 .release = usb_port_device_release,
Lan Tianyu971fcd42013-01-23 04:26:29 +0800152 .pm = &usb_port_pm_ops,
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800153};
154
155int usb_hub_create_port_device(struct usb_hub *hub, int port1)
156{
157 struct usb_port *port_dev = NULL;
158 int retval;
159
160 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
161 if (!port_dev) {
162 retval = -ENOMEM;
163 goto exit;
164 }
165
166 hub->ports[port1 - 1] = port_dev;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800167 port_dev->portnum = port1;
Lan Tianyuad493e52013-01-23 04:26:30 +0800168 port_dev->power_is_on = true;
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800169 port_dev->dev.parent = hub->intfdev;
Lan Tianyucef74682013-01-20 01:53:32 +0800170 port_dev->dev.groups = port_dev_group;
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800171 port_dev->dev.type = &usb_port_device_type;
172 dev_set_name(&port_dev->dev, "port%d", port1);
173
174 retval = device_register(&port_dev->dev);
175 if (retval)
176 goto error_register;
177
Lan Tianyu971fcd42013-01-23 04:26:29 +0800178 pm_runtime_set_active(&port_dev->dev);
Lan Tianyuf6cced12013-01-23 04:26:31 +0800179
180 /* It would be dangerous if user space couldn't
181 * prevent usb device from being powered off. So don't
182 * enable port runtime pm if failed to expose port's pm qos.
183 */
184 if (!dev_pm_qos_expose_flags(&port_dev->dev,
185 PM_QOS_FLAG_NO_POWER_OFF))
186 pm_runtime_enable(&port_dev->dev);
187
Lan Tianyu192fef12013-01-23 04:26:32 +0800188 device_enable_async_suspend(&port_dev->dev);
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800189 return 0;
190
191error_register:
192 put_device(&port_dev->dev);
193exit:
194 return retval;
195}
196
197void usb_hub_remove_port_device(struct usb_hub *hub,
198 int port1)
199{
200 device_unregister(&hub->ports[port1 - 1]->dev);
201}
202