字符设备研究_3

  研究上次留下的问题后发现思路比较乱,加上近期又有学习DSP变成的要求.这里先贴上实现多设备与信号量的代码,要说的都在注释里了.

sudo mknod /dev/globalmem0 c 250 0
sudo mknod /dev/globalmem1 c 250 1

  现在还只能这样在/dev下注册字符设备,以后也得加到代码里.

#include <linux/module.h>
/**
 *  动态加载模块
 *  定义 LICESENCE(), AUTHOR等等.
 *  kobject kset 定义
 */
#include <linux/init.h>
/**
 * 模块初始化的相关定义
 */
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/cdev.h>
#include <linux/slab.h>//kfree, kmalloc
#include <linux/semaphore.h>//信号量

#define MEM_SIZE 0x1000 //定义操作4KB内存
#define MEM_CLEAR 0x1 //定义fileoperations操作
#define MEM_MAJOR 250 //预设主设备号

#define EDITION 2 //调试版本

static int mem_major = MEM_MAJOR;//储存第一次分配到的主设备号,防止重复申请.
/**
 * 驱动操作的设备结构体
 */
struct mem_dev {
    int offset;
    struct cdev cdev;
    unsigned char mem[MEM_SIZE];
    struct semaphore sem;
}*mem_devp;

int mem_open(struct inode *inode, struct file *filp)
{
    //通过inode获得包含这个inode的cdev,以完成不同设备的打开
    struct mem_dev *devp = container_of(inode->i_cdev, struct mem_dev, cdev);
    filp->private_data = devp;//将设备结构体指针交给file,以后就由不同的file来分离对不同设备的操作

    //TODO:多设备

    return 0;
}

int mem_release(struct inode *inode, struct file *filp)
{
    filp->private_data = NULL;
    return 0;
}
static int mem_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
    struct mem_dev *dev = filp->private_data;//dev 则是操作的设备对象,可能是globalmem0或globalmem1

    switch (cmd) {
    case MEM_CLEAR:
        memset(dev->mem, 0, MEM_SIZE);
        printk(KERN_INFO "mem cleared\n");
        break;

    default :
        return - EINVAL;
    }
    return 0;
}
static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
    unsigned long p = *ppos;
    printk("read_ppos:%d\n", p);
    printk("read_filp->f_pos:%d\n", filp->f_pos);
    unsigned int count = size;
    int ret = 0;
    struct mem_dev *devp = filp->private_data;

    if (p >= MEM_SIZE) {
        printk(KERN_INFO "ppos >= mem size\n");
        return 0;
    }//越界
    if (count > MEM_SIZE - p) {
        count = MEM_SIZE - p;
    }//读取过多

    if (down_interruptible(&devp->sem))
        return - ERESTARTSYS;
    if (copy_to_user(buf, (void *)(devp->mem + p), count)) {
        printk(KERN_INFO "%s failed\n", __FUNCTION__);
        ret = - EFAULT;
    } else {
        *ppos += count;
        ret = count;
        printk(KERN_INFO "read %u bytes from %lu\n", count, p);
    }
    up(&devp->sem);
    return ret;
}

static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
    unsigned long p = *ppos;
    printk("write ppos:%d\n", p);
    printk("write filp->f_pos:%d\n", filp->f_pos);
    unsigned int count = size;
    int ret = 0;
    struct mem_dev *devp = filp->private_data;

    if (p >= MEM_SIZE) {
        printk(KERN_INFO "ppos >= mem size\n");
        return 0;
    }//越界
    if (count > MEM_SIZE - p) {
        count = MEM_SIZE - p;
    }//写过多
    if (down_interruptible(&devp->sem))//可中断的信号量,防止同时有两个进程向设备写入数据
        return - ERESTARTSYS;
    if (copy_from_user(devp->mem + p, buf, count)) {
        printk(KERN_INFO "%s failed\n", __FUNCTION__);
        ret = - EFAULT;
    } else {
        *ppos += count;
        ret = count;
        printk(KERN_INFO "written %u bytes, from ppos:%lu to:%lu\n", count, p, *ppos);
    }
    up(&devp->sem);//回复信号量
    return ret;
}

static loff_t mem_lseek(struct file *filp, loff_t offset, int orig)
{
    loff_t ret = 0;
    switch (orig) {
    case 0:
        if (offset < 0) {
            ret = - EINVAL;
            break;
        }
        if ((unsigned long)offset > MEM_SIZE) {
            ret = - EINVAL;
            break;
        }
        filp->f_pos = offset;
        ret = filp->f_pos;
        break;
    case 1:
        if ((filp->f_pos + offset) > MEM_SIZE) {
            ret = - EINVAL;
            break;
        }
        if ((filp->f_pos + offset) < 0) {
            ret = - EINVAL;
            break;
        }
        filp->f_pos += offset;
        ret = filp->f_pos;
        break;
    default:
        ret = - EINVAL;
        break;
    }
    return ret;
}

static const struct file_operations mem_fops = {
        .owner = THIS_MODULE,
        .llseek = mem_lseek,
        .read = mem_read,
        .write = mem_write,
        .release = mem_release,
        .open = mem_open,
        .compat_ioctl = mem_ioctl,
};

static void mem_setup_cdev(struct mem_dev *dev, int index)
{
    int err, devno = MKDEV(mem_major, index);//主,次设备号

    cdev_init(&dev->cdev, &mem_fops);//cdev的指针,fileoperations的指针
    dev->cdev.owner = THIS_MODULE;//TODO:THIS_MODULE的实现与作用

    sema_init(&dev->sem, 1);//信号量初始化,作为互斥体
    err = cdev_add(&dev->cdev, devno, 1);
    if (err)
        printk(KERN_NOTICE "Error %d adding globalmem cdev:%d", err, index);
}

static int globalmem_init(void)
{
    int result;
    dev_t devno = MKDEV(mem_major, 0);
    if (mem_major)
        result = register_chrdev_region(devno, 2, "globalmem");
    else {
        result = alloc_chrdev_region(&devno, 0 ,2, "globalmem");
        mem_major = MAJOR(devno);
    }
    if (result<0)
        return result;
    mem_devp = kmalloc(2*sizeof(struct mem_dev), GFP_KERNEL);//get free pages
    if (!mem_devp) {
        result = - ENOMEM;
        goto fail_malloc;
    }
    memset(mem_devp, 0, 2*sizeof(struct mem_dev));
    mem_setup_cdev(&mem_devp[0], 0);//次设备号
    mem_setup_cdev(&mem_devp[1], 1);
    mem_devp->offset = 0;
    mem_devp[1].offset = 0;
    printk(KERN_INFO "edition:%d init success_________________________________________________________\n", EDITION);
    return 0;//返回0或负值,如果不加return会返回17.弹出警告

fail_malloc:
    unregister_chrdev_region(devno, 2);
    return result;
}

static int globalmem_exit(void)
{
    cdev_del(&mem_devp->cdev);
    cdev_del(&(mem_devp[1].cdev));
    kfree(mem_devp);
    unregister_chrdev_region(MKDEV(mem_major, 0), 2);
    printk(KERN_INFO "exit success___________________________________________________________________\n");
    return 0;
}

MODULE_AUTHOR("huxy11");

module_param(mem_major, int, S_IRUGO);
module_init(globalmem_init);
module_exit(globalmem_exit

转载于:https://www.cnblogs.com/huxy11/p/6067064.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值