greybus: move methods into protocol

Get rid of the connection handler structure, and instead put the
methods that were there into the protocol structure.

Eliminate the big switch statement in connection_init() and just
call the connection's protocol's init function there directly.

Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <greg@kroah.com>
This commit is contained in:
Alex Elder 2014-11-05 16:12:54 -06:00 committed by Greg Kroah-Hartman
parent 19d03decd3
commit 5d9fd7e1ba
8 changed files with 24 additions and 71 deletions

View File

@ -400,15 +400,12 @@ static void gb_battery_connection_exit(struct gb_connection *connection)
kfree(gb);
}
struct gb_connection_handler gb_battery_connection_handler = {
.connection_init = gb_battery_connection_init,
.connection_exit = gb_battery_connection_exit,
};
static struct gb_protocol battery_protocol = {
.id = GREYBUS_PROTOCOL_BATTERY,
.major = 0,
.minor = 1,
.connection_init = gb_battery_connection_init,
.connection_exit = gb_battery_connection_exit,
};
bool gb_battery_protocol_init(void)

View File

@ -267,45 +267,18 @@ void gb_connection_err(struct gb_connection *connection, const char *fmt, ...)
va_end(args);
}
/*
* 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)
{
int ret;
/* Need to enable the connection to initialize it */
connection->state = GB_CONNECTION_STATE_ENABLED;
switch (connection->protocol->id) {
case GREYBUS_PROTOCOL_I2C:
connection->handler = &gb_i2c_connection_handler;
break;
case GREYBUS_PROTOCOL_GPIO:
connection->handler = &gb_gpio_connection_handler;
break;
case GREYBUS_PROTOCOL_BATTERY:
connection->handler = &gb_battery_connection_handler;
break;
case GREYBUS_PROTOCOL_UART:
connection->handler = &gb_uart_connection_handler;
break;
case GREYBUS_PROTOCOL_CONTROL:
case GREYBUS_PROTOCOL_AP:
case GREYBUS_PROTOCOL_HID:
case GREYBUS_PROTOCOL_LED:
case GREYBUS_PROTOCOL_VENDOR:
default:
gb_connection_err(connection, "unimplemented protocol %hhu",
connection->protocol->id);
ret = -ENXIO;
break;
if (!connection->protocol) {
gb_connection_err(connection, "uninitialized connection");
return -EIO;
}
ret = connection->handler->connection_init(connection);
/* Need to enable the connection to initialize it */
connection->state = GB_CONNECTION_STATE_ENABLED;
ret = connection->protocol->connection_init(connection);
if (ret)
connection->state = GB_CONNECTION_STATE_ERROR;
@ -314,10 +287,10 @@ int gb_connection_init(struct gb_connection *connection)
void gb_connection_exit(struct gb_connection *connection)
{
if (!connection->handler) {
if (!connection->protocol) {
gb_connection_err(connection, "uninitialized connection");
return;
}
connection->state = GB_CONNECTION_STATE_DESTROYING;
connection->handler->connection_exit(connection);
connection->protocol->connection_exit(connection);
}

View File

@ -21,15 +21,6 @@ enum gb_connection_state {
GB_CONNECTION_STATE_DESTROYING = 4,
};
struct gb_connection;
typedef int (*gb_connection_init_t)(struct gb_connection *);
typedef void (*gb_connection_exit_t)(struct gb_connection *);
struct gb_connection_handler {
gb_connection_init_t connection_init;
gb_connection_exit_t connection_exit;
};
struct gb_connection {
struct greybus_host_device *hd;
struct gb_interface *interface;
@ -48,8 +39,6 @@ struct gb_connection {
struct rb_root pending; /* awaiting reponse */
atomic_t op_cycle;
struct gb_connection_handler *handler;
void *private;
};
#define to_gb_connection(d) container_of(d, struct gb_connection, dev)

View File

@ -792,15 +792,12 @@ static void gb_gpio_connection_exit(struct gb_connection *connection)
kfree(gb_gpio_controller);
}
struct gb_connection_handler gb_gpio_connection_handler = {
.connection_init = gb_gpio_connection_init,
.connection_exit = gb_gpio_connection_exit,
};
static struct gb_protocol gpio_protocol = {
.id = GREYBUS_PROTOCOL_GPIO,
.major = 0,
.minor = 1,
.connection_init = gb_gpio_connection_init,
.connection_exit = gb_gpio_connection_exit,
};
bool gb_gpio_protocol_init(void)

View File

@ -518,15 +518,12 @@ static void gb_i2c_connection_exit(struct gb_connection *connection)
kfree(gb_i2c_dev);
}
struct gb_connection_handler gb_i2c_connection_handler = {
.connection_init = gb_i2c_connection_init,
.connection_exit = gb_i2c_connection_exit,
};
static struct gb_protocol i2c_protocol = {
.id = GREYBUS_PROTOCOL_I2C,
.major = 0,
.minor = 1,
.connection_init = gb_i2c_connection_init,
.connection_exit = gb_i2c_connection_exit,
};
bool gb_i2c_protocol_init(void)

View File

@ -11,6 +11,9 @@
#include "greybus.h"
typedef int (*gb_connection_init_t)(struct gb_connection *);
typedef void (*gb_connection_exit_t)(struct gb_connection *);
/*
* Protocols having the same id but different major and/or minor
* version numbers are treated as distinct protocols. If it makes
@ -23,6 +26,9 @@ struct gb_protocol {
u8 count;
struct list_head links; /* global list */
gb_connection_init_t connection_init;
gb_connection_exit_t connection_exit;
};
bool gb_protocol_register(struct gb_protocol *protocol);

View File

@ -77,15 +77,12 @@ static void gb_sdio_connection_exit(struct gb_connection *connection)
connection->private = NULL;
}
struct gb_connection_handler gb_sdio_connection_handler = {
.connection_init = gb_sdio_connection_init,
.connection_exit = gb_sdio_connection_exit,
};
static struct gb_protocol sdio_protocol = {
.id = GREYBUS_PROTOCOL_SDIO,
.major = 0,
.minor = 1,
.connection_init = gb_sdio_connection_init,
.connection_exit = gb_sdio_connection_exit,
};
bool gb_sdio_protocol_init(void)

View File

@ -520,15 +520,12 @@ static void gb_tty_exit(void)
unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS);
}
struct gb_connection_handler gb_uart_connection_handler = {
.connection_init = gb_uart_connection_init,
.connection_exit = gb_uart_connection_exit,
};
static struct gb_protocol uart_protocol = {
.id = GREYBUS_PROTOCOL_UART,
.major = 0,
.minor = 1,
.connection_init = gb_uart_connection_init,
.connection_exit = gb_uart_connection_exit,
};
bool gb_uart_protocol_init(void)