Linux内核学习散知识整理

本文详细介绍了container_of宏和probe函数在Linux系统中的使用方法及应用场景,包括如何通过container_of宏获取结构体指针,以及探针对设备驱动的调用时机与作用。

1、container_of(ptr, type, member)

使用方法:根据指向结构体type的成员member的指针ptr,获取指向改结构体的指针

/**
* container_of - cast a member of a structure out to the containing structure
* @ptr:        the pointer to the member. (指向member的指针)
* @type:       the type of the container struct this is embedded in. (该成员所属的结构体名称) 
* @member:     the name of the member within the struct.  (该成员在结构体中的名称)
* */

参考文章:

container_of宏

container_of分析

 

例子(来自于incluce/linux/virtio.h):

该函数的作用是:通过具体的设备驱动drv,获取其所属的virtio_driver指针

<span style="font-size:18px;">static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
{
       return container_of(drv, struct virtio_driver, driver)}</span>

2、probe()函数何时调用?  当系统启动时,设备device先注册到总线bus上,然后bus负责调用对应的probe()函数,这个probe()函数是预先编写好的回调函数,BUS架构通过便利其所管理的driver链表并进行“字符串匹配”,然后它就知道该调用哪个driver的probe函数,来处理目前探测到的设备。在调用probe函数的时候,传入的参数是描述该设备的结构体。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值