I suggest to use module parameters.
Include the #include <linux/moduleparam.h>
in kernel module.
use module_param()
variables and module_param_array()
for passing array to kernel modules.
Refer for how to pass values to module Passing an array as command line argument for linux kernel module
Here is a sample program for module_param()
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include "MyHeader.h"
int a = 0, b = 0, op = 0;
module_param(a, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
module_param(b, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
module_param(op, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
int __init init_module(void)
{
printk("\n Second Module Loaded...... \n");
if((a == 0) && (b == 0) && (op == 0))
{
printk("\n Supply Module Parameters...... \n");
printk("\n insmod <modulename>.ko op=<1-4> a=<int data> b=<int data> \n");
return 0;
}
switch(op)
{
case 1:
printk("\n Result of Addition:%d \n", add(a,b));
break;
case 2:
printk("\n Result of Subtraction:%d \n", sub(a,b));
break;
case 3:
printk("\n Result of Multiplication:%d \n", mul(a,b));
break;
case 4:
printk("\n Result of Division:%d \n", div(a,b));
break;
default:
printk("\n Unknown Operation... \n");
}
return 0;
}
void cleanup_module()
{
printk("\n Second Module UN--Loaded...... \n");
}
Another sample for passing array to a kernel module.
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
int a[5];
int count;
module_param_array(a, int, &count, 0);
int init_module(void)
{
int i = 0;
printk("\n Welcome to sample kernel module \n");
for(i = 0; i < count; i++)
{
printk("\na[%d] = %d", i, a[i]);
}
return 0;
}
void cleanup_module()
{
printk("\n Exit success \n");
}
Apart from module param, other alternatives also suggested in the following link.
http://kernelnewbies.org/FAQ/WhyWritingFilesFromKernelIsBad