wsdisplay_compat_usl.c revision 1.6 1 /* $NetBSD: wsdisplay_compat_usl.c,v 1.6 1999/01/26 14:22:14 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1998
5 * Matthias Drochner. 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 for the NetBSD Project
18 * by Matthias Drochner.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 #include "opt_compat_freebsd.h"
36 #include "opt_compat_netbsd.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/ioctl.h>
41 #include <sys/kernel.h>
42 #include <sys/proc.h>
43 #include <sys/signalvar.h>
44 #include <sys/malloc.h>
45 #include <sys/errno.h>
46
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wsdisplayvar.h>
49 #include <dev/wscons/wscons_callbacks.h>
50 #include <dev/wscons/wsdisplay_usl_io.h>
51
52 #include "opt_wsdisplay_compat.h"
53
54 struct usl_syncdata {
55 struct wsscreen *s_scr;
56 struct proc *s_proc;
57 pid_t s_pid;
58 int s_flags;
59 #define SF_DETACHPENDING 1
60 #define SF_ATTACHPENDING 2
61 int s_acqsig, s_relsig;
62 int s_frsig; /* unused */
63 void (*s_callback) __P((void *, int, int));
64 void *s_cbarg;
65 };
66
67 static int usl_sync_init __P((struct wsscreen *, struct usl_syncdata **,
68 struct proc *, int, int, int));
69 static void usl_sync_done __P((struct usl_syncdata *));
70 static int usl_sync_check __P((struct usl_syncdata *));
71 static struct usl_syncdata *usl_sync_get __P((struct wsscreen *));
72
73 static int usl_detachproc __P((void *, int,
74 void (*)(void *, int, int), void *));
75 static int usl_detachack __P((struct usl_syncdata *, int));
76 static void usl_detachtimeout __P((void *));
77 static int usl_attachproc __P((void *, int,
78 void (*)(void *, int, int), void *));
79 static int usl_attachack __P((struct usl_syncdata *, int));
80 static void usl_attachtimeout __P((void *));
81
82 static const struct wscons_syncops usl_syncops = {
83 usl_detachproc,
84 usl_attachproc,
85 #define _usl_sync_check ((int (*) __P((void *)))usl_sync_check)
86 _usl_sync_check,
87 #define _usl_sync_destroy ((void (*) __P((void *)))usl_sync_done)
88 _usl_sync_destroy
89 };
90
91 static int
92 usl_sync_init(scr, sdp, p, acqsig, relsig, frsig)
93 struct wsscreen *scr;
94 struct usl_syncdata **sdp;
95 struct proc *p;
96 int acqsig, relsig, frsig;
97 {
98 struct usl_syncdata *sd;
99 int res;
100
101 sd = malloc(sizeof(struct usl_syncdata), M_DEVBUF, M_WAITOK);
102 if (!sd)
103 return (ENOMEM);
104 sd->s_scr = scr;
105 sd->s_proc = p;
106 sd->s_pid = p->p_pid;
107 sd->s_flags = 0;
108 sd->s_acqsig = acqsig;
109 sd->s_relsig = relsig;
110 sd->s_frsig = frsig;
111 res = wsscreen_attach_sync(scr, &usl_syncops, sd);
112 if (res) {
113 free(sd, M_DEVBUF);
114 return (res);
115 }
116 *sdp = sd;
117 return (0);
118 }
119
120 static void
121 usl_sync_done(sd)
122 struct usl_syncdata *sd;
123 {
124 if (sd->s_flags & SF_DETACHPENDING) {
125 untimeout(usl_detachtimeout, sd);
126 (*sd->s_callback)(sd->s_cbarg, 0, 0);
127 }
128 if (sd->s_flags & SF_ATTACHPENDING) {
129 untimeout(usl_attachtimeout, sd);
130 (*sd->s_callback)(sd->s_cbarg, ENXIO, 0);
131 }
132 wsscreen_detach_sync(sd->s_scr);
133 free(sd, M_DEVBUF);
134 }
135
136 static int
137 usl_sync_check(sd)
138 struct usl_syncdata *sd;
139 {
140 if (sd->s_proc == pfind(sd->s_pid))
141 return (1);
142 printf("usl_sync_check: process %d died\n", sd->s_pid);
143 usl_sync_done(sd);
144 return (0);
145 }
146
147 static struct usl_syncdata *
148 usl_sync_get(scr)
149 struct wsscreen *scr;
150 {
151 struct usl_syncdata *sd;
152
153 if (wsscreen_lookup_sync(scr, &usl_syncops, (void **)&sd))
154 return (0);
155 return (sd);
156 }
157
158 static int
159 usl_detachproc(cookie, waitok, callback, cbarg)
160 void *cookie;
161 int waitok;
162 void (*callback) __P((void *, int, int));
163 void *cbarg;
164 {
165 struct usl_syncdata *sd = cookie;
166
167 if (!usl_sync_check(sd))
168 return (0);
169
170 /*
171 * Normally, this is called from the controlling process.
172 * Is is supposed to reply with a VT_RELDISP ioctl(), so
173 * it is not useful to tsleep() here.
174 */
175 sd->s_callback = callback;
176 sd->s_cbarg = cbarg;
177 sd->s_flags |= SF_DETACHPENDING;
178 psignal(sd->s_proc, sd->s_relsig);
179 timeout(usl_detachtimeout, sd, 5*hz);
180
181 return (EAGAIN);
182 }
183
184 static int
185 usl_detachack(sd, ack)
186 struct usl_syncdata *sd;
187 int ack;
188 {
189 if (!(sd->s_flags & SF_DETACHPENDING)) {
190 printf("usl_detachack: not detaching\n");
191 return (EINVAL);
192 }
193
194 untimeout(usl_detachtimeout, sd);
195 sd->s_flags &= ~SF_DETACHPENDING;
196
197 if (sd->s_callback)
198 (*sd->s_callback)(sd->s_cbarg, (ack ? 0 : EIO), 1);
199
200 return (0);
201 }
202
203 static void
204 usl_detachtimeout(arg)
205 void *arg;
206 {
207 struct usl_syncdata *sd = arg;
208
209 printf("usl_detachtimeout\n");
210
211 if (!(sd->s_flags & SF_DETACHPENDING)) {
212 printf("usl_detachtimeout: not detaching\n");
213 return;
214 }
215
216 sd->s_flags &= ~SF_DETACHPENDING;
217
218 if (sd->s_callback)
219 (*sd->s_callback)(sd->s_cbarg, EIO, 0);
220
221 (void) usl_sync_check(sd);
222 }
223
224 static int
225 usl_attachproc(cookie, waitok, callback, cbarg)
226 void *cookie;
227 int waitok;
228 void (*callback) __P((void *, int, int));
229 void *cbarg;
230 {
231 struct usl_syncdata *sd = cookie;
232
233 if (!usl_sync_check(sd))
234 return (0);
235
236 sd->s_callback = callback;
237 sd->s_cbarg = cbarg;
238 sd->s_flags |= SF_ATTACHPENDING;
239 psignal(sd->s_proc, sd->s_acqsig);
240 timeout(usl_attachtimeout, sd, 5*hz);
241
242 return (EAGAIN);
243 }
244
245 static int
246 usl_attachack(sd, ack)
247 struct usl_syncdata *sd;
248 int ack;
249 {
250 if (!(sd->s_flags & SF_ATTACHPENDING)) {
251 printf("usl_attachack: not attaching\n");
252 return (EINVAL);
253 }
254
255 untimeout(usl_attachtimeout, sd);
256 sd->s_flags &= ~SF_ATTACHPENDING;
257
258 if (sd->s_callback)
259 (*sd->s_callback)(sd->s_cbarg, (ack ? 0 : EIO), 1);
260
261 return (0);
262 }
263
264 static void
265 usl_attachtimeout(arg)
266 void *arg;
267 {
268 struct usl_syncdata *sd = arg;
269
270 printf("usl_attachtimeout\n");
271
272 if (!(sd->s_flags & SF_ATTACHPENDING)) {
273 printf("usl_attachtimeout: not attaching\n");
274 return;
275 }
276
277 sd->s_flags &= ~SF_ATTACHPENDING;
278
279 if (sd->s_callback)
280 (*sd->s_callback)(sd->s_cbarg, EIO, 0);
281
282 (void) usl_sync_check(sd);
283 }
284
285 int
286 wsdisplay_usl_ioctl(sc, scr, cmd, data, flag, p)
287 struct wsdisplay_softc *sc;
288 struct wsscreen *scr;
289 u_long cmd;
290 caddr_t data;
291 int flag;
292 struct proc *p;
293 {
294 int res, idx, maxidx;
295 struct usl_syncdata *sd;
296 int req, intarg;
297 struct wskbd_bell_data bd;
298 void *arg;
299
300 switch (cmd) {
301 case VT_SETMODE:
302 #define newmode ((struct vt_mode *)data)
303 if (newmode->mode == VT_PROCESS) {
304 res = usl_sync_init(scr, &sd, p, newmode->acqsig,
305 newmode->relsig, newmode->frsig);
306 if (res)
307 return (res);
308 } else {
309 sd = usl_sync_get(scr);
310 if (sd)
311 usl_sync_done(sd);
312 }
313 #undef newmode
314 return (0);
315 case VT_GETMODE:
316 #define cmode ((struct vt_mode *)data)
317 sd = usl_sync_get(scr);
318 if (sd) {
319 cmode->mode = VT_PROCESS;
320 cmode->relsig = sd->s_relsig;
321 cmode->acqsig = sd->s_acqsig;
322 cmode->frsig = sd->s_frsig;
323 } else
324 cmode->mode = VT_AUTO;
325 #undef cmode
326 return (0);
327 case VT_RELDISP:
328 #define d (*(int *)data)
329 sd = usl_sync_get(scr);
330 if (!sd)
331 return (EINVAL);
332 switch (d) {
333 case VT_FALSE:
334 case VT_TRUE:
335 return (usl_detachack(sd, (d == VT_TRUE)));
336 case VT_ACKACQ:
337 return (usl_attachack(sd, 1));
338 default:
339 return (EINVAL);
340 }
341 #undef d
342 return (0);
343 case VT_OPENQRY:
344 maxidx = wsdisplay_maxscreenidx(sc);
345 for (idx = 0; idx <= maxidx; idx++) {
346 if (wsdisplay_screenstate(sc, idx) == 0) {
347 *(int *)data = idx + 1;
348 return (0);
349 }
350 }
351 return (ENXIO);
352 case VT_GETACTIVE:
353 idx = wsdisplay_getactivescreen(sc);
354 *(int *)data = idx + 1;
355 return (0);
356 case VT_ACTIVATE:
357 idx = *(int *)data - 1;
358 return (wsdisplay_switch((struct device *)sc, idx, 1));
359 case VT_WAITACTIVE:
360 idx = *(int *)data - 1;
361 return (wsscreen_switchwait(sc, idx));
362 case VT_GETSTATE:
363 #define ss ((struct vt_stat *)data)
364 idx = wsdisplay_getactivescreen(sc);
365 ss->v_active = idx + 1;
366 ss->v_state = 0;
367 maxidx = wsdisplay_maxscreenidx(sc);
368 for (idx = 0; idx <= maxidx; idx++)
369 if (wsdisplay_screenstate(sc, idx) == EBUSY)
370 ss->v_state |= (1 << (idx + 1));
371 #undef s
372 return (0);
373 case KDENABIO:
374 if (suser(p->p_ucred, &p->p_acflag) || securelevel > 1)
375 return (EPERM);
376 /* FALLTHRU */
377 case KDDISABIO:
378 #if defined(__i386__)
379 #if defined(COMPAT_10) || defined(COMPAT_11) || defined(COMPAT_FREEBSD)
380 {
381 struct trapframe *fp = (struct trapframe *)p->p_md.md_regs;
382 if (cmd == KDENABIO)
383 fp->tf_eflags |= PSL_IOPL;
384 else
385 fp->tf_eflags &= ~PSL_IOPL;
386 }
387 #endif
388 #endif
389 return (0);
390 case KDSETRAD:
391 /* XXX ignore for now */
392 return (0);
393
394 #ifdef WSDISPLAY_COMPAT_PCVT
395 case VGAPCVTID:
396 #define id ((struct pcvtid *)data)
397 strcpy(id->name, "pcvt");
398 id->rmajor = 3;
399 id->rminor = 32;
400 #undef id
401 return (0);
402 #endif
403 #ifdef WSDISPLAY_COMPAT_SYSCONS
404 case CONS_GETVERS:
405 *(int *)data = 0x200; /* version 2.0 */
406 return (0);
407 #endif
408
409 default:
410 return (-1);
411
412 /*
413 * the following are converted to wsdisplay ioctls
414 */
415 case KDSETMODE:
416 req = WSDISPLAYIO_SMODE;
417 #define d (*(int *)data)
418 switch (d) {
419 case KD_GRAPHICS:
420 intarg = WSDISPLAYIO_MODE_MAPPED;
421 break;
422 case KD_TEXT:
423 intarg = WSDISPLAYIO_MODE_EMUL;
424 break;
425 default:
426 return (EINVAL);
427 }
428 #undef d
429 arg = &intarg;
430 break;
431 case KDMKTONE:
432 req = WSKBDIO_COMPLEXBELL;
433 #define d (*(int *)data)
434 if (d) {
435 bd.which = WSKBD_BELL_DOPITCH | WSKBD_BELL_DOPERIOD;
436 bd.pitch = d & 0xffff; /* Hz */
437 bd.period = d >> 16; /* ms */
438 } else
439 bd.which = 0; /* default */
440 #undef d
441 arg = &bd;
442 break;
443 case KDSETLED:
444 req = WSKBDIO_SETLEDS;
445 intarg = 0;
446 #define d (*(int *)data)
447 if (d & LED_CAP)
448 intarg |= WSKBD_LED_CAPS;
449 if (d & LED_NUM)
450 intarg |= WSKBD_LED_NUM;
451 if (d & LED_SCR)
452 intarg |= WSKBD_LED_SCROLL;
453 #undef d
454 arg = &intarg;
455 break;
456 case KDGETLED:
457 req = WSKBDIO_GETLEDS;
458 arg = &intarg;
459 break;
460 #ifdef WSDISPLAY_COMPAT_RAWKBD
461 case KDSKBMODE:
462 req = WSKBDIO_SETMODE;
463 switch (*(int *)data) {
464 case K_RAW:
465 intarg = WSKBD_RAW;
466 break;
467 case K_XLATE:
468 intarg = WSKBD_TRANSLATED;
469 break;
470 default:
471 return (EINVAL);
472 }
473 arg = &intarg;
474 break;
475 case KDGKBMODE:
476 req = WSKBDIO_GETMODE;
477 arg = &intarg;
478 break;
479 #endif
480 }
481
482 res = wsdisplay_internal_ioctl(sc, scr, req, arg, flag, p);
483 if (res)
484 return (res);
485
486 switch (cmd) {
487 case KDGETLED:
488 #define d (*(int *)data)
489 d = 0;
490 if (intarg & WSKBD_LED_CAPS)
491 d |= LED_CAP;
492 if (intarg & WSKBD_LED_NUM)
493 d |= LED_NUM;
494 if (intarg & WSKBD_LED_SCROLL)
495 d |= LED_SCR;
496 #undef d
497 break;
498 #ifdef WSDISPLAY_COMPAT_RAWKBD
499 case KDGKBMODE:
500 *(int *)data = (intarg == WSKBD_RAW ? K_RAW : K_XLATE);
501 break;
502 #endif
503 }
504
505 return (0);
506 }
507