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