fb.c revision 1.29 1 /* $NetBSD: fb.c,v 1.29 2009/03/14 15:36:21 dsl 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.29 2009/03/14 15:36:21 dsl 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 #include <sys/malloc.h>
57 #include <sys/types.h>
58
59 #include <machine/promlib.h>
60 #include <machine/autoconf.h>
61 #include <machine/kbd.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 "kbd.h"
69 #include "pfour.h"
70
71
72 struct fbdevlist {
73 struct fbdevice *fb_dev;
74 struct fbdevlist *fb_next;
75 };
76
77 static struct fbdevlist fblist = {
78 NULL,
79 NULL,
80 };
81
82 dev_type_open(fbopen);
83 dev_type_close(fbclose);
84 dev_type_ioctl(fbioctl);
85 dev_type_poll(fbpoll);
86 dev_type_mmap(fbmmap);
87 dev_type_kqfilter(fbkqfilter);
88
89 const struct cdevsw fb_cdevsw = {
90 fbopen, fbclose, noread, nowrite, fbioctl,
91 nostop, notty, fbpoll, fbmmap, fbkqfilter, D_OTHER
92 };
93
94 void
95 fb_unblank()
96 {
97
98 struct fbdevlist *fbl = &fblist;
99
100 while (fbl != NULL && fbl->fb_dev != NULL) {
101 (*fbl->fb_dev->fb_driver->fbd_unblank)(fbl->fb_dev->fb_device);
102 fbl = fbl->fb_next;
103 }
104 }
105
106 /*
107 * Helper function for frame buffer devices. Decides whether
108 * the device can be the console output device according to
109 * PROM info. The result from this function may not be conclusive
110 * on machines with old PROMs; in that case, drivers should consult
111 * other sources of configuration information (e.g. EEPROM entries).
112 */
113 int
114 fb_is_console(int node)
115 {
116 #if !defined(SUN4U)
117 int fbnode;
118
119 switch (prom_version()) {
120 case PROM_OLDMON:
121 /* `node' is not valid; just check for any fb device */
122 return (prom_stdout() == PROMDEV_SCREEN);
123
124 case PROM_OBP_V0:
125 /*
126 * First, check if prom_stdout() represents a frame buffer,
127 * then match on the `fb' property on the root node, if any.
128 */
129 if (prom_stdout() != PROMDEV_SCREEN)
130 return (0);
131
132 fbnode = prom_getpropint(findroot(), "fb", 0);
133 return (fbnode == 0 || node == fbnode);
134
135 case PROM_OBP_V2:
136 case PROM_OBP_V3:
137 case PROM_OPENFIRM:
138 /* Just match the nodes */
139 return (node == prom_stdout_node);
140 }
141
142 return (0);
143 #else
144 return (node == prom_stdout_node);
145 #endif
146 }
147
148 void
149 fb_attach(struct fbdevice *fb, int isconsole)
150 {
151 static int seen_force = 0;
152 int nfb = 0;
153 struct fbdevlist *fbl = &fblist;
154
155 /*
156 * Check to see if we're being forced into /dev/fb0, or if we're
157 * the console. If we are, then move/replace the current fb0.
158 */
159 if ((fb->fb_flags & FB_FORCE || (isconsole && !seen_force)) &&
160 fblist.fb_dev != NULL) {
161 while (fbl->fb_next != NULL) {
162 fbl = fbl->fb_next;
163 nfb++;
164 }
165 if ((fbl->fb_next = malloc(sizeof (struct fbdevlist),
166 M_DEVBUF, M_NOWAIT)) == NULL)
167 printf("%s: replacing %s at /dev/fb0\n",
168 device_xname(fb->fb_device),
169 device_xname(fblist.fb_dev->fb_device));
170 else {
171 fbl = fbl->fb_next;
172 nfb++;
173 fbl->fb_dev = fblist.fb_dev;
174 fbl->fb_next = NULL;
175 printf("%s: moved to /dev/fb%d\n",
176 device_xname(fbl->fb_dev->fb_device), nfb);
177 printf("%s: attached to /dev/fb0\n",
178 device_xname(fb->fb_device));
179 }
180 fblist.fb_dev = fb;
181 if (fb->fb_flags & FB_FORCE)
182 seen_force = 1;
183 /* Add to end of fb list. */
184 } else {
185 if (fblist.fb_dev != NULL) {
186 while (fbl->fb_next != NULL) {
187 fbl = fbl->fb_next;
188 nfb++;
189 }
190 if ((fbl->fb_next = malloc(sizeof (struct fbdevlist),
191 M_DEVBUF, M_NOWAIT)) == NULL) {
192 aprint_error_dev(fb->fb_device, "no space to attach after /dev/fb%d\n",
193 nfb);
194 return;
195 }
196 fbl = fbl->fb_next;
197 nfb++;
198 }
199 fbl->fb_dev = fb;
200 fbl->fb_next = NULL;
201 printf("%s: attached to /dev/fb%d\n",
202 device_xname(fbl->fb_dev->fb_device), nfb);
203 }
204 }
205
206 int
207 fbopen(dev, flags, mode, l)
208 dev_t dev;
209 int flags, mode;
210 struct lwp *l;
211 {
212 int unit, nunit;
213 struct fbdevlist *fbl = &fblist;
214
215 unit = minor(dev);
216 while (unit-- && fbl != NULL)
217 fbl = fbl->fb_next;
218 if (fbl == NULL || fbl->fb_dev == NULL)
219 return (ENXIO);
220
221 nunit = device_unit(fbl->fb_dev->fb_device);
222 return (fbl->fb_dev->fb_driver->fbd_open)(makedev(0, nunit), flags,
223 mode, l);
224 }
225
226 int
227 fbclose(dev, flags, mode, l)
228 dev_t dev;
229 int flags, mode;
230 struct lwp *l;
231 {
232 int unit, nunit;
233 struct fbdevlist *fbl = &fblist;
234
235 unit = minor(dev);
236 while (unit-- && fbl != NULL)
237 fbl = fbl->fb_next;
238 if (fbl == NULL || fbl->fb_dev == NULL)
239 return (ENXIO);
240
241 nunit = device_unit(fbl->fb_dev->fb_device);
242 return (fbl->fb_dev->fb_driver->fbd_close)(makedev(0, nunit), flags,
243 mode, l);
244 }
245
246 int
247 fbioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
248 {
249 int unit, nunit;
250 struct fbdevlist *fbl = &fblist;
251
252 unit = minor(dev);
253 while (unit-- && fbl != NULL)
254 fbl = fbl->fb_next;
255 if (fbl == NULL || fbl->fb_dev == NULL)
256 return (ENXIO);
257
258 nunit = device_unit(fbl->fb_dev->fb_device);
259 return (fbl->fb_dev->fb_driver->fbd_ioctl)(makedev(0, nunit), cmd,
260 data, flags, l);
261 }
262
263 int
264 fbpoll(dev_t dev, int events, struct lwp *l)
265 {
266 int unit, nunit;
267 struct fbdevlist *fbl = &fblist;
268
269 unit = minor(dev);
270 while (unit-- && fbl != NULL)
271 fbl = fbl->fb_next;
272 if (fbl == NULL || fbl->fb_dev == NULL)
273 return (ENXIO);
274
275 nunit = device_unit(fbl->fb_dev->fb_device);
276 return (fbl->fb_dev->fb_driver->fbd_poll)(makedev(0, nunit), events,
277 l);
278 }
279
280 int
281 fbkqfilter(dev_t dev, struct knote *kn)
282 {
283 int unit, nunit;
284 struct fbdevlist *fbl = &fblist;
285
286 unit = minor(dev);
287 while (unit-- && fbl != NULL)
288 fbl = fbl->fb_next;
289 if (fbl == NULL || fbl->fb_dev == NULL)
290 return (ENXIO);
291
292 nunit = device_unit(fbl->fb_dev->fb_device);
293 return (fbl->fb_dev->fb_driver->fbd_kqfilter)(makedev(0, nunit), kn);
294 }
295
296 paddr_t
297 fbmmap(dev_t dev, off_t off, int prot)
298 {
299 int unit, nunit;
300 struct fbdevlist *fbl = &fblist;
301
302 unit = minor(dev);
303 while (unit-- && fbl != NULL)
304 fbl = fbl->fb_next;
305 if (fbl == NULL || fbl->fb_dev == NULL)
306 return (ENXIO);
307
308 nunit = device_unit(fbl->fb_dev->fb_device);
309 paddr_t (*map)(dev_t, off_t, int) = fbl->fb_dev->fb_driver->fbd_mmap;
310
311 if (map == NULL)
312 return (-1);
313 return (map(makedev(0, nunit), off, prot));
314 }
315
316 void
317 fb_setsize_obp(fb, depth, def_width, def_height, node)
318 struct fbdevice *fb;
319 int depth, def_width, def_height, node;
320 {
321 fb->fb_type.fb_width = prom_getpropint(node, "width", def_width);
322 fb->fb_type.fb_height = prom_getpropint(node, "height", def_height);
323 fb->fb_linebytes = prom_getpropint(node, "linebytes",
324 (fb->fb_type.fb_width * depth) / 8);
325 }
326
327 void
328 fb_setsize_eeprom(fb, depth, def_width, def_height)
329 struct fbdevice *fb;
330 int depth, def_width, def_height;
331 {
332 #if !defined(SUN4U)
333 struct eeprom *eep = (struct eeprom *)eeprom_va;
334
335 if (!CPU_ISSUN4) {
336 printf("fb_setsize_eeprom: not sun4\n");
337 return;
338 }
339
340 /* Set up some defaults. */
341 fb->fb_type.fb_width = def_width;
342 fb->fb_type.fb_height = def_height;
343
344 if (fb->fb_flags & FB_PFOUR) {
345 #if NPFOUR > 0
346 fb_setsize_pfour(fb);
347 #endif
348 } else if (eep != NULL) {
349 switch (eep->eeScreenSize) {
350 case EE_SCR_1152X900:
351 fb->fb_type.fb_width = 1152;
352 fb->fb_type.fb_height = 900;
353 break;
354
355 case EE_SCR_1024X1024:
356 fb->fb_type.fb_width = 1024;
357 fb->fb_type.fb_height = 1024;
358 break;
359
360 case EE_SCR_1600X1280:
361 fb->fb_type.fb_width = 1600;
362 fb->fb_type.fb_height = 1280;
363 break;
364
365 case EE_SCR_1440X1440:
366 fb->fb_type.fb_width = 1440;
367 fb->fb_type.fb_height = 1440;
368 break;
369
370 default:
371 /*
372 * XXX: Do nothing, I guess.
373 * Should we print a warning about
374 * an unknown value? --thorpej
375 */
376 break;
377 }
378 }
379
380 fb->fb_linebytes = (fb->fb_type.fb_width * depth) / 8;
381 #endif /* !SUN4U */
382 }
383
384
385
386 #ifdef RASTERCONSOLE
387 static void fb_bell(int);
388
389 static void
390 fb_bell(int on)
391 {
392 #if NKBD > 0
393 kbd_bell(on);
394 #endif
395 }
396
397 void
398 fbrcons_init(struct fbdevice *fb)
399 {
400 struct rconsole *rc = &fb->fb_rcons;
401 struct rasops_info *ri = &fb->fb_rinfo;
402 int maxrow, maxcol;
403 #if !defined(RASTERCONS_FULLSCREEN)
404 int *row, *col;
405 #endif
406
407 /* Set up what rasops needs to know about */
408 bzero(ri, sizeof *ri);
409 ri->ri_stride = fb->fb_linebytes;
410 ri->ri_bits = (void *)fb->fb_pixels;
411 ri->ri_depth = fb->fb_type.fb_depth;
412 ri->ri_width = fb->fb_type.fb_width;
413 ri->ri_height = fb->fb_type.fb_height;
414 maxrow = 5000;
415 maxcol = 5000;
416
417 #if !defined(RASTERCONS_FULLSCREEN)
418 #if !defined(SUN4U)
419 if (CPU_ISSUN4) {
420 struct eeprom *eep = (struct eeprom *)eeprom_va;
421
422 if (eep == NULL) {
423 maxcol = 80;
424 maxrow = 34;
425 } else {
426 maxcol = eep->eeTtyCols;
427 maxrow = eep->eeTtyRows;
428 }
429 }
430 #endif /* !SUN4U */
431 if (!CPU_ISSUN4) {
432 char buf[6+1]; /* Enough for six digits */
433 maxcol = (prom_getoption("screen-#columns", buf, sizeof buf) == 0)
434 ? strtoul(buf, NULL, 10)
435 : 80;
436
437 maxrow = (prom_getoption("screen-#rows", buf, sizeof buf) != 0)
438 ? strtoul(buf, NULL, 10)
439 : 34;
440
441 }
442 #endif /* !RASTERCONS_FULLSCREEN */
443 /*
444 * - force monochrome output
445 * - eraserows() hack to clear the *entire* display
446 * - cursor is currently enabled
447 * - center output
448 */
449 ri->ri_flg = RI_FULLCLEAR | RI_CURSOR | RI_CENTER;
450
451 /* Get operations set and connect to rcons */
452 if (rasops_init(ri, maxrow, maxcol))
453 panic("fbrcons_init: rasops_init failed!");
454
455 if (ri->ri_depth == 8) {
456 int i;
457 for (i = 0; i < 16; i++) {
458
459 /*
460 * Cmap entries are repeated four times in the
461 * 32 bit wide `devcmap' entries for optimization
462 * purposes; see rasops(9)
463 */
464 #define I_TO_DEVCMAP(i) ((i) | ((i)<<8) | ((i)<<16) | ((i)<<24))
465
466 /*
467 * Use existing colormap entries for black and white
468 */
469 if ((i & 7) == WSCOL_BLACK) {
470 ri->ri_devcmap[i] = I_TO_DEVCMAP(255);
471 continue;
472 }
473
474 if ((i & 7) == WSCOL_WHITE) {
475 ri->ri_devcmap[i] = I_TO_DEVCMAP(0);
476 continue;
477 }
478 /*
479 * Other entries refer to ANSI map, which for now
480 * is setup in bt_subr.c
481 */
482 ri->ri_devcmap[i] = I_TO_DEVCMAP(i + 1);
483 #undef I_TO_DEVCMAP
484 }
485 }
486
487 rc->rc_row = rc->rc_col = 0;
488 #if !defined(RASTERCONS_FULLSCREEN)
489 /* Determine addresses of prom emulator row and column */
490 if (!CPU_ISSUN4 && !romgetcursoraddr(&row, &col)) {
491 rc->rc_row = *row;
492 rc->rc_col = *col;
493 }
494 #endif
495 ri->ri_crow = rc->rc_row;
496 ri->ri_ccol = rc->rc_col;
497
498 rc->rc_ops = &ri->ri_ops;
499 rc->rc_cookie = ri;
500 rc->rc_bell = fb_bell;
501 rc->rc_maxcol = ri->ri_cols;
502 rc->rc_maxrow = ri->ri_rows;
503 rc->rc_width = ri->ri_emuwidth;
504 rc->rc_height = ri->ri_emuheight;
505 rc->rc_deffgcolor = WSCOL_BLACK;
506 rc->rc_defbgcolor = WSCOL_WHITE;
507 rcons_init(rc, 0);
508
509 /* Hook up virtual console */
510 v_putc = rcons_cnputc;
511 }
512
513 int
514 fbrcons_rows()
515 {
516 return ((fblist.fb_dev != NULL) ?
517 fblist.fb_dev->fb_rcons.rc_maxrow : 0);
518 }
519
520 int
521 fbrcons_cols()
522 {
523 return ((fblist.fb_dev != NULL) ?
524 fblist.fb_dev->fb_rcons.rc_maxcol : 0);
525 }
526 #endif /* RASTERCONSOLE */
527