bytestream-example.c 4.0 KB

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