linux 驱动机制之设备,驱动,总线

 

        话说linux driver 机制博大精深,但归根到底主要由:设备(device),驱动(driver),总线(bus)组成,它们三者相互关联,互相寻找,互相映射。

1,device 、driver、bus 之间的关系

        何为设备?linux主要由字符设备,块设备,网络设备等组成。如何驱动这些设备,那就需要相应的driver来控制设备或者硬件。那设备如何去寻找驱动呢?在这里我先给你透露一下,这种牵线搭桥的工作,主要交给了bus。就拿usb 来说吧,一个usb host controller 就对应一条bus;

 

 

上图大致描述三者之间相互依赖的关系,如要理清需从设备查找驱动的流程入下:

首先,当一个设备接入到系统中,系统会将注册相应的设备节点,并告知bus(bus_type);

然后,通过bus_type 中的match函数,来查找driver,并进行匹配的动作。

最后,bus正确查找到设备驱动。进而调用驱动的probe函数,来进行初始化工作。

 

注意!

驱动模块在内核中有两种模式,一种为编译到内核中,另一种为编译成ko。后者需将模块的ko加载到linux kernel 。

 

总之,device,driver,bus 这三架战车,统统有bus 来进行来管理。它主要提供设备匹配驱动,管理设备,管理驱动的功能。

 

2,重要的数据结构

2.1  bus_type

<p>/**
 * struct bus_type - The bus type of the device
 *
 * @name: The name of the bus.
 * @dev_name: Used for subsystems to enumerate devices like ("foo%u", dev->id).
 * @dev_root: Default device to use as the parent.
 * @bus_attrs: Default attributes of the bus.
 * @dev_attrs: Default attributes of the devices on the bus.
 * @drv_attrs: Default attributes of the device drivers on the bus.
 * @match: Called, perhaps multiple times, whenever a new device or driver
 *  is added for this bus. It should return a nonzero value if the
 *  given device can be handled by the given driver.
 * @uevent: Called when a device is added, removed, or a few other things
 *  that generate uevents to add the environment variables.
 * @probe: Called when a new device or driver add to this bus, and callback
 *  the specific driver's probe to initial the matched device.
 * @remove: Called when a device removed from this bus.
 * @shutdown: Called at shut-down time to quiesce the device.
 * @suspend: Called when a device on this bus wants to go to sleep mode.
 * @res
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 Linux驱动与模块 1.1 Linux的模块 驱动程序为了与外设硬件进行交互,必须调用Linux内核提供的内存管理、文件系统等 多种功能函数。而用户态的程序只能通过系统调用来使用有限的内核功能,因此驱动程序在 linux系统中被安排在内核态运行,也即可以访问所有的内核功能。 但是为了把内核通用功能与这些驱动程序区分开来,Linux 通常要求驱动程序作为一个 模块(module)在内核空间运行,并且由内核进行管理。当然,实际上模块并非只能用来编写 驱动程序,模块可以作为任何一个独立的功能块,操作内核函数并通过特定接口向应用程序 提供服务。 1.2 模块的编写要求 模块的代码中,必须加入以下头文件,才能正确地引用模块机制的若干函数: #include <linux/module.h> #include<linux/init.h> 当然,如果编写的模块是驱动程序,则还需要加入与驱动程序相关的内存管理、设备注 册、中断管理等头文件。 模块与通常的应用程序不同,并没有一个main函数,而是一些相互作用的函数的集合。 当然,这些集合中必须有一个首先调用的初始化函数,以及一个退出函数。 初始化函数:在模块加载时被调用,负责申请并初始化模块运行时必须的数据结构,此 后这些结构有可能在模块存续期间一直存在。 退出函数:在模块卸载时被调用,卸载后模块内任何数据结构都不该继续存在,因此退 出函数必须仔细的把模块运行带来的所有数据结构释放掉,否则将会造成内存泄露。 为了使内核知道模块中初始化函数和退出函数的函数名,必须用以下两个宏来定义: module_init(s3c_ts_init); module_exit(s3c_ts_exit); 上例中s3c_ts_init被定义为模块的初始化函数,s3c_ts_exit被定

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值