chardev.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * chardev.c: Creates a read-only char device that says how many times
  3. * you have read from the dev file
  4. */
  5. #include <linux/cdev.h>
  6. #include <linux/delay.h>
  7. #include <linux/device.h>
  8. #include <linux/fs.h>
  9. #include <linux/init.h>
  10. #include <linux/irq.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/poll.h>
  14. /* Prototypes - this would normally go in a .h file */
  15. static int device_open(struct inode *, struct file *);
  16. static int device_release(struct inode *, struct file *);
  17. static ssize_t device_read(struct file *, char *, size_t, loff_t *);
  18. static ssize_t device_write(struct file *, const char *, size_t, loff_t *);
  19. #define SUCCESS 0
  20. #define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
  21. #define BUF_LEN 80 /* Max length of the message from the device */
  22. /* Global variables are declared as static, so are global within the file. */
  23. static int major; /* major number assigned to our device driver */
  24. static int open_device_cnt = 0; /* Is device open?
  25. * Used to prevent multiple access to device */
  26. static char msg[BUF_LEN]; /* The msg the device will give when asked */
  27. static char *msg_ptr;
  28. static struct class *cls;
  29. static struct file_operations chardev_fops = {
  30. .read = device_read,
  31. .write = device_write,
  32. .open = device_open,
  33. .release = device_release,
  34. };
  35. /* This function is called when the module is loaded. */
  36. int init_module(void)
  37. {
  38. major = register_chrdev(0, DEVICE_NAME, &chardev_fops);
  39. if (major < 0) {
  40. pr_alert("Registering char device failed with %d\n", major);
  41. return major;
  42. }
  43. pr_info("I was assigned major number %d.\n", major);
  44. cls = class_create(THIS_MODULE, DEVICE_NAME);
  45. device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
  46. pr_info("Device created on /dev/%s\n", DEVICE_NAME);
  47. return SUCCESS;
  48. }
  49. /* This function is called when the module is unloaded. */
  50. void cleanup_module(void)
  51. {
  52. device_destroy(cls, MKDEV(major, 0));
  53. class_destroy(cls);
  54. /* Unregister the device */
  55. unregister_chrdev(major, DEVICE_NAME);
  56. }
  57. /* Methods */
  58. /* Called when a process tries to open the device file, like
  59. * "sudo cat /dev/chardev"
  60. */
  61. static int device_open(struct inode *inode, struct file *file)
  62. {
  63. static int counter = 0;
  64. if (open_device_cnt)
  65. return -EBUSY;
  66. open_device_cnt++;
  67. sprintf(msg, "I already told you %d times Hello world!\n", counter++);
  68. msg_ptr = msg;
  69. try_module_get(THIS_MODULE);
  70. return SUCCESS;
  71. }
  72. /* Called when a process closes the device file. */
  73. static int device_release(struct inode *inode, struct file *file)
  74. {
  75. open_device_cnt--; /* We're now ready for our next caller */
  76. /* Decrement the usage count, or else once you opened the file, you will
  77. * never get get rid of the module.
  78. */
  79. module_put(THIS_MODULE);
  80. return SUCCESS;
  81. }
  82. /* Called when a process, which already opened the dev file, attempts to
  83. * read from it.
  84. */
  85. static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
  86. char *buffer, /* buffer to fill with data */
  87. size_t length, /* length of the buffer */
  88. loff_t *offset)
  89. {
  90. /* Number of bytes actually written to the buffer */
  91. int bytes_read = 0;
  92. /* If we are at the end of message, return 0 signifying end of file. */
  93. if (*msg_ptr == 0)
  94. return 0;
  95. /* Actually put the data into the buffer */
  96. while (length && *msg_ptr) {
  97. /* The buffer is in the user data segment, not the kernel
  98. * segment so "*" assignment won't work. We have to use
  99. * put_user which copies data from the kernel data segment to
  100. * the user data segment.
  101. */
  102. put_user(*(msg_ptr++), buffer++);
  103. length--;
  104. bytes_read++;
  105. }
  106. /* Most read functions return the number of bytes put into the buffer. */
  107. return bytes_read;
  108. }
  109. /* Called when a process writes to dev file: echo "hi" > /dev/hello */
  110. static ssize_t device_write(struct file *filp,
  111. const char *buff,
  112. size_t len,
  113. loff_t *off)
  114. {
  115. pr_alert("Sorry, this operation is not supported.\n");
  116. return -EINVAL;
  117. }
  118. MODULE_LICENSE("GPL");