bytestream-example.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 int __init testfunc(void)
  39. {
  40. unsigned char buf[6];
  41. unsigned char i;
  42. unsigned int ret;
  43. printk(KERN_INFO "byte stream fifo test start\n");
  44. /* put string into the fifo */
  45. kfifo_in(&test, "hello", 5);
  46. /* put values into the fifo */
  47. for (i = 0; i != 10; i++)
  48. kfifo_put(&test, &i);
  49. /* show the number of used elements */
  50. printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
  51. /* get max of 5 bytes from the fifo */
  52. i = kfifo_out(&test, buf, 5);
  53. printk(KERN_INFO "buf: %.*s\n", i, buf);
  54. /* get max of 2 elements from the fifo */
  55. ret = kfifo_out(&test, buf, 2);
  56. printk(KERN_INFO "ret: %d\n", ret);
  57. /* and put it back to the end of the fifo */
  58. ret = kfifo_in(&test, buf, ret);
  59. printk(KERN_INFO "ret: %d\n", ret);
  60. /* put values into the fifo until is full */
  61. for (i = 20; kfifo_put(&test, &i); i++)
  62. ;
  63. printk(KERN_INFO "queue len: %u\n", kfifo_len(&test));
  64. /* print out all values in the fifo */
  65. while (kfifo_get(&test, &i))
  66. printk("%d ", i);
  67. printk("\n");
  68. return 0;
  69. }
  70. static ssize_t fifo_write(struct file *file, const char __user *buf,
  71. size_t count, loff_t *ppos)
  72. {
  73. int ret;
  74. unsigned int copied;
  75. if (mutex_lock_interruptible(&write_lock))
  76. return -ERESTARTSYS;
  77. ret = kfifo_from_user(&test, buf, count, &copied);
  78. mutex_unlock(&write_lock);
  79. return ret ? ret : copied;
  80. }
  81. static ssize_t fifo_read(struct file *file, char __user *buf,
  82. size_t count, loff_t *ppos)
  83. {
  84. int ret;
  85. unsigned int copied;
  86. if (mutex_lock_interruptible(&read_lock))
  87. return -ERESTARTSYS;
  88. ret = kfifo_to_user(&test, buf, count, &copied);
  89. mutex_unlock(&read_lock);
  90. return ret ? ret : copied;
  91. }
  92. static const struct file_operations fifo_fops = {
  93. .owner = THIS_MODULE,
  94. .read = fifo_read,
  95. .write = fifo_write,
  96. };
  97. static int __init example_init(void)
  98. {
  99. #ifdef DYNAMIC
  100. int ret;
  101. ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
  102. if (ret) {
  103. printk(KERN_ERR "error kfifo_alloc\n");
  104. return ret;
  105. }
  106. #else
  107. INIT_KFIFO(test);
  108. #endif
  109. testfunc();
  110. if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
  111. #ifdef DYNAMIC
  112. kfifo_free(&test);
  113. #endif
  114. return -ENOMEM;
  115. }
  116. return 0;
  117. }
  118. static void __exit example_exit(void)
  119. {
  120. remove_proc_entry(PROC_FIFO, NULL);
  121. #ifdef DYNAMIC
  122. kfifo_free(&test);
  123. #endif
  124. }
  125. module_init(example_init);
  126. module_exit(example_exit);
  127. MODULE_LICENSE("GPL");
  128. MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");