greybus: move methods into protocol
authorAlex Elder <elder@linaro.org>
Wed, 5 Nov 2014 22:12:54 +0000 (16:12 -0600)
committerGreg Kroah-Hartman <greg@kroah.com>
Wed, 5 Nov 2014 22:23:50 +0000 (14:23 -0800)
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>
drivers/staging/greybus/battery-gb.c
drivers/staging/greybus/connection.c
drivers/staging/greybus/connection.h
drivers/staging/greybus/gpio-gb.c
drivers/staging/greybus/i2c-gb.c
drivers/staging/greybus/protocol.h
drivers/staging/greybus/sdio-gb.c
drivers/staging/greybus/uart-gb.c

index 457daf700543bc980a4fbff0f516379f3436f325..a4015673bf4c593933a04f009cfb69dc15243c2e 100644 (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)
index e3000f7eb7990b63f8a4a823a5adc3f46d833df0..586457f027de5dfc0d4de9310896cde760354178 100644 (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);
 }
index ea54334238bdc33c18887836286ebff7f0072a19..893c02af371053eadc5a725fa91a738ee1560919 100644 (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)
index 242b91a99d22377163b22d33b9f362b5c3aa459a..a4ee35671434fd76eec4bd8492cb02ab33ce55bc 100644 (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)
index c8fae17d4b7783a390ff87dede7413d3cb1e0c31..60db15ee364d02728119ad76fe9aca0f616b07a4 100644 (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)
index c2adfdca8bf785470c2f9e42fe1909546deaa900..32178f1f2671a28cef3d58b247d3544f4a47cf4b 100644 (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);
index 8fbfbca37c48d824397401c8de0075a3769c597e..9e5fb8f26425bda9856931ebb5040f3bae16e1b6 100644 (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)
index 5596644e952b8583cfc242279a8d756d60c4cd9e..56db8911d800ca4fd5b6ff29e3c8629442cd7e85 100644 (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)