Linux设备模型<四>实例一winter_kobject

一想到明天有那么多个1,就有点。。看来今晚得努力啊。面对前面那么多的结构体别说看了,我写的时候都够呛的。这么多的理论咚咚总得找点东西把他们消化消化一下吧

前面我第一个讲的就是kobject,还记得它用来干嘛的不?它用来在sys下创建目录的。就这个。。。。

我想了很久这么winter_kobject.c该怎么写?具体说该怎么修改人家的代码吧 嘿嘿。我觉得这不叫抄袭,大家都学过C语言,这应该叫做函数的调用哈.或者说JAVA中的方法的

调用。。。。

还记得之前学习驱动的时候,那老师说过,好像Linux设备驱动当中都是模板化的,先写什么 再写什么。。。

我又想到了面向对象了。。。

1)第一步不用多做解释

/**************1*****************/
static int __init winter_kobject_init(void){
    
    printk("kboject winter_kobject init.\n");
    /**初始化和添加一个kobject对象**/
    /*int kobject_init_and_add(struct kobject *kobj,
    * struct kobj_type *ktype,
    * struct kobject *parent,
    * const char format...);当parent=NULL则kobject这个目录就在sys目录下*/
  kobject_init_and_add(&winter_kobj,&winter_ktype,NULL,"winter_kobject");
  return 0;
}

来看看它做了一些什么工作好了,这刚好是驱动模块加载的时候所调用的函数吧。。那么它要做什么工作呢,打印一些乱七八糟的东西,在就是初始化kobject了

初始化kobje后那当然是要向内核注册kobject了kobject_init_and_add(&winter_kobj,&winter_ktype,NULL,"winter_kobject")这个刚好帮我完成了上面的工作

2)根据加载模块中所需要的去做我们想要做的事

再看一下 kobject_init_and_add(&winter_kobj,&winter_ktype,NULL,"winter_kobject");它好像需要两个结构体哈,那么好我们就外绕着这两个结构体转

一个一个的去实现

&winter_kobj参数的实现就一句话。。。声明一下就可以<这里说一下我写代码的习惯 我喜欢从下往上写 需要什么我写什么 呵呵。。>

struct kobject winter_kobj;

3)接下来应该是实现&winter_ktype这个参数的时候,这个参数就是struct kobj_type *ktype。kobject的属性以及一些操作。。

/**************4***************先声明这样一个结构体吧**/
struct kobj_type winter_ktype={
    .release = kobjec_winter_release,
    .sysfs_ops=&kobjec_winter_sysops,
    .default_attrs=default_attrs,    
};

问题又来了看上面这个结构体中的kobjec_winter_release,&kobjec_winter_sysops,default_attrs这些东西又是要我们自己去实现的。<我选择的还是从下往上看>

4)实现default_attrs,实现之前我们应该值得default_attrs的原型 struct attribute **default_attrs;一看它就是个双指针吧,也就是一个指针数组咯

它的每一个指针都代表着kobject目录下的一个文件。下面这个结构体就代表着一个文件

/**************5*****************/
static struct attribute *default_attrs[] = {
    &winter_attribute,
    NULL,
};

道理同上我们又需要去实现winter_attribute的

/**************6*****************/
struct attribute winter_attribute = {
    .name = "winter_kobject",
    .mode = S_IRWXUGO,
};

说一下这个结构,该结构刚好就是上述那个文件的属性撒,包含这个文件的名字、权限等。。

5)接下来我们要去实现sysfs_ops这个结构指针了啵

/**************7*****************/
struct sysfs_ops kobjec_winter_sysops = {

    .show = kobject_winter_show,
    .store = kobject_winter_store,
};

先弄清楚这个结构指针是用来干嘛的,前面说过当用户空间去读写winter_kobject这个"文件"的时候就会调用结构里面show 和 sore

然后我们就要去实现show 和 sore这两个函数了

6)show 和 sore的实现

/**************8*****************/
ssize_t kobject_winter_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 kobject_winter_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count)
{
        printk("havestore\n");
        printk("write: %s\n",buf);
        return count;
}

7)最后就是实现release了 这里就打印了一句话当释放kobject的时候会被调用

void kobjec_winter_release(struct kobject *kobject)
{
        printk("eric_winter: release .\n");
}

/***完了 这个驱动程序就这样结束了,这样一分析至少我不再是那么的迷茫了,最后再看一下整体的代码。

#include <linux/device.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/sysfs.h>
#include <linux/stat.h>

/**************3*****************/
struct kobject winter_kobj;
/**************9*****************/
void kobjec_winter_release(struct kobject *kobject)
{
        printk("eric_winter: release .\n");
}
/**************8*****************/
ssize_t kobject_winter_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 kobject_winter_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count)
{
        printk("havestore\n");
        printk("write: %s\n",buf);
        return count;
}
/**************7*****************/
struct sysfs_ops kobjec_winter_sysops = {

    .show = kobject_winter_show,
    .store = kobject_winter_store,
};
/**************6*****************/
struct attribute winter_attribute = {
    .name = "winter_kobject",
    .mode = S_IRWXUGO,
};
/**************5*****************/
static struct attribute *default_attrs[] = {
    &winter_attribute,
    NULL,
};
/**************4*****************/
struct kobj_type winter_ktype={
    .release = kobjec_winter_release,
    .sysfs_ops=&kobjec_winter_sysops,
    .default_attrs=default_attrs,    
};
/**************1*****************/
static int __init winter_kobject_init(void){
    
    printk("kboject winter_kobject init.\n");
    /**初始化和添加一个kobject对象**/
    /*int kobject_init_and_add(struct kobject *kobj,
    * struct kobj_type *ktype,
    * struct kobject *parent,
    * const char format...);当parent=NULL则kobject这个目录就在sys目录下*/
  kobject_init_and_add(&winter_kobj,&winter_ktype,NULL,"winter_kobject");
  return 0;
}
/**************2*****************/
static int winter_kobject_exit()
{
    printk("kobject winter_kobject exit.\n");
    /**删除一个kobject对象**/
    kobject_del(&winter_kobj);
    return 0;
}

module_init(winter_kobject_init);
module_exit(winter_kobject_exit);

MODULE_AUTHOR("Eilian.Lau");
MODULE_LICENSE("Dual BSD/GPL");


驱动的测试就是看那些打印信息了,加载的时候 读文件的时候看有没有打印,如果和你的程序一致说明你成功了。。。。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值