genfb.c revision 1.86 1 1.86 mlelstv /* $NetBSD: genfb.c,v 1.86 2022/03/28 11:21:40 mlelstv Exp $ */
2 1.1 macallan
3 1.1 macallan /*-
4 1.1 macallan * Copyright (c) 2007 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE.
27 1.1 macallan */
28 1.1 macallan
29 1.1 macallan #include <sys/cdefs.h>
30 1.86 mlelstv __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.86 2022/03/28 11:21:40 mlelstv Exp $");
31 1.1 macallan
32 1.1 macallan #include <sys/param.h>
33 1.1 macallan #include <sys/systm.h>
34 1.1 macallan #include <sys/kernel.h>
35 1.1 macallan #include <sys/device.h>
36 1.1 macallan #include <sys/proc.h>
37 1.1 macallan #include <sys/mutex.h>
38 1.1 macallan #include <sys/ioctl.h>
39 1.1 macallan #include <sys/kernel.h>
40 1.1 macallan #include <sys/systm.h>
41 1.23 jmcneill #include <sys/kmem.h>
42 1.71 martin #include <sys/reboot.h>
43 1.1 macallan
44 1.79 jmcneill #include <uvm/uvm_extern.h>
45 1.79 jmcneill
46 1.1 macallan #include <dev/wscons/wsconsio.h>
47 1.1 macallan #include <dev/wscons/wsdisplayvar.h>
48 1.1 macallan #include <dev/rasops/rasops.h>
49 1.1 macallan #include <dev/wsfont/wsfont.h>
50 1.1 macallan
51 1.1 macallan #include <dev/wscons/wsdisplay_vconsvar.h>
52 1.1 macallan
53 1.1 macallan #include <dev/wsfb/genfbvar.h>
54 1.1 macallan
55 1.65 jmcneill #include <dev/videomode/videomode.h>
56 1.65 jmcneill #include <dev/videomode/edidvar.h>
57 1.65 jmcneill
58 1.29 ahoka #ifdef GENFB_DISABLE_TEXT
59 1.29 ahoka #define DISABLESPLASH (boothowto & (RB_SINGLE | RB_USERCONF | RB_ASKNAME | \
60 1.29 ahoka AB_VERBOSE | AB_DEBUG) )
61 1.29 ahoka #endif
62 1.29 ahoka
63 1.53 riastrad #ifdef _KERNEL_OPT
64 1.1 macallan #include "opt_genfb.h"
65 1.1 macallan #include "opt_wsfb.h"
66 1.65 jmcneill #include "opt_rasops.h"
67 1.53 riastrad #endif
68 1.1 macallan
69 1.11 macallan #ifdef GENFB_DEBUG
70 1.11 macallan #define GPRINTF panic
71 1.11 macallan #else
72 1.49 jmcneill #define GPRINTF aprint_debug
73 1.11 macallan #endif
74 1.11 macallan
75 1.42 macallan #define GENFB_BRIGHTNESS_STEP 15
76 1.65 jmcneill #define GENFB_CHAR_WIDTH_MM 3
77 1.42 macallan
78 1.1 macallan static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
79 1.1 macallan static paddr_t genfb_mmap(void *, void *, off_t, int);
80 1.52 skrll static void genfb_pollc(void *, int);
81 1.52 skrll
82 1.1 macallan static void genfb_init_screen(void *, struct vcons_screen *, int, long *);
83 1.65 jmcneill static int genfb_calc_hsize(struct genfb_softc *);
84 1.85 jmcneill static int genfb_calc_cols(struct genfb_softc *, struct rasops_info *);
85 1.1 macallan
86 1.1 macallan static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
87 1.1 macallan static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
88 1.1 macallan static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
89 1.1 macallan uint8_t, uint8_t);
90 1.45 macallan static void genfb_init_palette(struct genfb_softc *);
91 1.1 macallan
92 1.31 macallan static void genfb_brightness_up(device_t);
93 1.31 macallan static void genfb_brightness_down(device_t);
94 1.31 macallan
95 1.81 macallan #if GENFB_GLYPHCACHE > 0
96 1.81 macallan static int genfb_setup_glyphcache(struct genfb_softc *, long);
97 1.81 macallan static void genfb_putchar(void *, int, int, u_int, long);
98 1.81 macallan #endif
99 1.81 macallan
100 1.1 macallan extern const u_char rasops_cmap[768];
101 1.1 macallan
102 1.18 jmcneill static int genfb_cnattach_called = 0;
103 1.20 jmcneill static int genfb_enabled = 1;
104 1.18 jmcneill
105 1.19 jmcneill static struct genfb_softc *genfb_softc = NULL;
106 1.19 jmcneill
107 1.2 macallan void
108 1.2 macallan genfb_init(struct genfb_softc *sc)
109 1.2 macallan {
110 1.2 macallan prop_dictionary_t dict;
111 1.42 macallan uint64_t cmap_cb, pmf_cb, mode_cb, bl_cb, br_cb, fbaddr;
112 1.72 msaitoh uint64_t fboffset;
113 1.31 macallan bool console;
114 1.2 macallan
115 1.32 macallan dict = device_properties(sc->sc_dev);
116 1.2 macallan #ifdef GENFB_DEBUG
117 1.54 riastrad printf("%s", prop_dictionary_externalize(dict));
118 1.2 macallan #endif
119 1.31 macallan prop_dictionary_get_bool(dict, "is_console", &console);
120 1.31 macallan
121 1.11 macallan if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width)) {
122 1.12 macallan GPRINTF("no width property\n");
123 1.11 macallan return;
124 1.11 macallan }
125 1.11 macallan if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height)) {
126 1.12 macallan GPRINTF("no height property\n");
127 1.11 macallan return;
128 1.11 macallan }
129 1.11 macallan if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth)) {
130 1.12 macallan GPRINTF("no depth property\n");
131 1.11 macallan return;
132 1.11 macallan }
133 1.2 macallan
134 1.72 msaitoh if (!prop_dictionary_get_uint64(dict, "address", &fboffset)) {
135 1.12 macallan GPRINTF("no address property\n");
136 1.11 macallan return;
137 1.11 macallan }
138 1.11 macallan
139 1.72 msaitoh sc->sc_fboffset = (bus_addr_t)fboffset;
140 1.2 macallan
141 1.39 macallan sc->sc_fbaddr = NULL;
142 1.39 macallan if (prop_dictionary_get_uint64(dict, "virtual_address", &fbaddr)) {
143 1.40 macallan sc->sc_fbaddr = (void *)(uintptr_t)fbaddr;
144 1.39 macallan }
145 1.39 macallan
146 1.59 nonaka sc->sc_shadowfb = NULL;
147 1.59 nonaka if (!prop_dictionary_get_bool(dict, "enable_shadowfb",
148 1.59 nonaka &sc->sc_enable_shadowfb))
149 1.59 nonaka #ifdef GENFB_SHADOWFB
150 1.59 nonaka sc->sc_enable_shadowfb = true;
151 1.59 nonaka #else
152 1.59 nonaka sc->sc_enable_shadowfb = false;
153 1.59 nonaka #endif
154 1.59 nonaka
155 1.2 macallan if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
156 1.2 macallan sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
157 1.13 macallan
158 1.13 macallan /*
159 1.13 macallan * deal with a bug in the Raptor firmware which always sets
160 1.13 macallan * stride = width even when depth != 8
161 1.13 macallan */
162 1.13 macallan if (sc->sc_stride < sc->sc_width * (sc->sc_depth >> 3))
163 1.13 macallan sc->sc_stride = sc->sc_width * (sc->sc_depth >> 3);
164 1.13 macallan
165 1.6 macallan sc->sc_fbsize = sc->sc_height * sc->sc_stride;
166 1.4 macallan
167 1.4 macallan /* optional colour map callback */
168 1.4 macallan sc->sc_cmcb = NULL;
169 1.4 macallan if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
170 1.4 macallan if (cmap_cb != 0)
171 1.4 macallan sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
172 1.4 macallan }
173 1.31 macallan
174 1.28 jmcneill /* optional pmf callback */
175 1.28 jmcneill sc->sc_pmfcb = NULL;
176 1.28 jmcneill if (prop_dictionary_get_uint64(dict, "pmf_callback", &pmf_cb)) {
177 1.28 jmcneill if (pmf_cb != 0)
178 1.28 jmcneill sc->sc_pmfcb = (void *)(vaddr_t)pmf_cb;
179 1.28 jmcneill }
180 1.31 macallan
181 1.34 jmcneill /* optional mode callback */
182 1.34 jmcneill sc->sc_modecb = NULL;
183 1.34 jmcneill if (prop_dictionary_get_uint64(dict, "mode_callback", &mode_cb)) {
184 1.34 jmcneill if (mode_cb != 0)
185 1.34 jmcneill sc->sc_modecb = (void *)(vaddr_t)mode_cb;
186 1.34 jmcneill }
187 1.34 jmcneill
188 1.31 macallan /* optional backlight control callback */
189 1.31 macallan sc->sc_backlight = NULL;
190 1.31 macallan if (prop_dictionary_get_uint64(dict, "backlight_callback", &bl_cb)) {
191 1.31 macallan if (bl_cb != 0) {
192 1.31 macallan sc->sc_backlight = (void *)(vaddr_t)bl_cb;
193 1.42 macallan aprint_naive_dev(sc->sc_dev,
194 1.31 macallan "enabling backlight control\n");
195 1.42 macallan }
196 1.42 macallan }
197 1.42 macallan
198 1.42 macallan /* optional brightness control callback */
199 1.42 macallan sc->sc_brightness = NULL;
200 1.42 macallan if (prop_dictionary_get_uint64(dict, "brightness_callback", &br_cb)) {
201 1.42 macallan if (br_cb != 0) {
202 1.42 macallan sc->sc_brightness = (void *)(vaddr_t)br_cb;
203 1.42 macallan aprint_naive_dev(sc->sc_dev,
204 1.42 macallan "enabling brightness control\n");
205 1.42 macallan if (console &&
206 1.42 macallan sc->sc_brightness->gpc_upd_parameter != NULL) {
207 1.42 macallan pmf_event_register(sc->sc_dev,
208 1.31 macallan PMFE_DISPLAY_BRIGHTNESS_UP,
209 1.31 macallan genfb_brightness_up, TRUE);
210 1.42 macallan pmf_event_register(sc->sc_dev,
211 1.31 macallan PMFE_DISPLAY_BRIGHTNESS_DOWN,
212 1.31 macallan genfb_brightness_down, TRUE);
213 1.31 macallan }
214 1.31 macallan }
215 1.31 macallan }
216 1.2 macallan }
217 1.2 macallan
218 1.2 macallan int
219 1.2 macallan genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
220 1.1 macallan {
221 1.1 macallan struct wsemuldisplaydev_attach_args aa;
222 1.1 macallan prop_dictionary_t dict;
223 1.1 macallan struct rasops_info *ri;
224 1.79 jmcneill paddr_t fb_phys;
225 1.21 jmcneill uint16_t crow;
226 1.1 macallan long defattr;
227 1.7 macallan bool console;
228 1.33 jmcneill #ifdef SPLASHSCREEN
229 1.45 macallan int i, j;
230 1.33 jmcneill int error = ENXIO;
231 1.33 jmcneill #endif
232 1.9 jmmv
233 1.32 macallan dict = device_properties(sc->sc_dev);
234 1.14 phx prop_dictionary_get_bool(dict, "is_console", &console);
235 1.14 phx
236 1.21 jmcneill if (prop_dictionary_get_uint16(dict, "cursor-row", &crow) == false)
237 1.21 jmcneill crow = 0;
238 1.21 jmcneill if (prop_dictionary_get_bool(dict, "clear-screen", &sc->sc_want_clear)
239 1.21 jmcneill == false)
240 1.21 jmcneill sc->sc_want_clear = true;
241 1.21 jmcneill
242 1.79 jmcneill fb_phys = (paddr_t)sc->sc_fboffset;
243 1.79 jmcneill if (fb_phys == 0) {
244 1.79 jmcneill KASSERT(sc->sc_fbaddr != NULL);
245 1.79 jmcneill (void)pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_fbaddr,
246 1.79 jmcneill &fb_phys);
247 1.79 jmcneill }
248 1.79 jmcneill
249 1.42 macallan aprint_verbose_dev(sc->sc_dev, "framebuffer at %p, size %dx%d, depth %d, "
250 1.42 macallan "stride %d\n",
251 1.79 jmcneill fb_phys ? (void *)(intptr_t)fb_phys : sc->sc_fbaddr,
252 1.9 jmmv sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
253 1.9 jmmv
254 1.1 macallan sc->sc_defaultscreen_descr = (struct wsscreen_descr){
255 1.1 macallan "default",
256 1.1 macallan 0, 0,
257 1.1 macallan NULL,
258 1.1 macallan 8, 16,
259 1.60 macallan WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
260 1.60 macallan WSSCREEN_RESIZE,
261 1.3 macallan NULL
262 1.1 macallan };
263 1.1 macallan sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
264 1.1 macallan sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
265 1.1 macallan memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
266 1.1 macallan sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
267 1.36 phx if (sc->sc_modecb != NULL)
268 1.34 jmcneill sc->sc_modecb->gmc_setmode(sc, sc->sc_mode);
269 1.1 macallan
270 1.41 macallan sc->sc_accessops.ioctl = genfb_ioctl;
271 1.41 macallan sc->sc_accessops.mmap = genfb_mmap;
272 1.52 skrll sc->sc_accessops.pollc = genfb_pollc;
273 1.41 macallan
274 1.59 nonaka if (sc->sc_enable_shadowfb) {
275 1.59 nonaka sc->sc_shadowfb = kmem_alloc(sc->sc_fbsize, KM_SLEEP);
276 1.61 chs if (sc->sc_want_clear == false)
277 1.59 nonaka memcpy(sc->sc_shadowfb, sc->sc_fbaddr, sc->sc_fbsize);
278 1.68 rin aprint_verbose_dev(sc->sc_dev,
279 1.68 rin "shadow framebuffer enabled, size %zu KB\n",
280 1.68 rin sc->sc_fbsize >> 10);
281 1.59 nonaka }
282 1.5 macallan
283 1.1 macallan vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
284 1.41 macallan &sc->sc_accessops);
285 1.1 macallan sc->vd.init_screen = genfb_init_screen;
286 1.1 macallan
287 1.10 jmmv /* Do not print anything between this point and the screen
288 1.10 jmmv * clear operation below. Otherwise it will be lost. */
289 1.10 jmmv
290 1.1 macallan ri = &sc->sc_console_screen.scr_ri;
291 1.1 macallan
292 1.14 phx vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
293 1.14 phx &defattr);
294 1.14 phx sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
295 1.14 phx
296 1.81 macallan #if GENFB_GLYPHCACHE > 0
297 1.81 macallan genfb_setup_glyphcache(sc, defattr);
298 1.81 macallan #endif
299 1.81 macallan
300 1.29 ahoka #ifdef SPLASHSCREEN
301 1.29 ahoka /*
302 1.29 ahoka * If system isn't going to go multiuser, or user has requested to see
303 1.29 ahoka * boot text, don't render splash screen immediately
304 1.29 ahoka */
305 1.29 ahoka if (DISABLESPLASH)
306 1.29 ahoka #endif
307 1.29 ahoka vcons_redraw_screen(&sc->sc_console_screen);
308 1.29 ahoka
309 1.14 phx sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
310 1.14 phx sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
311 1.14 phx sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
312 1.14 phx sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
313 1.41 macallan
314 1.44 macallan if (crow >= ri->ri_rows) {
315 1.44 macallan crow = 0;
316 1.44 macallan sc->sc_want_clear = 1;
317 1.44 macallan }
318 1.44 macallan
319 1.41 macallan if (console)
320 1.41 macallan wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, crow,
321 1.41 macallan defattr);
322 1.1 macallan
323 1.10 jmmv /* Clear the whole screen to bring it to a known state. */
324 1.21 jmcneill if (sc->sc_want_clear)
325 1.21 jmcneill (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
326 1.10 jmmv
327 1.45 macallan #ifdef SPLASHSCREEN
328 1.1 macallan j = 0;
329 1.64 riastrad for (i = 0; i < uimin(1 << sc->sc_depth, 256); i++) {
330 1.33 jmcneill if (i >= SPLASH_CMAP_OFFSET &&
331 1.29 ahoka i < SPLASH_CMAP_OFFSET + SPLASH_CMAP_SIZE) {
332 1.33 jmcneill splash_get_cmap(i,
333 1.33 jmcneill &sc->sc_cmap_red[i],
334 1.33 jmcneill &sc->sc_cmap_green[i],
335 1.33 jmcneill &sc->sc_cmap_blue[i]);
336 1.29 ahoka } else {
337 1.29 ahoka sc->sc_cmap_red[i] = rasops_cmap[j];
338 1.29 ahoka sc->sc_cmap_green[i] = rasops_cmap[j + 1];
339 1.29 ahoka sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
340 1.29 ahoka }
341 1.29 ahoka j += 3;
342 1.1 macallan }
343 1.33 jmcneill genfb_restore_palette(sc);
344 1.1 macallan
345 1.29 ahoka sc->sc_splash.si_depth = sc->sc_depth;
346 1.58 nat sc->sc_splash.si_bits = sc->sc_console_screen.scr_ri.ri_origbits;
347 1.29 ahoka sc->sc_splash.si_hwbits = sc->sc_fbaddr;
348 1.29 ahoka sc->sc_splash.si_width = sc->sc_width;
349 1.29 ahoka sc->sc_splash.si_height = sc->sc_height;
350 1.29 ahoka sc->sc_splash.si_stride = sc->sc_stride;
351 1.29 ahoka sc->sc_splash.si_fillrect = NULL;
352 1.33 jmcneill if (!DISABLESPLASH) {
353 1.33 jmcneill error = splash_render(&sc->sc_splash,
354 1.33 jmcneill SPLASH_F_CENTER|SPLASH_F_FILL);
355 1.33 jmcneill if (error) {
356 1.33 jmcneill SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
357 1.45 macallan genfb_init_palette(sc);
358 1.33 jmcneill vcons_replay_msgbuf(&sc->sc_console_screen);
359 1.33 jmcneill }
360 1.33 jmcneill }
361 1.29 ahoka #else
362 1.45 macallan genfb_init_palette(sc);
363 1.71 martin if (console && (boothowto & (AB_SILENT|AB_QUIET)) == 0)
364 1.53 riastrad vcons_replay_msgbuf(&sc->sc_console_screen);
365 1.29 ahoka #endif
366 1.27 macallan
367 1.19 jmcneill if (genfb_softc == NULL)
368 1.19 jmcneill genfb_softc = sc;
369 1.19 jmcneill
370 1.1 macallan aa.console = console;
371 1.1 macallan aa.scrdata = &sc->sc_screenlist;
372 1.41 macallan aa.accessops = &sc->sc_accessops;
373 1.1 macallan aa.accesscookie = &sc->vd;
374 1.1 macallan
375 1.29 ahoka #ifdef GENFB_DISABLE_TEXT
376 1.33 jmcneill if (!DISABLESPLASH && error == 0)
377 1.29 ahoka SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
378 1.29 ahoka #endif
379 1.29 ahoka
380 1.82 thorpej config_found(sc->sc_dev, &aa, wsemuldisplaydevprint,
381 1.83 thorpej CFARGS(.iattr = "wsemuldisplaydev"));
382 1.1 macallan
383 1.1 macallan return 0;
384 1.1 macallan }
385 1.1 macallan
386 1.1 macallan static int
387 1.1 macallan genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
388 1.1 macallan struct lwp *l)
389 1.1 macallan {
390 1.1 macallan struct vcons_data *vd = v;
391 1.1 macallan struct genfb_softc *sc = vd->cookie;
392 1.1 macallan struct wsdisplay_fbinfo *wdf;
393 1.1 macallan struct vcons_screen *ms = vd->active;
394 1.31 macallan struct wsdisplay_param *param;
395 1.56 macallan int new_mode, error, val, ret;
396 1.1 macallan
397 1.1 macallan switch (cmd) {
398 1.1 macallan case WSDISPLAYIO_GINFO:
399 1.6 macallan if (ms == NULL)
400 1.6 macallan return ENODEV;
401 1.1 macallan wdf = (void *)data;
402 1.1 macallan wdf->height = ms->scr_ri.ri_height;
403 1.1 macallan wdf->width = ms->scr_ri.ri_width;
404 1.1 macallan wdf->depth = ms->scr_ri.ri_depth;
405 1.1 macallan wdf->cmsize = 256;
406 1.1 macallan return 0;
407 1.1 macallan
408 1.1 macallan case WSDISPLAYIO_GETCMAP:
409 1.1 macallan return genfb_getcmap(sc,
410 1.1 macallan (struct wsdisplay_cmap *)data);
411 1.1 macallan
412 1.1 macallan case WSDISPLAYIO_PUTCMAP:
413 1.1 macallan return genfb_putcmap(sc,
414 1.1 macallan (struct wsdisplay_cmap *)data);
415 1.1 macallan
416 1.3 macallan case WSDISPLAYIO_LINEBYTES:
417 1.3 macallan *(u_int *)data = sc->sc_stride;
418 1.3 macallan return 0;
419 1.3 macallan
420 1.1 macallan case WSDISPLAYIO_SMODE:
421 1.24 jmcneill new_mode = *(int *)data;
422 1.11 macallan
423 1.24 jmcneill /* notify the bus backend */
424 1.24 jmcneill error = 0;
425 1.24 jmcneill if (sc->sc_ops.genfb_ioctl)
426 1.24 jmcneill error = sc->sc_ops.genfb_ioctl(sc, vs,
427 1.11 macallan cmd, data, flag, l);
428 1.50 jmcneill if (error && error != EPASSTHROUGH)
429 1.24 jmcneill return error;
430 1.11 macallan
431 1.24 jmcneill if (new_mode != sc->sc_mode) {
432 1.24 jmcneill sc->sc_mode = new_mode;
433 1.36 phx if (sc->sc_modecb != NULL)
434 1.34 jmcneill sc->sc_modecb->gmc_setmode(sc,
435 1.34 jmcneill sc->sc_mode);
436 1.24 jmcneill if (new_mode == WSDISPLAYIO_MODE_EMUL) {
437 1.24 jmcneill genfb_restore_palette(sc);
438 1.24 jmcneill vcons_redraw_screen(ms);
439 1.1 macallan }
440 1.1 macallan }
441 1.1 macallan return 0;
442 1.51 macallan
443 1.29 ahoka case WSDISPLAYIO_SSPLASH:
444 1.29 ahoka #if defined(SPLASHSCREEN)
445 1.29 ahoka if(*(int *)data == 1) {
446 1.29 ahoka SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
447 1.29 ahoka splash_render(&sc->sc_splash,
448 1.29 ahoka SPLASH_F_CENTER|SPLASH_F_FILL);
449 1.29 ahoka } else {
450 1.29 ahoka SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
451 1.45 macallan genfb_init_palette(sc);
452 1.29 ahoka }
453 1.29 ahoka vcons_redraw_screen(ms);
454 1.29 ahoka return 0;
455 1.29 ahoka #else
456 1.29 ahoka return ENODEV;
457 1.29 ahoka #endif
458 1.31 macallan case WSDISPLAYIO_GETPARAM:
459 1.31 macallan param = (struct wsdisplay_param *)data;
460 1.31 macallan switch (param->param) {
461 1.31 macallan case WSDISPLAYIO_PARAM_BRIGHTNESS:
462 1.42 macallan if (sc->sc_brightness == NULL)
463 1.42 macallan return EPASSTHROUGH;
464 1.31 macallan param->min = 0;
465 1.31 macallan param->max = 255;
466 1.42 macallan return sc->sc_brightness->gpc_get_parameter(
467 1.42 macallan sc->sc_brightness->gpc_cookie,
468 1.42 macallan ¶m->curval);
469 1.31 macallan case WSDISPLAYIO_PARAM_BACKLIGHT:
470 1.42 macallan if (sc->sc_backlight == NULL)
471 1.42 macallan return EPASSTHROUGH;
472 1.31 macallan param->min = 0;
473 1.31 macallan param->max = 1;
474 1.42 macallan return sc->sc_backlight->gpc_get_parameter(
475 1.42 macallan sc->sc_backlight->gpc_cookie,
476 1.42 macallan ¶m->curval);
477 1.31 macallan }
478 1.31 macallan return EPASSTHROUGH;
479 1.31 macallan
480 1.31 macallan case WSDISPLAYIO_SETPARAM:
481 1.31 macallan param = (struct wsdisplay_param *)data;
482 1.31 macallan switch (param->param) {
483 1.31 macallan case WSDISPLAYIO_PARAM_BRIGHTNESS:
484 1.42 macallan if (sc->sc_brightness == NULL)
485 1.42 macallan return EPASSTHROUGH;
486 1.42 macallan val = param->curval;
487 1.42 macallan if (val < 0) val = 0;
488 1.42 macallan if (val > 255) val = 255;
489 1.42 macallan return sc->sc_brightness->gpc_set_parameter(
490 1.42 macallan sc->sc_brightness->gpc_cookie, val);
491 1.31 macallan case WSDISPLAYIO_PARAM_BACKLIGHT:
492 1.42 macallan if (sc->sc_backlight == NULL)
493 1.42 macallan return EPASSTHROUGH;
494 1.42 macallan val = param->curval;
495 1.42 macallan if (val < 0) val = 0;
496 1.42 macallan if (val > 1) val = 1;
497 1.42 macallan return sc->sc_backlight->gpc_set_parameter(
498 1.42 macallan sc->sc_backlight->gpc_cookie, val);
499 1.31 macallan }
500 1.56 macallan return EPASSTHROUGH;
501 1.56 macallan }
502 1.56 macallan ret = EPASSTHROUGH;
503 1.56 macallan if (sc->sc_ops.genfb_ioctl)
504 1.56 macallan ret = sc->sc_ops.genfb_ioctl(sc, vs, cmd, data, flag, l);
505 1.56 macallan if (ret != EPASSTHROUGH)
506 1.56 macallan return ret;
507 1.56 macallan /*
508 1.56 macallan * XXX
509 1.56 macallan * handle these only if there either is no ioctl() handler or it didn't
510 1.56 macallan * know how to deal with them. This allows bus frontends to override
511 1.56 macallan * them but still provides fallback implementations
512 1.56 macallan */
513 1.56 macallan switch (cmd) {
514 1.42 macallan case WSDISPLAYIO_GET_EDID: {
515 1.42 macallan struct wsdisplayio_edid_info *d = data;
516 1.42 macallan return wsdisplayio_get_edid(sc->sc_dev, d);
517 1.42 macallan }
518 1.51 macallan
519 1.51 macallan case WSDISPLAYIO_GET_FBINFO: {
520 1.51 macallan struct wsdisplayio_fbinfo *fbi = data;
521 1.77 rin return wsdisplayio_get_fbinfo(&ms->scr_ri, fbi);
522 1.51 macallan }
523 1.1 macallan }
524 1.1 macallan return EPASSTHROUGH;
525 1.1 macallan }
526 1.1 macallan
527 1.1 macallan static paddr_t
528 1.1 macallan genfb_mmap(void *v, void *vs, off_t offset, int prot)
529 1.1 macallan {
530 1.1 macallan struct vcons_data *vd = v;
531 1.1 macallan struct genfb_softc *sc = vd->cookie;
532 1.1 macallan
533 1.1 macallan if (sc->sc_ops.genfb_mmap)
534 1.1 macallan return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
535 1.1 macallan
536 1.1 macallan return -1;
537 1.1 macallan }
538 1.1 macallan
539 1.1 macallan static void
540 1.52 skrll genfb_pollc(void *v, int on)
541 1.52 skrll {
542 1.52 skrll struct vcons_data *vd = v;
543 1.52 skrll struct genfb_softc *sc = vd->cookie;
544 1.52 skrll
545 1.52 skrll if (sc == NULL)
546 1.52 skrll return;
547 1.52 skrll
548 1.52 skrll if (on)
549 1.52 skrll genfb_enable_polling(sc->sc_dev);
550 1.52 skrll else
551 1.52 skrll genfb_disable_polling(sc->sc_dev);
552 1.52 skrll }
553 1.52 skrll
554 1.52 skrll static void
555 1.1 macallan genfb_init_screen(void *cookie, struct vcons_screen *scr,
556 1.1 macallan int existing, long *defattr)
557 1.1 macallan {
558 1.1 macallan struct genfb_softc *sc = cookie;
559 1.1 macallan struct rasops_info *ri = &scr->scr_ri;
560 1.65 jmcneill int wantcols;
561 1.84 jmcneill bool is_bgr, is_swapped, is_10bit;
562 1.1 macallan
563 1.1 macallan ri->ri_depth = sc->sc_depth;
564 1.1 macallan ri->ri_width = sc->sc_width;
565 1.1 macallan ri->ri_height = sc->sc_height;
566 1.1 macallan ri->ri_stride = sc->sc_stride;
567 1.21 jmcneill ri->ri_flg = RI_CENTER;
568 1.21 jmcneill if (sc->sc_want_clear)
569 1.21 jmcneill ri->ri_flg |= RI_FULLCLEAR;
570 1.1 macallan
571 1.60 macallan scr->scr_flags |= VCONS_LOADFONT;
572 1.60 macallan
573 1.5 macallan if (sc->sc_shadowfb != NULL) {
574 1.5 macallan ri->ri_hwbits = (char *)sc->sc_fbaddr;
575 1.5 macallan ri->ri_bits = (char *)sc->sc_shadowfb;
576 1.59 nonaka } else {
577 1.5 macallan ri->ri_bits = (char *)sc->sc_fbaddr;
578 1.30 macallan scr->scr_flags |= VCONS_DONT_READ;
579 1.30 macallan }
580 1.1 macallan
581 1.67 rin if (existing && sc->sc_want_clear)
582 1.1 macallan ri->ri_flg |= RI_CLEAR;
583 1.1 macallan
584 1.69 rin switch (ri->ri_depth) {
585 1.69 rin case 32:
586 1.69 rin case 24:
587 1.67 rin ri->ri_flg |= RI_ENABLE_ALPHA;
588 1.67 rin
589 1.70 rin is_bgr = false;
590 1.48 macallan prop_dictionary_get_bool(device_properties(sc->sc_dev),
591 1.48 macallan "is_bgr", &is_bgr);
592 1.78 rin
593 1.78 rin is_swapped = false;
594 1.78 rin prop_dictionary_get_bool(device_properties(sc->sc_dev),
595 1.78 rin "is_swapped", &is_swapped);
596 1.78 rin
597 1.84 jmcneill is_10bit = false;
598 1.84 jmcneill prop_dictionary_get_bool(device_properties(sc->sc_dev),
599 1.84 jmcneill "is_10bit", &is_10bit);
600 1.84 jmcneill
601 1.84 jmcneill const int bits = is_10bit ? 10 : 8;
602 1.84 jmcneill ri->ri_rnum = ri->ri_gnum = ri->ri_bnum = bits;
603 1.84 jmcneill
604 1.48 macallan if (is_bgr) {
605 1.48 macallan /* someone requested BGR */
606 1.84 jmcneill ri->ri_rpos = bits * 0;
607 1.84 jmcneill ri->ri_gpos = bits * 1;
608 1.84 jmcneill ri->ri_bpos = bits * 2;
609 1.78 rin } else if (is_swapped) {
610 1.78 rin /* byte-swapped, must be 32 bpp */
611 1.84 jmcneill KASSERT(ri->ri_depth == 32 && bits == 8);
612 1.78 rin ri->ri_rpos = 8;
613 1.78 rin ri->ri_gpos = 16;
614 1.78 rin ri->ri_bpos = 24;
615 1.48 macallan } else {
616 1.48 macallan /* assume RGB */
617 1.84 jmcneill ri->ri_rpos = bits * 2;
618 1.84 jmcneill ri->ri_gpos = bits * 1;
619 1.84 jmcneill ri->ri_bpos = bits * 0;
620 1.48 macallan }
621 1.69 rin break;
622 1.69 rin
623 1.69 rin case 16:
624 1.69 rin case 15:
625 1.69 rin ri->ri_flg |= RI_ENABLE_ALPHA;
626 1.69 rin break;
627 1.69 rin
628 1.69 rin case 8:
629 1.69 rin if (sc->sc_cmcb != NULL)
630 1.69 rin ri->ri_flg |= RI_ENABLE_ALPHA | RI_8BIT_IS_RGB;
631 1.69 rin break;
632 1.43 macallan
633 1.69 rin case 2:
634 1.66 rin ri->ri_flg |= RI_ENABLE_ALPHA;
635 1.69 rin break;
636 1.66 rin
637 1.69 rin default:
638 1.69 rin break;
639 1.69 rin }
640 1.45 macallan
641 1.85 jmcneill wantcols = genfb_calc_cols(sc, ri);
642 1.45 macallan
643 1.65 jmcneill rasops_init(ri, 0, wantcols);
644 1.60 macallan ri->ri_caps = WSSCREEN_WSCOLORS | WSSCREEN_HILIT | WSSCREEN_UNDERLINE |
645 1.60 macallan WSSCREEN_RESIZE;
646 1.1 macallan rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
647 1.1 macallan sc->sc_width / ri->ri_font->fontwidth);
648 1.1 macallan
649 1.1 macallan ri->ri_hw = scr;
650 1.81 macallan #if GENFB_GLYPHCACHE > 0
651 1.81 macallan sc->sc_putchar = ri->ri_ops.putchar;
652 1.81 macallan ri->ri_ops.putchar = genfb_putchar;
653 1.81 macallan #endif
654 1.29 ahoka #ifdef GENFB_DISABLE_TEXT
655 1.29 ahoka if (scr == &sc->sc_console_screen && !DISABLESPLASH)
656 1.29 ahoka SCREEN_DISABLE_DRAWING(&sc->sc_console_screen);
657 1.29 ahoka #endif
658 1.1 macallan }
659 1.1 macallan
660 1.65 jmcneill /* Returns the width of the display in millimeters, or 0 if not known. */
661 1.65 jmcneill static int
662 1.65 jmcneill genfb_calc_hsize(struct genfb_softc *sc)
663 1.65 jmcneill {
664 1.65 jmcneill device_t dev = sc->sc_dev;
665 1.65 jmcneill prop_dictionary_t dict = device_properties(dev);
666 1.65 jmcneill prop_data_t edid_data;
667 1.73 jdolecek struct edid_info *edid;
668 1.65 jmcneill const char *edid_ptr;
669 1.73 jdolecek int hsize;
670 1.65 jmcneill
671 1.65 jmcneill edid_data = prop_dictionary_get(dict, "EDID");
672 1.65 jmcneill if (edid_data == NULL || prop_data_size(edid_data) < 128)
673 1.65 jmcneill return 0;
674 1.65 jmcneill
675 1.73 jdolecek edid = kmem_alloc(sizeof(*edid), KM_SLEEP);
676 1.73 jdolecek
677 1.74 thorpej edid_ptr = prop_data_value(edid_data);
678 1.73 jdolecek if (edid_parse(__UNCONST(edid_ptr), edid) == 0)
679 1.73 jdolecek hsize = (int)edid->edid_max_hsize * 10;
680 1.73 jdolecek else
681 1.73 jdolecek hsize = 0;
682 1.73 jdolecek
683 1.73 jdolecek kmem_free(edid, sizeof(*edid));
684 1.65 jmcneill
685 1.73 jdolecek return hsize;
686 1.65 jmcneill }
687 1.65 jmcneill
688 1.65 jmcneill /* Return the minimum number of character columns based on DPI */
689 1.65 jmcneill static int
690 1.85 jmcneill genfb_calc_cols(struct genfb_softc *sc, struct rasops_info *ri)
691 1.65 jmcneill {
692 1.65 jmcneill const int hsize = genfb_calc_hsize(sc);
693 1.65 jmcneill
694 1.85 jmcneill if (hsize != 0) {
695 1.85 jmcneill ri->ri_flg |= RI_PREFER_WIDEFONT;
696 1.85 jmcneill }
697 1.85 jmcneill
698 1.65 jmcneill return MAX(RASOPS_DEFAULT_WIDTH, hsize / GENFB_CHAR_WIDTH_MM);
699 1.65 jmcneill }
700 1.65 jmcneill
701 1.1 macallan static int
702 1.1 macallan genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
703 1.1 macallan {
704 1.1 macallan u_char *r, *g, *b;
705 1.1 macallan u_int index = cm->index;
706 1.1 macallan u_int count = cm->count;
707 1.1 macallan int i, error;
708 1.1 macallan u_char rbuf[256], gbuf[256], bbuf[256];
709 1.1 macallan
710 1.1 macallan #ifdef GENFB_DEBUG
711 1.1 macallan aprint_debug("putcmap: %d %d\n",index, count);
712 1.1 macallan #endif
713 1.63 mlelstv if (index >= 256 || count > 256 || index + count > 256)
714 1.1 macallan return EINVAL;
715 1.63 mlelstv
716 1.1 macallan error = copyin(cm->red, &rbuf[index], count);
717 1.1 macallan if (error)
718 1.1 macallan return error;
719 1.1 macallan error = copyin(cm->green, &gbuf[index], count);
720 1.1 macallan if (error)
721 1.1 macallan return error;
722 1.1 macallan error = copyin(cm->blue, &bbuf[index], count);
723 1.1 macallan if (error)
724 1.1 macallan return error;
725 1.1 macallan
726 1.1 macallan memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
727 1.1 macallan memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
728 1.1 macallan memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
729 1.1 macallan
730 1.1 macallan r = &sc->sc_cmap_red[index];
731 1.1 macallan g = &sc->sc_cmap_green[index];
732 1.1 macallan b = &sc->sc_cmap_blue[index];
733 1.1 macallan
734 1.1 macallan for (i = 0; i < count; i++) {
735 1.1 macallan genfb_putpalreg(sc, index, *r, *g, *b);
736 1.1 macallan index++;
737 1.1 macallan r++, g++, b++;
738 1.1 macallan }
739 1.1 macallan return 0;
740 1.1 macallan }
741 1.1 macallan
742 1.1 macallan static int
743 1.1 macallan genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
744 1.1 macallan {
745 1.1 macallan u_int index = cm->index;
746 1.1 macallan u_int count = cm->count;
747 1.1 macallan int error;
748 1.1 macallan
749 1.63 mlelstv if (index >= 256 || count > 256 || index + count > 256)
750 1.1 macallan return EINVAL;
751 1.1 macallan
752 1.1 macallan error = copyout(&sc->sc_cmap_red[index], cm->red, count);
753 1.1 macallan if (error)
754 1.1 macallan return error;
755 1.1 macallan error = copyout(&sc->sc_cmap_green[index], cm->green, count);
756 1.1 macallan if (error)
757 1.1 macallan return error;
758 1.1 macallan error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
759 1.1 macallan if (error)
760 1.1 macallan return error;
761 1.1 macallan
762 1.1 macallan return 0;
763 1.1 macallan }
764 1.1 macallan
765 1.28 jmcneill void
766 1.1 macallan genfb_restore_palette(struct genfb_softc *sc)
767 1.1 macallan {
768 1.1 macallan int i;
769 1.1 macallan
770 1.27 macallan if (sc->sc_depth <= 8) {
771 1.27 macallan for (i = 0; i < (1 << sc->sc_depth); i++) {
772 1.27 macallan genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
773 1.27 macallan sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
774 1.27 macallan }
775 1.1 macallan }
776 1.1 macallan }
777 1.1 macallan
778 1.45 macallan static void
779 1.45 macallan genfb_init_palette(struct genfb_softc *sc)
780 1.45 macallan {
781 1.45 macallan int i, j, tmp;
782 1.45 macallan
783 1.45 macallan if (sc->sc_depth == 8) {
784 1.45 macallan /* generate an r3g3b2 colour map */
785 1.45 macallan for (i = 0; i < 256; i++) {
786 1.45 macallan tmp = i & 0xe0;
787 1.45 macallan /*
788 1.45 macallan * replicate bits so 0xe0 maps to a red value of 0xff
789 1.45 macallan * in order to make white look actually white
790 1.45 macallan */
791 1.45 macallan tmp |= (tmp >> 3) | (tmp >> 6);
792 1.45 macallan sc->sc_cmap_red[i] = tmp;
793 1.45 macallan
794 1.45 macallan tmp = (i & 0x1c) << 3;
795 1.45 macallan tmp |= (tmp >> 3) | (tmp >> 6);
796 1.45 macallan sc->sc_cmap_green[i] = tmp;
797 1.45 macallan
798 1.45 macallan tmp = (i & 0x03) << 6;
799 1.45 macallan tmp |= tmp >> 2;
800 1.45 macallan tmp |= tmp >> 4;
801 1.45 macallan sc->sc_cmap_blue[i] = tmp;
802 1.45 macallan
803 1.45 macallan genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
804 1.45 macallan sc->sc_cmap_green[i],
805 1.45 macallan sc->sc_cmap_blue[i]);
806 1.45 macallan }
807 1.45 macallan } else {
808 1.45 macallan /* steal rasops' ANSI cmap */
809 1.45 macallan j = 0;
810 1.45 macallan for (i = 0; i < 256; i++) {
811 1.45 macallan sc->sc_cmap_red[i] = rasops_cmap[j];
812 1.45 macallan sc->sc_cmap_green[i] = rasops_cmap[j + 1];
813 1.45 macallan sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
814 1.45 macallan j += 3;
815 1.45 macallan }
816 1.45 macallan }
817 1.45 macallan }
818 1.45 macallan
819 1.1 macallan static int
820 1.1 macallan genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
821 1.1 macallan uint8_t b)
822 1.1 macallan {
823 1.1 macallan
824 1.4 macallan if (sc->sc_cmcb) {
825 1.4 macallan
826 1.4 macallan sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
827 1.4 macallan idx, r, g, b);
828 1.4 macallan }
829 1.1 macallan return 0;
830 1.1 macallan }
831 1.1 macallan
832 1.18 jmcneill void
833 1.18 jmcneill genfb_cnattach(void)
834 1.18 jmcneill {
835 1.18 jmcneill genfb_cnattach_called = 1;
836 1.18 jmcneill }
837 1.18 jmcneill
838 1.86 mlelstv int
839 1.86 mlelstv genfb_cndetach(void)
840 1.86 mlelstv {
841 1.86 mlelstv
842 1.86 mlelstv if (genfb_cnattach_called) {
843 1.86 mlelstv genfb_cnattach_called = 0;
844 1.86 mlelstv return 1;
845 1.86 mlelstv }
846 1.86 mlelstv return 0;
847 1.86 mlelstv }
848 1.86 mlelstv
849 1.20 jmcneill void
850 1.20 jmcneill genfb_disable(void)
851 1.20 jmcneill {
852 1.20 jmcneill genfb_enabled = 0;
853 1.20 jmcneill }
854 1.20 jmcneill
855 1.18 jmcneill int
856 1.18 jmcneill genfb_is_console(void)
857 1.18 jmcneill {
858 1.18 jmcneill return genfb_cnattach_called;
859 1.18 jmcneill }
860 1.19 jmcneill
861 1.19 jmcneill int
862 1.20 jmcneill genfb_is_enabled(void)
863 1.20 jmcneill {
864 1.20 jmcneill return genfb_enabled;
865 1.20 jmcneill }
866 1.20 jmcneill
867 1.20 jmcneill int
868 1.19 jmcneill genfb_borrow(bus_addr_t addr, bus_space_handle_t *hdlp)
869 1.19 jmcneill {
870 1.19 jmcneill struct genfb_softc *sc = genfb_softc;
871 1.19 jmcneill
872 1.19 jmcneill if (sc && sc->sc_ops.genfb_borrow)
873 1.19 jmcneill return sc->sc_ops.genfb_borrow(sc, addr, hdlp);
874 1.19 jmcneill return 0;
875 1.19 jmcneill }
876 1.31 macallan
877 1.31 macallan static void
878 1.31 macallan genfb_brightness_up(device_t dev)
879 1.31 macallan {
880 1.31 macallan struct genfb_softc *sc = device_private(dev);
881 1.31 macallan
882 1.42 macallan KASSERT(sc->sc_brightness != NULL &&
883 1.42 macallan sc->sc_brightness->gpc_upd_parameter != NULL);
884 1.42 macallan
885 1.42 macallan (void)sc->sc_brightness->gpc_upd_parameter(
886 1.42 macallan sc->sc_brightness->gpc_cookie, GENFB_BRIGHTNESS_STEP);
887 1.31 macallan }
888 1.31 macallan
889 1.31 macallan static void
890 1.31 macallan genfb_brightness_down(device_t dev)
891 1.31 macallan {
892 1.31 macallan struct genfb_softc *sc = device_private(dev);
893 1.31 macallan
894 1.42 macallan KASSERT(sc->sc_brightness != NULL &&
895 1.42 macallan sc->sc_brightness->gpc_upd_parameter != NULL);
896 1.42 macallan
897 1.42 macallan (void)sc->sc_brightness->gpc_upd_parameter(
898 1.42 macallan sc->sc_brightness->gpc_cookie, - GENFB_BRIGHTNESS_STEP);
899 1.31 macallan }
900 1.35 jmcneill
901 1.35 jmcneill void
902 1.35 jmcneill genfb_enable_polling(device_t dev)
903 1.35 jmcneill {
904 1.35 jmcneill struct genfb_softc *sc = device_private(dev);
905 1.35 jmcneill
906 1.38 jmcneill if (sc->sc_console_screen.scr_vd) {
907 1.38 jmcneill SCREEN_ENABLE_DRAWING(&sc->sc_console_screen);
908 1.38 jmcneill vcons_hard_switch(&sc->sc_console_screen);
909 1.38 jmcneill vcons_enable_polling(&sc->vd);
910 1.55 riastrad if (sc->sc_ops.genfb_enable_polling)
911 1.55 riastrad (*sc->sc_ops.genfb_enable_polling)(sc);
912 1.38 jmcneill }
913 1.35 jmcneill }
914 1.35 jmcneill
915 1.35 jmcneill void
916 1.35 jmcneill genfb_disable_polling(device_t dev)
917 1.35 jmcneill {
918 1.35 jmcneill struct genfb_softc *sc = device_private(dev);
919 1.35 jmcneill
920 1.38 jmcneill if (sc->sc_console_screen.scr_vd) {
921 1.55 riastrad if (sc->sc_ops.genfb_disable_polling)
922 1.55 riastrad (*sc->sc_ops.genfb_disable_polling)(sc);
923 1.38 jmcneill vcons_disable_polling(&sc->vd);
924 1.38 jmcneill }
925 1.35 jmcneill }
926 1.81 macallan
927 1.81 macallan #if GENFB_GLYPHCACHE > 0
928 1.81 macallan #define GLYPHCACHESIZE ((GENFB_GLYPHCACHE) * 1024 * 1024)
929 1.81 macallan
930 1.81 macallan static inline int
931 1.81 macallan attr2idx(long attr)
932 1.81 macallan {
933 1.81 macallan if ((attr & 0xf0f00ff8) != 0)
934 1.81 macallan return -1;
935 1.81 macallan
936 1.81 macallan return (((attr >> 16) & 0x0f) | ((attr >> 20) & 0xf0));
937 1.81 macallan }
938 1.81 macallan
939 1.81 macallan static int
940 1.81 macallan genfb_setup_glyphcache(struct genfb_softc *sc, long defattr)
941 1.81 macallan {
942 1.81 macallan struct rasops_info *ri = &sc->sc_console_screen.scr_ri,
943 1.81 macallan *cri = &sc->sc_cache_ri;
944 1.81 macallan gc_bucket *b;
945 1.81 macallan int i, usedcells = 0, idx, j;
946 1.81 macallan
947 1.81 macallan sc->sc_cache = kmem_alloc(GLYPHCACHESIZE, KM_SLEEP);
948 1.81 macallan
949 1.81 macallan /*
950 1.81 macallan * now we build a mutant rasops_info for the cache - same pixel type
951 1.81 macallan * and such as the real fb, but only one character per line for
952 1.81 macallan * simplicity and locality
953 1.81 macallan */
954 1.81 macallan memcpy(cri, ri, sizeof(struct rasops_info));
955 1.81 macallan cri->ri_ops.putchar = sc->sc_putchar;
956 1.81 macallan cri->ri_width = ri->ri_font->fontwidth;
957 1.81 macallan cri->ri_stride = ri->ri_xscale;
958 1.81 macallan cri->ri_bits = sc->sc_cache;
959 1.81 macallan cri->ri_hwbits = NULL;
960 1.81 macallan cri->ri_origbits = sc->sc_cache;
961 1.81 macallan cri->ri_cols = 1;
962 1.81 macallan cri->ri_rows = GLYPHCACHESIZE /
963 1.81 macallan (cri->ri_stride * cri->ri_font->fontheight);
964 1.81 macallan cri->ri_xorigin = 0;
965 1.81 macallan cri->ri_yorigin = 0;
966 1.81 macallan cri->ri_xscale = ri->ri_xscale;
967 1.81 macallan cri->ri_yscale = ri->ri_font->fontheight * ri->ri_xscale;
968 1.81 macallan
969 1.81 macallan printf("size %d %d %d\n", GLYPHCACHESIZE, ri->ri_width, ri->ri_stride);
970 1.81 macallan printf("cells: %d\n", cri->ri_rows);
971 1.81 macallan sc->sc_nbuckets = uimin(256, cri->ri_rows / 223);
972 1.81 macallan sc->sc_buckets = kmem_alloc(sizeof(gc_bucket) * sc->sc_nbuckets, KM_SLEEP);
973 1.81 macallan printf("buckets: %d\n", sc->sc_nbuckets);
974 1.81 macallan for (i = 0; i < sc->sc_nbuckets; i++) {
975 1.81 macallan b = &sc->sc_buckets[i];
976 1.81 macallan b->gb_firstcell = usedcells;
977 1.81 macallan b->gb_numcells = uimin(223, cri->ri_rows - usedcells);
978 1.81 macallan usedcells += 223;
979 1.81 macallan b->gb_usedcells = 0;
980 1.81 macallan b->gb_index = -1;
981 1.81 macallan for (j = 0; j < 223; j++) b->gb_map[j] = -1;
982 1.81 macallan }
983 1.81 macallan
984 1.81 macallan /* initialize the attribute map... */
985 1.81 macallan for (i = 0; i < 256; i++) {
986 1.81 macallan sc->sc_attrmap[i] = -1;
987 1.81 macallan }
988 1.81 macallan
989 1.81 macallan /* first bucket goes to default attr */
990 1.81 macallan idx = attr2idx(defattr);
991 1.81 macallan printf("defattr %08lx idx %x\n", defattr, idx);
992 1.81 macallan
993 1.81 macallan if (idx >= 0) {
994 1.81 macallan sc->sc_attrmap[idx] = 0;
995 1.81 macallan sc->sc_buckets[0].gb_index = idx;
996 1.81 macallan }
997 1.81 macallan
998 1.81 macallan return 0;
999 1.81 macallan }
1000 1.81 macallan
1001 1.81 macallan static void
1002 1.81 macallan genfb_putchar(void *cookie, int row, int col, u_int c, long attr)
1003 1.81 macallan {
1004 1.81 macallan struct rasops_info *ri = cookie;
1005 1.81 macallan struct vcons_screen *scr = ri->ri_hw;
1006 1.81 macallan struct genfb_softc *sc = scr->scr_cookie;
1007 1.81 macallan uint8_t *src, *dst;
1008 1.81 macallan gc_bucket *b;
1009 1.81 macallan int i, idx, bi, cell;
1010 1.81 macallan
1011 1.81 macallan attr &= ~WSATTR_USERMASK;
1012 1.81 macallan
1013 1.81 macallan idx = attr2idx(attr);
1014 1.81 macallan if (c < 33 || c > 255 || idx < 0) goto nope;
1015 1.81 macallan
1016 1.81 macallan /* look for a bucket with the right attribute */
1017 1.81 macallan bi = sc->sc_attrmap[idx];
1018 1.81 macallan if (bi == -1) {
1019 1.81 macallan /* nope, see if there's an empty one left */
1020 1.81 macallan bi = 1;
1021 1.81 macallan while ((bi < sc->sc_nbuckets) &&
1022 1.81 macallan (sc->sc_buckets[bi].gb_index != -1)) {
1023 1.81 macallan bi++;
1024 1.81 macallan }
1025 1.81 macallan if (bi < sc->sc_nbuckets) {
1026 1.81 macallan /* found one -> grab it */
1027 1.81 macallan sc->sc_attrmap[idx] = bi;
1028 1.81 macallan b = &sc->sc_buckets[bi];
1029 1.81 macallan b->gb_index = idx;
1030 1.81 macallan b->gb_usedcells = 0;
1031 1.81 macallan /* make sure this doesn't get evicted right away */
1032 1.81 macallan b->gb_lastread = time_uptime;
1033 1.81 macallan } else {
1034 1.81 macallan /*
1035 1.81 macallan * still nothing
1036 1.81 macallan * steal the least recently read bucket
1037 1.81 macallan */
1038 1.81 macallan time_t moo = time_uptime;
1039 1.81 macallan int oldest = 1;
1040 1.81 macallan
1041 1.81 macallan for (i = 1; i < sc->sc_nbuckets; i++) {
1042 1.81 macallan if (sc->sc_buckets[i].gb_lastread < moo) {
1043 1.81 macallan oldest = i;
1044 1.81 macallan moo = sc->sc_buckets[i].gb_lastread;
1045 1.81 macallan }
1046 1.81 macallan }
1047 1.81 macallan
1048 1.81 macallan /* if we end up here all buckets must be in use */
1049 1.81 macallan b = &sc->sc_buckets[oldest];
1050 1.81 macallan sc->sc_attrmap[b->gb_index] = -1;
1051 1.81 macallan b->gb_index = idx;
1052 1.81 macallan b->gb_usedcells = 0;
1053 1.81 macallan sc->sc_attrmap[idx] = oldest;
1054 1.81 macallan /* now scrub it */
1055 1.81 macallan for (i = 0; i < 223; i++)
1056 1.81 macallan b->gb_map[i] = -1;
1057 1.81 macallan /* and set the time stamp */
1058 1.81 macallan b->gb_lastread = time_uptime;
1059 1.81 macallan }
1060 1.81 macallan } else {
1061 1.81 macallan /* found one */
1062 1.81 macallan b = &sc->sc_buckets[bi];
1063 1.81 macallan }
1064 1.81 macallan
1065 1.81 macallan /* see if there's room in the bucket */
1066 1.81 macallan if (b->gb_usedcells >= b->gb_numcells) goto nope;
1067 1.81 macallan
1068 1.81 macallan cell = b->gb_map[c - 33];
1069 1.81 macallan if (cell == -1) {
1070 1.81 macallan if (b->gb_usedcells >= b->gb_numcells)
1071 1.81 macallan goto nope;
1072 1.81 macallan cell = atomic_add_int_nv(&b->gb_usedcells, 1) - 1;
1073 1.81 macallan b->gb_map[c - 33] = cell;
1074 1.81 macallan cell += b->gb_firstcell;
1075 1.81 macallan sc->sc_putchar(&sc->sc_cache_ri, cell, 0, c, attr);
1076 1.81 macallan
1077 1.81 macallan } else
1078 1.81 macallan cell += b->gb_firstcell;
1079 1.81 macallan
1080 1.81 macallan src = sc->sc_cache + cell * sc->sc_cache_ri.ri_yscale;
1081 1.81 macallan dst = ri->ri_bits + row * ri->ri_yscale + col * ri->ri_xscale;
1082 1.81 macallan for (i = 0; i < ri->ri_font->fontheight; i++) {
1083 1.81 macallan memcpy(dst, src, ri->ri_xscale);
1084 1.81 macallan src += ri->ri_xscale;
1085 1.81 macallan dst += ri->ri_stride;
1086 1.81 macallan }
1087 1.81 macallan b->gb_lastread = time_uptime;
1088 1.81 macallan return;
1089 1.81 macallan nope:
1090 1.81 macallan sc->sc_putchar(cookie, row, col, c, attr);
1091 1.81 macallan }
1092 1.81 macallan
1093 1.81 macallan #endif
1094