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