014_驱动模块传参数
一、驱动模块传参数
1、传单个参数
(1)位置:在源码目录下的 include/linux/moduleparam.h 中
(2)函数原型
/*** 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)参数
module_param(name, type, perm)
name:模块参数的名称
type: 模块参数的数据类型(支持int long short uint ulong ushort类型)
perm: 模块参数的访问权限(S_IRUSR参数表示所有文件所有者可读)
2、传递多个参数
(1)函数原型
/*** 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)参数
module_param_array(name, type, nump, perm)
name:模块参数的名称
type: 模块参数的数据类型(支持int long short uint ulong ushort类型)
nump:保存参数个数的地址,是一个地址
perm: 模块参数的访问权限(S_IRUSR参数表示所有文件所有者可读)
二、测试例程
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文件
#!/bin/bash
$(warning KERNELRELEASE = $(KERNELRELEASE))ifeq ($(KERNELRELEASE),)#内核的源码路径, ?= 条件赋值, uname -r 得到内核版本号
KERNELDIR ?= /home/mint/itop/linux_3.0# := 立即赋值, 得到当前的绝对路径
PWD := $(shell pwd)# -C 切换工作路径, $(MAKE) = make
modules:$(MAKE) -C $(KERNELDIR) M=$(PWD) modulesclean:rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*.PHONY: modules cleanelse# 生成模块obj-m := module_param.o endif
3、编译生成的驱动文件
4、加载测试
insmod module_param.ko module_arg1=10 module_arg2=20 int_array=11,12,13,14,15,16,17,18
5、查询加载后的结果
cat /sys/module/module_param/parameters/xxx
6、卸载驱动
rmmod module_para
三、参数perm权限
位置: 在源码目录下的 include/linux/stat.h 中#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 //文件所有者有全部权限
#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 //与文件所有者不同组的用户可执行#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
014_驱动模块传参数相关推荐
- 十三、linux 内核驱动模块传参数
在加载模块的时候,可以向它传参数. 单个参数: 内核模块可以通过module_param来传单个参数 – module_param(name,type,perm) – ...
- pytest接口测试之fixture传参数request
本文主要介绍了pytest接口测试之fixture传参数request的使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 前言 有的测试用例,需要依赖于某些特定的 ...
- Java中传参数--值传递和引用传递
** Java中传参数–值传递和引用传递 ** 在Java中,传参数分为值传递和引用传递. 在Java中的数据类型分为两大类:一类是引用类型,也叫类类型(除了String以外的所有复合数据类型,包括数 ...
- python中函数的参数:必传参数(位置参数)、默认值参数、参数组传参、关键字传参...
1.必传参数也叫做位置参数,因为必填,也必须对应位置 2.默认值参数如上图的word 3.参数组参数:传进去的是0个.或多个value的形式,,,和位置参数有点像,只传value值,但是没有限制个数 ...
- struts2从action向jsp传参数
struts2从action向jsp传参数: 1.在action类里面的成员变量域那里写上你要返回给jsp的变量和相应的get set方法(比如list).. 在execute方法里为list填充了 ...
- Struts文件上传包含修改文件上传参数,多文件上传
配置xml: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC& ...
- HTTPS 传参数(Query String)安全吗?
前言 HTTP 的网络请求是可以被 sniffer 监听到的,因其是明文,所以一览无余. HTTP 的网络请求也是可以被 sniffer 监听到的,因其是密文,所以不能被解密时是安全的. 如何不被意外 ...
- 【微信小程序】给绑定事件传参数
前言 调试基础库 2.12.0 开发者工具 1.03.2008270 给绑定事件传参数 使用公共属性data-*给绑定事件传参数 使用方式 声明事件: Page({tapName: function( ...
- C#多线程函数如何传参数和返回值
提起多线程,不得不提起 委托(delegates)这个概念. 我理解的委托就是 具有 同样参数和返回值 的函数的集合. 比如 public delegate void MyDelegate(int a ...
- sh执行文件 参数传递_sh 脚本执行sql文件传参数
一.前言 今天做数据删除,用的命令行输入参数,并且调用执行的sql文件,我采用了sed命令,进行替换. sh脚本如下 #! /bin/sh echo "Please enter the ba ...
最新文章
- tms570 can 接收大量数据_CAN通讯系列--CAN总线基础3
- php imagevue,Imagevue 2.1.4 正式注册版(PHP Flash相册源代码下载)
- google reader很有用的技巧 F+F11
- 易天教你如何保养SFP光模块
- UICollectionView详解
- 如何制作linux系统硬盘,教你制作Linux操作系统的Boot/Root盘
- imread函数_MATLAB图像处理:27:使用imtranslate函数平移图像
- Celery+Rabbitmq实现异步任务
- 当ORACLE归档日志满后如何正确删除归档日志
- IDEA下Springcloud框架搭建(一)之服务注册与发现
- android活动中的变量,在不同的活动中保持变量值Android Studio
- RUP---统一软件开发过程
- CSV 文件中的字段中的开头和结尾上,可能会存在空格或制表符,但是该如何处理呢?
- 网上购物系统功能业务逻辑导图_功能流程说明_OctShop
- YYKit框架使用学习之整理
- OpenCV3.4.1+VS2018 安装并配置详细教程
- windows7修复计算机在哪里找,Windows7系统修复方法大全
- UVa 11991 - Easy Problem from Rujia Liu?
- 计算机应用情话,2018最新版情话大全浪漫情话 好听感人的情话
- hardfault常见原因_STM32 出现 hardfault_handler 处理方法
热门文章
- Kafka生产者、消费者的消息可靠性方案实现
- 会议一体机_多媒体会议系统方案
- 支付宝 手机h5支付
- 【大数据】城市公交网络分析与可视化(二):获取公交行驶路径并绘制散点图
- python-提取特征 特征选择
- matlab清除坐标轴,matlab 使用技巧之设置坐标轴
- 频数直方图的步骤_绘制频数直方图步骤
- 牛客小白月赛2 J 美 【构造】
- 2020年(农历庚子鼠年)春联大全(收藏必备)
- 新浪邮箱下载的都是php,为什么我用新浪邮箱以及手机号码注册的支付宝能够登陆,但是用163邮箱的支付宝登陆数据库就会报错...