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