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)
34 #include <sys/ioctl.h>
36 #include <linux/version.h>
38 #define PERROR(ctx) do { perror(ctx); exit(1); } while(0)
40 #ifndef KERNEL_VERSION
41 #define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
44 #define DEFAULTFBDEV "/dev/fb0"
45 #define DEFAULTFBMODE "/etc/fb.modes"
48 #define OPT_INFO (1 << 1)
49 #define OPT_READMODE (1 << 2)
54 #define CMD_GEOMETRY 3
61 /* #define CMD_XCOMPAT 10 */
66 #ifdef BB_FEATURE_FBSET_FANCY
73 #define CMD_PIXCLOCK 106
82 #define CMD_EXTSYNC 115
89 static unsigned int g_options = 0;
93 unsigned char param_count;
101 "-i", 0, CMD_INFO}, {
102 "-g", 5, CMD_GEOMETRY}, {
103 "-t", 7, CMD_TIMING}, {
104 "-accel", 1, CMD_ACCEL}, {
105 "-hsync", 1, CMD_HSYNC}, {
106 "-vsync", 1, CMD_VSYNC}, {
107 "-laced", 1, CMD_LACED}, {
108 "-double", 1, CMD_DOUBLE}, {
109 "-help", 0, CMD_HELP}, {
110 "-n", 0, CMD_CHANGE}, {
111 #ifdef BB_FEATURE_FBSET_FANCY
112 "-help", 0, CMD_HELP}, {
113 "-all", 0, CMD_ALL}, {
114 "-xres", 1, CMD_XRES}, {
115 "-yres", 1, CMD_YRES}, {
116 "-vxres", 1, CMD_VXRES}, {
117 "-vyres", 1, CMD_VYRES}, {
118 "-depth", 1, CMD_DEPTH}, {
119 "-match", 0, CMD_MATCH}, {
120 "-geometry", 5, CMD_GEOMETRY}, {
121 "-pixclock", 1, CMD_PIXCLOCK}, {
122 "-left", 1, CMD_LEFT}, {
123 "-right", 1, CMD_RIGHT}, {
124 "-upper", 1, CMD_UPPER}, {
125 "-lower", 1, CMD_LOWER}, {
126 "-hslen", 1, CMD_HSLEN}, {
127 "-vslen", 1, CMD_VSLEN}, {
128 "-timings", 7, CMD_TIMING}, {
129 "-csync", 1, CMD_CSYNC}, {
130 "-gsync", 1, CMD_GSYNC}, {
131 "-extsync", 1, CMD_EXTSYNC}, {
132 "-bcast", 1, CMD_BCAST}, {
133 "-rgba", 1, CMD_RGBA}, {
134 "-step", 1, CMD_STEP}, {
135 "-move", 1, CMD_MOVE}, {
140 static int readmode(struct fb_var_screeninfo *base, const char *fn,
143 #ifdef BB_FEATURE_FBSET_READMODE
148 if ((f = fopen(fn, "r")) == NULL)
149 PERROR("readmode(fopen)");
151 fgets(buf, sizeof(buf), f);
152 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
154 if ((p = strstr(buf, mode))) {
156 if (!isspace(*p) && (*p != 0) && (*p != '"')
157 && (*p != '\r') && (*p != '\n'))
158 continue; /* almost, but not quite */
160 fgets(buf, sizeof(buf), f);
162 if ((p = strstr(buf, "geometry "))) {
165 sscanf(p, "%d %d %d %d %d",
166 &(base->xres), &(base->yres),
167 &(base->xres_virtual), &(base->yres_virtual),
168 &(base->bits_per_pixel));
169 } else if ((p = strstr(buf, "timings "))) {
172 sscanf(p, "%d %d %d %d %d %d %d",
174 &(base->left_margin), &(base->right_margin),
175 &(base->upper_margin), &(base->lower_margin),
176 &(base->hsync_len), &(base->vsync_len));
177 } else if ((p = strstr(buf, "laced "))) {
180 if (strstr(buf, "false")) {
181 base->vmode &= ~FB_VMODE_INTERLACED;
183 base->vmode |= FB_VMODE_INTERLACED;
185 } else if ((p = strstr(buf, "double "))) {
188 if (strstr(buf, "false")) {
189 base->vmode &= ~FB_VMODE_DOUBLE;
191 base->vmode |= FB_VMODE_DOUBLE;
193 } else if ((p = strstr(buf, "vsync "))) {
196 if (strstr(buf, "low")) {
197 base->sync &= ~FB_SYNC_VERT_HIGH_ACT;
199 base->sync |= FB_SYNC_VERT_HIGH_ACT;
201 } else if ((p = strstr(buf, "hsync "))) {
204 if (strstr(buf, "low")) {
205 base->sync &= ~FB_SYNC_HOR_HIGH_ACT;
207 base->sync |= FB_SYNC_HOR_HIGH_ACT;
209 } else if ((p = strstr(buf, "csync "))) {
212 if (strstr(buf, "low")) {
213 base->sync &= ~FB_SYNC_COMP_HIGH_ACT;
215 base->sync |= FB_SYNC_COMP_HIGH_ACT;
217 } else if ((p = strstr(buf, "extsync "))) {
220 if (strstr(buf, "false")) {
221 base->sync &= ~FB_SYNC_EXT;
223 base->sync |= FB_SYNC_EXT;
227 if (strstr(buf, "endmode"))
234 errorMsg( "mode reading not compiled in\n");
239 static void setmode(struct fb_var_screeninfo *base,
240 struct fb_var_screeninfo *set)
242 if ((int) set->xres > 0)
243 base->xres = set->xres;
244 if ((int) set->yres > 0)
245 base->yres = set->yres;
246 if ((int) set->xres_virtual > 0)
247 base->xres_virtual = set->xres_virtual;
248 if ((int) set->yres_virtual > 0)
249 base->yres_virtual = set->yres_virtual;
250 if ((int) set->bits_per_pixel > 0)
251 base->bits_per_pixel = set->bits_per_pixel;
254 static void showmode(struct fb_var_screeninfo *v)
256 double drate = 0, hrate = 0, vrate = 0;
259 drate = 1e12 / v->pixclock;
261 drate / (v->left_margin + v->xres + v->right_margin +
264 hrate / (v->upper_margin + v->yres + v->lower_margin +
267 printf("\nmode \"%ux%u-%u\"\n", v->xres, v->yres, (int) (vrate + 0.5));
268 #ifdef BB_FEATURE_FBSET_FANCY
269 printf("\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n", drate / 1e6,
272 printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
273 v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
274 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
275 printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
276 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
278 printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
280 printf("\ttimings %lu %lu %lu %lu %lu %lu %lu\n", v->pixclock,
281 v->left_margin, v->right_margin, v->upper_margin,
282 v->lower_margin, v->hsync_len, v->vsync_len);
284 printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length,
285 v->red.offset, v->green.length, v->green.offset, v->blue.length,
286 v->blue.offset, v->transp.length, v->transp.offset);
287 printf("endmode\n\n");
290 static void fbset_usage(void)
292 #ifndef BB_FEATURE_TRIVIAL_HELP
297 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
300 fprintf(stderr, "Usage: fbset [options] [mode]\n");
301 #ifndef BB_FEATURE_TRIVIAL_HELP
302 fprintf(stderr, "\nShows and modifies frame buffer device settings\n\n");
303 fprintf(stderr, "The following options are recognized:\n");
304 for (i = 0; g_cmdoptions[i].name; i++)
305 fprintf(stderr, "\t%s\n", g_cmdoptions[i].name);
311 int main(int argc, char **argv)
313 extern int fbset_main(int argc, char **argv)
316 struct fb_var_screeninfo var, varset;
318 char *fbdev = DEFAULTFBDEV;
319 char *modefile = DEFAULTFBMODE;
320 char *thisarg, *mode = NULL;
322 memset(&varset, 0xFF, sizeof(varset));
324 /* parse cmd args.... why do they have to make things so difficult? */
327 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
328 for (i = 0; g_cmdoptions[i].name; i++) {
329 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
330 if (argc - 1 < g_cmdoptions[i].param_count)
332 switch (g_cmdoptions[i].code) {
342 varset.xres = strtoul(argv[1], 0, 0);
343 varset.yres = strtoul(argv[2], 0, 0);
344 varset.xres_virtual = strtoul(argv[3], 0, 0);
345 varset.yres_virtual = strtoul(argv[4], 0, 0);
346 varset.bits_per_pixel = strtoul(argv[5], 0, 0);
349 varset.pixclock = strtoul(argv[1], 0, 0);
350 varset.left_margin = strtoul(argv[2], 0, 0);
351 varset.right_margin = strtoul(argv[3], 0, 0);
352 varset.upper_margin = strtoul(argv[4], 0, 0);
353 varset.lower_margin = strtoul(argv[5], 0, 0);
354 varset.hsync_len = strtoul(argv[6], 0, 0);
355 varset.vsync_len = strtoul(argv[7], 0, 0);
358 g_options |= OPT_CHANGE;
360 #ifdef BB_FEATURE_FBSET_FANCY
362 varset.xres = strtoul(argv[1], 0, 0);
365 varset.yres = strtoul(argv[1], 0, 0);
369 argc -= g_cmdoptions[i].param_count;
370 argv += g_cmdoptions[i].param_count;
374 if (!g_cmdoptions[i].name) {
377 g_options |= OPT_READMODE;
384 if ((fh = open(fbdev, O_RDONLY)) < 0)
385 PERROR("fbset(open)");
386 if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
387 PERROR("fbset(ioctl)");
388 if (g_options & OPT_READMODE) {
389 if (!readmode(&var, modefile, mode)) {
390 errorMsg("Unknown video mode `%s'\n", mode);
395 setmode(&var, &varset);
396 if (g_options & OPT_CHANGE)
397 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
398 PERROR("fbset(ioctl)");
400 /* Don't close the file, as exiting will take care of that */