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