代码改变世界

Linux device files and device numbers

2011-08-06 23:44  RayLee  阅读(502)  评论(0编辑  收藏  举报

最近给系统添加了一个驱动,结果导致系统一些功能异常。查了好久,发现是device numbers冲突引起的。开始写驱动时,很自然的使用了动态分配device numbers(major=0 minor=0)的方法,而没有符合平台的特点。后来确认,平台中的device numbers都是预先分配的。

问题现象

使用系统函数open()打开device files总是成功,用ioctl()操作device总是失败,perror()返回的错误:“Inappropriate ioctl for device”。

原因分析

通过各种方式,查出device number冲突,这也解释了perror()为什么返回“Inappropriate ioctl for device”,ioctl期望操作的device跟实际的device是不符合的。

那device files和device numbers之间的关系是怎样的?

the files in /dev each have a major and minor device number associated with them. The kernel uses these numbers to map device-file references to the corresponding driver.

The major device number identifies the driver with which the file is associated. The minor device number usually identifies which particular instance of a given device type is to be addressed. The minor device number is sometimes called the unit number.