4 * Copyright 2014-2015 Google Inc.
5 * Copyright 2014-2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 /* Allow greybus to be disabled at boot if needed */
15 static bool nogreybus;
17 module_param(nogreybus, bool, 0444);
19 core_param(nogreybus, nogreybus, bool, 0444);
21 int greybus_disabled(void)
25 EXPORT_SYMBOL_GPL(greybus_disabled);
27 static int greybus_module_match(struct device *dev, struct device_driver *drv)
29 struct greybus_driver *driver = to_greybus_driver(drv);
30 struct gb_bundle *bundle = to_gb_bundle(dev);
31 const struct greybus_bundle_id *id;
33 id = gb_bundle_match_id(bundle, driver->id_table);
36 /* FIXME - Dynamic ids? */
40 static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
42 struct gb_module *module = NULL;
43 struct gb_interface *intf = NULL;
44 struct gb_bundle *bundle = NULL;
45 struct gb_connection *connection = NULL;
47 if (is_gb_endo(dev)) {
49 * Not much to do for an endo, just fall through, as the
50 * "default" attributes are good enough for us.
55 if (is_gb_module(dev)) {
56 module = to_gb_module(dev);
57 } else if (is_gb_interface(dev)) {
58 intf = to_gb_interface(dev);
59 } else if (is_gb_bundle(dev)) {
60 bundle = to_gb_bundle(dev);
62 } else if (is_gb_connection(dev)) {
63 connection = to_gb_connection(dev);
64 bundle = connection->bundle;
67 dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
73 // add a uevent that can "load" a connection type
79 // add a uevent that can "load" a bundle type
80 // This is what we need to bind a driver to so use the info
81 // in gmod here as well
86 // "just" a module, be vague here, nothing binds to a module except
87 // the greybus core, so there's not much, if anything, we need to
92 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 gb_bundle *bundle = to_gb_bundle(dev);
102 const struct greybus_bundle_id *id;
106 id = gb_bundle_match_id(bundle, driver->id_table);
110 retval = driver->probe(bundle, id);
117 static int greybus_remove(struct device *dev)
119 struct greybus_driver *driver = to_greybus_driver(dev->driver);
120 struct gb_bundle *bundle = to_gb_bundle(dev);
122 driver->disconnect(bundle);
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_driver(struct greybus_driver *driver)
151 driver_unregister(&driver->driver);
153 EXPORT_SYMBOL_GPL(greybus_deregister_driver);
156 static DEFINE_MUTEX(hd_mutex);
158 static void free_hd(struct kref *kref)
160 struct greybus_host_device *hd;
162 hd = container_of(kref, struct greybus_host_device, kref);
165 mutex_unlock(&hd_mutex);
168 struct greybus_host_device *greybus_create_hd(struct greybus_host_driver *driver,
169 struct device *parent,
170 size_t buffer_size_max)
172 struct greybus_host_device *hd;
175 * Validate that the driver implements all of the callbacks
176 * so that we don't have to every time we make them.
178 if ((!driver->message_send) || (!driver->message_cancel) ||
179 (!driver->submit_svc)) {
180 pr_err("Must implement all greybus_host_driver callbacks!\n");
181 return ERR_PTR(-EINVAL);
184 if (buffer_size_max < GB_OPERATION_MESSAGE_SIZE_MIN) {
185 dev_err(parent, "greybus host-device buffers too small\n");
190 * Make sure to never allocate messages larger than what the Greybus
193 if (buffer_size_max > GB_OPERATION_MESSAGE_SIZE_MAX) {
194 dev_warn(parent, "limiting buffer size to %u\n",
195 GB_OPERATION_MESSAGE_SIZE_MAX);
196 buffer_size_max = GB_OPERATION_MESSAGE_SIZE_MAX;
199 hd = kzalloc(sizeof(*hd) + driver->hd_priv_size, GFP_KERNEL);
201 return ERR_PTR(-ENOMEM);
203 kref_init(&hd->kref);
206 INIT_LIST_HEAD(&hd->interfaces);
207 INIT_LIST_HEAD(&hd->connections);
208 ida_init(&hd->cport_id_map);
209 hd->buffer_size_max = buffer_size_max;
213 EXPORT_SYMBOL_GPL(greybus_create_hd);
215 int greybus_endo_setup(struct greybus_host_device *hd, u16 endo_id,
218 struct gb_endo *endo;
220 endo = gb_endo_create(hd, endo_id, ap_intf_id);
222 return PTR_ERR(endo);
227 EXPORT_SYMBOL_GPL(greybus_endo_setup);
229 void greybus_remove_hd(struct greybus_host_device *hd)
232 * Tear down all interfaces, modules, and the endo that is associated
233 * with this host controller before freeing the memory associated with
234 * the host controller.
236 gb_interfaces_remove(hd);
237 gb_endo_remove(hd->endo);
238 kref_put_mutex(&hd->kref, free_hd, &hd_mutex);
240 EXPORT_SYMBOL_GPL(greybus_remove_hd);
242 static int __init gb_init(void)
246 if (greybus_disabled())
249 BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
253 retval = bus_register(&greybus_bus_type);
255 pr_err("bus_register failed (%d)\n", retval);
259 retval = gb_ap_init();
261 pr_err("gb_ap_init failed (%d)\n", retval);
265 retval = gb_operation_init();
267 pr_err("gb_operation_init failed (%d)\n", retval);
268 goto error_operation;
271 retval = gb_endo_init();
273 pr_err("gb_endo_init failed (%d)\n", retval);
277 return 0; /* Success */
284 bus_unregister(&greybus_bus_type);
286 gb_debugfs_cleanup();
290 module_init(gb_init);
292 static void __exit gb_exit(void)
297 bus_unregister(&greybus_bus_type);
298 gb_debugfs_cleanup();
300 module_exit(gb_exit);
301 MODULE_LICENSE("GPL v2");
302 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");