2 * linux/drivers/video/skeletonfb.c -- Skeleton for a frame buffer device
4 * Modified to new api Jan 2001 by James Simmons (jsimmons@transvirtual.com)
6 * Created 28 Dec 1997 by Geert Uytterhoeven
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
16 * First the roles of struct fb_info and struct display have changed. Struct
17 * display will go away. The way the 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.
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.
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
45 #include <linux/module.h>
46 #include <linux/kernel.h>
47 #include <linux/errno.h>
48 #include <linux/string.h>
50 #include <linux/tty.h>
51 #include <linux/slab.h>
52 #include <linux/delay.h>
54 #include <linux/init.h>
57 * This is just simple sample code.
59 * No warranty that it actually compiles.
60 * Even less warranty that it actually works :-)
64 * If your driver supports multiple boards, you should make the
65 * below data types arrays, or allocate them dynamically (using kmalloc()).
69 * This structure defines the hardware state of the graphics card. Normally
70 * you place this in a header file in linux/include/video. This file usually
71 * also includes register information. That allows other driver subsystems
72 * and userland applications the ability to use the same header file to
73 * avoid duplicate work and easy porting of software.
78 * Here we define the default structs fb_fix_screeninfo and fb_var_screeninfo
79 * if we don't use modedb. If we do use modedb see xxxfb_init how to use it
80 * to get a fb_var_screeninfo. Otherwise define a default var as well.
82 static struct fb_fix_screeninfo xxxfb_fix __initdata
= {
84 .type
= FB_TYPE_PACKED_PIXELS
,
85 .visual
= FB_VISUAL_PSEUDOCOLOR
,
89 .accel
= FB_ACCEL_NONE
,
93 * Modern graphical hardware not only supports pipelines but some
94 * also support multiple monitors where each display can have its
95 * its own unique data. In this case each display could be
96 * represented by a separate framebuffer device thus a separate
97 * struct fb_info. Now the struct xxx_par represents the graphics
98 * hardware state thus only one exist per card. In this case the
99 * struct xxx_par for each graphics card would be shared between
100 * every struct fb_info that represents a framebuffer on that card.
101 * This allows when one display changes it video resolution (info->var)
102 * the other displays know instantly. Each display can always be
103 * aware of the entire hardware state that affects it because they share
104 * the same xxx_par struct. The other side of the coin is multiple
105 * graphics cards that pass data around until it is finally displayed
106 * on one monitor. Such examples are the voodoo 1 cards and high end
107 * NUMA graphics servers. For this case we have a bunch of pars, each
108 * one that represents a graphics state, that belong to one struct
109 * fb_info. Their you would want to have *par point to a array of device
110 * states and have each struct fb_ops function deal with all those
111 * states. I hope this covers every possible hardware design. If not
112 * feel free to send your ideas at jsimmons@users.sf.net
116 * If your driver supports multiple boards or it supports multiple
117 * framebuffers, you should make these arrays, or allocate them
118 * dynamically (using kmalloc()).
120 static struct fb_info info
;
123 * Each one represents the a state of the hardware. Most hardware have
124 * just one hardware state. These here represent the default state(s).
126 static struct xxx_par __initdata current_par
;
128 int xxxfb_init(void);
129 int xxxfb_setup(char*);
132 * xxxfb_open - Optional function. Called when the framebuffer is
134 * @info: frame buffer structure that represents a single frame buffer
135 * @user: tell us if the userland (value=1) or the console is accessing
138 * This function is the first function called in the framebuffer api.
139 * Usually you don't need to provide this function. The case where it
140 * is used is to change from a text mode hardware state to a graphics
143 static int xxxfb_open(const struct fb_info
*info
, int user
)
149 * xxxfb_release - Optional function. Called when the framebuffer
151 * @info: frame buffer structure that represents a single frame buffer
152 * @user: tell us if the userland (value=1) or the console is accessing
155 * Thus function is called when we close /dev/fb or the framebuffer
156 * console system is released. Usually you don't need this function.
157 * The case where it is usually used is to go from a graphics state
158 * to a text mode state.
160 static int xxxfb_release(const struct fb_info
*info
, int user
)
166 * xxxfb_check_var - Optional function. Validates a var passed in.
167 * @var: frame buffer variable screen structure
168 * @info: frame buffer structure that represents a single frame buffer
170 * Checks to see if the hardware supports the state requested by
171 * var passed in. This function does not alter the hardware state!!!
172 * This means the data stored in struct fb_info and struct xxx_par do
173 * not change. This includes the var inside of struct fb_info.
174 * Do NOT change these. This function can be called on its own if we
175 * intent to only test a mode and not actually set it. The stuff in
176 * modedb.c is a example of this. If the var passed in is slightly
177 * off by what the hardware can support then we alter the var PASSED in
178 * to what we can do. If the hardware doesn't support mode change
179 * a -EINVAL will be returned by the upper layers. You don't need to
180 * implement this function then. If you hardware doesn't support
181 * changing the resolution then this function is not needed. In this
182 * case the driver woudl just provide a var that represents the static
183 * state the screen is in.
185 * Returns negative errno on error, or zero on success.
187 static int xxxfb_check_var(struct fb_var_screeninfo
*var
, struct fb_info
*info
)
189 const struct xxx_par
*par
= (const struct xxx_par
*) info
->par
;
195 * xxxfb_set_par - Optional function. Alters the hardware state.
196 * @info: frame buffer structure that represents a single frame buffer
198 * Using the fb_var_screeninfo in fb_info we set the resolution of the
199 * this particular framebuffer. This function alters the par AND the
200 * fb_fix_screeninfo stored in fb_info. It doesn't not alter var in
201 * fb_info since we are using that data. This means we depend on the
202 * data in var inside fb_info to be supported by the hardware.
203 * xxxfb_check_var is always called before xxxfb_set_par to ensure this.
204 * Again if you can't can't the resolution you don't need this function.
207 static int xxxfb_set_par(struct fb_info
*info
)
209 struct xxx_par
*par
= (struct xxx_par
*) info
->par
;
215 * xxxfb_setcolreg - Optional function. Sets a color register.
216 * @regno: Which register in the CLUT we are programming
217 * @red: The red value which can be up to 16 bits wide
218 * @green: The green value which can be up to 16 bits wide
219 * @blue: The blue value which can be up to 16 bits wide.
220 * @transp: If supported the alpha value which can be up to 16 bits wide.
221 * @info: frame buffer info structure
223 * Set a single color register. The values supplied have a 16 bit
224 * magnitude which needs to be scaled in this function for the hardware.
225 * Things to take into consideration are how many color registers, if
226 * any, are supported with the current color visual. With truecolor mode
227 * no color palettes are supported. Here a psuedo palette is created
228 * which we store the value in pseudo_palette in struct fb_info. For
229 * pseudocolor mode we have a limited color palette. To deal with this
230 * we can program what color is displayed for a particular pixel value.
231 * DirectColor is similar in that we can program each color field. If
232 * we have a static colormap we don't need to implement this function.
234 * Returns negative errno on error, or zero on success.
236 static int xxxfb_setcolreg(unsigned regno
, unsigned red
, unsigned green
,
237 unsigned blue
, unsigned transp
,
238 const struct fb_info
*info
)
240 if (regno
>= 256) /* no. of hw registers */
243 * Program hardware... do anything you want with transp
246 /* grayscale works only partially under directcolor */
247 if (info
->var
.grayscale
) {
248 /* grayscale = 0.30*R + 0.59*G + 0.11*B */
249 red
= green
= blue
= (red
* 77 + green
* 151 + blue
* 28) >> 8;
253 * var->{color}.offset contains start of bitfield
254 * var->{color}.length contains length of bitfield
255 * {hardwarespecific} contains width of DAC
256 * cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
257 * RAMDAC[X] is programmed to (red, green, blue)
260 * uses offset = 0 && length = DAC register width.
261 * var->{color}.offset is 0
262 * var->{color}.length contains widht of DAC
264 * DAC[X] is programmed to (red, green, blue)
266 * does not use RAMDAC (usually has 3 of them).
267 * var->{color}.offset contains start of bitfield
268 * var->{color}.length contains length of bitfield
269 * cmap is programmed to (red << red.offset) | (green << green.offset) |
270 * (blue << blue.offset) | (transp << transp.offset)
271 * RAMDAC does not exist
273 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
274 switch (info
->fix
.visual
) {
275 case FB_VISUAL_TRUECOLOR
:
276 case FB_VISUAL_PSEUDOCOLOR
:
277 red
= CNVT_TOHW(red
, info
->var
.red
.length
);
278 green
= CNVT_TOHW(green
, info
->var
.green
.length
);
279 blue
= CNVT_TOHW(blue
, info
->var
.blue
.length
);
280 transp
= CNVT_TOHW(transp
, info
->var
.transp
.length
);
282 case FB_VISUAL_DIRECTCOLOR
:
283 /* example here assumes 8 bit DAC. Might be different
284 * for your hardware */
285 red
= CNVT_TOHW(red
, 8);
286 green
= CNVT_TOHW(green
, 8);
287 blue
= CNVT_TOHW(blue
, 8);
288 /* hey, there is bug in transp handling... */
289 transp
= CNVT_TOHW(transp
, 8);
293 /* Truecolor has hardware independent palette */
294 if (info
->fix
.visual
== FB_VISUAL_TRUECOLOR
) {
300 v
= (red
<< info
->var
.red
.offset
) |
301 (green
<< info
->var
.green
.offset
) |
302 (blue
<< info
->var
.blue
.offset
) |
303 (transp
<< info
->var
.transp
.offset
);
305 switch (info
->var
.bits_per_pixel
) {
307 /* Yes some hand held devices have this. */
308 ((u8
*)(info
->pseudo_palette
))[regno
] = v
;
311 ((u16
*)(info
->pseudo_palette
))[regno
] = v
;
315 ((u32
*)(info
->pseudo_palette
))[regno
] = v
;
325 * xxxfb_pan_display - NOT a required function. Pans the display.
326 * @var: frame buffer variable screen structure
327 * @info: frame buffer structure that represents a single frame buffer
329 * Pan (or wrap, depending on the `vmode' field) the display using the
330 * `xoffset' and `yoffset' fields of the `var' structure.
331 * If the values don't fit, return -EINVAL.
333 * Returns negative errno on error, or zero on success.
335 static int xxxfb_pan_display(struct fb_var_screeninfo
*var
,
336 const struct fb_info
*info
)
343 * xxxfb_blank - NOT a required function. Blanks the display.
344 * @blank_mode: the blank mode we want.
345 * @info: frame buffer structure that represents a single frame buffer
347 * Blank the screen if blank_mode != 0, else unblank. Return 0 if
348 * blanking succeeded, != 0 if un-/blanking failed due to e.g. a
349 * video mode which doesn't support it. Implements VESA suspend
350 * and powerdown modes on hardware that supports disabling hsync/vsync:
351 * blank_mode == 2: suspend vsync
352 * blank_mode == 3: suspend hsync
353 * blank_mode == 4: powerdown
355 * Returns negative errno on error, or zero on success.
358 static int xxxfb_blank(int blank_mode
, const struct fb_info
*info
)
364 /* ------------ Accelerated Functions --------------------- */
367 * We provide our own functions if we have hardware acceleration
368 * or non packed pixel format layouts. If we have no hardware
369 * acceleration, we can use a generic unaccelerated function. If using
370 * a pack pixel format just use the functions in cfb_*.c. Each file
371 * has one of the three different accel functions we support.
375 * xxxfb_fillrect - REQUIRED function. Can use generic routines if
376 * non acclerated hardware and packed pixel based.
377 * Draws a rectangle on the screen.
379 * @info: frame buffer structure that represents a single frame buffer
380 * @region: The structure representing the rectangular region we
383 * This drawing operation places/removes a retangle on the screen
384 * depending on the rastering operation with the value of color which
385 * is in the current color depth format.
387 void xxfb_fillrect(struct fb_info
*p
, const struct fb_fillrect
*region
)
389 /* Meaning of struct fb_fillrect
391 * @dx: The x and y corrdinates of the upper left hand corner of the
392 * @dy: area we want to draw to.
393 * @width: How wide the rectangle is we want to draw.
394 * @height: How tall the rectangle is we want to draw.
395 * @color: The color to fill in the rectangle with.
396 * @rop: The raster operation. We can draw the rectangle with a COPY
397 * of XOR which provides erasing effect.
402 * xxxfb_copyarea - REQUIRED function. Can use generic routines if
403 * non acclerated hardware and packed pixel based.
404 * Copies one area of the screen to another area.
406 * @info: frame buffer structure that represents a single frame buffer
407 * @area: Structure providing the data to copy the framebuffer contents
408 * from one region to another.
410 * This drawing operation copies a rectangular area from one area of the
411 * screen to another area.
413 void xxxfb_copyarea(struct fb_info
*p
, const struct fb_copyarea
*area
)
416 * @dx: The x and y coordinates of the upper left hand corner of the
417 * @dy: destination area on the screen.
418 * @width: How wide the rectangle is we want to copy.
419 * @height: How tall the rectangle is we want to copy.
420 * @sx: The x and y coordinates of the upper left hand corner of the
421 * @sy: source area on the screen.
427 * xxxfb_imageblit - REQUIRED function. Can use generic routines if
428 * non acclerated hardware and packed pixel based.
429 * Copies a image from system memory to the screen.
431 * @info: frame buffer structure that represents a single frame buffer
432 * @image: structure defining the image.
434 * This drawing operation draws a image on the screen. It can be a
435 * mono image (needed for font handling) or a color image (needed for
438 void xxxfb_imageblit(struct fb_info
*p
, const struct fb_image
*image
)
441 * @dx: The x and y coordinates of the upper left hand corner of the
442 * @dy: destination area to place the image on the screen.
443 * @width: How wide the image is we want to copy.
444 * @height: How tall the image is we want to copy.
445 * @fg_color: For mono bitmap images this is color data for
446 * @bg_color: the foreground and background of the image to
447 * write directly to the frmaebuffer.
448 * @depth: How many bits represent a single pixel for this image.
449 * @data: The actual data used to construct the image on the display.
450 * @cmap: The colormap used for color images.
455 * xxxfb_cursor - REQUIRED function. If your hardware lacks support
456 * for a cursor you can use the default cursor whose
457 * function is called soft_cursor. It will always
458 * work since it uses xxxfb_imageblit function which
461 * @info: frame buffer structure that represents a single frame buffer
462 * @cursor: structure defining the cursor to draw.
464 * This operation is used to set or alter the properities of the
467 * Returns negative errno on error, or zero on success.
469 int xxxfb_cursor(struct fb_info
*info
, struct fb_cursor
*cursor
)
472 * @set: Which fields we are altering in struct fb_cursor
473 * @enable: Disable or enable the cursor
474 * @rop: The bit operation we want to do.
475 * @mask: This is the cursor mask bitmap.
476 * @dest: A image of the area we are going to display the cursor.
477 * Used internally by the driver.
478 * @hot: The hot spot.
479 * @image: The actual data for the cursor image.
484 * xxxfb_rotate - NOT a required function. If your hardware
485 * supports rotation the whole screen then
486 * you would provide a hook for this.
488 * @info: frame buffer structure that represents a single frame buffer
489 * @angle: The angle we rotate the screen.
491 * This operation is used to set or alter the properities of the
494 void xxxfb_rotate(struct fb_info
*info
, int angle
)
499 * xxxfb_poll - NOT a required function. The purpose of this
500 * function is to provide a way for some process
501 * to wait until a specific hardware event occurs
502 * for the framebuffer device.
504 * @info: frame buffer structure that represents a single frame buffer
505 * @wait: poll table where we store process that await a event.
507 void xxxfb_poll(struct fb_info
*info
, poll_table
*wait
)
512 * xxxfb_sync - NOT a required function. Normally the accel engine
513 * for a graphics card take a specific amount of time.
514 * Often we have to wait for the accelerator to finish
515 * its operation before we can write to the framebuffer
516 * so we can have consistent display output.
518 * @info: frame buffer structure that represents a single frame buffer
520 void xxxfb_sync(struct fb_info
*info
)
528 int __init
xxxfb_init(void)
530 int cmap_len
, retval
;
533 * Here we set the screen_base to the vitrual memory address
534 * for the framebuffer. Usually we obtain the resource address
535 * from the bus layer and then translate it to virtual memory
536 * space via ioremap. Consult ioport.h.
538 info
.screen_base
= framebuffer_virtual_memory
;
539 info
.fbops
= &xxxfb_ops
;
540 info
.fix
= xxxfb_fix
;
541 info
.pseudo_palette
= pseudo_palette
;
544 * Set up flags to indicate what sort of acceleration your
545 * driver can provide (pan/wrap/copyarea/etc.) and whether it
546 * is a module -- see FBINFO_* in include/linux/fb.h
548 info
.flags
= FBINFO_DEFAULT
;
549 info
.par
= current_par
;
552 * This should give a reasonable default video mode. The following is
553 * done when we can set a video mode.
556 mode_option
= "640x480@60";
558 retval
= fb_find_mode(&info
.var
, &info
, mode_option
, NULL
, 0, NULL
, 8);
560 if (!retval
|| retval
== 4)
563 /* This has to been done !!! */
564 fb_alloc_cmap(&info
.cmap
, cmap_len
, 0);
567 * The following is done in the case of having hardware with a static
568 * mode. If we are setting the mode ourselves we don't call this.
570 info
.var
= xxxfb_var
;
572 if (register_framebuffer(&info
) < 0)
574 printk(KERN_INFO
"fb%d: %s frame buffer device\n", info
.node
,
583 static void __exit
xxxfb_cleanup(void)
586 * For kernel boot options (in 'video=xxxfb:<options>' format)
591 if (fb_get_options("xxxfb", &option
))
597 * If your driver supports multiple boards, you should unregister and
598 * clean up all instances.
601 unregister_framebuffer(info
);
610 * Only necessary if your driver takes special options,
611 * otherwise we fall back on the generic fb_setup().
613 int __init
xxxfb_setup(char *options
)
615 /* Parse user speficied options (`video=xxxfb:') */
618 /* ------------------------------------------------------------------------- */
621 * Frame buffer operations
624 static struct fb_ops xxxfb_ops
= {
625 .owner
= THIS_MODULE
,
626 .fb_open
= xxxfb_open
,
627 .fb_read
= xxxfb_read
,
628 .fb_write
= xxxfb_write
,
629 .fb_release
= xxxfb_release
,
630 .fb_check_var
= xxxfb_check_var
,
631 .fb_set_par
= xxxfb_set_par
,
632 .fb_setcolreg
= xxxfb_setcolreg
,
633 .fb_blank
= xxxfb_blank
,
634 .fb_pan_display
= xxxfb_pan_display
,
635 .fb_fillrect
= xxxfb_fillrect
, /* Needed !!! */
636 .fb_copyarea
= xxxfb_copyarea
, /* Needed !!! */
637 .fb_imageblit
= xxxfb_imageblit
, /* Needed !!! */
638 .fb_cursor
= xxxfb_cursor
, /* Needed !!! */
639 .fb_rotate
= xxxfb_rotate
,
640 .fb_poll
= xxxfb_poll
,
641 .fb_sync
= xxxfb_sync
,
642 .fb_ioctl
= xxxfb_ioctl
,
643 .fb_mmap
= xxxfb_mmap
,
646 /* ------------------------------------------------------------------------- */
653 module_init(xxxfb_init
);
654 module_exit(xxxfb_cleanup
);
656 MODULE_LICENSE("GPL");