1
0

chardev.c 4.5 KB

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