xref: /linux/samples/kfifo/bytestream-example.c (revision 2aaf2092c168fc02df0645415f524b357ee7ec2e)
1 /*
2  * Sample kfifo byte stream 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 byte stream fifo.
18  */
19 
20 /* fifo size in elements (bytes) */
21 #define FIFO_SIZE	32
22 
23 /* name of the proc entry */
24 #define	PROC_FIFO	"bytestream-fifo"
25 
26 /* lock for procfs read access */
27 static DEFINE_MUTEX(read_lock);
28 
29 /* lock for procfs write access */
30 static 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
42 static struct kfifo test;
43 #else
44 static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
45 #endif
46 
47 static unsigned char 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 
54 static int __init testfunc(void)
55 {
56 	unsigned char	buf[6];
57 	unsigned char	i, j;
58 	unsigned int	ret;
59 
60 	printk(KERN_INFO "byte stream fifo test start\n");
61 
62 	/* put string into the fifo */
63 	kfifo_in(&test, "hello", 5);
64 
65 	/* put values into the fifo */
66 	for (i = 0; i != 10; i++)
67 		kfifo_put(&test, &i);
68 
69 	/* show the number of used elements */
70 	printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
71 
72 	/* get max of 5 bytes from the fifo */
73 	i = kfifo_out(&test, buf, 5);
74 	printk(KERN_INFO "buf: %.*s\n", i, buf);
75 
76 	/* get max of 2 elements from the fifo */
77 	ret = kfifo_out(&test, buf, 2);
78 	printk(KERN_INFO "ret: %d\n", ret);
79 	/* and put it back to the end of the fifo */
80 	ret = kfifo_in(&test, buf, ret);
81 	printk(KERN_INFO "ret: %d\n", ret);
82 
83 	/* skip first element of the fifo */
84 	printk(KERN_INFO "skip 1st element\n");
85 	kfifo_skip(&test);
86 
87 	/* put values into the fifo until is full */
88 	for (i = 20; kfifo_put(&test, &i); i++)
89 		;
90 
91 	printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
92 
93 	/* check the correctness of all values in the fifo */
94 	j = 0;
95 	while (kfifo_get(&test, &i)) {
96 		if (i != expected_result[j++]) {
97 			printk(KERN_WARNING "value mismatch: test failed\n");
98 			return -EIO;
99 		}
100 	}
101 	if (j != ARRAY_SIZE(expected_result)) {
102 		printk(KERN_WARNING "size mismatch: test failed\n");
103 		return -EIO;
104 	}
105 	printk(KERN_INFO "test passed\n");
106 
107 	return 0;
108 }
109 
110 static ssize_t fifo_write(struct file *file, const char __user *buf,
111 						size_t count, loff_t *ppos)
112 {
113 	int ret;
114 	unsigned int copied;
115 
116 	if (mutex_lock_interruptible(&write_lock))
117 		return -ERESTARTSYS;
118 
119 	ret = kfifo_from_user(&test, buf, count, &copied);
120 
121 	mutex_unlock(&write_lock);
122 
123 	return ret ? ret : copied;
124 }
125 
126 static ssize_t fifo_read(struct file *file, char __user *buf,
127 						size_t count, loff_t *ppos)
128 {
129 	int ret;
130 	unsigned int copied;
131 
132 	if (mutex_lock_interruptible(&read_lock))
133 		return -ERESTARTSYS;
134 
135 	ret = kfifo_to_user(&test, buf, count, &copied);
136 
137 	mutex_unlock(&read_lock);
138 
139 	return ret ? ret : copied;
140 }
141 
142 static const struct file_operations fifo_fops = {
143 	.owner		= THIS_MODULE,
144 	.read		= fifo_read,
145 	.write		= fifo_write,
146 };
147 
148 static int __init example_init(void)
149 {
150 #ifdef DYNAMIC
151 	int ret;
152 
153 	ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
154 	if (ret) {
155 		printk(KERN_ERR "error kfifo_alloc\n");
156 		return ret;
157 	}
158 #else
159 	INIT_KFIFO(test);
160 #endif
161 	if (testfunc() < 0) {
162 #ifdef DYNAMIC
163 		kfifo_free(&test);
164 #endif
165 		return -EIO;
166 	}
167 
168 	if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
169 #ifdef DYNAMIC
170 		kfifo_free(&test);
171 #endif
172 		return -ENOMEM;
173 	}
174 	return 0;
175 }
176 
177 static void __exit example_exit(void)
178 {
179 	remove_proc_entry(PROC_FIFO, NULL);
180 #ifdef DYNAMIC
181 	kfifo_free(&test);
182 #endif
183 }
184 
185 module_init(example_init);
186 module_exit(example_exit);
187 MODULE_LICENSE("GPL");
188 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");
189