linux下字符设备驱动的三种创办方式
linux下字符设备驱动的三种创建方式
第一种:作为混杂设备驱动
/* register globalmem as a miscdevice */ #include<linux/module.h> #include<linux/init.h> #include<linux/fs.h> #include<linux/sched.h> #include<linux/miscdevice.h> #include<linux/string.h> #include<linux/errno.h> #include<linux/types.h> #include<linux/slab.h> #include<asm/uaccess.h> #define GLOBALMEM_SIZE 0x1000 #define DEVICE_NAME "globalmem_misc" //create a struct for globalmem struct globalmem_dev { unsigned char mem[GLOBALMEM_SIZE]; }; struct globalmem_dev *globalmem_devp; int globalmem_open(struct inode *inode, struct file *filp) { filp->private_data = globalmem_devp; //storing the globalmem to the private_data of file struct return 0; } int globalmem_release(struct inode *inode, struct file *filp) { return 0; } static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data;//get globalmem_dev from file struct if (p >= GLOBALMEM_SIZE) return 0; if (count > GLOBALMEM_SIZE - p) count = GLOBALMEM_SIZE - p; if (copy_to_user(buf, (void*)(dev->mem + p), count)) ret = -EFAULT; else { *ppos += count; ret = count; printk(KERN_INFO "read %d bytes(s) from %d\n", count, p); } return ret; } static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos) { unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct globalmem_dev *dev = filp->private_data; if (p >= GLOBALMEM_SIZE) return 0; if (count > GLOBALMEM_SIZE - p) count = GLOBALMEM_SIZE - p; if (copy_from_user(dev->mem + p, buf, count)) ret = -EFAULT; else { *ppos += count; ret = count; printk(KERN_INFO "written %d butes(s) from %d \n", count, p); } return ret; } static const struct file_operations globalmem_fops = { .owner = THIS_MODULE, .read = globalmem_read, .write = globalmem_write, .open = globalmem_open, .release = globalmem_release, }; //miscdevice struct static struct miscdevice misc = { .minor = MISC_DYNAMIC_MINOR,//allocate minor device number automatically,major number of msicdevice is 10 .name = DEVICE_NAME, .fops = &globalmem_fops, }; int globalmem_init(void) { int result; result = misc_register(&misc);//register a miscdevice if (result) return result; globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL); if (!globalmem_devp) { result = -ENOMEM; goto fail_malloc; } memset(globalmem_devp, 0, sizeof(struct globalmem_dev)); return 0; fail_malloc: misc_deregister(&misc); return result; } void globalmem_exit(void) { kfree(globalmem_devp); misc_deregister(&misc);//deregister miscdevice } module_init(globalmem_init); module_exit(globalmem_exit); MODULE_LICENSE("GPL");
第二种:使用class_create和device_create
#include<linux/module.h> #include<linux/init.h> #include<linux/fs.h> #include<linux/sched.h> #include<linux/device.h> #include<linux/string.h> #include<linux/errno.h> #include<linux/types.h> #include<linux/slab.h> #include<asm/uaccess.h> #define GLOBALMEM_SIZE 0x1000 #define DEVICE_NAME "globalmem_class" unsigned char globalmem_major; static struct class *mem_class; struct globalmem_dev { unsigned char mem[GLOBALMEM_SIZE]; }; struct globalmem_dev *globalmem_devp; int globalmem_open(struct inode *inode, struct file *filp) { ··· } int globalmem_release(struct inode *inode, struct file *filp) { ··· } static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { ··· } static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos) { ··· } static const struct file_operations globalmem_fops = { ··· }; int globalmem_init(void) { globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL); if (!globalmem_devp) return -1; memset(globalmem_devp, 0, sizeof(struct globalmem_dev)); //the first parameter 0 means allocate major device number automatically globalmem_major = register_chrdev(0,DEVICE_NAME,&globalmem_fops); if (globalmem_major < 0) return globalmem_major; //create a globalmem class mem_class = class_create(THIS_MODULE,DEVICE_NAME); if (IS_ERR(mem_class)) return -1;
//create a globalmem device from this class device_create(mem_class,NULL,MKDEV(globalmem_major,0),NULL,DEVICE_NAME); return 0; } void globalmem_exit(void) { unregister_chrdev(globalmem_major,DEVICE_NAME);//release major device number device_destroy(mem_class,MKDEV(globalmem_major,0));//destroy globalmem device class_destroy(mem_class);//destroy globalmem class } module_init(globalmem_init); module_exit(globalmem_exit); MODULE_LICENSE("GPL");
第三种:使用cdev结构体
/* If we use cdev to register globalmem driver,we need to use command "mknod /dev/globalmem_cdev c 244 0" to create a device node (postulate 244 is the major device number of globalmem). */ #include<linux/module.h> #include<linux/init.h> #include<linux/fs.h> #include<linux/sched.h> #include<linux/cdev.h> #include<linux/string.h> #include<linux/errno.h> #include<linux/types.h> #include<linux/slab.h> #include<asm/uaccess.h> #define GLOBALMEM_SIZE 0x1000 #define MEM_CLEAR 0x1 #define GLOBALMEM_MAJOR 0 static int globalmem_major = GLOBALMEM_MAJOR; struct globalmem_dev { struct cdev cdev; unsigned char mem[GLOBALMEM_SIZE]; }; struct globalmem_dev *globalmem_devp; int globalmem_open(struct inode *inode, struct file *filp) { ··· } int globalmem_release(struct inode *inode, struct file *filp) { ··· } static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { ··· } static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos) { ··· } static const struct file_operations globalmem_fops = { ··· }; static void globalmem_setup_cdev(struct globalmem_dev *dev, int index) { int err, devno = MKDEV(globalmem_major, 0); cdev_init(&dev->cdev, &globalmem_fops);//initialize cdev dev->cdev.owner = THIS_MODULE; err = cdev_add(&dev->cdev, devno, 1);//add cdev to system if (err) printk(KERN_NOTICE "Error %d adding globalmem %d", err, index); } int globalmem_init(void) { int result; globalmem_devp = kmalloc(sizeof(struct globalmem_dev), GFP_KERNEL);//allocate memory for globalmem_devp if (!globalmem_devp) { result = -ENOMEM; return result; } memset(globalmem_devp, 0, sizeof(struct globalmem_dev));//initialize globalmem_devp dev_t devno = MKDEV(globalmem_major, 0);//create a device number if (globalmem_major) result = register_chrdev_region(devno, 1, "globalmem_cdev");//request device number else { result = alloc_chrdev_region(&devno, 0, 1, "globalmem_cdev");//request device number automatically globalmem_major = MAJOR(devno); } if (result < 0) return result; globalmem_setup_cdev(globalmem_devp, 0); return 0; } void globalmem_exit(void) { cdev_del(&globalmem_devp->cdev);//delete cdev from system kfree(globalmem_devp); unregister_chrdev_region(MKDEV(globalmem_major, 0), 1);//release device number } module_init(globalmem_init); module_exit(globalmem_exit); MODULE_LICENSE("GPL");