aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/typec/port-mapper.c
blob: 9b0991bdf391a9efd6064a862996730eb52eb0db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// SPDX-License-Identifier: GPL-2.0
/*
 * USB Type-C Connector Class Port Mapping Utility
 *
 * Copyright (C) 2021, Intel Corporation
 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
 */

#include <linux/acpi.h>
#include <linux/usb.h>
#include <linux/usb/typec.h>

#include "class.h"

struct port_node {
	struct list_head list;
	struct device *dev;
	void *pld;
};

static int acpi_pld_match(const struct acpi_pld_info *pld1,
			  const struct acpi_pld_info *pld2)
{
	if (!pld1 || !pld2)
		return 0;

	/*
	 * To speed things up, first checking only the group_position. It seems
	 * to often have the first unique value in the _PLD.
	 */
	if (pld1->group_position == pld2->group_position)
		return !memcmp(pld1, pld2, sizeof(struct acpi_pld_info));

	return 0;
}

static void *get_pld(struct device *dev)
{
#ifdef CONFIG_ACPI
	struct acpi_pld_info *pld;
	acpi_status status;

	if (!has_acpi_companion(dev))
		return NULL;

	status = acpi_get_physical_device_location(ACPI_HANDLE(dev), &pld);
	if (ACPI_FAILURE(status))
		return NULL;

	return pld;
#else
	return NULL;
#endif
}

static void free_pld(void *pld)
{
#ifdef CONFIG_ACPI
	ACPI_FREE(pld);
#endif
}

static int __link_port(struct typec_port *con, struct port_node *node)
{
	int ret;

	ret = sysfs_create_link(&node->dev->kobj, &con->dev.kobj, "connector");
	if (ret)
		return ret;

	ret = sysfs_create_link(&con->dev.kobj, &node->dev->kobj,
				dev_name(node->dev));
	if (ret) {
		sysfs_remove_link(&node->dev->kobj, "connector");
		return ret;
	}

	list_add_tail(&node->list, &con->port_list);

	return 0;
}

static int link_port(struct typec_port *con, struct port_node *node)
{
	int ret;

	mutex_lock(&con->port_list_lock);
	ret = __link_port(con, node);
	mutex_unlock(&con->port_list_lock);

	return ret;
}

static void __unlink_port(struct typec_port *con, struct port_node *node)
{
	sysfs_remove_link(&con->dev.kobj, dev_name(node->dev));
	sysfs_remove_link(&node->dev->kobj, "connector");
	list_del(&node->list);
}

static void unlink_port(struct typec_port *con, struct port_node *node)
{
	mutex_lock(&con->port_list_lock);
	__unlink_port(con, node);
	mutex_unlock(&con->port_list_lock);
}

static struct port_node *create_port_node(struct device *port)
{
	struct port_node *node;

	node = kzalloc(sizeof(*node), GFP_KERNEL);
	if (!node)
		return ERR_PTR(-ENOMEM);

	node->dev = get_device(port);
	node->pld = get_pld(port);

	return node;
}

static void remove_port_node(struct port_node *node)
{
	put_device(node->dev);
	free_pld(node->pld);
	kfree(node);
}

static int connector_match(struct device *dev, const void *data)
{
	const struct port_node *node = data;

	if (!is_typec_port(dev))
		return 0;

	return acpi_pld_match(to_typec_port(dev)->pld, node->pld);
}

static struct device *find_connector(struct port_node *node)
{
	if (!node->pld)
		return NULL;

	return class_find_device(&typec_class, NULL, node, connector_match);
}

/**
 * typec_link_port - Link a port to its connector
 * @port: The port device
 *
 * Find the connector of @port and create symlink named "connector" for it.
 * Returns 0 on success, or errno in case of a failure.
 *
 * NOTE. The function increments the reference count of @port on success.
 */
int typec_link_port(struct device *port)
{
	struct device *connector;
	struct port_node *node;
	int ret;

	node = create_port_node(port);
	if (IS_ERR(node))
		return PTR_ERR(node);

	connector = find_connector(node);
	if (!connector) {
		ret = 0;
		goto remove_node;
	}

	ret = link_port(to_typec_port(connector), node);
	if (ret)
		goto put_connector;

	return 0;

put_connector:
	put_device(connector);
remove_node:
	remove_port_node(node);

	return ret;
}
EXPORT_SYMBOL_GPL(typec_link_port);

static int port_match_and_unlink(struct device *connector, void *port)
{
	struct port_node *node;
	struct port_node *tmp;
	int ret = 0;

	if (!is_typec_port(connector))
		return 0;

	mutex_lock(&to_typec_port(connector)->port_list_lock);
	list_for_each_entry_safe(node, tmp, &to_typec_port(connector)->port_list, list) {
		ret = node->dev == port;
		if (ret) {
			unlink_port(to_typec_port(connector), node);
			remove_port_node(node);
			put_device(connector);
			break;
		}
	}
	mutex_unlock(&to_typec_port(connector)->port_list_lock);

	return ret;
}

/**
 * typec_unlink_port - Unlink port from its connector
 * @port: The port device
 *
 * Removes the symlink "connector" and decrements the reference count of @port.
 */
void typec_unlink_port(struct device *port)
{
	class_for_each_device(&typec_class, NULL, port, port_match_and_unlink);
}
EXPORT_SYMBOL_GPL(typec_unlink_port);

static int each_port(struct device *port, void *connector)
{
	struct port_node *node;
	int ret;

	node = create_port_node(port);
	if (IS_ERR(node))
		return PTR_ERR(node);

	if (!connector_match(connector, node)) {
		remove_port_node(node);
		return 0;
	}

	ret = link_port(to_typec_port(connector), node);
	if (ret) {
		remove_port_node(node->pld);
		return ret;
	}

	get_device(connector);

	return 0;
}

int typec_link_ports(struct typec_port *con)
{
	int ret = 0;

	con->pld = get_pld(&con->dev);
	if (!con->pld)
		return 0;

	ret = usb_for_each_port(&con->dev, each_port);
	if (ret)
		typec_unlink_ports(con);

	return ret;
}

void typec_unlink_ports(struct typec_port *con)
{
	struct port_node *node;
	struct port_node *tmp;

	mutex_lock(&con->port_list_lock);

	list_for_each_entry_safe(node, tmp, &con->port_list, list) {
		__unlink_port(con, node);
		remove_port_node(node);
		put_device(&con->dev);
	}

	mutex_unlock(&con->port_list_lock);

	free_pld(con->pld);
}