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