Linux Audio

Check our new training course

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