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