分享

【Tiny6410 And Linux】—(6.1)—LCD 驱动测试程序——原理 (转)

 WUCANADA 2013-05-14

【Tiny6410 And Linux】—(6.1)—LCD 驱动测试程序——原理

分类: Tiny6410 And Linux 303人阅读 评论(0) 收藏 举报

总算要涉及到LCD的驱动程序了,不过之前还是先弄一下LCD的测试程序的好(这里看的是国嵌的代码)!

之前在电脑上找了很久的LCD的驱动程序,也就是xxxfb.c,但是真心看不懂究竟是哪个驱动,但是共性的文件fbmem.c还是可以了解的,这 里就通过fbmem.c来进行简单的介绍吧,至于涉及到的相关的硬件设备(今天写的驱动测试没有涉及到硬件的具体信息),只能等以后再说了!

LCD 驱动测试程序:

首先介绍一下 Framebuffer:Framebuffer 从本质上讲是图形设备的硬件抽象。对开发者而言, Framebuffer 是一块显示缓存,往缓存中写入特定格式的数据就意味着向屏幕输出内容。通过不断的向 Framebuffer 中写入数据,显示控制器就会自动的从 Framebuffer 中取数据并显示出来,帧缓冲设备对应的设备文件为 /dev/fb0,如果系统有多个显示卡, Linux 下还可以支持多个帧缓冲设备,通常指向 /dev/fb0。真缓冲设备为标准的字符设备,主设备号为 29,次设备号从 0 到 31。

 

下面可以看看帧缓冲设备的架构:

 

 

1、帧缓冲设备结构体介绍:

 

Linux 内核使用 fb_info 结构来描述帧缓冲设备,定义在 include/linux/fb.h 中, fb_infor 结构常用成员:

 

  1. struct fb_info {  
  2.     int node;  
  3.     int flags;  
  4.     struct mutex lock;      /* Lock for open/release/ioctl funcs */  
  5.     struct mutex mm_lock;       /* Lock for fb_mmap and smem_* fields */  
  6.     struct fb_var_screeninfo var;   /* Current var */  
  7.     struct fb_fix_screeninfo fix;   /* Current fix */  
  8.     struct fb_monspecs monspecs;    /* Current Monitor specs */  
  9.     struct work_struct queue;   /* Framebuffer event queue */  
  10.     struct fb_pixmap pixmap;    /* Image hardware mapper */  
  11.     struct fb_pixmap sprite;    /* Cursor hardware mapper */  
  12.     struct fb_cmap cmap;        /* Current cmap */  
  13.     struct list_head modelist;      /* mode list */  
  14.     struct fb_videomode *mode;  /* current mode */  
  15.   
  16.     /* we need the PCI or similiar aperture base/size not  
  17.        smem_start/size as smem_start may just be an object  
  18.        allocated inside the aperture so may not actually overlap */  
  19.     struct apertures_struct {  
  20.         unsigned int count;  
  21.         struct aperture {  
  22.             resource_size_t base;  
  23.             resource_size_t size;  
  24.         } ranges[0];  
  25.     } *apertures;  
  26. };  

 

其中 fb_var_screeninfo 结构记录了用户可以修改的显示参数,包括屏幕分辨率等。 

  1. struct fb_var_screeninfo {  
  2.     __u32 xres;         /* visible resolution       */  
  3.     __u32 yres;  
  4.     __u32 xres_virtual;     /* virtual resolution       */  
  5.     __u32 yres_virtual;  
  6.     __u32 xoffset;          /* offset from virtual to visible */  
  7.     __u32 yoffset;          /* resolution           */  
  8.   
  9.     __u32 bits_per_pixel;       /* guess what           */  
  10.     __u32 grayscale;        /* != 0 Graylevels instead of colors */  
  11.   
  12.     struct fb_bitfield red;     /* bitfield in fb mem if true color, */  
  13.     struct fb_bitfield green;   /* else only length is significant */  
  14.     struct fb_bitfield blue;  
  15.     struct fb_bitfield transp;  /* transparency         */    
  16.   
  17.     __u32 nonstd;           /* != 0 Non standard pixel format */  
  18.   
  19.     __u32 activate;         /* see FB_ACTIVATE_*        */  
  20.   
  21.     __u32 height;           /* height of picture in mm    */  
  22.     __u32 width;            /* width of picture in mm     */  
  23.   
  24.     __u32 accel_flags;      /* (OBSOLETE) see fb_info.flags */  
  25.   
  26. };  

 

fb_fix_screeninfo 记录了用户不能修改的显示控制器参数,如显示缓存的物理地址等。

  1. struct fb_fix_screeninfo {  
  2.     char id[16];            /* identification string eg "TT Builtin" */  
  3.     unsigned long smem_start;   /* Start of frame buffer mem */  
  4.                     /* (physical address) */  
  5.     __u32 smem_len;         /* Length of frame buffer mem */  
  6.     __u32 type;         /* see FB_TYPE_*        */  
  7.     __u32 type_aux;         /* Interleave for interleaved Planes */  
  8.     __u32 visual;           /* see FB_VISUAL_*      */   
  9.     __u16 xpanstep;         /* zero if no hardware panning  */  
  10.     __u16 ypanstep;         /* zero if no hardware panning  */  
  11.     __u16 ywrapstep;        /* zero if no hardware ywrap    */  
  12.     __u32 line_length;      /* length of a line in bytes    */  
  13.     unsigned long mmio_start;   /* Start of Memory Mapped I/O   */  
  14.                     /* (physical address) */  
  15.     __u32 mmio_len;         /* Length of Memory Mapped I/O  */  
  16.     __u32 accel;            /* Indicate to driver which */  
  17.                     /*  specific chip/card we have  */  
  18.     __u16 reserved[3];      /* Reserved for future compatibility */  
  19. };  

 

还有就是 fb_ops 结构包含了对控制器进行操作的函数指针。

  1. struct fb_ops {  
  2.     /* open/release and usage marking */  
  3.     struct module *owner;  
  4.     int (*fb_open)(struct fb_info *info, int user);  
  5.     int (*fb_release)(struct fb_info *info, int user);  
  6.   
  7.     /* For framebuffers with strange non linear layouts or that do not  
  8.      * work with normal memory mapped access  
  9.      */  
  10.     ssize_t (*fb_read)(struct fb_info *info, char __user *buf,  
  11.                size_t count, loff_t *ppos);  
  12.     ssize_t (*fb_write)(struct fb_info *info, const char __user *buf,  
  13.                 size_t count, loff_t *ppos);  
  14.   
  15.     /* checks var and eventually tweaks it to something supported,  
  16.      * DO NOT MODIFY PAR */  
  17.     int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);  
  18.   
  19.     /* set the video mode according to info->var */  
  20.     int (*fb_set_par)(struct fb_info *info);  
  21.   
  22.     /* set color register */  
  23.     int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,  
  24.                 unsigned blue, unsigned transp, struct fb_info *info);  
  25.   
  26.     /* set color registers in batch */  
  27.     int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);  
  28.   
  29.     /* blank display */  
  30.     int (*fb_blank)(int blank, struct fb_info *info);  
  31.   
  32.     /* pan display */  
  33.     int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);  
  34.   
  35.     /* Draws a rectangle */  
  36.     void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);  
  37.     /* Copy data from area to another */  
  38.     void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);  
  39.     /* Draws a image to the display */  
  40.     void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);  
  41.   
  42.     /* Draws cursor */  
  43.     int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);  
  44.   
  45.     /* Rotates the display */  
  46.     void (*fb_rotate)(struct fb_info *info, int angle);  
  47.   
  48.     /* wait for blit idle, optional */  
  49.     int (*fb_sync)(struct fb_info *info);  
  50.   
  51. };  

 

2、控制器操作函数(fbmem.c):

 

这里首先讲述一下 LCD 测试程序的基本步骤:

 1)用 ioctl 操作取得当前屏幕的参数,根据屏幕参数可以计算屏幕缓冲区的大小。

2)将屏幕缓冲区映射到用户空间(mmap)。

3)映射后即可直接读写

 

这里用到 open、release、ioctl 等函数,主要介绍下 ioctl 函数吧,代码如下:

 

  1. static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)  
  2. {  
  3.     struct inode *inode = file->f_path.dentry->d_inode;  
  4.     int fbidx = iminor(inode);  
  5.     struct fb_info *info = registered_fb[fbidx];  
  6.   
  7.     return do_fb_ioctl(info, cmd, arg);  
  8. }  
  9.   
  10. static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,  
  11.             unsigned long arg)  
  12. {  
  13.     struct fb_ops *fb;  
  14.     struct fb_var_screeninfo var;  
  15.     struct fb_fix_screeninfo fix;  
  16.     struct fb_con2fbmap con2fb;  
  17.     struct fb_cmap cmap_from;  
  18.     struct fb_cmap_user cmap;  
  19.     struct fb_event event;  
  20.     void __user *argp = (void __user *)arg;  
  21.     long ret = 0;  
  22.   
  23.     switch (cmd) {  
  24.     case FBIOGET_VSCREENINFO:  
  25.         if (!lock_fb_info(info))  
  26.             return -ENODEV;  
  27.         var = info->var;  
  28.         unlock_fb_info(info);  
  29.   
  30.         ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;  
  31.         break;  
  32.     case FBIOPUT_VSCREENINFO:  
  33.         if (copy_from_user(&var, argp, sizeof(var)))  
  34.             return -EFAULT;  
  35.         if (!lock_fb_info(info))  
  36.             return -ENODEV;  
  37.         console_lock();  
  38.         info->flags |= FBINFO_MISC_USEREVENT;  
  39.         ret = fb_set_var(info, &var);  
  40.         info->flags &= ~FBINFO_MISC_USEREVENT;  
  41.         console_unlock();  
  42.         unlock_fb_info(info);  
  43.         if (!ret && copy_to_user(argp, &var, sizeof(var)))  
  44.             ret = -EFAULT;  
  45.         break;  
  46.     case FBIOGET_FSCREENINFO:  
  47.         if (!lock_fb_info(info))  
  48.             return -ENODEV;  
  49.         fix = info->fix;  
  50.         unlock_fb_info(info);  
  51.   
  52.         ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;  
  53.         break;  
  54.     case FBIOPUTCMAP:  
  55.         if (copy_from_user(&cmap, argp, sizeof(cmap)))  
  56.             return -EFAULT;  
  57.         ret = fb_set_user_cmap(&cmap, info);  
  58.         break;  
  59.     case FBIOGETCMAP:  
  60.         if (copy_from_user(&cmap, argp, sizeof(cmap)))  
  61.             return -EFAULT;  
  62.         if (!lock_fb_info(info))  
  63.             return -ENODEV;  
  64.         cmap_from = info->cmap;  
  65.         unlock_fb_info(info);  
  66.         ret = fb_cmap_to_user(&cmap_from, &cmap);  
  67.         break;  
  68.     case FBIOPAN_DISPLAY:  
  69.         if (copy_from_user(&var, argp, sizeof(var)))  
  70.             return -EFAULT;  
  71.         if (!lock_fb_info(info))  
  72.             return -ENODEV;  
  73.         console_lock();  
  74.         ret = fb_pan_display(info, &var);  
  75.         console_unlock();  
  76.         unlock_fb_info(info);  
  77.         if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))  
  78.             return -EFAULT;  
  79.         break;  
  80.     case FBIO_CURSOR:  
  81.         ret = -EINVAL;  
  82.         break;  
  83.     case FBIOGET_CON2FBMAP:  
  84.         if (copy_from_user(&con2fb, argp, sizeof(con2fb)))  
  85.             return -EFAULT;  
  86.         if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)  
  87.             return -EINVAL;  
  88.         con2fb.framebuffer = -1;  
  89.         event.data = &con2fb;  
  90.         if (!lock_fb_info(info))  
  91.             return -ENODEV;  
  92.         event.info = info;  
  93.         fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);  
  94.         unlock_fb_info(info);  
  95.         ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;  
  96.         break;  
  97.     case FBIOPUT_CON2FBMAP:  
  98.         if (copy_from_user(&con2fb, argp, sizeof(con2fb)))  
  99.             return -EFAULT;  
  100.         if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)  
  101.             return -EINVAL;  
  102.         if (con2fb.framebuffer < 0 || con2fb.framebuffer >= FB_MAX)  
  103.             return -EINVAL;  
  104.         if (!registered_fb[con2fb.framebuffer])  
  105.             request_module("fb%d", con2fb.framebuffer);  
  106.         if (!registered_fb[con2fb.framebuffer]) {  
  107.             ret = -EINVAL;  
  108.             break;  
  109.         }  
  110.         event.data = &con2fb;  
  111.         if (!lock_fb_info(info))  
  112.             return -ENODEV;  
  113.         event.info = info;  
  114.         ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP, &event);  
  115.         unlock_fb_info(info);  
  116.         break;  
  117.     case FBIOBLANK:  
  118.         if (!lock_fb_info(info))  
  119.             return -ENODEV;  
  120.         console_lock();  
  121.         info->flags |= FBINFO_MISC_USEREVENT;  
  122.         ret = fb_blank(info, arg);  
  123.         info->flags &= ~FBINFO_MISC_USEREVENT;  
  124.         console_unlock();  
  125.         unlock_fb_info(info);  
  126.         break;  
  127.     default:  
  128.         if (!lock_fb_info(info))  
  129.             return -ENODEV;  
  130.         fb = info->fbops;  
  131.         if (fb->fb_ioctl)  
  132.             ret = fb->fb_ioctl(info, cmd, arg);  
  133.         else  
  134.             ret = -ENOTTY;  
  135.         unlock_fb_info(info)    }  
  136.     return ret;  
  137. }  

 

如果对 ioctl 函数有所理解,可以很快看到 cmd 变量 FBIOGET_VSCREENINFO,这个变量就可以获得设备体 /dev/fb0 的相关的详细信息!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多