vncfb.c revision 1.8 1 /* $NetBSD: vncfb.c,v 1.8 2011/12/30 14:22:41 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
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 Jared D. McNeill.
18 * 4. Neither the name of The NetBSD Foundation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include "opt_wsemul.h"
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: vncfb.c,v 1.8 2011/12/30 14:22:41 jmcneill Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/device.h>
44 #include <sys/kmem.h>
45
46 #include <dev/wscons/wsconsio.h>
47
48 #include <dev/wscons/wsdisplayvar.h>
49 #include <dev/wsfont/wsfont.h>
50 #include <dev/rasops/rasops.h>
51 #include <dev/wscons/wsdisplay_vconsvar.h>
52
53 #include <dev/wscons/wskbdvar.h>
54 #include <dev/wscons/wsksymdef.h>
55 #include <dev/wscons/wsksymvar.h>
56
57 #include <machine/mainbus.h>
58 #include <machine/thunk.h>
59
60 struct vncfb_fbops {
61 void (*copycols)(void *, int, int, int, int);
62 void (*erasecols)(void *, int, int, int, long);
63 void (*copyrows)(void *, int, int, int);
64 void (*eraserows)(void *, int, int, long);
65 void (*putchar)(void *, int, int, u_int, long);
66 void (*cursor)(void *, int, int, int);
67 };
68
69 struct vncfb_softc {
70 device_t sc_dev;
71 device_t sc_wskbddev;
72 thunk_rfb_t sc_rfb;
73 unsigned int sc_width;
74 unsigned int sc_height;
75 unsigned int sc_depth;
76 int sc_mode;
77 uint8_t * sc_framebuf;
78 struct vcons_data sc_vd;
79 struct vncfb_fbops sc_ops;
80
81 int sc_kbd_enable;
82
83 void *sc_ih;
84 void *sc_sih;
85 };
86
87 static int vncfb_match(device_t, cfdata_t, void *);
88 static void vncfb_attach(device_t, device_t, void *);
89
90 CFATTACH_DECL_NEW(vncfb, sizeof(struct vncfb_softc),
91 vncfb_match, vncfb_attach, NULL, NULL);
92
93 static void vncfb_putchar(void *, int, int, u_int, long);
94 static void vncfb_copycols(void *, int, int, int, int);
95 static void vncfb_erasecols(void *, int, int, int, long);
96 static void vncfb_copyrows(void *, int, int, int);
97 static void vncfb_eraserows(void *, int, int, long);
98 static void vncfb_cursor(void *, int, int, int);
99
100 static int vncfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
101 static paddr_t vncfb_mmap(void *, void *, off_t, int);
102
103 static void vncfb_init_screen(void *, struct vcons_screen *, int, long *);
104
105 static void vncfb_update(struct vncfb_softc *, int, int, int, int);
106 static void vncfb_copyrect(struct vncfb_softc *, int, int, int, int, int, int);
107 static void vncfb_fillrect(struct vncfb_softc *, int, int, int, int, uint32_t);
108 static int vncfb_intr(void *);
109 static void vncfb_softintr(void *);
110
111 static int vncfb_kbd_enable(void *, int);
112 static void vncfb_kbd_set_leds(void *, int);
113 static int vncfb_kbd_ioctl(void *, u_long, void *, int, lwp_t *);
114
115 static void vncfb_kbd_cngetc(void *, u_int *, int *);
116 static void vncfb_kbd_cnpollc(void *, int);
117 static void vncfb_kbd_bell(void *, u_int, u_int, u_int);
118
119 static struct vcons_screen vncfb_console_screen;
120
121 static struct wsscreen_descr vncfb_defaultscreen = {
122 .name = "default",
123 .fontwidth = 8,
124 .fontheight = 16,
125 .capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
126 };
127
128 static const struct wsscreen_descr *vncfb_screens[] = {
129 &vncfb_defaultscreen,
130 };
131
132 static struct wsscreen_list vncfb_screenlist = {
133 .screens = vncfb_screens,
134 .nscreens = __arraycount(vncfb_screens),
135 };
136
137 static struct wsdisplay_accessops vncfb_accessops = {
138 .ioctl = vncfb_ioctl,
139 .mmap = vncfb_mmap,
140 };
141
142 extern const struct wscons_keydesc vnckbd_keydesctab[];
143
144 static const struct wskbd_mapdata vncfb_keymapdata = {
145 vnckbd_keydesctab,
146 KB_US,
147 };
148
149 static struct wskbd_accessops vncfb_kbd_accessops = {
150 vncfb_kbd_enable,
151 vncfb_kbd_set_leds,
152 vncfb_kbd_ioctl,
153 };
154
155 static const struct wskbd_consops vncfb_kbd_consops = {
156 vncfb_kbd_cngetc,
157 vncfb_kbd_cnpollc,
158 vncfb_kbd_bell,
159 };
160
161 static int
162 vncfb_match(device_t parent, cfdata_t match, void *priv)
163 {
164 struct thunkbus_attach_args *taa = priv;
165
166 return taa->taa_type == THUNKBUS_TYPE_VNCFB;
167 }
168
169 static void
170 vncfb_attach(device_t parent, device_t self, void *priv)
171 {
172 struct vncfb_softc *sc = device_private(self);
173 struct thunkbus_attach_args *taa = priv;
174 struct wsemuldisplaydev_attach_args waa;
175 struct wskbddev_attach_args kaa;
176 struct rasops_info *ri;
177 unsigned long defattr;
178
179 sc->sc_dev = self;
180 sc->sc_width = taa->u.vnc.width;
181 sc->sc_height = taa->u.vnc.height;
182 sc->sc_depth = 32;
183 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
184 #if notyet
185 sc->sc_sockfd = thunk_vnc_open_socket(taa->u.vnc.port);
186 if (sc->sc_sockfd == -1)
187 panic("couldn't open VNC socket");
188 #endif
189
190 sc->sc_framebuf = kmem_zalloc(sc->sc_width * sc->sc_height *
191 (sc->sc_depth / 8), KM_SLEEP);
192 KASSERT(sc->sc_framebuf != NULL);
193
194 aprint_naive("\n");
195 aprint_normal(": %ux%u %ubpp (port %u)\n",
196 sc->sc_width, sc->sc_height, sc->sc_depth, taa->u.vnc.port);
197
198 sc->sc_rfb.width = sc->sc_width;
199 sc->sc_rfb.height = sc->sc_height;
200 sc->sc_rfb.depth = sc->sc_depth;
201 sc->sc_rfb.framebuf = sc->sc_framebuf;
202 snprintf(sc->sc_rfb.name, sizeof(sc->sc_rfb.name),
203 "NetBSD/usermode %d.%d.%d",
204 __NetBSD_Version__ / 100000000,
205 (__NetBSD_Version__ / 1000000) % 100,
206 (__NetBSD_Version__ / 100) % 100);
207 if (thunk_rfb_open(&sc->sc_rfb, taa->u.vnc.port) != 0)
208 panic("couldn't open rfb server");
209
210 sc->sc_sih = softint_establish(SOFTINT_SERIAL, vncfb_softintr, sc);
211 sc->sc_ih = sigio_intr_establish(vncfb_intr, sc);
212
213 vcons_init(&sc->sc_vd, sc, &vncfb_defaultscreen, &vncfb_accessops);
214 sc->sc_vd.init_screen = vncfb_init_screen;
215
216 ri = &vncfb_console_screen.scr_ri;
217 vcons_init_screen(&sc->sc_vd, &vncfb_console_screen, 1, &defattr);
218 vncfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
219 vncfb_defaultscreen.textops = &ri->ri_ops;
220 vncfb_defaultscreen.capabilities = ri->ri_caps;
221 vncfb_defaultscreen.nrows = ri->ri_rows;
222 vncfb_defaultscreen.ncols = ri->ri_cols;
223 wsdisplay_cnattach(&vncfb_defaultscreen, ri, 0, 0, defattr);
224
225 vcons_replay_msgbuf(&vncfb_console_screen);
226
227 waa.console = true;
228 waa.scrdata = &vncfb_screenlist;
229 waa.accessops = &vncfb_accessops;
230 waa.accesscookie = &sc->sc_vd;
231
232 config_found(self, &waa, wsemuldisplaydevprint);
233
234 wskbd_cnattach(&vncfb_kbd_consops, sc, &vncfb_keymapdata);
235
236 kaa.console = true;
237 kaa.keymap = &vncfb_keymapdata;
238 kaa.accessops = &vncfb_kbd_accessops;
239 kaa.accesscookie = sc;
240
241 sc->sc_wskbddev = config_found_ia(self, "wskbddev", &kaa,
242 wskbddevprint);
243 }
244
245 static void
246 vncfb_init_screen(void *priv, struct vcons_screen *scr, int existing,
247 long *defattr)
248 {
249 struct vncfb_softc *sc = priv;
250 struct vncfb_fbops *ops = &sc->sc_ops;
251 struct rasops_info *ri = &scr->scr_ri;
252
253 ri->ri_width = sc->sc_width;
254 ri->ri_height = sc->sc_height;
255 ri->ri_depth = sc->sc_depth;
256 ri->ri_stride = sc->sc_width * ri->ri_depth / 8;
257 ri->ri_bits = sc->sc_framebuf;
258 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
259 if (existing)
260 ri->ri_flg |= RI_CLEAR;
261
262 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
263 ri->ri_caps = WSSCREEN_WSCOLORS;
264 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
265 sc->sc_width / ri->ri_font->fontwidth);
266
267 ri->ri_hw = scr;
268
269 ops->putchar = ri->ri_ops.putchar;
270 ops->copyrows = ri->ri_ops.copyrows;
271 ops->eraserows = ri->ri_ops.eraserows;
272 ops->copycols = ri->ri_ops.copycols;
273 ops->erasecols = ri->ri_ops.erasecols;
274 ops->cursor = ri->ri_ops.cursor;
275
276 ri->ri_ops.copyrows = vncfb_copyrows;
277 ri->ri_ops.copycols = vncfb_copycols;
278 ri->ri_ops.eraserows = vncfb_eraserows;
279 ri->ri_ops.erasecols = vncfb_erasecols;
280 ri->ri_ops.putchar = vncfb_putchar;
281 ri->ri_ops.cursor = vncfb_cursor;
282 }
283
284 static void
285 vncfb_putchar(void *priv, int row, int col, u_int c, long attr)
286 {
287 struct rasops_info *ri = priv;
288 struct vcons_screen *scr = ri->ri_hw;
289 struct vncfb_softc *sc = scr->scr_cookie;
290 struct vncfb_fbops *ops = &sc->sc_ops;
291 int x, y, w, h;
292
293 ops->putchar(ri, row, col, c, attr);
294
295 x = ri->ri_xorigin + (col * ri->ri_font->fontwidth);
296 y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
297 w = ri->ri_font->fontwidth;
298 h = ri->ri_font->fontheight;
299
300 vncfb_update(sc, x, y, w, h);
301 }
302
303 static void
304 vncfb_copycols(void *priv, int row, int srccol, int dstcol, int ncols)
305 {
306 struct rasops_info *ri = priv;
307 struct vcons_screen *scr = ri->ri_hw;
308 struct vncfb_softc *sc = scr->scr_cookie;
309 struct vncfb_fbops *ops = &sc->sc_ops;
310 int x, y, w, h;
311
312 ops->copycols(ri, row, srccol, dstcol, ncols);
313
314 y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
315 h = ri->ri_font->fontheight;
316 if (srccol < dstcol) {
317 x = ri->ri_xorigin + (srccol * ri->ri_font->fontwidth);
318 w = (dstcol - srccol) * ri->ri_font->fontwidth;
319
320 } else {
321 x = ri->ri_xorigin + (dstcol * ri->ri_font->fontwidth);
322 w = (srccol - dstcol) * ri->ri_font->fontwidth;
323 }
324
325 vncfb_update(sc, x, y, w, h);
326 }
327
328 static void
329 vncfb_erasecols(void *priv, int row, int startcol, int ncols, long fillattr)
330 {
331 struct rasops_info *ri = priv;
332 struct vcons_screen *scr = ri->ri_hw;
333 struct vncfb_softc *sc = scr->scr_cookie;
334 struct vncfb_fbops *ops = &sc->sc_ops;
335 int x, y, w, h, c;
336
337 ops->erasecols(ri, row, startcol, ncols, fillattr);
338
339 y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
340 h = ri->ri_font->fontheight;
341 x = ri->ri_xorigin + (startcol * ri->ri_font->fontwidth);
342 w = ncols * ri->ri_font->fontwidth;
343 c = ri->ri_devcmap[(fillattr >> 16) & 0xf] & 0xffffff;
344
345 vncfb_fillrect(sc, x, y, w, h, c);
346 }
347
348 static void
349 vncfb_copyrows(void *priv, int srcrow, int dstrow, int nrows)
350 {
351 struct rasops_info *ri = priv;
352 struct vcons_screen *scr = ri->ri_hw;
353 struct vncfb_softc *sc = scr->scr_cookie;
354 struct vncfb_fbops *ops = &sc->sc_ops;
355 int x, y, w, h, srcx, srcy;
356 int fontheight;
357
358 /* barrier */
359 while (sc->sc_rfb.nupdates > 0)
360 if (thunk_rfb_poll(&sc->sc_rfb, NULL) == -1)
361 break;
362
363 ops->copyrows(ri, srcrow, dstrow, nrows);
364
365 fontheight = ri->ri_font->fontheight;
366 x = ri->ri_xorigin;
367 y = ri->ri_yorigin + dstrow * fontheight;
368 w = ri->ri_width;
369 h = nrows * fontheight;
370
371 srcx = ri->ri_xorigin;
372 srcy = ri->ri_yorigin + srcrow * fontheight;
373
374 vncfb_copyrect(sc, x, y, w, h, srcx, srcy);
375 }
376
377 static void
378 vncfb_eraserows(void *priv, int row, int nrows, long fillattr)
379 {
380 struct rasops_info *ri = priv;
381 struct vcons_screen *scr = ri->ri_hw;
382 struct vncfb_softc *sc = scr->scr_cookie;
383 struct vncfb_fbops *ops = &sc->sc_ops;
384 int x, y, w, h, c;
385
386 ops->eraserows(ri, row, nrows, fillattr);
387
388 y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
389 h = nrows * ri->ri_font->fontheight;
390 x = ri->ri_xorigin;
391 w = ri->ri_width;
392 c = ri->ri_devcmap[(fillattr >> 16) & 0xf] & 0xffffff;
393
394 vncfb_fillrect(sc, x, y, w, h, c);
395 }
396
397 static void
398 vncfb_cursor(void *priv, int on, int row, int col)
399 {
400 struct rasops_info *ri = priv;
401 struct vcons_screen *scr = ri->ri_hw;
402 struct vncfb_softc *sc = scr->scr_cookie;
403 struct vncfb_fbops *ops = &sc->sc_ops;
404 int ox, oy, x, y, w, h;
405
406 w = ri->ri_font->fontwidth;
407 h = ri->ri_font->fontheight;
408
409 ox = ri->ri_ccol * w + ri->ri_xorigin;
410 oy = ri->ri_crow * h + ri->ri_yorigin;
411
412 ops->cursor(ri, on, row, col);
413
414 x = ri->ri_ccol * w + ri->ri_xorigin;
415 y = ri->ri_crow * h + ri->ri_yorigin;
416
417 vncfb_update(sc, ox, oy, w, h);
418 vncfb_update(sc, x, y, w, h);
419 }
420
421 static int
422 vncfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
423 {
424 struct vcons_data *vd = v;
425 struct vncfb_softc *sc = vd->cookie;
426 struct wsdisplay_fbinfo *wdf;
427 struct vcons_screen *ms = vd->active;
428 int new_mode;
429
430 switch (cmd) {
431 case WSDISPLAYIO_GTYPE:
432 *(u_int *)data = WSDISPLAY_TYPE_VNC;
433 return 0;
434 case WSDISPLAYIO_GINFO:
435 wdf = data;
436 wdf->height = ms->scr_ri.ri_height;
437 wdf->width = ms->scr_ri.ri_width;
438 wdf->depth = ms->scr_ri.ri_depth;
439 wdf->cmsize = 256;
440 return 0;
441 case WSDISPLAYIO_SMODE:
442 new_mode = *(int *)data;
443 if (sc->sc_mode != new_mode) {
444 sc->sc_mode = new_mode;
445 if (new_mode == WSDISPLAYIO_MODE_EMUL)
446 vcons_redraw_screen(ms);
447 }
448 return 0;
449 default:
450 return EPASSTHROUGH;
451 }
452 }
453
454 static paddr_t
455 vncfb_mmap(void *v, void *vs, off_t offset, int prot)
456 {
457 /* TODO */
458 return -1;
459 }
460
461 static void
462 vncfb_update(struct vncfb_softc *sc, int x, int y, int w, int h)
463 {
464 thunk_rfb_update(&sc->sc_rfb, x, y, w, h);
465 softint_schedule(sc->sc_sih);
466 }
467
468 static void
469 vncfb_copyrect(struct vncfb_softc *sc, int x, int y, int w, int h,
470 int srcx, int srcy)
471 {
472 thunk_rfb_copyrect(&sc->sc_rfb, x, y, w, h, srcx, srcy);
473 softint_schedule(sc->sc_sih);
474 }
475
476 static void
477 vncfb_fillrect(struct vncfb_softc *sc, int x, int y, int w, int h, uint32_t c)
478 {
479
480 thunk_rfb_fillrect(&sc->sc_rfb, x, y, w, h, (uint8_t *)&c);
481 softint_schedule(sc->sc_sih);
482 }
483
484 static int
485 vncfb_intr(void *priv)
486 {
487 struct vncfb_softc *sc = priv;
488
489 softint_schedule(sc->sc_sih);
490
491 return 0;
492 }
493
494 static void
495 vncfb_softintr(void *priv)
496 {
497 struct vncfb_softc *sc = priv;
498 thunk_rfb_event_t event;
499 int s;
500
501 while (thunk_rfb_poll(&sc->sc_rfb, &event) > 0) {
502 switch (event.message_type) {
503 case THUNK_RFB_KEY_EVENT:
504 s = spltty();
505 wskbd_input(sc->sc_wskbddev,
506 event.data.key_event.down_flag ?
507 WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP,
508 event.data.key_event.keysym & 0xfff);
509 splx(s);
510 break;
511 default:
512 break;
513 }
514 }
515 }
516
517 static int
518 vncfb_kbd_enable(void *priv, int on)
519 {
520 struct vncfb_softc *sc = priv;
521
522 sc->sc_kbd_enable = on;
523
524 return 0;
525 }
526
527 static void
528 vncfb_kbd_set_leds(void *priv, int leds)
529 {
530 }
531
532 static int
533 vncfb_kbd_ioctl(void *priv, u_long cmd, void *data, int flag, lwp_t *l)
534 {
535 struct wskbd_bell_data *bd;
536
537 switch (cmd) {
538 case WSKBDIO_GTYPE:
539 *(int *)data = WSKBD_TYPE_RFB;
540 return 0;
541 case WSKBDIO_COMPLEXBELL:
542 bd = data;
543 vncfb_kbd_bell(priv, bd->pitch, bd->period, bd->volume);
544 return 0;
545 default:
546 return EPASSTHROUGH;
547 }
548 }
549
550 static void
551 vncfb_kbd_cngetc(void *priv, u_int *type, int *data)
552 {
553 }
554
555 static void
556 vncfb_kbd_cnpollc(void *priv, int on)
557 {
558 }
559
560 static void
561 vncfb_kbd_bell(void *priv, u_int pitch, u_int period, u_int volume)
562 {
563 struct vncfb_softc *sc = priv;
564
565 thunk_rfb_bell(&sc->sc_rfb);
566 softint_schedule(sc->sc_sih);
567 }
568