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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
|
/*
* Greybus "Core"
*
* Copyright 2014 Google Inc.
*
* 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"
/* Allow greybus to be disabled at boot if needed */
static bool nogreybus;
#ifdef MODULE
module_param(nogreybus, bool, 0444);
#else
core_param(nogreybus, bool, 0444);
#endif
int greybus_disabled(void)
{
return nogreybus;
}
EXPORT_SYMBOL_GPL(greybus_disabled);
static int greybus_match_one_id(struct greybus_module *gmod,
const struct greybus_module_id *id)
{
struct greybus_descriptor_module *module;
module = &gmod->module;
if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_VENDOR) &&
(id->vendor != le16_to_cpu(module->vendor)))
return 0;
if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_PRODUCT) &&
(id->product != le16_to_cpu(module->product)))
return 0;
if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_SERIAL) &&
(id->serial_number != le64_to_cpu(module->serial_number)))
return 0;
return 1;
}
static const struct greybus_module_id *greybus_match_id(
struct greybus_module *gmod,
const struct greybus_module_id *id)
{
if (id == NULL)
return NULL;
for (; id->vendor || id->product || id->serial_number ||
id->driver_info ; id++) {
if (greybus_match_one_id(gmod, id))
return id;
}
return NULL;
}
static int greybus_module_match(struct device *dev, struct device_driver *drv)
{
struct greybus_driver *driver = to_greybus_driver(dev->driver);
struct greybus_module *gmod = to_greybus_module(dev);
const struct greybus_module_id *id;
id = greybus_match_id(gmod, driver->id_table);
if (id)
return 1;
/* FIXME - Dyanmic ids? */
return 0;
}
static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
{
/* struct greybus_module *gmod = to_greybus_module(dev); */
/* FIXME - add some uevents here... */
return 0;
}
static struct bus_type greybus_bus_type = {
.name = "greybus",
.match = greybus_module_match,
.uevent = greybus_uevent,
};
static int greybus_probe(struct device *dev)
{
struct greybus_driver *driver = to_greybus_driver(dev->driver);
struct greybus_module *gmod = to_greybus_module(dev);
const struct greybus_module_id *id;
int retval;
/* match id */
id = greybus_match_id(gmod, driver->id_table);
if (!id)
return -ENODEV;
retval = driver->probe(gmod, id);
if (retval)
return retval;
return 0;
}
static int greybus_remove(struct device *dev)
{
struct greybus_driver *driver = to_greybus_driver(dev->driver);
struct greybus_module *gmod = to_greybus_module(dev);
driver->disconnect(gmod);
return 0;
}
int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
const char *mod_name)
{
int retval;
if (greybus_disabled())
return -ENODEV;
driver->driver.name = driver->name;
driver->driver.probe = greybus_probe;
driver->driver.remove = greybus_remove;
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(greybus_register_driver);
void greybus_deregister(struct greybus_driver *driver)
{
driver_unregister(&driver->driver);
}
EXPORT_SYMBOL_GPL(greybus_deregister);
static void greybus_module_release(struct device *dev)
{
struct greybus_module *gmod = to_greybus_module(dev);
int i;
for (i = 0; i < gmod->num_strings; ++i)
kfree(gmod->string[i]);
kfree(gmod);
}
const u8 *greybus_string(struct greybus_module *gmod, int id)
{
int i;
struct gmod_string *string;
if (!gmod)
return NULL;
for (i = 0; i < gmod->num_strings; ++i) {
string = gmod->string[i];
if (string->id == id)
return &string->string[0];
}
return NULL;
}
static struct device_type greybus_module_type = {
.name = "greybus_module",
.release = greybus_module_release,
};
static int gb_init_subdevs(struct greybus_module *gmod,
const struct greybus_module_id *id)
{
int retval;
/* Allocate all of the different "sub device types" for this device */
retval = gb_i2c_probe(gmod, id);
if (retval)
goto error_i2c;
retval = gb_gpio_probe(gmod, id);
if (retval)
goto error_gpio;
retval = gb_sdio_probe(gmod, id);
if (retval)
goto error_sdio;
retval = gb_tty_probe(gmod, id);
if (retval)
goto error_tty;
retval = gb_battery_probe(gmod, id);
if (retval)
goto error_battery;
return 0;
error_battery:
gb_tty_disconnect(gmod);
error_tty:
gb_sdio_disconnect(gmod);
error_sdio:
gb_gpio_disconnect(gmod);
error_gpio:
gb_i2c_disconnect(gmod);
error_i2c:
return retval;
}
static const struct greybus_module_id fake_gb_id = {
GREYBUS_DEVICE(0x42, 0x42)
};
static int create_function(struct greybus_module *gmod,
struct greybus_descriptor_function *function,
size_t desc_size)
{
if (desc_size != sizeof(*function)) {
dev_err(gmod->dev.parent, "invalid function header size %zu\n",
desc_size);
return -EINVAL;
}
memcpy(&gmod->function, function, desc_size);
return 0;
}
static int create_module(struct greybus_module *gmod,
struct greybus_descriptor_module *module,
size_t desc_size)
{
if (desc_size != sizeof(*module)) {
dev_err(gmod->dev.parent, "invalid module header size %zu\n",
desc_size);
return -EINVAL;
}
memcpy(&gmod->module, module, desc_size);
return 0;
}
static int create_string(struct greybus_module *gmod,
struct greybus_descriptor_string *string,
size_t desc_size)
{
int string_size;
struct gmod_string *gmod_string;
if (gmod->num_strings == MAX_STRINGS_PER_MODULE) {
dev_err(gmod->dev.parent,
"too many strings for this module!\n");
return -EINVAL;
}
if (desc_size < sizeof(*string)) {
dev_err(gmod->dev.parent, "invalid string header size %zu\n",
desc_size);
return -EINVAL;
}
string_size = string->length;
gmod_string = kzalloc(sizeof(*gmod_string) + string_size + 1, GFP_KERNEL);
if (!gmod_string)
return -ENOMEM;
gmod_string->length = string_size;
gmod_string->id = string->id;
memcpy(&gmod_string->string, &string->string, string_size);
gmod->string[gmod->num_strings] = gmod_string;
gmod->num_strings++;
return 0;
}
static int create_cport(struct greybus_module *gmod,
struct greybus_descriptor_cport *cport,
size_t desc_size)
{
if (gmod->num_cports == MAX_CPORTS_PER_MODULE) {
dev_err(gmod->dev.parent, "too many cports for this module!\n");
return -EINVAL;
}
if (desc_size != sizeof(*cport)) {
dev_err(gmod->dev.parent,
"invalid serial number header size %zu\n", desc_size);
return -EINVAL;
}
gmod->cport_ids[gmod->num_cports] = le16_to_cpu(cport->id);
gmod->num_cports++;
return 0;
}
/**
* gb_add_module
*
* Pass in a buffer that _should_ contain a Greybus module manifest
* and register a greybus device structure with the kernel core.
*/
void gb_add_module(struct greybus_host_device *hd, u8 module_id,
u8 *data, int size)
{
struct greybus_module *gmod;
struct greybus_manifest *manifest;
int retval;
int overall_size;
/* we have to have at _least_ the manifest header */
if (size <= sizeof(manifest->header))
return;
gmod = kzalloc(sizeof(*gmod), GFP_KERNEL);
if (!gmod)
return;
gmod->module_number = module_id;
gmod->dev.parent = hd->parent;
gmod->dev.driver = NULL;
gmod->dev.bus = &greybus_bus_type;
gmod->dev.type = &greybus_module_type;
gmod->dev.groups = greybus_module_groups;
gmod->dev.dma_mask = hd->parent->dma_mask;
device_initialize(&gmod->dev);
dev_set_name(&gmod->dev, "%d", module_id);
manifest = (struct greybus_manifest *)data;
overall_size = le16_to_cpu(manifest->header.size);
if (overall_size != size) {
dev_err(hd->parent, "size != manifest header size, %d != %d\n",
size, overall_size);
goto error;
}
/* Validate major/minor number */
if (manifest->header.version_major > GREYBUS_VERSION_MAJOR) {
dev_err(hd->parent,
"Manifest version too new (%hhu.%hhu > %hhu.%hhu)\n",
manifest->header.version_major,
manifest->header.version_minor,
GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
goto error;
}
size -= sizeof(manifest->header);
data += sizeof(manifest->header);
while (size > 0) {
struct greybus_descriptor *desc;
u16 desc_size;
size_t data_size;
if (size < sizeof(desc->header)) {
dev_err(hd->parent, "remaining size %d too small\n",
size);
goto error;
}
desc = (struct greybus_descriptor *)data;
desc_size = le16_to_cpu(desc->header.size);
if (size < desc_size) {
dev_err(hd->parent, "descriptor size %d too big\n",
desc_size);
goto error;
}
data_size = (size_t)desc_size - sizeof(desc->header);
switch (le16_to_cpu(desc->header.type)) {
case GREYBUS_TYPE_FUNCTION:
retval = create_function(gmod, &desc->function,
data_size);
break;
case GREYBUS_TYPE_MODULE:
retval = create_module(gmod, &desc->module,
data_size);
break;
case GREYBUS_TYPE_STRING:
retval = create_string(gmod, &desc->string, data_size);
break;
case GREYBUS_TYPE_CPORT:
retval = create_cport(gmod, &desc->cport, data_size);
break;
case GREYBUS_TYPE_INVALID:
default:
dev_err(hd->parent, "invalid descriptor type %d\n",
desc->header.type);
goto error;
}
if (retval)
goto error;
size -= desc_size;
data += desc_size;
}
retval = gb_init_subdevs(gmod, &fake_gb_id);
if (retval)
goto error;
// FIXME device_add(&gmod->dev);
//return gmod;
return;
error:
put_device(&gmod->dev);
greybus_module_release(&gmod->dev);
}
void gb_remove_module(struct greybus_host_device *hd, u8 module_id)
{
// FIXME should be the remove_device call...
}
void greybus_remove_device(struct greybus_module *gmod)
{
/* tear down all of the "sub device types" for this device */
gb_i2c_disconnect(gmod);
gb_gpio_disconnect(gmod);
gb_sdio_disconnect(gmod);
gb_tty_disconnect(gmod);
gb_battery_disconnect(gmod);
// FIXME - device_remove(&gmod->dev);
}
static DEFINE_MUTEX(hd_mutex);
static void free_hd(struct kref *kref)
{
struct greybus_host_device *hd;
hd = container_of(kref, struct greybus_host_device, kref);
kfree(hd);
}
struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *driver,
struct device *parent)
{
struct greybus_host_device *hd;
hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
if (!hd)
return NULL;
kref_init(&hd->kref);
hd->parent = parent;
hd->driver = driver;
return hd;
}
EXPORT_SYMBOL_GPL(greybus_create_hd);
void greybus_remove_hd(struct greybus_host_device *hd)
{
kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
}
EXPORT_SYMBOL_GPL(greybus_remove_hd);
static int __init gb_init(void)
{
int retval;
retval = gb_debugfs_init();
if (retval) {
pr_err("debugfs failed\n");
return retval;
}
retval = bus_register(&greybus_bus_type);
if (retval) {
pr_err("bus_register failed\n");
goto error_bus;
}
retval = gb_ap_init();
if (retval) {
pr_err("gb_ap_init failed\n");
goto error_ap;
}
retval = gb_gbuf_init();
if (retval) {
pr_err("gb_gbuf_init failed\n");
goto error_gbuf;
}
retval = gb_tty_init();
if (retval) {
pr_err("gb_tty_init failed\n");
goto error_tty;
}
return 0;
error_tty:
gb_gbuf_exit();
error_gbuf:
gb_ap_exit();
error_ap:
bus_unregister(&greybus_bus_type);
error_bus:
gb_debugfs_cleanup();
return retval;
}
static void __exit gb_exit(void)
{
gb_tty_exit();
gb_gbuf_exit();
gb_ap_exit();
bus_unregister(&greybus_bus_type);
gb_debugfs_cleanup();
}
module_init(gb_init);
module_exit(gb_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
|