wsdisplay_compat_usl.c revision 1.41.8.1 1 /* $NetBSD: wsdisplay_compat_usl.c,v 1.41.8.1 2007/11/06 23:30:51 matt 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.41.8.1 2007/11/06 23:30:51 matt 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 callout_t s_attach_ch;
65 callout_t 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, 0);
113 callout_setfunc(&sd->s_attach_ch, usl_attachtimeout, sd);
114 callout_init(&sd->s_detach_ch, 0);
115 callout_setfunc(&sd->s_detach_ch, usl_detachtimeout, sd);
116 res = wsscreen_attach_sync(scr, &usl_syncops, sd);
117 if (res) {
118 free(sd, M_DEVBUF);
119 return (res);
120 }
121 *sdp = sd;
122 return (0);
123 }
124
125 static void
126 usl_sync_done(struct usl_syncdata *sd)
127 {
128 if (sd->s_flags & SF_DETACHPENDING) {
129 callout_stop(&sd->s_detach_ch);
130 (*sd->s_callback)(sd->s_cbarg, 0, 0);
131 }
132 if (sd->s_flags & SF_ATTACHPENDING) {
133 callout_stop(&sd->s_attach_ch);
134 (*sd->s_callback)(sd->s_cbarg, ENXIO, 0);
135 }
136 wsscreen_detach_sync(sd->s_scr);
137 free(sd, M_DEVBUF);
138 }
139
140 static int
141 usl_sync_check(struct usl_syncdata *sd)
142 {
143 int rv;
144 mutex_enter(&proclist_mutex); /* XXXSMP */
145 rv = (sd->s_proc == p_find(sd->s_pid, PFIND_LOCKED));
146 mutex_exit(&proclist_mutex); /* XXXSMP */
147 if (rv)
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 void *sd;
158
159 if (wsscreen_lookup_sync(scr, &usl_syncops, &sd))
160 return (0);
161 return (struct usl_syncdata *)sd;
162 }
163
164 static int
165 usl_detachproc(void *cookie, int waitok,
166 void (*callback)(void *, int, int), 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 mutex_enter(&proclist_mutex);
186 psignal(sd->s_proc, sd->s_relsig);
187 mutex_exit(&proclist_mutex);
188 callout_schedule(&sd->s_detach_ch, wscompat_usl_synctimeout * hz);
189
190 return (EAGAIN);
191 }
192
193 static int
194 usl_detachack(struct usl_syncdata *sd, int ack)
195 {
196 if (!(sd->s_flags & SF_DETACHPENDING)) {
197 printf("usl_detachack: not detaching\n");
198 return (EINVAL);
199 }
200
201 callout_stop(&sd->s_detach_ch);
202 sd->s_flags &= ~SF_DETACHPENDING;
203
204 if (sd->s_callback)
205 (*sd->s_callback)(sd->s_cbarg, (ack ? 0 : EIO), 1);
206
207 return (0);
208 }
209
210 static void
211 usl_detachtimeout(void *arg)
212 {
213 struct usl_syncdata *sd = arg;
214
215 printf("usl_detachtimeout\n");
216
217 if (!(sd->s_flags & SF_DETACHPENDING)) {
218 printf("usl_detachtimeout: not detaching\n");
219 return;
220 }
221
222 sd->s_flags &= ~SF_DETACHPENDING;
223
224 if (sd->s_callback)
225 (*sd->s_callback)(sd->s_cbarg, EIO, 0);
226
227 (void) usl_sync_check(sd);
228 }
229
230 static int
231 usl_attachproc(void *cookie, int waitok,
232 void (*callback)(void *, int, int), void *cbarg)
233 {
234 struct usl_syncdata *sd = cookie;
235
236 if (!usl_sync_check(sd))
237 return (0);
238
239 /* we really need a callback */
240 if (!callback)
241 return (EINVAL);
242
243 sd->s_callback = callback;
244 sd->s_cbarg = cbarg;
245 sd->s_flags |= SF_ATTACHPENDING;
246 mutex_enter(&proclist_mutex);
247 psignal(sd->s_proc, sd->s_acqsig);
248 mutex_exit(&proclist_mutex);
249 callout_schedule(&sd->s_attach_ch, wscompat_usl_synctimeout * hz);
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(device_t dv, u_long cmd, void *data,
293 int flag, struct lwp *l)
294 {
295 struct wsdisplay_softc *sc = device_private(dv);
296 int idx, maxidx;
297
298 switch (cmd) {
299 case VT_OPENQRY:
300 maxidx = wsdisplay_maxscreenidx(sc);
301 for (idx = 0; idx <= maxidx; idx++) {
302 if (wsdisplay_screenstate(sc, idx) == 0) {
303 *(int *)data = idx + 1;
304 return (0);
305 }
306 }
307 return (ENXIO);
308 case VT_GETACTIVE:
309 idx = wsdisplay_getactivescreen(sc);
310 *(int *)data = idx + 1;
311 return (0);
312 case VT_ACTIVATE:
313 /*
314 * a gross and disgusting hack to make this abused up ioctl,
315 * which is a gross and disgusting hack on its own, work on
316 * LP64/BE - we want the lower 32bit so we simply dereference
317 * the argument pointer as long. May cause problems with 32bit
318 * kernels on sparc64?
319 */
320
321 idx = *(long *)data - 1;
322 if (idx < 0)
323 return (EINVAL);
324 return (wsdisplay_switch(dv, idx, 1));
325 case VT_WAITACTIVE:
326 idx = *(long *)data - 1;
327 if (idx < 0)
328 return (EINVAL);
329 return (wsscreen_switchwait(sc, idx));
330 case VT_GETSTATE:
331 #define ss ((struct vt_stat *)data)
332 idx = wsdisplay_getactivescreen(sc);
333 ss->v_active = idx + 1;
334 ss->v_state = 0;
335 maxidx = wsdisplay_maxscreenidx(sc);
336 for (idx = 0; idx <= maxidx; idx++)
337 if (wsdisplay_screenstate(sc, idx) == EBUSY)
338 ss->v_state |= (1 << (idx + 1));
339 #undef ss
340 return (0);
341
342 #ifdef WSDISPLAY_COMPAT_PCVT
343 case VGAPCVTID:
344 #define id ((struct pcvtid *)data)
345 strlcpy(id->name, "pcvt", sizeof(id->name));
346 id->rmajor = 3;
347 id->rminor = 32;
348 #undef id
349 return (0);
350 #endif
351 #ifdef WSDISPLAY_COMPAT_SYSCONS
352 case CONS_GETVERS:
353 *(int *)data = 0x200; /* version 2.0 */
354 return (0);
355 #endif
356
357 default:
358 return (EPASSTHROUGH);
359 }
360 }
361
362 int
363 wsdisplay_usl_ioctl2(struct wsdisplay_softc *sc, struct wsscreen *scr,
364 u_long cmd, void *data, int flag, struct lwp *l)
365 {
366 struct proc *p = l->l_proc;
367 int intarg = 0, res;
368 u_long req;
369 void *arg;
370 struct usl_syncdata *sd;
371 struct wskbd_bell_data bd;
372
373 switch (cmd) {
374 case VT_SETMODE:
375 #define newmode ((struct vt_mode *)data)
376 if (newmode->mode == VT_PROCESS) {
377 res = usl_sync_init(scr, &sd, p, newmode->acqsig,
378 newmode->relsig, newmode->frsig);
379 if (res)
380 return (res);
381 } else {
382 sd = usl_sync_get(scr);
383 if (sd)
384 usl_sync_done(sd);
385 }
386 #undef newmode
387 return (0);
388 case VT_GETMODE:
389 #define cmode ((struct vt_mode *)data)
390 sd = usl_sync_get(scr);
391 if (sd) {
392 cmode->mode = VT_PROCESS;
393 cmode->relsig = sd->s_relsig;
394 cmode->acqsig = sd->s_acqsig;
395 cmode->frsig = sd->s_frsig;
396 } else
397 cmode->mode = VT_AUTO;
398 #undef cmode
399 return (0);
400 case VT_RELDISP:
401 #define d (*(long *)data)
402 sd = usl_sync_get(scr);
403 if (!sd)
404 return (EINVAL);
405 switch (d) {
406 case VT_FALSE:
407 case VT_TRUE:
408 return (usl_detachack(sd, (d == VT_TRUE)));
409 case VT_ACKACQ:
410 return (usl_attachack(sd, 1));
411 default:
412 return (EINVAL);
413 }
414 #undef d
415
416 case KDENABIO:
417 #if defined(__i386__) && (defined(COMPAT_11) || defined(COMPAT_FREEBSD))
418 if (kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
419 NULL, NULL, NULL, NULL) != 0)
420 return (EPERM);
421 #endif
422 /* FALLTHRU */
423 case KDDISABIO:
424 #if defined(__i386__) && (defined(COMPAT_11) || defined(COMPAT_FREEBSD))
425 {
426 /* XXX NJWLWP */
427 struct trapframe *fp = (struct trapframe *)curlwp->l_md.md_regs;
428 if (cmd == KDENABIO)
429 fp->tf_eflags |= PSL_IOPL;
430 else
431 fp->tf_eflags &= ~PSL_IOPL;
432 }
433 #endif
434 return (0);
435 case KDSETRAD:
436 /* XXX ignore for now */
437 return (0);
438
439 default:
440 return (EPASSTHROUGH);
441
442 /*
443 * the following are converted to wsdisplay ioctls
444 */
445 case KDSETMODE:
446 req = WSDISPLAYIO_SMODE;
447 #define d (*(int *)data)
448 switch (d) {
449 case KD_GRAPHICS:
450 intarg = WSDISPLAYIO_MODE_MAPPED;
451 break;
452 case KD_TEXT:
453 intarg = WSDISPLAYIO_MODE_EMUL;
454 break;
455 default:
456 return (EINVAL);
457 }
458 #undef d
459 arg = &intarg;
460 break;
461 case KDMKTONE:
462 req = WSKBDIO_COMPLEXBELL;
463 #define d (*(int *)data)
464 if (d) {
465 #define PCVT_SYSBEEPF 1193182
466 if (d >> 16) {
467 bd.which = WSKBD_BELL_DOPERIOD;
468 bd.period = d >> 16; /* ms */
469 }
470 else
471 bd.which = 0;
472 if (d & 0xffff) {
473 bd.which |= WSKBD_BELL_DOPITCH;
474 bd.pitch = PCVT_SYSBEEPF/(d & 0xffff); /* Hz */
475 }
476 } else
477 bd.which = 0; /* default */
478 #undef d
479 arg = &bd;
480 break;
481 case KDSETLED:
482 req = WSKBDIO_SETLEDS;
483 intarg = 0;
484 #define d (*(int *)data)
485 if (d & LED_CAP)
486 intarg |= WSKBD_LED_CAPS;
487 if (d & LED_NUM)
488 intarg |= WSKBD_LED_NUM;
489 if (d & LED_SCR)
490 intarg |= WSKBD_LED_SCROLL;
491 #undef d
492 arg = &intarg;
493 break;
494 case KDGETLED:
495 req = WSKBDIO_GETLEDS;
496 arg = &intarg;
497 break;
498 #ifdef WSDISPLAY_COMPAT_RAWKBD
499 case KDSKBMODE:
500 req = WSKBDIO_SETMODE;
501 switch (*(int *)data) {
502 case K_RAW:
503 intarg = WSKBD_RAW;
504 break;
505 case K_XLATE:
506 intarg = WSKBD_TRANSLATED;
507 break;
508 default:
509 return (EINVAL);
510 }
511 arg = &intarg;
512 break;
513 case KDGKBMODE:
514 req = WSKBDIO_GETMODE;
515 arg = &intarg;
516 break;
517 #endif
518 }
519
520 res = wsdisplay_internal_ioctl(sc, scr, req, arg, flag, l);
521 if (res != EPASSTHROUGH)
522 return (res);
523
524 switch (cmd) {
525 case KDGETLED:
526 #define d (*(int *)data)
527 d = 0;
528 if (intarg & WSKBD_LED_CAPS)
529 d |= LED_CAP;
530 if (intarg & WSKBD_LED_NUM)
531 d |= LED_NUM;
532 if (intarg & WSKBD_LED_SCROLL)
533 d |= LED_SCR;
534 #undef d
535 break;
536 #ifdef WSDISPLAY_COMPAT_RAWKBD
537 case KDGKBMODE:
538 *(int *)data = (intarg == WSKBD_RAW ? K_RAW : K_XLATE);
539 break;
540 #endif
541 }
542
543 return (0);
544 }
545