blob: c29145e5148a341ad26d6757da562785d9b084be [file] [log] [blame]
Erik Andersen1c5b2581999-12-16 20:59:36 +00001/*
2 * Mini fbset implementation for busybox
3 *
4 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * This is a from-scratch implementation of fbset; but the de facto fbset
21 * implementation was a good reference. fbset (original) is released under
22 * the GPL, and is (c) 1995-1999 by:
23 * Geert Uytterhoeven (Geert.Uytterhoeven@cs.kuleuven.ac.be)
24 */
25
26#include "internal.h"
27#include <stdio.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <fcntl.h>
31#include <errno.h>
32#include <ctype.h>
33#include <sys/ioctl.h>
34#include <linux/fb.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000035#include <linux/version.h>
Erik Andersen1c5b2581999-12-16 20:59:36 +000036
37#define PERROR(ctx) do { perror(ctx); exit(1); } while(0)
38
Erik Andersen4d1d0111999-12-17 18:44:15 +000039#ifndef KERNEL_VERSION
40#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
41#endif
42
Erik Andersen1c5b2581999-12-16 20:59:36 +000043#define DEFAULTFBDEV "/dev/fb0"
44#define DEFAULTFBMODE "/etc/fb.modes"
45
46#define OPT_CHANGE 1
47#define OPT_INFO (1 << 1)
48#define OPT_READMODE (1 << 2)
49
50#define CMD_HELP 0
51#define CMD_FB 1
52#define CMD_DB 2
53#define CMD_GEOMETRY 3
54#define CMD_TIMING 4
55#define CMD_ACCEL 5
56#define CMD_HSYNC 6
57#define CMD_VSYNC 7
58#define CMD_LACED 8
59#define CMD_DOUBLE 9
60/* #define CMD_XCOMPAT 10 */
61#define CMD_ALL 11
62#define CMD_INFO 12
63
64#ifdef BB_FBSET_FANCY
65#define CMD_XRES 13
66#define CMD_YRES 14
67#define CMD_VXRES 15
68#define CMD_VYRES 16
69#define CMD_DEPTH 17
70#define CMD_MATCH 18
71#define CMD_PIXCLOCK 19
72#define CMD_LEFT 20
73#define CMD_RIGHT 21
74#define CMD_UPPER 22
75#define CMD_LOWER 23
76#define CMD_HSLEN 24
77#define CMD_VSLEN 25
78#define CMD_CSYNC 26
79#define CMD_GSYNC 27
80#define CMD_EXTSYNC 28
81#define CMD_BCAST 29
82#define CMD_RGBA 30
83#define CMD_STEP 31
84#define CMD_MOVE 32
85#endif
86
87static unsigned int g_options = 0;
88
89struct cmdoptions_t {
90 char *name;
91 unsigned char param_count;
92 unsigned char code;
93} g_cmdoptions[] = {
94 { "-h", 0, CMD_HELP },
95 { "-fb", 1, CMD_FB },
96 { "-db", 1, CMD_DB },
97 { "-a", 0, CMD_ALL },
98 { "-i", 0, CMD_INFO },
99 { "-g", 5, CMD_GEOMETRY },
100 { "-t", 7, CMD_TIMING },
101 { "-accel", 1, CMD_ACCEL },
102 { "-hsync", 1, CMD_HSYNC },
103 { "-vsync", 1, CMD_VSYNC },
104 { "-laced", 1, CMD_LACED },
105 { "-double", 1, CMD_DOUBLE },
106
107#ifdef BB_FBSET_FANCY
108 { "--help", 0, CMD_HELP },
109 { "-all", 0, CMD_ALL },
110 { "-xres", 1, CMD_XRES },
111 { "-yres", 1, CMD_YRES },
112 { "-vxres", 1, CMD_VXRES },
113 { "-vyres", 1, CMD_VYRES },
114 { "-depth", 1, CMD_DEPTH },
115 { "-match", 0, CMD_MATCH },
116 { "--geometry", 5, CMD_GEOMETRY },
117
118 { "-pixclock", 1, CMD_PIXCLOCK },
119 { "-left", 1, CMD_LEFT },
120 { "-right", 1, CMD_RIGHT },
121 { "-upper", 1, CMD_UPPER },
122 { "-lower", 1, CMD_LOWER },
123 { "-hslen", 1, CMD_HSLEN },
124 { "-vslen", 1, CMD_VSLEN },
125 { "--timings", 7, CMD_TIMING },
126
127 { "-csync", 1, CMD_CSYNC },
128 { "-gsync", 1, CMD_GSYNC },
129 { "-extsync", 1, CMD_EXTSYNC },
130 { "-bcast", 1, CMD_BCAST },
131 { "-rgba", 1, CMD_RGBA },
132 { "-step", 1, CMD_STEP },
133 { "-move", 1, CMD_MOVE },
134#endif
135 { 0, 0, 0 }
136};
137
138static int readmode(struct fb_var_screeninfo *base, const char *fn,
139 const char *mode)
140{
141#ifdef BB_FBSET_READMODE
142 FILE *f;
143 char buf[256];
144 char *p = buf;
145
146 if ((f = fopen(fn, "r")) == NULL) PERROR("readmode(fopen)");
147 while (!feof(f)) {
148 fgets(buf, sizeof(buf), f);
149 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
150 p += 5;
151 if ((p = strstr(buf, mode))) {
152 p += strlen(mode);
153 if (!isspace(*p) && (*p != 0) && (*p != '"') && (*p != '\r')
154 && (*p != '\n')) continue; /* almost, but not quite */
155 while (!feof(f)) {
156 fgets(buf, sizeof(buf), f);
157 if (!strstr(buf, "endmode")) return 1;
158 }
159 }
160 }
161 }
162#else
163 fprintf(stderr, "W: mode reading was disabled on this copy of fbset; ignoring request\n");
164#endif
165 return 0;
166}
167
168static void setmode(struct fb_var_screeninfo *base,
169 struct fb_var_screeninfo *set)
170{
171 if ((int)set->xres > 0) base->xres = set->xres;
172 if ((int)set->yres > 0) base->yres = set->yres;
173 if ((int)set->xres_virtual > 0) base->xres_virtual = set->xres_virtual;
174 if ((int)set->yres_virtual > 0) base->yres_virtual = set->yres_virtual;
175 if ((int)set->bits_per_pixel > 0) base->bits_per_pixel = set->bits_per_pixel;
176}
177
178static void showmode(struct fb_var_screeninfo *v)
179{
180 double drate = 0, hrate = 0, vrate = 0;
181 if (v->pixclock) {
182 drate = 1e12 / v->pixclock;
183 hrate = drate / (v->left_margin+v->xres+v->right_margin+v->hsync_len);
184 vrate = hrate / (v->upper_margin+v->yres+v->lower_margin+v->vsync_len);
185 }
186 printf("\nmode \"%ux%u-%u\"\n", v->xres, v->yres, (int)(vrate+0.5));
187#ifdef BB_FBSET_FANCY
188 printf("\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n", drate/1e6, hrate/1e3,
189 vrate);
190#endif
191 printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
192 v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
Erik Andersen4d1d0111999-12-17 18:44:15 +0000193#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000194 printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
195 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
196 v->vsync_len);
197 printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
Erik Andersen4d1d0111999-12-17 18:44:15 +0000198#else
199 printf("\ttimings %lu %lu %lu %lu %lu %lu %lu\n", v->pixclock, v->left_margin,
200 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
201 v->vsync_len);
202#endif
Erik Andersen1c5b2581999-12-16 20:59:36 +0000203 printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length, v->red.offset,
204 v->green.length, v->green.offset, v->blue.length, v->blue.offset,
205 v->transp.length, v->transp.offset);
206 printf("endmode\n");
207}
208
209static void fbset_usage(void)
210{
211 int i;
212#ifndef STANDALONE
213 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", BB_VER, BB_BT);
214#endif
215 fprintf(stderr, "Usage: fbset [options] [mode]\n");
216 fprintf(stderr, "\tThe following options are recognized:\n");
217 for (i = 0; g_cmdoptions[i].name; i++)
218 fprintf(stderr, "\t\t%s\n", g_cmdoptions[i].name);
219 exit(-1);
220}
221
222#ifdef STANDALONE
223int main(int argc, char **argv)
224#else
225extern int fbset_main(int argc, char **argv)
226#endif
227{
228 struct fb_var_screeninfo var, varset;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000229 int fh, i;
230 char *fbdev = DEFAULTFBDEV;
231 char *modefile = DEFAULTFBMODE;
232 char *thisarg, *mode = NULL;
233
234 memset(&varset, 0xFF, sizeof(varset));
235
236 /* parse cmd args.... why do they have to make things so difficult? */
237 argv++; argc--;
238 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
239 for (i = 0; g_cmdoptions[i].name; i++) {
240 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
241 if (argc - 1 < g_cmdoptions[i].param_count) fbset_usage();
242 switch (g_cmdoptions[i].code) {
243 case CMD_HELP: fbset_usage();
244 case CMD_FB: fbdev = argv[1]; break;
245 case CMD_DB: modefile = argv[1]; break;
246 case CMD_GEOMETRY:
247 varset.xres = strtoul(argv[1],0,0);
248 varset.yres = strtoul(argv[2],0,0);
249 varset.xres_virtual = strtoul(argv[3],0,0);
250 varset.yres_virtual = strtoul(argv[4],0,0);
251 varset.bits_per_pixel = strtoul(argv[5],0,0);
252 break;
253 case CMD_TIMING:
254 varset.pixclock = strtoul(argv[1],0,0);
255 varset.left_margin = strtoul(argv[2],0,0);
256 varset.right_margin = strtoul(argv[3],0,0);
257 varset.upper_margin = strtoul(argv[4],0,0);
258 varset.lower_margin = strtoul(argv[5],0,0);
259 varset.hsync_len = strtoul(argv[6],0,0);
260 varset.vsync_len = strtoul(argv[7],0,0);
261 break;
262#ifdef BB_FBSET_FANCY
263 case CMD_XRES: varset.xres = strtoul(argv[1],0,0); break;
264 case CMD_YRES: varset.yres = strtoul(argv[1],0,0); break;
265#endif
266 }
267 argc -= g_cmdoptions[i].param_count;
268 argv += g_cmdoptions[i].param_count;
269 break;
270 }
271 }
272 if (!g_cmdoptions[i].name) {
273 if (argc == 1) {
274 mode = *argv;
275 g_options |= OPT_READMODE;
276 } else {
277 fbset_usage();
278 }
279 }
280 }
281
282 if ((fh = open(fbdev, O_RDONLY)) < 0) PERROR("fbset(open)");
283 if (ioctl(fh, FBIOGET_VSCREENINFO, &var)) PERROR("fbset(ioctl)");
284 if (g_options & OPT_READMODE) {
285 if (!readmode(&var, modefile, mode)) {
286 fprintf(stderr, "Unknown video mode `%s'\n", mode);
287 exit(1);
288 }
289 }
290
291 setmode(&var, &varset);
292 if (g_options & OPT_CHANGE)
293 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var)) PERROR("fbset(ioctl)");
294 showmode(&var);
295 close(fh);
296
297 return(TRUE);
298}
299