genfb.c revision 1.2.2.3 1 /* $NetBSD: genfb.c,v 1.2.2.3 2007/05/27 14:30:33 ad 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.2.2.3 2007/05/27 14:30:33 ad 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
45 #include <dev/wscons/wsconsio.h>
46 #include <dev/wscons/wsdisplayvar.h>
47 #include <dev/rasops/rasops.h>
48 #include <dev/wsfont/wsfont.h>
49
50 #include <dev/wscons/wsdisplay_vconsvar.h>
51
52 #include <dev/wsfb/genfbvar.h>
53
54 #include "opt_genfb.h"
55 #include "opt_wsfb.h"
56
57 static int genfb_ioctl(void *, void *, u_long, void *, int, struct lwp *);
58 static paddr_t genfb_mmap(void *, void *, off_t, int);
59 static void genfb_init_screen(void *, struct vcons_screen *, int, long *);
60
61 static int genfb_putcmap(struct genfb_softc *, struct wsdisplay_cmap *);
62 static int genfb_getcmap(struct genfb_softc *, struct wsdisplay_cmap *);
63 static void genfb_restore_palette(struct genfb_softc *);
64 static int genfb_putpalreg(struct genfb_softc *, uint8_t, uint8_t,
65 uint8_t, uint8_t);
66
67 extern const u_char rasops_cmap[768];
68
69 struct wsdisplay_accessops genfb_accessops = {
70 genfb_ioctl,
71 genfb_mmap,
72 NULL, /* alloc_screen */
73 NULL, /* free_screen */
74 NULL, /* show_screen */
75 NULL, /* load_font */
76 NULL, /* pollc */
77 NULL /* scroll */
78 };
79
80 void
81 genfb_init(struct genfb_softc *sc)
82 {
83 prop_dictionary_t dict;
84 uint64_t cmap_cb;
85 uint32_t fboffset;
86
87 dict = device_properties(&sc->sc_dev);
88 #ifdef GENFB_DEBUG
89 printf(prop_dictionary_externalize(dict));
90 #endif
91 if (!prop_dictionary_get_uint32(dict, "width", &sc->sc_width))
92 panic("no width property");
93 if (!prop_dictionary_get_uint32(dict, "height", &sc->sc_height))
94 panic("no height property");
95 if (!prop_dictionary_get_uint32(dict, "depth", &sc->sc_depth))
96 panic("no depth property");
97
98 /* XXX should be a 64bit value */
99 if (!prop_dictionary_get_uint32(dict, "address", &fboffset))
100 panic("no address property");
101 sc->sc_fboffset = fboffset;
102
103 if (!prop_dictionary_get_uint32(dict, "linebytes", &sc->sc_stride))
104 sc->sc_stride = (sc->sc_width * sc->sc_depth) >> 3;
105 sc->sc_fbsize = sc->sc_width * sc->sc_stride;
106
107 /* optional colour map callback */
108 sc->sc_cmcb = NULL;
109 if (prop_dictionary_get_uint64(dict, "cmap_callback", &cmap_cb)) {
110 if (cmap_cb != 0)
111 sc->sc_cmcb = (void *)(vaddr_t)cmap_cb;
112 }
113 }
114
115 int
116 genfb_attach(struct genfb_softc *sc, struct genfb_ops *ops)
117 {
118 struct wsemuldisplaydev_attach_args aa;
119 prop_dictionary_t dict;
120 struct rasops_info *ri;
121 long defattr;
122 int console, i, j;
123
124 sc->sc_defaultscreen_descr = (struct wsscreen_descr){
125 "default",
126 0, 0,
127 NULL,
128 8, 16,
129 WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
130 NULL
131 };
132 sc->sc_screens[0] = &sc->sc_defaultscreen_descr;
133 sc->sc_screenlist = (struct wsscreen_list){1, sc->sc_screens};
134 memcpy(&sc->sc_ops, ops, sizeof(struct genfb_ops));
135 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
136
137 dict = device_properties(&sc->sc_dev);
138
139 prop_dictionary_get_bool(dict, "is_console", &console);
140
141 vcons_init(&sc->vd, sc, &sc->sc_defaultscreen_descr,
142 &genfb_accessops);
143 sc->vd.init_screen = genfb_init_screen;
144
145 ri = &sc->sc_console_screen.scr_ri;
146
147 if (console) {
148 vcons_init_screen(&sc->vd, &sc->sc_console_screen, 1,
149 &defattr);
150 sc->sc_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
151
152 sc->sc_defaultscreen_descr.textops = &ri->ri_ops;
153 sc->sc_defaultscreen_descr.capabilities = ri->ri_caps;
154 sc->sc_defaultscreen_descr.nrows = ri->ri_rows;
155 sc->sc_defaultscreen_descr.ncols = ri->ri_cols;
156 wsdisplay_cnattach(&sc->sc_defaultscreen_descr, ri, 0, 0,
157 defattr);
158 } else {
159 /*
160 * since we're not the console we can postpone the rest
161 * until someone actually allocates a screen for us
162 */
163 }
164
165 j = 0;
166 for (i = 0; i < (1 << (sc->sc_depth - 1)); i++) {
167
168 sc->sc_cmap_red[i] = rasops_cmap[j];
169 sc->sc_cmap_green[i] = rasops_cmap[j + 1];
170 sc->sc_cmap_blue[i] = rasops_cmap[j + 2];
171 genfb_putpalreg(sc, i, rasops_cmap[j], rasops_cmap[j + 1],
172 rasops_cmap[j + 2]);
173 j += 3;
174 }
175
176 aa.console = console;
177 aa.scrdata = &sc->sc_screenlist;
178 aa.accessops = &genfb_accessops;
179 aa.accesscookie = &sc->vd;
180
181 config_found(&sc->sc_dev, &aa, wsemuldisplaydevprint);
182
183 return 0;
184 }
185
186 static int
187 genfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
188 struct lwp *l)
189 {
190 struct vcons_data *vd = v;
191 struct genfb_softc *sc = vd->cookie;
192 struct wsdisplay_fbinfo *wdf;
193 struct vcons_screen *ms = vd->active;
194
195 switch (cmd) {
196
197 case WSDISPLAYIO_GINFO:
198 wdf = (void *)data;
199 wdf->height = ms->scr_ri.ri_height;
200 wdf->width = ms->scr_ri.ri_width;
201 wdf->depth = ms->scr_ri.ri_depth;
202 wdf->cmsize = 256;
203 return 0;
204
205 case WSDISPLAYIO_GETCMAP:
206 return genfb_getcmap(sc,
207 (struct wsdisplay_cmap *)data);
208
209 case WSDISPLAYIO_PUTCMAP:
210 return genfb_putcmap(sc,
211 (struct wsdisplay_cmap *)data);
212
213 case WSDISPLAYIO_LINEBYTES:
214 *(u_int *)data = sc->sc_stride;
215 return 0;
216
217 case WSDISPLAYIO_SMODE:
218 {
219 int new_mode = *(int*)data;
220 if (new_mode != sc->sc_mode) {
221 sc->sc_mode = new_mode;
222 if(new_mode == WSDISPLAYIO_MODE_EMUL) {
223 genfb_restore_palette(sc);
224 vcons_redraw_screen(ms);
225 }
226 }
227 }
228 return 0;
229 default:
230 if (sc->sc_ops.genfb_ioctl)
231 return sc->sc_ops.genfb_ioctl(sc, vs, cmd,
232 data, flag, l);
233 }
234 return EPASSTHROUGH;
235 }
236
237 static paddr_t
238 genfb_mmap(void *v, void *vs, off_t offset, int prot)
239 {
240 struct vcons_data *vd = v;
241 struct genfb_softc *sc = vd->cookie;
242
243 if (sc->sc_ops.genfb_mmap)
244 return sc->sc_ops.genfb_mmap(sc, vs, offset, prot);
245
246 return -1;
247 }
248
249 static void
250 genfb_init_screen(void *cookie, struct vcons_screen *scr,
251 int existing, long *defattr)
252 {
253 struct genfb_softc *sc = cookie;
254 struct rasops_info *ri = &scr->scr_ri;
255
256 ri->ri_depth = sc->sc_depth;
257 ri->ri_width = sc->sc_width;
258 ri->ri_height = sc->sc_height;
259 ri->ri_stride = sc->sc_stride;
260 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
261
262 ri->ri_bits = (char *)sc->sc_fbaddr;
263
264 if (existing) {
265 ri->ri_flg |= RI_CLEAR;
266 }
267
268 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
269 ri->ri_caps = WSSCREEN_WSCOLORS;
270
271 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
272 sc->sc_width / ri->ri_font->fontwidth);
273
274 ri->ri_hw = scr;
275 }
276
277 static int
278 genfb_putcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
279 {
280 u_char *r, *g, *b;
281 u_int index = cm->index;
282 u_int count = cm->count;
283 int i, error;
284 u_char rbuf[256], gbuf[256], bbuf[256];
285
286 #ifdef GENFB_DEBUG
287 aprint_debug("putcmap: %d %d\n",index, count);
288 #endif
289 if (cm->index >= 256 || cm->count > 256 ||
290 (cm->index + cm->count) > 256)
291 return EINVAL;
292 error = copyin(cm->red, &rbuf[index], count);
293 if (error)
294 return error;
295 error = copyin(cm->green, &gbuf[index], count);
296 if (error)
297 return error;
298 error = copyin(cm->blue, &bbuf[index], count);
299 if (error)
300 return error;
301
302 memcpy(&sc->sc_cmap_red[index], &rbuf[index], count);
303 memcpy(&sc->sc_cmap_green[index], &gbuf[index], count);
304 memcpy(&sc->sc_cmap_blue[index], &bbuf[index], count);
305
306 r = &sc->sc_cmap_red[index];
307 g = &sc->sc_cmap_green[index];
308 b = &sc->sc_cmap_blue[index];
309
310 for (i = 0; i < count; i++) {
311 genfb_putpalreg(sc, index, *r, *g, *b);
312 index++;
313 r++, g++, b++;
314 }
315 return 0;
316 }
317
318 static int
319 genfb_getcmap(struct genfb_softc *sc, struct wsdisplay_cmap *cm)
320 {
321 u_int index = cm->index;
322 u_int count = cm->count;
323 int error;
324
325 if (index >= 255 || count > 256 || index + count > 256)
326 return EINVAL;
327
328 error = copyout(&sc->sc_cmap_red[index], cm->red, count);
329 if (error)
330 return error;
331 error = copyout(&sc->sc_cmap_green[index], cm->green, count);
332 if (error)
333 return error;
334 error = copyout(&sc->sc_cmap_blue[index], cm->blue, count);
335 if (error)
336 return error;
337
338 return 0;
339 }
340
341 static void
342 genfb_restore_palette(struct genfb_softc *sc)
343 {
344 int i;
345
346 for (i = 0; i < (1 << (sc->sc_depth - 1)); i++) {
347 genfb_putpalreg(sc, i, sc->sc_cmap_red[i],
348 sc->sc_cmap_green[i], sc->sc_cmap_blue[i]);
349 }
350 }
351
352 static int
353 genfb_putpalreg(struct genfb_softc *sc, uint8_t idx, uint8_t r, uint8_t g,
354 uint8_t b)
355 {
356
357 if (sc->sc_cmcb) {
358
359 sc->sc_cmcb->gcc_set_mapreg(sc->sc_cmcb->gcc_cookie,
360 idx, r, g, b);
361 }
362 return 0;
363 }
364
365