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