2014-10-02 02:54:14 +00:00
|
|
|
/*
|
|
|
|
* Greybus connections
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
2014-12-12 18:08:42 +00:00
|
|
|
* Copyright 2014 Linaro Ltd.
|
2014-10-02 02:54:14 +00:00
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "greybus.h"
|
|
|
|
|
2014-10-03 19:14:22 +00:00
|
|
|
static DEFINE_SPINLOCK(gb_connections_lock);
|
|
|
|
|
2015-06-08 17:05:11 +00:00
|
|
|
static struct gb_connection *
|
|
|
|
gb_connection_hd_find(struct greybus_host_device *hd, u16 cport_id)
|
2014-10-06 11:53:08 +00:00
|
|
|
{
|
|
|
|
struct gb_connection *connection = NULL;
|
2015-03-02 08:55:26 +00:00
|
|
|
unsigned long flags;
|
2014-10-06 11:53:08 +00:00
|
|
|
|
2015-03-02 08:55:26 +00:00
|
|
|
spin_lock_irqsave(&gb_connections_lock, flags);
|
2014-11-17 14:08:44 +00:00
|
|
|
list_for_each_entry(connection, &hd->connections, hd_links)
|
|
|
|
if (connection->hd_cport_id == cport_id)
|
2014-10-06 17:26:02 +00:00
|
|
|
goto found;
|
|
|
|
connection = NULL;
|
|
|
|
found:
|
2015-03-02 08:55:26 +00:00
|
|
|
spin_unlock_irqrestore(&gb_connections_lock, flags);
|
2014-10-06 11:53:08 +00:00
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
2014-11-20 22:09:18 +00:00
|
|
|
/*
|
|
|
|
* Callback from the host driver to let us know that data has been
|
2014-12-12 22:10:17 +00:00
|
|
|
* received on the bundle.
|
2014-11-20 22:09:18 +00:00
|
|
|
*/
|
|
|
|
void greybus_data_rcvd(struct greybus_host_device *hd, u16 cport_id,
|
2014-11-18 00:08:37 +00:00
|
|
|
u8 *data, size_t length)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
|
|
|
|
2015-05-20 11:18:00 +00:00
|
|
|
connection = gb_connection_hd_find(hd, cport_id);
|
2014-11-18 00:08:37 +00:00
|
|
|
if (!connection) {
|
|
|
|
dev_err(hd->parent,
|
|
|
|
"nonexistent connection (%zu bytes dropped)\n", length);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-18 19:26:50 +00:00
|
|
|
gb_connection_recv(connection, data, length);
|
2014-11-18 00:08:37 +00:00
|
|
|
}
|
2014-11-20 22:09:18 +00:00
|
|
|
EXPORT_SYMBOL_GPL(greybus_data_rcvd);
|
2014-11-18 00:08:37 +00:00
|
|
|
|
2014-10-03 19:14:24 +00:00
|
|
|
/*
|
|
|
|
* Allocate an available CPort Id for use for the host side of the
|
|
|
|
* given connection. The lowest-available id is returned, so the
|
|
|
|
* first call is guaranteed to allocate CPort Id 0.
|
|
|
|
*
|
|
|
|
* Assigns the connection's hd_cport_id and returns true if successful.
|
|
|
|
* Returns false otherwise.
|
|
|
|
*/
|
2014-10-06 11:53:06 +00:00
|
|
|
static bool gb_connection_hd_cport_id_alloc(struct gb_connection *connection)
|
2014-10-03 19:14:24 +00:00
|
|
|
{
|
|
|
|
struct ida *ida = &connection->hd->cport_id_map;
|
|
|
|
int id;
|
|
|
|
|
2015-02-12 03:22:47 +00:00
|
|
|
id = ida_simple_get(ida, 0, HOST_DEV_CPORT_ID_MAX, GFP_ATOMIC);
|
2014-10-03 19:14:24 +00:00
|
|
|
if (id < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
connection->hd_cport_id = (u16)id;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free a previously-allocated CPort Id on the given host device.
|
|
|
|
*/
|
2014-10-06 11:53:06 +00:00
|
|
|
static void gb_connection_hd_cport_id_free(struct gb_connection *connection)
|
2014-10-03 19:14:24 +00:00
|
|
|
{
|
|
|
|
struct ida *ida = &connection->hd->cport_id_map;
|
|
|
|
|
|
|
|
ida_simple_remove(ida, connection->hd_cport_id);
|
|
|
|
connection->hd_cport_id = CPORT_ID_BAD;
|
|
|
|
}
|
|
|
|
|
2014-10-24 09:34:46 +00:00
|
|
|
static ssize_t state_show(struct device *dev, struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection = to_gb_connection(dev);
|
|
|
|
|
2014-12-24 00:09:26 +00:00
|
|
|
return sprintf(buf, "%d\n", connection->state);
|
2014-10-24 09:34:46 +00:00
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(state);
|
|
|
|
|
2014-10-29 00:35:59 +00:00
|
|
|
static ssize_t
|
|
|
|
protocol_id_show(struct device *dev, struct device_attribute *attr, char *buf)
|
2014-10-24 09:34:46 +00:00
|
|
|
{
|
|
|
|
struct gb_connection *connection = to_gb_connection(dev);
|
|
|
|
|
2014-12-24 00:09:26 +00:00
|
|
|
return sprintf(buf, "%d\n", connection->protocol->id);
|
2014-10-24 09:34:46 +00:00
|
|
|
}
|
2014-10-29 00:35:59 +00:00
|
|
|
static DEVICE_ATTR_RO(protocol_id);
|
2014-10-24 09:34:46 +00:00
|
|
|
|
|
|
|
static struct attribute *connection_attrs[] = {
|
|
|
|
&dev_attr_state.attr,
|
2014-10-29 00:35:59 +00:00
|
|
|
&dev_attr_protocol_id.attr,
|
2014-10-24 09:34:46 +00:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
ATTRIBUTE_GROUPS(connection);
|
|
|
|
|
|
|
|
static void gb_connection_release(struct device *dev)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection = to_gb_connection(dev);
|
|
|
|
|
|
|
|
kfree(connection);
|
|
|
|
}
|
|
|
|
|
2014-11-15 20:12:16 +00:00
|
|
|
struct device_type greybus_connection_type = {
|
2014-10-24 09:34:46 +00:00
|
|
|
.name = "greybus_connection",
|
|
|
|
.release = gb_connection_release,
|
|
|
|
};
|
|
|
|
|
2014-12-23 23:16:53 +00:00
|
|
|
|
|
|
|
void gb_connection_bind_protocol(struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
struct gb_bundle *bundle;
|
|
|
|
struct gb_protocol *protocol;
|
|
|
|
|
|
|
|
/* If we already have a protocol bound here, just return */
|
|
|
|
if (connection->protocol)
|
|
|
|
return;
|
|
|
|
|
|
|
|
protocol = gb_protocol_get(connection->protocol_id,
|
|
|
|
connection->major,
|
|
|
|
connection->minor);
|
|
|
|
if (!protocol)
|
|
|
|
return;
|
|
|
|
connection->protocol = protocol;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have a valid device_id for the bundle, then we have an active
|
|
|
|
* device, so bring up the connection at the same time.
|
|
|
|
* */
|
|
|
|
bundle = connection->bundle;
|
2014-12-24 21:01:39 +00:00
|
|
|
if (bundle->device_id != GB_DEVICE_ID_BAD)
|
2014-12-23 23:16:53 +00:00
|
|
|
gb_connection_init(connection);
|
|
|
|
}
|
|
|
|
|
2014-10-02 02:54:14 +00:00
|
|
|
/*
|
|
|
|
* Set up a Greybus connection, representing the bidirectional link
|
|
|
|
* between a CPort on a (local) Greybus host device and a CPort on
|
|
|
|
* another Greybus module.
|
|
|
|
*
|
2014-10-02 02:54:15 +00:00
|
|
|
* A connection also maintains the state of operations sent over the
|
|
|
|
* connection.
|
|
|
|
*
|
2014-10-02 02:54:14 +00:00
|
|
|
* Returns a pointer to the new connection if successful, or a null
|
|
|
|
* pointer otherwise.
|
|
|
|
*/
|
2014-12-12 22:10:17 +00:00
|
|
|
struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
|
2014-10-29 00:35:59 +00:00
|
|
|
u16 cport_id, u8 protocol_id)
|
2014-10-02 02:54:14 +00:00
|
|
|
{
|
|
|
|
struct gb_connection *connection;
|
2014-10-02 17:30:05 +00:00
|
|
|
struct greybus_host_device *hd;
|
2014-10-24 09:34:46 +00:00
|
|
|
int retval;
|
2014-11-05 22:12:50 +00:00
|
|
|
u8 major = 0;
|
|
|
|
u8 minor = 1;
|
2014-10-02 02:54:14 +00:00
|
|
|
|
|
|
|
connection = kzalloc(sizeof(*connection), GFP_KERNEL);
|
|
|
|
if (!connection)
|
|
|
|
return NULL;
|
|
|
|
|
2014-12-23 23:16:53 +00:00
|
|
|
connection->protocol_id = protocol_id;
|
|
|
|
connection->major = major;
|
|
|
|
connection->minor = minor;
|
2014-10-29 00:36:00 +00:00
|
|
|
|
2014-12-19 22:56:31 +00:00
|
|
|
hd = bundle->intf->hd;
|
2014-10-29 01:57:08 +00:00
|
|
|
connection->hd = hd;
|
2014-10-06 11:53:06 +00:00
|
|
|
if (!gb_connection_hd_cport_id_alloc(connection)) {
|
2014-11-05 22:12:51 +00:00
|
|
|
gb_protocol_put(connection->protocol);
|
2014-10-02 17:30:04 +00:00
|
|
|
kfree(connection);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-07 03:29:40 +00:00
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
connection->bundle = bundle;
|
2015-06-04 04:48:01 +00:00
|
|
|
connection->intf_cport_id = cport_id;
|
2014-10-22 07:04:30 +00:00
|
|
|
connection->state = GB_CONNECTION_STATE_DISABLED;
|
2014-10-02 17:30:06 +00:00
|
|
|
|
2014-12-12 22:10:17 +00:00
|
|
|
connection->dev.parent = &bundle->dev;
|
2014-10-24 09:34:46 +00:00
|
|
|
connection->dev.bus = &greybus_bus_type;
|
|
|
|
connection->dev.type = &greybus_connection_type;
|
|
|
|
connection->dev.groups = connection_groups;
|
|
|
|
device_initialize(&connection->dev);
|
|
|
|
dev_set_name(&connection->dev, "%s:%d",
|
2014-12-12 22:10:17 +00:00
|
|
|
dev_name(&bundle->dev), cport_id);
|
2014-10-24 09:34:46 +00:00
|
|
|
|
|
|
|
retval = device_add(&connection->dev);
|
|
|
|
if (retval) {
|
2014-11-05 22:03:12 +00:00
|
|
|
pr_err("failed to add connection device for cport 0x%04hx\n",
|
|
|
|
cport_id);
|
2014-10-29 00:35:58 +00:00
|
|
|
gb_connection_hd_cport_id_free(connection);
|
2014-11-05 22:12:51 +00:00
|
|
|
gb_protocol_put(connection->protocol);
|
2014-10-29 01:57:08 +00:00
|
|
|
put_device(&connection->dev);
|
2014-11-13 12:44:39 +00:00
|
|
|
kfree(connection);
|
2014-10-24 09:34:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-12-23 23:16:53 +00:00
|
|
|
/* XXX Will have to establish connections to get version */
|
|
|
|
gb_connection_bind_protocol(connection);
|
|
|
|
if (!connection->protocol)
|
|
|
|
dev_warn(&bundle->dev,
|
|
|
|
"protocol 0x%02hhx handler not found\n", protocol_id);
|
|
|
|
|
2014-10-03 19:14:22 +00:00
|
|
|
spin_lock_irq(&gb_connections_lock);
|
2015-06-04 12:46:45 +00:00
|
|
|
list_add(&connection->hd_links, &hd->connections);
|
|
|
|
list_add(&connection->bundle_links, &bundle->connections);
|
2014-10-03 19:14:22 +00:00
|
|
|
spin_unlock_irq(&gb_connections_lock);
|
|
|
|
|
2014-12-03 14:35:08 +00:00
|
|
|
atomic_set(&connection->op_cycle, 0);
|
2014-10-02 02:54:15 +00:00
|
|
|
INIT_LIST_HEAD(&connection->operations);
|
2014-10-02 02:54:14 +00:00
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tear down a previously set up connection.
|
|
|
|
*/
|
|
|
|
void gb_connection_destroy(struct gb_connection *connection)
|
|
|
|
{
|
2014-10-22 07:04:29 +00:00
|
|
|
struct gb_operation *operation;
|
|
|
|
struct gb_operation *next;
|
|
|
|
|
2014-10-02 02:54:14 +00:00
|
|
|
if (WARN_ON(!connection))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* XXX Need to wait for any outstanding requests to complete */
|
2014-11-14 11:55:02 +00:00
|
|
|
if (WARN_ON(!list_empty(&connection->operations))) {
|
|
|
|
list_for_each_entry_safe(operation, next,
|
|
|
|
&connection->operations, links)
|
2014-11-22 01:29:17 +00:00
|
|
|
gb_operation_cancel(operation, -ESHUTDOWN);
|
2014-10-22 07:04:29 +00:00
|
|
|
}
|
2014-10-03 19:14:22 +00:00
|
|
|
spin_lock_irq(&gb_connections_lock);
|
2014-12-12 22:10:17 +00:00
|
|
|
list_del(&connection->bundle_links);
|
2014-11-17 14:08:44 +00:00
|
|
|
list_del(&connection->hd_links);
|
2014-10-03 19:14:22 +00:00
|
|
|
spin_unlock_irq(&gb_connections_lock);
|
|
|
|
|
2014-10-06 11:53:06 +00:00
|
|
|
gb_connection_hd_cport_id_free(connection);
|
2014-11-05 22:12:51 +00:00
|
|
|
gb_protocol_put(connection->protocol);
|
2014-10-24 09:34:46 +00:00
|
|
|
|
2015-04-02 12:23:47 +00:00
|
|
|
device_unregister(&connection->dev);
|
2014-10-02 02:54:14 +00:00
|
|
|
}
|
2014-10-02 02:54:15 +00:00
|
|
|
|
2014-10-16 11:35:35 +00:00
|
|
|
int gb_connection_init(struct gb_connection *connection)
|
|
|
|
{
|
2014-10-22 07:04:30 +00:00
|
|
|
int ret;
|
|
|
|
|
2014-11-05 22:12:54 +00:00
|
|
|
if (!connection->protocol) {
|
2014-12-23 23:16:53 +00:00
|
|
|
dev_warn(&connection->dev, "init without protocol.\n");
|
|
|
|
return 0;
|
2014-10-16 11:35:35 +00:00
|
|
|
}
|
2014-10-22 07:04:30 +00:00
|
|
|
|
2014-11-05 22:12:54 +00:00
|
|
|
/* Need to enable the connection to initialize it */
|
|
|
|
connection->state = GB_CONNECTION_STATE_ENABLED;
|
|
|
|
ret = connection->protocol->connection_init(connection);
|
2014-10-22 07:04:30 +00:00
|
|
|
if (ret)
|
|
|
|
connection->state = GB_CONNECTION_STATE_ERROR;
|
|
|
|
|
|
|
|
return ret;
|
2014-10-16 11:35:35 +00:00
|
|
|
}
|
2014-10-21 04:01:04 +00:00
|
|
|
|
|
|
|
void gb_connection_exit(struct gb_connection *connection)
|
|
|
|
{
|
2014-11-05 22:12:54 +00:00
|
|
|
if (!connection->protocol) {
|
2014-12-23 23:16:53 +00:00
|
|
|
dev_warn(&connection->dev, "exit without protocol.\n");
|
2014-10-27 11:04:30 +00:00
|
|
|
return;
|
2014-10-21 04:01:04 +00:00
|
|
|
}
|
2015-03-17 09:55:52 +00:00
|
|
|
|
|
|
|
if (connection->state != GB_CONNECTION_STATE_ENABLED)
|
|
|
|
return;
|
|
|
|
|
2014-10-27 11:04:30 +00:00
|
|
|
connection->state = GB_CONNECTION_STATE_DESTROYING;
|
2014-11-05 22:12:54 +00:00
|
|
|
connection->protocol->connection_exit(connection);
|
2014-10-21 04:01:04 +00:00
|
|
|
}
|