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