How to pass parameters to a module, Linux kernel provides a simple framework.
1. Module_param (name, type, perm);
Name is both the parameter name that the user sees and the variable that accepts the parameter within the module;
Type represents the data type of the parameter, which is one of the following: Byte, short, ushort, int, uint, long, ulong, CHARP, BOOL, invbool;
PERM Specifies the access rights for the corresponding file in the SYSFS. Access rights are managed in the same way as Linux file access, such as 0644, or by using macros in stat.h such as S_irugo.
0 indicates that the corresponding item in the SYSFS is closed completely.
#define S_IRUSR 00400 file owner readable
#define S_IWUSR 00200 file owner writable
#define S_IXUSR 00100 File owner executable
#define S_IRGRP 00040 User-readable with the same group as the file owner
#define S_IWGRP 00020
#define S_IXGRP 00010
#define S_iroth 00004 User-readable with different groups of file owners
#define S_iwoth 00002
#define S_ixoth 00001
These macros do not declare variables, so you must declare variables before you use them, typically with the following usage:
static unsigned int int_var = 0;
Module_param (Int_var, uint, s_irugo);
Insmod Xxxx.ko int_var=x
2. Passing multiple parameters can be achieved via macro Module_param_array (para, type, &n_para, perm).
Where para is both the parameter name of the external module and the variable name inside the program, type is the data type, and perm is the access permission of the SYSFS. The pointer nump points to an integer whose value indicates how many parameters are stored in the array para.
Para: parameter array; The size of the array is the determining factor that determines how many parameters can be entered.
N_para: number of parameters; This variable is not really decisive, as long as the para array size is large enough, when inserting the module, the number of input parameters will change the value of N_para, eventually passing the number of array elements exists N_para.
The typical usage is as follows:
static int para[max_fish];
static int N_para;
Module_param_array (para, int, &n_para, s_irugo);
#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 intCount =2;Static intpara[8] = {1,2,3,4};Static intN_para =1; Module_param (count,int, S_irugo); Module_param (name, Charp, S_irugo); Module_param_array (Para,int, &N_para, S_irugo);Static structFile_operations first_drv_fops={. Owner=this_module,. Open=First_drv_open,. Write=First_drv_write,};intFirst_drv_init (void) {PRINTK ("Init first_drv drv!\n"); inti; 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;}voidFirst_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");
The array size is 8, but only 4 parameters are passed, n_para=4.
The array size is 8, but only 8 parameters are passed, n_para=8.
The array size is 8, 9 parameters are passed in, and the array is not large enough to pass in.
Linux drivers: How to pass parameters to modules, Module_param and Module_param_array