fb.c revision 1.5 1 /* $NetBSD: fb.c,v 1.5 2001/11/12 08:14:30 uwe Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)fb.c 8.1 (Berkeley) 6/11/93
45 */
46
47 /*
48 * /dev/fb (indirect frame buffer driver). This is gross; we should
49 * just build cdevsw[] dynamically.
50 */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/device.h>
55 #include <sys/proc.h>
56 #include <sys/conf.h>
57
58 #include <machine/autoconf.h>
59 #include <machine/kbd.h>
60 #include <machine/conf.h>
61 #include <machine/eeprom.h>
62 #include <sparc/dev/cons.h>
63
64 #include <dev/sun/fbio.h>
65 #include <dev/sun/fbvar.h>
66
67 #include "kbd.h"
68 #include "pfour.h"
69
70 static struct fbdevice *devfb;
71
72 void
73 fb_unblank()
74 {
75
76 if (devfb)
77 (*devfb->fb_driver->fbd_unblank)(devfb->fb_device);
78 }
79
80 /*
81 * Helper function for frame buffer devices. Decides whether
82 * the device can be the console output device according to
83 * PROM info. The result from this function may not be conclusive
84 * on machines with old PROMs; in that case, drivers should consult
85 * other sources of configuration information (e.g. EEPROM entries).
86 */
87 #if defined(SUN4U)
88 /* Temporary special case for sun4u */
89 int
90 fb_is_console(node)
91 int node;
92 {
93 extern int fbnode;
94 return (node == fbnode);
95 }
96 #else
97 int
98 fb_is_console(node)
99 int node;
100 {
101 int fbnode;
102
103 switch (prom_version()) {
104 case PROM_OLDMON:
105 /* `node' is not valid; just check for any fb device */
106 return (prom_stdout() == PROMDEV_SCREEN);
107
108 case PROM_OBP_V0:
109 /*
110 * First, check if prom_stdout() represents a frame buffer,
111 * then match on the `fb' property on the root node, if any.
112 */
113 if (prom_stdout() != PROMDEV_SCREEN)
114 return (0);
115
116 fbnode = PROM_getpropint(findroot(), "fb", 0);
117 return (fbnode == 0 || node == fbnode);
118
119 case PROM_OBP_V2:
120 case PROM_OBP_V3:
121 case PROM_OPENFIRM:
122 /* Just match the nodes */
123 return (node == prom_stdout_node);
124 }
125
126 return (0);
127 }
128 #endif /* SUN4U */
129
130 void
131 fb_attach(fb, isconsole)
132 struct fbdevice *fb;
133 int isconsole;
134 {
135 static int no_replace, seen_force;
136
137 /*
138 * We've already had a framebuffer forced into /dev/fb. Don't
139 * allow any more, even if this is the console.
140 */
141 if (seen_force) {
142 if (devfb) { /* sanity */
143 printf("%s: /dev/fb already full\n",
144 fb->fb_device->dv_xname);
145 return;
146 } else
147 seen_force = 0;
148 }
149
150 /*
151 * Check to see if we're being forced into /dev/fb.
152 */
153 if (fb->fb_flags & FB_FORCE) {
154 if (devfb)
155 printf("%s: forcefully replacing %s\n",
156 fb->fb_device->dv_xname,
157 devfb->fb_device->dv_xname);
158 devfb = fb;
159 seen_force = no_replace = 1;
160 goto attached;
161 }
162
163 /*
164 * Check to see if we're the console. If we are, then replace
165 * any currently existing framebuffer.
166 */
167 if (isconsole) {
168 if (devfb)
169 printf("%s: replacing %s\n", fb->fb_device->dv_xname,
170 devfb->fb_device->dv_xname);
171 devfb = fb;
172 no_replace = 1;
173 goto attached;
174 }
175
176 /*
177 * For the final case, we check to see if we can replace an
178 * existing framebuffer, if not, say so and return.
179 */
180 if (no_replace) {
181 if (devfb) { /* sanity */
182 printf("%s: /dev/fb already full\n",
183 fb->fb_device->dv_xname);
184 return;
185 } else
186 no_replace = 0;
187 }
188
189 if (devfb)
190 printf("%s: replacing %s\n", fb->fb_device->dv_xname,
191 devfb->fb_device->dv_xname);
192 devfb = fb;
193
194 attached:
195 printf("%s: attached to /dev/fb\n", devfb->fb_device->dv_xname);
196 }
197
198 int
199 fbopen(dev, flags, mode, p)
200 dev_t dev;
201 int flags, mode;
202 struct proc *p;
203 {
204
205 if (devfb == NULL)
206 return (ENXIO);
207 return (devfb->fb_driver->fbd_open)(dev, flags, mode, p);
208 }
209
210 int
211 fbclose(dev, flags, mode, p)
212 dev_t dev;
213 int flags, mode;
214 struct proc *p;
215 {
216
217 return (devfb->fb_driver->fbd_close)(dev, flags, mode, p);
218 }
219
220 int
221 fbioctl(dev, cmd, data, flags, p)
222 dev_t dev;
223 u_long cmd;
224 caddr_t data;
225 int flags;
226 struct proc *p;
227 {
228
229 return (devfb->fb_driver->fbd_ioctl)(dev, cmd, data, flags, p);
230 }
231
232 int
233 fbpoll(dev, events, p)
234 dev_t dev;
235 int events;
236 struct proc *p;
237 {
238
239 return (devfb->fb_driver->fbd_poll)(dev, events, p);
240 }
241
242 paddr_t
243 fbmmap(dev, off, prot)
244 dev_t dev;
245 off_t off;
246 int prot;
247 {
248 paddr_t (*map)__P((dev_t, off_t, int)) = devfb->fb_driver->fbd_mmap;
249
250 if (map == NULL)
251 return (-1);
252 return (map(dev, off, prot));
253 }
254
255 void
256 fb_setsize_obp(fb, depth, def_width, def_height, node)
257 struct fbdevice *fb;
258 int depth, def_width, def_height, node;
259 {
260 fb->fb_type.fb_width = PROM_getpropint(node, "width", def_width);
261 fb->fb_type.fb_height = PROM_getpropint(node, "height", def_height);
262 fb->fb_linebytes = PROM_getpropint(node, "linebytes",
263 (fb->fb_type.fb_width * depth) / 8);
264 }
265
266 void
267 fb_setsize_eeprom(fb, depth, def_width, def_height)
268 struct fbdevice *fb;
269 int depth, def_width, def_height;
270 {
271 #if defined(SUN4)
272 struct eeprom *eep = (struct eeprom *)eeprom_va;
273
274 if (!CPU_ISSUN4) {
275 printf("fb_setsize_eeprom: not sun4\n");
276 return;
277 }
278
279 /* Set up some defaults. */
280 fb->fb_type.fb_width = def_width;
281 fb->fb_type.fb_height = def_height;
282
283 if (fb->fb_flags & FB_PFOUR) {
284 #if NPFOUR > 0
285 fb_setsize_pfour(fb);
286 #endif
287 } else if (eep != NULL) {
288 switch (eep->eeScreenSize) {
289 case EE_SCR_1152X900:
290 fb->fb_type.fb_width = 1152;
291 fb->fb_type.fb_height = 900;
292 break;
293
294 case EE_SCR_1024X1024:
295 fb->fb_type.fb_width = 1024;
296 fb->fb_type.fb_height = 1024;
297 break;
298
299 case EE_SCR_1600X1280:
300 fb->fb_type.fb_width = 1600;
301 fb->fb_type.fb_height = 1280;
302 break;
303
304 case EE_SCR_1440X1440:
305 fb->fb_type.fb_width = 1440;
306 fb->fb_type.fb_height = 1440;
307 break;
308
309 default:
310 /*
311 * XXX: Do nothing, I guess.
312 * Should we print a warning about
313 * an unknown value? --thorpej
314 */
315 break;
316 }
317 }
318
319 fb->fb_linebytes = (fb->fb_type.fb_width * depth) / 8;
320 #endif /* SUN4 */
321 }
322
323
324
325 #ifdef RASTERCONSOLE
326 #include <machine/kbd.h>
327
328 static void fb_bell __P((int));
329
330 #if !defined(RASTERCONS_FULLSCREEN)
331 static int a2int __P((char *, int));
332
333 static int
334 a2int(cp, deflt)
335 register char *cp;
336 register int deflt;
337 {
338 register int i = 0;
339
340 if (*cp == '\0')
341 return (deflt);
342 while (*cp != '\0')
343 i = i * 10 + *cp++ - '0';
344 return (i);
345 }
346 #endif
347
348 static void
349 fb_bell(on)
350 int on;
351 {
352 #if NKBD > 0
353 (void)kbd_docmd(on?KBD_CMD_BELL:KBD_CMD_NOBELL, 0);
354 #endif
355 }
356
357 void
358 fbrcons_init(fb)
359 struct fbdevice *fb;
360 {
361 struct rconsole *rc = &fb->fb_rcons;
362 struct rasops_info *ri = &fb->fb_rinfo;
363 int maxrow, maxcol;
364 #if !defined(RASTERCONS_FULLSCREEN)
365 int *row, *col;
366 #endif
367
368 /* Set up what rasops needs to know about */
369 bzero(ri, sizeof *ri);
370 ri->ri_stride = fb->fb_linebytes;
371 ri->ri_bits = (caddr_t)fb->fb_pixels;
372 ri->ri_depth = fb->fb_type.fb_depth;
373 ri->ri_width = fb->fb_type.fb_width;
374 ri->ri_height = fb->fb_type.fb_height;
375 maxrow = 5000;
376 maxcol = 5000;
377
378 #if !defined(RASTERCONS_FULLSCREEN)
379 #if defined(SUN4)
380 if (CPU_ISSUN4) {
381 struct eeprom *eep = (struct eeprom *)eeprom_va;
382
383 if (eep == NULL) {
384 maxcol = 80;
385 maxrow = 34;
386 } else {
387 maxcol = eep->eeTtyCols;
388 maxrow = eep->eeTtyRows;
389 }
390 }
391 #endif /* SUN4 */
392 if (!CPU_ISSUN4) {
393 maxcol =
394 a2int(PROM_getpropstring(optionsnode, "screen-#columns"), 80);
395 maxrow =
396 a2int(PROM_getpropstring(optionsnode, "screen-#rows"), 34);
397 }
398 #endif /* !RASTERCONS_FULLSCREEN */
399 /*
400 * - force monochrome output
401 * - eraserows() hack to clear the *entire* display
402 * - cursor is currently enabled
403 * - center output
404 */
405 ri->ri_flg = RI_FULLCLEAR | RI_CURSOR | RI_CENTER;
406
407 /* Get operations set and connect to rcons */
408 if (rasops_init(ri, maxrow, maxcol))
409 panic("fbrcons_init: rasops_init failed!");
410
411 if (ri->ri_depth == 8) {
412 int i;
413 for (i = 0; i < 16; i++) {
414
415 /*
416 * Cmap entries are repeated four times in the
417 * 32 bit wide `devcmap' entries for optimization
418 * purposes; see rasops(9)
419 */
420 #define I_TO_DEVCMAP(i) ((i) | ((i)<<8) | ((i)<<16) | ((i)<<24))
421
422 /*
423 * Use existing colormap entries for black and white
424 */
425 if ((i & 7) == WSCOL_BLACK) {
426 ri->ri_devcmap[i] = I_TO_DEVCMAP(255);
427 continue;
428 }
429
430 if ((i & 7) == WSCOL_WHITE) {
431 ri->ri_devcmap[i] = I_TO_DEVCMAP(0);
432 continue;
433 }
434 /*
435 * Other entries refer to ANSI map, which for now
436 * is setup in bt_subr.c
437 */
438 ri->ri_devcmap[i] = I_TO_DEVCMAP(i + 1);
439 #undef I_TO_DEVCMAP
440 }
441 }
442
443 rc->rc_row = rc->rc_col = 0;
444 #if !defined(RASTERCONS_FULLSCREEN)
445 /* Determine addresses of prom emulator row and column */
446 if (!CPU_ISSUN4 && !romgetcursoraddr(&row, &col)) {
447 rc->rc_row = *row;
448 rc->rc_col = *col;
449 }
450 #endif
451 ri->ri_crow = rc->rc_row;
452 ri->ri_ccol = rc->rc_col;
453
454 rc->rc_ops = &ri->ri_ops;
455 rc->rc_cookie = ri;
456 rc->rc_bell = fb_bell;
457 rc->rc_maxcol = ri->ri_cols;
458 rc->rc_maxrow = ri->ri_rows;
459 rc->rc_width = ri->ri_emuwidth;
460 rc->rc_height = ri->ri_emuheight;
461 rc->rc_deffgcolor = WSCOL_BLACK;
462 rc->rc_defbgcolor = WSCOL_WHITE;
463 rcons_init(rc, 0);
464
465 /* Hook up virtual console */
466 v_putc = rcons_cnputc;
467 }
468
469 int
470 fbrcons_rows()
471 {
472 return (devfb ? devfb->fb_rcons.rc_maxrow : 0);
473 }
474
475 int
476 fbrcons_cols()
477 {
478 return (devfb ? devfb->fb_rcons.rc_maxcol : 0);
479 }
480 #endif /* RASTERCONSOLE */
481