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
12 #define CREATE_TRACE_POINTS
14 #include "greybus_trace.h"
16 #define GB_BUNDLE_AUTOSUSPEND_MS 3000
18 /* Allow greybus to be disabled at boot if needed */
19 static bool nogreybus;
21 module_param(nogreybus, bool, 0444);
23 core_param(nogreybus, nogreybus, bool, 0444);
25 int greybus_disabled(void)
29 EXPORT_SYMBOL_GPL(greybus_disabled);
31 static bool greybus_match_one_id(struct gb_bundle *bundle,
32 const struct greybus_bundle_id *id)
34 if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
35 (id->vendor != bundle->intf->vendor_id))
38 if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
39 (id->product != bundle->intf->product_id))
42 if ((id->match_flags & GREYBUS_ID_MATCH_CLASS) &&
43 (id->class != bundle->class))
49 static const struct greybus_bundle_id *
50 greybus_match_id(struct gb_bundle *bundle, const struct greybus_bundle_id *id)
55 for (; id->vendor || id->product || id->class || id->driver_info;
57 if (greybus_match_one_id(bundle, id))
64 static int greybus_match_device(struct device *dev, struct device_driver *drv)
66 struct greybus_driver *driver = to_greybus_driver(drv);
67 struct gb_bundle *bundle;
68 const struct greybus_bundle_id *id;
70 if (!is_gb_bundle(dev))
73 bundle = to_gb_bundle(dev);
75 id = greybus_match_id(bundle, driver->id_table);
78 /* FIXME - Dynamic ids? */
82 static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
84 struct gb_host_device *hd;
85 struct gb_module *module = NULL;
86 struct gb_interface *intf = NULL;
87 struct gb_control *control = NULL;
88 struct gb_bundle *bundle = NULL;
89 struct gb_svc *svc = NULL;
91 if (is_gb_host_device(dev)) {
92 hd = to_gb_host_device(dev);
93 } else if (is_gb_module(dev)) {
94 module = to_gb_module(dev);
96 } else if (is_gb_interface(dev)) {
97 intf = to_gb_interface(dev);
98 module = intf->module;
100 } else if (is_gb_control(dev)) {
101 control = to_gb_control(dev);
102 intf = control->intf;
103 module = intf->module;
105 } else if (is_gb_bundle(dev)) {
106 bundle = to_gb_bundle(dev);
108 module = intf->module;
110 } else if (is_gb_svc(dev)) {
111 svc = to_gb_svc(dev);
114 dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
118 if (add_uevent_var(env, "BUS=%u", hd->bus_id))
122 if (add_uevent_var(env, "MODULE=%u", module->module_id))
127 if (add_uevent_var(env, "INTERFACE=%u", intf->interface_id))
129 if (add_uevent_var(env, "GREYBUS_ID=%08x/%08x",
130 intf->vendor_id, intf->product_id))
136 // add a uevent that can "load" a bundle type
137 // This is what we need to bind a driver to so use the info
138 // in gmod here as well
140 if (add_uevent_var(env, "BUNDLE=%u", bundle->id))
142 if (add_uevent_var(env, "BUNDLE_CLASS=%02x", bundle->class))
149 static void greybus_shutdown(struct device *dev)
151 if (is_gb_host_device(dev)) {
152 struct gb_host_device *hd;
154 hd = to_gb_host_device(dev);
159 struct bus_type greybus_bus_type = {
161 .match = greybus_match_device,
162 .uevent = greybus_uevent,
163 .shutdown = greybus_shutdown,
166 static int greybus_probe(struct device *dev)
168 struct greybus_driver *driver = to_greybus_driver(dev->driver);
169 struct gb_bundle *bundle = to_gb_bundle(dev);
170 const struct greybus_bundle_id *id;
174 id = greybus_match_id(bundle, driver->id_table);
178 retval = pm_runtime_get_sync(&bundle->intf->dev);
180 pm_runtime_put_noidle(&bundle->intf->dev);
184 retval = gb_control_bundle_activate(bundle->intf->control, bundle->id);
186 pm_runtime_put(&bundle->intf->dev);
191 * Unbound bundle devices are always deactivated. During probe, the
192 * Runtime PM is set to enabled and active and the usage count is
193 * incremented. If the driver supports runtime PM, it should call
194 * pm_runtime_put() in its probe routine and pm_runtime_get_sync()
197 pm_runtime_set_autosuspend_delay(dev, GB_BUNDLE_AUTOSUSPEND_MS);
198 pm_runtime_use_autosuspend(dev);
199 pm_runtime_get_noresume(dev);
200 pm_runtime_set_active(dev);
201 pm_runtime_enable(dev);
203 retval = driver->probe(bundle, id);
206 * Catch buggy drivers that fail to destroy their connections.
208 WARN_ON(!list_empty(&bundle->connections));
210 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
212 pm_runtime_disable(dev);
213 pm_runtime_set_suspended(dev);
214 pm_runtime_put_noidle(dev);
215 pm_runtime_dont_use_autosuspend(dev);
216 pm_runtime_put(&bundle->intf->dev);
221 pm_runtime_put(&bundle->intf->dev);
226 static int greybus_remove(struct device *dev)
228 struct greybus_driver *driver = to_greybus_driver(dev->driver);
229 struct gb_bundle *bundle = to_gb_bundle(dev);
230 struct gb_connection *connection;
233 retval = pm_runtime_get_sync(dev);
235 dev_err(dev, "failed to resume bundle: %d\n", retval);
238 * Disable (non-offloaded) connections early in case the interface is
239 * already gone to avoid unceccessary operation timeouts during
240 * driver disconnect. Otherwise, only disable incoming requests.
242 list_for_each_entry(connection, &bundle->connections, bundle_links) {
243 if (gb_connection_is_offloaded(connection))
246 if (bundle->intf->disconnected)
247 gb_connection_disable_forced(connection);
249 gb_connection_disable_rx(connection);
252 driver->disconnect(bundle);
254 /* Catch buggy drivers that fail to destroy their connections. */
255 WARN_ON(!list_empty(&bundle->connections));
257 if (!bundle->intf->disconnected)
258 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
260 pm_runtime_put_noidle(dev);
261 pm_runtime_disable(dev);
262 pm_runtime_set_suspended(dev);
263 pm_runtime_dont_use_autosuspend(dev);
264 pm_runtime_put_noidle(dev);
269 int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
270 const char *mod_name)
274 if (greybus_disabled())
277 driver->driver.bus = &greybus_bus_type;
278 driver->driver.name = driver->name;
279 driver->driver.probe = greybus_probe;
280 driver->driver.remove = greybus_remove;
281 driver->driver.owner = owner;
282 driver->driver.mod_name = mod_name;
284 retval = driver_register(&driver->driver);
288 pr_info("registered new driver %s\n", driver->name);
291 EXPORT_SYMBOL_GPL(greybus_register_driver);
293 void greybus_deregister_driver(struct greybus_driver *driver)
295 driver_unregister(&driver->driver);
297 EXPORT_SYMBOL_GPL(greybus_deregister_driver);
299 static int __init gb_init(void)
303 if (greybus_disabled())
306 BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
310 retval = bus_register(&greybus_bus_type);
312 pr_err("bus_register failed (%d)\n", retval);
316 retval = gb_hd_init();
318 pr_err("gb_hd_init failed (%d)\n", retval);
322 retval = gb_operation_init();
324 pr_err("gb_operation_init failed (%d)\n", retval);
325 goto error_operation;
327 return 0; /* Success */
332 bus_unregister(&greybus_bus_type);
334 gb_debugfs_cleanup();
338 module_init(gb_init);
340 static void __exit gb_exit(void)
344 bus_unregister(&greybus_bus_type);
345 gb_debugfs_cleanup();
346 tracepoint_synchronize_unregister();
348 module_exit(gb_exit);
349 MODULE_LICENSE("GPL v2");
350 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");