inttype-example.c 3.0 KB

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