1.sysfs操作的结构体
static struct sysfs_ops sysfs_ops_led{
.show = l_show,/*显示的函数实现*/
.store = l_store,/*存储的实现*/
};
2.创建/sys/class/pardevice/led/control/ 的kobject,目录下的每个文件对应于一个led灯
1)struct attibute *led_attrs[] = {
&led0.attr,
&led1.attr,
&led2.attr,
.....
NULL
};
struct kobj_type ktype_led = {
.sysfs_ops = &sysfs_ops_led,
.default_attrs = led_attrs,
};
3.在/sys/class/下创建pardevice class
led_class = class_create(THIS_MODULE, "pardevice");
4.在/sys/class/pardevice创建led class设备
c_d = class_device_create(led_class, NULL, dev_number, NULL, "led");
class_device_create的原型为:
struct class_device * class_device_create(struct class, struct class_device, struct dev_t, struct device, const char *fmt,...)
这样就在/sys/class/pardevice/led/
5.初始化kobject控制每个led
struct kobject kobj;
kobj.parent = &c_d->kobj;
strlcpy(kobj.name,"control",KOBJ_NAME_LEN);
kobj.ktype = &ktype_led;
kobject_register(&kobj);
此时在/sys/class/pardevice/led/control这个kobject下应该有ledX
6.销毁的时候顺序要倒过来
kobject_unregister----> class_device_destory----->class_destory
阅读(964) | 评论(0) | 转发(0) |