record-example.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Sample dynamic sized record fifo 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 variable sized record fifo.
  16. */
  17. /* fifo size in elements (bytes) */
  18. #define FIFO_SIZE 128
  19. /* name of the proc entry */
  20. #define PROC_FIFO "record-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. /*
  34. * struct kfifo_rec_ptr_1 and STRUCT_KFIFO_REC_1 can handle records of a
  35. * length between 0 and 255 bytes.
  36. *
  37. * struct kfifo_rec_ptr_2 and STRUCT_KFIFO_REC_2 can handle records of a
  38. * length between 0 and 65535 bytes.
  39. */
  40. #ifdef DYNAMIC
  41. struct kfifo_rec_ptr_1 test;
  42. #else
  43. typedef STRUCT_KFIFO_REC_1(FIFO_SIZE) mytest;
  44. static mytest test;
  45. #endif
  46. static int __init testfunc(void)
  47. {
  48. char buf[100];
  49. unsigned int i;
  50. unsigned int ret;
  51. struct { unsigned char buf[6]; } hello = { "hello" };
  52. printk(KERN_INFO "record fifo test start\n");
  53. kfifo_in(&test, &hello, sizeof(hello));
  54. /* show the size of the next record in the fifo */
  55. printk(KERN_INFO "fifo peek len: %u\n", kfifo_peek_len(&test));
  56. /* put in variable length data */
  57. for (i = 0; i < 10; i++) {
  58. memset(buf, 'a' + i, i + 1);
  59. kfifo_in(&test, buf, i + 1);
  60. }
  61. printk(KERN_INFO "fifo len: %u\n", kfifo_len(&test));
  62. /* show the first record without removing from the fifo */
  63. ret = kfifo_out_peek(&test, buf, sizeof(buf));
  64. if (ret)
  65. printk(KERN_INFO "%.*s\n", ret, buf);
  66. /* print out all records in the fifo */
  67. while (!kfifo_is_empty(&test)) {
  68. ret = kfifo_out(&test, buf, sizeof(buf));
  69. printk(KERN_INFO "%.*s\n", ret, buf);
  70. }
  71. return 0;
  72. }
  73. static ssize_t fifo_write(struct file *file, const char __user *buf,
  74. size_t count, loff_t *ppos)
  75. {
  76. int ret;
  77. unsigned int copied;
  78. if (mutex_lock_interruptible(&write_lock))
  79. return -ERESTARTSYS;
  80. ret = kfifo_from_user(&test, buf, count, &copied);
  81. mutex_unlock(&write_lock);
  82. return ret ? ret : copied;
  83. }
  84. static ssize_t fifo_read(struct file *file, char __user *buf,
  85. size_t count, loff_t *ppos)
  86. {
  87. int ret;
  88. unsigned int copied;
  89. if (mutex_lock_interruptible(&read_lock))
  90. return -ERESTARTSYS;
  91. ret = kfifo_to_user(&test, buf, count, &copied);
  92. mutex_unlock(&read_lock);
  93. return ret ? ret : copied;
  94. }
  95. static const struct file_operations fifo_fops = {
  96. .owner = THIS_MODULE,
  97. .read = fifo_read,
  98. .write = fifo_write,
  99. };
  100. static int __init example_init(void)
  101. {
  102. #ifdef DYNAMIC
  103. int ret;
  104. ret = kfifo_alloc(&test, FIFO_SIZE, GFP_KERNEL);
  105. if (ret) {
  106. printk(KERN_ERR "error kfifo_alloc\n");
  107. return ret;
  108. }
  109. #else
  110. INIT_KFIFO(test);
  111. #endif
  112. testfunc();
  113. if (proc_create(PROC_FIFO, 0, NULL, &fifo_fops) == NULL) {
  114. #ifdef DYNAMIC
  115. kfifo_free(&test);
  116. #endif
  117. return -ENOMEM;
  118. }
  119. return 0;
  120. }
  121. static void __exit example_exit(void)
  122. {
  123. remove_proc_entry(PROC_FIFO, NULL);
  124. #ifdef DYNAMIC
  125. kfifo_free(&test);
  126. #endif
  127. }
  128. module_init(example_init);
  129. module_exit(example_exit);
  130. MODULE_LICENSE("GPL");
  131. MODULE_AUTHOR("Stefani Seibold <stefani@seibold.net>");