Merge branch 'message-callback' into kbuild/kconfig
[pandora-kernel.git] / samples / kfifo / record-example.c
1 /*
2  * Sample dynamic sized record fifo 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 variable sized record fifo.
18  */
19
20 /* fifo size in elements (bytes) */
21 #define FIFO_SIZE       128
22
23 /* name of the proc entry */
24 #define PROC_FIFO       "record-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 /*
42  * struct kfifo_rec_ptr_1 and  STRUCT_KFIFO_REC_1 can handle records of a
43  * length between 0 and 255 bytes.
44  *
45  * struct kfifo_rec_ptr_2 and  STRUCT_KFIFO_REC_2 can handle records of a
46  * length between 0 and 65535 bytes.
47  */
48
49 #ifdef DYNAMIC
50 struct kfifo_rec_ptr_1 test;
51
52 #else
53 typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest;
54
55 static mytest test;
56 #endif
57
58 static int __init testfunc(void)
59 {
60         char            buf[100];
61         unsigned int    i;
62         unsigned int    ret;
63         struct { unsigned char buf[6]; } hello = { "hello" };
64
65         printk(KERN_INFO "record fifo test start\n");
66
67         kfifo_in(&test, &hello, sizeof(hello));
68
69         /* show the size of the next record in the fifo */
70         printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test));
71
72         /* put in variable length data */
73         for (i = 0; i < 10; i++) {
74                 memset(buf, 'a' + i, i + 1);
75                 kfifo_in(&test, buf, i + 1);
76         }
77
78         printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
79
80         /* show the first record without removing from the fifo */
81         ret = kfifo_out_peek(&test, buf, sizeof(buf));
82         if (ret)
83                 printk(KERN_INFO "%.*s\n", ret, buf);
84
85         /* print out all records in the fifo */
86         while (!kfifo_is_empty(&test)) {
87                 ret = kfifo_out(&test, buf, sizeof(buf));
88                 printk(KERN_INFO "%.*s\n", ret, buf);
89         }
90
91         return 0;
92 }
93
94 static ssize_t fifo_write(struct file *file, const char __user *buf,
95                                                 size_t count, loff_t *ppos)
96 {
97         int ret;
98         unsigned int copied;
99
100         if (mutex_lock_interruptible(&write_lock))
101                 return -ERESTARTSYS;
102
103         ret = kfifo_from_user(&test, buf, count, &copied);
104
105         mutex_unlock(&write_lock);
106
107         return ret ? ret : copied;
108 }
109
110 static ssize_t fifo_read(struct file *file, char __user *buf,
111                                                 size_t count, loff_t *ppos)
112 {
113         int ret;
114         unsigned int copied;
115
116         if (mutex_lock_interruptible(&read_lock))
117                 return -ERESTARTSYS;
118
119         ret = kfifo_to_user(&test, buf, count, &copied);
120
121         mutex_unlock(&read_lock);
122
123         return ret ? ret : copied;
124 }
125
126 static const struct file_operations fifo_fops = {
127         .owner          = THIS_MODULE,
128         .read           = fifo_read,
129         .write          = fifo_write,
130 };
131
132 static int __init example_init(void)
133 {
134 #ifdef DYNAMIC
135         int ret;
136
137         ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
138         if (ret) {
139                 printk(KERN_ERR "error kfifo_alloc\n");
140                 return ret;
141         }
142 #else
143         INIT_KFIFO(test);
144 #endif
145         testfunc();
146
147         if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
148 #ifdef DYNAMIC
149                 kfifo_free(&test);
150 #endif
151                 return -ENOMEM;
152         }
153         return 0;
154 }
155
156 static void __exit example_exit(void)
157 {
158         remove_proc_entry(PROC_FIFO, NULL);
159 #ifdef DYNAMIC
160         kfifo_free(&test);
161 #endif
162 }
163
164 module_init(example_init);
165 module_exit(example_exit);
166 MODULE_LICENSE("GPL");
167 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");