2013-02-19 84 views
1

我創建了一個簡單的測試設備。我的意圖是創建一些自定義的sysfs文件並從中獲取設置。我首先嚐試添加一個kobject並使用我自己的sys_ops。這很好。然而,試圖通過使用平臺設備來做同樣的事情就是給我一個我無法讀取或寫入的文件。任何人都可以告訴我這裏有什麼問題嗎?在linux內核驅動程序中添加屬性到平臺設備

============= 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"); 

======================================= ============================================= 輸出是:

 
[email protected]:ko_training$ cd /sys/devices/platform/Aerrow_Test_Platform.0/ 
[email protected]:Aerrow_Test_Platform.0$ ls 
custom_attrbute driver modalias power subsystem uevent 
[email protected]:Aerrow_Test_Platform.0$ cat custom_attrbute 
cat: custom_attrbute: Input/output error 
[email protected]:Aerrow_Test_Platform.0$ echo "HELLO" > custom_attrbute 
bash: echo: write error: Input/output error 
[email protected]:Aerrow_Test_Platform.0$ cat custom_attrbute cat: custom_attrbute: Input/output error 

平臺設備的ktype ops方法無法處理其他屬性或者我的代碼有問題嗎?如果我創建自己的kobject並將該屬性放在該屬性下,同樣的方法也可以工作。 「

+0

http://kernel.org/doc/Documentation/filesystems/sysfs.txt將提供一些提示。你需要實現struct sysfs_ops – nos 2013-02-19 12:44:04

+0

如果我的驅動程序創建了父級,則可以添加sysfs_ops。但是,父Kobject/Kset是由平臺設備內核創建的,我試圖添加特定於我的設備的其他屬性。就像我已經提到的那樣,我可以輕鬆添加自己的kobject並使其工作,但我試圖查看是否可以在不需要明確的kobject的情況下創建該屬性。 – preetam 2013-02-19 13:26:28

回答

2

」裸屬性不包含讀取或寫入屬性值的方法,我們鼓勵子系統定義它們自己的屬性結構和包裝函數,以便爲特定對象類型添加和刪除屬性。 - 來自kernel.org/doc/Documentation/filesystems/sysfs.txt。

您可以嘗試device_attribute而不是此裸露屬性結構。還要定義你自己的show和store來讀寫你的sysfs條目。

相關問題