blob: 32325a7cfbe880155533bbd55a708054f11e745d [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 *
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.
11 *
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.
16 *
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
20 *
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)
25 */
26
Eric Andersen3570a342000-09-25 21:45:58 +000027#include "busybox.h"
Erik Andersen1c5b2581999-12-16 20:59:36 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <unistd.h>
31#include <fcntl.h>
32#include <errno.h>
33#include <ctype.h>
34#include <sys/ioctl.h>
Erik Andersen1c5b2581999-12-16 20:59:36 +000035
36#define PERROR(ctx) do { perror(ctx); exit(1); } while(0)
37
38#define DEFAULTFBDEV "/dev/fb0"
39#define DEFAULTFBMODE "/etc/fb.modes"
40
41#define OPT_CHANGE 1
42#define OPT_INFO (1 << 1)
43#define OPT_READMODE (1 << 2)
44
45#define CMD_HELP 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000046#define CMD_FB 1
Erik Andersen1c5b2581999-12-16 20:59:36 +000047#define CMD_DB 2
48#define CMD_GEOMETRY 3
49#define CMD_TIMING 4
50#define CMD_ACCEL 5
51#define CMD_HSYNC 6
52#define CMD_VSYNC 7
53#define CMD_LACED 8
54#define CMD_DOUBLE 9
55/* #define CMD_XCOMPAT 10 */
56#define CMD_ALL 11
57#define CMD_INFO 12
Eric Andersenc8c10c52000-07-06 17:23:23 +000058#define CMD_CHANGE 13
Erik Andersen1c5b2581999-12-16 20:59:36 +000059
Erik Andersen298854f2000-03-23 01:09:18 +000060#ifdef BB_FEATURE_FBSET_FANCY
Eric Andersenc8c10c52000-07-06 17:23:23 +000061#define CMD_XRES 100
62#define CMD_YRES 101
63#define CMD_VXRES 102
64#define CMD_VYRES 103
65#define CMD_DEPTH 104
66#define CMD_MATCH 105
67#define CMD_PIXCLOCK 106
68#define CMD_LEFT 107
69#define CMD_RIGHT 108
70#define CMD_UPPER 109
71#define CMD_LOWER 110
72#define CMD_HSLEN 111
73#define CMD_VSLEN 112
74#define CMD_CSYNC 113
75#define CMD_GSYNC 114
76#define CMD_EXTSYNC 115
77#define CMD_BCAST 116
78#define CMD_RGBA 117
79#define CMD_STEP 118
80#define CMD_MOVE 119
Erik Andersen1c5b2581999-12-16 20:59:36 +000081#endif
82
83static unsigned int g_options = 0;
84
Eric Andersenc873d612000-09-21 04:09:58 +000085/* Stuff stolen from the kernel's fb.h */
86#define FBIOGET_VSCREENINFO 0x4600
87#define FBIOPUT_VSCREENINFO 0x4601
88#define __u32 unsigned int
89struct fb_bitfield {
90 __u32 offset; /* beginning of bitfield */
91 __u32 length; /* length of bitfield */
92 __u32 msb_right; /* != 0 : Most significant bit is */
93 /* right */
94};
95struct fb_var_screeninfo {
96 __u32 xres; /* visible resolution */
97 __u32 yres;
98 __u32 xres_virtual; /* virtual resolution */
99 __u32 yres_virtual;
100 __u32 xoffset; /* offset from virtual to visible */
101 __u32 yoffset; /* resolution */
102
103 __u32 bits_per_pixel; /* guess what */
104 __u32 grayscale; /* != 0 Graylevels instead of colors */
105
106 struct fb_bitfield red; /* bitfield in fb mem if true color, */
107 struct fb_bitfield green; /* else only length is significant */
108 struct fb_bitfield blue;
109 struct fb_bitfield transp; /* transparency */
110
111 __u32 nonstd; /* != 0 Non standard pixel format */
112
113 __u32 activate; /* see FB_ACTIVATE_* */
114
115 __u32 height; /* height of picture in mm */
116 __u32 width; /* width of picture in mm */
117
118 __u32 accel_flags; /* acceleration flags (hints) */
119
120 /* Timing: All values in pixclocks, except pixclock (of course) */
121 __u32 pixclock; /* pixel clock in ps (pico seconds) */
122 __u32 left_margin; /* time from sync to picture */
123 __u32 right_margin; /* time from picture to sync */
124 __u32 upper_margin; /* time from sync to picture */
125 __u32 lower_margin;
126 __u32 hsync_len; /* length of horizontal sync */
127 __u32 vsync_len; /* length of vertical sync */
128 __u32 sync; /* see FB_SYNC_* */
129 __u32 vmode; /* see FB_VMODE_* */
130 __u32 reserved[6]; /* Reserved for future compatibility */
131};
132
133
Erik Andersen1c5b2581999-12-16 20:59:36 +0000134struct cmdoptions_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 char *name;
136 unsigned char param_count;
137 unsigned char code;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000138} g_cmdoptions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000139 {
140 "-h", 0, CMD_HELP}, {
141 "-fb", 1, CMD_FB}, {
142 "-db", 1, CMD_DB}, {
143 "-a", 0, CMD_ALL}, {
144 "-i", 0, CMD_INFO}, {
145 "-g", 5, CMD_GEOMETRY}, {
146 "-t", 7, CMD_TIMING}, {
147 "-accel", 1, CMD_ACCEL}, {
148 "-hsync", 1, CMD_HSYNC}, {
149 "-vsync", 1, CMD_VSYNC}, {
150 "-laced", 1, CMD_LACED}, {
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000151 "-double", 1, CMD_DOUBLE}, {
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000152 "-help", 0, CMD_HELP}, {
Eric Andersenc8c10c52000-07-06 17:23:23 +0000153 "-n", 0, CMD_CHANGE}, {
Erik Andersen298854f2000-03-23 01:09:18 +0000154#ifdef BB_FEATURE_FBSET_FANCY
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000155 "-help", 0, CMD_HELP}, {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 "-all", 0, CMD_ALL}, {
157 "-xres", 1, CMD_XRES}, {
158 "-yres", 1, CMD_YRES}, {
159 "-vxres", 1, CMD_VXRES}, {
160 "-vyres", 1, CMD_VYRES}, {
161 "-depth", 1, CMD_DEPTH}, {
162 "-match", 0, CMD_MATCH}, {
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000163 "-geometry", 5, CMD_GEOMETRY}, {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000164 "-pixclock", 1, CMD_PIXCLOCK}, {
165 "-left", 1, CMD_LEFT}, {
166 "-right", 1, CMD_RIGHT}, {
167 "-upper", 1, CMD_UPPER}, {
168 "-lower", 1, CMD_LOWER}, {
169 "-hslen", 1, CMD_HSLEN}, {
170 "-vslen", 1, CMD_VSLEN}, {
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000171 "-timings", 7, CMD_TIMING}, {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172 "-csync", 1, CMD_CSYNC}, {
173 "-gsync", 1, CMD_GSYNC}, {
174 "-extsync", 1, CMD_EXTSYNC}, {
175 "-bcast", 1, CMD_BCAST}, {
176 "-rgba", 1, CMD_RGBA}, {
177 "-step", 1, CMD_STEP}, {
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000178 "-move", 1, CMD_MOVE}, {
Erik Andersen1c5b2581999-12-16 20:59:36 +0000179#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000180 0, 0, 0}
Erik Andersen1c5b2581999-12-16 20:59:36 +0000181};
182
183static int readmode(struct fb_var_screeninfo *base, const char *fn,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000184 const char *mode)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000185{
Eric Andersen03f4c272000-07-06 23:10:29 +0000186#ifdef BB_FEATURE_FBSET_READMODE
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 FILE *f;
188 char buf[256];
189 char *p = buf;
190
191 if ((f = fopen(fn, "r")) == NULL)
192 PERROR("readmode(fopen)");
193 while (!feof(f)) {
194 fgets(buf, sizeof(buf), f);
195 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
196 p += 5;
197 if ((p = strstr(buf, mode))) {
198 p += strlen(mode);
199 if (!isspace(*p) && (*p != 0) && (*p != '"')
200 && (*p != '\r') && (*p != '\n'))
201 continue; /* almost, but not quite */
202 while (!feof(f)) {
203 fgets(buf, sizeof(buf), f);
Eric Andersen6f96e672000-07-12 23:01:04 +0000204
205 if ((p = strstr(buf, "geometry "))) {
206 p += 9;
207
208 sscanf(p, "%d %d %d %d %d",
209 &(base->xres), &(base->yres),
210 &(base->xres_virtual), &(base->yres_virtual),
211 &(base->bits_per_pixel));
212 } else if ((p = strstr(buf, "timings "))) {
213 p += 8;
214
215 sscanf(p, "%d %d %d %d %d %d %d",
216 &(base->pixclock),
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 "))) {
221 p += 6;
222
223 if (strstr(buf, "false")) {
224 base->vmode &= ~FB_VMODE_INTERLACED;
225 } else {
226 base->vmode |= FB_VMODE_INTERLACED;
227 }
228 } else if ((p = strstr(buf, "double "))) {
229 p += 7;
230
231 if (strstr(buf, "false")) {
232 base->vmode &= ~FB_VMODE_DOUBLE;
233 } else {
234 base->vmode |= FB_VMODE_DOUBLE;
235 }
236 } else if ((p = strstr(buf, "vsync "))) {
237 p += 6;
238
239 if (strstr(buf, "low")) {
240 base->sync &= ~FB_SYNC_VERT_HIGH_ACT;
241 } else {
242 base->sync |= FB_SYNC_VERT_HIGH_ACT;
243 }
244 } else if ((p = strstr(buf, "hsync "))) {
245 p += 6;
246
247 if (strstr(buf, "low")) {
248 base->sync &= ~FB_SYNC_HOR_HIGH_ACT;
249 } else {
250 base->sync |= FB_SYNC_HOR_HIGH_ACT;
251 }
252 } else if ((p = strstr(buf, "csync "))) {
253 p += 6;
254
255 if (strstr(buf, "low")) {
256 base->sync &= ~FB_SYNC_COMP_HIGH_ACT;
257 } else {
258 base->sync |= FB_SYNC_COMP_HIGH_ACT;
259 }
260 } else if ((p = strstr(buf, "extsync "))) {
261 p += 8;
262
263 if (strstr(buf, "false")) {
264 base->sync &= ~FB_SYNC_EXT;
265 } else {
266 base->sync |= FB_SYNC_EXT;
267 }
268 }
269
270 if (strstr(buf, "endmode"))
Erik Andersene49d5ec2000-02-08 19:58:47 +0000271 return 1;
272 }
273 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000274 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000275 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000276#else
Erik Andersen298854f2000-03-23 01:09:18 +0000277 errorMsg( "mode reading not compiled in\n");
Erik Andersen1c5b2581999-12-16 20:59:36 +0000278#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000279 return 0;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000280}
281
Erik Andersene49d5ec2000-02-08 19:58:47 +0000282static void setmode(struct fb_var_screeninfo *base,
283 struct fb_var_screeninfo *set)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000284{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000285 if ((int) set->xres > 0)
286 base->xres = set->xres;
287 if ((int) set->yres > 0)
288 base->yres = set->yres;
289 if ((int) set->xres_virtual > 0)
290 base->xres_virtual = set->xres_virtual;
291 if ((int) set->yres_virtual > 0)
292 base->yres_virtual = set->yres_virtual;
293 if ((int) set->bits_per_pixel > 0)
294 base->bits_per_pixel = set->bits_per_pixel;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000295}
296
297static void showmode(struct fb_var_screeninfo *v)
298{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 double drate = 0, hrate = 0, vrate = 0;
300
301 if (v->pixclock) {
302 drate = 1e12 / v->pixclock;
303 hrate =
304 drate / (v->left_margin + v->xres + v->right_margin +
305 v->hsync_len);
306 vrate =
307 hrate / (v->upper_margin + v->yres + v->lower_margin +
308 v->vsync_len);
309 }
310 printf("\nmode \"%ux%u-%u\"\n", v->xres, v->yres, (int) (vrate + 0.5));
Erik Andersen298854f2000-03-23 01:09:18 +0000311#ifdef BB_FEATURE_FBSET_FANCY
Erik Andersene49d5ec2000-02-08 19:58:47 +0000312 printf("\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n", drate / 1e6,
313 hrate / 1e3, vrate);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000314#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
316 v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
318 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
319 v->vsync_len);
320 printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
Erik Andersene49d5ec2000-02-08 19:58:47 +0000321 printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length,
322 v->red.offset, v->green.length, v->green.offset, v->blue.length,
323 v->blue.offset, v->transp.length, v->transp.offset);
Erik Andersen9cf3bfa2000-04-13 18:49:43 +0000324 printf("endmode\n\n");
Erik Andersen1c5b2581999-12-16 20:59:36 +0000325}
326
327static void fbset_usage(void)
328{
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000329#ifndef BB_FEATURE_TRIVIAL_HELP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330 int i;
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000331#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000332
Erik Andersen1c5b2581999-12-16 20:59:36 +0000333#ifndef STANDALONE
Erik Andersene49d5ec2000-02-08 19:58:47 +0000334 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
335 BB_VER, BB_BT);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000336#endif
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000337 fprintf(stderr, "Usage: fbset [options] [mode]\n");
338#ifndef BB_FEATURE_TRIVIAL_HELP
339 fprintf(stderr, "\nShows and modifies frame buffer device settings\n\n");
Erik Andersen61677fe2000-04-13 01:18:56 +0000340 fprintf(stderr, "The following options are recognized:\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000341 for (i = 0; g_cmdoptions[i].name; i++)
Erik Andersen61677fe2000-04-13 01:18:56 +0000342 fprintf(stderr, "\t%s\n", g_cmdoptions[i].name);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000343#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000344 exit(-1);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000345}
346
347#ifdef STANDALONE
348int main(int argc, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000349#else
Erik Andersen1c5b2581999-12-16 20:59:36 +0000350extern int fbset_main(int argc, char **argv)
351#endif
352{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000353 struct fb_var_screeninfo var, varset;
354 int fh, i;
355 char *fbdev = DEFAULTFBDEV;
356 char *modefile = DEFAULTFBMODE;
357 char *thisarg, *mode = NULL;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000358
Erik Andersene49d5ec2000-02-08 19:58:47 +0000359 memset(&varset, 0xFF, sizeof(varset));
360
361 /* parse cmd args.... why do they have to make things so difficult? */
362 argv++;
363 argc--;
364 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
365 for (i = 0; g_cmdoptions[i].name; i++) {
366 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
367 if (argc - 1 < g_cmdoptions[i].param_count)
368 fbset_usage();
369 switch (g_cmdoptions[i].code) {
370 case CMD_HELP:
371 fbset_usage();
372 case CMD_FB:
373 fbdev = argv[1];
374 break;
375 case CMD_DB:
376 modefile = argv[1];
377 break;
378 case CMD_GEOMETRY:
379 varset.xres = strtoul(argv[1], 0, 0);
380 varset.yres = strtoul(argv[2], 0, 0);
381 varset.xres_virtual = strtoul(argv[3], 0, 0);
382 varset.yres_virtual = strtoul(argv[4], 0, 0);
383 varset.bits_per_pixel = strtoul(argv[5], 0, 0);
384 break;
385 case CMD_TIMING:
386 varset.pixclock = strtoul(argv[1], 0, 0);
387 varset.left_margin = strtoul(argv[2], 0, 0);
388 varset.right_margin = strtoul(argv[3], 0, 0);
389 varset.upper_margin = strtoul(argv[4], 0, 0);
390 varset.lower_margin = strtoul(argv[5], 0, 0);
391 varset.hsync_len = strtoul(argv[6], 0, 0);
392 varset.vsync_len = strtoul(argv[7], 0, 0);
393 break;
Eric Andersenc8c10c52000-07-06 17:23:23 +0000394 case CMD_CHANGE:
395 g_options |= OPT_CHANGE;
396 break;
Erik Andersen298854f2000-03-23 01:09:18 +0000397#ifdef BB_FEATURE_FBSET_FANCY
Erik Andersene49d5ec2000-02-08 19:58:47 +0000398 case CMD_XRES:
399 varset.xres = strtoul(argv[1], 0, 0);
400 break;
401 case CMD_YRES:
402 varset.yres = strtoul(argv[1], 0, 0);
403 break;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000404#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000405 }
406 argc -= g_cmdoptions[i].param_count;
407 argv += g_cmdoptions[i].param_count;
408 break;
409 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000410 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000411 if (!g_cmdoptions[i].name) {
412 if (argc == 1) {
413 mode = *argv;
414 g_options |= OPT_READMODE;
415 } else {
416 fbset_usage();
417 }
418 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000419 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000420
Erik Andersene49d5ec2000-02-08 19:58:47 +0000421 if ((fh = open(fbdev, O_RDONLY)) < 0)
422 PERROR("fbset(open)");
423 if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
424 PERROR("fbset(ioctl)");
425 if (g_options & OPT_READMODE) {
426 if (!readmode(&var, modefile, mode)) {
Matt Kraaid537a952000-07-14 01:51:25 +0000427 errorMsg("Unknown video mode `%s'\n", mode);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428 exit(1);
429 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000430 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000431
Erik Andersene49d5ec2000-02-08 19:58:47 +0000432 setmode(&var, &varset);
433 if (g_options & OPT_CHANGE)
434 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
435 PERROR("fbset(ioctl)");
436 showmode(&var);
Erik Andersen298854f2000-03-23 01:09:18 +0000437 /* Don't close the file, as exiting will take care of that */
438 /* close(fh); */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000439
Eric Andersenb6106152000-06-19 17:25:40 +0000440 return (TRUE);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000441}