wsdisplay_vcons.c revision 1.19 1 1.19 macallan /* $NetBSD: wsdisplay_vcons.c,v 1.19 2011/01/25 20:28:21 macallan Exp $ */
2 1.1 macallan
3 1.1 macallan /*-
4 1.1 macallan * Copyright (c) 2005, 2006 Michael Lorenz
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE.
27 1.1 macallan */
28 1.1 macallan
29 1.1 macallan #include <sys/cdefs.h>
30 1.19 macallan __KERNEL_RCSID(0, "$NetBSD: wsdisplay_vcons.c,v 1.19 2011/01/25 20:28:21 macallan Exp $");
31 1.1 macallan
32 1.1 macallan #include <sys/param.h>
33 1.1 macallan #include <sys/systm.h>
34 1.1 macallan #include <sys/kernel.h>
35 1.1 macallan #include <sys/buf.h>
36 1.1 macallan #include <sys/device.h>
37 1.1 macallan #include <sys/ioctl.h>
38 1.1 macallan #include <sys/malloc.h>
39 1.1 macallan #include <sys/mman.h>
40 1.1 macallan #include <sys/tty.h>
41 1.1 macallan #include <sys/conf.h>
42 1.1 macallan #include <sys/proc.h>
43 1.1 macallan #include <sys/kthread.h>
44 1.1 macallan #include <sys/tprintf.h>
45 1.19 macallan #include <sys/atomic.h>
46 1.1 macallan
47 1.1 macallan #include <dev/wscons/wsdisplayvar.h>
48 1.1 macallan #include <dev/wscons/wsconsio.h>
49 1.1 macallan #include <dev/wsfont/wsfont.h>
50 1.1 macallan #include <dev/rasops/rasops.h>
51 1.1 macallan
52 1.1 macallan #include <dev/wscons/wsdisplay_vconsvar.h>
53 1.1 macallan
54 1.14 macallan #include "opt_wsemul.h"
55 1.14 macallan #include "opt_wsdisplay_compat.h"
56 1.14 macallan #include "opt_vcons.h"
57 1.14 macallan
58 1.1 macallan static void vcons_dummy_init_screen(void *, struct vcons_screen *, int,
59 1.1 macallan long *);
60 1.1 macallan
61 1.10 christos static int vcons_ioctl(void *, void *, u_long, void *, int, struct lwp *);
62 1.1 macallan static int vcons_alloc_screen(void *, const struct wsscreen_descr *, void **,
63 1.1 macallan int *, int *, long *);
64 1.1 macallan static void vcons_free_screen(void *, void *);
65 1.1 macallan static int vcons_show_screen(void *, void *, int, void (*)(void *, int, int),
66 1.1 macallan void *);
67 1.1 macallan
68 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
69 1.14 macallan static void vcons_scroll(void *, void *, int);
70 1.14 macallan static void vcons_do_scroll(struct vcons_screen *);
71 1.14 macallan #endif
72 1.14 macallan
73 1.15 joerg static void vcons_do_switch(void *);
74 1.1 macallan
75 1.1 macallan /* methods that work only on text buffers */
76 1.1 macallan static void vcons_copycols_buffer(void *, int, int, int, int);
77 1.1 macallan static void vcons_erasecols_buffer(void *, int, int, int, long);
78 1.1 macallan static void vcons_copyrows_buffer(void *, int, int, int);
79 1.1 macallan static void vcons_eraserows_buffer(void *, int, int, long);
80 1.1 macallan static void vcons_putchar_buffer(void *, int, int, u_int, long);
81 1.1 macallan
82 1.19 macallan #ifdef VCONS_DRAW_ASYNC
83 1.19 macallan /* methods that work asynchronously */
84 1.19 macallan static void vcons_copycols_async(void *, int, int, int, int);
85 1.19 macallan static void vcons_erasecols_async(void *, int, int, int, long);
86 1.19 macallan static void vcons_copyrows_async(void *, int, int, int);
87 1.19 macallan static void vcons_eraserows_async(void *, int, int, long);
88 1.19 macallan static void vcons_putchar_async(void *, int, int, u_int, long);
89 1.19 macallan static void vcons_cursor_async(void *, int, int, int);
90 1.19 macallan #endif
91 1.19 macallan
92 1.1 macallan /*
93 1.1 macallan * actual wrapper methods which call both the _buffer ones above and the
94 1.1 macallan * driver supplied ones to do the drawing
95 1.1 macallan */
96 1.1 macallan static void vcons_copycols(void *, int, int, int, int);
97 1.1 macallan static void vcons_erasecols(void *, int, int, int, long);
98 1.1 macallan static void vcons_copyrows(void *, int, int, int);
99 1.1 macallan static void vcons_eraserows(void *, int, int, long);
100 1.1 macallan static void vcons_putchar(void *, int, int, u_int, long);
101 1.1 macallan static void vcons_cursor(void *, int, int, int);
102 1.1 macallan
103 1.17 macallan /*
104 1.17 macallan * methods that avoid framebuffer reads
105 1.17 macallan */
106 1.17 macallan static void vcons_copycols_noread(void *, int, int, int, int);
107 1.17 macallan static void vcons_copyrows_noread(void *, int, int, int);
108 1.17 macallan
109 1.17 macallan
110 1.14 macallan /* support for reading/writing text buffers. For wsmoused */
111 1.6 jmmv static int vcons_putwschar(struct vcons_screen *, struct wsdisplay_char *);
112 1.6 jmmv static int vcons_getwschar(struct vcons_screen *, struct wsdisplay_char *);
113 1.1 macallan
114 1.1 macallan static void vcons_lock(struct vcons_screen *);
115 1.1 macallan static void vcons_unlock(struct vcons_screen *);
116 1.1 macallan
117 1.19 macallan #ifdef VCONS_DRAW_ASYNC
118 1.14 macallan static void vcons_kthread(void *);
119 1.14 macallan #endif
120 1.1 macallan
121 1.1 macallan int
122 1.1 macallan vcons_init(struct vcons_data *vd, void *cookie, struct wsscreen_descr *def,
123 1.1 macallan struct wsdisplay_accessops *ao)
124 1.1 macallan {
125 1.2 macallan
126 1.2 macallan /* zero out everything so we can rely on untouched fields being 0 */
127 1.3 macallan memset(vd, 0, sizeof(struct vcons_data));
128 1.2 macallan
129 1.1 macallan vd->cookie = cookie;
130 1.1 macallan
131 1.1 macallan vd->init_screen = vcons_dummy_init_screen;
132 1.1 macallan vd->show_screen_cb = NULL;
133 1.1 macallan
134 1.6 jmmv /* keep a copy of the accessops that we replace below with our
135 1.6 jmmv * own wrappers */
136 1.6 jmmv vd->ioctl = ao->ioctl;
137 1.6 jmmv
138 1.6 jmmv /* configure the accessops */
139 1.6 jmmv ao->ioctl = vcons_ioctl;
140 1.1 macallan ao->alloc_screen = vcons_alloc_screen;
141 1.1 macallan ao->free_screen = vcons_free_screen;
142 1.1 macallan ao->show_screen = vcons_show_screen;
143 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
144 1.14 macallan ao->scroll = vcons_scroll;
145 1.14 macallan #endif
146 1.1 macallan
147 1.1 macallan LIST_INIT(&vd->screens);
148 1.1 macallan vd->active = NULL;
149 1.1 macallan vd->wanted = NULL;
150 1.1 macallan vd->currenttype = def;
151 1.11 ad callout_init(&vd->switch_callout, 0);
152 1.15 joerg callout_setfunc(&vd->switch_callout, vcons_do_switch, vd);
153 1.1 macallan
154 1.1 macallan /*
155 1.1 macallan * a lock to serialize access to the framebuffer.
156 1.1 macallan * when switching screens we need to make sure there's no rasops
157 1.1 macallan * operation in progress
158 1.1 macallan */
159 1.1 macallan #ifdef DIAGNOSTIC
160 1.1 macallan vd->switch_poll_count = 0;
161 1.1 macallan #endif
162 1.19 macallan #ifdef VCONS_DRAW_ASYNC
163 1.14 macallan kthread_create(PRI_NONE, 0, NULL, vcons_kthread, vd,
164 1.19 macallan &vd->drawing_thread, "vcons_draw");
165 1.14 macallan #endif
166 1.1 macallan return 0;
167 1.1 macallan }
168 1.1 macallan
169 1.1 macallan static void
170 1.1 macallan vcons_lock(struct vcons_screen *scr)
171 1.1 macallan {
172 1.1 macallan #ifdef VCONS_PARANOIA
173 1.1 macallan int s;
174 1.1 macallan
175 1.1 macallan s = splhigh();
176 1.1 macallan #endif
177 1.1 macallan SCREEN_BUSY(scr);
178 1.1 macallan #ifdef VCONS_PARANOIA
179 1.1 macallan splx(s);
180 1.1 macallan #endif
181 1.1 macallan }
182 1.1 macallan
183 1.1 macallan static void
184 1.1 macallan vcons_unlock(struct vcons_screen *scr)
185 1.1 macallan {
186 1.1 macallan #ifdef VCONS_PARANOIA
187 1.1 macallan int s;
188 1.1 macallan
189 1.1 macallan s = splhigh();
190 1.1 macallan #endif
191 1.1 macallan SCREEN_IDLE(scr);
192 1.1 macallan #ifdef VCONS_PARANOIA
193 1.1 macallan splx(s);
194 1.1 macallan #endif
195 1.1 macallan }
196 1.1 macallan
197 1.1 macallan static void
198 1.9 christos vcons_dummy_init_screen(void *cookie,
199 1.9 christos struct vcons_screen *scr, int exists,
200 1.9 christos long *defattr)
201 1.1 macallan {
202 1.1 macallan
203 1.1 macallan /*
204 1.1 macallan * default init_screen() method.
205 1.1 macallan * Needs to be overwritten so we bitch and whine in case anyone ends
206 1.1 macallan * up in here.
207 1.1 macallan */
208 1.1 macallan printf("vcons_init_screen: dummy function called. Your driver is "
209 1.1 macallan "supposed to supply a replacement for proper operation\n");
210 1.1 macallan }
211 1.1 macallan
212 1.1 macallan int
213 1.1 macallan vcons_init_screen(struct vcons_data *vd, struct vcons_screen *scr,
214 1.1 macallan int existing, long *defattr)
215 1.1 macallan {
216 1.1 macallan struct rasops_info *ri = &scr->scr_ri;
217 1.1 macallan int cnt, i;
218 1.1 macallan
219 1.1 macallan scr->scr_cookie = vd->cookie;
220 1.4 jmcneill scr->scr_vd = scr->scr_origvd = vd;
221 1.14 macallan scr->scr_busy = 0;
222 1.2 macallan
223 1.1 macallan /*
224 1.1 macallan * call the driver-supplied init_screen function which is expected
225 1.1 macallan * to set up rasops_info, override cursor() and probably others
226 1.1 macallan */
227 1.1 macallan vd->init_screen(vd->cookie, scr, existing, defattr);
228 1.1 macallan
229 1.1 macallan /*
230 1.1 macallan * save the non virtual console aware rasops and replace them with
231 1.1 macallan * our wrappers
232 1.1 macallan */
233 1.1 macallan vd->eraserows = ri->ri_ops.eraserows;
234 1.1 macallan vd->erasecols = ri->ri_ops.erasecols;
235 1.1 macallan vd->putchar = ri->ri_ops.putchar;
236 1.1 macallan vd->cursor = ri->ri_ops.cursor;
237 1.1 macallan
238 1.18 macallan if (scr->scr_flags & VCONS_NO_COPYCOLS) {
239 1.19 macallan vd->copycols = vcons_copycols_noread;
240 1.18 macallan } else {
241 1.19 macallan vd->copycols = ri->ri_ops.copycols;
242 1.18 macallan }
243 1.18 macallan
244 1.18 macallan if (scr->scr_flags & VCONS_NO_COPYROWS) {
245 1.19 macallan vd->copyrows = vcons_copyrows_noread;
246 1.17 macallan } else {
247 1.19 macallan vd->copyrows = ri->ri_ops.copyrows;
248 1.17 macallan }
249 1.17 macallan
250 1.19 macallan ri->ri_ops.eraserows = vcons_eraserows;
251 1.19 macallan ri->ri_ops.erasecols = vcons_erasecols;
252 1.19 macallan ri->ri_ops.putchar = vcons_putchar;
253 1.19 macallan ri->ri_ops.cursor = vcons_cursor;
254 1.19 macallan ri->ri_ops.copycols = vcons_copycols;
255 1.19 macallan ri->ri_ops.copyrows = vcons_copyrows;
256 1.19 macallan
257 1.19 macallan
258 1.1 macallan ri->ri_hw = scr;
259 1.1 macallan
260 1.1 macallan /*
261 1.1 macallan * we allocate both chars and attributes in one chunk, attributes first
262 1.1 macallan * because they have the (potentially) bigger alignment
263 1.1 macallan */
264 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
265 1.14 macallan cnt = (ri->ri_rows + WSDISPLAY_SCROLLBACK_LINES) * ri->ri_cols;
266 1.14 macallan scr->scr_lines_in_buffer = WSDISPLAY_SCROLLBACK_LINES;
267 1.14 macallan scr->scr_current_line = 0;
268 1.14 macallan scr->scr_line_wanted = 0;
269 1.14 macallan scr->scr_offset_to_zero = ri->ri_cols * WSDISPLAY_SCROLLBACK_LINES;
270 1.14 macallan scr->scr_current_offset = scr->scr_offset_to_zero;
271 1.14 macallan #else
272 1.1 macallan cnt = ri->ri_rows * ri->ri_cols;
273 1.14 macallan #endif
274 1.1 macallan scr->scr_attrs = (long *)malloc(cnt * (sizeof(long) +
275 1.1 macallan sizeof(uint16_t)), M_DEVBUF, M_WAITOK);
276 1.1 macallan if (scr->scr_attrs == NULL)
277 1.1 macallan return ENOMEM;
278 1.1 macallan
279 1.1 macallan scr->scr_chars = (uint16_t *)&scr->scr_attrs[cnt];
280 1.1 macallan
281 1.1 macallan ri->ri_ops.allocattr(ri, WS_DEFAULT_FG, WS_DEFAULT_BG, 0, defattr);
282 1.1 macallan scr->scr_defattr = *defattr;
283 1.1 macallan
284 1.1 macallan /*
285 1.1 macallan * fill the attribute buffer with *defattr, chars with 0x20
286 1.1 macallan * since we don't know if the driver tries to mimic firmware output or
287 1.1 macallan * reset everything we do nothing to VRAM here, any driver that feels
288 1.1 macallan * the need to clear screen or something will have to do it on its own
289 1.1 macallan * Additional screens will start out in the background anyway so
290 1.1 macallan * cleaning or not only really affects the initial console screen
291 1.1 macallan */
292 1.1 macallan for (i = 0; i < cnt; i++) {
293 1.1 macallan scr->scr_attrs[i] = *defattr;
294 1.1 macallan scr->scr_chars[i] = 0x20;
295 1.1 macallan }
296 1.1 macallan
297 1.2 macallan if(vd->active == NULL) {
298 1.2 macallan vd->active = scr;
299 1.2 macallan SCREEN_VISIBLE(scr);
300 1.2 macallan }
301 1.2 macallan
302 1.1 macallan if (existing) {
303 1.2 macallan SCREEN_VISIBLE(scr);
304 1.2 macallan vd->active = scr;
305 1.1 macallan } else {
306 1.2 macallan SCREEN_INVISIBLE(scr);
307 1.1 macallan }
308 1.1 macallan
309 1.1 macallan LIST_INSERT_HEAD(&vd->screens, scr, next);
310 1.1 macallan return 0;
311 1.1 macallan }
312 1.1 macallan
313 1.1 macallan static void
314 1.15 joerg vcons_do_switch(void *arg)
315 1.1 macallan {
316 1.15 joerg struct vcons_data *vd = arg;
317 1.1 macallan struct vcons_screen *scr, *oldscr;
318 1.1 macallan
319 1.1 macallan scr = vd->wanted;
320 1.1 macallan if (!scr) {
321 1.1 macallan printf("vcons_switch_screen: disappeared\n");
322 1.1 macallan vd->switch_cb(vd->switch_cb_arg, EIO, 0);
323 1.1 macallan return;
324 1.1 macallan }
325 1.1 macallan oldscr = vd->active; /* can be NULL! */
326 1.1 macallan
327 1.1 macallan /*
328 1.1 macallan * if there's an old, visible screen we mark it invisible and wait
329 1.1 macallan * until it's not busy so we can safely switch
330 1.1 macallan */
331 1.1 macallan if (oldscr != NULL) {
332 1.1 macallan SCREEN_INVISIBLE(oldscr);
333 1.1 macallan if (SCREEN_IS_BUSY(oldscr)) {
334 1.15 joerg callout_schedule(&vd->switch_callout, 1);
335 1.1 macallan #ifdef DIAGNOSTIC
336 1.1 macallan /* bitch if we wait too long */
337 1.1 macallan vd->switch_poll_count++;
338 1.1 macallan if (vd->switch_poll_count > 100) {
339 1.1 macallan panic("vcons: screen still busy");
340 1.1 macallan }
341 1.1 macallan #endif
342 1.1 macallan return;
343 1.1 macallan }
344 1.1 macallan /* invisible screen -> no visible cursor image */
345 1.1 macallan oldscr->scr_ri.ri_flg &= ~RI_CURSOR;
346 1.1 macallan #ifdef DIAGNOSTIC
347 1.1 macallan vd->switch_poll_count = 0;
348 1.1 macallan #endif
349 1.1 macallan }
350 1.1 macallan
351 1.1 macallan if (scr == oldscr)
352 1.1 macallan return;
353 1.1 macallan
354 1.1 macallan #ifdef DIAGNOSTIC
355 1.1 macallan if (SCREEN_IS_VISIBLE(scr))
356 1.14 macallan printf("vcons_switch_screen: already active");
357 1.1 macallan #endif
358 1.1 macallan
359 1.1 macallan #ifdef notyet
360 1.1 macallan if (vd->currenttype != type) {
361 1.1 macallan vcons_set_screentype(vd, type);
362 1.1 macallan vd->currenttype = type;
363 1.1 macallan }
364 1.1 macallan #endif
365 1.1 macallan
366 1.1 macallan SCREEN_VISIBLE(scr);
367 1.1 macallan vd->active = scr;
368 1.1 macallan vd->wanted = NULL;
369 1.1 macallan
370 1.1 macallan if (vd->show_screen_cb != NULL)
371 1.1 macallan vd->show_screen_cb(scr);
372 1.1 macallan
373 1.1 macallan if ((scr->scr_flags & VCONS_NO_REDRAW) == 0)
374 1.1 macallan vcons_redraw_screen(scr);
375 1.1 macallan
376 1.1 macallan if (vd->switch_cb)
377 1.1 macallan vd->switch_cb(vd->switch_cb_arg, 0, 0);
378 1.1 macallan }
379 1.1 macallan
380 1.1 macallan void
381 1.1 macallan vcons_redraw_screen(struct vcons_screen *scr)
382 1.1 macallan {
383 1.1 macallan uint16_t *charptr = scr->scr_chars;
384 1.1 macallan long *attrptr = scr->scr_attrs;
385 1.1 macallan struct rasops_info *ri = &scr->scr_ri;
386 1.1 macallan int i, j, offset;
387 1.1 macallan
388 1.1 macallan vcons_lock(scr);
389 1.5 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
390 1.1 macallan
391 1.1 macallan /*
392 1.1 macallan * only clear the screen when RI_FULLCLEAR is set since we're
393 1.1 macallan * going to overwrite every single character cell anyway
394 1.1 macallan */
395 1.1 macallan if (ri->ri_flg & RI_FULLCLEAR) {
396 1.1 macallan scr->scr_vd->eraserows(ri, 0, ri->ri_rows,
397 1.1 macallan scr->scr_defattr);
398 1.1 macallan }
399 1.1 macallan
400 1.1 macallan /* redraw the screen */
401 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
402 1.14 macallan offset = scr->scr_current_offset;
403 1.14 macallan #else
404 1.1 macallan offset = 0;
405 1.14 macallan #endif
406 1.1 macallan for (i = 0; i < ri->ri_rows; i++) {
407 1.1 macallan for (j = 0; j < ri->ri_cols; j++) {
408 1.1 macallan /*
409 1.1 macallan * no need to use the wrapper function - we
410 1.1 macallan * don't change any characters or attributes
411 1.1 macallan * and we already made sure the screen we're
412 1.1 macallan * working on is visible
413 1.1 macallan */
414 1.1 macallan scr->scr_vd->putchar(ri, i, j,
415 1.1 macallan charptr[offset], attrptr[offset]);
416 1.1 macallan offset++;
417 1.1 macallan }
418 1.1 macallan }
419 1.1 macallan ri->ri_flg &= ~RI_CURSOR;
420 1.1 macallan scr->scr_vd->cursor(ri, 1, ri->ri_crow, ri->ri_ccol);
421 1.1 macallan }
422 1.1 macallan vcons_unlock(scr);
423 1.1 macallan }
424 1.1 macallan
425 1.1 macallan static int
426 1.10 christos vcons_ioctl(void *v, void *vs, u_long cmd, void *data, int flag,
427 1.6 jmmv struct lwp *l)
428 1.6 jmmv {
429 1.6 jmmv struct vcons_data *vd = v;
430 1.6 jmmv int error;
431 1.6 jmmv
432 1.6 jmmv switch (cmd) {
433 1.6 jmmv case WSDISPLAYIO_GETWSCHAR:
434 1.6 jmmv error = vcons_getwschar((struct vcons_screen *)vs,
435 1.6 jmmv (struct wsdisplay_char *)data);
436 1.6 jmmv break;
437 1.6 jmmv
438 1.6 jmmv case WSDISPLAYIO_PUTWSCHAR:
439 1.6 jmmv error = vcons_putwschar((struct vcons_screen *)vs,
440 1.6 jmmv (struct wsdisplay_char *)data);
441 1.6 jmmv break;
442 1.6 jmmv
443 1.6 jmmv default:
444 1.6 jmmv if (vd->ioctl != NULL)
445 1.6 jmmv error = (*vd->ioctl)(v, vs, cmd, data, flag, l);
446 1.6 jmmv else
447 1.6 jmmv error = EPASSTHROUGH;
448 1.6 jmmv }
449 1.6 jmmv
450 1.6 jmmv return error;
451 1.6 jmmv }
452 1.6 jmmv
453 1.6 jmmv static int
454 1.1 macallan vcons_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
455 1.1 macallan int *curxp, int *curyp, long *defattrp)
456 1.1 macallan {
457 1.1 macallan struct vcons_data *vd = v;
458 1.1 macallan struct vcons_screen *scr;
459 1.1 macallan int ret;
460 1.1 macallan
461 1.1 macallan scr = malloc(sizeof(struct vcons_screen), M_DEVBUF, M_WAITOK | M_ZERO);
462 1.1 macallan if (scr == NULL)
463 1.1 macallan return ENOMEM;
464 1.1 macallan
465 1.1 macallan scr->scr_flags = 0;
466 1.2 macallan scr->scr_status = 0;
467 1.2 macallan scr->scr_busy = 0;
468 1.1 macallan scr->scr_type = type;
469 1.1 macallan
470 1.1 macallan ret = vcons_init_screen(vd, scr, 0, defattrp);
471 1.1 macallan if (ret != 0) {
472 1.1 macallan free(scr, M_DEVBUF);
473 1.1 macallan return ret;
474 1.1 macallan }
475 1.1 macallan
476 1.1 macallan if (vd->active == NULL) {
477 1.1 macallan SCREEN_VISIBLE(scr);
478 1.1 macallan vd->active = scr;
479 1.1 macallan vd->currenttype = type;
480 1.1 macallan }
481 1.1 macallan
482 1.1 macallan *cookiep = scr;
483 1.1 macallan *curxp = scr->scr_ri.ri_ccol;
484 1.1 macallan *curyp = scr->scr_ri.ri_crow;
485 1.1 macallan return 0;
486 1.1 macallan }
487 1.1 macallan
488 1.1 macallan static void
489 1.1 macallan vcons_free_screen(void *v, void *cookie)
490 1.1 macallan {
491 1.1 macallan struct vcons_data *vd = v;
492 1.1 macallan struct vcons_screen *scr = cookie;
493 1.1 macallan
494 1.1 macallan vcons_lock(scr);
495 1.1 macallan /* there should be no rasops activity here */
496 1.1 macallan
497 1.1 macallan LIST_REMOVE(scr, next);
498 1.1 macallan
499 1.1 macallan if ((scr->scr_flags & VCONS_SCREEN_IS_STATIC) == 0) {
500 1.1 macallan free(scr->scr_attrs, M_DEVBUF);
501 1.1 macallan free(scr, M_DEVBUF);
502 1.1 macallan } else {
503 1.1 macallan /*
504 1.1 macallan * maybe we should just restore the old rasops_info methods
505 1.1 macallan * and free the character/attribute buffer here?
506 1.1 macallan */
507 1.1 macallan #ifdef VCONS_DEBUG
508 1.1 macallan panic("vcons_free_screen: console");
509 1.1 macallan #else
510 1.1 macallan printf("vcons_free_screen: console\n");
511 1.1 macallan #endif
512 1.1 macallan }
513 1.1 macallan
514 1.1 macallan if (vd->active == scr)
515 1.1 macallan vd->active = NULL;
516 1.1 macallan }
517 1.1 macallan
518 1.1 macallan static int
519 1.9 christos vcons_show_screen(void *v, void *cookie, int waitok,
520 1.1 macallan void (*cb)(void *, int, int), void *cb_arg)
521 1.1 macallan {
522 1.1 macallan struct vcons_data *vd = v;
523 1.1 macallan struct vcons_screen *scr;
524 1.1 macallan
525 1.1 macallan scr = cookie;
526 1.1 macallan if (scr == vd->active)
527 1.1 macallan return 0;
528 1.1 macallan
529 1.1 macallan vd->wanted = scr;
530 1.1 macallan vd->switch_cb = cb;
531 1.1 macallan vd->switch_cb_arg = cb_arg;
532 1.1 macallan if (cb) {
533 1.15 joerg callout_schedule(&vd->switch_callout, 0);
534 1.1 macallan return EAGAIN;
535 1.1 macallan }
536 1.1 macallan
537 1.1 macallan vcons_do_switch(vd);
538 1.1 macallan return 0;
539 1.1 macallan }
540 1.1 macallan
541 1.1 macallan /* wrappers for rasops_info methods */
542 1.1 macallan
543 1.1 macallan static void
544 1.1 macallan vcons_copycols_buffer(void *cookie, int row, int srccol, int dstcol, int ncols)
545 1.1 macallan {
546 1.1 macallan struct rasops_info *ri = cookie;
547 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
548 1.1 macallan int from = srccol + row * ri->ri_cols;
549 1.1 macallan int to = dstcol + row * ri->ri_cols;
550 1.1 macallan
551 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
552 1.14 macallan int offset;
553 1.14 macallan offset = scr->scr_offset_to_zero;
554 1.14 macallan
555 1.14 macallan memmove(&scr->scr_attrs[offset + to], &scr->scr_attrs[offset + from],
556 1.14 macallan ncols * sizeof(long));
557 1.14 macallan memmove(&scr->scr_chars[offset + to], &scr->scr_chars[offset + from],
558 1.14 macallan ncols * sizeof(uint16_t));
559 1.14 macallan #else
560 1.1 macallan memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
561 1.1 macallan ncols * sizeof(long));
562 1.1 macallan memmove(&scr->scr_chars[to], &scr->scr_chars[from],
563 1.1 macallan ncols * sizeof(uint16_t));
564 1.14 macallan #endif
565 1.1 macallan }
566 1.1 macallan
567 1.1 macallan static void
568 1.1 macallan vcons_copycols(void *cookie, int row, int srccol, int dstcol, int ncols)
569 1.1 macallan {
570 1.1 macallan struct rasops_info *ri = cookie;
571 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
572 1.1 macallan
573 1.1 macallan vcons_copycols_buffer(cookie, row, srccol, dstcol, ncols);
574 1.1 macallan
575 1.1 macallan vcons_lock(scr);
576 1.5 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
577 1.19 macallan #ifdef VCONS_DRAW_ASYNC
578 1.19 macallan struct vcons_data *vd = scr->scr_vd;
579 1.19 macallan if (vd->use_async) {
580 1.19 macallan vcons_copycols_async(cookie, row, srccol, dstcol, ncols);
581 1.19 macallan } else
582 1.19 macallan #endif
583 1.19 macallan scr->scr_vd->copycols(cookie, row, srccol, dstcol, ncols);
584 1.1 macallan }
585 1.1 macallan vcons_unlock(scr);
586 1.1 macallan }
587 1.1 macallan
588 1.1 macallan static void
589 1.17 macallan vcons_copycols_noread(void *cookie, int row, int srccol, int dstcol, int ncols)
590 1.17 macallan {
591 1.17 macallan struct rasops_info *ri = cookie;
592 1.17 macallan struct vcons_screen *scr = ri->ri_hw;
593 1.17 macallan
594 1.17 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
595 1.17 macallan int pos, c, offset;
596 1.17 macallan
597 1.17 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
598 1.17 macallan offset = scr->scr_current_offset;
599 1.17 macallan #else
600 1.17 macallan offset = 0;
601 1.17 macallan #endif
602 1.17 macallan pos = ri->ri_cols * row + dstcol + offset;
603 1.17 macallan for (c = dstcol; c < (dstcol + ncols); c++) {
604 1.17 macallan scr->scr_vd->putchar(cookie, row, c,
605 1.17 macallan scr->scr_chars[pos], scr->scr_attrs[pos]);
606 1.17 macallan pos++;
607 1.17 macallan }
608 1.17 macallan }
609 1.17 macallan }
610 1.17 macallan
611 1.17 macallan static void
612 1.1 macallan vcons_erasecols_buffer(void *cookie, int row, int startcol, int ncols, long fillattr)
613 1.1 macallan {
614 1.1 macallan struct rasops_info *ri = cookie;
615 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
616 1.1 macallan int start = startcol + row * ri->ri_cols;
617 1.1 macallan int end = start + ncols, i;
618 1.1 macallan
619 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
620 1.14 macallan int offset;
621 1.14 macallan offset = scr->scr_offset_to_zero;
622 1.14 macallan
623 1.14 macallan for (i = start; i < end; i++) {
624 1.14 macallan scr->scr_attrs[offset + i] = fillattr;
625 1.14 macallan scr->scr_chars[offset + i] = 0x20;
626 1.14 macallan }
627 1.14 macallan #else
628 1.1 macallan for (i = start; i < end; i++) {
629 1.1 macallan scr->scr_attrs[i] = fillattr;
630 1.1 macallan scr->scr_chars[i] = 0x20;
631 1.1 macallan }
632 1.14 macallan #endif
633 1.1 macallan }
634 1.1 macallan
635 1.1 macallan static void
636 1.1 macallan vcons_erasecols(void *cookie, int row, int startcol, int ncols, long fillattr)
637 1.1 macallan {
638 1.1 macallan struct rasops_info *ri = cookie;
639 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
640 1.1 macallan
641 1.1 macallan vcons_erasecols_buffer(cookie, row, startcol, ncols, fillattr);
642 1.1 macallan
643 1.1 macallan vcons_lock(scr);
644 1.5 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
645 1.19 macallan #ifdef VCONS_DRAW_ASYNC
646 1.19 macallan struct vcons_data *vd = scr->scr_vd;
647 1.19 macallan if (vd->use_async) {
648 1.19 macallan vcons_erasecols_async(cookie, row, startcol, ncols,
649 1.19 macallan fillattr);
650 1.19 macallan } else
651 1.19 macallan #endif
652 1.19 macallan scr->scr_vd->erasecols(cookie, row, startcol, ncols,
653 1.19 macallan fillattr);
654 1.1 macallan }
655 1.1 macallan vcons_unlock(scr);
656 1.1 macallan }
657 1.1 macallan
658 1.1 macallan static void
659 1.1 macallan vcons_copyrows_buffer(void *cookie, int srcrow, int dstrow, int nrows)
660 1.1 macallan {
661 1.1 macallan struct rasops_info *ri = cookie;
662 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
663 1.1 macallan int from, to, len;
664 1.1 macallan
665 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
666 1.14 macallan int offset;
667 1.14 macallan offset = scr->scr_offset_to_zero;
668 1.14 macallan
669 1.14 macallan /* do we need to scroll the back buffer? */
670 1.14 macallan if (dstrow == 0) {
671 1.14 macallan from = ri->ri_cols * srcrow;
672 1.14 macallan to = ri->ri_cols * dstrow;
673 1.14 macallan
674 1.14 macallan memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
675 1.14 macallan scr->scr_offset_to_zero * sizeof(long));
676 1.14 macallan memmove(&scr->scr_chars[to], &scr->scr_chars[from],
677 1.14 macallan scr->scr_offset_to_zero * sizeof(uint16_t));
678 1.14 macallan }
679 1.14 macallan from = ri->ri_cols * srcrow + offset;
680 1.14 macallan to = ri->ri_cols * dstrow + offset;
681 1.14 macallan len = ri->ri_cols * nrows;
682 1.14 macallan
683 1.14 macallan #else
684 1.1 macallan from = ri->ri_cols * srcrow;
685 1.1 macallan to = ri->ri_cols * dstrow;
686 1.1 macallan len = ri->ri_cols * nrows;
687 1.14 macallan #endif
688 1.1 macallan memmove(&scr->scr_attrs[to], &scr->scr_attrs[from],
689 1.1 macallan len * sizeof(long));
690 1.1 macallan memmove(&scr->scr_chars[to], &scr->scr_chars[from],
691 1.1 macallan len * sizeof(uint16_t));
692 1.1 macallan }
693 1.1 macallan
694 1.1 macallan static void
695 1.1 macallan vcons_copyrows(void *cookie, int srcrow, int dstrow, int nrows)
696 1.1 macallan {
697 1.1 macallan struct rasops_info *ri = cookie;
698 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
699 1.1 macallan
700 1.1 macallan vcons_copyrows_buffer(cookie, srcrow, dstrow, nrows);
701 1.1 macallan
702 1.1 macallan vcons_lock(scr);
703 1.5 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
704 1.19 macallan #ifdef VCONS_DRAW_ASYNC
705 1.19 macallan struct vcons_data *vd = scr->scr_vd;
706 1.19 macallan if (vd->use_async) {
707 1.19 macallan vcons_copyrows_async(cookie, srcrow, dstrow, nrows);
708 1.19 macallan } else
709 1.19 macallan #endif
710 1.19 macallan scr->scr_vd->copyrows(cookie, srcrow, dstrow, nrows);
711 1.1 macallan }
712 1.1 macallan vcons_unlock(scr);
713 1.1 macallan }
714 1.1 macallan
715 1.1 macallan static void
716 1.17 macallan vcons_copyrows_noread(void *cookie, int srcrow, int dstrow, int nrows)
717 1.17 macallan {
718 1.17 macallan struct rasops_info *ri = cookie;
719 1.17 macallan struct vcons_screen *scr = ri->ri_hw;
720 1.17 macallan
721 1.17 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
722 1.17 macallan int pos, l, c, offset;
723 1.17 macallan
724 1.17 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
725 1.17 macallan offset = scr->scr_current_offset;
726 1.17 macallan #else
727 1.17 macallan offset = 0;
728 1.17 macallan #endif
729 1.17 macallan pos = ri->ri_cols * dstrow + offset;
730 1.17 macallan for (l = dstrow; l < (dstrow + nrows); l++) {
731 1.17 macallan for (c = 0; c < ri->ri_cols; c++) {
732 1.17 macallan scr->scr_vd->putchar(cookie, l, c,
733 1.17 macallan scr->scr_chars[pos], scr->scr_attrs[pos]);
734 1.17 macallan pos++;
735 1.17 macallan }
736 1.17 macallan }
737 1.17 macallan }
738 1.17 macallan }
739 1.17 macallan
740 1.17 macallan static void
741 1.1 macallan vcons_eraserows_buffer(void *cookie, int row, int nrows, long fillattr)
742 1.1 macallan {
743 1.1 macallan struct rasops_info *ri = cookie;
744 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
745 1.1 macallan int start, end, i;
746 1.1 macallan
747 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
748 1.14 macallan int offset;
749 1.14 macallan offset = scr->scr_offset_to_zero;
750 1.14 macallan
751 1.14 macallan start = ri->ri_cols * row + offset;
752 1.14 macallan end = ri->ri_cols * (row + nrows) + offset;
753 1.14 macallan #else
754 1.1 macallan start = ri->ri_cols * row;
755 1.1 macallan end = ri->ri_cols * (row + nrows);
756 1.14 macallan #endif
757 1.1 macallan
758 1.1 macallan for (i = start; i < end; i++) {
759 1.1 macallan scr->scr_attrs[i] = fillattr;
760 1.1 macallan scr->scr_chars[i] = 0x20;
761 1.1 macallan }
762 1.1 macallan }
763 1.1 macallan
764 1.1 macallan static void
765 1.1 macallan vcons_eraserows(void *cookie, int row, int nrows, long fillattr)
766 1.1 macallan {
767 1.1 macallan struct rasops_info *ri = cookie;
768 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
769 1.1 macallan
770 1.1 macallan vcons_eraserows_buffer(cookie, row, nrows, fillattr);
771 1.1 macallan
772 1.1 macallan vcons_lock(scr);
773 1.5 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
774 1.19 macallan #ifdef VCONS_DRAW_ASYNC
775 1.19 macallan struct vcons_data *vd = scr->scr_vd;
776 1.19 macallan if (vd->use_async) {
777 1.19 macallan vcons_eraserows_async(cookie, row, nrows, fillattr);
778 1.19 macallan } else
779 1.19 macallan #endif
780 1.19 macallan scr->scr_vd->eraserows(cookie, row, nrows, fillattr);
781 1.1 macallan }
782 1.1 macallan vcons_unlock(scr);
783 1.1 macallan }
784 1.1 macallan
785 1.1 macallan static void
786 1.1 macallan vcons_putchar_buffer(void *cookie, int row, int col, u_int c, long attr)
787 1.1 macallan {
788 1.1 macallan struct rasops_info *ri = cookie;
789 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
790 1.1 macallan int pos;
791 1.1 macallan
792 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
793 1.14 macallan int offset;
794 1.14 macallan offset = scr->scr_offset_to_zero;
795 1.14 macallan
796 1.14 macallan if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
797 1.14 macallan (col < ri->ri_cols)) {
798 1.14 macallan pos = col + row * ri->ri_cols;
799 1.14 macallan scr->scr_attrs[pos + offset] = attr;
800 1.14 macallan scr->scr_chars[pos + offset] = c;
801 1.14 macallan }
802 1.14 macallan #else
803 1.1 macallan if ((row >= 0) && (row < ri->ri_rows) && (col >= 0) &&
804 1.1 macallan (col < ri->ri_cols)) {
805 1.1 macallan pos = col + row * ri->ri_cols;
806 1.1 macallan scr->scr_attrs[pos] = attr;
807 1.1 macallan scr->scr_chars[pos] = c;
808 1.1 macallan }
809 1.14 macallan #endif
810 1.1 macallan }
811 1.1 macallan
812 1.1 macallan static void
813 1.1 macallan vcons_putchar(void *cookie, int row, int col, u_int c, long attr)
814 1.1 macallan {
815 1.1 macallan struct rasops_info *ri = cookie;
816 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
817 1.1 macallan
818 1.1 macallan vcons_putchar_buffer(cookie, row, col, c, attr);
819 1.1 macallan
820 1.1 macallan vcons_lock(scr);
821 1.5 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
822 1.19 macallan #ifdef VCONS_DRAW_ASYNC
823 1.19 macallan struct vcons_data *vd = scr->scr_vd;
824 1.19 macallan if (vd->use_async) {
825 1.19 macallan vcons_putchar_async(cookie, row, col, c, attr);
826 1.19 macallan } else
827 1.19 macallan #endif
828 1.19 macallan scr->scr_vd->putchar(cookie, row, col, c, attr);
829 1.1 macallan }
830 1.1 macallan vcons_unlock(scr);
831 1.1 macallan }
832 1.1 macallan
833 1.1 macallan static void
834 1.1 macallan vcons_cursor(void *cookie, int on, int row, int col)
835 1.1 macallan {
836 1.1 macallan struct rasops_info *ri = cookie;
837 1.1 macallan struct vcons_screen *scr = ri->ri_hw;
838 1.1 macallan
839 1.1 macallan vcons_lock(scr);
840 1.5 macallan if (SCREEN_IS_VISIBLE(scr) && SCREEN_CAN_DRAW(scr)) {
841 1.19 macallan #ifdef VCONS_DRAW_ASYNC
842 1.19 macallan struct vcons_data *vd = scr->scr_vd;
843 1.19 macallan if (vd->use_async) {
844 1.19 macallan vcons_cursor_async(cookie, on, row, col);
845 1.19 macallan } else
846 1.19 macallan #endif
847 1.19 macallan scr->scr_vd->cursor(cookie, on, row, col);
848 1.1 macallan } else {
849 1.1 macallan scr->scr_ri.ri_crow = row;
850 1.1 macallan scr->scr_ri.ri_ccol = col;
851 1.1 macallan }
852 1.1 macallan vcons_unlock(scr);
853 1.1 macallan }
854 1.1 macallan
855 1.1 macallan /* methods to read/write characters via ioctl() */
856 1.1 macallan
857 1.1 macallan static int
858 1.6 jmmv vcons_putwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
859 1.1 macallan {
860 1.1 macallan long attr;
861 1.6 jmmv struct rasops_info *ri;
862 1.6 jmmv
863 1.6 jmmv KASSERT(scr != NULL && wsc != NULL);
864 1.6 jmmv
865 1.13 rumble ri = &scr->scr_ri;
866 1.13 rumble
867 1.12 mjf if (__predict_false((unsigned int)wsc->col > ri->ri_cols ||
868 1.12 mjf (unsigned int)wsc->row > ri->ri_rows))
869 1.12 mjf return (EINVAL);
870 1.1 macallan
871 1.14 macallan if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
872 1.14 macallan (wsc->col < ri->ri_cols)) {
873 1.14 macallan
874 1.14 macallan ri->ri_ops.allocattr(ri, wsc->foreground, wsc->background,
875 1.14 macallan wsc->flags, &attr);
876 1.14 macallan vcons_putchar(ri, wsc->row, wsc->col, wsc->letter, attr);
877 1.14 macallan #ifdef VCONS_DEBUG
878 1.14 macallan printf("vcons_putwschar(%d, %d, %x, %lx\n", wsc->row, wsc->col,
879 1.14 macallan wsc->letter, attr);
880 1.14 macallan #endif
881 1.14 macallan return 0;
882 1.14 macallan } else
883 1.14 macallan return EINVAL;
884 1.1 macallan }
885 1.1 macallan
886 1.1 macallan static int
887 1.6 jmmv vcons_getwschar(struct vcons_screen *scr, struct wsdisplay_char *wsc)
888 1.1 macallan {
889 1.6 jmmv int offset;
890 1.1 macallan long attr;
891 1.6 jmmv struct rasops_info *ri;
892 1.6 jmmv
893 1.6 jmmv KASSERT(scr != NULL && wsc != NULL);
894 1.6 jmmv
895 1.6 jmmv ri = &scr->scr_ri;
896 1.1 macallan
897 1.14 macallan if ((wsc->row >= 0) && (wsc->row < ri->ri_rows) && (wsc->col >= 0) &&
898 1.14 macallan (wsc->col < ri->ri_cols)) {
899 1.14 macallan
900 1.14 macallan offset = ri->ri_cols * wsc->row + wsc->col;
901 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
902 1.14 macallan offset += scr->scr_offset_to_zero;
903 1.14 macallan #endif
904 1.14 macallan wsc->letter = scr->scr_chars[offset];
905 1.14 macallan attr = scr->scr_attrs[offset];
906 1.14 macallan
907 1.14 macallan /*
908 1.14 macallan * this is ugly. We need to break up an attribute into colours and
909 1.14 macallan * flags but there's no rasops method to do that so we must rely on
910 1.14 macallan * the 'canonical' encoding.
911 1.14 macallan */
912 1.14 macallan #ifdef VCONS_DEBUG
913 1.14 macallan printf("vcons_getwschar: %d, %d, %x, %lx\n", wsc->row,
914 1.14 macallan wsc->col, wsc->letter, attr);
915 1.14 macallan #endif
916 1.14 macallan wsc->foreground = (attr >> 24) & 0xff;
917 1.14 macallan wsc->background = (attr >> 16) & 0xff;
918 1.14 macallan wsc->flags = attr & 0xff;
919 1.14 macallan return 0;
920 1.14 macallan } else
921 1.14 macallan return EINVAL;
922 1.14 macallan }
923 1.14 macallan
924 1.14 macallan #ifdef WSDISPLAY_SCROLLSUPPORT
925 1.14 macallan
926 1.14 macallan static void
927 1.14 macallan vcons_scroll(void *cookie, void *vs, int where)
928 1.14 macallan {
929 1.14 macallan struct vcons_screen *scr = vs;
930 1.14 macallan
931 1.14 macallan if (where == 0) {
932 1.14 macallan scr->scr_line_wanted = 0;
933 1.14 macallan } else {
934 1.14 macallan scr->scr_line_wanted = scr->scr_line_wanted - where;
935 1.14 macallan if (scr->scr_line_wanted < 0)
936 1.14 macallan scr->scr_line_wanted = 0;
937 1.14 macallan if (scr->scr_line_wanted > scr->scr_lines_in_buffer)
938 1.14 macallan scr->scr_line_wanted = scr->scr_lines_in_buffer;
939 1.14 macallan }
940 1.14 macallan
941 1.14 macallan if (scr->scr_line_wanted != scr->scr_current_line) {
942 1.14 macallan
943 1.14 macallan vcons_do_scroll(scr);
944 1.14 macallan }
945 1.14 macallan }
946 1.14 macallan
947 1.14 macallan static void
948 1.14 macallan vcons_do_scroll(struct vcons_screen *scr)
949 1.14 macallan {
950 1.14 macallan int dist, from, to, num;
951 1.14 macallan int r_offset, r_start;
952 1.14 macallan int i, j;
953 1.14 macallan
954 1.14 macallan if (scr->scr_line_wanted == scr->scr_current_line)
955 1.14 macallan return;
956 1.14 macallan dist = scr->scr_line_wanted - scr->scr_current_line;
957 1.14 macallan scr->scr_current_line = scr->scr_line_wanted;
958 1.14 macallan scr->scr_current_offset = scr->scr_ri.ri_cols *
959 1.14 macallan (scr->scr_lines_in_buffer - scr->scr_current_line);
960 1.14 macallan if (abs(dist) >= scr->scr_ri.ri_rows) {
961 1.14 macallan vcons_redraw_screen(scr);
962 1.14 macallan return;
963 1.14 macallan }
964 1.14 macallan /* scroll and redraw only what we really have to */
965 1.14 macallan if (dist > 0) {
966 1.14 macallan /* we scroll down */
967 1.14 macallan from = 0;
968 1.14 macallan to = dist;
969 1.14 macallan num = scr->scr_ri.ri_rows - dist;
970 1.14 macallan /* now the redraw parameters */
971 1.14 macallan r_offset = scr->scr_current_offset;
972 1.14 macallan r_start = 0;
973 1.14 macallan } else {
974 1.14 macallan /* scrolling up */
975 1.14 macallan to = 0;
976 1.14 macallan from = -dist;
977 1.14 macallan num = scr->scr_ri.ri_rows + dist;
978 1.14 macallan r_offset = scr->scr_current_offset + num * scr->scr_ri.ri_cols;
979 1.14 macallan r_start = num;
980 1.14 macallan }
981 1.14 macallan scr->scr_vd->copyrows(scr, from, to, num);
982 1.14 macallan for (i = 0; i < abs(dist); i++) {
983 1.14 macallan for (j = 0; j < scr->scr_ri.ri_cols; j++) {
984 1.14 macallan scr->scr_vd->putchar(scr, i + r_start, j,
985 1.14 macallan scr->scr_chars[r_offset],
986 1.14 macallan scr->scr_attrs[r_offset]);
987 1.14 macallan r_offset++;
988 1.14 macallan }
989 1.14 macallan }
990 1.14 macallan
991 1.14 macallan if (scr->scr_line_wanted == 0) {
992 1.14 macallan /* this was a reset - need to draw the cursor */
993 1.14 macallan scr->scr_ri.ri_flg &= ~RI_CURSOR;
994 1.14 macallan scr->scr_vd->cursor(scr, 1, scr->scr_ri.ri_crow,
995 1.14 macallan scr->scr_ri.ri_ccol);
996 1.14 macallan }
997 1.14 macallan }
998 1.14 macallan
999 1.14 macallan #endif /* WSDISPLAY_SCROLLSUPPORT */
1000 1.14 macallan
1001 1.14 macallan /* async drawing using a kernel thread */
1002 1.14 macallan
1003 1.19 macallan #ifdef VCONS_DRAW_ASYNC
1004 1.19 macallan
1005 1.19 macallan static inline uint32_t
1006 1.19 macallan vcons_words_in_buffer(struct vcons_data *vd)
1007 1.19 macallan {
1008 1.19 macallan int len = vd->rb_write - vd->rb_read;
1009 1.19 macallan
1010 1.19 macallan if (len < 0) len += VCONS_RING_BUFFER_LENGTH;
1011 1.19 macallan if (len < 0) vd->use_async = 0;
1012 1.19 macallan if (len >= VCONS_RING_BUFFER_LENGTH) vd->use_async = 0;
1013 1.19 macallan return (uint32_t)len;
1014 1.19 macallan }
1015 1.19 macallan
1016 1.19 macallan static inline int
1017 1.19 macallan vcons_wait_buffer(struct vcons_data *vd, uint32_t words)
1018 1.19 macallan {
1019 1.19 macallan int bail = 0;
1020 1.19 macallan
1021 1.19 macallan mutex_enter(&vd->go_buffer_il);
1022 1.19 macallan while (((VCONS_RING_BUFFER_LENGTH - vcons_words_in_buffer(vd)) < words)
1023 1.19 macallan && (bail < 3)) {
1024 1.19 macallan if (cv_timedwait(&vd->go_buffer, &vd->go_buffer_il, hz)
1025 1.19 macallan == EWOULDBLOCK)
1026 1.19 macallan bail++;
1027 1.19 macallan }
1028 1.19 macallan if (bail >= 3) {
1029 1.19 macallan /*
1030 1.19 macallan * waited too long, something is wrong so fall back to sync
1031 1.19 macallan * we should probably kill the kthread here and try to empty
1032 1.19 macallan * the command buffer as well
1033 1.19 macallan */
1034 1.19 macallan vd->use_async = 0;
1035 1.19 macallan }
1036 1.19 macallan return 0;
1037 1.19 macallan }
1038 1.19 macallan
1039 1.19 macallan #define VRB_NEXT(idx) ((idx + 1) >= VCONS_RING_BUFFER_LENGTH) ? 0 : idx + 1
1040 1.19 macallan
1041 1.19 macallan static void
1042 1.19 macallan vcons_copycols_async(void *cookie, int row, int srccol, int dstcol, int ncols)
1043 1.19 macallan {
1044 1.19 macallan struct rasops_info *ri = cookie;
1045 1.19 macallan struct vcons_screen *scr = ri->ri_hw;
1046 1.19 macallan struct vcons_data *vd = scr->scr_vd;
1047 1.19 macallan int idx;
1048 1.19 macallan
1049 1.19 macallan vcons_wait_buffer(vd, 5);
1050 1.19 macallan mutex_enter(&vd->drawing_mutex);
1051 1.19 macallan mutex_exit(&vd->go_buffer_il);
1052 1.19 macallan idx = vd->rb_write;
1053 1.19 macallan vd->rb_buffer[idx] = VCMD_COPYCOLS;
1054 1.19 macallan idx = VRB_NEXT(idx);
1055 1.19 macallan vd->rb_buffer[idx] = row;
1056 1.19 macallan idx = VRB_NEXT(idx);
1057 1.19 macallan vd->rb_buffer[idx] = srccol;
1058 1.19 macallan idx = VRB_NEXT(idx);
1059 1.19 macallan vd->rb_buffer[idx] = dstcol;
1060 1.19 macallan idx = VRB_NEXT(idx);
1061 1.19 macallan vd->rb_buffer[idx] = ncols;
1062 1.19 macallan idx = VRB_NEXT(idx);
1063 1.19 macallan membar_producer();
1064 1.19 macallan vd->rb_write = idx;
1065 1.19 macallan membar_enter();
1066 1.19 macallan mutex_exit(&vd->drawing_mutex);
1067 1.19 macallan cv_signal(&vd->go_draw);
1068 1.19 macallan }
1069 1.19 macallan
1070 1.19 macallan static void
1071 1.19 macallan vcons_erasecols_async(void *cookie, int row, int startcol, int ncols,
1072 1.19 macallan long fillattr)
1073 1.19 macallan {
1074 1.19 macallan struct rasops_info *ri = cookie;
1075 1.19 macallan struct vcons_screen *scr = ri->ri_hw;
1076 1.19 macallan struct vcons_data *vd = scr->scr_vd;
1077 1.19 macallan int idx;
1078 1.19 macallan
1079 1.19 macallan vcons_wait_buffer(vd, 5);
1080 1.19 macallan mutex_enter(&vd->drawing_mutex);
1081 1.19 macallan mutex_exit(&vd->go_buffer_il);
1082 1.19 macallan idx = vd->rb_write;
1083 1.19 macallan vd->rb_buffer[idx] = VCMD_ERASECOLS;
1084 1.19 macallan idx = VRB_NEXT(idx);
1085 1.19 macallan vd->rb_buffer[idx] = row;
1086 1.19 macallan idx = VRB_NEXT(idx);
1087 1.19 macallan vd->rb_buffer[idx] = startcol;
1088 1.19 macallan idx = VRB_NEXT(idx);
1089 1.19 macallan vd->rb_buffer[idx] = ncols;
1090 1.19 macallan idx = VRB_NEXT(idx);
1091 1.19 macallan /*
1092 1.19 macallan * XXX all drivers I've seen use 32bit attributes although fillattr is
1093 1.19 macallan * a 64bit value on LP64
1094 1.19 macallan */
1095 1.19 macallan vd->rb_buffer[idx] = (uint32_t)fillattr;
1096 1.19 macallan idx = VRB_NEXT(idx);
1097 1.19 macallan membar_producer();
1098 1.19 macallan vd->rb_write = idx;
1099 1.19 macallan membar_enter();
1100 1.19 macallan mutex_exit(&vd->drawing_mutex);
1101 1.19 macallan cv_signal(&vd->go_draw);
1102 1.19 macallan }
1103 1.19 macallan
1104 1.19 macallan static void
1105 1.19 macallan vcons_copyrows_async(void *cookie, int srcrow, int dstrow, int nrows)
1106 1.19 macallan {
1107 1.19 macallan struct rasops_info *ri = cookie;
1108 1.19 macallan struct vcons_screen *scr = ri->ri_hw;
1109 1.19 macallan struct vcons_data *vd = scr->scr_vd;
1110 1.19 macallan int idx;
1111 1.19 macallan
1112 1.19 macallan vcons_wait_buffer(vd, 4);
1113 1.19 macallan mutex_enter(&vd->drawing_mutex);
1114 1.19 macallan mutex_exit(&vd->go_buffer_il);
1115 1.19 macallan idx = vd->rb_write;
1116 1.19 macallan vd->rb_buffer[idx] = VCMD_COPYROWS;
1117 1.19 macallan idx = VRB_NEXT(idx);
1118 1.19 macallan vd->rb_buffer[idx] = srcrow;
1119 1.19 macallan idx = VRB_NEXT(idx);
1120 1.19 macallan vd->rb_buffer[idx] = dstrow;
1121 1.19 macallan idx = VRB_NEXT(idx);
1122 1.19 macallan vd->rb_buffer[idx] = nrows;
1123 1.19 macallan idx = VRB_NEXT(idx);
1124 1.19 macallan membar_producer();
1125 1.19 macallan vd->rb_write = idx;
1126 1.19 macallan membar_enter();
1127 1.19 macallan mutex_exit(&vd->drawing_mutex);
1128 1.19 macallan cv_signal(&vd->go_draw);
1129 1.19 macallan }
1130 1.19 macallan
1131 1.19 macallan static void
1132 1.19 macallan vcons_eraserows_async(void *cookie, int row, int nrows, long fillattr)
1133 1.19 macallan {
1134 1.19 macallan struct rasops_info *ri = cookie;
1135 1.19 macallan struct vcons_screen *scr = ri->ri_hw;
1136 1.19 macallan struct vcons_data *vd = scr->scr_vd;
1137 1.19 macallan int idx;
1138 1.19 macallan
1139 1.19 macallan vcons_wait_buffer(vd, 4);
1140 1.19 macallan mutex_enter(&vd->drawing_mutex);
1141 1.19 macallan mutex_exit(&vd->go_buffer_il);
1142 1.19 macallan idx = vd->rb_write;
1143 1.19 macallan vd->rb_buffer[idx] = VCMD_ERASEROWS;
1144 1.19 macallan idx = VRB_NEXT(idx);
1145 1.19 macallan vd->rb_buffer[idx] = row;
1146 1.19 macallan idx = VRB_NEXT(idx);
1147 1.19 macallan vd->rb_buffer[idx] = nrows;
1148 1.19 macallan idx = VRB_NEXT(idx);
1149 1.19 macallan vd->rb_buffer[idx] = (uint32_t)fillattr;
1150 1.19 macallan idx = VRB_NEXT(idx);
1151 1.19 macallan membar_producer();
1152 1.19 macallan vd->rb_write = idx;
1153 1.19 macallan membar_enter();
1154 1.19 macallan mutex_exit(&vd->drawing_mutex);
1155 1.19 macallan cv_signal(&vd->go_draw);
1156 1.19 macallan }
1157 1.19 macallan
1158 1.19 macallan static void
1159 1.19 macallan vcons_putchar_async(void *cookie, int row, int col, u_int c, long attr)
1160 1.19 macallan {
1161 1.19 macallan struct rasops_info *ri = cookie;
1162 1.19 macallan struct vcons_screen *scr = ri->ri_hw;
1163 1.19 macallan struct vcons_data *vd = scr->scr_vd;
1164 1.19 macallan int idx;
1165 1.19 macallan
1166 1.19 macallan #ifdef VCONS_ASYNC_DEBUG
1167 1.19 macallan /* mess with the background attribute so we can see if we draw async */
1168 1.19 macallan attr &= 0xff00ffff;
1169 1.19 macallan attr |= (WSCOL_LIGHT_BROWN << 16);
1170 1.19 macallan #endif
1171 1.19 macallan
1172 1.19 macallan vcons_wait_buffer(vd, 5);
1173 1.19 macallan mutex_enter(&vd->drawing_mutex);
1174 1.19 macallan mutex_exit(&vd->go_buffer_il);
1175 1.19 macallan idx = vd->rb_write;
1176 1.19 macallan vd->rb_buffer[idx] = VCMD_PUTCHAR;
1177 1.19 macallan idx = VRB_NEXT(idx);
1178 1.19 macallan vd->rb_buffer[idx] = row;
1179 1.19 macallan idx = VRB_NEXT(idx);
1180 1.19 macallan vd->rb_buffer[idx] = col;
1181 1.19 macallan idx = VRB_NEXT(idx);
1182 1.19 macallan vd->rb_buffer[idx] = c;
1183 1.19 macallan idx = VRB_NEXT(idx);
1184 1.19 macallan vd->rb_buffer[idx] = (uint32_t)attr;
1185 1.19 macallan idx = VRB_NEXT(idx);
1186 1.19 macallan membar_producer();
1187 1.19 macallan vd->rb_write = idx;
1188 1.19 macallan membar_enter();
1189 1.19 macallan mutex_exit(&vd->drawing_mutex);
1190 1.19 macallan cv_signal(&vd->go_draw);
1191 1.19 macallan }
1192 1.19 macallan
1193 1.19 macallan static void
1194 1.19 macallan vcons_cursor_async(void *cookie, int on, int row, int col)
1195 1.19 macallan {
1196 1.19 macallan struct rasops_info *ri = cookie;
1197 1.19 macallan struct vcons_screen *scr = ri->ri_hw;
1198 1.19 macallan struct vcons_data *vd = scr->scr_vd;
1199 1.19 macallan int idx;
1200 1.19 macallan
1201 1.19 macallan vcons_wait_buffer(vd, 4);
1202 1.19 macallan mutex_enter(&vd->drawing_mutex);
1203 1.19 macallan mutex_exit(&vd->go_buffer_il);
1204 1.19 macallan idx = vd->rb_write;
1205 1.19 macallan vd->rb_buffer[idx] = VCMD_CURSOR;
1206 1.19 macallan idx = VRB_NEXT(idx);
1207 1.19 macallan vd->rb_buffer[idx] = on;
1208 1.19 macallan idx = VRB_NEXT(idx);
1209 1.19 macallan vd->rb_buffer[idx] = row;
1210 1.19 macallan idx = VRB_NEXT(idx);
1211 1.19 macallan vd->rb_buffer[idx] = col;
1212 1.19 macallan idx = VRB_NEXT(idx);
1213 1.19 macallan membar_producer();
1214 1.19 macallan vd->rb_write = idx;
1215 1.19 macallan membar_enter();
1216 1.19 macallan mutex_exit(&vd->drawing_mutex);
1217 1.19 macallan cv_signal(&vd->go_draw);
1218 1.19 macallan }
1219 1.19 macallan
1220 1.19 macallan static int
1221 1.19 macallan vcons_copy_params(struct vcons_data *vd, int len, uint32_t *buf)
1222 1.19 macallan {
1223 1.19 macallan int idx = vd->rb_read, i;
1224 1.19 macallan
1225 1.19 macallan for (i = 0; i < len; i++) {
1226 1.19 macallan buf[i] = vd->rb_buffer[idx];
1227 1.19 macallan idx = VRB_NEXT(idx);
1228 1.19 macallan }
1229 1.19 macallan return idx;
1230 1.19 macallan }
1231 1.19 macallan
1232 1.19 macallan
1233 1.14 macallan static void
1234 1.19 macallan vcons_process_command(struct vcons_data *vd)
1235 1.14 macallan {
1236 1.19 macallan /* we take a command out of the buffer, run it and return */
1237 1.19 macallan void *cookie;
1238 1.19 macallan int idx = vd->rb_read;
1239 1.19 macallan uint32_t cmd = vd->rb_buffer[idx];
1240 1.19 macallan uint32_t params[10];
1241 1.14 macallan
1242 1.19 macallan KASSERT(vd->active != NULL);
1243 1.19 macallan cookie = &vd->active->scr_ri;
1244 1.14 macallan
1245 1.19 macallan switch (cmd) {
1246 1.19 macallan case VCMD_COPYCOLS:
1247 1.19 macallan idx = vcons_copy_params(vd, 5, params);
1248 1.19 macallan vd->rb_read = idx;
1249 1.19 macallan membar_producer();
1250 1.19 macallan vd->copycols(cookie, params[1], params[2], params[3], params[4]);
1251 1.19 macallan break;
1252 1.19 macallan case VCMD_ERASECOLS:
1253 1.19 macallan idx = vcons_copy_params(vd, 5, params);
1254 1.19 macallan vd->rb_read = idx;
1255 1.19 macallan membar_producer();
1256 1.19 macallan vd->erasecols(cookie, params[1], params[2], params[3], params[4]);
1257 1.19 macallan break;
1258 1.19 macallan case VCMD_COPYROWS:
1259 1.19 macallan idx = vcons_copy_params(vd, 4, params);
1260 1.19 macallan vd->rb_read = idx;
1261 1.19 macallan membar_producer();
1262 1.19 macallan vd->copyrows(cookie, params[1], params[2], params[3]);
1263 1.19 macallan break;
1264 1.19 macallan case VCMD_ERASEROWS:
1265 1.19 macallan idx = vcons_copy_params(vd, 4, params);
1266 1.19 macallan vd->rb_read = idx;
1267 1.19 macallan membar_producer();
1268 1.19 macallan vd->eraserows(cookie, params[1], params[2], params[3]);
1269 1.19 macallan break;
1270 1.19 macallan case VCMD_PUTCHAR:
1271 1.19 macallan idx = vcons_copy_params(vd, 5, params);
1272 1.19 macallan vd->rb_read = idx;
1273 1.19 macallan membar_producer();
1274 1.19 macallan vd->putchar(cookie, params[1], params[2], params[3], params[4]);
1275 1.19 macallan break;
1276 1.19 macallan case VCMD_CURSOR:
1277 1.19 macallan idx = vcons_copy_params(vd, 4, params);
1278 1.19 macallan vd->rb_read = idx;
1279 1.19 macallan membar_producer();
1280 1.19 macallan vd->cursor(cookie, params[1], params[2], params[3]);
1281 1.19 macallan break;
1282 1.19 macallan default:
1283 1.14 macallan /*
1284 1.19 macallan * invalid command, something is wrong so we fall back
1285 1.19 macallan * to synchronous operations
1286 1.14 macallan */
1287 1.19 macallan vd->use_async = 0;
1288 1.19 macallan vd->rb_read = 0;
1289 1.19 macallan vd->rb_write = 0;
1290 1.19 macallan }
1291 1.19 macallan }
1292 1.19 macallan
1293 1.19 macallan static void vcons_cursor(void *, int, int, int);
1294 1.14 macallan
1295 1.19 macallan static void
1296 1.19 macallan vcons_kthread(void *cookie)
1297 1.19 macallan {
1298 1.19 macallan struct vcons_data *vd = cookie;
1299 1.14 macallan
1300 1.19 macallan /* initialize the synchronization goo */
1301 1.19 macallan cv_init(&vd->go_draw, "go_draw");
1302 1.19 macallan cv_init(&vd->go_buffer, "go_buffer");
1303 1.19 macallan mutex_init(&vd->drawing_mutex, MUTEX_DEFAULT, IPL_NONE);
1304 1.19 macallan mutex_init(&vd->go_draw_il, MUTEX_DEFAULT, IPL_NONE);
1305 1.19 macallan mutex_init(&vd->go_buffer_il, MUTEX_DEFAULT, IPL_NONE);
1306 1.19 macallan vd->rb_read = 1000;
1307 1.19 macallan vd->rb_write = 2;
1308 1.19 macallan printf("%d\n", vcons_words_in_buffer(vd));
1309 1.19 macallan vd->rb_read = 0;
1310 1.19 macallan vd->rb_write = 0;
1311 1.19 macallan printf("%d\n", vcons_words_in_buffer(vd));
1312 1.19 macallan printf("%d %d\n", VRB_NEXT(1), VRB_NEXT(1023));
1313 1.19 macallan /* now we're good to go */
1314 1.19 macallan vd->use_async = 1;
1315 1.14 macallan
1316 1.19 macallan while (1) {
1317 1.14 macallan
1318 1.19 macallan while (vcons_words_in_buffer(vd) > 0) {
1319 1.19 macallan vcons_process_command(vd);
1320 1.19 macallan cv_signal(&vd->go_buffer);
1321 1.19 macallan }
1322 1.19 macallan /*
1323 1.19 macallan * We don't really need the interlock here since there is no
1324 1.19 macallan * need for serializing access to the buffer - we're the only
1325 1.19 macallan * consumer. All we want is to sleep until someone gives us
1326 1.19 macallan * something to so.
1327 1.19 macallan */
1328 1.19 macallan mutex_enter(&vd->go_draw_il);
1329 1.19 macallan cv_timedwait(&vd->go_draw, &vd->go_draw_il, hz);
1330 1.19 macallan mutex_exit(&vd->go_draw_il);
1331 1.14 macallan }
1332 1.1 macallan }
1333 1.19 macallan #endif /* VCONS_DRAW_ASYNC */
1334