Linux driver development - installation driver parameter transfer

1. Introduction

When developing in C language under Linux, parameters are often passed to the C program on the command line. Common Linux commands also need to pass parameters, so it is very flexible to use, and different effects can be performed according to different parameters.

The Linux driver installation also supports passing parameters, which is similar to the command running on the command line.

It's just that when writing a driver, the relevant information needs to be declared in advance in the driver code before it can be used.

This article will introduce how to pass parameters to the driver code when commanding to install the driver, and demonstrate various types of parameter transmission.

image-20220124165011934

In the driver code, declare the type, permission, and variable name of the incoming parameter.

module_param(变量的名称,类型,权限)

2. Declare the format of passing parameters in the driver code

/*传递整型类型数据*/
int int_data = 0;
module_param(int_data, int ,0664);
MODULE_PARM_DESC(int_data, "是一个整型的参数.");

/*传递指针类型数据*/
char *p_data = NULL;
module_param(p_data, charp, 0664);
MODULE_PARM_DESC(p_data, "是一个指针类型数据.");

/*
传递数组类型数据
module_param_array(数组名, 元素类型, 元素个数(取地址), 权限);
*/
int array_data[3] = {
    
    };
int num = 3;
module_param_array(array_data, int, &num, 0664);
MODULE_PARM_DESC(array_data, "是一个数组类型数据.");

/*
传递字符串: module_param_string
(传递参数时的字符串名称, 字符串名称, 字符串大小, 权限);
*/
char str_data[12] = {
    
    };
module_param_string(str_data, str_data, sizeof(str_data), 0664);
MODULE_PARM_DESC(str_data, "是一个字符串类型数据.");

3. Complete code example

#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-cfg.h>

/*传递整型类型数据*/
int int_data = 0;
module_param(int_data, int ,0664);
MODULE_PARM_DESC(int_data, "是一个整型的参数.");

/*传递指针类型数据*/
char *p_data = NULL;
module_param(p_data, charp, 0664);
MODULE_PARM_DESC(p_data, "是一个指针类型数据.");

/*
传递数组类型数据
module_param_array(数组名, 元素类型, 元素个数(取地址), 权限);
*/
int array_data[3] = {
    
    };
int num = 3;
module_param_array(array_data, int, &num, 0664);
MODULE_PARM_DESC(array_data, "是一个数组类型数据.");

/*
传递字符串: module_param_string
(传递参数时的字符串名称, 字符串名称, 字符串大小, 权限);
*/
char str_data[12] = {
    
    };
module_param_string(str_data, str_data, sizeof(str_data), 0664);
MODULE_PARM_DESC(str_data, "是一个字符串类型数据.");

static int __init tiny4412_param_dev_init(void) 
{
    
    
	printk("安装驱动成功.\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("卸载驱动成功.\n");
}

module_init(tiny4412_param_dev_init);
module_exit(tiny4412_param_dev_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("wbyq");

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:是一个字符串类型数据.
parm:           array_data:是一个数组类型数据.
parm:           p_data:是一个指针类型数据.
parm:           int_data:是一个整型的参数.

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] 安装驱动成功.
[ 2692.220000] int_data=666
[ 2692.220000] p_data=789
[ 2692.220000] array_data=6
[ 2692.220000] str_data=123

6. The driver is installed successfully. Check the passed parameters 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]#

7. Definition of Permissions

用户
#define S_IRWXU 00700 
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

用户组
#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

其他用户
#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001

image-20220124164854786

Sample code:

/*传递整型类型数据*/
int int_data = 0;
module_param(int_data, int ,S_IRUSR|S_IWUSR|S_IXUSR);
MODULE_PARM_DESC(int_data, "是一个整型的参数.");

/*传递指针类型数据*/
char *p_data = NULL;
module_param(p_data, charp, S_IRUSR|S_IWUSR|S_IXUSR);
MODULE_PARM_DESC(p_data, "是一个指针类型数据.");

Guess you like

Origin blog.csdn.net/xiaolong1126626497/article/details/123321960