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