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