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 */ 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/* 39 * struct kfifo_rec_ptr_1 and STRUCT_KFIFO_REC_1 can handle records of a 40 * length between 0 and 255 bytes. 41 * 42 * struct kfifo_rec_ptr_2 and STRUCT_KFIFO_REC_2 can handle records of a 43 * length between 0 and 65535 bytes. 44 */ 45 46#ifdef DYNAMIC 47struct kfifo_rec_ptr_1 test; 48 49#else 50typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest; 51 52static mytest test; 53#endif 54 55static const char *expected_result[] = { 56 "a", 57 "bb", 58 "ccc", 59 "dddd", 60 "eeeee", 61 "ffffff", 62 "ggggggg", 63 "hhhhhhhh", 64 "iiiiiiiii", 65 "jjjjjjjjjj", 66}; 67 68static int __init testfunc(void) 69{ 70 char buf[100]; 71 unsigned int i; 72 unsigned int ret; 73 struct { unsigned char buf[6]; } hello = { "hello" }; 74 75 printk(KERN_INFO "record fifo test start\n"); 76 77 kfifo_in(&test, &hello, sizeof(hello)); 78 79 /* show the size of the next record in the fifo */ 80 printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test)); 81 82 /* put in variable length data */ 83 for (i = 0; i < 10; i++) { 84 memset(buf, 'a' + i, i + 1); 85 kfifo_in(&test, buf, i + 1); 86 } 87 88 /* skip first element of the fifo */ 89 printk(KERN_INFO "skip 1st element\n"); 90 kfifo_skip(&test); 91 92 printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test)); 93 94 /* show the first record without removing from the fifo */ 95 ret = kfifo_out_peek(&test, buf, sizeof(buf)); 96 if (ret) 97 printk(KERN_INFO "%.*s\n", ret, buf); 98 99 /* check the correctness of all values in the fifo */ 100 i = 0; 101 while (!kfifo_is_empty(&test)) { 102 ret = kfifo_out(&test, buf, sizeof(buf)); 103 buf[ret] = '\0'; 104 printk(KERN_INFO "item = %.*s\n", ret, buf); 105 if (strcmp(buf, expected_result[i++])) { 106 printk(KERN_WARNING "value mismatch: test failed\n"); 107 return -EIO; 108 } 109 } 110 if (i != ARRAY_SIZE(expected_result)) { 111 printk(KERN_WARNING "size mismatch: test failed\n"); 112 return -EIO; 113 } 114 printk(KERN_INFO "test passed\n"); 115 116 return 0; 117} 118 119static ssize_t fifo_write(struct file *file, const char __user *buf, 120 size_t count, loff_t *ppos) 121{ 122 int ret; 123 unsigned int copied; 124 125 if (mutex_lock_interruptible(&write_lock)) 126 return -ERESTARTSYS; 127 128 ret = kfifo_from_user(&test, buf, count, &copied); 129 130 mutex_unlock(&write_lock); 131 132 return ret ? ret : copied; 133} 134 135static ssize_t fifo_read(struct file *file, char __user *buf, 136 size_t count, loff_t *ppos) 137{ 138 int ret; 139 unsigned int copied; 140 141 if (mutex_lock_interruptible(&read_lock)) 142 return -ERESTARTSYS; 143 144 ret = kfifo_to_user(&test, buf, count, &copied); 145 146 mutex_unlock(&read_lock); 147 148 return ret ? ret : copied; 149} 150 151static const struct file_operations fifo_fops = { 152 .owner = THIS_MODULE, 153 .read = fifo_read, 154 .write = fifo_write, 155}; 156 157static int __init example_init(void) 158{ 159#ifdef DYNAMIC 160 int ret; 161 162 ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL); 163 if (ret) { 164 printk(KERN_ERR "error kfifo_alloc\n"); 165 return ret; 166 } 167#else 168 INIT_KFIFO(test); 169#endif 170 if (testfunc() < 0) { 171#ifdef DYNAMIC 172 kfifo_free(&test); 173#endif 174 return -EIO; 175 } 176 177 if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) { 178#ifdef DYNAMIC 179 kfifo_free(&test); 180#endif 181 return -ENOMEM; 182 } 183 return 0; 184} 185 186static void __exit example_exit(void) 187{ 188 remove_proc_entry(PROC_FIFO, NULL); 189#ifdef DYNAMIC 190 kfifo_free(&test); 191#endif 192} 193 194module_init(example_init); 195module_exit(example_exit); 196MODULE_LICENSE("GPL"); 197MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>"); 198