Skip to content
This repository
Browse code

+ldt_configfs_basic.c

  • Loading branch information...
commit a860d2e5e590522e5027ce5118b8c95b3d441ad5 1 parent 2f32335
Constantine authored

Showing 3 changed files with 96 additions and 2 deletions. Show diff stats Hide diff stats

  1. +2 1  Makefile
  2. +1 1  README.md
  3. +93 0 ldt_configfs_basic.c
3  Makefile
@@ -8,6 +8,7 @@ obj-m+= ldt.o
8 8 obj-m+= ldt_plat_drv.o # implements platform_driver only
9 9 obj-m+= ldt_plat_dev.o # implements platform_device and resource
10 10 #obj-m+= chrdev_region_sample.o
  11 +obj-m+= ldt_configfs_basic.o
11 12
12 13 KERNELDIR ?= /lib/modules/$(shell uname -r)/build
13 14
@@ -24,7 +25,7 @@ clean:
24 25
25 26 dio: CPPFLAGS+= -D CTRACER_ON -include ctracer.h -g
26 27
27   -_src = dio.c ldt.c ldt_plat_dev.c ldt_plat_drv.c ctracer.h
  28 +_src = dio.c ldt.c ldt_plat_dev.c ldt_plat_drv.c ctracer.h ldt_configfs_basic.c
28 29
29 30 checkpatch:
30 31 /usr/src/linux-headers-$(shell uname -r)/scripts/checkpatch.pl --no-tree --show-types --ignore LONG_LINE,LINE_CONTINUATIONS --terse -f $(_src) Makefile
2  README.md
Source Rendered
@@ -2,7 +2,7 @@
2 2
3 3 LDT project is useful for Linux driver development beginners and as starting point for a new drivers.
4 4 The driver uses following Linux facilities:
5   -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.
  5 +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.
6 6
7 7 ## Usage:
8 8
93 ldt_configfs_basic.c
... ... @@ -0,0 +1,93 @@
  1 +/*
  2 + * LDT - Linux Driver Template - basic configfs
  3 + *
  4 + * Copyright (C) 2012 Constantine Shulyupin http://www.makelinux.net/
  5 + *
  6 + * Dual BSD/GPL License
  7 + *
  8 + * based on configfs_example_explicit.c and much more simple, without containers, just 70 LOC
  9 + *
  10 + * Sample usage:
  11 + * sudo insmod ldt_configfs_basic.ko
  12 + * ls /configfs/ldt_configfs_basic/
  13 + * sudo sh -c "echo 123 > /configfs/ldt_configfs_basic/parameter"
  14 + * cat /configfs/ldt_configfs_basic/parameter
  15 + *
  16 + */
  17 +
  18 +#include <linux/module.h>
  19 +#include <linux/configfs.h>
  20 +
  21 +static int parameter;
  22 +
  23 +static struct configfs_attribute ldt_parameter_attr = {
  24 + .ca_owner = THIS_MODULE, .ca_name = "parameter", .ca_mode = S_IRUGO | S_IWUSR };
  25 +
  26 +static struct configfs_attribute ldt_description_attr = {
  27 + .ca_owner = THIS_MODULE, .ca_name = "description", .ca_mode = S_IRUGO };
  28 +
  29 +static struct configfs_attribute *ldt_attrs[] = {
  30 + &ldt_description_attr,
  31 + &ldt_parameter_attr,
  32 + NULL,
  33 +};
  34 +
  35 +static ssize_t ldt_attr_show(struct config_item *item, struct configfs_attribute *attr, char *page)
  36 +{
  37 + ssize_t ret = -EINVAL;
  38 + if (attr == &ldt_description_attr)
  39 + return sprintf(page, "basic sample of configfs\n");
  40 + else if (attr == &ldt_parameter_attr)
  41 + return sprintf(page, "%d\n", parameter);
  42 + return ret;
  43 +}
  44 +
  45 +static ssize_t ldt_attr_store(struct config_item *item, struct configfs_attribute *attr, const char *page, size_t count)
  46 +{
  47 + ssize_t ret = -EINVAL;
  48 + if (attr == &ldt_parameter_attr) {
  49 + ret = kstrtoint(page, 0, &parameter);
  50 + if (ret)
  51 + return ret;
  52 + ret = count;
  53 + }
  54 + return ret;
  55 +}
  56 +
  57 +static struct config_item_type ci_type = {
  58 + .ct_item_ops = (struct configfs_item_operations[]){ {
  59 + .show_attribute = ldt_attr_show,
  60 + .store_attribute = ldt_attr_store,} },
  61 + .ct_attrs = ldt_attrs,
  62 + .ct_owner = THIS_MODULE,
  63 +};
  64 +
  65 +static struct configfs_subsystem ldt_subsys = {
  66 + .su_group = {
  67 + .cg_item = {
  68 + .ci_namebuf = KBUILD_MODNAME,
  69 + .ci_type = &ci_type,
  70 + },
  71 + },
  72 +};
  73 +
  74 +static int __init configfs_example_init(void)
  75 +{
  76 + int ret;
  77 + config_group_init(&ldt_subsys.su_group);
  78 + mutex_init(&ldt_subsys.su_mutex);
  79 + ret = configfs_register_subsystem(&ldt_subsys);
  80 + if (ret)
  81 + printk(KERN_ERR "Error %d while registering subsystem %s\n",
  82 + ret, ldt_subsys.su_group.cg_item.ci_namebuf);
  83 + return ret;
  84 +}
  85 +
  86 +static void __exit configfs_example_exit(void)
  87 +{
  88 + configfs_unregister_subsystem(&ldt_subsys);
  89 +}
  90 +
  91 +module_init(configfs_example_init);
  92 +module_exit(configfs_example_exit);
  93 +MODULE_LICENSE("GPL");

0 comments on commit a860d2e

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