Linux内核学习散知识整理

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函数的时候,传入的参数是描述该设备的结构体。
 

原文地址:https://www.cnblogs.com/javaadu/p/11742631.html