1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright 2014-2015 Google Inc.
6 * Copyright 2014-2015 Linaro Ltd.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #define CREATE_TRACE_POINTS
12 #include <linux/greybus.h>
13 #include "greybus_trace.h"
15 #define GB_BUNDLE_AUTOSUSPEND_MS 3000
17 /* Allow greybus to be disabled at boot if needed */
18 static bool nogreybus;
20 module_param(nogreybus, bool, 0444);
22 core_param(nogreybus, nogreybus, bool, 0444);
24 int greybus_disabled(void)
28 EXPORT_SYMBOL_GPL(greybus_disabled);
30 static int is_gb_host_device(const struct device *dev)
32 return dev->type == &greybus_hd_type;
35 static int is_gb_module(const struct device *dev)
37 return dev->type == &greybus_module_type;
40 static int is_gb_interface(const struct device *dev)
42 return dev->type == &greybus_interface_type;
45 static int is_gb_control(const struct device *dev)
47 return dev->type == &greybus_control_type;
50 static int is_gb_bundle(const struct device *dev)
52 return dev->type == &greybus_bundle_type;
55 static int is_gb_svc(const struct device *dev)
57 return dev->type == &greybus_svc_type;
60 static bool greybus_match_one_id(struct gb_bundle *bundle,
61 const struct greybus_bundle_id *id)
63 if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
64 (id->vendor != bundle->intf->vendor_id))
67 if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
68 (id->product != bundle->intf->product_id))
71 if ((id->match_flags & GREYBUS_ID_MATCH_CLASS) &&
72 (id->class != bundle->class))
78 static const struct greybus_bundle_id *
79 greybus_match_id(struct gb_bundle *bundle, const struct greybus_bundle_id *id)
84 for (; id->vendor || id->product || id->class || id->driver_info;
86 if (greybus_match_one_id(bundle, id))
93 static int greybus_match_device(struct device *dev, const struct device_driver *drv)
95 const struct greybus_driver *driver = to_greybus_driver(drv);
96 struct gb_bundle *bundle;
97 const struct greybus_bundle_id *id;
99 if (!is_gb_bundle(dev))
102 bundle = to_gb_bundle(dev);
104 id = greybus_match_id(bundle, driver->id_table);
107 /* FIXME - Dynamic ids? */
111 static int greybus_uevent(const struct device *dev, struct kobj_uevent_env *env)
113 const struct gb_host_device *hd;
114 const struct gb_module *module = NULL;
115 const struct gb_interface *intf = NULL;
116 const struct gb_control *control = NULL;
117 const struct gb_bundle *bundle = NULL;
118 const struct gb_svc *svc = NULL;
120 if (is_gb_host_device(dev)) {
121 hd = to_gb_host_device(dev);
122 } else if (is_gb_module(dev)) {
123 module = to_gb_module(dev);
125 } else if (is_gb_interface(dev)) {
126 intf = to_gb_interface(dev);
127 module = intf->module;
129 } else if (is_gb_control(dev)) {
130 control = to_gb_control(dev);
131 intf = control->intf;
132 module = intf->module;
134 } else if (is_gb_bundle(dev)) {
135 bundle = to_gb_bundle(dev);
137 module = intf->module;
139 } else if (is_gb_svc(dev)) {
140 svc = to_gb_svc(dev);
143 dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
147 if (add_uevent_var(env, "BUS=%u", hd->bus_id))
151 if (add_uevent_var(env, "MODULE=%u", module->module_id))
156 if (add_uevent_var(env, "INTERFACE=%u", intf->interface_id))
158 if (add_uevent_var(env, "GREYBUS_ID=%08x/%08x",
159 intf->vendor_id, intf->product_id))
165 // add a uevent that can "load" a bundle type
166 // This is what we need to bind a driver to so use the info
167 // in gmod here as well
169 if (add_uevent_var(env, "BUNDLE=%u", bundle->id))
171 if (add_uevent_var(env, "BUNDLE_CLASS=%02x", bundle->class))
178 static void greybus_shutdown(struct device *dev)
180 if (is_gb_host_device(dev)) {
181 struct gb_host_device *hd;
183 hd = to_gb_host_device(dev);
188 const struct bus_type greybus_bus_type = {
190 .match = greybus_match_device,
191 .uevent = greybus_uevent,
192 .shutdown = greybus_shutdown,
195 static int greybus_probe(struct device *dev)
197 struct greybus_driver *driver = to_greybus_driver(dev->driver);
198 struct gb_bundle *bundle = to_gb_bundle(dev);
199 const struct greybus_bundle_id *id;
203 id = greybus_match_id(bundle, driver->id_table);
207 retval = pm_runtime_get_sync(&bundle->intf->dev);
209 pm_runtime_put_noidle(&bundle->intf->dev);
213 retval = gb_control_bundle_activate(bundle->intf->control, bundle->id);
215 pm_runtime_put(&bundle->intf->dev);
220 * Unbound bundle devices are always deactivated. During probe, the
221 * Runtime PM is set to enabled and active and the usage count is
222 * incremented. If the driver supports runtime PM, it should call
223 * pm_runtime_put() in its probe routine and pm_runtime_get_sync()
226 pm_runtime_set_autosuspend_delay(dev, GB_BUNDLE_AUTOSUSPEND_MS);
227 pm_runtime_use_autosuspend(dev);
228 pm_runtime_get_noresume(dev);
229 pm_runtime_set_active(dev);
230 pm_runtime_enable(dev);
232 retval = driver->probe(bundle, id);
235 * Catch buggy drivers that fail to destroy their connections.
237 WARN_ON(!list_empty(&bundle->connections));
239 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
241 pm_runtime_disable(dev);
242 pm_runtime_set_suspended(dev);
243 pm_runtime_put_noidle(dev);
244 pm_runtime_dont_use_autosuspend(dev);
245 pm_runtime_put(&bundle->intf->dev);
250 pm_runtime_put(&bundle->intf->dev);
255 static int greybus_remove(struct device *dev)
257 struct greybus_driver *driver = to_greybus_driver(dev->driver);
258 struct gb_bundle *bundle = to_gb_bundle(dev);
259 struct gb_connection *connection;
262 retval = pm_runtime_get_sync(dev);
264 dev_err(dev, "failed to resume bundle: %d\n", retval);
267 * Disable (non-offloaded) connections early in case the interface is
268 * already gone to avoid unceccessary operation timeouts during
269 * driver disconnect. Otherwise, only disable incoming requests.
271 list_for_each_entry(connection, &bundle->connections, bundle_links) {
272 if (gb_connection_is_offloaded(connection))
275 if (bundle->intf->disconnected)
276 gb_connection_disable_forced(connection);
278 gb_connection_disable_rx(connection);
281 driver->disconnect(bundle);
283 /* Catch buggy drivers that fail to destroy their connections. */
284 WARN_ON(!list_empty(&bundle->connections));
286 if (!bundle->intf->disconnected)
287 gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
289 pm_runtime_put_noidle(dev);
290 pm_runtime_disable(dev);
291 pm_runtime_set_suspended(dev);
292 pm_runtime_dont_use_autosuspend(dev);
293 pm_runtime_put_noidle(dev);
298 int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
299 const char *mod_name)
303 if (greybus_disabled())
306 driver->driver.bus = &greybus_bus_type;
307 driver->driver.name = driver->name;
308 driver->driver.probe = greybus_probe;
309 driver->driver.remove = greybus_remove;
310 driver->driver.owner = owner;
311 driver->driver.mod_name = mod_name;
313 retval = driver_register(&driver->driver);
317 pr_info("registered new driver %s\n", driver->name);
320 EXPORT_SYMBOL_GPL(greybus_register_driver);
322 void greybus_deregister_driver(struct greybus_driver *driver)
324 driver_unregister(&driver->driver);
326 EXPORT_SYMBOL_GPL(greybus_deregister_driver);
328 static int __init gb_init(void)
332 if (greybus_disabled())
335 BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
339 retval = bus_register(&greybus_bus_type);
341 pr_err("bus_register failed (%d)\n", retval);
345 retval = gb_hd_init();
347 pr_err("gb_hd_init failed (%d)\n", retval);
351 retval = gb_operation_init();
353 pr_err("gb_operation_init failed (%d)\n", retval);
354 goto error_operation;
356 return 0; /* Success */
361 bus_unregister(&greybus_bus_type);
363 gb_debugfs_cleanup();
367 module_init(gb_init);
369 static void __exit gb_exit(void)
373 bus_unregister(&greybus_bus_type);
374 gb_debugfs_cleanup();
375 tracepoint_synchronize_unregister();
377 module_exit(gb_exit);
378 MODULE_DESCRIPTION("Greybus core driver");
379 MODULE_LICENSE("GPL v2");