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
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
2014-12-12 18:08:42 +00:00
|
|
|
* Copyright 2014 Linaro Ltd.
|
2014-10-02 02:54:12 +00:00
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
static void gb_bundle_connections_exit(struct gb_bundle *bundle);
|
|
|
|
static int gb_bundle_connections_init(struct gb_bundle *bundle);
|
|
|
|
|
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
static ssize_t device_id_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2014-12-12 22:10:17 +00:00
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
2014-10-24 09:34:46 +00:00
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
return sprintf(buf, "%d", bundle->device_id);
|
2014-10-24 09:34:46 +00:00
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(device_id);
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
static struct attribute *bundle_attrs[] = {
|
2014-10-24 09:34:46 +00:00
|
|
|
&dev_attr_device_id.attr,
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
*/
|
2014-12-12 22:10:17 +00:00
|
|
|
struct gb_bundle *gb_bundle_create(struct gb_interface_block *gb_ib, u8 interface_id)
|
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
|
|
|
|
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-12 22:10:17 +00:00
|
|
|
bundle->gb_ib = gb_ib;
|
|
|
|
bundle->id = interface_id;
|
|
|
|
bundle->device_id = 0xff; /* Invalid device id to start with */
|
|
|
|
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-12 22:10:17 +00:00
|
|
|
bundle->dev.parent = &gb_ib->dev;
|
|
|
|
bundle->dev.bus = &greybus_bus_type;
|
|
|
|
bundle->dev.type = &greybus_bundle_type;
|
|
|
|
bundle->dev.groups = bundle_groups;
|
|
|
|
device_initialize(&bundle->dev);
|
|
|
|
dev_set_name(&bundle->dev, "%d:%d", gb_ib->module_id, interface_id);
|
|
|
|
|
|
|
|
retval = device_add(&bundle->dev);
|
2014-10-24 09:34:46 +00:00
|
|
|
if (retval) {
|
2014-12-12 22:10:17 +00:00
|
|
|
pr_err("failed to add bundle device for id 0x%02hhx\n",
|
2014-11-05 22:03:12 +00:00
|
|
|
interface_id);
|
2014-12-12 22:10:17 +00:00
|
|
|
put_device(&bundle->dev);
|
|
|
|
kfree(bundle);
|
2014-10-24 09:34:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
spin_lock_irq(&gb_bundles_lock);
|
2014-12-19 22:56:29 +00:00
|
|
|
list_add_tail(&bundle->links, &gb_ib->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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-12-12 22:10:17 +00:00
|
|
|
* Tear down a previously set up bundle.
|
2014-10-02 02:54:12 +00:00
|
|
|
*/
|
2014-12-12 22:10:17 +00:00
|
|
|
void gb_bundle_destroy(struct gb_interface_block *gb_ib)
|
2014-10-02 02:54:12 +00:00
|
|
|
{
|
2014-12-12 22:10:17 +00:00
|
|
|
struct gb_bundle *bundle;
|
|
|
|
struct gb_bundle *temp;
|
2014-10-24 09:34:46 +00:00
|
|
|
|
2014-12-11 22:10:56 +00:00
|
|
|
if (WARN_ON(!gb_ib))
|
2014-10-02 02:54:12 +00:00
|
|
|
return;
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
spin_lock_irq(&gb_bundles_lock);
|
2014-12-19 22:56:29 +00:00
|
|
|
list_for_each_entry_safe(bundle, temp, &gb_ib->bundles, links) {
|
2014-12-12 22:10:17 +00:00
|
|
|
list_del(&bundle->links);
|
|
|
|
gb_bundle_connections_exit(bundle);
|
|
|
|
device_del(&bundle->dev);
|
2014-10-24 09:34:46 +00:00
|
|
|
}
|
2014-12-12 22:10:17 +00:00
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
2014-10-02 02:54:12 +00:00
|
|
|
}
|
2014-10-16 11:35:35 +00:00
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
int gb_bundle_init(struct gb_interface_block *gb_ib, u8 bundle_id, u8 device_id)
|
2014-11-14 11:55:08 +00:00
|
|
|
{
|
2014-12-12 22:10:17 +00:00
|
|
|
struct gb_bundle *bundle;
|
2014-11-14 11:55:08 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
bundle = gb_bundle_find(gb_ib, bundle_id);
|
|
|
|
if (!bundle) {
|
|
|
|
dev_err(gb_ib->hd->parent, "bundle %hhu not found\n",
|
|
|
|
bundle_id);
|
2014-11-14 11:55:08 +00:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2014-12-12 22:10:17 +00:00
|
|
|
bundle->device_id = device_id;
|
2014-11-14 11:55:08 +00:00
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
ret = svc_set_route_send(bundle, gb_ib->hd);
|
2014-11-14 11:55:08 +00:00
|
|
|
if (ret) {
|
2014-12-11 22:10:56 +00:00
|
|
|
dev_err(gb_ib->hd->parent, "failed to set route (%d)\n", ret);
|
2014-11-14 11:55:08 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
ret = gb_bundle_connections_init(bundle);
|
2014-11-14 11:55:08 +00:00
|
|
|
if (ret) {
|
2014-12-12 22:10:17 +00:00
|
|
|
dev_err(gb_ib->hd->parent, "interface bundle init error %d\n",
|
2014-11-14 11:55:08 +00:00
|
|
|
ret);
|
|
|
|
/* XXX clear route */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
struct gb_bundle *gb_bundle_find(struct gb_interface_block *gb_ib, 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:29 +00:00
|
|
|
list_for_each_entry(bundle, &gb_ib->bundles, links)
|
2014-12-12 22:10:17 +00:00
|
|
|
if (bundle->id == bundle_id) {
|
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
|
|
|
return bundle;
|
2014-10-24 09:34:46 +00:00
|
|
|
}
|
2014-12-12 22:10:17 +00:00
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
2014-10-22 02:43:30 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
static int gb_bundle_connections_init(struct gb_bundle *bundle)
|
2014-10-16 11:35:35 +00:00
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
int ret = 0;
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
list_for_each_entry(connection, &bundle->connections, bundle_links) {
|
2014-10-16 11:35:35 +00:00
|
|
|
ret = gb_connection_init(connection);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-10-21 04:01:04 +00:00
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
static void gb_bundle_connections_exit(struct gb_bundle *bundle)
|
2014-10-21 04:01:04 +00:00
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
struct gb_connection *next;
|
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
list_for_each_entry_safe(connection, next, &bundle->connections,
|
|
|
|
bundle_links) {
|
2014-10-21 04:01:04 +00:00
|
|
|
gb_connection_exit(connection);
|
|
|
|
gb_connection_destroy(connection);
|
|
|
|
}
|
|
|
|
}
|