1
0

chardev.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 __user *, size_t, loff_t *);
  18. static ssize_t device_write(struct file *, const char __user *, size_t,
  19. loff_t *);
  20. #define SUCCESS 0
  21. #define DEVICE_NAME "chardev" /* Dev name as it appears in /proc/devices */
  22. #define BUF_LEN 80 /* Max length of the message from the device */
  23. /* Global variables are declared as static, so are global within the file. */
  24. static int major; /* major number assigned to our device driver */
  25. enum {
  26. CDEV_NOT_USED = 0,
  27. CDEV_EXCLUSIVE_OPEN = 1,
  28. };
  29. /* Is device open? Used to prevent multiple access to device */
  30. static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
  31. static char msg[BUF_LEN]; /* The msg the device will give when asked */
  32. static struct class *cls;
  33. static struct file_operations chardev_fops = {
  34. .read = device_read,
  35. .write = device_write,
  36. .open = device_open,
  37. .release = device_release,
  38. };
  39. static int __init chardev_init(void)
  40. {
  41. major = register_chrdev(0, DEVICE_NAME, &chardev_fops);
  42. if (major < 0) {
  43. pr_alert("Registering char device failed with %d\n", major);
  44. return major;
  45. }
  46. pr_info("I was assigned major number %d.\n", major);
  47. cls = class_create(THIS_MODULE, DEVICE_NAME);
  48. device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
  49. pr_info("Device created on /dev/%s\n", DEVICE_NAME);
  50. return SUCCESS;
  51. }
  52. static void __exit chardev_exit(void)
  53. {
  54. device_destroy(cls, MKDEV(major, 0));
  55. class_destroy(cls);
  56. /* Unregister the device */
  57. unregister_chrdev(major, DEVICE_NAME);
  58. }
  59. /* Methods */
  60. /* Called when a process tries to open the device file, like
  61. * "sudo cat /dev/chardev"
  62. */
  63. static int device_open(struct inode *inode, struct file *file)
  64. {
  65. static int counter = 0;
  66. if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
  67. return -EBUSY;
  68. sprintf(msg, "I already told you %d times Hello world!\n", counter++);
  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. /* We're now ready for our next caller */
  76. atomic_set(&already_open, CDEV_NOT_USED);
  77. /* Decrement the usage count, or else once you opened the file, you will
  78. * never get rid of the module.
  79. */
  80. module_put(THIS_MODULE);
  81. return SUCCESS;
  82. }
  83. /* Called when a process, which already opened the dev file, attempts to
  84. * read from it.
  85. */
  86. static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
  87. char __user *buffer, /* buffer to fill with data */
  88. size_t length, /* length of the buffer */
  89. loff_t *offset)
  90. {
  91. /* Number of bytes actually written to the buffer */
  92. int bytes_read = 0;
  93. const char *msg_ptr = msg;
  94. if (!*(msg_ptr + *offset)) { /* we are at the end of message */
  95. *offset = 0; /* reset the offset */
  96. return 0; /* signify end of file */
  97. }
  98. msg_ptr += *offset;
  99. /* Actually put the data into the buffer */
  100. while (length && *msg_ptr) {
  101. /* The buffer is in the user data segment, not the kernel
  102. * segment so "*" assignment won't work. We have to use
  103. * put_user which copies data from the kernel data segment to
  104. * the user data segment.
  105. */
  106. put_user(*(msg_ptr++), buffer++);
  107. length--;
  108. bytes_read++;
  109. }
  110. *offset += bytes_read;
  111. /* Most read functions return the number of bytes put into the buffer. */
  112. return bytes_read;
  113. }
  114. /* Called when a process writes to dev file: echo "hi" > /dev/hello */
  115. static ssize_t device_write(struct file *filp, const char __user *buff,
  116. size_t len, loff_t *off)
  117. {
  118. pr_alert("Sorry, this operation is not supported.\n");
  119. return -EINVAL;
  120. }
  121. module_init(chardev_init);
  122. module_exit(chardev_exit);
  123. MODULE_LICENSE("GPL");