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