xref: /netbsd-src/sys/dev/wscons/wsdisplay_compat_usl.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /* $NetBSD: wsdisplay_compat_usl.c,v 1.32 2006/05/14 21:47:00 elad 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.32 2006/05/14 21:47:00 elad 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, void (*callback)(void *, int, int),
160 	       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, void (*callback)(void *, int, int),
225 	       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, struct lwp *l)
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 (kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, &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 *)curlwp->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 (EPASSTHROUGH);
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, l);
511 	if (res != EPASSTHROUGH)
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