1 /* vi: set sw=4 ts=4: */
3 * Mini fbset implementation for busybox
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
11 * the GPL, and is (c) 1995-1999 by:
12 * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
17 #define DEFAULTFBDEV FB_0
18 #define DEFAULTFBMODE "/etc/fb.modes"
21 OPT_CHANGE = (1 << 0),
23 OPT_READMODE = (1 << 2),
35 /* CMD_XCOMPAT = 10, */
40 #if ENABLE_FEATURE_FBSET_FANCY
64 static unsigned g_options;
66 /* Stuff stolen from the kernel's fb.h */
67 #define FB_ACTIVATE_ALL 64
69 FBIOGET_VSCREENINFO = 0x4600,
70 FBIOPUT_VSCREENINFO = 0x4601
73 uint32_t offset; /* beginning of bitfield */
74 uint32_t length; /* length of bitfield */
75 uint32_t msb_right; /* !=0: Most significant bit is right */
77 struct fb_var_screeninfo {
78 uint32_t xres; /* visible resolution */
80 uint32_t xres_virtual; /* virtual resolution */
81 uint32_t yres_virtual;
82 uint32_t xoffset; /* offset from virtual to visible */
83 uint32_t yoffset; /* resolution */
85 uint32_t bits_per_pixel;
86 uint32_t grayscale; /* !=0 Graylevels instead of colors */
88 struct fb_bitfield red; /* bitfield in fb mem if true color, */
89 struct fb_bitfield green; /* else only length is significant */
90 struct fb_bitfield blue;
91 struct fb_bitfield transp; /* transparency */
93 uint32_t nonstd; /* !=0 Non standard pixel format */
95 uint32_t activate; /* see FB_ACTIVATE_x */
97 uint32_t height; /* height of picture in mm */
98 uint32_t width; /* width of picture in mm */
100 uint32_t accel_flags; /* acceleration flags (hints) */
102 /* Timing: All values in pixclocks, except pixclock (of course) */
103 uint32_t pixclock; /* pixel clock in ps (pico seconds) */
104 uint32_t left_margin; /* time from sync to picture */
105 uint32_t right_margin; /* time from picture to sync */
106 uint32_t upper_margin; /* time from sync to picture */
107 uint32_t lower_margin;
108 uint32_t hsync_len; /* length of horizontal sync */
109 uint32_t vsync_len; /* length of vertical sync */
110 uint32_t sync; /* see FB_SYNC_x */
111 uint32_t vmode; /* see FB_VMODE_x */
112 uint32_t reserved[6]; /* Reserved for future compatibility */
116 static const struct cmdoptions_t {
118 const unsigned char param_count;
119 const unsigned char code;
121 { "-fb", 1, CMD_FB },
122 { "-db", 1, CMD_DB },
123 { "-a", 0, CMD_ALL },
124 { "-i", 0, CMD_INFO },
125 { "-g", 5, CMD_GEOMETRY },
126 { "-t", 7, CMD_TIMING },
127 { "-accel", 1, CMD_ACCEL },
128 { "-hsync", 1, CMD_HSYNC },
129 { "-vsync", 1, CMD_VSYNC },
130 { "-laced", 1, CMD_LACED },
131 { "-double", 1, CMD_DOUBLE },
132 { "-n", 0, CMD_CHANGE },
133 #if ENABLE_FEATURE_FBSET_FANCY
134 { "-all", 0, CMD_ALL },
135 { "-xres", 1, CMD_XRES },
136 { "-yres", 1, CMD_YRES },
137 { "-vxres", 1, CMD_VXRES },
138 { "-vyres", 1, CMD_VYRES },
139 { "-depth", 1, CMD_DEPTH },
140 { "-match", 0, CMD_MATCH },
141 { "-geometry", 5, CMD_GEOMETRY },
142 { "-pixclock", 1, CMD_PIXCLOCK },
143 { "-left", 1, CMD_LEFT },
144 { "-right", 1, CMD_RIGHT },
145 { "-upper", 1, CMD_UPPER },
146 { "-lower", 1, CMD_LOWER },
147 { "-hslen", 1, CMD_HSLEN },
148 { "-vslen", 1, CMD_VSLEN },
149 { "-timings", 7, CMD_TIMING },
150 { "-csync", 1, CMD_CSYNC },
151 { "-gsync", 1, CMD_GSYNC },
152 { "-extsync", 1, CMD_EXTSYNC },
153 { "-bcast", 1, CMD_BCAST },
154 { "-rgba", 1, CMD_RGBA },
155 { "-step", 1, CMD_STEP },
156 { "-move", 1, CMD_MOVE },
161 #if ENABLE_FEATURE_FBSET_READMODE
162 /* taken from linux/fb.h */
164 FB_VMODE_INTERLACED = 1, /* interlaced */
165 FB_VMODE_DOUBLE = 2, /* double scan */
166 FB_SYNC_HOR_HIGH_ACT = 1, /* horizontal sync high active */
167 FB_SYNC_VERT_HIGH_ACT = 2, /* vertical sync high active */
168 FB_SYNC_EXT = 4, /* external sync */
169 FB_SYNC_COMP_HIGH_ACT = 8 /* composite sync high active */
173 static void ss(uint32_t *x, uint32_t flag, char *where, const char *what)
175 if (strstr(where, what))
181 #if ENABLE_FEATURE_FBSET_READMODE
182 static int readmode(struct fb_var_screeninfo *base, const char *fn,
189 f = xfopen_for_read(fn);
191 fgets(buf, sizeof(buf), f);
192 p = strstr(buf, "mode ");
193 if (!p && !(p = strstr(buf, "mode\t")))
195 p = strstr(p + 5, mode);
199 if (!isspace(*p) && (*p != 0) && (*p != '"')
200 && (*p != '\r') && (*p != '\n'))
201 continue; /* almost, but not quite */
204 fgets(buf, sizeof(buf), f);
205 p = strstr(buf, "geometry ");
208 /* FIXME: catastrophic on arches with 64bit ints */
209 sscanf(p, "%d %d %d %d %d",
210 &(base->xres), &(base->yres),
211 &(base->xres_virtual), &(base->yres_virtual),
212 &(base->bits_per_pixel));
213 } else if ((p = strstr(buf, "timings "))) {
215 sscanf(p, "%d %d %d %d %d %d %d",
217 &(base->left_margin), &(base->right_margin),
218 &(base->upper_margin), &(base->lower_margin),
219 &(base->hsync_len), &(base->vsync_len));
220 } else if ((p = strstr(buf, "laced "))) {
222 ss(&base->vmode, FB_VMODE_INTERLACED, buf, "false");
223 } else if ((p = strstr(buf, "double "))) {
225 ss(&base->vmode, FB_VMODE_DOUBLE, buf, "false");
226 } else if ((p = strstr(buf, "vsync "))) {
228 ss(&base->sync, FB_SYNC_VERT_HIGH_ACT, buf, "low");
229 } else if ((p = strstr(buf, "hsync "))) {
231 ss(&base->sync, FB_SYNC_HOR_HIGH_ACT, buf, "low");
232 } else if ((p = strstr(buf, "csync "))) {
234 ss(&base->sync, FB_SYNC_COMP_HIGH_ACT, buf, "low");
235 } else if ((p = strstr(buf, "extsync "))) {
237 ss(&base->sync, FB_SYNC_EXT, buf, "false");
240 if (strstr(buf, "endmode"))
248 static inline void setmode(struct fb_var_screeninfo *base,
249 struct fb_var_screeninfo *set)
251 if ((int) set->xres > 0)
252 base->xres = set->xres;
253 if ((int) set->yres > 0)
254 base->yres = set->yres;
255 if ((int) set->xres_virtual > 0)
256 base->xres_virtual = set->xres_virtual;
257 if ((int) set->yres_virtual > 0)
258 base->yres_virtual = set->yres_virtual;
259 if ((int) set->bits_per_pixel > 0)
260 base->bits_per_pixel = set->bits_per_pixel;
263 static inline void showmode(struct fb_var_screeninfo *v)
265 double drate = 0, hrate = 0, vrate = 0;
268 drate = 1e12 / v->pixclock;
269 hrate = drate / (v->left_margin + v->xres + v->right_margin + v->hsync_len);
270 vrate = hrate / (v->upper_margin + v->yres + v->lower_margin + v->vsync_len);
272 printf("\nmode \"%ux%u-%u\"\n"
273 #if ENABLE_FEATURE_FBSET_FANCY
274 "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
276 "\tgeometry %u %u %u %u %u\n"
277 "\ttimings %u %u %u %u %u %u %u\n"
279 "\trgba %u/%u,%u/%u,%u/%u,%u/%u\n"
281 v->xres, v->yres, (int) (vrate + 0.5),
282 #if ENABLE_FEATURE_FBSET_FANCY
283 drate / 1e6, hrate / 1e3, vrate,
285 v->xres, v->yres, v->xres_virtual, v->yres_virtual, v->bits_per_pixel,
286 v->pixclock, v->left_margin, v->right_margin, v->upper_margin, v->lower_margin,
287 v->hsync_len, v->vsync_len,
288 (v->accel_flags > 0 ? "true" : "false"),
289 v->red.length, v->red.offset, v->green.length, v->green.offset,
290 v->blue.length, v->blue.offset, v->transp.length, v->transp.offset);
294 int main(int argc, char **argv)
296 int fbset_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
297 int fbset_main(int argc, char **argv)
300 struct fb_var_screeninfo var, varset;
302 const char *fbdev = DEFAULTFBDEV;
303 const char *modefile = DEFAULTFBMODE;
304 char *thisarg, *mode = NULL;
306 memset(&varset, 0xFF, sizeof(varset));
308 /* parse cmd args.... why do they have to make things so difficult? */
311 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
312 for (i = 0; g_cmdoptions[i].name[0]; i++) {
313 if (strcmp(thisarg, g_cmdoptions[i].name))
315 if (argc-1 < g_cmdoptions[i].param_count)
318 switch (g_cmdoptions[i].code) {
326 varset.xres = xatou32(argv[1]);
327 varset.yres = xatou32(argv[2]);
328 varset.xres_virtual = xatou32(argv[3]);
329 varset.yres_virtual = xatou32(argv[4]);
330 varset.bits_per_pixel = xatou32(argv[5]);
333 varset.pixclock = xatou32(argv[1]);
334 varset.left_margin = xatou32(argv[2]);
335 varset.right_margin = xatou32(argv[3]);
336 varset.upper_margin = xatou32(argv[4]);
337 varset.lower_margin = xatou32(argv[5]);
338 varset.hsync_len = xatou32(argv[6]);
339 varset.vsync_len = xatou32(argv[7]);
342 g_options |= OPT_ALL;
345 g_options |= OPT_CHANGE;
347 #if ENABLE_FEATURE_FBSET_FANCY
349 varset.xres = xatou32(argv[1]);
352 varset.yres = xatou32(argv[1]);
355 varset.bits_per_pixel = xatou32(argv[1]);
359 argc -= g_cmdoptions[i].param_count;
360 argv += g_cmdoptions[i].param_count;
363 if (!g_cmdoptions[i].name[0]) {
367 g_options |= OPT_READMODE;
371 fh = xopen(fbdev, O_RDONLY);
372 xioctl(fh, FBIOGET_VSCREENINFO, &var);
373 if (g_options & OPT_READMODE) {
374 #if !ENABLE_FEATURE_FBSET_READMODE
377 if (!readmode(&var, modefile, mode)) {
378 bb_error_msg_and_die("unknown video mode '%s'", mode);
383 setmode(&var, &varset);
384 if (g_options & OPT_CHANGE) {
385 if (g_options & OPT_ALL)
386 var.activate = FB_ACTIVATE_ALL;
387 xioctl(fh, FBIOPUT_VSCREENINFO, &var);
390 /* Don't close the file, as exiting will take care of that */