Framebuffer驱动程序框架 skeletonfb.c 分析

Framebuffer驱动程序框架 skeletonfb.c 分析

最近想好好研究一下lcd驱动开发过程,lcd驱动开发主要就是framebuffer的编写了,这里只要想做framebuffer驱动的开发可能这里是必经之路,因为这里这个skeletnfb.c是framebuffer驱动程序开发的骨架,他没有具体去实现任何功能,没有针对任何设备,但是,他的作用却十分惊人,他就是使用说明文档一样,教你怎样一步一步进行framebuffer驱动编写

写贴出这个架构的实现过程,下面再慢慢分析:

具體在以下路徑:kernel/driver/video/skeletonfb.c

这里就开始慢慢啃一啃这个硬骨架吧
  1. /*
  2.  * linux/drivers/video/skeletonfb.-- Skeleton for a frame buffer device
  3.  *
  4.  * Modified to new api Jan 2001 by James Simmons (jsimmons@transvirtual.com)
  5.  *
  6.  * Created 28 Dec 1997 by Geert Uytterhoeven
  7.  *
  8.  *
  9.  * I have started rewriting this driver as a example of the upcoming new API
  10.  * The primary goal is to remove the console code from fbdev and place it
  11.  * into fbcon.c. This reduces the code and makes writing a new fbdev driver
  12.  * easy since the author doesn't need to worry about console internals. It
  13.  * also allows the ability to run fbdev without a console/tty system on top 
  14.  * of it. 
  15.  *
  16.  * First the roles of struct fb_info and struct display have changed. Struct
  17.  * display will go away. The way the new framebuffer console code will
  18.  * work is that it will act to translate data about the tty/console in 
  19.  * struct vc_data to data in a device independent way in struct fb_info. Then
  20.  * various functions in struct fb_ops will be called to store the device 
  21.  * dependent state in the par field in struct fb_info and to change the 
  22.  * hardware to that state. This allows a very clean separation of the fbdev
  23.  * layer from the console layer. It also allows one to use fbdev on its own
  24.  * which is a bounus for embedded devices. The reason this approach works is 
  25.  * for each framebuffer device when used as a tty/console device is allocated
  26.  * a set of virtual terminals to it. Only one virtual terminal can be active 
  27.  * per framebuffer device. We already have all the data we need in struct 
  28.  * vc_data so why store a bunch of colormaps and other fbdev specific data
  29.  * per virtual terminal. 
  30.  *
  31.  * As you can see doing this makes the con parameter pretty much useless
  32.  * for struct fb_ops functions, as it should be. Also having struct 
  33.  * fb_var_screeninfo and other data in fb_info pretty much eliminates the 
  34.  * need for get_fix and get_var. Once all drivers use the fix, var, and cmap
  35.  * fbcon can be written around these fields. This will also eliminate the
  36.  * need to regenerate struct fb_var_screeninfo, struct fb_fix_screeninfo
  37.  * struct fb_cmap every time get_var, get_fix, get_cmap functions are called
  38.  * as many drivers do now. 
  39.  *
  40.  * This file is subject to the terms and conditions of the GNU General Public
  41.  * License. See the file COPYING in the main directory of this archive for
  42.  * more details.
  43.  */

  44. #include <linux/module.h>
  45. #include <linux/kernel.h>
  46. #include <linux/errno.h>
  47. #include <linux/string.h>
  48. #include <linux/mm.h>
  49. #include <linux/slab.h>
  50. #include <linux/delay.h>
  51. #include <linux/fb.h>
  52. #include <linux/init.h>
  53. #include <linux/pci.h>

  54. /*
  55.  * This is just simple sample code.
  56.  * No warranty that it actually compiles.
  57.  * Even less warranty that it actually works :-)
  58.  */

  59. /*
  60.  * Driver data
  61.  */
  62. static char *mode_option __devinitdata;

  63. /*
  64.  * If your driver supports multiple boards, you should make the 
  65.  * below data types arrays, or allocate them dynamically (using kmalloc()). 
  66.  */ 

  67. /* 
  68.  * This structure defines the hardware state of the graphics card. Normally
  69.  * you place this in a header file in linux/include/video. This file usually
  70.  * also includes register information. That allows other driver subsystems
  71.  * and userland applications the ability to use the same header file to 
  72.  * avoid duplicate work and easy porting of software. 
  73.  */
  74. struct xxx_par;

  75. /*
  76.  * Here we define the default structs fb_fix_screeninfo and fb_var_screeninfo
  77.  * if we don't use modedb. If we do use modedb see xxxfb_init how to use it
  78.  * to get a fb_var_screeninfo. Otherwise define a default var as well. 
  79.  */
  80. static struct fb_fix_screeninfo xxxfb_fix __devinitdata = {
  81.     .id =        "FB's name", 
  82.     .type =        FB_TYPE_PACKED_PIXELS,
  83.     .visual =    FB_VISUAL_PSEUDOCOLOR,
  84.     .xpanstep =    1,
  85.     .ypanstep =    1,
  86.     .ywrapstep = 1, 
  87.     .accel =    FB_ACCEL_NONE,
  88. };

  89. /*
  90.  * Modern graphical hardware not only supports pipelines but some 
  91.  * also support multiple monitors where each display can have its 
  92.  * its own unique data. In this case each display could be 
  93.  * represented by a separate framebuffer device thus a separate 
  94.  * struct fb_info. Now the struct xxx_par represents the graphics
  95.  * hardware state thus only one exist per card. In this case the 
  96.  * struct xxx_par for each graphics card would be shared between 
  97.  * every struct fb_info that represents a framebuffer on that card. 
  98.  * This allows when one display changes it video resolution (info->var) 
  99.  * the other displays know instantly. Each display can always be
  100.  * aware of the entire hardware state that affects it because they share
  101.  * the same xxx_par struct. The other side of the coin is multiple
  102.  * graphics cards that pass data around until it is finally displayed
  103.  * on one monitor. Such examples are the voodoo 1 cards and high end
  104.  * NUMA graphics servers. For this case we have a bunch of pars, each
  105.  * one that represents a graphics state, that belong to one struct 
  106.  * fb_info. Their you would want to have *par point to a array of device
  107.  * states and have each struct fb_ops function deal with all those 
  108.  * states. I hope this covers every possible hardware design. If not
  109.  * feel free to send your ideas at jsimmons@users.sf.net 
  110.  */

  111. /*
  112.  * If your driver supports multiple boards or it supports multiple 
  113.  * framebuffers, you should make these arrays, or allocate them 
  114.  * dynamically using framebuffer_alloc() and free them with
  115.  * framebuffer_release().
  116.  */ 
  117. static struct fb_info info;

  118. /* 
  119.  * Each one represents the state of the hardware. Most hardware have
  120.  * just one hardware state. These here represent the default state(s). 
  121.  */
  122. static struct xxx_par __initdata current_par;

  123. int xxxfb_init(void);

  124. /**
  125.  *    xxxfb_open - Optional function. Called when the framebuffer is first accessed.
  126.  *    @info: frame buffer structure that represents a single frame buffer
  127.  *    @user: tell us if the userland (value=1) or the console is accessing the framebuffer. 
  128.  *
  129.  *    This function is the first function called in the framebuffer api.
  130.  *    Usually you don't need to provide this function. The case where it 
  131.  *    is used is to change from a text mode hardware state to a graphics
  132.  *     mode state. 
  133.  *
  134.  *    Returns negative errno on error, or zero on success.
  135.  */
  136. static int xxxfb_open(struct fb_info *info, int user)
  137. {
  138.     return 0;
  139. }

  140. /**
  141.  *    xxxfb_release - Optional function. Called when the framebuffer device is closed. 
  142.  *    @info: frame buffer structure that represents a single frame buffer
  143.  *    @user: tell us if the userland (value=1) or the console is accessing the framebuffer. 
  144.  *    
  145.  *    Thus function is called when we close /dev/fb or the framebuffer 
  146.  *    console system is released. Usually you don't need this function.
  147.  *    The case where it is usually used is to go from a graphics state
  148.  *    to a text mode state.
  149.  *
  150.  *    Returns negative errno on error, or zero on success.
  151.  */
  152. static int xxxfb_release(struct fb_info *info, int user)
  153. {
  154.     return 0;
  155. }

  156. /**
  157.  * xxxfb_check_var - Optional function. Validates a var passed in. 
  158.  * @var: frame buffer variable screen structure
  159.  * @info: frame buffer structure that represents a single frame buffer 
  160.  *    检查我们传入的var是否是硬件支持的属性,
  161.       这个方法不改变硬件,也就是fb_info中保存的data不会改变
  162.       这个方法用在我们只是想测试一下硬件,但并不是真正设置硬件属性
  163.  *    Checks to see if the hardware supports the state requested by
  164.  *    var passed in. This function does not alter the hardware 
  165.  *    This means the data stored in struct fb_info and struct xxx_par do 
  166.  *    not change. This includes the var inside of struct fb_info. 
  167.  *    Do NOT change these. This function can be called on its own if we
  168.  *    intent to only test a mode and not actually set it. The stuff in 
  169.  *    modedb.is a example of this. If the var passed in is slightly 
  170.  *    off by what the hardware can support then we alter the var PASSED in
  171.  *    to what we can do.
  172.  *
  173.  * For values that are off, this function must round them _up_ to the
  174.  * next value that is supported by the hardware. If the value is
  175.  * greater than the highest value supported by the hardware, then this
  176.  * function must return -EINVAL.
  177.  *
  178.  * Exception to the above rule: Some drivers have a fixed mode, ie,
  179.  * the hardware is already set at boot up, and cannot be changed. In
  180.  * this case, it is more acceptable that this function just return
  181.  * a copy of the currently working var (info->var). Better is to not
  182.  * implement this function, as the upper layer will do the copying
  183.  * of the current var for you.
  184.  *
  185.  * Note: This is the only function where the contents of var can be
  186.  * freely adjusted after the driver has been registered. If you find
  187.  * that you have code outside of this function that alters the content
  188.  * of var, then you are doing something wrong. Note also that the
  189.  * contents of info->var must be left untouched at all times after
  190.  * driver registration.
  191.  *
  192.  *    Returns negative errno on error, or zero on success.
  193.  */
  194. static int xxxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  195. {
  196.     /* ... */
  197.     return 0;         
  198. }

  199. /**
  200.  * xxxfb_set_par - Optional function. Alters the hardware state.
  201.  * @info: frame buffer structure that represents a single frame buffer
  202.  *    使用这个方法是用来设置framebuffer属性的,
  203.       会改变fb_info中的par和fb_fix_screeinfo数据,
  204.       但是不改变var的数据
  205.  *    Using the fb_var_screeninfo in fb_info we set the resolution of the
  206.  *    this particular framebuffer. This function alters the par AND the
  207.  *    fb_fix_screeninfo stored in fb_info. It doesn'not alter var in 
  208.  *    fb_info since we are using that data. This means we depend on the
  209.  *    data in var inside fb_info to be supported by the hardware. 
  210.  *
  211.  * This function is also used to recover/restore the hardware to a
  212.  * known working state.
  213.  *
  214.  *    xxxfb_check_var is always called before xxxfb_set_par to ensure that
  215.  * the contents of var is always valid.
  216.  *
  217.  *    Again if you can't change the resolution you don't need this function.
  218.  *
  219.  * However, even if your hardware does not support mode changing,
  220.  * a set_par might be needed to at least initialize the hardware to
  221.  * a known working state, especially if it came back from another
  222.  * process that also modifies the same hardware, such as X.
  223.  *
  224.  * If this is the case, a combination such as the following should work:
  225.  *
  226.  * static int xxxfb_check_var(struct fb_var_screeninfo *var,
  227.  * struct fb_info *info)
  228.  * {
  229.  * *var = info->var;
  230.  * return 0;
  231.  * }
  232.  *
  233.  * static int xxxfb_set_par(struct fb_info *info)
  234.  * {
  235.  * init your hardware here
  236.  * }
  237.  *
  238.  *    Returns negative errno on error, or zero on success.
  239.  */
  240. static int xxxfb_set_par(struct fb_info *info)
  241. {
  242.     struct xxx_par *par = info->par;
  243.     /* ... */
  244.     return 0;    
  245. }

  246. /**
  247.  *     xxxfb_setcolreg - Optional function. Sets a color register.
  248.  * @regno: Which register in the CLUT we are programming 
  249.  * @red: The red value which can be up to 16 bits wide 
  250.  *    @green: The green value which can be up to 16 bits wide 
  251.  *    @blue: The blue value which can be up to 16 bits wide.
  252.  *    @transp: If supported, the alpha value which can be up to 16 bits wide.
  253.  * @info: frame buffer info structure
  254.  * 
  255.  *     Set a single color register. The values supplied have a 16 bit
  256.  *     magnitude which needs to be scaled in this function for the hardware. 
  257.  *    Things to take into consideration are how many color registers, if
  258.  *    any, are supported with the current color visual. With truecolor mode
  259.  *    no color palettes are supported. Here a pseudo palette is created
  260.  *    which we store the value in pseudo_palette in struct fb_info. For
  261.  *    pseudocolor mode we have a limited color palette. To deal with this
  262.  *    we can program what color is displayed for a particular pixel value.
  263.  *    DirectColor is similar in that we can program each color field. If
  264.  *    we have a static colormap we don't need to implement this function. 
  265.  * 
  266.  *    Returns negative errno on error, or zero on success.
  267.  */
  268. static int xxxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  269.              unsigned blue, unsigned transp,
  270.              struct fb_info *info)
  271. {
  272.     if (regno >= 256) /* no. of hw registers */
  273.        return -EINVAL;
  274.     /*
  275.      * Program hardware... do anything you want with transp
  276.      */

  277.     /* grayscale works only partially under directcolor */
  278.     if (info->var.grayscale) {
  279.        /* grayscale = 0.30*+ 0.59*+ 0.11**/
  280.        red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
  281.     }

  282.     /* Directcolor:
  283.      * var->{color}.offset contains start of bitfield
  284.      * var->{color}.length contains length of bitfield
  285.      * {hardwarespecific} contains width of DAC
  286.      * pseudo_palette[X] is programmed to (<< red.offset) |
  287.      * (<< green.offset) |
  288.      * (<< blue.offset)
  289.      * RAMDAC[X] is programmed to (red, green, blue)
  290.      * color depth = SUM(var->{color}.length)
  291.      *
  292.      *     Pseudocolor:
  293.      * var->{color}.offset is 0 unless the palette index takes less than
  294.      * bits_per_pixel bits and is stored in the upper
  295.      * bits of the pixel value
  296.      * var->{color}.length is set so that 1 << length is the number of
  297.      * available palette entries
  298.      * pseudo_palette is not used
  299.      * RAMDAC[X] is programmed to (red, green, blue)
  300.      * color depth = var->{color}.length
  301.      *
  302.      *     Static pseudocolor:
  303.      * same as Pseudocolor, but the RAMDAC is not programmed (read-only)
  304.      *
  305.      *     Mono01/Mono10:
  306.      * Has only 2 values, black on white or white on black (fg on bg),
  307.      * var->{color}.offset is 0
  308.      * white = (<< var->{color}.length) - 1, black = 0
  309.      * pseudo_palette is not used
  310.      * RAMDAC does not exist
  311.      * color depth is always 2
  312.      *
  313.      *     Truecolor:
  314.      * does not use RAMDAC (usually has 3 of them).
  315.      * var->{color}.offset contains start of bitfield
  316.      * var->{color}.length contains length of bitfield
  317.      * pseudo_palette is programmed to (red << red.offset) |
  318.      * (green << green.offset) |
  319.      * (blue << blue.offset) |
  320.      * (transp << transp.offset)
  321.      * RAMDAC does not exist
  322.      * color depth = SUM(var->{color}.length})
  323.      *
  324.      *     The color depth is used by fbcon for choosing the logo and also
  325.      *     for color palette transformation if color depth < 4
  326.      *
  327.      *    As can be seen from the above, the field bits_per_pixel is _NOT_
  328.      *    a criteria for describing the color visual.
  329.      *
  330.      *    A common mistake is assuming that bits_per_pixel <= 8 is pseudocolor,
  331.      *    and higher than that, true/directcolor. This is incorrect, one needs
  332.      *    to look at the fix->visual.
  333.      *
  334.      *     Another common mistake is using bits_per_pixel to calculate the color
  335.      *     depth. The bits_per_pixel field does not directly translate to color
  336.      *     depth. You have to compute for the color depth (using the color
  337.      *     bitfields) and fix->visual as seen above.
  338.      */

  339.     /*
  340.      *     This is the point where the color is converted to something that
  341.      *     is acceptable by the hardware.
  342.      */
  343. #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
  344.     red = CNVT_TOHW(red, info->var.red.length);
  345.     green = CNVT_TOHW(green, info->var.green.length);
  346.     blue = CNVT_TOHW(blue, info->var.blue.length);
  347.     transp = CNVT_TOHW(transp, info->var.transp.length);
  348. #undef CNVT_TOHW
  349.     /*
  350.      * This is the point where the function feeds the color to the hardware
  351.      * palette after converting the colors to something acceptable by
  352.      * the hardware. Note, only FB_VISUAL_DIRECTCOLOR and
  353.      * FB_VISUAL_PSEUDOCOLOR visuals need to write to the hardware palette.
  354.      * If you have code that writes to the hardware CLUT, and it'not
  355.      * any of the above visuals, then you are doing something wrong.
  356.      */
  357.     if (info->fix.visual == FB_VISUAL_DIRECTCOLOR ||
  358.     info->fix.visual == FB_VISUAL_TRUECOLOR)
  359.      write_{red|green|blue|transp}_to_clut();

  360.     /*This is the point were you need to fill up the contents of
  361.      * info->pseudo_palette. This structure is used _only_ by fbcon, thus
  362.      * it only contains 16 entries to match the number of colors supported
  363.      * by the console. The pseudo_palette is used only if the visual is
  364.      * in directcolor or truecolor mode. With other visuals, the
  365.      * pseudo_palette is not used. (This might change in the future.)
  366.      *
  367.      * The contents of the pseudo_palette is in raw pixel format. Ie, each
  368.      * entry can be written directly to the framebuffer without any conversion.
  369.      * The pseudo_palette is (void *). However, if using the generic
  370.      * drawing functions (cfb_imageblit, cfb_fillrect), the pseudo_palette
  371.      * must be casted to (u32 *) _regardless_ of the bits per pixel. If the
  372.      * driver is using its own drawing functions, then it can use whatever
  373.      * size it wants.
  374.      */
  375.     if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
  376.     info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  377.      u32 v;

  378.      if (regno >= 16)
  379.          return -EINVAL;

  380.      v = (red << info->var.red.offset) |
  381.          (green << info->var.green.offset) |
  382.          (blue << info->var.blue.offset) |
  383.          (transp << info->var.transp.offset);

  384.      ((u32*)(info->pseudo_palette))[regno] = v;
  385.     }

  386.     /* ... */
  387.     return 0;
  388. }

  389. /**
  390.  * xxxfb_pan_display - NOT a required function. Pans the display.
  391.  * @var: frame buffer variable screen structure
  392.  * @info: frame buffer structure that represents a single frame buffer
  393.  *
  394.  * Pan (or wrap, depending on the `vmode' field) the display using the
  395.  * 'xoffset' and `yoffset' fields of the `var' structure.
  396.  * If the values don't fit, return -EINVAL.
  397.  *
  398.  * Returns negative errno on error, or zero on success.
  399.  */
  400. static int xxxfb_pan_display(struct fb_var_screeninfo *var,
  401.              struct fb_info *info)
  402. {
  403.     /*
  404.      * If your hardware does not support panning, _do_ _not_ implement this
  405.      * function. Creating a dummy function will just confuse user apps.
  406.      */

  407.     /*
  408.      * Note that even if this function is fully functional, a setting of
  409.      * 0 in both xpanstep and ypanstep means that this function will never
  410.      * get called.
  411.      */

  412.     /* ... */
  413.     return 0;
  414. }

  415. /**
  416.  * xxxfb_blank - NOT a required function. Blanks the display.
  417.  * @blank_mode: the blank mode we want. 
  418.  * @info: frame buffer structure that represents a single frame buffer
  419.  *
  420.  * Blank the screen if blank_mode != FB_BLANK_UNBLANK, else unblank.
  421.  * Return 0 if blanking succeeded, != 0 if un-/blanking failed due to
  422.  * e.g. a video mode which doesn't support it.
  423.  *
  424.  * Implements VESA suspend and powerdown modes on hardware that supports
  425.  * disabling hsync/vsync:
  426.  *
  427.  * FB_BLANK_NORMAL = display is blanked, syncs are on.
  428.  * FB_BLANK_HSYNC_SUSPEND = hsync off
  429.  * FB_BLANK_VSYNC_SUSPEND = vsync off
  430.  * FB_BLANK_POWERDOWN = hsync and vsync off
  431.  *
  432.  * If implementing this function, at least support FB_BLANK_UNBLANK.
  433.  * Return !for any modes that are unimplemented.
  434.  *
  435.  */
  436. static int xxxfb_blank(int blank_mode, struct fb_info *info)
  437. {
  438.     /* ... */
  439.     return 0;
  440. }

  441. /* ------------ Accelerated Functions --------------------- */

  442. /*
  443.  * We provide our own functions if we have hardware acceleration
  444.  * or non packed pixel format layouts. If we have no hardware 
  445.  * acceleration, we can use a generic unaccelerated function. If using
  446.  * a pack pixel format just use the functions in cfb_*.c. Each file 
  447.  * has one of the three different accel functions we support.
  448.  */

  449. /**
  450.  * xxxfb_fillrect - REQUIRED function. Can use generic routines if 
  451.  *              non acclerated hardware and packed pixel based.
  452.  *             Draws a rectangle on the screen.        
  453.  *
  454.  * @info: frame buffer structure that represents a single frame buffer
  455.  *    @region: The structure representing the rectangular region we 
  456.  *         wish to draw to.
  457.  *
  458.  *    This drawing operation places/removes a retangle on the screen 
  459.  *    depending on the rastering operation with the value of color which
  460.  *    is in the current color depth format.
  461.  */
  462. void xxxfb_fillrect(struct fb_info *p, const struct fb_fillrect *region)
  463. {
  464. /*    Meaning of struct fb_fillrect
  465.  *
  466.  *    @dx: The x and y corrdinates of the upper left hand corner of the 
  467.  *    @dy: area we want to draw to. 
  468.  *    @width: How wide the rectangle is we want to draw.
  469.  *    @height: How tall the rectangle is we want to draw.
  470.  *    @color:    The color to fill in the rectangle with. 
  471.  *    @rop: The raster operation. We can draw the rectangle with a COPY
  472.  *     of XOR which provides erasing effect. 
  473.  */
  474. }

  475. /**
  476.  * xxxfb_copyarea - REQUIRED function. Can use generic routines if
  477.  * non acclerated hardware and packed pixel based.
  478.  * Copies one area of the screen to another area.
  479.  *
  480.  * @info: frame buffer structure that represents a single frame buffer
  481.  * @area: Structure providing the data to copy the framebuffer contents
  482.  *     from one region to another.
  483.  *
  484.  * This drawing operation copies a rectangular area from one area of the
  485.  *    screen to another area.
  486.  */
  487. void xxxfb_copyarea(struct fb_info *p, const struct fb_copyarea *area) 
  488. {
  489. /*
  490.  * @dx: The x and y coordinates of the upper left hand corner of the
  491.  *    @dy: destination area on the screen.
  492.  * @width: How wide the rectangle is we want to copy.
  493.  * @height: How tall the rectangle is we want to copy.
  494.  * @sx: The x and y coordinates of the upper left hand corner of the
  495.  * @sy: source area on the screen.
  496.  */
  497. }


  498. /**
  499.  * xxxfb_imageblit - REQUIRED function. Can use generic routines if
  500.  * non acclerated hardware and packed pixel based.
  501.  * Copies a image from system memory to the screen. 
  502.  *
  503.  * @info: frame buffer structure that represents a single frame buffer
  504.  *    @image:    structure defining the image.
  505.  *
  506.  * This drawing operation draws a image on the screen. It can be a 
  507.  *    mono image (needed for font handling) or a color image (needed for
  508.  *    tux). 
  509.  */
  510. void xxxfb_imageblit(struct fb_info *p, const struct fb_image *image) 
  511. {
  512. /*
  513.  * @dx: The x and y coordinates of the upper left hand corner of the
  514.  *    @dy: destination area to place the image on the screen.
  515.  * @width: How wide the image is we want to copy.
  516.  * @height: How tall the image is we want to copy.
  517.  * @fg_color: For mono bitmap images this is color data for 
  518.  * @bg_color: the foreground and background of the image to
  519.  *         write directly to the frmaebuffer.
  520.  *    @depth:    How many bits represent a single pixel for this image.
  521.  *    @data: The actual data used to construct the image on the display.
  522.  *    @cmap: The colormap used for color images. 
  523.  */

  524. /*
  525.  * The generic function, cfb_imageblit, expects that the bitmap scanlines are
  526.  * padded to the next byte. Most hardware accelerators may require padding to
  527.  * the next u16 or the next u32. If that is the case, the driver can specify
  528.  * this by setting info->pixmap.scan_align = 2 or 4. See a more
  529.  * comprehensive description of the pixmap below.
  530.  */
  531. }

  532. /**
  533.  *    xxxfb_cursor -     OPTIONAL. If your hardware lacks support
  534.  *            for a cursor, leave this field NULL.
  535.  *
  536.  * @info: frame buffer structure that represents a single frame buffer
  537.  *    @cursor: structure defining the cursor to draw.
  538.  *
  539.  * This operation is used to set or alter the properities of the cursor.
  540.  *
  541.  *    Returns negative errno on error, or zero on success.
  542.  */
  543. int xxxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
  544. {
  545. /*
  546.  * @set:     Which fields we are altering in struct fb_cursor 
  547.  *    @enable: Disable or enable the cursor 
  548.  * @rop:     The bit operation we want to do. 
  549.  * @mask: This is the cursor mask bitmap. 
  550.  * @dest: A image of the area we are going to display the cursor.
  551.  *        Used internally by the driver.     
  552.  * @hot:    The hot spot. 
  553.  *    @image:    The actual data for the cursor image.
  554.  *
  555.  * NOTES ON FLAGS (cursor->set):
  556.  *
  557.  * FB_CUR_SETIMAGE - the cursor image has changed (cursor->image.data)
  558.  * FB_CUR_SETPOS - the cursor position has changed (cursor->image.dx|dy)
  559.  * FB_CUR_SETHOT - the cursor hot spot has changed (cursor->hot.dx|dy)
  560.  * FB_CUR_SETCMAP - the cursor colors has changed (cursor->fg_color|bg_color)
  561.  * FB_CUR_SETSHAPE - the cursor bitmask has changed (cursor->mask)
  562.  * FB_CUR_SETSIZE - the cursor size has changed (cursor->width|height)
  563.  * FB_CUR_SETALL - everything has changed
  564.  *
  565.  * NOTES ON ROPs (cursor->rop, Raster Operation)
  566.  *
  567.  * ROP_XOR - cursor->image.data XOR cursor->mask
  568.  * ROP_COPY - curosr->image.data AND cursor->mask
  569.  *
  570.  * OTHER NOTES:
  571.  *
  572.  * - fbcon only supports a 2-color cursor (cursor->image.depth = 1)
  573.  * - The fb_cursor structure, @cursor, _will_ always contain valid
  574.  * fields, whether any particular bitfields in cursor->set is set
  575.  * or not.
  576.  */
  577. }

  578. /**
  579.  *    xxxfb_rotate - NOT a required function. If your hardware
  580.  *            supports rotation the whole screen then 
  581.  *            you would provide a hook for this. 
  582.  *
  583.  * @info: frame buffer structure that represents a single frame buffer
  584.  *    @angle: The angle we rotate the screen. 
  585.  *
  586.  * This operation is used to set or alter the properities of the
  587.  *    cursor.
  588.  */
  589. void xxxfb_rotate(struct fb_info *info, int angle)
  590. {
  591. /* Will be deprecated */
  592. }

  593. /**
  594.  *    xxxfb_sync - NOT a required function. Normally the accel engine 
  595.  *         for a graphics card take a specific amount of time.
  596.  *         Often we have to wait for the accelerator to finish
  597.  *         its operation before we can write to the framebuffer
  598.  *         so we can have consistent display output. 
  599.  *
  600.  * @info: frame buffer structure that represents a single frame buffer
  601.  *
  602.  * If the driver has implemented its own hardware-based drawing function,
  603.  * implementing this function is highly recommended.
  604.  */
  605. int xxxfb_sync(struct fb_info *info)
  606. {
  607.     return 0;
  608. }

  609.     /*
  610.      * Frame buffer operations
  611.      */

  612. static struct fb_ops xxxfb_ops = {
  613.     .owner        = THIS_MODULE,
  614.     .fb_open    = xxxfb_open,
  615.     .fb_read    = xxxfb_read,
  616.     .fb_write    = xxxfb_write,
  617.     .fb_release    = xxxfb_release,
  618.     .fb_check_var    = xxxfb_check_var,
  619.     .fb_set_par    = xxxfb_set_par,
  620.     .fb_setcolreg    = xxxfb_setcolreg,
  621.     .fb_blank    = xxxfb_blank,
  622.     .fb_pan_display    = xxxfb_pan_display,
  623.     .fb_fillrect    = xxxfb_fillrect,     /* Needed !!! */
  624.     .fb_copyarea    = xxxfb_copyarea,    /* Needed !!! */
  625.     .fb_imageblit    = xxxfb_imageblit,    /* Needed !!! */
  626.     .fb_cursor    = xxxfb_cursor,        /* Optional !!! */
  627.     .fb_rotate    = xxxfb_rotate,
  628.     .fb_sync    = xxxfb_sync,
  629.     .fb_ioctl    = xxxfb_ioctl,
  630.     .fb_mmap    = xxxfb_mmap,
  631. };

  632. /* ------------------------------------------------------------------------- */

  633. /*
  634.  * Initialization
  635.  */
  636. 这里他们的定义是不一样的,唯一的不同
  637. /* static int __init xxfb_probe (struct platform_device *pdev) -- for platform devs */
  638. static int __devinit xxxfb_probe(struct pci_dev *dev, const struct pci_device_id *ent)
  639. {
  640.     struct fb_info *info;
  641.     struct xxx_par *par;
  642.     struct device *device = &dev->dev; /* or &pdev->dev */
  643.     int cmap_len, retval;    
  644.    
  645.     /*
  646.      * Dynamically allocate info and par
  647.      */
  648.     info = framebuffer_alloc(sizeof(struct xxx_par), device);

  649.     if (!info) {
  650.      /* goto error path */
  651.     }

  652.     par = info->par;把我们驱动中的私有数据指向info->par,我们在上面已经通过framebuffer_alloc为他申请了内存

  653.     /* 
  654.      * Here we set the screen_base to the virtual memory address
  655.      * for the framebuffer. Usually we obtain the resource address
  656.      * from the bus layer and then translate it to virtual memory
  657.      * space via ioremap. Consult ioport.h. 
  658.      */
  659.     这里注释也已经说的很清楚了,
  660.     我们通常通过注册好的设备信息获取到memory address,
  661.     然后把这个地址转换为virtual memory通过ioremap这个方法
  662.     info->screen_base = framebuffer_virtual_memory;
  663.     info->fbops = &xxxfb_ops
  664.     这个定义好的xxxfb_ops结构中包含了用户空间访问driver的所有方法,
  665.     用户访问/dev/fb0最终通过fbmem中注册的方法调用这里的方法
  666.     info->fix = xxxfb_fix; /* this will be the only time xxxfb_fix will be used, so mark it as __devinitdata */
  667.     info->pseudo_palette = pseudo_palette; /* The pseudopalette is an 16-member array*/
  668.     /*
  669.      * Set up flags to indicate what sort of acceleration your
  670.      * driver can provide (pan/wrap/copyarea/etc.) and whether it
  671.      * is a module -- see FBINFO_* in include/linux/fb.h
  672.      *
  673.      * If your hardware can support any of the hardware accelerated functions
  674.      * fbcon performance will improve if info->flags is set properly.
  675.      *
  676.      * FBINFO_HWACCEL_COPYAREA - hardware moves
  677.      * FBINFO_HWACCEL_FILLRECT - hardware fills
  678.      * FBINFO_HWACCEL_IMAGEBLIT - hardware mono->color expansion
  679.      * FBINFO_HWACCEL_YPAN - hardware can pan display in y-axis
  680.      * FBINFO_HWACCEL_YWRAP - hardware can wrap display in y-axis
  681.      * FBINFO_HWACCEL_DISABLED - supports hardware accels, but disabled
  682.      * FBINFO_READS_FAST - if set, prefer moves over mono->color expansion
  683.      * FBINFO_MISC_TILEBLITTING - hardware can do tile blits
  684.      *
  685.      * NOTE: These are for fbcon use only.
  686.      */
  687.     info->flags = FBINFO_DEFAULT;

  688. /********************* This stage is optional ******************************/
  689.      /*
  690.      * The struct pixmap is a scratch pad for the drawing functions. This
  691.      * is where the monochrome bitmap is constructed by the higher layers
  692.      * and then passed to the accelerator. For drivers that uses
  693.      * cfb_imageblit, you can skip this part. For those that have a more
  694.      * rigorous requirement, this stage is needed
  695.      */

  696.     /* PIXMAP_SIZE should be small enough to optimize drawing, but not
  697.      * large enough that memory is wasted. A safe size is
  698.      * (max_xres * max_font_height/8). max_xres is driver dependent,
  699.      * max_font_height is 32.
  700.      */
  701.     info->pixmap.addr = kmalloc(PIXMAP_SIZE, GFP_KERNEL);
  702.     if (!info->pixmap.addr) {
  703.      /* goto error */
  704.     }

  705.     info->pixmap.size = PIXMAP_SIZE;

  706.     /*
  707.      * FB_PIXMAP_SYSTEM - memory is in system ram
  708.      * FB_PIXMAP_IO - memory is iomapped
  709.      * FB_PIXMAP_SYNC - if set, will call fb_sync() per access to pixmap,
  710.      * usually if FB_PIXMAP_IO is set.
  711.      *
  712.      * Currently, FB_PIXMAP_IO is unimplemented.
  713.      */
  714.     info->pixmap.flags = FB_PIXMAP_SYSTEM;

  715.     /*
  716.      * scan_align is the number of padding for each scanline. It is in bytes.
  717.      * Thus for accelerators that need padding to the next u32, put 4 here.
  718.      */
  719.     info->pixmap.scan_align = 4;

  720.     /*
  721.      * buf_align is the amount to be padded for the buffer. For example,
  722.      * the i810fb needs a scan_align of 2 but expects it to be fed with
  723.      * dwords, so a buf_align = 4 is required.
  724.      */
  725.     info->pixmap.buf_align = 4;

  726.     /* access_align is how many bits can be accessed from the framebuffer
  727.      * ie. some epson cards allow 16-bit access only. Most drivers will
  728.      * be safe with u32 here.
  729.      *
  730.      * NOTE: This field is currently unused.
  731.      */
  732.     info->pixmap.access_align = 32;
  733. /***************************** End optional stage ***************************/

  734.     /*
  735.      * This should give a reasonable default video mode. The following is
  736.      * done when we can set a video mode. 
  737.      */
  738.     if (!mode_option)
  739.     mode_option = "640x480@60";         

  740.     retval = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
  741.   
  742.     if (!retval || retval == 4)
  743.     return -EINVAL;            

  744.     /* This has to be */
  745.     if (fb_alloc_cmap(&info->cmap, cmap_len, 0))
  746.     return -ENOMEM;
  747.     
  748.     /* 
  749.      * The following is done in the case of having hardware with a static 
  750.      * mode. If we are setting the mode ourselves we don'call this. 
  751.      */    
  752.     info->var = xxxfb_var;

  753.     /*
  754.      * For drivers that can...
  755.      */
  756.     xxxfb_check_var(&info->var, info);

  757.     /*
  758.      * Does a call to fb_set_par() before register_framebuffer needed? This
  759.      * will depend on you and the hardware. If you are sure that your driver
  760.      * is the only device in the system, a call to fb_set_par() is safe.
  761.      *
  762.      * Hardware in x86 systems has a VGA core. Calling set_par() at this
  763.      * point will corrupt the VGA console, so it might be safer to skip a
  764.      * call to set_par here and just allow fbcon to do it for you.
  765.      */
  766.     /* xxxfb_set_par(info); */

  767.     if (register_framebuffer(info) < 0) {
  768.     注册设备,fbmem中负责注册了一个fb 字符设备,
  769.     只有调用了这个方法,才真正创建了这个fb字符设备
  770.     fb_dealloc_cmap(&info->cmap);
  771.     return -EINVAL;
  772.     }
  773.     printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
  774.      info->fix.id);
  775.     pci_set_drvdata(dev, info); /* or platform_set_drvdata(pdev, info) */
  776.     方便后面使用info这个结构中的所有数据
  777.     return 0;
  778. }

  779.     /*
  780.      * Cleanup
  781.      */
  782. /* static void __devexit xxxfb_remove(struct platform_device *pdev) */
  783. static void __devexit xxxfb_remove(struct pci_dev *dev)
  784. {
  785.     struct fb_info *info = pci_get_drvdata(dev);
  786.     /* or platform_get_drvdata(pdev); */

  787.     if (info) {
  788.         unregister_framebuffer(info);
  789.         fb_dealloc_cmap(&info->cmap);
  790.         /* ... */
  791.         framebuffer_release(info);
  792.     }
  793. }

  794. #ifdef CONFIG_PCI
  795. #ifdef CONFIG_PM
  796. /**
  797.  *    xxxfb_suspend - Optional but recommended function. Suspend the device.
  798.  *    @dev: PCI device
  799.  *    @msg: the suspend event code.
  800.  *
  801.  * See Documentation/power/devices.txt for more information
  802.  */
  803. static int xxxfb_suspend(struct pci_dev *dev, pm_message_t msg)
  804. {
  805.     struct fb_info *info = pci_get_drvdata(dev);
  806.     struct xxxfb_par *par = info->par;

  807.     /* suspend here */
  808.     return 0;
  809. }

  810. /**
  811.  *    xxxfb_resume - Optional but recommended function. Resume the device.
  812.  *    @dev: PCI device
  813.  *
  814.  * See Documentation/power/devices.txt for more information
  815.  */
  816. static int xxxfb_resume(struct pci_dev *dev)
  817. {
  818.     struct fb_info *info = pci_get_drvdata(dev);
  819.     struct xxxfb_par *par = info->par;

  820.     /* resume here */
  821.     return 0;
  822. }
  823. #else
  824. #define xxxfb_suspend NULL
  825. #define xxxfb_resume NULL
  826. #endif /* CONFIG_PM */

  827. static struct pci_device_id xxxfb_id_table[] = {
  828.     { PCI_VENDOR_ID_XXX, PCI_DEVICE_ID_XXX,
  829.      PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
  830.      PCI_CLASS_MASK, 0 },
  831.     { 0, }
  832. };

  833. /* For PCI drivers */
  834. static struct pci_driver xxxfb_driver = {
  835.     .name =        "xxxfb",
  836.     .id_table =    xxxfb_id_table,
  837.     .probe =    xxxfb_probe,
  838.     .remove =    __devexit_p(xxxfb_remove),
  839.     .suspend = xxxfb_suspend, /* optional but recommended */
  840.     .resume = xxxfb_resume, /* optional but recommended */
  841. };

  842. MODULE_DEVICE_TABLE(pci, xxxfb_id_table);

  843. int __init xxxfb_init(void)
  844. {
  845.     /*
  846.      * For kernel boot options (in 'video=xxxfb:<options>' format)
  847.      */
  848. #ifndef MODULE
  849.     char *option = NULL;

  850.     if (fb_get_options("xxxfb", &option))
  851.         return -ENODEV;
  852.     xxxfb_setup(option);
  853. #endif

  854.     return pci_register_driver(&xxxfb_driver);
  855. }

  856. static void __exit xxxfb_exit(void)
  857. {
  858.     pci_unregister_driver(&xxxfb_driver);
  859. }
  860. #else /* non PCI, platform drivers */
  861. #include <linux/platform_device.h>
  862. /* for platform devices */

  863. #ifdef CONFIG_PM
  864. /**
  865.  *    xxxfb_suspend - Optional but recommended function. Suspend the device.
  866.  *    @dev: platform device
  867.  *    @msg: the suspend event code.
  868.  *
  869.  * See Documentation/power/devices.txt for more information
  870.  */
  871. static int xxxfb_suspend(struct platform_device *dev, pm_message_t msg)
  872. {
  873.     struct fb_info *info = platform_get_drvdata(dev);
  874.     struct xxxfb_par *par = info->par;

  875.     /* suspend here */
  876.     return 0;
  877. }

  878. /**
  879.  *    xxxfb_resume - Optional but recommended function. Resume the device.
  880.  *    @dev: platform device
  881.  *
  882.  * See Documentation/power/devices.txt for more information
  883.  */
  884. static int xxxfb_resume(struct platform_dev *dev)
  885. {
  886.     struct fb_info *info = platform_get_drvdata(dev);
  887.     struct xxxfb_par *par = info->par;

  888.     /* resume here */
  889.     return 0;
  890. }
  891. #else
  892. #define xxxfb_suspend NULL
  893. #define xxxfb_resume NULL
  894. #endif /* CONFIG_PM */

  895. static struct platform_device_driver xxxfb_driver = {
  896.     .probe = xxxfb_probe,
  897.     .remove = xxxfb_remove,
  898.     .suspend = xxxfb_suspend, /* optional but recommended */
  899.     .resume = xxxfb_resume, /* optional but recommended */
  900.     .driver = {
  901.         .name = "xxxfb",
  902.     },
  903. };

  904. static struct platform_device *xxxfb_device;

  905. #ifndef MODULE
  906.     /*
  907.      * Setup
  908.      */

  909. /*
  910.  * Only necessary if your driver takes special options,
  911.  * otherwise we fall back on the generic fb_setup().
  912.  */
  913. int __init xxxfb_setup(char *options)
  914. {
  915.     /* Parse user speficied options (`video=xxxfb:') */
  916. }
  917. #endif /* MODULE */

  918. static int __init xxxfb_init(void)
  919. {
  920.     int ret;
  921.     /*
  922.      * For kernel boot options (in 'video=xxxfb:<options>' format)
  923.      */
  924. #ifndef MODULE
  925.     char *option = NULL;

  926.     if (fb_get_options("xxxfb", &option))
  927.         return -ENODEV;
  928.     xxxfb_setup(option);
  929. #endif
  930.     ret = platform_driver_register(&xxxfb_driver);

  931.     if (!ret) {
  932.         xxxfb_device = platform_device_register_simple("xxxfb", 0,
  933.                                 NULL, 0);

  934.         if (IS_ERR(xxxfb_device)) {
  935.             platform_driver_unregister(&xxxfb_driver);
  936.             ret = PTR_ERR(xxxfb_device);
  937.         }
  938.     }

  939.     return ret;
  940. }

  941. static void __exit xxxfb_exit(void)
  942. {
  943.     platform_device_unregister(xxxfb_device);
  944.     platform_driver_unregister(&xxxfb_driver);
  945. }
  946. #endif /* CONFIG_PCI */

  947. /* ------------------------------------------------------------------------- */


  948.     /*
  949.      * Modularization
  950.      */

  951. module_init(xxxfb_init);
  952. module_exit(xxxfb_remove);

  953. MODULE_LICENSE("GPL");
这里就开始慢慢啃一啃这个硬骨架吧

毫无疑问,先找到module_init(xxxfb_init),xxxfb_init这个方式是这个驱动的入口,我们要找到这个方法,代码中我已经标注出来,原来在这个架构中采用了两种方法来实现framebuffer驱动,分别是pci子系统方式和platform子系统方式,这里他们的实现方法基本相似,只是在匹配device时有所不同,我们没有必要过于关注这里,大家也可以看到,他们的proble方法是一样的,这个proble才是我们要真正好好分析的

第一件事就是为fb_info动态申请内存空间,很有必要分析一下这个方法

/**
 * framebuffer_alloc - creates a new frame buffer info structure
 *
 * @size: size of driver private data, can be zero
 * @dev: pointer to the device for this fb, this can be NULL
 *
 * Creates a new frame buffer info structure. Also reserves @size bytes
 * for driver private data (info->par). info->par (if any) will be
 * aligned to sizeof(long).
 *
 * Returns the new structure, or NULL if an error occured.
 *
 */
struct fb_info *framebuffer_alloc(size_t size, struct device *dev)
{
#define BYTES_PER_LONG (BITS_PER_LONG/8)
#define PADDING (BYTES_PER_LONG - (sizeof(struct fb_info) % BYTES_PER_LONG))
    int fb_info_size = sizeof(struct fb_info);
    struct fb_info *info;
    char *p;

    if (size)
        fb_info_size += PADDING;

    p = kzalloc(fb_info_size + size, GFP_KERNEL);

    if (!p)
        return NULL;

    info = (struct fb_info *) p;

    if (size)
        info->par = p + fb_info_size;

    info->device = dev;

#ifdef CONFIG_FB_BACKLIGHT
    mutex_init(&info->bl_curve_mutex);
#endif

    return info;
#undef PADDING
#undef BYTES_PER_LONG
}

其实这里注释也已经说清楚了,这个方法不止为fb_info申请了内存空间,还为fb_info中的一个void型指针par分配了内存空间,这个par将会保存之后在这个驱动中的私有变量,是一个结构,保存了很多driver的相关信息,保存到par中,之后想要使用的时候就很容易得到。

这个架构中的其他方法都没有实现,他们就是访问driver时候具体的处理函数,就是要我们要去花大力气去做的事情,之后在真正的framebuffer驱动程序中再去好好分析这些方法吧。

附上不錯的一篇文章:http://www.linuxgraphics.cn/graphics/writing_lcd_driver.html

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值