Chinaunix首页 | 论坛 | 博客
  • 博客访问: 583082
  • 博文数量: 88
  • 博客积分: 4769
  • 博客等级: 中校
  • 技术积分: 989
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-24 02:31
文章分类

全部博文(88)

文章存档

2012年(4)

2011年(35)

2010年(49)

分类: LINUX

2011-04-11 23:14:55

来源于《Linux设备驱动开发详解》的一个带参数的模块,代码如下:

  1.   1 #include <linux/module.h>
  2.   2 #include <linux/kernel.h>
  3.   3 #include <linux/init.h>
  4.   4
  5.   5 MODULE_LICENSE ("GPL");
  6.   6
  7.   7 static char *book_name = "dissecting Linux Device Driver";
  8.   8 static int num = 4000;
  9.   9
  10.  10 static int __init book_init (void)
  11.  11 {
  12.  12     printk(KERN_INFO "book name: %s\n", book_name);
  13.  13     printk(KERN_INFO "book num: %d\n", num);
  14.  14
  15.  15     return 0;
  16.  16 }
  17.  17
  18.  18 static void __exit book_exit (void)
  19.  19 {
  20.  20     printk (KERN_INFO "book module exit\n");
  21.  21 }
  22.  22
  23.  23 module_init (book_init);
  24.  24 module_exit (book_exit);
  25.  25 module_param(num, int, S_IRUGO);
  26.  26 module_param(book_name, charp, S_IRUGO);
        在代码中,我们使用“module_param(参数名,参数类型,参数读/写权限)”为模块定义了一个参数。
        参数类型可以是byte, short, ushort, int, uint, long, ulong, charp(字符指针),bool或invbool(bool的反),在
模块被编译时,会将module_param中声明的类型与变量定义的类型尽心对比,判断是否一致。

    编译模块,当然这里使用的是Makefile的方式,此例中模块的文档名字为:hello_with_para.c, Makefile的
内容如下:

  1.   1 EXTRA_CFLAGS += $(DEBFLAGS)
  2.   2
  3.   3 ifeq ($(KERNELRELEASE),)
  4.   4
  5.   5 KERNELDIR ?= /home/moran/program/micro2440/kernel/linux-2.6.32.2
  6.   6
  7.   7 PWD := $(shell pwd)
  8.   8
  9.   9 modules:
  10.  10 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
  11.  11
  12.  12 modules_install:
  13.  13 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
  14.  14
  15.  15 clean:
  16.  16 rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions
  17.  17
  18.  18 .PHONY: modules modules_install clean
  19.  19
  20.  20 else
  21.  21 obj-m := hello_with_para.o
  22.  22 endif
       将编译出的hello_with_para.ko拷贝到NFS的目录下(我的开发板使用NFS的方式挂载主机上的rootfs),然
后在开发板上进行测试:
   
  1. #demsg -n7
  2. #insmod hello_with_para.ko
此时内核的输出如下:
  1. [root@FriendlyARM /root]# insmod hello_with_para.ko
  2. book name: dissecting Linux Device Driver
  3. book num: 4000
  4. [root@FriendlyARM /root]#
下面我们带参数来执行:
  1. # insmod hello_with_para.ko book_name='ubuntu_program' num=2000
此时内核的输入如下:
  1. book name: ubuntu_program
  2. book num: 2000
与我们输入的参数值一致。

    模块被加载后,在/sys/module/目录下将出现以此模块命名的目录。当“参数读/写权限”为0时,表示
此参数不存在sysfs文件系统下对应的文件节点,如果此模块存在“参数读/写权限”不为0的命令行参数,
在此目录下还将出现parameters目录:
  1. [root@FriendlyARM hello_with_para]# ls -l
  2. drwxr-xr-x 2 root root 0 Apr 11 23:43 holders
  3. -r--r--r-- 1 root root 4096 Apr 11 23:43 initstate
  4. drwxr-xr-x 2 root root 0 Apr 11 23:43 notes
  5. drwxr-xr-x 2 root root 0 Apr 11 23:43 parameters
  6. -r--r--r-- 1 root root 4096 Apr 11 23:43 refcnt
  7. drwxr-xr-x 2 root root 0 Apr 11 23:43 sections
  8. [root@FriendlyARM hello_with_para]#
里面包含有一系列以参数命名的文件节点:
  1. [root@FriendlyARM parameters]# ls -l
  2. -r--r--r-- 1 root root 4096 Apr 11 23:44 book_name
  3. -r--r--r-- 1 root root 4096 Apr 11 23:44 num
  4. [root@FriendlyARM parameters]#
而文件的内容呢,就是我们在加载模块时,输入的参数值:
  1. [root@FriendlyARM parameters]# cat book_name num
  2. ubuntu_program
  3. 2000
  4. [root@FriendlyARM parameters]#




阅读(2846) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~