3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シンプルなキャラクタ型デバイスドライバ作成メモ(Read/Write実装)

Last updated at Posted at 2017-08-16

前提

  • デバイスファイルに文字列を書き込み、読み込みをする
  • その他は前回の記事同様

メモ

コード

simple_device_driver2.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/uaccess.h> // copy_from_user
#define NODE_NAME "kuredev"
#define SUCCESS 0

MODULE_LICENSE("Dual BSD/GPL");

static int major_num = 0;
static unsigned char k_buf[1024];
static int data_size;
static int read_count = 0;

static int kure_open(struct inode *inode, struct file *file){
        printk("kure_open\n");
        return SUCCESS;
}

static int kure_release(struct inode *inode, struct file *file){
        printk("kure_release\n");
        return SUCCESS;
}

static ssize_t kure_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos){
        printk("kure_read, count: %d, data_size: %d\n", count, data_size);
        if(read_count){
                return 0;
        }
        int len;
        if(count > data_size){
                len = data_size;
        }else{
                len = count;
        }
        printk("len: %d\n", len);
        copy_to_user(buf, k_buf, len);
        read_count++;
        return len;
}

static ssize_t kure_write(struct file *file, char __user *buf, size_t count,loff_t *f_pos){
        printk("kure_write, count = %d\n", count);
        copy_from_user(k_buf, buf, count);
        data_size = count;
        return count;
}

struct file_operations kure_fops = {
        .owner = THIS_MODULE,
        .read = kure_read,
        .write = kure_write,
        .open = kure_open,
        .release = kure_release
};

static int kure_init(void){
        major_num = register_chrdev(major_num, NODE_NAME, &kure_fops);
        printk("kure_init\n");
        return SUCCESS;
}

static void kure_exit(void){
        unregister_chrdev(major_num, NODE_NAME);
        printk("kure_exit\n");
}

module_init(kure_init);
module_exit(kure_exit);

実行結果

# make
# insmod simple_device_driver2.ko
# cat /proc/devices | grep kure
246 kuredev
# mknod -m 666 /dev/kuredev c 246 0
# echo kure > /dev/kuredev
# cat /dev/kuredev
kure

参考

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?