Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1644903
  • 博文数量: 511
  • 博客积分: 967
  • 博客等级: 准尉
  • 技术积分: 2560
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-06 14:19
文章分类

全部博文(511)

文章存档

2016年(11)

2015年(61)

2014年(257)

2013年(63)

2012年(119)

分类: LINUX

2012-10-23 11:33:51

 
分析过mdev(udev的BusyBox简化版)源码的都知道mdev的基本原理:
a、执行mdev -s命令时,mdev扫描/sys/block(块设备保存在/sys/block目录下,内核2.6.25版本以后,块设备也保存在/sys/class/block目录下。mdev扫描/sys/block是为了实现向后兼容)和/sys/class两个目录下的dev属性文件,从该dev属性文件中获取到设备编号(dev属性文件以"major:minor\n"形式保存设备编号),并以包含该dev属性文件的目录名称作为设备名device_name(即包含dev属性文件的目录称为device_name,而/sys/class和device_name之间的那部分目录称为subsystem。也就是每个dev属性文件所在的路径都可表示为/sys/class/subsystem/device_name/dev),在/dev目录下创建相应的设备文件。例如,cat /sys/class/tty/tty0/dev会得到4:0,subsystem为tty,device_name为tty0。
b、当mdev因uevnet事件(以前叫hotplug事件)被调用时,mdev通过由uevent事件传递给它的环境变量获取到:引起该uevent事件的设备action及该设备所在的路径device path。然后判断引起该uevent事件的action是什么。若该action是add,即有新设备加入到系统中,不管该设备是虚拟设备还是实际物理设备,mdev都会通过device path路径下的dev属性文件获取到设备编号,然后以device path路径最后一个目录(即包含该dev属性文件的目录)作为设备名,在/dev目录下创建相应的设备文件。若该action是remote,即设备已从系统中移除,则删除/dev目录下以device path路径最后一个目录名称作为文件名的设备文件。如果该action既不是add也不是remove,mdev则什么都不做。
由上面可知,如果我们想在设备加入到系统中或从系统中移除时,由mdev自动地创建和删除设备文件,那么就必须做到以下三点:1、在/sys/class的某一subsystem目录下,2、创建一个以设备名device_name作为名称的目录,3、并且在该device_name目录下还必须包含一个dev属性文件,该dev属性文件以"major:minor\n"形式输出设备编号。
思路有了,剩下的就是如何实现了。下面通过介绍linux设备驱动模型里两个重要的数据结构:class和class_device,顺带讲述如何架构mdev驱动程序,并给出2个例子:HelloWorld和udev_demo。
注意:下面讲到的结构体、函数以及例子都是针对linux-2.6.24.7版本内核,并不适合于所有版本的linux内核。各版本内核的结构体和函数原型可能有变。比如我在Ubuntu9.04下make一下第二个驱动例子,出现了好几个错误,后来发现Ubuntu9.04的内核linux-2.6.28里面关于linux设备驱动模型的数据结构有了很大改变,没有了class_device。所以当你跑下面两个例子时,如不是linux-2.6.24.7版本内核,那就要注意一下例子里使用数据结构与你内核里的数据结构是否一致。
1、class
一个类是一个设备的高层视图,它抽象掉了底层的实现细节。例如,在驱动层面时,你可能会见到SCSI磁盘或者ATA磁盘;但在类层面时,它们都是磁盘。类允许用户空间基于它们做什么来使用设备,而不是它们如何被连接或者它们如何工作。
class表示一类设备,所有class都属于class_subsys(class子系统),即出现在/sys/class目录下,除了块设备(可能出现在/sys/block/或/sys/class/block,上面讲过了)。
其实,class在/sys/class下生成的目录也就是上面提到subsystem。这样第1点就有了。


/* class结构体 */


struct class {
const char * name; /* class的名称 */
struct module * owner; /* 拥有该class的模块 */
struct kset subsys; /* 该class对应的子系统 */
struct list_head children; /* 该class的class_device列表 */
struct list_head devices;
struct list_head interfaces;
struct kset class_dirs;
struct semaphore sem; /* locks both the children and interfaces lists */
struct class_attribute * class_attrs; /* 该class的默认属性,以NULL结尾 */
struct class_device_attribute * class_dev_attrs; /* 添加到class的class_device所拥有的默认属性 */
struct device_attribute * dev_attrs;

/* 该函数提供在产生热插拔class_device事件时,添加环境变量的能力 */
int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
/* 该函数提供在产生热插拔device(物理设备)事件时,添加环境变量的能力 */
int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
/* 添加到class的class_device移除时,调用该函数进行必要的清理工作 */
void (*release)(struct class_device *dev);
/* class被移除时,调用该函数进行必要的清理工作 */
void (*class_release)(struct class *class);
void (*dev_release)(struct device *dev);
int (*suspend)(struct device *, pm_message_t state);
int (*resume)(struct device *);
};
/* class注册函数 */
int __must_check class_register(struct class *);
void class_unregister(struct class *);



建立一个class有两种方法
a、根据需要,填充一个struct class,然后再调用class_register注册该class就ok了(此法比较灵活,可以自己定制很多东西)
b、就是通过下面的class_create来创建一个class,该函数会返回一个指向刚建立的class的指针(创建class的最简单方法)


/* class_create用于创建一个名为name的class,其owner参数一般为THIS_MODULE。class_create内部调用了class_register */
struct class *class_create(struct module *owner, const char *name);
/* class_destroy用于删除一个class,实际上其内部只是简单调用了class_unregister(cls)来注销cls */
void class_destroy(struct class *cls);



一个class属性对应于/sys/class/class.name(class.name就是该class的名称)目录里的一个文件。通过这些文件,可以向用户空间输出一些关于该class的信息,也可从用户空间获取到一些信息。


/* class属性结构体 */
struct class_attribute {
struct attribute attr;
/* 当用户空间读取该属性时,调用show函数输出一个"属性值"给用户空间 */
ssize_t (*show)(struct class *, char * buf);
/* 当用户空间写该属性时,调用store函数保存用户写入的"属性值" */
ssize_t (*store)(struct class *, const char * buf, size_t count);
};
struct attribute {
const char * name;
struct module * owner;
mode_t mode;
};
/* CLASS_ATTR可以在编译时创建一个class属性,该属性的名称为class_attr_name */
#define CLASS_ATTR(_name,_mode,_show,_store) \
struct class_attribute class_attr_##_name = __ATTR(_name,_mode,_show,_store)
/* class_create_file与class_remove_file用于创建与删除class默认属性外的属性 */
int __must_check class_create_file(struct class *,
const struct class_attribute *);
void class_remove_file(struct class *, const struct class_attribute *);


2、class_device
一个class可以看成是一个容器(一个子系统subsystem),包含了很多的class_device,这些class_device是由class这个大的容器来管理的,而每个class_device都对应着一个具体的设备。
每个class对象包括一个class_device链表,每个class_device对象表示一个逻辑设备并通过struct class_device中的dev成员(一个指向struct device的指针)关联一个物理设备。一个逻辑设备总是对应一个物理设备,而一个物理设备却可以对应多个逻辑设备。
实际上,class_device在/sys/class/subsystem生成的目录就是上面提到的class_device。这样第2点也有了。


/* class_device结构体 */


struct class_device {
struct list_head node; /* 仅供驱动核心内部使用 */
struct kobject kobj; /* 该class_device相应的kobject,仅供驱动核心内部使用 */
struct class * class; /* 该class_device所属的class,必须有 */
dev_t devt; /* 该class_device的设备编号,用于创建其dev属性文件,仅供驱动核心内部使用 */
struct device * dev; /* 指向该class_device相关的device结构体(物理设备),可选.若不为NULL,用于创建一个从class入口到/sys/devices下相应入口的符号连接,以便用户空间查找设备入口 */
void * class_data; /* 该class_device的私有数据指针 */
struct class_device *parent; /* parent of this child device, if there is one */
struct attribute_group ** groups; /* optional groups */
void (*release)(struct class_device *dev);
int (*uevent)(struct class_device *dev, struct kobj_uevent_env *env);
char class_id[BUS_ID_SIZE]; /* 该class_device的名称,在其所属class中应是唯一的,不可重名 */
};
/* class_devic注册函数 */
int __must_check class_device_register(struct class_device *);
void class_device_unregister(struct class_device *);



与class一样,建立一个class_device也有两种方法
a、根据需要,填充一个struct class_device,然后再调用class_device_register注册该class_device就ok了(此法比较灵活,可以自己定制很多东西)
b、就是通过下面的class_device_create来创建一个class_device,该函数会返回一个指向刚建立的class_device的指针(创建class_device的最简单方法)


/* class_device_create用于创建一个class_device,其名称最后两个参数决定(类似于printf的格式化字符串)
* cls指明了其所属的class,可以是自己填充的class或由class_create返回的class
* parent指明了该class_device的父class_device,若没有,则为NULL
* 该class_device的设备编号,用于创建其dev属性文件,必须指明
* device指明了该class_device(逻辑设备)对应的device(物理设备),可有可无,无则为NULL
* 实际上,class_device_create也就是填充一个class_device,然后调用了class_device_register注册该class_device
*/
struct class_device *class_device_create(struct class *cls,
struct class_device *parent,
dev_t devt,
struct device *device,
const char *fmt, ...)
__attribute__((format(printf,5,6)));
/* class_destroy用于删除一个class,内部调用了class_unregister(cls)来注销cls */
void class_device_destroy(struct class *cls, dev_t devt);



class_device属性对应于/sys/class/class.name/class_device.class_id目录下一个文件。通过这些文件,可以向用户空间输出一些关于该class_device的信息,也可从用户空间获取到一些信息。


/* class_device属性,其show和store函数类似于class属性的show和store函数 */
struct class_device_attribute {
struct attribute attr;
ssize_t (*show)(struct class_device *, char * buf);
ssize_t (*store)(struct class_device *, const char * buf, size_t count);
};
/* CLASS_DEVICE_ATTR可以在编译时创建一个class_device属性,该属性的名称为class_device_attr_name */
#define CLASS_DEVICE_ATTR(_name,_mode,_show,_store) \
struct class_device_attribute class_device_attr_##_name = \
__ATTR(_name,_mode,_show,_store)
/* class_device_create_file与class_device_remove_file用于创建与删除class_device默认属性外的属性 */
int __must_check class_device_create_file(struct class_device *,
const struct class_device_attribute *);
void class_device_remove_file(struct class_device * class_dev,
const struct class_device_attribute * attr);



其实,在调用class_device_register注册一个class_device时,该函数内部调用了
int class_device_add(struct class_device *class_dev)
在class_device_add内,通过class_device_create_file创建dev、uevent和该class_device所拥有的默认属性(由class_device.class->class_dev_attrs指定)等属性文件。这样第3点也有了。
dev属性文件用于向用户空间输出该class_device的设备编号。
uevent属性文件使用户可以手动触发uevent事件(通过向该文件写,如add、remove等字符串)。

第一个例子:HelloWorld。它是架构基于mdev(udev)的驱动最简单的方法,它在建立class和device_class时使用的是最简单的方法,即通过class_create和class_device_create。


#include
#include
#include
#include /* printk() */
#include /* everything... */
#include /* character devices */
#define DEV_NAME "hello"

/* hello's device structure */
struct hello_dev
{
char devname[20]; /* device name */
struct cdev cdev; /* character device structure */
struct class_device *clsdev; /* class_device structure */
};
static struct hello_dev hello_devices;
/* hello's file_operations */
struct file_operations hello_fops = {
.owner = THIS_MODULE,
};
int hello_init_module(void)
{
int retval;
dev_t devno;
struct class *hello_class;
devno = 0;
/* dynamic allocate major */
retval = alloc_chrdev_region(&devno, 0, 1, DEV_NAME);
if (retval < 0)
{/* failed to allocate major */
printk(KERN_WARNING "hello: can't get major %d\n", MAJOR(devno));
return retval;
}
/* Initialize and register cdev */
cdev_init(&hello_devices.cdev, &hello_fops);
hello_devices.cdev.owner = THIS_MODULE;
hello_devices.cdev.ops = &hello_fops;
retval = cdev_add(&hello_devices.cdev, devno, 1);
if (0 != retval)
{
printk(KERN_NOTICE "Error %d adding hello cdev", retval);
goto fail_add_cdev;
}
/* create class */
hello_class = class_create(THIS_MODULE, DEV_NAME);
if (IS_ERR(hello_class))
{
retval = PTR_ERR(hello_class);
goto fail_create_class;
}
/* create class_device */
hello_devices.clsdev = class_device_create(hello_class, NULL, devno, NULL, DEV_NAME);
if (IS_ERR(hello_devices.clsdev))
{
retval = PTR_ERR(hello_devices.clsdev);
goto fail_create_class_device;
}
printk("hello: Hello World!\n");
return 0; /* succeed */
fail_create_class_device:
class_destroy(hello_class);
fail_create_class:
cdev_del(&hello_devices.cdev);
fail_add_cdev:
unregister_chrdev_region(devno, 1);
return retval;
}
void hello_exit_module(void)
{
dev_t devno = hello_devices.clsdev->devt;
struct class *hello_class = hello_devices.clsdev->class;
class_device_destroy(hello_class, devno);
class_destroy(hello_class);
cdev_del(&hello_devices.cdev);
unregister_chrdev_region(devno, 1);
printk("hello: Goodbye World\n");
}
module_init(hello_init_module);
module_exit(hello_exit_module);
MODULE_AUTHOR("lingd");
MODULE_LICENSE("Dual BSD/GPL");


接下来是两个基于mdev的驱动例子。
struct class_interface {
struct list_head node;
struct class *class; /* 该class_interface所属的class */
int (*add) (struct class_device *, struct class_interface *); /* class属性 */
void (*remove) (struct class_device *, struct class_interface *); /* class属性 */
int (*add_dev) (struct device *, struct class_interface *);
void (*remove_dev) (struct device *, struct class_interface *);
};
/* class_interface注册函数 */
int __must_check class_interface_register(struct class_interface *);
void class_interface_unregister(struct class_interface *);
阅读(4258) | 评论(0) | 转发(0) |
0

上一篇:linux 属性文件

下一篇:Linux文件系统之sysfs

给主人留下些什么吧!~~