4 * Copyright 2014 Google Inc.
6 * Released under the GPLv2 only.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/types.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
20 /* Allow greybus to be disabled at boot if needed */
21 static bool nogreybus;
23 module_param(nogreybus, bool, 0444);
25 core_param(nogreybus, bool, 0444);
27 int greybus_disabled(void)
31 EXPORT_SYMBOL_GPL(greybus_disabled);
33 static int greybus_match_one_id(struct greybus_module *gmod,
34 const struct greybus_module_id *id)
36 struct greybus_descriptor_module *module;
38 module = &gmod->module;
40 if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_VENDOR) &&
41 (id->vendor != le16_to_cpu(module->vendor)))
44 if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_PRODUCT) &&
45 (id->product != le16_to_cpu(module->product)))
48 if ((id->match_flags & GREYBUS_DEVICE_ID_MATCH_SERIAL) &&
49 (id->serial_number != le64_to_cpu(module->serial_number)))
55 static const struct greybus_module_id *greybus_match_id(
56 struct greybus_module *gmod,
57 const struct greybus_module_id *id)
62 for (; id->vendor || id->product || id->serial_number ||
63 id->driver_info ; id++) {
64 if (greybus_match_one_id(gmod, id))
71 static int greybus_module_match(struct device *dev, struct device_driver *drv)
73 struct greybus_driver *driver = to_greybus_driver(dev->driver);
74 struct greybus_module *gmod = to_greybus_module(dev);
75 const struct greybus_module_id *id;
77 id = greybus_match_id(gmod, driver->id_table);
80 /* FIXME - Dyanmic ids? */
84 static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
86 /* struct greybus_module *gmod = to_greybus_module(dev); */
88 /* FIXME - add some uevents here... */
92 static struct bus_type greybus_bus_type = {
94 .match = greybus_module_match,
95 .uevent = greybus_uevent,
98 static int greybus_probe(struct device *dev)
100 struct greybus_driver *driver = to_greybus_driver(dev->driver);
101 struct greybus_module *gmod = to_greybus_module(dev);
102 const struct greybus_module_id *id;
106 id = greybus_match_id(gmod, driver->id_table);
110 retval = driver->probe(gmod, id);
117 static int greybus_remove(struct device *dev)
119 struct greybus_driver *driver = to_greybus_driver(dev->driver);
120 struct greybus_module *gmod = to_greybus_module(dev);
122 driver->disconnect(gmod);
126 int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
127 const char *mod_name)
131 if (greybus_disabled())
134 driver->driver.name = driver->name;
135 driver->driver.probe = greybus_probe;
136 driver->driver.remove = greybus_remove;
137 driver->driver.owner = owner;
138 driver->driver.mod_name = mod_name;
140 retval = driver_register(&driver->driver);
144 pr_info("registered new driver %s\n", driver->name);
147 EXPORT_SYMBOL_GPL(greybus_register_driver);
149 void greybus_deregister(struct greybus_driver *driver)
151 driver_unregister(&driver->driver);
153 EXPORT_SYMBOL_GPL(greybus_deregister);
156 static void greybus_module_release(struct device *dev)
158 struct greybus_module *gmod = to_greybus_module(dev);
161 for (i = 0; i < gmod->num_strings; ++i)
162 kfree(gmod->string[i]);
167 const u8 *greybus_string(struct greybus_module *gmod, int id)
170 struct gmod_string *string;
175 for (i = 0; i < gmod->num_strings; ++i) {
176 string = gmod->string[i];
177 if (string->id == id)
178 return &string->string[0];
183 static struct device_type greybus_module_type = {
184 .name = "greybus_module",
185 .release = greybus_module_release,
188 static int gb_init_subdevs(struct greybus_module *gmod,
189 const struct greybus_module_id *id)
193 /* Allocate all of the different "sub device types" for this device */
194 retval = gb_i2c_probe(gmod, id);
198 retval = gb_gpio_probe(gmod, id);
202 retval = gb_sdio_probe(gmod, id);
206 retval = gb_tty_probe(gmod, id);
210 retval = gb_battery_probe(gmod, id);
216 gb_tty_disconnect(gmod);
219 gb_sdio_disconnect(gmod);
222 gb_gpio_disconnect(gmod);
225 gb_i2c_disconnect(gmod);
231 static const struct greybus_module_id fake_gb_id = {
232 GREYBUS_DEVICE(0x42, 0x42)
235 static int create_function(struct greybus_module *gmod,
236 struct greybus_descriptor_function *function,
239 if (desc_size != sizeof(*function)) {
240 dev_err(gmod->dev.parent, "invalid function header size %zu\n",
244 memcpy(&gmod->function, function, desc_size);
248 static int create_module(struct greybus_module *gmod,
249 struct greybus_descriptor_module *module,
252 if (desc_size != sizeof(*module)) {
253 dev_err(gmod->dev.parent, "invalid module header size %zu\n",
257 memcpy(&gmod->module, module, desc_size);
261 static int create_string(struct greybus_module *gmod,
262 struct greybus_descriptor_string *string,
266 struct gmod_string *gmod_string;
268 if (gmod->num_strings == MAX_STRINGS_PER_MODULE) {
269 dev_err(gmod->dev.parent,
270 "too many strings for this module!\n");
274 if (desc_size < sizeof(*string)) {
275 dev_err(gmod->dev.parent, "invalid string header size %zu\n",
280 string_size = string->length;
281 gmod_string = kzalloc(sizeof(*gmod_string) + string_size + 1, GFP_KERNEL);
285 gmod_string->length = string_size;
286 gmod_string->id = string->id;
287 memcpy(&gmod_string->string, &string->string, string_size);
289 gmod->string[gmod->num_strings] = gmod_string;
295 static int create_cport(struct greybus_module *gmod,
296 struct greybus_descriptor_cport *cport,
299 if (gmod->num_cports == MAX_CPORTS_PER_MODULE) {
300 dev_err(gmod->dev.parent, "too many cports for this module!\n");
304 if (desc_size != sizeof(*cport)) {
305 dev_err(gmod->dev.parent,
306 "invalid cport descriptor size %zu\n", desc_size);
310 gmod->cport_ids[gmod->num_cports] = le16_to_cpu(cport->id);
319 * Pass in a buffer that _should_ contain a Greybus module manifest
320 * and register a greybus device structure with the kernel core.
322 void gb_add_module(struct greybus_host_device *hd, u8 module_id,
325 struct greybus_module *gmod;
326 struct greybus_manifest *manifest;
330 /* we have to have at _least_ the manifest header */
331 if (size <= sizeof(manifest->header))
334 gmod = kzalloc(sizeof(*gmod), GFP_KERNEL);
338 gmod->module_number = module_id;
339 gmod->dev.parent = hd->parent;
340 gmod->dev.driver = NULL;
341 gmod->dev.bus = &greybus_bus_type;
342 gmod->dev.type = &greybus_module_type;
343 gmod->dev.groups = greybus_module_groups;
344 gmod->dev.dma_mask = hd->parent->dma_mask;
345 device_initialize(&gmod->dev);
346 dev_set_name(&gmod->dev, "%d", module_id);
348 manifest = (struct greybus_manifest *)data;
349 overall_size = le16_to_cpu(manifest->header.size);
350 if (overall_size != size) {
351 dev_err(hd->parent, "size != manifest header size, %d != %d\n",
356 /* Validate major/minor number */
357 if (manifest->header.version_major > GREYBUS_VERSION_MAJOR) {
359 "Manifest version too new (%hhu.%hhu > %hhu.%hhu)\n",
360 manifest->header.version_major,
361 manifest->header.version_minor,
362 GREYBUS_VERSION_MAJOR, GREYBUS_VERSION_MINOR);
366 size -= sizeof(manifest->header);
367 data += sizeof(manifest->header);
369 struct greybus_descriptor *desc;
373 if (size < sizeof(desc->header)) {
374 dev_err(hd->parent, "remaining size %d too small\n",
378 desc = (struct greybus_descriptor *)data;
379 desc_size = le16_to_cpu(desc->header.size);
380 if (size < desc_size) {
381 dev_err(hd->parent, "descriptor size %d too big\n",
385 data_size = (size_t)desc_size - sizeof(desc->header);
387 switch (le16_to_cpu(desc->header.type)) {
388 case GREYBUS_TYPE_FUNCTION:
389 retval = create_function(gmod, &desc->function,
393 case GREYBUS_TYPE_MODULE:
394 retval = create_module(gmod, &desc->module,
398 case GREYBUS_TYPE_STRING:
399 retval = create_string(gmod, &desc->string, data_size);
402 case GREYBUS_TYPE_CPORT:
403 retval = create_cport(gmod, &desc->cport, data_size);
406 case GREYBUS_TYPE_INVALID:
408 dev_err(hd->parent, "invalid descriptor type %d\n",
418 retval = gb_init_subdevs(gmod, &fake_gb_id);
422 // FIXME device_add(&gmod->dev);
427 put_device(&gmod->dev);
428 greybus_module_release(&gmod->dev);
431 void gb_remove_module(struct greybus_host_device *hd, u8 module_id)
433 // FIXME should be the remove_device call...
436 void greybus_remove_device(struct greybus_module *gmod)
438 /* tear down all of the "sub device types" for this device */
439 gb_i2c_disconnect(gmod);
440 gb_gpio_disconnect(gmod);
441 gb_sdio_disconnect(gmod);
442 gb_tty_disconnect(gmod);
443 gb_battery_disconnect(gmod);
445 // FIXME - device_remove(&gmod->dev);
448 static DEFINE_MUTEX(hd_mutex);
450 static void free_hd(struct kref *kref)
452 struct greybus_host_device *hd;
454 hd = container_of(kref, struct greybus_host_device, kref);
459 struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *driver,
460 struct device *parent)
462 struct greybus_host_device *hd;
464 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
468 kref_init(&hd->kref);
474 EXPORT_SYMBOL_GPL(greybus_create_hd);
476 void greybus_remove_hd(struct greybus_host_device *hd)
478 kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
480 EXPORT_SYMBOL_GPL(greybus_remove_hd);
483 static int __init gb_init(void)
487 retval = gb_debugfs_init();
489 pr_err("debugfs failed\n");
493 retval = bus_register(&greybus_bus_type);
495 pr_err("bus_register failed\n");
499 retval = gb_ap_init();
501 pr_err("gb_ap_init failed\n");
505 retval = gb_gbuf_init();
507 pr_err("gb_gbuf_init failed\n");
511 retval = gb_tty_init();
513 pr_err("gb_tty_init failed\n");
526 bus_unregister(&greybus_bus_type);
529 gb_debugfs_cleanup();
534 static void __exit gb_exit(void)
539 bus_unregister(&greybus_bus_type);
540 gb_debugfs_cleanup();
543 module_init(gb_init);
544 module_exit(gb_exit);
546 MODULE_LICENSE("GPL");
547 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");