summaryrefslogtreecommitdiff
path: root/drivers/base/bus.c
blob: a5c9c930da8339d3961d68762cd93148a2b3cbeb (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
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
 */

#include <common.h>
#include <driver.h>
#include <errno.h>
#include <of.h>

LIST_HEAD(bus_list);
EXPORT_SYMBOL(bus_list);

static struct bus_type *get_bus_by_name(const char *name)
{
	struct bus_type *bus;

	for_each_bus(bus) {
		if(!strcmp(bus->name, name))
			return bus;
	}

	return NULL;
}

int bus_register(struct bus_type *bus)
{
	int ret;

	if (get_bus_by_name(bus->name))
		return -EEXIST;

	bus->dev = xzalloc(sizeof(*bus->dev));
	dev_set_name(bus->dev, bus->name);
	bus->dev->id = DEVICE_ID_SINGLE;

	ret = register_device(bus->dev);
	if (ret)
		return ret;


	INIT_LIST_HEAD(&bus->device_list);
	INIT_LIST_HEAD(&bus->driver_list);

	list_add_tail(&bus->list, &bus_list);

	return 0;
}

int device_match(struct device *dev, struct driver *drv)
{
	if (IS_ENABLED(CONFIG_OFDEVICE) && dev->of_node &&
	    drv->of_compatible)
		return of_match(dev, drv);

	if (drv->id_table) {
		const struct platform_device_id *id = drv->id_table;

		while (id->name) {
			if (!strcmp(id->name, dev->name)) {
				dev->id_entry = id;
				return 0;
			}
			id++;
		}
	} else if (!strcmp(dev->name, drv->name)) {
		return 0;
	}

	return -1;
}

int device_match_of_modalias(struct device *dev, struct driver *drv)
{
	const struct platform_device_id *id = drv->id_table;
	const char *of_modalias = NULL, *p;
	const struct property *prop;
	const char *compat;

	if (!device_match(dev, drv))
		return 0;

	if (!id || !IS_ENABLED(CONFIG_OFDEVICE) || !dev->of_node)
		return -1;

	of_property_for_each_string(dev->of_node, "compatible", prop, compat) {
		p = strchr(compat, ',');
		of_modalias = p ? p + 1 : compat;

		for (id = drv->id_table; id->name; id++) {
			if (!strcmp(id->name, dev->name) || !strcmp(id->name, of_modalias)) {
				dev->id_entry = id;
				return 0;
			}
		}
	}

	return -1;
}