linux kernel 字符设备详解

时间:2021-07-20 08:13:34

有关Linux kernel 字符设备分析:

参考:http://blog.jobbole.com/86531/

一.linux kernel 将设备分为3大类,字符设备,块设备,网络设备.

linux kernel 字符设备详解

字符设备是指只能一个字节一个字节读写的设备, 常见的外设基本上都是字符设备.

块设备:常见的存储设备,硬盘,SD卡都归为块设备,块设备是按一块一块读取的.

网络设备:linux 将对外通信的一个机制抽象成一个设备, 通过套接字对其进行相关的操作.

每一个字符设备或块设备都在/dev目录下对应一个设备文件。linux用户程序通过设备文件(或称设备节点)来使用驱动程序操作字符设备和块设备。

二、字符设备、字符设备驱动与用户空间访问该设备的程序三者之间的关系。

linux kernel 字符设备详解

三.字符设备的模型

linux kernel 字符设备详解

linux kernel 字符设备详解

四.下面讲一下字符设备驱动的编写流程,linux 内核为字符设备的创建提供了一套接口.

首先介绍一下dev_t , 他是主设备号和次设备号的结构体生成,他就代表了一个主次设备号

通过函数MKDEV (MAJ , MINOR) ; 生成.我们注册一个字符设备可以通过动态注册也可以静态注册 ,  linux kernel 为我们提供了所需要的接口

首先讲一下静态注册的方法

     //分配主设备号为200  次设备号从5开始分配5个  设备名字叫MONEY
int ret ;
DeviceId = MKDEV(MAJ , BASEMINOR);
ret = register_chrdev_region(DeviceId , MINORCNT , "MONEY");
if(ret < )
{
return ret ;
} //********************************************
//方法一
//1> 初始化
cdev_init(&device, &fops);
//2> 添加 domain->probes HASH表上
cdev_add(&device,DeviceId , MINORCNT);

通过函数register_chrdev_region() , 我们可以注册主设备号为MAJ , 次设备号BASEMINOR 开始 ,  一共注册MINORCNT 个次设备号 , 名字为MONEY 的字符设备.

第二种方法是动态申请主次设备号:

     //动态分配一个主设备号
int ret ;
ret = alloc_chrdev_region(&DeviceId , BASEMINOR , MINORCNT,"TONY");
if(ret < )
{
return ret ;
} printk("major:%d \n" , MAJOR(DeviceId)); //********************************************
//方法二
//1> 分配空间
device = cdev_alloc();

我们可以通过linux kernel 提供的alloc_chrdev_region () 的方法 , 申请一个主设备号和基础设备号 , 一共申请 MINORCNT , 名字为 TONY 的一个字符设备.

这里涉及一个结构体:

 struct cdev {
struct kobject kobj;
struct module *owner;
4 const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};

这里的话还要申请一个cdev 结构体的空间

通过cdev_alloc() ;

搞定了主次设备号的问题 , 接下来就是涉及到了初始化和添加到设备列表 .

linux kernel 为我们提供了以下的方法:

     //2> 初始化
cdev_init(device, &fops);
//3> 添加 domain->probes HASH表上
cdev_add(device,DeviceId , MINORCNT);

这里面涉及到了一个&fops 的文件操作结构体

 static struct file_operations  fops = {
.owner = THIS_MODULE,
.open = myopen,
.read = myread ,
.write = mywrite,
.release = myclose,
.unlocked_ioctl = myioctl,
};

上层的open read write 等函数经过一系列的转换都会到对应的函数

相对应的, 释放主次设备号, 删除在设备列表的节点, linux kernel 为我们提供了一下接口:

     cdev_del(device);

     unregister_chrdev_region(DeviceId , MINORCNT);

下面的代码是想深入了解里面的代码的看看就行了 , 如果只是向了解接口的 , 上面的就行了

从静态申请注册开始跟起吧

 #define MKDEV(ma,mi)    ((ma)<<8 | (mi))

上面这个是制作一个主次设备号的结构体

 extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
extern int register_chrdev_region(dev_t, unsigned, const char *);
extern int __register_chrdev(unsigned int major, unsigned int baseminor,
unsigned int count, const char *name,
const struct file_operations *fops);
extern void __unregister_chrdev(unsigned int major, unsigned int baseminor, unsigned int count, const char *name);
extern void unregister_chrdev_region(dev_t, unsigned);

这是几个将要用的函数的函数声明  ,  它在 include/linux/fs.h 文件中

首先看一下 register_chrdev_region() 函数

 /**
* register_chrdev_region() - register a range of device numbers
* @from: the first in the desired range of device numbers; must include
* the major number.
* @count: the number of consecutive device numbers required
* @name: the name of the device or driver.
*
* Return value is zero on success, a negative error code on failure.
*/

注释说明:  注册一个范围的设备号 , 原型如下:

 int register_chrdev_region(dev_t from, unsigned count, const char *name)
{
struct char_device_struct *cd;
dev_t to = from + count;
dev_t n, next; for (n = from; n < to; n = next) { next = MKDEV(MAJOR(n)+, );
if (next > to)
next = to;
cd = __register_chrdev_region(MAJOR(n), MINOR(n),
next - n, name);
if (IS_ERR(cd))
goto fail;
}
return ;
fail:
to = n;
for (n = from; n < to; n = next) {
next = MKDEV(MAJOR(n)+, );
kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
}
return PTR_ERR(cd);
}

它是调用了

      cd = __register_chrdev_region(MAJOR(n), MINOR(n),    

进里面看看

 /*
* Register a single major with a specified minor range.
*
* If major == 0 this functions will dynamically allocate a major and return
* its number.
*
* If major > 0 this function will attempt to reserve the passed range of
* minors and will return zero on success.
*
* Returns a -ve errno on failure.
*/

还是看注释: 注册一个指定的主设备号 和一个指定的次设备号范围

判断 主设备号是不是为零 , 如果是零的话 就动态申请一个主设备号 , 这就是后面要讲的那个动态申请 , 它也是调用了这个.

代码如下

 static struct char_device_struct *
__register_chrdev_region(unsigned int major, unsigned int baseminor,
int minorct, const char *name)
{
struct char_device_struct *cd, **cp;
int ret = ;
int i;

这里面涉及一个结构体没讲:
 static struct char_device_struct {
struct char_device_struct *next;
unsigned int major;
unsigned int baseminor;
int minorct;
char name[];
struct cdev *cdev; /* will die */
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
     cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);     //动态申请了一个char_device_struct 结构体
if (cd == NULL)
return ERR_PTR(-ENOMEM); mutex_lock(&chrdevs_lock); //加一个互斥锁 , 防止其他进程并发 /* temporary */
if (major == ) {
for (i = ARRAY_SIZE(chrdevs)-; i > ; i--) { //这里其实就是做了一个动态申请主设备号的功能
if (chrdevs[i] == NULL)
break;
} if (i == ) {      //没有申请到主设备号, 直接退出
ret = -EBUSY;
goto out;
}
major = i;
ret = major;
} cd->major = major;                  //对结构体进行初始化
cd->baseminor = baseminor;
cd->minorct = minorct;
strlcpy(cd->name, name, sizeof(cd->name)); i = major_to_index(major);      // 哈希表的下标生成 for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)    //进入chdevs[i] 哈希表快速进入
if ((*cp)->major > major ||
((*cp)->major == major &&
(((*cp)->baseminor >= baseminor) ||
((*cp)->baseminor + (*cp)->minorct > baseminor))))
break; /* Check for overlapping minor ranges. */      // 检查次设备号会不会重叠
if (*cp && (*cp)->major == major) {
int old_min = (*cp)->baseminor;
int old_max = (*cp)->baseminor + (*cp)->minorct - ;
int new_min = baseminor;
int new_max = baseminor + minorct - ; /* New driver overlaps from the left. */
if (new_max >= old_min && new_max <= old_max) {
ret = -EBUSY;
goto out;
} /* New driver overlaps from the right. */
if (new_min <= old_max && new_min >= old_min) {
ret = -EBUSY;
goto out;
}
} cd->next = *cp;
*cp = cd;
mutex_unlock(&chrdevs_lock);      //解锁
return cd;
out:
mutex_unlock(&chrdevs_lock);
kfree(cd);
return ERR_PTR(ret);
}

到这里一个主次设备号就搞定了 , 并申请一个char_device_struct 结构体, 并对其进行赋值初始化.

第二步就是对cdev  进行init :

 void cdev_init(struct cdev *, const struct file_operations *);    

这里又涉及到一个struct cdev 的结构体:

 struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};

进初始化代码一看究竟:

 /**
* cdev_init() - initialize a cdev structure
* @cdev: the structure to initialize
* @fops: the file_operations for this device
*
* Initializes @cdev, remembering @fops, making it ready to add to the
* system with cdev_add().
*/
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
memset(cdev, , sizeof *cdev);
INIT_LIST_HEAD(&cdev->list);
kobject_init(&cdev->kobj, &ktype_cdev_default);
cdev->ops = fops;
}

看一段代码之前我们尽可能的先看注释, 这样会让我们跟代码轻松很多 , 我们可以顺着代码的作者的思路走

注释: 初始化一个cdev  结构体

进kobject_init() 看看:

 /**
* kobject_init - initialize a kobject structure 初始化一个内核项目结构体
* @kobj: pointer to the kobject to initialize
* @ktype: pointer to the ktype for this kobject.
*
* This function will properly initialize a kobject such that it can then
* be passed to the kobject_add() call.
*
* After this function is called, the kobject MUST be cleaned up by a call
* to kobject_put(), not by a call to kfree directly to ensure that all of
* the memory is cleaned up properly.
*/
 void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
{
char *err_str; if (!kobj) {
err_str = "invalid kobject pointer!";
goto error;
}
if (!ktype) {
err_str = "must have a ktype to be initialized properly!\n";
goto error;
}
if (kobj->state_initialized) {
/* do not error out as sometimes we can recover */
printk(KERN_ERR "kobject (%p): tried to init an initialized "
"object, something is seriously wrong.\n", kobj);
dump_stack();
} kobject_init_internal(kobj);
kobj->ktype = ktype;
return; error:
printk(KERN_ERR "kobject (%p): %s\n", kobj, err_str);
dump_stack();
}
EXPORT_SYMBOL(kobject_init);
 struct kobject {
const char *name;
struct list_head entry;
struct kobject *parent;
struct kset *kset;
struct kobj_type *ktype;
struct sysfs_dirent *sd;
struct kref kref;
unsigned int state_initialized:;
unsigned int state_in_sysfs:;
unsigned int state_add_uevent_sent:;
unsigned int state_remove_uevent_sent:;
unsigned int uevent_suppress:;
};

下一部就是 cdev_add () ;

 int cdev_add(struct cdev *, dev_t, unsigned);