greybus: define greybus function abstraction
authorAlex Elder <elder@linaro.org>
Thu, 2 Oct 2014 02:54:13 +0000 (21:54 -0500)
committerGreg Kroah-Hartman <greg@kroah.com>
Fri, 3 Oct 2014 04:18:40 +0000 (21:18 -0700)
Define new source files "function.h" and "function.c" to contain the
definitions of the Greybus function abstraction.  A Greybus function
represents an active entity connected to a CPort implemented by a
Greybus interface.  A Greybus function has a type, which defines the
protocol to be used to interact with the function.  A Greybus
interface normally has at least two functions, but potentially many
more.

Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: Greg Kroah-Hartman <greg@kroah.com>
drivers/staging/greybus/Makefile
drivers/staging/greybus/function.c [new file with mode: 0644]
drivers/staging/greybus/function.h [new file with mode: 0644]
drivers/staging/greybus/greybus.h
drivers/staging/greybus/interface.c
drivers/staging/greybus/interface.h

index 21f6358..0efb695 100644 (file)
@@ -5,6 +5,7 @@ greybus-y :=    core.o          \
                ap.o            \
                module.o        \
                interface.o     \
+               function.o      \
                i2c-gb.o        \
                gpio-gb.o       \
                sdio-gb.o       \
diff --git a/drivers/staging/greybus/function.c b/drivers/staging/greybus/function.c
new file mode 100644 (file)
index 0000000..b06265c
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Greybus functions
+ *
+ * Copyright 2014 Google Inc.
+ *
+ * Released under the GPLv2 only.
+ */
+
+#include "greybus.h"
+
+/* XXX This could be per-host device or per-module or per-interface */
+static DEFINE_SPINLOCK(gb_functions_lock);
+
+/*
+ * A Greybus function generically defines an entity associated with
+ * a CPort within a module.  Each function has a type (e.g. i2c,
+ * GPIO, etc.) that defines how it behaves and how the AP interacts
+ * with it.
+ *
+ * Create a gb_function structure to represent a discovered
+ * function.  Returns a pointer to the new function or a null
+ * pointer if a failure occurs due to memory exhaustion.
+ */
+struct gb_function *gb_function_create(struct gb_interface *interface,
+                       u16 cport_id, enum greybus_function_type type)
+{
+       struct gb_function *function;
+
+       function = kzalloc(sizeof(*function), GFP_KERNEL);
+       if (!function)
+               return NULL;
+
+       function->interface = interface;        /* XXX refcount? */
+       function->cport_id = cport_id;
+       function->type = type;
+
+       spin_lock_irq(&gb_functions_lock);
+       list_add_tail(&function->links, &interface->functions);
+       spin_unlock_irq(&gb_functions_lock);
+
+       return function;
+}
+
+/*
+ * Tear down a previously set up function.
+ */
+void gb_function_destroy(struct gb_function *function)
+{
+       if (WARN_ON(!function))
+               return;
+
+       spin_lock_irq(&gb_functions_lock);
+       list_del(&function->links);
+       spin_unlock_irq(&gb_functions_lock);
+
+       /* kref_put(gmod); */
+       kfree(function);
+}
diff --git a/drivers/staging/greybus/function.h b/drivers/staging/greybus/function.h
new file mode 100644 (file)
index 0000000..379ffcd
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Greybus functions
+ *
+ * Copyright 2014 Google Inc.
+ *
+ * Released under the GPLv2 only.
+ */
+
+#ifndef __FUNCTION_H
+#define __FUNCTION_H
+
+struct gb_function {
+       struct gb_interface             *interface;
+       u16                             cport_id;
+       enum greybus_function_type      type;
+
+       struct list_head                links;  /* interface->functions */
+};
+
+struct gb_function *gb_function_create(struct gb_interface *interface,
+                               u16 cport_id,
+                               enum greybus_function_type function_type);
+void gb_function_destroy(struct gb_function *function);
+
+#endif /* __FUNCTION_H */
index c82c630..4eb70af 100644 (file)
@@ -22,6 +22,7 @@
 #include "greybus_manifest.h"
 #include "module.h"
 #include "interface.h"
+#include "function.h"
 
 
 /* Matches up with the Greybus Protocol specification document */
index a6e6e0e..dc7d009 100644 (file)
@@ -33,6 +33,7 @@ gb_interface_create(struct gb_module *gmod, u8 interface_id)
 
        interface->gmod = gmod;         /* XXX refcount? */
        interface->interface_id = interface_id;
+       INIT_LIST_HEAD(&interface->functions);
 
        spin_lock_irq(&gb_interfaces_lock);
        list_add_tail(&interface->links, &gmod->interfaces);
index 63ae762..7c3feb7 100644 (file)
@@ -14,6 +14,7 @@
 struct gb_interface {
        struct gb_module        *gmod;
        u8                      interface_id;
+       struct list_head        functions;
 
        struct list_head        links;  /* module->interfaces */
 };