Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Sample kfifo int type implementation
4 *
5 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
6 */
7
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/proc_fs.h>
11#include <linux/mutex.h>
12#include <linux/kfifo.h>
13
14/*
15 * This module shows how to create a int type fifo.
16 */
17
18/* fifo size in elements (ints) */
19#define FIFO_SIZE 32
20
21/* name of the proc entry */
22#define PROC_FIFO "int-fifo"
23
24/* lock for procfs read access */
25static DEFINE_MUTEX(read_lock);
26
27/* lock for procfs write access */
28static DEFINE_MUTEX(write_lock);
29
30/*
31 * define DYNAMIC in this example for a dynamically allocated fifo.
32 *
33 * Otherwise the fifo storage will be a part of the fifo structure.
34 */
35#if 0
36#define DYNAMIC
37#endif
38
39#ifdef DYNAMIC
40static DECLARE_KFIFO_PTR(test, int);
41#else
42static DEFINE_KFIFO(test, int, FIFO_SIZE);
43#endif
44
45static const int expected_result[FIFO_SIZE] = {
46 3, 4, 5, 6, 7, 8, 9, 0,
47 1, 20, 21, 22, 23, 24, 25, 26,
48 27, 28, 29, 30, 31, 32, 33, 34,
49 35, 36, 37, 38, 39, 40, 41, 42,
50};
51
52static int __init testfunc(void)
53{
54 int buf[6];
55 int i, j;
56 unsigned int ret;
57
58 printk(KERN_INFO "int fifo test start\n");
59
60 /* put values into the fifo */
61 for (i = 0; i != 10; i++)
62 kfifo_put(&test, i);
63
64 /* show the number of used elements */
65 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
66
67 /* get max of 2 elements from the fifo */
68 ret = kfifo_out(&test, buf, 2);
69 printk(KERN_INFO "ret: %d\n", ret);
70 /* and put it back to the end of the fifo */
71 ret = kfifo_in(&test, buf, ret);
72 printk(KERN_INFO "ret: %d\n", ret);
73
74 /* skip first element of the fifo */
75 printk(KERN_INFO "skip 1st element\n");
76 kfifo_skip(&test);
77
78 /* put values into the fifo until is full */
79 for (i = 20; kfifo_put(&test, i); i++)
80 ;
81
82 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
83
84 /* show the first value without removing from the fifo */
85 if (kfifo_peek(&test, &i))
86 printk(KERN_INFO "%d\n", i);
87
88 /* check the correctness of all values in the fifo */
89 j = 0;
90 while (kfifo_get(&test, &i)) {
91 printk(KERN_INFO "item = %d\n", i);
92 if (i != expected_result[j++]) {
93 printk(KERN_WARNING "value mismatch: test failed\n");
94 return -EIO;
95 }
96 }
97 if (j != ARRAY_SIZE(expected_result)) {
98 printk(KERN_WARNING "size mismatch: test failed\n");
99 return -EIO;
100 }
101 printk(KERN_INFO "test passed\n");
102
103 return 0;
104}
105
106static ssize_t fifo_write(struct file *file, const char __user *buf,
107 size_t count, loff_t *ppos)
108{
109 int ret;
110 unsigned int copied;
111
112 if (mutex_lock_interruptible(&write_lock))
113 return -ERESTARTSYS;
114
115 ret = kfifo_from_user(&test, buf, count, &copied);
116
117 mutex_unlock(&write_lock);
118
119 return ret ? ret : copied;
120}
121
122static ssize_t fifo_read(struct file *file, char __user *buf,
123 size_t count, loff_t *ppos)
124{
125 int ret;
126 unsigned int copied;
127
128 if (mutex_lock_interruptible(&read_lock))
129 return -ERESTARTSYS;
130
131 ret = kfifo_to_user(&test, buf, count, &copied);
132
133 mutex_unlock(&read_lock);
134
135 return ret ? ret : copied;
136}
137
138static const struct proc_ops fifo_proc_ops = {
139 .proc_read = fifo_read,
140 .proc_write = fifo_write,
141 .proc_lseek = noop_llseek,
142};
143
144static int __init example_init(void)
145{
146#ifdef DYNAMIC
147 int ret;
148
149 ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
150 if (ret) {
151 printk(KERN_ERR "error kfifo_alloc\n");
152 return ret;
153 }
154#endif
155 if (testfunc() < 0) {
156#ifdef DYNAMIC
157 kfifo_free(&test);
158#endif
159 return -EIO;
160 }
161
162 if (proc_create(PROC_FIFO, 0, NULL, &fifo_proc_ops) == NULL) {
163#ifdef DYNAMIC
164 kfifo_free(&test);
165#endif
166 return -ENOMEM;
167 }
168 return 0;
169}
170
171static void __exit example_exit(void)
172{
173 remove_proc_entry(PROC_FIFO, NULL);
174#ifdef DYNAMIC
175 kfifo_free(&test);
176#endif
177}
178
179module_init(example_init);
180module_exit(example_exit);
181MODULE_LICENSE("GPL");
182MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");
1/*
2 * Sample kfifo int type implementation
3 *
4 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
5 *
6 * Released under the GPL version 2 only.
7 *
8 */
9
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/proc_fs.h>
13#include <linux/mutex.h>
14#include <linux/kfifo.h>
15
16/*
17 * This module shows how to create a int type fifo.
18 */
19
20/* fifo size in elements (ints) */
21#define FIFO_SIZE 32
22
23/* name of the proc entry */
24#define PROC_FIFO "int-fifo"
25
26/* lock for procfs read access */
27static DEFINE_MUTEX(read_lock);
28
29/* lock for procfs write access */
30static DEFINE_MUTEX(write_lock);
31
32/*
33 * define DYNAMIC in this example for a dynamically allocated fifo.
34 *
35 * Otherwise the fifo storage will be a part of the fifo structure.
36 */
37#if 0
38#define DYNAMIC
39#endif
40
41#ifdef DYNAMIC
42static DECLARE_KFIFO_PTR(test, int);
43#else
44static DEFINE_KFIFO(test, int, FIFO_SIZE);
45#endif
46
47static const int expected_result[FIFO_SIZE] = {
48 3, 4, 5, 6, 7, 8, 9, 0,
49 1, 20, 21, 22, 23, 24, 25, 26,
50 27, 28, 29, 30, 31, 32, 33, 34,
51 35, 36, 37, 38, 39, 40, 41, 42,
52};
53
54static int __init testfunc(void)
55{
56 int buf[6];
57 int i, j;
58 unsigned int ret;
59
60 printk(KERN_INFO "int fifo test start\n");
61
62 /* put values into the fifo */
63 for (i = 0; i != 10; i++)
64 kfifo_put(&test, i);
65
66 /* show the number of used elements */
67 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
68
69 /* get max of 2 elements from the fifo */
70 ret = kfifo_out(&test, buf, 2);
71 printk(KERN_INFO "ret: %d\n", ret);
72 /* and put it back to the end of the fifo */
73 ret = kfifo_in(&test, buf, ret);
74 printk(KERN_INFO "ret: %d\n", ret);
75
76 /* skip first element of the fifo */
77 printk(KERN_INFO "skip 1st element\n");
78 kfifo_skip(&test);
79
80 /* put values into the fifo until is full */
81 for (i = 20; kfifo_put(&test, i); i++)
82 ;
83
84 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
85
86 /* show the first value without removing from the fifo */
87 if (kfifo_peek(&test, &i))
88 printk(KERN_INFO "%d\n", i);
89
90 /* check the correctness of all values in the fifo */
91 j = 0;
92 while (kfifo_get(&test, &i)) {
93 printk(KERN_INFO "item = %d\n", i);
94 if (i != expected_result[j++]) {
95 printk(KERN_WARNING "value mismatch: test failed\n");
96 return -EIO;
97 }
98 }
99 if (j != ARRAY_SIZE(expected_result)) {
100 printk(KERN_WARNING "size mismatch: test failed\n");
101 return -EIO;
102 }
103 printk(KERN_INFO "test passed\n");
104
105 return 0;
106}
107
108static ssize_t fifo_write(struct file *file, const char __user *buf,
109 size_t count, loff_t *ppos)
110{
111 int ret;
112 unsigned int copied;
113
114 if (mutex_lock_interruptible(&write_lock))
115 return -ERESTARTSYS;
116
117 ret = kfifo_from_user(&test, buf, count, &copied);
118
119 mutex_unlock(&write_lock);
120
121 return ret ? ret : copied;
122}
123
124static ssize_t fifo_read(struct file *file, char __user *buf,
125 size_t count, loff_t *ppos)
126{
127 int ret;
128 unsigned int copied;
129
130 if (mutex_lock_interruptible(&read_lock))
131 return -ERESTARTSYS;
132
133 ret = kfifo_to_user(&test, buf, count, &copied);
134
135 mutex_unlock(&read_lock);
136
137 return ret ? ret : copied;
138}
139
140static const struct file_operations fifo_fops = {
141 .owner = THIS_MODULE,
142 .read = fifo_read,
143 .write = fifo_write,
144 .llseek = noop_llseek,
145};
146
147static int __init example_init(void)
148{
149#ifdef DYNAMIC
150 int ret;
151
152 ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
153 if (ret) {
154 printk(KERN_ERR "error kfifo_alloc\n");
155 return ret;
156 }
157#endif
158 if (testfunc() < 0) {
159#ifdef DYNAMIC
160 kfifo_free(&test);
161#endif
162 return -EIO;
163 }
164
165 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
166#ifdef DYNAMIC
167 kfifo_free(&test);
168#endif
169 return -ENOMEM;
170 }
171 return 0;
172}
173
174static void __exit example_exit(void)
175{
176 remove_proc_entry(PROC_FIFO, NULL);
177#ifdef DYNAMIC
178 kfifo_free(&test);
179#endif
180}
181
182module_init(example_init);
183module_exit(example_exit);
184MODULE_LICENSE("GPL");
185MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");