一、基于linux-3.18.20、mac驅(qū)動(dòng)
二、啟動(dòng)時(shí)機(jī):
所謂的"probe”,是指在Linux內(nèi)核中,如果存在相同名稱的device和device_driver,內(nèi)核就會(huì)執(zhí)行device_driver中的probe回調(diào)函數(shù),而該函數(shù)就是所有driver的入口,可以執(zhí)行諸如硬件設(shè)備初始化、字符設(shè)備注冊(cè)、設(shè)備文件操作ops注冊(cè)等動(dòng)作("remove”是它的反操作,發(fā)生在device或者device_driver任何一方從內(nèi)核注銷時(shí)。
將struct device類型的變量注冊(cè)到內(nèi)核中時(shí)自動(dòng)觸發(fā)(device_register,device_add,device_create_vargs,device_create)
將struct device_driver類型的變量注冊(cè)到內(nèi)核中時(shí)自動(dòng)觸發(fā)(driver_register)
手動(dòng)查找同一bus下的所有device_driver,如果有和指定device同名的driver,執(zhí)行probe操作(device_attach)
手動(dòng)查找同一bus下的所有device,如果有和指定driver同名的device,執(zhí)行probe操作(driver_attach)
自行調(diào)用driver的probe接口,并在該接口中將該driver綁定到某個(gè)device結(jié)構(gòu)中----即設(shè)置dev->driver(device_bind_driver)
三、流程
3.1 注冊(cè)平臺(tái)驅(qū)動(dòng)
ret = platform_driver_register(&usrmac_dev_driver);
#define platform_driver_register(drv) platform_driver_register(drv, THIS_MODULE)
__platform_driver_register(drv, THIS_MODULE)
{
...
return driver_register(&drv->driver);
}
int driver_attach(struct device_driver *drv)
{
...
ret = bus_add_driver(drv);
...
}
int bus_add_driver(struct device_driver *drv)
{
...
return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
...
}
本函數(shù)沒有給__driver_attach傳遞參數(shù)。
int bus_for_each_dev(struct bus_type *bus, struct device *start,?void *data, int (*fn)(struct device *, void *))
{
....
klist_iter_init_node(&bus->p->klist_devices, &i,(start ? &start->p->knode_bus : NULL));
while ((dev = next_device(&i)) && !error)
error = fn(dev, data);
....
}
分支一:賦值i->i_klist、i->i_cur
因?yàn)閟tart為NULL,故傳遞的第三個(gè)參數(shù)n為NULL
void klist_iter_init_node(struct klist *k, struct klist_iter *i,
struct klist_node *n)
{
i->i_klist = k;
i->i_cur = n;
if (n)
kref_get(&n->n_ref);
}
其中
i->i_klist =?k =?&bus->p->klist_devices
i->i_cur =?n =?(start ? &start->p->knode_bus : NULL) = NULL;
分之二:
static struct device *next_device(struct klist_iter *i)
{
struct klist_node *n = klist_next(i);
struct device *dev = NULL;
struct device_private *dev_prv;
if (n)
{
dev_prv = to_device_private_bus(n);
dev = dev_prv->device;
}
return dev;
}
#define to_device_private_bus(obj) ?container_of(obj, struct device_private, knode_bus)
參數(shù):
i為
struct klist_iter {
struct klist *i_klist;
struct klist_node *i_cur;
};
被賦值為
i->i_klist = k;
i->i_cur = n;
next_device(&i),因?yàn)榈谝粋€(gè)節(jié)點(diǎn)為頭節(jié)點(diǎn),需要從下一個(gè)開始
struct klist_node *n = klist_next(i);
n為
struct klist_node {
void *n_klist; /* never access directly */
struct list_head n_node;
struct kref n_ref;
};
struct kref {
atomic_t refcount;
};
klist_iter_init_node(&bus->p->klist_devices, &i,(start ?&start->p->knode_bus :?NULL))作用是定義個(gè)klist_iter指向此klist,以便以后直接使用
struct klist_node *klist_next(struct klist_iter *i)
{
...
struct klist_node *last = i->i_cur;
if (last)
{
//此處不執(zhí)行
}
else
next = to_klist_node(i->i_klist->k_list.next);
i->i_cur = NULL;
while (next != to_klist_node(&i->i_klist->k_list))
{
if (likely(!knode_dead(next)))
{
kref_get(&next->n_ref);
i->i_cur = next;
break;
}
next = to_klist_node(next->n_node.next);
}
...
}
static struct klist_node *to_klist_node(struct list_head *n)
{
return container_of(n, struct klist_node, n_node);
}
取出了包含i->i_klist->k_list.next的n_node指針,不過next所指的和n_node地址偏差一個(gè)head指針(list_head包括head和next倆指針)。
while循環(huán)是從第一個(gè)目標(biāo)to_klist_node(i->i_klist->k_list.next)循環(huán),當(dāng)再次循環(huán)到頭節(jié)點(diǎn)to_klist_node(&i->i_klist->k_list)時(shí)截止(這是個(gè)循環(huán)鏈表,總會(huì)再次循環(huán)回來的)。
還一個(gè)結(jié)束的條件,當(dāng)循環(huán)到knode_dead(next)為真時(shí)break,不過,likely說明了next通常不會(huì)是dead的。
Klist_iter找到合適的即停止搜索,找到此處的device_private的device。
此結(jié)構(gòu)即為傳入probe函數(shù)的參數(shù)。
找到參數(shù)后,繼續(xù)執(zhí)行return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);中error = fn(dev, data);即__driver_attach函數(shù)。
第一個(gè)參數(shù)dev為剛剛while ((dev = next_device(&i)) && !error)索引產(chǎn)生。
static int __driver_attach(struct device *dev, void *data)
{
...
if (!dev->driver)
driver_probe_device(drv, dev);
...
}
int driver_probe_device(struct device_driver *drv, struct device *dev)
{
...
ret = really_probe(dev, drv);
...
}
static int really_probe(struct device *dev, struct device_driver *drv)
{
...
if (dev->bus->probe)
{
ret = dev->bus->probe(dev);
if (ret)
goto probe_failed;
}
else if (drv->probe)
{
ret = drv->probe(dev);
if (ret)
goto probe_failed;
}
...
}
static struct platform_driver usrmac_dev_driver = {
.probe = usrmac_dev_probe,
.remove = usrmac_dev_remove,
.suspend = usrmac_dev_suspend,
.resume = usrmac_dev_resume,
.driver =
{
.owner = THIS_MODULE,
.name = USRMAC_DRIVER_NAME,
.of_match_table = usrmac_of_match,
},
};
static int usrmac_dev_probe(struct platform_device *pdev)
{
//struct platform_device *pdev即Klist_iter找到的
}
?
評(píng)論
查看更多