Link:http://www.freesoftwaremagazine.com/articles/drivers_linux
1267111075_Writing device drivers in Linux- A brief tutorial.pdf

위의 소스대로 하면 제대로 컴파일 되지 않습니다.
아마도 2.4 대와 2.6대가 모듈 빌드하는 법이 좀 다른 것 같습니다.
root@ubuntu:~/works/driver# make -C /usr/src/kernel-source-2.6.31/ M=pwd modulesmake: Entering directory `/usr/src/kernel-source-2.6.31'make: *** No rule to make target `modules'. Stop.make: Leaving directory `/usr/src/kernel-source-2.6.31'
이렇게만 나오고 아무 파일도 안생기는군요.
그래서 찾은 것이 아래 사이트입니다.
(여기는 스크랩 가능한 위치입니다:http://blog.paran.com/coacoma/5011889)
<hello.c>
#include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h>int hellow_init(void){
printk("<1>Hello, world\n");return 0;
}void hellow_exit(void){
printk("<1>Goodbye cruel Worled\n");
}module_init(hellow_init);module_exit(hellow_exit);
</hello.c>
<Makefile>
# Makefileobj-m += hello.o
</Makefile>
이 파일을 다음과 같이 실행하면 hello.ko 의 모듈 파일이 생깁니다.
root@ubuntu:~/works/driver# make -C /lib/modules/`uname -r`/build M=`pwd` modulesmake: Entering directory `/usr/src/linux-headers-2.6.31-19-generic'CC [M] /home/lebych/works/driver/hello.oBuilding modules, stage 2.MODPOST 1 modulesCC /home/lebych/works/driver/hello.mod.oLD [M] /home/lebych/works/driver/hello.komake: Leaving directory `/usr/src/linux-headers-2.6.31-19-generic'
hello.ko 파일로 올려보고 내려봅니다.
root@ubuntu:~/works/driver# lsMakefile Module.symvers hello.ko hello.o modules.order test.cMakefile~ hello.c hello.mod.c linux-headers-2.6.31-19-generic_2.6.31-19.56_i386.deb nothing.c test.c~Module.markers hello.c~ hello.mod.o mk nothing.c~root@ubuntu:~/works/driver# insmod hello.koroot@ubuntu:~/works/driver# dmesg |tail[ 21.160163] eth0: no IPv6 routers present[ 1494.370719] nfsd: last server has exited, flushing export cache[ 1495.511473] svc: failed to register lockdv1 RPC service (errno 97).[ 1495.512307] NFSD: Using /var/lib/nfs/v4recovery as the NFSv4 state recovery directory[ 1495.512354] NFSD: starting 90-second grace period[ 3280.652439] VMCIUtil: Updating context id from 0xffffffff to 0x2854cf9d on event 0.[ 6840.241356] VMCIUtil: Updating context id from 0x2854cf9d to 0x2854cf9d on event 0.[10997.302030] hello: module license 'unspecified' taints kernel.[10997.302056] Disabling lock debugging due to kernel taint[10997.302661] Hello, worldroot@ubuntu:~/works/driver# rmmod hello.koroot@ubuntu:~/works/driver# dmesg|tail[ 1494.370719] nfsd: last server has exited, flushing export cache[ 1495.511473] svc: failed to register lockdv1 RPC service (errno 97).[ 1495.512307] NFSD: Using /var/lib/nfs/v4recovery as the NFSv4 state recovery directory[ 1495.512354] NFSD: starting 90-second grace period[ 3280.652439] VMCIUtil: Updating context id from 0xffffffff to 0x2854cf9d on event 0.[ 6840.241356] VMCIUtil: Updating context id from 0x2854cf9d to 0x2854cf9d on event 0.[10997.302030] hello: module license 'unspecified' taints kernel.[10997.302056] Disabling lock debugging due to kernel taint[10997.302661] Hello, world[11012.765777] Goodbye cruel Worledroot@ubuntu:~/works/driver#
일단은 잘되는 것 같습니다.
좀더 본격적인 구현을 해봐야겠습니다.
<추가>기존의 2.4.x 버젼의 Makefile을 가지고 빌드를 할 경우와의 차이점을 알아냈습니다.2.4.x 버젼에서는 모듈 소스를 현재 위치에 두고, kernel include만을 참조했는데,2.6.x 버젼에서는 반대로 kernel build 디렉토리로 이동해서 모듈 소스위치를 참조합니다.빌드위치가 반드시 /lib/modules/2.6.31-19-generic/build/ 에서 되어야 하며,컴파일 시 소스 위치에 소스 디렉토리를 포함시켜줍니다.일일히 쳐줘도 됩니다만, 오타의 가능성을 최대한 배제하고 자동화하기 위해서위의 make 커맨드 라인을 Makefile파일로 만들면 다음과 같습니다.(반복적으로 들어가는 Makefile을 간이화 하기위해 이렇게 된 것 같습니다. /lib/modules/(version)/build 위치에 보시면 Makefile이 있군요. 드라이버 소스 위치에서 make를 하면 여기에 있는 Makefile을 참조합니다. 2.4.x 버젼에 비해서 2.6.x 버젼의 모듈 빌드는 컴파일 directive을 사용하지 않고, 따로 Makefile을 쓰게되었군요. 좀 더 편해지자고 한 것 같습니다.) |