1 /*
2  *  linux/drivers/video/modedb.c -- Standard video mode database management
3  *
4  *	Copyright (C) 1999 Geert Uytterhoeven
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 for
11  *  more details.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/fb.h>
17 #include <linux/console_struct.h>
18 #include <linux/sched.h>
19 #include <video/fbcon.h>
20 
21 
22 #undef DEBUG
23 
24 #define name_matches(v, s, l) \
25     ((v).name && !strncmp((s), (v).name, (l)) && strlen((v).name) == (l))
26 #define res_matches(v, x, y) \
27     ((v).xres == (x) && (v).yres == (y))
28 
29 #ifdef DEBUG
30 #define DPRINTK(fmt, args...)	printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
31 #else
32 #define DPRINTK(fmt, args...)
33 #endif
34 
35 
36 const char *global_mode_option = NULL;
37 
38 
39     /*
40      *  Standard video mode definitions (taken from XFree86)
41      */
42 
43 #define DEFAULT_MODEDB_INDEX	0
44 
45 static struct fb_videomode modedb[] __initdata = {
46     {
47 	/* 640x400 @ 70 Hz, 31.5 kHz hsync */
48 	NULL, 70, 640, 400, 39721, 40, 24, 39, 9, 96, 2,
49 	0, FB_VMODE_NONINTERLACED
50     }, {
51 	/* 640x480 @ 60 Hz, 31.5 kHz hsync */
52 	NULL, 60, 640, 480, 39721, 40, 24, 32, 11, 96, 2,
53 	0, FB_VMODE_NONINTERLACED
54     }, {
55 	/* 800x600 @ 56 Hz, 35.15 kHz hsync */
56 	NULL, 56, 800, 600, 27777, 128, 24, 22, 1, 72, 2,
57 	0, FB_VMODE_NONINTERLACED
58     }, {
59 	/* 1024x768 @ 87 Hz interlaced, 35.5 kHz hsync */
60 	NULL, 87, 1024, 768, 22271, 56, 24, 33, 8, 160, 8,
61 	0, FB_VMODE_INTERLACED
62     }, {
63 	/* 640x400 @ 85 Hz, 37.86 kHz hsync */
64 	NULL, 85, 640, 400, 31746, 96, 32, 41, 1, 64, 3,
65 	FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
66     }, {
67 	/* 640x480 @ 72 Hz, 36.5 kHz hsync */
68 	NULL, 72, 640, 480, 31746, 144, 40, 30, 8, 40, 3,
69 	0, FB_VMODE_NONINTERLACED
70     }, {
71 	/* 640x480 @ 75 Hz, 37.50 kHz hsync */
72 	NULL, 75, 640, 480, 31746, 120, 16, 16, 1, 64, 3,
73 	0, FB_VMODE_NONINTERLACED
74     }, {
75 	/* 800x600 @ 60 Hz, 37.8 kHz hsync */
76 	NULL, 60, 800, 600, 25000, 88, 40, 23, 1, 128, 4,
77 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
78     }, {
79 	/* 640x480 @ 85 Hz, 43.27 kHz hsync */
80 	NULL, 85, 640, 480, 27777, 80, 56, 25, 1, 56, 3,
81 	0, FB_VMODE_NONINTERLACED
82     }, {
83 	/* 1152x864 @ 89 Hz interlaced, 44 kHz hsync */
84 	NULL, 69, 1152, 864, 15384, 96, 16, 110, 1, 216, 10,
85 	0, FB_VMODE_INTERLACED
86     }, {
87 	/* 800x600 @ 72 Hz, 48.0 kHz hsync */
88 	NULL, 72, 800, 600, 20000, 64, 56, 23, 37, 120, 6,
89 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
90     }, {
91 	/* 1024x768 @ 60 Hz, 48.4 kHz hsync */
92 	NULL, 60, 1024, 768, 15384, 168, 8, 29, 3, 144, 6,
93 	0, FB_VMODE_NONINTERLACED
94     }, {
95 	/* 640x480 @ 100 Hz, 53.01 kHz hsync */
96 	NULL, 100, 640, 480, 21834, 96, 32, 36, 8, 96, 6,
97 	0, FB_VMODE_NONINTERLACED
98     }, {
99 	/* 1152x864 @ 60 Hz, 53.5 kHz hsync */
100 	NULL, 60, 1152, 864, 11123, 208, 64, 16, 4, 256, 8,
101 	0, FB_VMODE_NONINTERLACED
102     }, {
103 	/* 800x600 @ 85 Hz, 55.84 kHz hsync */
104 	NULL, 85, 800, 600, 16460, 160, 64, 36, 16, 64, 5,
105 	0, FB_VMODE_NONINTERLACED
106     }, {
107 	/* 1024x768 @ 70 Hz, 56.5 kHz hsync */
108 	NULL, 70, 1024, 768, 13333, 144, 24, 29, 3, 136, 6,
109 	0, FB_VMODE_NONINTERLACED
110     }, {
111 	/* 1280x1024 @ 87 Hz interlaced, 51 kHz hsync */
112 	NULL, 87, 1280, 1024, 12500, 56, 16, 128, 1, 216, 12,
113 	0, FB_VMODE_INTERLACED
114     }, {
115 	/* 800x600 @ 100 Hz, 64.02 kHz hsync */
116 	NULL, 100, 800, 600, 14357, 160, 64, 30, 4, 64, 6,
117 	0, FB_VMODE_NONINTERLACED
118     }, {
119 	/* 1024x768 @ 76 Hz, 62.5 kHz hsync */
120 	NULL, 76, 1024, 768, 11764, 208, 8, 36, 16, 120, 3,
121 	0, FB_VMODE_NONINTERLACED
122     }, {
123 	/* 1152x864 @ 70 Hz, 62.4 kHz hsync */
124 	NULL, 70, 1152, 864, 10869, 106, 56, 20, 1, 160, 10,
125 	0, FB_VMODE_NONINTERLACED
126     }, {
127 	/* 1280x1024 @ 61 Hz, 64.2 kHz hsync */
128 	NULL, 61, 1280, 1024, 9090, 200, 48, 26, 1, 184, 3,
129 	0, FB_VMODE_NONINTERLACED
130     }, {
131 	/* 1024x768 @ 85 Hz, 70.24 kHz hsync */
132 	NULL, 85, 1024, 768, 10111, 192, 32, 34, 14, 160, 6,
133 	0, FB_VMODE_NONINTERLACED
134     }, {
135 	/* 1152x864 @ 78 Hz, 70.8 kHz hsync */
136 	NULL, 78, 1152, 864, 9090, 228, 88, 32, 0, 84, 12,
137 	0, FB_VMODE_NONINTERLACED
138     }, {
139 	/* 1280x1024 @ 70 Hz, 74.59 kHz hsync */
140 	NULL, 70, 1280, 1024, 7905, 224, 32, 28, 8, 160, 8,
141 	0, FB_VMODE_NONINTERLACED
142     }, {
143 	/* 1600x1200 @ 60Hz, 75.00 kHz hsync */
144 	NULL, 60, 1600, 1200, 6172, 304, 64, 46, 1, 192, 3,
145 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
146     }, {
147 	/* 1152x864 @ 84 Hz, 76.0 kHz hsync */
148 	NULL, 84, 1152, 864, 7407, 184, 312, 32, 0, 128, 12,
149 	0, FB_VMODE_NONINTERLACED
150     }, {
151 	/* 1280x1024 @ 74 Hz, 78.85 kHz hsync */
152 	NULL, 74, 1280, 1024, 7407, 256, 32, 34, 3, 144, 3,
153 	0, FB_VMODE_NONINTERLACED
154     }, {
155 	/* 1024x768 @ 100Hz, 80.21 kHz hsync */
156 	NULL, 100, 1024, 768, 8658, 192, 32, 21, 3, 192, 10,
157 	0, FB_VMODE_NONINTERLACED
158     }, {
159 	/* 1280x1024 @ 76 Hz, 81.13 kHz hsync */
160 	NULL, 76, 1280, 1024, 7407, 248, 32, 34, 3, 104, 3,
161 	0, FB_VMODE_NONINTERLACED
162     }, {
163 	/* 1600x1200 @ 70 Hz, 87.50 kHz hsync */
164 	NULL, 70, 1600, 1200, 5291, 304, 64, 46, 1, 192, 3,
165 	0, FB_VMODE_NONINTERLACED
166     }, {
167 	/* 1152x864 @ 100 Hz, 89.62 kHz hsync */
168 	NULL, 100, 1152, 864, 7264, 224, 32, 17, 2, 128, 19,
169 	0, FB_VMODE_NONINTERLACED
170     }, {
171 	/* 1280x1024 @ 85 Hz, 91.15 kHz hsync */
172 	NULL, 85, 1280, 1024, 6349, 224, 64, 44, 1, 160, 3,
173 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
174     }, {
175 	/* 1600x1200 @ 75 Hz, 93.75 kHz hsync */
176 	NULL, 75, 1600, 1200, 4938, 304, 64, 46, 1, 192, 3,
177 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
178     }, {
179 	/* 1600x1200 @ 85 Hz, 105.77 kHz hsync */
180 	NULL, 85, 1600, 1200, 4545, 272, 16, 37, 4, 192, 3,
181 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
182     }, {
183 	/* 1280x1024 @ 100 Hz, 107.16 kHz hsync */
184 	NULL, 100, 1280, 1024, 5502, 256, 32, 26, 7, 128, 15,
185 	0, FB_VMODE_NONINTERLACED
186     }, {
187 	/* 1800x1440 @ 64Hz, 96.15 kHz hsync  */
188 	NULL, 64, 1800, 1440, 4347, 304, 96, 46, 1, 192, 3,
189 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
190     }, {
191 	/* 1800x1440 @ 70Hz, 104.52 kHz hsync  */
192 	NULL, 70, 1800, 1440, 4000, 304, 96, 46, 1, 192, 3,
193 	FB_SYNC_HOR_HIGH_ACT|FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED
194     }, {
195 	/* 512x384 @ 78 Hz, 31.50 kHz hsync */
196 	NULL, 78, 512, 384, 49603, 48, 16, 16, 1, 64, 3,
197 	0, FB_VMODE_NONINTERLACED
198     }, {
199 	/* 512x384 @ 85 Hz, 34.38 kHz hsync */
200 	NULL, 85, 512, 384, 45454, 48, 16, 16, 1, 64, 3,
201 	0, FB_VMODE_NONINTERLACED
202     }, {
203 	/* 320x200 @ 70 Hz, 31.5 kHz hsync, 8:5 aspect ratio */
204 	NULL, 70, 320, 200, 79440, 16, 16, 20, 4, 48, 1,
205 	0, FB_VMODE_DOUBLE
206     }, {
207 	/* 320x240 @ 60 Hz, 31.5 kHz hsync, 4:3 aspect ratio */
208 	NULL, 60, 320, 240, 79440, 16, 16, 16, 5, 48, 1,
209 	0, FB_VMODE_DOUBLE
210     }, {
211 	/* 320x240 @ 72 Hz, 36.5 kHz hsync */
212 	NULL, 72, 320, 240, 63492, 16, 16, 16, 4, 48, 2,
213 	0, FB_VMODE_DOUBLE
214     }, {
215 	/* 400x300 @ 56 Hz, 35.2 kHz hsync, 4:3 aspect ratio */
216 	NULL, 56, 400, 300, 55555, 64, 16, 10, 1, 32, 1,
217 	0, FB_VMODE_DOUBLE
218     }, {
219 	/* 400x300 @ 60 Hz, 37.8 kHz hsync */
220 	NULL, 60, 400, 300, 50000, 48, 16, 11, 1, 64, 2,
221 	0, FB_VMODE_DOUBLE
222     }, {
223 	/* 400x300 @ 72 Hz, 48.0 kHz hsync */
224 	NULL, 72, 400, 300, 40000, 32, 24, 11, 19, 64, 3,
225 	0, FB_VMODE_DOUBLE
226     }, {
227 	/* 480x300 @ 56 Hz, 35.2 kHz hsync, 8:5 aspect ratio */
228 	NULL, 56, 480, 300, 46176, 80, 16, 10, 1, 40, 1,
229 	0, FB_VMODE_DOUBLE
230     }, {
231 	/* 480x300 @ 60 Hz, 37.8 kHz hsync */
232 	NULL, 60, 480, 300, 41858, 56, 16, 11, 1, 80, 2,
233 	0, FB_VMODE_DOUBLE
234     }, {
235 	/* 480x300 @ 63 Hz, 39.6 kHz hsync */
236 	NULL, 63, 480, 300, 40000, 56, 16, 11, 1, 80, 2,
237 	0, FB_VMODE_DOUBLE
238     }, {
239 	/* 480x300 @ 72 Hz, 48.0 kHz hsync */
240 	NULL, 72, 480, 300, 33386, 40, 24, 11, 19, 80, 3,
241 	0, FB_VMODE_DOUBLE
242     },
243 };
244 
245 
my_atoi(const char * name)246 static int __init my_atoi(const char *name)
247 {
248     int val = 0;
249 
250     for (;; name++) {
251 	switch (*name) {
252 	    case '0'...'9':
253 		val = 10*val+(*name-'0');
254 		break;
255 	    default:
256 		return val;
257 	}
258     }
259 }
260 
261 
262 /**
263  *	__fb_try_mode - test a video mode
264  *	@var: frame buffer user defined part of display
265  *	@info: frame buffer info structure
266  *	@mode: frame buffer video mode structure
267  *	@bpp: color depth in bits per pixel
268  *
269  *	Tries a video mode to test it's validity for device @info.
270  *
271  *	Returns 1 on success.
272  *
273  */
274 
__fb_try_mode(struct fb_var_screeninfo * var,struct fb_info * info,const struct fb_videomode * mode,unsigned int bpp)275 int __fb_try_mode(struct fb_var_screeninfo *var, struct fb_info *info,
276 		  const struct fb_videomode *mode, unsigned int bpp)
277 {
278     int err;
279 
280     DPRINTK("Trying mode %s %dx%d-%d@%d\n", mode->name ? mode->name : "noname",
281 	    mode->xres, mode->yres, bpp, mode->refresh);
282     var->xres = mode->xres;
283     var->yres = mode->yres;
284     var->xres_virtual = mode->xres;
285     var->yres_virtual = mode->yres;
286     var->xoffset = 0;
287     var->yoffset = 0;
288     var->bits_per_pixel = bpp;
289     var->activate |= FB_ACTIVATE_TEST;
290     var->pixclock = mode->pixclock;
291     var->left_margin = mode->left_margin;
292     var->right_margin = mode->right_margin;
293     var->upper_margin = mode->upper_margin;
294     var->lower_margin = mode->lower_margin;
295     var->hsync_len = mode->hsync_len;
296     var->vsync_len = mode->vsync_len;
297     var->sync = mode->sync;
298     var->vmode = mode->vmode;
299     err = info->fbops->fb_set_var(var, PROC_CONSOLE(info), info);
300     var->activate &= ~FB_ACTIVATE_TEST;
301     return !err;
302 }
303 
304 
305 /**
306  *	fb_find_mode - finds a valid video mode
307  *	@var: frame buffer user defined part of display
308  *	@info: frame buffer info structure
309  *	@mode_option: string video mode to find
310  *	@db: video mode database
311  *	@dbsize: size of @db
312  *	@default_mode: default video mode to fall back to
313  *	@default_bpp: default color depth in bits per pixel
314  *
315  *	Finds a suitable video mode, starting with the specified mode
316  *	in @mode_option with fallback to @default_mode.  If
317  *	@default_mode fails, all modes in the video mode database will
318  *	be tried.
319  *
320  *	Valid mode specifiers for @mode_option:
321  *
322  *	<xres>x<yres>[-<bpp>][@<refresh>] or
323  *	<name>[-<bpp>][@<refresh>]
324  *
325  *	with <xres>, <yres>, <bpp> and <refresh> decimal numbers and
326  *	<name> a string.
327  *
328  *	NOTE: The passed struct @var is _not_ cleared!  This allows you
329  *	to supply values for e.g. the grayscale and accel_flags fields.
330  *
331  *	Returns zero for failure, 1 if using specified @mode_option,
332  *	2 if using specified @mode_option with an ignored refresh rate,
333  *	3 if default mode is used, 4 if fall back to any valid mode.
334  *
335  */
336 
fb_find_mode(struct fb_var_screeninfo * var,struct fb_info * info,const char * mode_option,const struct fb_videomode * db,unsigned int dbsize,const struct fb_videomode * default_mode,unsigned int default_bpp)337 int __init fb_find_mode(struct fb_var_screeninfo *var,
338 			struct fb_info *info, const char *mode_option,
339 			const struct fb_videomode *db, unsigned int dbsize,
340 			const struct fb_videomode *default_mode,
341 			unsigned int default_bpp)
342 {
343     int i, j;
344 
345     /* Set up defaults */
346     if (!db) {
347 	db = modedb;
348 	dbsize = sizeof(modedb)/sizeof(*modedb);
349     }
350     if (!default_mode)
351 	default_mode = &modedb[DEFAULT_MODEDB_INDEX];
352     if (!default_bpp)
353 	default_bpp = 8;
354 
355     /* Did the user specify a video mode? */
356     if (mode_option || (mode_option = global_mode_option)) {
357 	const char *name = mode_option;
358 	unsigned int namelen = strlen(name);
359 	int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
360 	unsigned int xres = 0, yres = 0, bpp = default_bpp, refresh = 0;
361 	int yres_specified = 0;
362 
363 	for (i = namelen-1; i >= 0; i--) {
364 	    switch (name[i]) {
365 		case '@':
366 		    namelen = i;
367 		    if (!refresh_specified && !bpp_specified &&
368 			!yres_specified) {
369 			refresh = my_atoi(&name[i+1]);
370 			refresh_specified = 1;
371 		    } else
372 			goto done;
373 		    break;
374 		case '-':
375 		    namelen = i;
376 		    if (!bpp_specified && !yres_specified) {
377 			bpp = my_atoi(&name[i+1]);
378 			bpp_specified = 1;
379 		    } else
380 			goto done;
381 		    break;
382 		case 'x':
383 		    if (!yres_specified) {
384 			yres = my_atoi(&name[i+1]);
385 			yres_specified = 1;
386 		    } else
387 			goto done;
388 		    break;
389 		case '0'...'9':
390 		    break;
391 		default:
392 		    goto done;
393 	    }
394 	}
395 	if (i < 0 && yres_specified) {
396 	    xres = my_atoi(name);
397 	    res_specified = 1;
398 	}
399 done:
400 	for (i = refresh_specified; i >= 0; i--) {
401 	    DPRINTK("Trying specified video mode%s\n",
402 		    i ? "" : " (ignoring refresh rate)");
403 	    for (j = 0; j < dbsize; j++)
404 		if ((name_matches(db[j], name, namelen) ||
405 		     (res_specified && res_matches(db[j], xres, yres))) &&
406 		    (!i || db[j].refresh == refresh) &&
407 		    __fb_try_mode(var, info, &db[j], bpp))
408 		    return 2-i;
409 	}
410     }
411 
412     DPRINTK("Trying default video mode\n");
413     if (__fb_try_mode(var, info, default_mode, default_bpp))
414 	return 3;
415 
416     DPRINTK("Trying all modes\n");
417     for (i = 0; i < dbsize; i++)
418 	if (__fb_try_mode(var, info, &db[i], default_bpp))
419 	    return 4;
420 
421     DPRINTK("No valid mode found\n");
422     return 0;
423 }
424 
425 EXPORT_SYMBOL(__fb_try_mode);
426