第12课第2.1节 字符设备驱动程序之LED驱动程序_编写编译(2016/9/21)
一个最简单的字符型驱动:
功能:read write
过程:
| 当调用read或write时会打开文件 open("/dev/xxx") ,将“xxx”的类型和设备号等信息做为寻址依据 |
|
内核中的NFS(虚拟文件系统)通过类型和设备号等信息找到该种类型在寄存器(or内存)中保存的段,段中的file_operation指向到驱动程序中的file_operation定义函数,接着函数再通过其定义结构体中的.open、.write找到open、write函数,执行。 (相关设备注册是通过在驱动中写的regiter函数得到) |
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h> static int first_drv_open(struct inode *inode, struct file *file) { printk("first_drv_open\n"); return 0; } static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos) { printk("first_drv_write\n"); return 0; } static struct file_operations first_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的 __this_module变量 */ //实际是红色的 .open = first_drv_open, .write = first_drv_write, }; int first_drv_init(void) { register_chrdev(111, "first_drv", &first_drv_fops) ;//注册驱动程序这里也是黑色的 return 0; } void first_drv_exit(void) { unregister_chrdev(111, "first_drv") ;//注册驱动程序这里也是黑色的 return 0; } /* xiushi */ module_init(first_drv_init); module_exit(first_drv_exit);
附图:几种驱动在整个系统中的框架

浙公网安备 33010602011771号