初步认识总线设备模型机制

一、模型思想

当插入设备时,总线会去寻找次设备对应的驱动,并且调用驱动的probe函数。不管先有驱动后有设备,或者先有设备后有驱动。都会调用驱动中的probe进行驱动的注册。

注:并不是所以设备和驱动分别只有一条链表。有多条总线。
eg: usb_bus | i2c_bus | spi_bus

二、过程解析

①构造platform_driver结构体和platform_device结构体
②将二者在init函数中注册至总线中

platform_driver_register( )负责注册平台驱动程序



剖析代码流程:
__platform_driver_register(drv,THIS_MODULE)
	drv->driver.bus=&platform_bus_type
	driver_register(&drv->driver)
		ret=bus_add_driver(drv)  //在总线下面添加driver
			klist_add_tail(&priv->knode_bus,&bus->p->klist_drivers); //在总线的驱动链表尾部添加驱动节点
			error=driver_attach(drv)
				bus_for_each_dev(drv->bus,NULL,drv,__driver_attach);//对于总线每一个设备调用driver_accach函数
```c
__driver_attach()中调用
driver_match_device(drv,dev)判断是否匹配,若匹配调用driver_probe_device(drv,dev);

文字描述:注册一个platform_driver时,会把platform_driver放置到总线链表中,对于总线下的每一个设备都会去判断是否匹配,若匹配则调用probe函数。

③两个链表对比链表中的没个成员,若匹配则调用driver_probe

④在probe中定义file_operation结构体和注册驱动
在这里插入图片描述

三、代码

device.c


#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>


static void hello_dev_release(struct device *dev)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
}

//通过资源来设置硬件
static struct resource hello_dev_res[] ={
	{
		//可以定义一些配置参数
		.start=XXX, 
		.end=XXX,   
		.flags=IORESOURCE_TYPE_BITS,
	}
}


static struct platform_device hello_dev = {
        .name = "100ask_hello",
        .dev = {
                .release = hello_dev_release,     
         },
         .num_resources=1,       	//表示有一个资源
         .resource=hello_dev_res,
};

static int __init hello_dev_init(void)
{
    int err;
    
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    err = platform_device_register(&hello_dev);   
    
    return err;
}

static void __exit hello_dev_exit(void)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    platform_device_unregister(&hello_dev);
}

module_init(hello_dev_init);
module_exit(hello_dev_exit);

MODULE_LICENSE("GPL");


driver.c

#include <linux/module.h>

#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>
#include <linux/platform_device.h>

static int major=0;
static int ker_val=123;
static int pin;

static struct file_operations hello_fops = {
	.owner =THIS_MODULE,
	.read  =hello_read,
	.write =hello_write,
}

static int hello_read(struct file *file,char __user *buf, size_t size, loff_t *ppos)
{

    printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    copy_to_user(buf,&ker_val,4);
	return 4;

}

static ssize_t hello_write(struct file *file,const char __user *buf,size_t count, loff_t *ppos)
{
	int err;
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
	copy_from_user(&ker_val,buf,4);
	if(ker_val)
	{
		/*控制LED*/
	}

    return 4;

}

static int hello_probe(struct platform_device *pdev)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
   
    //1.获得资源
    pin=pdev->resource[0].start;
    
	//2.注册file_operations结构体
	major=register_chrdev(0,"hello_drv",&hello_fops);
	return 0;
}

static int hello_remove(struct platform_device *pdev)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 0;
}


static struct platform_driver hello_driver = {
    .probe      = hello_probe,
    .remove     = hello_remove,
    .driver     = {
        .name   = "100ask_hello",
    },
};

static int __init hello_drv_init(void)
{
    int err;
    
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    err = platform_driver_register(&hello_driver); 
    
    return err;
}

static void __exit lhello_drv_exit(void)
{
	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    platform_driver_unregister(&hello_driver);
}

module_init(hello_drv_init);
module_exit(lhello_drv_exit);

MODULE_LICENSE("GPL");

注意:
①当有多个device注册时,若driver适配,则probe会被调用多次
(一个driver支持多个device)
②当多个driver适配一个device,则只调用一次
(一个device仅有一个driver)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值