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