grfconfig.c revision 1.14 1 1.14 mlelstv /* $NetBSD: grfconfig.c,v 1.14 2009/04/26 19:24:18 mlelstv Exp $ */
2 1.1 chopps
3 1.5 veego /*-
4 1.5 veego * Copyright (c) 1997 The NetBSD Foundation, Inc.
5 1.1 chopps * All rights reserved.
6 1.1 chopps *
7 1.5 veego * This code is derived from software contributed to The NetBSD Foundation
8 1.5 veego * by Ezra Story and Bernd Ernesti.
9 1.5 veego *
10 1.1 chopps * Redistribution and use in source and binary forms, with or without
11 1.1 chopps * modification, are permitted provided that the following conditions
12 1.1 chopps * are met:
13 1.1 chopps * 1. Redistributions of source code must retain the above copyright
14 1.1 chopps * notice, this list of conditions and the following disclaimer.
15 1.1 chopps * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 chopps * notice, this list of conditions and the following disclaimer in the
17 1.1 chopps * documentation and/or other materials provided with the distribution.
18 1.1 chopps *
19 1.5 veego * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.5 veego * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.5 veego * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.7 jtc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.7 jtc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.5 veego * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.5 veego * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.5 veego * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.5 veego * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.5 veego * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.5 veego * POSSIBILITY OF SUCH DAMAGE.
30 1.1 chopps */
31 1.5 veego
32 1.5 veego #include <sys/cdefs.h>
33 1.5 veego #ifndef lint
34 1.13 lukem __COPYRIGHT("@(#) Copyright (c) 1997\
35 1.13 lukem The NetBSD Foundation, Inc. All rights reserved.");
36 1.5 veego #endif /* not lint */
37 1.5 veego
38 1.5 veego #ifndef lint
39 1.14 mlelstv __RCSID("$NetBSD: grfconfig.c,v 1.14 2009/04/26 19:24:18 mlelstv Exp $");
40 1.5 veego #endif /* not lint */
41 1.5 veego
42 1.5 veego #include <sys/file.h>
43 1.5 veego #include <sys/ioctl.h>
44 1.5 veego #include <ctype.h>
45 1.5 veego #include <limits.h>
46 1.1 chopps #include <stdio.h>
47 1.1 chopps #include <stdlib.h>
48 1.1 chopps #include <string.h>
49 1.5 veego #include <unistd.h>
50 1.1 chopps
51 1.1 chopps #include <amiga/dev/grfioctl.h>
52 1.1 chopps
53 1.5 veego int main __P((int, char **));
54 1.5 veego static void print_rawdata __P((struct grfvideo_mode *, int));
55 1.5 veego
56 1.5 veego static struct grf_flag {
57 1.5 veego u_short grf_flag_number;
58 1.14 mlelstv const char *grf_flag_name;
59 1.5 veego } grf_flags[] = {
60 1.5 veego {GRF_FLAGS_DBLSCAN, "doublescan"},
61 1.5 veego {GRF_FLAGS_LACE, "interlace"},
62 1.5 veego {GRF_FLAGS_PHSYNC, "+hsync"},
63 1.5 veego {GRF_FLAGS_NHSYNC, "-hsync"},
64 1.5 veego {GRF_FLAGS_PVSYNC, "+vsync"},
65 1.5 veego {GRF_FLAGS_NVSYNC, "-vsync"},
66 1.5 veego {GRF_FLAGS_SYNC_ON_GREEN, "sync-on-green"},
67 1.5 veego {0, 0}
68 1.5 veego };
69 1.5 veego
70 1.1 chopps /*
71 1.1 chopps * Dynamic mode loader for NetBSD/Amiga grf devices.
72 1.1 chopps */
73 1.1 chopps int
74 1.1 chopps main(ac, av)
75 1.1 chopps int ac;
76 1.1 chopps char **av;
77 1.1 chopps {
78 1.5 veego struct grfvideo_mode gv[1];
79 1.5 veego struct grf_flag *grf_flagp;
80 1.5 veego FILE *fp;
81 1.5 veego int c, y, grffd;
82 1.5 veego int i, lineno = 0;
83 1.5 veego int uplim, lowlim;
84 1.5 veego char rawdata = 0, testmode = 0;
85 1.5 veego char *grfdevice = 0;
86 1.5 veego char *modefile = 0;
87 1.9 veego char buf[_POSIX2_LINE_MAX];
88 1.5 veego char *cps[31];
89 1.5 veego char *p;
90 1.14 mlelstv const char *errortext;
91 1.5 veego
92 1.1 chopps
93 1.5 veego while ((c = getopt(ac, av, "rt")) != -1) {
94 1.1 chopps switch (c) {
95 1.1 chopps case 'r': /* raw output */
96 1.1 chopps rawdata = 1;
97 1.1 chopps break;
98 1.5 veego case 't': /* test the modefile without setting it */
99 1.5 veego testmode = 1;
100 1.5 veego break;
101 1.1 chopps default:
102 1.2 chopps printf("grfconfig [-r] device [file]\n");
103 1.1 chopps return (1);
104 1.1 chopps }
105 1.1 chopps }
106 1.1 chopps ac -= optind;
107 1.1 chopps av += optind;
108 1.1 chopps
109 1.1 chopps
110 1.1 chopps if (ac >= 1)
111 1.1 chopps grfdevice = av[0];
112 1.1 chopps else {
113 1.1 chopps printf("grfconfig: No grf device specified.\n");
114 1.1 chopps return (1);
115 1.1 chopps }
116 1.1 chopps
117 1.1 chopps if (ac >= 2)
118 1.1 chopps modefile = av[1];
119 1.1 chopps
120 1.1 chopps if ((grffd = open(grfdevice, O_RDWR)) < 0) {
121 1.1 chopps printf("grfconfig: can't open grf device.\n");
122 1.1 chopps return (1);
123 1.1 chopps }
124 1.1 chopps /* If a mode file is specificied, load it in, don't display any info. */
125 1.1 chopps
126 1.1 chopps if (modefile) {
127 1.1 chopps if (!(fp = fopen(modefile, "r"))) {
128 1.5 veego printf("grfconfig: Cannot open mode definition "
129 1.5 veego "file.\n");
130 1.1 chopps return (1);
131 1.1 chopps }
132 1.5 veego while (fgets(buf, sizeof(buf), fp)) {
133 1.9 veego char *obuf, tbuf[_POSIX2_LINE_MAX], *tbuf2;
134 1.5 veego /*
135 1.5 veego * check for end-of-section, comments, strip off trailing
136 1.5 veego * spaces and newline character.
137 1.5 veego */
138 1.11 he for (p = buf; isspace((unsigned char)*p); ++p)
139 1.1 chopps continue;
140 1.5 veego if (*p == '\0' || *p == '#')
141 1.5 veego continue;
142 1.11 he for (p = strchr(buf, '\0'); isspace((unsigned char)*--p);)
143 1.5 veego continue;
144 1.5 veego *++p = '\0';
145 1.5 veego
146 1.9 veego obuf = buf;
147 1.9 veego tbuf2 = tbuf;
148 1.9 veego while ((*tbuf2 = *obuf) != '\0') {
149 1.9 veego if (*tbuf2 == '#') {
150 1.9 veego *tbuf2 = '\0';
151 1.9 veego break;
152 1.9 veego }
153 1.11 he if (isupper((unsigned char)*tbuf2)) {
154 1.11 he *tbuf2 = tolower((unsigned char)*tbuf2);
155 1.9 veego }
156 1.9 veego obuf++;
157 1.9 veego tbuf2++;
158 1.9 veego }
159 1.9 veego obuf = tbuf;
160 1.9 veego
161 1.5 veego lineno = lineno + 1;
162 1.5 veego
163 1.5 veego for (i = 0, *cps = strtok(buf, " \b\t\r\n");
164 1.5 veego cps[i] != NULL && i < 30; i++)
165 1.5 veego cps[i + 1] = strtok(NULL, " \b\t\r\n");
166 1.5 veego cps[i] = NULL;
167 1.5 veego
168 1.10 is if (i < 14) {
169 1.5 veego printf("grfconfig: too few values in mode "
170 1.5 veego "definition file:\n %s\n", obuf);
171 1.5 veego return (1);
172 1.5 veego }
173 1.5 veego
174 1.5 veego gv->pixel_clock = atoi(cps[1]);
175 1.5 veego gv->disp_width = atoi(cps[2]);
176 1.5 veego gv->disp_height = atoi(cps[3]);
177 1.5 veego gv->depth = atoi(cps[4]);
178 1.5 veego gv->hblank_start = atoi(cps[5]);
179 1.5 veego gv->hsync_start = atoi(cps[6]);
180 1.5 veego gv->hsync_stop = atoi(cps[7]);
181 1.5 veego gv->htotal = atoi(cps[8]);
182 1.5 veego gv->vblank_start = atoi(cps[9]);
183 1.5 veego gv->vsync_start = atoi(cps[10]);
184 1.5 veego gv->vsync_stop = atoi(cps[11]);
185 1.5 veego gv->vtotal = atoi(cps[12]);
186 1.1 chopps
187 1.5 veego if ((y = atoi(cps[0])))
188 1.5 veego gv->mode_num = y;
189 1.5 veego else
190 1.5 veego if (strncasecmp("c", cps[0], 1) == 0) {
191 1.5 veego gv->mode_num = 255;
192 1.5 veego gv->depth = 4;
193 1.5 veego } else {
194 1.5 veego printf("grfconfig: Illegal mode "
195 1.5 veego "number: %s\n", cps[0]);
196 1.5 veego return (1);
197 1.5 veego }
198 1.5 veego
199 1.5 veego if ((gv->pixel_clock == 0) ||
200 1.5 veego (gv->disp_width == 0) ||
201 1.5 veego (gv->disp_height == 0) ||
202 1.5 veego (gv->depth == 0) ||
203 1.5 veego (gv->hblank_start == 0) ||
204 1.5 veego (gv->hsync_start == 0) ||
205 1.5 veego (gv->hsync_stop == 0) ||
206 1.5 veego (gv->htotal == 0) ||
207 1.5 veego (gv->vblank_start == 0) ||
208 1.5 veego (gv->vsync_start == 0) ||
209 1.5 veego (gv->vsync_stop == 0) ||
210 1.5 veego (gv->vtotal == 0)) {
211 1.5 veego printf("grfconfig: Illegal value in "
212 1.5 veego "mode #%d:\n %s\n", gv->mode_num, obuf);
213 1.5 veego return (1);
214 1.5 veego }
215 1.5 veego
216 1.5 veego if (strstr(obuf, "default") != NULL) {
217 1.5 veego gv->disp_flags = GRF_FLAGS_DEFAULT;
218 1.5 veego } else {
219 1.5 veego gv->disp_flags = GRF_FLAGS_DEFAULT;
220 1.5 veego for (grf_flagp = grf_flags;
221 1.5 veego grf_flagp->grf_flag_number; grf_flagp++) {
222 1.5 veego if (strstr(obuf, grf_flagp->grf_flag_name) != NULL) {
223 1.5 veego gv->disp_flags |= grf_flagp->grf_flag_number;
224 1.5 veego }
225 1.5 veego }
226 1.5 veego if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
227 1.5 veego printf("grfconfig: Your are using an "
228 1.5 veego "mode file with an obsolete "
229 1.5 veego "format.\n See the manpage of "
230 1.5 veego "grfconfig for more information "
231 1.5 veego "about the new mode definition "
232 1.5 veego "file.\n");
233 1.5 veego return (1);
234 1.5 veego }
235 1.5 veego }
236 1.1 chopps
237 1.5 veego /*
238 1.5 veego * Check for impossible gv->disp_flags:
239 1.5 veego * doublescan and interlace,
240 1.5 veego * +hsync and -hsync
241 1.5 veego * +vsync and -vsync.
242 1.5 veego */
243 1.5 veego errortext = NULL;
244 1.5 veego if ((gv->disp_flags & GRF_FLAGS_DBLSCAN) &&
245 1.5 veego (gv->disp_flags & GRF_FLAGS_LACE))
246 1.5 veego errortext = "Interlace and Doublescan";
247 1.5 veego if ((gv->disp_flags & GRF_FLAGS_PHSYNC) &&
248 1.5 veego (gv->disp_flags & GRF_FLAGS_NHSYNC))
249 1.5 veego errortext = "+hsync and -hsync";
250 1.5 veego if ((gv->disp_flags & GRF_FLAGS_PVSYNC) &&
251 1.5 veego (gv->disp_flags & GRF_FLAGS_NVSYNC))
252 1.5 veego errortext = "+vsync and -vsync";
253 1.5 veego
254 1.5 veego if (errortext != NULL) {
255 1.5 veego printf("grfconfig: Illegal flags in "
256 1.5 veego "mode #%d: %s are both defined!\n",
257 1.5 veego gv->mode_num, errortext);
258 1.5 veego return (1);
259 1.5 veego }
260 1.5 veego
261 1.5 veego /* Check for old horizontal cycle values */
262 1.5 veego if ((gv->htotal < (gv->disp_width / 4))) {
263 1.5 veego gv->hblank_start *= 8;
264 1.5 veego gv->hsync_start *= 8;
265 1.5 veego gv->hsync_stop *= 8;
266 1.5 veego gv->htotal *= 8;
267 1.5 veego printf("grfconfig: Old and no longer "
268 1.5 veego "supported horizontal videoclock cycle "
269 1.5 veego "values.\n Wrong mode line:\n %s\n "
270 1.5 veego "This could be a possible good mode "
271 1.5 veego "line:\n ", obuf);
272 1.5 veego printf("%d ", gv->mode_num);
273 1.5 veego print_rawdata(gv, 0);
274 1.5 veego printf(" See the manpage of grfconfig for "
275 1.5 veego "more information about the new mode "
276 1.5 veego "definition file.\n");
277 1.5 veego return (1);
278 1.5 veego }
279 1.5 veego
280 1.5 veego /* Check for old interlace or doublescan modes */
281 1.5 veego uplim = gv->disp_height + (gv->disp_height / 4);
282 1.5 veego lowlim = gv->disp_height - (gv->disp_height / 4);
283 1.5 veego if (((gv->vtotal * 2) > lowlim) &&
284 1.5 veego ((gv->vtotal * 2) < uplim)) {
285 1.5 veego gv->vblank_start *= 2;
286 1.5 veego gv->vsync_start *= 2;
287 1.5 veego gv->vsync_stop *= 2;
288 1.5 veego gv->vtotal *= 2;
289 1.6 veego gv->disp_flags &= ~GRF_FLAGS_DBLSCAN;
290 1.6 veego gv->disp_flags |= GRF_FLAGS_LACE;
291 1.5 veego printf("grfconfig: Old and no longer "
292 1.5 veego "supported vertical values for "
293 1.5 veego "interlace modes.\n Wrong mode "
294 1.5 veego "line:\n %s\n This could be a "
295 1.5 veego "possible good mode line:\n ", obuf);
296 1.5 veego printf("%d ", gv->mode_num);
297 1.5 veego print_rawdata(gv, 0);
298 1.5 veego printf(" See the manpage of grfconfig for "
299 1.5 veego "more information about the new mode "
300 1.5 veego "definition file.\n");
301 1.5 veego return (1);
302 1.6 veego } else if (((gv->vtotal / 2) > lowlim) &&
303 1.5 veego ((gv->vtotal / 2) < uplim)) {
304 1.5 veego gv->vblank_start /= 2;
305 1.5 veego gv->vsync_start /= 2;
306 1.5 veego gv->vsync_stop /= 2;
307 1.5 veego gv->vtotal /= 2;
308 1.6 veego gv->disp_flags &= ~GRF_FLAGS_LACE;
309 1.6 veego gv->disp_flags |= GRF_FLAGS_DBLSCAN;
310 1.5 veego printf("grfconfig: Old and no longer "
311 1.5 veego "supported vertical values for "
312 1.5 veego "doublescan modes.\n Wrong mode "
313 1.5 veego "line:\n %s\n This could be a "
314 1.5 veego "possible good mode line:\n ", obuf);
315 1.5 veego printf("%d ", gv->mode_num);
316 1.5 veego print_rawdata(gv, 0);
317 1.5 veego printf(" See the manpage of grfconfig for "
318 1.5 veego "more information about the new mode "
319 1.5 veego "definition file.\n");
320 1.5 veego return (1);
321 1.5 veego }
322 1.5 veego
323 1.5 veego if (testmode == 1) {
324 1.5 veego if (lineno == 1)
325 1.5 veego printf("num clk wid hi dep hbs "
326 1.5 veego "hss hse ht vbs vss vse vt "
327 1.5 veego "flags\n");
328 1.5 veego printf("%d ", gv->mode_num);
329 1.5 veego print_rawdata(gv, 1);
330 1.5 veego } else {
331 1.1 chopps gv->mode_descr[0] = 0;
332 1.1 chopps if (ioctl(grffd, GRFIOCSETMON, (char *) gv) < 0)
333 1.3 neil printf("grfconfig: bad monitor "
334 1.4 veego "definition for mode #%d.\n",
335 1.4 veego gv->mode_num);
336 1.1 chopps }
337 1.1 chopps }
338 1.1 chopps fclose(fp);
339 1.1 chopps } else {
340 1.1 chopps ioctl(grffd, GRFGETNUMVM, &y);
341 1.1 chopps y += 2;
342 1.1 chopps for (c = 1; c < y; c++) {
343 1.1 chopps c = gv->mode_num = (c != (y - 1)) ? c : 255;
344 1.1 chopps if (ioctl(grffd, GRFGETVMODE, gv) < 0)
345 1.1 chopps continue;
346 1.1 chopps if (rawdata) {
347 1.1 chopps if (c == 255)
348 1.1 chopps printf("c ");
349 1.1 chopps else
350 1.1 chopps printf("%d ", c);
351 1.5 veego print_rawdata(gv, 0);
352 1.1 chopps continue;
353 1.1 chopps }
354 1.1 chopps if (c == 255)
355 1.1 chopps printf("Console: ");
356 1.1 chopps else
357 1.1 chopps printf("%2d: ", gv->mode_num);
358 1.1 chopps
359 1.1 chopps printf("%dx%d",
360 1.1 chopps gv->disp_width,
361 1.1 chopps gv->disp_height);
362 1.1 chopps
363 1.1 chopps if (c != 255)
364 1.1 chopps printf("x%d", gv->depth);
365 1.1 chopps else
366 1.1 chopps printf(" (%dx%d)",
367 1.1 chopps gv->disp_width / 8,
368 1.1 chopps gv->disp_height / gv->depth);
369 1.1 chopps
370 1.5 veego printf("\t%ld.%ldkHz @ %ldHz",
371 1.5 veego gv->pixel_clock / (gv->htotal * 1000),
372 1.5 veego (gv->pixel_clock / (gv->htotal * 100))
373 1.1 chopps % 10,
374 1.5 veego gv->pixel_clock / (gv->htotal * gv->vtotal));
375 1.5 veego printf(" flags:");
376 1.5 veego
377 1.5 veego if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
378 1.5 veego printf(" default");
379 1.5 veego } else {
380 1.5 veego for (grf_flagp = grf_flags;
381 1.5 veego grf_flagp->grf_flag_number; grf_flagp++) {
382 1.5 veego if (gv->disp_flags & grf_flagp->grf_flag_number) {
383 1.5 veego printf(" %s", grf_flagp->grf_flag_name);
384 1.5 veego }
385 1.5 veego }
386 1.5 veego }
387 1.5 veego printf("\n");
388 1.1 chopps }
389 1.1 chopps }
390 1.1 chopps
391 1.1 chopps close(grffd);
392 1.1 chopps return (0);
393 1.5 veego }
394 1.5 veego
395 1.5 veego static void
396 1.5 veego print_rawdata(gv, rawflags)
397 1.5 veego struct grfvideo_mode *gv;
398 1.5 veego int rawflags;
399 1.5 veego {
400 1.5 veego struct grf_flag *grf_flagp;
401 1.5 veego
402 1.5 veego printf("%ld %d %d %d %d %d %d %d %d %d %d %d",
403 1.5 veego gv->pixel_clock,
404 1.5 veego gv->disp_width,
405 1.5 veego gv->disp_height,
406 1.5 veego gv->depth,
407 1.5 veego gv->hblank_start,
408 1.5 veego gv->hsync_start,
409 1.5 veego gv->hsync_stop,
410 1.5 veego gv->htotal,
411 1.5 veego gv->vblank_start,
412 1.5 veego gv->vsync_start,
413 1.5 veego gv->vsync_stop,
414 1.5 veego gv->vtotal);
415 1.5 veego if (rawflags) {
416 1.5 veego printf(" 0x%.2x", gv->disp_flags);
417 1.5 veego } else {
418 1.5 veego if (gv->disp_flags == GRF_FLAGS_DEFAULT) {
419 1.5 veego printf(" default");
420 1.5 veego } else {
421 1.5 veego for (grf_flagp = grf_flags;
422 1.5 veego grf_flagp->grf_flag_number; grf_flagp++) {
423 1.5 veego if (gv->disp_flags & grf_flagp->grf_flag_number) {
424 1.5 veego printf(" %s", grf_flagp->grf_flag_name);
425 1.5 veego }
426 1.5 veego }
427 1.5 veego }
428 1.5 veego }
429 1.5 veego printf("\n");
430 1.1 chopps }
431