xref: /linux/drivers/video/fbdev/skeletonfb.c (revision 382fc1f681324bb38bedfe763107a60256c4ddc8)
1 /*
2  * linux/drivers/video/skeletonfb.c -- 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 
45 #include <linux/aperture.h>
46 #include <linux/module.h>
47 #include <linux/kernel.h>
48 #include <linux/errno.h>
49 #include <linux/string.h>
50 #include <linux/mm.h>
51 #include <linux/slab.h>
52 #include <linux/delay.h>
53 #include <linux/fb.h>
54 #include <linux/init.h>
55 #include <linux/pci.h>
56 
57     /*
58      *  This is just simple sample code.
59      *
60      *  No warranty that it actually compiles.
61      *  Even less warranty that it actually works :-)
62      */
63 
64 /*
65  * Driver data
66  */
67 static char *mode_option;
68 
69 /*
70  *  If your driver supports multiple boards, you should make the
71  *  below data types arrays, or allocate them dynamically (using kmalloc()).
72  */
73 
74 /*
75  * This structure defines the hardware state of the graphics card. Normally
76  * you place this in a header file in linux/include/video. This file usually
77  * also includes register information. That allows other driver subsystems
78  * and userland applications the ability to use the same header file to
79  * avoid duplicate work and easy porting of software.
80  */
81 struct xxx_par;
82 
83 /*
84  * Here we define the default structs fb_fix_screeninfo and fb_var_screeninfo
85  * if we don't use modedb. If we do use modedb see xxxfb_init how to use it
86  * to get a fb_var_screeninfo. Otherwise define a default var as well.
87  */
88 static const struct fb_fix_screeninfo xxxfb_fix = {
89 	.id =		"FB's name",
90 	.type =		FB_TYPE_PACKED_PIXELS,
91 	.visual =	FB_VISUAL_PSEUDOCOLOR,
92 	.xpanstep =	1,
93 	.ypanstep =	1,
94 	.ywrapstep =	1,
95 	.accel =	FB_ACCEL_NONE,
96 };
97 
98     /*
99      * 	Modern graphical hardware not only supports pipelines but some
100      *  also support multiple monitors where each display can have
101      *  its own unique data. In this case each display could be
102      *  represented by a separate framebuffer device thus a separate
103      *  struct fb_info. Now the struct xxx_par represents the graphics
104      *  hardware state thus only one exist per card. In this case the
105      *  struct xxx_par for each graphics card would be shared between
106      *  every struct fb_info that represents a framebuffer on that card.
107      *  This allows when one display changes it video resolution (info->var)
108      *  the other displays know instantly. Each display can always be
109      *  aware of the entire hardware state that affects it because they share
110      *  the same xxx_par struct. The other side of the coin is multiple
111      *  graphics cards that pass data around until it is finally displayed
112      *  on one monitor. Such examples are the voodoo 1 cards and high end
113      *  NUMA graphics servers. For this case we have a bunch of pars, each
114      *  one that represents a graphics state, that belong to one struct
115      *  fb_info. Their you would want to have *par point to a array of device
116      *  states and have each struct fb_ops function deal with all those
117      *  states. I hope this covers every possible hardware design. If not
118      *  feel free to send your ideas at jsimmons@users.sf.net
119      */
120 
121     /*
122      *  If your driver supports multiple boards or it supports multiple
123      *  framebuffers, you should make these arrays, or allocate them
124      *  dynamically using framebuffer_alloc() and free them with
125      *  framebuffer_release().
126      */
127 static struct fb_info info;
128 
129     /*
130      * Each one represents the state of the hardware. Most hardware have
131      * just one hardware state. These here represent the default state(s).
132      */
133 static struct xxx_par __initdata current_par;
134 
135 int xxxfb_init(void);
136 
137 /**
138  *	xxxfb_open - Optional function. Called when the framebuffer is
139  *		     first accessed.
140  *	@info: frame buffer structure that represents a single frame buffer
141  *	@user: tell us if the userland (value=1) or the console is accessing
142  *	       the framebuffer.
143  *
144  *	This function is the first function called in the framebuffer api.
145  *	Usually you don't need to provide this function. The case where it
146  *	is used is to change from a text mode hardware state to a graphics
147  * 	mode state.
148  *
149  *	Returns negative errno on error, or zero on success.
150  */
151 static int xxxfb_open(struct fb_info *info, int user)
152 {
153     return 0;
154 }
155 
156 /**
157  *	xxxfb_release - Optional function. Called when the framebuffer
158  *			device is closed.
159  *	@info: frame buffer structure that represents a single frame buffer
160  *	@user: tell us if the userland (value=1) or the console is accessing
161  *	       the framebuffer.
162  *
163  *	Thus function is called when we close /dev/fb or the framebuffer
164  *	console system is released. Usually you don't need this function.
165  *	The case where it is usually used is to go from a graphics state
166  *	to a text mode state.
167  *
168  *	Returns negative errno on error, or zero on success.
169  */
170 static int xxxfb_release(struct fb_info *info, int user)
171 {
172     return 0;
173 }
174 
175 /**
176  *      xxxfb_check_var - Optional function. Validates a var passed in.
177  *      @var: frame buffer variable screen structure
178  *      @info: frame buffer structure that represents a single frame buffer
179  *
180  *	Checks to see if the hardware supports the state requested by
181  *	var passed in. This function does not alter the hardware state!!!
182  *	This means the data stored in struct fb_info and struct xxx_par do
183  *      not change. This includes the var inside of struct fb_info.
184  *	Do NOT change these. This function can be called on its own if we
185  *	intent to only test a mode and not actually set it. The stuff in
186  *	modedb.c is a example of this. If the var passed in is slightly
187  *	off by what the hardware can support then we alter the var PASSED in
188  *	to what we can do.
189  *
190  *      For values that are off, this function must round them _up_ to the
191  *      next value that is supported by the hardware.  If the value is
192  *      greater than the highest value supported by the hardware, then this
193  *      function must return -EINVAL.
194  *
195  *      Exception to the above rule:  Some drivers have a fixed mode, ie,
196  *      the hardware is already set at boot up, and cannot be changed.  In
197  *      this case, it is more acceptable that this function just return
198  *      a copy of the currently working var (info->var). Better is to not
199  *      implement this function, as the upper layer will do the copying
200  *      of the current var for you.
201  *
202  *      Note:  This is the only function where the contents of var can be
203  *      freely adjusted after the driver has been registered. If you find
204  *      that you have code outside of this function that alters the content
205  *      of var, then you are doing something wrong.  Note also that the
206  *      contents of info->var must be left untouched at all times after
207  *      driver registration.
208  *
209  *	Returns negative errno on error, or zero on success.
210  */
211 static int xxxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
212 {
213     /* ... */
214     return 0;
215 }
216 
217 /**
218  *      xxxfb_set_par - Optional function. Alters the hardware state.
219  *      @info: frame buffer structure that represents a single frame buffer
220  *
221  *	Using the fb_var_screeninfo in fb_info we set the resolution of the
222  *	this particular framebuffer. This function alters the par AND the
223  *	fb_fix_screeninfo stored in fb_info. It doesn't not alter var in
224  *	fb_info since we are using that data. This means we depend on the
225  *	data in var inside fb_info to be supported by the hardware.
226  *
227  *      This function is also used to recover/restore the hardware to a
228  *      known working state.
229  *
230  *	xxxfb_check_var is always called before xxxfb_set_par to ensure that
231  *      the contents of var is always valid.
232  *
233  *	Again if you can't change the resolution you don't need this function.
234  *
235  *      However, even if your hardware does not support mode changing,
236  *      a set_par might be needed to at least initialize the hardware to
237  *      a known working state, especially if it came back from another
238  *      process that also modifies the same hardware, such as X.
239  *
240  *      If this is the case, a combination such as the following should work:
241  *
242  *      static int xxxfb_check_var(struct fb_var_screeninfo *var,
243  *                                struct fb_info *info)
244  *      {
245  *              *var = info->var;
246  *              return 0;
247  *      }
248  *
249  *      static int xxxfb_set_par(struct fb_info *info)
250  *      {
251  *              init your hardware here
252  *      }
253  *
254  *	Returns negative errno on error, or zero on success.
255  */
256 static int xxxfb_set_par(struct fb_info *info)
257 {
258     struct xxx_par *par = info->par;
259     /* ... */
260     return 0;
261 }
262 
263 /**
264  *  	xxxfb_setcolreg - Optional function. Sets a color register.
265  *      @regno: Which register in the CLUT we are programming
266  *      @red: The red value which can be up to 16 bits wide
267  *	@green: The green value which can be up to 16 bits wide
268  *	@blue:  The blue value which can be up to 16 bits wide.
269  *	@transp: If supported, the alpha value which can be up to 16 bits wide.
270  *      @info: frame buffer info structure
271  *
272  *  	Set a single color register. The values supplied have a 16 bit
273  *  	magnitude which needs to be scaled in this function for the hardware.
274  *	Things to take into consideration are how many color registers, if
275  *	any, are supported with the current color visual. With truecolor mode
276  *	no color palettes are supported. Here a pseudo palette is created
277  *	which we store the value in pseudo_palette in struct fb_info. For
278  *	pseudocolor mode we have a limited color palette. To deal with this
279  *	we can program what color is displayed for a particular pixel value.
280  *	DirectColor is similar in that we can program each color field. If
281  *	we have a static colormap we don't need to implement this function.
282  *
283  *	Returns negative errno on error, or zero on success.
284  */
285 static int xxxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
286 			   unsigned blue, unsigned transp,
287 			   struct fb_info *info)
288 {
289     if (regno >= 256)  /* no. of hw registers */
290        return -EINVAL;
291     /*
292      * Program hardware... do anything you want with transp
293      */
294 
295     /* grayscale works only partially under directcolor */
296     if (info->var.grayscale) {
297        /* grayscale = 0.30*R + 0.59*G + 0.11*B */
298        red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
299     }
300 
301     /* Directcolor:
302      *   var->{color}.offset contains start of bitfield
303      *   var->{color}.length contains length of bitfield
304      *   {hardwarespecific} contains width of DAC
305      *   pseudo_palette[X] is programmed to (X << red.offset) |
306      *                                      (X << green.offset) |
307      *                                      (X << blue.offset)
308      *   RAMDAC[X] is programmed to (red, green, blue)
309      *   color depth = SUM(var->{color}.length)
310      *
311      * Pseudocolor:
312      *    var->{color}.offset is 0 unless the palette index takes less than
313      *                        bits_per_pixel bits and is stored in the upper
314      *                        bits of the pixel value
315      *    var->{color}.length is set so that 1 << length is the number of
316      *                        available palette entries
317      *    pseudo_palette is not used
318      *    RAMDAC[X] is programmed to (red, green, blue)
319      *    color depth = var->{color}.length
320      *
321      * Static pseudocolor:
322      *    same as Pseudocolor, but the RAMDAC is not programmed (read-only)
323      *
324      * Mono01/Mono10:
325      *    Has only 2 values, black on white or white on black (fg on bg),
326      *    var->{color}.offset is 0
327      *    white = (1 << var->{color}.length) - 1, black = 0
328      *    pseudo_palette is not used
329      *    RAMDAC does not exist
330      *    color depth is always 2
331      *
332      * Truecolor:
333      *    does not use RAMDAC (usually has 3 of them).
334      *    var->{color}.offset contains start of bitfield
335      *    var->{color}.length contains length of bitfield
336      *    pseudo_palette is programmed to (red << red.offset) |
337      *                                    (green << green.offset) |
338      *                                    (blue << blue.offset) |
339      *                                    (transp << transp.offset)
340      *    RAMDAC does not exist
341      *    color depth = SUM(var->{color}.length})
342      *
343      *  The color depth is used by fbcon for choosing the logo and also
344      *  for color palette transformation if color depth < 4
345      *
346      *  As can be seen from the above, the field bits_per_pixel is _NOT_
347      *  a criteria for describing the color visual.
348      *
349      *  A common mistake is assuming that bits_per_pixel <= 8 is pseudocolor,
350      *  and higher than that, true/directcolor.  This is incorrect, one needs
351      *  to look at the fix->visual.
352      *
353      *  Another common mistake is using bits_per_pixel to calculate the color
354      *  depth.  The bits_per_pixel field does not directly translate to color
355      *  depth. You have to compute for the color depth (using the color
356      *  bitfields) and fix->visual as seen above.
357      */
358 
359     /*
360      * This is the point where the color is converted to something that
361      * is acceptable by the hardware.
362      */
363 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
364     red = CNVT_TOHW(red, info->var.red.length);
365     green = CNVT_TOHW(green, info->var.green.length);
366     blue = CNVT_TOHW(blue, info->var.blue.length);
367     transp = CNVT_TOHW(transp, info->var.transp.length);
368 #undef CNVT_TOHW
369     /*
370      * This is the point where the function feeds the color to the hardware
371      * palette after converting the colors to something acceptable by
372      * the hardware. Note, only FB_VISUAL_DIRECTCOLOR and
373      * FB_VISUAL_PSEUDOCOLOR visuals need to write to the hardware palette.
374      * If you have code that writes to the hardware CLUT, and it's not
375      * any of the above visuals, then you are doing something wrong.
376      */
377     if (info->fix.visual == FB_VISUAL_DIRECTCOLOR ||
378 	info->fix.visual == FB_VISUAL_TRUECOLOR)
379 	    write_{red|green|blue|transp}_to_clut();
380 
381     /* This is the point were you need to fill up the contents of
382      * info->pseudo_palette. This structure is used _only_ by fbcon, thus
383      * it only contains 16 entries to match the number of colors supported
384      * by the console. The pseudo_palette is used only if the visual is
385      * in directcolor or truecolor mode.  With other visuals, the
386      * pseudo_palette is not used. (This might change in the future.)
387      *
388      * The contents of the pseudo_palette is in raw pixel format.  Ie, each
389      * entry can be written directly to the framebuffer without any conversion.
390      * The pseudo_palette is (void *).  However, if using the generic
391      * drawing functions (cfb_imageblit, cfb_fillrect), the pseudo_palette
392      * must be casted to (u32 *) _regardless_ of the bits per pixel. If the
393      * driver is using its own drawing functions, then it can use whatever
394      * size it wants.
395      */
396     if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
397 	info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
398 	    u32 v;
399 
400 	    if (regno >= 16)
401 		    return -EINVAL;
402 
403 	    v = (red << info->var.red.offset) |
404 		    (green << info->var.green.offset) |
405 		    (blue << info->var.blue.offset) |
406 		    (transp << info->var.transp.offset);
407 
408 	    ((u32*)(info->pseudo_palette))[regno] = v;
409     }
410 
411     /* ... */
412     return 0;
413 }
414 
415 /**
416  *      xxxfb_pan_display - NOT a required function. Pans the display.
417  *      @var: frame buffer variable screen structure
418  *      @info: frame buffer structure that represents a single frame buffer
419  *
420  *	Pan (or wrap, depending on the `vmode' field) the display using the
421  *  	`xoffset' and `yoffset' fields of the `var' structure.
422  *  	If the values don't fit, return -EINVAL.
423  *
424  *      Returns negative errno on error, or zero on success.
425  */
426 static int xxxfb_pan_display(struct fb_var_screeninfo *var,
427 			     struct fb_info *info)
428 {
429     /*
430      * If your hardware does not support panning, _do_ _not_ implement this
431      * function. Creating a dummy function will just confuse user apps.
432      */
433 
434     /*
435      * Note that even if this function is fully functional, a setting of
436      * 0 in both xpanstep and ypanstep means that this function will never
437      * get called.
438      */
439 
440     /* ... */
441     return 0;
442 }
443 
444 /**
445  *      xxxfb_blank - NOT a required function. Blanks the display.
446  *      @blank_mode: the blank mode we want.
447  *      @info: frame buffer structure that represents a single frame buffer
448  *
449  *      Blank the screen if blank_mode != FB_BLANK_UNBLANK, else unblank.
450  *      Return 0 if blanking succeeded, != 0 if un-/blanking failed due to
451  *      e.g. a video mode which doesn't support it.
452  *
453  *      Implements VESA suspend and powerdown modes on hardware that supports
454  *      disabling hsync/vsync:
455  *
456  *      FB_BLANK_NORMAL = display is blanked, syncs are on.
457  *      FB_BLANK_HSYNC_SUSPEND = hsync off
458  *      FB_BLANK_VSYNC_SUSPEND = vsync off
459  *      FB_BLANK_POWERDOWN =  hsync and vsync off
460  *
461  *      If implementing this function, at least support FB_BLANK_UNBLANK.
462  *      Return !0 for any modes that are unimplemented.
463  *
464  */
465 static int xxxfb_blank(int blank_mode, struct fb_info *info)
466 {
467     /* ... */
468     return 0;
469 }
470 
471 /* ------------ Accelerated Functions --------------------- */
472 
473 /*
474  * We provide our own functions if we have hardware acceleration
475  * or non packed pixel format layouts. If we have no hardware
476  * acceleration, we can use a generic unaccelerated function. If using
477  * a pack pixel format just use the functions in cfb_*.c. Each file
478  * has one of the three different accel functions we support.
479  */
480 
481 /**
482  *      xxxfb_fillrect - REQUIRED function. Can use generic routines if
483  *		 	 non acclerated hardware and packed pixel based.
484  *			 Draws a rectangle on the screen.
485  *
486  *      @info: frame buffer structure that represents a single frame buffer
487  *	@region: The structure representing the rectangular region we
488  *		 wish to draw to.
489  *
490  *	This drawing operation places/removes a retangle on the screen
491  *	depending on the rastering operation with the value of color which
492  *	is in the current color depth format.
493  */
494 void xxxfb_fillrect(struct fb_info *p, const struct fb_fillrect *region)
495 {
496 /*	Meaning of struct fb_fillrect
497  *
498  *	@dx: The x and y corrdinates of the upper left hand corner of the
499  *	@dy: area we want to draw to.
500  *	@width: How wide the rectangle is we want to draw.
501  *	@height: How tall the rectangle is we want to draw.
502  *	@color:	The color to fill in the rectangle with.
503  *	@rop: The raster operation. We can draw the rectangle with a COPY
504  *	      of XOR which provides erasing effect.
505  */
506 }
507 
508 /**
509  *      xxxfb_copyarea - REQUIRED function. Can use generic routines if
510  *                       non acclerated hardware and packed pixel based.
511  *                       Copies one area of the screen to another area.
512  *
513  *      @info: frame buffer structure that represents a single frame buffer
514  *      @area: Structure providing the data to copy the framebuffer contents
515  *	       from one region to another.
516  *
517  *      This drawing operation copies a rectangular area from one area of the
518  *	screen to another area.
519  */
520 void xxxfb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
521 {
522 /*
523  *      @dx: The x and y coordinates of the upper left hand corner of the
524  *	@dy: destination area on the screen.
525  *      @width: How wide the rectangle is we want to copy.
526  *      @height: How tall the rectangle is we want to copy.
527  *      @sx: The x and y coordinates of the upper left hand corner of the
528  *      @sy: source area on the screen.
529  */
530 }
531 
532 
533 /**
534  *      xxxfb_imageblit - REQUIRED function. Can use generic routines if
535  *                        non acclerated hardware and packed pixel based.
536  *                        Copies a image from system memory to the screen.
537  *
538  *      @info: frame buffer structure that represents a single frame buffer
539  *	@image:	structure defining the image.
540  *
541  *      This drawing operation draws a image on the screen. It can be a
542  *	mono image (needed for font handling) or a color image (needed for
543  *	tux).
544  */
545 void xxxfb_imageblit(struct fb_info *p, const struct fb_image *image)
546 {
547 /*
548  *      @dx: The x and y coordinates of the upper left hand corner of the
549  *	@dy: destination area to place the image on the screen.
550  *      @width: How wide the image is we want to copy.
551  *      @height: How tall the image is we want to copy.
552  *      @fg_color: For mono bitmap images this is color data for
553  *      @bg_color: the foreground and background of the image to
554  *		   write directly to the frmaebuffer.
555  *	@depth:	How many bits represent a single pixel for this image.
556  *	@data: The actual data used to construct the image on the display.
557  *	@cmap: The colormap used for color images.
558  */
559 
560 /*
561  * The generic function, cfb_imageblit, expects that the bitmap scanlines are
562  * padded to the next byte.  Most hardware accelerators may require padding to
563  * the next u16 or the next u32.  If that is the case, the driver can specify
564  * this by setting info->pixmap.scan_align = 2 or 4.  See a more
565  * comprehensive description of the pixmap below.
566  */
567 }
568 
569 /**
570  *	xxxfb_cursor - 	OPTIONAL. If your hardware lacks support
571  *			for a cursor, leave this field NULL.
572  *
573  *      @info: frame buffer structure that represents a single frame buffer
574  *	@cursor: structure defining the cursor to draw.
575  *
576  *      This operation is used to set or alter the properities of the
577  *	cursor.
578  *
579  *	Returns negative errno on error, or zero on success.
580  */
581 int xxxfb_cursor(struct fb_info *info, struct fb_cursor *cursor)
582 {
583 /*
584  *      @set: 	Which fields we are altering in struct fb_cursor
585  *	@enable: Disable or enable the cursor
586  *      @rop: 	The bit operation we want to do.
587  *      @mask:  This is the cursor mask bitmap.
588  *      @dest:  A image of the area we are going to display the cursor.
589  *		Used internally by the driver.
590  *      @hot:	The hot spot.
591  *	@image:	The actual data for the cursor image.
592  *
593  *      NOTES ON FLAGS (cursor->set):
594  *
595  *      FB_CUR_SETIMAGE - the cursor image has changed (cursor->image.data)
596  *      FB_CUR_SETPOS   - the cursor position has changed (cursor->image.dx|dy)
597  *      FB_CUR_SETHOT   - the cursor hot spot has changed (cursor->hot.dx|dy)
598  *      FB_CUR_SETCMAP  - the cursor colors has changed (cursor->fg_color|bg_color)
599  *      FB_CUR_SETSHAPE - the cursor bitmask has changed (cursor->mask)
600  *      FB_CUR_SETSIZE  - the cursor size has changed (cursor->width|height)
601  *      FB_CUR_SETALL   - everything has changed
602  *
603  *      NOTES ON ROPs (cursor->rop, Raster Operation)
604  *
605  *      ROP_XOR         - cursor->image.data XOR cursor->mask
606  *      ROP_COPY        - curosr->image.data AND cursor->mask
607  *
608  *      OTHER NOTES:
609  *
610  *      - fbcon only supports a 2-color cursor (cursor->image.depth = 1)
611  *      - The fb_cursor structure, @cursor, _will_ always contain valid
612  *        fields, whether any particular bitfields in cursor->set is set
613  *        or not.
614  */
615 }
616 
617 /**
618  *	xxxfb_sync - NOT a required function. Normally the accel engine
619  *		     for a graphics card take a specific amount of time.
620  *		     Often we have to wait for the accelerator to finish
621  *		     its operation before we can write to the framebuffer
622  *		     so we can have consistent display output.
623  *
624  *      @info: frame buffer structure that represents a single frame buffer
625  *
626  *      If the driver has implemented its own hardware-based drawing function,
627  *      implementing this function is highly recommended.
628  */
629 int xxxfb_sync(struct fb_info *info)
630 {
631 	return 0;
632 }
633 
634     /*
635      *  Frame buffer operations
636      */
637 
638 static const struct fb_ops xxxfb_ops = {
639 	.owner		= THIS_MODULE,
640 	.fb_open	= xxxfb_open,
641 	.fb_read	= xxxfb_read,
642 	.fb_write	= xxxfb_write,
643 	.fb_release	= xxxfb_release,
644 	.fb_check_var	= xxxfb_check_var,
645 	.fb_set_par	= xxxfb_set_par,
646 	.fb_setcolreg	= xxxfb_setcolreg,
647 	.fb_blank	= xxxfb_blank,
648 	.fb_pan_display	= xxxfb_pan_display,
649 	.fb_fillrect	= xxxfb_fillrect, 	/* Needed !!! */
650 	.fb_copyarea	= xxxfb_copyarea,	/* Needed !!! */
651 	.fb_imageblit	= xxxfb_imageblit,	/* Needed !!! */
652 	.fb_cursor	= xxxfb_cursor,		/* Optional !!! */
653 	.fb_sync	= xxxfb_sync,
654 	.fb_ioctl	= xxxfb_ioctl,
655 	.fb_mmap	= xxxfb_mmap,
656 };
657 
658 /* ------------------------------------------------------------------------- */
659 
660     /*
661      *  Initialization
662      */
663 
664 /* static int __init xxfb_probe (struct platform_device *pdev) -- for platform devs */
665 static int xxxfb_probe(struct pci_dev *dev, const struct pci_device_id *ent)
666 {
667     struct fb_info *info;
668     struct xxx_par *par;
669     struct device *device = &dev->dev; /* or &pdev->dev */
670     int cmap_len, retval;
671 
672     /*
673      * Remove firmware-based drivers that create resource conflicts.
674      */
675     retval = aperture_remove_conflicting_pci_devices(pdev, "xxxfb");
676     if (retval)
677 	    return retval;
678 
679     /*
680      * Dynamically allocate info and par
681      */
682     info = framebuffer_alloc(sizeof(struct xxx_par), device);
683 
684     if (!info) {
685 	    /* goto error path */
686     }
687 
688     par = info->par;
689 
690     /*
691      * Here we set the screen_base to the virtual memory address
692      * for the framebuffer. Usually we obtain the resource address
693      * from the bus layer and then translate it to virtual memory
694      * space via ioremap. Consult ioport.h.
695      */
696     info->screen_base = framebuffer_virtual_memory;
697     info->fbops = &xxxfb_ops;
698     info->fix = xxxfb_fix;
699     info->pseudo_palette = pseudo_palette; /* The pseudopalette is an
700 					    * 16-member array
701 					    */
702     /*
703      * Set up flags to indicate what sort of acceleration your
704      * driver can provide (pan/wrap/copyarea/etc.) and whether it
705      * is a module -- see FBINFO_* in include/linux/fb.h
706      *
707      * If your hardware can support any of the hardware accelerated functions
708      * fbcon performance will improve if info->flags is set properly.
709      *
710      * FBINFO_HWACCEL_COPYAREA - hardware moves
711      * FBINFO_HWACCEL_FILLRECT - hardware fills
712      * FBINFO_HWACCEL_IMAGEBLIT - hardware mono->color expansion
713      * FBINFO_HWACCEL_YPAN - hardware can pan display in y-axis
714      * FBINFO_HWACCEL_YWRAP - hardware can wrap display in y-axis
715      * FBINFO_HWACCEL_DISABLED - supports hardware accels, but disabled
716      * FBINFO_READS_FAST - if set, prefer moves over mono->color expansion
717      * FBINFO_MISC_TILEBLITTING - hardware can do tile blits
718      *
719      * NOTE: These are for fbcon use only.
720      */
721     info->flags = FBINFO_DEFAULT;
722 
723 /********************* This stage is optional ******************************/
724      /*
725      * The struct pixmap is a scratch pad for the drawing functions. This
726      * is where the monochrome bitmap is constructed by the higher layers
727      * and then passed to the accelerator.  For drivers that uses
728      * cfb_imageblit, you can skip this part.  For those that have a more
729      * rigorous requirement, this stage is needed
730      */
731 
732     /* PIXMAP_SIZE should be small enough to optimize drawing, but not
733      * large enough that memory is wasted.  A safe size is
734      * (max_xres * max_font_height/8). max_xres is driver dependent,
735      * max_font_height is 32.
736      */
737     info->pixmap.addr = kmalloc(PIXMAP_SIZE, GFP_KERNEL);
738     if (!info->pixmap.addr) {
739 	    /* goto error */
740     }
741 
742     info->pixmap.size = PIXMAP_SIZE;
743 
744     /*
745      * FB_PIXMAP_SYSTEM - memory is in system ram
746      * FB_PIXMAP_IO     - memory is iomapped
747      * FB_PIXMAP_SYNC   - if set, will call fb_sync() per access to pixmap,
748      *                    usually if FB_PIXMAP_IO is set.
749      *
750      * Currently, FB_PIXMAP_IO is unimplemented.
751      */
752     info->pixmap.flags = FB_PIXMAP_SYSTEM;
753 
754     /*
755      * scan_align is the number of padding for each scanline.  It is in bytes.
756      * Thus for accelerators that need padding to the next u32, put 4 here.
757      */
758     info->pixmap.scan_align = 4;
759 
760     /*
761      * buf_align is the amount to be padded for the buffer. For example,
762      * the i810fb needs a scan_align of 2 but expects it to be fed with
763      * dwords, so a buf_align = 4 is required.
764      */
765     info->pixmap.buf_align = 4;
766 
767     /* access_align is how many bits can be accessed from the framebuffer
768      * ie. some epson cards allow 16-bit access only.  Most drivers will
769      * be safe with u32 here.
770      *
771      * NOTE: This field is currently unused.
772      */
773     info->pixmap.access_align = 32;
774 /***************************** End optional stage ***************************/
775 
776     /*
777      * This should give a reasonable default video mode. The following is
778      * done when we can set a video mode.
779      */
780     if (!mode_option)
781 	mode_option = "640x480@60";
782 
783     retval = fb_find_mode(&info->var, info, mode_option, NULL, 0, NULL, 8);
784 
785     if (!retval || retval == 4)
786 	return -EINVAL;
787 
788     /* This has to be done! */
789     if (fb_alloc_cmap(&info->cmap, cmap_len, 0))
790 	return -ENOMEM;
791 
792     /*
793      * The following is done in the case of having hardware with a static
794      * mode. If we are setting the mode ourselves we don't call this.
795      */
796     info->var = xxxfb_var;
797 
798     /*
799      * For drivers that can...
800      */
801     xxxfb_check_var(&info->var, info);
802 
803     /*
804      * Does a call to fb_set_par() before register_framebuffer needed?  This
805      * will depend on you and the hardware.  If you are sure that your driver
806      * is the only device in the system, a call to fb_set_par() is safe.
807      *
808      * Hardware in x86 systems has a VGA core.  Calling set_par() at this
809      * point will corrupt the VGA console, so it might be safer to skip a
810      * call to set_par here and just allow fbcon to do it for you.
811      */
812     /* xxxfb_set_par(info); */
813 
814     if (register_framebuffer(info) < 0) {
815 	fb_dealloc_cmap(&info->cmap);
816 	return -EINVAL;
817     }
818     fb_info(info, "%s frame buffer device\n", info->fix.id);
819     pci_set_drvdata(dev, info); /* or platform_set_drvdata(pdev, info) */
820     return 0;
821 }
822 
823     /*
824      *  Cleanup
825      */
826 /* static void xxxfb_remove(struct platform_device *pdev) */
827 static void xxxfb_remove(struct pci_dev *dev)
828 {
829 	struct fb_info *info = pci_get_drvdata(dev);
830 	/* or platform_get_drvdata(pdev); */
831 
832 	if (info) {
833 		unregister_framebuffer(info);
834 		fb_dealloc_cmap(&info->cmap);
835 		/* ... */
836 		framebuffer_release(info);
837 	}
838 }
839 
840 #ifdef CONFIG_PCI
841 #ifdef CONFIG_PM
842 /**
843  *	xxxfb_suspend - Optional but recommended function. Suspend the device.
844  *	@dev: PCI device
845  *	@msg: the suspend event code.
846  *
847  *      See Documentation/driver-api/pm/devices.rst for more information
848  */
849 static int xxxfb_suspend(struct device *dev)
850 {
851 	struct fb_info *info = dev_get_drvdata(dev);
852 	struct xxxfb_par *par = info->par;
853 
854 	/* suspend here */
855 	return 0;
856 }
857 
858 /**
859  *	xxxfb_resume - Optional but recommended function. Resume the device.
860  *	@dev: PCI device
861  *
862  *      See Documentation/driver-api/pm/devices.rst for more information
863  */
864 static int xxxfb_resume(struct device *dev)
865 {
866 	struct fb_info *info = dev_get_drvdata(dev);
867 	struct xxxfb_par *par = info->par;
868 
869 	/* resume here */
870 	return 0;
871 }
872 #else
873 #define xxxfb_suspend NULL
874 #define xxxfb_resume NULL
875 #endif /* CONFIG_PM */
876 
877 static const struct pci_device_id xxxfb_id_table[] = {
878 	{ PCI_VENDOR_ID_XXX, PCI_DEVICE_ID_XXX,
879 	  PCI_ANY_ID, PCI_ANY_ID, PCI_BASE_CLASS_DISPLAY << 16,
880 	  PCI_CLASS_MASK, 0 },
881 	{ 0, }
882 };
883 
884 static SIMPLE_DEV_PM_OPS(xxxfb_pm_ops, xxxfb_suspend, xxxfb_resume);
885 
886 /* For PCI drivers */
887 static struct pci_driver xxxfb_driver = {
888 	.name =		"xxxfb",
889 	.id_table =	xxxfb_id_table,
890 	.probe =	xxxfb_probe,
891 	.remove =	xxxfb_remove,
892 	.driver.pm =	xxxfb_pm_ops, /* optional but recommended */
893 };
894 
895 MODULE_DEVICE_TABLE(pci, xxxfb_id_table);
896 
897 int __init xxxfb_init(void)
898 {
899 	/*
900 	 *  For kernel boot options (in 'video=xxxfb:<options>' format)
901 	 */
902 #ifndef MODULE
903 	char *option = NULL;
904 
905 	if (fb_get_options("xxxfb", &option))
906 		return -ENODEV;
907 	xxxfb_setup(option);
908 #endif
909 
910 	return pci_register_driver(&xxxfb_driver);
911 }
912 
913 static void __exit xxxfb_exit(void)
914 {
915 	pci_unregister_driver(&xxxfb_driver);
916 }
917 #else /* non PCI, platform drivers */
918 #include <linux/platform_device.h>
919 /* for platform devices */
920 
921 #ifdef CONFIG_PM
922 /**
923  *	xxxfb_suspend - Optional but recommended function. Suspend the device.
924  *	@dev: platform device
925  *	@msg: the suspend event code.
926  *
927  *      See Documentation/driver-api/pm/devices.rst for more information
928  */
929 static int xxxfb_suspend(struct platform_device *dev, pm_message_t msg)
930 {
931 	struct fb_info *info = platform_get_drvdata(dev);
932 	struct xxxfb_par *par = info->par;
933 
934 	/* suspend here */
935 	return 0;
936 }
937 
938 /**
939  *	xxxfb_resume - Optional but recommended function. Resume the device.
940  *	@dev: platform device
941  *
942  *      See Documentation/driver-api/pm/devices.rst for more information
943  */
944 static int xxxfb_resume(struct platform_dev *dev)
945 {
946 	struct fb_info *info = platform_get_drvdata(dev);
947 	struct xxxfb_par *par = info->par;
948 
949 	/* resume here */
950 	return 0;
951 }
952 #else
953 #define xxxfb_suspend NULL
954 #define xxxfb_resume NULL
955 #endif /* CONFIG_PM */
956 
957 static struct platform_device_driver xxxfb_driver = {
958 	.probe = xxxfb_probe,
959 	.remove = xxxfb_remove,
960 	.suspend = xxxfb_suspend, /* optional but recommended */
961 	.resume = xxxfb_resume,   /* optional but recommended */
962 	.driver = {
963 		.name = "xxxfb",
964 	},
965 };
966 
967 static struct platform_device *xxxfb_device;
968 
969 #ifndef MODULE
970     /*
971      *  Setup
972      */
973 
974 /*
975  * Only necessary if your driver takes special options,
976  * otherwise we fall back on the generic fb_setup().
977  */
978 int __init xxxfb_setup(char *options)
979 {
980     /* Parse user specified options (`video=xxxfb:') */
981 }
982 #endif /* MODULE */
983 
984 static int __init xxxfb_init(void)
985 {
986 	int ret;
987 	/*
988 	 *  For kernel boot options (in 'video=xxxfb:<options>' format)
989 	 */
990 #ifndef MODULE
991 	char *option = NULL;
992 
993 	if (fb_get_options("xxxfb", &option))
994 		return -ENODEV;
995 	xxxfb_setup(option);
996 #endif
997 	ret = platform_driver_register(&xxxfb_driver);
998 
999 	if (!ret) {
1000 		xxxfb_device = platform_device_register_simple("xxxfb", 0,
1001 								NULL, 0);
1002 
1003 		if (IS_ERR(xxxfb_device)) {
1004 			platform_driver_unregister(&xxxfb_driver);
1005 			ret = PTR_ERR(xxxfb_device);
1006 		}
1007 	}
1008 
1009 	return ret;
1010 }
1011 
1012 static void __exit xxxfb_exit(void)
1013 {
1014 	platform_device_unregister(xxxfb_device);
1015 	platform_driver_unregister(&xxxfb_driver);
1016 }
1017 #endif /* CONFIG_PCI */
1018 
1019 /* ------------------------------------------------------------------------- */
1020 
1021 
1022     /*
1023      *  Modularization
1024      */
1025 
1026 module_init(xxxfb_init);
1027 module_exit(xxxfb_exit);
1028 
1029 MODULE_LICENSE("GPL");
1030