1 /*
2 * linux/drivers/video/fbmem.c
3 *
4 * Copyright (C) 1994 Martin Schaller
5 *
6 * 2001 - Documented with DocBook
7 * - Brad Douglas <brad@neruo.com>
8 *
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of this archive
11 * for more details.
12 */
13
14 #include <linux/module.h>
15
16 #include <linux/compat.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/major.h>
21 #include <linux/slab.h>
22 #include <linux/mm.h>
23 #include <linux/mman.h>
24 #include <linux/vt.h>
25 #include <linux/init.h>
26 #include <linux/linux_logo.h>
27 #include <linux/proc_fs.h>
28 #include <linux/seq_file.h>
29 #include <linux/console.h>
30 #include <linux/kmod.h>
31 #include <linux/err.h>
32 #include <linux/device.h>
33 #include <linux/efi.h>
34 #include <linux/fb.h>
35 #include <linux/fbcon.h>
36 #include <linux/mem_encrypt.h>
37
38 #include <asm/fb.h>
39
40
41 /*
42 * Frame buffer device initialization and setup routines
43 */
44
45 #define FBPIXMAPSIZE (1024 * 8)
46
47 static DEFINE_MUTEX(registration_lock);
48
49 struct fb_info *registered_fb[FB_MAX] __read_mostly;
50 EXPORT_SYMBOL(registered_fb);
51
52 int num_registered_fb __read_mostly;
53 EXPORT_SYMBOL(num_registered_fb);
54
get_fb_info(unsigned int idx)55 static struct fb_info *get_fb_info(unsigned int idx)
56 {
57 struct fb_info *fb_info;
58
59 if (idx >= FB_MAX)
60 return ERR_PTR(-ENODEV);
61
62 mutex_lock(®istration_lock);
63 fb_info = registered_fb[idx];
64 if (fb_info)
65 atomic_inc(&fb_info->count);
66 mutex_unlock(®istration_lock);
67
68 return fb_info;
69 }
70
put_fb_info(struct fb_info * fb_info)71 static void put_fb_info(struct fb_info *fb_info)
72 {
73 if (!atomic_dec_and_test(&fb_info->count))
74 return;
75 if (fb_info->fbops->fb_destroy)
76 fb_info->fbops->fb_destroy(fb_info);
77 }
78
lock_fb_info(struct fb_info * info)79 int lock_fb_info(struct fb_info *info)
80 {
81 mutex_lock(&info->lock);
82 if (!info->fbops) {
83 mutex_unlock(&info->lock);
84 return 0;
85 }
86 return 1;
87 }
88 EXPORT_SYMBOL(lock_fb_info);
89
90 /*
91 * Helpers
92 */
93
fb_get_color_depth(struct fb_var_screeninfo * var,struct fb_fix_screeninfo * fix)94 int fb_get_color_depth(struct fb_var_screeninfo *var,
95 struct fb_fix_screeninfo *fix)
96 {
97 int depth = 0;
98
99 if (fix->visual == FB_VISUAL_MONO01 ||
100 fix->visual == FB_VISUAL_MONO10)
101 depth = 1;
102 else {
103 if (var->green.length == var->blue.length &&
104 var->green.length == var->red.length &&
105 var->green.offset == var->blue.offset &&
106 var->green.offset == var->red.offset)
107 depth = var->green.length;
108 else
109 depth = var->green.length + var->red.length +
110 var->blue.length;
111 }
112
113 return depth;
114 }
115 EXPORT_SYMBOL(fb_get_color_depth);
116
117 /*
118 * Data padding functions.
119 */
fb_pad_aligned_buffer(u8 * dst,u32 d_pitch,u8 * src,u32 s_pitch,u32 height)120 void fb_pad_aligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 s_pitch, u32 height)
121 {
122 __fb_pad_aligned_buffer(dst, d_pitch, src, s_pitch, height);
123 }
124 EXPORT_SYMBOL(fb_pad_aligned_buffer);
125
fb_pad_unaligned_buffer(u8 * dst,u32 d_pitch,u8 * src,u32 idx,u32 height,u32 shift_high,u32 shift_low,u32 mod)126 void fb_pad_unaligned_buffer(u8 *dst, u32 d_pitch, u8 *src, u32 idx, u32 height,
127 u32 shift_high, u32 shift_low, u32 mod)
128 {
129 u8 mask = (u8) (0xfff << shift_high), tmp;
130 int i, j;
131
132 for (i = height; i--; ) {
133 for (j = 0; j < idx; j++) {
134 tmp = dst[j];
135 tmp &= mask;
136 tmp |= *src >> shift_low;
137 dst[j] = tmp;
138 tmp = *src << shift_high;
139 dst[j+1] = tmp;
140 src++;
141 }
142 tmp = dst[idx];
143 tmp &= mask;
144 tmp |= *src >> shift_low;
145 dst[idx] = tmp;
146 if (shift_high < mod) {
147 tmp = *src << shift_high;
148 dst[idx+1] = tmp;
149 }
150 src++;
151 dst += d_pitch;
152 }
153 }
154 EXPORT_SYMBOL(fb_pad_unaligned_buffer);
155
156 /*
157 * we need to lock this section since fb_cursor
158 * may use fb_imageblit()
159 */
fb_get_buffer_offset(struct fb_info * info,struct fb_pixmap * buf,u32 size)160 char* fb_get_buffer_offset(struct fb_info *info, struct fb_pixmap *buf, u32 size)
161 {
162 u32 align = buf->buf_align - 1, offset;
163 char *addr = buf->addr;
164
165 /* If IO mapped, we need to sync before access, no sharing of
166 * the pixmap is done
167 */
168 if (buf->flags & FB_PIXMAP_IO) {
169 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
170 info->fbops->fb_sync(info);
171 return addr;
172 }
173
174 /* See if we fit in the remaining pixmap space */
175 offset = buf->offset + align;
176 offset &= ~align;
177 if (offset + size > buf->size) {
178 /* We do not fit. In order to be able to re-use the buffer,
179 * we must ensure no asynchronous DMA'ing or whatever operation
180 * is in progress, we sync for that.
181 */
182 if (info->fbops->fb_sync && (buf->flags & FB_PIXMAP_SYNC))
183 info->fbops->fb_sync(info);
184 offset = 0;
185 }
186 buf->offset = offset + size;
187 addr += offset;
188
189 return addr;
190 }
191 EXPORT_SYMBOL(fb_get_buffer_offset);
192
193 #ifdef CONFIG_LOGO
194
safe_shift(unsigned d,int n)195 static inline unsigned safe_shift(unsigned d, int n)
196 {
197 return n < 0 ? d >> -n : d << n;
198 }
199
fb_set_logocmap(struct fb_info * info,const struct linux_logo * logo)200 static void fb_set_logocmap(struct fb_info *info,
201 const struct linux_logo *logo)
202 {
203 struct fb_cmap palette_cmap;
204 u16 palette_green[16];
205 u16 palette_blue[16];
206 u16 palette_red[16];
207 int i, j, n;
208 const unsigned char *clut = logo->clut;
209
210 palette_cmap.start = 0;
211 palette_cmap.len = 16;
212 palette_cmap.red = palette_red;
213 palette_cmap.green = palette_green;
214 palette_cmap.blue = palette_blue;
215 palette_cmap.transp = NULL;
216
217 for (i = 0; i < logo->clutsize; i += n) {
218 n = logo->clutsize - i;
219 /* palette_cmap provides space for only 16 colors at once */
220 if (n > 16)
221 n = 16;
222 palette_cmap.start = 32 + i;
223 palette_cmap.len = n;
224 for (j = 0; j < n; ++j) {
225 palette_cmap.red[j] = clut[0] << 8 | clut[0];
226 palette_cmap.green[j] = clut[1] << 8 | clut[1];
227 palette_cmap.blue[j] = clut[2] << 8 | clut[2];
228 clut += 3;
229 }
230 fb_set_cmap(&palette_cmap, info);
231 }
232 }
233
fb_set_logo_truepalette(struct fb_info * info,const struct linux_logo * logo,u32 * palette)234 static void fb_set_logo_truepalette(struct fb_info *info,
235 const struct linux_logo *logo,
236 u32 *palette)
237 {
238 static const unsigned char mask[] = { 0,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff };
239 unsigned char redmask, greenmask, bluemask;
240 int redshift, greenshift, blueshift;
241 int i;
242 const unsigned char *clut = logo->clut;
243
244 /*
245 * We have to create a temporary palette since console palette is only
246 * 16 colors long.
247 */
248 /* Bug: Doesn't obey msb_right ... (who needs that?) */
249 redmask = mask[info->var.red.length < 8 ? info->var.red.length : 8];
250 greenmask = mask[info->var.green.length < 8 ? info->var.green.length : 8];
251 bluemask = mask[info->var.blue.length < 8 ? info->var.blue.length : 8];
252 redshift = info->var.red.offset - (8 - info->var.red.length);
253 greenshift = info->var.green.offset - (8 - info->var.green.length);
254 blueshift = info->var.blue.offset - (8 - info->var.blue.length);
255
256 for ( i = 0; i < logo->clutsize; i++) {
257 palette[i+32] = (safe_shift((clut[0] & redmask), redshift) |
258 safe_shift((clut[1] & greenmask), greenshift) |
259 safe_shift((clut[2] & bluemask), blueshift));
260 clut += 3;
261 }
262 }
263
fb_set_logo_directpalette(struct fb_info * info,const struct linux_logo * logo,u32 * palette)264 static void fb_set_logo_directpalette(struct fb_info *info,
265 const struct linux_logo *logo,
266 u32 *palette)
267 {
268 int redshift, greenshift, blueshift;
269 int i;
270
271 redshift = info->var.red.offset;
272 greenshift = info->var.green.offset;
273 blueshift = info->var.blue.offset;
274
275 for (i = 32; i < 32 + logo->clutsize; i++)
276 palette[i] = i << redshift | i << greenshift | i << blueshift;
277 }
278
fb_set_logo(struct fb_info * info,const struct linux_logo * logo,u8 * dst,int depth)279 static void fb_set_logo(struct fb_info *info,
280 const struct linux_logo *logo, u8 *dst,
281 int depth)
282 {
283 int i, j, k;
284 const u8 *src = logo->data;
285 u8 xor = (info->fix.visual == FB_VISUAL_MONO01) ? 0xff : 0;
286 u8 fg = 1, d;
287
288 switch (fb_get_color_depth(&info->var, &info->fix)) {
289 case 1:
290 fg = 1;
291 break;
292 case 2:
293 fg = 3;
294 break;
295 default:
296 fg = 7;
297 break;
298 }
299
300 if (info->fix.visual == FB_VISUAL_MONO01 ||
301 info->fix.visual == FB_VISUAL_MONO10)
302 fg = ~((u8) (0xfff << info->var.green.length));
303
304 switch (depth) {
305 case 4:
306 for (i = 0; i < logo->height; i++)
307 for (j = 0; j < logo->width; src++) {
308 *dst++ = *src >> 4;
309 j++;
310 if (j < logo->width) {
311 *dst++ = *src & 0x0f;
312 j++;
313 }
314 }
315 break;
316 case 1:
317 for (i = 0; i < logo->height; i++) {
318 for (j = 0; j < logo->width; src++) {
319 d = *src ^ xor;
320 for (k = 7; k >= 0 && j < logo->width; k--) {
321 *dst++ = ((d >> k) & 1) ? fg : 0;
322 j++;
323 }
324 }
325 }
326 break;
327 }
328 }
329
330 /*
331 * Three (3) kinds of logo maps exist. linux_logo_clut224 (>16 colors),
332 * linux_logo_vga16 (16 colors) and linux_logo_mono (2 colors). Depending on
333 * the visual format and color depth of the framebuffer, the DAC, the
334 * pseudo_palette, and the logo data will be adjusted accordingly.
335 *
336 * Case 1 - linux_logo_clut224:
337 * Color exceeds the number of console colors (16), thus we set the hardware DAC
338 * using fb_set_cmap() appropriately. The "needs_cmapreset" flag will be set.
339 *
340 * For visuals that require color info from the pseudo_palette, we also construct
341 * one for temporary use. The "needs_directpalette" or "needs_truepalette" flags
342 * will be set.
343 *
344 * Case 2 - linux_logo_vga16:
345 * The number of colors just matches the console colors, thus there is no need
346 * to set the DAC or the pseudo_palette. However, the bitmap is packed, ie,
347 * each byte contains color information for two pixels (upper and lower nibble).
348 * To be consistent with fb_imageblit() usage, we therefore separate the two
349 * nibbles into separate bytes. The "depth" flag will be set to 4.
350 *
351 * Case 3 - linux_logo_mono:
352 * This is similar with Case 2. Each byte contains information for 8 pixels.
353 * We isolate each bit and expand each into a byte. The "depth" flag will
354 * be set to 1.
355 */
356 static struct logo_data {
357 int depth;
358 int needs_directpalette;
359 int needs_truepalette;
360 int needs_cmapreset;
361 const struct linux_logo *logo;
362 } fb_logo __read_mostly;
363
fb_rotate_logo_ud(const u8 * in,u8 * out,u32 width,u32 height)364 static void fb_rotate_logo_ud(const u8 *in, u8 *out, u32 width, u32 height)
365 {
366 u32 size = width * height, i;
367
368 out += size - 1;
369
370 for (i = size; i--; )
371 *out-- = *in++;
372 }
373
fb_rotate_logo_cw(const u8 * in,u8 * out,u32 width,u32 height)374 static void fb_rotate_logo_cw(const u8 *in, u8 *out, u32 width, u32 height)
375 {
376 int i, j, h = height - 1;
377
378 for (i = 0; i < height; i++)
379 for (j = 0; j < width; j++)
380 out[height * j + h - i] = *in++;
381 }
382
fb_rotate_logo_ccw(const u8 * in,u8 * out,u32 width,u32 height)383 static void fb_rotate_logo_ccw(const u8 *in, u8 *out, u32 width, u32 height)
384 {
385 int i, j, w = width - 1;
386
387 for (i = 0; i < height; i++)
388 for (j = 0; j < width; j++)
389 out[height * (w - j) + i] = *in++;
390 }
391
fb_rotate_logo(struct fb_info * info,u8 * dst,struct fb_image * image,int rotate)392 static void fb_rotate_logo(struct fb_info *info, u8 *dst,
393 struct fb_image *image, int rotate)
394 {
395 u32 tmp;
396
397 if (rotate == FB_ROTATE_UD) {
398 fb_rotate_logo_ud(image->data, dst, image->width,
399 image->height);
400 image->dx = info->var.xres - image->width - image->dx;
401 image->dy = info->var.yres - image->height - image->dy;
402 } else if (rotate == FB_ROTATE_CW) {
403 fb_rotate_logo_cw(image->data, dst, image->width,
404 image->height);
405 tmp = image->width;
406 image->width = image->height;
407 image->height = tmp;
408 tmp = image->dy;
409 image->dy = image->dx;
410 image->dx = info->var.xres - image->width - tmp;
411 } else if (rotate == FB_ROTATE_CCW) {
412 fb_rotate_logo_ccw(image->data, dst, image->width,
413 image->height);
414 tmp = image->width;
415 image->width = image->height;
416 image->height = tmp;
417 tmp = image->dx;
418 image->dx = image->dy;
419 image->dy = info->var.yres - image->height - tmp;
420 }
421
422 image->data = dst;
423 }
424
fb_do_show_logo(struct fb_info * info,struct fb_image * image,int rotate,unsigned int num)425 static void fb_do_show_logo(struct fb_info *info, struct fb_image *image,
426 int rotate, unsigned int num)
427 {
428 unsigned int x;
429
430 if (image->width > info->var.xres || image->height > info->var.yres)
431 return;
432
433 if (rotate == FB_ROTATE_UR) {
434 for (x = 0;
435 x < num && image->dx + image->width <= info->var.xres;
436 x++) {
437 info->fbops->fb_imageblit(info, image);
438 image->dx += image->width + 8;
439 }
440 } else if (rotate == FB_ROTATE_UD) {
441 u32 dx = image->dx;
442
443 for (x = 0; x < num && image->dx <= dx; x++) {
444 info->fbops->fb_imageblit(info, image);
445 image->dx -= image->width + 8;
446 }
447 } else if (rotate == FB_ROTATE_CW) {
448 for (x = 0;
449 x < num && image->dy + image->height <= info->var.yres;
450 x++) {
451 info->fbops->fb_imageblit(info, image);
452 image->dy += image->height + 8;
453 }
454 } else if (rotate == FB_ROTATE_CCW) {
455 u32 dy = image->dy;
456
457 for (x = 0; x < num && image->dy <= dy; x++) {
458 info->fbops->fb_imageblit(info, image);
459 image->dy -= image->height + 8;
460 }
461 }
462 }
463
fb_show_logo_line(struct fb_info * info,int rotate,const struct linux_logo * logo,int y,unsigned int n)464 static int fb_show_logo_line(struct fb_info *info, int rotate,
465 const struct linux_logo *logo, int y,
466 unsigned int n)
467 {
468 u32 *palette = NULL, *saved_pseudo_palette = NULL;
469 unsigned char *logo_new = NULL, *logo_rotate = NULL;
470 struct fb_image image;
471
472 /* Return if the frame buffer is not mapped or suspended */
473 if (logo == NULL || info->state != FBINFO_STATE_RUNNING ||
474 info->fbops->owner)
475 return 0;
476
477 image.depth = 8;
478 image.data = logo->data;
479
480 if (fb_logo.needs_cmapreset)
481 fb_set_logocmap(info, logo);
482
483 if (fb_logo.needs_truepalette ||
484 fb_logo.needs_directpalette) {
485 palette = kmalloc(256 * 4, GFP_KERNEL);
486 if (palette == NULL)
487 return 0;
488
489 if (fb_logo.needs_truepalette)
490 fb_set_logo_truepalette(info, logo, palette);
491 else
492 fb_set_logo_directpalette(info, logo, palette);
493
494 saved_pseudo_palette = info->pseudo_palette;
495 info->pseudo_palette = palette;
496 }
497
498 if (fb_logo.depth <= 4) {
499 logo_new = kmalloc(logo->width * logo->height, GFP_KERNEL);
500 if (logo_new == NULL) {
501 kfree(palette);
502 if (saved_pseudo_palette)
503 info->pseudo_palette = saved_pseudo_palette;
504 return 0;
505 }
506 image.data = logo_new;
507 fb_set_logo(info, logo, logo_new, fb_logo.depth);
508 }
509
510 image.dx = 0;
511 image.dy = y;
512 image.width = logo->width;
513 image.height = logo->height;
514
515 if (rotate) {
516 logo_rotate = kmalloc(logo->width *
517 logo->height, GFP_KERNEL);
518 if (logo_rotate)
519 fb_rotate_logo(info, logo_rotate, &image, rotate);
520 }
521
522 fb_do_show_logo(info, &image, rotate, n);
523
524 kfree(palette);
525 if (saved_pseudo_palette != NULL)
526 info->pseudo_palette = saved_pseudo_palette;
527 kfree(logo_new);
528 kfree(logo_rotate);
529 return logo->height;
530 }
531
532
533 #ifdef CONFIG_FB_LOGO_EXTRA
534
535 #define FB_LOGO_EX_NUM_MAX 10
536 static struct logo_data_extra {
537 const struct linux_logo *logo;
538 unsigned int n;
539 } fb_logo_ex[FB_LOGO_EX_NUM_MAX];
540 static unsigned int fb_logo_ex_num;
541
fb_append_extra_logo(const struct linux_logo * logo,unsigned int n)542 void fb_append_extra_logo(const struct linux_logo *logo, unsigned int n)
543 {
544 if (!n || fb_logo_ex_num == FB_LOGO_EX_NUM_MAX)
545 return;
546
547 fb_logo_ex[fb_logo_ex_num].logo = logo;
548 fb_logo_ex[fb_logo_ex_num].n = n;
549 fb_logo_ex_num++;
550 }
551
fb_prepare_extra_logos(struct fb_info * info,unsigned int height,unsigned int yres)552 static int fb_prepare_extra_logos(struct fb_info *info, unsigned int height,
553 unsigned int yres)
554 {
555 unsigned int i;
556
557 /* FIXME: logo_ex supports only truecolor fb. */
558 if (info->fix.visual != FB_VISUAL_TRUECOLOR)
559 fb_logo_ex_num = 0;
560
561 for (i = 0; i < fb_logo_ex_num; i++) {
562 if (fb_logo_ex[i].logo->type != fb_logo.logo->type) {
563 fb_logo_ex[i].logo = NULL;
564 continue;
565 }
566 height += fb_logo_ex[i].logo->height;
567 if (height > yres) {
568 height -= fb_logo_ex[i].logo->height;
569 fb_logo_ex_num = i;
570 break;
571 }
572 }
573 return height;
574 }
575
fb_show_extra_logos(struct fb_info * info,int y,int rotate)576 static int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
577 {
578 unsigned int i;
579
580 for (i = 0; i < fb_logo_ex_num; i++)
581 y += fb_show_logo_line(info, rotate,
582 fb_logo_ex[i].logo, y, fb_logo_ex[i].n);
583
584 return y;
585 }
586
587 #else /* !CONFIG_FB_LOGO_EXTRA */
588
fb_prepare_extra_logos(struct fb_info * info,unsigned int height,unsigned int yres)589 static inline int fb_prepare_extra_logos(struct fb_info *info,
590 unsigned int height,
591 unsigned int yres)
592 {
593 return height;
594 }
595
fb_show_extra_logos(struct fb_info * info,int y,int rotate)596 static inline int fb_show_extra_logos(struct fb_info *info, int y, int rotate)
597 {
598 return y;
599 }
600
601 #endif /* CONFIG_FB_LOGO_EXTRA */
602
603
fb_prepare_logo(struct fb_info * info,int rotate)604 int fb_prepare_logo(struct fb_info *info, int rotate)
605 {
606 int depth = fb_get_color_depth(&info->var, &info->fix);
607 unsigned int yres;
608
609 memset(&fb_logo, 0, sizeof(struct logo_data));
610
611 if (info->flags & FBINFO_MISC_TILEBLITTING ||
612 info->fbops->owner)
613 return 0;
614
615 if (info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
616 depth = info->var.blue.length;
617 if (info->var.red.length < depth)
618 depth = info->var.red.length;
619 if (info->var.green.length < depth)
620 depth = info->var.green.length;
621 }
622
623 if (info->fix.visual == FB_VISUAL_STATIC_PSEUDOCOLOR && depth > 4) {
624 /* assume console colormap */
625 depth = 4;
626 }
627
628 /* Return if no suitable logo was found */
629 fb_logo.logo = fb_find_logo(depth);
630
631 if (!fb_logo.logo) {
632 return 0;
633 }
634
635 if (rotate == FB_ROTATE_UR || rotate == FB_ROTATE_UD)
636 yres = info->var.yres;
637 else
638 yres = info->var.xres;
639
640 if (fb_logo.logo->height > yres) {
641 fb_logo.logo = NULL;
642 return 0;
643 }
644
645 /* What depth we asked for might be different from what we get */
646 if (fb_logo.logo->type == LINUX_LOGO_CLUT224)
647 fb_logo.depth = 8;
648 else if (fb_logo.logo->type == LINUX_LOGO_VGA16)
649 fb_logo.depth = 4;
650 else
651 fb_logo.depth = 1;
652
653
654 if (fb_logo.depth > 4 && depth > 4) {
655 switch (info->fix.visual) {
656 case FB_VISUAL_TRUECOLOR:
657 fb_logo.needs_truepalette = 1;
658 break;
659 case FB_VISUAL_DIRECTCOLOR:
660 fb_logo.needs_directpalette = 1;
661 fb_logo.needs_cmapreset = 1;
662 break;
663 case FB_VISUAL_PSEUDOCOLOR:
664 fb_logo.needs_cmapreset = 1;
665 break;
666 }
667 }
668
669 return fb_prepare_extra_logos(info, fb_logo.logo->height, yres);
670 }
671
fb_show_logo(struct fb_info * info,int rotate)672 int fb_show_logo(struct fb_info *info, int rotate)
673 {
674 int y;
675
676 y = fb_show_logo_line(info, rotate, fb_logo.logo, 0,
677 num_online_cpus());
678 y = fb_show_extra_logos(info, y, rotate);
679
680 return y;
681 }
682 #else
fb_prepare_logo(struct fb_info * info,int rotate)683 int fb_prepare_logo(struct fb_info *info, int rotate) { return 0; }
fb_show_logo(struct fb_info * info,int rotate)684 int fb_show_logo(struct fb_info *info, int rotate) { return 0; }
685 #endif /* CONFIG_LOGO */
686 EXPORT_SYMBOL(fb_prepare_logo);
687 EXPORT_SYMBOL(fb_show_logo);
688
fb_seq_start(struct seq_file * m,loff_t * pos)689 static void *fb_seq_start(struct seq_file *m, loff_t *pos)
690 {
691 mutex_lock(®istration_lock);
692 return (*pos < FB_MAX) ? pos : NULL;
693 }
694
fb_seq_next(struct seq_file * m,void * v,loff_t * pos)695 static void *fb_seq_next(struct seq_file *m, void *v, loff_t *pos)
696 {
697 (*pos)++;
698 return (*pos < FB_MAX) ? pos : NULL;
699 }
700
fb_seq_stop(struct seq_file * m,void * v)701 static void fb_seq_stop(struct seq_file *m, void *v)
702 {
703 mutex_unlock(®istration_lock);
704 }
705
fb_seq_show(struct seq_file * m,void * v)706 static int fb_seq_show(struct seq_file *m, void *v)
707 {
708 int i = *(loff_t *)v;
709 struct fb_info *fi = registered_fb[i];
710
711 if (fi)
712 seq_printf(m, "%d %s\n", fi->node, fi->fix.id);
713 return 0;
714 }
715
716 static const struct seq_operations proc_fb_seq_ops = {
717 .start = fb_seq_start,
718 .next = fb_seq_next,
719 .stop = fb_seq_stop,
720 .show = fb_seq_show,
721 };
722
proc_fb_open(struct inode * inode,struct file * file)723 static int proc_fb_open(struct inode *inode, struct file *file)
724 {
725 return seq_open(file, &proc_fb_seq_ops);
726 }
727
728 static const struct file_operations fb_proc_fops = {
729 .owner = THIS_MODULE,
730 .open = proc_fb_open,
731 .read = seq_read,
732 .llseek = seq_lseek,
733 .release = seq_release,
734 };
735
736 /*
737 * We hold a reference to the fb_info in file->private_data,
738 * but if the current registered fb has changed, we don't
739 * actually want to use it.
740 *
741 * So look up the fb_info using the inode minor number,
742 * and just verify it against the reference we have.
743 */
file_fb_info(struct file * file)744 static struct fb_info *file_fb_info(struct file *file)
745 {
746 struct inode *inode = file_inode(file);
747 int fbidx = iminor(inode);
748 struct fb_info *info = registered_fb[fbidx];
749
750 if (info != file->private_data)
751 info = NULL;
752 return info;
753 }
754
755 static ssize_t
fb_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)756 fb_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
757 {
758 unsigned long p = *ppos;
759 struct fb_info *info = file_fb_info(file);
760 u8 *buffer, *dst;
761 u8 __iomem *src;
762 int c, cnt = 0, err = 0;
763 unsigned long total_size;
764
765 if (!info || ! info->screen_base)
766 return -ENODEV;
767
768 if (info->state != FBINFO_STATE_RUNNING)
769 return -EPERM;
770
771 if (info->fbops->fb_read)
772 return info->fbops->fb_read(info, buf, count, ppos);
773
774 total_size = info->screen_size;
775
776 if (total_size == 0)
777 total_size = info->fix.smem_len;
778
779 if (p >= total_size)
780 return 0;
781
782 if (count >= total_size)
783 count = total_size;
784
785 if (count + p > total_size)
786 count = total_size - p;
787
788 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
789 GFP_KERNEL);
790 if (!buffer)
791 return -ENOMEM;
792
793 src = (u8 __iomem *) (info->screen_base + p);
794
795 if (info->fbops->fb_sync)
796 info->fbops->fb_sync(info);
797
798 while (count) {
799 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
800 dst = buffer;
801 fb_memcpy_fromfb(dst, src, c);
802 dst += c;
803 src += c;
804
805 if (copy_to_user(buf, buffer, c)) {
806 err = -EFAULT;
807 break;
808 }
809 *ppos += c;
810 buf += c;
811 cnt += c;
812 count -= c;
813 }
814
815 kfree(buffer);
816
817 return (err) ? err : cnt;
818 }
819
820 static ssize_t
fb_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)821 fb_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
822 {
823 unsigned long p = *ppos;
824 struct fb_info *info = file_fb_info(file);
825 u8 *buffer, *src;
826 u8 __iomem *dst;
827 int c, cnt = 0, err = 0;
828 unsigned long total_size;
829
830 if (!info || !info->screen_base)
831 return -ENODEV;
832
833 if (info->state != FBINFO_STATE_RUNNING)
834 return -EPERM;
835
836 if (info->fbops->fb_write)
837 return info->fbops->fb_write(info, buf, count, ppos);
838
839 total_size = info->screen_size;
840
841 if (total_size == 0)
842 total_size = info->fix.smem_len;
843
844 if (p > total_size)
845 return -EFBIG;
846
847 if (count > total_size) {
848 err = -EFBIG;
849 count = total_size;
850 }
851
852 if (count + p > total_size) {
853 if (!err)
854 err = -ENOSPC;
855
856 count = total_size - p;
857 }
858
859 buffer = kmalloc((count > PAGE_SIZE) ? PAGE_SIZE : count,
860 GFP_KERNEL);
861 if (!buffer)
862 return -ENOMEM;
863
864 dst = (u8 __iomem *) (info->screen_base + p);
865
866 if (info->fbops->fb_sync)
867 info->fbops->fb_sync(info);
868
869 while (count) {
870 c = (count > PAGE_SIZE) ? PAGE_SIZE : count;
871 src = buffer;
872
873 if (copy_from_user(src, buf, c)) {
874 err = -EFAULT;
875 break;
876 }
877
878 fb_memcpy_tofb(dst, src, c);
879 dst += c;
880 src += c;
881 *ppos += c;
882 buf += c;
883 cnt += c;
884 count -= c;
885 }
886
887 kfree(buffer);
888
889 return (cnt) ? cnt : err;
890 }
891
892 int
fb_pan_display(struct fb_info * info,struct fb_var_screeninfo * var)893 fb_pan_display(struct fb_info *info, struct fb_var_screeninfo *var)
894 {
895 struct fb_fix_screeninfo *fix = &info->fix;
896 unsigned int yres = info->var.yres;
897 int err = 0;
898
899 if (var->yoffset > 0) {
900 if (var->vmode & FB_VMODE_YWRAP) {
901 if (!fix->ywrapstep || (var->yoffset % fix->ywrapstep))
902 err = -EINVAL;
903 else
904 yres = 0;
905 } else if (!fix->ypanstep || (var->yoffset % fix->ypanstep))
906 err = -EINVAL;
907 }
908
909 if (var->xoffset > 0 && (!fix->xpanstep ||
910 (var->xoffset % fix->xpanstep)))
911 err = -EINVAL;
912
913 if (err || !info->fbops->fb_pan_display ||
914 var->yoffset > info->var.yres_virtual - yres ||
915 var->xoffset > info->var.xres_virtual - info->var.xres)
916 return -EINVAL;
917
918 if ((err = info->fbops->fb_pan_display(var, info)))
919 return err;
920 info->var.xoffset = var->xoffset;
921 info->var.yoffset = var->yoffset;
922 if (var->vmode & FB_VMODE_YWRAP)
923 info->var.vmode |= FB_VMODE_YWRAP;
924 else
925 info->var.vmode &= ~FB_VMODE_YWRAP;
926 return 0;
927 }
928 EXPORT_SYMBOL(fb_pan_display);
929
fb_check_caps(struct fb_info * info,struct fb_var_screeninfo * var,u32 activate)930 static int fb_check_caps(struct fb_info *info, struct fb_var_screeninfo *var,
931 u32 activate)
932 {
933 struct fb_event event;
934 struct fb_blit_caps caps, fbcaps;
935 int err = 0;
936
937 memset(&caps, 0, sizeof(caps));
938 memset(&fbcaps, 0, sizeof(fbcaps));
939 caps.flags = (activate & FB_ACTIVATE_ALL) ? 1 : 0;
940 event.info = info;
941 event.data = ∩︀
942 fb_notifier_call_chain(FB_EVENT_GET_REQ, &event);
943 info->fbops->fb_get_caps(info, &fbcaps, var);
944
945 if (((fbcaps.x ^ caps.x) & caps.x) ||
946 ((fbcaps.y ^ caps.y) & caps.y) ||
947 (fbcaps.len < caps.len))
948 err = -EINVAL;
949
950 return err;
951 }
952
953 int
fb_set_var(struct fb_info * info,struct fb_var_screeninfo * var)954 fb_set_var(struct fb_info *info, struct fb_var_screeninfo *var)
955 {
956 int flags = info->flags;
957 int ret = 0;
958
959 if (var->activate & FB_ACTIVATE_INV_MODE) {
960 struct fb_videomode mode1, mode2;
961
962 fb_var_to_videomode(&mode1, var);
963 fb_var_to_videomode(&mode2, &info->var);
964 /* make sure we don't delete the videomode of current var */
965 ret = fb_mode_is_equal(&mode1, &mode2);
966
967 if (!ret) {
968 struct fb_event event;
969
970 event.info = info;
971 event.data = &mode1;
972 ret = fb_notifier_call_chain(FB_EVENT_MODE_DELETE, &event);
973 }
974
975 if (!ret)
976 fb_delete_videomode(&mode1, &info->modelist);
977
978
979 ret = (ret) ? -EINVAL : 0;
980 goto done;
981 }
982
983 if ((var->activate & FB_ACTIVATE_FORCE) ||
984 memcmp(&info->var, var, sizeof(struct fb_var_screeninfo))) {
985 u32 activate = var->activate;
986
987 /* When using FOURCC mode, make sure the red, green, blue and
988 * transp fields are set to 0.
989 */
990 if ((info->fix.capabilities & FB_CAP_FOURCC) &&
991 var->grayscale > 1) {
992 if (var->red.offset || var->green.offset ||
993 var->blue.offset || var->transp.offset ||
994 var->red.length || var->green.length ||
995 var->blue.length || var->transp.length ||
996 var->red.msb_right || var->green.msb_right ||
997 var->blue.msb_right || var->transp.msb_right)
998 return -EINVAL;
999 }
1000
1001 if (!info->fbops->fb_check_var) {
1002 *var = info->var;
1003 goto done;
1004 }
1005
1006 ret = info->fbops->fb_check_var(var, info);
1007
1008 if (ret)
1009 goto done;
1010
1011 if ((var->activate & FB_ACTIVATE_MASK) == FB_ACTIVATE_NOW) {
1012 struct fb_var_screeninfo old_var;
1013 struct fb_videomode mode;
1014
1015 if (info->fbops->fb_get_caps) {
1016 ret = fb_check_caps(info, var, activate);
1017
1018 if (ret)
1019 goto done;
1020 }
1021
1022 old_var = info->var;
1023 info->var = *var;
1024
1025 if (info->fbops->fb_set_par) {
1026 ret = info->fbops->fb_set_par(info);
1027
1028 if (ret) {
1029 info->var = old_var;
1030 printk(KERN_WARNING "detected "
1031 "fb_set_par error, "
1032 "error code: %d\n", ret);
1033 goto done;
1034 }
1035 }
1036
1037 fb_pan_display(info, &info->var);
1038 fb_set_cmap(&info->cmap, info);
1039 fb_var_to_videomode(&mode, &info->var);
1040
1041 if (info->modelist.prev && info->modelist.next &&
1042 !list_empty(&info->modelist))
1043 ret = fb_add_videomode(&mode, &info->modelist);
1044
1045 if (!ret && (flags & FBINFO_MISC_USEREVENT)) {
1046 struct fb_event event;
1047 int evnt = (activate & FB_ACTIVATE_ALL) ?
1048 FB_EVENT_MODE_CHANGE_ALL :
1049 FB_EVENT_MODE_CHANGE;
1050
1051 info->flags &= ~FBINFO_MISC_USEREVENT;
1052 event.info = info;
1053 event.data = &mode;
1054 fb_notifier_call_chain(evnt, &event);
1055 }
1056 }
1057 }
1058
1059 done:
1060 return ret;
1061 }
1062 EXPORT_SYMBOL(fb_set_var);
1063
1064 int
fb_blank(struct fb_info * info,int blank)1065 fb_blank(struct fb_info *info, int blank)
1066 {
1067 struct fb_event event;
1068 int ret = -EINVAL, early_ret;
1069
1070 if (blank > FB_BLANK_POWERDOWN)
1071 blank = FB_BLANK_POWERDOWN;
1072
1073 event.info = info;
1074 event.data = ␣
1075
1076 early_ret = fb_notifier_call_chain(FB_EARLY_EVENT_BLANK, &event);
1077
1078 if (info->fbops->fb_blank)
1079 ret = info->fbops->fb_blank(blank, info);
1080
1081 if (!ret)
1082 fb_notifier_call_chain(FB_EVENT_BLANK, &event);
1083 else {
1084 /*
1085 * if fb_blank is failed then revert effects of
1086 * the early blank event.
1087 */
1088 if (!early_ret)
1089 fb_notifier_call_chain(FB_R_EARLY_EVENT_BLANK, &event);
1090 }
1091
1092 return ret;
1093 }
1094 EXPORT_SYMBOL(fb_blank);
1095
do_fb_ioctl(struct fb_info * info,unsigned int cmd,unsigned long arg)1096 static long do_fb_ioctl(struct fb_info *info, unsigned int cmd,
1097 unsigned long arg)
1098 {
1099 struct fb_ops *fb;
1100 struct fb_var_screeninfo var;
1101 struct fb_fix_screeninfo fix;
1102 struct fb_con2fbmap con2fb;
1103 struct fb_cmap cmap_from;
1104 struct fb_cmap_user cmap;
1105 struct fb_event event;
1106 void __user *argp = (void __user *)arg;
1107 long ret = 0;
1108
1109 switch (cmd) {
1110 case FBIOGET_VSCREENINFO:
1111 if (!lock_fb_info(info))
1112 return -ENODEV;
1113 var = info->var;
1114 unlock_fb_info(info);
1115
1116 ret = copy_to_user(argp, &var, sizeof(var)) ? -EFAULT : 0;
1117 break;
1118 case FBIOPUT_VSCREENINFO:
1119 if (copy_from_user(&var, argp, sizeof(var)))
1120 return -EFAULT;
1121 console_lock();
1122 if (!lock_fb_info(info)) {
1123 console_unlock();
1124 return -ENODEV;
1125 }
1126 info->flags |= FBINFO_MISC_USEREVENT;
1127 ret = fb_set_var(info, &var);
1128 info->flags &= ~FBINFO_MISC_USEREVENT;
1129 unlock_fb_info(info);
1130 console_unlock();
1131 if (!ret && copy_to_user(argp, &var, sizeof(var)))
1132 ret = -EFAULT;
1133 break;
1134 case FBIOGET_FSCREENINFO:
1135 if (!lock_fb_info(info))
1136 return -ENODEV;
1137 fix = info->fix;
1138 unlock_fb_info(info);
1139
1140 ret = copy_to_user(argp, &fix, sizeof(fix)) ? -EFAULT : 0;
1141 break;
1142 case FBIOPUTCMAP:
1143 if (copy_from_user(&cmap, argp, sizeof(cmap)))
1144 return -EFAULT;
1145 ret = fb_set_user_cmap(&cmap, info);
1146 break;
1147 case FBIOGETCMAP:
1148 if (copy_from_user(&cmap, argp, sizeof(cmap)))
1149 return -EFAULT;
1150 if (!lock_fb_info(info))
1151 return -ENODEV;
1152 cmap_from = info->cmap;
1153 unlock_fb_info(info);
1154 ret = fb_cmap_to_user(&cmap_from, &cmap);
1155 break;
1156 case FBIOPAN_DISPLAY:
1157 if (copy_from_user(&var, argp, sizeof(var)))
1158 return -EFAULT;
1159 console_lock();
1160 if (!lock_fb_info(info)) {
1161 console_unlock();
1162 return -ENODEV;
1163 }
1164 ret = fb_pan_display(info, &var);
1165 unlock_fb_info(info);
1166 console_unlock();
1167 if (ret == 0 && copy_to_user(argp, &var, sizeof(var)))
1168 return -EFAULT;
1169 break;
1170 case FBIO_CURSOR:
1171 ret = -EINVAL;
1172 break;
1173 case FBIOGET_CON2FBMAP:
1174 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1175 return -EFAULT;
1176 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1177 return -EINVAL;
1178 con2fb.framebuffer = -1;
1179 event.data = &con2fb;
1180 if (!lock_fb_info(info))
1181 return -ENODEV;
1182 event.info = info;
1183 fb_notifier_call_chain(FB_EVENT_GET_CONSOLE_MAP, &event);
1184 unlock_fb_info(info);
1185 ret = copy_to_user(argp, &con2fb, sizeof(con2fb)) ? -EFAULT : 0;
1186 break;
1187 case FBIOPUT_CON2FBMAP:
1188 if (copy_from_user(&con2fb, argp, sizeof(con2fb)))
1189 return -EFAULT;
1190 if (con2fb.console < 1 || con2fb.console > MAX_NR_CONSOLES)
1191 return -EINVAL;
1192 if (con2fb.framebuffer >= FB_MAX)
1193 return -EINVAL;
1194 if (!registered_fb[con2fb.framebuffer])
1195 request_module("fb%d", con2fb.framebuffer);
1196 if (!registered_fb[con2fb.framebuffer]) {
1197 ret = -EINVAL;
1198 break;
1199 }
1200 event.data = &con2fb;
1201 console_lock();
1202 if (!lock_fb_info(info)) {
1203 console_unlock();
1204 return -ENODEV;
1205 }
1206 event.info = info;
1207 ret = fb_notifier_call_chain(FB_EVENT_SET_CONSOLE_MAP, &event);
1208 unlock_fb_info(info);
1209 console_unlock();
1210 break;
1211 case FBIOBLANK:
1212 console_lock();
1213 if (!lock_fb_info(info)) {
1214 console_unlock();
1215 return -ENODEV;
1216 }
1217 info->flags |= FBINFO_MISC_USEREVENT;
1218 ret = fb_blank(info, arg);
1219 info->flags &= ~FBINFO_MISC_USEREVENT;
1220 unlock_fb_info(info);
1221 console_unlock();
1222 break;
1223 default:
1224 if (!lock_fb_info(info))
1225 return -ENODEV;
1226 fb = info->fbops;
1227 if (fb->fb_ioctl)
1228 ret = fb->fb_ioctl(info, cmd, arg);
1229 else
1230 ret = -ENOTTY;
1231 unlock_fb_info(info);
1232 }
1233 return ret;
1234 }
1235
fb_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1236 static long fb_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1237 {
1238 struct fb_info *info = file_fb_info(file);
1239
1240 if (!info)
1241 return -ENODEV;
1242 return do_fb_ioctl(info, cmd, arg);
1243 }
1244
1245 #ifdef CONFIG_COMPAT
1246 struct fb_fix_screeninfo32 {
1247 char id[16];
1248 compat_caddr_t smem_start;
1249 u32 smem_len;
1250 u32 type;
1251 u32 type_aux;
1252 u32 visual;
1253 u16 xpanstep;
1254 u16 ypanstep;
1255 u16 ywrapstep;
1256 u32 line_length;
1257 compat_caddr_t mmio_start;
1258 u32 mmio_len;
1259 u32 accel;
1260 u16 reserved[3];
1261 };
1262
1263 struct fb_cmap32 {
1264 u32 start;
1265 u32 len;
1266 compat_caddr_t red;
1267 compat_caddr_t green;
1268 compat_caddr_t blue;
1269 compat_caddr_t transp;
1270 };
1271
fb_getput_cmap(struct fb_info * info,unsigned int cmd,unsigned long arg)1272 static int fb_getput_cmap(struct fb_info *info, unsigned int cmd,
1273 unsigned long arg)
1274 {
1275 struct fb_cmap_user __user *cmap;
1276 struct fb_cmap32 __user *cmap32;
1277 __u32 data;
1278 int err;
1279
1280 cmap = compat_alloc_user_space(sizeof(*cmap));
1281 cmap32 = compat_ptr(arg);
1282
1283 if (copy_in_user(&cmap->start, &cmap32->start, 2 * sizeof(__u32)))
1284 return -EFAULT;
1285
1286 if (get_user(data, &cmap32->red) ||
1287 put_user(compat_ptr(data), &cmap->red) ||
1288 get_user(data, &cmap32->green) ||
1289 put_user(compat_ptr(data), &cmap->green) ||
1290 get_user(data, &cmap32->blue) ||
1291 put_user(compat_ptr(data), &cmap->blue) ||
1292 get_user(data, &cmap32->transp) ||
1293 put_user(compat_ptr(data), &cmap->transp))
1294 return -EFAULT;
1295
1296 err = do_fb_ioctl(info, cmd, (unsigned long) cmap);
1297
1298 if (!err) {
1299 if (copy_in_user(&cmap32->start,
1300 &cmap->start,
1301 2 * sizeof(__u32)))
1302 err = -EFAULT;
1303 }
1304 return err;
1305 }
1306
do_fscreeninfo_to_user(struct fb_fix_screeninfo * fix,struct fb_fix_screeninfo32 __user * fix32)1307 static int do_fscreeninfo_to_user(struct fb_fix_screeninfo *fix,
1308 struct fb_fix_screeninfo32 __user *fix32)
1309 {
1310 __u32 data;
1311 int err;
1312
1313 err = copy_to_user(&fix32->id, &fix->id, sizeof(fix32->id));
1314
1315 data = (__u32) (unsigned long) fix->smem_start;
1316 err |= put_user(data, &fix32->smem_start);
1317
1318 err |= put_user(fix->smem_len, &fix32->smem_len);
1319 err |= put_user(fix->type, &fix32->type);
1320 err |= put_user(fix->type_aux, &fix32->type_aux);
1321 err |= put_user(fix->visual, &fix32->visual);
1322 err |= put_user(fix->xpanstep, &fix32->xpanstep);
1323 err |= put_user(fix->ypanstep, &fix32->ypanstep);
1324 err |= put_user(fix->ywrapstep, &fix32->ywrapstep);
1325 err |= put_user(fix->line_length, &fix32->line_length);
1326
1327 data = (__u32) (unsigned long) fix->mmio_start;
1328 err |= put_user(data, &fix32->mmio_start);
1329
1330 err |= put_user(fix->mmio_len, &fix32->mmio_len);
1331 err |= put_user(fix->accel, &fix32->accel);
1332 err |= copy_to_user(fix32->reserved, fix->reserved,
1333 sizeof(fix->reserved));
1334
1335 if (err)
1336 return -EFAULT;
1337 return 0;
1338 }
1339
fb_get_fscreeninfo(struct fb_info * info,unsigned int cmd,unsigned long arg)1340 static int fb_get_fscreeninfo(struct fb_info *info, unsigned int cmd,
1341 unsigned long arg)
1342 {
1343 struct fb_fix_screeninfo fix;
1344
1345 if (!lock_fb_info(info))
1346 return -ENODEV;
1347 fix = info->fix;
1348 unlock_fb_info(info);
1349 return do_fscreeninfo_to_user(&fix, compat_ptr(arg));
1350 }
1351
fb_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1352 static long fb_compat_ioctl(struct file *file, unsigned int cmd,
1353 unsigned long arg)
1354 {
1355 struct fb_info *info = file_fb_info(file);
1356 struct fb_ops *fb;
1357 long ret = -ENOIOCTLCMD;
1358
1359 if (!info)
1360 return -ENODEV;
1361 fb = info->fbops;
1362 switch(cmd) {
1363 case FBIOGET_VSCREENINFO:
1364 case FBIOPUT_VSCREENINFO:
1365 case FBIOPAN_DISPLAY:
1366 case FBIOGET_CON2FBMAP:
1367 case FBIOPUT_CON2FBMAP:
1368 arg = (unsigned long) compat_ptr(arg);
1369 case FBIOBLANK:
1370 ret = do_fb_ioctl(info, cmd, arg);
1371 break;
1372
1373 case FBIOGET_FSCREENINFO:
1374 ret = fb_get_fscreeninfo(info, cmd, arg);
1375 break;
1376
1377 case FBIOGETCMAP:
1378 case FBIOPUTCMAP:
1379 ret = fb_getput_cmap(info, cmd, arg);
1380 break;
1381
1382 default:
1383 if (fb->fb_compat_ioctl)
1384 ret = fb->fb_compat_ioctl(info, cmd, arg);
1385 break;
1386 }
1387 return ret;
1388 }
1389 #endif
1390
1391 static int
fb_mmap(struct file * file,struct vm_area_struct * vma)1392 fb_mmap(struct file *file, struct vm_area_struct * vma)
1393 {
1394 struct fb_info *info = file_fb_info(file);
1395 struct fb_ops *fb;
1396 unsigned long mmio_pgoff;
1397 unsigned long start;
1398 u32 len;
1399
1400 if (!info)
1401 return -ENODEV;
1402 fb = info->fbops;
1403 if (!fb)
1404 return -ENODEV;
1405 mutex_lock(&info->mm_lock);
1406 if (fb->fb_mmap) {
1407 int res;
1408
1409 /*
1410 * The framebuffer needs to be accessed decrypted, be sure
1411 * SME protection is removed ahead of the call
1412 */
1413 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1414 res = fb->fb_mmap(info, vma);
1415 mutex_unlock(&info->mm_lock);
1416 return res;
1417 }
1418
1419 /*
1420 * Ugh. This can be either the frame buffer mapping, or
1421 * if pgoff points past it, the mmio mapping.
1422 */
1423 start = info->fix.smem_start;
1424 len = info->fix.smem_len;
1425 mmio_pgoff = PAGE_ALIGN((start & ~PAGE_MASK) + len) >> PAGE_SHIFT;
1426 if (vma->vm_pgoff >= mmio_pgoff) {
1427 if (info->var.accel_flags) {
1428 mutex_unlock(&info->mm_lock);
1429 return -EINVAL;
1430 }
1431
1432 vma->vm_pgoff -= mmio_pgoff;
1433 start = info->fix.mmio_start;
1434 len = info->fix.mmio_len;
1435 }
1436 mutex_unlock(&info->mm_lock);
1437
1438 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
1439 /*
1440 * The framebuffer needs to be accessed decrypted, be sure
1441 * SME protection is removed
1442 */
1443 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
1444 fb_pgprotect(file, vma, start);
1445
1446 return vm_iomap_memory(vma, start, len);
1447 }
1448
1449 static int
fb_open(struct inode * inode,struct file * file)1450 fb_open(struct inode *inode, struct file *file)
1451 __acquires(&info->lock)
1452 __releases(&info->lock)
1453 {
1454 int fbidx = iminor(inode);
1455 struct fb_info *info;
1456 int res = 0;
1457
1458 info = get_fb_info(fbidx);
1459 if (!info) {
1460 request_module("fb%d", fbidx);
1461 info = get_fb_info(fbidx);
1462 if (!info)
1463 return -ENODEV;
1464 }
1465 if (IS_ERR(info))
1466 return PTR_ERR(info);
1467
1468 mutex_lock(&info->lock);
1469 if (!try_module_get(info->fbops->owner)) {
1470 res = -ENODEV;
1471 goto out;
1472 }
1473 file->private_data = info;
1474 if (info->fbops->fb_open) {
1475 res = info->fbops->fb_open(info,1);
1476 if (res)
1477 module_put(info->fbops->owner);
1478 }
1479 #ifdef CONFIG_FB_DEFERRED_IO
1480 if (info->fbdefio)
1481 fb_deferred_io_open(info, inode, file);
1482 #endif
1483 out:
1484 mutex_unlock(&info->lock);
1485 if (res)
1486 put_fb_info(info);
1487 return res;
1488 }
1489
1490 static int
fb_release(struct inode * inode,struct file * file)1491 fb_release(struct inode *inode, struct file *file)
1492 __acquires(&info->lock)
1493 __releases(&info->lock)
1494 {
1495 struct fb_info * const info = file->private_data;
1496
1497 mutex_lock(&info->lock);
1498 if (info->fbops->fb_release)
1499 info->fbops->fb_release(info,1);
1500 module_put(info->fbops->owner);
1501 mutex_unlock(&info->lock);
1502 put_fb_info(info);
1503 return 0;
1504 }
1505
1506 #if defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && !defined(CONFIG_MMU)
get_fb_unmapped_area(struct file * filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags)1507 unsigned long get_fb_unmapped_area(struct file *filp,
1508 unsigned long addr, unsigned long len,
1509 unsigned long pgoff, unsigned long flags)
1510 {
1511 struct fb_info * const info = filp->private_data;
1512 unsigned long fb_size = PAGE_ALIGN(info->fix.smem_len);
1513
1514 if (pgoff > fb_size || len > fb_size - pgoff)
1515 return -EINVAL;
1516
1517 return (unsigned long)info->screen_base + pgoff;
1518 }
1519 #endif
1520
1521 static const struct file_operations fb_fops = {
1522 .owner = THIS_MODULE,
1523 .read = fb_read,
1524 .write = fb_write,
1525 .unlocked_ioctl = fb_ioctl,
1526 #ifdef CONFIG_COMPAT
1527 .compat_ioctl = fb_compat_ioctl,
1528 #endif
1529 .mmap = fb_mmap,
1530 .open = fb_open,
1531 .release = fb_release,
1532 #if defined(HAVE_ARCH_FB_UNMAPPED_AREA) || \
1533 (defined(CONFIG_FB_PROVIDE_GET_FB_UNMAPPED_AREA) && \
1534 !defined(CONFIG_MMU))
1535 .get_unmapped_area = get_fb_unmapped_area,
1536 #endif
1537 #ifdef CONFIG_FB_DEFERRED_IO
1538 .fsync = fb_deferred_io_fsync,
1539 #endif
1540 .llseek = default_llseek,
1541 };
1542
1543 struct class *fb_class;
1544 EXPORT_SYMBOL(fb_class);
1545
fb_check_foreignness(struct fb_info * fi)1546 static int fb_check_foreignness(struct fb_info *fi)
1547 {
1548 const bool foreign_endian = fi->flags & FBINFO_FOREIGN_ENDIAN;
1549
1550 fi->flags &= ~FBINFO_FOREIGN_ENDIAN;
1551
1552 #ifdef __BIG_ENDIAN
1553 fi->flags |= foreign_endian ? 0 : FBINFO_BE_MATH;
1554 #else
1555 fi->flags |= foreign_endian ? FBINFO_BE_MATH : 0;
1556 #endif /* __BIG_ENDIAN */
1557
1558 if (fi->flags & FBINFO_BE_MATH && !fb_be_math(fi)) {
1559 pr_err("%s: enable CONFIG_FB_BIG_ENDIAN to "
1560 "support this framebuffer\n", fi->fix.id);
1561 return -ENOSYS;
1562 } else if (!(fi->flags & FBINFO_BE_MATH) && fb_be_math(fi)) {
1563 pr_err("%s: enable CONFIG_FB_LITTLE_ENDIAN to "
1564 "support this framebuffer\n", fi->fix.id);
1565 return -ENOSYS;
1566 }
1567
1568 return 0;
1569 }
1570
apertures_overlap(struct aperture * gen,struct aperture * hw)1571 static bool apertures_overlap(struct aperture *gen, struct aperture *hw)
1572 {
1573 /* is the generic aperture base the same as the HW one */
1574 if (gen->base == hw->base)
1575 return true;
1576 /* is the generic aperture base inside the hw base->hw base+size */
1577 if (gen->base > hw->base && gen->base < hw->base + hw->size)
1578 return true;
1579 return false;
1580 }
1581
fb_do_apertures_overlap(struct apertures_struct * gena,struct apertures_struct * hwa)1582 static bool fb_do_apertures_overlap(struct apertures_struct *gena,
1583 struct apertures_struct *hwa)
1584 {
1585 int i, j;
1586 if (!hwa || !gena)
1587 return false;
1588
1589 for (i = 0; i < hwa->count; ++i) {
1590 struct aperture *h = &hwa->ranges[i];
1591 for (j = 0; j < gena->count; ++j) {
1592 struct aperture *g = &gena->ranges[j];
1593 printk(KERN_DEBUG "checking generic (%llx %llx) vs hw (%llx %llx)\n",
1594 (unsigned long long)g->base,
1595 (unsigned long long)g->size,
1596 (unsigned long long)h->base,
1597 (unsigned long long)h->size);
1598 if (apertures_overlap(g, h))
1599 return true;
1600 }
1601 }
1602
1603 return false;
1604 }
1605
1606 static int do_unregister_framebuffer(struct fb_info *fb_info);
1607
1608 #define VGA_FB_PHYS 0xA0000
do_remove_conflicting_framebuffers(struct apertures_struct * a,const char * name,bool primary)1609 static int do_remove_conflicting_framebuffers(struct apertures_struct *a,
1610 const char *name, bool primary)
1611 {
1612 int i, ret;
1613
1614 /* check all firmware fbs and kick off if the base addr overlaps */
1615 for (i = 0 ; i < FB_MAX; i++) {
1616 struct apertures_struct *gen_aper;
1617 if (!registered_fb[i])
1618 continue;
1619
1620 if (!(registered_fb[i]->flags & FBINFO_MISC_FIRMWARE))
1621 continue;
1622
1623 gen_aper = registered_fb[i]->apertures;
1624 if (fb_do_apertures_overlap(gen_aper, a) ||
1625 (primary && gen_aper && gen_aper->count &&
1626 gen_aper->ranges[0].base == VGA_FB_PHYS)) {
1627
1628 printk(KERN_INFO "fb: switching to %s from %s\n",
1629 name, registered_fb[i]->fix.id);
1630 ret = do_unregister_framebuffer(registered_fb[i]);
1631 if (ret)
1632 return ret;
1633 }
1634 }
1635
1636 return 0;
1637 }
1638
1639 static bool lockless_register_fb;
1640 module_param_named_unsafe(lockless_register_fb, lockless_register_fb, bool, 0400);
1641 MODULE_PARM_DESC(lockless_register_fb,
1642 "Lockless framebuffer registration for debugging [default=off]");
1643
do_register_framebuffer(struct fb_info * fb_info)1644 static int do_register_framebuffer(struct fb_info *fb_info)
1645 {
1646 int i, ret;
1647 struct fb_event event;
1648 struct fb_videomode mode;
1649
1650 if (fb_check_foreignness(fb_info))
1651 return -ENOSYS;
1652
1653 ret = do_remove_conflicting_framebuffers(fb_info->apertures,
1654 fb_info->fix.id,
1655 fb_is_primary_device(fb_info));
1656 if (ret)
1657 return ret;
1658
1659 if (num_registered_fb == FB_MAX)
1660 return -ENXIO;
1661
1662 num_registered_fb++;
1663 for (i = 0 ; i < FB_MAX; i++)
1664 if (!registered_fb[i])
1665 break;
1666 fb_info->node = i;
1667 atomic_set(&fb_info->count, 1);
1668 mutex_init(&fb_info->lock);
1669 mutex_init(&fb_info->mm_lock);
1670
1671 fb_info->dev = device_create(fb_class, fb_info->device,
1672 MKDEV(FB_MAJOR, i), NULL, "fb%d", i);
1673 if (IS_ERR(fb_info->dev)) {
1674 /* Not fatal */
1675 printk(KERN_WARNING "Unable to create device for framebuffer %d; errno = %ld\n", i, PTR_ERR(fb_info->dev));
1676 fb_info->dev = NULL;
1677 } else
1678 fb_init_device(fb_info);
1679
1680 if (fb_info->pixmap.addr == NULL) {
1681 fb_info->pixmap.addr = kmalloc(FBPIXMAPSIZE, GFP_KERNEL);
1682 if (fb_info->pixmap.addr) {
1683 fb_info->pixmap.size = FBPIXMAPSIZE;
1684 fb_info->pixmap.buf_align = 1;
1685 fb_info->pixmap.scan_align = 1;
1686 fb_info->pixmap.access_align = 32;
1687 fb_info->pixmap.flags = FB_PIXMAP_DEFAULT;
1688 }
1689 }
1690 fb_info->pixmap.offset = 0;
1691
1692 if (!fb_info->pixmap.blit_x)
1693 fb_info->pixmap.blit_x = ~(u32)0;
1694
1695 if (!fb_info->pixmap.blit_y)
1696 fb_info->pixmap.blit_y = ~(u32)0;
1697
1698 if (!fb_info->modelist.prev || !fb_info->modelist.next)
1699 INIT_LIST_HEAD(&fb_info->modelist);
1700
1701 if (fb_info->skip_vt_switch)
1702 pm_vt_switch_required(fb_info->dev, false);
1703 else
1704 pm_vt_switch_required(fb_info->dev, true);
1705
1706 fb_var_to_videomode(&mode, &fb_info->var);
1707 fb_add_videomode(&mode, &fb_info->modelist);
1708 registered_fb[i] = fb_info;
1709
1710 event.info = fb_info;
1711 if (!lockless_register_fb)
1712 console_lock();
1713 if (!lock_fb_info(fb_info)) {
1714 if (!lockless_register_fb)
1715 console_unlock();
1716 return -ENODEV;
1717 }
1718
1719 fb_notifier_call_chain(FB_EVENT_FB_REGISTERED, &event);
1720 unlock_fb_info(fb_info);
1721 if (!lockless_register_fb)
1722 console_unlock();
1723 return 0;
1724 }
1725
unbind_console(struct fb_info * fb_info)1726 static int unbind_console(struct fb_info *fb_info)
1727 {
1728 struct fb_event event;
1729 int ret;
1730 int i = fb_info->node;
1731
1732 if (i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)
1733 return -EINVAL;
1734
1735 console_lock();
1736 if (!lock_fb_info(fb_info)) {
1737 console_unlock();
1738 return -ENODEV;
1739 }
1740
1741 event.info = fb_info;
1742 ret = fb_notifier_call_chain(FB_EVENT_FB_UNBIND, &event);
1743 unlock_fb_info(fb_info);
1744 console_unlock();
1745
1746 return ret;
1747 }
1748
1749 static int __unlink_framebuffer(struct fb_info *fb_info);
1750
do_unregister_framebuffer(struct fb_info * fb_info)1751 static int do_unregister_framebuffer(struct fb_info *fb_info)
1752 {
1753 struct fb_event event;
1754 int ret;
1755
1756 ret = unbind_console(fb_info);
1757
1758 if (ret)
1759 return -EINVAL;
1760
1761 pm_vt_switch_unregister(fb_info->dev);
1762
1763 __unlink_framebuffer(fb_info);
1764 if (fb_info->pixmap.addr &&
1765 (fb_info->pixmap.flags & FB_PIXMAP_DEFAULT))
1766 kfree(fb_info->pixmap.addr);
1767 fb_destroy_modelist(&fb_info->modelist);
1768 registered_fb[fb_info->node] = NULL;
1769 num_registered_fb--;
1770 fb_cleanup_device(fb_info);
1771 event.info = fb_info;
1772 console_lock();
1773 fb_notifier_call_chain(FB_EVENT_FB_UNREGISTERED, &event);
1774 console_unlock();
1775
1776 /* this may free fb info */
1777 put_fb_info(fb_info);
1778 return 0;
1779 }
1780
__unlink_framebuffer(struct fb_info * fb_info)1781 static int __unlink_framebuffer(struct fb_info *fb_info)
1782 {
1783 int i;
1784
1785 i = fb_info->node;
1786 if (i < 0 || i >= FB_MAX || registered_fb[i] != fb_info)
1787 return -EINVAL;
1788
1789 if (fb_info->dev) {
1790 device_destroy(fb_class, MKDEV(FB_MAJOR, i));
1791 fb_info->dev = NULL;
1792 }
1793
1794 return 0;
1795 }
1796
unlink_framebuffer(struct fb_info * fb_info)1797 int unlink_framebuffer(struct fb_info *fb_info)
1798 {
1799 int ret;
1800
1801 ret = __unlink_framebuffer(fb_info);
1802 if (ret)
1803 return ret;
1804
1805 unbind_console(fb_info);
1806
1807 return 0;
1808 }
1809 EXPORT_SYMBOL(unlink_framebuffer);
1810
remove_conflicting_framebuffers(struct apertures_struct * a,const char * name,bool primary)1811 int remove_conflicting_framebuffers(struct apertures_struct *a,
1812 const char *name, bool primary)
1813 {
1814 int ret;
1815
1816 mutex_lock(®istration_lock);
1817 ret = do_remove_conflicting_framebuffers(a, name, primary);
1818 mutex_unlock(®istration_lock);
1819
1820 return ret;
1821 }
1822 EXPORT_SYMBOL(remove_conflicting_framebuffers);
1823
1824 /**
1825 * register_framebuffer - registers a frame buffer device
1826 * @fb_info: frame buffer info structure
1827 *
1828 * Registers a frame buffer device @fb_info.
1829 *
1830 * Returns negative errno on error, or zero for success.
1831 *
1832 */
1833 int
register_framebuffer(struct fb_info * fb_info)1834 register_framebuffer(struct fb_info *fb_info)
1835 {
1836 int ret;
1837
1838 mutex_lock(®istration_lock);
1839 ret = do_register_framebuffer(fb_info);
1840 mutex_unlock(®istration_lock);
1841
1842 return ret;
1843 }
1844 EXPORT_SYMBOL(register_framebuffer);
1845
1846 /**
1847 * unregister_framebuffer - releases a frame buffer device
1848 * @fb_info: frame buffer info structure
1849 *
1850 * Unregisters a frame buffer device @fb_info.
1851 *
1852 * Returns negative errno on error, or zero for success.
1853 *
1854 * This function will also notify the framebuffer console
1855 * to release the driver.
1856 *
1857 * This is meant to be called within a driver's module_exit()
1858 * function. If this is called outside module_exit(), ensure
1859 * that the driver implements fb_open() and fb_release() to
1860 * check that no processes are using the device.
1861 */
1862 int
unregister_framebuffer(struct fb_info * fb_info)1863 unregister_framebuffer(struct fb_info *fb_info)
1864 {
1865 int ret;
1866
1867 mutex_lock(®istration_lock);
1868 ret = do_unregister_framebuffer(fb_info);
1869 mutex_unlock(®istration_lock);
1870
1871 return ret;
1872 }
1873 EXPORT_SYMBOL(unregister_framebuffer);
1874
1875 /**
1876 * fb_set_suspend - low level driver signals suspend
1877 * @info: framebuffer affected
1878 * @state: 0 = resuming, !=0 = suspending
1879 *
1880 * This is meant to be used by low level drivers to
1881 * signal suspend/resume to the core & clients.
1882 * It must be called with the console semaphore held
1883 */
fb_set_suspend(struct fb_info * info,int state)1884 void fb_set_suspend(struct fb_info *info, int state)
1885 {
1886 struct fb_event event;
1887
1888 event.info = info;
1889 if (state) {
1890 fb_notifier_call_chain(FB_EVENT_SUSPEND, &event);
1891 info->state = FBINFO_STATE_SUSPENDED;
1892 } else {
1893 info->state = FBINFO_STATE_RUNNING;
1894 fb_notifier_call_chain(FB_EVENT_RESUME, &event);
1895 }
1896 }
1897 EXPORT_SYMBOL(fb_set_suspend);
1898
1899 /**
1900 * fbmem_init - init frame buffer subsystem
1901 *
1902 * Initialize the frame buffer subsystem.
1903 *
1904 * NOTE: This function is _only_ to be called by drivers/char/mem.c.
1905 *
1906 */
1907
1908 static int __init
fbmem_init(void)1909 fbmem_init(void)
1910 {
1911 int ret;
1912
1913 if (!proc_create("fb", 0, NULL, &fb_proc_fops))
1914 return -ENOMEM;
1915
1916 ret = register_chrdev(FB_MAJOR, "fb", &fb_fops);
1917 if (ret) {
1918 printk("unable to get major %d for fb devs\n", FB_MAJOR);
1919 goto err_chrdev;
1920 }
1921
1922 fb_class = class_create(THIS_MODULE, "graphics");
1923 if (IS_ERR(fb_class)) {
1924 ret = PTR_ERR(fb_class);
1925 pr_warn("Unable to create fb class; errno = %d\n", ret);
1926 fb_class = NULL;
1927 goto err_class;
1928 }
1929
1930 fb_console_init();
1931
1932 return 0;
1933
1934 err_class:
1935 unregister_chrdev(FB_MAJOR, "fb");
1936 err_chrdev:
1937 remove_proc_entry("fb", NULL);
1938 return ret;
1939 }
1940
1941 #ifdef MODULE
1942 module_init(fbmem_init);
1943 static void __exit
fbmem_exit(void)1944 fbmem_exit(void)
1945 {
1946 fb_console_exit();
1947
1948 remove_proc_entry("fb", NULL);
1949 class_destroy(fb_class);
1950 unregister_chrdev(FB_MAJOR, "fb");
1951 }
1952
1953 module_exit(fbmem_exit);
1954 MODULE_LICENSE("GPL");
1955 MODULE_DESCRIPTION("Framebuffer base");
1956 #else
1957 subsys_initcall(fbmem_init);
1958 #endif
1959
fb_new_modelist(struct fb_info * info)1960 int fb_new_modelist(struct fb_info *info)
1961 {
1962 struct fb_event event;
1963 struct fb_var_screeninfo var = info->var;
1964 struct list_head *pos, *n;
1965 struct fb_modelist *modelist;
1966 struct fb_videomode *m, mode;
1967 int err = 1;
1968
1969 list_for_each_safe(pos, n, &info->modelist) {
1970 modelist = list_entry(pos, struct fb_modelist, list);
1971 m = &modelist->mode;
1972 fb_videomode_to_var(&var, m);
1973 var.activate = FB_ACTIVATE_TEST;
1974 err = fb_set_var(info, &var);
1975 fb_var_to_videomode(&mode, &var);
1976 if (err || !fb_mode_is_equal(m, &mode)) {
1977 list_del(pos);
1978 kfree(pos);
1979 }
1980 }
1981
1982 err = 1;
1983
1984 if (!list_empty(&info->modelist)) {
1985 event.info = info;
1986 err = fb_notifier_call_chain(FB_EVENT_NEW_MODELIST, &event);
1987 }
1988
1989 return err;
1990 }
1991
1992 MODULE_LICENSE("GPL");
1993