blob: 11cb20cb2630619a37d3d6db0b1d639c77b38d36 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen1c5b2581999-12-16 20:59:36 +00002/*
3 * Mini fbset implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen1c5b2581999-12-16 20:59:36 +00008 *
9 * This is a from-scratch implementation of fbset; but the de facto fbset
10 * implementation was a good reference. fbset (original) is released under
Eric Andersenc7bda1c2004-03-15 08:29:22 +000011 * the GPL, and is (c) 1995-1999 by:
Erik Andersen1c5b2581999-12-16 20:59:36 +000012 * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
13 */
Denys Vlasenkodd898c92016-11-23 11:46:32 +010014//config:config FBSET
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020015//config: bool "fbset (5.8 kb)"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010016//config: default y
17//config: select PLATFORM_LINUX
18//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020019//config: fbset is used to show or change the settings of a Linux frame buffer
20//config: device. The frame buffer device provides a simple and unique
21//config: interface to access a graphics display. Enable this option
22//config: if you wish to enable the 'fbset' utility.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010023//config:
24//config:config FEATURE_FBSET_FANCY
Denys Vlasenkof5604222017-01-10 14:58:54 +010025//config: bool "Enable extra options"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010026//config: default y
27//config: depends on FBSET
28//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020029//config: This option enables extended fbset options, allowing one to set the
30//config: framebuffer size, color depth, etc. interface to access a graphics
31//config: display. Enable this option if you wish to enable extended fbset
32//config: options.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010033//config:
34//config:config FEATURE_FBSET_READMODE
Denys Vlasenkof5604222017-01-10 14:58:54 +010035//config: bool "Enable readmode support"
Denys Vlasenkodd898c92016-11-23 11:46:32 +010036//config: default y
37//config: depends on FBSET
38//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020039//config: This option allows fbset to read the video mode database stored by
40//config: default as /etc/fb.modes, which can be used to set frame buffer
41//config: device to pre-defined video modes.
Denys Vlasenkodd898c92016-11-23 11:46:32 +010042
43//applet:IF_FBSET(APPLET(fbset, BB_DIR_USR_SBIN, BB_SUID_DROP))
44
45//kbuild:lib-$(CONFIG_FBSET) += fbset.o
Erik Andersen1c5b2581999-12-16 20:59:36 +000046
Pere Orga5bc8c002011-04-11 03:29:49 +020047//usage:#define fbset_trivial_usage
48//usage: "[OPTIONS] [MODE]"
49//usage:#define fbset_full_usage "\n\n"
50//usage: "Show and modify frame buffer settings"
51//usage:
52//usage:#define fbset_example_usage
53//usage: "$ fbset\n"
54//usage: "mode \"1024x768-76\"\n"
55//usage: " # D: 78.653 MHz, H: 59.949 kHz, V: 75.694 Hz\n"
56//usage: " geometry 1024 768 1024 768 16\n"
57//usage: " timings 12714 128 32 16 4 128 4\n"
58//usage: " accel false\n"
59//usage: " rgba 5/11,6/5,5/0,0/0\n"
60//usage: "endmode\n"
61
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000062#include "libbb.h"
Erik Andersen1c5b2581999-12-16 20:59:36 +000063
Eric Andersen5f284552003-11-14 03:11:29 +000064#define DEFAULTFBDEV FB_0
Erik Andersen1c5b2581999-12-16 20:59:36 +000065#define DEFAULTFBMODE "/etc/fb.modes"
66
Eric Andersenc873d612000-09-21 04:09:58 +000067/* Stuff stolen from the kernel's fb.h */
Denis Vlasenko274b8c02006-09-30 16:22:59 +000068#define FB_ACTIVATE_ALL 64
Rob Landleybc68cd12006-03-10 19:22:06 +000069enum {
70 FBIOGET_VSCREENINFO = 0x4600,
71 FBIOPUT_VSCREENINFO = 0x4601
72};
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +010073
Eric Andersenc873d612000-09-21 04:09:58 +000074struct fb_bitfield {
Denis Vlasenko39d551f2006-09-30 16:28:30 +000075 uint32_t offset; /* beginning of bitfield */
Denys Vlasenkofb132e42010-10-29 11:46:52 +020076 uint32_t length; /* length of bitfield */
Denis Vlasenko39d551f2006-09-30 16:28:30 +000077 uint32_t msb_right; /* !=0: Most significant bit is right */
Eric Andersenc873d612000-09-21 04:09:58 +000078};
79struct fb_var_screeninfo {
Denis Vlasenko39d551f2006-09-30 16:28:30 +000080 uint32_t xres; /* visible resolution */
Eric Andersen1eceb122003-07-14 19:32:40 +000081 uint32_t yres;
Denis Vlasenko39d551f2006-09-30 16:28:30 +000082 uint32_t xres_virtual; /* virtual resolution */
Eric Andersen1eceb122003-07-14 19:32:40 +000083 uint32_t yres_virtual;
Denis Vlasenko39d551f2006-09-30 16:28:30 +000084 uint32_t xoffset; /* offset from virtual to visible */
85 uint32_t yoffset; /* resolution */
Eric Andersenc873d612000-09-21 04:09:58 +000086
Denis Vlasenko39d551f2006-09-30 16:28:30 +000087 uint32_t bits_per_pixel;
88 uint32_t grayscale; /* !=0 Graylevels instead of colors */
Eric Andersenc873d612000-09-21 04:09:58 +000089
Denis Vlasenko39d551f2006-09-30 16:28:30 +000090 struct fb_bitfield red; /* bitfield in fb mem if true color, */
91 struct fb_bitfield green; /* else only length is significant */
Eric Andersenc873d612000-09-21 04:09:58 +000092 struct fb_bitfield blue;
Denis Vlasenko39d551f2006-09-30 16:28:30 +000093 struct fb_bitfield transp; /* transparency */
Eric Andersenc873d612000-09-21 04:09:58 +000094
Denis Vlasenko39d551f2006-09-30 16:28:30 +000095 uint32_t nonstd; /* !=0 Non standard pixel format */
Eric Andersenc873d612000-09-21 04:09:58 +000096
Denis Vlasenko39d551f2006-09-30 16:28:30 +000097 uint32_t activate; /* see FB_ACTIVATE_x */
Eric Andersenc873d612000-09-21 04:09:58 +000098
Denis Vlasenko39d551f2006-09-30 16:28:30 +000099 uint32_t height; /* height of picture in mm */
100 uint32_t width; /* width of picture in mm */
Eric Andersenc873d612000-09-21 04:09:58 +0000101
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200102 uint32_t accel_flags; /* acceleration flags (hints) */
Eric Andersenc873d612000-09-21 04:09:58 +0000103
104 /* Timing: All values in pixclocks, except pixclock (of course) */
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000105 uint32_t pixclock; /* pixel clock in ps (pico seconds) */
106 uint32_t left_margin; /* time from sync to picture */
107 uint32_t right_margin; /* time from picture to sync */
108 uint32_t upper_margin; /* time from sync to picture */
Eric Andersen1eceb122003-07-14 19:32:40 +0000109 uint32_t lower_margin;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000110 uint32_t hsync_len; /* length of horizontal sync */
111 uint32_t vsync_len; /* length of vertical sync */
112 uint32_t sync; /* see FB_SYNC_x */
113 uint32_t vmode; /* see FB_VMODE_x */
114 uint32_t reserved[6]; /* Reserved for future compatibility */
Eric Andersenc873d612000-09-21 04:09:58 +0000115};
116
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100117static void copy_if_gt0(uint32_t *src, uint32_t *dst, unsigned cnt)
118{
119 do {
120 if ((int32_t) *src > 0)
121 *dst = *src;
122 src++;
123 dst++;
124 } while (--cnt);
125}
126
127static NOINLINE void copy_changed_values(
128 struct fb_var_screeninfo *base,
129 struct fb_var_screeninfo *set)
130{
131 //if ((int32_t) set->xres > 0) base->xres = set->xres;
132 //if ((int32_t) set->yres > 0) base->yres = set->yres;
133 //if ((int32_t) set->xres_virtual > 0) base->xres_virtual = set->xres_virtual;
134 //if ((int32_t) set->yres_virtual > 0) base->yres_virtual = set->yres_virtual;
135 copy_if_gt0(&set->xres, &base->xres, 4);
136
137 if ((int32_t) set->bits_per_pixel > 0) base->bits_per_pixel = set->bits_per_pixel;
138 //copy_if_gt0(&set->bits_per_pixel, &base->bits_per_pixel, 1);
139
140 //if ((int32_t) set->pixclock > 0) base->pixclock = set->pixclock;
141 //if ((int32_t) set->left_margin > 0) base->left_margin = set->left_margin;
142 //if ((int32_t) set->right_margin > 0) base->right_margin = set->right_margin;
143 //if ((int32_t) set->upper_margin > 0) base->upper_margin = set->upper_margin;
144 //if ((int32_t) set->lower_margin > 0) base->lower_margin = set->lower_margin;
145 //if ((int32_t) set->hsync_len > 0) base->hsync_len = set->hsync_len;
146 //if ((int32_t) set->vsync_len > 0) base->vsync_len = set->vsync_len;
147 //if ((int32_t) set->sync > 0) base->sync = set->sync;
148 //if ((int32_t) set->vmode > 0) base->vmode = set->vmode;
149 copy_if_gt0(&set->pixclock, &base->pixclock, 9);
150}
151
152
153enum {
154 CMD_FB = 1,
155 CMD_DB = 2,
156 CMD_GEOMETRY = 3,
157 CMD_TIMING = 4,
158 CMD_ACCEL = 5,
159 CMD_HSYNC = 6,
160 CMD_VSYNC = 7,
161 CMD_LACED = 8,
162 CMD_DOUBLE = 9,
163/* CMD_XCOMPAT = 10, */
164 CMD_ALL = 11,
165 CMD_INFO = 12,
166 CMD_SHOW = 13,
167 CMD_CHANGE = 14,
168
169#if ENABLE_FEATURE_FBSET_FANCY
170 CMD_XRES = 100,
171 CMD_YRES = 101,
172 CMD_VXRES = 102,
173 CMD_VYRES = 103,
174 CMD_DEPTH = 104,
175 CMD_MATCH = 105,
176 CMD_PIXCLOCK = 106,
177 CMD_LEFT = 107,
178 CMD_RIGHT = 108,
179 CMD_UPPER = 109,
180 CMD_LOWER = 110,
181 CMD_HSLEN = 111,
182 CMD_VSLEN = 112,
183 CMD_CSYNC = 113,
184 CMD_GSYNC = 114,
185 CMD_EXTSYNC = 115,
186 CMD_BCAST = 116,
187 CMD_RGBA = 117,
188 CMD_STEP = 118,
189 CMD_MOVE = 119,
190#endif
191};
Eric Andersenc873d612000-09-21 04:09:58 +0000192
"Vladimir N. Oleynik"b399a962006-02-01 12:41:35 +0000193static const struct cmdoptions_t {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000194 const char name[9];
Aaron Lehmann60694412002-08-13 04:19:23 +0000195 const unsigned char param_count;
196 const unsigned char code;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000197} g_cmdoptions[] = {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000198 /*"12345678" + NUL */
Denys Vlasenko015db582016-06-19 18:15:33 +0200199//TODO: convert to index_in_strings()
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000200 { "fb" , 1, CMD_FB },
201 { "db" , 1, CMD_DB },
202 { "a" , 0, CMD_ALL },
203 { "i" , 0, CMD_INFO },
204 { "g" , 5, CMD_GEOMETRY },
205 { "t" , 7, CMD_TIMING },
206 { "accel" , 1, CMD_ACCEL },
207 { "hsync" , 1, CMD_HSYNC },
208 { "vsync" , 1, CMD_VSYNC },
209 { "laced" , 1, CMD_LACED },
210 { "double" , 1, CMD_DOUBLE },
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000211 { "show" , 0, CMD_SHOW },
212 { "s" , 0, CMD_SHOW },
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000213#if ENABLE_FEATURE_FBSET_FANCY
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000214 { "all" , 0, CMD_ALL },
215 { "xres" , 1, CMD_XRES },
216 { "yres" , 1, CMD_YRES },
217 { "vxres" , 1, CMD_VXRES },
218 { "vyres" , 1, CMD_VYRES },
219 { "depth" , 1, CMD_DEPTH },
220 { "match" , 0, CMD_MATCH },
221 { "geometry", 5, CMD_GEOMETRY },
222 { "pixclock", 1, CMD_PIXCLOCK },
223 { "left" , 1, CMD_LEFT },
224 { "right" , 1, CMD_RIGHT },
225 { "upper" , 1, CMD_UPPER },
226 { "lower" , 1, CMD_LOWER },
227 { "hslen" , 1, CMD_HSLEN },
228 { "vslen" , 1, CMD_VSLEN },
229 { "timings" , 7, CMD_TIMING },
230 { "csync" , 1, CMD_CSYNC },
231 { "gsync" , 1, CMD_GSYNC },
232 { "extsync" , 1, CMD_EXTSYNC },
233 { "bcast" , 1, CMD_BCAST },
234 { "rgba" , 1, CMD_RGBA },
235 { "step" , 1, CMD_STEP },
236 { "move" , 1, CMD_MOVE },
Erik Andersen1c5b2581999-12-16 20:59:36 +0000237#endif
Erik Andersen1c5b2581999-12-16 20:59:36 +0000238};
239
Matt Kraai269e07c2000-10-28 16:56:32 +0000240/* taken from linux/fb.h */
Rob Landleybc68cd12006-03-10 19:22:06 +0000241enum {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000242 FB_SYNC_HOR_HIGH_ACT = 1, /* horizontal sync high active */
243 FB_SYNC_VERT_HIGH_ACT = 2, /* vertical sync high active */
Denys Vlasenko6b01b712010-01-24 22:52:21 +0100244#if ENABLE_FEATURE_FBSET_READMODE
245 FB_VMODE_INTERLACED = 1, /* interlaced */
246 FB_VMODE_DOUBLE = 2, /* double scan */
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000247 FB_SYNC_EXT = 4, /* external sync */
248 FB_SYNC_COMP_HIGH_ACT = 8, /* composite sync high active */
Matt Kraai269e07c2000-10-28 16:56:32 +0000249#endif
Denys Vlasenko6b01b712010-01-24 22:52:21 +0100250};
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000251
Denis Vlasenkod91afa32008-07-29 11:10:01 +0000252#if ENABLE_FEATURE_FBSET_READMODE
Denis Vlasenko8d523cb2008-07-27 21:16:30 +0000253static void ss(uint32_t *x, uint32_t flag, char *buf, const char *what)
Denis Vlasenko09f5ecf2008-07-27 20:25:29 +0000254{
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000255 if (strcmp(buf, what) == 0)
Denis Vlasenko09f5ecf2008-07-27 20:25:29 +0000256 *x &= ~flag;
257 else
258 *x |= flag;
259}
260
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100261/* Mode db file contains mode definitions like this:
262 * mode "800x600-48-lace"
263 * # D: 36.00 MHz, H: 33.835 kHz, V: 96.39 Hz
264 * geometry 800 600 800 600 8
265 * timings 27778 56 80 79 11 128 12
266 * laced true
267 * hsync high
268 * vsync high
269 * endmode
270 */
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000271static int read_mode_db(struct fb_var_screeninfo *base, const char *fn,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000272 const char *mode)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000273{
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000274 char *token[2], *p, *s;
275 parser_t *parser = config_open(fn);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000277 while (config_read(parser, token, 2, 1, "# \t\r", PARSE_NORMAL)) {
278 if (strcmp(token[0], "mode") != 0 || !token[1])
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000279 continue;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000280 p = strstr(token[1], mode);
Denis Vlasenko6bef3d12007-11-06 03:05:54 +0000281 if (!p)
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000282 continue;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000283 s = p + strlen(mode);
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200284 //bb_error_msg("CHECK[%s][%s][%d]", mode, p-1, *s);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000285 /* exact match? */
286 if (((!*s || isspace(*s)) && '"' != s[-1]) /* end-of-token */
287 || ('"' == *s && '"' == p[-1]) /* ends with " but starts with " too! */
Denis Vlasenko8d523cb2008-07-27 21:16:30 +0000288 ) {
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200289 //bb_error_msg("FOUND[%s][%s][%s][%d]", token[1], p, mode, isspace(*s));
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000290 break;
Denis Vlasenko8d523cb2008-07-27 21:16:30 +0000291 }
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000292 }
Eric Andersen6f96e672000-07-12 23:01:04 +0000293
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000294 if (!token[0])
295 return 0;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000296
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000297 while (config_read(parser, token, 2, 1, "# \t", PARSE_NORMAL)) {
298 int i;
299
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200300//bb_error_msg("???[%s][%s]", token[0], token[1]);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000301 if (strcmp(token[0], "endmode") == 0) {
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200302//bb_error_msg("OK[%s]", mode);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000303 return 1;
304 }
305 p = token[1];
306 i = index_in_strings(
Linus Walleijb5c72202012-07-01 22:34:42 +0200307 "geometry\0timings\0interlaced\0double\0vsync\0hsync\0csync\0extsync\0rgba\0",
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000308 token[0]);
309 switch (i) {
310 case 0:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100311 if (sizeof(int) == sizeof(base->xres)) {
312 sscanf(p, "%d %d %d %d %d",
313 &base->xres, &base->yres,
314 &base->xres_virtual, &base->yres_virtual,
315 &base->bits_per_pixel);
316 } else {
317 int base_xres, base_yres;
318 int base_xres_virtual, base_yres_virtual;
319 int base_bits_per_pixel;
320 sscanf(p, "%d %d %d %d %d",
321 &base_xres, &base_yres,
322 &base_xres_virtual, &base_yres_virtual,
323 &base_bits_per_pixel);
324 base->xres = base_xres;
325 base->yres = base_yres;
326 base->xres_virtual = base_xres_virtual;
327 base->yres_virtual = base_yres_virtual;
328 base->bits_per_pixel = base_bits_per_pixel;
329 }
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200330//bb_error_msg("GEO[%s]", p);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000331 break;
332 case 1:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100333 if (sizeof(int) == sizeof(base->xres)) {
334 sscanf(p, "%d %d %d %d %d %d %d",
335 &base->pixclock,
336 &base->left_margin, &base->right_margin,
337 &base->upper_margin, &base->lower_margin,
338 &base->hsync_len, &base->vsync_len);
339 } else {
340 int base_pixclock;
341 int base_left_margin, base_right_margin;
342 int base_upper_margin, base_lower_margin;
343 int base_hsync_len, base_vsync_len;
344 sscanf(p, "%d %d %d %d %d %d %d",
345 &base_pixclock,
346 &base_left_margin, &base_right_margin,
347 &base_upper_margin, &base_lower_margin,
348 &base_hsync_len, &base_vsync_len);
349 base->pixclock = base_pixclock;
350 base->left_margin = base_left_margin;
351 base->right_margin = base_right_margin;
352 base->upper_margin = base_upper_margin;
353 base->lower_margin = base_lower_margin;
354 base->hsync_len = base_hsync_len;
355 base->vsync_len = base_vsync_len;
356 }
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200357//bb_error_msg("TIM[%s]", p);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000358 break;
359 case 2:
360 case 3: {
361 static const uint32_t syncs[] = {FB_VMODE_INTERLACED, FB_VMODE_DOUBLE};
362 ss(&base->vmode, syncs[i-2], p, "false");
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200363//bb_error_msg("VMODE[%s]", p);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000364 break;
365 }
366 case 4:
367 case 5:
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200368 case 6: {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000369 static const uint32_t syncs[] = {FB_SYNC_VERT_HIGH_ACT, FB_SYNC_HOR_HIGH_ACT, FB_SYNC_COMP_HIGH_ACT};
370 ss(&base->sync, syncs[i-4], p, "low");
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200371//bb_error_msg("SYNC[%s]", p);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000372 break;
373 }
374 case 7:
375 ss(&base->sync, FB_SYNC_EXT, p, "false");
Denys Vlasenko76b680c2016-03-30 16:04:37 +0200376//bb_error_msg("EXTSYNC[%s]", p);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000377 break;
Linus Walleijb5c72202012-07-01 22:34:42 +0200378 case 8: {
379 int red_offset, red_length;
380 int green_offset, green_length;
381 int blue_offset, blue_length;
382 int transp_offset, transp_length;
383
384 sscanf(p, "%d/%d,%d/%d,%d/%d,%d/%d",
Linus Walleij52f2f372015-07-27 09:59:04 +0200385 &red_length, &red_offset,
386 &green_length, &green_offset,
387 &blue_length, &blue_offset,
388 &transp_length, &transp_offset);
Linus Walleijb5c72202012-07-01 22:34:42 +0200389 base->red.offset = red_offset;
390 base->red.length = red_length;
391 base->red.msb_right = 0;
392 base->green.offset = green_offset;
393 base->green.length = green_length;
394 base->green.msb_right = 0;
395 base->blue.offset = blue_offset;
396 base->blue.length = blue_length;
397 base->blue.msb_right = 0;
398 base->transp.offset = transp_offset;
399 base->transp.length = transp_length;
400 base->transp.msb_right = 0;
401 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000402 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000403 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000404 return 0;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000405}
Denis Vlasenko85c24712008-03-17 09:04:04 +0000406#endif
Erik Andersen1c5b2581999-12-16 20:59:36 +0000407
Denys Vlasenkoef5bc2c2009-10-08 14:54:18 +0200408static NOINLINE void showmode(struct fb_var_screeninfo *v)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000409{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000410 double drate = 0, hrate = 0, vrate = 0;
411
412 if (v->pixclock) {
413 drate = 1e12 / v->pixclock;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000414 hrate = drate / (v->left_margin + v->xres + v->right_margin + v->hsync_len);
415 vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000416 }
Aaron Lehmann60694412002-08-13 04:19:23 +0000417 printf("\nmode \"%ux%u-%u\"\n"
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000418#if ENABLE_FEATURE_FBSET_FANCY
Aaron Lehmann60694412002-08-13 04:19:23 +0000419 "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
Erik Andersen1c5b2581999-12-16 20:59:36 +0000420#endif
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000421 "\tgeometry %u %u %u %u %u\n"
422 "\ttimings %u %u %u %u %u %u %u\n"
423 "\taccel %s\n"
424 "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
425 "endmode\n\n",
426 v->xres, v->yres, (int) (vrate + 0.5),
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000427#if ENABLE_FEATURE_FBSET_FANCY
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000428 drate / 1e6, hrate / 1e3, vrate,
Aaron Lehmann60694412002-08-13 04:19:23 +0000429#endif
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000430 v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
431 v->pixclock, v->left_margin, v->right_margin, v->upper_margin, v->lower_margin,
432 v->hsync_len, v->vsync_len,
433 (v->accel_flags > 0 ? "true" : "false"),
434 v->red.length, v->red.offset, v->green.length, v->green.offset,
435 v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000436}
437
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000438int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000439int fbset_main(int argc, char **argv)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000440{
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000441 enum {
442 OPT_CHANGE = (1 << 0),
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000443 OPT_SHOW = (1 << 1),
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000444 OPT_READMODE = (1 << 2),
Denys Vlasenko972e0462011-06-06 04:21:39 +0200445 OPT_ALL = (1 << 3),
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000446 };
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100447 struct fb_var_screeninfo var_old, var_set;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000448 int fh, i;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000449 unsigned options = 0;
450
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +0000451 const char *fbdev = DEFAULTFBDEV;
Denys Vlasenko015db582016-06-19 18:15:33 +0200452 IF_FEATURE_FBSET_READMODE(const char *modefile = DEFAULTFBMODE;)
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100453 char *thisarg;
454 char *mode = mode; /* for compiler */
Erik Andersen1c5b2581999-12-16 20:59:36 +0000455
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100456 memset(&var_set, 0xff, sizeof(var_set)); /* set all to -1 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457
458 /* parse cmd args.... why do they have to make things so difficult? */
459 argv++;
460 argc--;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000461 for (; argc > 0 && (thisarg = *argv) != NULL; argc--, argv++) {
Denys Vlasenko12bc1522011-05-09 03:57:27 +0200462 if (thisarg[0] != '-') {
463 if (!ENABLE_FEATURE_FBSET_READMODE || argc != 1)
464 bb_show_usage();
465 mode = thisarg;
466 options |= OPT_READMODE;
467 goto contin;
468 }
469 for (i = 0; i < ARRAY_SIZE(g_cmdoptions); i++) {
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000470 if (strcmp(thisarg + 1, g_cmdoptions[i].name) != 0)
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000471 continue;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000472 if (argc <= g_cmdoptions[i].param_count)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000473 bb_show_usage();
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000474
475 switch (g_cmdoptions[i].code) {
476 case CMD_FB:
477 fbdev = argv[1];
478 break;
479 case CMD_DB:
Denys Vlasenko015db582016-06-19 18:15:33 +0200480 IF_FEATURE_FBSET_READMODE(modefile = argv[1];)
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000481 break;
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000482 case CMD_ALL:
483 options |= OPT_ALL;
484 break;
485 case CMD_SHOW:
486 options |= OPT_SHOW;
487 break;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000488 case CMD_GEOMETRY:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100489 var_set.xres = xatou32(argv[1]);
490 var_set.yres = xatou32(argv[2]);
491 var_set.xres_virtual = xatou32(argv[3]);
492 var_set.yres_virtual = xatou32(argv[4]);
493 var_set.bits_per_pixel = xatou32(argv[5]);
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000494 break;
495 case CMD_TIMING:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100496 var_set.pixclock = xatou32(argv[1]);
497 var_set.left_margin = xatou32(argv[2]);
498 var_set.right_margin = xatou32(argv[3]);
499 var_set.upper_margin = xatou32(argv[4]);
500 var_set.lower_margin = xatou32(argv[5]);
501 var_set.hsync_len = xatou32(argv[6]);
502 var_set.vsync_len = xatou32(argv[7]);
503 break;
504 case CMD_ACCEL:
505 break;
506 case CMD_HSYNC:
507 var_set.sync |= FB_SYNC_HOR_HIGH_ACT;
508 break;
509 case CMD_VSYNC:
510 var_set.sync |= FB_SYNC_VERT_HIGH_ACT;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000511 break;
Denis Vlasenkoe3241842007-08-13 10:36:25 +0000512#if ENABLE_FEATURE_FBSET_FANCY
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000513 case CMD_XRES:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100514 var_set.xres = xatou32(argv[1]);
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000515 break;
516 case CMD_YRES:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100517 var_set.yres = xatou32(argv[1]);
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000518 break;
519 case CMD_DEPTH:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100520 var_set.bits_per_pixel = xatou32(argv[1]);
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000521 break;
522#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 }
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000524 switch (g_cmdoptions[i].code) {
525 case CMD_FB:
526 case CMD_DB:
527 case CMD_ALL:
528 case CMD_SHOW:
529 break;
530 default:
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100531 /* other commands imply changes */
532 options |= OPT_CHANGE;
Bernhard Reutner-Fischer7307e062009-02-18 15:28:43 +0000533 }
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000534 argc -= g_cmdoptions[i].param_count;
535 argv += g_cmdoptions[i].param_count;
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000536 goto contin;
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000537 }
Denys Vlasenko12bc1522011-05-09 03:57:27 +0200538 bb_show_usage();
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000539 contin: ;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000540 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000541
Rob Landleyd921b2e2006-08-03 15:41:12 +0000542 fh = xopen(fbdev, O_RDONLY);
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100543 xioctl(fh, FBIOGET_VSCREENINFO, &var_old);
544
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000545 if (options & OPT_READMODE) {
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100546#if ENABLE_FEATURE_FBSET_READMODE
547 if (!read_mode_db(&var_old, modefile, mode)) {
Denis Vlasenko39d551f2006-09-30 16:28:30 +0000548 bb_error_msg_and_die("unknown video mode '%s'", mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000549 }
Denys Vlasenko972e0462011-06-06 04:21:39 +0200550 options |= OPT_CHANGE;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000551#endif
Erik Andersen1c5b2581999-12-16 20:59:36 +0000552 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000553
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000554 if (options & OPT_CHANGE) {
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100555 copy_changed_values(&var_old, &var_set);
Denis Vlasenkob66d16a2008-08-24 16:25:40 +0000556 if (options & OPT_ALL)
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100557 var_old.activate = FB_ACTIVATE_ALL;
558 xioctl(fh, FBIOPUT_VSCREENINFO, &var_old);
Denis Vlasenko274b8c02006-09-30 16:22:59 +0000559 }
Michael Grzeschik24dc9ab2010-01-23 03:40:28 +0100560
561 if (options == 0 || (options & OPT_SHOW))
562 showmode(&var_old);
563
564 if (ENABLE_FEATURE_CLEAN_UP)
565 close(fh);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000566
Matt Kraai3e856ce2000-12-01 02:55:13 +0000567 return EXIT_SUCCESS;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000568}