xref: /linux/drivers/video/fbdev/vfb.c (revision db10cb9b)
1 /*
2  *  linux/drivers/video/vfb.c -- Virtual frame buffer device
3  *
4  *      Copyright (C) 2002 James Simmons
5  *
6  *	Copyright (C) 1997 Geert Uytterhoeven
7  *
8  *  This file is subject to the terms and conditions of the GNU General Public
9  *  License. See the file COPYING in the main directory of this archive for
10  *  more details.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/string.h>
17 #include <linux/mm.h>
18 #include <linux/vmalloc.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 
23 #include <linux/fb.h>
24 #include <linux/init.h>
25 
26     /*
27      *  RAM we reserve for the frame buffer. This defines the maximum screen
28      *  size
29      *
30      *  The default can be overridden if the driver is compiled as a module
31      */
32 
33 #define VIDEOMEMSIZE	(1*1024*1024)	/* 1 MB */
34 
35 static void *videomemory;
36 static u_long videomemorysize = VIDEOMEMSIZE;
37 module_param(videomemorysize, ulong, 0);
38 MODULE_PARM_DESC(videomemorysize, "RAM available to frame buffer (in bytes)");
39 
40 static char *mode_option = NULL;
41 module_param(mode_option, charp, 0);
42 MODULE_PARM_DESC(mode_option, "Preferred video mode (e.g. 640x480-8@60)");
43 
44 static const struct fb_videomode vfb_default = {
45 	.xres =		640,
46 	.yres =		480,
47 	.pixclock =	20000,
48 	.left_margin =	64,
49 	.right_margin =	64,
50 	.upper_margin =	32,
51 	.lower_margin =	32,
52 	.hsync_len =	64,
53 	.vsync_len =	2,
54 	.vmode =	FB_VMODE_NONINTERLACED,
55 };
56 
57 static struct fb_fix_screeninfo vfb_fix = {
58 	.id =		"Virtual FB",
59 	.type =		FB_TYPE_PACKED_PIXELS,
60 	.visual =	FB_VISUAL_PSEUDOCOLOR,
61 	.xpanstep =	1,
62 	.ypanstep =	1,
63 	.ywrapstep =	1,
64 	.accel =	FB_ACCEL_NONE,
65 };
66 
67 static bool vfb_enable __initdata = 0;	/* disabled by default */
68 module_param(vfb_enable, bool, 0);
69 MODULE_PARM_DESC(vfb_enable, "Enable Virtual FB driver");
70 
71 static int vfb_check_var(struct fb_var_screeninfo *var,
72 			 struct fb_info *info);
73 static int vfb_set_par(struct fb_info *info);
74 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
75 			 u_int transp, struct fb_info *info);
76 static int vfb_pan_display(struct fb_var_screeninfo *var,
77 			   struct fb_info *info);
78 static int vfb_mmap(struct fb_info *info,
79 		    struct vm_area_struct *vma);
80 
81 static const struct fb_ops vfb_ops = {
82 	.owner		= THIS_MODULE,
83 	.fb_read        = fb_sys_read,
84 	.fb_write       = fb_sys_write,
85 	.fb_check_var	= vfb_check_var,
86 	.fb_set_par	= vfb_set_par,
87 	.fb_setcolreg	= vfb_setcolreg,
88 	.fb_pan_display	= vfb_pan_display,
89 	.fb_fillrect	= sys_fillrect,
90 	.fb_copyarea	= sys_copyarea,
91 	.fb_imageblit	= sys_imageblit,
92 	.fb_mmap	= vfb_mmap,
93 };
94 
95     /*
96      *  Internal routines
97      */
98 
99 static u_long get_line_length(int xres_virtual, int bpp)
100 {
101 	u_long length;
102 
103 	length = xres_virtual * bpp;
104 	length = (length + 31) & ~31;
105 	length >>= 3;
106 	return (length);
107 }
108 
109     /*
110      *  Setting the video mode has been split into two parts.
111      *  First part, xxxfb_check_var, must not write anything
112      *  to hardware, it should only verify and adjust var.
113      *  This means it doesn't alter par but it does use hardware
114      *  data from it to check this var.
115      */
116 
117 static int vfb_check_var(struct fb_var_screeninfo *var,
118 			 struct fb_info *info)
119 {
120 	u_long line_length;
121 
122 	/*
123 	 *  FB_VMODE_CONUPDATE and FB_VMODE_SMOOTH_XPAN are equal!
124 	 *  as FB_VMODE_SMOOTH_XPAN is only used internally
125 	 */
126 
127 	if (var->vmode & FB_VMODE_CONUPDATE) {
128 		var->vmode |= FB_VMODE_YWRAP;
129 		var->xoffset = info->var.xoffset;
130 		var->yoffset = info->var.yoffset;
131 	}
132 
133 	/*
134 	 *  Some very basic checks
135 	 */
136 	if (!var->xres)
137 		var->xres = 1;
138 	if (!var->yres)
139 		var->yres = 1;
140 	if (var->xres > var->xres_virtual)
141 		var->xres_virtual = var->xres;
142 	if (var->yres > var->yres_virtual)
143 		var->yres_virtual = var->yres;
144 	if (var->bits_per_pixel <= 1)
145 		var->bits_per_pixel = 1;
146 	else if (var->bits_per_pixel <= 8)
147 		var->bits_per_pixel = 8;
148 	else if (var->bits_per_pixel <= 16)
149 		var->bits_per_pixel = 16;
150 	else if (var->bits_per_pixel <= 24)
151 		var->bits_per_pixel = 24;
152 	else if (var->bits_per_pixel <= 32)
153 		var->bits_per_pixel = 32;
154 	else
155 		return -EINVAL;
156 
157 	if (var->xres_virtual < var->xoffset + var->xres)
158 		var->xres_virtual = var->xoffset + var->xres;
159 	if (var->yres_virtual < var->yoffset + var->yres)
160 		var->yres_virtual = var->yoffset + var->yres;
161 
162 	/*
163 	 *  Memory limit
164 	 */
165 	line_length =
166 	    get_line_length(var->xres_virtual, var->bits_per_pixel);
167 	if (line_length * var->yres_virtual > videomemorysize)
168 		return -ENOMEM;
169 
170 	/*
171 	 * Now that we checked it we alter var. The reason being is that the video
172 	 * mode passed in might not work but slight changes to it might make it
173 	 * work. This way we let the user know what is acceptable.
174 	 */
175 	switch (var->bits_per_pixel) {
176 	case 1:
177 	case 8:
178 		var->red.offset = 0;
179 		var->red.length = 8;
180 		var->green.offset = 0;
181 		var->green.length = 8;
182 		var->blue.offset = 0;
183 		var->blue.length = 8;
184 		var->transp.offset = 0;
185 		var->transp.length = 0;
186 		break;
187 	case 16:		/* RGBA 5551 */
188 		if (var->transp.length) {
189 			var->red.offset = 0;
190 			var->red.length = 5;
191 			var->green.offset = 5;
192 			var->green.length = 5;
193 			var->blue.offset = 10;
194 			var->blue.length = 5;
195 			var->transp.offset = 15;
196 			var->transp.length = 1;
197 		} else {	/* RGB 565 */
198 			var->red.offset = 0;
199 			var->red.length = 5;
200 			var->green.offset = 5;
201 			var->green.length = 6;
202 			var->blue.offset = 11;
203 			var->blue.length = 5;
204 			var->transp.offset = 0;
205 			var->transp.length = 0;
206 		}
207 		break;
208 	case 24:		/* RGB 888 */
209 		var->red.offset = 0;
210 		var->red.length = 8;
211 		var->green.offset = 8;
212 		var->green.length = 8;
213 		var->blue.offset = 16;
214 		var->blue.length = 8;
215 		var->transp.offset = 0;
216 		var->transp.length = 0;
217 		break;
218 	case 32:		/* RGBA 8888 */
219 		var->red.offset = 0;
220 		var->red.length = 8;
221 		var->green.offset = 8;
222 		var->green.length = 8;
223 		var->blue.offset = 16;
224 		var->blue.length = 8;
225 		var->transp.offset = 24;
226 		var->transp.length = 8;
227 		break;
228 	}
229 	var->red.msb_right = 0;
230 	var->green.msb_right = 0;
231 	var->blue.msb_right = 0;
232 	var->transp.msb_right = 0;
233 
234 	return 0;
235 }
236 
237 /* This routine actually sets the video mode. It's in here where we
238  * the hardware state info->par and fix which can be affected by the
239  * change in par. For this driver it doesn't do much.
240  */
241 static int vfb_set_par(struct fb_info *info)
242 {
243 	switch (info->var.bits_per_pixel) {
244 	case 1:
245 		info->fix.visual = FB_VISUAL_MONO01;
246 		break;
247 	case 8:
248 		info->fix.visual = FB_VISUAL_PSEUDOCOLOR;
249 		break;
250 	case 16:
251 	case 24:
252 	case 32:
253 		info->fix.visual = FB_VISUAL_TRUECOLOR;
254 		break;
255 	}
256 
257 	info->fix.line_length = get_line_length(info->var.xres_virtual,
258 						info->var.bits_per_pixel);
259 
260 	return 0;
261 }
262 
263     /*
264      *  Set a single color register. The values supplied are already
265      *  rounded down to the hardware's capabilities (according to the
266      *  entries in the var structure). Return != 0 for invalid regno.
267      */
268 
269 static int vfb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
270 			 u_int transp, struct fb_info *info)
271 {
272 	if (regno >= 256)	/* no. of hw registers */
273 		return 1;
274 	/*
275 	 * Program hardware... do anything you want with transp
276 	 */
277 
278 	/* grayscale works only partially under directcolor */
279 	if (info->var.grayscale) {
280 		/* grayscale = 0.30*R + 0.59*G + 0.11*B */
281 		red = green = blue =
282 		    (red * 77 + green * 151 + blue * 28) >> 8;
283 	}
284 
285 	/* Directcolor:
286 	 *   var->{color}.offset contains start of bitfield
287 	 *   var->{color}.length contains length of bitfield
288 	 *   {hardwarespecific} contains width of RAMDAC
289 	 *   cmap[X] is programmed to (X << red.offset) | (X << green.offset) | (X << blue.offset)
290 	 *   RAMDAC[X] is programmed to (red, green, blue)
291 	 *
292 	 * Pseudocolor:
293 	 *    var->{color}.offset is 0 unless the palette index takes less than
294 	 *                        bits_per_pixel bits and is stored in the upper
295 	 *                        bits of the pixel value
296 	 *    var->{color}.length is set so that 1 << length is the number of available
297 	 *                        palette entries
298 	 *    cmap is not used
299 	 *    RAMDAC[X] is programmed to (red, green, blue)
300 	 *
301 	 * Truecolor:
302 	 *    does not use DAC. Usually 3 are present.
303 	 *    var->{color}.offset contains start of bitfield
304 	 *    var->{color}.length contains length of bitfield
305 	 *    cmap is programmed to (red << red.offset) | (green << green.offset) |
306 	 *                      (blue << blue.offset) | (transp << transp.offset)
307 	 *    RAMDAC does not exist
308 	 */
309 #define CNVT_TOHW(val,width) ((((val)<<(width))+0x7FFF-(val))>>16)
310 	switch (info->fix.visual) {
311 	case FB_VISUAL_TRUECOLOR:
312 	case FB_VISUAL_PSEUDOCOLOR:
313 		red = CNVT_TOHW(red, info->var.red.length);
314 		green = CNVT_TOHW(green, info->var.green.length);
315 		blue = CNVT_TOHW(blue, info->var.blue.length);
316 		transp = CNVT_TOHW(transp, info->var.transp.length);
317 		break;
318 	case FB_VISUAL_DIRECTCOLOR:
319 		red = CNVT_TOHW(red, 8);	/* expect 8 bit DAC */
320 		green = CNVT_TOHW(green, 8);
321 		blue = CNVT_TOHW(blue, 8);
322 		/* hey, there is bug in transp handling... */
323 		transp = CNVT_TOHW(transp, 8);
324 		break;
325 	}
326 #undef CNVT_TOHW
327 	/* Truecolor has hardware independent palette */
328 	if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
329 		u32 v;
330 
331 		if (regno >= 16)
332 			return 1;
333 
334 		v = (red << info->var.red.offset) |
335 		    (green << info->var.green.offset) |
336 		    (blue << info->var.blue.offset) |
337 		    (transp << info->var.transp.offset);
338 		switch (info->var.bits_per_pixel) {
339 		case 8:
340 			break;
341 		case 16:
342 			((u32 *) (info->pseudo_palette))[regno] = v;
343 			break;
344 		case 24:
345 		case 32:
346 			((u32 *) (info->pseudo_palette))[regno] = v;
347 			break;
348 		}
349 		return 0;
350 	}
351 	return 0;
352 }
353 
354     /*
355      *  Pan or Wrap the Display
356      *
357      *  This call looks only at xoffset, yoffset and the FB_VMODE_YWRAP flag
358      */
359 
360 static int vfb_pan_display(struct fb_var_screeninfo *var,
361 			   struct fb_info *info)
362 {
363 	if (var->vmode & FB_VMODE_YWRAP) {
364 		if (var->yoffset >= info->var.yres_virtual ||
365 		    var->xoffset)
366 			return -EINVAL;
367 	} else {
368 		if (var->xoffset + info->var.xres > info->var.xres_virtual ||
369 		    var->yoffset + info->var.yres > info->var.yres_virtual)
370 			return -EINVAL;
371 	}
372 	info->var.xoffset = var->xoffset;
373 	info->var.yoffset = var->yoffset;
374 	if (var->vmode & FB_VMODE_YWRAP)
375 		info->var.vmode |= FB_VMODE_YWRAP;
376 	else
377 		info->var.vmode &= ~FB_VMODE_YWRAP;
378 	return 0;
379 }
380 
381     /*
382      *  Most drivers don't need their own mmap function
383      */
384 
385 static int vfb_mmap(struct fb_info *info,
386 		    struct vm_area_struct *vma)
387 {
388 	return remap_vmalloc_range(vma, (void *)info->fix.smem_start, vma->vm_pgoff);
389 }
390 
391 #ifndef MODULE
392 /*
393  * The virtual framebuffer driver is only enabled if explicitly
394  * requested by passing 'video=vfb:' (or any actual options).
395  */
396 static int __init vfb_setup(char *options)
397 {
398 	char *this_opt;
399 
400 	vfb_enable = 0;
401 
402 	if (!options)
403 		return 1;
404 
405 	vfb_enable = 1;
406 
407 	if (!*options)
408 		return 1;
409 
410 	while ((this_opt = strsep(&options, ",")) != NULL) {
411 		if (!*this_opt)
412 			continue;
413 		/* Test disable for backwards compatibility */
414 		if (!strcmp(this_opt, "disable"))
415 			vfb_enable = 0;
416 		else
417 			mode_option = this_opt;
418 	}
419 	return 1;
420 }
421 #endif  /*  MODULE  */
422 
423     /*
424      *  Initialisation
425      */
426 
427 static int vfb_probe(struct platform_device *dev)
428 {
429 	struct fb_info *info;
430 	unsigned int size = PAGE_ALIGN(videomemorysize);
431 	int retval = -ENOMEM;
432 
433 	/*
434 	 * For real video cards we use ioremap.
435 	 */
436 	if (!(videomemory = vmalloc_32_user(size)))
437 		return retval;
438 
439 	info = framebuffer_alloc(sizeof(u32) * 256, &dev->dev);
440 	if (!info)
441 		goto err;
442 
443 	info->screen_buffer = videomemory;
444 	info->fbops = &vfb_ops;
445 
446 	if (!fb_find_mode(&info->var, info, mode_option,
447 			  NULL, 0, &vfb_default, 8)){
448 		fb_err(info, "Unable to find usable video mode.\n");
449 		retval = -EINVAL;
450 		goto err1;
451 	}
452 
453 	vfb_fix.smem_start = (unsigned long) videomemory;
454 	vfb_fix.smem_len = videomemorysize;
455 	info->fix = vfb_fix;
456 	info->pseudo_palette = info->par;
457 	info->par = NULL;
458 
459 	retval = fb_alloc_cmap(&info->cmap, 256, 0);
460 	if (retval < 0)
461 		goto err1;
462 
463 	retval = register_framebuffer(info);
464 	if (retval < 0)
465 		goto err2;
466 	platform_set_drvdata(dev, info);
467 
468 	vfb_set_par(info);
469 
470 	fb_info(info, "Virtual frame buffer device, using %ldK of video memory\n",
471 		videomemorysize >> 10);
472 	return 0;
473 err2:
474 	fb_dealloc_cmap(&info->cmap);
475 err1:
476 	framebuffer_release(info);
477 err:
478 	vfree(videomemory);
479 	return retval;
480 }
481 
482 static void vfb_remove(struct platform_device *dev)
483 {
484 	struct fb_info *info = platform_get_drvdata(dev);
485 
486 	if (info) {
487 		unregister_framebuffer(info);
488 		vfree(videomemory);
489 		fb_dealloc_cmap(&info->cmap);
490 		framebuffer_release(info);
491 	}
492 }
493 
494 static struct platform_driver vfb_driver = {
495 	.probe	= vfb_probe,
496 	.remove_new = vfb_remove,
497 	.driver = {
498 		.name	= "vfb",
499 	},
500 };
501 
502 static struct platform_device *vfb_device;
503 
504 static int __init vfb_init(void)
505 {
506 	int ret = 0;
507 
508 #ifndef MODULE
509 	char *option = NULL;
510 
511 	if (fb_get_options("vfb", &option))
512 		return -ENODEV;
513 	vfb_setup(option);
514 #endif
515 
516 	if (!vfb_enable)
517 		return -ENXIO;
518 
519 	ret = platform_driver_register(&vfb_driver);
520 
521 	if (!ret) {
522 		vfb_device = platform_device_alloc("vfb", 0);
523 
524 		if (vfb_device)
525 			ret = platform_device_add(vfb_device);
526 		else
527 			ret = -ENOMEM;
528 
529 		if (ret) {
530 			platform_device_put(vfb_device);
531 			platform_driver_unregister(&vfb_driver);
532 		}
533 	}
534 
535 	return ret;
536 }
537 
538 module_init(vfb_init);
539 
540 #ifdef MODULE
541 static void __exit vfb_exit(void)
542 {
543 	platform_device_unregister(vfb_device);
544 	platform_driver_unregister(&vfb_driver);
545 }
546 
547 module_exit(vfb_exit);
548 
549 MODULE_LICENSE("GPL");
550 #endif				/* MODULE */
551