2大类型的设备驱动程序(2 main types of device driver)

在Linux 操作系统(实际上是任何操作系统都有着两个主要的device driver)中, 主要有2大device driver, 分别如下:

(1) character device driver(字符设备驱动) 特点如下:

character device driver writes and reads  character by character to the device  and from the device

      operrrating in a blocking mode whereby when the user writes information to the device,   the user must wait for the device (which is attached to your computer)to be finished being  written to before  continuing execution of the user code。 (该工作模式又称为synchronous operation(同步操作))

most common of all device driver。 



(2)block device driver(块设备驱动), 特点如下:

writing and reading block by block.

CPU intensive,  operations take long time to complete.  (可以称其工作在asynchronous mode operation,即用户不用等待设备完成读取或者写入就可以运行后面的程序)。

block device driver 具有cache(缓存)。


我们重点关注character device driver(字符设备驱动器)。


接下来, 我们概述一下Linux系统中构建一个device driver所需要的几大步。 参见下图:



首先, 第一步, 就是创建一个device File(设备文件)。 

打开Linux的terminal, 切换到/dev 目录中:

程序如下:

cd  /dev


所以我们进入到了dev 目录中了。

我们可以使用ls 查看其中所有设备:


下面我们查看一个device的信息:



上述信息解释如下:

crw的意思是character device, 可以read, 可以write。 这个device的major number为4(device driver为4), minor number为0.。  

接下来, 创建一个新的自己的device File(是与计算机连接的一个device的映射到Linux的文件), 命令如下:

mkmod  /dev/myTestDeviceFile c 900 1

解释: c 表示character device, b 代表block device。 900 表示major number, 1表示minor number。major number 必须是unique的, 即和你的kernel里面的所有的device driver都不同。  如果不满足唯一这个条件, 就会出现conflict的错误。 

要查看整个的device driver的major number, 地址如下:



点击Enter, 发现这个命令的执行必须是超级用户, 所以切换为超级用户:



解决办法如下:



这样查看, 可以看出, 我们成功的创建了我们的device File:


我们可以查看我们的这个device File的信息:


这个设备文件将与用户如何去access到kernel space中的真实的设备有关。 我们知道, user处于userspace中, 永远无法直接access kernel space(核空间),  所以我们唯一可以和device diver 交流的途径就是通过我们创建的这个device File。 user 可以open 这个device File, 可以打开, 可以读取其中的内蓉, 也可以写进去内容。 但是一旦我们执行这些操作后, 就会调用device driver, 然后我们的设备驱动程序就开始talk to the physical device。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值