|
| 1 | +/* null-driver.c */ |
| 2 | +/* |
| 3 | + Usage: |
| 4 | +
|
| 5 | + make |
| 6 | + sudo insmod null-driver.ko |
| 7 | + echo "some-text" > /dev/mynull |
| 8 | + cat /dev/mynull |
| 9 | + sudo rmmod null-driver.ko |
| 10 | + */ |
| 11 | +#include <linux/module.h> |
| 12 | +#include <linux/version.h> |
| 13 | +#include <linux/kernel.h> |
| 14 | +#include <linux/types.h> |
| 15 | +#include <linux/kdev_t.h> |
| 16 | +#include <linux/fs.h> |
| 17 | +#include <linux/device.h> |
| 18 | +#include <linux/cdev.h> |
| 19 | + |
| 20 | +static dev_t first; // Global variable for the first device number |
| 21 | +static struct cdev c_dev; // Global variable for the character device structure |
| 22 | +static struct class *cl; // Global variable for the device class |
| 23 | +static int my_open(struct inode *i, struct file *f) |
| 24 | +{ |
| 25 | + printk(KERN_INFO "Driver: open()\n"); |
| 26 | + return 0; |
| 27 | +} |
| 28 | +static int my_close(struct inode *i, struct file *f) |
| 29 | +{ |
| 30 | + printk(KERN_INFO "Driver: close()\n"); |
| 31 | + return 0; |
| 32 | +} |
| 33 | +static ssize_t my_read(struct file *f, char __user *buf, size_t |
| 34 | + len, loff_t *off) |
| 35 | +{ |
| 36 | + printk(KERN_INFO "Driver: read()\n"); |
| 37 | + return 0; |
| 38 | +} |
| 39 | +static ssize_t my_write(struct file *f, const char __user *buf, |
| 40 | + size_t len, loff_t *off) |
| 41 | +{ |
| 42 | + printk(KERN_INFO "Driver: write()\n"); |
| 43 | + return len; |
| 44 | +} |
| 45 | +static struct file_operations pugs_fops = |
| 46 | + { |
| 47 | + .owner = THIS_MODULE, |
| 48 | + .open = my_open, |
| 49 | + .release = my_close, |
| 50 | + .read = my_read, |
| 51 | + .write = my_write |
| 52 | + }; |
| 53 | + |
| 54 | +static int __init ofcd_init(void) /* Constructor */ |
| 55 | +{ |
| 56 | + printk(KERN_INFO "null-driver: ofcd registered"); |
| 57 | + if (alloc_chrdev_region(&first, 0, 1, "Shweta") < 0) |
| 58 | + { |
| 59 | + return -1; |
| 60 | + } |
| 61 | + if ((cl = class_create(THIS_MODULE, "chardrv")) == NULL) |
| 62 | + { |
| 63 | + unregister_chrdev_region(first, 1); |
| 64 | + return -1; |
| 65 | + } |
| 66 | + if (device_create(cl, NULL, first, NULL, "mynull") == NULL) |
| 67 | + { |
| 68 | + class_destroy(cl); |
| 69 | + unregister_chrdev_region(first, 1); |
| 70 | + return -1; |
| 71 | + } |
| 72 | + cdev_init(&c_dev, &pugs_fops); |
| 73 | + if (cdev_add(&c_dev, first, 1) == -1) |
| 74 | + { |
| 75 | + device_destroy(cl, first); |
| 76 | + class_destroy(cl); |
| 77 | + unregister_chrdev_region(first, 1); |
| 78 | + return -1; |
| 79 | + } |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +static void __exit ofcd_exit(void) /* Destructor */ |
| 84 | +{ |
| 85 | + cdev_del(&c_dev); |
| 86 | + device_destroy(cl, first); |
| 87 | + class_destroy(cl); |
| 88 | + unregister_chrdev_region(first, 1); |
| 89 | + printk(KERN_INFO "null-driver: ofcd unregistered"); |
| 90 | +} |
| 91 | + |
| 92 | +module_init(ofcd_init); |
| 93 | +module_exit(ofcd_exit); |
| 94 | +MODULE_LICENSE("GPL"); |
| 95 | +MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>"); |
| 96 | +MODULE_DESCRIPTION("Our First Character Driver"); |
0 commit comments