2017-11-07 13:58:41 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-08-11 07:30:45 +00:00
|
|
|
/*
|
|
|
|
* Greybus "Core"
|
|
|
|
*
|
2015-05-22 17:59:16 +00:00
|
|
|
* Copyright 2014-2015 Google Inc.
|
|
|
|
* Copyright 2014-2015 Linaro Ltd.
|
2014-08-11 07:30:45 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2015-09-18 15:38:45 +00:00
|
|
|
#define CREATE_TRACE_POINTS
|
2019-08-25 05:54:27 +00:00
|
|
|
#include <linux/greybus.h>
|
2015-09-18 15:38:45 +00:00
|
|
|
#include "greybus_trace.h"
|
2014-08-11 07:30:45 +00:00
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
#define GB_BUNDLE_AUTOSUSPEND_MS 3000
|
|
|
|
|
2014-08-11 07:30:45 +00:00
|
|
|
/* Allow greybus to be disabled at boot if needed */
|
|
|
|
static bool nogreybus;
|
|
|
|
#ifdef MODULE
|
|
|
|
module_param(nogreybus, bool, 0444);
|
|
|
|
#else
|
2014-10-20 11:15:50 +00:00
|
|
|
core_param(nogreybus, nogreybus, bool, 0444);
|
2014-08-11 07:30:45 +00:00
|
|
|
#endif
|
|
|
|
int greybus_disabled(void)
|
|
|
|
{
|
|
|
|
return nogreybus;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(greybus_disabled);
|
|
|
|
|
2016-06-09 11:04:36 +00:00
|
|
|
static bool greybus_match_one_id(struct gb_bundle *bundle,
|
2018-11-09 13:53:31 +00:00
|
|
|
const struct greybus_bundle_id *id)
|
2015-11-21 09:52:03 +00:00
|
|
|
{
|
|
|
|
if ((id->match_flags & GREYBUS_ID_MATCH_VENDOR) &&
|
2015-11-25 14:58:56 +00:00
|
|
|
(id->vendor != bundle->intf->vendor_id))
|
2016-06-09 11:04:36 +00:00
|
|
|
return false;
|
2015-11-21 09:52:03 +00:00
|
|
|
|
|
|
|
if ((id->match_flags & GREYBUS_ID_MATCH_PRODUCT) &&
|
2015-11-25 14:58:56 +00:00
|
|
|
(id->product != bundle->intf->product_id))
|
2016-06-09 11:04:36 +00:00
|
|
|
return false;
|
2015-11-21 09:52:03 +00:00
|
|
|
|
|
|
|
if ((id->match_flags & GREYBUS_ID_MATCH_CLASS) &&
|
|
|
|
(id->class != bundle->class))
|
2016-06-09 11:04:36 +00:00
|
|
|
return false;
|
2015-11-21 09:52:03 +00:00
|
|
|
|
2016-06-09 11:04:36 +00:00
|
|
|
return true;
|
2015-11-21 09:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct greybus_bundle_id *
|
|
|
|
greybus_match_id(struct gb_bundle *bundle, const struct greybus_bundle_id *id)
|
|
|
|
{
|
2018-11-09 13:53:24 +00:00
|
|
|
if (!id)
|
2015-11-21 09:52:03 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (; id->vendor || id->product || id->class || id->driver_info;
|
|
|
|
id++) {
|
|
|
|
if (greybus_match_one_id(bundle, id))
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-12 06:04:05 +00:00
|
|
|
static int greybus_match_device(struct device *dev, struct device_driver *drv)
|
2014-08-11 07:30:45 +00:00
|
|
|
{
|
2014-11-14 11:54:59 +00:00
|
|
|
struct greybus_driver *driver = to_greybus_driver(drv);
|
2016-01-08 19:13:41 +00:00
|
|
|
struct gb_bundle *bundle;
|
2015-04-01 15:02:04 +00:00
|
|
|
const struct greybus_bundle_id *id;
|
2014-08-11 07:30:45 +00:00
|
|
|
|
2016-01-08 19:13:41 +00:00
|
|
|
if (!is_gb_bundle(dev))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bundle = to_gb_bundle(dev);
|
|
|
|
|
2015-11-21 09:52:03 +00:00
|
|
|
id = greybus_match_id(bundle, driver->id_table);
|
2014-08-11 07:30:45 +00:00
|
|
|
if (id)
|
|
|
|
return 1;
|
2014-11-21 05:56:30 +00:00
|
|
|
/* FIXME - Dynamic ids? */
|
2014-08-11 07:30:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 11:30:17 +00:00
|
|
|
static int greybus_uevent(const struct device *dev, struct kobj_uevent_env *env)
|
2014-08-11 07:30:45 +00:00
|
|
|
{
|
2023-01-11 11:30:17 +00:00
|
|
|
const struct gb_host_device *hd;
|
|
|
|
const struct gb_module *module = NULL;
|
|
|
|
const struct gb_interface *intf = NULL;
|
|
|
|
const struct gb_control *control = NULL;
|
|
|
|
const struct gb_bundle *bundle = NULL;
|
|
|
|
const struct gb_svc *svc = NULL;
|
2014-11-15 20:12:16 +00:00
|
|
|
|
2015-11-25 14:59:02 +00:00
|
|
|
if (is_gb_host_device(dev)) {
|
|
|
|
hd = to_gb_host_device(dev);
|
2016-04-23 16:47:24 +00:00
|
|
|
} else if (is_gb_module(dev)) {
|
|
|
|
module = to_gb_module(dev);
|
|
|
|
hd = module->hd;
|
2014-12-21 22:10:26 +00:00
|
|
|
} else if (is_gb_interface(dev)) {
|
2014-12-19 22:56:31 +00:00
|
|
|
intf = to_gb_interface(dev);
|
2016-04-23 16:47:24 +00:00
|
|
|
module = intf->module;
|
2015-12-03 18:18:02 +00:00
|
|
|
hd = intf->hd;
|
2016-04-13 17:19:02 +00:00
|
|
|
} else if (is_gb_control(dev)) {
|
|
|
|
control = to_gb_control(dev);
|
|
|
|
intf = control->intf;
|
2016-05-04 23:21:29 +00:00
|
|
|
module = intf->module;
|
2016-04-13 17:19:02 +00:00
|
|
|
hd = intf->hd;
|
2014-12-12 22:10:17 +00:00
|
|
|
} else if (is_gb_bundle(dev)) {
|
|
|
|
bundle = to_gb_bundle(dev);
|
2014-12-19 22:56:31 +00:00
|
|
|
intf = bundle->intf;
|
2016-04-23 16:47:24 +00:00
|
|
|
module = intf->module;
|
2015-12-03 18:18:02 +00:00
|
|
|
hd = intf->hd;
|
2015-11-25 14:59:08 +00:00
|
|
|
} else if (is_gb_svc(dev)) {
|
|
|
|
svc = to_gb_svc(dev);
|
2015-12-03 18:18:02 +00:00
|
|
|
hd = svc->hd;
|
2014-11-15 20:12:16 +00:00
|
|
|
} else {
|
|
|
|
dev_WARN(dev, "uevent for unknown greybus device \"type\"!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-12-03 18:18:02 +00:00
|
|
|
if (add_uevent_var(env, "BUS=%u", hd->bus_id))
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2016-04-23 16:47:24 +00:00
|
|
|
if (module) {
|
|
|
|
if (add_uevent_var(env, "MODULE=%u", module->module_id))
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2015-12-03 18:18:04 +00:00
|
|
|
if (intf) {
|
|
|
|
if (add_uevent_var(env, "INTERFACE=%u", intf->interface_id))
|
|
|
|
return -ENOMEM;
|
2016-02-27 05:54:38 +00:00
|
|
|
if (add_uevent_var(env, "GREYBUS_ID=%08x/%08x",
|
|
|
|
intf->vendor_id, intf->product_id))
|
2016-01-12 03:24:54 +00:00
|
|
|
return -ENOMEM;
|
2015-12-03 18:18:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
if (bundle) {
|
2014-11-15 20:12:16 +00:00
|
|
|
// FIXME
|
2014-12-12 22:10:17 +00:00
|
|
|
// add a uevent that can "load" a bundle type
|
2014-11-15 20:12:16 +00:00
|
|
|
// This is what we need to bind a driver to so use the info
|
|
|
|
// in gmod here as well
|
2015-12-04 09:44:24 +00:00
|
|
|
|
|
|
|
if (add_uevent_var(env, "BUNDLE=%u", bundle->id))
|
|
|
|
return -ENOMEM;
|
2016-01-22 02:13:41 +00:00
|
|
|
if (add_uevent_var(env, "BUNDLE_CLASS=%02x", bundle->class))
|
|
|
|
return -ENOMEM;
|
2014-11-15 20:12:16 +00:00
|
|
|
}
|
|
|
|
|
2014-08-11 07:30:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-07-13 00:41:21 +00:00
|
|
|
static void greybus_shutdown(struct device *dev)
|
|
|
|
{
|
|
|
|
if (is_gb_host_device(dev)) {
|
|
|
|
struct gb_host_device *hd;
|
|
|
|
|
|
|
|
hd = to_gb_host_device(dev);
|
|
|
|
gb_hd_shutdown(hd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
struct bus_type greybus_bus_type = {
|
2014-08-11 07:30:45 +00:00
|
|
|
.name = "greybus",
|
2016-05-12 06:04:05 +00:00
|
|
|
.match = greybus_match_device,
|
2014-08-11 07:30:45 +00:00
|
|
|
.uevent = greybus_uevent,
|
2016-07-13 00:41:21 +00:00
|
|
|
.shutdown = greybus_shutdown,
|
2014-08-11 07:30:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int greybus_probe(struct device *dev)
|
|
|
|
{
|
|
|
|
struct greybus_driver *driver = to_greybus_driver(dev->driver);
|
2015-04-01 15:02:04 +00:00
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
const struct greybus_bundle_id *id;
|
2014-08-11 07:30:45 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
/* match id */
|
2015-11-21 09:52:03 +00:00
|
|
|
id = greybus_match_id(bundle, driver->id_table);
|
2014-08-11 07:30:45 +00:00
|
|
|
if (!id)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
retval = pm_runtime_get_sync(&bundle->intf->dev);
|
|
|
|
if (retval < 0) {
|
|
|
|
pm_runtime_put_noidle(&bundle->intf->dev);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-07-21 16:09:34 +00:00
|
|
|
retval = gb_control_bundle_activate(bundle->intf->control, bundle->id);
|
|
|
|
if (retval) {
|
|
|
|
pm_runtime_put(&bundle->intf->dev);
|
|
|
|
return retval;
|
|
|
|
}
|
2016-07-14 20:13:00 +00:00
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
/*
|
|
|
|
* Unbound bundle devices are always deactivated. During probe, the
|
|
|
|
* Runtime PM is set to enabled and active and the usage count is
|
|
|
|
* incremented. If the driver supports runtime PM, it should call
|
|
|
|
* pm_runtime_put() in its probe routine and pm_runtime_get_sync()
|
|
|
|
* in remove routine.
|
|
|
|
*/
|
|
|
|
pm_runtime_set_autosuspend_delay(dev, GB_BUNDLE_AUTOSUSPEND_MS);
|
|
|
|
pm_runtime_use_autosuspend(dev);
|
|
|
|
pm_runtime_get_noresume(dev);
|
|
|
|
pm_runtime_set_active(dev);
|
|
|
|
pm_runtime_enable(dev);
|
|
|
|
|
2015-04-01 15:02:04 +00:00
|
|
|
retval = driver->probe(bundle, id);
|
2016-01-21 16:34:09 +00:00
|
|
|
if (retval) {
|
|
|
|
/*
|
|
|
|
* Catch buggy drivers that fail to destroy their connections.
|
|
|
|
*/
|
|
|
|
WARN_ON(!list_empty(&bundle->connections));
|
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
|
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
pm_runtime_disable(dev);
|
|
|
|
pm_runtime_set_suspended(dev);
|
|
|
|
pm_runtime_put_noidle(dev);
|
|
|
|
pm_runtime_dont_use_autosuspend(dev);
|
|
|
|
pm_runtime_put(&bundle->intf->dev);
|
|
|
|
|
2014-08-11 07:30:45 +00:00
|
|
|
return retval;
|
2016-01-21 16:34:09 +00:00
|
|
|
}
|
2014-08-11 07:30:45 +00:00
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
pm_runtime_put(&bundle->intf->dev);
|
|
|
|
|
2014-08-11 07:30:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int greybus_remove(struct device *dev)
|
|
|
|
{
|
|
|
|
struct greybus_driver *driver = to_greybus_driver(dev->driver);
|
2015-04-01 15:02:04 +00:00
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
2016-01-19 11:51:09 +00:00
|
|
|
struct gb_connection *connection;
|
2016-07-14 20:13:00 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = pm_runtime_get_sync(dev);
|
|
|
|
if (retval < 0)
|
|
|
|
dev_err(dev, "failed to resume bundle: %d\n", retval);
|
2016-01-19 11:51:09 +00:00
|
|
|
|
2016-06-09 16:42:19 +00:00
|
|
|
/*
|
|
|
|
* Disable (non-offloaded) connections early in case the interface is
|
|
|
|
* already gone to avoid unceccessary operation timeouts during
|
|
|
|
* driver disconnect. Otherwise, only disable incoming requests.
|
|
|
|
*/
|
2016-01-19 11:51:18 +00:00
|
|
|
list_for_each_entry(connection, &bundle->connections, bundle_links) {
|
2016-06-09 16:42:19 +00:00
|
|
|
if (gb_connection_is_offloaded(connection))
|
|
|
|
continue;
|
|
|
|
|
2016-01-19 11:51:18 +00:00
|
|
|
if (bundle->intf->disconnected)
|
2016-05-27 15:26:22 +00:00
|
|
|
gb_connection_disable_forced(connection);
|
2016-01-19 11:51:18 +00:00
|
|
|
else
|
|
|
|
gb_connection_disable_rx(connection);
|
|
|
|
}
|
2014-08-11 07:30:45 +00:00
|
|
|
|
2015-04-01 15:02:04 +00:00
|
|
|
driver->disconnect(bundle);
|
2016-01-19 11:51:10 +00:00
|
|
|
|
2016-01-21 16:34:09 +00:00
|
|
|
/* Catch buggy drivers that fail to destroy their connections. */
|
|
|
|
WARN_ON(!list_empty(&bundle->connections));
|
2016-01-19 11:51:10 +00:00
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
if (!bundle->intf->disconnected)
|
|
|
|
gb_control_bundle_deactivate(bundle->intf->control, bundle->id);
|
|
|
|
|
2016-07-14 20:13:00 +00:00
|
|
|
pm_runtime_put_noidle(dev);
|
|
|
|
pm_runtime_disable(dev);
|
|
|
|
pm_runtime_set_suspended(dev);
|
|
|
|
pm_runtime_dont_use_autosuspend(dev);
|
|
|
|
pm_runtime_put_noidle(dev);
|
|
|
|
|
2014-08-11 07:30:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int greybus_register_driver(struct greybus_driver *driver, struct module *owner,
|
2019-02-26 00:32:44 +00:00
|
|
|
const char *mod_name)
|
2014-08-11 07:30:45 +00:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (greybus_disabled())
|
|
|
|
return -ENODEV;
|
|
|
|
|
2016-01-08 19:13:42 +00:00
|
|
|
driver->driver.bus = &greybus_bus_type;
|
2014-08-11 07:30:45 +00:00
|
|
|
driver->driver.name = driver->name;
|
|
|
|
driver->driver.probe = greybus_probe;
|
|
|
|
driver->driver.remove = greybus_remove;
|
|
|
|
driver->driver.owner = owner;
|
|
|
|
driver->driver.mod_name = mod_name;
|
|
|
|
|
|
|
|
retval = driver_register(&driver->driver);
|
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
pr_info("registered new driver %s\n", driver->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(greybus_register_driver);
|
|
|
|
|
2015-06-08 17:05:13 +00:00
|
|
|
void greybus_deregister_driver(struct greybus_driver *driver)
|
2014-08-11 07:30:45 +00:00
|
|
|
{
|
|
|
|
driver_unregister(&driver->driver);
|
|
|
|
}
|
2015-06-08 17:05:13 +00:00
|
|
|
EXPORT_SYMBOL_GPL(greybus_deregister_driver);
|
2014-08-11 07:30:45 +00:00
|
|
|
|
2014-08-30 23:21:03 +00:00
|
|
|
static int __init gb_init(void)
|
2014-08-30 23:20:22 +00:00
|
|
|
{
|
2014-08-30 23:47:26 +00:00
|
|
|
int retval;
|
|
|
|
|
2015-03-20 14:59:13 +00:00
|
|
|
if (greybus_disabled())
|
|
|
|
return -ENODEV;
|
|
|
|
|
2015-06-13 16:02:09 +00:00
|
|
|
BUILD_BUG_ON(CPORT_ID_MAX >= (long)CPORT_ID_BAD);
|
2014-10-02 17:30:03 +00:00
|
|
|
|
2015-03-27 10:38:06 +00:00
|
|
|
gb_debugfs_init();
|
2014-08-30 23:47:26 +00:00
|
|
|
|
2014-08-31 20:54:59 +00:00
|
|
|
retval = bus_register(&greybus_bus_type);
|
2014-09-13 23:15:52 +00:00
|
|
|
if (retval) {
|
2015-06-09 22:42:51 +00:00
|
|
|
pr_err("bus_register failed (%d)\n", retval);
|
2014-08-31 20:54:59 +00:00
|
|
|
goto error_bus;
|
2014-09-13 23:15:52 +00:00
|
|
|
}
|
2014-08-31 20:54:59 +00:00
|
|
|
|
2015-11-25 14:59:02 +00:00
|
|
|
retval = gb_hd_init();
|
|
|
|
if (retval) {
|
|
|
|
pr_err("gb_hd_init failed (%d)\n", retval);
|
|
|
|
goto error_hd;
|
|
|
|
}
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
retval = gb_operation_init();
|
|
|
|
if (retval) {
|
2015-06-09 22:42:51 +00:00
|
|
|
pr_err("gb_operation_init failed (%d)\n", retval);
|
2014-10-16 11:35:34 +00:00
|
|
|
goto error_operation;
|
|
|
|
}
|
2014-11-05 22:12:53 +00:00
|
|
|
return 0; /* Success */
|
|
|
|
|
2014-10-16 11:35:34 +00:00
|
|
|
error_operation:
|
2015-11-25 14:59:02 +00:00
|
|
|
gb_hd_exit();
|
|
|
|
error_hd:
|
2014-08-31 20:54:59 +00:00
|
|
|
bus_unregister(&greybus_bus_type);
|
|
|
|
error_bus:
|
2014-08-31 23:17:04 +00:00
|
|
|
gb_debugfs_cleanup();
|
2014-08-31 20:54:59 +00:00
|
|
|
|
|
|
|
return retval;
|
2014-08-30 23:20:22 +00:00
|
|
|
}
|
2015-03-19 11:32:49 +00:00
|
|
|
module_init(gb_init);
|
2014-08-30 23:20:22 +00:00
|
|
|
|
2014-08-30 23:21:03 +00:00
|
|
|
static void __exit gb_exit(void)
|
2014-08-30 23:20:22 +00:00
|
|
|
{
|
2014-10-16 11:35:34 +00:00
|
|
|
gb_operation_exit();
|
2015-11-25 14:59:02 +00:00
|
|
|
gb_hd_exit();
|
2014-08-31 20:54:59 +00:00
|
|
|
bus_unregister(&greybus_bus_type);
|
2014-08-31 23:17:04 +00:00
|
|
|
gb_debugfs_cleanup();
|
2015-09-18 15:38:45 +00:00
|
|
|
tracepoint_synchronize_unregister();
|
2014-08-30 23:20:22 +00:00
|
|
|
}
|
|
|
|
module_exit(gb_exit);
|
2015-04-13 17:51:33 +00:00
|
|
|
MODULE_LICENSE("GPL v2");
|
2014-08-11 07:30:45 +00:00
|
|
|
MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");
|