6410之驱动的一些优化(自动创建/dev设备节点)
2013-07-16 16:12
483 查看
先列出驱动程序框架:
一、验证上层app是根据设备类型和主设备号而不是根据名字来找底层驱动的。
验证程序:
mknod /dev/xxx c 123 0
执行测试程序的结果为:
/KO # ./first_drv_test
first_drv_open!!
first_drv_write!!
可以看到,执行测试程序后,会调用到底层驱动的open和write函数。接下来将驱动中的主设备号改为111测试,register_chrdev(111,"first_drv",&first_drv_fops);再运行测试程序发现结果如下:
/KO # ./first_drv_test
Can not open devices
这时的/dev/xxx的属性仍是:
crw-r--r-- 1 0 0 123, 0
Jan 1 00:05 /dev/xxx
所以打不开设备的错误提示。
二、自动获取主设备号:
方法:
major = register_chrdev(0,"first_drv",&first_drv_fops);
cat /proc/devices会发现:
180 usb
188 ttyUSB
189 usb_device
204 s3c2410_serial
253 first_drv
254 rtc
主设备号会被自动的分配为253.register_chrdev的返回值就是主设备号
三、如何自动注册设备节点,而不用手动mknod来设置?
定义类和设备
struct class *first_dev_class;
struct device *first_dev;
创建类和设备:
first_dev_class = class_create(THIS_MODULE, "first_drv_class");
first_dev= device_create(first_dev_class, NULL, MKDEV(major,0), NULL, "xyz");//创建的设备节点名字为:xyz ,最后会根据此时生成的系统信息来自动添加设备节点
销毁类和设备:
device_destroy(first_dev_class, MKDEV(major,0));
class_destroy(first_dev_class);
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/irq.h> #include <linux/interrupt.h> #include <asm/uaccess.h> static int first_drv_open(struct inode *inode, struct file *file) { printk("first_drv_open!!\n"); return 0; } static int first_drv_write(struct file *filp, char __user *buff, size_t count, loff_t *offp) { printk("first_drv_write!!\n"); return 0; } static struct file_operations first_drv_fops = { .owner = THIS_MODULE, .open = first_drv_open, .write = first_drv_write, }; static int __init first_drv_init(void) { printk("first_drv_init\n"); register_chrdev(123,"first_drv",&first_drv_fops);//主设备号为123 return 0; } static void __exit first_drv_exit(void) { printk("first_drv_exit\n"); unregister_chrdev(123,"first_drv"); } module_init(first_drv_init); module_exit(first_drv_exit); MODULE_LICENSE("GPL");
一、验证上层app是根据设备类型和主设备号而不是根据名字来找底层驱动的。
验证程序:
#include <stdio.h> #include <fcntl.h> int main() { int fd ; int val = 1; fd = open("/dev/xxx",O_RDWR); //打开设备节点/dev/xxx if(fd < 0){ printf("Can not open devices\n"); return -1; } write(fd,&val,4);//向设备节点做写操作 return 0; }创建设备节点:
mknod /dev/xxx c 123 0
执行测试程序的结果为:
/KO # ./first_drv_test
first_drv_open!!
first_drv_write!!
可以看到,执行测试程序后,会调用到底层驱动的open和write函数。接下来将驱动中的主设备号改为111测试,register_chrdev(111,"first_drv",&first_drv_fops);再运行测试程序发现结果如下:
/KO # ./first_drv_test
Can not open devices
这时的/dev/xxx的属性仍是:
crw-r--r-- 1 0 0 123, 0
Jan 1 00:05 /dev/xxx
所以打不开设备的错误提示。
二、自动获取主设备号:
方法:
major = register_chrdev(0,"first_drv",&first_drv_fops);
cat /proc/devices会发现:
180 usb
188 ttyUSB
189 usb_device
204 s3c2410_serial
253 first_drv
254 rtc
主设备号会被自动的分配为253.register_chrdev的返回值就是主设备号
三、如何自动注册设备节点,而不用手动mknod来设置?
定义类和设备
struct class *first_dev_class;
struct device *first_dev;
创建类和设备:
first_dev_class = class_create(THIS_MODULE, "first_drv_class");
first_dev= device_create(first_dev_class, NULL, MKDEV(major,0), NULL, "xyz");//创建的设备节点名字为:xyz ,最后会根据此时生成的系统信息来自动添加设备节点
销毁类和设备:
device_destroy(first_dev_class, MKDEV(major,0));
class_destroy(first_dev_class);
相关文章推荐
- android 字符设备驱动模块及在 /dev 下自动创建设备节点问题
- 内核驱动模块如何在/dev文件下自动创建设备
- Linux 字符设备驱动结构(二)—— 自动创建设备节点
- (一)小记——驱动中使用class_device_create()报错的原因、自动创建设备节点
- linux字符驱动之自动创建设备节点
- linux字符设备驱动 自动创建设备节点的的方法
- Linux驱动编程 step-by-step (四) 字符设备的注册与设备节点的自动创建
- linux驱动开发--字符设备:自动创建设备节点
- 驱动中使用class_device_create()报错的原因,自动创建设备节点
- 设备驱动--自动创建节点
- linux字符驱动之自动创建设备节点
- Linux驱动开发之四-----LED改进测试(增加自动创建设备节点)
- LED驱动,自动创建设备节点
- android中usb设备驱动不能自动创建设备节点
- linux字符设备驱动中自动创建设备节点【转】
- linux驱动开发之字符设备--自动创建设备节点
- linux字符设备驱动:自动创建设备及其节点
- 内核驱动模块如何在/dev文件下自动创建设备
- linux字符驱动之自动创建设备节点
- Linux驱动编程 step-by-step (四) 字符设备的注册与设备节点的自动创建