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
15 #include "greybus_trace.h"
17 EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_create);
18 EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_release);
19 EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_add);
20 EXPORT_TRACEPOINT_SYMBOL_GPL(gb_hd_del);
22 /* Allow greybus to be disabled at boot if needed */
23 static bool nogreybus;
25 module_param(nogreybus, bool, 0444);
27 core_param(nogreybus, nogreybus, bool, 0444);
29 int greybus_disabled(void)
33 EXPORT_SYMBOL_GPL(greybus_disabled);
35 static int greybus_match_one_id(struct gb_bundle *bundle,
36 const struct greybus_bundle_id *id)
38 if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
39 (id->vendor != bundle->intf->vendor_id))
42 if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
43 (id->product != bundle->intf->product_id))
46 if ((id->match_flags & GREYBUS_ID_MATCH_CLASS) &&
47 (id->class != bundle->class))
53 static const struct greybus_bundle_id *
54 greybus_match_id(struct gb_bundle *bundle, const struct greybus_bundle_id *id)
59 for (; id->vendor || id->product || id->class || id->driver_info;
61 if (greybus_match_one_id(bundle, id))
68 static int greybus_match_device(struct device *dev, struct device_driver *drv)
70 struct greybus_driver *driver = to_greybus_driver(drv);
71 struct gb_bundle *bundle;
72 const struct greybus_bundle_id *id;
74 if (!is_gb_bundle(dev))
77 bundle = to_gb_bundle(dev);
79 id = greybus_match_id(bundle, driver->id_table);
82 /* FIXME - Dynamic ids? */
86 static int greybus_uevent(struct device *dev, struct kobj_uevent_env *env)
88 struct gb_host_device *hd;
89 struct gb_module *module = NULL;
90 struct gb_interface *intf = NULL;
91 struct gb_control *control = NULL;
92 struct gb_bundle *bundle = NULL;
93 struct gb_svc *svc = NULL;
95 if (is_gb_host_device(dev)) {
96 hd = to_gb_host_device(dev);
97 } else if (is_gb_module(dev)) {
98 module = to_gb_module(dev);
100 } else if (is_gb_interface(dev)) {
101 intf = to_gb_interface(dev);
102 module = intf->module;
104 } else if (is_gb_control(dev)) {
105 control = to_gb_control(dev);
106 intf = control->intf;
107 module = intf->module;
109 } else if (is_gb_bundle(dev)) {
110 bundle = to_gb_bundle(dev);
112 module = intf->module;
114 } else if (is_gb_svc(dev)) {
115 svc = to_gb_svc(dev);
118 dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
122 if (add_uevent_var(env, "BUS=%u", hd->bus_id))
126 if (add_uevent_var(env, "MODULE=%u", module->module_id))
131 if (add_uevent_var(env, "INTERFACE=%u", intf->interface_id))
133 if (add_uevent_var(env, "GREYBUS_ID=%08x/%08x",
134 intf->vendor_id, intf->product_id))
140 // add a uevent that can "load" a bundle type
141 // This is what we need to bind a driver to so use the info
142 // in gmod here as well
144 if (add_uevent_var(env, "BUNDLE=%u", bundle->id))
146 if (add_uevent_var(env, "BUNDLE_CLASS=%02x", bundle->class))
153 struct bus_type greybus_bus_type = {
155 .match = greybus_match_device,
156 .uevent = greybus_uevent,
159 static int greybus_probe(struct device *dev)
161 struct greybus_driver *driver = to_greybus_driver(dev->driver);
162 struct gb_bundle *bundle = to_gb_bundle(dev);
163 const struct greybus_bundle_id *id;
167 id = greybus_match_id(bundle, driver->id_table);
171 retval = driver->probe(bundle, id);
174 * Catch buggy drivers that fail to destroy their connections.
176 WARN_ON(!list_empty(&bundle->connections));
181 gb_timesync_schedule_asynchronous(bundle->intf);
186 static int greybus_remove(struct device *dev)
188 struct greybus_driver *driver = to_greybus_driver(dev->driver);
189 struct gb_bundle *bundle = to_gb_bundle(dev);
190 struct gb_connection *connection;
193 * Disable (non-offloaded) connections early in case the interface is
194 * already gone to avoid unceccessary operation timeouts during
195 * driver disconnect. Otherwise, only disable incoming requests.
197 list_for_each_entry(connection, &bundle->connections, bundle_links) {
198 if (gb_connection_is_offloaded(connection))
201 if (bundle->intf->disconnected)
202 gb_connection_disable_forced(connection);
204 gb_connection_disable_rx(connection);
207 driver->disconnect(bundle);
209 /* Catch buggy drivers that fail to destroy their connections. */
210 WARN_ON(!list_empty(&bundle->connections));
215 int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
216 const char *mod_name)
220 if (greybus_disabled())
223 driver->driver.bus = &greybus_bus_type;
224 driver->driver.name = driver->name;
225 driver->driver.probe = greybus_probe;
226 driver->driver.remove = greybus_remove;
227 driver->driver.owner = owner;
228 driver->driver.mod_name = mod_name;
230 retval = driver_register(&driver->driver);
234 pr_info("registered new driver %s\n", driver->name);
237 EXPORT_SYMBOL_GPL(greybus_register_driver);
239 void greybus_deregister_driver(struct greybus_driver *driver)
241 driver_unregister(&driver->driver);
243 EXPORT_SYMBOL_GPL(greybus_deregister_driver);
245 static int __init gb_init(void)
249 if (greybus_disabled())
252 BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
256 retval = bus_register(&greybus_bus_type);
258 pr_err("bus_register failed (%d)\n", retval);
262 retval = gb_hd_init();
264 pr_err("gb_hd_init failed (%d)\n", retval);
268 retval = gb_operation_init();
270 pr_err("gb_operation_init failed (%d)\n", retval);
271 goto error_operation;
274 retval = gb_bootrom_init();
276 pr_err("gb_bootrom_init failed\n");
280 retval = gb_timesync_init();
282 pr_err("gb_timesync_init failed\n");
285 return 0; /* Success */
294 bus_unregister(&greybus_bus_type);
296 gb_debugfs_cleanup();
300 module_init(gb_init);
302 static void __exit gb_exit(void)
308 bus_unregister(&greybus_bus_type);
309 gb_debugfs_cleanup();
310 tracepoint_synchronize_unregister();
312 module_exit(gb_exit);
313 MODULE_LICENSE("GPL v2");
314 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");