ASoC: WM8904: Set `invert' bit for Capture Switch
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / samples / kfifo / record-example.c
blob2d7529eeb2940a8d6f92cc2104e0f8379dcb74ca
1 /*
2 * Sample dynamic sized record fifo implementation
4 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
6 * Released under the GPL version 2 only.
8 */
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 variable sized record fifo.
20 /* fifo size in elements (bytes) */
21 #define FIFO_SIZE 128
23 /* name of the proc entry */
24 #define PROC_FIFO "record-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.
37 #if 0
38 #define DYNAMIC
39 #endif
42 * struct kfifo_rec_ptr_1 and STRUCT_KFIFO_REC_1 can handle records of a
43 * length between 0 and 255 bytes.
45 * struct kfifo_rec_ptr_2 and STRUCT_KFIFO_REC_2 can handle records of a
46 * length between 0 and 65535 bytes.
49 #ifdef DYNAMIC
50 struct kfifo_rec_ptr_1 test;
52 #else
53 typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest;
55 static mytest test;
56 #endif
58 static const char *expected_result[] = {
59 "a",
60 "bb",
61 "ccc",
62 "dddd",
63 "eeeee",
64 "ffffff",
65 "ggggggg",
66 "hhhhhhhh",
67 "iiiiiiiii",
68 "jjjjjjjjjj",
71 static int __init testfunc(void)
73 char buf[100];
74 unsigned int i;
75 unsigned int ret;
76 struct { unsigned char buf[6]; } hello = { "hello" };
78 printk(KERN_INFO "record fifo test start\n");
80 kfifo_in(&test, &hello, sizeof(hello));
82 /* show the size of the next record in the fifo */
83 printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test));
85 /* put in variable length data */
86 for (i = 0; i < 10; i++) {
87 memset(buf, 'a' + i, i + 1);
88 kfifo_in(&test, buf, i + 1);
91 /* skip first element of the fifo */
92 printk(KERN_INFO "skip 1st element\n");
93 kfifo_skip(&test);
95 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
97 /* show the first record without removing from the fifo */
98 ret = kfifo_out_peek(&test, buf, sizeof(buf));
99 if (ret)
100 printk(KERN_INFO "%.*s\n", ret, buf);
102 /* check the correctness of all values in the fifo */
103 i = 0;
104 while (!kfifo_is_empty(&test)) {
105 ret = kfifo_out(&test, buf, sizeof(buf));
106 buf[ret] = '\0';
107 printk(KERN_INFO "item = %.*s\n", ret, buf);
108 if (strcmp(buf, expected_result[i++])) {
109 printk(KERN_WARNING "value mismatch: test failed\n");
110 return -EIO;
113 if (i != ARRAY_SIZE(expected_result)) {
114 printk(KERN_WARNING "size mismatch: test failed\n");
115 return -EIO;
117 printk(KERN_INFO "test passed\n");
119 return 0;
122 static ssize_t fifo_write(struct file *file, const char __user *buf,
123 size_t count, loff_t *ppos)
125 int ret;
126 unsigned int copied;
128 if (mutex_lock_interruptible(&write_lock))
129 return -ERESTARTSYS;
131 ret = kfifo_from_user(&test, buf, count, &copied);
133 mutex_unlock(&write_lock);
135 return ret ? ret : copied;
138 static ssize_t fifo_read(struct file *file, char __user *buf,
139 size_t count, loff_t *ppos)
141 int ret;
142 unsigned int copied;
144 if (mutex_lock_interruptible(&read_lock))
145 return -ERESTARTSYS;
147 ret = kfifo_to_user(&test, buf, count, &copied);
149 mutex_unlock(&read_lock);
151 return ret ? ret : copied;
154 static const struct file_operations fifo_fops = {
155 .owner = THIS_MODULE,
156 .read = fifo_read,
157 .write = fifo_write,
158 .llseek = noop_llseek,
161 static int __init example_init(void)
163 #ifdef DYNAMIC
164 int ret;
166 ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
167 if (ret) {
168 printk(KERN_ERR "error kfifo_alloc\n");
169 return ret;
171 #else
172 INIT_KFIFO(test);
173 #endif
174 if (testfunc() < 0) {
175 #ifdef DYNAMIC
176 kfifo_free(&test);
177 #endif
178 return -EIO;
181 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
182 #ifdef DYNAMIC
183 kfifo_free(&test);
184 #endif
185 return -ENOMEM;
187 return 0;
190 static void __exit example_exit(void)
192 remove_proc_entry(PROC_FIFO, NULL);
193 #ifdef DYNAMIC
194 kfifo_free(&test);
195 #endif
198 module_init(example_init);
199 module_exit(example_exit);
200 MODULE_LICENSE("GPL");
201 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");