linux如何用同一个驱动驱动多个网卡? [英] How linux drive many network cards with the same driver?

查看:29
本文介绍了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设备一个——并使用<存储到代表LCD的平台设备中代码>platform_set_drvdata.然后在所有其他驱动程序函数的开始处取回为该设备分配的实例,以便它知道它正在处理哪个实例:

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);

to_our_lcd 扩展为 lcd_get_data,如果查看 include/linux/,它本身扩展为 dev_get_drvdata(platform_set_drvdata 的对应物)液晶.h.然后该函数可以知道设备的状态已被调用.

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天全站免登陆