Linux网卡驱动详解:总线、设备与驱动的核心架构

4星 · 超过85%的资源 需积分: 9 28 下载量 130 浏览量 更新于2024-08-01 1 收藏 101KB DOC 举报
Linux网卡驱动流程是操作系统内部实现硬件通信的关键环节,它涉及到Linux设备模型中的核心概念:总线(bus)、设备(device)和驱动程序(driver)。这些概念在Linux内核中通过特定的数据结构来表示,包括`struct bus_type`、`struct device`和`struct device_driver`。 1. **总线、设备和驱动**: - **总线**: Linux设备模型中的总线充当处理器与外部设备之间的通信通道,如PCI、SCSI等,它是设备间通信的基础。每个总线都有一个`struct kset`,其中包含`drivers`和`devices`两个链表,分别代表支持该总线的驱动集合和挂载在总线上的设备列表。 - **设备**: 每个物理设备在Linux中由一个`struct device`实例表示,包含`bus`成员,指示设备连接的总线类型,以及`driver`成员,表示驱动程序。 - **驱动程序**: 驱动程序是实现设备功能的核心组件,通过`struct device_driver`定义。它包含`bus`字段指明驱动所属的总线,以及`devices`链表,记录该驱动支持的所有设备。 2. **绑定过程**: - 在系统启动时,内核会遍历所有类型的总线,为每个连接的设备创建`struct device`实例并将其添加到相应总线的`devices`链表中。同时,加载新的驱动程序时,会创建`struct device_driver`实例,并将其添加到对应总线的`drivers`链表上。 - 设备的`bus`和`driver`成员确保了设备与驱动的关联性,使得系统能够正确地识别和配置设备的功能。每当有新设备接入或驱动程序加载,系统都会自动更新这些关系,以保持设备模型的完整性和一致性。 理解Linux网卡驱动流程对于开发者来说至关重要,因为它涉及到了内核如何管理和控制硬件设备,确保数据传输的可靠性和性能。开发者需要熟悉这些结构和机制,以便编写兼容各种总线和设备的高效驱动程序。
2020-02-13 上传
1.基于Igh-master1.5.2的多种Ethercat IO 模块及伺服电机的操作例子 2.基于Ethercat-Ighmaster二次开发库简化你的开发流程 所有源码及文档托管在码云平台,附件中有代码仓库地址 3.含有IghMaster1.5.2最新版本代码,编译脚本及安装到 最新的代码托管在: https://gitee.com/wllw7176/MyEthercat-IGH-1.5.2.git EtherCAT 1.5.2 编译及使用说明 1.交叉编译源码 源码中有内核模块编译,指令定模块目录 根据实际情况修改build_ethercat-1.5.2.sh中以下几个变量: output_dir='output' #编译输出目录 module_install_dir='module_install' #内核模块安装目录 kernel_source_dir='/mnt/fs_ext/imx6/linux-3.0.35' #内核源码目录 lib_modules_kernel_promt='3.0.35-2666-gbdde708' #内核版本号 host=arm-fsl-linux-gnueabi #交叉编译链前缀 注意要先编译内核,然后编译Ethrecat因为Ethercat依赖于内核通用网卡模块 以root用户运行./build_ethercat-1.5.2.sh #编译具体根据提示选择y/n就可以了 ---------------------------------------------------------------------------------------------------------------------------------------------- 手动安装Ethercat到ARM开发板: 2.内核模块安装 2.1 将output文件下通lib_modules_kernel_promt='3.0.35-2666-gbdde708'(build_ethercat-1.5.2.sh 中定义)放入开发板/lib/modules下 2.2 开发板中执行"depmod"命令 3. output里面的其它文件放入开发板对应位置 4. 设置参数 修改/etc/sysconfig/ethercat MASTER0_DEVICE="e4:f3:f5:c6:41:b6" #与ethercat绑定的Mac地址 DEVICE_MODULES="generic" #通用网卡就填generic,其余支持网卡换成模块名字就行 5.添加udev规则 echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\" > /etc/udev/rules.d/99-EtherCAT.rules 6. 启动服务 /etc/init.d/ethercat restart 出现以下信息表明移植成功 Shutting down EtherCAT master 1.5.2 done Starting EtherCAT master 1.5.2 ec_generic: Binding socket to interface 3 (eth0). done 7. 应用层测试 root@linaro-ubuntu-desktop:~# ethercat Please specify a command! Usage: ethercat [OPTIONS] [ARGUMENTS] Commands (can be abbreviated): alias Write alias addresses. config Show slave configurations. cstruct Generate slave PDO information in C language. data Output binary domain process data. debug Set the master's debug level. domains Show configured domains. download Write an SDO entry to a slave. eoe Display Ethernet over EtherCAT statictics. foe_read Read a file from a slave via FoE. foe_write Store a file on a slave via FoE. graph Output the bus topology as a graph. master Show master and Ethernet device information. pdos List Sync managers, PDO assignment and mapping. reg_read Output a slave's register contents. reg_write Write data to a slave's registers. rescan Rescan the bus. sdos List SDO dictionaries. sii_read Output a slave's SII contents. sii_write Write SII contents to a slave. slaves Display slaves on the bus. soe_read Read an SoE IDN from a slave. soe_write Write an SoE IDN to a slave. states Request application-layer states. upload Read an SDO entry from a slave. version Show version information. xml Generate slave information XML. Global options: --master -m Comma separated list of masters to select, ranges are allowed. Examples: '1,3', '5-7,9', '-3'. Default: '-' (all). --force -f Force a command. --quiet -q Output less information. --verbose -v Output more information. --help -h Show this help. Numerical values can be specified either with decimal (no prefix), octal (prefix '0') or hexadecimal (prefix '0x') base. Call 'ethercat --help' for command-specific help. 以上内容为基本的使用,进一步使用要结合ethercat说明文档和电机说明。 ---------------------------------------------------------------------------------------------------------------------------------------------- 自动安装Ethercat到ARM开发板: 将output目录复制到开发板然后运行output目录下install_to_arm.sh脚本 执行6,7步骤测试自动安装是否成功 ---------------------------------------------------------------------------------------------------------------------------------------------- 问题汇总: (1)Starting EtherCAT master 1.5.2 EtherCAT ERROR: MAC address may not be empty. FATAL: Error inserting ec_master (/lib/modules/3.0.35-2666-gbdde708-gbdbf2583/mnt/hgfs/win_linux_share/linux/download/ethercat-1.5.2/module_install/master/ec_master.ko): Invalid argument failed Mac地址为空,将对应的mac地址MASTER0_DEVICE="e4:f3:f5:c6:41:b6" (2)编译Ethercat 一定要保证所用编译器与内核与硬件平台一致,否则会出现内核模块不能使用或者其它未知问题 (3)内核源码要先编译通过,否则Ethercat模块将不能编译通过,因为Ethercat模块依赖于内核本身驱动模块。