vncfb.c revision 1.1 1 /* $NetBSD: vncfb.c,v 1.1 2011/12/29 21:22:49 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.1 2011/12/29 21:22:49 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 };
67
68 struct vncfb_softc {
69 device_t sc_dev;
70 device_t sc_wskbddev;
71 thunk_rfb_t sc_rfb;
72 unsigned int sc_width;
73 unsigned int sc_height;
74 unsigned int sc_depth;
75 int sc_mode;
76 uint8_t * sc_framebuf;
77 struct vcons_data sc_vd;
78 struct vncfb_fbops sc_ops;
79
80 int sc_kbd_enable;
81
82 callout_t sc_callout;
83 void *sc_sih;
84 };
85
86 static int vncfb_match(device_t, cfdata_t, void *);
87 static void vncfb_attach(device_t, device_t, void *);
88
89 CFATTACH_DECL_NEW(vncfb, sizeof(struct vncfb_softc),
90 vncfb_match, vncfb_attach, NULL, NULL);
91
92 static void vncfb_putchar(void *, int, int, u_int, long);
93 static void vncfb_copycols(void *, int, int, int, int);
94 static void vncfb_erasecols(void *, int, int, int, long);
95 static void vncfb_copyrows(void *, int, int, int);
96 static void vncfb_eraserows(void *, int, int, long);
97
98 static int vncfb_ioctl(void *, void *, u_long, void *, int, lwp_t *);
99 static paddr_t vncfb_mmap(void *, void *, off_t, int);
100
101 static void vncfb_init_screen(void *, struct vcons_screen *, int, long *);
102
103 static void vncfb_update(struct vncfb_softc *, int, int, int, int);
104 static void vncfb_poll(void *);
105 static void vncfb_softintr(void *);
106
107 static int vncfb_kbd_enable(void *, int);
108 static void vncfb_kbd_set_leds(void *, int);
109 static int vncfb_kbd_ioctl(void *, u_long, void *, int, lwp_t *);
110
111 static void vncfb_kbd_cngetc(void *, u_int *, int *);
112 static void vncfb_kbd_cnpollc(void *, int);
113
114 static struct vcons_screen vncfb_console_screen;
115
116 static struct wsscreen_descr vncfb_defaultscreen = {
117 .name = "default",
118 .fontwidth = 8,
119 .fontheight = 16,
120 .capabilities = WSSCREEN_WSCOLORS | WSSCREEN_HILIT,
121 };
122
123 static const struct wsscreen_descr *vncfb_screens[] = {
124 &vncfb_defaultscreen,
125 };
126
127 static struct wsscreen_list vncfb_screenlist = {
128 .screens = vncfb_screens,
129 .nscreens = __arraycount(vncfb_screens),
130 };
131
132 static struct wsdisplay_accessops vncfb_accessops = {
133 .ioctl = vncfb_ioctl,
134 .mmap = vncfb_mmap,
135 };
136
137 extern const struct wscons_keydesc vnckbd_keydesctab[];
138
139 static const struct wskbd_mapdata vncfb_keymapdata = {
140 vnckbd_keydesctab,
141 KB_US,
142 };
143
144 static struct wskbd_accessops vncfb_kbd_accessops = {
145 vncfb_kbd_enable,
146 vncfb_kbd_set_leds,
147 vncfb_kbd_ioctl,
148 };
149
150 static const struct wskbd_consops vncfb_kbd_consops = {
151 vncfb_kbd_cngetc,
152 vncfb_kbd_cnpollc,
153 NULL, /* bell */
154 };
155
156 static int
157 vncfb_match(device_t parent, cfdata_t match, void *priv)
158 {
159 struct thunkbus_attach_args *taa = priv;
160
161 return taa->taa_type == THUNKBUS_TYPE_VNCFB;
162 }
163
164 static void
165 vncfb_attach(device_t parent, device_t self, void *priv)
166 {
167 struct vncfb_softc *sc = device_private(self);
168 struct thunkbus_attach_args *taa = priv;
169 struct wsemuldisplaydev_attach_args waa;
170 struct wskbddev_attach_args kaa;
171 struct rasops_info *ri;
172 unsigned long defattr;
173
174 sc->sc_dev = self;
175 sc->sc_width = taa->u.vnc.width;
176 sc->sc_height = taa->u.vnc.height;
177 sc->sc_depth = 32;
178 sc->sc_mode = WSDISPLAYIO_MODE_EMUL;
179 #if notyet
180 sc->sc_sockfd = thunk_vnc_open_socket(taa->u.vnc.port);
181 if (sc->sc_sockfd == -1)
182 panic("couldn't open VNC socket");
183 #endif
184
185 sc->sc_framebuf = kmem_zalloc(sc->sc_width * sc->sc_height *
186 (sc->sc_depth / 8), KM_SLEEP);
187 KASSERT(sc->sc_framebuf != NULL);
188
189 callout_init(&sc->sc_callout, 0);
190 callout_setfunc(&sc->sc_callout, vncfb_poll, sc);
191
192 aprint_naive("\n");
193 aprint_normal(": %ux%u %ubpp (port %u)\n",
194 sc->sc_width, sc->sc_height, sc->sc_depth, taa->u.vnc.port);
195
196 sc->sc_rfb.width = sc->sc_width;
197 sc->sc_rfb.height = sc->sc_height;
198 sc->sc_rfb.depth = sc->sc_depth;
199 sc->sc_rfb.framebuf = sc->sc_framebuf;
200 snprintf(sc->sc_rfb.name, sizeof(sc->sc_rfb.name),
201 "NetBSD/usermode %d.%d.%d",
202 __NetBSD_Version__ / 100000000,
203 (__NetBSD_Version__ / 1000000) % 100,
204 (__NetBSD_Version__ / 100) % 100);
205 if (thunk_rfb_open(&sc->sc_rfb, taa->u.vnc.port) != 0)
206 panic("couldn't open rfb server");
207
208 sc->sc_sih = softint_establish(SOFTINT_SERIAL, vncfb_softintr, sc);
209 callout_schedule(&sc->sc_callout, 1);
210
211 vcons_init(&sc->sc_vd, sc, &vncfb_defaultscreen, &vncfb_accessops);
212 sc->sc_vd.init_screen = vncfb_init_screen;
213
214 ri = &vncfb_console_screen.scr_ri;
215 vcons_init_screen(&sc->sc_vd, &vncfb_console_screen, 1, &defattr);
216 vncfb_console_screen.scr_flags |= VCONS_SCREEN_IS_STATIC;
217 vncfb_defaultscreen.textops = &ri->ri_ops;
218 vncfb_defaultscreen.capabilities = ri->ri_caps;
219 vncfb_defaultscreen.nrows = ri->ri_rows;
220 vncfb_defaultscreen.ncols = ri->ri_cols;
221 wsdisplay_cnattach(&vncfb_defaultscreen, ri, 0, 0, defattr);
222
223 vcons_replay_msgbuf(&vncfb_console_screen);
224
225 waa.console = true;
226 waa.scrdata = &vncfb_screenlist;
227 waa.accessops = &vncfb_accessops;
228 waa.accesscookie = &sc->sc_vd;
229
230 config_found(self, &waa, wsemuldisplaydevprint);
231
232 wskbd_cnattach(&vncfb_kbd_consops, sc, &vncfb_keymapdata);
233
234 kaa.console = true;
235 kaa.keymap = &vncfb_keymapdata;
236 kaa.accessops = &vncfb_kbd_accessops;
237 kaa.accesscookie = sc;
238
239 sc->sc_wskbddev = config_found_ia(self, "wskbddev", &kaa,
240 wskbddevprint);
241 }
242
243 static void
244 vncfb_init_screen(void *priv, struct vcons_screen *scr, int existing,
245 long *defattr)
246 {
247 struct vncfb_softc *sc = priv;
248 struct vncfb_fbops *ops = &sc->sc_ops;
249 struct rasops_info *ri = &scr->scr_ri;
250
251 ri->ri_width = sc->sc_width;
252 ri->ri_height = sc->sc_height;
253 ri->ri_depth = sc->sc_depth;
254 ri->ri_stride = sc->sc_width * ri->ri_depth / 8;
255 ri->ri_bits = sc->sc_framebuf;
256 ri->ri_flg = RI_CENTER | RI_FULLCLEAR;
257 if (existing)
258 ri->ri_flg |= RI_CLEAR;
259
260 rasops_init(ri, sc->sc_height / 8, sc->sc_width / 8);
261 ri->ri_caps = WSSCREEN_WSCOLORS;
262 rasops_reconfig(ri, sc->sc_height / ri->ri_font->fontheight,
263 sc->sc_width / ri->ri_font->fontwidth);
264
265 ri->ri_hw = scr;
266
267 ops->putchar = ri->ri_ops.putchar;
268 ops->copyrows = ri->ri_ops.copyrows;
269 ops->eraserows = ri->ri_ops.eraserows;
270 ops->copycols = ri->ri_ops.copycols;
271 ops->erasecols = ri->ri_ops.erasecols;
272
273 ri->ri_ops.copyrows = vncfb_copyrows;
274 ri->ri_ops.copycols = vncfb_copycols;
275 ri->ri_ops.eraserows = vncfb_eraserows;
276 ri->ri_ops.erasecols = vncfb_erasecols;
277 ri->ri_ops.putchar = vncfb_putchar;
278 }
279
280 static void
281 vncfb_putchar(void *priv, int row, int col, u_int c, long attr)
282 {
283 struct rasops_info *ri = priv;
284 struct vcons_screen *scr = ri->ri_hw;
285 struct vncfb_softc *sc = scr->scr_cookie;
286 struct vncfb_fbops *ops = &sc->sc_ops;
287 int x, y, w, h;
288
289 ops->putchar(ri, row, col, c, attr);
290
291 x = ri->ri_xorigin + (col * ri->ri_font->fontwidth);
292 y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
293 w = ri->ri_font->fontwidth;
294 h = ri->ri_font->fontheight;
295
296 vncfb_update(sc, x, y, w, h);
297 }
298
299 static void
300 vncfb_copycols(void *priv, int row, int srccol, int dstcol, int ncols)
301 {
302 struct rasops_info *ri = priv;
303 struct vcons_screen *scr = ri->ri_hw;
304 struct vncfb_softc *sc = scr->scr_cookie;
305 struct vncfb_fbops *ops = &sc->sc_ops;
306 int x, y, w, h;
307
308 ops->copycols(ri, row, srccol, dstcol, ncols);
309
310 y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
311 h = ri->ri_font->fontheight;
312 if (srccol < dstcol) {
313 x = ri->ri_xorigin + (srccol * ri->ri_font->fontwidth);
314 w = (dstcol - srccol) * ri->ri_font->fontwidth;
315
316 } else {
317 x = ri->ri_xorigin + (dstcol * ri->ri_font->fontwidth);
318 w = (srccol - dstcol) * ri->ri_font->fontwidth;
319 }
320
321 vncfb_update(sc, x, y, w, h);
322 }
323
324 static void
325 vncfb_erasecols(void *priv, int row, int startcol, int ncols, long fillattr)
326 {
327 struct rasops_info *ri = priv;
328 struct vcons_screen *scr = ri->ri_hw;
329 struct vncfb_softc *sc = scr->scr_cookie;
330 struct vncfb_fbops *ops = &sc->sc_ops;
331 int x, y, w, h;
332
333 ops->erasecols(ri, row, startcol, ncols, fillattr);
334
335 y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
336 h = ri->ri_font->fontheight;
337 x = ri->ri_xorigin + (startcol * ri->ri_font->fontwidth);
338 w = ncols * ri->ri_font->fontwidth;
339
340 vncfb_update(sc, x, y, w, h);
341 }
342
343 static void
344 vncfb_copyrows(void *priv, int srcrow, int dstrow, int nrows)
345 {
346 struct rasops_info *ri = priv;
347 struct vcons_screen *scr = ri->ri_hw;
348 struct vncfb_softc *sc = scr->scr_cookie;
349 struct vncfb_fbops *ops = &sc->sc_ops;
350 int x, y, w, h;
351
352 ops->copyrows(ri, srcrow, dstrow, nrows);
353
354 x = ri->ri_xorigin;
355 w = ri->ri_width;
356 if (srcrow < dstrow) {
357 y = ri->ri_yorigin + (srcrow * ri->ri_font->fontheight);
358 h = (dstrow - srcrow) * ri->ri_font->fontheight;
359 } else {
360 y = ri->ri_yorigin + (dstrow * ri->ri_font->fontheight);
361 h = (srcrow - dstrow) * ri->ri_font->fontheight;
362 }
363
364 vncfb_update(sc, x, y, w, h);
365 }
366
367 static void
368 vncfb_eraserows(void *priv, int row, int nrows, long fillattr)
369 {
370 struct rasops_info *ri = priv;
371 struct vcons_screen *scr = ri->ri_hw;
372 struct vncfb_softc *sc = scr->scr_cookie;
373 struct vncfb_fbops *ops = &sc->sc_ops;
374 int x, y, w, h;
375
376 ops->eraserows(ri, row, nrows, fillattr);
377
378 y = ri->ri_yorigin + (row * ri->ri_font->fontheight);
379 h = nrows * ri->ri_font->fontheight;
380 x = ri->ri_xorigin;
381 w = ri->ri_width;
382
383 vncfb_update(sc, x, y, w, h);
384 }
385
386 static int
387 vncfb_ioctl(void *v, void *vs, u_long cmd, void *data, int flag, lwp_t *l)
388 {
389 struct vcons_data *vd = v;
390 struct vncfb_softc *sc = vd->cookie;
391 struct wsdisplay_fbinfo *wdf;
392 struct vcons_screen *ms = vd->active;
393 int new_mode;
394
395 switch (cmd) {
396 case WSDISPLAYIO_GTYPE:
397 *(u_int *)data = WSDISPLAY_TYPE_VNC;
398 return 0;
399 case WSDISPLAYIO_GINFO:
400 wdf = data;
401 wdf->height = ms->scr_ri.ri_height;
402 wdf->width = ms->scr_ri.ri_width;
403 wdf->depth = ms->scr_ri.ri_depth;
404 wdf->cmsize = 256;
405 return 0;
406 case WSDISPLAYIO_SMODE:
407 new_mode = *(int *)data;
408 if (sc->sc_mode != new_mode) {
409 sc->sc_mode = new_mode;
410 if (new_mode == WSDISPLAYIO_MODE_EMUL)
411 vcons_redraw_screen(ms);
412 }
413 return 0;
414 default:
415 return EPASSTHROUGH;
416 }
417 }
418
419 static paddr_t
420 vncfb_mmap(void *v, void *vs, off_t offset, int prot)
421 {
422 /* TODO */
423 return -1;
424 }
425
426 static void
427 vncfb_update(struct vncfb_softc *sc, int x, int y, int w, int h)
428 {
429 thunk_rfb_update(&sc->sc_rfb, x, y, w, h);
430 }
431
432 static void
433 vncfb_poll(void *priv)
434 {
435 struct vncfb_softc *sc = priv;
436
437 softint_schedule(sc->sc_sih);
438 }
439
440 static void
441 vncfb_softintr(void *priv)
442 {
443 struct vncfb_softc *sc = priv;
444 thunk_rfb_event_t event;
445 int s;
446
447 while (thunk_rfb_poll(&sc->sc_rfb, &event) > 0) {
448 switch (event.message_type) {
449 case THUNK_RFB_KEY_EVENT:
450 s = spltty();
451 wskbd_input(sc->sc_wskbddev,
452 event.data.key_event.down_flag ?
453 WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP,
454 event.data.key_event.keysym & 0xfff);
455 splx(s);
456 break;
457 default:
458 break;
459 }
460 }
461
462 callout_schedule(&sc->sc_callout, 1);
463 }
464
465 static int
466 vncfb_kbd_enable(void *priv, int on)
467 {
468 struct vncfb_softc *sc = priv;
469
470 sc->sc_kbd_enable = on;
471
472 return 0;
473 }
474
475 static void
476 vncfb_kbd_set_leds(void *priv, int leds)
477 {
478 }
479
480 static int
481 vncfb_kbd_ioctl(void *priv, u_long cmd, void *data, int flag, lwp_t *l)
482 {
483 switch (cmd) {
484 case WSKBDIO_GTYPE:
485 *(int *)data = WSKBD_TYPE_RFB;
486 return 0;
487 default:
488 return EPASSTHROUGH;
489 }
490 }
491
492 static void
493 vncfb_kbd_cngetc(void *priv, u_int *type, int *data)
494 {
495 }
496
497 static void
498 vncfb_kbd_cnpollc(void *priv, int on)
499 {
500 }
501