kbd.c revision 1.2 1 1.2 mycroft /* $NetBSD: kbd.c,v 1.2 1995/04/10 08:54:41 mycroft Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Leo Weppelman
5 1.1 leo * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
6 1.1 leo * All rights reserved.
7 1.1 leo *
8 1.1 leo * Redistribution and use in source and binary forms, with or without
9 1.1 leo * modification, are permitted provided that the following conditions
10 1.1 leo * are met:
11 1.1 leo * 1. Redistributions of source code must retain the above copyright
12 1.1 leo * notice, this list of conditions and the following disclaimer.
13 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 leo * notice, this list of conditions and the following disclaimer in the
15 1.1 leo * documentation and/or other materials provided with the distribution.
16 1.1 leo * 3. All advertising materials mentioning features or use of this software
17 1.1 leo * must display the following acknowledgement:
18 1.1 leo * This product includes software developed by the University of
19 1.1 leo * California, Berkeley and its contributors.
20 1.1 leo * 4. Neither the name of the University nor the names of its contributors
21 1.1 leo * may be used to endorse or promote products derived from this software
22 1.1 leo * without specific prior written permission.
23 1.1 leo *
24 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 leo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 leo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 leo * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 leo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 leo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 leo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 leo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 leo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 leo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 leo * SUCH DAMAGE.
35 1.1 leo */
36 1.1 leo
37 1.1 leo #include <sys/param.h>
38 1.1 leo #include <sys/systm.h>
39 1.1 leo #include <sys/device.h>
40 1.1 leo #include <sys/ioctl.h>
41 1.1 leo #include <sys/tty.h>
42 1.1 leo #include <sys/proc.h>
43 1.1 leo #include <sys/conf.h>
44 1.1 leo #include <sys/file.h>
45 1.1 leo #include <sys/kernel.h>
46 1.1 leo #include <sys/syslog.h>
47 1.1 leo #include <dev/cons.h>
48 1.1 leo #include <machine/cpu.h>
49 1.1 leo #include <machine/iomap.h>
50 1.1 leo #include <machine/mfp.h>
51 1.1 leo #include <machine/acia.h>
52 1.1 leo #include <machine/video.h>
53 1.1 leo #include <atari/dev/itevar.h>
54 1.1 leo #include <atari/dev/kbdreg.h>
55 1.1 leo #include <atari/dev/event_var.h>
56 1.1 leo #include <atari/dev/vuid_event.h>
57 1.1 leo
58 1.1 leo /*
59 1.1 leo * The ringbuffer is the interface between the hard and soft interrupt handler.
60 1.1 leo * The hard interrupt runs straight from the MFP interrupt.
61 1.1 leo */
62 1.1 leo #define KBD_RING_SIZE 16 /* Size of the ring buffer, must be power of 2 */
63 1.1 leo #define KBD_RING_MASK 15 /* Modulo mask for above */
64 1.1 leo
65 1.1 leo static u_char kbd_ring[KBD_RING_SIZE];
66 1.1 leo static volatile u_int kbd_rbput = 0; /* 'put' index */
67 1.1 leo static u_int kbd_rbget = 0; /* 'get' index */
68 1.1 leo static u_char kbd_soft = 0; /* 1: Softint has been scheduled*/
69 1.1 leo
70 1.1 leo struct kbd_softc {
71 1.1 leo int k_event_mode; /* if 1, collect events, */
72 1.1 leo /* else pass to ite */
73 1.1 leo struct evvar k_events; /* event queue state */
74 1.1 leo };
75 1.1 leo
76 1.1 leo static struct kbd_softc kbd_softc;
77 1.1 leo
78 1.1 leo static void kbdsoft __P((void));
79 1.1 leo static void kbdattach __P((struct device *, struct device *, void *));
80 1.1 leo static int kbdmatch __P((struct device *, struct cfdata *, void *));
81 1.1 leo
82 1.1 leo struct cfdriver kbdcd = {
83 1.1 leo NULL, "kbd", (cfmatch_t)kbdmatch, kbdattach,
84 1.1 leo DV_DULL, sizeof(struct device), NULL, 0 };
85 1.1 leo
86 1.1 leo
87 1.1 leo /*ARGSUSED*/
88 1.1 leo static int
89 1.1 leo kbdmatch(pdp, cfp, auxp)
90 1.1 leo struct device *pdp;
91 1.1 leo struct cfdata *cfp;
92 1.1 leo void *auxp;
93 1.1 leo {
94 1.1 leo if(!strcmp((char *)auxp, "kbd"))
95 1.1 leo return(1);
96 1.1 leo return(0);
97 1.1 leo }
98 1.1 leo
99 1.1 leo /*ARGSUSED*/
100 1.1 leo static void
101 1.1 leo kbdattach(pdp, dp, auxp)
102 1.1 leo struct device *pdp, *dp;
103 1.1 leo void *auxp;
104 1.1 leo {
105 1.1 leo printf("\n");
106 1.1 leo }
107 1.1 leo
108 1.1 leo /* definitions for atari keyboard encoding. */
109 1.1 leo #define KEY_CODE(c) ((c) & 0x7f)
110 1.1 leo #define KEY_UP(c) ((c) & 0x80)
111 1.1 leo
112 1.1 leo void
113 1.1 leo kbdenable()
114 1.1 leo {
115 1.1 leo int s, code;
116 1.1 leo
117 1.1 leo s = spltty();
118 1.1 leo /*
119 1.1 leo * Initialize ACIA port
120 1.1 leo */
121 1.1 leo code = KBD->ac_da; /* Clear error conditions */
122 1.1 leo
123 1.1 leo /* divide by 16, 8 data, 1 stop, no parity, enable interrupts */
124 1.1 leo KBD->ac_cs = KBD_INIT | A_RXINT;
125 1.1 leo #if 0 /* XXX Turn off mouse??? */
126 1.1 leo KBD->ac_da = 0x12;
127 1.1 leo #endif
128 1.1 leo
129 1.1 leo /*
130 1.1 leo * Enable interrupts from MFP
131 1.1 leo */
132 1.1 leo MFP->mf_iprb &= ~IB_AINT;
133 1.1 leo MFP->mf_ierb |= IB_AINT;
134 1.1 leo MFP->mf_imrb |= IB_AINT;
135 1.1 leo code = KBD->ac_da; /* Clear error conditions */
136 1.1 leo
137 1.1 leo kbd_softc.k_event_mode = 0;
138 1.1 leo kbd_softc.k_events.ev_io = 0;
139 1.1 leo splx(s);
140 1.1 leo }
141 1.1 leo
142 1.1 leo int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
143 1.1 leo {
144 1.1 leo int s, error;
145 1.1 leo
146 1.1 leo if(kbd_softc.k_events.ev_io)
147 1.1 leo return EBUSY;
148 1.1 leo
149 1.1 leo kbd_softc.k_events.ev_io = p;
150 1.1 leo ev_init(&kbd_softc.k_events);
151 1.1 leo return (0);
152 1.1 leo }
153 1.1 leo
154 1.1 leo int
155 1.1 leo kbdclose(dev_t dev, int flags, int mode, struct proc *p)
156 1.1 leo {
157 1.1 leo /* Turn off event mode, dump the queue */
158 1.1 leo kbd_softc.k_event_mode = 0;
159 1.1 leo ev_fini(&kbd_softc.k_events);
160 1.1 leo kbd_softc.k_events.ev_io = NULL;
161 1.1 leo return (0);
162 1.1 leo }
163 1.1 leo
164 1.1 leo int
165 1.1 leo kbdread(dev_t dev, struct uio *uio, int flags)
166 1.1 leo {
167 1.1 leo return ev_read(&kbd_softc.k_events, uio, flags);
168 1.1 leo }
169 1.1 leo
170 1.1 leo int
171 1.1 leo kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
172 1.1 leo {
173 1.1 leo register struct kbd_softc *k = &kbd_softc;
174 1.1 leo
175 1.1 leo switch (cmd) {
176 1.1 leo case KIOCTRANS:
177 1.1 leo if(*(int *)data == TR_UNTRANS_EVENT)
178 1.1 leo return 0;
179 1.1 leo break;
180 1.1 leo
181 1.1 leo case KIOCGTRANS:
182 1.1 leo /*
183 1.1 leo * Get translation mode
184 1.1 leo */
185 1.1 leo *(int *)data = TR_UNTRANS_EVENT;
186 1.1 leo return 0;
187 1.1 leo
188 1.1 leo case KIOCSDIRECT:
189 1.1 leo k->k_event_mode = *(int *)data;
190 1.1 leo return 0;
191 1.1 leo
192 1.1 leo case FIONBIO: /* we will remove this someday (soon???) */
193 1.1 leo return 0;
194 1.1 leo
195 1.1 leo case FIOASYNC:
196 1.1 leo k->k_events.ev_async = *(int *)data != 0;
197 1.1 leo return 0;
198 1.1 leo
199 1.1 leo case TIOCSPGRP:
200 1.1 leo if(*(int *)data != k->k_events.ev_io->p_pgid)
201 1.1 leo return EPERM;
202 1.1 leo return 0;
203 1.1 leo
204 1.1 leo default:
205 1.1 leo return ENOTTY;
206 1.1 leo }
207 1.1 leo
208 1.1 leo /*
209 1.1 leo * We identified the ioctl, but we do not handle it.
210 1.1 leo */
211 1.1 leo return EOPNOTSUPP; /* misuse, but what the heck */
212 1.1 leo }
213 1.1 leo
214 1.1 leo int
215 1.1 leo kbdselect (dev_t dev, int rw, struct proc *p)
216 1.1 leo {
217 1.1 leo return ev_select (&kbd_softc.k_events, rw, p);
218 1.1 leo }
219 1.1 leo
220 1.1 leo /*
221 1.1 leo * Keyboard interrupt handler called straight from MFP.
222 1.1 leo */
223 1.1 leo int
224 1.1 leo kbdintr(sr)
225 1.1 leo int sr; /* sr at time of interrupt */
226 1.1 leo {
227 1.1 leo int code;
228 1.1 leo
229 1.1 leo /*
230 1.1 leo * There may be multiple keys available. Read them all.
231 1.1 leo */
232 1.1 leo while(KBD->ac_cs & (A_IRQ|A_RXRDY)) {
233 1.1 leo if(KBD->ac_cs & (A_OE|A_PE)) {
234 1.1 leo code = KBD->ac_da; /* Silently ignore overruns */
235 1.1 leo continue;
236 1.1 leo }
237 1.1 leo kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
238 1.1 leo }
239 1.1 leo if(!BASEPRI(sr)) {
240 1.1 leo if(!kbd_soft++)
241 1.1 leo add_sicallback(kbdsoft, 0, 0);
242 1.1 leo }
243 1.1 leo else {
244 1.1 leo spl1();
245 1.1 leo kbdsoft();
246 1.1 leo }
247 1.1 leo }
248 1.1 leo
249 1.1 leo /*
250 1.1 leo * Keyboard soft interrupt handler
251 1.1 leo */
252 1.1 leo void
253 1.1 leo kbdsoft()
254 1.1 leo {
255 1.1 leo int s;
256 1.1 leo u_char code;
257 1.1 leo struct kbd_softc *k = &kbd_softc;
258 1.1 leo struct firm_event *fe;
259 1.1 leo int put;
260 1.1 leo int n, get;
261 1.1 leo
262 1.1 leo kbd_soft = 0;
263 1.1 leo get = kbd_rbget;
264 1.1 leo
265 1.1 leo for(;;) {
266 1.1 leo n = kbd_rbput;
267 1.1 leo if(get == n) /* We're done */
268 1.1 leo break;
269 1.1 leo n -= get;
270 1.1 leo if(n > KBD_RING_SIZE) { /* Ring buffer overflow */
271 1.1 leo get += n - KBD_RING_SIZE;
272 1.1 leo n = KBD_RING_SIZE;
273 1.1 leo }
274 1.1 leo while(--n >= 0) {
275 1.1 leo code = kbd_ring[get++ & KBD_RING_MASK];
276 1.1 leo
277 1.1 leo /*
278 1.1 leo * if not in event mode, deliver straight to ite to
279 1.1 leo * process key stroke
280 1.1 leo */
281 1.1 leo if(!k->k_event_mode) {
282 1.1 leo /* Gets to spltty() by itself */
283 1.1 leo ite_filter(code, ITEFILT_TTY);
284 1.1 leo continue;
285 1.1 leo }
286 1.1 leo
287 1.1 leo /*
288 1.1 leo * Keyboard is generating events. Turn this keystroke
289 1.1 leo * into an event and put it in the queue. If the queue
290 1.1 leo * is full, the keystroke is lost (sorry!).
291 1.1 leo */
292 1.1 leo s = spltty();
293 1.1 leo put = k->k_events.ev_put;
294 1.1 leo fe = &k->k_events.ev_q[put];
295 1.1 leo put = (put + 1) % EV_QSIZE;
296 1.1 leo if(put == k->k_events.ev_get) {
297 1.1 leo log(LOG_WARNING,
298 1.1 leo "keyboard event queue overflow\n");
299 1.1 leo splx(s);
300 1.1 leo continue;
301 1.1 leo }
302 1.1 leo fe->id = KEY_CODE(code);
303 1.1 leo fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
304 1.1 leo fe->time = time;
305 1.1 leo k->k_events.ev_put = put;
306 1.1 leo EV_WAKEUP(&k->k_events);
307 1.1 leo splx(s);
308 1.1 leo }
309 1.1 leo kbd_rbget = get;
310 1.1 leo }
311 1.1 leo }
312 1.1 leo
313 1.1 leo static char sound[] = {
314 1.1 leo 0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
315 1.1 leo 0xF8,0x10,0x10,0x10,0x00,0x20,0x03
316 1.1 leo };
317 1.1 leo
318 1.1 leo int
319 1.1 leo kbdbell()
320 1.1 leo {
321 1.1 leo register int i, sps;
322 1.1 leo
323 1.1 leo sps = spltty();
324 1.1 leo for(i = 0; i < sizeof(sound); i++) {
325 1.1 leo SOUND->sd_selr = i;
326 1.1 leo SOUND->sd_wdat = sound[i];
327 1.1 leo }
328 1.1 leo splx(sps);
329 1.1 leo }
330 1.1 leo
331 1.1 leo int
332 1.1 leo kbdgetcn()
333 1.1 leo {
334 1.1 leo u_char code;
335 1.1 leo int s = spltty();
336 1.1 leo
337 1.1 leo MFP->mf_imrb &= ~IB_AINT;
338 1.1 leo while(!(KBD->ac_cs & A_IRQ))
339 1.1 leo ; /* Wait for key */
340 1.1 leo
341 1.1 leo MFP->mf_iprb &= ~IB_AINT;
342 1.1 leo MFP->mf_imrb |= IB_AINT;
343 1.1 leo
344 1.1 leo code = KBD->ac_da;
345 1.1 leo splx (s);
346 1.1 leo return code;
347 1.1 leo }
348