1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| # include <linux/module.h> # include <linux/types.h> # include <linux/fs.h> # include <linux/version.h> # include <linux/delay.h> # include <linux/crc32.h> # include <linux/interrupt.h> # include <linux/init.h> # include <linux/miscdevice.h> # include <linux/cdev.h> # include <linux/uaccess.h> # define DEV_DRIVER_NAME "HelloDev" char memory[50]();
int hello_open(struct inode * inode, struct file * file) { return 0; }
int hello_close(struct inode * inode, struct file * file) { return 0; }
ssize_t hello_read(struct file *filp, char __user *buff, size_t count, loff_t *pos) { if (count > strlen(memory)) { count = strlen(memory); } if (count < 0) { return -EINVAL; } if (copy_to_user(buff,memory,count)){ return EFAULT; }
return count;
}
ssize_t hello_write(struct file *filp, const char __user *buff, size_t count, loff_t *pos) {
if (count > strlen(memory)) { count = strlen(memory); } if (count < 0) { return -EINVAL; } if (copy_from_user(buff,memory,count)){ return EFAULT; } return count;
}
ssize_t hello_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { return 0; }
ssize_t unlocked_hello_ioctl(struct file *file, unsigned int cmd, unsigned long arg) { return hello_ioctl(NULL, file, cmd, arg); }
static struct file_operations hello_fops = { .owner = THIS_MODULE, # if (LINUX_VERSION_CODE < KERNEL_VERSION( 2,6,35 )) .ioctl = hello_ioctl, # else .unlocked_ioctl = unlocked_hello_ioctl, # endif .open = hello_open, .release = hello_close, .read = hello_read, .write = hello_write };
static struct miscdevice hello_miscdevice = { MISC_DYNAMIC_MINOR, DEV_DRIVER_NAME, &hello_fops, };
static int __init hello_init_module(void) { return misc_register(&hello_miscdevice); }
static void __exit hello_cleanup_module(void) { misc_deregister(&hello_miscdevice); printk(KERN_ALERT”Hello__world!n”); }
module_init(hello_init_module); module_exit(hello_cleanup_module);_
|