Linux如何使用相同的驱动程序来驱动许多网卡? [英] How linux drive many network cards with the same driver?

查看:112
本文介绍了Linux如何使用相同的驱动程序来驱动许多网卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近正在学习linux网络驱动程序,我想知道如果我的板上有很多同类型的网卡,内核如何驱动它们?内核是否需要多次加载同一驱动程序?我认为这是不可能的,insmod不会这样做,那么如何使所有相同种类的卡同时工作?

I am learning linux network driver recently, and I wonder that if I have many network cards in same type on my board, how does the kernel drive them? Does the kernel need to load the same driver many times? I think it's not possible, insmod won't do that, so how can I make all same kind cards work at same time?

致谢

推荐答案

每张卡的状态(I/O地址,IRQ,...)存储在特定于驱动程序的结构中,该结构直接(或间接)传递到驱动程序的每个入口点,这样可以区分卡.这样,完全相同的代码就可以控制不同的卡(也就是说,无论控制的设备数量是多少,内核仅保留一个驱动程序模块的实例).

The state of every card (I/O addresses, IRQs, ...) is stored into a driver-specific structure that is passed (directly or indirectly) to every entry point of the driver which can this way differenciate the cards. That way the very same code can control different cards (which means that yes, the kernel only keeps one instance of a driver's module no matter the number of devices it controls).

例如,看一下drivers/video/backlight/platform_lcd.c,它是一个非常简单的LCD电源驱动器.它包含一个名为platform_lcd的结构,该结构专用于此文件,并存储LCD的状态(是否已通电以及是否已挂起).通过kzalloc在驱动程序的probe功能中分配此结构的一个实例,即每个LCD设备分配一个实例,并使用platform_set_drvdata存储到代表LCD的平台设备中.然后,已在所有其他驱动程序功能的开头取回已为此设备分配的实例,以便它知道它正在处理的实例:

For instance, have a look at drivers/video/backlight/platform_lcd.c, which is a very simple LCD power driver. It contains a structure called platform_lcd that is private to this file and stores the state of the LCD (whether it is powered, and whether it is suspended). One instance of this structure is allocated in the probe function of the driver through kzalloc - that is, one per LCD device - and stored into the platform device representing the LCD using platform_set_drvdata. The instance that has been allocated for this device is then fetched back at the beginning of all other driver functions so that it knows which instance it is working on:

struct platform_lcd *plcd = to_our_lcd(lcd);

如果查看include/linux/lcd.h

to_our_lcd会扩展为lcd_get_data,而lcd_get_data本身也会扩展为dev_get_drvdata(platform_set_drvdata的副本).然后,该函数可以知道设备的状态已被调用.

to_our_lcd expands to lcd_get_data which itself expands to dev_get_drvdata (a counterpart of platform_set_drvdata) if you look at include/linux/lcd.h. The function can then know the state of the device is has been invoked for.

这是一个非常简单的示例,platform_lcd驱动程序不直接控制任何设备(这将延迟到平台数据中的功能指针),而是添加特定于硬件的参数(IRQ,I/O基础,等),您将了解Linux中99%的驱动程序是如何工作的.

This is a very simple example, and the platform_lcd driver does not directly control any device (this is deferred to a function pointer in the platform data), but add hardware-specific parameters (IRQ, I/O base, etc.) and you get how 99% of the drivers in Linux work.

这篇关于Linux如何使用相同的驱动程序来驱动许多网卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆