Linux Audio

Check our new training course

Loading...
v3.1
 
  1/*
  2 * Sample kfifo byte stream 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 byte stream fifo.
 18 */
 19
 20/* fifo size in elements (bytes) */
 21#define FIFO_SIZE	32
 22
 23/* name of the proc entry */
 24#define	PROC_FIFO	"bytestream-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#if 0
 38#define DYNAMIC
 39#endif
 40
 41#ifdef DYNAMIC
 42static struct kfifo test;
 43#else
 44static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
 45#endif
 46
 47static const unsigned char expected_result[FIFO_SIZE] = {
 48	 3,  4,  5,  6,  7,  8,  9,  0,
 49	 1, 20, 21, 22, 23, 24, 25, 26,
 50	27, 28, 29, 30, 31, 32, 33, 34,
 51	35, 36, 37, 38, 39, 40, 41, 42,
 52};
 53
 54static int __init testfunc(void)
 55{
 56	unsigned char	buf[6];
 57	unsigned char	i, j;
 58	unsigned int	ret;
 59
 60	printk(KERN_INFO "byte stream fifo test start\n");
 61
 62	/* put string into the fifo */
 63	kfifo_in(&test, "hello", 5);
 64
 65	/* put values into the fifo */
 66	for (i = 0; i != 10; i++)
 67		kfifo_put(&test, &i);
 68
 69	/* show the number of used elements */
 70	printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
 71
 72	/* get max of 5 bytes from the fifo */
 73	i = kfifo_out(&test, buf, 5);
 74	printk(KERN_INFO "buf: %.*s\n", i, buf);
 75
 76	/* get max of 2 elements from the fifo */
 77	ret = kfifo_out(&test, buf, 2);
 78	printk(KERN_INFO "ret: %d\n", ret);
 79	/* and put it back to the end of the fifo */
 80	ret = kfifo_in(&test, buf, ret);
 81	printk(KERN_INFO "ret: %d\n", ret);
 82
 83	/* skip first element of the fifo */
 84	printk(KERN_INFO "skip 1st element\n");
 85	kfifo_skip(&test);
 86
 87	/* put values into the fifo until is full */
 88	for (i = 20; kfifo_put(&test, &i); i++)
 89		;
 90
 91	printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
 92
 93	/* show the first value without removing from the fifo */
 94	if (kfifo_peek(&test, &i))
 95		printk(KERN_INFO "%d\n", i);
 96
 97	/* check the correctness of all values in the fifo */
 98	j = 0;
 99	while (kfifo_get(&test, &i)) {
100		printk(KERN_INFO "item = %d\n", i);
101		if (i != expected_result[j++]) {
102			printk(KERN_WARNING "value mismatch: test failed\n");
103			return -EIO;
104		}
105	}
106	if (j != ARRAY_SIZE(expected_result)) {
107		printk(KERN_WARNING "size mismatch: test failed\n");
108		return -EIO;
109	}
110	printk(KERN_INFO "test passed\n");
111
112	return 0;
113}
114
115static ssize_t fifo_write(struct file *file, const char __user *buf,
116						size_t count, loff_t *ppos)
117{
118	int ret;
119	unsigned int copied;
120
121	if (mutex_lock_interruptible(&write_lock))
122		return -ERESTARTSYS;
123
124	ret = kfifo_from_user(&test, buf, count, &copied);
125
126	mutex_unlock(&write_lock);
 
 
127
128	return ret ? ret : copied;
129}
130
131static ssize_t fifo_read(struct file *file, char __user *buf,
132						size_t count, loff_t *ppos)
133{
134	int ret;
135	unsigned int copied;
136
137	if (mutex_lock_interruptible(&read_lock))
138		return -ERESTARTSYS;
139
140	ret = kfifo_to_user(&test, buf, count, &copied);
141
142	mutex_unlock(&read_lock);
 
 
143
144	return ret ? ret : copied;
145}
146
147static const struct file_operations fifo_fops = {
148	.owner		= THIS_MODULE,
149	.read		= fifo_read,
150	.write		= fifo_write,
151	.llseek		= noop_llseek,
152};
153
154static int __init example_init(void)
155{
156#ifdef DYNAMIC
157	int ret;
158
159	ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
160	if (ret) {
161		printk(KERN_ERR "error kfifo_alloc\n");
162		return ret;
163	}
164#else
165	INIT_KFIFO(test);
166#endif
167	if (testfunc() < 0) {
168#ifdef DYNAMIC
169		kfifo_free(&test);
170#endif
171		return -EIO;
172	}
173
174	if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
175#ifdef DYNAMIC
176		kfifo_free(&test);
177#endif
178		return -ENOMEM;
179	}
180	return 0;
181}
182
183static void __exit example_exit(void)
184{
185	remove_proc_entry(PROC_FIFO, NULL);
186#ifdef DYNAMIC
187	kfifo_free(&test);
188#endif
189}
190
191module_init(example_init);
192module_exit(example_exit);
 
193MODULE_LICENSE("GPL");
194MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Sample kfifo byte stream implementation
  4 *
  5 * Copyright (C) 2010 Stefani Seibold <stefani@seibold.net>
 
 
 
  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 byte stream fifo.
 16 */
 17
 18/* fifo size in elements (bytes) */
 19#define FIFO_SIZE	32
 20
 21/* name of the proc entry */
 22#define	PROC_FIFO	"bytestream-fifo"
 23
 24/* lock for procfs read access */
 25static DEFINE_MUTEX(read_access);
 26
 27/* lock for procfs write access */
 28static 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
 40static struct kfifo test;
 41#else
 42static DECLARE_KFIFO(test, unsigned char, FIFO_SIZE);
 43#endif
 44
 45static const unsigned char 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
 52static int __init testfunc(void)
 53{
 54	unsigned char	buf[6];
 55	unsigned char	i, j;
 56	unsigned int	ret;
 57
 58	printk(KERN_INFO "byte stream fifo test start\n");
 59
 60	/* put string into the fifo */
 61	kfifo_in(&test, "hello", 5);
 62
 63	/* put values into the fifo */
 64	for (i = 0; i != 10; i++)
 65		kfifo_put(&test, i);
 66
 67	/* show the number of used elements */
 68	printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
 69
 70	/* get max of 5 bytes from the fifo */
 71	i = kfifo_out(&test, buf, 5);
 72	printk(KERN_INFO "buf: %.*s\n", i, buf);
 73
 74	/* get max of 2 elements from the fifo */
 75	ret = kfifo_out(&test, buf, 2);
 76	printk(KERN_INFO "ret: %d\n", ret);
 77	/* and put it back to the end of the fifo */
 78	ret = kfifo_in(&test, buf, ret);
 79	printk(KERN_INFO "ret: %d\n", ret);
 80
 81	/* skip first element of the fifo */
 82	printk(KERN_INFO "skip 1st element\n");
 83	kfifo_skip(&test);
 84
 85	/* put values into the fifo until is full */
 86	for (i = 20; kfifo_put(&test, i); i++)
 87		;
 88
 89	printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
 90
 91	/* show the first value without removing from the fifo */
 92	if (kfifo_peek(&test, &i))
 93		printk(KERN_INFO "%d\n", i);
 94
 95	/* check the correctness of all values in the fifo */
 96	j = 0;
 97	while (kfifo_get(&test, &i)) {
 98		printk(KERN_INFO "item = %d\n", i);
 99		if (i != expected_result[j++]) {
100			printk(KERN_WARNING "value mismatch: test failed\n");
101			return -EIO;
102		}
103	}
104	if (j != ARRAY_SIZE(expected_result)) {
105		printk(KERN_WARNING "size mismatch: test failed\n");
106		return -EIO;
107	}
108	printk(KERN_INFO "test passed\n");
109
110	return 0;
111}
112
113static ssize_t fifo_write(struct file *file, const char __user *buf,
114						size_t count, loff_t *ppos)
115{
116	int ret;
117	unsigned int copied;
118
119	if (mutex_lock_interruptible(&write_access))
120		return -ERESTARTSYS;
121
122	ret = kfifo_from_user(&test, buf, count, &copied);
123
124	mutex_unlock(&write_access);
125	if (ret)
126		return ret;
127
128	return copied;
129}
130
131static ssize_t fifo_read(struct file *file, char __user *buf,
132						size_t count, loff_t *ppos)
133{
134	int ret;
135	unsigned int copied;
136
137	if (mutex_lock_interruptible(&read_access))
138		return -ERESTARTSYS;
139
140	ret = kfifo_to_user(&test, buf, count, &copied);
141
142	mutex_unlock(&read_access);
143	if (ret)
144		return ret;
145
146	return copied;
147}
148
149static const struct proc_ops fifo_proc_ops = {
150	.proc_read	= fifo_read,
151	.proc_write	= fifo_write,
152	.proc_lseek	= noop_llseek,
 
153};
154
155static int __init example_init(void)
156{
157#ifdef DYNAMIC
158	int ret;
159
160	ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
161	if (ret) {
162		printk(KERN_ERR "error kfifo_alloc\n");
163		return ret;
164	}
165#else
166	INIT_KFIFO(test);
167#endif
168	if (testfunc() < 0) {
169#ifdef DYNAMIC
170		kfifo_free(&test);
171#endif
172		return -EIO;
173	}
174
175	if (proc_create(PROC_FIFO, 0, NULL, &fifo_proc_ops) == NULL) {
176#ifdef DYNAMIC
177		kfifo_free(&test);
178#endif
179		return -ENOMEM;
180	}
181	return 0;
182}
183
184static void __exit example_exit(void)
185{
186	remove_proc_entry(PROC_FIFO, NULL);
187#ifdef DYNAMIC
188	kfifo_free(&test);
189#endif
190}
191
192module_init(example_init);
193module_exit(example_exit);
194MODULE_DESCRIPTION("Sample kfifo byte stream implementation");
195MODULE_LICENSE("GPL");
196MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");