歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux下PCI設備驅動程序之注冊詳解

Linux下PCI設備驅動程序之注冊詳解

日期:2017/3/1 9:48:10   编辑:Linux編程

一 識別方式:

天朝為了方便每個人的管理,搞了一套身份識別的玩意(檔案和身份證)。kernel本省就像一個社會,每個進來的設備必須要有它獨特的名字和一些檔案。

這個工作對PCI來說,它是由pci_device_id這個結構體來進行身份信息保存的:

17 struct pci_device_id {
18 __u32 vendor, device; /* Vendor and device ID or PCI_ANY_ID*/
19 __u32 subvendor, subdevice; /* Subsystem ID's or PCI_ANY_ID */
20 __u32 class, class_mask; /* (class,subclass,prog-if) triplet */
21 kernel_ulong_t driver_data; /* Data private to the driver */
22 };

針對一些英文不好的同學,還是有必要對上面的信息進行翻譯一下。vendor,device 分別代表設備商給的設備編號(身份證號)和該設備的名字(姓名)

subverdor,subdevice 分別對應的是該設備是否有別名,絕大多數設備是不需要的。class,class_mask分別對應的是該pci設備屬於哪個設備:比如說,屬於網絡,塊設備。

driver_data是屬於該設備的私有數據,每個設備都不一樣。

二 PCI設備注冊和解注冊:

人有生老病死,同樣,每個驅動也有他們的宿命:出生->生活->死亡(任何中形式)這裡,我就介紹一下PCI設備是怎麼誕生,怎麼死亡的。

誕生:需要注冊函數:pci_register_driver 該函數在Android4.2內核中的路徑是:kernel/drivers/pci/pci-driver.c

該函數的定義:

int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1106 const char *mod_name)
1107 {
1108 int error;
1109
1110 /* initialize common driver fields */
1111 drv->driver.name = drv->name;
1112 drv->driver.bus = &pci_bus_type;
1113 drv->driver.owner = owner;
1114 drv->driver.mod_name = mod_name;
1115
1116 spin_lock_init(&drv->dynids.lock);
1117 INIT_LIST_HEAD(&drv->dynids.list);
1118
1119 /* register with core */
1120 error = driver_register(&drv->driver);
1121 if (error)
1122 goto out;
1123
1124 error = pci_create_newid_file(drv);/* crate new id file of pci */
1125 if (error)
1126 goto out_newid;
1127
1128 error = pci_create_removeid_file(drv); /* same as above function only different in create different attr */
1129 if (error)
1130 goto out_removeid;
1131 out:
1132 return error;
1133
1134 out_removeid:
1135 pci_remove_newid_file(drv);
1136 out_newid:
1137 driver_unregister(&drv->driver);
1138 goto out;
1139 }

死亡:設備的死亡就意味著是解注冊,這裡面要做的是刪除注冊過的文件設備,刪除驅動,把該設備從鏈表中刪除等操作。

1151 void
1152 pci_unregister_driver(struct pci_driver *drv)
1153 {
1154 pci_remove_removeid_file(drv);
1155 pci_remove_newid_file(drv);
1156 driver_unregister(&drv->driver);
1157 pci_free_dynids(drv);
1158 }

Copyright © Linux教程網 All Rights Reserved