#include
#include
#include
#include
#include
#include
#include
#include
MODULE_AUTHOR("yangweihao");
MODULE_LICENSE("Dual BSD/GPL");
struct kobject *kobj;
struct kobject *ywhkobj;
char *ywhname;
void obj_test_release(struct kobject *kobject);
ssize_t ywh_test_show(struct kobject * kobject,struct attribute * attr,char * buf)
{
printk("have show .\n");
printk("attrname:%s .\n",attr->name);
sprintf(buf,"%s\n",attr->name);
return strlen(attr->name)+2;
}
ssize_t ywh_test_store(struct kobject * kobject,struct attribute * attr,char * buf,size_t count)
{
printk("havwstore\n");
kfree(ywhname);
ywhname = kmalloc(count,GFP_KERNEL);
if(!ywhname)
{
printk("kmalloc error\n");
return ENOMEM;
}
for(;*buf !='\0';buf++)
{
*(ywhname)++ = *buf;
}
*(ywhname) = '\0';
ywhname = ywhname - count;
attr->name = ywhname;
printk("write: %s\n",attr->name);
return count;
}
/*
默认属性添加
*/
struct attribute test_attr ={
.name = "yangweihao",
.mode = S_IRWXUGO,
};
static struct attribute *def_attrs[] ={
&test_attr,
NULL
};
struct sysfs_ops obj_test_sysops ={
.show = ywh_test_show,
.store = ywh_test_store
};
struct kobj_type ktype = {
.release = obj_test_release,
.sysfs_ops = &obj_test_sysops,
.default_attrs= def_attrs
};
void obj_test_release(struct kobject * kobject)
{
printk("kobject_test: release .\n");
}
static int kobject_test_init()
{
printk("kobject test init.\n");
kobj = kmalloc(sizeof(struct kobject),GFP_KERNEL);
ywhkobj= kmalloc(sizeof(struct kobject),GFP_KERNEL);
ywhname = kmalloc(0,GFP_KERNEL);
if(!kobj)
{
printk("kmalloc error\n");
return ENOMEM;
}
memset(kobj,0,sizeof(struct kobject));
memset(ywhkobj,0,sizeof(struct kobject));
kobject_init_and_add(kobj,&ktype,NULL,"ywh");
kobject_init_and_add(ywhkobj,&ktype,kobj,"hlp");
return 0;
}
static int kobject_test_exit()
{
printk("kobject test exit.\n");
kobject_del(kobj);
kfree(kobj);
kfree(ywhkobj);
kfree(ywhname);
return 0;
}
module_init(kobject_test_init);
module_exit(kobject_test_exit);
运行如下:
[root@FriendlyARM /sys]# ls
block class devices fs module
bus dev firmware kernel ywh
[root@FriendlyARM ywh]# ls
hlp yangweihao
[root@FriendlyARM hlp]# ls
yangweihao
不做讲述了....
阅读(775) | 评论(0) | 转发(0) |