blob: 7ca41733b36edc8b54e72b49186f96e2c6fb17c9 [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
27#include "internal.h"
28#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>
35#include <linux/fb.h>
Erik Andersen4d1d0111999-12-17 18:44:15 +000036#include <linux/version.h>
Erik Andersen1c5b2581999-12-16 20:59:36 +000037
38#define PERROR(ctx) do { perror(ctx); exit(1); } while(0)
39
Erik Andersen4d1d0111999-12-17 18:44:15 +000040#ifndef KERNEL_VERSION
41#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
42#endif
43
Erik Andersen1c5b2581999-12-16 20:59:36 +000044#define DEFAULTFBDEV "/dev/fb0"
45#define DEFAULTFBMODE "/etc/fb.modes"
46
47#define OPT_CHANGE 1
48#define OPT_INFO (1 << 1)
49#define OPT_READMODE (1 << 2)
50
51#define CMD_HELP 0
Erik Andersene49d5ec2000-02-08 19:58:47 +000052#define CMD_FB 1
Erik Andersen1c5b2581999-12-16 20:59:36 +000053#define CMD_DB 2
54#define CMD_GEOMETRY 3
55#define CMD_TIMING 4
56#define CMD_ACCEL 5
57#define CMD_HSYNC 6
58#define CMD_VSYNC 7
59#define CMD_LACED 8
60#define CMD_DOUBLE 9
61/* #define CMD_XCOMPAT 10 */
62#define CMD_ALL 11
63#define CMD_INFO 12
64
Erik Andersen298854f2000-03-23 01:09:18 +000065#ifdef BB_FEATURE_FBSET_FANCY
Erik Andersen1c5b2581999-12-16 20:59:36 +000066#define CMD_XRES 13
67#define CMD_YRES 14
68#define CMD_VXRES 15
69#define CMD_VYRES 16
70#define CMD_DEPTH 17
71#define CMD_MATCH 18
72#define CMD_PIXCLOCK 19
73#define CMD_LEFT 20
74#define CMD_RIGHT 21
75#define CMD_UPPER 22
76#define CMD_LOWER 23
77#define CMD_HSLEN 24
78#define CMD_VSLEN 25
79#define CMD_CSYNC 26
80#define CMD_GSYNC 27
81#define CMD_EXTSYNC 28
82#define CMD_BCAST 29
83#define CMD_RGBA 30
84#define CMD_STEP 31
85#define CMD_MOVE 32
86#endif
87
88static unsigned int g_options = 0;
89
90struct cmdoptions_t {
Erik Andersene49d5ec2000-02-08 19:58:47 +000091 char *name;
92 unsigned char param_count;
93 unsigned char code;
Erik Andersen1c5b2581999-12-16 20:59:36 +000094} g_cmdoptions[] = {
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 {
96 "-h", 0, CMD_HELP}, {
97 "-fb", 1, CMD_FB}, {
98 "-db", 1, CMD_DB}, {
99 "-a", 0, CMD_ALL}, {
100 "-i", 0, CMD_INFO}, {
101 "-g", 5, CMD_GEOMETRY}, {
102 "-t", 7, CMD_TIMING}, {
103 "-accel", 1, CMD_ACCEL}, {
104 "-hsync", 1, CMD_HSYNC}, {
105 "-vsync", 1, CMD_VSYNC}, {
106 "-laced", 1, CMD_LACED}, {
107 "-double", 1, CMD_DOUBLE},
Erik Andersen298854f2000-03-23 01:09:18 +0000108#ifdef BB_FEATURE_FBSET_FANCY
Erik Andersene49d5ec2000-02-08 19:58:47 +0000109 {
110 "--help", 0, CMD_HELP}, {
111 "-all", 0, CMD_ALL}, {
112 "-xres", 1, CMD_XRES}, {
113 "-yres", 1, CMD_YRES}, {
114 "-vxres", 1, CMD_VXRES}, {
115 "-vyres", 1, CMD_VYRES}, {
116 "-depth", 1, CMD_DEPTH}, {
117 "-match", 0, CMD_MATCH}, {
118 "--geometry", 5, CMD_GEOMETRY}, {
119 "-pixclock", 1, CMD_PIXCLOCK}, {
120 "-left", 1, CMD_LEFT}, {
121 "-right", 1, CMD_RIGHT}, {
122 "-upper", 1, CMD_UPPER}, {
123 "-lower", 1, CMD_LOWER}, {
124 "-hslen", 1, CMD_HSLEN}, {
125 "-vslen", 1, CMD_VSLEN}, {
126 "--timings", 7, CMD_TIMING}, {
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},
Erik Andersen1c5b2581999-12-16 20:59:36 +0000134#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 {
136 0, 0, 0}
Erik Andersen1c5b2581999-12-16 20:59:36 +0000137};
138
139static int readmode(struct fb_var_screeninfo *base, const char *fn,
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 const char *mode)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000141{
142#ifdef BB_FBSET_READMODE
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 FILE *f;
144 char buf[256];
145 char *p = buf;
146
147 if ((f = fopen(fn, "r")) == NULL)
148 PERROR("readmode(fopen)");
149 while (!feof(f)) {
150 fgets(buf, sizeof(buf), f);
151 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
152 p += 5;
153 if ((p = strstr(buf, mode))) {
154 p += strlen(mode);
155 if (!isspace(*p) && (*p != 0) && (*p != '"')
156 && (*p != '\r') && (*p != '\n'))
157 continue; /* almost, but not quite */
158 while (!feof(f)) {
159 fgets(buf, sizeof(buf), f);
160 if (!strstr(buf, "endmode"))
161 return 1;
162 }
163 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000164 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000165 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000166#else
Erik Andersen298854f2000-03-23 01:09:18 +0000167 errorMsg( "mode reading not compiled in\n");
Erik Andersen1c5b2581999-12-16 20:59:36 +0000168#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000169 return 0;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000170}
171
Erik Andersene49d5ec2000-02-08 19:58:47 +0000172static void setmode(struct fb_var_screeninfo *base,
173 struct fb_var_screeninfo *set)
Erik Andersen1c5b2581999-12-16 20:59:36 +0000174{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000175 if ((int) set->xres > 0)
176 base->xres = set->xres;
177 if ((int) set->yres > 0)
178 base->yres = set->yres;
179 if ((int) set->xres_virtual > 0)
180 base->xres_virtual = set->xres_virtual;
181 if ((int) set->yres_virtual > 0)
182 base->yres_virtual = set->yres_virtual;
183 if ((int) set->bits_per_pixel > 0)
184 base->bits_per_pixel = set->bits_per_pixel;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000185}
186
187static void showmode(struct fb_var_screeninfo *v)
188{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000189 double drate = 0, hrate = 0, vrate = 0;
190
191 if (v->pixclock) {
192 drate = 1e12 / v->pixclock;
193 hrate =
194 drate / (v->left_margin + v->xres + v->right_margin +
195 v->hsync_len);
196 vrate =
197 hrate / (v->upper_margin + v->yres + v->lower_margin +
198 v->vsync_len);
199 }
200 printf("\nmode \"%ux%u-%u\"\n", v->xres, v->yres, (int) (vrate + 0.5));
Erik Andersen298854f2000-03-23 01:09:18 +0000201#ifdef BB_FEATURE_FBSET_FANCY
Erik Andersene49d5ec2000-02-08 19:58:47 +0000202 printf("\t# D: %.3f MHz, H: %.3f kHz, V: %.3f Hz\n", drate / 1e6,
203 hrate / 1e3, vrate);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000204#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000205 printf("\tgeometry %u %u %u %u %u\n", v->xres, v->yres,
206 v->xres_virtual, v->yres_virtual, v->bits_per_pixel);
Erik Andersen4d1d0111999-12-17 18:44:15 +0000207#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000208 printf("\ttimings %u %u %u %u %u %u %u\n", v->pixclock, v->left_margin,
209 v->right_margin, v->upper_margin, v->lower_margin, v->hsync_len,
210 v->vsync_len);
211 printf("\taccel %s\n", (v->accel_flags > 0 ? "true" : "false"));
Erik Andersen4d1d0111999-12-17 18:44:15 +0000212#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 printf("\ttimings %lu %lu %lu %lu %lu %lu %lu\n", v->pixclock,
214 v->left_margin, v->right_margin, v->upper_margin,
215 v->lower_margin, v->hsync_len, v->vsync_len);
Erik Andersen4d1d0111999-12-17 18:44:15 +0000216#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 printf("\trgba %u/%u,%u/%u,%u/%u,%u/%u\n", v->red.length,
218 v->red.offset, v->green.length, v->green.offset, v->blue.length,
219 v->blue.offset, v->transp.length, v->transp.offset);
220 printf("endmode\n");
Erik Andersen1c5b2581999-12-16 20:59:36 +0000221}
222
223static void fbset_usage(void)
224{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 int i;
226
Erik Andersen1c5b2581999-12-16 20:59:36 +0000227#ifndef STANDALONE
Erik Andersene49d5ec2000-02-08 19:58:47 +0000228 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
229 BB_VER, BB_BT);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000230#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 fprintf(stderr, "Usage: fbset [options] [mode]\n");
232 fprintf(stderr, "\tThe following options are recognized:\n");
233 for (i = 0; g_cmdoptions[i].name; i++)
234 fprintf(stderr, "\t\t%s\n", g_cmdoptions[i].name);
235 exit(-1);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000236}
237
238#ifdef STANDALONE
239int main(int argc, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240#else
Erik Andersen1c5b2581999-12-16 20:59:36 +0000241extern int fbset_main(int argc, char **argv)
242#endif
243{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 struct fb_var_screeninfo var, varset;
245 int fh, i;
246 char *fbdev = DEFAULTFBDEV;
247 char *modefile = DEFAULTFBMODE;
248 char *thisarg, *mode = NULL;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000249
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 memset(&varset, 0xFF, sizeof(varset));
251
252 /* parse cmd args.... why do they have to make things so difficult? */
253 argv++;
254 argc--;
255 for (; argc > 0 && (thisarg = *argv); argc--, argv++) {
256 for (i = 0; g_cmdoptions[i].name; i++) {
257 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
258 if (argc - 1 < g_cmdoptions[i].param_count)
259 fbset_usage();
260 switch (g_cmdoptions[i].code) {
261 case CMD_HELP:
262 fbset_usage();
263 case CMD_FB:
264 fbdev = argv[1];
265 break;
266 case CMD_DB:
267 modefile = argv[1];
268 break;
269 case CMD_GEOMETRY:
270 varset.xres = strtoul(argv[1], 0, 0);
271 varset.yres = strtoul(argv[2], 0, 0);
272 varset.xres_virtual = strtoul(argv[3], 0, 0);
273 varset.yres_virtual = strtoul(argv[4], 0, 0);
274 varset.bits_per_pixel = strtoul(argv[5], 0, 0);
275 break;
276 case CMD_TIMING:
277 varset.pixclock = strtoul(argv[1], 0, 0);
278 varset.left_margin = strtoul(argv[2], 0, 0);
279 varset.right_margin = strtoul(argv[3], 0, 0);
280 varset.upper_margin = strtoul(argv[4], 0, 0);
281 varset.lower_margin = strtoul(argv[5], 0, 0);
282 varset.hsync_len = strtoul(argv[6], 0, 0);
283 varset.vsync_len = strtoul(argv[7], 0, 0);
284 break;
Erik Andersen298854f2000-03-23 01:09:18 +0000285#ifdef BB_FEATURE_FBSET_FANCY
Erik Andersene49d5ec2000-02-08 19:58:47 +0000286 case CMD_XRES:
287 varset.xres = strtoul(argv[1], 0, 0);
288 break;
289 case CMD_YRES:
290 varset.yres = strtoul(argv[1], 0, 0);
291 break;
Erik Andersen1c5b2581999-12-16 20:59:36 +0000292#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +0000293 }
294 argc -= g_cmdoptions[i].param_count;
295 argv += g_cmdoptions[i].param_count;
296 break;
297 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000298 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000299 if (!g_cmdoptions[i].name) {
300 if (argc == 1) {
301 mode = *argv;
302 g_options |= OPT_READMODE;
303 } else {
304 fbset_usage();
305 }
306 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000307 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000308
Erik Andersene49d5ec2000-02-08 19:58:47 +0000309 if ((fh = open(fbdev, O_RDONLY)) < 0)
310 PERROR("fbset(open)");
311 if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
312 PERROR("fbset(ioctl)");
313 if (g_options & OPT_READMODE) {
314 if (!readmode(&var, modefile, mode)) {
315 fprintf(stderr, "Unknown video mode `%s'\n", mode);
316 exit(1);
317 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000318 }
Erik Andersen1c5b2581999-12-16 20:59:36 +0000319
Erik Andersene49d5ec2000-02-08 19:58:47 +0000320 setmode(&var, &varset);
321 if (g_options & OPT_CHANGE)
322 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
323 PERROR("fbset(ioctl)");
324 showmode(&var);
Erik Andersen298854f2000-03-23 01:09:18 +0000325 /* Don't close the file, as exiting will take care of that */
326 /* close(fh); */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000327
Erik Andersen298854f2000-03-23 01:09:18 +0000328 exit (TRUE);
Erik Andersen1c5b2581999-12-16 20:59:36 +0000329}