summaryrefslogtreecommitdiffstats
path: root/drivers/staging/greybus/module.c
diff options
context:
space:
mode:
authorGreg Kroah-Hartman2014-12-21 23:10:26 +0100
committerGreg Kroah-Hartman2014-12-24 00:30:00 +0100
commitdf671553cbe286e885e61b61f8c126e034854a89 (patch)
tree848b1069b3bece302dd1803174f019d017999811 /drivers/staging/greybus/module.c
parentgreybus: interface: rename gb_modules_lock -> gb_interfaces_lock (diff)
downloadkernel-qcow2-linux-df671553cbe286e885e61b61f8c126e034854a89.tar.gz
kernel-qcow2-linux-df671553cbe286e885e61b61f8c126e034854a89.tar.xz
kernel-qcow2-linux-df671553cbe286e885e61b61f8c126e034854a89.zip
greybus: add module support
Modules in the greybus system sit above the interface, so insert them early in the sysfs tree. We dynamically create them when we have an interface that references a module, as we don't get a "module create" message directly. They also dynamically go away when the last interface associated with a module is removed. Naming scheme for modules/interfaces/bundles/connections is bumped up by one ':', and now looks like the following: /sys/bus/greybus $ tree . ├── devices │   ├── 7 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-1/7 │   ├── 7:7 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-1/7/7:7 │   ├── 7:7:0 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-1/7/7:7/7:7:0 │   └── 7:7:0:1 -> ../../../devices/pci0000:00/0000:00:14.0/usb1/1-1/7/7:7/7:7:0/7:7:0:1 ├── drivers ├── drivers_autoprobe ├── drivers_probe └── uevent 6 directories, 3 files /sys/bus/greybus $ grep . devices/*/uevent devices/7/uevent:DEVTYPE=greybus_module devices/7:7/uevent:DEVTYPE=greybus_interface devices/7:7:0/uevent:DEVTYPE=greybus_bundle devices/7:7:0:1/uevent:DEVTYPE=greybus_connection We still have some "confusion" about interface ids and module ids, which will be cleaned up later when the svc control protocol changes die down, right now we just name a module after the interface as we don't have any modules that have multiple interfaces in our systems. This has been tested with gbsim. Signed-off-by: Greg Kroah-Hartman <greg@kroah.com>
Diffstat (limited to 'drivers/staging/greybus/module.c')
-rw-r--r--drivers/staging/greybus/module.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/drivers/staging/greybus/module.c b/drivers/staging/greybus/module.c
new file mode 100644
index 000000000000..625e2d436073
--- /dev/null
+++ b/drivers/staging/greybus/module.c
@@ -0,0 +1,140 @@
+/*
+ * Greybus module code
+ *
+ * Copyright 2014 Google Inc.
+ * Copyright 2014 Linaro Ltd.
+ *
+ * Released under the GPLv2 only.
+ */
+
+#include "greybus.h"
+
+
+/*
+ * List of modules in the system. We really should just walk the list the
+ * driver core provides us, but as we have lots of different things on the same
+ * "bus" at the same time, a single list of modules is simplest for now.
+ */
+static DEFINE_SPINLOCK(gb_modules_lock);
+static LIST_HEAD(module_list);
+
+/* module sysfs attributes */
+#define gb_module_attr(field, type) \
+static ssize_t field##_show(struct device *dev, \
+ struct device_attribute *attr, \
+ char *buf) \
+{ \
+ struct gb_module *module = to_gb_module(dev); \
+ return sprintf(buf, "%"#type"\n", module->field); \
+} \
+static DEVICE_ATTR_RO(field)
+
+// FIXME, do we really need this attribute?
+gb_module_attr(module_id, x);
+
+static ssize_t epm_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ // FIXME, implement something here
+ return sprintf(buf, "1\n");
+}
+
+static ssize_t epm_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t size)
+{
+ // FIXME, implement something here.
+ return 0;
+}
+static DEVICE_ATTR_RW(epm);
+
+static struct attribute *module_attrs[] = {
+ &dev_attr_module_id.attr,
+ &dev_attr_epm.attr,
+ NULL,
+};
+ATTRIBUTE_GROUPS(module);
+
+static void greybus_module_release(struct device *dev)
+{
+ struct gb_module *module = to_gb_module(dev);
+
+ spin_lock(&gb_modules_lock);
+ list_del(&module->list);
+ spin_unlock(&gb_modules_lock);
+
+ kfree(module);
+}
+
+struct device_type greybus_module_type = {
+ .name = "greybus_module",
+ .release = greybus_module_release,
+};
+
+/*
+ * Search the list of modules in the system. If one is found, return it, with
+ * the reference count incremented.
+ */
+static struct gb_module *gb_module_find(u8 module_id)
+{
+ struct gb_module *module;
+
+ spin_lock(&gb_modules_lock);
+ list_for_each_entry(module, &module_list, list) {
+ if (module->module_id == module_id) {
+ get_device(&module->dev);
+ goto exit;
+ }
+ }
+ module = NULL;
+exit:
+ spin_unlock(&gb_modules_lock);
+ return module;
+}
+
+static struct gb_module *gb_module_create(struct greybus_host_device *hd,
+ u8 module_id)
+{
+ struct gb_module *module;
+ int retval;
+
+ module = kzalloc(sizeof(*module), GFP_KERNEL);
+ if (!module)
+ return NULL;
+
+ module->module_id = module_id;
+ module->dev.parent = hd->parent;
+ module->dev.bus = &greybus_bus_type;
+ module->dev.type = &greybus_module_type;
+ module->dev.groups = module_groups;
+ module->dev.dma_mask = hd->parent->dma_mask;
+ device_initialize(&module->dev);
+ dev_set_name(&module->dev, "%d", module_id);
+
+ retval = device_add(&module->dev);
+ if (retval) {
+ pr_err("failed to add module device for id 0x%02hhx\n",
+ module_id);
+ put_device(&module->dev);
+ kfree(module);
+ return NULL;
+ }
+
+ spin_lock(&gb_modules_lock);
+ list_add_tail(&module->list, &module_list);
+ spin_unlock(&gb_modules_lock);
+
+ return module;
+}
+
+struct gb_module *gb_module_find_or_create(struct greybus_host_device *hd,
+ u8 module_id)
+{
+ struct gb_module *module;
+
+ module = gb_module_find(module_id);
+ if (module)
+ return module;
+
+ return gb_module_create(hd, module_id);
+}
+