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