| 1 | |
| 2 | |
| 3 | * Sample kfifo int type implementation
|
| 4 | *
|
| 5 | * Copyright (C) 2010 Stefani Seibold <[email protected]>
|
| 6 |
|
| 7 |
|
| 8 | #include <linux/init.h>
|
| 9 | #include <linux/module.h>
|
| 10 | #include <linux/proc_fs.h>
|
| 11 | #include <linux/mutex.h>
|
| 12 | #include <linux/kfifo.h>
|
| 13 |
|
| 14 | |
| 15 | * This module shows how to create a int type fifo.
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 | #define FIFO_SIZE 32
|
| 20 |
|
| 21 |
|
| 22 | #define PROC_FIFO "int-fifo"
|
| 23 |
|
| 24 |
|
| 25 | static DEFINE_MUTEX(read_access);
|
| 26 |
|
| 27 |
|
| 28 | static DEFINE_MUTEX(write_access);
|
| 29 |
|
| 30 | |
| 31 | * define DYNAMIC in this example for a dynamically allocated fifo.
|
| 32 | *
|
| 33 | * Otherwise the fifo storage will be a part of the fifo structure.
|
| 34 |
|
| 35 | #if 0 |
| 36 | #define DYNAMIC
|
| 37 | #endif
|
| 38 |
|
| 39 | #ifdef DYNAMIC
|
| 40 | static DECLARE_KFIFO_PTR(test, int);
|
| 41 | #else
|
| 42 | static DEFINE_KFIFO(test, int, FIFO_SIZE);
|
| 43 | #endif
|
| 44 |
|
| 45 | static const int expected_result[FIFO_SIZE] = {
|
| 46 | 3, 4, 5, 6, 7, 8, 9, 0,
|
| 47 | 1, 20, 21, 22, 23, 24, 25, 26,
|
| 48 | 27, 28, 29, 30, 31, 32, 33, 34,
|
| 49 | 35, 36, 37, 38, 39, 40, 41, 42,
|
| 50 | };
|
| 51 |
|
| 52 | static int __init testfunc(void)
|
| 53 | {
|
| 54 | int buf[6];
|
| 55 | int i, j;
|
| 56 | unsigned int ret;
|
| 57 |
|
| 58 | printk(KERN_INFO "int fifo test start\n");
|
| 59 |
|
| 60 |
|
| 61 | for (i = 0; i != 10; i++)
|
| 62 | kfifo_put(&test, i);
|
| 63 |
|
| 64 |
|
| 65 | printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
|
| 66 |
|
| 67 |
|
| 68 | ret = kfifo_out(&test, buf, 2);
|
| 69 | printk(KERN_INFO "ret: %d\n", ret);
|
| 70 |
|
| 71 | ret = kfifo_in(&test, buf, ret);
|
| 72 | printk(KERN_INFO "ret: %d\n", ret);
|
| 73 |
|
| 74 |
|
| 75 | printk(KERN_INFO "skip 1st element\n");
|
| 76 | kfifo_skip(&test);
|
| 77 |
|
| 78 |
|
| 79 | for (i = 20; kfifo_put(&test, i); i++)
|
| 80 | ;
|
| 81 |
|
| 82 | printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
|
| 83 |
|
| 84 |
|
| 85 | if (kfifo_peek(&test, &i))
|
| 86 | printk(KERN_INFO "%d\n", i);
|
| 87 |
|
| 88 |
|
| 89 | j = 0;
|
| 90 | while (kfifo_get(&test, &i)) {
|
| 91 | printk(KERN_INFO "item = %d\n", i);
|
| 92 | if (i != expected_result[j++]) {
|
| 93 | printk(KERN_WARNING "value mismatch: test failed\n");
|
| 94 | return -EIO;
|
| 95 | }
|
| 96 | }
|
| 97 | if (j != ARRAY_SIZE(expected_result)) {
|
| 98 | printk(KERN_WARNING "size mismatch: test failed\n");
|
| 99 | return -EIO;
|
| 100 | }
|
| 101 | printk(KERN_INFO "test passed\n");
|
| 102 |
|
| 103 | return 0;
|
| 104 | }
|
| 105 |
|
| 106 | static ssize_t fifo_write(struct file *file, const char __user *buf,
|
| 107 | size_t count, loff_t *ppos)
|
| 108 | {
|
| 109 | int ret;
|
| 110 | unsigned int copied;
|
| 111 |
|
| 112 | if (mutex_lock_interruptible(&write_access))
|
| 113 | return -ERESTARTSYS;
|
| 114 |
|
| 115 | ret = kfifo_from_user(&test, buf, count, &copied);
|
| 116 |
|
| 117 | mutex_unlock(&write_access);
|
| 118 | if (ret)
|
| 119 | return ret;
|
| 120 |
|
| 121 | return copied;
|
| 122 | }
|
| 123 |
|
| 124 | static ssize_t fifo_read(struct file *file, char __user *buf,
|
| 125 | size_t count, loff_t *ppos)
|
| 126 | {
|
| 127 | int ret;
|
| 128 | unsigned int copied;
|
| 129 |
|
| 130 | if (mutex_lock_interruptible(&read_access))
|
| 131 | return -ERESTARTSYS;
|
| 132 |
|
| 133 | ret = kfifo_to_user(&test, buf, count, &copied);
|
| 134 |
|
| 135 | mutex_unlock(&read_access);
|
| 136 | if (ret)
|
| 137 | return ret;
|
| 138 |
|
| 139 | return copied;
|
| 140 | }
|
| 141 |
|
| 142 | static const struct proc_ops fifo_proc_ops = {
|
| 143 | .proc_read = fifo_read,
|
| 144 | .proc_write = fifo_write,
|
| 145 | .proc_lseek = noop_llseek,
|
| 146 | };
|
| 147 |
|
| 148 | static int __init example_init(void)
|
| 149 | {
|
| 150 | #ifdef DYNAMIC
|
| 151 | int ret;
|
| 152 |
|
| 153 | ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
|
| 154 | if (ret) {
|
| 155 | printk(KERN_ERR "error kfifo_alloc\n");
|
| 156 | return ret;
|
| 157 | }
|
| 158 | #endif
|
| 159 | if (testfunc() < 0) {
|
| 160 | #ifdef DYNAMIC
|
| 161 | kfifo_free(&test);
|
| 162 | #endif
|
| 163 | return -EIO;
|
| 164 | }
|
| 165 |
|
| 166 | if (proc_create(PROC_FIFO, 0, NULL, &fifo_proc_ops) == NULL) {
|
| 167 | #ifdef DYNAMIC
|
| 168 | kfifo_free(&test);
|
| 169 | #endif
|
| 170 | return -ENOMEM;
|
| 171 | }
|
| 172 | return 0;
|
| 173 | }
|
| 174 |
|
| 175 | static void __exit example_exit(void)
|
| 176 | {
|
| 177 | remove_proc_entry(PROC_FIFO, NULL);
|
| 178 | #ifdef DYNAMIC
|
| 179 | kfifo_free(&test);
|
| 180 | #endif
|
| 181 | }
|
| 182 |
|
| 183 | module_init(example_init);
|
| 184 | module_exit(example_exit);
|
| 185 | MODULE_DESCRIPTION("Sample kfifo int type implementation");
|
| 186 | MODULE_LICENSE("GPL");
|
| 187 | MODULE_AUTHOR("Stefani Seibold <[email protected]>");
|