Linux 驱动之模块参数--Linux设备驱动程序--http://blog.csdn.net/gatieme/article/details/51009094

This commit is contained in:
gatieme
2016-04-02 20:00:58 +08:00
parent 768bd2a1c4
commit eaa3f5710f
9 changed files with 1330 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
cmd_/home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/param/param.ko := ld -r -m elf_x86_64 -T ./scripts/module-common.lds --build-id -o /home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/param/param.ko /home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/param/param.o /home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/param/param.mod.o
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
/home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/param/param.ko
/home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/param/param.o
+14
View File
@@ -0,0 +1,14 @@
obj-m := param.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
all:
make -C $(KERNELDIR) M=$(PWD) modules
clean:
make -C $(KERNELDIR) M=$(PWD) clean
View File
+1
View File
@@ -0,0 +1 @@
kernel//home/gatieme/Work/GitHub/LDD-LinuxDeviceDrivers/study/param/param.ko
+110
View File
@@ -0,0 +1,110 @@
/*************************************************************************
> File Name: param.c
> Author: GatieMe
> Mail: gatieme@163.com
> Created Time: 2016年04月01日 星期五 21时37分37秒
************************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
/*
* 在模块里面, 声明一个变量(全局变量),
* 用来接收用户加载模块哦时传递的参数
*
* module_param(name, type, perm)
**/
static int value = 0;
module_param(value, int, 0644);
MODULE_PARM_DESC(value_int, "Get an value from user...\n");
/*
* 在模块内部变量的名字和加载模块时传递的参数名字不同
*
* module_param_named(name_out, name_in, type, perm)
*
* @name_out 加载模块时,参数的名字
* @name_in 模块内部变量的名字
* @type 参数类型
* @perm 访问权限
* */
static int value_in = 0;
module_param_named(value_out, value_in, int, 0644);
MODULE_PARM_DESC(value_in, "value_in named var_out...\n");
/*
* 加载模块的时候, 传递字符串到模块的一个全局字符数组里面
*
* module_param_string(name, string, len, perm)
*
* @name 在加载模块时,参数的名字
* @string 模块内部的字符数组的名字
* @len 模块内部的字符数组的大小
* #perm 访问权限
*
* */
static char *string = NULL;
module_param(string, charp, 0644);
MODULE_PARM_DESC(string, "Get an string(char *) value from user...\n");
static char buffer[20] = "gatieme";
module_param_string(buffer, buffer, sizeof(buffer), 0644);
MODULE_PARM_DESC(value_charp, "Get an string buffer from user...\n");
/*
* 加载模块的时候, 传递参数到模块的数组中
*
* module_param_array(name, type, num_point, perm)
*
* @name 模块的数组名,也是外部制定的数组名
* @type 模块数组的数据类型
* @num_point 用来获取用户在加载模块时传递的参数个数,
* 为NULL时,表示不关心用户传递的参数个数
* @perm 访问权限
*
* */
static int array[3];
int num;
module_param_array(array, int, &num, 0644);
MODULE_PARM_DESC(array, "Get an array from user...\n");
int __init param_module_init(void)
{
int index = 0;
printk("\n---------------------\n");
printk("value : %d\n", value);
printk("value_in : %d\n", value_in);
printk("string : %s\n", string);
printk("buffer : %s\n", buffer);
for(index = 0; index < num; index++)
{
printk("array[%2d] : %d\n", index, array[index]);
}
printk("---------------------\n");
return 0;
}
void __exit param_module_exit(void)
{
printk("\n---------------------\n");
printk("exit param dobule\n");
printk("---------------------\n");
}
module_init(param_module_init);
module_exit(param_module_exit);
+34
View File
@@ -0,0 +1,34 @@
#include <linux/module.h>
#include <linux/vermagic.h>
#include <linux/compiler.h>
MODULE_INFO(vermagic, VERMAGIC_STRING);
__visible struct module __this_module
__attribute__((section(".gnu.linkonce.this_module"))) = {
.name = KBUILD_MODNAME,
.init = init_module,
#ifdef CONFIG_MODULE_UNLOAD
.exit = cleanup_module,
#endif
.arch = MODULE_ARCH_INIT,
};
static const struct modversion_info ____versions[]
__used
__attribute__((section("__versions"))) = {
{ 0xa139692d, __VMLINUX_SYMBOL_STR(module_layout) },
{ 0x62a79a6c, __VMLINUX_SYMBOL_STR(param_ops_charp) },
{ 0xb71871f6, __VMLINUX_SYMBOL_STR(param_ops_string) },
{ 0x51eafc8e, __VMLINUX_SYMBOL_STR(param_ops_int) },
{ 0x1f54e93d, __VMLINUX_SYMBOL_STR(param_array_ops) },
{ 0x27e1a049, __VMLINUX_SYMBOL_STR(printk) },
};
static const char __module_depends[]
__used
__attribute__((section(".modinfo"))) =
"depends=";
MODULE_INFO(srcversion, "6E71C9CD204443CA3FC9684");