2017-11-07 20:58:41 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2014-10-02 09:54:12 +07:00
|
|
|
/*
|
2014-12-13 05:10:17 +07:00
|
|
|
* Greybus bundles
|
2014-10-02 09:54:12 +07:00
|
|
|
*
|
2015-04-14 00:48:37 +07:00
|
|
|
* Copyright 2014-2015 Google Inc.
|
|
|
|
* Copyright 2014-2015 Linaro Ltd.
|
2014-10-02 09:54:12 +07:00
|
|
|
*/
|
|
|
|
|
2019-08-25 12:54:27 +07:00
|
|
|
#include <linux/greybus.h>
|
2016-06-04 03:55:36 +07:00
|
|
|
#include "greybus_trace.h"
|
2014-10-02 09:54:12 +07:00
|
|
|
|
2015-11-25 21:58:59 +07:00
|
|
|
static ssize_t bundle_class_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2015-04-01 22:02:04 +07:00
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
|
2015-11-25 21:59:00 +07:00
|
|
|
return sprintf(buf, "0x%02x\n", bundle->class);
|
2015-04-01 22:02:04 +07:00
|
|
|
}
|
2015-11-25 21:58:59 +07:00
|
|
|
static DEVICE_ATTR_RO(bundle_class);
|
2015-04-01 22:02:04 +07:00
|
|
|
|
2015-11-25 21:59:01 +07:00
|
|
|
static ssize_t bundle_id_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%u\n", bundle->id);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(bundle_id);
|
|
|
|
|
2015-04-14 00:48:37 +07:00
|
|
|
static ssize_t state_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
|
2019-03-20 03:03:08 +07:00
|
|
|
if (!bundle->state)
|
2015-04-14 00:48:37 +07:00
|
|
|
return sprintf(buf, "\n");
|
|
|
|
|
|
|
|
return sprintf(buf, "%s\n", bundle->state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t state_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
const char *buf, size_t size)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
|
|
|
|
kfree(bundle->state);
|
2015-04-18 02:41:47 +07:00
|
|
|
bundle->state = kstrdup(buf, GFP_KERNEL);
|
2015-04-14 00:48:37 +07:00
|
|
|
if (!bundle->state)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Tell userspace that the file contents changed */
|
|
|
|
sysfs_notify(&bundle->dev.kobj, NULL, "state");
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RW(state);
|
|
|
|
|
2014-12-13 05:10:17 +07:00
|
|
|
static struct attribute *bundle_attrs[] = {
|
2015-11-25 21:58:59 +07:00
|
|
|
&dev_attr_bundle_class.attr,
|
2015-11-25 21:59:01 +07:00
|
|
|
&dev_attr_bundle_id.attr,
|
2015-04-14 00:48:37 +07:00
|
|
|
&dev_attr_state.attr,
|
2014-10-24 16:34:46 +07:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2014-12-13 05:10:17 +07:00
|
|
|
ATTRIBUTE_GROUPS(bundle);
|
2014-10-24 16:34:46 +07:00
|
|
|
|
2015-12-07 21:05:40 +07:00
|
|
|
static struct gb_bundle *gb_bundle_find(struct gb_interface *intf,
|
2019-02-26 07:32:44 +07:00
|
|
|
u8 bundle_id)
|
2015-12-07 21:05:40 +07:00
|
|
|
{
|
|
|
|
struct gb_bundle *bundle;
|
|
|
|
|
|
|
|
list_for_each_entry(bundle, &intf->bundles, links) {
|
|
|
|
if (bundle->id == bundle_id)
|
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:10:17 +07:00
|
|
|
static void gb_bundle_release(struct device *dev)
|
2014-10-24 16:34:46 +07:00
|
|
|
{
|
2014-12-13 05:10:17 +07:00
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
2014-10-24 16:34:46 +07:00
|
|
|
|
2016-06-04 03:55:36 +07:00
|
|
|
trace_gb_bundle_release(bundle);
|
|
|
|
|
2015-04-14 00:48:37 +07:00
|
|
|
kfree(bundle->state);
|
2016-01-21 23:34:09 +07:00
|
|
|
kfree(bundle->cport_desc);
|
2014-12-13 05:10:17 +07:00
|
|
|
kfree(bundle);
|
2014-10-24 16:34:46 +07:00
|
|
|
}
|
|
|
|
|
2016-09-09 14:47:01 +07:00
|
|
|
#ifdef CONFIG_PM
|
2016-07-15 03:13:00 +07:00
|
|
|
static void gb_bundle_disable_all_connections(struct gb_bundle *bundle)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
|
|
|
|
list_for_each_entry(connection, &bundle->connections, bundle_links)
|
|
|
|
gb_connection_disable(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_bundle_enable_all_connections(struct gb_bundle *bundle)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
|
|
|
|
list_for_each_entry(connection, &bundle->connections, bundle_links)
|
|
|
|
gb_connection_enable(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_bundle_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
const struct dev_pm_ops *pm = dev->driver->pm;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (pm && pm->runtime_suspend) {
|
|
|
|
ret = pm->runtime_suspend(&bundle->dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
gb_bundle_disable_all_connections(bundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = gb_control_bundle_suspend(bundle->intf->control, bundle->id);
|
|
|
|
if (ret) {
|
|
|
|
if (pm && pm->runtime_resume)
|
|
|
|
ret = pm->runtime_resume(dev);
|
|
|
|
else
|
|
|
|
gb_bundle_enable_all_connections(bundle);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_bundle_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
const struct dev_pm_ops *pm = dev->driver->pm;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gb_control_bundle_resume(bundle->intf->control, bundle->id);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (pm && pm->runtime_resume) {
|
|
|
|
ret = pm->runtime_resume(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
gb_bundle_enable_all_connections(bundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gb_bundle_idle(struct device *dev)
|
|
|
|
{
|
|
|
|
pm_runtime_mark_last_busy(dev);
|
|
|
|
pm_request_autosuspend(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct dev_pm_ops gb_bundle_pm_ops = {
|
|
|
|
SET_RUNTIME_PM_OPS(gb_bundle_suspend, gb_bundle_resume, gb_bundle_idle)
|
|
|
|
};
|
|
|
|
|
2014-12-13 05:10:17 +07:00
|
|
|
struct device_type greybus_bundle_type = {
|
|
|
|
.name = "greybus_bundle",
|
|
|
|
.release = gb_bundle_release,
|
2016-07-15 03:13:00 +07:00
|
|
|
.pm = &gb_bundle_pm_ops,
|
2014-10-24 16:34:46 +07:00
|
|
|
};
|
|
|
|
|
2014-10-02 09:54:12 +07:00
|
|
|
/*
|
2014-12-13 05:10:17 +07:00
|
|
|
* Create a gb_bundle structure to represent a discovered
|
|
|
|
* bundle. Returns a pointer to the new bundle or a null
|
2014-10-02 09:54:12 +07:00
|
|
|
* pointer if a failure occurs due to memory exhaustion.
|
|
|
|
*/
|
2015-04-01 22:02:00 +07:00
|
|
|
struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id,
|
2015-04-06 17:19:36 +07:00
|
|
|
u8 class)
|
2014-10-02 09:54:12 +07:00
|
|
|
{
|
2014-12-13 05:10:17 +07:00
|
|
|
struct gb_bundle *bundle;
|
2014-10-02 09:54:12 +07:00
|
|
|
|
2016-06-04 03:55:30 +07:00
|
|
|
if (bundle_id == BUNDLE_ID_NONE) {
|
|
|
|
dev_err(&intf->dev, "can't use bundle id %u\n", bundle_id);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-10 05:42:57 +07:00
|
|
|
/*
|
|
|
|
* Reject any attempt to reuse a bundle id. We initialize
|
|
|
|
* these serially, so there's no need to worry about keeping
|
|
|
|
* the interface bundle list locked here.
|
|
|
|
*/
|
|
|
|
if (gb_bundle_find(intf, bundle_id)) {
|
2015-12-07 21:05:41 +07:00
|
|
|
dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id);
|
2015-06-10 05:42:57 +07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-13 05:10:17 +07:00
|
|
|
bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
|
|
|
|
if (!bundle)
|
2014-10-02 09:54:12 +07:00
|
|
|
return NULL;
|
|
|
|
|
2014-12-20 05:56:31 +07:00
|
|
|
bundle->intf = intf;
|
2015-04-01 22:02:00 +07:00
|
|
|
bundle->id = bundle_id;
|
2015-04-06 17:19:36 +07:00
|
|
|
bundle->class = class;
|
2014-12-13 05:10:17 +07:00
|
|
|
INIT_LIST_HEAD(&bundle->connections);
|
2014-10-02 09:54:12 +07:00
|
|
|
|
2014-12-20 05:56:31 +07:00
|
|
|
bundle->dev.parent = &intf->dev;
|
2014-12-13 05:10:17 +07:00
|
|
|
bundle->dev.bus = &greybus_bus_type;
|
|
|
|
bundle->dev.type = &greybus_bundle_type;
|
|
|
|
bundle->dev.groups = bundle_groups;
|
2016-04-25 23:20:36 +07:00
|
|
|
bundle->dev.dma_mask = intf->dev.dma_mask;
|
2014-12-13 05:10:17 +07:00
|
|
|
device_initialize(&bundle->dev);
|
2015-11-25 21:59:05 +07:00
|
|
|
dev_set_name(&bundle->dev, "%s.%d", dev_name(&intf->dev), bundle_id);
|
2014-12-13 05:10:17 +07:00
|
|
|
|
2015-06-04 19:46:45 +07:00
|
|
|
list_add(&bundle->links, &intf->bundles);
|
2014-10-02 09:54:12 +07:00
|
|
|
|
2016-06-04 03:55:36 +07:00
|
|
|
trace_gb_bundle_create(bundle);
|
|
|
|
|
2014-12-13 05:10:17 +07:00
|
|
|
return bundle;
|
2014-10-02 09:54:12 +07:00
|
|
|
}
|
|
|
|
|
2015-12-07 21:05:43 +07:00
|
|
|
int gb_bundle_add(struct gb_bundle *bundle)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = device_add(&bundle->dev);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&bundle->dev, "failed to register bundle: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-04 03:55:36 +07:00
|
|
|
trace_gb_bundle_add(bundle);
|
|
|
|
|
2015-12-07 21:05:43 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-02 09:54:12 +07:00
|
|
|
/*
|
2014-12-13 05:10:17 +07:00
|
|
|
* Tear down a previously set up bundle.
|
2014-10-02 09:54:12 +07:00
|
|
|
*/
|
2015-06-12 22:21:11 +07:00
|
|
|
void gb_bundle_destroy(struct gb_bundle *bundle)
|
2014-10-02 09:54:12 +07:00
|
|
|
{
|
2016-06-04 03:55:36 +07:00
|
|
|
trace_gb_bundle_destroy(bundle);
|
|
|
|
|
2015-12-07 21:05:43 +07:00
|
|
|
if (device_is_registered(&bundle->dev))
|
|
|
|
device_del(&bundle->dev);
|
2014-10-22 09:43:30 +07:00
|
|
|
|
2015-12-07 21:05:43 +07:00
|
|
|
list_del(&bundle->links);
|
|
|
|
|
|
|
|
put_device(&bundle->dev);
|
|
|
|
}
|