Skip to content
Browse files

+ldt_configfs_basic.c

  • Loading branch information...
1 parent 2f32335 commit a860d2e5e590522e5027ce5118b8c95b3d441ad5 @makelinux committed Oct 15, 2012
Showing with 96 additions and 2 deletions.
  1. +2 −1 Makefile
  2. +1 −1 README.md
  3. +93 −0 ldt_configfs_basic.c
View
3 Makefile
@@ -8,6 +8,7 @@ obj-m+= ldt.o
obj-m+= ldt_plat_drv.o # implements platform_driver only
obj-m+= ldt_plat_dev.o # implements platform_device and resource
#obj-m+= chrdev_region_sample.o
+obj-m+= ldt_configfs_basic.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
@@ -24,7 +25,7 @@ clean:
dio: CPPFLAGS+= -D CTRACER_ON -include ctracer.h -g
-_src = dio.c ldt.c ldt_plat_dev.c ldt_plat_drv.c ctracer.h
+_src = dio.c ldt.c ldt_plat_dev.c ldt_plat_drv.c ctracer.h ldt_configfs_basic.c
checkpatch:
/usr/src/linux-headers-$(shell uname -r)/scripts/checkpatch.pl --no-tree --show-types --ignore LONG_LINE,LINE_CONTINUATIONS --terse -f $(_src) Makefile
View
2 README.md
@@ -2,7 +2,7 @@
LDT project is useful for Linux driver development beginners and as starting point for a new drivers.
The driver uses following Linux facilities:
-module, platform driver, file operations (read/write, mmap, ioctl, blocking and nonblocking mode, polling), kfifo, completion, interrupt, tasklet, work, kthread, timer, simple misc device, multiple char devices, Device Model, proc fs, UART 0x3f8, HW loopback, SW loopback, ftracer.
+module, platform driver, file operations (read/write, mmap, ioctl, blocking and nonblocking mode, polling), kfifo, completion, interrupt, tasklet, work, kthread, timer, simple misc device, multiple char devices, Device Model, proc fs, configfs, UART 0x3f8, HW loopback, SW loopback, ftracer.
## Usage:
View
93 ldt_configfs_basic.c
@@ -0,0 +1,93 @@
+/*
+ * LDT - Linux Driver Template - basic configfs
+ *
+ * Copyright (C) 2012 Constantine Shulyupin http://www.makelinux.net/
+ *
+ * Dual BSD/GPL License
+ *
+ * based on configfs_example_explicit.c and much more simple, without containers, just 70 LOC
+ *
+ * Sample usage:
+ * sudo insmod ldt_configfs_basic.ko
+ * ls /configfs/ldt_configfs_basic/
+ * sudo sh -c "echo 123 > /configfs/ldt_configfs_basic/parameter"
+ * cat /configfs/ldt_configfs_basic/parameter
+ *
+ */
+
+#include <linux/module.h>
+#include <linux/configfs.h>
+
+static int parameter;
+
+static struct configfs_attribute ldt_parameter_attr = {
+ .ca_owner = THIS_MODULE, .ca_name = "parameter", .ca_mode = S_IRUGO | S_IWUSR };
+
+static struct configfs_attribute ldt_description_attr = {
+ .ca_owner = THIS_MODULE, .ca_name = "description", .ca_mode = S_IRUGO };
+
+static struct configfs_attribute *ldt_attrs[] = {
+ &ldt_description_attr,
+ &ldt_parameter_attr,
+ NULL,
+};
+
+static ssize_t ldt_attr_show(struct config_item *item, struct configfs_attribute *attr, char *page)
+{
+ ssize_t ret = -EINVAL;
+ if (attr == &ldt_description_attr)
+ return sprintf(page, "basic sample of configfs\n");
+ else if (attr == &ldt_parameter_attr)
+ return sprintf(page, "%d\n", parameter);
+ return ret;
+}
+
+static ssize_t ldt_attr_store(struct config_item *item, struct configfs_attribute *attr, const char *page, size_t count)
+{
+ ssize_t ret = -EINVAL;
+ if (attr == &ldt_parameter_attr) {
+ ret = kstrtoint(page, 0, &parameter);
+ if (ret)
+ return ret;
+ ret = count;
+ }
+ return ret;
+}
+
+static struct config_item_type ci_type = {
+ .ct_item_ops = (struct configfs_item_operations[]){ {
+ .show_attribute = ldt_attr_show,
+ .store_attribute = ldt_attr_store,} },
+ .ct_attrs = ldt_attrs,
+ .ct_owner = THIS_MODULE,
+};
+
+static struct configfs_subsystem ldt_subsys = {
+ .su_group = {
+ .cg_item = {
+ .ci_namebuf = KBUILD_MODNAME,
+ .ci_type = &ci_type,
+ },
+ },
+};
+
+static int __init configfs_example_init(void)
+{
+ int ret;
+ config_group_init(&ldt_subsys.su_group);
+ mutex_init(&ldt_subsys.su_mutex);
+ ret = configfs_register_subsystem(&ldt_subsys);
+ if (ret)
+ printk(KERN_ERR "Error %d while registering subsystem %s\n",
+ ret, ldt_subsys.su_group.cg_item.ci_namebuf);
+ return ret;
+}
+
+static void __exit configfs_example_exit(void)
+{
+ configfs_unregister_subsystem(&ldt_subsys);
+}
+
+module_init(configfs_example_init);
+module_exit(configfs_example_exit);
+MODULE_LICENSE("GPL");

0 comments on commit a860d2e

Please sign in to comment.
Something went wrong with that request. Please try again.