Linux设备模型

simple.c

#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>

MODULE_AUTHOR("haoyu");
MODULE_LICENSE("Dual BSD/GPL");

struct my_kobject
{
    int value;
    struct kobject kobj;
};
    
struct my_kobject my_kobj;

void kobject_release(struct kobject *kobject);
ssize_t kobject_attr_show(struct kobject *kobject, struct attribute *attr,char *buf);
ssize_t kobject_attr_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count);

struct attribute kobject_attr1 = {
    .name = "name",
    .mode = S_IRWXUGO,
};

struct attribute kobject_attr2 = {
    .name = "value",
    .mode = S_IRWXUGO,
};

static struct attribute *kobject_def_attrs[] = {
    &kobject_attr1,
    &kobject_attr2,
    NULL,
};

struct sysfs_ops kobject_sysfs_ops =
{
    .show = kobject_attr_show,
    .store = kobject_attr_store,
};

struct kobj_type ktype =
{
    .release = kobject_release,
    .sysfs_ops = &kobject_sysfs_ops,
    .default_attrs = kobject_def_attrs,
};

void kobject_release(struct kobject *kobject)
{
    printk("kobject release.\n");
}

ssize_t kobject_attr_show(struct kobject *kobject, struct attribute *attr,char *buf)
{
    int count = 0;
    struct my_kobject *my_kobj = container_of(kobject, struct my_kobject, kobj);
    printk("kobject attribute show.\n");
    if(strcmp(attr->name, "name") == 0)
        count = sprintf(buf, "%s\n", kobject->name);
    else if(strcmp(attr->name, "value") == 0)
        count = sprintf(buf, "%d\n", my_kobj->value);
    else
        printk("no this attribute.\n");
    
    return count;
}

ssize_t kobject_attr_store(struct kobject *kobject,struct attribute *attr,const char *buf, size_t count)
{
    int val;
    struct my_kobject *my_kobj = container_of(kobject, struct my_kobject, kobj);
    printk("kobject attribute store.\n");
    if(strcmp(attr->name, "name") == 0)
        printk("Can not change name.\n");
    else if(strcmp(attr->name, "value") == 0)
    {
        val = buf[0] - '0';
        if(val == 0 || val == 1)
            my_kobj->value = val;
        else
            printk("value is '0' or '1'\n");
    }
    else
        printk("no this attribute.\n");
        
    return count;
}

static int kobject_test_init(void)
{
    printk("kboject test init.\n");
    kobject_init_and_add(&my_kobj.kobj,&ktype,NULL,"kobject_test");
    return 0;
}

static void kobject_test_exit(void)
{
    printk("kobject test exit.\n");
    kobject_del(&my_kobj.kobj);
}

module_init(kobject_test_init);
module_exit(kobject_test_exit);

Makefile

# To build modules outside of the kernel tree, we run "make"
# in the kernel source tree; the Makefile these then includes this
# Makefile once again.
# This conditional selects whether we are being included from the
# kernel Makefile or not.
ifeq ($(KERNELRELEASE),)

    # Assume the source tree is where the running kernel was built
    # You should set KERNELDIR in the environment if it's elsewhere
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    # The current directory is passed to sub-makes as argument
    PWD := $(shell pwd)

modules:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

modules_install:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko* *.mod.c .tmp_versions Module* module*

.PHONY: modules modules_install clean

else
    # called from kernel build system: just declare what our modules are
    obj-m := jiq.o 
endif



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值