티스토리 뷰
Kernel Module 뼈대 소스
#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 static int onevalue = 1; static char *twostring = NULL; module_param(onevalue,int,0); module_param(twostring,charp,0); /** * module_param(variable,type,property) * * type / variable * * short : short * ushort : unsigned short * int : int * uint : unsigned int * long : long * ulong : unsigned long * charp : char * * bool : int * invbool : int * intarray : int * * * property : generally using 0. * * */ //to use the param : insmod test.ko onevalue=0x27 twostring"Oh Oh Oh Oh!" int call_open(struct inode *inode, struct file *filp) { int num = MINOR(inode->i_rdev); printk("call open -> minor : %d\n",num); return 0; } loff_t call_llseek(struct file *filp, loff_t off, int whence) { printk("call llseek -> off : %08X, whence : %08X\n",off,whence); return 0x23; } ssize_t call_read(struct file *filp, char *buf, size_t count, loff_t *f_pos) { printk("call read -> buf : %08X, count : %08X \n",buf,count); return 0x33; } ssize_t call_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos) { printk("call write -> buf : %08X, count : %08X \n", buf, count); return 0x43; } int call_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg) { printk("call ioctl -> cmd : %08X, arg : %08X \n",cmd,arg); return 0x53; } int call_release(struct inode *inode, struct file *filp) { printk("call release\n"); return 0; } struct file_operations call_fops = { .owner = THIS_MODULE, .llseek = call_llseek, .read = call_read, .write = call_write, .ioctl = call_ioctl, .open = call_open, .release= call_release, }; static int call_init(void) { printk("Hello ,world onevalue:%d twostring=%s\n",onevalue,twostring); int result = -1; result = register_chrdev(CALL_DEV_MAJOR,CALL_DEV_NAME,&call_fops); if(result < 0) return result; return 0; } static void call_exit(void) { printk("BYe Bye\n"); unregister_chrdev(CALL_DEV_MAJOR, CALL_DEV_NAME); } module_init(call_init); module_exit(call_exit); MODULE_AUTHOR("pe.kr.roter"); MODULE_DESCRIPTION("Standard Module Example"); MODULE_LICENSE("Dual BSD/GPL");
Makefile
obj-m := call_dev.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.ko
rm -rf *.mod.*
rm -rf .*.cmd
rm -rf *.o
rm -rf Module.markers
rm -rf modules.order
rm -rf Module.symvers
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -rf *.ko
rm -rf *.mod.*
rm -rf .*.cmd
rm -rf *.o
rm -rf Module.markers
rm -rf modules.order
rm -rf Module.symvers
예제 app 소스
#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 open\n"); dev = open(DEVICE_FILENAME, O_RDWR|O_NDELAY); if(dev >= 0) { printf("2) seek function call\n"); ret = lseek(dev, 0x20, SEEK_SET); printf("ret = %08X\n",ret); printf("3) read function call\n"); ret = read(dev, 0x30, 0x31); printf("ret = %08X\n",ret); printf("4) write function call\n"); ret = write(dev, 0x40, 0x41); printf("ret = %08X\n", ret); printf("5) ioctl function call\n"); ret = ioctl(dev, 0x51, 0x52); printf("ret = %08X\n",ret); printf("6) device file close\n"); ret = close(dev); printf("ret = %08X\n",ret); } else { printf("Device open failed\n"); } return 0; }
실행
#su
#mknod /dev/calldev c 240 32
#insmod call_dev.ko
#rmmod calldev
#mknod /dev/calldev c 240 32
#insmod call_dev.ko
#rmmod calldev
'Development > Linux' 카테고리의 다른 글
Ubuntu 14.04에 Synergy 설치하여 시작 프로그램에 등록 하기 / 사라진 커서 보이게 하기 (0) | 2015.07.21 |
---|---|
[Linux] Ubuntu 10.10 server 인터넷 연결하기 (0) | 2011.02.07 |
리눅스에서 해당하는 함수의 라이브러리 찾기 (0) | 2010.05.17 |
FILE Input/Output System Call Function (0) | 2008.11.27 |
mknod에서 동적으로 major no를 할당 받자 (0) | 2008.11.06 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 리눅스
- winapi
- 음악
- android
- source
- database
- Troubleshooting
- AWS
- Cloud
- it
- jni
- C++
- Visual C++
- 안드로이드
- Quiz
- kering
- linux
- Python
- C
- NDK
- API
- driver
- jni강좌
- algorithm
- MFC
- 프로그래밍
- gcc
- 드라이버
- java
- db
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
글 보관함