Commit 5d9fd7e1 authored by Alex Elder's avatar Alex Elder Committed by Greg Kroah-Hartman

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: default avatarAlex Elder <elder@linaro.org>
Signed-off-by: default avatarGreg Kroah-Hartman <greg@kroah.com>
parent 19d03dec
...@@ -400,15 +400,12 @@ static void gb_battery_connection_exit(struct gb_connection *connection) ...@@ -400,15 +400,12 @@ static void gb_battery_connection_exit(struct gb_connection *connection)
kfree(gb); 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 = { static struct gb_protocol battery_protocol = {
.id = GREYBUS_PROTOCOL_BATTERY, .id = GREYBUS_PROTOCOL_BATTERY,
.major = 0, .major = 0,
.minor = 1, .minor = 1,
.connection_init = gb_battery_connection_init,
.connection_exit = gb_battery_connection_exit,
}; };
bool gb_battery_protocol_init(void) bool gb_battery_protocol_init(void)
......
...@@ -267,45 +267,18 @@ void gb_connection_err(struct gb_connection *connection, const char *fmt, ...) ...@@ -267,45 +267,18 @@ void gb_connection_err(struct gb_connection *connection, const char *fmt, ...)
va_end(args); 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 gb_connection_init(struct gb_connection *connection)
{ {
int ret; int ret;
/* Need to enable the connection to initialize it */ if (!connection->protocol) {
connection->state = GB_CONNECTION_STATE_ENABLED; gb_connection_err(connection, "uninitialized connection");
switch (connection->protocol->id) { return -EIO;
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;
} }
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) if (ret)
connection->state = GB_CONNECTION_STATE_ERROR; connection->state = GB_CONNECTION_STATE_ERROR;
...@@ -314,10 +287,10 @@ int gb_connection_init(struct gb_connection *connection) ...@@ -314,10 +287,10 @@ int gb_connection_init(struct gb_connection *connection)
void gb_connection_exit(struct gb_connection *connection) void gb_connection_exit(struct gb_connection *connection)
{ {
if (!connection->handler) { if (!connection->protocol) {
gb_connection_err(connection, "uninitialized connection"); gb_connection_err(connection, "uninitialized connection");
return; return;
} }
connection->state = GB_CONNECTION_STATE_DESTROYING; connection->state = GB_CONNECTION_STATE_DESTROYING;
connection->handler->connection_exit(connection); connection->protocol->connection_exit(connection);
} }
...@@ -21,15 +21,6 @@ enum gb_connection_state { ...@@ -21,15 +21,6 @@ enum gb_connection_state {
GB_CONNECTION_STATE_DESTROYING = 4, 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 gb_connection {
struct greybus_host_device *hd; struct greybus_host_device *hd;
struct gb_interface *interface; struct gb_interface *interface;
...@@ -48,8 +39,6 @@ struct gb_connection { ...@@ -48,8 +39,6 @@ struct gb_connection {
struct rb_root pending; /* awaiting reponse */ struct rb_root pending; /* awaiting reponse */
atomic_t op_cycle; atomic_t op_cycle;
struct gb_connection_handler *handler;
void *private; void *private;
}; };
#define to_gb_connection(d) container_of(d, struct gb_connection, dev) #define to_gb_connection(d) container_of(d, struct gb_connection, dev)
......
...@@ -792,15 +792,12 @@ static void gb_gpio_connection_exit(struct gb_connection *connection) ...@@ -792,15 +792,12 @@ static void gb_gpio_connection_exit(struct gb_connection *connection)
kfree(gb_gpio_controller); 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 = { static struct gb_protocol gpio_protocol = {
.id = GREYBUS_PROTOCOL_GPIO, .id = GREYBUS_PROTOCOL_GPIO,
.major = 0, .major = 0,
.minor = 1, .minor = 1,
.connection_init = gb_gpio_connection_init,
.connection_exit = gb_gpio_connection_exit,
}; };
bool gb_gpio_protocol_init(void) bool gb_gpio_protocol_init(void)
......
...@@ -518,15 +518,12 @@ static void gb_i2c_connection_exit(struct gb_connection *connection) ...@@ -518,15 +518,12 @@ static void gb_i2c_connection_exit(struct gb_connection *connection)
kfree(gb_i2c_dev); 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 = { static struct gb_protocol i2c_protocol = {
.id = GREYBUS_PROTOCOL_I2C, .id = GREYBUS_PROTOCOL_I2C,
.major = 0, .major = 0,
.minor = 1, .minor = 1,
.connection_init = gb_i2c_connection_init,
.connection_exit = gb_i2c_connection_exit,
}; };
bool gb_i2c_protocol_init(void) bool gb_i2c_protocol_init(void)
......
...@@ -11,6 +11,9 @@ ...@@ -11,6 +11,9 @@
#include "greybus.h" #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 * Protocols having the same id but different major and/or minor
* version numbers are treated as distinct protocols. If it makes * version numbers are treated as distinct protocols. If it makes
...@@ -23,6 +26,9 @@ struct gb_protocol { ...@@ -23,6 +26,9 @@ struct gb_protocol {
u8 count; u8 count;
struct list_head links; /* global list */ 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); bool gb_protocol_register(struct gb_protocol *protocol);
......
...@@ -77,15 +77,12 @@ static void gb_sdio_connection_exit(struct gb_connection *connection) ...@@ -77,15 +77,12 @@ static void gb_sdio_connection_exit(struct gb_connection *connection)
connection->private = NULL; 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 = { static struct gb_protocol sdio_protocol = {
.id = GREYBUS_PROTOCOL_SDIO, .id = GREYBUS_PROTOCOL_SDIO,
.major = 0, .major = 0,
.minor = 1, .minor = 1,
.connection_init = gb_sdio_connection_init,
.connection_exit = gb_sdio_connection_exit,
}; };
bool gb_sdio_protocol_init(void) bool gb_sdio_protocol_init(void)
......
...@@ -520,15 +520,12 @@ static void gb_tty_exit(void) ...@@ -520,15 +520,12 @@ static void gb_tty_exit(void)
unregister_chrdev_region(MKDEV(major, minor), GB_NUM_MINORS); 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 = { static struct gb_protocol uart_protocol = {
.id = GREYBUS_PROTOCOL_UART, .id = GREYBUS_PROTOCOL_UART,
.major = 0, .major = 0,
.minor = 1, .minor = 1,
.connection_init = gb_uart_connection_init,
.connection_exit = gb_uart_connection_exit,
}; };
bool gb_uart_protocol_init(void) bool gb_uart_protocol_init(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment