1 min read

리눅스 문자드라이버 샘플코드

리눅스 문자드라이버 샘플코드
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

#define DEVICE_NAME "simple" // 디바이스 이름

static int major_number; // 메이저 디바이스 번호
static int device_open_count = 0; // 디바이스가 열려있는 횟수
static char message[256] = {0}; // 디바이스에 보낼 메시지
static char *message_ptr; // 메시지를 가리키는 포인터

// 디바이스를 열 때 호출되는 함수
static int device_open(struct inode *inode, struct file *file) {
    if (device_open_count)
        return -EBUSY;

    device_open_count++;
    message_ptr = message;
    try_module_get(THIS_MODULE);

    return 0;
}

// 디바이스를 닫을 때 호출되는 함수
static int device_release(struct inode *inode, struct file *file) {
    device_open_count--;

    module_put(THIS_MODULE);

    return 0;
}

// 디바이스로부터 데이터를 읽을 때 호출되는 함수
static ssize_t device_read(struct file *file, char *buffer, size_t length, loff_t *offset) {
    int bytes_read = 0;

    if (*message_ptr == 0)
        return 0;

    while (length && *message_ptr) {
        put_user(*(message_ptr++), buffer++);
        length--;
        bytes_read++;
    }

    return bytes_read;
}

// 디바이스에 데이터를 쓸 때 호출되는 함수
static ssize_t device_write(struct file *file, const char *buffer, size_t length, loff_t *offset) {
    int i;

    for (i = 0; i < length && i < sizeof(message); i++)
        get_user(message[i], buffer + i);

    message_ptr = message;

    return i;
}

// 디바이스 드라이버의 연산 구조체
static struct file_operations fops = {
    .open = device_open,
    .release = device_release,
    .read = device_read,
    .write = device_write,
};

// 모듈 초기화 함수
static int __init char_device_init(void) {
    major_number = register_chrdev(0, DEVICE_NAME, &fops);
    if (major_number < 0) {
        printk(KERN_ALERT "Registering char device failed with %d\n", major_number);
        return major_number;
    }
    printk(KERN_INFO "Registered a device with major number %d\n", major_number);

    return 0;
}

// 모듈 정리 함수
static void __exit char_device_exit(void) {
    unregister_chrdev(major_number, DEVICE_NAME);
    printk(KERN_INFO "Unregistered the device\n");
}

module_init(char_device_init);
module_exit(char_device_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple character device driver");