先列出驱动程序框架:
#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");
验证程序:
#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; }创建设备节点:
/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);