linux字符设备驱动中内核如何调用驱动入口函数 一点记录

2019/04/10 10:10
阅读数 309

/* 内核如何调用驱动入口函数 ? */
/* 答: 使用module_init()函数,
module_init()函数定义一个结构体,这个结构体里面有一个函数指针,
指向first_drv_init()这个驱动入口函数,当我们加载或安装一个驱动程序时,
内核就会自动找到这样一个结构体,然后调用这个结构体中的函数指针,
从而调用了驱动入口函数first_drv_init(void),该驱动入口函数中有
register_chrdev(主设备号,设备名,struct file_operations结构体指针)函数,
该函数会将驱动程序的file_operations结构体连同其主设备号一起向内核进行注册,
从而该驱动入口函数将一个struct file_operations 类型的结构体传送给内核,
内核可以调用这个结构中的函数指针,从而调用具体的驱动操作函数,
    应用程序操作设备文件时所调用的open, read,write等函数,最终都会
调用struct file_operations类型的结构体,例如在应用程序中用Open()函数打开驱动文件时,

linux内核会根据该设备文件的类型以及主设备号找到在内核中注册的file_operations类型的结构体*/

 

#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 struct class *firstdrv_class;
static struct class_device    *firstdrv_class_dev;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;


static int first_drv_open(struct inode *inode, struct file *file)
{
    //printk("first_drv_open\n");
    /* 配置GPF4,5,6为输出 */
    *gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
    *gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
    return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
    int val;

    //printk("first_drv_write\n");

    copy_from_user(&val, buf, count); //    copy_to_user();

    if (val == 1)
    {
        // 点灯
        *gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
    }
    else
    {
        // 灭灯
        *gpfdat |= (1<<4) | (1<<5) | (1<<6);
    }
    
    return 0;
}

static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open,     
    .write    =    first_drv_write,       
};


int major;
static int first_drv_init(void)/*驱动的入口函数*/
{
    major = register_chrdev(0, "first_drv", &first_drv_fops); /*注册, 把first_drv_fops告诉内核*/

    firstdrv_class = class_create(THIS_MODULE, "firstdrv");

    firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */

    gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
    gpfdat = gpfcon + 1;

    return 0;
}

static void first_drv_exit(void)
{
    unregister_chrdev(major, "first_drv"); // 卸载

    class_device_unregister(firstdrv_class_dev);
    class_destroy(firstdrv_class);
    iounmap(gpfcon);
}
/* 内核如何调用驱动入口函数 ? */
/* 答: 使用module_init()函数,
module_init()函数定义一个结构体,这个结构体里面有一个函数指针,
指向first_drv_init()这个驱动入口函数,当我们加载或安装一个驱动程序时,
内核就会自动找到这样一个结构体,然后调用这个结构体中的函数指针,
从而调用了驱动入口函数first_drv_init(void),该驱动入口函数中有
register_chrdev(主设备号,设备名,struct file_operations结构体指针)函数,
该函数会将驱动程序的file_operations结构体连同其主设备号一起向内核进行注册,
从而该驱动入口函数将一个struct file_operations 类型的结构体传送给内核,
内核可以调用这个结构中的函数指针,从而调用具体的驱动操作函数,
    应用程序操作设备文件时所调用的open, read,write等函数,最终都会
调用struct file_operations类型的结构体*/
module_init(first_drv_init);
/**/
module_exit(first_drv_exit);

/**/
MODULE_LICENSE("GPL");

 

原文出处:https://www.cnblogs.com/yan-hui/p/11290522.html

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部