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