2014-10-02 04:54:12 +02:00
|
|
|
/*
|
2014-12-12 23:10:17 +01:00
|
|
|
* Greybus bundles
|
2014-10-02 04:54:12 +02:00
|
|
|
*
|
2015-04-13 19:48:37 +02:00
|
|
|
* Copyright 2014-2015 Google Inc.
|
|
|
|
* Copyright 2014-2015 Linaro Ltd.
|
2014-10-02 04:54:12 +02:00
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
2015-11-25 15:58:59 +01:00
|
|
|
static ssize_t bundle_class_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
2015-04-01 17:02:04 +02:00
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
|
2015-11-25 15:59:00 +01:00
|
|
|
return sprintf(buf, "0x%02x\n", bundle->class);
|
2015-04-01 17:02:04 +02:00
|
|
|
}
|
2015-11-25 15:58:59 +01:00
|
|
|
static DEVICE_ATTR_RO(bundle_class);
|
2015-04-01 17:02:04 +02:00
|
|
|
|
2015-11-25 15:59:01 +01: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-13 19:48:37 +02:00
|
|
|
static ssize_t state_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
|
|
|
|
|
|
|
if (bundle->state == NULL)
|
|
|
|
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-17 21:41:47 +02:00
|
|
|
bundle->state = kstrdup(buf, GFP_KERNEL);
|
2015-04-13 19:48:37 +02: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-12 23:10:17 +01:00
|
|
|
static struct attribute *bundle_attrs[] = {
|
2015-11-25 15:58:59 +01:00
|
|
|
&dev_attr_bundle_class.attr,
|
2015-11-25 15:59:01 +01:00
|
|
|
&dev_attr_bundle_id.attr,
|
2015-04-13 19:48:37 +02:00
|
|
|
&dev_attr_state.attr,
|
2014-10-24 11:34:46 +02:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
ATTRIBUTE_GROUPS(bundle);
|
2014-10-24 11:34:46 +02:00
|
|
|
|
2015-12-07 15:05:40 +01:00
|
|
|
static struct gb_bundle *gb_bundle_find(struct gb_interface *intf,
|
|
|
|
u8 bundle_id)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle;
|
|
|
|
|
|
|
|
list_for_each_entry(bundle, &intf->bundles, links) {
|
|
|
|
if (bundle->id == bundle_id)
|
|
|
|
return bundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
static void gb_bundle_release(struct device *dev)
|
2014-10-24 11:34:46 +02:00
|
|
|
{
|
2014-12-12 23:10:17 +01:00
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
2014-10-24 11:34:46 +02:00
|
|
|
|
2015-04-13 19:48:37 +02:00
|
|
|
kfree(bundle->state);
|
2014-12-12 23:10:17 +01:00
|
|
|
kfree(bundle);
|
2014-10-24 11:34:46 +02:00
|
|
|
}
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
struct device_type greybus_bundle_type = {
|
|
|
|
.name = "greybus_bundle",
|
|
|
|
.release = gb_bundle_release,
|
2014-10-24 11:34:46 +02:00
|
|
|
};
|
|
|
|
|
2014-10-02 04:54:12 +02:00
|
|
|
/*
|
2014-12-12 23:10:17 +01:00
|
|
|
* Create a gb_bundle structure to represent a discovered
|
|
|
|
* bundle. Returns a pointer to the new bundle or a null
|
2014-10-02 04:54:12 +02:00
|
|
|
* pointer if a failure occurs due to memory exhaustion.
|
|
|
|
*/
|
2015-04-01 17:02:00 +02:00
|
|
|
struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 bundle_id,
|
2015-04-06 12:19:36 +02:00
|
|
|
u8 class)
|
2014-10-02 04:54:12 +02:00
|
|
|
{
|
2014-12-12 23:10:17 +01:00
|
|
|
struct gb_bundle *bundle;
|
2014-10-24 11:34:46 +02:00
|
|
|
int retval;
|
2014-10-02 04:54:12 +02:00
|
|
|
|
2015-06-10 00:42:57 +02: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 15:05:41 +01:00
|
|
|
dev_err(&intf->dev, "duplicate bundle id %u\n", bundle_id);
|
2015-06-10 00:42:57 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
|
|
|
|
if (!bundle)
|
2014-10-02 04:54:12 +02:00
|
|
|
return NULL;
|
|
|
|
|
2014-12-19 23:56:31 +01:00
|
|
|
bundle->intf = intf;
|
2015-04-01 17:02:00 +02:00
|
|
|
bundle->id = bundle_id;
|
2015-04-06 12:19:36 +02:00
|
|
|
bundle->class = class;
|
2014-12-12 23:10:17 +01:00
|
|
|
INIT_LIST_HEAD(&bundle->connections);
|
2014-10-02 04:54:12 +02:00
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
/* Build up the bundle device structures and register it with the
|
2014-10-24 11:34:46 +02:00
|
|
|
* driver core */
|
2014-12-19 23:56:31 +01:00
|
|
|
bundle->dev.parent = &intf->dev;
|
2014-12-12 23:10:17 +01:00
|
|
|
bundle->dev.bus = &greybus_bus_type;
|
|
|
|
bundle->dev.type = &greybus_bundle_type;
|
|
|
|
bundle->dev.groups = bundle_groups;
|
|
|
|
device_initialize(&bundle->dev);
|
2015-11-25 15:59:05 +01:00
|
|
|
dev_set_name(&bundle->dev, "%s.%d", dev_name(&intf->dev), bundle_id);
|
2014-12-12 23:10:17 +01:00
|
|
|
|
|
|
|
retval = device_add(&bundle->dev);
|
2014-10-24 11:34:46 +02:00
|
|
|
if (retval) {
|
2015-12-07 15:05:38 +01:00
|
|
|
pr_err("failed to add bundle device for id %u\n", bundle_id);
|
2014-12-12 23:10:17 +01:00
|
|
|
put_device(&bundle->dev);
|
2014-10-24 11:34:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-04 14:46:45 +02:00
|
|
|
list_add(&bundle->links, &intf->bundles);
|
2014-10-02 04:54:12 +02:00
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
return bundle;
|
2014-10-02 04:54:12 +02:00
|
|
|
}
|
|
|
|
|
2015-08-31 13:51:08 +02:00
|
|
|
static void gb_bundle_connections_exit(struct gb_bundle *bundle)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
struct gb_connection *next;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(connection, next, &bundle->connections,
|
2015-08-31 13:51:13 +02:00
|
|
|
bundle_links)
|
2015-08-31 13:51:08 +02:00
|
|
|
gb_connection_destroy(connection);
|
|
|
|
}
|
|
|
|
|
2014-10-02 04:54:12 +02:00
|
|
|
/*
|
2014-12-12 23:10:17 +01:00
|
|
|
* Tear down a previously set up bundle.
|
2014-10-02 04:54:12 +02:00
|
|
|
*/
|
2015-06-12 17:21:11 +02:00
|
|
|
void gb_bundle_destroy(struct gb_bundle *bundle)
|
2014-10-02 04:54:12 +02:00
|
|
|
{
|
2015-06-12 17:21:11 +02:00
|
|
|
list_del(&bundle->links);
|
2015-02-13 04:28:09 +01:00
|
|
|
|
2015-06-12 17:21:11 +02:00
|
|
|
gb_bundle_connections_exit(bundle);
|
|
|
|
device_unregister(&bundle->dev);
|
2014-10-02 04:54:12 +02:00
|
|
|
}
|
2014-10-16 13:35:35 +02:00
|
|
|
|
2014-10-22 04:43:30 +02:00
|
|
|
|