1 /* vi: set sw=4 ts=4: */
3 * Mini fbset implementation for busybox
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This is a from-scratch implementation of fbset; but the de facto fbset
22 * implementation was a good reference. fbset (original) is released under
23 * the GPL, and is (c) 1995-1999 by:
24 * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
35 #include <sys/ioctl.h>
37 #define DEFAULTFBDEV "/dev/fb0"
38 #define DEFAULTFBMODE "/etc/fb.modes"
40 static const int OPT_CHANGE = (1 << 0);
41 static const int OPT_INFO = (1 << 1);
42 static const int OPT_READMODE = (1 << 2);
55 /* CMD_XCOMPAT = 10, */
60 #ifdef BB_FEATURE_FBSET_FANCY
84 static unsigned int g_options = 0;
86 /* Stuff stolen from the kernel's fb.h */
87 static const int FBIOGET_VSCREENINFO = 0x4600;
88 static const int FBIOPUT_VSCREENINFO = 0x4601;
89 #define __u32 unsigned int
91 __u32 offset; /* beginning of bitfield */
92 __u32 length; /* length of bitfield */
93 __u32 msb_right; /* != 0 : Most significant bit is */
96 struct fb_var_screeninfo {
97 __u32 xres; /* visible resolution */
99 __u32 xres_virtual; /* virtual resolution */
101 __u32 xoffset; /* offset from virtual to visible */
102 __u32 yoffset; /* resolution */
104 __u32 bits_per_pixel; /* guess what */
105 __u32 grayscale; /* != 0 Graylevels instead of colors */
107 struct fb_bitfield red; /* bitfield in fb mem if true color, */
108 struct fb_bitfield green; /* else only length is significant */
109 struct fb_bitfield blue;
110 struct fb_bitfield transp; /* transparency */
112 __u32 nonstd; /* != 0 Non standard pixel format */
114 __u32 activate; /* see FB_ACTIVATE_* */
116 __u32 height; /* height of picture in mm */
117 __u32 width; /* width of picture in mm */
119 __u32 accel_flags; /* acceleration flags (hints) */
121 /* Timing: All values in pixclocks, except pixclock (of course) */
122 __u32 pixclock; /* pixel clock in ps (pico seconds) */
123 __u32 left_margin; /* time from sync to picture */
124 __u32 right_margin; /* time from picture to sync */
125 __u32 upper_margin; /* time from sync to picture */
127 __u32 hsync_len; /* length of horizontal sync */
128 __u32 vsync_len; /* length of vertical sync */
129 __u32 sync; /* see FB_SYNC_* */
130 __u32 vmode; /* see FB_VMODE_* */
131 __u32 reserved[6]; /* Reserved for future compatibility */
135 struct cmdoptions_t {
137 unsigned char param_count;
141 "-h", 0, CMD_HELP}, {
145 "-i", 0, CMD_INFO}, {
146 "-g", 5, CMD_GEOMETRY}, {
147 "-t", 7, CMD_TIMING}, {
148 "-accel", 1, CMD_ACCEL}, {
149 "-hsync", 1, CMD_HSYNC}, {
150 "-vsync", 1, CMD_VSYNC}, {
151 "-laced", 1, CMD_LACED}, {
152 "-double", 1, CMD_DOUBLE}, {
153 "-help", 0, CMD_HELP}, {
154 "-n", 0, CMD_CHANGE}, {
155 #ifdef BB_FEATURE_FBSET_FANCY
156 "-help", 0, CMD_HELP}, {
157 "-all", 0, CMD_ALL}, {
158 "-xres", 1, CMD_XRES}, {
159 "-yres", 1, CMD_YRES}, {
160 "-vxres", 1, CMD_VXRES}, {
161 "-vyres", 1, CMD_VYRES}, {
162 "-depth", 1, CMD_DEPTH}, {
163 "-match", 0, CMD_MATCH}, {
164 "-geometry", 5, CMD_GEOMETRY}, {
165 "-pixclock", 1, CMD_PIXCLOCK}, {
166 "-left", 1, CMD_LEFT}, {
167 "-right", 1, CMD_RIGHT}, {
168 "-upper", 1, CMD_UPPER}, {
169 "-lower", 1, CMD_LOWER}, {
170 "-hslen", 1, CMD_HSLEN}, {
171 "-vslen", 1, CMD_VSLEN}, {
172 "-timings", 7, CMD_TIMING}, {
173 "-csync", 1, CMD_CSYNC}, {
174 "-gsync", 1, CMD_GSYNC}, {
175 "-extsync", 1, CMD_EXTSYNC}, {
176 "-bcast", 1, CMD_BCAST}, {
177 "-rgba", 1, CMD_RGBA}, {
178 "-step", 1, CMD_STEP}, {
179 "-move", 1, CMD_MOVE}, {
184 #ifdef BB_FEATURE_FBSET_READMODE
185 /* taken from linux/fb.h */
186 static const int FB_VMODE_INTERLACED = 1; /* interlaced */
187 static const int FB_VMODE_DOUBLE = 2; /* double scan */
188 static const int FB_SYNC_HOR_HIGH_ACT = 1; /* horizontal sync high active */
189 static const int FB_SYNC_VERT_HIGH_ACT = 2; /* vertical sync high active */
190 static const int FB_SYNC_EXT = 4; /* external sync */
191 static const int FB_SYNC_COMP_HIGH_ACT = 8; /* composite sync high active */
193 static int readmode(struct fb_var_screeninfo *base, const char *fn,
196 #ifdef BB_FEATURE_FBSET_READMODE
201 if ((f = fopen(fn, "r")) == NULL)
202 perror_msg_and_die("readmode(fopen)");
204 fgets(buf, sizeof(buf), f);
205 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
207 if ((p = strstr(buf, mode))) {
209 if (!isspace(*p) && (*p != 0) && (*p != '"')
210 && (*p != '\r') && (*p != '\n'))
211 continue; /* almost, but not quite */
213 fgets(buf, sizeof(buf), f);
215 if ((p = strstr(buf, "geometry "))) {
218 sscanf(p, "%d %d %d %d %d",
219 &(base->xres), &(base->yres),
220 &(base->xres_virtual), &(base->yres_virtual),
221 &(base->bits_per_pixel));
222 } else if ((p = strstr(buf, "timings "))) {
225 sscanf(p, "%d %d %d %d %d %d %d",
227 &(base->left_margin), &(base->right_margin),
228 &(base->upper_margin), &(base->lower_margin),
229 &(base->hsync_len), &(base->vsync_len));
230 } else if ((p = strstr(buf, "laced "))) {
233 if (strstr(buf, "false")) {
234 base->vmode &= ~FB_VMODE_INTERLACED;
236 base->vmode |= FB_VMODE_INTERLACED;
238 } else if ((p = strstr(buf, "double "))) {
241 if (strstr(buf, "false")) {
242 base->vmode &= ~FB_VMODE_DOUBLE;
244 base->vmode |= FB_VMODE_DOUBLE;
246 } else if ((p = strstr(buf, "vsync "))) {
249 if (strstr(buf, "low")) {
250 base->sync &= ~FB_SYNC_VERT_HIGH_ACT;
252 base->sync |= FB_SYNC_VERT_HIGH_ACT;
254 } else if ((p = strstr(buf, "hsync "))) {
257 if (strstr(buf, "low")) {
258 base->sync &= ~FB_SYNC_HOR_HIGH_ACT;
260 base->sync |= FB_SYNC_HOR_HIGH_ACT;
262 } else if ((p = strstr(buf, "csync "))) {
265 if (strstr(buf, "low")) {
266 base->sync &= ~FB_SYNC_COMP_HIGH_ACT;
268 base->sync |= FB_SYNC_COMP_HIGH_ACT;
270 } else if ((p = strstr(buf, "extsync "))) {
273 if (strstr(buf, "false")) {
274 base->sync &= ~FB_SYNC_EXT;
276 base->sync |= FB_SYNC_EXT;
280 if (strstr(buf, "endmode"))
287 error_msg( "mode reading not compiled in\n");
292 static void setmode(struct fb_var_screeninfo *base,
293 struct fb_var_screeninfo *set)
295 if ((int) set->xres > 0)
296 base->xres = set->xres;
297 if ((int) set->yres > 0)
298 base->yres = set->yres;
299 if ((int) set->xres_virtual > 0)
300 base->xres_virtual = set->xres_virtual;
301 if ((int) set->yres_virtual > 0)
302 base->yres_virtual = set->yres_virtual;
303 if ((int) set->bits_per_pixel > 0)
304 base->bits_per_pixel = set->bits_per_pixel;
307 static void showmode(struct fb_var_screeninfo *v)
309 double drate = 0, hrate = 0, vrate = 0;
312 drate = 1e12 / v->pixclock;
314 drate / (v->left_margin + v->xres + v->right_margin +
317 hrate / (v->upper_margin + v->yres + v->lower_margin +
320 printf("\nmode \"%ux%u-%u\"\n", v->xres, v->yres, (int) (vrate + 0.5));
321 #ifdef BB_FEATURE_FBSET_FANCY
322 printf("\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n", drate / 1e6,
325 printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
326 v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
327 printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
328 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
330 printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
331 printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length,
332 v->red.offset, v->green.length, v->green.offset, v->blue.length,
333 v->blue.offset, v->transp.length, v->transp.offset);
334 printf("endmode\n\n");
337 static void fbset_usage(void)
339 #ifndef BB_FEATURE_TRIVIAL_HELP
344 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
347 fprintf(stderr, "Usage: fbset [options] [mode]\n");
348 #ifndef BB_FEATURE_TRIVIAL_HELP
349 fprintf(stderr, "\nShows and modifies frame buffer device settings\n\n");
350 fprintf(stderr, "The following options are recognized:\n");
351 for (i = 0; g_cmdoptions[i].name; i++)
352 fprintf(stderr, "\t%s\n", g_cmdoptions[i].name);
358 int main(int argc, char **argv)
360 extern int fbset_main(int argc, char **argv)
363 struct fb_var_screeninfo var, varset;
365 char *fbdev = DEFAULTFBDEV;
366 char *modefile = DEFAULTFBMODE;
367 char *thisarg, *mode = NULL;
369 memset(&varset, 0xFF, sizeof(varset));
371 /* parse cmd args.... why do they have to make things so difficult? */
374 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
375 for (i = 0; g_cmdoptions[i].name; i++) {
376 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
377 if (argc - 1 < g_cmdoptions[i].param_count)
379 switch (g_cmdoptions[i].code) {
389 varset.xres = strtoul(argv[1], 0, 0);
390 varset.yres = strtoul(argv[2], 0, 0);
391 varset.xres_virtual = strtoul(argv[3], 0, 0);
392 varset.yres_virtual = strtoul(argv[4], 0, 0);
393 varset.bits_per_pixel = strtoul(argv[5], 0, 0);
396 varset.pixclock = strtoul(argv[1], 0, 0);
397 varset.left_margin = strtoul(argv[2], 0, 0);
398 varset.right_margin = strtoul(argv[3], 0, 0);
399 varset.upper_margin = strtoul(argv[4], 0, 0);
400 varset.lower_margin = strtoul(argv[5], 0, 0);
401 varset.hsync_len = strtoul(argv[6], 0, 0);
402 varset.vsync_len = strtoul(argv[7], 0, 0);
405 g_options |= OPT_CHANGE;
407 #ifdef BB_FEATURE_FBSET_FANCY
409 varset.xres = strtoul(argv[1], 0, 0);
412 varset.yres = strtoul(argv[1], 0, 0);
416 argc -= g_cmdoptions[i].param_count;
417 argv += g_cmdoptions[i].param_count;
421 if (!g_cmdoptions[i].name) {
424 g_options |= OPT_READMODE;
431 if ((fh = open(fbdev, O_RDONLY)) < 0)
432 perror_msg_and_die("fbset(open)");
433 if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
434 perror_msg_and_die("fbset(ioctl)");
435 if (g_options & OPT_READMODE) {
436 if (!readmode(&var, modefile, mode)) {
437 error_msg("Unknown video mode `%s'\n", mode);
442 setmode(&var, &varset);
443 if (g_options & OPT_CHANGE)
444 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
445 perror_msg_and_die("fbset(ioctl)");
447 /* Don't close the file, as exiting will take care of that */