sys檔案的建立,有兩種方式,一種如例1自己在驅動任何需要的地方建立,是一種很靈活的方式;
還有一種(例2)是在驅動初始化的時候自動幫你建立,這種方式節省了精力,但是不如例1靈活。
##### 下面的例子都是基于linux3.10 #######
例1:
/*===============================================================
* Copyright (C) 2015 All rights reserved.
*
* File :sys_1.c
* Author:Gavin ([email protected])
* Date :2015-04-22
* Remark:基于linux3.10
*
================================================================*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/sysfs.h>
MODULE_LICENSE("GPL");
static struct kobject *sys_kobj;
/* 讀sys檔案,例如cat sys_test */
static size_t sys_test_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
return snprintf(buf, 10, "%s", "1234567890\n");
}
/* 寫入sys檔案,例如echo xxx sys_test*/
static size_t sys_test_store(struct kobject *kobj, struct kobj_attribute
*attr, const char *buf, size_t count)
{
return 0;
}
/* 定義建立sys檔案的屬性 */
static struct kobj_attribute sys_file_attribute =
__ATTR(sys_test, 0775, sys_test_show, sys_test_store);
static struct attribute *sys_test_attr[] = {
&sys_file_attribute.attr,
NULL,
};
/* 定義一個group */
static struct attribute_group sys_file_test_group = {
.name = "sys_file_test",
.attrs = sys_test_attr,
};
/* /fs/sysfs/group.c */
static int __init create_sys_file_init(void)
{
int ret;
sys_kobj = kobject_create_and_add("sys_test_group", NULL);
ret = sysfs_create_group(sys_kobj, &sys_file_test_group);
if(ret) {
printk(KERN_ERR "sysfs_create_group error\n");
return -1;
}
printk(KERN_ERR "sysfs_create_group ok\n");
sysfs_remove_group(sys_kobj, &sys_file_test_group);
printk( KERN_ERR "sysfs_remove_group\n");
return 0;
}
static void __exit create_sys_file_exit(void)
{
sysfs_remove_group(sys_kobj, &sys_file_test_group);
printk( KERN_ERR "sysfs_remove_group\n");
}
module_init(create_sys_file_init);
module_exit(create_sys_file_exit);
MODULE_AUTHOR("[email protected]");
MODULE_DESCRIPTION("create sys file");
例2:
/*===============================================================
* Copyright (C) 2015 All rights reserved.
*
* File :sys.c
* Author:Gavin ([email protected])
* Date :2015-04-22
* Remark:基于linux3.10
*
================================================================*/
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/platform_device.h>
MODULE_LICENSE("GPL");
/* 讀sys檔案,例如cat sys_test*/
static size_t sys_test_show(struct kobject *kobj, struct kobj_attribute
*attr, char *buf)
{
}
/* 寫入sys檔案,例如echo xxx > sys_test*/
static size_t sys_test_store(struct kobject *kobj, struct kobj_attribute
*attr, char *buf, size_t count)
{
}
/* 定義建立sys檔案的屬性 */
static struct kobj_attribute sys_file_attribute =
_ATTR(sys_test, 0775, sys_test_show, NULL);
static struct attribute *sys_test_attr[] = {
&sys_file_attribute.attr,
NULL,
};
/* 定義一個group */
static struct attribute_group sys_file_test_group = {
.name = "sys_file_test",
.attrs = sys_test_attr,
};
/* 可以将多個group組合在一起 */
static attribute_group *groups[] = {
&sys_file_test_group,
NULL,
};
/* device tree */
static struct of_device_id create_sys_dt_ids[] = {
{ .compatible = "gavin,create_sys_file_test", },
{},
};
/* sys檔案是基于此驅動初始化時而建立的 */
static struct platform_driver sys_test_driver = {
.driver = {
.name = "create_sys_test",
.owner = THIS_MODULE,
.group = groups,
/* 根據定義的資訊在device tree進行比對 */
.of_match_table = of_match_ptr(create_sys_dt_ids),
},
};
static int __init create_sys_file_init(void)
{
platform_driver_register(&create_sys_file_drv);
return 0;
}
static void _exit create_sys_file_exit(void)
{
platform_driver_unregister(&create_sys_file_drv);
}
module_init(create_sys_file_init);
module_exit(create_sys_file_exit);
MODULE_AUTHOR("[email protected]");
MODULE_DESCRIPTION("create sys file");