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,
134 static const struct greybus_module_id fake_greybus_module_id = {
135 GREYBUS_DEVICE(0x42, 0x42)
142 * Pass in a buffer that _should_ contain a Greybus module manifest
143 * and register a greybus device structure with the kernel core.
145 void gb_add_module(struct greybus_host_device *hd, u8 module_id,
148 struct gb_module *gmod;
151 gmod = gb_module_create(hd, module_id);
153 dev_err(hd->parent, "failed to create module\n");
158 * Parse the manifest and build up our data structures
159 * representing what's in it.
161 if (!gb_manifest_parse(gmod, data, size)) {
162 dev_err(hd->parent, "manifest error\n");
168 * We've successfully parsed the manifest. Now we need to
169 * allocate CPort Id's for connecting to the CPorts found on
170 * other modules. For each of these, establish a connection
171 * between the local and remote CPorts (including
172 * configuring the switch to allow them to communicate).
175 gmod->dev.parent = hd->parent;
176 gmod->dev.driver = NULL;
177 gmod->dev.bus = &greybus_bus_type;
178 gmod->dev.type = &greybus_module_type;
179 gmod->dev.groups = greybus_module_groups;
180 gmod->dev.dma_mask = hd->parent->dma_mask;
181 device_initialize(&gmod->dev);
182 dev_set_name(&gmod->dev, "%d", module_id);
184 retval = device_add(&gmod->dev);
186 return; /* Success */
188 gb_module_destroy(gmod);
190 put_device(&gmod->dev);
191 greybus_module_release(&gmod->dev);
194 void gb_remove_module(struct greybus_host_device *hd, u8 module_id)
196 struct gb_module *gmod;
199 list_for_each_entry(gmod, &hd->modules, links)
200 if (gmod->module_id == module_id) {
206 greybus_remove_device(gmod);
208 dev_err(hd->parent, "module id %d remove error\n", module_id);
211 void greybus_remove_device(struct gb_module *gmod)
213 device_del(&gmod->dev);
214 put_device(&gmod->dev);
217 static DEFINE_MUTEX(hd_mutex);
219 static void free_hd(struct kref *kref)
221 struct greybus_host_device *hd;
223 hd = container_of(kref, struct greybus_host_device, kref);
226 mutex_unlock(&hd_mutex);
229 struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *driver,
230 struct device *parent)
232 struct greybus_host_device *hd;
234 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
238 kref_init(&hd->kref);
241 INIT_LIST_HEAD(&hd->modules);
242 hd->connections = RB_ROOT;
243 ida_init(&hd->cport_id_map);
244 spin_lock_init(&hd->cport_id_map_lock);
248 EXPORT_SYMBOL_GPL(greybus_create_hd);
250 void greybus_remove_hd(struct greybus_host_device *hd)
252 kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
254 EXPORT_SYMBOL_GPL(greybus_remove_hd);
257 static int __init gb_init(void)
261 BUILD_BUG_ON(HOST_DEV_CPORT_ID_MAX >= (long)CPORT_ID_BAD);
263 retval = gb_debugfs_init();
265 pr_err("debugfs failed\n");
269 retval = bus_register(&greybus_bus_type);
271 pr_err("bus_register failed\n");
275 retval = gb_ap_init();
277 pr_err("gb_ap_init failed\n");
281 retval = gb_gbuf_init();
283 pr_err("gb_gbuf_init failed\n");
287 retval = gb_operation_init();
289 pr_err("gb_operation_init failed\n");
290 goto error_operation;
293 retval = gb_tty_init();
295 pr_err("gb_tty_init failed\n");
311 bus_unregister(&greybus_bus_type);
314 gb_debugfs_cleanup();
319 static void __exit gb_exit(void)
325 bus_unregister(&greybus_bus_type);
326 gb_debugfs_cleanup();
329 module_init(gb_init);
330 module_exit(gb_exit);
332 MODULE_LICENSE("GPL");
333 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");