Advertisement
alsiva

alsiva_driver.c

Dec 16th, 2022
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.89 KB | None | 0 0
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/kernel.h>
  4.  
  5. #include <linux/fs.h>
  6. #include <linux/cdev.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/device.h>
  9. #include <linux/vmalloc.h>
  10.  
  11. #include <linux/ioctl.h>
  12. #include <linux/pid.h>
  13. #include <linux/sched.h>
  14. #include <linux/pci.h>
  15. #include <linux/signal.h>
  16.  
  17. #include "driver.h"
  18.  
  19. #define WR_VALUE _IOW('a','a', int32_t*)
  20. #define RD_VALUE _IOR('a','b', char[])
  21.  
  22. MODULE_LICENSE("Dual BSD/GPL");
  23. MODULE_DESCRIPTION("Stab linux module");
  24. MODULE_VERSION("1.0");
  25.  
  26. dev_t dev = 0;
  27. static struct class *dev_class;
  28. static struct cdev etx_cdev;
  29.  
  30. int32_t pid = 0;
  31.  
  32. struct message {
  33.     struct signal_struct_info* ssi;
  34.     struct vm_area_struct_info* vasi;
  35.     pid_t pid;
  36. };
  37.  
  38. struct task_struct* ts;
  39.  
  40. /*
  41. ** Function Prototypes
  42. */
  43. static int      __init etx_driver_init(void);
  44. static void     __exit etx_driver_exit(void);
  45. static int      etx_open(struct inode *inode, struct file *file);
  46. static int      etx_release(struct inode *inode, struct file *file);
  47. static ssize_t  etx_read(struct file *filp, char __user *buf, size_t len,loff_t * off);
  48. static ssize_t  etx_write(struct file *filp, const char *buf, size_t len, loff_t * off);
  49. static long     etx_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  50.  
  51. struct message get_message_with_struct_info(void);
  52.  
  53. /*
  54. ** File operation sturcture
  55. */
  56. static struct file_operations fops =
  57. {
  58.         .owner          = THIS_MODULE,
  59.         .read           = etx_read,
  60.         .write          = etx_write,
  61.         .open           = etx_open,
  62.         .unlocked_ioctl = etx_ioctl,
  63.         .release        = etx_release,
  64. };
  65.  
  66. /*
  67. ** This function will be called when we open the Device file
  68. */
  69. static int etx_open(struct inode *inode, struct file *file)
  70. {
  71.         pr_info("Device File Opened...!!!\n");
  72.         return 0;
  73. }
  74.  
  75. /*
  76. ** This function will be called when we close the Device file
  77. */
  78. static int etx_release(struct inode *inode, struct file *file)
  79. {
  80.         pr_info("Device File Closed...!!!\n");
  81.         return 0;
  82. }
  83.  
  84. /*
  85. ** This function will be called when we read the Device file
  86. */
  87. static ssize_t etx_read(struct file *filp, char __user *buf, size_t len, loff_t *off)
  88. {
  89.         pr_info("Read Function\n");
  90.         return 0;
  91. }
  92.  
  93. /*
  94. ** This function will be called when we write the Device file
  95. */
  96. static ssize_t etx_write(struct file *filp, const char __user *buf, size_t len, loff_t *off)
  97. {
  98.         pr_info("Write function\n");
  99.         return len;
  100. }
  101.  
  102.  
  103. /*
  104. ** This function will be called when we write IOCTL on the Device file
  105. */
  106. static long etx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  107. {
  108.          switch(cmd) {
  109.             case WR_VALUE:
  110.                 if(copy_from_user(&pid ,(int32_t*) arg, sizeof(pid))) {
  111.                     pr_err("Data Write : Err!\n");
  112.                 }
  113.                 pr_info("Value = %d\n", pid);
  114.                 break;
  115.  
  116.             case IOCTL_READ_VM_AREA_STRUCT: ;
  117.                 struct task_struct *tsk;
  118.                 struct vm_area_struct *vma;
  119.  
  120.                 tsk = get_pid_task(find_get_pid(pid), PIDTYPE_PID);
  121.                 vma = tsk->mm->mmap;
  122.  
  123.                 struct vm_area_struct_info info = (struct vm_area_struct_info) {
  124.                     .vm_start = vma->vm_start,
  125.                     .vm_end = vma->vm_end,
  126.                     .vm_flags = vma->vm_flags,
  127.                     .vm_pgoff = vma->vm_pgoff,
  128.                     .rb_subtree_gap = vma->rb_subtree_gap
  129.                 };
  130.  
  131.                 if(copy_to_user((struct vm_area_struct_info *) arg, &info, sizeof(info))) {
  132.                     pr_err("Data Read : Err!\n");
  133.                 }
  134.                 break;
  135.  
  136.             case IOCTL_READ_PCI_DEVICE_INFO: ;
  137.                 struct pci_dev *dev;
  138.                 dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, NULL);
  139.  
  140.                 struct pci_device_info data = (struct pci_device_info) {
  141.                     .vendor = dev->vendor,
  142.                     .device = dev->device,
  143.                     .devfn = dev->devfn,
  144.                     .pin = dev->pin,
  145.                     .pci_class = dev->class,
  146.                     .revision = dev->revision
  147.                 };
  148.  
  149.                 if(copy_to_user((struct pci_device_info *) arg, &data, sizeof(data))) {
  150.                     pr_err("Data Read : Err!\n");
  151.                 }
  152.                 break;
  153.  
  154.  
  155.             default:
  156.                 pr_info("Default\n");
  157.                 break;
  158.         }
  159.  
  160.         return 0;
  161. }
  162.  
  163. /*
  164. ** Module Init function
  165. */
  166. static int __init etx_driver_init(void)
  167. {
  168.         /*Allocating Major number*/
  169.         if((alloc_chrdev_region(&dev, 0, 1, "etx_Dev")) <0){
  170.                 pr_err("Cannot allocate major number\n");
  171.                 return -1;
  172.         }
  173.         pr_info("Major = %d Minor = %d \n",MAJOR(dev), MINOR(dev));
  174.  
  175.         /*Creating cdev structure*/
  176.         cdev_init(&etx_cdev,&fops);
  177.  
  178.         /*Adding character device to the system*/
  179.         if((cdev_add(&etx_cdev,dev,1)) < 0){
  180.             pr_err("Cannot add the device to the system\n");
  181.             goto r_class;
  182.         }
  183.  
  184.         /*Creating struct class*/
  185.         if((dev_class = class_create(THIS_MODULE,"etx_class")) == NULL){
  186.             pr_err("Cannot create the struct class\n");
  187.             goto r_class;
  188.         }
  189.  
  190.         /*Creating device*/
  191.         if((device_create(dev_class,NULL,dev,NULL,"etx_device")) == NULL){
  192.             pr_err("Cannot create the Device 1\n");
  193.             goto r_device;
  194.         }
  195.         pr_info("Device Driver Insert...Done!!!\n");
  196.         return 0;
  197.  
  198. r_device:
  199.         class_destroy(dev_class);
  200. r_class:
  201.         unregister_chrdev_region(dev,1);
  202.         return -1;
  203. }
  204.  
  205. /*
  206. ** Module exit function
  207. */
  208. static void __exit etx_driver_exit(void)
  209. {
  210.         device_destroy(dev_class,dev);
  211.         class_destroy(dev_class);
  212.         cdev_del(&etx_cdev);
  213.         unregister_chrdev_region(dev, 1);
  214.         pr_info("Device Driver Remove...Done!!!\n");
  215. }
  216.  
  217. module_init(etx_driver_init);
  218. module_exit(etx_driver_exit);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement