basic.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
int basic_init(void){
printk(KERN_INFO "Hello Basic\n"); // 모듈이 붙으면서 수행되는 함수
return 0;
}
void basic_exit(void){
printk(KERN_INFO "Bye Basic\n"); // 모듈이 종료되면서 수행되는 함수
}
module_init(basic_init);
module_exit(basic_exit);
MODULE_LICENSE("GPL");
Makefile
CC := gcc # Compiler 'gcc'
obj-m := basic.o # Make moduler object file
KDIR := /usr/src/linux-headers-3.11.0-15-generic # Set Kernel dir
PWD := $(shell pwd) # Set Source dir
#default target
default :
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean :
rm -f *.ko # kernel src
rm -f *.o # obj src
rm -f *.mod.* # mod src
rm -f .*.cmd # hidden src
root@KMU-UBUNTU:/work/dev_basic# cat /proc/devices ( 이곳에 basic 이라는 모듈이 올라가야함)
0~100 까지는 잘안쓰고 101~ 부터 쓰면 좋다.
int register_chrdev(unsigned int major, const char *name, struct file_operations *fops)// 주번호, 디바이스 이름, 구조체변수 주소
major 가 0 일 경우 사용하지 않는 번호가 자동으로 할당
basic.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
struct file_operations basic_fops = {
};
int basic_init(void){
int result;
result = register_chrdev(240,"basic_driver",&basic_fops);
if ( result < 0 ) {
printk(KERN_ERR "Basic Registration Failure\n");
return result;
} else {
printk(KERN_INFO "Hello Basic %d\n",result);
}
return 0;
}
void basic_exit(void){
unregister_chrdev(240,"basic_driver");
printk(KERN_INFO "Bye Basic\n");
}
module_init(basic_init);
module_exit(basic_exit);
MODULE_LICENSE("GPL");
/////////////////////////////////////////open release 추가
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
int basic_open(struct inode* pinode, struct file* pfile) {
printk(KERN_INFO "Open Success\n");
return 0;
}
int basic_release(struct inode* pinode, struct file* pfile) {
printk(KERN_INFO "Release Success\n");
return 0;
}
struct file_operations basic_fops = {
open : basic_open,
release : basic_release,
};
int basic_init(void){
int result;
result = register_chrdev(240,"basic_driver",&basic_fops);
if ( result < 0 ) printk(KERN_ERR "Basic Registration Failure\n");
return result;
} else {
printk(KERN_INFO "Hello Basic %d\n",result);
}
return 0;
}
void basic_exit(void){
unregister_chrdev(240,"basic_driver");
printk(KERN_INFO "Bye Basic\n");
}
module_init(basic_init);
module_exit(basic_exit);
MODULE_LICENSE("GPL");
///////////////////////////////////////////////////////////////////////
test.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main(void){
int fd;
fd = open("/dev/basic", O_RDONLY);
if ( fd < 0 ) {
perror("open");
exit(1);
}
printf("fd = %d\n", fd);
close(fd);
return 0;
}
root@KMU-UBUNTU:/work/dev_basic# mknod /dev/basic c 240 0
[ 5285.824505] Hello Basic 0
[ 5782.516085] Open Success
[ 5782.516227] Release Success
'이전것 > LINUX' 카테고리의 다른 글
우분투 16.04 기본셋팅 (0) | 2016.11.10 |
---|---|
device driver 3 (0) | 2016.05.23 |
device driver (0) | 2016.05.09 |
make (0) | 2016.04.04 |
압축 (0) | 2016.03.21 |