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 bool greybus_match_one_id(struct gb_bundle
*bundle
,
31 const struct greybus_bundle_id
*id
)
33 if ((id
->match_flags
& GREYBUS_ID_MATCH_VENDOR
) &&
34 (id
->vendor
!= bundle
->intf
->vendor_id
))
37 if ((id
->match_flags
& GREYBUS_ID_MATCH_PRODUCT
) &&
38 (id
->product
!= bundle
->intf
->product_id
))
41 if ((id
->match_flags
& GREYBUS_ID_MATCH_CLASS
) &&
42 (id
->class != bundle
->class))
48 static const struct greybus_bundle_id
*
49 greybus_match_id(struct gb_bundle
*bundle
, const struct greybus_bundle_id
*id
)
54 for (; id
->vendor
|| id
->product
|| id
->class || id
->driver_info
;
56 if (greybus_match_one_id(bundle
, id
))
63 static int greybus_match_device(struct device
*dev
, struct device_driver
*drv
)
65 struct greybus_driver
*driver
= to_greybus_driver(drv
);
66 struct gb_bundle
*bundle
;
67 const struct greybus_bundle_id
*id
;
69 if (!is_gb_bundle(dev
))
72 bundle
= to_gb_bundle(dev
);
74 id
= greybus_match_id(bundle
, driver
->id_table
);
77 /* FIXME - Dynamic ids? */
81 static int greybus_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
83 struct gb_host_device
*hd
;
84 struct gb_module
*module
= NULL
;
85 struct gb_interface
*intf
= NULL
;
86 struct gb_control
*control
= NULL
;
87 struct gb_bundle
*bundle
= NULL
;
88 struct gb_svc
*svc
= NULL
;
90 if (is_gb_host_device(dev
)) {
91 hd
= to_gb_host_device(dev
);
92 } else if (is_gb_module(dev
)) {
93 module
= to_gb_module(dev
);
95 } else if (is_gb_interface(dev
)) {
96 intf
= to_gb_interface(dev
);
97 module
= intf
->module
;
99 } else if (is_gb_control(dev
)) {
100 control
= to_gb_control(dev
);
101 intf
= control
->intf
;
102 module
= intf
->module
;
104 } else if (is_gb_bundle(dev
)) {
105 bundle
= to_gb_bundle(dev
);
107 module
= intf
->module
;
109 } else if (is_gb_svc(dev
)) {
110 svc
= to_gb_svc(dev
);
113 dev_WARN(dev
, "uevent for unknown greybus device \"type\"!\n");
117 if (add_uevent_var(env
, "BUS=%u", hd
->bus_id
))
121 if (add_uevent_var(env
, "MODULE=%u", module
->module_id
))
126 if (add_uevent_var(env
, "INTERFACE=%u", intf
->interface_id
))
128 if (add_uevent_var(env
, "GREYBUS_ID=%08x/%08x",
129 intf
->vendor_id
, intf
->product_id
))
135 // add a uevent that can "load" a bundle type
136 // This is what we need to bind a driver to so use the info
137 // in gmod here as well
139 if (add_uevent_var(env
, "BUNDLE=%u", bundle
->id
))
141 if (add_uevent_var(env
, "BUNDLE_CLASS=%02x", bundle
->class))
148 static void greybus_shutdown(struct device
*dev
)
150 if (is_gb_host_device(dev
)) {
151 struct gb_host_device
*hd
;
153 hd
= to_gb_host_device(dev
);
158 struct bus_type greybus_bus_type
= {
160 .match
= greybus_match_device
,
161 .uevent
= greybus_uevent
,
162 .shutdown
= greybus_shutdown
,
165 static int greybus_probe(struct device
*dev
)
167 struct greybus_driver
*driver
= to_greybus_driver(dev
->driver
);
168 struct gb_bundle
*bundle
= to_gb_bundle(dev
);
169 const struct greybus_bundle_id
*id
;
173 id
= greybus_match_id(bundle
, driver
->id_table
);
177 retval
= pm_runtime_get_sync(&bundle
->intf
->dev
);
179 pm_runtime_put_noidle(&bundle
->intf
->dev
);
183 retval
= gb_control_bundle_activate(bundle
->intf
->control
, bundle
->id
);
185 pm_runtime_put(&bundle
->intf
->dev
);
190 * Unbound bundle devices are always deactivated. During probe, the
191 * Runtime PM is set to enabled and active and the usage count is
192 * incremented. If the driver supports runtime PM, it should call
193 * pm_runtime_put() in its probe routine and pm_runtime_get_sync()
196 pm_runtime_set_autosuspend_delay(dev
, GB_BUNDLE_AUTOSUSPEND_MS
);
197 pm_runtime_use_autosuspend(dev
);
198 pm_runtime_get_noresume(dev
);
199 pm_runtime_set_active(dev
);
200 pm_runtime_enable(dev
);
202 retval
= driver
->probe(bundle
, id
);
205 * Catch buggy drivers that fail to destroy their connections.
207 WARN_ON(!list_empty(&bundle
->connections
));
209 gb_control_bundle_deactivate(bundle
->intf
->control
, bundle
->id
);
211 pm_runtime_disable(dev
);
212 pm_runtime_set_suspended(dev
);
213 pm_runtime_put_noidle(dev
);
214 pm_runtime_dont_use_autosuspend(dev
);
215 pm_runtime_put(&bundle
->intf
->dev
);
220 pm_runtime_put(&bundle
->intf
->dev
);
225 static int greybus_remove(struct device
*dev
)
227 struct greybus_driver
*driver
= to_greybus_driver(dev
->driver
);
228 struct gb_bundle
*bundle
= to_gb_bundle(dev
);
229 struct gb_connection
*connection
;
232 retval
= pm_runtime_get_sync(dev
);
234 dev_err(dev
, "failed to resume bundle: %d\n", retval
);
237 * Disable (non-offloaded) connections early in case the interface is
238 * already gone to avoid unceccessary operation timeouts during
239 * driver disconnect. Otherwise, only disable incoming requests.
241 list_for_each_entry(connection
, &bundle
->connections
, bundle_links
) {
242 if (gb_connection_is_offloaded(connection
))
245 if (bundle
->intf
->disconnected
)
246 gb_connection_disable_forced(connection
);
248 gb_connection_disable_rx(connection
);
251 driver
->disconnect(bundle
);
253 /* Catch buggy drivers that fail to destroy their connections. */
254 WARN_ON(!list_empty(&bundle
->connections
));
256 if (!bundle
->intf
->disconnected
)
257 gb_control_bundle_deactivate(bundle
->intf
->control
, bundle
->id
);
259 pm_runtime_put_noidle(dev
);
260 pm_runtime_disable(dev
);
261 pm_runtime_set_suspended(dev
);
262 pm_runtime_dont_use_autosuspend(dev
);
263 pm_runtime_put_noidle(dev
);
268 int greybus_register_driver(struct greybus_driver
*driver
, struct module
*owner
,
269 const char *mod_name
)
273 if (greybus_disabled())
276 driver
->driver
.bus
= &greybus_bus_type
;
277 driver
->driver
.name
= driver
->name
;
278 driver
->driver
.probe
= greybus_probe
;
279 driver
->driver
.remove
= greybus_remove
;
280 driver
->driver
.owner
= owner
;
281 driver
->driver
.mod_name
= mod_name
;
283 retval
= driver_register(&driver
->driver
);
287 pr_info("registered new driver %s\n", driver
->name
);
290 EXPORT_SYMBOL_GPL(greybus_register_driver
);
292 void greybus_deregister_driver(struct greybus_driver
*driver
)
294 driver_unregister(&driver
->driver
);
296 EXPORT_SYMBOL_GPL(greybus_deregister_driver
);
298 static int __init
gb_init(void)
302 if (greybus_disabled())
305 BUILD_BUG_ON(CPORT_ID_MAX
>= (long)CPORT_ID_BAD
);
309 retval
= bus_register(&greybus_bus_type
);
311 pr_err("bus_register failed (%d)\n", retval
);
315 retval
= gb_hd_init();
317 pr_err("gb_hd_init failed (%d)\n", retval
);
321 retval
= gb_operation_init();
323 pr_err("gb_operation_init failed (%d)\n", retval
);
324 goto error_operation
;
326 return 0; /* Success */
331 bus_unregister(&greybus_bus_type
);
333 gb_debugfs_cleanup();
337 module_init(gb_init
);
339 static void __exit
gb_exit(void)
343 bus_unregister(&greybus_bus_type
);
344 gb_debugfs_cleanup();
345 tracepoint_synchronize_unregister();
347 module_exit(gb_exit
);
348 MODULE_LICENSE("GPL v2");
349 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");