我创建了一个简单的测试设备。我的目的是创建一些自定义 sysfs 文件并从中获取设置。我首先尝试添加一个 kobject 和我自己的 sys_ops。效果很好。但是,尝试通过使用平台设备来做同样的事情会给我一个我无法读取或写入的文件。谁能告诉我这里有什么问题?
=============sysfs_file.c==================================
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/kobject.h>
#include <linux/slab.h>
struct kobject *myob;
char *ops_buffer;
struct attribute my_atr = {
.name = "custom_attrbute",
.mode = S_IWUGO|S_IRUGO,
};
int atp_probe(struct platform_device *dev)
{
printk("%s\n", __func__);
myob = &dev->dev.kobj;
ops_buffer = kzalloc(PAGE_SIZE, GFP_KERNEL);
sysfs_create_file(myob, &my_atr);
return 0;
}
int atp_remove(struct platform_device *dev)
{
printk("%s\n", __func__);
sysfs_remove_file(myob, &my_atr);
kfree(ops_buffer);
return 0;
}
struct platform_device atp_dev = {
.name = "Aerrow_Test_Platform",
.id = 0,
};
struct platform_driver atp_drv = {
.driver = {
.name = "Aerrow_Test_Platform",
.owner = THIS_MODULE,
},
.probe = atp_probe,
.remove = atp_remove,
};
static int __init sfst_init(void)
{
int ret;
printk("%s\n",__func__);
ret = platform_device_register(&atp_dev);
printk("%s: device add ret = %d\n", __func__, ret);
ret = platform_driver_register(&atp_drv);
printk("%s: driver register ret = %d\n", __func__, ret);
return 0;
}
static void __exit sfst_exit(void)
{
printk("%s", __func__);
platform_driver_unregister(&atp_drv);
platform_device_del(&atp_dev);
}
module_init(sfst_init);
module_exit(sfst_exit);
MODULE_LICENSE("GPL");
==================================================== =================================== 输出为:
preetam@preetam-Veriton-Series:ko_training$ cd /sys/devices/platform/Aerrow_Test_Platform.0/ preetam@preetam-Veriton-Series:Aerrow_Test_Platform.0$ ls custom_attrbute 驱动程序模式 电源子系统 uevent preetam@preetam-Veriton-Series:Aerrow_Test_Platform.0$ cat custom_attrbute cat:custom_attrbute:输入/输出错误 preetam@preetam-Veriton-Series:Aerrow_Test_Platform.0$ echo "HELLO" > custom_attrbute bash:回声:写入错误:输入/输出错误 preetam@preetam-Veriton-Series:Aerrow_Test_Platform.0$ cat custom_attrbute cat: custom_attrbute: 输入/输出错误
平台设备的 ktype ops 方法是无法处理其他属性还是我的代码有问题?如果我创建自己的 kobject 并将这个属性放在它下面,同样的事情也会起作用。