天天看點

linux驅動中如何向子產品傳遞參數方法

     大家好,今天主要和大家分享一下,如何向子產品傳遞參數?

     Linux kernel 提供了一個簡單的架構。利用module_param和module_param_arra來實作。

linux驅動中如何向子產品傳遞參數方法

第一:module_param(name, type, perm)函數

           name 既是使用者看到的參數名,又是子產品内接受參數的變量;

      type 表示參數的資料類型,是下列之一:byte, short, ushort, int, uint, long, ulong, charp, bool, invbool;

      perm 指定了在sysfs中相應檔案的通路權限。通路權限與linux檔案通路權限相同的方式管理,如0644,或使用stat.h中的宏如S_IRUGO表示。

            0    表示完全關閉在sysfs中相對應的項。

            #define S_IRUSR    00400 檔案所有者可讀

            #define S_IWUSR    00200 檔案所有者可寫

            #define S_IXUSR    00100 檔案所有者可執行

            #define S_IRGRP    00040 與檔案所有者同組的使用者可讀

            #define S_IWGRP    00020

            #define S_IXGRP    00010

            #define S_IROTH    00004 與檔案所有者不同組的使用者可讀

            #define S_IWOTH    00002

            #define S_IXOTH    00001

  這些宏不會聲明變量,是以在使用宏之前,必須聲明變量,典型地用法如下:

  static unsigned int int_var = 0;

  module_param(int_var, uint, S_IRUGO);

       insmod xxxx.ko int_var=x

第二:實作多參數的傳遞給子產品

     傳遞多個參數可以通過宏 module_param_array(para , type , &n_para , perm) 實作。

第三:源檔案代碼:info_test.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

static char *name = "Ocean";
static int count = 2;
static int para[8] = {1,2,3,4};
static int n_para = 1;
module_param(count, int, S_IRUGO);
module_param(name, charp, S_IRUGO);
module_param_array(para , int , &n_para , S_IRUGO);

static struct file_operations first_drv_fops={
    .owner = THIS_MODULE,
    .open = first_drv_open,
    .write = first_drv_write,
};

int first_drv_init(void)
{
    printk("init first_drv drv!\n");

    int i;
    for (i = 0; i < count; i++)
        printk(KERN_ALERT "(%d) Hello, %s !\n", i, name);


    for (i = 0; i < 8; i++)
        printk(KERN_ALERT "para[%d] : %d \n", i, para[i]);

    for(i = 0; i < n_para; i++)
        printk(KERN_ALERT "para[%d] : %d \n", i, para[i]);

    return 0;
}

void first_drv_exit(void)
{
    printk("exit first_drv drv!\n");
}   

module_init(first_drv_init);
module_exit(first_drv_exit);

MODULE_AUTHOR("Ocean Byond");
MODULE_DESCRIPTION("my first char driver");
MODULE_LICENSE("GPL");      

第四:實驗結果

繼續閱讀