bytestream-example.c 4.0 KB

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