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