6410之驱动的一些优化(自动创建/dev设备节点)

先列出驱动程序框架:

#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);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值