《Linux驱动开发详解》——LCD设备驱动重要数据结构及驱动框架

来源:互联网 发布:软件产品质量缺陷等级 编辑:程序博客网 时间:2024/06/06 12:57

核心文件:/drivers/video/fbmem.c

18.2.3.Linux帧缓冲相关数据结构与函数

         1. fb_info结构体(最关键)

[cpp] view plain copy
  1. /* struct fb_info 结构体 */  
  2. struct fb_info {  
  3.     int node;  
  4.     int flags;  
  5.     struct mutex lock;        /* 用于 open/release/ioctl的锁 */  
  6.     struct mutex mm_lock;        /* Lock for fb_mmap and smem_* fields */  
  7.     struct fb_var_screeninfo var;    /* Current var */  
  8.     struct fb_fix_screeninfo fix;    /* Current fix */  
  9.     struct fb_monspecs monspecs;    /* 显示器标准 */  
  10.     struct work_struct queue;    /* Framebuffer event queue 帧缓冲事件队列 */  
  11.     struct fb_pixmap pixmap;    /* Image hardware mapper 图像硬件mapper */  
  12.     struct fb_pixmap sprite;    /* Cursor hardware mapper 光标硬件mapper */  
  13.     struct fb_cmap cmap;        /* Current cmap 目前颜色表 */  
  14.     struct list_head modelist;      /* mode list */  
  15.     struct fb_videomode *mode;    /* current mode 目前video模式  */  
  16.   
  17. #ifdef CONFIG_FB_BACKLIGHT  
  18.     /* assigned backlight device 对应的背光设备 */  
  19.     /* set before framebuffer registration,  
  20.        remove after unregister */  
  21.     struct backlight_device *bl_dev;  
  22.   
  23.     /* Backlight level curve 背光调整 */  
  24.     struct mutex bl_curve_mutex;      
  25.     u8 bl_curve[FB_BACKLIGHT_LEVELS];  
  26. #endif  
  27. #ifdef CONFIG_FB_DEFERRED_IO  
  28.     struct delayed_work deferred_work;  
  29.     struct fb_deferred_io *fbdefio;  
  30. #endif  
  31.   
  32.     struct fb_ops *fbops;         /* 帧缓冲操作 */  
  33.     struct device *device;        /* This is the parent 父设备 */  
  34.     struct device *dev;        /* This is this fb device fb设备 */  
  35.     int class_flag;                    /* private sysfs flags */  
  36. #ifdef CONFIG_FB_TILEBLITTING  
  37.     struct fb_tile_ops *tileops;    /* Tile Blitting */  
  38. #endif  
  39.     char __iomem *screen_base;    /* Virtual address */  
  40.     unsigned long screen_size;    /* Amount of ioremapped VRAM or 0 */   
  41.     void *pseudo_palette;        /* Fake palette of 16 colors */   
  42. #define FBINFO_STATE_RUNNING    0  
  43. #define FBINFO_STATE_SUSPENDED    1  
  44.     u32 state;            /* Hardware state i.e suspend */  
  45.     void *fbcon_par;                /* fbcon use-only private area */  
  46.     /* From here on everything is device dependent */  
  47.     void *par;  
  48.     /* we need the PCI or similiar aperture base/size not 
  49.        smem_start/size as smem_start may just be an object 
  50.        allocated inside the aperture so may not actually overlap */  
  51.     resource_size_t aperture_base;  
  52.     resource_size_t aperture_size;  
  53. };  

        2.fb_ops结构体

                 fb_info成员变量fb_ops为指向底层操作的函数指针,这些函数需要驱动程序猿自己编写。

[cpp] view plain copy
  1. /* fb_ops 结构体 */  
  2. struct fb_ops {  
  3.     /* open/release and usage marking */  
  4.     struct module *owner;  
  5.     int (*fb_open)(struct fb_info *info, int user);  
  6.     int (*fb_release)(struct fb_info *info, int user);  
  7.   
  8.     /* 对于非线性布局的/常规内存映射无法工作的帧缓冲设备*/  
  9.     ssize_t (*fb_read)(struct fb_info *info, char __user *buf,size_t count, loff_t *ppos);  
  10.     ssize_t (*fb_write)(struct fb_info *info, const char __user *buf,size_t count, loff_t *ppos);  
  11.   
  12.     /* 调整可变参数,并调整到支持的值 */  
  13.     int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);  
  14.   
  15.     /* 根据info->var设置video模式 */  
  16.     int (*fb_set_par)(struct fb_info *info);  
  17.   
  18.     /* set color register */  
  19.     int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,unsigned blue, unsigned transp, struct fb_info *info);  
  20.   
  21.     /* 批量设置color寄存器,设置颜色表 */  
  22.     int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);  
  23.   
  24.     /* 显示空白 */  
  25.     int (*fb_blank)(int blank, struct fb_info *info);  
  26.   
  27.     /* pan display */  
  28.     int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);  
  29.   
  30.     /* 矩形填充 */  
  31.     void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);  
  32.     /* 数据复制 */  
  33.     void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);  
  34.     /* 图形填充 */  
  35.     void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);  
  36.   
  37.     /* 绘制光标 */  
  38.     int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);  
  39.   
  40.     /* 旋转显示 */  
  41.     void (*fb_rotate)(struct fb_info *info, int angle);  
  42.   
  43.     /* 等待blit空闲(可选) */  
  44.     int (*fb_sync)(struct fb_info *info);  
  45.   
  46.     /* fb特定的ioctl(可选) */  
  47.     int (*fb_ioctl)(struct fb_info *info, unsigned int cmd,unsigned long arg);  
  48.   
  49.     /* 处理32位的compat ioctl(可选) */  
  50.     int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,unsigned long arg);  
  51.   
  52.     /* fb特定的mmap */  
  53.     int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);  
  54.   
  55.     /* get capability given var */  
  56.     void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,struct fb_var_screeninfo *var);  
  57.   
  58.     /* teardown any resources to do with this framebuffer */  
  59.     void (*fb_destroy)(struct fb_info *info);  
  60. };  
      fb_ops 的 fb_check_var() 成员函数用于检查可以修改的屏幕参数并调整到合适的值,而 fb_set_par() 则使得用户设置的屏幕参数在硬件上有效。

       3. fb_var_screeninfo 和 fb_fix_screeninfo 结构体

               fb_var_screeninfo 记录用户可以修改的显示控制器参数,包括屏幕分辨率和每个像素点的比特数。  

               fb_fix_screeninfo 记录用户不能修改的显示控制器参数,包括屏幕缓冲区的物理地址、长度。当对帧缓冲舍内进行映射操作的时候,就是从fb_fix_screeninfo中取得缓冲区的物理地址。上述结构体都需要在驱动程序中初始化和设置。

[cpp] view plain copy
  1. /* fb_var_screeninfo 结构体 */  
  2. struct fb_var_screeninfo {  
  3.     __u32 xres;            /* visible resolution  可见度解析      */  
  4.     __u32 yres;  
  5.     __u32 xres_virtual;        /* virtual resolution  虚拟度解析      */  
  6.     __u32 yres_virtual;  
  7.     __u32 xoffset;            /* offset from virtual to visible 虚拟到可见之间的偏移 */  
  8.     __u32 yoffset;            /* resolution            */  
  9.   
  10.     __u32 bits_per_pixel;        /* guess what            */  
  11.     __u32 grayscale;        /* 非0时指灰度 */  
  12.   
  13.     /* fb缓存的R/G/B位域 */  
  14.     struct fb_bitfield red;        /* bitfield in fb mem if true color, */  
  15.     struct fb_bitfield green;      
  16.     struct fb_bitfield blue;  
  17.     struct fb_bitfield transp;    /* transparency 透明度           */      
  18.   
  19.     __u32 nonstd;            /* != 0 非标准像素格式 */  
  20.   
  21.     __u32 activate;            /* see FB_ACTIVATE_*        */  
  22.   
  23.     __u32 height;            /* height of picture in mm    */  
  24.     __u32 width;            /* width of picture in mm     */  
  25.   
  26.     __u32 accel_flags;        /* (OBSOLETE) see fb_info.flags */  
  27.   
  28.     /* 定时:除了pixclock本身外,其他的都以像素时钟为单位 */  
  29.     __u32 pixclock;            /* 像素时钟(皮秒) */  
  30.     __u32 left_margin;        /* 行切换:从同步到绘图之间的延迟    */  
  31.     __u32 right_margin;        /* 行切换:从绘图到同步之间的延迟    */  
  32.     __u32 upper_margin;        /* 帧切换:从同步到绘图之间的延迟    */  
  33.     __u32 lower_margin;        /* 帧切换:从绘图到同步之间的延迟    */     
  34.     __u32 hsync_len;        /*   水平同步的长度  */  
  35.     __u32 vsync_len;        /* 垂直同步的长度  */  
  36.     __u32 sync;            /* see FB_SYNC_*        */  
  37.     __u32 vmode;            /* see FB_VMODE_*        */  
  38.     __u32 rotate;            /* 顺时针旋转的角度 */  
  39.     __u32 reserved[5];        /* Reserved for future compatibility */  
  40. };  

[cpp] view plain copy
  1. /* fb_fix_screeninfo 结构体*/  
  2. struct fb_fix_screeninfo {  
  3.     char id[16];            /* identification string eg "TT Builtin" */  
  4.     unsigned long smem_start;    /* fb缓存的开始位置(物理地址) */  
  5.     __u32 smem_len;            /* fb缓存的长度 */  
  6.     __u32 type;            /* see FB_TYPE_*        */  
  7.     __u32 type_aux;            /* Interleave for interleaved Planes */  
  8.     /* visual:记录屏幕使用的色彩模式 ①Monochrome(FB_VISUAL_MON01、FB_VISUAL_MON10) 每个像素都是黑或白 
  9.          ②Pseudo color(FB_VISUAL_PSEDOCOLOR、FB_VISUAL_STATIC_PSEUDOCOLOR), 即伪彩色,采用索引颜色显示 
  10.          ③True color(FB_VISUAL_TRUECOLOR),即彩色,分成红绿蓝三基色 
  11.          ④Direct color(FB_VISUAL_DIRECTCOLOR)每个像素偃师市也是由红绿蓝组成,不过每个颜色值是个索引,需要查表 
  12.          ⑤Grayscale displays,灰度显示,红、绿、蓝的值都一样 
  13.      */  
  14.     __u32 visual;            /* see FB_VISUAL_* 记录屏幕使用的色彩模式 */   
  15.     __u16 xpanstep;            /* zero if no hardware panning  */  
  16.     __u16 ypanstep;            /* zero if no hardware panning  */  
  17.     __u16 ywrapstep;        /* zero if no hardware ywrap    */  
  18.     __u32 line_length;        /* 1行的字节数    */  
  19.     unsigned long mmio_start;    /*  内存映射IO长度 */  
  20.     __u32 mmio_len;            /* 内存映射IO长度 */  
  21.     __u32 accel;            /* Indicate to driver which    */  
  22.     __u16 reserved[3];        /* Reserved for future compatibility */  
  23. };  
      
          4.fb_bitfield 结构体

                 fb_bitfield描述每一像素显示缓冲区的组织方式,包含位域偏移、位域长度和MSB(最高有效位)指示。

[cpp] view plain copy
  1. /* fb_bitfield 结构体 */  
  2. struct fb_bitfield {  
  3.     __u32 offset;            /* beginning of bitfield    */  
  4.     __u32 length;            /* length of bitfield        */  
  5.     __u32 msb_right;        /* != 0 : Most significant bit MSB在右边 */   
  6. };  

        5.fb_cmap结构体

                fb_cmap结构体记录设备无关的颜色表信息,用户空间可以通过 ioctl() 的 FBIOGETCMAP 和 FBIOPUTCMAP 命令读取或设定 颜色表。

[cpp] view plain copy
  1. /* fb_cmap 结构体 */  
  2. struct fb_cmap {  
  3.     __u32 start;            /* First entry  第一个元素入口  */  
  4.     __u32 len;            /* Number of entries 原色数量 */  
  5.     __u16 *red;            /* Red values  RGB透明度  */  
  6.     __u16 *green;  
  7.     __u16 *blue;  
  8.     __u16 *transp;            /* transparency, can be NULL */  
  9. };  

   
[cpp] view plain copy
  1. /* 用户空间获取颜色表的例程 
  2.  * 若BPP为8位,颜色表长度为256 
  3.  * 若BPP为4位,颜色表长度为16,否则颜色表长度为0 
  4.  * 因为对于BPP大于等于16的情况,使用颜色表超不划算 
  5. */  
  6. /* 读取颜色表 */  
  7. if( (vinfo.bits_per_pixel == 8) || (vinfo.bits_per_pixel == 4) ){  
  8.     screencols = (vinfo.bits_per_pixel == 8) ? 256 : 16; /* 颜色表的大小 */  
  9.     int loopc;  
  10.     startcmap = new fb_cmap;  
  11.     startcmap->start = 0;  
  12.     startcmap->len = screencols;  
  13.     /* 分配颜色表的内存 */  
  14.     startcmap->red = (unsigned short int*)malloc(sizeof(unsigned short int) * screencols);  
  15.     startcmap->green = (unsigned short int*)malloc(sizeof(unsigned short int) * screencols);  
  16.     startcmap->blue = (unsigned short int*)malloc(sizeof(unsigned short int) * screencols);  
  17.     startcmap->transp = (unsigned short int*)malloc(sizeof(unsigned short int) * screencols);;  
  18.     /* 获取颜色表 */  
  19.     ioctl(df, FBIOGETCMAP, startcmap);  
  20.     for( loopc = 0; loopc < screencols; loopc++ ){  
  21.         screenclut[loopc] = qRgb(startcmap->red[loopc] >> 8, startcmap->green[loopc] >> 8, startcmap->blue[loopc] >> 8)  
  22.     }  
  23. }   
          对于一个256色(BPP=8)的800*600的图像,若使用红绿蓝分别用一个字节描述,则需要800*600*3=1 440 000Byte,若使用颜色表,需要800*600*1+256*3=480 768Byte的空间。

        7.文件操作结构体

[cpp] view plain copy
  1. /* fb_ops */  
  2. static const struct file_operations fb_fops = {  
  3.     .owner =    THIS_MODULE,  
  4.     .read =        fb_read,  
  5.     .write =    fb_write,  
  6.     .unlocked_ioctl = fb_ioctl,  
  7. #ifdef CONFIG_COMPAT  
  8.     .compat_ioctl = fb_compat_ioctl,  
  9. #endif  
  10.     .mmap =        fb_mmap,  
  11.     .open =        fb_open,  
  12.     .release =    fb_release,  
  13. #ifdef HAVE_ARCH_FB_UNMAPPED_AREA  
  14.     .get_unmapped_area = get_fb_unmapped_area,  
  15. #endif  
  16. #ifdef CONFIG_FB_DEFERRED_IO  
  17.     .fsync =    fb_deferred_io_fsync,  
  18. #endif  
  19. };  

        7.注册于注销帧缓冲设备

                     int  register_framebuffer(struct fb_info *fb_info); /* 注册成功返回0, 超过设备最大数目FB_MAX(32)返回-ENXIO */

                     int  unregister_framebuffer(struct fb_info *fb_info);


18.3帧缓冲设备驱动结构

            

18.4帧缓冲设备驱动的模块加载与卸载函数

           帧缓冲设备驱动的模块加载函数:

                   (1)申请FBI结构体的内存空间,初始化FBI结构体中固定和可变的屏幕参数。

                   (2)根据具体的LCD屏幕特点,完成LCD控制器硬件的初始化。

                   (3)申请帧缓冲设备的显示缓冲区空间。

                   (4)注册帧缓冲设备。

          在帧缓冲设备驱动的模块加载函数中完成的工作只是注册平台驱动,初始化FBI结构体中的固定和可变参数、LCD控制器硬件的初始化、申请帧缓冲设备的显示缓冲区空间和注册帧缓冲设备的工作在平台驱动的探测函数中完成。

[cpp] view plain copy
  1. /* 帧缓冲设备驱动的模块加载/卸载及平台驱动的探测/移除函数模板*/  
  2. /* 平台驱动结构体 */  
  3. static struct platform_driver xxxfb_driver = {  
  4.     .probe     = xxxfb_probe,  
  5.     .remove    = xxxfb_remove,  
  6.     .suspend   = xxxfb_suspend,  
  7.     .resume    = xxxfb_resume,  
  8.     .driver    = {  
  9.             .name  = "xxx-lcd"/* 驱动名 */  
  10.             .owner = THIS_MODULE,  
  11.         },  
  12. };  
  13. /* 平台驱动探测函数 */  
  14. static int __init xxxfb_probe(...)  
  15. {  
  16.     struct fb_info* info;  
  17.   
  18.     /* 分配fb_info结构体 */  
  19.     info = framebuffer_alloc(...);  
  20.   
  21.     info->screen_base = framebuffer_virtual_memory;  
  22.     info->var = xxxfb_var;  
  23.     info->fix = xxxfb_fix;  
  24.   
  25.     /* 分配显示缓冲区 */  
  26.     alloc_dis_buffer(...);  
  27.   
  28.     /* 初始化LCD控制器 */  
  29.     lcd_init(...);  
  30.       
  31.     /* 检查可变参数 */  
  32.     xxxfb_check_var(&info->var, info);  
  33.       
  34.     /* 注册fb_info */  
  35.     if( register_framebuffer(info)<0 )  
  36.         return -EINVAL;  
  37.   
  38.     return 0;  
  39. }  
  40.   
  41. /* 平台驱动移除函数 */  
  42. static void __exit xxxfb_remove(...)  
  43. {  
  44.     struct fb_info* info = dev_get_drv_data(dev);  
  45.   
  46.     if(info){  
  47.         unregister_framebuffer(info);  
  48.         dealloc_dis_buffer(...); /* 释放缓冲区 */  
  49.         framebuffer_release(info); /* 注销 fb_info */  
  50.     }  
  51. }  
  52.   
  53. int __init xxxfb_init(void)  
  54. {  
  55.     return platform_driver_register(&xxxfb_driver);  /* 注册平台驱动 */  
  56. }  
  57.   
  58. static void __exit xxxfb_cleanup()  
  59. {  
  60.     platform_driver_unregister(&xxxfb_driver);  
  61. }  
  62. module_init(xxxfb_init);  
  63. module_exit(xxxfb_cleanup);  

18.5 帧缓冲设备显示缓冲区的申请与释放

         在分配缓冲区时一定要考虑cache的一致性问题,因为系统往往会通过DMA方式搬移显示数据。合适的方式是使用 dma_alloc_writecombine() 函数分配一段 writecombining 区域,对应的 writecombining 区域由 dma_free_writecombine() 函数释放。

         writecombing 意味着“写合并”,它允许写入的数据被合并,并临时保存在写合并缓冲区(WCB)中,直到进行一次 burst 传输而不再需要多次 single 传输。通过 dma_alloc_writecombine() 分配的显示缓冲区不会出现 cache 一致性问题,这一点类似 dma_alloc_coherent()。

[cpp] view plain copy
  1. /* 帧缓冲设备显示缓冲区的分配与释放 */  
  2. static int __init xxxfb_map_video_memory(struct xxxfb_info *fbi)  
  3. {  
  4.     fbi->map_size = PAGE_ALIGN(fbi->fb->fix.smem_len + PAGE_SIZE);  
  5.     fbi->map_cpu  = dma_alloc_writecombine(fbi->dev, fbi->map_size, &fbi->map_dma, GFP_KERNEL); /* 分配内存 */  
  6.   
  7.     fbi->map_size = fbi->fb->fix.smem_len;  /* 显示缓冲区大小 */  
  8.   
  9.     if( fbi->map_cpu ){  
  10.         memset(fbi->map_cpu, 0xf0, fbi->map_size);  
  11.   
  12.         fbi->screen_dma = fbi->map_dma;  
  13.         fbi->fb->screen_base = fbi->map_cpu;  
  14.         fbi->fb->fix.smem_start = fbi->screen_dma;  
  15.     }  
  16.     return fbi->map_cpu ? 0 : -ENOMEM;  
  17. }  
  18.   
  19. static inline void xxxfb_unmap_video_memory(struct s3c2410fb_info* fbi)  
  20. {  
  21.     /* 释放显示缓冲区 */  
  22.     dma_free_writecombine(fbi->dev, fbi->map_size, fbi->map_cpu, fbi->map_dma);  
  23. }  

18.6 帧缓冲设备的参数设置

18.6.1 定时参数 

            FBI结构体可变参数var中的left_margin、right_margin、upper_margin、lower_margin、hsync_len和vsync_len直接查LCD数据手册就可以得到。

18.6.2 像素时钟 

          FBI可变参数var中的pixclock意味着像素时钟,例如,如果为28.37516MHz,那么画一个像素需要35242ps(皮秒):

                    1/(28.37516MHz) = 35.242E-9s

                    如果屏幕的分辨率是640*480,显示一行的时间是:640*35.242E-9s = 22.555E-6 s

          每条扫描线是640,但是水平回扫和水平同步也需要时间,假设水平回扫和同步需要272个像素时钟,因此,画一条扫描线完整的时间是:(640+272)* 35.242E-9 s - 9 s = 32.141E-6 s

          可以计算出水平扫描率大约是32kHz:1/(32.141E-6 s) = 31.113E3 Hz

         完整的屏幕有480条线,但是垂直回扫和垂直同步也需要时间,假设垂直回扫和垂直同步需要49个像素时钟,因此,画一个完整的屏幕的时间是:(480+49)*32.141E-6 s = 17.002E-3 s

        可以计算出垂直扫描率大约是59kHz:1/(17.002E-3 s) = 58.815 Hz

         这意味着屏幕数据每秒钟大约刷新59次。


18.6.3 颜色位域

            FBI可变参数var中的red、green、blue、位域的设置直接由显示缓冲区与显示点的对应关系决定,例如,对于RGB565模式,red占居5位,偏移11位,green占居6位,偏移5位,blue占居5位,偏移0位,即:

[cpp] view plain copy
  1. fbinfo->var.red.offset = 11;  
  2. fbinfo->var.green.offset = 5;  
  3. fbinfo->var.blue.offset  = 0;  
  4. fbinfo->var.transp.offset = 0;  
  5. fbinfo->var.red.length = 5;  
  6. fbinfo->var.green.length = 6;  
  7. gbinfo->var.blue.length = 5;  

18.6.4 固定参数 

          FBI固定参数 fix 中 smem_start 指示帧缓冲设备显示缓冲区的首地址,smem_len 为帧缓冲设备显示缓冲区的大小,计算公式为:

                     smem_len = max_xres * max_yres * max_bpp;

           即:帧缓冲设备显示缓冲区的大小 = 最大的 x 解析度 * 最大的 y 解析度 * 最大的 BPP;

        

18.7 帧缓冲设备驱动的 fb_ops 成员函数 

           fb_check_var() 用于调整可变参数,并修正为硬件所支持的值;fb_set_par() 则根据屏幕参数设置具体读写LCD控制器的寄存器以使得LCD控制器进入相应的工作状态。

          对于 fb_ops 中的 fb_fillrect() 、 fb_copyarea() 和 fb_imageblit() 成员函数,通常直接使用对应的通用的 cfb_fillrect() 、cfb_copyarea() 和 cfb_imageblit() 函数即可。cfb_fillrect() 函数定义在 drivers/video/cfbfillrect.c 文件中,cfb_copyarea() 定义在 /drivers/video/cfbcopyarea.c 文件中, cfb_imageblit() 定义在 /drivers/cfbimagblt.c 文件中。


[cpp] view plain copy
  1. /* fb_setcolreg() 函数模板,实现伪颜色表(针对 FB_VISUAL_TRUECOLOCK、FB_VISUAL_DIRECTCOLOR模式)和颜色表的填充*/  
  2. static int xxxfb_setcolreg(unsigned regno, unsigned red, unsigned green, unsigned blue, unsigned transp, struct fb_info * info)  
  3. {  
  4.     struct xxxfb_info* fbi = info->par;  
  5.     unsigned int val;  
  6.   
  7.     switch( fbi->fb->fix.visual ){  
  8.         case FB_VISUAL_TRUECOLOR:  
  9.             /* 真彩色,设置伪颜色表 */  
  10.             if( regno < 16 ){     /* 伪颜色表只有16个成员,实际上,他们对应16种控制台颜色,logo显示也会使用伪颜色表 */  
  11.                 u32 * pal = fbi->fb->pseudo_palette;  
  12.   
  13.                 val = chan_to_field(red, &fbi->fb->var.red);  
  14.                 val |= chan_to_field(green, &fbi->fb->var.green);  
  15.                 val |= charn_to_field(blue, &fbi->fb->var.blue);  
  16.   
  17.                 pal[regno] = val;  
  18.             }  
  19.              break;  
  20.         case FB_VISUAL_PSEUDOCOLOR:  
  21.             if( regno < 256 ){  
  22.                 /* RGB565模式 */  
  23.                 val = ( (red >> 0) & 0xf800 );  
  24.                 val |= ( (green >> 5) & 0x07e0 );  
  25.                 val |= ( (blue >> 11) & 0x001f );  
  26.   
  27.                 writel(val, XXX_TFTPAL(regno));  
  28.                 schedule_palette_update(fbi, regno, val);  
  29.             }  
  30.             break;  
  31.         ...  
  32.     }  
  33.     return 0;  
  34. }  

18.8 LCD设备驱动的读写、mmap 和 ioctl 函数

         (仔细分析)

18.9 帧缓冲设备的用户空间访问

        通过/dev/fbn,应用程序可进行的针对帧缓冲设备的操作主要有:

              (1)读/写 /dev/fbn : 相当于读写帧缓冲区。例如 cp /dev/fb0 tmp 可以将当前屏幕的内容复制到一个文件中。cp tmp > /dev/fb0则讲图像文件tmp显示在屏幕上。

              (2)映射操作:对于帧缓冲设备,可通过 mmap() 映射操作将屏幕缓冲区的物理地址映射到用户控件的一段虚拟地址中,之后用户控件就可以通过读写这段虚拟地址访问屏幕缓冲区,在屏幕上绘图。而且若干个进程可以映射到同一个显示缓冲区。实际上,使用帧缓冲设备的应用程序都是通过映射操作来显示图形的。

              (3)I/O控制:对于帧缓冲设备,对设备文件的 ioctl() 操作可读取/设置显示设备及屏幕的参数,如分辨率、显示颜色数、屏幕大小等。  

                 应用程序操作/dev/fbn的一般步骤:

             (1)打开 /dev/fbn 设备文件。

             (2)用 ioctl() 操作取得当前显示屏幕的参数,如分辨率、每个像素点的比特位数和偏移。根据屏幕参数课计算屏幕缓冲区的大小。

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

            (4)映射后就可以直接读/写屏幕缓冲区进行绘图和图片显示了。

[cpp] view plain copy
  1. /* 用户空间访问帧缓冲设备显示缓冲区范例 */  
  2. #include <unistd.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5. #include <fcntl.h>  
  6. #include <linux/fb.h>  
  7. #include <sys/mman.h>  
  8.   
  9. int main()  
  10. {  
  11.     int fbfd = 0;  
  12.     struct fb_var_screeninfo vinfo;  
  13.     unsigned long screensize = 0;  
  14.     char* fbp = 0;  
  15.     int x = 0, y = 0;  
  16.     int i = 0;  
  17.   
  18.     fbfd = open("/dev/fb0", O_RDWR);  
  19.     if( !fbfd ){  
  20.         printf("Error:cannot open framebuffer device.\n");  
  21.         exit(1);  
  22.     }  
  23.     printf("The framebuffer device was opened successfully.\n");  
  24.   
  25.     //Get Variable screen information  
  26.     if(ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)){  
  27.         printf("Error reading variable information.\n");  
  28.         exit(1);  
  29.     }  
  30.   
  31.     printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);  
  32.     if(vinfo.bits_per_pixel != 16){  
  33.         printf("Error:not supported bits_per_pixel,it only supports 16 bit color\n");  
  34.         exit(1);  
  35.     }  
  36.   
  37.     //Figure out the size of the screen in bytes  
  38.     screensize = vinfo.xres * vinfo.yres * 2;  
  39.   
  40.     //Map the device to memory  
  41.     fbp = (char*)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);  
  42.     if( (int)fbp == -1 ){  
  43.         printf("Error:failed to map framebuffer device to memory.\n");  
  44.         exit(4);  
  45.     }  
  46.     printf("The framebuffer device was mapped to memory successfully.\n");  
  47.   
  48.     //Draw 3 rect with graduated RED/GREEN/BLUE  
  49.     for(i = 0; i < 3; i++ ){  
  50.         for(y = i * (vinfo.yres / 3); y < (i + 1) * (vinfo.yres / 3); y++ ){  
  51.             for(x = 0; x < vinfo.xres; x++ ){  
  52.                 long location = x * 2 + y * vinfo.xres * 2;  
  53.                 int r = 0, g = 0, b = 0;  
  54.                 unsigned short rgb;  
  55.                 if(i == 0)  
  56.                     r = ((x * 1.0) / vinfo.xres) * 32;  
  57.                 if(i == 1)  
  58.                     g = ((x * 1.0) / vinfo.xres) * 64;  
  59.                 if(i == 2)  
  60.                     b = ((x * 1.0) / vinfo.xres) * 32;  
  61.   
  62.                 rgb = (r << 11) | (g << 5) | b;  
  63.                 *((unsigned short*)(fbp + location)) = rgb;  
  64.             }  
  65.         }  
  66.     }  
  67.     munmap(fbp, screensize);  
  68.     close(fbfd);  
  69.     return 0;  
  70. }  

阅读全文
0 0
原创粉丝点击