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