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_module_match(struct device *dev, struct device_driver *drv)
35 struct greybus_driver *driver = to_greybus_driver(dev->driver);
36 struct gb_module *gmod = to_gb_module(dev);
37 const struct greybus_module_id *id;
39 id = gb_module_match_id(gmod, driver->id_table);
42 /* FIXME - Dyanmic ids? */
46 static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
48 /* struct gb_module *gmod = to_gb_module(dev); */
50 /* FIXME - add some uevents here... */
54 static struct bus_type greybus_bus_type = {
56 .match = greybus_module_match,
57 .uevent = greybus_uevent,
60 static int greybus_probe(struct device *dev)
62 struct greybus_driver *driver = to_greybus_driver(dev->driver);
63 struct gb_module *gmod = to_gb_module(dev);
64 const struct greybus_module_id *id;
68 id = gb_module_match_id(gmod, driver->id_table);
72 retval = driver->probe(gmod, id);
79 static int greybus_remove(struct device *dev)
81 struct greybus_driver *driver = to_greybus_driver(dev->driver);
82 struct gb_module *gmod = to_gb_module(dev);
84 driver->disconnect(gmod);
88 int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
93 if (greybus_disabled())
96 driver->driver.name = driver->name;
97 driver->driver.probe = greybus_probe;
98 driver->driver.remove = greybus_remove;
99 driver->driver.owner = owner;
100 driver->driver.mod_name = mod_name;
102 retval = driver_register(&driver->driver);
106 pr_info("registered new driver %s\n", driver->name);
109 EXPORT_SYMBOL_GPL(greybus_register_driver);
111 void greybus_deregister(struct greybus_driver *driver)
113 driver_unregister(&driver->driver);
115 EXPORT_SYMBOL_GPL(greybus_deregister);
118 static void greybus_module_release(struct device *dev)
120 struct gb_module *gmod = to_gb_module(dev);
123 for (i = 0; i < gmod->num_strings; ++i)
124 kfree(gmod->string[i]);
129 static struct device_type greybus_module_type = {
130 .name = "greybus_module",
131 .release = greybus_module_release,
135 * This needs to be driven by the list of functions that the
136 * manifest says are present.
138 static int gb_init_subdevs(struct gb_module *gmod,
139 const struct greybus_module_id *id)
143 /* Allocate all of the different "sub device types" for this device */
146 * Decide what exactly we should get supplied for the i2c
147 * probe, and then work that back to what should be present
150 retval = gb_i2c_probe(gmod, id);
154 retval = gb_gpio_probe(gmod, id);
158 retval = gb_sdio_probe(gmod, id);
162 retval = gb_tty_probe(gmod, id);
166 retval = gb_battery_probe(gmod, id);
172 gb_tty_disconnect(gmod);
175 gb_sdio_disconnect(gmod);
178 gb_gpio_disconnect(gmod);
181 gb_i2c_disconnect(gmod);
187 static const struct greybus_module_id fake_greybus_module_id = {
188 GREYBUS_DEVICE(0x42, 0x42)
195 * Pass in a buffer that _should_ contain a Greybus module manifest
196 * and register a greybus device structure with the kernel core.
198 void gb_add_module(struct greybus_host_device *hd, u8 module_id,
201 struct gb_module *gmod;
204 gmod = gb_module_create(hd, module_id);
206 dev_err(hd->parent, "failed to create module\n");
211 * Parse the manifest and build up our data structures
212 * representing what's in it.
214 if (!gb_manifest_parse(gmod, data, size)) {
215 dev_err(hd->parent, "manifest error\n");
221 * We've successfully parsed the manifest. Now we need to
222 * allocate CPort Id's for connecting to the CPorts found on
223 * other modules. For each of these, establish a connection
224 * between the local and remote CPorts (including
225 * configuring the switch to allow them to communicate).
228 gmod->dev.parent = hd->parent;
229 gmod->dev.driver = NULL;
230 gmod->dev.bus = &greybus_bus_type;
231 gmod->dev.type = &greybus_module_type;
232 gmod->dev.groups = greybus_module_groups;
233 gmod->dev.dma_mask = hd->parent->dma_mask;
234 device_initialize(&gmod->dev);
235 dev_set_name(&gmod->dev, "%d", module_id);
237 retval = device_add(&gmod->dev);
241 retval = gb_init_subdevs(gmod, &fake_greybus_module_id);
249 device_del(&gmod->dev);
252 gb_module_destroy(gmod);
254 put_device(&gmod->dev);
255 greybus_module_release(&gmod->dev);
258 void gb_remove_module(struct greybus_host_device *hd, u8 module_id)
260 struct gb_module *gmod;
263 list_for_each_entry(gmod, &hd->modules, links)
264 if (gmod->module_id == module_id) {
270 greybus_remove_device(gmod);
272 dev_err(hd->parent, "module id %d remove error\n", module_id);
275 void greybus_remove_device(struct gb_module *gmod)
277 /* tear down all of the "sub device types" for this device */
278 gb_i2c_disconnect(gmod);
279 gb_gpio_disconnect(gmod);
280 gb_sdio_disconnect(gmod);
281 gb_tty_disconnect(gmod);
282 gb_battery_disconnect(gmod);
284 device_del(&gmod->dev);
285 put_device(&gmod->dev);
288 static DEFINE_MUTEX(hd_mutex);
290 static void free_hd(struct kref *kref)
292 struct greybus_host_device *hd;
294 hd = container_of(kref, struct greybus_host_device, kref);
299 struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *driver,
300 struct device *parent)
302 struct greybus_host_device *hd;
304 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
308 kref_init(&hd->kref);
311 INIT_LIST_HEAD(&hd->modules);
312 hd->connections = RB_ROOT;
313 ida_init(&hd->cport_id_map);
314 spin_lock_init(&hd->cport_id_map_lock);
318 EXPORT_SYMBOL_GPL(greybus_create_hd);
320 void greybus_remove_hd(struct greybus_host_device *hd)
322 kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
324 EXPORT_SYMBOL_GPL(greybus_remove_hd);
327 static int __init gb_init(void)
331 BUILD_BUG_ON(HOST_DEV_CPORT_ID_MAX >= (long)CPORT_ID_BAD);
333 retval = gb_debugfs_init();
335 pr_err("debugfs failed\n");
339 retval = bus_register(&greybus_bus_type);
341 pr_err("bus_register failed\n");
345 retval = gb_ap_init();
347 pr_err("gb_ap_init failed\n");
351 retval = gb_gbuf_init();
353 pr_err("gb_gbuf_init failed\n");
357 retval = gb_tty_init();
359 pr_err("gb_tty_init failed\n");
372 bus_unregister(&greybus_bus_type);
375 gb_debugfs_cleanup();
380 static void __exit gb_exit(void)
385 bus_unregister(&greybus_bus_type);
386 gb_debugfs_cleanup();
389 module_init(gb_init);
390 module_exit(gb_exit);
392 MODULE_LICENSE("GPL");
393 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");