cpp
复制代码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/pci.h>
MODULE_LICENSE("GPL");
#define MY_VENDOR_ID 0xAAAA //厂家ID, PCI厂家ID在全球统一分配和管理
#define MY_DEVICE_ID_NET1 0xFF01 //卡ID
#define MY_DEVICE_ID_NET2 0xFF02 //卡ID
//驱动中支持多个卡,用此来标识每个卡的信息
enum my_asic_type {
CHIP_MY_A0 = 0,
CHIP_MY_B0 = 1,
CHIP_LAST
};
//设置驱动所支持的PCI卡列表,本例子支持A0, B0两个卡
struct pci_device_id my_network_driver_pci_table[] = {
{
MY_VENDOR_ID,
MY_DEVICE_ID_NET1,
PCI_ANY_ID,
PCI_ANY_ID,
0,
0,
CHIP_MY_A0
},
{
MY_VENDOR_ID, //factory id
MY_DEVICE_ID_NET2,//device id
PCI_ANY_ID,
PCI_ANY_ID,
0,
0,
CHIP_MY_B0
},
};
//probe函数
static int my_driver_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
return 0;
}
//remove 函数
static void my_driver_remove(struct pci_dev *pdev)
{
return;
}
//pci_driver 定义
struct pci_driver my_network_pci_driver = {
.name = "myntwrk",
.probe = my_driver_probe,
.remove = my_driver_remove,
.id_table = my_network_driver_pci_table,
};
static int my_network_driver_init(void)
{
pci_register_driver(&my_network_pci_driver);
return 0;
}
static void my_network_driver_exit(void)
{
pci_unregister_driver(&my_network_pci_driver);
}
module_init(my_network_driver_init);
module_exit(my_network_driver_exit);
//MakeFile
obj-m := mypci.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
EXTRA_CFLAGS = -g
ccflags-y := -O0
all:
make -C $(KERNEL_DIR) M=$(PWD) modules
clean:
make -C $(KERNEL_DIR) M=$(PWD) clean