1
0

chardev.c 4.5 KB

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