1 /*        $NetBSD: ite_compat.c,v 1.14 2023/03/26 15:12:34 andvar Exp $         */
2 
3 /*
4  * Copyright (C) 2000 Scott Reynolds
5  * 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  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * The main thing to realize about this emulator is that the old console
32  * emulator was largely compatible with the DEC VT-220.  Since the
33  * wsdisplay driver has a more complete emulation of that terminal, it's
34  * reasonable to pass virtually everything up to that driver without
35  * modification.
36  */
37 
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: ite_compat.c,v 1.14 2023/03/26 15:12:34 andvar Exp $");
40 
41 #include "ite.h"
42 #include "wsdisplay.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/conf.h>
47 #include <sys/device.h>
48 #include <sys/ioctl.h>
49 #include <sys/ttycom.h>
50 
51 #include <dev/cons.h>
52 
53 #include <machine/cpu.h>
54 #include <machine/iteioctl.h>
55 
56 #include "ioconf.h"
57 
58 dev_type_open(iteopen);
59 dev_type_close(iteclose);
60 dev_type_read(iteread);
61 dev_type_write(itewrite);
62 dev_type_ioctl(iteioctl);
63 dev_type_tty(itetty);
64 dev_type_poll(itepoll);
65 dev_type_kqfilter(itekqfilter);
66 
67 const struct cdevsw ite_cdevsw = {
68           .d_open = iteopen,
69           .d_close = iteclose,
70           .d_read = iteread,
71           .d_write = itewrite,
72           .d_ioctl = iteioctl,
73           .d_stop = nostop,
74           .d_tty = itetty,
75           .d_poll = itepoll,
76           .d_mmap = nommap,
77           .d_kqfilter = itekqfilter,
78           .d_discard = nodiscard,
79           .d_flag = D_TTY
80 };
81 
82 #if NWSDISPLAY > 0
83 extern const struct cdevsw wsdisplay_cdevsw;
84 #endif
85 
86 static int          ite_initted = 0;
87 static int          ite_bell_freq = 1880;
88 static int          ite_bell_length = 10;
89 static int          ite_bell_volume = 100;
90 
91 
92 /*ARGSUSED*/
93 void
iteattach(int n)94 iteattach(int n)
95 {
96 #if NWSDISPLAY > 0
97           int maj;
98 
99           maj = cdevsw_lookup_major(&wsdisplay_cdevsw);
100           KASSERT(maj != -1);
101 
102           if (maj != major(cn_tab->cn_dev))
103                     return;
104 
105           ite_initted = 1;
106 #endif
107 }
108 
109 /*
110  * Tty handling functions
111  */
112 
113 /*ARGSUSED*/
114 int
iteopen(dev_t dev,int mode,int devtype,struct lwp * l)115 iteopen(dev_t dev, int mode, int devtype, struct lwp *l)
116 {
117           return ite_initted ? (0) : (ENXIO);
118 }
119 
120 /*ARGSUSED*/
121 int
iteclose(dev_t dev,int flag,int mode,struct lwp * l)122 iteclose(dev_t dev, int flag, int mode, struct lwp *l)
123 {
124           return ite_initted ? (0) : (ENXIO);
125 }
126 
127 /*ARGSUSED*/
128 int
iteread(dev_t dev,struct uio * uio,int flag)129 iteread(dev_t dev, struct uio *uio, int flag)
130 {
131           return ite_initted ?
132               (*wsdisplay_cdevsw.d_read)(cn_tab->cn_dev, uio, flag) : (ENXIO);
133 }
134 
135 /*ARGSUSED*/
136 int
itewrite(dev_t dev,struct uio * uio,int flag)137 itewrite(dev_t dev, struct uio *uio, int flag)
138 {
139           return ite_initted ?
140               (*wsdisplay_cdevsw.d_write)(cn_tab->cn_dev, uio, flag) : (ENXIO);
141 }
142 
143 /*ARGSUSED*/
144 struct tty *
itetty(dev_t dev)145 itetty(dev_t dev)
146 {
147           return ite_initted ? (*wsdisplay_cdevsw.d_tty)(cn_tab->cn_dev) : (NULL);
148 }
149 
150 /*ARGSUSED*/
151 int
iteioctl(dev_t dev,u_long cmd,void * addr,int flag,struct lwp * l)152 iteioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
153 {
154           if (!ite_initted)
155                     return (ENXIO);
156 
157           switch (cmd) {
158           case ITEIOC_RINGBELL:
159                     return mac68k_ring_bell(ite_bell_freq,
160                         ite_bell_length, ite_bell_volume);
161           case ITEIOC_SETBELL:
162                     {
163                               struct bellparams *bp = (void *)addr;
164 
165                               /* Do some sanity checks. */
166                               if (bp->freq < 10 || bp->freq > 40000)
167                                         return (EINVAL);
168                               if (bp->len < 0 || bp->len > 3600)
169                                         return (EINVAL);
170                               if (bp->vol < 0 || bp->vol > 100)
171                                         return (EINVAL);
172 
173                               ite_bell_freq = bp->freq;
174                               ite_bell_length = bp->len;
175                               ite_bell_volume = bp->vol;
176                               return (0);
177                     }
178           case ITEIOC_GETBELL:
179                     {
180                               struct bellparams *bp = (void *)addr;
181 
182                               ite_bell_freq = bp->freq;
183                               ite_bell_length = bp->len;
184                               ite_bell_volume = bp->vol;
185                               return (0);
186                     }
187           default:
188                     return ((*wsdisplay_cdevsw.d_ioctl)(cn_tab->cn_dev, cmd,
189                                                                 addr, flag, l));
190           }
191 
192           return (ENOTTY);
193 }
194 
195 /*ARGSUSED*/
196 int
itepoll(dev_t dev,int events,struct lwp * l)197 itepoll(dev_t dev, int events, struct lwp *l)
198 {
199           return ite_initted ?
200               (*wsdisplay_cdevsw.d_poll)(cn_tab->cn_dev, events, l) : (ENXIO);
201 }
202 
203 int
itekqfilter(dev_t dev,struct knote * kn)204 itekqfilter(dev_t dev, struct knote *kn)
205 {
206           return ite_initted ?
207               (*wsdisplay_cdevsw.d_kqfilter)(cn_tab->cn_dev, kn) : (ENXIO);
208 }
209