8.类
8.1 class
l Class是对设备按照功能进行的分类,这样做是为了给用户空间提供一个比较友好的界面。Class也是一个kset,对应于/sys/class下的一个目录
int __init classes_init(void)
{
class_kset =kset_create_and_add("class", NULL, NULL);
if (!class_kset)
return -ENOMEM;
return 0;
}
l 在设备模型中类由class结构表示
struct class {
const char *name;
struct module *owner;
struct class_attribute *class_attrs;
struct device_attribute *dev_attrs;
struct bin_attribute *dev_bin_attrs;
struct kobject *dev_kobj;
int (*dev_uevent)(struct device *dev,struct kobj_uevent_env *env);
char *(*devnode)(struct device *dev,mode_t *mode);
void (*class_release)(struct class*class);
void (*dev_release)(struct device*dev);
const struct dev_pm_ops *pm;
struct subsys_private *p;
};
8.2 Class属性
l Class属性用class_attribute来表示
struct class_attribute {
struct attribute attr;
ssize_t (*show)(struct class *class,struct class_attribute *attr,
char *buf);
ssize_t (*store)(struct class *class,struct class_attribute *attr,
const char *buf,size_t count);
};
CLASS_ATTR(_name,_mode, _show, _store)
l 属性操作
int __must_checkclass_create_file(struct class *class,
const struct class_attribute *attr);
void class_remove_file(struct class *class,
const struct class_attribute *attr);
8.3 Class基本操作
l class注册和注销
int class_register(struct class *class);
void class_unregister(struct class *class);
l class创建和销毁
struct class * class_create(struct module *owner, const char *name)
void class_destroy(struct class *cls);
l 向类添加设备和删除设备
struct device * device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char*fmt, ...)
void device_destroy(struct class *class, dev_t devt)
devt:设备号
8.4 实例解析
#include
#include
#include
#include
static char * Version = "revision 1.0,scclass";
struct class * scclass;
EXPORT_SYMBOL_GPL(scclass);
static ssize_t class_show_version(structclass *class,
structclass_attribute *attr, char *buf)
{
return sprintf(buf, "%s\n", Version);
}
CLASS_ATTR(version, 0666,class_show_version, NULL);
static int __init scclass_init(void)
{
int err;
scclass= class_create(THIS_MODULE, "scclass");
err= PTR_ERR(scclass);
if(IS_ERR(scclass))
return err;
err= class_create_file(scclass, &class_attr_version);
if(err)
goto err_class_create;
printk("class create\n");
return 0;
err_class_create:
class_destroy(scclass);
return err;
}
static void __exit scclass_exit(void)
{
class_remove_file(scclass,&class_attr_version);
class_destroy(scclass);
printk("class destory\n");
}
module_init(scclass_init);
module_exit(scclass_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("CJOK");
上面代码只是创建一个类,我们还需要向类里添加设备,修改设备代码:
//@@ -8,6 +8,7 @@
extern struct bus_type
scbus_type
;
extern struct
device
scbus;
+extern struct class * scclass;
static char * Version = "revision 1.0, scdevice";
//@@ -45,6 +46,8 @@
ret = device_create_file(&scdevice, &dev_attr_version);
if (ret)
goto err_create;
+
+ device_create(scclass, NULL, 0, 0, "scdevice0");
printk("Create a scdevice");
return 0;
//@@ -56,6 +59,7 @@
static void __exit scdevice_exit(void)
{
+ device_destroy(scclass, 0);
device_remove_file(&scdevice, &dev_attr_version);
灰色为添加的代码
试验结果: