Linux驱动学习笔记(1) hello world !

最简单的Linux模块,先用它来入个门,

1.看一下我们的环境是否正常;

2.简单的驱动模块,有那些东西构成!

 

/* * $Id: hellop.c,v 1.4 2004/09/26 07:02:43 gregkh Exp $ */ #include <linux/init.h> #include <linux/module.h> #include <linux/moduleparam.h> MODULE_LICENSE("Dual BSD/GPL"); /* * These lines, although not shown in the book, * are needed to make hello.c run properly even when * your kernel has version support enabled */ /* * A couple of parameters that can be passed in: how many times we say * hello, and to whom. */ static char *whom = "world"; static int howmany = 1; module_param(howmany, int, S_IRUGO); module_param(whom, charp, S_IRUGO); static int hello_init(void) { int i; for (i = 0; i < howmany; i++) printk(KERN_ALERT "(%d) Hello, %s\n", i, whom); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Goodbye, cruel world\n"); } module_init(hello_init); module_exit(hello_exit);   一个带参数版的 hello world!

root@ubuntu:/home/allen/share/ldd3/src/misc-modules/test# make
make -C /usr/src/linux-headers-2.6.38-8-generic/ M=http://www.likecs.com/home/allen/share/ldd3/src/misc-modules/test modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.38-8-generic\'
  CC [M]  /home/allen/share/ldd3/src/misc-modules/test/hello.o
  CC [M]  /home/allen/share/ldd3/src/misc-modules/test/hellop.o
  Building modules, stage 2.
  MODPOST 2 modules
  CC      /home/allen/share/ldd3/src/misc-modules/test/hello.mod.o
  LD [M]  /home/allen/share/ldd3/src/misc-modules/test/hello.ko
  CC      /home/allen/share/ldd3/src/misc-modules/test/hellop.mod.o
  LD [M]  /home/allen/share/ldd3/src/misc-modules/test/hellop.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.38-8-generic\'
root@ubuntu:/home/allen/share/ldd3/src/misc-modules/test# insmod hellop.ko  whom="mm" howmany=2
root@ubuntu:/home/allen/share/ldd3/src/misc-modules/test# rmmod hellop
root@ubuntu:/home/allen/share/ldd3/src/misc-modules/test# dmesg

。。。。。。。

[17277.821902] (0) Hello, mm
[17277.821908] (1) Hello, mm
[17288.977547] Goodbye, cruel world

注释:

1.关于 module_param(howmany, int, S_IRUGO);

module_param(参数名 参数类型 参数权限)

最后的 module_param 字段是一个权限值; 你应当使用 <linux/stat.h> 中定义的值. 这个值控制谁可
以存取这些模块参数在 sysfs 中的表示. 如果 perm 被设为 0, 就根本没有 sysfs 项. 否则, 它出现在 /sys/
module[5] 下面, 带有给定的权限. 使用 S_IRUGO 作为参数可以被所有人读取, 但是不能改变;
S_IRUGO|S_IWUSR 允许 root 来改变参数. 注意, 如果一个参数被 sysfs 修改, 你的模块看到的参数值
也改变了, 但是你的模块没有任何其他的通知. 你应当不要使模块参数可写, 除非你准备好检测这
个改变并且因而作出反应.
2.

/dev/下是设备节点号

/sys/module 是一个 sysfs 目录层次, 包含当前加载模块的信息. /proc/moudles 是旧式的, 那种信息的单个文件版本. 其中的条目包含了模块名, 每个模块占用的内存数量, 以及使用计数. 另外的字串追加到每行的末尾来指定标志, 对这个模块当前是活动的.
/proc 文件系统是一种特殊的、由程序创建的文件系统,内核使用它向外界输出信息。/proc 下面的每个文件都绑定于一个内核函数,这个函数在文件被读取时,动态地生成文件的"内容",例如,/proc/modules 列出的是当前载入模块的列表。这样可以动态访问其中进程和内核信息。

3.ls /sys/modules/hellop

holders  initstate  notes  parameters  refcnt  sections  srcversion

holders  持有人,是写本模块的人

initstate  记录模块活动
notes   暂且没有查到,好像是日记,有个隐藏文件,可能就是记录本模块的信息 *
parameters  使用的变量
refcnt   模块的加载计数
sections目录下

__param  __versions
其中sections目录中, __param是参数起始地址。
而sections目录中,__versions   好像和版本有关 *
这两个参数和地址有关。
srcversion    BDF6D850ED985425407E440     模块版本号 像模块的ID一样

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwyygg.html