实现一个字符设备的读写

#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/fs.h>
#include<linux/slab.h>
#include<linux/timer.h>
#include<linux/sched.h>
#include<linux/list.h>
#include<linux/interrupt.h>

#include<linux/cdev.h> //cdev_init() cdev_add() cdev_del()
#include<linux/types.h> //dev_t
#include<linux/kdev_t.h> //有两个宏获取主设备号和次设备号

#include<linux/uaccess.h> //container_of
MODULE_AUTHOR("Tan xujia");
MODULE_LICENSE("Dual BSD/GPL");

dev_t c_devt;  //设备的主次设备号


/*定义一个结构体,包含了cdev,类似与继承,把这个设备文件的属性给继承过来*/
struct char_dev {
    struct cdev c_dev;
    char *c;
    int n;
};


/*设备的操作方法*/
int
char_open(struct inode *inode, struct file *filp)
{
    /*inode->i_cdev是
    cdev_add的第一个参数*/
    struct char_dev *cdev_p = container_of(inode->i_cdev, struct char_dev, c_dev);
    printk("char_open, c_dev%d, %d\n", iminor(inode), MINOR(inode->i_cdev->dev));
    filp->private_data = cdev_p;
    return 0;
}

ssize_t
char_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos)
{
    
    ssize_t ret = 0;
    struct char_dev *cdev_p = filp->private_data;
    printk("char_read, cdev_p = %p\n", cdev_p);
    if(*fpos >= cdev_p->n)
        goto fail;

    if((*fpos + count) > cdev_p->n)
        count = cdev_p->n - *fpos;

    if(copy_to_user(buf, cdev_p->c, count)) {
        ret = -EFAULT;
        goto fail;
    }

    *fpos += count;
    return count;
fail:
    return ret;
}

ssize_t
char_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos)
{
    
    struct char_dev *cdev_p = filp->private_data;
    int ret = -ENOMEM, index = 0;
    printk("char_write, cdev_p = %p, count = %ld\n", cdev_p, count);

    if(cdev_p->c) {
        kfree(cdev_p->c);
        cdev_p->c = NULL;
    }

    cdev_p->n = 0;
 
    cdev_p->c  = kzalloc(count, GFP_KERNEL);
    if(!cdev_p->c) 
        goto fail;
    
    if(copy_from_user(cdev_p->c, buf, count)) {
        ret = -EFAULT;
        goto fail;
    }

    /*这里应该是count -1*/
    for(index = 0; index < count; index++)
        printk("cdev_p->c[%d] = %c\n", index, cdev_p->c[index]);
    cdev_p->n = count;
    return count;
fail:
    return ret;
}

int
char_release(struct inode *inode, struct file *filp)
{
    printk("char_release\n");
    return 0;
}

struct file_operations char_fops = {
    .owner = THIS_MODULE,
    .read = char_read,
    .write = char_write,
    .open = char_open,
    .release = char_release,
};


struct class *cdev_class;
struct char_dev *cdev_p;

static
int __init hello_init (void)
{
    int ret = 0, index = 0;
    printk("hello_init\n");
    ret = alloc_chrdev_region(&c_devt, 0, 2, "char_dev");
    if(ret)
         printk("alloc_chrdev_region fail\n");

    printk("major = %d, minor = %d\n", MAJOR(c_devt), MINOR(c_devt));

    cdev_p = kzalloc(sizeof(struct char_dev) * 2, GFP_KERNEL);
    if(!cdev_p)
        printk("alloc fail\n");

    for(index = 0; index < 2; index++) {
        cdev_init(&cdev_p[index].c_dev, &char_fops);
        cdev_p[index].c_dev.owner = THIS_MODULE;
        ret = cdev_add(&cdev_p[index].c_dev, 
            MKDEV(MAJOR(c_devt), MINOR(c_devt) + index), 1);
        if(ret) 
            printk("cdev_add fail\n");
    }

    /*根据以下api替代mknod生成设备节点*/
    cdev_class = class_create(THIS_MODULE, "c_dev");
    if(!cdev_class)
        printk("class_create fail\n");

    for(index = 0; index < 2; index++) {
        device_create(cdev_class, NULL, 
            MKDEV(MAJOR(c_devt), MINOR(c_devt) + index), 
            NULL, "c_dev%d", index);
    }
    return ret;
}

static
void __exit hello_exit (void)
{
    int index = 0;
    printk("hello_exit\n");
    for(index = 0; index < 2; index++) {
        device_destroy(cdev_class, 
            MKDEV(MAJOR(c_devt), MINOR(c_devt) + index));
    }

    class_destroy(cdev_class);

    for(index = 0; index < 2; index++) {
         cdev_del(&cdev_p[index].c_dev);
    }

    kfree(cdev_p);

    unregister_chrdev_region(c_devt, 2);
}

module_init(hello_init);
module_exit(hello_exit);
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD       := $(shell pwd)

obj-m	:=cdev_register_node.o

all:
	make -C $(KERNELDIR) M=$(PWD) modules

clean:
	rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.* .tmp_versions *.mod *.order *.symvers *.dwo

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值