chardev.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * chardev.c: Creates a read-only char device that says how many times
  3. * you've 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. /*
  15. * Prototypes - this would normally go in a .h file
  16. */
  17. int init_module(void);
  18. void cleanup_module(void);
  19. static int device_open(struct inode *, struct file *);
  20. static int device_release(struct inode *, struct file *);
  21. static ssize_t device_read(struct file *, char *, size_t, loff_t *);
  22. static ssize_t device_write(struct file *, const char *, size_t, 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. /*
  27. * Global variables are declared as static, so are global within the file.
  28. */
  29. static int Major; /* Major number assigned to our device driver */
  30. static int Device_Open = 0; /* Is device open?
  31. * Used to prevent multiple access to device */
  32. static char msg[BUF_LEN]; /* The msg the device will give when asked */
  33. static char *msg_Ptr;
  34. static struct class *cls;
  35. static struct file_operations chardev_fops = {.read = device_read,
  36. .write = device_write,
  37. .open = device_open,
  38. .release = device_release};
  39. /*
  40. * This function is called when the module is loaded
  41. */
  42. int init_module(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. /*
  56. * This function is called when the module is unloaded
  57. */
  58. void cleanup_module(void)
  59. {
  60. device_destroy(cls, MKDEV(Major, 0));
  61. class_destroy(cls);
  62. /*
  63. * Unregister the device
  64. */
  65. unregister_chrdev(Major, DEVICE_NAME);
  66. }
  67. /*
  68. * Methods
  69. */
  70. /*
  71. * Called when a process tries to open the device file, like
  72. * "sudo cat /dev/chardev"
  73. */
  74. static int device_open(struct inode *inode, struct file *file)
  75. {
  76. static int counter = 0;
  77. if (Device_Open)
  78. return -EBUSY;
  79. Device_Open++;
  80. sprintf(msg, "I already told you %d times Hello world!\n", counter++);
  81. msg_Ptr = msg;
  82. try_module_get(THIS_MODULE);
  83. return SUCCESS;
  84. }
  85. /*
  86. * Called when a process closes the device file.
  87. */
  88. static int device_release(struct inode *inode, struct file *file)
  89. {
  90. Device_Open--; /* We're now ready for our next caller */
  91. /*
  92. * Decrement the usage count, or else once you opened the file, you'll
  93. * never get get rid of the module.
  94. */
  95. module_put(THIS_MODULE);
  96. return SUCCESS;
  97. }
  98. /*
  99. * Called when a process, which already opened the dev file, attempts to
  100. * read from it.
  101. */
  102. static ssize_t device_read(struct file *filp, /* see include/linux/fs.h */
  103. char *buffer, /* buffer to fill with data */
  104. size_t length, /* length of the buffer */
  105. loff_t *offset)
  106. {
  107. /*
  108. * Number of bytes actually written to the buffer
  109. */
  110. int bytes_read = 0;
  111. /*
  112. * If we're at the end of the message,
  113. * return 0 signifying end of file
  114. */
  115. if (*msg_Ptr == 0)
  116. return 0;
  117. /*
  118. * Actually put the data into the buffer
  119. */
  120. while (length && *msg_Ptr) {
  121. /*
  122. * The buffer is in the user data segment, not the kernel
  123. * segment so "*" assignment won't work. We have to use
  124. * put_user which copies data from the kernel data segment to
  125. * the user data segment.
  126. */
  127. put_user(*(msg_Ptr++), buffer++);
  128. length--;
  129. bytes_read++;
  130. }
  131. /*
  132. * Most read functions return the number of bytes put into the buffer
  133. */
  134. return bytes_read;
  135. }
  136. /*
  137. * Called when a process writes to dev file: echo "hi" > /dev/hello
  138. */
  139. static ssize_t device_write(struct file *filp,
  140. const char *buff,
  141. size_t len,
  142. loff_t *off)
  143. {
  144. pr_alert("Sorry, this operation isn't supported.\n");
  145. return -EINVAL;
  146. }
  147. MODULE_LICENSE("GPL");