wsdisplay_compat_usl.c revision 1.33.4.3 1 /* $NetBSD: wsdisplay_compat_usl.c,v 1.33.4.3 2007/01/19 09:39:58 ad 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.33.4.3 2007/01/19 09:39:58 ad 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,
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 mutex_enter(&proclist_mutex);
180 psignal(sd->s_proc, sd->s_relsig);
181 mutex_exit(&proclist_mutex);
182 callout_reset(&sd->s_detach_ch, wscompat_usl_synctimeout * hz,
183 usl_detachtimeout, sd);
184
185 return (EAGAIN);
186 }
187
188 static int
189 usl_detachack(struct usl_syncdata *sd, int ack)
190 {
191 if (!(sd->s_flags & SF_DETACHPENDING)) {
192 printf("usl_detachack: not detaching\n");
193 return (EINVAL);
194 }
195
196 callout_stop(&sd->s_detach_ch);
197 sd->s_flags &= ~SF_DETACHPENDING;
198
199 if (sd->s_callback)
200 (*sd->s_callback)(sd->s_cbarg, (ack ? 0 : EIO), 1);
201
202 return (0);
203 }
204
205 static void
206 usl_detachtimeout(void *arg)
207 {
208 struct usl_syncdata *sd = arg;
209
210 printf("usl_detachtimeout\n");
211
212 if (!(sd->s_flags & SF_DETACHPENDING)) {
213 printf("usl_detachtimeout: not detaching\n");
214 return;
215 }
216
217 sd->s_flags &= ~SF_DETACHPENDING;
218
219 if (sd->s_callback)
220 (*sd->s_callback)(sd->s_cbarg, EIO, 0);
221
222 (void) usl_sync_check(sd);
223 }
224
225 static int
226 usl_attachproc(void *cookie, int waitok,
227 void (*callback)(void *, int, int), void *cbarg)
228 {
229 struct usl_syncdata *sd = cookie;
230
231 if (!usl_sync_check(sd))
232 return (0);
233
234 /* we really need a callback */
235 if (!callback)
236 return (EINVAL);
237
238 sd->s_callback = callback;
239 sd->s_cbarg = cbarg;
240 sd->s_flags |= SF_ATTACHPENDING;
241 mutex_enter(&proclist_mutex);
242 psignal(sd->s_proc, sd->s_acqsig);
243 mutex_exit(&proclist_mutex);
244 callout_reset(&sd->s_attach_ch, wscompat_usl_synctimeout * hz,
245 usl_attachtimeout, sd);
246
247 return (EAGAIN);
248 }
249
250 static int
251 usl_attachack(struct usl_syncdata *sd, int ack)
252 {
253 if (!(sd->s_flags & SF_ATTACHPENDING)) {
254 printf("usl_attachack: not attaching\n");
255 return (EINVAL);
256 }
257
258 callout_stop(&sd->s_attach_ch);
259 sd->s_flags &= ~SF_ATTACHPENDING;
260
261 if (sd->s_callback)
262 (*sd->s_callback)(sd->s_cbarg, (ack ? 0 : EIO), 1);
263
264 return (0);
265 }
266
267 static void
268 usl_attachtimeout(void *arg)
269 {
270 struct usl_syncdata *sd = arg;
271
272 printf("usl_attachtimeout\n");
273
274 if (!(sd->s_flags & SF_ATTACHPENDING)) {
275 printf("usl_attachtimeout: not attaching\n");
276 return;
277 }
278
279 sd->s_flags &= ~SF_ATTACHPENDING;
280
281 if (sd->s_callback)
282 (*sd->s_callback)(sd->s_cbarg, EIO, 0);
283
284 (void) usl_sync_check(sd);
285 }
286
287 int
288 wsdisplay_usl_ioctl1(struct wsdisplay_softc *sc, u_long cmd, caddr_t data,
289 int flag, struct lwp *l)
290 {
291 int idx, maxidx;
292
293 switch (cmd) {
294 case VT_OPENQRY:
295 maxidx = wsdisplay_maxscreenidx(sc);
296 for (idx = 0; idx <= maxidx; idx++) {
297 if (wsdisplay_screenstate(sc, idx) == 0) {
298 *(int *)data = idx + 1;
299 return (0);
300 }
301 }
302 return (ENXIO);
303 case VT_GETACTIVE:
304 idx = wsdisplay_getactivescreen(sc);
305 *(int *)data = idx + 1;
306 return (0);
307 case VT_ACTIVATE:
308 /*
309 * a gross and disgusting hack to make this abused up ioctl,
310 * which is a gross and disgusting hack on its own, work on
311 * LP64/BE - we want the lower 32bit so we simply dereference
312 * the argument pointer as long. May cause problems with 32bit
313 * kernels on sparc64?
314 */
315
316 idx = *(long *)data - 1;
317 if (idx < 0)
318 return (EINVAL);
319 return (wsdisplay_switch((struct device *)sc, idx, 1));
320 case VT_WAITACTIVE:
321 idx = *(long *)data - 1;
322 if (idx < 0)
323 return (EINVAL);
324 return (wsscreen_switchwait(sc, idx));
325 case VT_GETSTATE:
326 #define ss ((struct vt_stat *)data)
327 idx = wsdisplay_getactivescreen(sc);
328 ss->v_active = idx + 1;
329 ss->v_state = 0;
330 maxidx = wsdisplay_maxscreenidx(sc);
331 for (idx = 0; idx <= maxidx; idx++)
332 if (wsdisplay_screenstate(sc, idx) == EBUSY)
333 ss->v_state |= (1 << (idx + 1));
334 #undef ss
335 return (0);
336
337 #ifdef WSDISPLAY_COMPAT_PCVT
338 case VGAPCVTID:
339 #define id ((struct pcvtid *)data)
340 strlcpy(id->name, "pcvt", sizeof(id->name));
341 id->rmajor = 3;
342 id->rminor = 32;
343 #undef id
344 return (0);
345 #endif
346 #ifdef WSDISPLAY_COMPAT_SYSCONS
347 case CONS_GETVERS:
348 *(int *)data = 0x200; /* version 2.0 */
349 return (0);
350 #endif
351
352 default:
353 return (EPASSTHROUGH);
354 }
355 }
356
357 int
358 wsdisplay_usl_ioctl2(struct wsdisplay_softc *sc, struct wsscreen *scr,
359 u_long cmd, caddr_t data, int flag, struct lwp *l)
360 {
361 struct proc *p = l->l_proc;
362 int intarg = 0, res;
363 u_long req;
364 void *arg;
365 struct usl_syncdata *sd;
366 struct wskbd_bell_data bd;
367
368 switch (cmd) {
369 case VT_SETMODE:
370 #define newmode ((struct vt_mode *)data)
371 if (newmode->mode == VT_PROCESS) {
372 res = usl_sync_init(scr, &sd, p, newmode->acqsig,
373 newmode->relsig, newmode->frsig);
374 if (res)
375 return (res);
376 } else {
377 sd = usl_sync_get(scr);
378 if (sd)
379 usl_sync_done(sd);
380 }
381 #undef newmode
382 return (0);
383 case VT_GETMODE:
384 #define cmode ((struct vt_mode *)data)
385 sd = usl_sync_get(scr);
386 if (sd) {
387 cmode->mode = VT_PROCESS;
388 cmode->relsig = sd->s_relsig;
389 cmode->acqsig = sd->s_acqsig;
390 cmode->frsig = sd->s_frsig;
391 } else
392 cmode->mode = VT_AUTO;
393 #undef cmode
394 return (0);
395 case VT_RELDISP:
396 #define d (*(long *)data)
397 sd = usl_sync_get(scr);
398 if (!sd)
399 return (EINVAL);
400 switch (d) {
401 case VT_FALSE:
402 case VT_TRUE:
403 return (usl_detachack(sd, (d == VT_TRUE)));
404 case VT_ACKACQ:
405 return (usl_attachack(sd, 1));
406 default:
407 return (EINVAL);
408 }
409 #undef d
410
411 case KDENABIO:
412 #if defined(__i386__) && (defined(COMPAT_11) || defined(COMPAT_FREEBSD))
413 if (kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
414 NULL, NULL, NULL, NULL) != 0)
415 return (EPERM);
416 #endif
417 /* FALLTHRU */
418 case KDDISABIO:
419 #if defined(__i386__) && (defined(COMPAT_11) || defined(COMPAT_FREEBSD))
420 {
421 /* XXX NJWLWP */
422 struct trapframe *fp = (struct trapframe *)curlwp->l_md.md_regs;
423 if (cmd == KDENABIO)
424 fp->tf_eflags |= PSL_IOPL;
425 else
426 fp->tf_eflags &= ~PSL_IOPL;
427 }
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