linux字符設備驅動中內核如何調用驅動入口函數 一點記錄


/* 內核如何調用驅動入口函數 ? */
/* 答: 使用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");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM