port.c 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * usb port device code
 *
 * Copyright (C) 2012 Intel Corp
 *
 * Author: Lan Tianyu <tianyu.lan@intel.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 */

19
#include <linux/slab.h>
20
#include <linux/pm_qos.h>
21

22 23
#include "hub.h"

24 25
static const struct attribute_group *port_dev_group[];

26 27
static ssize_t connect_type_show(struct device *dev,
				 struct device_attribute *attr, char *buf)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{
	struct usb_port *port_dev = to_usb_port(dev);
	char *result;

	switch (port_dev->connect_type) {
	case USB_PORT_CONNECT_TYPE_HOT_PLUG:
		result = "hotplug";
		break;
	case USB_PORT_CONNECT_TYPE_HARD_WIRED:
		result = "hardwired";
		break;
	case USB_PORT_NOT_USED:
		result = "not used";
		break;
	default:
		result = "unknown";
		break;
	}

	return sprintf(buf, "%s\n", result);
}
49
static DEVICE_ATTR_RO(connect_type);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

static struct attribute *port_dev_attrs[] = {
	&dev_attr_connect_type.attr,
	NULL,
};

static struct attribute_group port_dev_attr_grp = {
	.attrs = port_dev_attrs,
};

static const struct attribute_group *port_dev_group[] = {
	&port_dev_attr_grp,
	NULL,
};

65 66 67 68 69 70 71
static void usb_port_device_release(struct device *dev)
{
	struct usb_port *port_dev = to_usb_port(dev);

	kfree(port_dev);
}

72
#ifdef CONFIG_PM_RUNTIME
73 74 75 76 77
static int usb_port_runtime_resume(struct device *dev)
{
	struct usb_port *port_dev = to_usb_port(dev);
	struct usb_device *hdev = to_usb_device(dev->parent->parent);
	struct usb_interface *intf = to_usb_interface(dev->parent);
78 79
	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
	int port1 = port_dev->portnum;
80 81
	int retval;

82 83 84
	if (!hub)
		return -EINVAL;

85
	usb_autopm_get_interface(intf);
86 87
	set_bit(port1, hub->busy_bits);

88
	retval = usb_hub_set_port_power(hdev, hub, port1, true);
89 90
	if (port_dev->child && !retval) {
		/*
91 92 93 94
		 * Attempt to wait for usb hub port to be reconnected in order
		 * to make the resume procedure successful.  The device may have
		 * disconnected while the port was powered off, so ignore the
		 * return status.
95 96
		 */
		retval = hub_port_debounce_be_connected(hub, port1);
97
		if (retval < 0)
98 99 100 101 102 103 104
			dev_dbg(&port_dev->dev, "can't get reconnection after setting port  power on, status %d\n",
					retval);
		usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_ENABLE);
		retval = 0;
	}

	clear_bit(port1, hub->busy_bits);
105 106 107 108 109 110 111 112 113
	usb_autopm_put_interface(intf);
	return retval;
}

static int usb_port_runtime_suspend(struct device *dev)
{
	struct usb_port *port_dev = to_usb_port(dev);
	struct usb_device *hdev = to_usb_device(dev->parent->parent);
	struct usb_interface *intf = to_usb_interface(dev->parent);
114 115
	struct usb_hub *hub = usb_hub_to_struct_hub(hdev);
	int port1 = port_dev->portnum;
116 117
	int retval;

118 119 120
	if (!hub)
		return -EINVAL;

121 122 123 124 125
	if (dev_pm_qos_flags(&port_dev->dev, PM_QOS_FLAG_NO_POWER_OFF)
			== PM_QOS_FLAGS_ALL)
		return -EAGAIN;

	usb_autopm_get_interface(intf);
126
	set_bit(port1, hub->busy_bits);
127
	retval = usb_hub_set_port_power(hdev, hub, port1, false);
128 129 130
	usb_clear_port_feature(hdev, port1, USB_PORT_FEAT_C_CONNECTION);
	usb_clear_port_feature(hdev, port1,	USB_PORT_FEAT_C_ENABLE);
	clear_bit(port1, hub->busy_bits);
131 132 133 134 135 136
	usb_autopm_put_interface(intf);
	return retval;
}
#endif

static const struct dev_pm_ops usb_port_pm_ops = {
137
#ifdef CONFIG_PM_RUNTIME
138 139 140 141 142
	.runtime_suspend =	usb_port_runtime_suspend,
	.runtime_resume =	usb_port_runtime_resume,
#endif
};

143 144 145
struct device_type usb_port_device_type = {
	.name =		"usb_port",
	.release =	usb_port_device_release,
146
	.pm =		&usb_port_pm_ops,
147 148 149 150 151 152 153 154 155 156 157 158 159 160
};

int usb_hub_create_port_device(struct usb_hub *hub, int port1)
{
	struct usb_port *port_dev = NULL;
	int retval;

	port_dev = kzalloc(sizeof(*port_dev), GFP_KERNEL);
	if (!port_dev) {
		retval = -ENOMEM;
		goto exit;
	}

	hub->ports[port1 - 1] = port_dev;
161
	port_dev->portnum = port1;
162
	port_dev->power_is_on = true;
163
	port_dev->dev.parent = hub->intfdev;
164
	port_dev->dev.groups = port_dev_group;
165 166 167 168 169 170 171
	port_dev->dev.type = &usb_port_device_type;
	dev_set_name(&port_dev->dev, "port%d", port1);

	retval = device_register(&port_dev->dev);
	if (retval)
		goto error_register;

172
	pm_runtime_set_active(&port_dev->dev);
173 174 175 176 177 178 179 180 181

	/* It would be dangerous if user space couldn't
	 * prevent usb device from being powered off. So don't
	 * enable port runtime pm if failed to expose port's pm qos.
	 */
	if (!dev_pm_qos_expose_flags(&port_dev->dev,
			PM_QOS_FLAG_NO_POWER_OFF))
		pm_runtime_enable(&port_dev->dev);

182
	device_enable_async_suspend(&port_dev->dev);
183 184 185 186 187 188 189 190 191 192 193 194 195 196
	return 0;

error_register:
	put_device(&port_dev->dev);
exit:
	return retval;
}

void usb_hub_remove_port_device(struct usb_hub *hub,
				       int port1)
{
	device_unregister(&hub->ports[port1 - 1]->dev);
}