Advertisement
lukicdarkoo

Skeleton for Kernel Module

Nov 21st, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/i2c.h>
  4. #include <linux/fs.h>
  5. #include <linux/kernel.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/kdev_t.h>
  8. #include <asm/uaccess.h>
  9. #include <linux/cdev.h>
  10. #include <linux/string.h>
  11.  
  12. #define DEVICE_NAME "simple_driver"
  13. #define BUFFER_SIZE 10
  14.  
  15. static int device_count = 1;
  16. static dev_t device;
  17. static struct cdev character_device;
  18. static int current_buffer_size;
  19.  
  20. static char data_buffer[BUFFER_SIZE];
  21.  
  22.  
  23. static ssize_t on_read(struct file *file, char __user *buf, size_t count, loff_t *ppos);
  24. static ssize_t on_write(struct file *file, char __user *buf, size_t count, loff_t *ppos);
  25. static long on_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  26.  
  27.  
  28. static struct file_operations fops =
  29. {
  30.     .owner = THIS_MODULE,
  31.     .read = on_read,
  32.     .write = on_write,
  33.     .unlocked_ioctl = on_ioctl
  34. };
  35.  
  36. static int __init device_init(void)
  37. {
  38.     int err;
  39.     /* Say hello */
  40.     pr_info("I just started initalization\n");
  41.  
  42.     /* Alocate device number, MAJOR & MINOR */
  43.     if (alloc_chrdev_region(&device, 0, device_count, DEVICE_NAME)) {
  44.         pr_warn("Failed to allocate device number\n");
  45.         err =- ENODEV;
  46.         goto err_exit;
  47.     } else {
  48.         pr_info("Device major number: %d, minor number: %d\n", MAJOR(device), MINOR(device));
  49.     }
  50.  
  51.     /* Register char device */
  52.     cdev_init(&character_device, &fops);
  53.     if (cdev_add(&character_device, device, device_count)) {
  54.         err =- ENODEV;
  55.         goto err_dev_unregister;
  56.     }
  57.  
  58.     return 0;
  59.  
  60.     err_dev_unregister:
  61.     unregister_chrdev_region(device, device_count);
  62.     err_exit:
  63.     return err;
  64. }
  65.  
  66. static void __exit device_exit(void)
  67. {
  68.     cdev_del(&character_device);
  69.     unregister_chrdev_region(device, device_count);
  70. }
  71.  
  72.  
  73. static ssize_t on_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) {
  74.     int bytes_to_read = (current_buffer_size < count) ? current_buffer_size : count;
  75.     current_buffer_size = 0;
  76.  
  77.     if (copy_to_user(buf, data_buffer, bytes_to_read)) {
  78.         return -EFAULT;
  79.     }
  80.  
  81.     return bytes_to_read;
  82. }
  83.  
  84. static ssize_t on_write(struct file *file, char __user *buf, size_t count, loff_t *ppos) {
  85.     if (copy_from_user(data_buffer, buf, count)) {
  86.         return -EFAULT;
  87.     }
  88.  
  89.     current_buffer_size = count;
  90.  
  91.     return count;
  92. }
  93.  
  94. static long on_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
  95.     switch (cmd) {
  96.         case 0:
  97.             pr_warn("Command 0 called\n");
  98.             break;
  99.  
  100.         default:
  101.             return -ENOTTY;
  102.             break;
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
  108. module_init(device_init);
  109. module_exit(device_exit);
  110. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement