LINUX內核 DEVICE_ATTR與cat echo命令 直接讀寫調用Kernel測試方法【原】


sysfs接口函數的建立_DEVICE_ATTR  【轉】

出自:http://blog.csdn.net/manshq163com/article/details/7848714

2012-05-09 11:36:46|  分類:linux文件系統 |  標簽:device_attr  sysfs接口函數  |字號 訂閱

 
 

說道sysfs接口,就不得不提到函數宏 DEVICE_ATTR,原型是

#define DEVICE_ATTR(_name, _mode, _show, _store) \

struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

函數宏DEVICE_ATTR內封裝的是__ATTR(_name,_mode,_show,_stroe)方法

_show:表示的是讀方法,

_stroe表示的是寫方法。

 

當然_ATTR不是獨生子女,他還有一系列的姊妹__ATTR_RO宏只有讀方法,__ATTR_NULL等等

如對設備的使用        DEVICE_ATTR   

對驅動使用               DRIVER_ATTR

對總線使用               BUS_ATTR 

對類別 (class) 使用  CLASS_ATTR

這四個高級的宏來自於<include/linux/device.h> 

DEVICE_ATTR  宏聲明有四個參數,分別是名稱、權限位、讀函數、寫函數。其中讀函數和寫函數是讀寫功能函數的函數名。

如果你完成了DEVICE_ATTR函數宏的填充,下面就需要創建接口了

例如:

    static DEVICE_ATTR(polling, S_IRUGO | S_IWUSR, show_polling, set_polling);
    static struct attribute *dev_attrs[] = {
            &dev_attr_polling.attr,
            NULL,
    };

當你想要實現的接口名字是polling的時候,需要實現結構體struct attribute *dev_attrs[]

其中成員變量的名字必須是&dev_attr_polling.attr

然后再封裝

    static struct attribute_group dev_attr_grp = {
            .attrs = dev_attrs,
    };

在利用 sysfs_create_group(&pdev->dev.kobj, &dev_attr_grp);創建接口

       通過以上簡單的三個步驟,就可以在adb shell 終端查看到接口了。當我們將數據 echo 到接口中時,在上層實際上完成了一次 write 操作,對應到 kernel ,調用了驅動中的 “store”。同理,當我們cat 一個 接口時則會調用 “show” 。到這里,只是簡單的建立了 android 層到 kernel 的橋梁,真正實現對硬件操作的,還是在 "show" 和 "store" 中完成的。

 

       其實呢?!用個proc文件系統的就知道,這個就和proc中的write和read一樣的,以我的理解:proc有點老了,以后肯定會大量使用attribute,proc好比是Windows XP,attribute就像是Windows Seven
 
 
 
 
/****************************************下面是內核驅動例子  這樣就比較清晰了****************/
  1 #include <linux/io.h>
  2 #include <linux/gpio.h>
  3 #include <plat/gpio-cfg.h>
  4 #include <mach/regs-gpio.h>
  5 #include <linux/module.h>
  6 #include <linux/delay.h>
  7 #include <linux/platform_device.h>
  8 #include <linux/workqueue.h>
  9 
 10 //#include <mach/gpio-v8.h>
 11 
 12 /* Variables */
 13 static struct delayed_work vib_delayed_work;
 14 
 15 /* Definitions */
 16 #define VIB_TEST    0
 17 
 18 #define GPIO_VIBCTL        EXYNOS4_GPD0(0)
 19 
 20 #define CFG_VIB_CTRL    do {\
 21     gpio_request(GPIO_VIBCTL, "MOTOR_PWM");\
 22     s3c_gpio_cfgpin(GPIO_VIBCTL,S3C_GPIO_OUTPUT);\
 23     s3c_gpio_setpull(GPIO_VIBCTL,S3C_GPIO_PULL_NONE);\
 24 } while(0)
 25 
 26 #define VIB_START do {\
 27     gpio_set_value(GPIO_VIBCTL, 1);\
 28 } while(0)
 29 
 30 #define VIB_STOP do {\
 31     gpio_set_value(GPIO_VIBCTL, 0);\
 32 } while(0)
 33 
 34 #define RELEASE_VIB do {\
 35     gpio_free(GPIO_VIBCTL);\
 36 } while(0)
 37 
 38 #ifndef CONFIG_TC4_DVT
 39  #define USE_MOTOR_8997
 40 #else
 41  #undef USE_MOTOR_8997
 42 #endif
 43 
 44 #ifdef USE_MOTOR_8997
 45 extern void motor8997_on(int on);
 46 #endif
 47 
 48 /* Functions */
 49 static void vib_delayed_work_func(struct work_struct *work)
 50 {
 51 #ifdef USE_MOTOR_8997
 52     motor8997_on(0);
 53 #else
 54     VIB_STOP;
 55 #endif
 56 }
 57 
 58 int strtoint(const char *str,int len)
 59 {
 60     int result = 0;
 61     int i = 0;
 62     char c;
 63     while(i <= len-1)
 64     {   
 65         c = str[i++];
 66         if(c<'0' || c>'9')
 67             break;
 68         result = 10*result + c - '0';
 69     }
 70     return result;
 71 }
 72 
 73 static ssize_t vib_show(struct device *dev,
 74         struct device_attribute *attr, char *buf)
 75 {
 76     return printk("write a number in to vibrate\n");
 77 }
 78 
 79 static ssize_t vib_store(struct device *dev,
 80         struct device_attribute *attr,
 81         const char *buf, size_t count)
 82 {
 83     int retval;
 84     int value;
 85     value = strtoint(buf,count);
 86 #if VIB_TEST
 87     printk("count:%d, buf:%s",count,buf);
 88     printk("inv:%d ms\n",value);
 89 #endif
 90     retval = count;
 91     if(value)
 92     {
 93 #ifdef USE_MOTOR_8997
 94         motor8997_on(1);
 95 #else
 96         VIB_START;
 97 #endif
 98         schedule_delayed_work(&vib_delayed_work, msecs_to_jiffies(value));
 99     }
100     else
101     {
102 #ifdef USE_MOTOR_8997
103         motor8997_on(0);
104 #else
105         VIB_STOP;
106 #endif
107     }
108     return retval;
109 }
110 
111 /*
112  * sysfs: /sys/devices/platform/s5p-vib
113  * usage: echo ms > /sys/devices/platform/s5p-vib/vib
114  */
115 static DEVICE_ATTR(vib, S_IRUGO | S_IWUSR, vib_show, vib_store);
116 
117 static int __devinit s3c_vib_probe(struct platform_device *pdev)
118 {
119     int err;
120     err = device_create_file(&pdev->dev, &dev_attr_vib);
121     if (err)
122         goto error1;
123 
124     /* delayed work */
125     INIT_DELAYED_WORK(&vib_delayed_work, vib_delayed_work_func);
126 
127     CFG_VIB_CTRL;
128 
129 #if VIB_TEST
130 #ifdef USE_MOTOR_8997
131     motor8997_on(1);
132 #else
133     VIB_START;
134 #endif
135     schedule_delayed_work(&vib_delayed_work, msecs_to_jiffies(1000));
136 #endif
137     printk("vibrator probe success\n");
138 
139     return 0;
140 
141 error1:
142     return err;
143 }
144 
145 static int __devexit s3c_vib_remove(struct platform_device *dev)
146 {
147     RELEASE_VIB;
148     return 0;
149 }
150 
151 #ifdef CONFIG_PM
152 static int s3c_vib_suspend(struct platform_device *pdev, pm_message_t state)
153 {
154 #ifdef USE_MOTOR_8997
155     motor8997_on(0);
156 #else
157     VIB_STOP;
158 #endif
159     return 0;
160 }
161 
162 static int s3c_vib_resume(struct platform_device *pdev)
163 {
164     return 0;
165 }
166 #else
167 #define s3c_vib_suspend NULL
168 #define s3c_vib_resume  NULL
169 #endif
170 
171 static struct platform_driver s5p_vib_driver = {
172     .probe        = s3c_vib_probe,
173     .remove        = __devexit_p(s3c_vib_remove),
174     .suspend    = s3c_vib_suspend,
175     .resume        = s3c_vib_resume,
176     .driver        = {
177         .name    = "s5p-vib",
178         .owner    = THIS_MODULE,
179     },
180 };
181 
182 static char __initdata banner[] = "S5P VIBRATOR\n";
183 
184 static int __init s3c_vib_init(void)
185 {
186     printk(banner);
187     return platform_driver_register(&s5p_vib_driver);
188 }
189 
190 static void __exit s3c_vib_exit(void)
191 {
192     printk("%s()\n",__FUNCTION__);
193     platform_driver_unregister(&s5p_vib_driver);
194 }
195 
196 module_init(s3c_vib_init);
197 module_exit(s3c_vib_exit);

按照這個接口 我們可以操作寄存器,可以調試流驅動


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM