component.c 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Componentized device handling.
 *
 * 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 is work in progress.  We gather up the component devices into a list,
 * and bind them when instructed.  At the moment, we're specific to the DRM
 * subsystem, and only handles one master device, but this doesn't have to be
 * the case.
 */
#include <linux/component.h>
#include <linux/device.h>
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>

21 22
struct component;

23 24 25 26 27 28 29 30
struct component_match_array {
	void *data;
	int (*compare)(struct device *, void *);
	void (*release)(struct device *, void *);
	struct component *component;
	bool duplicate;
};

31 32 33
struct component_match {
	size_t alloc;
	size_t num;
34
	struct component_match_array *compare;
35 36
};

37 38 39 40 41 42
struct master {
	struct list_head node;
	bool bound;

	const struct component_master_ops *ops;
	struct device *dev;
43
	struct component_match *match;
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
};

struct component {
	struct list_head node;
	struct master *master;
	bool bound;

	const struct component_ops *ops;
	struct device *dev;
};

static DEFINE_MUTEX(component_mutex);
static LIST_HEAD(component_list);
static LIST_HEAD(masters);

static struct master *__master_find(struct device *dev,
	const struct component_master_ops *ops)
{
	struct master *m;

	list_for_each_entry(m, &masters, node)
		if (m->dev == dev && (!ops || m->ops == ops))
			return m;

	return NULL;
}

71
static struct component *find_component(struct master *master,
72 73 74 75 76
	int (*compare)(struct device *, void *), void *compare_data)
{
	struct component *c;

	list_for_each_entry(c, &component_list, node) {
77
		if (c->master && c->master != master)
78 79
			continue;

80 81
		if (compare(c->dev, compare_data))
			return c;
82 83
	}

84
	return NULL;
85 86
}

87 88 89 90 91 92 93 94 95 96 97
static int find_components(struct master *master)
{
	struct component_match *match = master->match;
	size_t i;
	int ret = 0;

	/*
	 * Scan the array of match functions and attach
	 * any components which are found to this master.
	 */
	for (i = 0; i < match->num; i++) {
98
		struct component_match_array *mc = &match->compare[i];
99 100 101 102 103 104 105
		struct component *c;

		dev_dbg(master->dev, "Looking for component %zu\n", i);

		if (match->compare[i].component)
			continue;

106
		c = find_component(master, mc->compare, mc->data);
107 108
		if (!c) {
			ret = -ENXIO;
109
			break;
110 111 112 113 114 115 116 117
		}

		dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);

		/* Attach this component to the master */
		match->compare[i].duplicate = !!c->master;
		match->compare[i].component = c;
		c->master = master;
118 119 120 121
	}
	return ret;
}

122 123
/* Detach component from associated master */
static void remove_component(struct master *master, struct component *c)
124
{
125
	size_t i;
126

127 128 129 130
	/* Detach the component from this master. */
	for (i = 0; i < master->match->num; i++)
		if (master->match->compare[i].component == c)
			master->match->compare[i].component = NULL;
131 132 133 134 135 136 137 138 139 140 141 142
}

/*
 * Try to bring up a master.  If component is NULL, we're interested in
 * this master, otherwise it's a component which must be present to try
 * and bring up the master.
 *
 * Returns 1 for successful bringup, 0 if not ready, or -ve errno.
 */
static int try_to_bring_up_master(struct master *master,
	struct component *component)
{
143 144
	int ret;

145 146
	dev_dbg(master->dev, "trying to bring up master\n");

147
	if (find_components(master)) {
148 149
		dev_dbg(master->dev, "master has incomplete components\n");
		return 0;
150
	}
151

152
	if (component && component->master != master) {
153 154 155
		dev_dbg(master->dev, "master is not for this component (%s)\n",
			dev_name(component->dev));
		return 0;
156
	}
157

158 159
	if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
		return -ENOMEM;
160

161 162 163 164 165
	/* Found all components */
	ret = master->ops->bind(master->dev);
	if (ret < 0) {
		devres_release_group(master->dev, NULL);
		dev_info(master->dev, "master bind failed: %d\n", ret);
166
		return ret;
167
	}
168

169 170
	master->bound = true;
	return 1;
171 172 173 174 175 176 177 178
}

static int try_to_bring_up_masters(struct component *component)
{
	struct master *m;
	int ret = 0;

	list_for_each_entry(m, &masters, node) {
179 180 181 182 183
		if (!m->bound) {
			ret = try_to_bring_up_master(m, component);
			if (ret != 0)
				break;
		}
184 185 186 187 188 189 190 191 192
	}

	return ret;
}

static void take_down_master(struct master *master)
{
	if (master->bound) {
		master->ops->unbind(master->dev);
193
		devres_release_group(master->dev, NULL);
194 195 196 197
		master->bound = false;
	}
}

198 199
static void component_match_release(struct device *master,
	struct component_match *match)
200
{
201 202 203 204 205 206 207 208
	unsigned int i;

	for (i = 0; i < match->num; i++) {
		struct component_match_array *mc = &match->compare[i];

		if (mc->release)
			mc->release(master, mc->data);
	}
209 210
}

211 212 213 214 215 216
static void devm_component_match_release(struct device *dev, void *res)
{
	component_match_release(dev, res);
}

static int component_match_realloc(struct device *dev,
217 218
	struct component_match *match, size_t num)
{
219
	struct component_match_array *new;
220

221 222
	if (match->alloc == num)
		return 0;
223

224
	new = devm_kmalloc_array(dev, num, sizeof(*new), GFP_KERNEL);
225
	if (!new)
226
		return -ENOMEM;
227

228 229 230 231
	if (match->compare) {
		memcpy(new, match->compare, sizeof(*new) *
					    min(match->num, num));
		devm_kfree(dev, match->compare);
232
	}
233 234
	match->compare = new;
	match->alloc = num;
235

236
	return 0;
237 238 239
}

/*
240
 * Add a component to be matched, with a release function.
241 242 243
 *
 * The match array is first created or extended if necessary.
 */
244 245 246
void component_match_add_release(struct device *master,
	struct component_match **matchptr,
	void (*release)(struct device *, void *),
247 248 249 250 251 252 253
	int (*compare)(struct device *, void *), void *compare_data)
{
	struct component_match *match = *matchptr;

	if (IS_ERR(match))
		return;

254 255 256 257 258 259 260
	if (!match) {
		match = devres_alloc(devm_component_match_release,
				     sizeof(*match), GFP_KERNEL);
		if (!match) {
			*matchptr = ERR_PTR(-ENOMEM);
			return;
		}
261

262
		devres_add(master, match);
263 264

		*matchptr = match;
265
	}
266

267 268 269 270 271 272 273
	if (match->num == match->alloc) {
		size_t new_size = match ? match->alloc + 16 : 15;
		int ret;

		ret = component_match_realloc(master, match, new_size);
		if (ret) {
			*matchptr = ERR_PTR(ret);
274
			return;
275
		}
276 277
	}

278 279
	match->compare[match->num].compare = compare;
	match->compare[match->num].release = release;
280
	match->compare[match->num].data = compare_data;
281
	match->compare[match->num].component = NULL;
282 283
	match->num++;
}
284
EXPORT_SYMBOL(component_match_add_release);
285 286 287 288

int component_master_add_with_match(struct device *dev,
	const struct component_master_ops *ops,
	struct component_match *match)
289 290 291 292
{
	struct master *master;
	int ret;

293
	/* Reallocate the match array for its true size */
294 295 296
	ret = component_match_realloc(dev, match, match->num);
	if (ret)
		return ret;
297

298 299 300 301 302 303
	master = kzalloc(sizeof(*master), GFP_KERNEL);
	if (!master)
		return -ENOMEM;

	master->dev = dev;
	master->ops = ops;
304
	master->match = match;
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

	/* Add to the list of available masters. */
	mutex_lock(&component_mutex);
	list_add(&master->node, &masters);

	ret = try_to_bring_up_master(master, NULL);

	if (ret < 0) {
		/* Delete off the list if we weren't successful */
		list_del(&master->node);
		kfree(master);
	}
	mutex_unlock(&component_mutex);

	return ret < 0 ? ret : 0;
}
321 322
EXPORT_SYMBOL_GPL(component_master_add_with_match);

323 324 325 326
void component_master_del(struct device *dev,
	const struct component_master_ops *ops)
{
	struct master *master;
327
	int i;
328 329 330 331

	mutex_lock(&component_mutex);
	master = __master_find(dev, ops);
	if (master) {
332 333
		struct component_match *match = master->match;

334 335 336
		take_down_master(master);

		list_del(&master->node);
337 338 339 340 341 342 343 344

		if (match) {
			for (i = 0; i < match->num; i++) {
				struct component *c = match->compare[i].component;
				if (c)
					c->master = NULL;
			}
		}
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
		kfree(master);
	}
	mutex_unlock(&component_mutex);
}
EXPORT_SYMBOL_GPL(component_master_del);

static void component_unbind(struct component *component,
	struct master *master, void *data)
{
	WARN_ON(!component->bound);

	component->ops->unbind(component->dev, master->dev, data);
	component->bound = false;

	/* Release all resources claimed in the binding of this component */
	devres_release_group(component->dev, component);
}

void component_unbind_all(struct device *master_dev, void *data)
{
	struct master *master;
	struct component *c;
367
	size_t i;
368 369 370 371 372 373 374

	WARN_ON(!mutex_is_locked(&component_mutex));

	master = __master_find(master_dev, NULL);
	if (!master)
		return;

375 376 377 378 379 380
	/* Unbind components in reverse order */
	for (i = master->match->num; i--; )
		if (!master->match->compare[i].duplicate) {
			c = master->match->compare[i].component;
			component_unbind(c, master, data);
		}
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
}
EXPORT_SYMBOL_GPL(component_unbind_all);

static int component_bind(struct component *component, struct master *master,
	void *data)
{
	int ret;

	/*
	 * Each component initialises inside its own devres group.
	 * This allows us to roll-back a failed component without
	 * affecting anything else.
	 */
	if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
		return -ENOMEM;

	/*
	 * Also open a group for the device itself: this allows us
	 * to release the resources claimed against the sub-device
	 * at the appropriate moment.
	 */
	if (!devres_open_group(component->dev, component, GFP_KERNEL)) {
		devres_release_group(master->dev, NULL);
		return -ENOMEM;
	}

	dev_dbg(master->dev, "binding %s (ops %ps)\n",
		dev_name(component->dev), component->ops);

	ret = component->ops->bind(component->dev, master->dev, data);
	if (!ret) {
		component->bound = true;

		/*
		 * Close the component device's group so that resources
		 * allocated in the binding are encapsulated for removal
		 * at unbind.  Remove the group on the DRM device as we
		 * can clean those resources up independently.
		 */
		devres_close_group(component->dev, NULL);
		devres_remove_group(master->dev, NULL);

		dev_info(master->dev, "bound %s (ops %ps)\n",
			 dev_name(component->dev), component->ops);
	} else {
		devres_release_group(component->dev, NULL);
		devres_release_group(master->dev, NULL);

		dev_err(master->dev, "failed to bind %s (ops %ps): %d\n",
			dev_name(component->dev), component->ops, ret);
	}

	return ret;
}

int component_bind_all(struct device *master_dev, void *data)
{
	struct master *master;
	struct component *c;
440
	size_t i;
441 442 443 444 445 446 447 448
	int ret = 0;

	WARN_ON(!mutex_is_locked(&component_mutex));

	master = __master_find(master_dev, NULL);
	if (!master)
		return -EINVAL;

449 450 451 452 453 454 455 456
	/* Bind components in match order */
	for (i = 0; i < master->match->num; i++)
		if (!master->match->compare[i].duplicate) {
			c = master->match->compare[i].component;
			ret = component_bind(c, master, data);
			if (ret)
				break;
		}
457 458

	if (ret != 0) {
459 460 461 462 463
		for (; i--; )
			if (!master->match->compare[i].duplicate) {
				c = master->match->compare[i].component;
				component_unbind(c, master, data);
			}
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	}

	return ret;
}
EXPORT_SYMBOL_GPL(component_bind_all);

int component_add(struct device *dev, const struct component_ops *ops)
{
	struct component *component;
	int ret;

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

	component->ops = ops;
	component->dev = dev;

	dev_dbg(dev, "adding component (ops %ps)\n", ops);

	mutex_lock(&component_mutex);
	list_add_tail(&component->node, &component_list);

	ret = try_to_bring_up_masters(component);
	if (ret < 0) {
		list_del(&component->node);

		kfree(component);
	}
	mutex_unlock(&component_mutex);

	return ret < 0 ? ret : 0;
}
EXPORT_SYMBOL_GPL(component_add);

void component_del(struct device *dev, const struct component_ops *ops)
{
	struct component *c, *component = NULL;

	mutex_lock(&component_mutex);
	list_for_each_entry(c, &component_list, node)
		if (c->dev == dev && c->ops == ops) {
			list_del(&c->node);
			component = c;
			break;
		}

511
	if (component && component->master) {
512
		take_down_master(component->master);
513 514
		remove_component(component->master, component);
	}
515 516 517 518 519 520 521 522 523

	mutex_unlock(&component_mutex);

	WARN_ON(!component);
	kfree(component);
}
EXPORT_SYMBOL_GPL(component_del);

MODULE_LICENSE("GPL v2");