Linux设备模型
来源:互联网 发布:手机的编程游戏平台 编辑:程序博客网 时间:2024/06/07 02:32
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) modulesmodules_install:$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_installclean:rm -rf *.o *~ core .depend .*.cmd *.ko* *.mod.c .tmp_versions Module* module*.PHONY: modules modules_install cleanelse # called from kernel build system: just declare what our modules are obj-m := jiq.o endif
0 0
- Linux设备模型_platform设备
- linux设备模型详解
- linux设备模型详解
- Linux设备驱动模型
- linux的设备模型
- Linux设备模型
- [转]linux设备模型
- Linux的设备模型
- Linux设备模型
- Linux 设备模型
- Linux设备模型
- linux设备模型分析
- Linux 2.6 设备模型
- Linux设备模型 (1)
- Linux设备模型 (2)
- Linux设备模型 (3)
- Linux设备模型 (4)
- Linux设备模型
- hadoop SequenceFile 读取例程注释
- 简述WebService的使用
- java静态类声明--java类可以声明为static吗
- PLSQL_统计信息系列1_统计信息的概念和重要性(概念)
- 【数据结构基础】队列的链式存储结构
- Linux设备模型
- 任意数进制转换
- VMware虚拟机中安装Linux系统步骤(Redhat6.5)
- 红橙黄绿蓝靛紫-RGB-十六进制
- hdu 5135 Little Zu Chongzhi's Triangles(贪心)
- tomcat目录结构
- 扫描目录文件
- 关于css3 transition transform animation属性
- [android] 百度地图开发 (三).定位当前位置及getLastKnownLocation获取location总为空问题