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