第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)
                return info->fbops->fb_write(info, buf, count, ppos);


        total_size = info->screen_size;

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

        if (p > total_size)
                return -EFBIG;

        if (count > total_size) {
                err = -EFBIG;
                count = total_size;
        }

        if (count + p > total_size) {
                if (!err)
                        err = -ENOSPC;

                count = total_size - p;
        }

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

        dst = (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;
                src = buffer;
                if (copy_from_user(src, buf, c)) {
                        err = -EFAULT;
                        break;
                }

                fb_memcpy_tofb(dst, src, c);
                dst += c;
                src += c;
                *ppos += c;
                buf += c;
                cnt += c;
                count -= c;
        }

        kfree(buffer);

        return (cnt) ? cnt : err;
}

        file_operations 中的 mmap()函数非常关键,将显示缓冲区映射到用户空间,从而使得用户空间可以直接操作显示缓冲区而省去一次用户空间到内核空间的内存复制过程,提高效率,其源代码如代码清单 18.13 所示。

代码清单 18.13 帧缓冲设备驱动的 mmap 函数

static int
fb_mmap(struct file *file, struct vm_area_struct * vma)
{
        struct fb_info *info = file_fb_info(file);
        struct fb_ops *fb;
        unsigned long mmio_pgoff;
        unsigned long start;
        u32 len;

        if (!info)
                return -ENODEV;
        fb = info->fbops;
        if (!fb)
                return -ENODEV;
        mutex_lock(&info->mm_lock);
        if (fb->fb_mmap) {
                int res;
                res = fb->fb_mmap(info, vma);
                mutex_unlock(&info->mm_lock);
                return res;
        }

        /*
         * Ugh. This can be either the frame buffer mapping, or
         * if pgoff points past it, the mmio mapping.
         */
        start = info->fix.smem_start;
        len = info->fix.smem_len;
        mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT;
        if (vma->vm_pgoff >= mmio_pgoff) {
                if (info->var.accel_flags) {
                        mutex_unlock(&info->mm_lock);
                        return -EINVAL;
                }

                vma->vm_pgoff -= mmio_pgoff;
                start = info->fix.mmio_start;
                len = info->fix.mmio_len;
        }
        mutex_unlock(&info->mm_lock);

        vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
        fb_pgprotect(file, vma, start);

        return vm_iomap_memory(vma, start, len);
}

        fb_ioctl()函数最终实现对用户 I/O 控制命令的执行,这些命令包括 FBIOGET_VSCREENINFO(获得可变的屏幕参数)、FBIOPUT_VSCREENINFO(设置可变的屏幕参数)、FBIOGET _FSCREENINFO(获得固定的屏幕参数设置,注意,固定的屏幕参数不能由用户设置)、FBIOPUTCMAP(设置颜色表)、FBIOGETCMAP(获得颜色表)等。代码清单 18.14 所示为帧缓冲设备 ioctl()函数的源代码。

代码清单 18.14 帧缓冲设备驱动的 ioctl 函数


static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
                        unsigned long arg, struct file *file)
{
        struct fb_ops *fb;
        struct fb_var_screeninfo var;
        struct fb_fix_screeninfo fix;
        struct fb_con2fbmap con2fb;
        struct fb_cmap cmap_from;
        struct fb_cmap_user cmap;
        struct fb_event event;
        void __user *argp = (void __user *)arg;
        long ret = 0;

        memset(&var, 0, sizeof(var));
        memset(&fix, 0, sizeof(fix));
        memset(&con2fb, 0, sizeof(con2fb));
        memset(&cmap_from, 0, sizeof(cmap_from));
        memset(&cmap, 0, sizeof(cmap));
        memset(&event, 0, sizeof(event));

        switch (cmd) {
        case FBIOGET_VSCREENINFO:
                if (!lock_fb_info(info))
                        return -ENODEV;
                var = info->var;
                unlock_fb_info(info);

                ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
                break;
        case FBIOPUT_VSCREENINFO:
                if (copy_from_user(&var, argp, sizeof(var)))
                        return -EFAULT;
                console_lock();
                if (!lock_fb_info(info)) {
                        console_unlock();
                        return -ENODEV;
                }
                info->flags |= FBINFO_MISC_USEREVENT;
                ret = fb_set_var(info, &var);
                info->flags &= ~FBINFO_MISC_USEREVENT;
                unlock_fb_info(info);
                console_unlock();
                if (!ret && copy_to_user(argp, &var, sizeof(var)))
                        ret = -EFAULT;
                break;
        case FBIOGET_FSCREENINFO:
                if (!lock_fb_info(info))
                        return -ENODEV;
                fix = info->fix;
                unlock_fb_info(info);

                ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
                break;
        case FBIOPUTCMAP:
                if (copy_from_user(&cmap, argp, sizeof(cmap)))
                        return -EFAULT;
                ret = fb_set_user_cmap(&cmap, info);
                break;
        case FBIOGETCMAP:
                if (copy_from_user(&cmap, argp, sizeof(cmap)))
                        return -EFAULT;
                if (!lock_fb_info(info))
                        return -ENODEV;
                cmap_from = info->cmap;
                unlock_fb_info(info);
                ret = fb_cmap_to_user(&cmap_from, &cmap);
                break;
        case FBIOPAN_DISPLAY:
                if (copy_from_user(&var, argp, sizeof(var)))
                        return -EFAULT;
                console_lock();
                if (!lock_fb_info(info)) {
                        console_unlock();
                        return -ENODEV;
                }
                ret = fb_pan_display(info, &var);
                unlock_fb_info(info);
                console_unlock();
                if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
                        return -EFAULT;
                break;
        case FBIO_CURSOR:
                ret = -EINVAL;
                break;
        case FBIOGET_CON2FBMAP:
                if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
                        return -EFAULT;
                if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
                        return -EINVAL;
                con2fb.framebuffer = -1;
                event.data = &con2fb;
                if (!lock_fb_info(info))
                        return -ENODEV;
                event.info = info;
                fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
                unlock_fb_info(info);
                ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
                break;
        case FBIOPUT_CON2FBMAP:
                if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
                        return -EFAULT;
                if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
                        return -EINVAL;
                if (con2fb.framebuffer >= FB_MAX)
                        return -EINVAL;
                if (!registered_fb[con2fb.framebuffer])
                        request_module("fb%d", con2fb.framebuffer);
                if (!registered_fb[con2fb.framebuffer]) {
                        ret = -EINVAL;
                        break;
                }
                event.data = &con2fb;
                console_lock();
                if (!lock_fb_info(info)) {
                        console_unlock();
                        return -ENODEV;
                }
                event.info = info;
                ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP, &event);
                unlock_fb_info(info);
                console_unlock();
                break;
        case FBIOBLANK:
                console_lock();
                if (!lock_fb_info(info)) {
                        console_unlock();
                        return -ENODEV;
                }
                info->flags |= FBINFO_MISC_USEREVENT;
                ret = fb_blank(info, arg);
                info->flags &= ~FBINFO_MISC_USEREVENT;
                unlock_fb_info(info);
                console_unlock();
                break;
        default:
                fb = info->fbops;
                if (fb->fb_ioctl_v2)
                        ret = fb->fb_ioctl_v2(info, cmd, arg, file);
                else if (fb->fb_ioctl)
                        ret = fb->fb_ioctl(info, cmd, arg);
                else
                        ret = -ENOTTY;
        }
        return ret;
}


static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
        struct fb_info *info = file_fb_info(file);

        if (!info)
                return -ENODEV;
        return do_fb_ioctl(info, cmd, arg, file);
}


猜你喜欢

转载自blog.csdn.net/xiezhi123456/article/details/80756501
今日推荐