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 gb_timesync_schedule_synchronous(bundle
->intf
);
223 pm_runtime_put(&bundle
->intf
->dev
);
228 static int greybus_remove(struct device
*dev
)
230 struct greybus_driver
*driver
= to_greybus_driver(dev
->driver
);
231 struct gb_bundle
*bundle
= to_gb_bundle(dev
);
232 struct gb_connection
*connection
;
235 retval
= pm_runtime_get_sync(dev
);
237 dev_err(dev
, "failed to resume bundle: %d\n", retval
);
240 * Disable (non-offloaded) connections early in case the interface is
241 * already gone to avoid unceccessary operation timeouts during
242 * driver disconnect. Otherwise, only disable incoming requests.
244 list_for_each_entry(connection
, &bundle
->connections
, bundle_links
) {
245 if (gb_connection_is_offloaded(connection
))
248 if (bundle
->intf
->disconnected
)
249 gb_connection_disable_forced(connection
);
251 gb_connection_disable_rx(connection
);
254 driver
->disconnect(bundle
);
256 /* Catch buggy drivers that fail to destroy their connections. */
257 WARN_ON(!list_empty(&bundle
->connections
));
259 if (!bundle
->intf
->disconnected
)
260 gb_control_bundle_deactivate(bundle
->intf
->control
, bundle
->id
);
262 pm_runtime_put_noidle(dev
);
263 pm_runtime_disable(dev
);
264 pm_runtime_set_suspended(dev
);
265 pm_runtime_dont_use_autosuspend(dev
);
266 pm_runtime_put_noidle(dev
);
271 int greybus_register_driver(struct greybus_driver
*driver
, struct module
*owner
,
272 const char *mod_name
)
276 if (greybus_disabled())
279 driver
->driver
.bus
= &greybus_bus_type
;
280 driver
->driver
.name
= driver
->name
;
281 driver
->driver
.probe
= greybus_probe
;
282 driver
->driver
.remove
= greybus_remove
;
283 driver
->driver
.owner
= owner
;
284 driver
->driver
.mod_name
= mod_name
;
286 retval
= driver_register(&driver
->driver
);
290 pr_info("registered new driver %s\n", driver
->name
);
293 EXPORT_SYMBOL_GPL(greybus_register_driver
);
295 void greybus_deregister_driver(struct greybus_driver
*driver
)
297 driver_unregister(&driver
->driver
);
299 EXPORT_SYMBOL_GPL(greybus_deregister_driver
);
301 static int __init
gb_init(void)
305 if (greybus_disabled())
308 BUILD_BUG_ON(CPORT_ID_MAX
>= (long)CPORT_ID_BAD
);
312 retval
= bus_register(&greybus_bus_type
);
314 pr_err("bus_register failed (%d)\n", retval
);
318 retval
= gb_hd_init();
320 pr_err("gb_hd_init failed (%d)\n", retval
);
324 retval
= gb_operation_init();
326 pr_err("gb_operation_init failed (%d)\n", retval
);
327 goto error_operation
;
330 retval
= gb_timesync_init();
332 pr_err("gb_timesync_init failed\n");
335 return 0; /* Success */
342 bus_unregister(&greybus_bus_type
);
344 gb_debugfs_cleanup();
348 module_init(gb_init
);
350 static void __exit
gb_exit(void)
355 bus_unregister(&greybus_bus_type
);
356 gb_debugfs_cleanup();
357 tracepoint_synchronize_unregister();
359 module_exit(gb_exit
);
360 MODULE_LICENSE("GPL v2");
361 MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");