シンプルなキャラクタ型デバイスドライバ作成メモ（cdev）

# 前提
- register_chrdevの代わりにcdev関数を利用
- Linuxカーネル2.6以降ではこの方が推奨らしい
- その他は[前回の記事](http://qiita.com/kure/items/1e7397082f5f059354f6)同様

# メモ
## コード
```c:simple_device_driver3.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <asm/uaccess.h> // copy_from_user
#include <linux/cdev.h> // cdev★
#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 minor_num = 1;//★
static struct cdev kure_cdev;//★

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);
        dev_t dev = MKDEV(major_num, 0);//★
        alloc_chrdev_region(&dev, 0, minor_num, NODE_NAME);//★
        major_num = MAJOR(dev);//★
        cdev_init(&kure_cdev, &kure_fops);//★
        kure_cdev.owner = THIS_MODULE;//★
        cdev_add(&kure_cdev, MKDEV(major_num, 0), minor_num);//★

        printk("kure_init\n");
        return SUCCESS;
}

static void kure_exit(void){
//      unregister_chrdev(major_num, NODE_NAME);
        dev_t dev = MKDEV(major_num, 0);//★
        cdev_del(&kure_cdev);//★
        unregister_chrdev_region(dev, minor_num);//★

        printk("kure_exit\n");
}

module_init(kure_init);
module_exit(kure_exit);
```

## 実行結果
[前回](http://qiita.com/kure/items/1e7397082f5f059354f6)と一緒

# 参考

<div class="amazlet-box" style="margin-bottom:0px;"><div class="amazlet-image" style="float:left;margin:0px 12px 1px 0px;"><a href="http://www.amazon.co.jp/exec/obidos/ASIN/4774164755/kuredev-22/ref=nosim/" name="amazletlink" target="_blank"><img src="https://images-fe.ssl-images-amazon.com/images/I/512ND4tXmaL._SL160_.jpg" alt="ARMマイコンではじめる電子工作 ~組込みボードの選び方、使い方、作り方がわかる!" style="border: none;" /></a></div><div class="amazlet-info" style="line-height:120%; margin-bottom: 10px"><div class="amazlet-name" style="margin-bottom:10px;line-height:120%"><a href="http://www.amazon.co.jp/exec/obidos/ASIN/4774164755/kuredev-22/ref=nosim/" name="amazletlink" target="_blank">ARMマイコンではじめる電子工作 ~組込みボードの選び方、使い方、作り方がわかる!</a></div><div class="amazlet-detail">鳥海 佳孝 <br />技術評論社 <br />売り上げランキング: 703,695<br /></div><div class="amazlet-sub-info" style="float: left;"><div class="amazlet-link" style="margin-top: 5px"><a href="http://www.amazon.co.jp/exec/obidos/ASIN/4774164755/kuredev-22/ref=nosim/" name="amazletlink" target="_blank">Amazon.co.jpで詳細を見る</a></div></div></div><div class="amazlet-footer" style="clear: left"></div></div>