fb.c revision 1.3.4.2 1 /* $NetBSD: fb.c,v 1.3.4.2 2001/10/10 11:57:01 fvdl 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 #include <sys/vnode.h>
58
59 #include <machine/autoconf.h>
60 #include <machine/kbd.h>
61 #include <machine/conf.h>
62 #include <machine/eeprom.h>
63 #include <sparc/dev/cons.h>
64
65 #include <dev/sun/fbio.h>
66 #include <dev/sun/fbvar.h>
67
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(devvp, flags, mode, p)
200 struct vnode *devvp;
201 int flags, mode;
202 struct proc *p;
203 {
204
205 if (devfb == NULL)
206 return (ENXIO);
207 return (devfb->fb_driver->fbd_open)(devvp, flags, mode, p);
208 }
209
210 int
211 fbclose(devvp, flags, mode, p)
212 struct vnode *devvp;
213 int flags, mode;
214 struct proc *p;
215 {
216
217 return (devfb->fb_driver->fbd_close)(devvp, flags, mode, p);
218 }
219
220 int
221 fbioctl(devvp, cmd, data, flags, p)
222 struct vnode *devvp;
223 u_long cmd;
224 caddr_t data;
225 int flags;
226 struct proc *p;
227 {
228
229 return (devfb->fb_driver->fbd_ioctl)(devvp, cmd, data, flags, p);
230 }
231
232 int
233 fbpoll(devvp, events, p)
234 struct vnode *devvp;
235 int events;
236 struct proc *p;
237 {
238
239 return (devfb->fb_driver->fbd_poll)(devvp, events, p);
240 }
241
242 paddr_t
243 fbmmap(devvp, off, prot)
244 struct vnode *devvp;
245 off_t off;
246 int prot;
247 {
248 paddr_t (*map)__P((struct vnode *, off_t, int)) =
249 devfb->fb_driver->fbd_mmap;
250
251 if (map == NULL)
252 return (-1);
253 return (map(devvp, off, prot));
254 }
255
256 void
257 fb_setsize_obp(fb, depth, def_width, def_height, node)
258 struct fbdevice *fb;
259 int depth, def_width, def_height, node;
260 {
261 fb->fb_type.fb_width = PROM_getpropint(node, "width", def_width);
262 fb->fb_type.fb_height = PROM_getpropint(node, "height", def_height);
263 fb->fb_linebytes = PROM_getpropint(node, "linebytes",
264 (fb->fb_type.fb_width * depth) / 8);
265 }
266
267 void
268 fb_setsize_eeprom(fb, depth, def_width, def_height)
269 struct fbdevice *fb;
270 int depth, def_width, def_height;
271 {
272 #if defined(SUN4)
273 struct eeprom *eep = (struct eeprom *)eeprom_va;
274
275 if (!CPU_ISSUN4) {
276 printf("fb_setsize_eeprom: not sun4\n");
277 return;
278 }
279
280 /* Set up some defaults. */
281 fb->fb_type.fb_width = def_width;
282 fb->fb_type.fb_height = def_height;
283
284 if (fb->fb_flags & FB_PFOUR) {
285 #if NPFOUR > 0
286 fb_setsize_pfour(fb);
287 #endif
288 } else if (eep != NULL) {
289 switch (eep->eeScreenSize) {
290 case EE_SCR_1152X900:
291 fb->fb_type.fb_width = 1152;
292 fb->fb_type.fb_height = 900;
293 break;
294
295 case EE_SCR_1024X1024:
296 fb->fb_type.fb_width = 1024;
297 fb->fb_type.fb_height = 1024;
298 break;
299
300 case EE_SCR_1600X1280:
301 fb->fb_type.fb_width = 1600;
302 fb->fb_type.fb_height = 1280;
303 break;
304
305 case EE_SCR_1440X1440:
306 fb->fb_type.fb_width = 1440;
307 fb->fb_type.fb_height = 1440;
308 break;
309
310 default:
311 /*
312 * XXX: Do nothing, I guess.
313 * Should we print a warning about
314 * an unknown value? --thorpej
315 */
316 break;
317 }
318 }
319
320 fb->fb_linebytes = (fb->fb_type.fb_width * depth) / 8;
321 #endif /* SUN4 */
322 }
323
324
325
326 #ifdef RASTERCONSOLE
327 #include <machine/kbd.h>
328
329 static void fb_bell __P((int));
330
331 #if !defined(RASTERCONS_FULLSCREEN)
332 static int a2int __P((char *, int));
333
334 static int
335 a2int(cp, deflt)
336 register char *cp;
337 register int deflt;
338 {
339 register int i = 0;
340
341 if (*cp == '\0')
342 return (deflt);
343 while (*cp != '\0')
344 i = i * 10 + *cp++ - '0';
345 return (i);
346 }
347 #endif
348
349 static void
350 fb_bell(on)
351 int on;
352 {
353 (void)kbd_docmd(on?KBD_CMD_BELL:KBD_CMD_NOBELL, 0);
354 }
355
356 void
357 fbrcons_init(fb)
358 struct fbdevice *fb;
359 {
360 struct rconsole *rc = &fb->fb_rcons;
361 struct rasops_info *ri = &fb->fb_rinfo;
362 int maxrow, maxcol;
363 #if !defined(RASTERCONS_FULLSCREEN)
364 int *row, *col;
365 #endif
366
367 /* Set up what rasops needs to know about */
368 bzero(ri, sizeof *ri);
369 ri->ri_stride = fb->fb_linebytes;
370 ri->ri_bits = (caddr_t)fb->fb_pixels;
371 ri->ri_depth = fb->fb_type.fb_depth;
372 ri->ri_width = fb->fb_type.fb_width;
373 ri->ri_height = fb->fb_type.fb_height;
374 maxrow = 5000;
375 maxcol = 5000;
376
377 #if !defined(RASTERCONS_FULLSCREEN)
378 #if defined(SUN4)
379 if (CPU_ISSUN4) {
380 struct eeprom *eep = (struct eeprom *)eeprom_va;
381
382 if (eep == NULL) {
383 maxcol = 80;
384 maxrow = 34;
385 } else {
386 maxcol = eep->eeTtyCols;
387 maxrow = eep->eeTtyRows;
388 }
389 }
390 #endif /* SUN4 */
391 if (!CPU_ISSUN4) {
392 maxcol =
393 a2int(PROM_getpropstring(optionsnode, "screen-#columns"), 80);
394 maxrow =
395 a2int(PROM_getpropstring(optionsnode, "screen-#rows"), 34);
396 }
397 #endif /* !RASTERCONS_FULLSCREEN */
398 /*
399 * - force monochrome output
400 * - eraserows() hack to clear the *entire* display
401 * - cursor is currently enabled
402 * - center output
403 */
404 ri->ri_flg = RI_FULLCLEAR | RI_CURSOR | RI_CENTER;
405
406 /* Get operations set and connect to rcons */
407 if (rasops_init(ri, maxrow, maxcol))
408 panic("fbrcons_init: rasops_init failed!");
409
410 if (ri->ri_depth == 8) {
411 int i;
412 for (i = 0; i < 16; i++) {
413
414 /*
415 * Cmap entries are repeated four times in the
416 * 32 bit wide `devcmap' entries for optimization
417 * purposes; see rasops(9)
418 */
419 #define I_TO_DEVCMAP(i) ((i) | ((i)<<8) | ((i)<<16) | ((i)<<24))
420
421 /*
422 * Use existing colormap entries for black and white
423 */
424 if ((i & 7) == WSCOL_BLACK) {
425 ri->ri_devcmap[i] = I_TO_DEVCMAP(255);
426 continue;
427 }
428
429 if ((i & 7) == WSCOL_WHITE) {
430 ri->ri_devcmap[i] = I_TO_DEVCMAP(0);
431 continue;
432 }
433 /*
434 * Other entries refer to ANSI map, which for now
435 * is setup in bt_subr.c
436 */
437 ri->ri_devcmap[i] = I_TO_DEVCMAP(i + 1);
438 #undef I_TO_DEVCMAP
439 }
440 }
441
442 rc->rc_row = rc->rc_col = 0;
443 #if !defined(RASTERCONS_FULLSCREEN)
444 /* Determine addresses of prom emulator row and column */
445 if (!CPU_ISSUN4 && !romgetcursoraddr(&row, &col)) {
446 rc->rc_row = *row;
447 rc->rc_col = *col;
448 }
449 #endif
450 ri->ri_crow = rc->rc_row;
451 ri->ri_ccol = rc->rc_col;
452
453 rc->rc_ops = &ri->ri_ops;
454 rc->rc_cookie = ri;
455 rc->rc_bell = fb_bell;
456 rc->rc_maxcol = ri->ri_cols;
457 rc->rc_maxrow = ri->ri_rows;
458 rc->rc_width = ri->ri_emuwidth;
459 rc->rc_height = ri->ri_emuheight;
460 rc->rc_deffgcolor = WSCOL_BLACK;
461 rc->rc_defbgcolor = WSCOL_WHITE;
462 rcons_init(rc, 0);
463
464 /* Hook up virtual console */
465 v_putc = rcons_cnputc;
466 }
467
468 int
469 fbrcons_rows()
470 {
471 return (devfb ? devfb->fb_rcons.rc_maxrow : 0);
472 }
473
474 int
475 fbrcons_cols()
476 {
477 return (devfb ? devfb->fb_rcons.rc_maxcol : 0);
478 }
479 #endif /* RASTERCONSOLE */
480