1*27d434e5Sriastradh /* $NetBSD: wsdisplay_compat_usl.c,v 1.54 2021/06/01 23:28:07 riastradh Exp $ */
28fb1d35aSdrochner
38fb1d35aSdrochner /*
48fb1d35aSdrochner * Copyright (c) 1998
58fb1d35aSdrochner * Matthias Drochner. All rights reserved.
68fb1d35aSdrochner *
78fb1d35aSdrochner * Redistribution and use in source and binary forms, with or without
88fb1d35aSdrochner * modification, are permitted provided that the following conditions
98fb1d35aSdrochner * are met:
108fb1d35aSdrochner * 1. Redistributions of source code must retain the above copyright
118fb1d35aSdrochner * notice, this list of conditions and the following disclaimer.
128fb1d35aSdrochner * 2. Redistributions in binary form must reproduce the above copyright
138fb1d35aSdrochner * notice, this list of conditions and the following disclaimer in the
148fb1d35aSdrochner * documentation and/or other materials provided with the distribution.
158fb1d35aSdrochner *
168fb1d35aSdrochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
178fb1d35aSdrochner * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
188fb1d35aSdrochner * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
198fb1d35aSdrochner * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
208fb1d35aSdrochner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
218fb1d35aSdrochner * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
228fb1d35aSdrochner * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
238fb1d35aSdrochner * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
248fb1d35aSdrochner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
258fb1d35aSdrochner * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
268fb1d35aSdrochner *
278fb1d35aSdrochner */
288fb1d35aSdrochner
29139747fbSlukem #include <sys/cdefs.h>
30*27d434e5Sriastradh __KERNEL_RCSID(0, "$NetBSD: wsdisplay_compat_usl.c,v 1.54 2021/06/01 23:28:07 riastradh Exp $");
31139747fbSlukem
32d8e04c90Spooka #ifdef _KERNEL_OPT
331a2cb1b9Sthorpej #include "opt_compat_freebsd.h"
343472ba67Sjonathan #include "opt_compat_netbsd.h"
35d8e04c90Spooka #endif
361a2cb1b9Sthorpej
378fb1d35aSdrochner #include <sys/param.h>
388fb1d35aSdrochner #include <sys/systm.h>
39fc96443dSthorpej #include <sys/callout.h>
408fb1d35aSdrochner #include <sys/ioctl.h>
418fb1d35aSdrochner #include <sys/kernel.h>
424ccb5b37Smaya #include <sys/kmem.h>
438fb1d35aSdrochner #include <sys/proc.h>
448fb1d35aSdrochner #include <sys/signalvar.h>
458fb1d35aSdrochner #include <sys/errno.h>
462867b68bSelad #include <sys/kauth.h>
478fb1d35aSdrochner
488fb1d35aSdrochner #include <dev/wscons/wsconsio.h>
498fb1d35aSdrochner #include <dev/wscons/wsdisplayvar.h>
508fb1d35aSdrochner #include <dev/wscons/wscons_callbacks.h>
518fb1d35aSdrochner #include <dev/wscons/wsdisplay_usl_io.h>
528fb1d35aSdrochner
538fb1d35aSdrochner #include "opt_wsdisplay_compat.h"
548fb1d35aSdrochner
558fb1d35aSdrochner struct usl_syncdata {
568fb1d35aSdrochner struct wsscreen *s_scr;
578fb1d35aSdrochner struct proc *s_proc;
588fb1d35aSdrochner pid_t s_pid;
598fb1d35aSdrochner int s_flags;
608fb1d35aSdrochner #define SF_DETACHPENDING 1
618fb1d35aSdrochner #define SF_ATTACHPENDING 2
628fb1d35aSdrochner int s_acqsig, s_relsig;
638fb1d35aSdrochner int s_frsig; /* unused */
6482e5e6abSaugustss void (*s_callback)(void *, int, int);
658fb1d35aSdrochner void *s_cbarg;
6688ab7da9Sad callout_t s_attach_ch;
6788ab7da9Sad callout_t s_detach_ch;
688fb1d35aSdrochner };
698fb1d35aSdrochner
7082e5e6abSaugustss static int usl_sync_init(struct wsscreen *, struct usl_syncdata **,
7182e5e6abSaugustss struct proc *, int, int, int);
7282e5e6abSaugustss static void usl_sync_done(struct usl_syncdata *);
738644062cSdrochner static int usl_sync_check(void *);
748644062cSdrochner static int usl_sync_check_sig(struct usl_syncdata *, int, int);
7582e5e6abSaugustss static struct usl_syncdata *usl_sync_get(struct wsscreen *);
768fb1d35aSdrochner
7782e5e6abSaugustss static int usl_detachproc(void *, int, void (*)(void *, int, int), void *);
7882e5e6abSaugustss static int usl_detachack(struct usl_syncdata *, int);
7982e5e6abSaugustss static void usl_detachtimeout(void *);
8082e5e6abSaugustss static int usl_attachproc(void *, int, void (*)(void *, int, int), void *);
8182e5e6abSaugustss static int usl_attachack(struct usl_syncdata *, int);
8282e5e6abSaugustss static void usl_attachtimeout(void *);
83*27d434e5Sriastradh static void usl_sync_destroy(void *);
848fb1d35aSdrochner
858fb1d35aSdrochner static const struct wscons_syncops usl_syncops = {
86*27d434e5Sriastradh .detach = usl_detachproc,
87*27d434e5Sriastradh .attach = usl_attachproc,
88*27d434e5Sriastradh .check = usl_sync_check,
89*27d434e5Sriastradh .destroy = usl_sync_destroy,
908fb1d35aSdrochner };
918fb1d35aSdrochner
92b6867c91Sdrochner #ifndef WSCOMPAT_USL_SYNCTIMEOUT
93b6867c91Sdrochner #define WSCOMPAT_USL_SYNCTIMEOUT 5 /* seconds */
94b6867c91Sdrochner #endif
95b6867c91Sdrochner static int wscompat_usl_synctimeout = WSCOMPAT_USL_SYNCTIMEOUT;
96b6867c91Sdrochner
978fb1d35aSdrochner static int
usl_sync_init(struct wsscreen * scr,struct usl_syncdata ** sdp,struct proc * p,int acqsig,int relsig,int frsig)9882e5e6abSaugustss usl_sync_init(struct wsscreen *scr, struct usl_syncdata **sdp,
9982e5e6abSaugustss struct proc *p, int acqsig, int relsig, int frsig)
1008fb1d35aSdrochner {
1018fb1d35aSdrochner struct usl_syncdata *sd;
1028fb1d35aSdrochner int res;
1038fb1d35aSdrochner
10429ca7124Schristos sd = kmem_intr_alloc(sizeof(*sd), KM_SLEEP);
1054ccb5b37Smaya
1068fb1d35aSdrochner sd->s_scr = scr;
1075a3dd224Sdrochner sd->s_proc = p;
1085a3dd224Sdrochner sd->s_pid = p->p_pid;
109b45b8e0fSdrochner sd->s_flags = 0;
1105a3dd224Sdrochner sd->s_acqsig = acqsig;
1115a3dd224Sdrochner sd->s_relsig = relsig;
1125a3dd224Sdrochner sd->s_frsig = frsig;
11388ab7da9Sad callout_init(&sd->s_attach_ch, 0);
11455e50c10Sjoerg callout_setfunc(&sd->s_attach_ch, usl_attachtimeout, sd);
11588ab7da9Sad callout_init(&sd->s_detach_ch, 0);
11655e50c10Sjoerg callout_setfunc(&sd->s_detach_ch, usl_detachtimeout, sd);
1178fb1d35aSdrochner res = wsscreen_attach_sync(scr, &usl_syncops, sd);
1188fb1d35aSdrochner if (res) {
11929ca7124Schristos kmem_intr_free(sd, sizeof(*sd));
12029ca7124Schristos return res;
1218fb1d35aSdrochner }
1228fb1d35aSdrochner *sdp = sd;
12329ca7124Schristos return 0;
1248fb1d35aSdrochner }
1258fb1d35aSdrochner
1268fb1d35aSdrochner static void
usl_sync_done(struct usl_syncdata * sd)12782e5e6abSaugustss usl_sync_done(struct usl_syncdata *sd)
1288fb1d35aSdrochner {
129b45b8e0fSdrochner if (sd->s_flags & SF_DETACHPENDING) {
130fc96443dSthorpej callout_stop(&sd->s_detach_ch);
131309847b3Sdrochner (*sd->s_callback)(sd->s_cbarg, 0, 0);
132b45b8e0fSdrochner }
133309847b3Sdrochner if (sd->s_flags & SF_ATTACHPENDING) {
134fc96443dSthorpej callout_stop(&sd->s_attach_ch);
135309847b3Sdrochner (*sd->s_callback)(sd->s_cbarg, ENXIO, 0);
136309847b3Sdrochner }
1378fb1d35aSdrochner wsscreen_detach_sync(sd->s_scr);
13829ca7124Schristos kmem_intr_free(sd, sizeof(*sd));
1398fb1d35aSdrochner }
1408fb1d35aSdrochner
1418fb1d35aSdrochner static int
usl_sync_check_sig(struct usl_syncdata * sd,int sig,int flags)1428644062cSdrochner usl_sync_check_sig(struct usl_syncdata *sd, int sig, int flags)
1438fb1d35aSdrochner {
1448644062cSdrochner
1450eaaa024Sad mutex_enter(&proc_lock);
1463c507045Srmind if (sd->s_proc == proc_find(sd->s_pid)) {
1478644062cSdrochner sd->s_flags |= flags;
1488644062cSdrochner if (sig)
1498644062cSdrochner psignal(sd->s_proc, sig);
1500eaaa024Sad mutex_exit(&proc_lock);
15129ca7124Schristos return 1;
1528644062cSdrochner }
1530eaaa024Sad mutex_exit(&proc_lock);
1548644062cSdrochner
15529ca7124Schristos printf("%s: process %d died\n", __func__, sd->s_pid);
1568fb1d35aSdrochner usl_sync_done(sd);
15729ca7124Schristos return 0;
1588fb1d35aSdrochner }
1598fb1d35aSdrochner
1608644062cSdrochner static int
usl_sync_check(void * vsd)1618644062cSdrochner usl_sync_check(void *vsd)
1628644062cSdrochner {
1638644062cSdrochner
1648644062cSdrochner struct usl_syncdata *sd = vsd;
1658644062cSdrochner return usl_sync_check_sig(sd, 0, 0);
1668644062cSdrochner }
1678644062cSdrochner
1688fb1d35aSdrochner static struct usl_syncdata *
usl_sync_get(struct wsscreen * scr)16982e5e6abSaugustss usl_sync_get(struct wsscreen *scr)
1708fb1d35aSdrochner {
171e718b57aSfvdl void *sd;
1728fb1d35aSdrochner
173e718b57aSfvdl if (wsscreen_lookup_sync(scr, &usl_syncops, &sd))
17429ca7124Schristos return 0;
17529ca7124Schristos return sd;
1768fb1d35aSdrochner }
1778fb1d35aSdrochner
1788fb1d35aSdrochner static int
usl_detachproc(void * cookie,int waitok,void (* callback)(void *,int,int),void * cbarg)179168cd830Schristos usl_detachproc(void *cookie, int waitok,
1804d595fd7Schristos void (*callback)(void *, int, int), void *cbarg)
1818fb1d35aSdrochner {
1828fb1d35aSdrochner struct usl_syncdata *sd = cookie;
1838fb1d35aSdrochner
1848996199aSdrochner /* we really need a callback */
1858996199aSdrochner if (!callback)
18629ca7124Schristos return EINVAL;
1878996199aSdrochner
1888fb1d35aSdrochner /*
1898fb1d35aSdrochner * Normally, this is called from the controlling process.
1908fb1d35aSdrochner * Is is supposed to reply with a VT_RELDISP ioctl(), so
1918fb1d35aSdrochner * it is not useful to tsleep() here.
1928fb1d35aSdrochner */
1938fb1d35aSdrochner sd->s_callback = callback;
1948fb1d35aSdrochner sd->s_cbarg = cbarg;
19528b27193Schristos if (waitok) {
1968644062cSdrochner if (!usl_sync_check_sig(sd, sd->s_relsig, SF_DETACHPENDING))
19729ca7124Schristos return 0;
19828b27193Schristos }
1998fb1d35aSdrochner
2008644062cSdrochner callout_schedule(&sd->s_detach_ch, wscompat_usl_synctimeout * hz);
20129ca7124Schristos return EAGAIN;
2028fb1d35aSdrochner }
2038fb1d35aSdrochner
2048fb1d35aSdrochner static int
usl_detachack(struct usl_syncdata * sd,int ack)20582e5e6abSaugustss usl_detachack(struct usl_syncdata *sd, int ack)
2068fb1d35aSdrochner {
2078fb1d35aSdrochner if (!(sd->s_flags & SF_DETACHPENDING)) {
20829ca7124Schristos printf("%s: not detaching\n", __func__);
20929ca7124Schristos return EINVAL;
2108fb1d35aSdrochner }
2118fb1d35aSdrochner
212fc96443dSthorpej callout_stop(&sd->s_detach_ch);
2138fb1d35aSdrochner sd->s_flags &= ~SF_DETACHPENDING;
2148fb1d35aSdrochner
215309847b3Sdrochner if (sd->s_callback)
216309847b3Sdrochner (*sd->s_callback)(sd->s_cbarg, (ack ? 0 : EIO), 1);
2178fb1d35aSdrochner
21829ca7124Schristos return 0;
2198fb1d35aSdrochner }
2208fb1d35aSdrochner
2218fb1d35aSdrochner static void
usl_detachtimeout(void * arg)22282e5e6abSaugustss usl_detachtimeout(void *arg)
2238fb1d35aSdrochner {
2248fb1d35aSdrochner struct usl_syncdata *sd = arg;
2258fb1d35aSdrochner
22629ca7124Schristos printf("%s\n", __func__);
2278fb1d35aSdrochner
2288fb1d35aSdrochner if (!(sd->s_flags & SF_DETACHPENDING)) {
22929ca7124Schristos printf("%s: not detaching\n", __func__);
2308fb1d35aSdrochner return;
2318fb1d35aSdrochner }
2328fb1d35aSdrochner
2338fb1d35aSdrochner sd->s_flags &= ~SF_DETACHPENDING;
234309847b3Sdrochner
235309847b3Sdrochner if (sd->s_callback)
236309847b3Sdrochner (*sd->s_callback)(sd->s_cbarg, EIO, 0);
237309847b3Sdrochner
2388fb1d35aSdrochner (void) usl_sync_check(sd);
2398fb1d35aSdrochner }
2408fb1d35aSdrochner
2418fb1d35aSdrochner static int
usl_attachproc(void * cookie,int waitok,void (* callback)(void *,int,int),void * cbarg)242168cd830Schristos usl_attachproc(void *cookie, int waitok,
2434d595fd7Schristos void (*callback)(void *, int, int), void *cbarg)
2448fb1d35aSdrochner {
2458fb1d35aSdrochner struct usl_syncdata *sd = cookie;
2468fb1d35aSdrochner
2478996199aSdrochner /* we really need a callback */
2488996199aSdrochner if (!callback)
24929ca7124Schristos return EINVAL;
2508996199aSdrochner
251309847b3Sdrochner sd->s_callback = callback;
252309847b3Sdrochner sd->s_cbarg = cbarg;
2538644062cSdrochner if (!usl_sync_check_sig(sd, sd->s_acqsig, SF_ATTACHPENDING))
25429ca7124Schristos return 0;
2558fb1d35aSdrochner
2568644062cSdrochner callout_schedule(&sd->s_attach_ch, wscompat_usl_synctimeout * hz);
25729ca7124Schristos return EAGAIN;
2588fb1d35aSdrochner }
2598fb1d35aSdrochner
2608fb1d35aSdrochner static int
usl_attachack(struct usl_syncdata * sd,int ack)26182e5e6abSaugustss usl_attachack(struct usl_syncdata *sd, int ack)
2628fb1d35aSdrochner {
2638fb1d35aSdrochner if (!(sd->s_flags & SF_ATTACHPENDING)) {
26429ca7124Schristos printf("%s: not attaching\n", __func__);
26529ca7124Schristos return EINVAL;
2668fb1d35aSdrochner }
2678fb1d35aSdrochner
268fc96443dSthorpej callout_stop(&sd->s_attach_ch);
2698fb1d35aSdrochner sd->s_flags &= ~SF_ATTACHPENDING;
270309847b3Sdrochner
271309847b3Sdrochner if (sd->s_callback)
272309847b3Sdrochner (*sd->s_callback)(sd->s_cbarg, (ack ? 0 : EIO), 1);
273309847b3Sdrochner
27429ca7124Schristos return 0;
2758fb1d35aSdrochner }
2768fb1d35aSdrochner
2778fb1d35aSdrochner static void
usl_attachtimeout(void * arg)27882e5e6abSaugustss usl_attachtimeout(void *arg)
2798fb1d35aSdrochner {
2808fb1d35aSdrochner struct usl_syncdata *sd = arg;
2818fb1d35aSdrochner
28229ca7124Schristos printf("%s\n", __func__);
2838fb1d35aSdrochner
2848fb1d35aSdrochner if (!(sd->s_flags & SF_ATTACHPENDING)) {
28529ca7124Schristos printf("%s: not attaching\n", __func__);
2868fb1d35aSdrochner return;
2878fb1d35aSdrochner }
2888fb1d35aSdrochner
2898fb1d35aSdrochner sd->s_flags &= ~SF_ATTACHPENDING;
290309847b3Sdrochner
291309847b3Sdrochner if (sd->s_callback)
292309847b3Sdrochner (*sd->s_callback)(sd->s_cbarg, EIO, 0);
293309847b3Sdrochner
2948fb1d35aSdrochner (void) usl_sync_check(sd);
2958fb1d35aSdrochner }
2968fb1d35aSdrochner
297*27d434e5Sriastradh static void
usl_sync_destroy(void * cookie)298*27d434e5Sriastradh usl_sync_destroy(void *cookie)
299*27d434e5Sriastradh {
300*27d434e5Sriastradh struct usl_syncdata *sd = cookie;
301*27d434e5Sriastradh
302*27d434e5Sriastradh usl_sync_done(sd);
303*27d434e5Sriastradh }
304*27d434e5Sriastradh
3058fb1d35aSdrochner int
wsdisplay_usl_ioctl1(device_t dv,u_long cmd,void * data,int flag,struct lwp * l)306e4a56b25Sjoerg wsdisplay_usl_ioctl1(device_t dv, u_long cmd, void *data,
307168cd830Schristos int flag, struct lwp *l)
30802bd5fa8Smycroft {
309e4a56b25Sjoerg struct wsdisplay_softc *sc = device_private(dv);
31002bd5fa8Smycroft int idx, maxidx;
31102bd5fa8Smycroft
31202bd5fa8Smycroft switch (cmd) {
31302bd5fa8Smycroft case VT_OPENQRY:
31402bd5fa8Smycroft maxidx = wsdisplay_maxscreenidx(sc);
31502bd5fa8Smycroft for (idx = 0; idx <= maxidx; idx++) {
31602bd5fa8Smycroft if (wsdisplay_screenstate(sc, idx) == 0) {
31702bd5fa8Smycroft *(int *)data = idx + 1;
31829ca7124Schristos return 0;
31902bd5fa8Smycroft }
32002bd5fa8Smycroft }
32129ca7124Schristos return ENXIO;
32202bd5fa8Smycroft case VT_GETACTIVE:
32302bd5fa8Smycroft idx = wsdisplay_getactivescreen(sc);
32402bd5fa8Smycroft *(int *)data = idx + 1;
32529ca7124Schristos return 0;
32602bd5fa8Smycroft case VT_ACTIVATE:
327826e1b36Smacallan /*
328826e1b36Smacallan * a gross and disgusting hack to make this abused up ioctl,
329826e1b36Smacallan * which is a gross and disgusting hack on its own, work on
330826e1b36Smacallan * LP64/BE - we want the lower 32bit so we simply dereference
331826e1b36Smacallan * the argument pointer as long. May cause problems with 32bit
332826e1b36Smacallan * kernels on sparc64?
333826e1b36Smacallan */
334826e1b36Smacallan
335826e1b36Smacallan idx = *(long *)data - 1;
336ed9f910cStakemura if (idx < 0)
33729ca7124Schristos return EINVAL;
33829ca7124Schristos return wsdisplay_switch(dv, idx, 1);
33902bd5fa8Smycroft case VT_WAITACTIVE:
340826e1b36Smacallan idx = *(long *)data - 1;
341ed9f910cStakemura if (idx < 0)
34229ca7124Schristos return EINVAL;
34329ca7124Schristos return wsscreen_switchwait(sc, idx);
34402bd5fa8Smycroft case VT_GETSTATE:
34502bd5fa8Smycroft #define ss ((struct vt_stat *)data)
34602bd5fa8Smycroft idx = wsdisplay_getactivescreen(sc);
34702bd5fa8Smycroft ss->v_active = idx + 1;
34802bd5fa8Smycroft ss->v_state = 0;
34902bd5fa8Smycroft maxidx = wsdisplay_maxscreenidx(sc);
35002bd5fa8Smycroft for (idx = 0; idx <= maxidx; idx++)
35102bd5fa8Smycroft if (wsdisplay_screenstate(sc, idx) == EBUSY)
35202bd5fa8Smycroft ss->v_state |= (1 << (idx + 1));
3539eba7eabSmartin #undef ss
35429ca7124Schristos return 0;
35502bd5fa8Smycroft
35602bd5fa8Smycroft #ifdef WSDISPLAY_COMPAT_PCVT
35702bd5fa8Smycroft case VGAPCVTID:
35802bd5fa8Smycroft #define id ((struct pcvtid *)data)
359db9226c7Sitojun strlcpy(id->name, "pcvt", sizeof(id->name));
36002bd5fa8Smycroft id->rmajor = 3;
36102bd5fa8Smycroft id->rminor = 32;
36202bd5fa8Smycroft #undef id
36329ca7124Schristos return 0;
36402bd5fa8Smycroft #endif
36502bd5fa8Smycroft #ifdef WSDISPLAY_COMPAT_SYSCONS
36602bd5fa8Smycroft case CONS_GETVERS:
36702bd5fa8Smycroft *(int *)data = 0x200; /* version 2.0 */
36829ca7124Schristos return 0;
36902bd5fa8Smycroft #endif
37002bd5fa8Smycroft
37102bd5fa8Smycroft default:
37229ca7124Schristos return EPASSTHROUGH;
37302bd5fa8Smycroft }
37402bd5fa8Smycroft }
37502bd5fa8Smycroft
37602bd5fa8Smycroft int
wsdisplay_usl_ioctl2(struct wsdisplay_softc * sc,struct wsscreen * scr,u_long cmd,void * data,int flag,struct lwp * l)37782e5e6abSaugustss wsdisplay_usl_ioctl2(struct wsdisplay_softc *sc, struct wsscreen *scr,
37853524e44Schristos u_long cmd, void *data, int flag, struct lwp *l)
3798fb1d35aSdrochner {
38095e1ffb1Schristos struct proc *p = l->l_proc;
381a737ed39Sjmcneill int intarg = 0, res;
382665eca96Ssimonb u_long req;
3837381ac70Ssimonb void *arg;
3848fb1d35aSdrochner struct usl_syncdata *sd;
3858fb1d35aSdrochner struct wskbd_bell_data bd;
3868fb1d35aSdrochner
3878fb1d35aSdrochner switch (cmd) {
3888fb1d35aSdrochner case VT_SETMODE:
3898fb1d35aSdrochner #define newmode ((struct vt_mode *)data)
3908fb1d35aSdrochner if (newmode->mode == VT_PROCESS) {
3915a3dd224Sdrochner res = usl_sync_init(scr, &sd, p, newmode->acqsig,
3925a3dd224Sdrochner newmode->relsig, newmode->frsig);
3938fb1d35aSdrochner if (res)
39429ca7124Schristos return res;
3958fb1d35aSdrochner } else {
3968fb1d35aSdrochner sd = usl_sync_get(scr);
3978fb1d35aSdrochner if (sd)
3988fb1d35aSdrochner usl_sync_done(sd);
3998fb1d35aSdrochner }
4008fb1d35aSdrochner #undef newmode
40129ca7124Schristos return 0;
4028fb1d35aSdrochner case VT_GETMODE:
4038fb1d35aSdrochner #define cmode ((struct vt_mode *)data)
4048fb1d35aSdrochner sd = usl_sync_get(scr);
4058fb1d35aSdrochner if (sd) {
4068fb1d35aSdrochner cmode->mode = VT_PROCESS;
4078fb1d35aSdrochner cmode->relsig = sd->s_relsig;
4088fb1d35aSdrochner cmode->acqsig = sd->s_acqsig;
4098fb1d35aSdrochner cmode->frsig = sd->s_frsig;
4108fb1d35aSdrochner } else
4118fb1d35aSdrochner cmode->mode = VT_AUTO;
4128fb1d35aSdrochner #undef cmode
41329ca7124Schristos return 0;
4148fb1d35aSdrochner case VT_RELDISP:
415826e1b36Smacallan #define d (*(long *)data)
4168fb1d35aSdrochner sd = usl_sync_get(scr);
4178fb1d35aSdrochner if (!sd)
41829ca7124Schristos return EINVAL;
4198fb1d35aSdrochner switch (d) {
4208fb1d35aSdrochner case VT_FALSE:
4218fb1d35aSdrochner case VT_TRUE:
42229ca7124Schristos return usl_detachack(sd, (d == VT_TRUE));
4238fb1d35aSdrochner case VT_ACKACQ:
42429ca7124Schristos return usl_attachack(sd, 1);
4258fb1d35aSdrochner default:
42629ca7124Schristos return EINVAL;
4278fb1d35aSdrochner }
4288fb1d35aSdrochner #undef d
42902bd5fa8Smycroft
4308fb1d35aSdrochner case KDENABIO:
4317cd2ca16Sbjh21 #if defined(__i386__) && (defined(COMPAT_11) || defined(COMPAT_FREEBSD))
4320ffd2bf3Selad if (kauth_authorize_machdep(l->l_cred, KAUTH_MACHDEP_IOPL,
4330ffd2bf3Selad NULL, NULL, NULL, NULL) != 0)
43429ca7124Schristos return EPERM;
43508544bdaSelad #endif
4368fb1d35aSdrochner /* FALLTHRU */
4378fb1d35aSdrochner case KDDISABIO:
4387cd2ca16Sbjh21 #if defined(__i386__) && (defined(COMPAT_11) || defined(COMPAT_FREEBSD))
4398fb1d35aSdrochner {
440c62a74e6Sthorpej /* XXX NJWLWP */
441c62a74e6Sthorpej struct trapframe *fp = (struct trapframe *)curlwp->l_md.md_regs;
4428fb1d35aSdrochner if (cmd == KDENABIO)
4438fb1d35aSdrochner fp->tf_eflags |= PSL_IOPL;
4448fb1d35aSdrochner else
4458fb1d35aSdrochner fp->tf_eflags &= ~PSL_IOPL;
4468fb1d35aSdrochner }
4478fb1d35aSdrochner #endif
44829ca7124Schristos return 0;
4498fb1d35aSdrochner case KDSETRAD:
4508fb1d35aSdrochner /* XXX ignore for now */
45129ca7124Schristos return 0;
4528fb1d35aSdrochner
4538fb1d35aSdrochner default:
45429ca7124Schristos return EPASSTHROUGH;
4558fb1d35aSdrochner
4568fb1d35aSdrochner /*
4578fb1d35aSdrochner * the following are converted to wsdisplay ioctls
4588fb1d35aSdrochner */
4598fb1d35aSdrochner case KDSETMODE:
4608fb1d35aSdrochner req = WSDISPLAYIO_SMODE;
4614e198487Smacallan #define d (*(long *)data)
4628fb1d35aSdrochner switch (d) {
4638fb1d35aSdrochner case KD_GRAPHICS:
4648fb1d35aSdrochner intarg = WSDISPLAYIO_MODE_MAPPED;
4658fb1d35aSdrochner break;
4668fb1d35aSdrochner case KD_TEXT:
4678fb1d35aSdrochner intarg = WSDISPLAYIO_MODE_EMUL;
4688fb1d35aSdrochner break;
4698fb1d35aSdrochner default:
47029ca7124Schristos return EINVAL;
4718fb1d35aSdrochner }
4728fb1d35aSdrochner #undef d
4738fb1d35aSdrochner arg = &intarg;
4748fb1d35aSdrochner break;
4758fb1d35aSdrochner case KDMKTONE:
4768fb1d35aSdrochner req = WSKBDIO_COMPLEXBELL;
4774e198487Smacallan #define d (*(long *)data)
4788fb1d35aSdrochner if (d) {
4798567d6caSchristos #define PCVT_SYSBEEPF 1193182
4808567d6caSchristos if (d >> 16) {
4818567d6caSchristos bd.which = WSKBD_BELL_DOPERIOD;
4828fb1d35aSdrochner bd.period = d >> 16; /* ms */
4838567d6caSchristos }
484b4bf637aSchristos else
485b4bf637aSchristos bd.which = 0;
4868567d6caSchristos if (d & 0xffff) {
4878567d6caSchristos bd.which |= WSKBD_BELL_DOPITCH;
4888567d6caSchristos bd.pitch = PCVT_SYSBEEPF/(d & 0xffff); /* Hz */
4898567d6caSchristos }
4908fb1d35aSdrochner } else
4918fb1d35aSdrochner bd.which = 0; /* default */
4928fb1d35aSdrochner #undef d
4938fb1d35aSdrochner arg = &bd;
4948fb1d35aSdrochner break;
4958fb1d35aSdrochner case KDSETLED:
4968fb1d35aSdrochner req = WSKBDIO_SETLEDS;
4978fb1d35aSdrochner intarg = 0;
4984e198487Smacallan #define d (*(long *)data)
4998fb1d35aSdrochner if (d & LED_CAP)
5008fb1d35aSdrochner intarg |= WSKBD_LED_CAPS;
5018fb1d35aSdrochner if (d & LED_NUM)
5028fb1d35aSdrochner intarg |= WSKBD_LED_NUM;
5038fb1d35aSdrochner if (d & LED_SCR)
5048fb1d35aSdrochner intarg |= WSKBD_LED_SCROLL;
5058fb1d35aSdrochner #undef d
5068fb1d35aSdrochner arg = &intarg;
5078fb1d35aSdrochner break;
5088fb1d35aSdrochner case KDGETLED:
5098fb1d35aSdrochner req = WSKBDIO_GETLEDS;
5108fb1d35aSdrochner arg = &intarg;
5118fb1d35aSdrochner break;
5128fb1d35aSdrochner #ifdef WSDISPLAY_COMPAT_RAWKBD
5138fb1d35aSdrochner case KDSKBMODE:
5148fb1d35aSdrochner req = WSKBDIO_SETMODE;
5154e198487Smacallan switch (*(long *)data) {
5168fb1d35aSdrochner case K_RAW:
5178fb1d35aSdrochner intarg = WSKBD_RAW;
5188fb1d35aSdrochner break;
5198fb1d35aSdrochner case K_XLATE:
5208fb1d35aSdrochner intarg = WSKBD_TRANSLATED;
5218fb1d35aSdrochner break;
5228fb1d35aSdrochner default:
52329ca7124Schristos return EINVAL;
5248fb1d35aSdrochner }
5258fb1d35aSdrochner arg = &intarg;
5268fb1d35aSdrochner break;
5278fb1d35aSdrochner case KDGKBMODE:
5288fb1d35aSdrochner req = WSKBDIO_GETMODE;
5298fb1d35aSdrochner arg = &intarg;
5308fb1d35aSdrochner break;
5318fb1d35aSdrochner #endif
5328fb1d35aSdrochner }
5338fb1d35aSdrochner
53495e1ffb1Schristos res = wsdisplay_internal_ioctl(sc, scr, req, arg, flag, l);
53531144d99Satatat if (res != EPASSTHROUGH)
53629ca7124Schristos return res;
5378fb1d35aSdrochner
5388fb1d35aSdrochner switch (cmd) {
5398fb1d35aSdrochner case KDGETLED:
5408fb1d35aSdrochner #define d (*(int *)data)
5418fb1d35aSdrochner d = 0;
5428fb1d35aSdrochner if (intarg & WSKBD_LED_CAPS)
5438fb1d35aSdrochner d |= LED_CAP;
5448fb1d35aSdrochner if (intarg & WSKBD_LED_NUM)
5458fb1d35aSdrochner d |= LED_NUM;
5468fb1d35aSdrochner if (intarg & WSKBD_LED_SCROLL)
5478fb1d35aSdrochner d |= LED_SCR;
5488fb1d35aSdrochner #undef d
5498fb1d35aSdrochner break;
5508fb1d35aSdrochner #ifdef WSDISPLAY_COMPAT_RAWKBD
5518fb1d35aSdrochner case KDGKBMODE:
5528fb1d35aSdrochner *(int *)data = (intarg == WSKBD_RAW ? K_RAW : K_XLATE);
5538fb1d35aSdrochner break;
5548fb1d35aSdrochner #endif
5558fb1d35aSdrochner }
5568fb1d35aSdrochner
55729ca7124Schristos return 0;
5588fb1d35aSdrochner }
559