|
| 1 | +#include <linux/init.h> |
| 2 | +#include <linux/kernel.h> |
| 3 | +#include <linux/module.h> |
| 4 | +#include <linux/fs.h> |
| 5 | + |
| 6 | +static ssize_t drv_read(struct file* filp, char* buf, size_t count, loff_t* ppos) { |
| 7 | + printk("device read\n"); |
| 8 | + return count; |
| 9 | +} |
| 10 | + |
| 11 | +static ssize_t drv_write(struct file* filp, const char* buf, size_t count, loff_t* ppos) { |
| 12 | + printk("device write\n"); |
| 13 | + return count; |
| 14 | +} |
| 15 | + |
| 16 | +static int drv_open(struct inode* inode, struct file* filp) { |
| 17 | + printk("device open\n"); |
| 18 | + return 0; |
| 19 | +} |
| 20 | + |
| 21 | +static long drv_ioctl(struct file* filp, unsigned int cmd, unsigned long value) { |
| 22 | + printk("device ioctl\n"); |
| 23 | + return 0; |
| 24 | +} |
| 25 | + |
| 26 | +static int drv_release(struct inode* inode, struct file* filp) { |
| 27 | + printk("device close\n"); |
| 28 | + return 0; |
| 29 | +} |
| 30 | + |
| 31 | +struct file_operations drv_fops = { |
| 32 | +read: |
| 33 | + drv_read, |
| 34 | +write: |
| 35 | + drv_write, |
| 36 | +unlocked_ioctl: |
| 37 | + drv_ioctl, |
| 38 | +open: |
| 39 | + drv_open, |
| 40 | +release: |
| 41 | + drv_release, |
| 42 | +}; |
| 43 | + |
| 44 | +#define MAJOR_NUM 60 |
| 45 | +#define MODULE_NAME "DEMO" |
| 46 | + |
| 47 | +static int demo_init(void) { |
| 48 | + if (register_chrdev(MAJOR_NUM, "demo", &drv_fops) < 0) { |
| 49 | + printk("<1>%s: can't get major %d\n", MODULE_NAME, MAJOR_NUM); |
| 50 | + return (-EBUSY); |
| 51 | + } |
| 52 | + |
| 53 | + printk("<1>%s: started\n", MODULE_NAME); |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +static void demo_exit(void) { |
| 58 | + unregister_chrdev(MAJOR_NUM, "demo"); |
| 59 | + printk("<1>%s: removed\n", MODULE_NAME); |
| 60 | +} |
| 61 | + |
| 62 | +module_init(demo_init); |
| 63 | +module_exit(demo_exit); |
| 64 | + |
| 65 | + |
0 commit comments