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