代码学习inux内核驱动(八)

代码学习inux内核驱动(八)

sys文件系统

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/types.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/slab.h>
#include <linux/atomic.h>
#include <linux/spinlock.h>
#include <linux/irq.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/semaphore.h>
#include <linux/wait.h>
#include <linux/kthread.h>
#include <linux/kobject.h>
#include <linux/sysfs.h>

/******************************************************
sysfs 文件系统:
向用户空间展示内核数据结构、属性即他们之间的关系、其实基于ram的内存文件系统。
void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
int kobject_add(struct kobject *kobj, struct kobject *parent,const char *fmt, ...)
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,struct kobject *parent, const char *fmt, ...)

void kobject_del(struct kobject *kobj)
struct kobject *kobject_get(struct kobject *kobj)  //增加引用计数
void kobject_put(struct kobject *kobj) // 减少引用计数

struct kobject   // 对应一个目录
struct kobj_type
struct attribute  //对应一个文件

Kset 是具有相同类型的的Kobject的集合,

kset * kset_create_and_add(const char *name,const struct kset_uevent_ops *u,
    struct kobject *parent_kobj);
kset_register(&kset_c);  //注册时,kset_c.kobj_type不能为NULL
kset_unregister
系统配置发生变化,可以发送事件信息到用户态
struct kset_uevent_ops {
	int (* const filter)(struct kset *kset, struct kobject *kobj);  //决定是否将事件传递到用户空间,返回0不传递
	const char *(* const name)(struct kset *kset, struct kobject *kobj);  //用户将字符串传递给用户空间
	int (* const uevent)(struct kset *kset, struct kobject *kobj,         //将用户空间需要的参数添加到环境变量中
		      struct kobj_uevent_env *env);
};

kset_create_and_add
******************************************************/
MODULE_AUTHOR("xyzeng");
MODULE_LICENSE("Dual BSD/GPL");


void obj_test_release(struct kobject *kobject);
ssize_t kobj_test_show(struct kobject *kobject, struct attribute *attr,char *buf);
ssize_t kobj_test_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count);

struct kset  * kset_p;
struct kset  kset_c;

int kset_filter(struct kset *kset, struct kobject *kobj)
{
        printk("Filter: kobj %s.\n",kobj->name);
        return 1;
}
 
const char *kset_name(struct kset *kset, struct kobject *kobj)
{
        static char buf[20];
        printk("Name: kobj %s.\n",kobj->name);
        sprintf(buf,"%s","kset_name");
        return buf;
}
 
int kset_uevent(struct kset *kset, struct kobject *kobj,struct kobj_uevent_env *env)
{
        int i = 0;
        printk("uevent: kobj %s.\n",kobj->name);

        while( i < env->envp_idx){
                printk("%s.\n",env->envp[i]);
                i++;
        }

        return 0;
}

struct kset_uevent_ops uevent_ops = 
{
        .filter = kset_filter,
        .name   = kset_name,
        .uevent = kset_uevent,
};

struct attribute test_attr = {
        .name = "kobj_config",
        .mode = S_IRWXUGO,
};
 
static struct attribute *def_attrs[] = {
        &test_attr,
        NULL,
};
 
 
struct sysfs_ops obj_test_sysops =
{
        .show = kobj_test_show,
        .store = kobj_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("eric_test: release .\n");
}
 
ssize_t kobj_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 kobj_test_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count)
{
        printk("havestore\n");
        printk("write: %s\n",buf);
        return count;
}
 
struct kobject kobj;

static int code_case_sysfs_init(void)
{
    printk("[%s,%d] enter!\n",__FUNCTION__,__LINE__);

    printk("kset test init.\n");
    kset_p =  kset_create_and_add("kset_p",&uevent_ops,NULL);
    
    printk("kset test init 1.\n");

    kobject_set_name(&kset_c.kobj,"kset_c");
    printk("kset test init 3.\n");

    kset_c.kobj.kset = kset_p;
    kset_c.kobj.ktype = &ktype;
    kset_register(&kset_c);
    printk("kset test init 4.\n");

   // kobj.kset = &kset_p;
   // kobject_init_and_add(&kobj,&ktype,&kset_p->kobj,"kset_c");
     printk("kset test init 5.\n");
   
    return 0;
}
static void code_case_sysfs_exit(void)
{
     printk("[%s,%d] enter!\n",__FUNCTION__,__LINE__); 
    // kobject_del(&kobj);    
     kset_unregister(kset_p);
     kset_unregister(&kset_c);
}

module_init(code_case_sysfs_init);
module_exit(code_case_sysfs_exit);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值