검색결과 리스트
알짜정보/Linux device driver에 해당되는 글 1건
- 2010.03.18 hello 디바스 드라이버 46
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int __init hello_init(void) {
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void __exit hello_exit(void) {
printk(KERN_ALERT "Goodbye, world\n");
}
module_init(hello_init);
module_exit(hello_exit);
사실 MODULE_LICENSE 는 옵션이다. 그러나 지정하지 않은 모듈을 적재시 "오염 상태" 가 된다."GPL" , "GPL v2" , "GPL and additional rights" , "Dual BSD/GPL" , "Dual MPL/GPL"
"Proprietary"
KERNELDIR 에는 커널헤더 경로가 들어가기 때문에 위와 같은 명령을 사용하면 헤더의 경로를 가져올 수 있다.KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
obj-m := hello.o
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm *.ko *.o Module.* modules.* *.mod.c
위와 같이 나오면 제대로 컴파일 된 것이고 hello.ko 파일이 생겼을 것이다.root@cranix-desktop:~/work/drivers# make
make -C /lib/modules/2.6.31-20-generic-pae/build M=/root/work/drivers modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.31-20-generic-pae'
CC [M] /root/work/drivers/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /root/work/drivers/hello.mod.o
LD [M] /root/work/drivers/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.31-20-generic-pae'
위와같이 디바이스 드라이버는 insmod 와 rmmod 로 적재와 삭제를 하고 lsmod 로 리스트를 볼 수 있다.root@cranix-desktop:~/work/drivers# insmod hello.ko
root@cranix-desktop:~/work/drivers# lsmod |grep hello
hello 1052 0
root@cranix-desktop:~/work/drivers# rmmod hello
root@cranix-desktop:~/work/drivers# lsmod |grep hello
root@cranix-desktop:~/work/drivers#
메시지가 쭈욱 나오는데 제일 아래보면 원하던 메시지를 볼 수 있을 것이다.root@cranix-desktop:~/work/drivers# dmesg
...
...
[ 2747.649737] Hello, world[ 2759.753311] Goodbye, world
RECENT COMMENT