1
0

chardev.c 4.6 KB

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