第18章LCD设备驱动之帧缓冲设备驱动的读写、mmap和ioctl函数

18.8 帧缓冲设备驱动的读写、mmap和ioctl函数

     虽然帧缓冲设备的 file_operations 中的成员函数,即文件操作函数由内核在 fbmem.c 文件中实现,一般不再需要驱动工程师修改,但分析这些函数对于巩固字符设备驱动的知识以及加深对帧缓冲设备驱动的理解是大有好处的。

    代码清单 18.12所示为 帧缓冲设备驱动的文件操作读写函数的源代码。

代码清单 18.12 帧缓冲设备驱动的读写函数

static ssize_t
fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
        unsigned long p = *ppos;
        struct fb_info *info = file_fb_info(file);
        u8 *buffer, *dst;
        u8 __iomem *src;
        int c, cnt = 0, err = 0;
        unsigned long total_size;

        if (!info || ! info->screen_base)
                return -ENODEV;

        if (info->state != FBINFO_STATE_RUNNING)
                return -EPERM;

        if (info->fbops->fb_read)
                return info->fbops->fb_read(info, buf, count, ppos);


        total_size = info->screen_size;

        if (total_size == 0)
                total_size = info->fix.smem_len;

        if (p >= total_size)
                return 0;

        if (count >= total_size)
                count = total_size;

        if (count + p > total_size)
                count = total_size - p;

        buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count, GFP_KERNEL);
        if (!buffer)
                return -ENOMEM;

        src = (u8 __iomem *) (info->screen_base + p);

        if (info->fbops->fb_sync)
                info->fbops->fb_sync(info);


        while (count) {
                c  = (count > PAGE_SIZE) ? PAGE_SIZE : count;
                dst = buffer;
                fb_memcpy_fromfb(dst, src, c);
                dst += c;
                src += c;

                if (copy_to_user(buf, buffer, c)) {
                        err = -EFAULT;
                        break;
                }
                *ppos += c;
                buf += c;
                cnt += c;
                count -= c;
        }

        kfree(buffer);

        return (err) ? err : cnt;

}

static ssize_t
fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
        unsigned long p = *ppos;
        struct fb_info *info = file_fb_info(file);
        u8 *buffer, *src;
        u8 __iomem *dst;
        int c, cnt = 0, err = 0;
        unsigned long total_size;

        if (!info || !info->screen_base)
                return -ENODEV;

        if (info->state != FBINFO_STATE_RUNNING)
                return -EPERM;

        if (info->fbops->fb_write)
           

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值