blob: 53c1f6e604b152923bf323317d270b05da6c20dd [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
Dan Williams6c79fe42014-06-17 16:16:27 -070024static int usb_port_block_power_off;
25
Lan Tianyucef74682013-01-20 01:53:32 +080026static const struct attribute_group *port_dev_group[];
27
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070028static ssize_t connect_type_show(struct device *dev,
29 struct device_attribute *attr, char *buf)
Lan Tianyucef74682013-01-20 01:53:32 +080030{
31 struct usb_port *port_dev = to_usb_port(dev);
32 char *result;
33
34 switch (port_dev->connect_type) {
35 case USB_PORT_CONNECT_TYPE_HOT_PLUG:
36 result = "hotplug";
37 break;
38 case USB_PORT_CONNECT_TYPE_HARD_WIRED:
39 result = "hardwired";
40 break;
41 case USB_PORT_NOT_USED:
42 result = "not used";
43 break;
44 default:
45 result = "unknown";
46 break;
47 }
48
49 return sprintf(buf, "%s\n", result);
50}
Greg Kroah-Hartmand03f2542013-08-23 16:05:26 -070051static DEVICE_ATTR_RO(connect_type);
Lan Tianyucef74682013-01-20 01:53:32 +080052
Lu Baolu513072d2015-11-14 16:26:33 +080053static ssize_t usb3_lpm_permit_show(struct device *dev,
54 struct device_attribute *attr, char *buf)
55{
56 struct usb_port *port_dev = to_usb_port(dev);
57 const char *p;
58
59 if (port_dev->usb3_lpm_u1_permit) {
60 if (port_dev->usb3_lpm_u2_permit)
61 p = "u1_u2";
62 else
63 p = "u1";
64 } else {
65 if (port_dev->usb3_lpm_u2_permit)
66 p = "u2";
67 else
68 p = "0";
69 }
70
71 return sprintf(buf, "%s\n", p);
72}
73
74static ssize_t usb3_lpm_permit_store(struct device *dev,
75 struct device_attribute *attr,
76 const char *buf, size_t count)
77{
78 struct usb_port *port_dev = to_usb_port(dev);
79 struct usb_device *udev = port_dev->child;
80 struct usb_hcd *hcd;
81
82 if (!strncmp(buf, "u1_u2", 5)) {
83 port_dev->usb3_lpm_u1_permit = 1;
84 port_dev->usb3_lpm_u2_permit = 1;
85
86 } else if (!strncmp(buf, "u1", 2)) {
87 port_dev->usb3_lpm_u1_permit = 1;
88 port_dev->usb3_lpm_u2_permit = 0;
89
90 } else if (!strncmp(buf, "u2", 2)) {
91 port_dev->usb3_lpm_u1_permit = 0;
92 port_dev->usb3_lpm_u2_permit = 1;
93
94 } else if (!strncmp(buf, "0", 1)) {
95 port_dev->usb3_lpm_u1_permit = 0;
96 port_dev->usb3_lpm_u2_permit = 0;
97 } else
98 return -EINVAL;
99
100 /* If device is connected to the port, disable or enable lpm
101 * to make new u1 u2 setting take effect immediately.
102 */
103 if (udev) {
104 hcd = bus_to_hcd(udev->bus);
105 if (!hcd)
106 return -EINVAL;
107 usb_lock_device(udev);
108 mutex_lock(hcd->bandwidth_mutex);
109 if (!usb_disable_lpm(udev))
110 usb_enable_lpm(udev);
111 mutex_unlock(hcd->bandwidth_mutex);
112 usb_unlock_device(udev);
113 }
114
115 return count;
116}
117static DEVICE_ATTR_RW(usb3_lpm_permit);
118
Lan Tianyucef74682013-01-20 01:53:32 +0800119static struct attribute *port_dev_attrs[] = {
120 &dev_attr_connect_type.attr,
121 NULL,
122};
123
124static struct attribute_group port_dev_attr_grp = {
125 .attrs = port_dev_attrs,
126};
127
128static const struct attribute_group *port_dev_group[] = {
129 &port_dev_attr_grp,
130 NULL,
131};
132
Lu Baolu513072d2015-11-14 16:26:33 +0800133static struct attribute *port_dev_usb3_attrs[] = {
134 &dev_attr_usb3_lpm_permit.attr,
135 NULL,
136};
137
138static struct attribute_group port_dev_usb3_attr_grp = {
139 .attrs = port_dev_usb3_attrs,
140};
141
142static const struct attribute_group *port_dev_usb3_group[] = {
143 &port_dev_attr_grp,
144 &port_dev_usb3_attr_grp,
145 NULL,
146};
147
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800148static void usb_port_device_release(struct device *dev)
149{
150 struct usb_port *port_dev = to_usb_port(dev);
151
Dan Williamse3d10502014-06-17 16:16:32 -0700152 kfree(port_dev->req);
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800153 kfree(port_dev);
154}
155
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +0100156#ifdef CONFIG_PM
Lan Tianyu971fcd42013-01-23 04:26:29 +0800157static int usb_port_runtime_resume(struct device *dev)
158{
159 struct usb_port *port_dev = to_usb_port(dev);
160 struct usb_device *hdev = to_usb_device(dev->parent->parent);
161 struct usb_interface *intf = to_usb_interface(dev->parent);
Lan Tianyuad493e52013-01-23 04:26:30 +0800162 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Dan Williams7027df32014-05-20 18:09:36 -0700163 struct usb_device *udev = port_dev->child;
Dan Williams7ad3c472014-05-20 18:08:57 -0700164 struct usb_port *peer = port_dev->peer;
Lan Tianyuad493e52013-01-23 04:26:30 +0800165 int port1 = port_dev->portnum;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800166 int retval;
167
Lan Tianyuad493e52013-01-23 04:26:30 +0800168 if (!hub)
169 return -EINVAL;
Alan Stern600856c2014-05-20 18:08:07 -0700170 if (hub->in_reset) {
Dan Williamsd5c38342014-05-20 18:08:52 -0700171 set_bit(port1, hub->power_bits);
Alan Stern600856c2014-05-20 18:08:07 -0700172 return 0;
173 }
Lan Tianyuad493e52013-01-23 04:26:30 +0800174
Dan Williams7ad3c472014-05-20 18:08:57 -0700175 /*
176 * Power on our usb3 peer before this usb2 port to prevent a usb3
177 * device from degrading to its usb2 connection
178 */
179 if (!port_dev->is_superspeed && peer)
180 pm_runtime_get_sync(&peer->dev);
181
Eugeniu Rosca480dc952020-02-26 18:50:36 +0100182 retval = usb_autopm_get_interface(intf);
183 if (retval < 0)
184 return retval;
185
Mathias Nyman41341262013-06-18 17:28:48 +0300186 retval = usb_hub_set_port_power(hdev, hub, port1, true);
Dan Williams7ad3c472014-05-20 18:08:57 -0700187 msleep(hub_power_on_good_delay(hub));
Dan Williams7027df32014-05-20 18:09:36 -0700188 if (udev && !retval) {
Lan Tianyuad493e52013-01-23 04:26:30 +0800189 /*
Dan Williams3cd12f92014-05-29 12:58:46 -0700190 * Our preference is to simply wait for the port to reconnect,
191 * as that is the lowest latency method to restart the port.
192 * However, there are cases where toggling port power results in
193 * the host port and the device port getting out of sync causing
194 * a link training live lock. Upon timeout, flag the port as
195 * needing warm reset recovery (to be performed later by
196 * usb_port_resume() as requested via usb_wakeup_notification())
Lan Tianyuad493e52013-01-23 04:26:30 +0800197 */
Dan Williams3cd12f92014-05-29 12:58:46 -0700198 if (hub_port_debounce_be_connected(hub, port1) < 0) {
199 dev_dbg(&port_dev->dev, "reconnect timeout\n");
200 if (hub_is_superspeed(hdev))
201 set_bit(port1, hub->warm_reset_bits);
202 }
Dan Williams7027df32014-05-20 18:09:36 -0700203
204 /* Force the child awake to revalidate after the power loss. */
205 if (!test_and_set_bit(port1, hub->child_usage_bits)) {
206 pm_runtime_get_noresume(&port_dev->dev);
207 pm_request_resume(&udev->dev);
208 }
Lan Tianyuad493e52013-01-23 04:26:30 +0800209 }
210
Lan Tianyu971fcd42013-01-23 04:26:29 +0800211 usb_autopm_put_interface(intf);
Dan Williams7ad3c472014-05-20 18:08:57 -0700212
Lan Tianyu971fcd42013-01-23 04:26:29 +0800213 return retval;
214}
215
216static int usb_port_runtime_suspend(struct device *dev)
217{
218 struct usb_port *port_dev = to_usb_port(dev);
219 struct usb_device *hdev = to_usb_device(dev->parent->parent);
220 struct usb_interface *intf = to_usb_interface(dev->parent);
Lan Tianyuad493e52013-01-23 04:26:30 +0800221 struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
Dan Williams7ad3c472014-05-20 18:08:57 -0700222 struct usb_port *peer = port_dev->peer;
Lan Tianyuad493e52013-01-23 04:26:30 +0800223 int port1 = port_dev->portnum;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800224 int retval;
225
Lan Tianyuad493e52013-01-23 04:26:30 +0800226 if (!hub)
227 return -EINVAL;
Alan Stern600856c2014-05-20 18:08:07 -0700228 if (hub->in_reset)
229 return -EBUSY;
Lan Tianyuad493e52013-01-23 04:26:30 +0800230
Lan Tianyu971fcd42013-01-23 04:26:29 +0800231 if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
232 == PM_QOS_FLAGS_ALL)
233 return -EAGAIN;
234
Dan Williams6c79fe42014-06-17 16:16:27 -0700235 if (usb_port_block_power_off)
236 return -EBUSY;
237
Eugeniu Rosca480dc952020-02-26 18:50:36 +0100238 retval = usb_autopm_get_interface(intf);
239 if (retval < 0)
240 return retval;
241
Mathias Nyman41341262013-06-18 17:28:48 +0300242 retval = usb_hub_set_port_power(hdev, hub, port1, false);
Lan Tianyuad493e52013-01-23 04:26:30 +0800243 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
Dan Williams69080582014-05-20 18:09:10 -0700244 if (!port_dev->is_superspeed)
245 usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
Lan Tianyu971fcd42013-01-23 04:26:29 +0800246 usb_autopm_put_interface(intf);
Dan Williams7ad3c472014-05-20 18:08:57 -0700247
248 /*
249 * Our peer usb3 port may now be able to suspend, so
250 * asynchronously queue a suspend request to observe that this
251 * usb2 port is now off.
252 */
253 if (!port_dev->is_superspeed && peer)
254 pm_runtime_put(&peer->dev);
255
Lan Tianyu971fcd42013-01-23 04:26:29 +0800256 return retval;
257}
258#endif
259
260static const struct dev_pm_ops usb_port_pm_ops = {
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +0100261#ifdef CONFIG_PM
Lan Tianyu971fcd42013-01-23 04:26:29 +0800262 .runtime_suspend = usb_port_runtime_suspend,
263 .runtime_resume = usb_port_runtime_resume,
Lan Tianyu971fcd42013-01-23 04:26:29 +0800264#endif
265};
266
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800267struct device_type usb_port_device_type = {
268 .name = "usb_port",
269 .release = usb_port_device_release,
Lan Tianyu971fcd42013-01-23 04:26:29 +0800270 .pm = &usb_port_pm_ops,
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800271};
272
Dan Williamsd99f6b42014-05-20 18:08:17 -0700273static struct device_driver usb_port_driver = {
274 .name = "usb",
275 .owner = THIS_MODULE,
276};
277
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700278static int link_peers(struct usb_port *left, struct usb_port *right)
Dan Williamsd8521af2014-05-20 18:08:28 -0700279{
Dan Williams7ad3c472014-05-20 18:08:57 -0700280 struct usb_port *ss_port, *hs_port;
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700281 int rc;
282
Dan Williamsd8521af2014-05-20 18:08:28 -0700283 if (left->peer == right && right->peer == left)
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700284 return 0;
Dan Williamsd8521af2014-05-20 18:08:28 -0700285
286 if (left->peer || right->peer) {
287 struct usb_port *lpeer = left->peer;
288 struct usb_port *rpeer = right->peer;
Dan Williams6c79fe42014-06-17 16:16:27 -0700289 char *method;
Dan Williamsd8521af2014-05-20 18:08:28 -0700290
Dan Williams6c79fe42014-06-17 16:16:27 -0700291 if (left->location && left->location == right->location)
292 method = "location";
293 else
294 method = "default";
295
Don Zickus6406eeb2015-12-03 17:26:27 -0500296 pr_debug("usb: failed to peer %s and %s by %s (%s:%s) (%s:%s)\n",
Dan Williams6c79fe42014-06-17 16:16:27 -0700297 dev_name(&left->dev), dev_name(&right->dev), method,
298 dev_name(&left->dev),
299 lpeer ? dev_name(&lpeer->dev) : "none",
300 dev_name(&right->dev),
301 rpeer ? dev_name(&rpeer->dev) : "none");
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700302 return -EBUSY;
303 }
304
305 rc = sysfs_create_link(&left->dev.kobj, &right->dev.kobj, "peer");
306 if (rc)
307 return rc;
308 rc = sysfs_create_link(&right->dev.kobj, &left->dev.kobj, "peer");
309 if (rc) {
310 sysfs_remove_link(&left->dev.kobj, "peer");
311 return rc;
Dan Williamsd8521af2014-05-20 18:08:28 -0700312 }
313
Dan Williams7ad3c472014-05-20 18:08:57 -0700314 /*
315 * We need to wake the HiSpeed port to make sure we don't race
316 * setting ->peer with usb_port_runtime_suspend(). Otherwise we
317 * may miss a suspend event for the SuperSpeed port.
318 */
319 if (left->is_superspeed) {
320 ss_port = left;
321 WARN_ON(right->is_superspeed);
322 hs_port = right;
323 } else {
324 ss_port = right;
325 WARN_ON(!right->is_superspeed);
326 hs_port = left;
327 }
328 pm_runtime_get_sync(&hs_port->dev);
329
Dan Williamsd8521af2014-05-20 18:08:28 -0700330 left->peer = right;
331 right->peer = left;
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700332
Dan Williams7ad3c472014-05-20 18:08:57 -0700333 /*
334 * The SuperSpeed reference is dropped when the HiSpeed port in
335 * this relationship suspends, i.e. when it is safe to allow a
336 * SuperSpeed connection to drop since there is no risk of a
337 * device degrading to its powered-off HiSpeed connection.
338 *
339 * Also, drop the HiSpeed ref taken above.
340 */
341 pm_runtime_get_sync(&ss_port->dev);
342 pm_runtime_put(&hs_port->dev);
343
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700344 return 0;
345}
346
347static void link_peers_report(struct usb_port *left, struct usb_port *right)
348{
349 int rc;
350
351 rc = link_peers(left, right);
352 if (rc == 0) {
353 dev_dbg(&left->dev, "peered to %s\n", dev_name(&right->dev));
354 } else {
Don Zickus6406eeb2015-12-03 17:26:27 -0500355 dev_dbg(&left->dev, "failed to peer to %s (%d)\n",
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700356 dev_name(&right->dev), rc);
357 pr_warn_once("usb: port power management may be unreliable\n");
Dan Williams6c79fe42014-06-17 16:16:27 -0700358 usb_port_block_power_off = 1;
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700359 }
Dan Williamsd8521af2014-05-20 18:08:28 -0700360}
361
362static void unlink_peers(struct usb_port *left, struct usb_port *right)
363{
Dan Williams7ad3c472014-05-20 18:08:57 -0700364 struct usb_port *ss_port, *hs_port;
365
Dan Williamsd8521af2014-05-20 18:08:28 -0700366 WARN(right->peer != left || left->peer != right,
367 "%s and %s are not peers?\n",
368 dev_name(&left->dev), dev_name(&right->dev));
369
Dan Williams7ad3c472014-05-20 18:08:57 -0700370 /*
371 * We wake the HiSpeed port to make sure we don't race its
372 * usb_port_runtime_resume() event which takes a SuperSpeed ref
373 * when ->peer is !NULL.
374 */
375 if (left->is_superspeed) {
376 ss_port = left;
377 hs_port = right;
378 } else {
379 ss_port = right;
380 hs_port = left;
381 }
382
383 pm_runtime_get_sync(&hs_port->dev);
384
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700385 sysfs_remove_link(&left->dev.kobj, "peer");
Dan Williamsd8521af2014-05-20 18:08:28 -0700386 right->peer = NULL;
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700387 sysfs_remove_link(&right->dev.kobj, "peer");
Dan Williamsd8521af2014-05-20 18:08:28 -0700388 left->peer = NULL;
Dan Williams7ad3c472014-05-20 18:08:57 -0700389
390 /* Drop the SuperSpeed ref held on behalf of the active HiSpeed port */
391 pm_runtime_put(&ss_port->dev);
392
393 /* Drop the ref taken above */
394 pm_runtime_put(&hs_port->dev);
Dan Williamsd8521af2014-05-20 18:08:28 -0700395}
396
Dan Williams8b1ba802014-05-20 18:08:33 -0700397/*
Dan Williams3bfd6592014-05-20 18:08:40 -0700398 * For each usb hub device in the system check to see if it is in the
399 * peer domain of the given port_dev, and if it is check to see if it
400 * has a port that matches the given port by location
401 */
402static int match_location(struct usb_device *peer_hdev, void *p)
403{
404 int port1;
405 struct usb_hcd *hcd, *peer_hcd;
406 struct usb_port *port_dev = p, *peer;
407 struct usb_hub *peer_hub = usb_hub_to_struct_hub(peer_hdev);
408 struct usb_device *hdev = to_usb_device(port_dev->dev.parent->parent);
409
410 if (!peer_hub)
411 return 0;
412
413 hcd = bus_to_hcd(hdev->bus);
414 peer_hcd = bus_to_hcd(peer_hdev->bus);
415 /* peer_hcd is provisional until we verify it against the known peer */
416 if (peer_hcd != hcd->shared_hcd)
417 return 0;
418
419 for (port1 = 1; port1 <= peer_hdev->maxchild; port1++) {
420 peer = peer_hub->ports[port1 - 1];
421 if (peer && peer->location == port_dev->location) {
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700422 link_peers_report(port_dev, peer);
Dan Williams3bfd6592014-05-20 18:08:40 -0700423 return 1; /* done */
424 }
425 }
426
427 return 0;
428}
429
430/*
431 * Find the peer port either via explicit platform firmware "location"
432 * data, the peer hcd for root hubs, or the upstream peer relationship
433 * for all other hubs.
Dan Williams8b1ba802014-05-20 18:08:33 -0700434 */
Dan Williamsd8521af2014-05-20 18:08:28 -0700435static void find_and_link_peer(struct usb_hub *hub, int port1)
436{
437 struct usb_port *port_dev = hub->ports[port1 - 1], *peer;
438 struct usb_device *hdev = hub->hdev;
Dan Williams8b1ba802014-05-20 18:08:33 -0700439 struct usb_device *peer_hdev;
440 struct usb_hub *peer_hub;
Dan Williamsd8521af2014-05-20 18:08:28 -0700441
Dan Williams3bfd6592014-05-20 18:08:40 -0700442 /*
443 * If location data is available then we can only peer this port
444 * by a location match, not the default peer (lest we create a
445 * situation where we need to go back and undo a default peering
446 * when the port is later peered by location data)
447 */
448 if (port_dev->location) {
449 /* we link the peer in match_location() if found */
450 usb_for_each_dev(port_dev, match_location);
451 return;
452 } else if (!hdev->parent) {
Dan Williamsd8521af2014-05-20 18:08:28 -0700453 struct usb_hcd *hcd = bus_to_hcd(hdev->bus);
454 struct usb_hcd *peer_hcd = hcd->shared_hcd;
455
456 if (!peer_hcd)
457 return;
458
459 peer_hdev = peer_hcd->self.root_hub;
Dan Williams8b1ba802014-05-20 18:08:33 -0700460 } else {
461 struct usb_port *upstream;
462 struct usb_device *parent = hdev->parent;
463 struct usb_hub *parent_hub = usb_hub_to_struct_hub(parent);
464
465 if (!parent_hub)
Dan Williamsd8521af2014-05-20 18:08:28 -0700466 return;
467
Dan Williams8b1ba802014-05-20 18:08:33 -0700468 upstream = parent_hub->ports[hdev->portnum - 1];
469 if (!upstream || !upstream->peer)
470 return;
Dan Williamsd8521af2014-05-20 18:08:28 -0700471
Dan Williams8b1ba802014-05-20 18:08:33 -0700472 peer_hdev = upstream->peer->child;
Dan Williamsd8521af2014-05-20 18:08:28 -0700473 }
Dan Williams8b1ba802014-05-20 18:08:33 -0700474
475 peer_hub = usb_hub_to_struct_hub(peer_hdev);
476 if (!peer_hub || port1 > peer_hdev->maxchild)
477 return;
478
Dan Williams3bfd6592014-05-20 18:08:40 -0700479 /*
480 * we found a valid default peer, last check is to make sure it
481 * does not have location data
482 */
Dan Williams8b1ba802014-05-20 18:08:33 -0700483 peer = peer_hub->ports[port1 - 1];
Dan Williams3bfd6592014-05-20 18:08:40 -0700484 if (peer && peer->location == 0)
Dan Williamsb7e38ea2014-05-20 18:08:45 -0700485 link_peers_report(port_dev, peer);
Dan Williamsd8521af2014-05-20 18:08:28 -0700486}
487
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800488int usb_hub_create_port_device(struct usb_hub *hub, int port1)
489{
Dan Williamsd8521af2014-05-20 18:08:28 -0700490 struct usb_port *port_dev;
Lu Baolu513072d2015-11-14 16:26:33 +0800491 struct usb_device *hdev = hub->hdev;
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800492 int retval;
493
494 port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
Dan Williamse3d10502014-06-17 16:16:32 -0700495 if (!port_dev)
496 return -ENOMEM;
497
498 port_dev->req = kzalloc(sizeof(*(port_dev->req)), GFP_KERNEL);
499 if (!port_dev->req) {
500 kfree(port_dev);
501 return -ENOMEM;
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800502 }
503
504 hub->ports[port1 - 1] = port_dev;
Lan Tianyu971fcd42013-01-23 04:26:29 +0800505 port_dev->portnum = port1;
Dan Williamsd5c38342014-05-20 18:08:52 -0700506 set_bit(port1, hub->power_bits);
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800507 port_dev->dev.parent = hub->intfdev;
Lu Baolu513072d2015-11-14 16:26:33 +0800508 if (hub_is_superspeed(hdev)) {
509 port_dev->usb3_lpm_u1_permit = 1;
510 port_dev->usb3_lpm_u2_permit = 1;
511 port_dev->dev.groups = port_dev_usb3_group;
512 } else
513 port_dev->dev.groups = port_dev_group;
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800514 port_dev->dev.type = &usb_port_device_type;
Dan Williamsd99f6b42014-05-20 18:08:17 -0700515 port_dev->dev.driver = &usb_port_driver;
Dan Williams7ad3c472014-05-20 18:08:57 -0700516 if (hub_is_superspeed(hub->hdev))
517 port_dev->is_superspeed = 1;
Dan Williamsd99f6b42014-05-20 18:08:17 -0700518 dev_set_name(&port_dev->dev, "%s-port%d", dev_name(&hub->hdev->dev),
519 port1);
Dan Williams5c79a1e2014-05-20 18:09:26 -0700520 mutex_init(&port_dev->status_lock);
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800521 retval = device_register(&port_dev->dev);
Dan Williamse3d10502014-06-17 16:16:32 -0700522 if (retval) {
523 put_device(&port_dev->dev);
524 return retval;
525 }
526
527 /* Set default policy of port-poweroff disabled. */
528 retval = dev_pm_qos_add_request(&port_dev->dev, port_dev->req,
529 DEV_PM_QOS_FLAGS, PM_QOS_FLAG_NO_POWER_OFF);
530 if (retval < 0) {
531 device_unregister(&port_dev->dev);
532 return retval;
533 }
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800534
Dan Williamsd8521af2014-05-20 18:08:28 -0700535 find_and_link_peer(hub, port1);
536
Dan Williamse3d10502014-06-17 16:16:32 -0700537 /*
538 * Enable runtime pm and hold a refernce that hub_configure()
539 * will drop once the PM_QOS_NO_POWER_OFF flag state has been set
540 * and the hub has been fully registered (hdev->maxchild set).
541 */
Lan Tianyu971fcd42013-01-23 04:26:29 +0800542 pm_runtime_set_active(&port_dev->dev);
Dan Williamse3d10502014-06-17 16:16:32 -0700543 pm_runtime_get_noresume(&port_dev->dev);
544 pm_runtime_enable(&port_dev->dev);
545 device_enable_async_suspend(&port_dev->dev);
Lan Tianyuf6cced12013-01-23 04:26:31 +0800546
Dan Williams9262c192014-05-20 18:08:12 -0700547 /*
Dan Williamse3d10502014-06-17 16:16:32 -0700548 * Keep hidden the ability to enable port-poweroff if the hub
549 * does not support power switching.
Lan Tianyuf6cced12013-01-23 04:26:31 +0800550 */
Dan Williamse3d10502014-06-17 16:16:32 -0700551 if (!hub_is_port_power_switchable(hub))
552 return 0;
Lan Tianyuf6cced12013-01-23 04:26:31 +0800553
Dan Williamse3d10502014-06-17 16:16:32 -0700554 /* Attempt to let userspace take over the policy. */
555 retval = dev_pm_qos_expose_flags(&port_dev->dev,
556 PM_QOS_FLAG_NO_POWER_OFF);
557 if (retval < 0) {
558 dev_warn(&port_dev->dev, "failed to expose pm_qos_no_poweroff\n");
559 return 0;
560 }
561
562 /* Userspace owns the policy, drop the kernel 'no_poweroff' request. */
563 retval = dev_pm_qos_remove_request(port_dev->req);
564 if (retval >= 0) {
565 kfree(port_dev->req);
566 port_dev->req = NULL;
567 }
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800568 return 0;
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800569}
570
Dan Williamsd8521af2014-05-20 18:08:28 -0700571void usb_hub_remove_port_device(struct usb_hub *hub, int port1)
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800572{
Dan Williamsd8521af2014-05-20 18:08:28 -0700573 struct usb_port *port_dev = hub->ports[port1 - 1];
574 struct usb_port *peer;
Lan Tianyu6e30d7c2013-01-11 20:10:38 +0800575
Dan Williamsd8521af2014-05-20 18:08:28 -0700576 peer = port_dev->peer;
577 if (peer)
578 unlink_peers(port_dev, peer);
579 device_unregister(&port_dev->dev);
580}