好记性不如铅笔头

C && C++, kernel, 编程

驱动学习笔记:module_param

重要参考网址:【 http://blog.chinaunix.net/uid-10747583-id-2920908.html 】

CONTENTS

module_param的使用:

这里笔记下实例代码。

Makefile:

obj-m := helloparam.o
KERNEL_DIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
	make -C $(KERNEL_DIR) SUBDIRS=$(PWD) modules
clean:
	rm *.o *.ko *.mod.c
 
.PHONY:clean

helloparam.c:

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
 
MODULE_LICENSE("Dual BSD/GPL");

#define DEFAULT_BOOL_ARRAY_NUM 4
#define DEFAULT_STR_ARRAY_NUM 100

static int default_Num = 100;
static char *pDefault_Str = "[A]Hello World From Kernel";
static char default_Strs[DEFAULT_STR_ARRAY_NUM] = "[B]Hello World From Kernel";
static bool default_Bools[DEFAULT_BOOL_ARRAY_NUM] = {0}; 
static int boolArr_Num = DEFAULT_BOOL_ARRAY_NUM;

module_param(default_Num, int, S_IRUGO);
module_param(pDefault_Str, charp, S_IRUGO);
module_param_array(default_Bools,bool, &boolArr_Num, S_IRUGO);
module_param_string(default_Strs, default_Strs, DEFAULT_STR_ARRAY_NUM, S_IRUGO);


static int __init hello_init(void)
{
	int idx = 0;
	
    printk(KERN_ALERT "Hello world\n");   
    printk(KERN_ALERT "===============\n");
    printk(KERN_ALERT "default_Num:%d\n", default_Num);
    printk(KERN_ALERT "pDefault_Str:%s\n", pDefault_Str);
    printk(KERN_ALERT "default_Strs:%s\n", default_Strs);
    printk(KERN_ALERT "boolArr_Num:%d\n", boolArr_Num);
    for(idx = 0; idx<boolArr_Num; idx++)
    {
    	printk(KERN_ALERT "default_Bools[%d]:%d\n", idx, default_Bools[idx]);
    }

    printk(KERN_ALERT "===============\n");    
    return 0;
}
 
static void __exit hello_exit(void)
{
    printk(KERN_ALERT "Goodbye world\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

使用方式:

root@ubuntu:/home/cstriker1407/drivers/helloparam# make
make -C /lib/modules/3.13.0-24-generic/build SUBDIRS=/home/cstriker1407/drivers/helloparam modules
make[1]: 正在进入目录 `/usr/src/linux-headers-3.13.0-24-generic'
  CC [M]  /home/cstriker1407/drivers/helloparam/helloparam.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/cstriker1407/drivers/helloparam/helloparam.mod.o
  LD [M]  /home/cstriker1407/drivers/helloparam/helloparam.ko
make[1]:正在离开目录 `/usr/src/linux-headers-3.13.0-24-generic'
root@ubuntu:/home/cstriker1407/drivers/helloparam# insmod helloparam.ko default_Num=500 pDefault_Str=AA default_Strs=BB default_Bools=1,1,0 xx=yy
root@ubuntu:/home/cstriker1407/drivers/helloparam# rmmod helloparam
root@ubuntu:/home/cstriker1407/drivers/helloparam# insmod helloparam.ko
root@ubuntu:/home/cstriker1407/drivers/helloparam# rmmod helloparam

日志:

我们连续插入卸载模块两次,日志如下:

Oct 25 23:31:10 ubuntu kernel: [ 7153.176135] helloparam: unknown parameter 'xx' ignored
Oct 25 23:31:10 ubuntu kernel: [ 7153.177261] Hello world
Oct 25 23:31:10 ubuntu kernel: [ 7153.177266] ===============
Oct 25 23:31:10 ubuntu kernel: [ 7153.177268] default_Num:500
Oct 25 23:31:10 ubuntu kernel: [ 7153.177270] pDefault_Str:AA
Oct 25 23:31:10 ubuntu kernel: [ 7153.177271] default_Strs:BB
Oct 25 23:31:10 ubuntu kernel: [ 7153.177273] boolArr_Num:3
Oct 25 23:31:10 ubuntu kernel: [ 7153.177275] default_Bools[0]:1
Oct 25 23:31:10 ubuntu kernel: [ 7153.177276] default_Bools[1]:1
Oct 25 23:31:10 ubuntu kernel: [ 7153.177278] default_Bools[2]:0
Oct 25 23:31:10 ubuntu kernel: [ 7153.177279] ===============
Oct 25 23:31:27 ubuntu kernel: [ 7170.528498] Goodbye world
Oct 25 23:31:35 ubuntu kernel: [ 7178.189365] Hello world
Oct 25 23:31:35 ubuntu kernel: [ 7178.189376] ===============
Oct 25 23:31:35 ubuntu kernel: [ 7178.189381] default_Num:100
Oct 25 23:31:35 ubuntu kernel: [ 7178.189385] pDefault_Str:[A]Hello World From Kernel
Oct 25 23:31:35 ubuntu kernel: [ 7178.189389] default_Strs:[B]Hello World From Kernel
Oct 25 23:31:35 ubuntu kernel: [ 7178.189393] boolArr_Num:4
Oct 25 23:31:35 ubuntu kernel: [ 7178.189409] default_Bools[0]:0
Oct 25 23:31:35 ubuntu kernel: [ 7178.189413] default_Bools[1]:0
Oct 25 23:31:35 ubuntu kernel: [ 7178.189417] default_Bools[2]:0
Oct 25 23:31:35 ubuntu kernel: [ 7178.189420] default_Bools[3]:0
Oct 25 23:31:35 ubuntu kernel: [ 7178.189423] ===============
Oct 25 23:31:43 ubuntu kernel: [ 7185.667195] Goodbye world

 

发表评论

13 − 9 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据