2 * Sample kfifo int type implementation
4 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
6 * Released under the GPL version 2 only.
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>
17 * This module shows how to create a int type fifo.
20 /* fifo size in elements (ints) */
23 /* name of the proc entry */
24 #define PROC_FIFO "int-fifo"
26 /* lock for procfs read access */
27 static DEFINE_MUTEX(read_lock
);
29 /* lock for procfs write access */
30 static DEFINE_MUTEX(write_lock
);
33 * define DYNAMIC in this example for a dynamically allocated fifo.
35 * Otherwise the fifo storage will be a part of the fifo structure.
42 static DECLARE_KFIFO_PTR(test
, int);
44 static DEFINE_KFIFO(test
, int, FIFO_SIZE
);
47 static 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,
54 static int __init
testfunc(void)
60 printk(KERN_INFO
"int fifo test start\n");
62 /* put values into the fifo */
63 for (i
= 0; i
!= 10; i
++)
66 /* show the number of used elements */
67 printk(KERN_INFO
"fifo len: %u\n", kfifo_len(&test
));
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
);
76 /* skip first element of the fifo */
77 printk(KERN_INFO
"skip 1st element\n");
80 /* put values into the fifo until is full */
81 for (i
= 20; kfifo_put(&test
, i
); i
++)
84 printk(KERN_INFO
"queue len: %u\n", kfifo_len(&test
));
86 /* show the first value without removing from the fifo */
87 if (kfifo_peek(&test
, &i
))
88 printk(KERN_INFO
"%d\n", i
);
90 /* check the correctness of all values in the fifo */
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");
99 if (j
!= ARRAY_SIZE(expected_result
)) {
100 printk(KERN_WARNING
"size mismatch: test failed\n");
103 printk(KERN_INFO
"test passed\n");
108 static ssize_t
fifo_write(struct file
*file
, const char __user
*buf
,
109 size_t count
, loff_t
*ppos
)
114 if (mutex_lock_interruptible(&write_lock
))
117 ret
= kfifo_from_user(&test
, buf
, count
, &copied
);
119 mutex_unlock(&write_lock
);
121 return ret
? ret
: copied
;
124 static ssize_t
fifo_read(struct file
*file
, char __user
*buf
,
125 size_t count
, loff_t
*ppos
)
130 if (mutex_lock_interruptible(&read_lock
))
133 ret
= kfifo_to_user(&test
, buf
, count
, &copied
);
135 mutex_unlock(&read_lock
);
137 return ret
? ret
: copied
;
140 static const struct file_operations fifo_fops
= {
141 .owner
= THIS_MODULE
,
144 .llseek
= noop_llseek
,
147 static int __init
example_init(void)
152 ret
= kfifo_alloc(&test
, FIFO_SIZE
, GFP_KERNEL
);
154 printk(KERN_ERR
"error kfifo_alloc\n");
158 if (testfunc() < 0) {
165 if (proc_create(PROC_FIFO
, 0, NULL
, &fifo_fops
) == NULL
) {
174 static void __exit
example_exit(void)
176 remove_proc_entry(PROC_FIFO
, NULL
);
182 module_init(example_init
);
183 module_exit(example_exit
);
184 MODULE_LICENSE("GPL");
185 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");