Linux PCI驅(qū)動(dòng)的編寫(xiě)
作者:曹忠明,華清遠(yuǎn)見(jiàn)嵌入式學(xué)院講師。
PCI是外圍設(shè)備互聯(lián)的簡(jiǎn)稱(Peripheral Component Interconnect)的簡(jiǎn)稱,作為一種通用的總線接口標(biāo)準(zhǔn),他在計(jì)算機(jī)系統(tǒng)中得到了廣泛的使用。PCI的速度能夠達(dá)到132M/s。在這里簡(jiǎn)單的介紹一下 linux 下PCI驅(qū)動(dòng)的實(shí)現(xiàn)。
在編寫(xiě)一個(gè)PCI驅(qū)動(dòng)的時(shí)候我們先得確定系統(tǒng)中是否有我們的設(shè)備。我們可以通過(guò)lspci查看PCI設(shè)備。
[root@localhost ~]# lspci
00:00.0 Host bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX Host bridge (rev 01)
00:01.0 PCI bridge: Intel Corporation 440BX/ZX/DX - 82443BX/ZX/DX AGP bridge (rev 01)
00:07.0 ISA bridge: Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)
00:07.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:07.2 USB Controller: Intel Corporation 82371AB/EB/MB PIIX4 USB
00:07.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:0f.0 VGA compatible controller: VMware Inc Abstract SVGA II Adapter
00:10.0 SCSI storage controller: LSI Logic / Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)
00:11.0 PCI bridge: VMware Inc PCI bridge (rev 02)
02:00.0 Ethernet controller: Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE] (rev 10)
02:01.0 Multimedia audio controller: Ensoniq ES1371 [AudioPCI-97] (rev 02)
02:02.0 USB Controller: VMware Inc Abstract USB2 EHCI Controller
確定有設(shè)備以后,我們就可以開(kāi)始我們的PCI設(shè)備驅(qū)動(dòng)的編寫(xiě)了。
1、 首先我們介紹幾個(gè)必須了解的結(jié)構(gòu)體
pci_driver:這個(gè)結(jié)構(gòu)體定義在include/linux/pci.h,這里我們最關(guān)注的是id_table、probe和remove。id_table是一個(gè)結(jié)構(gòu)體數(shù)組,用來(lái)存放驅(qū)動(dòng)程序適用的設(shè)備信息,probe用于檢測(cè)設(shè)備,remove為設(shè)備卸載時(shí)調(diào)用。
struct pci_driver {
struct list_head node;
char *nAME;
const struct pci_device_id *id_table; /* must be non-NULL for probe to be called */
int (*probe) (struct pci_dev *dev, const struct pci_device_id *id); /* New device inserted */
void (*remove) (struct pci_dev *dev); /* Device removed (NULL if not a hot-plug capable driver) */
int (*suspend) (struct pci_dev *dev, pm_message_t state); /* Device suspended */
int (*resume) (struct pci_dev *dev); /* Device woken up */
int (*enable_wake) (struct pci_dev *dev, pci_power_t state, int enable); /* Enable wake event */
void (*shutdown) (struct pci_dev *dev);
struct pci_error_handlers *err_handler;
struct device_driver driver;
struct pci_dynids dynids;
};
pci_dev:這個(gè)結(jié)構(gòu)體同樣也是定義在include/linux/pci.h,它詳細(xì)的定義了PCI的設(shè)備的信息。這些信息我們可以通過(guò)查看proc及sys目錄先相應(yīng)文件得到。
struct pci_dev {
struct list_head global_list; /* node in list of all PCI devices */
struct list_head bus_list; /* node in per-bus list */
struct pci_bus *bus; /* bus this device is on */
struct pci_bus *subordinate; /* bus this device bridges to */
void *sysdata; /* hook for sys-specific extension */
struct proc_dir_entry *procent; /* device entry in /proc/bus/pci */
unsigned int devfn; /* encoded device & function index */
unsigned short vendor;
unsigned short device;
unsigned short subsystem_vendor;
unsigned short subsystem_device;
unsigned int class; /* 3 bytes: (base,sub,prog-if) */
u8 hdr_type; /* PCI header type (`multi" flag masked out) */
u8 rom_base_reg; /* which config register controls the ROM */
u8 pin; /* which interrupt pin this device uses */
struct pci_driver *driver; /* which driver has allocated this device */
u64 dma_mask; /* Mask of the bits of bus address this
device implements. Normally this is
0xffffffff. You only need to change
this if your device has broken DMA
or supports 64-bit transfers. */
pci_power_t current_state; /* Current operating state. In ACPI-speak,
this is D0-D3, D0 being fully functional,
and D3 being off. */
pci_channel_state_t error_state; /* current connectivity state */
struct device dev; /* Generic device interface */
/* device is compatible with these IDs */
unsigned short vendor_compatible[DEVICE_COUNT_COMPATIBLE];
unsigned short device_compatible[DEVICE_COUNT_COMPATIBLE];
int cfg_size; /* Size of configuration space */
/*
* Instead of touching interrupt line and base address registers
* directly, use the values stored here. They might be different!
*/
unsigned int irq;
struct resource resource[DEVICE_COUNT_RESOURCE]; /* I/O and memory regions + expansion ROMs */
/* These fields are used by common fixups */
unsigned int transparent:1; /* Transparent PCI bridge */
unsigned int multifunction:1; /* Part of multi-function device */
/* keep track of device state */
unsigned int is_enabled:1; /* pci_enable_device has been called */
unsigned int is_busmaster:1; /* device is busmaster */
unsigned int no_msi:1; /* device may not use msi */
unsigned int no_d1d2:1; /* only allow d0 or d3 */
unsigned int block_ucfg_access:1; /* userspace config space access is blocked */
unsigned int broken_parity_status:1; /* Device generates false positive parity */
unsigned int msi_enabled:1;
unsigned int msix_enabled:1;
#ifndef __GENKSYMS__
unsigned int is_managed:1;
#endif
u32 saved_config_space[16]; /* config space saved at suspend time */
struct hlist_head saved_cap_space;
struct bin_attribute *rom_attr; /* attribute descriptor for sysfs ROM entry */
int rom_attr_enabled; /* has display of the rom attribute been enabled? */
struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */
#ifndef __GENKSYMS__
u8 revision; /* PCI revision, low byte of class word */
#endif
};
2、 這里我們開(kāi)始編寫(xiě)一個(gè)簡(jiǎn)單的PCI驅(qū)動(dòng)
● LICENSE的聲明必不可少:
MODULE_LICENSE ("GPL");
● pci_driver的定義:
#define PCI_MODULE_NAME "dsp_pci_module"
static struct pci_driver new_pci_driver = {
name: PCI_MODULE_NAME,
id_table:new_pci_tbl,
probe: pci_probe,
remove: pci_remove,
};
pci_driver中對(duì)應(yīng)的pci_tbl定義:
#define NEW_PCI_VENDOR_ID 0x15ad
#define NEW_PCI_DEVICE_ID 0x0405
static struct pci_device_id new_pci_tbl[] __initdata = {
{NEW_PCI_VENDOR_ID, NEW_PCI_DEVICE_ID,
PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
{0,}
};
MODULE_DEVICE_TABLE (pci, new_pci_tbl);
● probe函數(shù)的聲明:
static int __devinit pci_probe(struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
{
/*在這里我們可以對(duì)PCI設(shè)備進(jìn)行初始化及IO的注冊(cè)等操作*/
return 0;
}
● remove函數(shù)的聲明:
static void __devexit pci_remove(struct pci_dev *pci_dev)
{
/*對(duì)資源釋放*/
}
● module_init和module_exit這兩個(gè)函數(shù)在驅(qū)動(dòng)中必不可少,分別在驅(qū)動(dòng)被加載和卸載時(shí)調(diào)用:
static int __init pci_init_module (void)
{
return pci_register_driver(&new_pci_driver);
}
static void __exit pci_cleanup_module (void)
{
pci_unregister_driver(&new_pci_driver);
}
module_init (pci_init_module);
module_exit (pci_cleanup_module);
下面我們說(shuō)一下這個(gè)驅(qū)動(dòng)的執(zhí)行過(guò)程:
系統(tǒng)加載模塊是調(diào)用pci_init_module函數(shù),在這個(gè)函數(shù)中我們通過(guò)pci_register_driver把new_pci_driver注冊(cè)到系統(tǒng)中,這個(gè)函數(shù)首先檢測(cè)id_table中定義的PCI信息是否和系統(tǒng)中的PCI信息有匹配,如果有則返回0,匹配成功后調(diào)用probe函數(shù)對(duì)PCI設(shè)備進(jìn)行進(jìn)一步的操作。同樣在卸載模塊時(shí)調(diào)用pci_cleanup_module,這個(gè)函數(shù)中通過(guò)pci_unregister_driver對(duì)new_pci_driver進(jìn)行注銷(xiāo),這個(gè)會(huì)調(diào)用到remove函數(shù)。
“本文由華清遠(yuǎn)見(jiàn)http://www.embedu.org/index.htm提供”
華清遠(yuǎn)見(jiàn)