014_Driver Module Transfer Parameters

Posted by trevorturtle on Wed, 02 Oct 2019 13:55:16 +0200

I. Driver Module Transfer Parameters

1. Pass a single parameter

(1) Location: in include/linux/moduleparam.h under the source directory

(2) Function prototype

/**
 * module_param - typesafe helper for a module/cmdline parameter
 * @value: the variable to alter, and exposed parameter name.
 * @type: the type of the parameter
 * @perm: visibility in sysfs.
 *
 * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a
 * ".") the kernel commandline parameter.  Note that - is changed to _, so
 * the user can use "foo-bar=1" even for variable "foo_bar".
 *
 * @perm is 0 if the the variable is not to appear in sysfs, or 0444
 * for world-readable, 0644 for root-writable, etc.  Note that if it
 * is writable, you may need to use kparam_block_sysfs_write() around
 * accesses (esp. charp, which can be kfreed when it changes).
 *
 * The @type is simply pasted to refer to a param_ops_##type and a
 * param_check_##type: for convenience many standard types are provided but
 * you can create your own by defining those variables.
 *
 * Standard types are:
 *	byte, short, ushort, int, uint, long, ulong
 *	charp: a character pointer
 *	bool: a bool, values 0/1, y/n, Y/N.
 *	invbool: the above, only sense-reversed (N = true).
 */
#define module_param(name, type, perm)				\
	module_param_named(name, name, type, perm)

(3) Parameters

module_param(name, type, perm)

Name: the name of the module parameter
Type: Data type of module parameters (supporting int long short uint ulong ushort type)
perm: Access permissions for module parameters (S_IRUSR parameter means that all file owners are readable)

2. Passing multiple parameters

(1) Function prototype

/**
 * module_param_array - a parameter which is an array of some type
 * @name: the name of the array variable
 * @type: the type, as per module_param()
 * @nump: optional pointer filled in with the number written
 * @perm: visibility in sysfs
 *
 * Input and output are as comma-separated values.  Commas inside values
 * don't work properly (eg. an array of charp).
 *
 * ARRAY_SIZE(@name) is used to determine the number of elements in the
 * array, so the definition must be visible.
 */
#define module_param_array(name, type, nump, perm)		\
module_param_array_named(name, name, type, nump, perm)

(2) Parameters

module_param_array(name, type, nump, perm)

Name: the name of the module parameter
Type: Data type of module parameters (supporting int long short uint ulong ushort type)
nump: The address that holds the number of parameters is an address
perm: Access permissions for module parameters (S_IRUSR parameter means that all file owners are readable)

II. Test routines

1,module_param.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/stat.h>

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("SKYFALL");


static int module_arg1,module_arg2;
static int int_array[50];
static int int_num;

module_param(module_arg1,int,S_IRUSR);

module_param(module_arg2,int,S_IRUSR);

module_param_array(int_array,int,&int_num,S_IRUSR);


static int __init skyfall_init(void)
{
    int i;
    printk("%s,%d\n",__func__,__LINE__);
    
    printk(KERN_EMERG "module_arg1 is %d!\n",module_arg1);
    printk(KERN_EMERG "module_arg2 is %d!\n",module_arg2);
    
    for(i = 0; i < int_num; i++)
    {
        printk(KERN_EMERG "int_array[%d] is %d!\n",i,int_array[i]);
    }
        
    printk(KERN_EMERG "skyfall dirver enter!\n");
    
    return 0;
}

static void __exit skyfall_exit(void)
{
    
    printk("%s,%d\n",__func__,__LINE__);
    
    printk(KERN_EMERG "skyfall world exit!\n");

    return ;
}


module_init(skyfall_init);

module_exit(skyfall_exit);

2. Makefile file file

#!/bin/bash
$(warning KERNELRELEASE = $(KERNELRELEASE))

ifeq ($(KERNELRELEASE),)

#The source path of the kernel,?= conditional assignment, uname-r gets the version number of the kernel
KERNELDIR ?=  /home/mint/itop/linux_3.0

# := assign immediately to get the current absolute path
PWD := $(shell pwd)


# - C switch working path, $(MAKE) = make
modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
    rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*

.PHONY: modules clean

else
      # Generating module
       obj-m := module_param.o 
    
endif

3. Driver files generated by compilation

4. Loading test

insmod  module_param.ko module_arg1=10 module_arg2=20  int_array=11,12,13,14,15,16,17,18


5. Results of Query Loading

cat /sys/module/module_param/parameters/xxx

6. Unloading Drive

					    rmmod module_para

3. Parametric perm permissions

 position: In the source directory   include/linux/stat.h   in

#ifndef _LINUX_STAT_H
#define _LINUX_STAT_H

#ifdef __KERNEL__

#include <asm/stat.h>

#endif

#if defined(__KERNEL__) || !defined(__GLIBC__) || (__GLIBC__ < 2)

#define S_IFMT  00170000
#define S_IFSOCK 0140000
#define S_IFLNK	 0120000
#define S_IFREG  0100000
#define S_IFBLK  0060000
#define S_IFDIR  0040000
#define S_IFCHR  0020000
#define S_IFIFO  0010000
#define S_ISUID  0004000
#define S_ISGID  0002000
#define S_ISVTX  0001000

#define S_ISLNK(m)	(((m) & S_IFMT) == S_IFLNK)
#define S_ISREG(m)	(((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)
#define S_ISCHR(m)	(((m) & S_IFMT) == S_IFCHR)
#define S_ISBLK(m)	(((m) & S_IFMT) == S_IFBLK)
#define S_ISFIFO(m)	(((m) & S_IFMT) == S_IFIFO)
#define S_ISSOCK(m)	(((m) & S_IFMT) == S_IFSOCK)

#define S_IRWXU 00700// File owner has full permissions
#define S_IRUSR 00400// File Owner Readable
#define S_IWUSR 00200// File Owner Writable
#define S_IXUSR 00100// File Owner Executable

#define S_IRWXG 00070/ Users in the same group as the file owner have full privileges
#define S_IRGRP 00040// Readable by users in the same group as the file owner
#define S_IWGRP 00020// Writable by users in the same group as the file owner
#define S_IXGRP 00010// / Users in the same group as the file owner can execute

#define S_IRWXO 00007// Users in different groups from the file owner have full privileges
#define S_IROTH 00004// User Readable in Different Groups from File Owner
#define S_IWOTH 00002// Writable by users in different groups from the file owner
#define S_IXOTH 00001// / Users in different groups from the file owner can execute

#endif

#ifdef __KERNEL__
#define S_IRWXUGO	(S_IRWXU|S_IRWXG|S_IRWXO)
#define S_IALLUGO	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
#define S_IRUGO		(S_IRUSR|S_IRGRP|S_IROTH)
#define S_IWUGO		(S_IWUSR|S_IWGRP|S_IWOTH)
#define S_IXUGO		(S_IXUSR|S_IXGRP|S_IXOTH)

#define UTIME_NOW	((1l << 30) - 1l)
#define UTIME_OMIT	((1l << 30) - 2l)

#include <linux/types.h>
#include <linux/time.h>

struct kstat {
	u64		ino;
	dev_t		dev;
	umode_t		mode;
	unsigned int	nlink;
	uid_t		uid;
	gid_t		gid;
	dev_t		rdev;
	loff_t		size;
	struct timespec  atime;
	struct timespec	mtime;
	struct timespec	ctime;
	unsigned long	blksize;
	unsigned long long	blocks;
};

#endif

#endif

Topics: Linux Makefile shell