Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. // osinfo.c
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4. #include <linux/fs.h>
  5. #include <asm/uaccess.h>
  6. /* Needed by all modules */
  7. /* Needed for KERN_INFO */
  8. MODULE_LICENSE("GPL");
  9. MODULE_AUTHOR("KRERK PIROMSOPA, PH.D. <Krerk.P@chula.ac.th>");
  10. MODULE_DESCRIPTION("\"osinfo\" Character Device");
  11. #define DEVICENAME "osinfo"
  12. static int dev_major;
  13. static int dev_open =0;
  14. static char *f_ptr;
  15.  
  16. static const char f_data0[] = "0:CP ENG CU OS 2016S1 - Instructors\n1:\tVeera Muangsin, Ph.D.\n2:\tKrerk Piromsopa,Ph.D.\n3:\tKunwadee Sripanidkulchai, Ph.D.\t\n4:\tThongchai Rojkangsadan\n";
  17.  
  18. static const char f_data1[] = "1:\t5830462321\t Rujikorn Charakorn.\n2:\t5831020821\t Natthaphat Kanchanapraphat";
  19.  
  20. // prototypes for device functions
  21. static int device_open(struct inode *, struct file *);
  22. static int device_release(struct inode *inode, struct file *file);
  23. static ssize_t device_read(struct file *, char *, size_t, loff_t *);
  24. // File operations structor
  25. // Only implement those that will be used.
  26. static struct file_operations dev_fops = {
  27. .read = device_read,
  28. .open = device_open,
  29. .release = device_release
  30. };
  31. int init_module(void)
  32. {
  33. printk(KERN_INFO "CPCHAR: dev osinfo init\n");
  34. dev_major=register_chrdev(0, DEVICENAME, &dev_fops);
  35. if (dev_major < 0) {
  36. printk(KERN_ALERT "Fail register_chrdev osinfo with %d\n",dev_major);
  37. return dev_major;
  38. }
  39. printk(KERN_INFO "Device MajorNumber %d.\n", dev_major);
  40. printk(KERN_INFO "To create a device file:\n");
  41. printk(KERN_INFO "\t'mknod /dev/%s c %d 0'.\n", DEVICENAME, dev_major);
  42. printk(KERN_INFO "Try varying minor numbers.\n");
  43. printk(KERN_INFO "Please remove the device file and module when done.\n");
  44. /* * non 0 - means init_module failed */
  45. return 0;
  46. }
  47.  
  48. void cleanup_module(void)
  49. {
  50. printk(KERN_INFO "CPCHAR: dev osinfo cleanup\n");
  51. unregister_chrdev(dev_major, DEVICENAME);
  52. }
  53. static int device_open(struct inode *inode, struct file *file)
  54. {
  55. if (dev_open)
  56. return -EBUSY;
  57. dev_open++;
  58. printk(KERN_INFO "dev minor %d\n", MINOR(inode->i_rdev));
  59. if (MINOR(inode->i_rdev)==0) f_ptr=(char * )f_data0;
  60. else f_ptr=(char * )f_data1;
  61. // lock module
  62. try_module_get(THIS_MODULE);
  63. return 0;
  64. }
  65. static int device_release(struct inode *inode, struct file *file) {
  66. dev_open--; /* We're now ready for our next caller */
  67. // release module
  68. module_put(THIS_MODULE);
  69. return 0;
  70. }
  71. static ssize_t device_read(struct file *filp,char *buffer,size_t length,loff_t * offset){
  72. int bytes_read=0;
  73.  
  74. if (*f_ptr ==0) {
  75. return 0;
  76. }
  77. while (length && *f_ptr) {
  78. put_user(*(f_ptr++), buffer++);
  79. length--;
  80. bytes_read++;
  81. }
  82. return bytes_read;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement