Path: blob/master/samples/kfifo/bytestream-example.c
10818 views
/*1* Sample kfifo byte stream implementation2*3* Copyright (C) 2010 Stefani Seibold <[email protected]>4*5* Released under the GPL version 2 only.6*7*/89#include <linux/init.h>10#include <linux/module.h>11#include <linux/proc_fs.h>12#include <linux/mutex.h>13#include <linux/kfifo.h>1415/*16* This module shows how to create a byte stream fifo.17*/1819/* fifo size in elements (bytes) */20#define FIFO_SIZE 322122/* name of the proc entry */23#define PROC_FIFO "bytestream-fifo"2425/* lock for procfs read access */26static DEFINE_MUTEX(read_lock);2728/* lock for procfs write access */29static DEFINE_MUTEX(write_lock);3031/*32* define DYNAMIC in this example for a dynamically allocated fifo.33*34* Otherwise the fifo storage will be a part of the fifo structure.35*/36#if 037#define DYNAMIC38#endif3940#ifdef DYNAMIC41static struct kfifo test;42#else43static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);44#endif4546static const unsigned char expected_result[FIFO_SIZE] = {473, 4, 5, 6, 7, 8, 9, 0,481, 20, 21, 22, 23, 24, 25, 26,4927, 28, 29, 30, 31, 32, 33, 34,5035, 36, 37, 38, 39, 40, 41, 42,51};5253static int __init testfunc(void)54{55unsigned char buf[6];56unsigned char i, j;57unsigned int ret;5859printk(KERN_INFO "byte stream fifo test start\n");6061/* put string into the fifo */62kfifo_in(&test, "hello", 5);6364/* put values into the fifo */65for (i = 0; i != 10; i++)66kfifo_put(&test, &i);6768/* show the number of used elements */69printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));7071/* get max of 5 bytes from the fifo */72i = kfifo_out(&test, buf, 5);73printk(KERN_INFO "buf: %.*s\n", i, buf);7475/* get max of 2 elements from the fifo */76ret = kfifo_out(&test, buf, 2);77printk(KERN_INFO "ret: %d\n", ret);78/* and put it back to the end of the fifo */79ret = kfifo_in(&test, buf, ret);80printk(KERN_INFO "ret: %d\n", ret);8182/* skip first element of the fifo */83printk(KERN_INFO "skip 1st element\n");84kfifo_skip(&test);8586/* put values into the fifo until is full */87for (i = 20; kfifo_put(&test, &i); i++)88;8990printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));9192/* show the first value without removing from the fifo */93if (kfifo_peek(&test, &i))94printk(KERN_INFO "%d\n", i);9596/* check the correctness of all values in the fifo */97j = 0;98while (kfifo_get(&test, &i)) {99printk(KERN_INFO "item = %d\n", i);100if (i != expected_result[j++]) {101printk(KERN_WARNING "value mismatch: test failed\n");102return -EIO;103}104}105if (j != ARRAY_SIZE(expected_result)) {106printk(KERN_WARNING "size mismatch: test failed\n");107return -EIO;108}109printk(KERN_INFO "test passed\n");110111return 0;112}113114static ssize_t fifo_write(struct file *file, const char __user *buf,115size_t count, loff_t *ppos)116{117int ret;118unsigned int copied;119120if (mutex_lock_interruptible(&write_lock))121return -ERESTARTSYS;122123ret = kfifo_from_user(&test, buf, count, &copied);124125mutex_unlock(&write_lock);126127return ret ? ret : copied;128}129130static ssize_t fifo_read(struct file *file, char __user *buf,131size_t count, loff_t *ppos)132{133int ret;134unsigned int copied;135136if (mutex_lock_interruptible(&read_lock))137return -ERESTARTSYS;138139ret = kfifo_to_user(&test, buf, count, &copied);140141mutex_unlock(&read_lock);142143return ret ? ret : copied;144}145146static const struct file_operations fifo_fops = {147.owner = THIS_MODULE,148.read = fifo_read,149.write = fifo_write,150.llseek = noop_llseek,151};152153static int __init example_init(void)154{155#ifdef DYNAMIC156int ret;157158ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);159if (ret) {160printk(KERN_ERR "error kfifo_alloc\n");161return ret;162}163#else164INIT_KFIFO(test);165#endif166if (testfunc() < 0) {167#ifdef DYNAMIC168kfifo_free(&test);169#endif170return -EIO;171}172173if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {174#ifdef DYNAMIC175kfifo_free(&test);176#endif177return -ENOMEM;178}179return 0;180}181182static void __exit example_exit(void)183{184remove_proc_entry(PROC_FIFO, NULL);185#ifdef DYNAMIC186kfifo_free(&test);187#endif188}189190module_init(example_init);191module_exit(example_exit);192MODULE_LICENSE("GPL");193MODULE_AUTHOR("Stefani Seibold <[email protected]>");194195196