chardev.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. static int __init chardev_init(void)
  36. {
  37. major = register_chrdev(0, DEVICE_NAME, &chardev_fops);
  38. if (major < 0) {
  39. pr_alert("Registering char device failed with %d\n", major);
  40. return major;
  41. }
  42. pr_info("I was assigned major number %d.\n", major);
  43. cls = class_create(THIS_MODULE, DEVICE_NAME);
  44. device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
  45. pr_info("Device created on /dev/%s\n", DEVICE_NAME);
  46. return SUCCESS;
  47. }
  48. static void __exit chardev_exit(void)
  49. {
  50. device_destroy(cls, MKDEV(major, 0));
  51. class_destroy(cls);
  52. /* Unregister the device */
  53. unregister_chrdev(major, DEVICE_NAME);
  54. }
  55. /* Methods */
  56. /* Called when a process tries to open the device file, like
  57. * "sudo cat /dev/chardev"
  58. */
  59. static int device_open(struct inode *inode, struct file *file)
  60. {
  61. static int counter = 0;
  62. if (open_device_cnt)
  63. return -EBUSY;
  64. open_device_cnt++;
  65. sprintf(msg, "I already told you %d times Hello world!\n", counter++);
  66. msg_ptr = msg;
  67. try_module_get(THIS_MODULE);
  68. return SUCCESS;
  69. }
  70. /* Called when a process closes the device file. */
  71. static int device_release(struct inode *inode, struct file *file)
  72. {
  73. open_device_cnt--; /* We're now ready for our next caller */
  74. /* Decrement the usage count, or else once you opened the file, you will
  75. * never get get rid of the module.
  76. */
  77. module_put(THIS_MODULE);
  78. return SUCCESS;
  79. }
  80. /* Called when a process, which already opened the dev file, attempts to
  81. * read from it.
  82. */
  83. static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
  84. char *buffer, /* buffer to fill with data */
  85. size_t length, /* length of the buffer */
  86. loff_t *offset)
  87. {
  88. /* Number of bytes actually written to the buffer */
  89. int bytes_read = 0;
  90. /* If we are at the end of message, return 0 signifying end of file. */
  91. if (*msg_ptr == 0)
  92. return 0;
  93. /* Actually put the data into the buffer */
  94. while (length && *msg_ptr) {
  95. /* The buffer is in the user data segment, not the kernel
  96. * segment so "*" assignment won't work. We have to use
  97. * put_user which copies data from the kernel data segment to
  98. * the user data segment.
  99. */
  100. put_user(*(msg_ptr++), buffer++);
  101. length--;
  102. bytes_read++;
  103. }
  104. /* Most read functions return the number of bytes put into the buffer. */
  105. return bytes_read;
  106. }
  107. /* Called when a process writes to dev file: echo "hi" > /dev/hello */
  108. static ssize_t device_write(struct file *filp,
  109. const char *buff,
  110. size_t len,
  111. loff_t *off)
  112. {
  113. pr_alert("Sorry, this operation is not supported.\n");
  114. return -EINVAL;
  115. }
  116. module_init(chardev_init);
  117. module_exit(chardev_exit);
  118. MODULE_LICENSE("GPL");