180 likes | 337 Views
컴퓨터 응용실험 1. 2. 장치 드라이버의 구조 및 형태. 목차. 장치 드라이버란 장치 드라이버의 구분 장치 드라이버의 형태 file_operations 구조체 File_operations 구조체의 함수 장치 드라이버의 커널 적재 및 사용. 장치 드라이버란. 운영체제의 커널과 하드웨어를 연결 하드웨어의 논리적 인터페이스 제공 장점 커널이 단순화 됨 운영체제는 각각의 장치에 관한 프로그램을 작성할 필요가 없음. 장치 드라이버의 구분. 문자 디바이스 (Character Device)
E N D
컴퓨터 응용실험 1 2. 장치 드라이버의 구조 및 형태
목차 • 장치 드라이버란 • 장치 드라이버의 구분 • 장치 드라이버의 형태 • file_operations구조체 • File_operations구조체의 함수 • 장치 드라이버의 커널 적재 및 사용
장치 드라이버란 • 운영체제의 커널과 하드웨어를 연결 • 하드웨어의 논리적 인터페이스 제공 • 장점 • 커널이 단순화 됨 • 운영체제는 각각의 장치에 관한 프로그램을 작성할 필요가 없음
장치 드라이버의 구분 • 문자 디바이스(Character Device) • 블럭 디바이스(Block Device) • 네트워크 디바이스(Network Device)
문자 디바이스(Character Device) • 바이트 단위로 입출력 • open, close, read, write • 텍스트 콘솔, 시리얼 포트 등 • 순차적으로만 접근 가능
블럭 디바이스(Block Device) • 2n KB 단위로 입출력 • open, close, read, write
네트워크 디바이스(Network Device) • 데이터패킷의 송수신 • 개개의 패킷의 응용을 구분하지 못함 • eth0등과 같은고유의 이름 • read, write 대신 패킷 전송과 관련된 함수들을 호출
리눅스에서 장치 드라이버의 위치 Image by S.J. Lee
file_operations구조체 • file을 처리하는 함수들의 포인터들을 모아 놓은 구조체 • <linux/fs.h>에 정의되어 있음 • 표준화된 인터페이스 제공
file_operations구조체 #include <linux/fs.h> structfile_operations { loff_t (*llseak)(struct file *, loff_t, int); ssize_t (*read)(struct file *, char *, size_t, loff_t); ssize_t (*write)(struct file *, char *, size_t, loff_t); int (*readdir)(struct file *, void *, filldir_t); unsigned int (*poll)(struct file *,structpoll_table_struct *); int (*ioctl)(structinode *, struct file *, unsigned int, unsigned long); int (*mmap)(struct file *, structvm_area_struct *); int (*open)(structinode *, struct file *); int (*flush)( struct file *); int (*release)(structinode *, struct file *); int (*fsync)(structinode , structdentry *, int); int (*fasync)(int, struct file *, int); int (*lock)(struct file *, int, structfile_lock *); ssize_t (*readv)(struct file *, const structiovec *, unsigned long, loft_t *); ssize_t (*writev)(struct file *, const structiovec *, unsigned long, loft_t *); struct module *owner; // 구조체를 소유하고 있는 모듈에 대한 포인터 };
file_operations구조체의 함수 • llseek • read • write • readdir • poll • ioctl • mmap • open • flush • release • fsync • fasync • check_media_change • revalidate • lock • readv • writev • sendpage • get_unmapped_are
장치 드라이버 사용 방법 Device driver insmod 커널 내 모듈 적재 init_module() Application Program call return dev_open() open() 드라이버 동작 dev_Write() dev_Read() 드라이버 동작 write() read() call return call return dev_release() close() rmmod 커널 내 모듈 제거 cleanup_module()
장치 드라이버의 커널적재/삭제 • insmod: 장치 드라이버 모듈을 커널에 적재하는 명령어 • #insmodDriverName.o • 커널 안에서 진행 순서 • sys_create_module(): 메모리 할당 • sys_get_kernel_syms(): 심벌 등록 • sys_init_module(): 드라이버 적재, 드라이버의 init_module() 실행->register_chrdev() 수행 • rmmod: 장치 드라이버를 커널에서 삭제 • #rmmodDriverName
장치 파일의 생성 • 장치 유형, 주 번호, 부 번호 값이 필요 • #mknode /dev/DeviceName [장치 유형] [주 번호] [부 번호] • 장치 유형 • b: 블록, c: 문자 • 주 번호 • 디바이스 종류마다 한 개 • 예: Driver4 manages /dev/tty1, /dev/ttyS0, and /dev/ttyS1 • 부 번호 • 디바이스 종류에 해당하는 디바이스마다 한 개
장치 파일의 생성 • 주 번호 결정하는 방법 • 자주 사용되는 디바이스들의 주 번호는 미리 결정 • Documentation/devices.txt • “experimental or local use”로 예약되어 있는 주 번호 • 60-63, 120-127, 240-254 • 주 번호를 동적으로 할당하는 방법 • register_chrdev()를 호출할 때, 파라미터major의 값 =0 • Return value • 양수: 할당된 major number • 음수: error code
응용프로그램에서 사용: open() • 대부분의 drivers의 경우 open에서 수행하는 일 • Increment the usage count (release method에서 usage count 감소) • Check for device-specific errors (such as device-not-ready error or similar hardware problems) • Initialize the device, if it is being opened for the first time • Identify the minor number and update the f_op pointer, if necessary. • Allocate and fill any data structure to be put in filp->private_data
응용프로그램에서 사용: release() • release에서 수행하는 일 • open에서 filp->private_data에 할당한 것들을 deallocate • decrement the usage count • shut down the device on last close