“This is the 17th day of my participation in the First Challenge 2022.

A list,

In Linux C language development, often in the command line to pass parameters to THE C program, common Linux commands also need to pass parameters, so that the use is very flexible, according to different parameters can perform different effects.

Linux driver installation also supports passing parameters, similar to commands run on the command line.

Only in the writing of the driver, the need to advance in the driver code will be related to the information declaration can be used.

This article shows you how to pass parameters to driver code when you command to install the driver, demonstrating the various types of parameter transfer.

Declare the type, permission, and name of the received variable of the passed parameter in the driver code.

Module_param (variable name, type, permission)Copy the code

Declare the format of the pass parameter in the driver code

/* Pass int data */ int int_data = 0; module_param(int_data, int ,0664); MODULE_PARM_DESC(int_data, "is an integer parameter."); /* pass pointer data */ char *p_data = NULL; module_param(p_data, charp, 0664); MODULE_PARM_DESC(p_data, "is a pointer type data."); /* Pass array type data module_param_array(array name, element type, number of elements (address), permission); */ int array_data[3] = {}; int num = 3; module_param_array(array_data, int, &num, 0664); MODULE_PARM_DESC(array_data, "is an array type of data."); /* Pass string: module_param_string (string name, string name, string size, permission); */ char str_data[12] = {}; module_param_string(str_data, str_data, sizeof(str_data), 0664); MODULE_PARM_DESC(str_data, "is a string of data.");Copy the code

Three, complete code examples

#include <linux/kernel.h> #include <linux/module.h> #include <linux/miscdevice.h> #include <linux/fs.h> #include <asm/uaccess.h> #include < Linux /gpio.h> #include < Mach /gpio.h> #include <plat/ gpio.h>  module_param(int_data, int ,0664); MODULE_PARM_DESC(int_data, "is an integer parameter."); /* pass pointer data */ char *p_data = NULL; module_param(p_data, charp, 0664); MODULE_PARM_DESC(p_data, "is a pointer type data."); /* Pass array type data module_param_array(array name, element type, number of elements (address), permission); */ int array_data[3] = {}; int num = 3; module_param_array(array_data, int, &num, 0664); MODULE_PARM_DESC(array_data, "is an array type of data."); /* Pass string: module_param_string (string name, string name, string size, permission); */ char str_data[12] = {}; module_param_string(str_data, str_data, sizeof(str_data), 0664); MODULE_PARM_DESC(str_data, "is a string of data."); Static int __init tiny4412_param_dev_init(void) {printk(" driver installed successfully.\n"); printk("int_data=%d\n",int_data); printk("p_data=%s\n",p_data); printk("array_data=%d\n",array_data[0]); printk("str_data=%s\n",str_data); return 0; } static void __exit tiny4412_param_dev_exit(void) {printk(" unmount driver successful.\ n"); } module_init(tiny4412_param_dev_init); module_exit(tiny4412_param_dev_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("wbyq");Copy the code

4. View the driver prompt information

[root@wbyq code]#modinfo led_drv.ko filename: led_drv.ko license: GPL author: wbyq depends: vermagic: 3.5.0-FriendlyARM SMP preEMpt mod_unload ARMv7 p2V8 PARm: str_data: string data Parm: array_data: array data Parm: string data P_data: a pointer parm: int_data: an integer.Copy the code

5. Pass parameters when installing the driver

[root@wbyq code]#insmod led_drv.ko str_data="123" int_data=666 p_data="789" array_data=6,7,8 [2692.220000] driver installed successfully. [ 2692.220000] int_data=666 [2692.220000] p_data=789 [2692.220000] array_data=6 [2692.220000] str_data=123Copy the code

6. The driver is successfully installed. Check the parameters passed in the sys directory

[root@wbyq code]#cd /sys/module/led_drv/parameters/
[root@wbyq parameters]#ls
array_data  int_data    p_data      str_data
[root@wbyq parameters]#cat array_data 
6,7,8
[root@wbyq parameters]#cat int_data 
666
[root@wbyq parameters]#cat p_data 
789
[root@wbyq parameters]#cat str_data 
123
[root@wbyq parameters]#
Copy the code

Definition of authority

User #define S_IRWXU 00700 #define S_IRUSR 00400 #define S_IWUSR 00200 #define S_IXUSR 00100 User group #define S_IRWXG 00070 Other users #define S_IRWXO 00007 #define S_IROTH 00004 #define S_IWOTH 00002 #define S_IXOTH 00001Copy the code

Sample code:

/* Pass int data */ int int_data = 0; module_param(int_data, int ,S_IRUSR|S_IWUSR|S_IXUSR); MODULE_PARM_DESC(int_data, "is an integer parameter."); /* pass pointer data */ char *p_data = NULL; module_param(p_data, charp, S_IRUSR|S_IWUSR|S_IXUSR); MODULE_PARM_DESC(p_data, "is a pointer type data.");Copy the code