2014-10-02 02:54:14 +00:00
|
|
|
/*
|
|
|
|
* Greybus connections
|
|
|
|
*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Released under the GPLv2 only.
|
|
|
|
*/
|
|
|
|
|
2014-10-02 02:54:15 +00:00
|
|
|
#include <linux/atomic.h>
|
|
|
|
|
|
|
|
#include "kernel_ver.h"
|
2014-10-02 02:54:14 +00:00
|
|
|
#include "greybus.h"
|
|
|
|
|
2014-10-03 19:14:22 +00:00
|
|
|
static DEFINE_SPINLOCK(gb_connections_lock);
|
|
|
|
|
2014-10-06 11:53:08 +00:00
|
|
|
static void _gb_hd_connection_insert(struct greybus_host_device *hd,
|
|
|
|
struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
struct rb_root *root = &hd->connections;
|
|
|
|
struct rb_node *node = &connection->hd_node;
|
|
|
|
struct rb_node **link = &root->rb_node;
|
|
|
|
struct rb_node *above = NULL;
|
|
|
|
u16 cport_id = connection->hd_cport_id;
|
|
|
|
|
|
|
|
while (*link) {
|
|
|
|
struct gb_connection *connection;
|
|
|
|
|
|
|
|
above = *link;
|
|
|
|
connection = rb_entry(above, struct gb_connection, hd_node);
|
|
|
|
if (connection->hd_cport_id > cport_id)
|
|
|
|
link = &above->rb_left;
|
|
|
|
else if (connection->hd_cport_id < cport_id)
|
|
|
|
link = &above->rb_right;
|
|
|
|
}
|
|
|
|
rb_link_node(node, above, link);
|
|
|
|
rb_insert_color(node, root);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _gb_hd_connection_remove(struct gb_connection *connection)
|
|
|
|
{
|
|
|
|
rb_erase(&connection->hd_node, &connection->hd->connections);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gb_connection *gb_hd_connection_find(struct greybus_host_device *hd,
|
|
|
|
u16 cport_id)
|
|
|
|
{
|
|
|
|
struct gb_connection *connection = NULL;
|
|
|
|
struct rb_node *node;
|
|
|
|
|
|
|
|
spin_lock_irq(&gb_connections_lock);
|
|
|
|
node = hd->connections.rb_node;
|
|
|
|
while (node) {
|
|
|
|
connection = rb_entry(node, struct gb_connection, hd_node);
|
|
|
|
if (connection->hd_cport_id > cport_id)
|
|
|
|
node = node->rb_left;
|
|
|
|
else if (connection->hd_cport_id < cport_id)
|
|
|
|
node = node->rb_right;
|
|
|
|
else
|
2014-10-06 17:26:02 +00:00
|
|
|
goto found;
|
2014-10-06 11:53:08 +00:00
|
|
|
}
|
2014-10-06 17:26:02 +00:00
|
|
|
connection = NULL;
|
|
|
|
found:
|
2014-10-06 11:53:08 +00:00
|
|
|
spin_unlock_irq(&gb_connections_lock);
|
|
|
|
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2014-10-07 03:29:40 +00:00
|
|
|
spin_lock(&connection->hd->cport_id_map_lock);
|
2014-10-03 19:14:24 +00:00
|
|
|
id = ida_simple_get(ida, 0, HOST_DEV_CPORT_ID_MAX, GFP_KERNEL);
|
2014-10-07 03:29:40 +00:00
|
|
|
spin_unlock(&connection->hd->cport_id_map_lock);
|
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;
|
|
|
|
|
2014-10-07 03:29:40 +00:00
|
|
|
spin_lock(&connection->hd->cport_id_map_lock);
|
2014-10-03 19:14:24 +00:00
|
|
|
ida_simple_remove(ida, connection->hd_cport_id);
|
2014-10-07 03:29:40 +00:00
|
|
|
spin_unlock(&connection->hd->cport_id_map_lock);
|
2014-10-03 19:14:24 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
return sprintf(buf, "%d", connection->state);
|
|
|
|
}
|
|
|
|
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-10-29 00:36:00 +00:00
|
|
|
return sprintf(buf, "%d", 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct device_type greybus_connection_type = {
|
|
|
|
.name = "greybus_connection",
|
|
|
|
.release = gb_connection_release,
|
|
|
|
};
|
|
|
|
|
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-10-02 17:30:05 +00:00
|
|
|
struct gb_connection *gb_connection_create(struct gb_interface *interface,
|
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-10-02 02:54:14 +00:00
|
|
|
|
|
|
|
connection = kzalloc(sizeof(*connection), GFP_KERNEL);
|
|
|
|
if (!connection)
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-29 00:36:00 +00:00
|
|
|
INIT_LIST_HEAD(&connection->protocol_links);
|
|
|
|
if (!gb_protocol_get(connection, protocol_id)) {
|
|
|
|
kfree(connection);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-02 17:30:05 +00:00
|
|
|
hd = interface->gmod->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-10-29 00:36:00 +00:00
|
|
|
gb_protocol_put(connection);
|
2014-10-02 17:30:04 +00:00
|
|
|
kfree(connection);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-10-07 03:29:40 +00:00
|
|
|
|
2014-10-27 09:58:54 +00:00
|
|
|
connection->interface = interface;
|
2014-10-02 17:30:05 +00:00
|
|
|
connection->interface_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-10-24 09:34:46 +00:00
|
|
|
connection->dev.parent = &interface->dev;
|
|
|
|
connection->dev.driver = NULL;
|
|
|
|
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",
|
|
|
|
dev_name(&interface->dev), cport_id);
|
|
|
|
|
|
|
|
retval = device_add(&connection->dev);
|
|
|
|
if (retval) {
|
2014-10-29 00:35:58 +00:00
|
|
|
gb_connection_hd_cport_id_free(connection);
|
2014-10-29 00:36:00 +00:00
|
|
|
gb_protocol_put(connection);
|
2014-10-29 01:57:08 +00:00
|
|
|
put_device(&connection->dev);
|
2014-10-24 09:34:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-03 19:14:22 +00:00
|
|
|
spin_lock_irq(&gb_connections_lock);
|
2014-10-06 11:53:08 +00:00
|
|
|
_gb_hd_connection_insert(hd, connection);
|
2014-10-03 19:14:22 +00:00
|
|
|
list_add_tail(&connection->interface_links, &interface->connections);
|
|
|
|
spin_unlock_irq(&gb_connections_lock);
|
|
|
|
|
2014-10-02 02:54:15 +00:00
|
|
|
INIT_LIST_HEAD(&connection->operations);
|
2014-10-16 11:35:32 +00:00
|
|
|
connection->pending = RB_ROOT;
|
2014-10-02 02:54:15 +00:00
|
|
|
atomic_set(&connection->op_cycle, 0);
|
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-10-02 02:54:15 +00:00
|
|
|
WARN_ON(!list_empty(&connection->operations));
|
2014-10-02 02:54:14 +00:00
|
|
|
|
2014-10-22 07:04:29 +00:00
|
|
|
list_for_each_entry_safe(operation, next, &connection->operations,
|
|
|
|
links) {
|
|
|
|
gb_operation_cancel(operation);
|
|
|
|
}
|
2014-10-03 19:14:22 +00:00
|
|
|
spin_lock_irq(&gb_connections_lock);
|
|
|
|
list_del(&connection->interface_links);
|
2014-10-06 11:53:08 +00:00
|
|
|
_gb_hd_connection_remove(connection);
|
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-10-29 00:36:00 +00:00
|
|
|
/* kref_put(connection->hd); */
|
|
|
|
gb_protocol_put(connection);
|
2014-10-24 09:34:46 +00:00
|
|
|
|
|
|
|
device_del(&connection->dev);
|
2014-10-02 02:54:14 +00:00
|
|
|
}
|
2014-10-02 02:54:15 +00:00
|
|
|
|
2014-10-16 11:35:32 +00:00
|
|
|
u16 gb_connection_operation_id(struct gb_connection *connection)
|
2014-10-02 02:54:15 +00:00
|
|
|
{
|
2014-10-21 04:01:02 +00:00
|
|
|
return (u16)(atomic_inc_return(&connection->op_cycle) & (int)U16_MAX);
|
2014-10-02 02:54:15 +00:00
|
|
|
}
|
2014-10-03 20:05:22 +00:00
|
|
|
|
|
|
|
void gb_connection_err(struct gb_connection *connection, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct va_format vaf;
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
vaf.fmt = fmt;
|
|
|
|
vaf.va = &args;
|
|
|
|
|
|
|
|
pr_err("greybus: [%hhu:%hhu:%hu]: %pV\n",
|
|
|
|
connection->interface->gmod->module_id,
|
|
|
|
connection->interface->id,
|
|
|
|
connection->interface_cport_id, &vaf);
|
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
2014-10-16 11:35:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* XXX Protocols should have a set of function pointers:
|
|
|
|
* ->init (called here, to initialize the device)
|
|
|
|
* ->input_handler
|
|
|
|
* ->exit (reverse of init)
|
|
|
|
*/
|
|
|
|
int gb_connection_init(struct gb_connection *connection)
|
|
|
|
{
|
2014-10-22 07:04:30 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Need to enable the connection to initialize it */
|
|
|
|
connection->state = GB_CONNECTION_STATE_ENABLED;
|
2014-10-29 00:36:00 +00:00
|
|
|
switch (connection->protocol->id) {
|
2014-10-16 11:35:35 +00:00
|
|
|
case GREYBUS_PROTOCOL_I2C:
|
2014-10-27 11:04:30 +00:00
|
|
|
connection->handler = &gb_i2c_connection_handler;
|
2014-10-22 07:04:30 +00:00
|
|
|
break;
|
2014-10-16 11:35:35 +00:00
|
|
|
case GREYBUS_PROTOCOL_GPIO:
|
2014-10-27 11:04:30 +00:00
|
|
|
connection->handler = &gb_gpio_connection_handler;
|
2014-10-22 07:04:30 +00:00
|
|
|
break;
|
2014-10-20 07:24:57 +00:00
|
|
|
case GREYBUS_PROTOCOL_BATTERY:
|
2014-10-27 11:04:30 +00:00
|
|
|
connection->handler = &gb_battery_connection_handler;
|
2014-10-22 07:04:30 +00:00
|
|
|
break;
|
2014-10-27 09:32:34 +00:00
|
|
|
case GREYBUS_PROTOCOL_UART:
|
2014-10-27 11:04:30 +00:00
|
|
|
connection->handler = &gb_uart_connection_handler;
|
2014-10-22 07:04:30 +00:00
|
|
|
break;
|
2014-10-20 07:24:57 +00:00
|
|
|
case GREYBUS_PROTOCOL_CONTROL:
|
|
|
|
case GREYBUS_PROTOCOL_AP:
|
2014-10-16 11:35:35 +00:00
|
|
|
case GREYBUS_PROTOCOL_HID:
|
2014-10-20 08:02:56 +00:00
|
|
|
case GREYBUS_PROTOCOL_LED:
|
2014-10-16 11:35:35 +00:00
|
|
|
case GREYBUS_PROTOCOL_VENDOR:
|
|
|
|
default:
|
2014-10-29 00:35:59 +00:00
|
|
|
gb_connection_err(connection, "unimplemented protocol %hhu",
|
2014-10-29 00:36:00 +00:00
|
|
|
connection->protocol->id);
|
2014-10-22 07:04:30 +00:00
|
|
|
ret = -ENXIO;
|
2014-10-16 11:35:35 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-10-22 07:04:30 +00:00
|
|
|
|
2014-10-28 14:08:13 +00:00
|
|
|
ret = connection->handler->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-10-27 11:04:30 +00:00
|
|
|
if (!connection->handler) {
|
|
|
|
gb_connection_err(connection, "uninitialized connection");
|
|
|
|
return;
|
2014-10-21 04:01:04 +00:00
|
|
|
}
|
2014-10-27 11:04:30 +00:00
|
|
|
connection->state = GB_CONNECTION_STATE_DESTROYING;
|
|
|
|
connection->handler->connection_exit(connection);
|
2014-10-21 04:01:04 +00:00
|
|
|
}
|