Application area Application System Call Interface VFS Kernel

  • Slides: 71
Download presentation

디바이스 드라이버 구조 Application area Application System Call Interface VFS Kernel area Char Device

디바이스 드라이버 구조 Application area Application System Call Interface VFS Kernel area Char Device Driver Buffer Cache Network Subsystem Block D/D Network D/D Device Interface Hardware 10

Character 디바이스 드라이버 등록 o Character 디바이스 드라이버 등록 n 커널의 초기화 시기에 character

Character 디바이스 드라이버 등록 o Character 디바이스 드라이버 등록 n 커널의 초기화 시기에 character device driver 를 file operation과 함께 등록 하여야 사용이 int register_chrdev( unsigned int major, char * name, 가능하다. const struct file_operations *fops) int unregister_chrdev(unsigned int major, const char * name) o Character 디바이스 드라이버 등록 해제 16

커널 모듈의 컴파일 (1) o arm-linux-gcc 이용한 컴파일 arm-linux-gcc -c -D__KERNEL__ -DMODULE -O 2

커널 모듈의 컴파일 (1) o arm-linux-gcc 이용한 컴파일 arm-linux-gcc -c -D__KERNEL__ -DMODULE -O 2 test. c -c : an object file will be linked into the kernel during runtime using insmod. -O 2 : the kernel makes extensive use of inline functions. -Wall : All warning flag -D__KERNEL__ : the code will be run in kernel mode, not as a user process. -DMODULE : this symbol tells the header files to give the appropriate definitions for a kernel module. 28

커널 모듈의 컴파일 (2) o Makefile을 작성하여 make 하는 방법 CC = arm-linux-gcc INCLUDE

커널 모듈의 컴파일 (2) o Makefile을 작성하여 make 하는 방법 CC = arm-linux-gcc INCLUDE = /root/linux/include CFLAGS = -D__KERNEL__ -DMODULE –I$(INCLUDE) -mapcs-32 -march=armv 4 -mtune=arm 9 tdmi all: test. o: test. c $(CC) $(CFLAGS) -c -o test. c clean: rm –rf *. o 29

커널 모듈의 작성 예 (1) o 예제 프로그램 n 커널에 모듈이 로딩될 때 “Hello

커널 모듈의 작성 예 (1) o 예제 프로그램 n 커널에 모듈이 로딩될 때 “Hello module”를 출력 n 모듈이 제거될 때 “Goodbye module”를 출력 n Source file : test. c /* test. c */ #include <init. h> #include <linux/module. h> /* 모든 모듈에 필요 */ #include <linux/kernel. h> /* printk() 등에 필요 */ int init_module(void) { // 모듈이 로딩될 때 호출 printk (“Hello modulen”); return 0; // 0: success , 기타 - fail } void cleanup_module(void) { // 제거 될 때 호출 printk (“KERN_ALERT “Goodbye module”); } 32

커널 모듈의 작성 예 (2) o 매크로 module_init(), module_exit() 사용 n __init 지시어와 __exit

커널 모듈의 작성 예 (2) o 매크로 module_init(), module_exit() 사용 n __init 지시어와 __exit 지시어 반드시 사용 #include <linux/init. h> #include <linux/module. h> #include <linux/kernel. h> static int __init_device(void) { printk(“Hello modulen”); return 0; } static void __exit cleanup_device(void) { printk(“Goodbye modulen”); } module_init(init_device); module_exit(cleanup_device) 33

확인 과정 (1) o Linux version 확인 n cat /proc/version o 디바이스 드라이버를 모듈로

확인 과정 (1) o Linux version 확인 n cat /proc/version o 디바이스 드라이버를 모듈로 적재하기 위한 확 인과정 n 커널 소스 디렉토리로 이동 o cd /usr/src/kernels/2. 6. 11 -1. 1369_FC 4 -i 686 n make menuconfig o Loadable module support 항목 이동 o Enable loadable module support 항목이 * 표시 확인 37

디바이스 드라이버 실습 – example 1 (1) o /root 에 device_driver/example 1 디렉토리 생성

디바이스 드라이버 실습 – example 1 (1) o /root 에 device_driver/example 1 디렉토리 생성 n mkdir /root/device_driver/example 1 o test. c , Makefile 을 복사 n 윈도우 상에서 Share_dir 에 파일 다운로드 n Vmware 상에서 복사 o o cd /mnt/hgfs/Shared_dir cp. /test. c. /Makefile /root/device_driver/example 1 cd /root/device_driver/example 1 ls n 파일 복사 확인 39

디바이스 드라이버 실습 – example 1 (2) ▪ test. c #include <linux/init. h> #include

디바이스 드라이버 실습 – example 1 (2) ▪ test. c #include <linux/init. h> #include <linux/module. h> #include <linux/kernel. h> static int hello_init(void) { printk("Hello, world n"); return 0; } static void hello_exit(void) { printk("Goodbye, worldn"); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("Dual BSD/GPL"); ▪ Makefile obj-m : = test. o KDIR: = /lib/modules/$(shell uname r)/build PWD: = $(shell pwd) default: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules clean: rm rm -rf -rf *. ko *. mod. *. *. cmd *. o 40

디바이스 드라이버 실습 – example 2 (1) o /root 에 device_driver/example 2 디렉토리 생

디바이스 드라이버 실습 – example 2 (1) o /root 에 device_driver/example 2 디렉토리 생 성 n mkdir /root/device_driver/example 2 o app, dev 디렉토리를 복사 n 윈도우 상에서 Share_dir 에 파일 다운로드 n Vmware 상에서 복사 o o cd /mnt/hgfs/Shared_dir cp -r. /app. /dev /root/device_driver/example 2 cd /root/device_driver/example 2 ls n 디렉토리 복사 확인 43

디바이스 드라이버 실습 – example 2 (2) ▪. /dev/call_dev. c #include <linux/init. h> #include

디바이스 드라이버 실습 – example 2 (2) ▪. /dev/call_dev. c #include <linux/init. h> #include <linux/module. h> #include <linux/kernel. h> #include <linux/fs. h> #include <linux/errno. h> #include <linux/types. h> #include <linux/fcntl. h> #define CALL_DEV_NAME "calldev" #define CALL_DEV_MAJOR 240 int call_open (struct inode *inode, struct file *filp) { int num = MINOR(inode->i_rdev); printk( "call open -> minor : %dn", num ); return 0; } ssize_t call_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) { printk( "call read -> buf : %08 X, count : %08 X n", buf, count ); return 0 x 33; } ssize_t call_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos) { printk( "call write -> buf : %08 X, count : %08 X n", buf, count ); struct file_operations call_fops = {. owner = THIS_MODULE, . read = call_read, . write = call_write, . open = call_open, . release = call_release, }; int call_init(void) { int result; printk( "call_init n" ); result = register_chrdev( CALL_DEV_MAJOR, CALL_DEV_NAME, &call_fops); if (result < 0) return result; return 0; } void call_exit(void) { printk( "call_exit n" ); unregister_chrdev( CALL_DEV_MAJOR, CALL_DEV_NAME ); } module_init(call_init); 44 module_exit(call_exit); MODULE_LICENSE("Dual BSD/GPL");

디바이스 드라이버 실습 – example 2 (3) o. /dev/Makefile obj-m : = call_dev. o

디바이스 드라이버 실습 – example 2 (3) o. /dev/Makefile obj-m : = call_dev. o KDIR PWD : = /lib/modules/$(shell uname -r)/build : = $(shell pwd) default: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules clean: rm rm -rf -rf *. ko *. mod. *. *. cmd *. o 45

디바이스 드라이버 실습 – example 2 (4) ▪. /app/call_app. c #include <stdio. h> #include

디바이스 드라이버 실습 – example 2 (4) ▪. /app/call_app. c #include <stdio. h> #include <sys/types. h> #include <sys/stat. h> #include <sys/ioctl. h> #include <fcntl. h> #include <unistd. h> #define DEVICE_FILENAME "/dev/calldev" int main() { int dev; char buff[128]; int ret; printf( "1) device file openn"); dev = open( DEVICE_FILENAME, O_RDWR|O_NDELAY ); if( dev >= 0 ) { printf( "2) read function calln"); ret = read(dev, 0 x 30, 0 x 31 ); printf( "ret = %08 Xn", ret ); printf( "3) write function calln"); ret = write(dev, 0 x 40, 0 x 41 ); printf( "ret = %08 Xn", ret ); printf( "4) device file closen"); ret = close(dev); printf( "ret = %08 Xn", ret ); } return 0; } 46

디바이스 드라이버 실습 – example 2 (5) o 디바이스 드라이버 모듈 생성 후 커널에

디바이스 드라이버 실습 – example 2 (5) o 디바이스 드라이버 모듈 생성 후 커널에 적재 n mknod /dev/calldev c 240 32 o /dev 디렉토리에 calldev 파일 생성 확인 n n cd /root/device_driver/example 2 cd. /dev make ls o call_dev. ko 파일 생성 확인 n insmod call_dev. ko n dmesg 47

디바이스 드라이버 실습 – example 2 (6) o 응용 프로그램을 컴파일 후, 실행 n

디바이스 드라이버 실습 – example 2 (6) o 응용 프로그램을 컴파일 후, 실행 n n cd /root/device_driver/example 2/app ls gcc –o call_app. exe call_app. c ls o call_app. exe 파일 생성 확인 n. /call_app. exe n dmesg o call_app. exe 실행 동안의 커널 메시지 확인 n rmmod call_dev n dmesg 48

7 -세그먼트 디바이스 드라이버를 만 들기 (1) o 커널버젼 2 -4인 소스 파일을 작업

7 -세그먼트 디바이스 드라이버를 만 들기 (1) o 커널버젼 2 -4인 소스 파일을 작업 디렉토리로 복 사 n n n n 윈도우상에서 Linux_Kernel. tar. bz 2를 Shared_dir 복사 mkdir /root/kernel_2_4 cd /root/kernel_2_4 cp /mnt/hgfs/Shared_dir/Linux_Kernel. tar. bz 2. / ls (복사 확인) tar xvfj Linux_Kernel. tar. bz 2 ls o linux-2. 4. 19 -rmk 7 -xp 100_RAM 32 M 폴더 생성 확인 56

7 -세그먼트 디바이스 드라이버를 만 들기 (2) o 7 Segment_Appl, 7 Segment_Drv 디렉토리를 작업디렉토리로

7 -세그먼트 디바이스 드라이버를 만 들기 (2) o 7 Segment_Appl, 7 Segment_Drv 디렉토리를 작업디렉토리로 이동 n 윈도우상에서 7 Segment_Appl, 7 Segment_Drv 디 렉토리를 Shared_dir 복사 n cd /root/device_driver n mkdir 7 Segment n cd 7 Segment n cp -r /mnt/hgfs/Shared_dir/7 Segment_Appl. / n cp -r /mnt/hgfs/Shared_dir/7 Segment_Drv. / n ls 57

7 -세그먼트 디바이스 드라이버를 만 들기 (3) o 7 Segment_Appl 디렉토리의 파일 내용 확

7 -세그먼트 디바이스 드라이버를 만 들기 (3) o 7 Segment_Appl 디렉토리의 파일 내용 확 인 n 7 seg_appl. h, 7 seg_appl. c n Makefile INCLUDEDIR : = /root/kernel_2_4/linux-2. 4. 19 -rmk 7 xp 100_RAM 32 M/include CFLAGS : = -I$(INCLUDEDIR) -Wall CROSS_COMPILE : = arm-linux. CC=$(CROSS_COMPILE)gcc LD=$(CROSS_COMPILE)ld all: 7 seg_appl: $(CC) $(CFLAGS) 7 seg_appl. c -o 7 seg_appl 58 clean:

7 -세그먼트 디바이스 드라이버를 만 들기 (4) o 7 Segment_Drv 디렉토리의 파일 내용 확인

7 -세그먼트 디바이스 드라이버를 만 들기 (4) o 7 Segment_Drv 디렉토리의 파일 내용 확인 n 7_segment. h, 7_segment. c n Makefile o 기존 작업 파일 삭제 n cd /root/device_driver/7 Segment_Appl n make clean o 7 seg_appl 실행 파일 삭제 확인 n make o 7 seg_appl 실행 파일 생성 확인 n n cd. . /7 Segment_Drv make clean o 7_segment. o 모듈 파일 삭제 확인 n make o 7_segment. o 모듈 파일 생성 확인 59

7 -세그먼트 디바이스 드라이버를 만 들기 (5) o 생성된 응용 파일과 디바이스 드라이버 모

7 -세그먼트 디바이스 드라이버를 만 들기 (5) o 생성된 응용 파일과 디바이스 드라이버 모 듈을 Shared_dir 로 복사 n cd / root/device_driver/7 Segment_Ap pl/ n cp. /7 seg_appl /mnt/hgfs/Shared_dir n cd. . /7 Segment_Drv n cp. /7_segment. o /mnt/hgfs/Shared_dir 60

7 -세그먼트 디바이스 드라이버를 만 들기 (8) o 임베디드 리눅스에 적재된 7_segment 디바이스 모듈

7 -세그먼트 디바이스 드라이버를 만 들기 (8) o 임베디드 리눅스에 적재된 7_segment 디바이스 모듈 제거 후 적재 n lsmod n rmmod 7_segment o n n dmesg rm /dev/skull o n n 디바이스 파일 제거 mknod /dev/skull c 232 6 insmod. /7_segment. o o n n n 디바이스 모듈 제거 디바이스 모듈 삽입 lsmod chmod 775 7 seg_appl. /7 seg_appl 63

Text LCD 디바이스 드라이버를 만 들기 (1) o Text_LCD_Appl, Text_LCD_Drv 디렉토리를 작업디렉토리로 이동 n

Text LCD 디바이스 드라이버를 만 들기 (1) o Text_LCD_Appl, Text_LCD_Drv 디렉토리를 작업디렉토리로 이동 n 윈도우상에서 Text_LCD_Appl, Text_LCD_Drv 디 렉토리를 Shared_dir 복사 n cd /root/device_driver n mkdir text_lcd n cd text_lcd n cp -r /mnt/hgfs/Shared_dir/Text_LCD_Appl. / n cp -r /mnt/hgfs/Shared_dir/Text_LCD_Drv. / n ls 65

Text LCD 디바이스 드라이버를 만 들기 (2) o Text_LCD_Appl 디렉토리의 파일 내용 확 인

Text LCD 디바이스 드라이버를 만 들기 (2) o Text_LCD_Appl 디렉토리의 파일 내용 확 인 n lcd_appl. h, lcd_appl. c n Makefile o Text_LCD_Drv 디렉토리의 파일 내용 확인 n txt_lcd. h, txt_lcd. c n Makefile 66

Text LCD 디바이스 드라이버를 만 들기 (3) o 기존 작업 파일 삭제 n cd

Text LCD 디바이스 드라이버를 만 들기 (3) o 기존 작업 파일 삭제 n cd /root/device_driver/text_lcd/Text_LCD_Appl n make clean o lcd_appl 실행 파일 삭제 확인 n make o lcd_appl 실행 파일 생성 확인 n cd. . /Text_LCD_Drv n make clean o txt_lcd. o 모듈 파일 삭제 확인 n make o txt_lcd. o 모듈 파일 생성 확인 67

Text LCD 디바이스 드라이버를 만 들기 (4) o 생성된 응용 파일과 디바이스 드라이버 모

Text LCD 디바이스 드라이버를 만 들기 (4) o 생성된 응용 파일과 디바이스 드라이버 모 듈을 Shared_dir 로 복사 n cd / root/device_driver/text_lcd/Text_LCD_Appl/ n cp. /lcd_appl /mnt/hgfs/Shared_dir n cd. . /Text_LCD_Drv n cp. /txt_lcd. o /mnt/hgfs/Shared_dir 68

Text LCD 디바이스 드라이버를 만 들기 (6) o 임베디드 리눅스에 적재된 Text LCD 디바이스

Text LCD 디바이스 드라이버를 만 들기 (6) o 임베디드 리눅스에 적재된 Text LCD 디바이스 모듈 제거 후 적재 n lsmod n rmmod txt_lcd o 디바이스 모듈 제거 n n dmesg rm /dev/txt_lcd o 디바이스 파일 제거 n n mknod /dev/txt_lcd c 233 6 insmod. /txt_lcd. o o 디바이스 모듈 삽입 n n n lsmod chmod 775. /lcd_appl 70