字符设备驱动第五课----读写

概念描述

【内核空间往用户空间写】

“内核空间往用户空间写”这句话站在用户空间的角度就是“读”

1.直接使用

对于32位系统,虚拟空间的低1G一般为内核空间,高3G一般为用户空间。对于用户空间是这样使用的:
用用程序要执行时现搬到内存;
优先级高的可以优先占用内存;
假如内存空间不够用,系统会将低优先级的先“下架”,等有空闲再重新“上架”,假设一个运用程序很大,现在执行性到某个位置,内核还没有将其完全加载进内存,此时来了一个更高优先级的进程,由于空间不够用,内核不得不让当前运行的低优先级程序“下架”,让新来的“上架”。新进程执行完后,要将之前下架的进程重新上架继续执行。那么问题就来了,怎么接续原来的状态往下走呢?难道要完全从头再重新执行一遍?故此memcpy()这个函数要慎用。一般不推荐用。
memcpy();

2.使用内核提供的专用函数

/*
 * 功能:拷贝给用户
 * 参数:
 *  @void __user *to :       - 用户空间首地址,目标
 *  @const void *from:       - 内核空间首地址,源
 *  @unsigned long n:        - 要拷贝的大小
 * 返回值:成功:0  失败:n
 */
long copy_to_user(void __user *to, const void *from, unsigned long n);

【内核空间往用户空间读】

“内核空间往用户空间读”这句话站在用户空间的角度就是“写”

1.直接使用

会存在问题,如上,慎用。
memcpy();

2.使用内核提供的专用函数

/*
 * 功能:从用户拷贝到内核
 * 参数:
 *  @void *to:                   - 内核空间首地址,目标
 *  @const void __user *from:    - 用户空间首地址,源
 *  @unsigned long n :           - 要拷贝的大小
 * 返回值:成功:0    失败:n
 */
long copy_from_user(void *to, const void __user * from, unsigned long n);

工程实例

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/errno.h>

#include <asm/current.h>
#include <linux/sched.h>

#include <linux/uaccess.h>

#include <linux/device.h>
static struct class *cls = NULL;

static int major = 0;
static int minor = 0;
const  int count = 6;

#define DEVNAME "demo"

static struct cdev *demop = NULL;

#define KMAX 1024
static int counter = 0;
static char kbuf[KMAX];

//打开设备
static int demo_open(struct inode *inode, struct file *filp)
{
    //get command and pid
    printk(KERN_INFO "(%s:pid=%d), %s : %s : %d\n",
        current->comm, current->pid, __FILE__, __func__, __LINE__);
    //get major and minor from inode
    printk(KERN_INFO "(major=%d, minor=%d), %s : %s : %d\n",
        imajor(inode), iminor(inode), __FILE__, __func__, __LINE__);

    memset(kbuf, 0, KMAX);
    counter = 0;

    return 0;
}

//关闭设备
static int demo_release(struct inode *inode, struct file *filp)
{
    //get command and pid
    printk(KERN_INFO "(%s:pid=%d), %s : %s : %d\n",
        current->comm, current->pid, __FILE__, __func__, __LINE__);
    //get major and minor from inode
    printk(KERN_INFO "(major=%d, minor=%d), %s : %s : %d\n",
        imajor(inode), iminor(inode), __FILE__, __func__, __LINE__);

    return 0;
}

//读设备
//ssize_t read(int fd, void *buf, size_t count)
static ssize_t demo_read(struct file *filp, char __user *buf, size_t size, loff_t *offset)
{
    struct inode *inode = filp->f_path.dentry->d_inode;
    //get command and pid
    printk(KERN_INFO "(%s:pid=%d), %s : %s : %d\n",
        current->comm, current->pid, __FILE__, __func__, __LINE__);
    //get major and minor from inode
    printk(KERN_INFO "(major=%d, minor=%d), %s : %s : %d\n",
        imajor(inode), iminor(inode), __FILE__, __func__, __LINE__);

    if(counter < size){
        size = counter;
    }

    if(copy_to_user(buf, kbuf, size)){
        return -EAGAIN;
    }

    counter = 0;

    return size;
}

//写设备
static ssize_t demo_write(struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{
    struct inode *inode = filp->f_path.dentry->d_inode;
    //get command and pid
    printk(KERN_INFO "(%s:pid=%d), %s : %s : %d\n",
        current->comm, current->pid, __FILE__, __func__, __LINE__);
    //get major and minor from inode
    printk(KERN_INFO "(major=%d, minor=%d), %s : %s : %d\n",
        imajor(inode), iminor(inode), __FILE__, __func__, __LINE__);

    if(size > KMAX){
        return -ENOMEM;
    }

    if(copy_from_user(kbuf, buf, size)){
        return -EAGAIN;
    }

    counter = size;

    return counter;
}

static struct file_operations fops = {
    .owner  = THIS_MODULE,
    .open   = demo_open,
    .release= demo_release,
    .read   = demo_read,
    .write  = demo_write,
};

static int __init demo_init(void)
{
    dev_t devnum;
    int ret, i;

    struct device *devp = NULL;

    //get command and pid
    printk(KERN_INFO "(%s:pid=%d), %s : %s : %d\n",
        current->comm, current->pid, __FILE__, __func__, __LINE__);

    //1. alloc cdev obj
    demop = cdev_alloc();
    if(NULL == demop){
        return -ENOMEM;
    }

    //2. init cdev obj
    cdev_init(demop, &fops);

    ret = alloc_chrdev_region(&devnum, minor, count, DEVNAME);
    if(ret){
        goto ERR_STEP;
    }
    major = MAJOR(devnum);

    //3. register cdev obj
    ret = cdev_add(demop, devnum, count);
    if(ret){
        goto ERR_STEP1;
    }

    cls = class_create(THIS_MODULE, DEVNAME);
    if(IS_ERR(cls)){
        ret = PTR_ERR(cls);
        goto ERR_STEP1;
    }

    for(i = minor; i < (count+minor); i++){
        devp = device_create(cls, NULL, MKDEV(major, i), NULL, "%s%d", DEVNAME, i);
        if(IS_ERR(devp)){
            ret = PTR_ERR(devp);
            goto ERR_STEP2;
        }
    }

    //get command and pid
    printk(KERN_INFO "(%s:pid=%d), %s : %s : %d - ok.\n",
        current->comm, current->pid, __FILE__, __func__, __LINE__);
    return 0;

ERR_STEP2:
    for(--i; i >= minor; i--){
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);

ERR_STEP1:
    unregister_chrdev_region(devnum, count);

ERR_STEP:
    cdev_del(demop);

    //get command and pid
    printk(KERN_INFO "(%s:pid=%d), %s : %s : %d - fail.\n",
        current->comm, current->pid, __FILE__, __func__, __LINE__);
    return ret;
}

static void __exit demo_exit(void)
{
    int i;
    //get command and pid
    printk(KERN_INFO "(%s:pid=%d), %s : %s : %d - leave.\n",
        current->comm, current->pid, __FILE__, __func__, __LINE__);

    for(i=minor; i < (count+minor); i++){
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);

    unregister_chrdev_region(MKDEV(major, minor), count);

    cdev_del(demop);
}

module_init(demo_init);
module_exit(demo_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Farsight");
MODULE_DESCRIPTION("Demo for kernel module");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxgui1992

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值