Chinaunix首页 | 论坛 | 博客
  • 博客访问: 402853
  • 博文数量: 62
  • 博客积分: 1483
  • 博客等级: 上尉
  • 技术积分: 779
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-24 12:25
文章分类

全部博文(62)

文章存档

2012年(2)

2011年(6)

2010年(6)

2009年(48)

我的朋友

分类: LINUX

2009-09-11 16:49:42

我的第一个字符驱动模块 from《linux设备驱动程序开发详解》
下载:http://blogimg.chinaunix.net/blog/upfile2/090911165142.tar 模块
http://blogimg.chinaunix.net/blog/upfile2/090912213619.tar 测试

测试如下:
#su
#make
#chmod +x gm.ko
#insmod gm.ko
找到这个字符模块的主设备号(major)
#cat /proc/devices

root@lzd-laptop:/sys/module/gm# cat /proc/devices
Character devices:
  1 mem
  2 pty
  3 ttyp
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  6 lp
  7 vcs
 10 misc
 13 input
 21 sg
 29 fb
 99 ppdev
116 alsa
128 ptm
136 pts
171 ieee1394
180 usb
189 usb_device
216 rfcomm
252 globalmem
253 pcmcia
254 hidraw

Block devices:
  1 ramdisk
  2 fd
259 blkext
  8 sd

可见在我的系统上是252
#mknod /dev/gm c 252 0

root@lzd-laptop:/home/lzd/modules_test# ls -l
total 280
-rw-r--r-- 1 lzd  lzd    4304 2010-09-11 16:12 gm.c
-rw-r--r-- 1 root root 124198 2010-09-11 16:23 gm.ko
-rw-r--r-- 1 root root    444 2010-09-11 16:23 gm.mod.c
-rw-r--r-- 1 root root  32920 2010-09-11 16:23 gm.mod.o
-rw-r--r-- 1 root root  92320 2010-09-11 16:23 gm.o
-rw-r--r-- 1 lzd  lzd     825 2010-09-11 15:39 Makefile
-rw-r--r-- 1 root root      0 2010-09-11 16:23 Module.markers
-rw-r--r-- 1 root root     36 2010-09-11 16:23 modules.order
-rw-r--r-- 1 root root      0 2010-09-11 16:23 Module.symvers
root@lzd-laptop:/home/lzd/modules_test#


#ls -l > /dev/gm
#cat /dev/gm
root@lzd-laptop:/home/lzd/modules_test# ls -l
total 280
-rw-r--r-- 1 lzd  lzd    4304 2010-09-11 16:12 gm.c
-rw-r--r-- 1 root root 124198 2010-09-11 16:23 gm.ko
-rw-r--r-- 1 root root    444 2010-09-11 16:23 gm.mod.c
-rw-r--r-- 1 root root  32920 2010-09-11 16:23 gm.mod.o
-rw-r--r-- 1 root root  92320 2010-09-11 16:23 gm.o
-rw-r--r-- 1 lzd  lzd     825 2010-09-11 15:39 Makefile
-rw-r--r-- 1 root root      0 2010-09-11 16:23 Module.markers
-rw-r--r-- 1 root root     36 2010-09-11 16:23 modules.order
-rw-r--r-- 1 root root      0 2010-09-11 16:23 Module.symvers
root@lzd-laptop:/home/lzd/modules_test#

验证了gm模块的正确。
root@lzd-laptop:/home/lzd/modules_test# dmesg |tail -4
[ 6534.240394] linux1x[12627]: segfault at 6e0a009c ip 08058963 sp b7d8e380 error 4 in linux1x[8048000+2e000]
[ 6615.140600] write 520 bytes from 0
[ 6619.772677] read 4096 bytes from 0
[ 6619.772784] read 0 bytes from 4096
root@lzd-laptop:/home/lzd/modules_test#

看看这个modules在sysfs里是什么样子
root@lzd-laptop:/sys/module/gm# cd /sys/module/gm/
root@lzd-laptop:/sys/module/gm# tree -a
.
|-- holders
|-- initstate
|-- notes
|   `-- .note.gnu.build-id
|-- parameters
|   `-- globalmem_major
|-- refcnt
`-- sections
    |-- .bss
    |-- .data
    |-- .gnu.linkonce.this_module
    |-- .note.gnu.build-id
    |-- .rodata
    |-- .rodata.str1.1
    |-- .strtab
    |-- .symtab
    |-- .text
    `-- __param

4 directories, 14 files
root@lzd-laptop:/sys/module/gm# cat refcnt
0
root@lzd-laptop:/sys/module/gm# lsmod |grep gm
gm                      3076  0
root@lzd-laptop:/sys/module/gm#
可见没有程序用这个模块,就这样了。





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