From 504db8a2bae95d7bafb9b68074fdc46c49f08c9f Mon Sep 17 00:00:00 2001 From: Manuel Traut Date: Fri, 4 Jan 2019 09:32:18 +0100 Subject: [PATCH] klist example this can be used to show the usage of klists Signed-off-by: Manuel Traut --- drivers/staging/Kconfig | 2 + drivers/staging/Makefile | 1 + drivers/staging/klist/Kconfig | 5 +++ drivers/staging/klist/Makefile | 1 + drivers/staging/klist/klist.c | 68 +++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 drivers/staging/klist/Kconfig create mode 100644 drivers/staging/klist/Makefile create mode 100644 drivers/staging/klist/klist.c diff --git a/drivers/staging/Kconfig b/drivers/staging/Kconfig index bfa2bf720a03..98541d7a41be 100644 --- a/drivers/staging/Kconfig +++ b/drivers/staging/Kconfig @@ -24,6 +24,8 @@ menuconfig STAGING if STAGING +source "drivers/staging/klist/Kconfig" + source "drivers/staging/wlan-ng/Kconfig" source "drivers/staging/comedi/Kconfig" diff --git a/drivers/staging/Makefile b/drivers/staging/Makefile index 25b3a8eafaa1..7a6d82750008 100644 --- a/drivers/staging/Makefile +++ b/drivers/staging/Makefile @@ -2,6 +2,7 @@ # Makefile for staging directory obj-y += media/ +obj-$(CONFIG_KLISTEXAMPLE) += klist/ obj-$(CONFIG_PRISM2_USB) += wlan-ng/ obj-$(CONFIG_COMEDI) += comedi/ obj-$(CONFIG_FB_OLPC_DCON) += olpc_dcon/ diff --git a/drivers/staging/klist/Kconfig b/drivers/staging/klist/Kconfig new file mode 100644 index 000000000000..18f517e56cb9 --- /dev/null +++ b/drivers/staging/klist/Kconfig @@ -0,0 +1,4 @@ +config KLISTEXAMPLE + tristate "module to teach klist usage" + ---help--- + This is only useful for learning the usage of klist. diff --git a/drivers/staging/klist/Makefile b/drivers/staging/klist/Makefile new file mode 100644 index 000000000000..2939c11d72ff --- /dev/null +++ b/drivers/staging/klist/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_KLISTEXAMPLE) += klist.o diff --git a/drivers/staging/klist/klist.c b/drivers/staging/klist/klist.c new file mode 100644 index 000000000000..365acaa3263a --- /dev/null +++ b/drivers/staging/klist/klist.c @@ -0,0 +1,68 @@ +/* + * Example driver for using klist + * Copyright (C) 2019, Manuel Traut + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + */ + +#include +#include +#include +#include + +#define DRV_NAME "klist" + +struct mynode { + struct list_head node; + unsigned long val; +}; + +static struct list_head mylist; +static char mystring[] = "1 2 3 4 5\n"; + +static void klist_free_list(void) { + struct mynode *n, *tmp; + list_for_each_entry_safe(n, tmp, &mylist, node) + kfree(n); +} + +static void klist_init_list(void) { + unsigned long val; + struct mynode *n; + char *tok; + char *b = kstrdup(mystring, GFP_KERNEL); + + INIT_LIST_HEAD(&mylist); + + while ((tok = strsep(&b, " ")) && *tok) { + printk(KERN_INFO "adding %s to list\n", tok); + if (kstrtoul(tok, 0, &val)) { + printk(KERN_ERR "failed to convert %s to int\n", tok); + goto exit_init; + } + n = kzalloc(sizeof(*n), GFP_KERNEL); + n->val = val; + list_add_tail(&n->node, &mylist); + } +exit_init: + kfree(b); +} + +static int __init klist_init(void) { + printk(KERN_INFO "%s\n", __func__); + klist_init_list(); + return 0; +} + +static void __exit klist_exit(void) { + printk(KERN_INFO "%s\n", __func__); + klist_free_list(); +} + +module_init(klist_init); +module_exit(klist_exit); +MODULE_AUTHOR("Manuel Traut"); +MODULE_LICENSE("GPL"); -- 2.19.2