wsdisplay_vcons.c revision 1.7.6.2 1 1.7.6.2 yamt /* $NetBSD: wsdisplay_vcons.c,v 1.7.6.2 2006/06/21 15:08:12 yamt Exp $ */
2 1.7.6.2 yamt
3 1.7.6.2 yamt /*-
4 1.7.6.2 yamt * Copyright (c) 2005, 2006 Michael Lorenz
5 1.7.6.2 yamt * All rights reserved.
6 1.7.6.2 yamt *
7 1.7.6.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.7.6.2 yamt * modification, are permitted provided that the following conditions
9 1.7.6.2 yamt * are met:
10 1.7.6.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.7.6.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.7.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.7.6.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.7.6.2 yamt * documentation and/or other materials provided with the distribution.
15 1.7.6.2 yamt * 3. All advertising materials mentioning features or use of this software
16 1.7.6.2 yamt * must display the following acknowledgement:
17 1.7.6.2 yamt * This product includes software developed by the NetBSD
18 1.7.6.2 yamt * Foundation, Inc. and its contributors.
19 1.7.6.2 yamt * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.7.6.2 yamt * contributors may be used to endorse or promote products derived
21 1.7.6.2 yamt * from this software without specific prior written permission.
22 1.7.6.2 yamt *
23 1.7.6.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.7.6.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.7.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.7.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.7.6.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.7.6.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.7.6.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.7.6.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.7.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.7.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.7.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
34 1.7.6.2 yamt */
35 1.7.6.2 yamt
36 1.7.6.2 yamt #include <sys/cdefs.h>
37 1.7.6.2 yamt __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.7.6.2 2006/06/21 15:08:12 yamt Exp $");
38 1.7.6.2 yamt
39 1.7.6.2 yamt #include <sys/param.h>
40 1.7.6.2 yamt #include <sys/systm.h>
41 1.7.6.2 yamt #include <sys/kernel.h>
42 1.7.6.2 yamt #include <sys/buf.h>
43 1.7.6.2 yamt #include <sys/device.h>
44 1.7.6.2 yamt #include <sys/ioctl.h>
45 1.7.6.2 yamt #include <sys/malloc.h>
46 1.7.6.2 yamt #include <sys/mman.h>
47 1.7.6.2 yamt #include <sys/tty.h>
48 1.7.6.2 yamt #include <sys/conf.h>
49 1.7.6.2 yamt #include <sys/proc.h>
50 1.7.6.2 yamt #include <sys/kthread.h>
51 1.7.6.2 yamt #include <sys/tprintf.h>
52 1.7.6.2 yamt
53 1.7.6.2 yamt #include <dev/wscons/wsdisplayvar.h>
54 1.7.6.2 yamt #include <dev/wscons/wsconsio.h>
55 1.7.6.2 yamt #include <dev/wsfont/wsfont.h>
56 1.7.6.2 yamt #include <dev/rasops/rasops.h>
57 1.7.6.2 yamt
58 1.7.6.2 yamt #include <dev/wscons/wsdisplay_vconsvar.h>
59 1.7.6.2 yamt
60 1.7.6.2 yamt static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
61 1.7.6.2 yamt long *);
62 1.7.6.2 yamt
63 1.7.6.2 yamt static int vcons_ioctl(void *, void *, u_long, caddr_t, int, struct lwp *);
64 1.7.6.2 yamt static int vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
65 1.7.6.2 yamt int *, int *, long *);
66 1.7.6.2 yamt static void vcons_free_screen(void *, void *);
67 1.7.6.2 yamt static int vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
68 1.7.6.2 yamt void *);
69 1.7.6.2 yamt
70 1.7.6.2 yamt static void vcons_do_switch(struct vcons_data *);
71 1.7.6.2 yamt
72 1.7.6.2 yamt /* methods that work only on text buffers */
73 1.7.6.2 yamt static void vcons_copycols_buffer(void *, int, int, int, int);
74 1.7.6.2 yamt static void vcons_erasecols_buffer(void *, int, int, int, long);
75 1.7.6.2 yamt static void vcons_copyrows_buffer(void *, int, int, int);
76 1.7.6.2 yamt static void vcons_eraserows_buffer(void *, int, int, long);
77 1.7.6.2 yamt static void vcons_putchar_buffer(void *, int, int, u_int, long);
78 1.7.6.2 yamt
79 1.7.6.2 yamt /*
80 1.7.6.2 yamt * actual wrapper methods which call both the _buffer ones above and the
81 1.7.6.2 yamt * driver supplied ones to do the drawing
82 1.7.6.2 yamt */
83 1.7.6.2 yamt static void vcons_copycols(void *, int, int, int, int);
84 1.7.6.2 yamt static void vcons_erasecols(void *, int, int, int, long);
85 1.7.6.2 yamt static void vcons_copyrows(void *, int, int, int);
86 1.7.6.2 yamt static void vcons_eraserows(void *, int, int, long);
87 1.7.6.2 yamt static void vcons_putchar(void *, int, int, u_int, long);
88 1.7.6.2 yamt static void vcons_cursor(void *, int, int, int);
89 1.7.6.2 yamt
90 1.7.6.2 yamt /* support for readin/writing text buffers. For wsmoused */
91 1.7.6.2 yamt static int vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
92 1.7.6.2 yamt static int vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
93 1.7.6.2 yamt
94 1.7.6.2 yamt static void vcons_lock(struct vcons_screen *);
95 1.7.6.2 yamt static void vcons_unlock(struct vcons_screen *);
96 1.7.6.2 yamt
97 1.7.6.2 yamt
98 1.7.6.2 yamt int
99 1.7.6.2 yamt vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def,
100 1.7.6.2 yamt struct wsdisplay_accessops *ao)
101 1.7.6.2 yamt {
102 1.7.6.2 yamt
103 1.7.6.2 yamt /* zero out everything so we can rely on untouched fields being 0 */
104 1.7.6.2 yamt memset(vd, 0, sizeof(struct vcons_data));
105 1.7.6.2 yamt
106 1.7.6.2 yamt vd->cookie = cookie;
107 1.7.6.2 yamt
108 1.7.6.2 yamt vd->init_screen = vcons_dummy_init_screen;
109 1.7.6.2 yamt vd->show_screen_cb = NULL;
110 1.7.6.2 yamt
111 1.7.6.2 yamt /* keep a copy of the accessops that we replace below with our
112 1.7.6.2 yamt * own wrappers */
113 1.7.6.2 yamt vd->ioctl = ao->ioctl;
114 1.7.6.2 yamt
115 1.7.6.2 yamt /* configure the accessops */
116 1.7.6.2 yamt ao->ioctl = vcons_ioctl;
117 1.7.6.2 yamt ao->alloc_screen = vcons_alloc_screen;
118 1.7.6.2 yamt ao->free_screen = vcons_free_screen;
119 1.7.6.2 yamt ao->show_screen = vcons_show_screen;
120 1.7.6.2 yamt
121 1.7.6.2 yamt LIST_INIT(&vd->screens);
122 1.7.6.2 yamt vd->active = NULL;
123 1.7.6.2 yamt vd->wanted = NULL;
124 1.7.6.2 yamt vd->currenttype = def;
125 1.7.6.2 yamt callout_init(&vd->switch_callout);
126 1.7.6.2 yamt
127 1.7.6.2 yamt /*
128 1.7.6.2 yamt * a lock to serialize access to the framebuffer.
129 1.7.6.2 yamt * when switching screens we need to make sure there's no rasops
130 1.7.6.2 yamt * operation in progress
131 1.7.6.2 yamt */
132 1.7.6.2 yamt #ifdef DIAGNOSTIC
133 1.7.6.2 yamt vd->switch_poll_count = 0;
134 1.7.6.2 yamt #endif
135 1.7.6.2 yamt return 0;
136 1.7.6.2 yamt }
137 1.7.6.2 yamt
138 1.7.6.2 yamt static void
139 1.7.6.2 yamt vcons_lock(struct vcons_screen *scr)
140 1.7.6.2 yamt {
141 1.7.6.2 yamt #ifdef VCONS_PARANOIA
142 1.7.6.2 yamt int s;
143 1.7.6.2 yamt
144 1.7.6.2 yamt s = splhigh();
145 1.7.6.2 yamt #endif
146 1.7.6.2 yamt SCREEN_BUSY(scr);
147 1.7.6.2 yamt #ifdef VCONS_PARANOIA
148 1.7.6.2 yamt splx(s);
149 1.7.6.2 yamt #endif
150 1.7.6.2 yamt }
151 1.7.6.2 yamt
152 1.7.6.2 yamt static void
153 1.7.6.2 yamt vcons_unlock(struct vcons_screen *scr)
154 1.7.6.2 yamt {
155 1.7.6.2 yamt #ifdef VCONS_PARANOIA
156 1.7.6.2 yamt int s;
157 1.7.6.2 yamt
158 1.7.6.2 yamt s = splhigh();
159 1.7.6.2 yamt #endif
160 1.7.6.2 yamt SCREEN_IDLE(scr);
161 1.7.6.2 yamt #ifdef VCONS_PARANOIA
162 1.7.6.2 yamt splx(s);
163 1.7.6.2 yamt #endif
164 1.7.6.2 yamt }
165 1.7.6.2 yamt
166 1.7.6.2 yamt static void
167 1.7.6.2 yamt vcons_dummy_init_screen(void *cookie, struct vcons_screen *scr, int exists,
168 1.7.6.2 yamt long *defattr)
169 1.7.6.2 yamt {
170 1.7.6.2 yamt
171 1.7.6.2 yamt /*
172 1.7.6.2 yamt * default init_screen() method.
173 1.7.6.2 yamt * Needs to be overwritten so we bitch and whine in case anyone ends
174 1.7.6.2 yamt * up in here.
175 1.7.6.2 yamt */
176 1.7.6.2 yamt printf("vcons_init_screen: dummy function called. Your driver is "
177 1.7.6.2 yamt "supposed to supply a replacement for proper operation\n");
178 1.7.6.2 yamt }
179 1.7.6.2 yamt
180 1.7.6.2 yamt int
181 1.7.6.2 yamt vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr,
182 1.7.6.2 yamt int existing, long *defattr)
183 1.7.6.2 yamt {
184 1.7.6.2 yamt struct rasops_info *ri = &scr->scr_ri;
185 1.7.6.2 yamt int cnt, i;
186 1.7.6.2 yamt
187 1.7.6.2 yamt scr->scr_cookie = vd->cookie;
188 1.7.6.2 yamt scr->scr_vd = scr->scr_origvd = vd;
189 1.7.6.2 yamt SCREEN_IDLE(scr);
190 1.7.6.2 yamt
191 1.7.6.2 yamt /*
192 1.7.6.2 yamt * call the driver-supplied init_screen function which is expected
193 1.7.6.2 yamt * to set up rasops_info, override cursor() and probably others
194 1.7.6.2 yamt */
195 1.7.6.2 yamt vd->init_screen(vd->cookie, scr, existing, defattr);
196 1.7.6.2 yamt
197 1.7.6.2 yamt /*
198 1.7.6.2 yamt * save the non virtual console aware rasops and replace them with
199 1.7.6.2 yamt * our wrappers
200 1.7.6.2 yamt */
201 1.7.6.2 yamt vd->eraserows = ri->ri_ops.eraserows;
202 1.7.6.2 yamt vd->copyrows = ri->ri_ops.copyrows;
203 1.7.6.2 yamt vd->erasecols = ri->ri_ops.erasecols;
204 1.7.6.2 yamt vd->copycols = ri->ri_ops.copycols;
205 1.7.6.2 yamt vd->putchar = ri->ri_ops.putchar;
206 1.7.6.2 yamt vd->cursor = ri->ri_ops.cursor;
207 1.7.6.2 yamt
208 1.7.6.2 yamt ri->ri_ops.eraserows = vcons_eraserows;
209 1.7.6.2 yamt ri->ri_ops.copyrows = vcons_copyrows;
210 1.7.6.2 yamt ri->ri_ops.erasecols = vcons_erasecols;
211 1.7.6.2 yamt ri->ri_ops.copycols = vcons_copycols;
212 1.7.6.2 yamt ri->ri_ops.putchar = vcons_putchar;
213 1.7.6.2 yamt ri->ri_ops.cursor = vcons_cursor;
214 1.7.6.2 yamt ri->ri_hw = scr;
215 1.7.6.2 yamt
216 1.7.6.2 yamt /*
217 1.7.6.2 yamt * we allocate both chars and attributes in one chunk, attributes first
218 1.7.6.2 yamt * because they have the (potentially) bigger alignment
219 1.7.6.2 yamt */
220 1.7.6.2 yamt cnt = ri->ri_rows * ri->ri_cols;
221 1.7.6.2 yamt scr->scr_attrs = (long *)malloc(cnt * (sizeof(long) +
222 1.7.6.2 yamt sizeof(uint16_t)), M_DEVBUF, M_WAITOK);
223 1.7.6.2 yamt if (scr->scr_attrs == NULL)
224 1.7.6.2 yamt return ENOMEM;
225 1.7.6.2 yamt
226 1.7.6.2 yamt scr->scr_chars = (uint16_t *)&scr->scr_attrs[cnt];
227 1.7.6.2 yamt
228 1.7.6.2 yamt ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
229 1.7.6.2 yamt scr->scr_defattr = *defattr;
230 1.7.6.2 yamt
231 1.7.6.2 yamt /*
232 1.7.6.2 yamt * fill the attribute buffer with *defattr, chars with 0x20
233 1.7.6.2 yamt * since we don't know if the driver tries to mimic firmware output or
234 1.7.6.2 yamt * reset everything we do nothing to VRAM here, any driver that feels
235 1.7.6.2 yamt * the need to clear screen or something will have to do it on its own
236 1.7.6.2 yamt * Additional screens will start out in the background anyway so
237 1.7.6.2 yamt * cleaning or not only really affects the initial console screen
238 1.7.6.2 yamt */
239 1.7.6.2 yamt for (i = 0; i < cnt; i++) {
240 1.7.6.2 yamt scr->scr_attrs[i] = *defattr;
241 1.7.6.2 yamt scr->scr_chars[i] = 0x20;
242 1.7.6.2 yamt }
243 1.7.6.2 yamt
244 1.7.6.2 yamt if(vd->active == NULL) {
245 1.7.6.2 yamt vd->active = scr;
246 1.7.6.2 yamt SCREEN_VISIBLE(scr);
247 1.7.6.2 yamt }
248 1.7.6.2 yamt
249 1.7.6.2 yamt if (existing) {
250 1.7.6.2 yamt SCREEN_VISIBLE(scr);
251 1.7.6.2 yamt vd->active = scr;
252 1.7.6.2 yamt } else {
253 1.7.6.2 yamt SCREEN_INVISIBLE(scr);
254 1.7.6.2 yamt }
255 1.7.6.2 yamt
256 1.7.6.2 yamt LIST_INSERT_HEAD(&vd->screens, scr, next);
257 1.7.6.2 yamt return 0;
258 1.7.6.2 yamt }
259 1.7.6.2 yamt
260 1.7.6.2 yamt static void
261 1.7.6.2 yamt vcons_do_switch(struct vcons_data *vd)
262 1.7.6.2 yamt {
263 1.7.6.2 yamt struct vcons_screen *scr, *oldscr;
264 1.7.6.2 yamt
265 1.7.6.2 yamt scr = vd->wanted;
266 1.7.6.2 yamt if (!scr) {
267 1.7.6.2 yamt printf("vcons_switch_screen: disappeared\n");
268 1.7.6.2 yamt vd->switch_cb(vd->switch_cb_arg, EIO, 0);
269 1.7.6.2 yamt return;
270 1.7.6.2 yamt }
271 1.7.6.2 yamt oldscr = vd->active; /* can be NULL! */
272 1.7.6.2 yamt
273 1.7.6.2 yamt /*
274 1.7.6.2 yamt * if there's an old, visible screen we mark it invisible and wait
275 1.7.6.2 yamt * until it's not busy so we can safely switch
276 1.7.6.2 yamt */
277 1.7.6.2 yamt if (oldscr != NULL) {
278 1.7.6.2 yamt SCREEN_INVISIBLE(oldscr);
279 1.7.6.2 yamt if (SCREEN_IS_BUSY(oldscr)) {
280 1.7.6.2 yamt callout_reset(&vd->switch_callout, 1,
281 1.7.6.2 yamt (void(*)(void *))vcons_do_switch, vd);
282 1.7.6.2 yamt #ifdef DIAGNOSTIC
283 1.7.6.2 yamt /* bitch if we wait too long */
284 1.7.6.2 yamt vd->switch_poll_count++;
285 1.7.6.2 yamt if (vd->switch_poll_count > 100) {
286 1.7.6.2 yamt panic("vcons: screen still busy");
287 1.7.6.2 yamt }
288 1.7.6.2 yamt #endif
289 1.7.6.2 yamt return;
290 1.7.6.2 yamt }
291 1.7.6.2 yamt /* invisible screen -> no visible cursor image */
292 1.7.6.2 yamt oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
293 1.7.6.2 yamt #ifdef DIAGNOSTIC
294 1.7.6.2 yamt vd->switch_poll_count = 0;
295 1.7.6.2 yamt #endif
296 1.7.6.2 yamt }
297 1.7.6.2 yamt
298 1.7.6.2 yamt if (scr == oldscr)
299 1.7.6.2 yamt return;
300 1.7.6.2 yamt
301 1.7.6.2 yamt #ifdef DIAGNOSTIC
302 1.7.6.2 yamt if (SCREEN_IS_VISIBLE(scr))
303 1.7.6.2 yamt panic("vcons_switch_screen: already active");
304 1.7.6.2 yamt #endif
305 1.7.6.2 yamt
306 1.7.6.2 yamt #ifdef notyet
307 1.7.6.2 yamt if (vd->currenttype != type) {
308 1.7.6.2 yamt vcons_set_screentype(vd, type);
309 1.7.6.2 yamt vd->currenttype = type;
310 1.7.6.2 yamt }
311 1.7.6.2 yamt #endif
312 1.7.6.2 yamt
313 1.7.6.2 yamt SCREEN_VISIBLE(scr);
314 1.7.6.2 yamt vd->active = scr;
315 1.7.6.2 yamt vd->wanted = NULL;
316 1.7.6.2 yamt
317 1.7.6.2 yamt if (vd->show_screen_cb != NULL)
318 1.7.6.2 yamt vd->show_screen_cb(scr);
319 1.7.6.2 yamt
320 1.7.6.2 yamt if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
321 1.7.6.2 yamt vcons_redraw_screen(scr);
322 1.7.6.2 yamt
323 1.7.6.2 yamt if (vd->switch_cb)
324 1.7.6.2 yamt vd->switch_cb(vd->switch_cb_arg, 0, 0);
325 1.7.6.2 yamt }
326 1.7.6.2 yamt
327 1.7.6.2 yamt void
328 1.7.6.2 yamt vcons_redraw_screen(struct vcons_screen *scr)
329 1.7.6.2 yamt {
330 1.7.6.2 yamt uint16_t *charptr = scr->scr_chars;
331 1.7.6.2 yamt long *attrptr = scr->scr_attrs;
332 1.7.6.2 yamt struct rasops_info *ri = &scr->scr_ri;
333 1.7.6.2 yamt int i, j, offset;
334 1.7.6.2 yamt
335 1.7.6.2 yamt vcons_lock(scr);
336 1.7.6.2 yamt if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
337 1.7.6.2 yamt
338 1.7.6.2 yamt /*
339 1.7.6.2 yamt * only clear the screen when RI_FULLCLEAR is set since we're
340 1.7.6.2 yamt * going to overwrite every single character cell anyway
341 1.7.6.2 yamt */
342 1.7.6.2 yamt if (ri->ri_flg & RI_FULLCLEAR) {
343 1.7.6.2 yamt scr->scr_vd->eraserows(ri, 0, ri->ri_rows,
344 1.7.6.2 yamt scr->scr_defattr);
345 1.7.6.2 yamt }
346 1.7.6.2 yamt
347 1.7.6.2 yamt /* redraw the screen */
348 1.7.6.2 yamt offset = 0;
349 1.7.6.2 yamt for (i = 0; i < ri->ri_rows; i++) {
350 1.7.6.2 yamt for (j = 0; j < ri->ri_cols; j++) {
351 1.7.6.2 yamt /*
352 1.7.6.2 yamt * no need to use the wrapper function - we
353 1.7.6.2 yamt * don't change any characters or attributes
354 1.7.6.2 yamt * and we already made sure the screen we're
355 1.7.6.2 yamt * working on is visible
356 1.7.6.2 yamt */
357 1.7.6.2 yamt scr->scr_vd->putchar(ri, i, j,
358 1.7.6.2 yamt charptr[offset], attrptr[offset]);
359 1.7.6.2 yamt offset++;
360 1.7.6.2 yamt }
361 1.7.6.2 yamt }
362 1.7.6.2 yamt ri->ri_flg &= ~RI_CURSOR;
363 1.7.6.2 yamt scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
364 1.7.6.2 yamt }
365 1.7.6.2 yamt vcons_unlock(scr);
366 1.7.6.2 yamt }
367 1.7.6.2 yamt
368 1.7.6.2 yamt static int
369 1.7.6.2 yamt vcons_ioctl(void *v, void *vs, u_long cmd, caddr_t data, int flag,
370 1.7.6.2 yamt struct lwp *l)
371 1.7.6.2 yamt {
372 1.7.6.2 yamt struct vcons_data *vd = v;
373 1.7.6.2 yamt int error;
374 1.7.6.2 yamt
375 1.7.6.2 yamt switch (cmd) {
376 1.7.6.2 yamt case WSDISPLAYIO_GETWSCHAR:
377 1.7.6.2 yamt error = vcons_getwschar((struct vcons_screen *)vs,
378 1.7.6.2 yamt (struct wsdisplay_char *)data);
379 1.7.6.2 yamt break;
380 1.7.6.2 yamt
381 1.7.6.2 yamt case WSDISPLAYIO_PUTWSCHAR:
382 1.7.6.2 yamt error = vcons_putwschar((struct vcons_screen *)vs,
383 1.7.6.2 yamt (struct wsdisplay_char *)data);
384 1.7.6.2 yamt break;
385 1.7.6.2 yamt
386 1.7.6.2 yamt default:
387 1.7.6.2 yamt if (vd->ioctl != NULL)
388 1.7.6.2 yamt error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
389 1.7.6.2 yamt else
390 1.7.6.2 yamt error = EPASSTHROUGH;
391 1.7.6.2 yamt }
392 1.7.6.2 yamt
393 1.7.6.2 yamt return error;
394 1.7.6.2 yamt }
395 1.7.6.2 yamt
396 1.7.6.2 yamt static int
397 1.7.6.2 yamt vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
398 1.7.6.2 yamt int *curxp, int *curyp, long *defattrp)
399 1.7.6.2 yamt {
400 1.7.6.2 yamt struct vcons_data *vd = v;
401 1.7.6.2 yamt struct vcons_screen *scr;
402 1.7.6.2 yamt int ret;
403 1.7.6.2 yamt
404 1.7.6.2 yamt scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO);
405 1.7.6.2 yamt if (scr == NULL)
406 1.7.6.2 yamt return ENOMEM;
407 1.7.6.2 yamt
408 1.7.6.2 yamt scr->scr_flags = 0;
409 1.7.6.2 yamt scr->scr_status = 0;
410 1.7.6.2 yamt scr->scr_busy = 0;
411 1.7.6.2 yamt scr->scr_type = type;
412 1.7.6.2 yamt
413 1.7.6.2 yamt ret = vcons_init_screen(vd, scr, 0, defattrp);
414 1.7.6.2 yamt if (ret != 0) {
415 1.7.6.2 yamt free(scr, M_DEVBUF);
416 1.7.6.2 yamt return ret;
417 1.7.6.2 yamt }
418 1.7.6.2 yamt
419 1.7.6.2 yamt if (vd->active == NULL) {
420 1.7.6.2 yamt SCREEN_VISIBLE(scr);
421 1.7.6.2 yamt vd->active = scr;
422 1.7.6.2 yamt vd->currenttype = type;
423 1.7.6.2 yamt }
424 1.7.6.2 yamt
425 1.7.6.2 yamt *cookiep = scr;
426 1.7.6.2 yamt *curxp = scr->scr_ri.ri_ccol;
427 1.7.6.2 yamt *curyp = scr->scr_ri.ri_crow;
428 1.7.6.2 yamt return 0;
429 1.7.6.2 yamt }
430 1.7.6.2 yamt
431 1.7.6.2 yamt static void
432 1.7.6.2 yamt vcons_free_screen(void *v, void *cookie)
433 1.7.6.2 yamt {
434 1.7.6.2 yamt struct vcons_data *vd = v;
435 1.7.6.2 yamt struct vcons_screen *scr = cookie;
436 1.7.6.2 yamt
437 1.7.6.2 yamt vcons_lock(scr);
438 1.7.6.2 yamt /* there should be no rasops activity here */
439 1.7.6.2 yamt
440 1.7.6.2 yamt LIST_REMOVE(scr, next);
441 1.7.6.2 yamt
442 1.7.6.2 yamt if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) {
443 1.7.6.2 yamt free(scr->scr_attrs, M_DEVBUF);
444 1.7.6.2 yamt free(scr, M_DEVBUF);
445 1.7.6.2 yamt } else {
446 1.7.6.2 yamt /*
447 1.7.6.2 yamt * maybe we should just restore the old rasops_info methods
448 1.7.6.2 yamt * and free the character/attribute buffer here?
449 1.7.6.2 yamt */
450 1.7.6.2 yamt #ifdef VCONS_DEBUG
451 1.7.6.2 yamt panic("vcons_free_screen: console");
452 1.7.6.2 yamt #else
453 1.7.6.2 yamt printf("vcons_free_screen: console\n");
454 1.7.6.2 yamt #endif
455 1.7.6.2 yamt }
456 1.7.6.2 yamt
457 1.7.6.2 yamt if (vd->active == scr)
458 1.7.6.2 yamt vd->active = NULL;
459 1.7.6.2 yamt }
460 1.7.6.2 yamt
461 1.7.6.2 yamt static int
462 1.7.6.2 yamt vcons_show_screen(void *v, void *cookie, int waitok,
463 1.7.6.2 yamt void (*cb)(void *, int, int), void *cb_arg)
464 1.7.6.2 yamt {
465 1.7.6.2 yamt struct vcons_data *vd = v;
466 1.7.6.2 yamt struct vcons_screen *scr;
467 1.7.6.2 yamt
468 1.7.6.2 yamt scr = cookie;
469 1.7.6.2 yamt if (scr == vd->active)
470 1.7.6.2 yamt return 0;
471 1.7.6.2 yamt
472 1.7.6.2 yamt vd->wanted = scr;
473 1.7.6.2 yamt vd->switch_cb = cb;
474 1.7.6.2 yamt vd->switch_cb_arg = cb_arg;
475 1.7.6.2 yamt if (cb) {
476 1.7.6.2 yamt callout_reset(&vd->switch_callout, 0,
477 1.7.6.2 yamt (void(*)(void *))vcons_do_switch, vd);
478 1.7.6.2 yamt return EAGAIN;
479 1.7.6.2 yamt }
480 1.7.6.2 yamt
481 1.7.6.2 yamt vcons_do_switch(vd);
482 1.7.6.2 yamt return 0;
483 1.7.6.2 yamt }
484 1.7.6.2 yamt
485 1.7.6.2 yamt /* wrappers for rasops_info methods */
486 1.7.6.2 yamt
487 1.7.6.2 yamt static void
488 1.7.6.2 yamt vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
489 1.7.6.2 yamt {
490 1.7.6.2 yamt struct rasops_info *ri = cookie;
491 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
492 1.7.6.2 yamt int from = srccol + row * ri->ri_cols;
493 1.7.6.2 yamt int to = dstcol + row * ri->ri_cols;
494 1.7.6.2 yamt
495 1.7.6.2 yamt memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
496 1.7.6.2 yamt ncols * sizeof(long));
497 1.7.6.2 yamt memmove(&scr->scr_chars[to], &scr->scr_chars[from],
498 1.7.6.2 yamt ncols * sizeof(uint16_t));
499 1.7.6.2 yamt }
500 1.7.6.2 yamt
501 1.7.6.2 yamt static void
502 1.7.6.2 yamt vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
503 1.7.6.2 yamt {
504 1.7.6.2 yamt struct rasops_info *ri = cookie;
505 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
506 1.7.6.2 yamt
507 1.7.6.2 yamt vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
508 1.7.6.2 yamt
509 1.7.6.2 yamt vcons_lock(scr);
510 1.7.6.2 yamt if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
511 1.7.6.2 yamt scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols);
512 1.7.6.2 yamt }
513 1.7.6.2 yamt vcons_unlock(scr);
514 1.7.6.2 yamt }
515 1.7.6.2 yamt
516 1.7.6.2 yamt static void
517 1.7.6.2 yamt vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr)
518 1.7.6.2 yamt {
519 1.7.6.2 yamt struct rasops_info *ri = cookie;
520 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
521 1.7.6.2 yamt int start = startcol + row * ri->ri_cols;
522 1.7.6.2 yamt int end = start + ncols, i;
523 1.7.6.2 yamt
524 1.7.6.2 yamt for (i = start; i < end; i++) {
525 1.7.6.2 yamt scr->scr_attrs[i] = fillattr;
526 1.7.6.2 yamt scr->scr_chars[i] = 0x20;
527 1.7.6.2 yamt }
528 1.7.6.2 yamt }
529 1.7.6.2 yamt
530 1.7.6.2 yamt static void
531 1.7.6.2 yamt vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
532 1.7.6.2 yamt {
533 1.7.6.2 yamt struct rasops_info *ri = cookie;
534 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
535 1.7.6.2 yamt
536 1.7.6.2 yamt vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr);
537 1.7.6.2 yamt
538 1.7.6.2 yamt vcons_lock(scr);
539 1.7.6.2 yamt if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
540 1.7.6.2 yamt scr->scr_vd->erasecols(cookie, row, startcol, ncols,
541 1.7.6.2 yamt fillattr);
542 1.7.6.2 yamt }
543 1.7.6.2 yamt vcons_unlock(scr);
544 1.7.6.2 yamt }
545 1.7.6.2 yamt
546 1.7.6.2 yamt static void
547 1.7.6.2 yamt vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
548 1.7.6.2 yamt {
549 1.7.6.2 yamt struct rasops_info *ri = cookie;
550 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
551 1.7.6.2 yamt int from, to, len;
552 1.7.6.2 yamt
553 1.7.6.2 yamt from = ri->ri_cols * srcrow;
554 1.7.6.2 yamt to = ri->ri_cols * dstrow;
555 1.7.6.2 yamt len = ri->ri_cols * nrows;
556 1.7.6.2 yamt
557 1.7.6.2 yamt memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
558 1.7.6.2 yamt len * sizeof(long));
559 1.7.6.2 yamt memmove(&scr->scr_chars[to], &scr->scr_chars[from],
560 1.7.6.2 yamt len * sizeof(uint16_t));
561 1.7.6.2 yamt }
562 1.7.6.2 yamt
563 1.7.6.2 yamt static void
564 1.7.6.2 yamt vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
565 1.7.6.2 yamt {
566 1.7.6.2 yamt struct rasops_info *ri = cookie;
567 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
568 1.7.6.2 yamt
569 1.7.6.2 yamt vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
570 1.7.6.2 yamt
571 1.7.6.2 yamt vcons_lock(scr);
572 1.7.6.2 yamt if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
573 1.7.6.2 yamt scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows);
574 1.7.6.2 yamt }
575 1.7.6.2 yamt vcons_unlock(scr);
576 1.7.6.2 yamt }
577 1.7.6.2 yamt
578 1.7.6.2 yamt static void
579 1.7.6.2 yamt vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr)
580 1.7.6.2 yamt {
581 1.7.6.2 yamt struct rasops_info *ri = cookie;
582 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
583 1.7.6.2 yamt int start, end, i;
584 1.7.6.2 yamt
585 1.7.6.2 yamt start = ri->ri_cols * row;
586 1.7.6.2 yamt end = ri->ri_cols * (row + nrows);
587 1.7.6.2 yamt
588 1.7.6.2 yamt for (i = start; i < end; i++) {
589 1.7.6.2 yamt scr->scr_attrs[i] = fillattr;
590 1.7.6.2 yamt scr->scr_chars[i] = 0x20;
591 1.7.6.2 yamt }
592 1.7.6.2 yamt }
593 1.7.6.2 yamt
594 1.7.6.2 yamt static void
595 1.7.6.2 yamt vcons_eraserows(void *cookie, int row, int nrows, long fillattr)
596 1.7.6.2 yamt {
597 1.7.6.2 yamt struct rasops_info *ri = cookie;
598 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
599 1.7.6.2 yamt
600 1.7.6.2 yamt vcons_eraserows_buffer(cookie, row, nrows, fillattr);
601 1.7.6.2 yamt
602 1.7.6.2 yamt vcons_lock(scr);
603 1.7.6.2 yamt if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
604 1.7.6.2 yamt scr->scr_vd->eraserows(cookie, row, nrows, fillattr);
605 1.7.6.2 yamt }
606 1.7.6.2 yamt vcons_unlock(scr);
607 1.7.6.2 yamt }
608 1.7.6.2 yamt
609 1.7.6.2 yamt static void
610 1.7.6.2 yamt vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
611 1.7.6.2 yamt {
612 1.7.6.2 yamt struct rasops_info *ri = cookie;
613 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
614 1.7.6.2 yamt int pos;
615 1.7.6.2 yamt
616 1.7.6.2 yamt if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
617 1.7.6.2 yamt (col < ri->ri_cols)) {
618 1.7.6.2 yamt pos = col + row * ri->ri_cols;
619 1.7.6.2 yamt scr->scr_attrs[pos] = attr;
620 1.7.6.2 yamt scr->scr_chars[pos] = c;
621 1.7.6.2 yamt }
622 1.7.6.2 yamt }
623 1.7.6.2 yamt
624 1.7.6.2 yamt static void
625 1.7.6.2 yamt vcons_putchar(void *cookie, int row, int col, u_int c, long attr)
626 1.7.6.2 yamt {
627 1.7.6.2 yamt struct rasops_info *ri = cookie;
628 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
629 1.7.6.2 yamt
630 1.7.6.2 yamt vcons_putchar_buffer(cookie, row, col, c, attr);
631 1.7.6.2 yamt
632 1.7.6.2 yamt vcons_lock(scr);
633 1.7.6.2 yamt if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
634 1.7.6.2 yamt scr->scr_vd->putchar(cookie, row, col, c, attr);
635 1.7.6.2 yamt }
636 1.7.6.2 yamt vcons_unlock(scr);
637 1.7.6.2 yamt }
638 1.7.6.2 yamt
639 1.7.6.2 yamt static void
640 1.7.6.2 yamt vcons_cursor(void *cookie, int on, int row, int col)
641 1.7.6.2 yamt {
642 1.7.6.2 yamt struct rasops_info *ri = cookie;
643 1.7.6.2 yamt struct vcons_screen *scr = ri->ri_hw;
644 1.7.6.2 yamt
645 1.7.6.2 yamt vcons_lock(scr);
646 1.7.6.2 yamt if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
647 1.7.6.2 yamt scr->scr_vd->cursor(cookie, on, row, col);
648 1.7.6.2 yamt } else {
649 1.7.6.2 yamt scr->scr_ri.ri_crow = row;
650 1.7.6.2 yamt scr->scr_ri.ri_ccol = col;
651 1.7.6.2 yamt }
652 1.7.6.2 yamt vcons_unlock(scr);
653 1.7.6.2 yamt }
654 1.7.6.2 yamt
655 1.7.6.2 yamt /* methods to read/write characters via ioctl() */
656 1.7.6.2 yamt
657 1.7.6.2 yamt static int
658 1.7.6.2 yamt vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
659 1.7.6.2 yamt {
660 1.7.6.2 yamt long attr;
661 1.7.6.2 yamt struct rasops_info *ri;
662 1.7.6.2 yamt
663 1.7.6.2 yamt KASSERT(scr != NULL && wsc != NULL);
664 1.7.6.2 yamt
665 1.7.6.2 yamt ri = &scr->scr_ri;
666 1.7.6.2 yamt
667 1.7.6.2 yamt ri->ri_ops.allocattr(ri, wsc->foreground, wsc->background,
668 1.7.6.2 yamt wsc->flags, &attr);
669 1.7.6.2 yamt vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr);
670 1.7.6.2 yamt return 0;
671 1.7.6.2 yamt }
672 1.7.6.2 yamt
673 1.7.6.2 yamt static int
674 1.7.6.2 yamt vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
675 1.7.6.2 yamt {
676 1.7.6.2 yamt int offset;
677 1.7.6.2 yamt long attr;
678 1.7.6.2 yamt struct rasops_info *ri;
679 1.7.6.2 yamt
680 1.7.6.2 yamt KASSERT(scr != NULL && wsc != NULL);
681 1.7.6.2 yamt
682 1.7.6.2 yamt ri = &scr->scr_ri;
683 1.7.6.2 yamt offset = ri->ri_cols * wsc->row + wsc->col;
684 1.7.6.2 yamt wsc->letter = scr->scr_chars[offset];
685 1.7.6.2 yamt attr = scr->scr_attrs[offset];
686 1.7.6.2 yamt
687 1.7.6.2 yamt /*
688 1.7.6.2 yamt * this is ugly. We need to break up an attribute into colours and
689 1.7.6.2 yamt * flags but there's no rasops method to do that so we must rely on
690 1.7.6.2 yamt * the 'canonical' encoding.
691 1.7.6.2 yamt */
692 1.7.6.2 yamt wsc->foreground = (attr & 0xff000000) >> 24;
693 1.7.6.2 yamt wsc->background = (attr & 0x00ff0000) >> 16;
694 1.7.6.2 yamt wsc->flags = (attr & 0x0000ff00) >> 8;
695 1.7.6.2 yamt return 0;
696 1.7.6.2 yamt }
697