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
);
70 usb_acpi_unregister_power_resources(dev
);
74 #ifdef CONFIG_USB_SUSPEND
75 static int usb_port_runtime_resume(struct device
*dev
)
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
);
82 usb_autopm_get_interface(intf
);
83 retval
= usb_hub_set_port_power(hdev
, port_dev
->portnum
, true);
84 usb_autopm_put_interface(intf
);
88 static int usb_port_runtime_suspend(struct device
*dev
)
90 struct usb_port
*port_dev
= to_usb_port(dev
);
91 struct usb_device
*hdev
= to_usb_device(dev
->parent
->parent
);
92 struct usb_interface
*intf
= to_usb_interface(dev
->parent
);
95 if (dev_pm_qos_flags(&port_dev
->dev
, PM_QOS_FLAG_NO_POWER_OFF
)
99 usb_autopm_get_interface(intf
);
100 retval
= usb_hub_set_port_power(hdev
, port_dev
->portnum
, false);
101 usb_autopm_put_interface(intf
);
106 static const struct dev_pm_ops usb_port_pm_ops
= {
107 #ifdef CONFIG_USB_SUSPEND
108 .runtime_suspend
= usb_port_runtime_suspend
,
109 .runtime_resume
= usb_port_runtime_resume
,
110 .runtime_idle
= pm_generic_runtime_idle
,
114 struct device_type usb_port_device_type
= {
116 .release
= usb_port_device_release
,
117 .pm
= &usb_port_pm_ops
,
120 int usb_hub_create_port_device(struct usb_hub
*hub
, int port1
)
122 struct usb_port
*port_dev
= NULL
;
125 port_dev
= kzalloc(sizeof(*port_dev
), GFP_KERNEL
);
131 hub
->ports
[port1
- 1] = port_dev
;
132 port_dev
->portnum
= port1
;
133 port_dev
->dev
.parent
= hub
->intfdev
;
134 port_dev
->dev
.groups
= port_dev_group
;
135 port_dev
->dev
.type
= &usb_port_device_type
;
136 dev_set_name(&port_dev
->dev
, "port%d", port1
);
138 retval
= device_register(&port_dev
->dev
);
142 pm_runtime_set_active(&port_dev
->dev
);
143 pm_runtime_enable(&port_dev
->dev
);
145 retval
= usb_acpi_register_power_resources(&port_dev
->dev
);
146 if (retval
&& retval
!= -ENODEV
)
147 dev_warn(&port_dev
->dev
, "the port can't register its ACPI power resource.\n");
152 put_device(&port_dev
->dev
);
157 void usb_hub_remove_port_device(struct usb_hub
*hub
,
160 device_unregister(&hub
->ports
[port1
- 1]->dev
);