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
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
2014-12-12 19:08:42 +01:00
|
|
|
* Copyright 2014 Linaro Ltd.
|
2014-10-02 04:54:12 +02:00
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
static void gb_bundle_connections_exit(struct gb_bundle *bundle);
|
|
|
|
static int gb_bundle_connections_init(struct gb_bundle *bundle);
|
|
|
|
|
|
|
|
|
2014-10-24 11:34:46 +02:00
|
|
|
static ssize_t device_id_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2014-12-12 23:10:17 +01:00
|
|
|
struct gb_bundle *bundle = to_gb_bundle(dev);
|
2014-10-24 11:34:46 +02:00
|
|
|
|
2014-12-24 01:09:26 +01:00
|
|
|
return sprintf(buf, "%d\n", bundle->device_id);
|
2014-10-24 11:34:46 +02:00
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(device_id);
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
static struct attribute *bundle_attrs[] = {
|
2014-10-24 11:34:46 +02:00
|
|
|
&dev_attr_device_id.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
ATTRIBUTE_GROUPS(bundle);
|
2014-10-24 11:34:46 +02:00
|
|
|
|
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
|
|
|
|
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
|
|
|
/* XXX This could be per-host device or per-module */
|
2014-12-12 23:10:17 +01:00
|
|
|
static DEFINE_SPINLOCK(gb_bundles_lock);
|
2014-10-02 04:54:12 +02:00
|
|
|
|
2014-12-24 00:16:53 +01: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-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,
|
|
|
|
u8 class_type)
|
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
|
|
|
|
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;
|
|
|
|
bundle->class_type = class_type;
|
2014-12-12 23:10:17 +01:00
|
|
|
INIT_LIST_HEAD(&bundle->connections);
|
2014-10-02 04:54:12 +02:00
|
|
|
|
2014-12-24 22:01:39 +01:00
|
|
|
/* Invalid device id to start with */
|
|
|
|
bundle->device_id = GB_DEVICE_ID_BAD;
|
|
|
|
|
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-04-01 17:02:00 +02: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) {
|
2014-12-12 23:10:17 +01:00
|
|
|
pr_err("failed to add bundle device for id 0x%02hhx\n",
|
2015-04-01 17:02:00 +02:00
|
|
|
bundle_id);
|
2014-12-12 23:10:17 +01:00
|
|
|
put_device(&bundle->dev);
|
|
|
|
kfree(bundle);
|
2014-10-24 11:34:46 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
spin_lock_irq(&gb_bundles_lock);
|
2014-12-19 23:56:31 +01:00
|
|
|
list_add_tail(&bundle->links, &intf->bundles);
|
2014-12-12 23:10:17 +01:00
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-12-12 23:10:17 +01:00
|
|
|
* Tear down a previously set up bundle.
|
2014-10-02 04:54:12 +02:00
|
|
|
*/
|
2014-12-19 23:56:31 +01:00
|
|
|
void gb_bundle_destroy(struct gb_interface *intf)
|
2014-10-02 04:54:12 +02:00
|
|
|
{
|
2015-02-13 04:28:09 +01:00
|
|
|
LIST_HEAD(list);
|
2014-12-12 23:10:17 +01:00
|
|
|
struct gb_bundle *bundle;
|
|
|
|
struct gb_bundle *temp;
|
2014-10-24 11:34:46 +02:00
|
|
|
|
2014-12-19 23:56:31 +01:00
|
|
|
if (WARN_ON(!intf))
|
2014-10-02 04:54:12 +02:00
|
|
|
return;
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
spin_lock_irq(&gb_bundles_lock);
|
2015-02-13 04:28:09 +01:00
|
|
|
list_splice_init(&intf->bundles, &list);
|
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
|
|
|
|
|
|
|
list_for_each_entry_safe(bundle, temp, &list, links) {
|
2014-12-12 23:10:17 +01:00
|
|
|
list_del(&bundle->links);
|
|
|
|
gb_bundle_connections_exit(bundle);
|
2015-04-02 14:23:47 +02:00
|
|
|
device_unregister(&bundle->dev);
|
2014-10-24 11:34:46 +02:00
|
|
|
}
|
2014-10-02 04:54:12 +02:00
|
|
|
}
|
2014-10-16 13:35:35 +02:00
|
|
|
|
2014-12-19 23:56:31 +01:00
|
|
|
int gb_bundle_init(struct gb_interface *intf, u8 bundle_id, u8 device_id)
|
2014-11-14 12:55:08 +01:00
|
|
|
{
|
2014-12-12 23:10:17 +01:00
|
|
|
struct gb_bundle *bundle;
|
2014-11-14 12:55:08 +01:00
|
|
|
int ret;
|
|
|
|
|
2014-12-19 23:56:31 +01:00
|
|
|
bundle = gb_bundle_find(intf, bundle_id);
|
2014-12-12 23:10:17 +01:00
|
|
|
if (!bundle) {
|
2014-12-19 23:56:31 +01:00
|
|
|
dev_err(intf->hd->parent, "bundle %hhu not found\n", bundle_id);
|
2014-11-14 12:55:08 +01:00
|
|
|
return -ENOENT;
|
|
|
|
}
|
2014-12-12 23:10:17 +01:00
|
|
|
bundle->device_id = device_id;
|
2014-11-14 12:55:08 +01:00
|
|
|
|
2014-12-19 23:56:31 +01:00
|
|
|
ret = svc_set_route_send(bundle, intf->hd);
|
2014-11-14 12:55:08 +01:00
|
|
|
if (ret) {
|
2014-12-19 23:56:31 +01:00
|
|
|
dev_err(intf->hd->parent, "failed to set route (%d)\n", ret);
|
2014-11-14 12:55:08 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
ret = gb_bundle_connections_init(bundle);
|
2014-11-14 12:55:08 +01:00
|
|
|
if (ret) {
|
2014-12-19 23:56:31 +01:00
|
|
|
dev_err(intf->hd->parent, "interface bundle init error %d\n",
|
2014-11-14 12:55:08 +01:00
|
|
|
ret);
|
|
|
|
/* XXX clear route */
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-19 23:56:31 +01:00
|
|
|
struct gb_bundle *gb_bundle_find(struct gb_interface *intf, u8 bundle_id)
|
2014-10-22 04:43:30 +02:00
|
|
|
{
|
2014-12-12 23:10:17 +01:00
|
|
|
struct gb_bundle *bundle;
|
2014-10-22 04:43:30 +02:00
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
spin_lock_irq(&gb_bundles_lock);
|
2014-12-19 23:56:31 +01:00
|
|
|
list_for_each_entry(bundle, &intf->bundles, links)
|
2014-12-12 23:10:17 +01:00
|
|
|
if (bundle->id == bundle_id) {
|
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
|
|
|
return bundle;
|
2014-10-24 11:34:46 +02:00
|
|
|
}
|
2014-12-12 23:10:17 +01:00
|
|
|
spin_unlock_irq(&gb_bundles_lock);
|
2014-10-22 04:43:30 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
static int gb_bundle_connections_init(struct gb_bundle *bundle)
|
2014-10-16 13:35:35 +02:00
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
int ret = 0;
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
list_for_each_entry(connection, &bundle->connections, bundle_links) {
|
2014-10-16 13:35:35 +02:00
|
|
|
ret = gb_connection_init(connection);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2014-10-21 06:01:04 +02:00
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
static void gb_bundle_connections_exit(struct gb_bundle *bundle)
|
2014-10-21 06:01:04 +02:00
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
struct gb_connection *next;
|
|
|
|
|
2014-12-12 23:10:17 +01:00
|
|
|
list_for_each_entry_safe(connection, next, &bundle->connections,
|
|
|
|
bundle_links) {
|
2014-10-21 06:01:04 +02:00
|
|
|
gb_connection_exit(connection);
|
|
|
|
gb_connection_destroy(connection);
|
|
|
|
}
|
|
|
|
}
|