usb: assign default peer ports for root hubs
[firefly-linux-kernel-4.4.55.git] / drivers / usb / core / port.c
1 /*
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
19 #include <linux/slab.h>
20 #include <linux/pm_qos.h>
21
22 #include "hub.h"
23
24 static const struct attribute_group *port_dev_group[];
25
26 static ssize_t connect_type_show(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 }
49 static DEVICE_ATTR_RO(connect_type);
50
51 static struct attribute *port_dev_attrs[] = {
52         &dev_attr_connect_type.attr,
53         NULL,
54 };
55
56 static struct attribute_group port_dev_attr_grp = {
57         .attrs = port_dev_attrs,
58 };
59
60 static const struct attribute_group *port_dev_group[] = {
61         &port_dev_attr_grp,
62         NULL,
63 };
64
65 static void usb_port_device_release(struct device *dev)
66 {
67         struct usb_port *port_dev = to_usb_port(dev);
68
69         kfree(port_dev);
70 }
71
72 #ifdef CONFIG_PM_RUNTIME
73 static int usb_port_runtime_resume(struct device *dev)
74 {
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;
80         int retval;
81
82         if (!hub)
83                 return -EINVAL;
84         if (hub->in_reset) {
85                 port_dev->power_is_on = 1;
86                 return 0;
87         }
88
89         usb_autopm_get_interface(intf);
90         set_bit(port1, hub->busy_bits);
91
92         retval = usb_hub_set_port_power(hdev, hub, port1, true);
93         if (port_dev->child && !retval) {
94                 /*
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
98                  * return status.
99                  */
100                 retval = hub_port_debounce_be_connected(hub, port1);
101                 if (retval < 0)
102                         dev_dbg(&port_dev->dev, "can't get reconnection after setting port  power on, status %d\n",
103                                         retval);
104                 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
105                 retval = 0;
106         }
107
108         clear_bit(port1, hub->busy_bits);
109         usb_autopm_put_interface(intf);
110         return retval;
111 }
112
113 static int usb_port_runtime_suspend(struct device *dev)
114 {
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;
120         int retval;
121
122         if (!hub)
123                 return -EINVAL;
124         if (hub->in_reset)
125                 return -EBUSY;
126
127         if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
128                         == PM_QOS_FLAGS_ALL)
129                 return -EAGAIN;
130
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);
138         return retval;
139 }
140 #endif
141
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,
146 #endif
147 };
148
149 struct device_type usb_port_device_type = {
150         .name =         "usb_port",
151         .release =      usb_port_device_release,
152         .pm =           &usb_port_pm_ops,
153 };
154
155 static struct device_driver usb_port_driver = {
156         .name = "usb",
157         .owner = THIS_MODULE,
158 };
159
160 static void link_peers(struct usb_port *left, struct usb_port *right)
161 {
162         if (left->peer == right && right->peer == left)
163                 return;
164
165         if (left->peer || right->peer) {
166                 struct usb_port *lpeer = left->peer;
167                 struct usb_port *rpeer = right->peer;
168
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);
173                 return;
174         }
175
176         left->peer = right;
177         right->peer = left;
178 }
179
180 static void unlink_peers(struct usb_port *left, struct usb_port *right)
181 {
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));
185
186         right->peer = NULL;
187         left->peer = NULL;
188 }
189
190 /* set the default peer port for root hubs */
191 static void find_and_link_peer(struct usb_hub *hub, int port1)
192 {
193         struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
194         struct usb_device *hdev = hub->hdev;
195
196         if (!hdev->parent) {
197                 struct usb_hub *peer_hub;
198                 struct usb_device *peer_hdev;
199                 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
200                 struct usb_hcd *peer_hcd = hcd->shared_hcd;
201
202                 if (!peer_hcd)
203                         return;
204
205                 peer_hdev = peer_hcd->self.root_hub;
206                 peer_hub = usb_hub_to_struct_hub(peer_hdev);
207                 if (!peer_hub || port1 > peer_hdev->maxchild)
208                         return;
209
210                 peer = peer_hub->ports[port1 - 1];
211
212                 if (peer)
213                         link_peers(port_dev, peer);
214         }
215 }
216
217 int usb_hub_create_port_device(struct usb_hub *hub, int port1)
218 {
219         struct usb_port *port_dev;
220         int retval;
221
222         port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
223         if (!port_dev) {
224                 retval = -ENOMEM;
225                 goto exit;
226         }
227
228         hub->ports[port1 - 1] = port_dev;
229         port_dev->portnum = port1;
230         port_dev->power_is_on = true;
231         port_dev->dev.parent = hub->intfdev;
232         port_dev->dev.groups = port_dev_group;
233         port_dev->dev.type = &usb_port_device_type;
234         port_dev->dev.driver = &usb_port_driver;
235         dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
236                         port1);
237         retval = device_register(&port_dev->dev);
238         if (retval)
239                 goto error_register;
240
241         find_and_link_peer(hub, port1);
242
243         pm_runtime_set_active(&port_dev->dev);
244
245         /*
246          * Do not enable port runtime pm if the hub does not support
247          * power switching.  Also, userspace must have final say of
248          * whether a port is permitted to power-off.  Do not enable
249          * runtime pm if we fail to expose pm_qos_no_power_off.
250          */
251         if (hub_is_port_power_switchable(hub)
252                         && dev_pm_qos_expose_flags(&port_dev->dev,
253                         PM_QOS_FLAG_NO_POWER_OFF) == 0)
254                 pm_runtime_enable(&port_dev->dev);
255
256         device_enable_async_suspend(&port_dev->dev);
257         return 0;
258
259 error_register:
260         put_device(&port_dev->dev);
261 exit:
262         return retval;
263 }
264
265 void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
266 {
267         struct usb_port *port_dev = hub->ports[port1 - 1];
268         struct usb_port *peer;
269
270         peer = port_dev->peer;
271         if (peer)
272                 unlink_peers(port_dev, peer);
273         device_unregister(&port_dev->dev);
274 }