summaryrefslogtreecommitdiffstats
path: root/drivers/staging/greybus/protocol.c
blob: 057ab6057ee6248e4c04f933defd8889bf35ede0 (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
/*
 * Greybus protocol handling
 *
 * Copyright 2014-2015 Google Inc.
 * Copyright 2014-2015 Linaro Ltd.
 *
 * Released under the GPLv2 only.
 */

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include "greybus.h"

/* Global list of registered protocols */
static DEFINE_SPINLOCK(gb_protocols_lock);
static LIST_HEAD(gb_protocols);

/* Caller must hold gb_protocols_lock */
static struct gb_protocol *gb_protocol_find(u8 id, u8 major, u8 minor)
{
	struct gb_protocol *protocol;

	list_for_each_entry(protocol, &gb_protocols, links) {
		if (protocol->id < id)
			continue;
		if (protocol->id > id)
			break;

		if (protocol->major > major)
			continue;
		if (protocol->major < major)
			break;

		if (protocol->minor > minor)
			continue;
		if (protocol->minor < minor)
			break;

		return protocol;
	}
	return NULL;
}

int __gb_protocol_register(struct gb_protocol *protocol, struct module *module)
{
	struct gb_protocol *existing;
	u8 id = protocol->id;
	u8 major = protocol->major;
	u8 minor = protocol->minor;

	protocol->owner = module;

	/*
	 * The protocols list is sorted first by protocol id (low to
	 * high), then by major version (high to low), and finally
	 * by minor version (high to low).  Searching only by
	 * protocol id will produce the newest implemented version
	 * of the protocol.
	 */
	spin_lock_irq(&gb_protocols_lock);

	list_for_each_entry(existing, &gb_protocols, links) {
		if (existing->id < id)
			continue;
		if (existing->id > id)
			break;

		if (existing->major > major)
			continue;
		if (existing->major < major)
			break;

		if (existing->minor > minor)
			continue;
		if (existing->minor < minor)
			break;

		/* A matching protocol has already been registered */
		spin_unlock_irq(&gb_protocols_lock);

		return -EEXIST;
	}

	/*
	 * We need to insert the protocol here, before the existing one
	 * (or before the head if we searched the whole list)
	 */
	list_add_tail(&protocol->links, &existing->links);
	spin_unlock_irq(&gb_protocols_lock);

	pr_info("Registered %s protocol.\n", protocol->name);

	return 0;
}
EXPORT_SYMBOL_GPL(__gb_protocol_register);

/*
 * De-register a previously registered protocol.
 */
void gb_protocol_deregister(struct gb_protocol *protocol)
{
	if (!protocol)
		return;

	spin_lock_irq(&gb_protocols_lock);
	protocol = gb_protocol_find(protocol->id, protocol->major,
				    protocol->minor);
	if (WARN_ON(!protocol || protocol->count)) {
		spin_unlock_irq(&gb_protocols_lock);
		return;
	}

	list_del(&protocol->links);
	spin_unlock_irq(&gb_protocols_lock);

	pr_info("Deregistered %s protocol.\n", protocol->name);
}
EXPORT_SYMBOL_GPL(gb_protocol_deregister);

/* Returns the requested protocol if available, or a null pointer */
struct gb_protocol *gb_protocol_get(u8 id, u8 major, u8 minor)
{
	struct gb_protocol *protocol;
	u8 protocol_count;

	spin_lock_irq(&gb_protocols_lock);
	protocol = gb_protocol_find(id, major, minor);
	if (protocol) {
		if (!try_module_get(protocol->owner)) {
			protocol = NULL;
		} else {
			protocol_count = protocol->count;
			if (protocol_count != U8_MAX)
				protocol->count++;
		}
	}
	spin_unlock_irq(&gb_protocols_lock);

	if (protocol)
		WARN_ON(protocol_count == U8_MAX);

	return protocol;
}
EXPORT_SYMBOL_GPL(gb_protocol_get);

int gb_protocol_get_version(struct gb_connection *connection)
{
	struct gb_protocol_version_request request;
	struct gb_protocol_version_response response;
	struct gb_protocol *protocol = connection->protocol;
	int retval;

	request.major = protocol->major;
	request.minor = protocol->minor;

	retval = gb_operation_sync(connection, GB_REQUEST_TYPE_PROTOCOL_VERSION,
				   &request, sizeof(request), &response,
				   sizeof(response));
	if (retval)
		return retval;

	if (response.major > connection->protocol->major) {
		dev_err(&connection->hd->dev,
			"%s: unsupported major version (%u > %u)\n",
			connection->name, response.major,
			connection->protocol->major);
		return -ENOTSUPP;
	}

	connection->module_major = response.major;
	connection->module_minor = response.minor;

	dev_dbg(&connection->hd->dev,
		"%s: %s (0x%02x) v%u.%u\n", connection->name,
		protocol->name, protocol->id, response.major, response.minor);

	return 0;
}
EXPORT_SYMBOL_GPL(gb_protocol_get_version);

void gb_protocol_put(struct gb_protocol *protocol)
{
	u8 id;
	u8 major;
	u8 minor;

	id = protocol->id;
	major = protocol->major;
	minor = protocol->minor;

	spin_lock_irq(&gb_protocols_lock);
	protocol = gb_protocol_find(id, major, minor);
	if (WARN_ON(!protocol || !protocol->count))
		goto out;

	protocol->count--;
	module_put(protocol->owner);
out:
	spin_unlock_irq(&gb_protocols_lock);
}
EXPORT_SYMBOL_GPL(gb_protocol_put);