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)
23 #include <sys/ioctl.h>
26 #define DEFAULTFBDEV FB_0
27 #define DEFAULTFBMODE "/etc/fb.modes"
30 OPT_CHANGE = (1 << 0),
32 OPT_READMODE = (1 << 2),
43 /* CMD_XCOMPAT = 10, */
48 #ifdef CONFIG_FEATURE_FBSET_FANCY
72 static unsigned int g_options = 0;
74 /* Stuff stolen from the kernel's fb.h */
76 FBIOGET_VSCREENINFO = 0x4600,
77 FBIOPUT_VSCREENINFO = 0x4601
80 uint32_t offset; /* beginning of bitfield */
81 uint32_t length; /* length of bitfield */
82 uint32_t msb_right; /* != 0 : Most significant bit is */
85 struct fb_var_screeninfo {
86 uint32_t xres; /* visible resolution */
88 uint32_t xres_virtual; /* virtual resolution */
89 uint32_t yres_virtual;
90 uint32_t xoffset; /* offset from virtual to visible */
91 uint32_t yoffset; /* resolution */
93 uint32_t bits_per_pixel; /* guess what */
94 uint32_t grayscale; /* != 0 Graylevels instead of colors */
96 struct fb_bitfield red; /* bitfield in fb mem if true color, */
97 struct fb_bitfield green; /* else only length is significant */
98 struct fb_bitfield blue;
99 struct fb_bitfield transp; /* transparency */
101 uint32_t nonstd; /* != 0 Non standard pixel format */
103 uint32_t activate; /* see FB_ACTIVATE_* */
105 uint32_t height; /* height of picture in mm */
106 uint32_t width; /* width of picture in mm */
108 uint32_t accel_flags; /* acceleration flags (hints) */
110 /* Timing: All values in pixclocks, except pixclock (of course) */
111 uint32_t pixclock; /* pixel clock in ps (pico seconds) */
112 uint32_t left_margin; /* time from sync to picture */
113 uint32_t right_margin; /* time from picture to sync */
114 uint32_t upper_margin; /* time from sync to picture */
115 uint32_t lower_margin;
116 uint32_t hsync_len; /* length of horizontal sync */
117 uint32_t vsync_len; /* length of vertical sync */
118 uint32_t sync; /* see FB_SYNC_* */
119 uint32_t vmode; /* see FB_VMODE_* */
120 uint32_t reserved[6]; /* Reserved for future compatibility */
124 static const struct cmdoptions_t {
126 const unsigned char param_count;
127 const unsigned char code;
133 "-i", 0, CMD_INFO}, {
134 "-g", 5, CMD_GEOMETRY}, {
135 "-t", 7, CMD_TIMING}, {
136 "-accel", 1, CMD_ACCEL}, {
137 "-hsync", 1, CMD_HSYNC}, {
138 "-vsync", 1, CMD_VSYNC}, {
139 "-laced", 1, CMD_LACED}, {
140 "-double", 1, CMD_DOUBLE}, {
141 "-n", 0, CMD_CHANGE}, {
142 #ifdef CONFIG_FEATURE_FBSET_FANCY
143 "-all", 0, CMD_ALL}, {
144 "-xres", 1, CMD_XRES}, {
145 "-yres", 1, CMD_YRES}, {
146 "-vxres", 1, CMD_VXRES}, {
147 "-vyres", 1, CMD_VYRES}, {
148 "-depth", 1, CMD_DEPTH}, {
149 "-match", 0, CMD_MATCH}, {
150 "-geometry", 5, CMD_GEOMETRY}, {
151 "-pixclock", 1, CMD_PIXCLOCK}, {
152 "-left", 1, CMD_LEFT}, {
153 "-right", 1, CMD_RIGHT}, {
154 "-upper", 1, CMD_UPPER}, {
155 "-lower", 1, CMD_LOWER}, {
156 "-hslen", 1, CMD_HSLEN}, {
157 "-vslen", 1, CMD_VSLEN}, {
158 "-timings", 7, CMD_TIMING}, {
159 "-csync", 1, CMD_CSYNC}, {
160 "-gsync", 1, CMD_GSYNC}, {
161 "-extsync", 1, CMD_EXTSYNC}, {
162 "-bcast", 1, CMD_BCAST}, {
163 "-rgba", 1, CMD_RGBA}, {
164 "-step", 1, CMD_STEP}, {
165 "-move", 1, CMD_MOVE}, {
170 #ifdef CONFIG_FEATURE_FBSET_READMODE
171 /* taken from linux/fb.h */
173 FB_VMODE_INTERLACED = 1, /* interlaced */
174 FB_VMODE_DOUBLE = 2, /* double scan */
175 FB_SYNC_HOR_HIGH_ACT = 1, /* horizontal sync high active */
176 FB_SYNC_VERT_HIGH_ACT = 2, /* vertical sync high active */
177 FB_SYNC_EXT = 4, /* external sync */
178 FB_SYNC_COMP_HIGH_ACT = 8 /* composite sync high active */
181 static int readmode(struct fb_var_screeninfo *base, const char *fn,
184 #ifdef CONFIG_FEATURE_FBSET_READMODE
189 f = bb_xfopen(fn, "r");
191 fgets(buf, sizeof(buf), f);
192 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
194 if ((p = strstr(buf, mode))) {
196 if (!isspace(*p) && (*p != 0) && (*p != '"')
197 && (*p != '\r') && (*p != '\n'))
198 continue; /* almost, but not quite */
200 fgets(buf, sizeof(buf), f);
202 if ((p = strstr(buf, "geometry "))) {
205 sscanf(p, "%d %d %d %d %d",
206 &(base->xres), &(base->yres),
207 &(base->xres_virtual), &(base->yres_virtual),
208 &(base->bits_per_pixel));
209 } else if ((p = strstr(buf, "timings "))) {
212 sscanf(p, "%d %d %d %d %d %d %d",
214 &(base->left_margin), &(base->right_margin),
215 &(base->upper_margin), &(base->lower_margin),
216 &(base->hsync_len), &(base->vsync_len));
217 } else if ((p = strstr(buf, "laced "))) {
220 if (strstr(buf, "false")) {
221 base->vmode &= ~FB_VMODE_INTERLACED;
223 base->vmode |= FB_VMODE_INTERLACED;
225 } else if ((p = strstr(buf, "double "))) {
228 if (strstr(buf, "false")) {
229 base->vmode &= ~FB_VMODE_DOUBLE;
231 base->vmode |= FB_VMODE_DOUBLE;
233 } else if ((p = strstr(buf, "vsync "))) {
236 if (strstr(buf, "low")) {
237 base->sync &= ~FB_SYNC_VERT_HIGH_ACT;
239 base->sync |= FB_SYNC_VERT_HIGH_ACT;
241 } else if ((p = strstr(buf, "hsync "))) {
244 if (strstr(buf, "low")) {
245 base->sync &= ~FB_SYNC_HOR_HIGH_ACT;
247 base->sync |= FB_SYNC_HOR_HIGH_ACT;
249 } else if ((p = strstr(buf, "csync "))) {
252 if (strstr(buf, "low")) {
253 base->sync &= ~FB_SYNC_COMP_HIGH_ACT;
255 base->sync |= FB_SYNC_COMP_HIGH_ACT;
257 } else if ((p = strstr(buf, "extsync "))) {
260 if (strstr(buf, "false")) {
261 base->sync &= ~FB_SYNC_EXT;
263 base->sync |= FB_SYNC_EXT;
267 if (strstr(buf, "endmode"))
274 bb_error_msg( "mode reading not compiled in");
279 static inline void setmode(struct fb_var_screeninfo *base,
280 struct fb_var_screeninfo *set)
282 if ((int) set->xres > 0)
283 base->xres = set->xres;
284 if ((int) set->yres > 0)
285 base->yres = set->yres;
286 if ((int) set->xres_virtual > 0)
287 base->xres_virtual = set->xres_virtual;
288 if ((int) set->yres_virtual > 0)
289 base->yres_virtual = set->yres_virtual;
290 if ((int) set->bits_per_pixel > 0)
291 base->bits_per_pixel = set->bits_per_pixel;
294 static inline void showmode(struct fb_var_screeninfo *v)
296 double drate = 0, hrate = 0, vrate = 0;
299 drate = 1e12 / v->pixclock;
301 drate / (v->left_margin + v->xres + v->right_margin +
304 hrate / (v->upper_margin + v->yres + v->lower_margin +
307 printf("\nmode \"%ux%u-%u\"\n"
308 #ifdef CONFIG_FEATURE_FBSET_FANCY
309 "\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n"
311 "\tgeometry %u %u %u %u %u\n\ttimings %u %u %u %u %u %u %u\n\taccel %s\n\trgba %u/%u,%u/%u,%u/%u,%u/%u\nendmode\n\n",
312 v->xres, v->yres, (int) (vrate + 0.5),
313 #ifdef CONFIG_FEATURE_FBSET_FANCY
314 drate / 1e6, hrate / 1e3, vrate,
316 v->xres, v->yres, v->xres_virtual, v->yres_virtual,
317 v->bits_per_pixel, v->pixclock, v->left_margin,
318 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
319 v->vsync_len, (v->accel_flags > 0 ? "true" : "false"), v->red.length,
320 v->red.offset, v->green.length, v->green.offset, v->blue.length,
321 v->blue.offset, v->transp.length, v->transp.offset);
325 int main(int argc, char **argv)
327 int fbset_main(int argc, char **argv)
330 struct fb_var_screeninfo var, varset;
332 char *fbdev = DEFAULTFBDEV;
333 char *modefile = DEFAULTFBMODE;
334 char *thisarg, *mode = NULL;
336 memset(&varset, 0xFF, sizeof(varset));
338 /* parse cmd args.... why do they have to make things so difficult? */
341 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
342 for (i = 0; g_cmdoptions[i].name; i++) {
343 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
344 if (argc - 1 < g_cmdoptions[i].param_count)
346 switch (g_cmdoptions[i].code) {
354 varset.xres = strtoul(argv[1], 0, 0);
355 varset.yres = strtoul(argv[2], 0, 0);
356 varset.xres_virtual = strtoul(argv[3], 0, 0);
357 varset.yres_virtual = strtoul(argv[4], 0, 0);
358 varset.bits_per_pixel = strtoul(argv[5], 0, 0);
361 varset.pixclock = strtoul(argv[1], 0, 0);
362 varset.left_margin = strtoul(argv[2], 0, 0);
363 varset.right_margin = strtoul(argv[3], 0, 0);
364 varset.upper_margin = strtoul(argv[4], 0, 0);
365 varset.lower_margin = strtoul(argv[5], 0, 0);
366 varset.hsync_len = strtoul(argv[6], 0, 0);
367 varset.vsync_len = strtoul(argv[7], 0, 0);
370 g_options |= OPT_CHANGE;
372 #ifdef CONFIG_FEATURE_FBSET_FANCY
374 varset.xres = strtoul(argv[1], 0, 0);
377 varset.yres = strtoul(argv[1], 0, 0);
380 varset.bits_per_pixel = strtoul(argv[1], 0, 0);
384 argc -= g_cmdoptions[i].param_count;
385 argv += g_cmdoptions[i].param_count;
389 if (!g_cmdoptions[i].name) {
392 g_options |= OPT_READMODE;
399 fh = bb_xopen(fbdev, O_RDONLY);
400 if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
401 bb_perror_msg_and_die("fbset(ioctl)");
402 if (g_options & OPT_READMODE) {
403 if (!readmode(&var, modefile, mode)) {
404 bb_error_msg("Unknown video mode `%s'", mode);
409 setmode(&var, &varset);
410 if (g_options & OPT_CHANGE)
411 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
412 bb_perror_msg_and_die("fbset(ioctl)");
414 /* Don't close the file, as exiting will take care of that */