wsdisplay_compat_usl.c revision 1.40 1 /* $NetBSD: wsdisplay_compat_usl.c,v 1.40 2007/03/04 06:02:51 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.40 2007/03/04 06:02:51 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 int rv;
142 mutex_enter(&proclist_mutex); /* XXXSMP */
143 rv = (sd->s_proc == p_find(sd->s_pid, PFIND_LOCKED));
144 mutex_exit(&proclist_mutex); /* XXXSMP */
145 if (rv)
146 return (1);
147 printf("usl_sync_check: process %d died\n", sd->s_pid);
148 usl_sync_done(sd);
149 return (0);
150 }
151
152 static struct usl_syncdata *
153 usl_sync_get(struct wsscreen *scr)
154 {
155 void *sd;
156
157 if (wsscreen_lookup_sync(scr, &usl_syncops, &sd))
158 return (0);
159 return (struct usl_syncdata *)sd;
160 }
161
162 static int
163 usl_detachproc(void *cookie, int waitok,
164 void (*callback)(void *, int, int), void *cbarg)
165 {
166 struct usl_syncdata *sd = cookie;
167
168 if (!usl_sync_check(sd))
169 return (0);
170
171 /* we really need a callback */
172 if (!callback)
173 return (EINVAL);
174
175 /*
176 * Normally, this is called from the controlling process.
177 * Is is supposed to reply with a VT_RELDISP ioctl(), so
178 * it is not useful to tsleep() here.
179 */
180 sd->s_callback = callback;
181 sd->s_cbarg = cbarg;
182 sd->s_flags |= SF_DETACHPENDING;
183 mutex_enter(&proclist_mutex);
184 psignal(sd->s_proc, sd->s_relsig);
185 mutex_exit(&proclist_mutex);
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,
231 void (*callback)(void *, int, int), 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 mutex_enter(&proclist_mutex);
246 psignal(sd->s_proc, sd->s_acqsig);
247 mutex_exit(&proclist_mutex);
248 callout_reset(&sd->s_attach_ch, wscompat_usl_synctimeout * hz,
249 usl_attachtimeout, sd);
250
251 return (EAGAIN);
252 }
253
254 static int
255 usl_attachack(struct usl_syncdata *sd, int ack)
256 {
257 if (!(sd->s_flags & SF_ATTACHPENDING)) {
258 printf("usl_attachack: not attaching\n");
259 return (EINVAL);
260 }
261
262 callout_stop(&sd->s_attach_ch);
263 sd->s_flags &= ~SF_ATTACHPENDING;
264
265 if (sd->s_callback)
266 (*sd->s_callback)(sd->s_cbarg, (ack ? 0 : EIO), 1);
267
268 return (0);
269 }
270
271 static void
272 usl_attachtimeout(void *arg)
273 {
274 struct usl_syncdata *sd = arg;
275
276 printf("usl_attachtimeout\n");
277
278 if (!(sd->s_flags & SF_ATTACHPENDING)) {
279 printf("usl_attachtimeout: not attaching\n");
280 return;
281 }
282
283 sd->s_flags &= ~SF_ATTACHPENDING;
284
285 if (sd->s_callback)
286 (*sd->s_callback)(sd->s_cbarg, EIO, 0);
287
288 (void) usl_sync_check(sd);
289 }
290
291 int
292 wsdisplay_usl_ioctl1(struct wsdisplay_softc *sc, u_long cmd, void *data,
293 int flag, struct lwp *l)
294 {
295 int idx, maxidx;
296
297 switch (cmd) {
298 case VT_OPENQRY:
299 maxidx = wsdisplay_maxscreenidx(sc);
300 for (idx = 0; idx <= maxidx; idx++) {
301 if (wsdisplay_screenstate(sc, idx) == 0) {
302 *(int *)data = idx + 1;
303 return (0);
304 }
305 }
306 return (ENXIO);
307 case VT_GETACTIVE:
308 idx = wsdisplay_getactivescreen(sc);
309 *(int *)data = idx + 1;
310 return (0);
311 case VT_ACTIVATE:
312 /*
313 * a gross and disgusting hack to make this abused up ioctl,
314 * which is a gross and disgusting hack on its own, work on
315 * LP64/BE - we want the lower 32bit so we simply dereference
316 * the argument pointer as long. May cause problems with 32bit
317 * kernels on sparc64?
318 */
319
320 idx = *(long *)data - 1;
321 if (idx < 0)
322 return (EINVAL);
323 return (wsdisplay_switch((struct device *)sc, idx, 1));
324 case VT_WAITACTIVE:
325 idx = *(long *)data - 1;
326 if (idx < 0)
327 return (EINVAL);
328 return (wsscreen_switchwait(sc, idx));
329 case VT_GETSTATE:
330 #define ss ((struct vt_stat *)data)
331 idx = wsdisplay_getactivescreen(sc);
332 ss->v_active = idx + 1;
333 ss->v_state = 0;
334 maxidx = wsdisplay_maxscreenidx(sc);
335 for (idx = 0; idx <= maxidx; idx++)
336 if (wsdisplay_screenstate(sc, idx) == EBUSY)
337 ss->v_state |= (1 << (idx + 1));
338 #undef ss
339 return (0);
340
341 #ifdef WSDISPLAY_COMPAT_PCVT
342 case VGAPCVTID:
343 #define id ((struct pcvtid *)data)
344 strlcpy(id->name, "pcvt", sizeof(id->name));
345 id->rmajor = 3;
346 id->rminor = 32;
347 #undef id
348 return (0);
349 #endif
350 #ifdef WSDISPLAY_COMPAT_SYSCONS
351 case CONS_GETVERS:
352 *(int *)data = 0x200; /* version 2.0 */
353 return (0);
354 #endif
355
356 default:
357 return (EPASSTHROUGH);
358 }
359 }
360
361 int
362 wsdisplay_usl_ioctl2(struct wsdisplay_softc *sc, struct wsscreen *scr,
363 u_long cmd, void *data, int flag, struct lwp *l)
364 {
365 struct proc *p = l->l_proc;
366 int intarg = 0, res;
367 u_long req;
368 void *arg;
369 struct usl_syncdata *sd;
370 struct wskbd_bell_data bd;
371
372 switch (cmd) {
373 case VT_SETMODE:
374 #define newmode ((struct vt_mode *)data)
375 if (newmode->mode == VT_PROCESS) {
376 res = usl_sync_init(scr, &sd, p, newmode->acqsig,
377 newmode->relsig, newmode->frsig);
378 if (res)
379 return (res);
380 } else {
381 sd = usl_sync_get(scr);
382 if (sd)
383 usl_sync_done(sd);
384 }
385 #undef newmode
386 return (0);
387 case VT_GETMODE:
388 #define cmode ((struct vt_mode *)data)
389 sd = usl_sync_get(scr);
390 if (sd) {
391 cmode->mode = VT_PROCESS;
392 cmode->relsig = sd->s_relsig;
393 cmode->acqsig = sd->s_acqsig;
394 cmode->frsig = sd->s_frsig;
395 } else
396 cmode->mode = VT_AUTO;
397 #undef cmode
398 return (0);
399 case VT_RELDISP:
400 #define d (*(long *)data)
401 sd = usl_sync_get(scr);
402 if (!sd)
403 return (EINVAL);
404 switch (d) {
405 case VT_FALSE:
406 case VT_TRUE:
407 return (usl_detachack(sd, (d == VT_TRUE)));
408 case VT_ACKACQ:
409 return (usl_attachack(sd, 1));
410 default:
411 return (EINVAL);
412 }
413 #undef d
414
415 case KDENABIO:
416 #if defined(__i386__) && (defined(COMPAT_11) || defined(COMPAT_FREEBSD))
417 if (kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
418 NULL, NULL, NULL, NULL) != 0)
419 return (EPERM);
420 #endif
421 /* FALLTHRU */
422 case KDDISABIO:
423 #if defined(__i386__) && (defined(COMPAT_11) || defined(COMPAT_FREEBSD))
424 {
425 /* XXX NJWLWP */
426 struct trapframe *fp = (struct trapframe *)curlwp->l_md.md_regs;
427 if (cmd == KDENABIO)
428 fp->tf_eflags |= PSL_IOPL;
429 else
430 fp->tf_eflags &= ~PSL_IOPL;
431 }
432 #endif
433 return (0);
434 case KDSETRAD:
435 /* XXX ignore for now */
436 return (0);
437
438 default:
439 return (EPASSTHROUGH);
440
441 /*
442 * the following are converted to wsdisplay ioctls
443 */
444 case KDSETMODE:
445 req = WSDISPLAYIO_SMODE;
446 #define d (*(int *)data)
447 switch (d) {
448 case KD_GRAPHICS:
449 intarg = WSDISPLAYIO_MODE_MAPPED;
450 break;
451 case KD_TEXT:
452 intarg = WSDISPLAYIO_MODE_EMUL;
453 break;
454 default:
455 return (EINVAL);
456 }
457 #undef d
458 arg = &intarg;
459 break;
460 case KDMKTONE:
461 req = WSKBDIO_COMPLEXBELL;
462 #define d (*(int *)data)
463 if (d) {
464 #define PCVT_SYSBEEPF 1193182
465 if (d >> 16) {
466 bd.which = WSKBD_BELL_DOPERIOD;
467 bd.period = d >> 16; /* ms */
468 }
469 else
470 bd.which = 0;
471 if (d & 0xffff) {
472 bd.which |= WSKBD_BELL_DOPITCH;
473 bd.pitch = PCVT_SYSBEEPF/(d & 0xffff); /* Hz */
474 }
475 } else
476 bd.which = 0; /* default */
477 #undef d
478 arg = &bd;
479 break;
480 case KDSETLED:
481 req = WSKBDIO_SETLEDS;
482 intarg = 0;
483 #define d (*(int *)data)
484 if (d & LED_CAP)
485 intarg |= WSKBD_LED_CAPS;
486 if (d & LED_NUM)
487 intarg |= WSKBD_LED_NUM;
488 if (d & LED_SCR)
489 intarg |= WSKBD_LED_SCROLL;
490 #undef d
491 arg = &intarg;
492 break;
493 case KDGETLED:
494 req = WSKBDIO_GETLEDS;
495 arg = &intarg;
496 break;
497 #ifdef WSDISPLAY_COMPAT_RAWKBD
498 case KDSKBMODE:
499 req = WSKBDIO_SETMODE;
500 switch (*(int *)data) {
501 case K_RAW:
502 intarg = WSKBD_RAW;
503 break;
504 case K_XLATE:
505 intarg = WSKBD_TRANSLATED;
506 break;
507 default:
508 return (EINVAL);
509 }
510 arg = &intarg;
511 break;
512 case KDGKBMODE:
513 req = WSKBDIO_GETMODE;
514 arg = &intarg;
515 break;
516 #endif
517 }
518
519 res = wsdisplay_internal_ioctl(sc, scr, req, arg, flag, l);
520 if (res != EPASSTHROUGH)
521 return (res);
522
523 switch (cmd) {
524 case KDGETLED:
525 #define d (*(int *)data)
526 d = 0;
527 if (intarg & WSKBD_LED_CAPS)
528 d |= LED_CAP;
529 if (intarg & WSKBD_LED_NUM)
530 d |= LED_NUM;
531 if (intarg & WSKBD_LED_SCROLL)
532 d |= LED_SCR;
533 #undef d
534 break;
535 #ifdef WSDISPLAY_COMPAT_RAWKBD
536 case KDGKBMODE:
537 *(int *)data = (intarg == WSKBD_RAW ? K_RAW : K_XLATE);
538 break;
539 #endif
540 }
541
542 return (0);
543 }
544