genfb.c revision 1.10 1 /* $NetBSD: genfb.c,v 1.10 2007/08/30 15:40:41 jmmv Exp $ */
2
3 /*-
4 * Copyright (c) 2007 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: genfb.c,v 1.10 2007/08/30 15:40:41 jmmv Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/proc.h>
40 #include <sys/mutex.h>
41 #include <sys/ioctl.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/malloc.h>
45
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsdisplayvar.h>
48 #include <dev/rasops/rasops.h>
49 #include <dev/wsfont/wsfont.h>
50
51 #include <dev/wscons/wsdisplay_vconsvar.h>
52
53 #include <dev/wsfb/genfbvar.h>
54
55 #include "opt_genfb.h"
56 #include "opt_wsfb.h"
57
58 static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
59 static paddr_t genfb_mmap(void *, void *, off_t, int);
60 static void genfb_init_screen(void *, struct vcons_screen *, int, long *);
61
62 static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
63 static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
64 static void genfb_restore_palette(struct genfb_softc *);
65 static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
66 uint8_t, uint8_t);
67
68 extern const u_char rasops_cmap[768];
69
70 struct wsdisplay_accessops genfb_accessops = {
71 genfb_ioctl,
72 genfb_mmap,
73 NULL, /* alloc_screen */
74 NULL, /* free_screen */
75 NULL, /* show_screen */
76 NULL, /* load_font */
77 NULL, /* pollc */
78 NULL /* scroll */
79 };
80
81 void
82 genfb_init(struct genfb_softc *sc)
83 {
84 prop_dictionary_t dict;
85 uint64_t cmap_cb;
86 uint32_t fboffset;
87
88 dict = device_properties(&sc->sc_dev);
89 #ifdef GENFB_DEBUG
90 printf(prop_dictionary_externalize(dict));
91 #endif
92 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width))
93 panic("no width property");
94 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height))
95 panic("no height property");
96 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth))
97 panic("no depth property");
98
99 /* XXX should be a 64bit value */
100 if (!prop_dictionary_get_uint32(dict, "address", &fboffset))
101 panic("no address property");
102 sc->sc_fboffset = fboffset;
103
104 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
105 sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
106 sc->sc_fbsize = sc->sc_height * sc->sc_stride;
107
108 /* optional colour map callback */
109 sc->sc_cmcb = NULL;
110 if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
111 if (cmap_cb != 0)
112 sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
113 }
114 }
115
116 int
117 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
118 {
119 struct wsemuldisplaydev_attach_args aa;
120 prop_dictionary_t dict;
121 struct rasops_info *ri;
122 long defattr;
123 int i, j;
124 bool console;
125
126 aprint_verbose("%s: framebuffer at %p, size %dx%d, depth %d, "
127 "stride %d\n", sc->sc_dev.dv_xname, sc->sc_fbaddr,
128 sc->sc_width, sc->sc_height, sc->sc_depth, sc->sc_stride);
129
130 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
131 "default",
132 0, 0,
133 NULL,
134 8, 16,
135 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
136 NULL
137 };
138 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
139 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
140 memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
141 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
142
143 sc->sc_shadowfb = malloc(sc->sc_fbsize, M_DEVBUF, M_WAITOK);
144
145 dict = device_properties(&sc->sc_dev);
146
147 prop_dictionary_get_bool(dict, "is_console", &console);
148
149 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
150 &genfb_accessops);
151 sc->vd.init_screen = genfb_init_screen;
152
153 /* Do not print anything between this point and the screen
154 * clear operation below. Otherwise it will be lost. */
155
156 ri = &sc->sc_console_screen.scr_ri;
157
158 if (console) {
159 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
160 &defattr);
161 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
162
163 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
164 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
165 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
166 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
167 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
168 defattr);
169 } else {
170 /*
171 * since we're not the console we can postpone the rest
172 * until someone actually allocates a screen for us
173 */
174 (*ri->ri_ops.allocattr)(ri, 0, 0, 0, &defattr);
175 }
176
177 /* Clear the whole screen to bring it to a known state. */
178 (*ri->ri_ops.eraserows)(ri, 0, ri->ri_rows, defattr);
179
180 j = 0;
181 for (i = 0; i < (1 << sc->sc_depth); i++) {
182
183 sc->sc_cmap_red[i] = rasops_cmap[j];
184 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
185 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
186 genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
187 rasops_cmap[j + 2]);
188 j += 3;
189 }
190
191 aa.console = console;
192 aa.scrdata = &sc->sc_screenlist;
193 aa.accessops = &genfb_accessops;
194 aa.accesscookie = &sc->vd;
195
196 config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
197
198 return 0;
199 }
200
201 static int
202 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
203 struct lwp *l)
204 {
205 struct vcons_data *vd = v;
206 struct genfb_softc *sc = vd->cookie;
207 struct wsdisplay_fbinfo *wdf;
208 struct vcons_screen *ms = vd->active;
209
210 switch (cmd) {
211
212 case WSDISPLAYIO_GINFO:
213 if (ms == NULL)
214 return ENODEV;
215 wdf = (void *)data;
216 wdf->height = ms->scr_ri.ri_height;
217 wdf->width = ms->scr_ri.ri_width;
218 wdf->depth = ms->scr_ri.ri_depth;
219 wdf->cmsize = 256;
220 return 0;
221
222 case WSDISPLAYIO_GETCMAP:
223 return genfb_getcmap(sc,
224 (struct wsdisplay_cmap *)data);
225
226 case WSDISPLAYIO_PUTCMAP:
227 return genfb_putcmap(sc,
228 (struct wsdisplay_cmap *)data);
229
230 case WSDISPLAYIO_LINEBYTES:
231 *(u_int *)data = sc->sc_stride;
232 return 0;
233
234 case WSDISPLAYIO_SMODE:
235 {
236 int new_mode = *(int*)data;
237 if (new_mode != sc->sc_mode) {
238 sc->sc_mode = new_mode;
239 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
240 genfb_restore_palette(sc);
241 vcons_redraw_screen(ms);
242 }
243 }
244 }
245 return 0;
246 default:
247 if (sc->sc_ops.genfb_ioctl)
248 return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
249 data, flag, l);
250 }
251 return EPASSTHROUGH;
252 }
253
254 static paddr_t
255 genfb_mmap(void *v, void *vs, off_t offset, int prot)
256 {
257 struct vcons_data *vd = v;
258 struct genfb_softc *sc = vd->cookie;
259
260 if (sc->sc_ops.genfb_mmap)
261 return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
262
263 return -1;
264 }
265
266 static void
267 genfb_init_screen(void *cookie, struct vcons_screen *scr,
268 int existing, long *defattr)
269 {
270 struct genfb_softc *sc = cookie;
271 struct rasops_info *ri = &scr->scr_ri;
272
273 ri->ri_depth = sc->sc_depth;
274 ri->ri_width = sc->sc_width;
275 ri->ri_height = sc->sc_height;
276 ri->ri_stride = sc->sc_stride;
277 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
278
279 if (sc->sc_shadowfb != NULL) {
280
281 ri->ri_hwbits = (char *)sc->sc_fbaddr;
282 ri->ri_bits = (char *)sc->sc_shadowfb;
283 } else
284 ri->ri_bits = (char *)sc->sc_fbaddr;
285
286 if (existing) {
287 ri->ri_flg |= RI_CLEAR;
288 }
289
290 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
291 ri->ri_caps = WSSCREEN_WSCOLORS;
292
293 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
294 sc->sc_width / ri->ri_font->fontwidth);
295
296 ri->ri_hw = scr;
297 }
298
299 static int
300 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
301 {
302 u_char *r, *g, *b;
303 u_int index = cm->index;
304 u_int count = cm->count;
305 int i, error;
306 u_char rbuf[256], gbuf[256], bbuf[256];
307
308 #ifdef GENFB_DEBUG
309 aprint_debug("putcmap: %d %d\n",index, count);
310 #endif
311 if (cm->index >= 256 || cm->count > 256 ||
312 (cm->index + cm->count) > 256)
313 return EINVAL;
314 error = copyin(cm->red, &rbuf[index], count);
315 if (error)
316 return error;
317 error = copyin(cm->green, &gbuf[index], count);
318 if (error)
319 return error;
320 error = copyin(cm->blue, &bbuf[index], count);
321 if (error)
322 return error;
323
324 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
325 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
326 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
327
328 r = &sc->sc_cmap_red[index];
329 g = &sc->sc_cmap_green[index];
330 b = &sc->sc_cmap_blue[index];
331
332 for (i = 0; i < count; i++) {
333 genfb_putpalreg(sc, index, *r, *g, *b);
334 index++;
335 r++, g++, b++;
336 }
337 return 0;
338 }
339
340 static int
341 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
342 {
343 u_int index = cm->index;
344 u_int count = cm->count;
345 int error;
346
347 if (index >= 255 || count > 256 || index + count > 256)
348 return EINVAL;
349
350 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
351 if (error)
352 return error;
353 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
354 if (error)
355 return error;
356 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
357 if (error)
358 return error;
359
360 return 0;
361 }
362
363 static void
364 genfb_restore_palette(struct genfb_softc *sc)
365 {
366 int i;
367
368 for (i = 0; i < (1 << sc->sc_depth); i++) {
369 genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
370 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
371 }
372 }
373
374 static int
375 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
376 uint8_t b)
377 {
378
379 if (sc->sc_cmcb) {
380
381 sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
382 idx, r, g, b);
383 }
384 return 0;
385 }
386
387