linux下字符设备驱动的三种创建方式
来源:互联网 发布:2000年总决赛第场数据 编辑:程序博客网 时间:2024/05/19 20:00
第一种:作为混杂设备驱动
/*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 globalmemstruct 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 structreturn 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 structif (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 structstatic 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 miscdeviceif (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 automaticallyglobalmem_major = register_chrdev(0,DEVICE_NAME,&globalmem_fops);if (globalmem_major < 0) return globalmem_major;//create a globalmem classmem_class = class_create(THIS_MODULE,DEVICE_NAME);if (IS_ERR(mem_class))return -1;
//create a globalmem device from this classdevice_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 numberdevice_destroy(mem_class,MKDEV(globalmem_major,0));//destroy globalmem deviceclass_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 0static 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 cdevdev->cdev.owner = THIS_MODULE;err = cdev_add(&dev->cdev, devno, 1);//add cdev to systemif (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_devpif (!globalmem_devp) {result = -ENOMEM;return result;}memset(globalmem_devp, 0, sizeof(struct globalmem_dev));//initialize globalmem_devpdev_t devno = MKDEV(globalmem_major, 0);//create a device numberif (globalmem_major) result = register_chrdev_region(devno, 1, "globalmem_cdev");//request device numberelse {result = alloc_chrdev_region(&devno, 0, 1, "globalmem_cdev");//request device number automaticallyglobalmem_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 systemkfree(globalmem_devp);unregister_chrdev_region(MKDEV(globalmem_major, 0), 1);//release device number}module_init(globalmem_init);module_exit(globalmem_exit);MODULE_LICENSE("GPL");
0 0
- linux下字符设备驱动的三种创建方式
- linux下的字符设备驱动
- linux下的字符设备驱动
- Linux字符设备驱动(三)
- Linux字符设备驱动(三)
- Linux字符设备驱动(三)
- Linux字符设备驱动(三)
- 字符设备驱动--中断方式下的按键驱动
- linux字符设备驱动之设备节点的自动创建
- Linux 设备驱动之字符设备(三)
- Linux 设备驱动之字符设备(三)
- 嵌入式Linux驱动开发(三)——字符设备驱动之查询的方式获取按键值
- 创建字符设备的三种方法
- 创建字符设备的三种方法
- 创建字符设备的三种方法
- 《Linux驱动》创建字符类设备节点
- linux-3.0.1下ok6410的led字符设备驱动
- Linux ALSA声卡驱动之三:PCM设备的创建
- ZOJ 3203 Light Bulb (三分+计算几何)
- Android应用之——仿美团loading加载中动画
- navicat for mysql 9.0.15注册码
- ado链接C++
- Spark(十一) -- Mllib API编程 线性回归、KMeans、协同过滤演示
- linux下字符设备驱动的三种创建方式
- Autolayout(自动布局)
- 生成32位随机字符
- POJ 3744 水概率dp+水矩阵快速幂
- short类型复合操作符问题
- Android十六进制颜色说明
- PowerPC平台 Linux移植二
- _USRDLL _AFXDLL _WINDLL 三种dll编译宏的具体含义
- 设计思路 为机床增加防错功能以实现机床动作的安全控制