Linux编程:网络设备驱动详解
2009-06-30 04:36:00 来源:WEB开发网2,PCI设备探测函数probe,初始化网络设备
static int __devinit tg3_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
{
//初始化设备,使I/O,memory可用,唤醒设备
pci_enable_device(pdev);
//申请内存空间,配置网卡的I/O,memory资源
pci_request_regions(pdev, DRV_MODULE_NAME);
pci_set_master(pdev);
//设置DMA属性
pci_set_dma_mask(pdev, (u64) 0xffffffffffffffff);
//网卡 I/O,memory资源的启始地址
tg3reg_base = pci_resource_start(pdev, 0);
//网卡I/O,memory资源的大小
tg3reg_len = pci_resource_len(pdev, 0);
//分配并设置网络设备
dev = alloc_etherdev(sizeof(*tp));
//申明为内核设备模块
SET_MODULE_OWNER(dev);
//初始化私有结构中的各成员值
tp = dev->priv;
tp->pdev = pdev;
tp->dev = dev;
……
//锁的初始化
spin_lock_init(&tp->lock);
//映射I/O,memory地址到私有域中的寄存器结构
tp->regs = (unsigned long) ioremap(tg3reg_base, tg3reg_len);
dev->irq = pdev->irq;
//网络设备回调函数赋值
dev->open = tg3_open;
dev->stop = tg3_close;
dev->get_stats = tg3_get_stats;
dev->set_multicast_list = tg3_set_rx_mode;
dev->set_mac_address = tg3_set_mac_addr;
dev->do_ioctl = tg3_ioctl;
dev->tx_timeout = tg3_tx_timeout;
dev->hard_start_xmit= tg3_start_xmit;
//网卡的MAC地址赋值dev->addr
tg3_get_device_address(tp);
//注册网络设备
register_netdev(dev);
//把网络设备指针地址放入PCI设备中的设备指针中
pci_set_drvdata(pdev, dev);
}
更多精彩
赞助商链接