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