162306a36Sopenharmony_ci=======================
262306a36Sopenharmony_ciThe Frame Buffer Device
362306a36Sopenharmony_ci=======================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciLast revised: May 10, 2001
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci0. Introduction
962306a36Sopenharmony_ci---------------
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ciThe frame buffer device provides an abstraction for the graphics hardware. It
1262306a36Sopenharmony_cirepresents the frame buffer of some video hardware and allows application
1362306a36Sopenharmony_cisoftware to access the graphics hardware through a well-defined interface, so
1462306a36Sopenharmony_cithe software doesn't need to know anything about the low-level (hardware
1562306a36Sopenharmony_ciregister) stuff.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ciThe device is accessed through special device nodes, usually located in the
1862306a36Sopenharmony_ci/dev directory, i.e. /dev/fb*.
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci1. User's View of /dev/fb*
2262306a36Sopenharmony_ci--------------------------
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ciFrom the user's point of view, the frame buffer device looks just like any
2562306a36Sopenharmony_ciother device in /dev. It's a character device using major 29; the minor
2662306a36Sopenharmony_cispecifies the frame buffer number.
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciBy convention, the following device nodes are used (numbers indicate the device
2962306a36Sopenharmony_ciminor numbers)::
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci      0 = /dev/fb0	First frame buffer
3262306a36Sopenharmony_ci      1 = /dev/fb1	Second frame buffer
3362306a36Sopenharmony_ci	  ...
3462306a36Sopenharmony_ci     31 = /dev/fb31	32nd frame buffer
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciFor backwards compatibility, you may want to create the following symbolic
3762306a36Sopenharmony_cilinks::
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci    /dev/fb0current -> fb0
4062306a36Sopenharmony_ci    /dev/fb1current -> fb1
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciand so on...
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ciThe frame buffer devices are also `normal` memory devices, this means, you can
4562306a36Sopenharmony_ciread and write their contents. You can, for example, make a screen snapshot by::
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci  cp /dev/fb0 myfile
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciThere also can be more than one frame buffer at a time, e.g. if you have a
5062306a36Sopenharmony_cigraphics card in addition to the built-in hardware. The corresponding frame
5162306a36Sopenharmony_cibuffer devices (/dev/fb0 and /dev/fb1 etc.) work independently.
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ciApplication software that uses the frame buffer device (e.g. the X server) will
5462306a36Sopenharmony_ciuse /dev/fb0 by default (older software uses /dev/fb0current). You can specify
5562306a36Sopenharmony_cian alternative frame buffer device by setting the environment variable
5662306a36Sopenharmony_ci$FRAMEBUFFER to the path name of a frame buffer device, e.g. (for sh/bash
5762306a36Sopenharmony_ciusers)::
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci    export FRAMEBUFFER=/dev/fb1
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_cior (for csh users)::
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci    setenv FRAMEBUFFER /dev/fb1
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciAfter this the X server will use the second frame buffer.
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci2. Programmer's View of /dev/fb*
6962306a36Sopenharmony_ci--------------------------------
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ciAs you already know, a frame buffer device is a memory device like /dev/mem and
7262306a36Sopenharmony_ciit has the same features. You can read it, write it, seek to some location in
7362306a36Sopenharmony_ciit and mmap() it (the main usage). The difference is just that the memory that
7462306a36Sopenharmony_ciappears in the special file is not the whole memory, but the frame buffer of
7562306a36Sopenharmony_cisome video hardware.
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/dev/fb* also allows several ioctls on it, by which lots of information about
7862306a36Sopenharmony_cithe hardware can be queried and set. The color map handling works via ioctls,
7962306a36Sopenharmony_citoo. Look into <linux/fb.h> for more information on what ioctls exist and on
8062306a36Sopenharmony_ciwhich data structures they work. Here's just a brief overview:
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci  - You can request unchangeable information about the hardware, like name,
8362306a36Sopenharmony_ci    organization of the screen memory (planes, packed pixels, ...) and address
8462306a36Sopenharmony_ci    and length of the screen memory.
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci  - You can request and change variable information about the hardware, like
8762306a36Sopenharmony_ci    visible and virtual geometry, depth, color map format, timing, and so on.
8862306a36Sopenharmony_ci    If you try to change that information, the driver maybe will round up some
8962306a36Sopenharmony_ci    values to meet the hardware's capabilities (or return EINVAL if that isn't
9062306a36Sopenharmony_ci    possible).
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci  - You can get and set parts of the color map. Communication is done with 16
9362306a36Sopenharmony_ci    bits per color part (red, green, blue, transparency) to support all
9462306a36Sopenharmony_ci    existing hardware. The driver does all the computations needed to apply
9562306a36Sopenharmony_ci    it to the hardware (round it down to less bits, maybe throw away
9662306a36Sopenharmony_ci    transparency).
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ciAll this hardware abstraction makes the implementation of application programs
9962306a36Sopenharmony_cieasier and more portable. E.g. the X server works completely on /dev/fb* and
10062306a36Sopenharmony_cithus doesn't need to know, for example, how the color registers of the concrete
10162306a36Sopenharmony_cihardware are organized. XF68_FBDev is a general X server for bitmapped,
10262306a36Sopenharmony_ciunaccelerated video hardware. The only thing that has to be built into
10362306a36Sopenharmony_ciapplication programs is the screen organization (bitplanes or chunky pixels
10462306a36Sopenharmony_cietc.), because it works on the frame buffer image data directly.
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ciFor the future it is planned that frame buffer drivers for graphics cards and
10762306a36Sopenharmony_cithe like can be implemented as kernel modules that are loaded at runtime. Such
10862306a36Sopenharmony_cia driver just has to call register_framebuffer() and supply some functions.
10962306a36Sopenharmony_ciWriting and distributing such drivers independently from the kernel will save
11062306a36Sopenharmony_cimuch trouble...
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci3. Frame Buffer Resolution Maintenance
11462306a36Sopenharmony_ci--------------------------------------
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ciFrame buffer resolutions are maintained using the utility `fbset`. It can
11762306a36Sopenharmony_cichange the video mode properties of a frame buffer device. Its main usage is
11862306a36Sopenharmony_cito change the current video mode, e.g. during boot up in one of your `/etc/rc.*`
11962306a36Sopenharmony_cior `/etc/init.d/*` files.
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciFbset uses a video mode database stored in a configuration file, so you can
12262306a36Sopenharmony_cieasily add your own modes and refer to them with a simple identifier.
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci4. The X Server
12662306a36Sopenharmony_ci---------------
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciThe X server (XF68_FBDev) is the most notable application program for the frame
12962306a36Sopenharmony_cibuffer device. Starting with XFree86 release 3.2, the X server is part of
13062306a36Sopenharmony_ciXFree86 and has 2 modes:
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci  - If the `Display` subsection for the `fbdev` driver in the /etc/XF86Config
13362306a36Sopenharmony_ci    file contains a::
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	Modes "default"
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci    line, the X server will use the scheme discussed above, i.e. it will start
13862306a36Sopenharmony_ci    up in the resolution determined by /dev/fb0 (or $FRAMEBUFFER, if set). You
13962306a36Sopenharmony_ci    still have to specify the color depth (using the Depth keyword) and virtual
14062306a36Sopenharmony_ci    resolution (using the Virtual keyword) though. This is the default for the
14162306a36Sopenharmony_ci    configuration file supplied with XFree86. It's the most simple
14262306a36Sopenharmony_ci    configuration, but it has some limitations.
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci  - Therefore it's also possible to specify resolutions in the /etc/XF86Config
14562306a36Sopenharmony_ci    file. This allows for on-the-fly resolution switching while retaining the
14662306a36Sopenharmony_ci    same virtual desktop size. The frame buffer device that's used is still
14762306a36Sopenharmony_ci    /dev/fb0current (or $FRAMEBUFFER), but the available resolutions are
14862306a36Sopenharmony_ci    defined by /etc/XF86Config now. The disadvantage is that you have to
14962306a36Sopenharmony_ci    specify the timings in a different format (but `fbset -x` may help).
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ciTo tune a video mode, you can use fbset or xvidtune. Note that xvidtune doesn't
15262306a36Sopenharmony_ciwork 100% with XF68_FBDev: the reported clock values are always incorrect.
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci5. Video Mode Timings
15662306a36Sopenharmony_ci---------------------
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ciA monitor draws an image on the screen by using an electron beam (3 electron
15962306a36Sopenharmony_cibeams for color models, 1 electron beam for monochrome monitors). The front of
16062306a36Sopenharmony_cithe screen is covered by a pattern of colored phosphors (pixels). If a phosphor
16162306a36Sopenharmony_ciis hit by an electron, it emits a photon and thus becomes visible.
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ciThe electron beam draws horizontal lines (scanlines) from left to right, and
16462306a36Sopenharmony_cifrom the top to the bottom of the screen. By modifying the intensity of the
16562306a36Sopenharmony_cielectron beam, pixels with various colors and intensities can be shown.
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciAfter each scanline the electron beam has to move back to the left side of the
16862306a36Sopenharmony_ciscreen and to the next line: this is called the horizontal retrace. After the
16962306a36Sopenharmony_ciwhole screen (frame) was painted, the beam moves back to the upper left corner:
17062306a36Sopenharmony_cithis is called the vertical retrace. During both the horizontal and vertical
17162306a36Sopenharmony_ciretrace, the electron beam is turned off (blanked).
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ciThe speed at which the electron beam paints the pixels is determined by the
17462306a36Sopenharmony_cidotclock in the graphics board. For a dotclock of e.g. 28.37516 MHz (millions
17562306a36Sopenharmony_ciof cycles per second), each pixel is 35242 ps (picoseconds) long::
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci    1/(28.37516E6 Hz) = 35.242E-9 s
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ciIf the screen resolution is 640x480, it will take::
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci    640*35.242E-9 s = 22.555E-6 s
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_cito paint the 640 (xres) pixels on one scanline. But the horizontal retrace
18462306a36Sopenharmony_cialso takes time (e.g. 272 `pixels`), so a full scanline takes::
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci    (640+272)*35.242E-9 s = 32.141E-6 s
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ciWe'll say that the horizontal scanrate is about 31 kHz::
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci    1/(32.141E-6 s) = 31.113E3 Hz
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ciA full screen counts 480 (yres) lines, but we have to consider the vertical
19362306a36Sopenharmony_ciretrace too (e.g. 49 `lines`). So a full screen will take::
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci    (480+49)*32.141E-6 s = 17.002E-3 s
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ciThe vertical scanrate is about 59 Hz::
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci    1/(17.002E-3 s) = 58.815 Hz
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ciThis means the screen data is refreshed about 59 times per second. To have a
20262306a36Sopenharmony_cistable picture without visible flicker, VESA recommends a vertical scanrate of
20362306a36Sopenharmony_ciat least 72 Hz. But the perceived flicker is very human dependent: some people
20462306a36Sopenharmony_cican use 50 Hz without any trouble, while I'll notice if it's less than 80 Hz.
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ciSince the monitor doesn't know when a new scanline starts, the graphics board
20762306a36Sopenharmony_ciwill supply a synchronization pulse (horizontal sync or hsync) for each
20862306a36Sopenharmony_ciscanline.  Similarly it supplies a synchronization pulse (vertical sync or
20962306a36Sopenharmony_civsync) for each new frame. The position of the image on the screen is
21062306a36Sopenharmony_ciinfluenced by the moments at which the synchronization pulses occur.
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ciThe following picture summarizes all timings. The horizontal retrace time is
21362306a36Sopenharmony_cithe sum of the left margin, the right margin and the hsync length, while the
21462306a36Sopenharmony_civertical retrace time is the sum of the upper margin, the lower margin and the
21562306a36Sopenharmony_civsync length::
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci  +----------+---------------------------------------------+----------+-------+
21862306a36Sopenharmony_ci  |          |                ↑                            |          |       |
21962306a36Sopenharmony_ci  |          |                |upper_margin                |          |       |
22062306a36Sopenharmony_ci  |          |                ↓                            |          |       |
22162306a36Sopenharmony_ci  +----------###############################################----------+-------+
22262306a36Sopenharmony_ci  |          #                ↑                            #          |       |
22362306a36Sopenharmony_ci  |          #                |                            #          |       |
22462306a36Sopenharmony_ci  |          #                |                            #          |       |
22562306a36Sopenharmony_ci  |          #                |                            #          |       |
22662306a36Sopenharmony_ci  |   left   #                |                            #  right   | hsync |
22762306a36Sopenharmony_ci  |  margin  #                |       xres                 #  margin  |  len  |
22862306a36Sopenharmony_ci  |<-------->#<---------------+--------------------------->#<-------->|<----->|
22962306a36Sopenharmony_ci  |          #                |                            #          |       |
23062306a36Sopenharmony_ci  |          #                |                            #          |       |
23162306a36Sopenharmony_ci  |          #                |                            #          |       |
23262306a36Sopenharmony_ci  |          #                |yres                        #          |       |
23362306a36Sopenharmony_ci  |          #                |                            #          |       |
23462306a36Sopenharmony_ci  |          #                |                            #          |       |
23562306a36Sopenharmony_ci  |          #                |                            #          |       |
23662306a36Sopenharmony_ci  |          #                |                            #          |       |
23762306a36Sopenharmony_ci  |          #                |                            #          |       |
23862306a36Sopenharmony_ci  |          #                |                            #          |       |
23962306a36Sopenharmony_ci  |          #                |                            #          |       |
24062306a36Sopenharmony_ci  |          #                |                            #          |       |
24162306a36Sopenharmony_ci  |          #                ↓                            #          |       |
24262306a36Sopenharmony_ci  +----------###############################################----------+-------+
24362306a36Sopenharmony_ci  |          |                ↑                            |          |       |
24462306a36Sopenharmony_ci  |          |                |lower_margin                |          |       |
24562306a36Sopenharmony_ci  |          |                ↓                            |          |       |
24662306a36Sopenharmony_ci  +----------+---------------------------------------------+----------+-------+
24762306a36Sopenharmony_ci  |          |                ↑                            |          |       |
24862306a36Sopenharmony_ci  |          |                |vsync_len                   |          |       |
24962306a36Sopenharmony_ci  |          |                ↓                            |          |       |
25062306a36Sopenharmony_ci  +----------+---------------------------------------------+----------+-------+
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ciThe frame buffer device expects all horizontal timings in number of dotclocks
25362306a36Sopenharmony_ci(in picoseconds, 1E-12 s), and vertical timings in number of scanlines.
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci6. Converting XFree86 timing values info frame buffer device timings
25762306a36Sopenharmony_ci--------------------------------------------------------------------
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ciAn XFree86 mode line consists of the following fields::
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci "800x600"     50      800  856  976 1040    600  637  643  666
26262306a36Sopenharmony_ci < name >     DCF       HR  SH1  SH2  HFL     VR  SV1  SV2  VFL
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ciThe frame buffer device uses the following fields:
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci  - pixclock: pixel clock in ps (pico seconds)
26762306a36Sopenharmony_ci  - left_margin: time from sync to picture
26862306a36Sopenharmony_ci  - right_margin: time from picture to sync
26962306a36Sopenharmony_ci  - upper_margin: time from sync to picture
27062306a36Sopenharmony_ci  - lower_margin: time from picture to sync
27162306a36Sopenharmony_ci  - hsync_len: length of horizontal sync
27262306a36Sopenharmony_ci  - vsync_len: length of vertical sync
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci1) Pixelclock:
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci   xfree: in MHz
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci   fb: in picoseconds (ps)
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci   pixclock = 1000000 / DCF
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci2) horizontal timings:
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci   left_margin = HFL - SH2
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci   right_margin = SH1 - HR
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci   hsync_len = SH2 - SH1
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci3) vertical timings:
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci   upper_margin = VFL - SV2
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci   lower_margin = SV1 - VR
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci   vsync_len = SV2 - SV1
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_ciGood examples for VESA timings can be found in the XFree86 source tree,
29962306a36Sopenharmony_ciunder "xc/programs/Xserver/hw/xfree86/doc/modeDB.txt".
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci7. References
30362306a36Sopenharmony_ci-------------
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ciFor more specific information about the frame buffer device and its
30662306a36Sopenharmony_ciapplications, please refer to the Linux-fbdev website:
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ci    http://linux-fbdev.sourceforge.net/
30962306a36Sopenharmony_ci
31062306a36Sopenharmony_ciand to the following documentation:
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci  - The manual pages for fbset: fbset(8), fb.modes(5)
31362306a36Sopenharmony_ci  - The manual pages for XFree86: XF68_FBDev(1), XF86Config(4/5)
31462306a36Sopenharmony_ci  - The mighty kernel sources:
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci      - linux/drivers/video/
31762306a36Sopenharmony_ci      - linux/include/linux/fb.h
31862306a36Sopenharmony_ci      - linux/include/video/
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ci8. Mailing list
32362306a36Sopenharmony_ci---------------
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ciThere is a frame buffer device related mailing list at kernel.org:
32662306a36Sopenharmony_cilinux-fbdev@vger.kernel.org.
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ciPoint your web browser to http://sourceforge.net/projects/linux-fbdev/ for
32962306a36Sopenharmony_cisubscription information and archive browsing.
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci9. Downloading
33362306a36Sopenharmony_ci--------------
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ciAll necessary files can be found at
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci    ftp://ftp.uni-erlangen.de/pub/Linux/LOCAL/680x0/
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_ciand on its mirrors.
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ciThe latest version of fbset can be found at
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci    http://www.linux-fbdev.org/
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci10. Credits
34762306a36Sopenharmony_ci-----------
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ciThis readme was written by Geert Uytterhoeven, partly based on the original
35062306a36Sopenharmony_ci`X-framebuffer.README` by Roman Hodek and Martin Schaller. Section 6 was
35162306a36Sopenharmony_ciprovided by Frank Neumann.
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ciThe frame buffer device abstraction was designed by Martin Schaller.
354