본문 바로가기

Embedded/Device Driver

[Device Driver] 커널에 Device Driver 등록 및 실행

작업 환경

-메인 OS : Windows 8.1K(Intel Core i5-4590)

-작업 OS : Ubuntu 14.04.5 LTS 64bit(VirtualBox)

-장 비 명 : Hybus-Smart4412



1. 초기설정

- mkdir device --> device 디렉토리에 skeleton.c 와 Makefile을 만든다.


skeleton.c

/*뼈대 작성*/
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/major.h>

MODULE_LICENSE("GPL");

int result;

int skeleton_open(struct inode *inode, struct file *filp) {
printk("Device Open!!\n");
return 0;
}

int skeleton_release(struct inode *inode, struct file *filp) {
printk("Device Release!!\n");
return 0;
}

int skeleton_read(struct file *filp, const char *buf, size_t count, loff_t *f_pos){
printk("Device Read!!\n");
return 0;
}

int skeleton_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
printk("Device Ioctl!!\n");
return 0;
}

int skeleton_write(struct file *filp, unsigned int *buf, size_t count, loff_t *f_pos)
{
printk("Device Write!!\n");
return 0;
}


struct file_operations skeleton_fops = {
.open   = skeleton_open, 
.release= skeleton_release,
.read = skeleton_read,
.unlocked_ioctl = skeleton_ioctl,
.write = skeleton_write,
};

static int skeleton_init(void) 
{
    printk("skeleton module init!!\n");
    result = register_chrdev(0, "skeleton!!", &skeleton_fops);
    printk("major number=%d\n", result);
    return 0;
}

static void skeleton_exit(void)
{
    printk("skeleton module exit!!\n");
}

module_init(skeleton_init);        //모듈등록
module_exit(skeleton_exit);      //모듈해제


Makefile

CC := /usr/local/CodeSourcery/Sourcery_G++_Lite/bin/arm-none-eabi-gcc

obj-m := skeleton.o

KDIR := ~/Smart4412/Development/Source/Kernel/kernel_4412  

all:

        make -C $(KDIR) SUBDIRS=$(PWD) modules

clean:

        rm -rf *.o *.mod *.ko *.cmd *.mod.c



2. 빌드

- make (device 디렉토리에서)

- skeleton.ko , skeleton.mod.c , skeleton.mod.o , skeleton.o 파일들이 만들어 진다.


3. Device Driver 등록

- Tera Term에서 포트 설정 후  설정 -> 시리얼 포트 -> 속도를 115200으로 설정

- Hybus-Smart4412 장비 부팅

- 메뉴 -> 전송 -> ZMODEM -> 보내기에서 skeleton.ko 선택

- insmod skeleton.ko (커널(모듈) 등록)

 [init module success!!.. skeleton major numver : 248] 출력되면 성공

mknod /dev/skeleton c 248 0  (디바이스 등록)

(/dev/skeleton(사용하려는 디바이스이름) , c(캐릭터 디바이스) , 248(주번호) , 0(부번호)) 

커널에 등록되어진 것을 디바이스드라이버에 등록


4. Skeleton Driver 사용 및 테스트

userapp.c

#include <stdio.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdlib.h>

#include <string.h> 

int main()

{

int fd;

fd = open("/dev/skeleton", O_RDWR);

printf("Device Driver Test Application\n");

read(fd, 0, 0);

ioctl(fd, NULL, 0);

close(fd);

return 0;

}


/opt/gnueabi/opt/ext-toolchain/bin/arm-linux-gnueabihf-gcc userapp.c -o userapp (해당 디렉토리에서 컴파일)

- Tera term으로 이동 (http://cccding.tistory.com/58 에서 1) 참조)

- 업로드 후 ./userapp 실행