2014-10-01 20:54:12 -06:00
|
|
|
/*
|
2014-12-12 15:10:17 -07:00
|
|
|
* Greybus bundles
|
2014-10-01 20:54:12 -06:00
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
2014-12-12 11:08:42 -07:00
|
|
|
* Copyright 2014 Linaro Ltd.
|
2014-10-01 20:54:12 -06:00
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
static void gb_bundle_connections_exit(struct gb_bundle *bundle);
|
|
|
|
static int gb_bundle_connections_init(struct gb_bundle *bundle);
|
|
|
|
|
|
|
|
|
2014-10-24 03:34:46 -06:00
|
|
|
static ssize_t device_id_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2014-12-12 15:10:17 -07:00
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
2014-10-24 03:34:46 -06:00
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
return sprintf(buf, "%d", bundle->device_id);
|
2014-10-24 03:34:46 -06:00
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(device_id);
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
static struct attribute *bundle_attrs[] = {
|
2014-10-24 03:34:46 -06:00
|
|
|
&dev_attr_device_id.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
ATTRIBUTE_GROUPS(bundle);
|
2014-10-24 03:34:46 -06:00
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
static void gb_bundle_release(struct device *dev)
|
2014-10-24 03:34:46 -06:00
|
|
|
{
|
2014-12-12 15:10:17 -07:00
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
2014-10-24 03:34:46 -06:00
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
kfree(bundle);
|
2014-10-24 03:34:46 -06:00
|
|
|
}
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
struct device_type greybus_bundle_type = {
|
|
|
|
.name = "greybus_bundle",
|
|
|
|
.release = gb_bundle_release,
|
2014-10-24 03:34:46 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-10-01 20:54:12 -06:00
|
|
|
/* XXX This could be per-host device or per-module */
|
2014-12-12 15:10:17 -07:00
|
|
|
static DEFINE_SPINLOCK(gb_bundles_lock);
|
2014-10-01 20:54:12 -06:00
|
|
|
|
2014-12-23 16:16:53 -07:00
|
|
|
static int __bundle_bind_protocols(struct device *dev, void *data)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle;
|
|
|
|
struct gb_connection *connection;
|
|
|
|
|
|
|
|
if (!is_gb_bundle(dev))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bundle = to_gb_bundle(dev);
|
|
|
|
|
|
|
|
list_for_each_entry(connection, &bundle->connections, bundle_links) {
|
|
|
|
gb_connection_bind_protocol(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Walk all bundles in the system, and see if any connections are not bound to a
|
|
|
|
* specific prototcol. If they are not, then try to find one for it and bind it
|
|
|
|
* to it.
|
|
|
|
*
|
|
|
|
* This is called after registering a new protocol.
|
|
|
|
*/
|
|
|
|
void gb_bundle_bind_protocols(void)
|
|
|
|
{
|
|
|
|
bus_for_each_dev(&greybus_bus_type, NULL, NULL,
|
|
|
|
__bundle_bind_protocols);
|
|
|
|
}
|
|
|
|
|
2014-10-01 20:54:12 -06:00
|
|
|
/*
|
2014-12-12 15: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-01 20:54:12 -06:00
|
|
|
* pointer if a failure occurs due to memory exhaustion.
|
|
|
|
*/
|
2014-12-19 15:56:31 -07:00
|
|
|
struct gb_bundle *gb_bundle_create(struct gb_interface *intf, u8 interface_id)
|
2014-10-01 20:54:12 -06:00
|
|
|
{
|
2014-12-12 15:10:17 -07:00
|
|
|
struct gb_bundle *bundle;
|
2014-10-24 03:34:46 -06:00
|
|
|
int retval;
|
2014-10-01 20:54:12 -06:00
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
bundle = kzalloc(sizeof(*bundle), GFP_KERNEL);
|
|
|
|
if (!bundle)
|
2014-10-01 20:54:12 -06:00
|
|
|
return NULL;
|
|
|
|
|
2014-12-19 15:56:31 -07:00
|
|
|
bundle->intf = intf;
|
2014-12-12 15:10:17 -07:00
|
|
|
bundle->id = interface_id;
|
|
|
|
INIT_LIST_HEAD(&bundle->connections);
|
2014-10-01 20:54:12 -06:00
|
|
|
|
2014-12-24 14:01:39 -07:00
|
|
|
/* Invalid device id to start with */
|
|
|
|
bundle->device_id = GB_DEVICE_ID_BAD;
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
/* Build up the bundle device structures and register it with the
|
2014-10-24 03:34:46 -06:00
|
|
|
* driver core */
|
2014-12-19 15:56:31 -07:00
|
|
|
bundle->dev.parent = &intf->dev;
|
2014-12-12 15:10:17 -07:00
|
|
|
bundle->dev.bus = &greybus_bus_type;
|
|
|
|
bundle->dev.type = &greybus_bundle_type;
|
|
|
|
bundle->dev.groups = bundle_groups;
|
|
|
|
device_initialize(&bundle->dev);
|
2014-12-21 15:10:26 -07:00
|
|
|
dev_set_name(&bundle->dev, "%s:%d", dev_name(&intf->dev), interface_id);
|
2014-12-12 15:10:17 -07:00
|
|
|
|
|
|
|
retval = device_add(&bundle->dev);
|
2014-10-24 03:34:46 -06:00
|
|
|
if (retval) {
|
2014-12-12 15:10:17 -07:00
|
|
|
pr_err("failed to add bundle device for id 0x%02hhx\n",
|
2014-11-05 15:03:12 -07:00
|
|
|
interface_id);
|
2014-12-12 15:10:17 -07:00
|
|
|
put_device(&bundle->dev);
|
|
|
|
kfree(bundle);
|
2014-10-24 03:34:46 -06:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
spin_lock_irq(&gb_bundles_lock);
|
2014-12-19 15:56:31 -07:00
|
|
|
list_add_tail(&bundle->links, &intf->bundles);
|
2014-12-12 15:10:17 -07:00
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
2014-10-01 20:54:12 -06:00
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
return bundle;
|
2014-10-01 20:54:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-12-12 15:10:17 -07:00
|
|
|
* Tear down a previously set up bundle.
|
2014-10-01 20:54:12 -06:00
|
|
|
*/
|
2014-12-19 15:56:31 -07:00
|
|
|
void gb_bundle_destroy(struct gb_interface *intf)
|
2014-10-01 20:54:12 -06:00
|
|
|
{
|
2014-12-12 15:10:17 -07:00
|
|
|
struct gb_bundle *bundle;
|
|
|
|
struct gb_bundle *temp;
|
2014-10-24 03:34:46 -06:00
|
|
|
|
2014-12-19 15:56:31 -07:00
|
|
|
if (WARN_ON(!intf))
|
2014-10-01 20:54:12 -06:00
|
|
|
return;
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
spin_lock_irq(&gb_bundles_lock);
|
2014-12-19 15:56:31 -07:00
|
|
|
list_for_each_entry_safe(bundle, temp, &intf->bundles, links) {
|
2014-12-12 15:10:17 -07:00
|
|
|
list_del(&bundle->links);
|
|
|
|
gb_bundle_connections_exit(bundle);
|
|
|
|
device_del(&bundle->dev);
|
2014-10-24 03:34:46 -06:00
|
|
|
}
|
2014-12-12 15:10:17 -07:00
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
2014-10-01 20:54:12 -06:00
|
|
|
}
|
2014-10-16 05:35:35 -06:00
|
|
|
|
2014-12-19 15:56:31 -07:00
|
|
|
int gb_bundle_init(struct gb_interface *intf, u8 bundle_id, u8 device_id)
|
2014-11-14 04:55:08 -07:00
|
|
|
{
|
2014-12-12 15:10:17 -07:00
|
|
|
struct gb_bundle *bundle;
|
2014-11-14 04:55:08 -07:00
|
|
|
int ret;
|
|
|
|
|
2014-12-19 15:56:31 -07:00
|
|
|
bundle = gb_bundle_find(intf, bundle_id);
|
2014-12-12 15:10:17 -07:00
|
|
|
if (!bundle) {
|
2014-12-19 15:56:31 -07:00
|
|
|
dev_err(intf->hd->parent, "bundle %hhu not found\n", bundle_id);
|
2014-11-14 04:55:08 -07:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2014-12-12 15:10:17 -07:00
|
|
|
bundle->device_id = device_id;
|
2014-11-14 04:55:08 -07:00
|
|
|
|
2014-12-19 15:56:31 -07:00
|
|
|
ret = svc_set_route_send(bundle, intf->hd);
|
2014-11-14 04:55:08 -07:00
|
|
|
if (ret) {
|
2014-12-19 15:56:31 -07:00
|
|
|
dev_err(intf->hd->parent, "failed to set route (%d)\n", ret);
|
2014-11-14 04:55:08 -07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
ret = gb_bundle_connections_init(bundle);
|
2014-11-14 04:55:08 -07:00
|
|
|
if (ret) {
|
2014-12-19 15:56:31 -07:00
|
|
|
dev_err(intf->hd->parent, "interface bundle init error %d\n",
|
2014-11-14 04:55:08 -07:00
|
|
|
ret);
|
|
|
|
/* XXX clear route */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-19 15:56:31 -07:00
|
|
|
struct gb_bundle *gb_bundle_find(struct gb_interface *intf, u8 bundle_id)
|
2014-10-21 20:43:30 -06:00
|
|
|
{
|
2014-12-12 15:10:17 -07:00
|
|
|
struct gb_bundle *bundle;
|
2014-10-21 20:43:30 -06:00
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
spin_lock_irq(&gb_bundles_lock);
|
2014-12-19 15:56:31 -07:00
|
|
|
list_for_each_entry(bundle, &intf->bundles, links)
|
2014-12-12 15:10:17 -07:00
|
|
|
if (bundle->id == bundle_id) {
|
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
|
|
|
return bundle;
|
2014-10-24 03:34:46 -06:00
|
|
|
}
|
2014-12-12 15:10:17 -07:00
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
2014-10-21 20:43:30 -06:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
static int gb_bundle_connections_init(struct gb_bundle *bundle)
|
2014-10-16 05:35:35 -06:00
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
int ret = 0;
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
list_for_each_entry(connection, &bundle->connections, bundle_links) {
|
2014-10-16 05:35:35 -06:00
|
|
|
ret = gb_connection_init(connection);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-10-20 22:01:04 -06:00
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
static void gb_bundle_connections_exit(struct gb_bundle *bundle)
|
2014-10-20 22:01:04 -06:00
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
struct gb_connection *next;
|
|
|
|
|
2014-12-12 15:10:17 -07:00
|
|
|
list_for_each_entry_safe(connection, next, &bundle->connections,
|
|
|
|
bundle_links) {
|
2014-10-20 22:01:04 -06:00
|
|
|
gb_connection_exit(connection);
|
|
|
|
gb_connection_destroy(connection);
|
|
|
|
}
|
|
|
|
}
|