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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
/*
* Greybus GP Bridge driver
*
* Copyright 2014 Google Inc.
* Copyright 2014 Linaro Ltd.
*
* Released under the GPLv2 only.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/types.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/device.h>
#include "greybus.h"
#include "gpbridge.h"
struct gpbridge_host {
struct gb_bundle *bundle;
struct list_head devices;
};
static DEFINE_IDA(gpbridge_id);
static ssize_t protocol_id_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gpbridge_device *gpbdev = to_gpbridge_dev(dev);
return sprintf(buf, "0x%02x\n", gpbdev->cport_desc->protocol_id);
}
static DEVICE_ATTR_RO(protocol_id);
static struct attribute *gpbdev_attrs[] = {
&dev_attr_protocol_id.attr,
NULL,
};
ATTRIBUTE_GROUPS(gpbdev);
static void gpbdev_release(struct device *dev)
{
struct gpbridge_device *gpbdev = to_gpbridge_dev(dev);
ida_simple_remove(&gpbridge_id, gpbdev->id);
kfree(gpbdev);
}
struct device_type greybus_gpbdev_type = {
.name = "gpbridge_device",
.release = gpbdev_release,
};
static int gpbdev_uevent(struct device *dev, struct kobj_uevent_env *env)
{
struct gpbridge_device *gpbdev = to_gpbridge_dev(dev);
struct greybus_descriptor_cport *cport_desc = gpbdev->cport_desc;
struct gb_bundle *bundle = gpbdev->bundle;
struct gb_interface *intf = bundle->intf;
struct gb_module *module = intf->module;
struct gb_host_device *hd = intf->hd;
if (add_uevent_var(env, "BUS=%u", hd->bus_id))
return -ENOMEM;
if (add_uevent_var(env, "MODULE=%u", module->module_id))
return -ENOMEM;
if (add_uevent_var(env, "INTERFACE=%u", intf->interface_id))
return -ENOMEM;
if (add_uevent_var(env, "GREYBUS_ID=%08x/%08x",
intf->vendor_id, intf->product_id))
return -ENOMEM;
if (add_uevent_var(env, "BUNDLE=%u", gpbdev->bundle->id))
return -ENOMEM;
if (add_uevent_var(env, "BUNDLE_CLASS=%02x", bundle->class))
return -ENOMEM;
if (add_uevent_var(env, "GPBDEV_ID=%u", gpbdev->id))
return -ENOMEM;
if (add_uevent_var(env, "PROTOCOL_ID=%02x", cport_desc->protocol_id))
return -ENOMEM;
return 0;
}
static const struct gpbridge_device_id *
gpbdev_match_id(struct gpbridge_device *gpbdev, struct gpbridge_driver *gpbdrv)
{
const struct gpbridge_device_id *id = gpbdrv->id_table;
if (!id)
return NULL;
for (; id->protocol_id; id++)
if (id->protocol_id == gpbdev->cport_desc->protocol_id)
return id;
return NULL;
}
static int gpbdev_match(struct device *dev, struct device_driver *drv)
{
struct gpbridge_driver *gpbdrv = to_gpbridge_driver(drv);
struct gpbridge_device *gpbdev = to_gpbridge_dev(dev);
const struct gpbridge_device_id *id;
id = gpbdev_match_id(gpbdev, gpbdrv);
if (id)
return 1;
return 0;
}
static int gpbdev_probe(struct device *dev)
{
struct gpbridge_driver *gpbdrv = to_gpbridge_driver(dev->driver);
struct gpbridge_device *gpbdev = to_gpbridge_dev(dev);
const struct gpbridge_device_id *id;
id = gpbdev_match_id(gpbdev, gpbdrv);
if (!id)
return -ENODEV;
return gpbdrv->probe(gpbdev, id);
}
static int gpbdev_remove(struct device *dev)
{
struct gpbridge_driver *gpbdrv = to_gpbridge_driver(dev->driver);
struct gpbridge_device *gpbdev = to_gpbridge_dev(dev);
gpbdrv->remove(gpbdev);
return 0;
}
static struct bus_type gpbridge_bus_type = {
.name = "gpbridge",
.match = gpbdev_match,
.probe = gpbdev_probe,
.remove = gpbdev_remove,
.uevent = gpbdev_uevent,
};
int gb_gpbridge_register_driver(struct gpbridge_driver *driver,
struct module *owner, const char *mod_name)
{
int retval;
if (greybus_disabled())
return -ENODEV;
driver->driver.bus = &gpbridge_bus_type;
driver->driver.name = driver->name;
driver->driver.owner = owner;
driver->driver.mod_name = mod_name;
retval = driver_register(&driver->driver);
if (retval)
return retval;
pr_info("registered new driver %s\n", driver->name);
return 0;
}
EXPORT_SYMBOL_GPL(gb_gpbridge_register_driver);
void gb_gpbridge_deregister_driver(struct gpbridge_driver *driver)
{
driver_unregister(&driver->driver);
}
EXPORT_SYMBOL_GPL(gb_gpbridge_deregister_driver);
int gb_gpbridge_get_version(struct gb_connection *connection)
{
struct gb_protocol_version_request request;
struct gb_protocol_version_response response;
int retval;
request.major = 1;
request.minor = 0;
retval = gb_operation_sync(connection, GB_REQUEST_TYPE_PROTOCOL_VERSION,
&request, sizeof(request), &response,
sizeof(response));
if (retval)
return retval;
/* FIXME - do proper version negotiation here someday... */
connection->module_major = response.major;
connection->module_minor = response.minor;
dev_dbg(&connection->hd->dev, "%s: v%u.%u\n", connection->name,
response.major, response.minor);
return 0;
}
EXPORT_SYMBOL_GPL(gb_gpbridge_get_version);
static struct gpbridge_device *gb_gpbridge_create_dev(struct gb_bundle *bundle,
struct greybus_descriptor_cport *cport_desc)
{
struct gpbridge_device *gpbdev;
int retval;
int id;
id = ida_simple_get(&gpbridge_id, 1, 0, GFP_KERNEL);
if (id < 0)
return ERR_PTR(id);
gpbdev = kzalloc(sizeof(*gpbdev), GFP_KERNEL);
if (!gpbdev) {
ida_simple_remove(&gpbridge_id, id);
return ERR_PTR(-ENOMEM);
}
gpbdev->id = id;
gpbdev->bundle = bundle;
gpbdev->cport_desc = cport_desc;
gpbdev->dev.parent = &bundle->dev;
gpbdev->dev.bus = &gpbridge_bus_type;
gpbdev->dev.type = &greybus_gpbdev_type;
gpbdev->dev.groups = gpbdev_groups;
gpbdev->dev.dma_mask = bundle->dev.dma_mask;
dev_set_name(&gpbdev->dev, "gpb%d", id);
retval = device_register(&gpbdev->dev);
if (retval) {
put_device(&gpbdev->dev);
return ERR_PTR(retval);
}
return gpbdev;
}
static void gb_gpbridge_disconnect(struct gb_bundle *bundle)
{
struct gpbridge_host *gpb_host = greybus_get_drvdata(bundle);
struct gpbridge_device *gpbdev, *temp;
list_for_each_entry_safe(gpbdev, temp, &gpb_host->devices, list) {
list_del(&gpbdev->list);
device_unregister(&gpbdev->dev);
}
kfree(gpb_host);
}
static int gb_gpbridge_probe(struct gb_bundle *bundle,
const struct greybus_bundle_id *id)
{
struct gpbridge_host *gpb_host;
struct gpbridge_device *gpbdev;
int i;
if (bundle->num_cports == 0)
return -ENODEV;
gpb_host
|