Linux驱动修炼之道-framebuffer

帧缓冲(frame buffer)是Linux视频系统的核心概念,因此先了解一下他的功能。

因为视频适配器可能基于不同的硬件体系架构,较高内核层和应用程序的实现可能会因视频卡的不同而不同,这会导致在使用不同视频卡的时需要采用不同的方案。随之而来的低可移植性和冗余的代码需要大量的投入和维护开销。帧缓冲的概念解决了这个问题,它进行了一般化的抽象并规定编程接口,从而开发人员可以以与平台无关的方式编写应用层和较高内核层程序。因此,内核的帧缓冲接口允许应用程序与底层图形硬件的变化无关,如果应用和显示器驱动程序遵循帧缓冲接口,应用程序不用改变就可以在不同类型的视频硬件上运行。

s3c2410在Linux系统中的framebuffer驱动框架:

再来看framebuffer中用到的各种数据结构:

[c-sharp]  view plain copy
  1. struct fb_fix_screeninfo {  
  2.     char id[16];            /*字符串形式的标识符*/  
  3.     unsigned long smem_start;   /*fb缓冲内存的开始位置(物理地址)*/  
  4.     __u32 smem_len;         /*fb缓冲的长度*/  
  5.     __u32 type;         /*FB_TYPE_* */  
  6.     __u32 type_aux;         /*Interleave*/  
  7.     __u32 visual;           /*FB_VISUAL_* */   
  8.     __u16 xpanstep;         /*如果没有硬件panning,赋0*/  
  9.     __u16 ypanstep;           
  10.     __u16 ywrapstep;          
  11.     __u32 line_length;      /*一行的字节数*/  
  12.     unsigned long mmio_start;   /*内存映射I/O的开始位置*/  
  13.     __u32 mmio_len;         /*内存映射I/O的长度*/  
  14.     __u32 accel;              
  15.     __u16 reserved[3];      /*保留*/  
  16. };  

[c-sharp]  view plain copy
  1. struct fb_var_screeninfo {  
  2.     /*可见解析度*/  
  3.     __u32 xres;           
  4.     __u32 yres;  
  5.     /*虚拟解析度*/  
  6.     __u32 xres_virtual;       
  7.     __u32 yres_virtual;  
  8.     /*虚拟到可见之间的偏移*/  
  9.     __u32 xoffset;            
  10.     __u32 yoffset;            
  11.     __u32 bits_per_pixel;       /*每像素的位数,BPP*/  
  12.     __u32 grayscale;        /*非0时指灰度*/  
  13.     /*fb缓存的R/G/B位域*/  
  14.     struct fb_bitfield red;       
  15.     struct fb_bitfield green;     
  16.     struct fb_bitfield blue;  
  17.     struct fb_bitfield transp;  /*透明度*/   
  18.     __u32 nonstd;           /*!=0非标准像素格式*/  
  19.     __u32 activate;           
  20.     __u32 height;           /*高度*/  
  21.     __u32 width;            /*宽度*/  
  22.     __u32 accel_flags;        
  23.     /*除pixclock本身外,其他都以像素时钟为单位*/  
  24.     __u32 pixclock;         /*像素时钟(皮秒)*/  
  25.     __u32 left_margin;      /*行切换:从同步到绘图之间的延迟*/  
  26.     __u32 right_margin;     /*行切换:从绘图到同步之间的延迟*/  
  27.     __u32 upper_margin;     /*帧切换:从同步到绘图之间的延迟*/  
  28.     __u32 lower_margin;     /*帧切换:从绘图到同步之间的延迟*/  
  29.     __u32 hsync_len;        /*水平同步的长度*/  
  30.     __u32 vsync_len;        /*垂直同步的长度*/  
  31.     __u32 sync;           
  32.     __u32 vmode;              
  33.     __u32 rotate;           /*顺时针旋转的角度*/  
  34.     __u32 reserved[5];      /*保留*/  
  35. };  

看下图可能会对fb_var_screeninfo中涉及的时序更清楚一些了。

[c-sharp]  view plain copy
  1. struct fb_cmap {  
  2.     __u32 start;            /*第1个元素入口*/  
  3.     __u32 len;          /*元素数量*/  
  4.     /*R,G,B,透明度*/  
  5.     __u16 *red;           
  6.     __u16 *green;  
  7.     __u16 *blue;  
  8.     __u16 *transp;            
  9. };  

[c-sharp]  view plain copy
  1. struct fb_bitfield {  
  2.     __u32 offset;       /*位域的偏移*/  
  3.     __u32 length;       /*位域的长度*/  
  4.     __u32 msb_right;    /*!=0 MSB在右边*/    
  5. };  

[c-sharp]  view plain copy
  1. struct fb_ops {  
  2.     struct module *owner;  
  3.     /*打开/释放*/  
  4.     int (*fb_open)(struct fb_info *info, int user);  
  5.     int (*fb_release)(struct fb_info *info, int user);  
  6.     /*对于非线性布局的/常规内存映射无法工作的帧缓冲设备需要*/  
  7.     ssize_t (*fb_read)(struct fb_info *info, char __user *buf,  
  8.                size_t count, loff_t *ppos);  
  9.     ssize_t (*fb_write)(struct fb_info *info, const char __user *buf,  
  10.                 size_t count, loff_t *ppos);  
  11.     /*检测可变参数,并调整到支持的值*/  
  12.     int (*fb_check_var)(struct fb_var_screeninfo *var, struct fb_info *info);  
  13.     /*根据info->var设置video模式*/  
  14.     int (*fb_set_par)(struct fb_info *info);  
  15.     /*设置color寄存器*/  
  16.     int (*fb_setcolreg)(unsigned regno, unsigned red, unsigned green,  
  17.                 unsigned blue, unsigned transp, struct fb_info *info);  
  18.     /*批量设置color寄存器,设置颜色表*/  
  19.     int (*fb_setcmap)(struct fb_cmap *cmap, struct fb_info *info);  
  20.     /*显示空白*/  
  21.     int (*fb_blank)(int blank, struct fb_info *info);  
  22.     /*pan显示*/  
  23.     int (*fb_pan_display)(struct fb_var_screeninfo *var, struct fb_info *info);  
  24.     /*矩形填充*/  
  25.     void (*fb_fillrect) (struct fb_info *info, const struct fb_fillrect *rect);  
  26.     /*数据复制*/  
  27.     void (*fb_copyarea) (struct fb_info *info, const struct fb_copyarea *region);  
  28.     /*图形填充*/  
  29.     void (*fb_imageblit) (struct fb_info *info, const struct fb_image *image);  
  30.     /*绘制光标*/  
  31.     int (*fb_cursor) (struct fb_info *info, struct fb_cursor *cursor);  
  32.     /*旋转显示*/  
  33.     void (*fb_rotate)(struct fb_info *info, int angle);  
  34.     /*等待blit空闲*/  
  35.     int (*fb_sync)(struct fb_info *info);  
  36.     /*fb特定的ioctl*/  
  37.     int (*fb_ioctl)(struct fb_info *info, unsigned int cmd,  
  38.             unsigned long arg);  
  39.     /*处理32位的compat ioctl*/  
  40.     int (*fb_compat_ioctl)(struct fb_info *info, unsigned cmd,  
  41.             unsigned long arg);  
  42.     /*fb特定的mmap*/  
  43.     int (*fb_mmap)(struct fb_info *info, struct vm_area_struct *vma);  
  44.     /*保存目前的硬件状态*/  
  45.     void (*fb_save_state)(struct fb_info *info);  
  46.     /*恢复被保存的硬件状态*/  
  47.     void (*fb_restore_state)(struct fb_info *info);  
  48.     /**/  
  49.     void (*fb_get_caps)(struct fb_info *info, struct fb_blit_caps *caps,  
  50.                 struct fb_var_screeninfo *var);  
  51. };  

[c-sharp]  view plain copy
  1. struct fb_info {  
  2.     int node;  
  3.     int flags;  
  4.     struct mutex lock;      /*用于open/release/ioctl的锁*/  
  5.     struct fb_var_screeninfo var;   /*可变参数*/  
  6.     struct fb_fix_screeninfo fix;   /*固定参数*/  
  7.     struct fb_monspecs monspecs;    /*显示器标准*/  
  8.     struct work_struct queue;   /*帧缓冲事件队列*/  
  9.     struct fb_pixmap pixmap;    /*图像硬件mapper*/  
  10.     struct fb_pixmap sprite;    /*光标硬件mapper*/  
  11.     struct fb_cmap cmap;        /*目前的颜色表*/  
  12.     struct list_head modelist;        
  13.     struct fb_videomode *mode;  /*目前的video模式*/  
  14.  
  15. #ifdef CONFIG_FB_BACKLIGHT  
  16.     struct backlight_device *bl_dev; /*对应的背光设备*/  
  17.     struct mutex bl_curve_mutex;     /*背光调整*/  
  18.     u8 bl_curve[FB_BACKLIGHT_LEVELS];  
  19. #endif  
  20. #ifdef CONFIG_FB_DEFERRED_IO  
  21.     struct delayed_work deferred_work;  
  22.     struct fb_deferred_io *fbdefio;  
  23. #endif  
  24.   
  25.     struct fb_ops *fbops;       /*帧缓冲操作*/  
  26.     struct device *device;      /*父设备*/  
  27.     struct device *dev;     /*fb设备*/  
  28.     int class_flag;                 /*私有sysfs标志*/  
  29. #ifdef CONFIG_FB_TILEBLITTING  
  30.     struct fb_tile_ops *tileops;    /*图块blitting*/  
  31. #endif  
  32.     char __iomem *screen_base;  /*虚拟基地址*/  
  33.     unsigned long screen_size;  /*ioremapped的虚拟内存大小*/   
  34.     void *pseudo_palette;       /*伪16色颜色表*/   
  35. #define FBINFO_STATE_RUNNING    0  
  36. #define FBINFO_STATE_SUSPENDED  1  
  37.     u32 state;          /*硬件状态,如挂起*/  
  38.     void *fbcon_par;                  
  39.     void *par;    
  40. };  
  41. 下面详细分析一下framebuffer的驱动源码,framebuffer作为一个平台驱动注册进内核:

    [c-sharp]  view plain copy
    1. static struct platform_driver s3c2410fb_driver = {  
    2.     .probe      = s3c2410fb_probe,  
    3.     .remove     = s3c2410fb_remove,  
    4.     .suspend    = s3c2410fb_suspend,  
    5.     .resume     = s3c2410fb_resume,  
    6.     .driver     = {  
    7.         .name   = "s3c2410-lcd",  
    8.         .owner  = THIS_MODULE,  
    9.     },  
    10. };  
    11.   
    12. int __init s3c2410fb_init(void)  
    13. {  
    14.     int ret = platform_driver_register(&s3c2410fb_driver);  
    15.   
    16.     if (ret == 0)  
    17.         ret = platform_driver_register(&s3c2412fb_driver);;  
    18.   
    19.     return ret;  
    20. }  
    21.   
    22. static void __exit s3c2410fb_cleanup(void)  
    23. {  
    24.     platform_driver_unregister(&s3c2410fb_driver);  
    25.     platform_driver_unregister(&s3c2412fb_driver);  
    26. }  
    27.   
    28. module_init(s3c2410fb_init);  
    29. module_exit(s3c2410fb_cleanup);  
     

    在arch/arm/plat-s3c24xx/devs.c中定义了framebuffer的平台设备:

    [c-sharp]  view plain copy
    1. /* LCD Controller */  
    2. static struct resource s3c_lcd_resource[] = {  
    3.     [0] = {  
    4.         .start = S3C24XX_PA_LCD,                              //IO内存的物理起始地址  
    5.         .end   = S3C24XX_PA_LCD + S3C24XX_SZ_LCD - 1,         //IO内存的物理结束地址   
    6.         .flags = IORESOURCE_MEM,                  
    7.     },  
    8.     [1] = {  
    9.         .start = IRQ_LCD,                                     //LCD的中断号  
    10.         .end   = IRQ_LCD,  
    11.         .flags = IORESOURCE_IRQ,  
    12.     }  
    13.   
    14. };  
    15. static u64 s3c_device_lcd_dmamask = 0xffffffffUL;  
    16. struct platform_device s3c_device_lcd = {  
    17.     .name         = "s3c2410-lcd",  
    18.     .id       = -1,  
    19.     .num_resources    = ARRAY_SIZE(s3c_lcd_resource),  
    20.     .resource     = s3c_lcd_resource,  
    21.     .dev              = {  
    22.         .dma_mask       = &s3c_device_lcd_dmamask,  
    23.         .coherent_dma_mask  = 0xffffffffUL  
    24.     }  
    25. };  
    26.   
    27. EXPORT_SYMBOL(s3c_device_lcd);  
     

    devs.c中的这个函数把s3c2410fb_mach_info存放到s3c_device_lcd.dev.platform_data,probe函数中会用到的。

    [c-sharp]  view plain copy
    1. void __init s3c24xx_fb_set_platdata(struct s3c2410fb_mach_info *pd)  
    2. {  
    3.     struct s3c2410fb_mach_info *npd;  
    4.   
    5.     npd = kmalloc(sizeof(*npd), GFP_KERNEL);  
    6.     if (npd) {  
    7.         memcpy(npd, pd, sizeof(*npd));  
    8.         s3c_device_lcd.dev.platform_data = npd;  
    9.     } else {  
    10.         printk(KERN_ERR "no memory for LCD platform data/n");  
    11.     }  
    12. }  
     

    这个函数是在arch/arm/mach-s3c2440/mach-smdk2440.c中的smdk2440_machine_init中调用的,所以在系统启动后会自动调用。

    [c-sharp]  view plain copy
    1. static void __init smdk2440_machine_init(void)  
    2. {  
    3.     s3c24xx_fb_set_platdata(&smdk2440_fb_info);  
    4.     s3c_i2c0_set_platdata(NULL);  
    5.     platform_add_devices(smdk2440_devices, ARRAY_SIZE(smdk2440_devices));  
    6.     smdk_machine_init();  
    7. }  
     

    s3c2410fb_display表示屏的显示参数,这个结构体在我们移植LCD驱动的时候需要根据我们屏的参数重新设置。

    [c-sharp]  view plain copy
    1. /* LCD driver info */  
    2. static struct s3c2410fb_display smdk2440_lcd_cfg __initdata = {  
    3.   
    4.     .lcdcon5    = S3C2410_LCDCON5_FRM565 |  
    5.               S3C2410_LCDCON5_INVVLINE |  
    6.               S3C2410_LCDCON5_INVVFRAME |  
    7.               S3C2410_LCDCON5_PWREN |  
    8.               S3C2410_LCDCON5_HWSWP,  
    9.   
    10.     .type       = S3C2410_LCDCON1_TFT,  
    11.   
    12.     .width      = 240,  
    13.     .height     = 320,  
    14.   
    15.     .pixclock   = 270000,   
    16.     .xres       = 320,  
    17.     .yres       = 240,  
    18.     .bpp        = 16,  
    19.     .left_margin    = 8,  
    20.     .right_margin   = 5,  
    21.     .hsync_len  = 63,  
    22.     .upper_margin   = 15,  
    23.     .lower_margin   = 3,  
    24.     .vsync_len  = 5,  
    25. };  
     

    将s3c2410fb_display结构体存于s3c2410fb_mach_info的displays域。

    [c-sharp]  view plain copy
    1. static struct s3c2410fb_mach_info smdk2440_fb_info __initdata = {  
    2.     .displays   = &smdk2440_lcd_cfg,  
    3.     .num_displays   = 1,  
    4.     .default_display = 0,  
    5.     .lpcsel     = 0,  
    6. };  
     

    下面来看看当lcd驱动和设备匹配成功后会调用的探测函数:

    [c-sharp]  view plain copy
    1. static int __init s3c2410fb_probe(struct platform_device *pdev)  
    2. {  
    3.     return s3c24xxfb_probe(pdev, DRV_S3C2410);  
    4. }  
     

    这里调用了s3c24xxfb_probe(pdev, DRV_S3C2410),进行了一层封装,因为这样这部分代码可以与s3c2412进行复用。

    [c-sharp]  view plain copy
    1. static int __init s3c24xxfb_probe(struct platform_device *pdev,  
    2.                   enum s3c_drv_type drv_type)  
    3. {  
    4.     struct s3c2410fb_info *info;  
    5.     struct s3c2410fb_display *display;  
    6.     struct fb_info *fbinfo;  
    7.     struct s3c2410fb_mach_info *mach_info;  
    8.     struct resource *res;  
    9.     int ret;  
    10.     int irq;  
    11.     int i;  
    12.     int size;  
    13.     u32 lcdcon1;  
    14.     /*这就获得了刚才保存的s3c2410fb_mach_info*/  
    15.     mach_info = pdev->dev.platform_data;  
    16.     if (mach_info == NULL) {  
    17.         dev_err(&pdev->dev,  
    18.             "no platform data for lcd, cannot attach/n");  
    19.         return -EINVAL;  
    20.     }  
    21.     if (mach_info->default_display >= mach_info->num_displays) {  
    22.         dev_err(&pdev->dev, "default is %d but only %d displays/n",  
    23.             mach_info->default_display, mach_info->num_displays);  
    24.         return -EINVAL;  
    25.     }  
    26.     /*获取显示屏的相关参数*/  
    27.     display = mach_info->displays + mach_info->default_display;  
    28.     /*获得中断号*/  
    29.     irq = platform_get_irq(pdev, 0);  
    30.     if (irq < 0) {  
    31.         dev_err(&pdev->dev, "no irq for device/n");  
    32.         return -ENOENT;  
    33.     }  
    34.     /*分配一个fb_info结构体*/  
    35.     fbinfo = framebuffer_alloc(sizeof(struct s3c2410fb_info), &pdev->dev);  
    36.     if (!fbinfo)  
    37.         return -ENOMEM;  
    38.     /*设置pdev->dev->driver_data保存fbinfo的地址*/  
    39.     platform_set_drvdata(pdev, fbinfo);  
    40.     info = fbinfo->par;  
    41.     info->dev = &pdev->dev;  
    42.     info->drv_type = drv_type;  
    43.     这4句构建的关系图如下:  
    44.   
    45.     /*获得IO内存*/    
    46.     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);  
    47.     if (res == NULL) {  
    48.         dev_err(&pdev->dev, "failed to get memory registers/n");  
    49.         ret = -ENXIO;  
    50.         goto dealloc_fb;  
    51.     }  
    52.   
    53.     size = (res->end - res->start) + 1;  
    54.     /*申请IO内存*/  
    55.     info->mem = request_mem_region(res->start, size, pdev->name);  
    56.     if (info->mem == NULL) {  
    57.         dev_err(&pdev->dev, "failed to get memory region/n");  
    58.         ret = -ENOENT;  
    59.         goto dealloc_fb;  
    60.     }  
    61.     /*映射IO内存*/  
    62.     info->io = ioremap(res->start, size);  
    63.     if (info->io == NULL) {  
    64.         dev_err(&pdev->dev, "ioremap() of registers failed/n");  
    65.         ret = -ENXIO;  
    66.         goto release_mem;  
    67.     }  
    68.     /*获得LCD中断挂起寄存器的基地址*/  
    69.     info->irq_base = info->io + ((drv_type == DRV_S3C2412) ? S3C2412_LCDINTBASE : S3C2410_LCDINTBASE);  
    70.   
    71.     dprintk("devinit/n");  
    72.   
    73.     strcpy(fbinfo->fix.id, driver_name);  
    74.   
    75.     /*暂时关闭LCD控制器*/  
    76.     lcdcon1 = readl(info->io + S3C2410_LCDCON1);  
    77.     writel(lcdcon1 & ~S3C2410_LCDCON1_ENVID, info->io + S3C2410_LCDCON1);  
    78.       
    79.     fbinfo->fix.type     = FB_TYPE_PACKED_PIXELS;  
    80.     fbinfo->fix.type_aux     = 0;  
    81.     fbinfo->fix.xpanstep     = 0;  
    82.     fbinfo->fix.ypanstep     = 0;  
    83.     fbinfo->fix.ywrapstep        = 0;  
    84.     fbinfo->fix.accel        = FB_ACCEL_NONE;  
    85.   
    86.     fbinfo->var.nonstd       = 0;  
    87.     fbinfo->var.activate     = FB_ACTIVATE_NOW;  
    88.     fbinfo->var.accel_flags     = 0;  
    89.     fbinfo->var.vmode        = FB_VMODE_NONINTERLACED;  
    90.     /*将底层操作函数与上层联系起来*/  
    91.     fbinfo->fbops            = &s3c2410fb_ops;  
    92.     fbinfo->flags            = FBINFO_FLAG_DEFAULT;  
    93.     fbinfo->pseudo_palette      = &info->pseudo_pal;  
    94.     /*用于填充调色板*/  
    95.     for (i = 0; i < 256; i++)  
    96.         info->palette_buffer[i] = PALETTE_BUFF_CLEAR;  
    97.     /*注册中断处理函数*/  
    98.     ret = request_irq(irq, s3c2410fb_irq, IRQF_DISABLED, pdev->name, info);  
    99.     if (ret) {  
    100.         dev_err(&pdev->dev, "cannot get irq %d - err %d/n", irq, ret);  
    101.         ret = -EBUSY;  
    102.         goto release_regs;  
    103.     }  
    104.     /*获得LCD时钟*/  
    105.     info->clk = clk_get(NULL, "lcd");  
    106.     if (!info->clk || IS_ERR(info->clk)) {  
    107.         printk(KERN_ERR "failed to get lcd clock source/n");  
    108.         ret = -ENOENT;  
    109.         goto release_irq;  
    110.     }  
    111.     /*使能LCD时钟*/  
    112.     clk_enable(info->clk);  
    113.     dprintk("got and enabled clock/n");  
    114.     /*初始化LCD控制器之前要延时一段时间*/  
    115.     msleep(1);  
    116.   
    117.     /*计算缓冲区需要的最大内存,就是缓冲区一共占多少字节,xres*yres*bpp/8 */  
    118.     for (i = 0; i < mach_info->num_displays; i++) {  
    119.         unsigned long smem_len = mach_info->displays[i].xres;  
    120.   
    121.         smem_len *= mach_info->displays[i].yres;  
    122.         smem_len *= mach_info->displays[i].bpp;  
    123.         smem_len >>= 3;  
    124.         if (fbinfo->fix.smem_len < smem_len)  
    125.             fbinfo->fix.smem_len = smem_len;  
    126.     }  
    127.     /*申请帧缓冲内存*/  
    128.     ret = s3c2410fb_map_video_memory(fbinfo);  
    129.     if (ret) {  
    130.         printk(KERN_ERR "Failed to allocate video RAM: %d/n", ret);  
    131.         ret = -ENOMEM;  
    132.         goto release_clock;  
    133.     }  
    134.   
    135.     dprintk("got video memory/n");  
    136.       
    137.     fbinfo->var.xres = display->xres;  
    138.     fbinfo->var.yres = display->yres;  
    139.     fbinfo->var.bits_per_pixel = display->bpp;  
    140.     /*初始化相关寄存器*/  
    141.     s3c2410fb_init_registers(fbinfo);  
    142.     /*检查fb_info中的可变参数*/  
    143.     s3c2410fb_check_var(&fbinfo->var, fbinfo);  
    144.   
    145.     ret = register_framebuffer(fbinfo);               //注册帧缓冲设备  
    146.     if (ret < 0) {  
    147.         printk(KERN_ERR "Failed to register framebuffer device: %d/n",  
    148.             ret);  
    149.         goto free_video_memory;  
    150.     }  
    151.   
    152.     /* create device files */  
    153.     ret = device_create_file(&pdev->dev, &dev_attr_debug);  //创建设备文件  
    154.     if (ret) {  
    155.         printk(KERN_ERR "failed to add debug attribute/n");  
    156.     }  
    157.   
    158.     printk(KERN_INFO "fb%d: %s frame buffer device/n",  
    159.         fbinfo->node, fbinfo->fix.id);  
    160.   
    161.     return 0;  
    162.   
    163. free_video_memory:  
    164.     s3c2410fb_unmap_video_memory(fbinfo);  
    165. release_clock:  
    166.     clk_disable(info->clk);  
    167.     clk_put(info->clk);  
    168. release_irq:  
    169.     free_irq(irq, info);  
    170. release_regs:  
    171.     iounmap(info->io);  
    172. release_mem:  
    173.     release_resource(info->mem);  
    174.     kfree(info->mem);  
    175. dealloc_fb:  
    176.     platform_set_drvdata(pdev, NULL);  
    177.     framebuffer_release(fbinfo);  
    178.     return ret;  
    179. }  
     

    总结一下探测函数完成的任务:
    1)申请fb_info结构体的内存空间,初始化fb_info结构中固定和可变的内存参数,即填充fb_info中的fb_var_screeninfo var和struct fb_fix_screeninfo fix成员。
    2)申请帧缓冲设备的显示缓冲区空间
    3)注册帧缓冲设备

    [c-sharp]  view plain copy
    1. struct fb_info *framebuffer_alloc(size_t size, struct device *dev)  
    2. {  
    3. #define BYTES_PER_LONG (BITS_PER_LONG/8)  
    4. #define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))  
    5.     int fb_info_size = sizeof(struct fb_info);  
    6.     struct fb_info *info;  
    7.     char *p;  
    8.     if (size)  
    9.         fb_info_size += PADDING;  
    10.     /*这里开辟的堆空间用来存储struct fb_info结构体和struct s3c2410fb_info结构体*/  
    11.     p = kzalloc(fb_info_size + size, GFP_KERNEL);  
    12.     if (!p)  
    13.         return NULL;  
    14.     info = (struct fb_info *) p;  
    15.     /*在这里将par成员赋值,以后用于存储struct s3c2410fb_info结构*/  
    16.     if (size)  
    17.         info->par = p + fb_info_size;  
    18.     info->device = dev;  
    19.  
    20. #ifdef CONFIG_FB_BACKLIGHT  
    21.     mutex_init(&info->bl_curve_mutex);  
    22. #endif  
    23.   
    24.     return info;  
    25. #undef PADDING  
    26. #undef BYTES_PER_LONG  
    27. }  
     

    中断处理函数:

    [c-sharp]  view plain copy
    1. static irqreturn_t s3c2410fb_irq(int irq, void *dev_id)  
    2. {  
    3.     struct s3c2410fb_info *fbi = dev_id;  
    4.     /*LCD中断挂起寄存器基地址*/  
    5.     void __iomem *irq_base = fbi->irq_base;  
    6.     /*读取LCD中断挂起寄存器值*/  
    7.     unsigned long lcdirq = readl(irq_base + S3C24XX_LCDINTPND);  
    8.     /*如果framebuffer发出了中断请求*/  
    9.     if (lcdirq & S3C2410_LCDINT_FRSYNC) {  
    10.         /*填充调色板*/  
    11.         if (fbi->palette_ready)  
    12.             s3c2410fb_write_palette(fbi);  
    13.         /*设置帧已插入中断请求*/  
    14.         writel(S3C2410_LCDINT_FRSYNC, irq_base + S3C24XX_LCDINTPND);  
    15.         writel(S3C2410_LCDINT_FRSYNC, irq_base + S3C24XX_LCDSRCPND);  
    16.     }  
    17.   
    18.     return IRQ_HANDLED;  
    19. }  
     

    填充调色板:

    [c-sharp]  view plain copy
    1. static void s3c2410fb_write_palette(struct s3c2410fb_info *fbi)  
    2. {  
    3.     unsigned int i;  
    4.     void __iomem *regs = fbi->io;  
    5.   
    6.     fbi->palette_ready = 0;  
    7.   
    8.     for (i = 0; i < 256; i++) {  
    9.         unsigned long ent = fbi->palette_buffer[i];  
    10.         if (ent == PALETTE_BUFF_CLEAR)  
    11.             continue;  
    12.   
    13.         writel(ent, regs + S3C2410_TFTPAL(i));  
    14.   
    15.         /* it seems the only way to know exactly 
    16.          * if the palette wrote ok, is to check 
    17.          * to see if the value verifies ok 
    18.          */  
    19.   
    20.         if (readw(regs + S3C2410_TFTPAL(i)) == ent)  
    21.             fbi->palette_buffer[i] = PALETTE_BUFF_CLEAR;  
    22.         else  
    23.             fbi->palette_ready = 1;   /* retry */  
    24.     }  
    25. }  
     

    申请帧缓冲设备fb_info的缓冲区空间:

    [c-sharp]  view plain copy
    1. static int __init s3c2410fb_map_video_memory(struct fb_info *info)  
    2. {  
    3.     struct s3c2410fb_info *fbi = info->par;  
    4.     dma_addr_t map_dma;  
    5.     /*获得帧缓冲区的大小*/  
    6.     unsigned map_size = PAGE_ALIGN(info->fix.smem_len);  
    7.   
    8.     dprintk("map_video_memory(fbi=%p) map_size %u/n", fbi, map_size);  
    9.     /*分配一个写合并缓冲区来设置帧缓冲的虚拟地址*/  
    10.     info->screen_base = dma_alloc_writecombine(fbi->dev, map_size,  
    11.                            &map_dma, GFP_KERNEL);  
    12.     if (info->screen_base) {  
    13.         /* prevent initial garbage on screen */  
    14.         dprintk("map_video_memory: clear %p:%08x/n",  
    15.             info->screen_base, map_size);  
    16.         /*初始化为0*/  
    17.         memset(info->screen_base, 0x00, map_size);   
    18.         /*设置物理地址*/  
    19.         info->fix.smem_start = map_dma;  
    20.         dprintk("map_video_memory: dma=%08lx cpu=%p size=%08x/n",  
    21.             info->fix.smem_start, info->screen_base, map_size);  
    22.     }  
    23.     /*返回虚拟地址*/  
    24.     return info->screen_base ? 0 : -ENOMEM;  
    25. }  
     

    初始化相关寄存器:

    [c-sharp]  view plain copy
    1. static int s3c2410fb_init_registers(struct fb_info *info)  
    2. {  
    3.     struct s3c2410fb_info *fbi = info->par;  
    4.     struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;  
    5.     unsigned long flags;  
    6.     /*获得LCD寄存器基地址,这个在probe中获得*/  
    7.     void __iomem *regs = fbi->io;  
    8.     void __iomem *tpal;  
    9.     void __iomem *lpcsel;  
    10.   
    11.     if (is_s3c2412(fbi)) {  
    12.         tpal = regs + S3C2412_TPAL;  
    13.         lpcsel = regs + S3C2412_TCONSEL;  
    14.     } else {  
    15.         /*获得LCD调色板寄存器基地址,注意对于lpcsel这是一个针对三星TFT屏的一个专用寄存器,如果用的不是三星的屏就不用管它*/  
    16.         tpal = regs + S3C2410_TPAL;  
    17.         lpcsel = regs + S3C2410_LPCSEL;  
    18.     }  
    19.   
    20.     /* Initialise LCD with values from haret */  
    21.     /*关中断*/  
    22.     local_irq_save(flags);  
    23.   
    24.     /* modify the gpio(s) with interrupts set (bjd) */  
    25.     /*把IO端口C和D设置成LCD模式*/  
    26.     modify_gpio(S3C2410_GPCUP,  mach_info->gpcup,  mach_info->gpcup_mask);  
    27.     modify_gpio(S3C2410_GPCCON, mach_info->gpccon, mach_info->gpccon_mask);  
    28.     modify_gpio(S3C2410_GPDUP,  mach_info->gpdup,  mach_info->gpdup_mask);  
    29.     modify_gpio(S3C2410_GPDCON, mach_info->gpdcon, mach_info->gpdcon_mask);  
    30.     /*恢复被屏蔽的中断*/  
    31.     local_irq_restore(flags);  
    32.   
    33.     dprintk("LPCSEL    = 0x%08lx/n", mach_info->lpcsel);  
    34.     writel(mach_info->lpcsel, lpcsel);  
    35.   
    36.     dprintk("replacing TPAL %08x/n", readl(tpal));  
    37.   
    38.     /*临时调色板使能禁止*/  
    39.     writel(0x00, tpal);  
    40.   
    41.     return 0;  
    42. }  
     

    设置fb_info中的可变参数:

    [c-sharp]  view plain copy
    1. static int s3c2410fb_check_var(struct fb_var_screeninfo *var,  
    2.                    struct fb_info *info)  
    3. {  
    4.     struct s3c2410fb_info *fbi = info->par;  
    5.     struct s3c2410fb_mach_info *mach_info = fbi->dev->platform_data;  
    6.     struct s3c2410fb_display *display = NULL;  
    7.     struct s3c2410fb_display *default_display = mach_info->displays +  
    8.                             mach_info->default_display;  
    9.     /*LCD的类型,S3C2410_LCDCON1_TFT*/  
    10.     int type = default_display->type;  
    11.     unsigned i;  
    12.   
    13.     dprintk("check_var(var=%p, info=%p)/n", var, info);  
    14.   
    15.     /*获取与LCD屏有关的参数,封装在s3c2410fb_display中*/  
    16.     if (var->yres == default_display->yres &&  
    17.         var->xres == default_display->xres &&  
    18.         var->bits_per_pixel == default_display->bpp)  
    19.         display = default_display;  
    20.     else  
    21.         for (i = 0; i < mach_info->num_displays; i++)  
    22.             if (type == mach_info->displays[i].type &&  
    23.                 var->yres == mach_info->displays[i].yres &&  
    24.                 var->xres == mach_info->displays[i].xres &&  
    25.                 var->bits_per_pixel == mach_info->displays[i].bpp) {  
    26.                 display = mach_info->displays + i;  
    27.                 break;  
    28.             }  
    29.   
    30.     if (!display) {  
    31.         dprintk("wrong resolution or depth %dx%d at %d bpp/n",  
    32.             var->xres, var->yres, var->bits_per_pixel);  
    33.         return -EINVAL;  
    34.     }  
    35.   
    36.     /*配置屏的虚拟解析度和高度宽度*/  
    37.     var->xres_virtual = display->xres;  
    38.     var->yres_virtual = display->yres;  
    39.     var->height = display->height;  
    40.     var->width = display->width;  
    41.   
    42.     /*这里是时序了,设置时钟像素,行帧切换值,水平同步,垂直同步切换值*/  
    43.     var->pixclock = display->pixclock;  
    44.     var->left_margin = display->left_margin;  
    45.     var->right_margin = display->right_margin;  
    46.     var->upper_margin = display->upper_margin;  
    47.     var->lower_margin = display->lower_margin;  
    48.     var->vsync_len = display->vsync_len;  
    49.     var->hsync_len = display->hsync_len;  
    50.   
    51.     /*配置LCD控制寄存器1中5-6位(配置成TFT类型),配置寄存器5*/  
    52.     fbi->regs.lcdcon5 = display->lcdcon5;  
    53.     /* set display type */  
    54.     fbi->regs.lcdcon1 = display->type;  
    55.   
    56.     /*设置透明度*/  
    57.     var->transp.offset = 0;  
    58.     var->transp.length = 0;  
    59.     /*根据色位模式设置(BPP)来设置可变参数中R,G,B的颜色位域,显示缓冲区与显示点对应如下图:*/  
     

     

    [c-sharp]  view plain copy
    1. switch (var->bits_per_pixel) {  
    2.     case 1:  
    3.     case 2:  
    4.     case 4:  
    5.         var->red.offset  = 0;  
    6.         var->red.length  = var->bits_per_pixel;  
    7.         var->green   = var->red;  
    8.         var->blue    = var->red;  
    9.         break;  
    10.     case 8:  
    11.         if (display->type != S3C2410_LCDCON1_TFT) {  
    12.             /* 8 bpp 332 */  
    13.             var->red.length      = 3;  
    14.             var->red.offset      = 5;  
    15.             var->green.length    = 3;  
    16.             var->green.offset    = 2;  
    17.             var->blue.length = 2;  
    18.             var->blue.offset = 0;  
    19.         } else {  
    20.             var->red.offset      = 0;  
    21.             var->red.length      = 8;  
    22.             var->green       = var->red;  
    23.             var->blue        = var->red;  
    24.         }  
    25.         break;  
    26.     case 12:  
    27.         /* 12 bpp 444 */  
    28.         var->red.length      = 4;  
    29.         var->red.offset      = 8;  
    30.         var->green.length    = 4;  
    31.         var->green.offset    = 4;  
    32.         var->blue.length = 4;  
    33.         var->blue.offset = 0;  
    34.         break;  
    35.   
    36.     default:  
    37.     case 16:  
    38.         if (display->lcdcon5 & S3C2410_LCDCON5_FRM565) {  
    39.             /* 16 bpp, 565 format */  
    40.             var->red.offset      = 11;  
    41.             var->green.offset    = 5;  
    42.             var->blue.offset = 0;  
    43.             var->red.length      = 5;  
    44.             var->green.length    = 6;  
    45.             var->blue.length = 5;  
    46.         } else {  
    47.             /* 16 bpp, 5551 format */  
    48.             var->red.offset      = 11;  
    49.             var->green.offset    = 6;  
    50.             var->blue.offset = 1;  
    51.             var->red.length      = 5;  
    52.             var->green.length    = 5;  
    53.             var->blue.length = 5;  
    54.         }  
    55.         break;  
    56.     case 32:  
    57.         /* 24 bpp 888 and 8 dummy */  
    58.         var->red.length      = 8;  
    59.         var->red.offset      = 16;  
    60.         var->green.length    = 8;  
    61.         var->green.offset    = 8;  
    62.         var->blue.length = 8;  
    63.         var->blue.offset = 0;  
    64.         break;  
    65.     }  
    66.     return 0;  
    67. }  

    注册帧缓冲设备:

    [c-sharp]  view plain copy
    1. int  
    2. register_framebuffer(struct fb_info *fb_info)  
    3. {  
    4.     int i;  
    5.     struct fb_event event;  
    6.     struct fb_videomode mode;  
    7.   
    8.     if (num_registered_fb == FB_MAX)  
    9.         return -ENXIO;  
    10.   
    11.     if (fb_check_foreignness(fb_info))  
    12.         return -ENOSYS;  
    13.     /* 
    14.      *每一个注册的fb_info,都会分配一个下标"i",对应的就是registered_fb[i] 
    15.      *最多能注册的fb_info个数为FB_MAX,若新注册FB则num_registered_fb++ 
    16.      */  
    17.     num_registered_fb++;  
    18.     for (i = 0 ; i < FB_MAX; i++)  
    19.         if (!registered_fb[i])  
    20.             break;  
    21.     /*找到空闲的i,赋值给fb_info->node,这个node相当于次设备号了,以后通过这个i找到fb_info*/  
    22.     fb_info->node = i;  
    23.     mutex_init(&fb_info->lock);  
    24.     /*创建设备文件*/  
    25.     fb_info->dev = device_create(fb_class, fb_info->device,  
    26.                      MKDEV(FB_MAJOR, i), NULL, "fb%d", i);  
    27.     if (IS_ERR(fb_info->dev)) {  
    28.         /* Not fatal */  
    29.         printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld/n", i, PTR_ERR(fb_info->dev));  
    30.         fb_info->dev = NULL;  
    31.     } else  
    32.         /*初始化fb的属性文件*/  
    33.         fb_init_device(fb_info);  
    34.     。。。。。。。。。。。。。。  
    35.     return 0;  
    36. }  
    37.   
    38. static struct fb_ops s3c2410fb_ops = {  
    39.     .owner      = THIS_MODULE,  
    40.     .fb_check_var   = s3c2410fb_check_var,  
    41.     .fb_set_par = s3c2410fb_set_par,  
    42.     .fb_blank   = s3c2410fb_blank,  
    43.     .fb_setcolreg   = s3c2410fb_setcolreg,  
    44.     .fb_fillrect    = cfb_fillrect,  
    45.     .fb_copyarea    = cfb_copyarea,  
    46.     .fb_imageblit   = cfb_imageblit,  
    47. };  
     

    设置参数,根据可变参数设置固定参数:

    [c-sharp]  view plain copy
    1. static int s3c2410fb_set_par(struct fb_info *info)  
    2. {  
    3.     struct fb_var_screeninfo *var = &info->var;  
    4.     /*根据可变参数的位色模式*/  
    5.     switch (var->bits_per_pixel) {  
    6.     case 32:  
    7.     case 16:  
    8.     case 12://设置成真彩,分红,绿,蓝三基色  
    9.         info->fix.visual = FB_VISUAL_TRUECOLOR;  
    10.         break;  
    11.     case 1://设置为黑白,FB_VISUAL_MONO01代表黑,FB_VISUAL_MONO10代表白  
    12.         info->fix.visual = FB_VISUAL_MONO01;  
    13.         break;  
    14.     default://默认设置为伪彩色,采用索引颜色显示  
    15.         info->fix.visual = FB_VISUAL_PSEUDOCOLOR;  
    16.         break;  
    17.     }  
    18.     /*设置fb_info中固定参数一行的字节数*/  
    19.     info->fix.line_length = (var->xres_virtual * var->bits_per_pixel) / 8;  
    20.   
    21.     /*激活新的参数配置,设置控制寄存器的值*/  
    22.     s3c2410fb_activate_var(info);  
    23.     return 0;  
    24. }  
     

    激活设置:

    [c-sharp]  view plain copy
    1. static void s3c2410fb_activate_var(struct fb_info *info)  
    2. {  
    3.     struct s3c2410fb_info *fbi = info->par;  
    4.     void __iomem *regs = fbi->io;  
    5.     /*获得屏的类型*/  
    6.     int type = fbi->regs.lcdcon1 & S3C2410_LCDCON1_TFT;  
    7.     struct fb_var_screeninfo *var = &info->var;  
    8.     /*获得CLKVAL*/  
    9.     int clkdiv = s3c2410fb_calc_pixclk(fbi, var->pixclock) / 2;  
    10.   
    11.     dprintk("%s: var->xres  = %d/n", __func__, var->xres);  
    12.     dprintk("%s: var->yres  = %d/n", __func__, var->yres);  
    13.     dprintk("%s: var->bpp   = %d/n", __func__, var->bits_per_pixel);  
    14.   
    15.     if (type == S3C2410_LCDCON1_TFT) {  
    16.         /*就是根据可变参数结构设置lcdcon1~5*/  
    17.         s3c2410fb_calculate_tft_lcd_regs(info, &fbi->regs);  
    18.         --clkdiv;  
    19.         if (clkdiv < 0)  
    20.             clkdiv = 0;  
    21.     } else {  
    22.         s3c2410fb_calculate_stn_lcd_regs(info, &fbi->regs);  
    23.         if (clkdiv < 2)  
    24.             clkdiv = 2;  
    25.     }  
    26.     /*设置分频值*/  
    27.     fbi->regs.lcdcon1 |=  S3C2410_LCDCON1_CLKVAL(clkdiv);  
    28.   
    29.     /* write new registers */  
    30.   
    31.     dprintk("new register set:/n");  
    32.     dprintk("lcdcon[1] = 0x%08lx/n", fbi->regs.lcdcon1);  
    33.     dprintk("lcdcon[2] = 0x%08lx/n", fbi->regs.lcdcon2);  
    34.     dprintk("lcdcon[3] = 0x%08lx/n", fbi->regs.lcdcon3);  
    35.     dprintk("lcdcon[4] = 0x%08lx/n", fbi->regs.lcdcon4);  
    36.     dprintk("lcdcon[5] = 0x%08lx/n", fbi->regs.lcdcon5);  
    37.     /*设置寄存器前先把LCD使能关闭,然后将刚才设置的值写入真正的寄存器*/  
    38.     writel(fbi->regs.lcdcon1 & ~S3C2410_LCDCON1_ENVID,  
    39.         regs + S3C2410_LCDCON1);  
    40.     writel(fbi->regs.lcdcon2, regs + S3C2410_LCDCON2);  
    41.     writel(fbi->regs.lcdcon3, regs + S3C2410_LCDCON3);  
    42.     writel(fbi->regs.lcdcon4, regs + S3C2410_LCDCON4);  
    43.     writel(fbi->regs.lcdcon5, regs + S3C2410_LCDCON5);  
    44.   
    45.     /*设置LCDSADDR1~3*/  
    46.     s3c2410fb_set_lcdaddr(info);  
    47.     /*使能LCD*/  
    48.     fbi->regs.lcdcon1 |= S3C2410_LCDCON1_ENVID,  
    49.     writel(fbi->regs.lcdcon1, regs + S3C2410_LCDCON1);  
    50. }  
     

    显示空白:blank_mode有5中模式,是一个枚举,定义在include/linux/fb.h中:

    [c-sharp]  view plain copy
    1. static int s3c2410fb_blank(int blank_mode, struct fb_info *info)  
    2. {  
    3.     struct s3c2410fb_info *fbi = info->par;  
    4.     void __iomem *tpal_reg = fbi->io;  
    5.   
    6.     dprintk("blank(mode=%d, info=%p)/n", blank_mode, info);  
    7.   
    8.     tpal_reg += is_s3c2412(fbi) ? S3C2412_TPAL : S3C2410_TPAL;  
    9.           
    10.     if (blank_mode == FB_BLANK_POWERDOWN) {   //如果是空白模式,则关闭LCD  
    11.         s3c2410fb_lcd_enable(fbi, 0);  
    12.     } else {  
    13.         s3c2410fb_lcd_enable(fbi, 1);  
    14.     }  
    15.     if (blank_mode == FB_BLANK_UNBLANK)  
    16.         /*临时调色板无效*/  
    17.         writel(0x0, tpal_reg);  
    18.     else {  
    19.         /*临时调色板有效*/  
    20.         dprintk("setting TPAL to output 0x000000/n");  
    21.         writel(S3C2410_TPAL_EN, tpal_reg);  
    22.     }  
    23.     return 0;  
    24. }  
     

    设置颜色表:

    [c-sharp]  view plain copy
    1. static int s3c2410fb_setcolreg(unsigned regno,  
    2.                    unsigned red, unsigned green, unsigned blue,  
    3.                    unsigned transp, struct fb_info *info)  
    4. {  
    5.     struct s3c2410fb_info *fbi = info->par;  
    6.     void __iomem *regs = fbi->io;  
    7.     unsigned int val;  
    8.   
    9.     /* dprintk("setcol: regno=%d, rgb=%d,%d,%d/n", 
    10.            regno, red, green, blue); */  
    11.   
    12.     switch (info->fix.visual) {  
    13.         /*真彩色*/  
    14.     case FB_VISUAL_TRUECOLOR:  
    15.         /* true-colour, use pseudo-palette */  
    16.   
    17.         if (regno < 16) {  
    18.             u32 *pal = info->pseudo_palette;  
    19.   
    20.             val  = chan_to_field(red,   &info->var.red);  
    21.             val |= chan_to_field(green, &info->var.green);  
    22.             val |= chan_to_field(blue,  &info->var.blue);  
    23.   
    24.             pal[regno] = val;  
    25.         }  
    26.         break;  
    27.         /*伪彩色*/  
    28.     case FB_VISUAL_PSEUDOCOLOR:  
    29.         if (regno < 256) {  
    30.             /* currently assume RGB 5-6-5 mode */  
    31.   
    32.             val  = (red   >>  0) & 0xf800;  
    33.             val |= (green >>  5) & 0x07e0;  
    34.             val |= (blue  >> 11) & 0x001f;  
    35.   
    36.             writel(val, regs + S3C2410_TFTPAL(regno));  
    37.             /*修改调色板*/  
    38.             schedule_palette_update(fbi, regno, val);  
    39.         }  
    40.         break;  
    41.     default:  
    42.         return 1;   /* unknown type */  
    43.     }  
    44.     return 0;  
    45. }  
    46. static inline unsigned int chan_to_field(unsigned int chan,  
    47.                      struct fb_bitfield *bf)  
    48. {  
    49.     chan &= 0xffff;  
    50.     chan >>= 16 - bf->length;  
    51.     return chan << bf->offset;  
    52. }  
     

    修改调色板:

    [c-sharp]  view plain copy
    1. static void schedule_palette_update(struct s3c2410fb_info *fbi,  
    2.                     unsigned int regno, unsigned int val)  
    3. {  
    4.     unsigned long flags;  
    5.     unsigned long irqen;  
    6.     /*LCD中断挂起寄存器基地址*/  
    7.     void __iomem *irq_base = fbi->irq_base;  
    8.   
    9.     /*屏蔽中断,将中断状态保存在flags中*/  
    10.     local_irq_save(flags);  
    11.   
    12.     fbi->palette_buffer[regno] = val;  
    13.     /*判断调色板是否准备就绪*/  
    14.     if (!fbi->palette_ready) {  
    15.         fbi->palette_ready = 1;  
    16.         /*使能中断屏蔽寄存器*/  
    17.         irqen = readl(irq_base + S3C24XX_LCDINTMSK);  
    18.         irqen &= ~S3C2410_LCDINT_FRSYNC;  
    19.         writel(irqen, irq_base + S3C24XX_LCDINTMSK);  
    20.     }  
    21.     /*回复被屏蔽的中断*/  
    22.     local_irq_restore(flags);  
    23. }  
    24. 用户写屏程序:

      [c-sharp]  view plain copy
      1. #include <stdlib.h>  
      2. #include <unistd.h>  
      3. #include <stdio.h>  
      4. #include <fcntl.h>  
      5. #include <linux/fb.h>  
      6. #include <sys/mman.h>  
      7. #include <sys/ioctl.h>  
      8.   
      9.  int main()  
      10.  {  
      11.      int fbfd = 0;  
      12.      struct fb_var_screeninfo vinfo;  
      13.      struct fb_fix_screeninfo finfo;  
      14.      long int screensize = 0;  
      15.      char *fbp = 0;  
      16.      int x = 0, y = 0;  
      17.      long int location = 0;  
      18.    
      19.      // Open the file for reading and writing  
      20.      fbfd = open("/dev/fb0", O_RDWR);  
      21.      if (fbfd == -1) {  
      22.          perror("Error: cannot open framebuffer device");  
      23.          exit(1);  
      24.      }  
      25.      printf("The framebuffer device was opened successfully./n");  
      26.      // Get fixed screen information  
      27.      if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {  
      28.          perror("Error reading fixed information");  
      29.          exit(2);  
      30.      }  
      31.      // Get variable screen information  
      32.   
      33.      if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {  
      34.         perror("Error reading variable information");  
      35.          exit(3);  
      36.      }  
      37.   
      38.      printf("%dx%d, %dbpp/n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);  
      39.      // Figure out the size of the screen in bytes  
      40.      screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;  
      41.   
      42.      // Map the device to memory  
      43.   
      44.      fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,  
      45.                         fbfd, 0);  
      46.   
      47.      if ((int)fbp == -1) {  
      48.          perror("Error: failed to map framebuffer device to memory");  
      49.          exit(4);  
      50.      }  
      51.      printf("The framebuffer device was mapped to memory successfully./n");  
      52.   
      53.      x = 100; y = 100;       // Where we are going to put the pixel  
      54.   
      55.      // Figure out where in memory to put the pixel  
      56.      for (y = 100; y < 300; y++)  
      57.          for (x = 100; x < 300; x++) {  
      58.              location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +  
      59.                         (y+vinfo.yoffset) * finfo.line_length;  
      60.   
      61.              if (vinfo.bits_per_pixel == 32) {  
      62.                  *(fbp + location) = 100;        // Some blue  
      63.                  *(fbp + location + 1) = 15+(x-100)/2;     // A little green  
      64.                  *(fbp + location + 2) = 200-(y-100)/5;    // A lot of red  
      65.                  *(fbp + location + 3) = 0;      // No transparency  
      66.              } else  { //assume 16bpp  
      67.                  int b = 10;  
      68.                  int g = (x-100)/6;     // A little green  
      69.                  int r = 31-(y-100)/16;    // A lot of red  
      70.                  unsigned short int t = r<<11 | g << 5 | b;  
      71.                  *((unsigned short int*)(fbp + location)) = t;  
      72.              }  
      73.          }  
      74.      munmap(fbp, screensize);  
      75.      close(fbfd);  
      76.      return 0;  
      77.  }  

      这个是网上流行的用户测试程序,总结一下用户程序写屏步骤:
      1)打开设备节点
      2)获得fb_info的固定参数与可变参数结构体
      3)计算帧缓冲区大小
      4)调用mmap将缓冲区映射到进程的地址空间
      5)写屏
      这里注意fb_var_screeninfo中的可见分辨率xres,yres是屏的实际大小,即320,240。而虚拟分辨率xres_virtual,yres_virtual是虚拟窗口的大小。但是在s3c2440中被设为相等的。看源码:
      s3c24xxfb_probe函数中:

      [c-sharp]  view plain copy
      1. fbinfo->var.xres = display->xres;  
      2. fbinfo->var.yres = display->yres;  
      3. fbinfo->var.bits_per_pixel = display->bpp;  

      s3c2410fb_check_var函数中:

      [c-sharp]  view plain copy
      1. var->xres_virtual = display->xres;  
      2. var->yres_virtual = display->yres;  

      所以xres=xres_virtual,yres=yres_virtual。
      而实际的xres_virtual=xres + 2 * x_offset,yres_virtual = yres + 2 * y_offset。可以看s3c2440的datasheet,有一个图,

      看那个for循环,如果是在PC机上当然就是32BPP,R,G,B分别一个字节,另外一个字节空着或者干其他的事情,比如这里就用来表示透明度。如果在开发板上运行就是16BPP,在开发板上运行要改一下x,y的范围。在开发板上运行清屏命令:

      [c-sharp]  view plain copy
      1. dd if=/dev/zero of=/dev/fb0 bs=320 count=240   

      然后运行效果如下:


    转:http://blog.csdn.net/woshixingaaa/article/details/6452688


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值