xref: /OK3568_Linux_fs/kernel/samples/kfifo/record-example.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Sample dynamic sized record fifo implementation
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/init.h>
9*4882a593Smuzhiyun #include <linux/module.h>
10*4882a593Smuzhiyun #include <linux/proc_fs.h>
11*4882a593Smuzhiyun #include <linux/mutex.h>
12*4882a593Smuzhiyun #include <linux/kfifo.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun  * This module shows how to create a variable sized record fifo.
16*4882a593Smuzhiyun  */
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /* fifo size in elements (bytes) */
19*4882a593Smuzhiyun #define FIFO_SIZE	128
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /* name of the proc entry */
22*4882a593Smuzhiyun #define	PROC_FIFO	"record-fifo"
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /* lock for procfs read access */
25*4882a593Smuzhiyun static DEFINE_MUTEX(read_lock);
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun /* lock for procfs write access */
28*4882a593Smuzhiyun static DEFINE_MUTEX(write_lock);
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun  * define DYNAMIC in this example for a dynamically allocated fifo.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * Otherwise the fifo storage will be a part of the fifo structure.
34*4882a593Smuzhiyun  */
35*4882a593Smuzhiyun #if 0
36*4882a593Smuzhiyun #define DYNAMIC
37*4882a593Smuzhiyun #endif
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun /*
40*4882a593Smuzhiyun  * struct kfifo_rec_ptr_1 and  STRUCT_KFIFO_REC_1 can handle records of a
41*4882a593Smuzhiyun  * length between 0 and 255 bytes.
42*4882a593Smuzhiyun  *
43*4882a593Smuzhiyun  * struct kfifo_rec_ptr_2 and  STRUCT_KFIFO_REC_2 can handle records of a
44*4882a593Smuzhiyun  * length between 0 and 65535 bytes.
45*4882a593Smuzhiyun  */
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun #ifdef DYNAMIC
48*4882a593Smuzhiyun struct kfifo_rec_ptr_1 test;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun #else
51*4882a593Smuzhiyun typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static mytest test;
54*4882a593Smuzhiyun #endif
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static const char *expected_result[] = {
57*4882a593Smuzhiyun 	"a",
58*4882a593Smuzhiyun 	"bb",
59*4882a593Smuzhiyun 	"ccc",
60*4882a593Smuzhiyun 	"dddd",
61*4882a593Smuzhiyun 	"eeeee",
62*4882a593Smuzhiyun 	"ffffff",
63*4882a593Smuzhiyun 	"ggggggg",
64*4882a593Smuzhiyun 	"hhhhhhhh",
65*4882a593Smuzhiyun 	"iiiiiiiii",
66*4882a593Smuzhiyun 	"jjjjjjjjjj",
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun 
testfunc(void)69*4882a593Smuzhiyun static int __init testfunc(void)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	char		buf[100];
72*4882a593Smuzhiyun 	unsigned int	i;
73*4882a593Smuzhiyun 	unsigned int	ret;
74*4882a593Smuzhiyun 	struct { unsigned char buf[6]; } hello = { "hello" };
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	printk(KERN_INFO "record fifo test start\n");
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	kfifo_in(&test, &hello, sizeof(hello));
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	/* show the size of the next record in the fifo */
81*4882a593Smuzhiyun 	printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test));
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	/* put in variable length data */
84*4882a593Smuzhiyun 	for (i = 0; i < 10; i++) {
85*4882a593Smuzhiyun 		memset(buf, 'a' + i, i + 1);
86*4882a593Smuzhiyun 		kfifo_in(&test, buf, i + 1);
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/* skip first element of the fifo */
90*4882a593Smuzhiyun 	printk(KERN_INFO "skip 1st element\n");
91*4882a593Smuzhiyun 	kfifo_skip(&test);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	/* show the first record without removing from the fifo */
96*4882a593Smuzhiyun 	ret = kfifo_out_peek(&test, buf, sizeof(buf));
97*4882a593Smuzhiyun 	if (ret)
98*4882a593Smuzhiyun 		printk(KERN_INFO "%.*s\n", ret, buf);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	/* check the correctness of all values in the fifo */
101*4882a593Smuzhiyun 	i = 0;
102*4882a593Smuzhiyun 	while (!kfifo_is_empty(&test)) {
103*4882a593Smuzhiyun 		ret = kfifo_out(&test, buf, sizeof(buf));
104*4882a593Smuzhiyun 		buf[ret] = '\0';
105*4882a593Smuzhiyun 		printk(KERN_INFO "item = %.*s\n", ret, buf);
106*4882a593Smuzhiyun 		if (strcmp(buf, expected_result[i++])) {
107*4882a593Smuzhiyun 			printk(KERN_WARNING "value mismatch: test failed\n");
108*4882a593Smuzhiyun 			return -EIO;
109*4882a593Smuzhiyun 		}
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 	if (i != ARRAY_SIZE(expected_result)) {
112*4882a593Smuzhiyun 		printk(KERN_WARNING "size mismatch: test failed\n");
113*4882a593Smuzhiyun 		return -EIO;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 	printk(KERN_INFO "test passed\n");
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	return 0;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun 
fifo_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)120*4882a593Smuzhiyun static ssize_t fifo_write(struct file *file, const char __user *buf,
121*4882a593Smuzhiyun 						size_t count, loff_t *ppos)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	int ret;
124*4882a593Smuzhiyun 	unsigned int copied;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	if (mutex_lock_interruptible(&write_lock))
127*4882a593Smuzhiyun 		return -ERESTARTSYS;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	ret = kfifo_from_user(&test, buf, count, &copied);
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	mutex_unlock(&write_lock);
132*4882a593Smuzhiyun 	if (ret)
133*4882a593Smuzhiyun 		return ret;
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	return copied;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
fifo_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)138*4882a593Smuzhiyun static ssize_t fifo_read(struct file *file, char __user *buf,
139*4882a593Smuzhiyun 						size_t count, loff_t *ppos)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun 	int ret;
142*4882a593Smuzhiyun 	unsigned int copied;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	if (mutex_lock_interruptible(&read_lock))
145*4882a593Smuzhiyun 		return -ERESTARTSYS;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	ret = kfifo_to_user(&test, buf, count, &copied);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	mutex_unlock(&read_lock);
150*4882a593Smuzhiyun 	if (ret)
151*4882a593Smuzhiyun 		return ret;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	return copied;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun static const struct proc_ops fifo_proc_ops = {
157*4882a593Smuzhiyun 	.proc_read	= fifo_read,
158*4882a593Smuzhiyun 	.proc_write	= fifo_write,
159*4882a593Smuzhiyun 	.proc_lseek	= noop_llseek,
160*4882a593Smuzhiyun };
161*4882a593Smuzhiyun 
example_init(void)162*4882a593Smuzhiyun static int __init example_init(void)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun #ifdef DYNAMIC
165*4882a593Smuzhiyun 	int ret;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
168*4882a593Smuzhiyun 	if (ret) {
169*4882a593Smuzhiyun 		printk(KERN_ERR "error kfifo_alloc\n");
170*4882a593Smuzhiyun 		return ret;
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun #else
173*4882a593Smuzhiyun 	INIT_KFIFO(test);
174*4882a593Smuzhiyun #endif
175*4882a593Smuzhiyun 	if (testfunc() < 0) {
176*4882a593Smuzhiyun #ifdef DYNAMIC
177*4882a593Smuzhiyun 		kfifo_free(&test);
178*4882a593Smuzhiyun #endif
179*4882a593Smuzhiyun 		return -EIO;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	if (proc_create(PROC_FIFO, 0, NULL, &fifo_proc_ops) == NULL) {
183*4882a593Smuzhiyun #ifdef DYNAMIC
184*4882a593Smuzhiyun 		kfifo_free(&test);
185*4882a593Smuzhiyun #endif
186*4882a593Smuzhiyun 		return -ENOMEM;
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 	return 0;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
example_exit(void)191*4882a593Smuzhiyun static void __exit example_exit(void)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun 	remove_proc_entry(PROC_FIFO, NULL);
194*4882a593Smuzhiyun #ifdef DYNAMIC
195*4882a593Smuzhiyun 	kfifo_free(&test);
196*4882a593Smuzhiyun #endif
197*4882a593Smuzhiyun }
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun module_init(example_init);
200*4882a593Smuzhiyun module_exit(example_exit);
201*4882a593Smuzhiyun MODULE_LICENSE("GPL");
202*4882a593Smuzhiyun MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");
203