#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/cdev.h> #include <linux/fs.h> #include <linux/sched.h> #include <asm/uaccess.h>
MODULE_LICENSE("GPL"); MODULE_AUTHOR("lan");
#define DP_MAJOR 251//主设备号 #define DP_MINOR 0 //次设备号
static int char_read(struct file *filp, char __user *buffer, size_t, loff_t*); static int char_open(struct inode*, struct file*); static int char_write(struct file *filp, const char __user *buffer, size_t, loff_t*); static int char_release(struct inode*, struct file*);
static int chropen; struct cdev *chardev; static int len;
static char *to; static const struct file_operations char_ops={ .read = char_read, .write = char_write, .open = char_open, .release = char_release, };
static int __init char_init(void) { dev_t dev; printk(KERN_ALERT"Initing......\n"); dev = MKDEV(DP_MAJOR, DP_MINOR); chardev = cdev_alloc();
if(chardev == NULL){ return -1; } if(register_chrdev_region(dev, 10, "chardev")){ printk(KERN_ALERT"Register char dev error\n"); return -1; } chropen = 0; len = 0; cdev_init(chardev, &char_ops); if(cdev_add(chardev, dev, 1)){ printk(KERN_ALERT"Add char dev error!\n"); } return 0; }
static int char_open(struct inode *inode, struct file *file) { if(chropen == 0) chropen++; else{ printk(KERN_ALERT"Another process open the char device\n"); return -1; } try_module_get(THIS_MODULE); return 0; }
static int char_release(struct inode *inode,struct file *file) { chropen--; module_put(THIS_MODULE); return 0; }
static int char_read(struct file *filp,char __user *buffer,size_t length,loff_t *offset) { unsigned long nn; nn = copy_to_user(buffer, to, length); printk("nn = %ld\n", nn); printk("buffer = %s\n", buffer); return length; }
static int char_write(struct file *filp, const char __user *buffer, size_t length, loff_t *offset) { unsigned long n; to = (char *)kmalloc((sizeof(char)) * (length+1), GFP_KERNEL); memset(to, '\0', length+1); n = copy_from_user(to, buffer, length); printk("n = %ld\n", n); printk("to = %s\n", to); return length; }
static void __exit module_close(void) { len=0; printk(KERN_ALERT"Unloading..........\n");
unregister_chrdev_region(MKDEV(DP_MAJOR,DP_MINOR),10); cdev_del(chardev); }
module_init(char_init); module_exit(module_close);
|