[NFS] Set CONFIG_KEYS when CONFIG_NFS_USE_KERNEL_DNS is set
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / samples / kfifo / bytestream-example.c
blob642eef3f633624413dccda3ab85b1eb6e2bcc578
1 /*
2 * Sample kfifo byte stream 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 byte stream fifo.
20 /* fifo size in elements (bytes) */
21 #define FIFO_SIZE 32
23 /* name of the proc entry */
24 #define PROC_FIFO "bytestream-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
41 #ifdef DYNAMIC
42 static struct kfifo test;
43 #else
44 static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
45 #endif
47 static int __init testfunc(void)
49 unsigned char buf[6];
50 unsigned char i;
51 unsigned int ret;
53 printk(KERN_INFO "byte stream fifo test start\n");
55 /* put string into the fifo */
56 kfifo_in(&test, "hello", 5);
58 /* put values into the fifo */
59 for (i = 0; i != 10; i++)
60 kfifo_put(&test, &i);
62 /* show the number of used elements */
63 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
65 /* get max of 5 bytes from the fifo */
66 i = kfifo_out(&test, buf, 5);
67 printk(KERN_INFO "buf: %.*s\n", i, buf);
69 /* get max of 2 elements from the fifo */
70 ret = kfifo_out(&test, buf, 2);
71 printk(KERN_INFO "ret: %d\n", ret);
72 /* and put it back to the end of the fifo */
73 ret = kfifo_in(&test, buf, ret);
74 printk(KERN_INFO "ret: %d\n", ret);
76 /* put values into the fifo until is full */
77 for (i = 20; kfifo_put(&test, &i); i++)
80 printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
82 /* print out all values in the fifo */
83 while (kfifo_get(&test, &i))
84 printk("%d ", i);
85 printk("\n");
87 return 0;
90 static ssize_t fifo_write(struct file *file, const char __user *buf,
91 size_t count, loff_t *ppos)
93 int ret;
94 unsigned int copied;
96 if (mutex_lock_interruptible(&write_lock))
97 return -ERESTARTSYS;
99 ret = kfifo_from_user(&test, buf, count, &copied);
101 mutex_unlock(&write_lock);
103 return ret ? ret : copied;
106 static ssize_t fifo_read(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
109 int ret;
110 unsigned int copied;
112 if (mutex_lock_interruptible(&read_lock))
113 return -ERESTARTSYS;
115 ret = kfifo_to_user(&test, buf, count, &copied);
117 mutex_unlock(&read_lock);
119 return ret ? ret : copied;
122 static const struct file_operations fifo_fops = {
123 .owner = THIS_MODULE,
124 .read = fifo_read,
125 .write = fifo_write,
128 static int __init example_init(void)
130 #ifdef DYNAMIC
131 int ret;
133 ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
134 if (ret) {
135 printk(KERN_ERR "error kfifo_alloc\n");
136 return ret;
138 #else
139 INIT_KFIFO(test);
140 #endif
141 testfunc();
143 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
144 #ifdef DYNAMIC
145 kfifo_free(&test);
146 #endif
147 return -ENOMEM;
149 return 0;
152 static void __exit example_exit(void)
154 remove_proc_entry(PROC_FIFO, NULL);
155 #ifdef DYNAMIC
156 kfifo_free(&test);
157 #endif
160 module_init(example_init);
161 module_exit(example_exit);
162 MODULE_LICENSE("GPL");
163 MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");