编写了一个简单的内核模块
C文件如下
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
static int my_first_drv_open(struct inode *inode, struct file *file)
{
printk(" my_first_drv opend ok!!!\n");
return 0;
}
static ssize_t my_first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
printk(" my_first_drv writed ok!!!\n");
return 0;
}
/* 这个结构是字符设备驱动程序的核心
* 当应用程序操作设备文件时所调用的open、read、write等函数,
* 最终会调用这个结构中指定的对应函数
*/
static struct file_operations my_first_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = my_first_drv_open,
.write = my_first_drv_write,
};
int my_first_drv_init(void) //入口函数
{
register_chrdev(111, "my_first_drv", &my_first_drv_fops); //注册驱动程序
return 0;
}
void my_first_drv_exit(void) //出口函数
{
unregister_chrdev(111, "my_first_drv"); //卸载驱动程序
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(my_first_drv_init);
module_exit(my_first_drv_exit);
MODULE_LICENSE("GPL");
测试程序如下#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>
int main(int argc, char **argv){ int fd; int val=1; fd=open("/dev/my_first_drv",0);if(fd<0) printf("can't open!\n"); write(fd,&val,4);
return 0;
}
模块成功加载到mini2440的内核中,在2440上执行测试程序编译出来的可执行程序输出my_first_drv opend ok!!!就是open里的输出,但是没有write,不知道为啥,各位大神帮分析分析