kbd.c revision 1.26 1 1.26 agc /* $NetBSD: kbd.c,v 1.26 2003/08/07 16:27:01 agc Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.26 agc * 3. Neither the name of the University nor the names of its contributors
16 1.26 agc * may be used to endorse or promote products derived from this software
17 1.26 agc * without specific prior written permission.
18 1.26 agc *
19 1.26 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.26 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.26 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.26 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.26 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.26 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.26 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.26 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.26 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.26 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.26 agc * SUCH DAMAGE.
30 1.26 agc */
31 1.26 agc
32 1.26 agc /*
33 1.26 agc * Copyright (c) 1995 Leo Weppelman
34 1.26 agc *
35 1.26 agc * Redistribution and use in source and binary forms, with or without
36 1.26 agc * modification, are permitted provided that the following conditions
37 1.26 agc * are met:
38 1.26 agc * 1. Redistributions of source code must retain the above copyright
39 1.26 agc * notice, this list of conditions and the following disclaimer.
40 1.26 agc * 2. Redistributions in binary form must reproduce the above copyright
41 1.26 agc * notice, this list of conditions and the following disclaimer in the
42 1.26 agc * documentation and/or other materials provided with the distribution.
43 1.1 leo * 3. All advertising materials mentioning features or use of this software
44 1.1 leo * must display the following acknowledgement:
45 1.1 leo * This product includes software developed by the University of
46 1.1 leo * California, Berkeley and its contributors.
47 1.1 leo * 4. Neither the name of the University nor the names of its contributors
48 1.1 leo * may be used to endorse or promote products derived from this software
49 1.1 leo * without specific prior written permission.
50 1.1 leo *
51 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 1.1 leo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 1.1 leo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 1.1 leo * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 1.1 leo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 1.1 leo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 1.1 leo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 1.1 leo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 1.1 leo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 1.1 leo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 1.1 leo * SUCH DAMAGE.
62 1.1 leo */
63 1.25 lukem
64 1.25 lukem #include <sys/cdefs.h>
65 1.26 agc __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.26 2003/08/07 16:27:01 agc Exp $");
66 1.1 leo
67 1.23 thomas #include "mouse.h"
68 1.23 thomas #include "ite.h"
69 1.23 thomas #include "wskbd.h"
70 1.23 thomas
71 1.1 leo #include <sys/param.h>
72 1.1 leo #include <sys/systm.h>
73 1.1 leo #include <sys/device.h>
74 1.1 leo #include <sys/ioctl.h>
75 1.1 leo #include <sys/tty.h>
76 1.1 leo #include <sys/proc.h>
77 1.1 leo #include <sys/conf.h>
78 1.1 leo #include <sys/file.h>
79 1.1 leo #include <sys/kernel.h>
80 1.9 leo #include <sys/signalvar.h>
81 1.1 leo #include <sys/syslog.h>
82 1.1 leo #include <dev/cons.h>
83 1.1 leo #include <machine/cpu.h>
84 1.1 leo #include <machine/iomap.h>
85 1.1 leo #include <machine/mfp.h>
86 1.1 leo #include <machine/acia.h>
87 1.1 leo #include <atari/dev/itevar.h>
88 1.1 leo #include <atari/dev/event_var.h>
89 1.1 leo #include <atari/dev/vuid_event.h>
90 1.9 leo #include <atari/dev/ym2149reg.h>
91 1.9 leo #include <atari/dev/kbdreg.h>
92 1.9 leo #include <atari/dev/kbdvar.h>
93 1.15 leo #include <atari/dev/kbdmap.h>
94 1.9 leo #include <atari/dev/msvar.h>
95 1.1 leo
96 1.23 thomas #if NWSKBD>0
97 1.23 thomas #include <dev/wscons/wsconsio.h>
98 1.23 thomas #include <dev/wscons/wskbdvar.h>
99 1.23 thomas #include <dev/wscons/wsksymdef.h>
100 1.23 thomas #include <dev/wscons/wsksymvar.h>
101 1.23 thomas #include <atari/dev/wskbdmap_atari.h>
102 1.23 thomas #endif
103 1.23 thomas
104 1.23 thomas /* WSKBD */
105 1.23 thomas /*
106 1.23 thomas * If NWSKBD>0 we try to attach an wskbd device to us. What follows
107 1.23 thomas * is definitions of callback functions and structures that are passed
108 1.23 thomas * to wscons when initializing.
109 1.23 thomas */
110 1.23 thomas
111 1.23 thomas /*
112 1.23 thomas * Now with wscons this driver exhibits some weird behaviour.
113 1.23 thomas * It may act both as a driver of its own and the md part of the
114 1.23 thomas * wskbd driver. Therefore it can be accessed through /dev/kbd
115 1.23 thomas * and /dev/wskbd0 both.
116 1.23 thomas *
117 1.23 thomas * The data from they keyboard may end up in at least four different
118 1.23 thomas * places:
119 1.23 thomas * - If this driver has been opened (/dev/kbd) and the
120 1.23 thomas * direct mode (TIOCDIRECT) has been set, data goes to
121 1.23 thomas * the process who opened the device. Data will transmit itself
122 1.23 thomas * as described by the firm_event structure.
123 1.23 thomas * - If wskbd support is compiled in and a wskbd driver has been
124 1.23 thomas * attached then the data is sent to it. Wskbd in turn may
125 1.23 thomas * - Send the data in the wscons_event form to a process that
126 1.23 thomas * has opened /dev/wskbd0
127 1.23 thomas * - Feed the data to a virtual terminal.
128 1.23 thomas * - If an ite is present the data may be fed to it.
129 1.23 thomas */
130 1.5 leo
131 1.15 leo u_char kbd_modifier; /* Modifier mask */
132 1.15 leo
133 1.1 leo static u_char kbd_ring[KBD_RING_SIZE];
134 1.1 leo static volatile u_int kbd_rbput = 0; /* 'put' index */
135 1.1 leo static u_int kbd_rbget = 0; /* 'get' index */
136 1.1 leo static u_char kbd_soft = 0; /* 1: Softint has been scheduled*/
137 1.1 leo
138 1.1 leo static struct kbd_softc kbd_softc;
139 1.1 leo
140 1.6 leo /* {b,c}devsw[] function prototypes */
141 1.6 leo dev_type_open(kbdopen);
142 1.6 leo dev_type_close(kbdclose);
143 1.6 leo dev_type_read(kbdread);
144 1.6 leo dev_type_ioctl(kbdioctl);
145 1.13 leo dev_type_poll(kbdpoll);
146 1.22 jdolecek dev_type_kqfilter(kbdkqfilter);
147 1.6 leo
148 1.6 leo /* Interrupt handler */
149 1.6 leo void kbdintr __P((int));
150 1.6 leo
151 1.10 leo static void kbdsoft __P((void *, void *));
152 1.1 leo static void kbdattach __P((struct device *, struct device *, void *));
153 1.14 leo static int kbdmatch __P((struct device *, struct cfdata *, void *));
154 1.23 thomas #if NITE>0
155 1.15 leo static int kbd_do_modifier __P((u_char));
156 1.23 thomas #endif
157 1.4 leo static int kbd_write_poll __P((u_char *, int));
158 1.3 leo static void kbd_pkg_start __P((struct kbd_softc *, u_char));
159 1.1 leo
160 1.21 thorpej CFATTACH_DECL(kbd, sizeof(struct device),
161 1.21 thorpej kbdmatch, kbdattach, NULL, NULL);
162 1.19 gehenna
163 1.19 gehenna const struct cdevsw kbd_cdevsw = {
164 1.19 gehenna kbdopen, kbdclose, kbdread, nowrite, kbdioctl,
165 1.22 jdolecek nostop, notty, kbdpoll, nommap, kbdkqfilter,
166 1.7 thorpej };
167 1.9 leo
168 1.23 thomas #if NWSKBD>0
169 1.23 thomas /* accessops */
170 1.23 thomas static int kbd_enable(void *, int);
171 1.23 thomas static void kbd_set_leds(void *, int);
172 1.23 thomas static int kbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
173 1.23 thomas
174 1.23 thomas /* console ops */
175 1.23 thomas static void kbd_getc(void *, u_int *, int *);
176 1.23 thomas static void kbd_pollc(void *, int);
177 1.23 thomas static void kbd_bell(void *, u_int, u_int, u_int);
178 1.23 thomas
179 1.23 thomas static struct wskbd_accessops kbd_accessops = {
180 1.23 thomas kbd_enable,
181 1.23 thomas kbd_set_leds,
182 1.23 thomas kbd_ioctl
183 1.23 thomas };
184 1.23 thomas
185 1.23 thomas static struct wskbd_consops kbd_consops = {
186 1.23 thomas kbd_getc,
187 1.23 thomas kbd_pollc,
188 1.23 thomas kbd_bell
189 1.23 thomas };
190 1.23 thomas
191 1.23 thomas /* Pointer to keymaps. */
192 1.23 thomas static struct wskbd_mapdata kbd_mapdata = {
193 1.23 thomas atarikbd_keydesctab,
194 1.23 thomas KB_US
195 1.23 thomas };
196 1.23 thomas #endif /* WSKBD */
197 1.23 thomas
198 1.1 leo /*ARGSUSED*/
199 1.1 leo static int
200 1.14 leo kbdmatch(pdp, cfp, auxp)
201 1.14 leo struct device *pdp;
202 1.14 leo struct cfdata *cfp;
203 1.14 leo void *auxp;
204 1.1 leo {
205 1.3 leo if (!strcmp((char *)auxp, "kbd"))
206 1.3 leo return (1);
207 1.3 leo return (0);
208 1.1 leo }
209 1.1 leo
210 1.1 leo /*ARGSUSED*/
211 1.1 leo static void
212 1.1 leo kbdattach(pdp, dp, auxp)
213 1.1 leo struct device *pdp, *dp;
214 1.1 leo void *auxp;
215 1.1 leo {
216 1.3 leo int timeout;
217 1.4 leo u_char kbd_rst[] = { 0x80, 0x01 };
218 1.4 leo u_char kbd_icmd[] = { 0x12, 0x15 };
219 1.3 leo
220 1.3 leo /*
221 1.3 leo * Disable keyboard interrupts from MFP
222 1.3 leo */
223 1.3 leo MFP->mf_ierb &= ~IB_AINT;
224 1.3 leo
225 1.3 leo /*
226 1.3 leo * Reset ACIA and intialize to:
227 1.3 leo * divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
228 1.3 leo */
229 1.3 leo KBD->ac_cs = A_RESET;
230 1.3 leo delay(100); /* XXX: enough? */
231 1.3 leo KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
232 1.3 leo
233 1.3 leo /*
234 1.3 leo * Clear error conditions
235 1.3 leo */
236 1.3 leo while (KBD->ac_cs & (A_IRQ|A_RXRDY))
237 1.3 leo timeout = KBD->ac_da;
238 1.3 leo
239 1.3 leo /*
240 1.3 leo * Now send the reset string, and read+ignore it's response
241 1.3 leo */
242 1.4 leo if (!kbd_write_poll(kbd_rst, 2))
243 1.12 christos printf("kbd: error cannot reset keyboard\n");
244 1.3 leo for (timeout = 1000; timeout > 0; timeout--) {
245 1.3 leo if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
246 1.3 leo timeout = KBD->ac_da;
247 1.3 leo timeout = 100;
248 1.3 leo }
249 1.3 leo delay(100);
250 1.3 leo }
251 1.4 leo /*
252 1.4 leo * Send init command: disable mice & joysticks
253 1.4 leo */
254 1.4 leo kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
255 1.3 leo
256 1.12 christos printf("\n");
257 1.23 thomas
258 1.23 thomas #if NWSKBD>0
259 1.23 thomas if (dp != NULL) {
260 1.23 thomas /*
261 1.23 thomas * Try to attach the wskbd.
262 1.23 thomas */
263 1.23 thomas struct wskbddev_attach_args waa;
264 1.23 thomas
265 1.23 thomas /* Maybe should be done before this?... */
266 1.23 thomas wskbd_cnattach(&kbd_consops, NULL, &kbd_mapdata);
267 1.23 thomas
268 1.23 thomas waa.console = 1;
269 1.23 thomas waa.keymap = &kbd_mapdata;
270 1.23 thomas waa.accessops = &kbd_accessops;
271 1.23 thomas waa.accesscookie = NULL;
272 1.23 thomas kbd_softc.k_wskbddev = config_found(dp, &waa, wskbddevprint);
273 1.23 thomas
274 1.23 thomas kbd_softc.k_pollingmode = 0;
275 1.23 thomas
276 1.23 thomas kbdenable();
277 1.23 thomas }
278 1.23 thomas #endif /* WSKBD */
279 1.1 leo }
280 1.1 leo
281 1.1 leo void
282 1.1 leo kbdenable()
283 1.1 leo {
284 1.3 leo int s, code;
285 1.1 leo
286 1.1 leo s = spltty();
287 1.3 leo
288 1.1 leo /*
289 1.3 leo * Clear error conditions...
290 1.1 leo */
291 1.3 leo while (KBD->ac_cs & (A_IRQ|A_RXRDY))
292 1.3 leo code = KBD->ac_da;
293 1.1 leo /*
294 1.1 leo * Enable interrupts from MFP
295 1.1 leo */
296 1.18 leo MFP->mf_iprb = (u_int8_t)~IB_AINT;
297 1.1 leo MFP->mf_ierb |= IB_AINT;
298 1.1 leo MFP->mf_imrb |= IB_AINT;
299 1.1 leo
300 1.1 leo kbd_softc.k_event_mode = 0;
301 1.1 leo kbd_softc.k_events.ev_io = 0;
302 1.3 leo kbd_softc.k_pkg_size = 0;
303 1.1 leo splx(s);
304 1.1 leo }
305 1.1 leo
306 1.1 leo int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
307 1.1 leo {
308 1.3 leo if (kbd_softc.k_events.ev_io)
309 1.1 leo return EBUSY;
310 1.1 leo
311 1.1 leo kbd_softc.k_events.ev_io = p;
312 1.1 leo ev_init(&kbd_softc.k_events);
313 1.1 leo return (0);
314 1.1 leo }
315 1.1 leo
316 1.1 leo int
317 1.1 leo kbdclose(dev_t dev, int flags, int mode, struct proc *p)
318 1.1 leo {
319 1.1 leo /* Turn off event mode, dump the queue */
320 1.1 leo kbd_softc.k_event_mode = 0;
321 1.1 leo ev_fini(&kbd_softc.k_events);
322 1.1 leo kbd_softc.k_events.ev_io = NULL;
323 1.1 leo return (0);
324 1.1 leo }
325 1.1 leo
326 1.1 leo int
327 1.1 leo kbdread(dev_t dev, struct uio *uio, int flags)
328 1.1 leo {
329 1.1 leo return ev_read(&kbd_softc.k_events, uio, flags);
330 1.1 leo }
331 1.1 leo
332 1.1 leo int
333 1.1 leo kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
334 1.1 leo {
335 1.1 leo register struct kbd_softc *k = &kbd_softc;
336 1.16 leo struct kbdbell *kb;
337 1.1 leo
338 1.1 leo switch (cmd) {
339 1.1 leo case KIOCTRANS:
340 1.3 leo if (*(int *)data == TR_UNTRANS_EVENT)
341 1.1 leo return 0;
342 1.1 leo break;
343 1.1 leo
344 1.1 leo case KIOCGTRANS:
345 1.1 leo /*
346 1.1 leo * Get translation mode
347 1.1 leo */
348 1.1 leo *(int *)data = TR_UNTRANS_EVENT;
349 1.1 leo return 0;
350 1.1 leo
351 1.1 leo case KIOCSDIRECT:
352 1.1 leo k->k_event_mode = *(int *)data;
353 1.1 leo return 0;
354 1.16 leo
355 1.16 leo case KIOCRINGBELL:
356 1.16 leo kb = (struct kbdbell *)data;
357 1.16 leo if (kb)
358 1.16 leo kbd_bell_sparms(kb->volume, kb->pitch,
359 1.16 leo kb->duration);
360 1.16 leo kbdbell();
361 1.16 leo return 0;
362 1.1 leo
363 1.1 leo case FIONBIO: /* we will remove this someday (soon???) */
364 1.1 leo return 0;
365 1.1 leo
366 1.1 leo case FIOASYNC:
367 1.1 leo k->k_events.ev_async = *(int *)data != 0;
368 1.1 leo return 0;
369 1.1 leo
370 1.1 leo case TIOCSPGRP:
371 1.3 leo if (*(int *)data != k->k_events.ev_io->p_pgid)
372 1.1 leo return EPERM;
373 1.1 leo return 0;
374 1.1 leo
375 1.1 leo default:
376 1.1 leo return ENOTTY;
377 1.1 leo }
378 1.1 leo
379 1.1 leo /*
380 1.1 leo * We identified the ioctl, but we do not handle it.
381 1.1 leo */
382 1.1 leo return EOPNOTSUPP; /* misuse, but what the heck */
383 1.1 leo }
384 1.1 leo
385 1.1 leo int
386 1.13 leo kbdpoll (dev_t dev, int events, struct proc *p)
387 1.1 leo {
388 1.13 leo return ev_poll (&kbd_softc.k_events, events, p);
389 1.22 jdolecek }
390 1.22 jdolecek
391 1.22 jdolecek int
392 1.22 jdolecek kbdkqfilter(dev_t dev, struct knote *kn)
393 1.22 jdolecek {
394 1.22 jdolecek
395 1.22 jdolecek return (ev_kqfilter(&kbd_softc.k_events, kn));
396 1.1 leo }
397 1.1 leo
398 1.1 leo /*
399 1.3 leo * Keyboard interrupt handler called straight from MFP at spl6.
400 1.1 leo */
401 1.6 leo void
402 1.1 leo kbdintr(sr)
403 1.1 leo int sr; /* sr at time of interrupt */
404 1.1 leo {
405 1.1 leo int code;
406 1.3 leo int got_char = 0;
407 1.1 leo
408 1.1 leo /*
409 1.1 leo * There may be multiple keys available. Read them all.
410 1.1 leo */
411 1.3 leo while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
412 1.3 leo got_char = 1;
413 1.3 leo if (KBD->ac_cs & (A_OE|A_PE)) {
414 1.3 leo code = KBD->ac_da; /* Silently ignore errors */
415 1.1 leo continue;
416 1.1 leo }
417 1.1 leo kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
418 1.1 leo }
419 1.3 leo
420 1.3 leo /*
421 1.3 leo * If characters are waiting for transmit, send them.
422 1.3 leo */
423 1.3 leo if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
424 1.3 leo if (kbd_softc.k_sendp != NULL)
425 1.3 leo KBD->ac_da = *kbd_softc.k_sendp++;
426 1.3 leo if (--kbd_softc.k_send_cnt <= 0) {
427 1.3 leo /*
428 1.3 leo * The total package has been transmitted,
429 1.3 leo * wakeup anyone waiting for it.
430 1.3 leo */
431 1.3 leo KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
432 1.3 leo kbd_softc.k_sendp = NULL;
433 1.3 leo kbd_softc.k_send_cnt = 0;
434 1.3 leo wakeup((caddr_t)&kbd_softc.k_send_cnt);
435 1.3 leo }
436 1.3 leo }
437 1.3 leo
438 1.3 leo /*
439 1.3 leo * Activate software-level to handle possible input.
440 1.3 leo */
441 1.3 leo if (got_char) {
442 1.3 leo if (!BASEPRI(sr)) {
443 1.3 leo if (!kbd_soft++)
444 1.3 leo add_sicallback(kbdsoft, 0, 0);
445 1.3 leo } else {
446 1.3 leo spl1();
447 1.10 leo kbdsoft(NULL, NULL);
448 1.3 leo }
449 1.1 leo }
450 1.1 leo }
451 1.1 leo
452 1.1 leo /*
453 1.1 leo * Keyboard soft interrupt handler
454 1.1 leo */
455 1.1 leo void
456 1.10 leo kbdsoft(junk1, junk2)
457 1.10 leo void *junk1, *junk2;
458 1.1 leo {
459 1.1 leo int s;
460 1.1 leo u_char code;
461 1.1 leo struct kbd_softc *k = &kbd_softc;
462 1.1 leo struct firm_event *fe;
463 1.1 leo int put;
464 1.1 leo int n, get;
465 1.1 leo
466 1.1 leo kbd_soft = 0;
467 1.1 leo get = kbd_rbget;
468 1.1 leo
469 1.3 leo for (;;) {
470 1.1 leo n = kbd_rbput;
471 1.3 leo if (get == n) /* We're done */
472 1.1 leo break;
473 1.1 leo n -= get;
474 1.3 leo if (n > KBD_RING_SIZE) { /* Ring buffer overflow */
475 1.1 leo get += n - KBD_RING_SIZE;
476 1.1 leo n = KBD_RING_SIZE;
477 1.1 leo }
478 1.3 leo while (--n >= 0) {
479 1.1 leo code = kbd_ring[get++ & KBD_RING_MASK];
480 1.1 leo
481 1.1 leo /*
482 1.3 leo * If collecting a package, stuff it in and
483 1.3 leo * continue.
484 1.3 leo */
485 1.3 leo if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
486 1.9 leo k->k_package[k->k_pkg_idx++] = code;
487 1.9 leo if (k->k_pkg_idx == k->k_pkg_size) {
488 1.9 leo /*
489 1.9 leo * Package is complete.
490 1.9 leo */
491 1.9 leo switch(k->k_pkg_type) {
492 1.5 leo #if NMOUSE > 0
493 1.9 leo case KBD_AMS_PKG:
494 1.9 leo case KBD_RMS_PKG:
495 1.9 leo case KBD_JOY1_PKG:
496 1.9 leo mouse_soft((REL_MOUSE *)k->k_package,
497 1.9 leo k->k_pkg_size, k->k_pkg_type);
498 1.5 leo #endif /* NMOUSE */
499 1.3 leo }
500 1.9 leo k->k_pkg_size = 0;
501 1.9 leo }
502 1.9 leo continue;
503 1.3 leo }
504 1.3 leo /*
505 1.3 leo * If this is a package header, init pkg. handling.
506 1.3 leo */
507 1.15 leo if (!KBD_IS_KEY(code)) {
508 1.3 leo kbd_pkg_start(k, code);
509 1.3 leo continue;
510 1.3 leo }
511 1.23 thomas #if NWSKBD>0
512 1.23 thomas /*
513 1.23 thomas * If we have attached a wskbd and not in polling mode and
514 1.23 thomas * nobody has opened us directly, then send the keystroke
515 1.23 thomas * to the wskbd.
516 1.23 thomas */
517 1.23 thomas
518 1.23 thomas if (kbd_softc.k_pollingmode == 0
519 1.24 leo && kbd_softc.k_wskbddev != NULL
520 1.23 thomas && k->k_event_mode == 0) {
521 1.23 thomas wskbd_input(kbd_softc.k_wskbddev,
522 1.23 thomas KBD_RELEASED(code) ?
523 1.23 thomas WSCONS_EVENT_KEY_UP :
524 1.23 thomas WSCONS_EVENT_KEY_DOWN,
525 1.23 thomas KBD_SCANCODE(code));
526 1.23 thomas continue;
527 1.23 thomas }
528 1.23 thomas #endif /* NWSKBD */
529 1.23 thomas #if NITE>0
530 1.15 leo if (kbd_do_modifier(code) && !k->k_event_mode)
531 1.15 leo continue;
532 1.23 thomas #endif
533 1.15 leo
534 1.3 leo /*
535 1.1 leo * if not in event mode, deliver straight to ite to
536 1.1 leo * process key stroke
537 1.1 leo */
538 1.3 leo if (!k->k_event_mode) {
539 1.1 leo /* Gets to spltty() by itself */
540 1.23 thomas #if NITE>0
541 1.1 leo ite_filter(code, ITEFILT_TTY);
542 1.23 thomas #endif
543 1.1 leo continue;
544 1.1 leo }
545 1.1 leo
546 1.1 leo /*
547 1.1 leo * Keyboard is generating events. Turn this keystroke
548 1.1 leo * into an event and put it in the queue. If the queue
549 1.1 leo * is full, the keystroke is lost (sorry!).
550 1.1 leo */
551 1.1 leo s = spltty();
552 1.1 leo put = k->k_events.ev_put;
553 1.1 leo fe = &k->k_events.ev_q[put];
554 1.1 leo put = (put + 1) % EV_QSIZE;
555 1.3 leo if (put == k->k_events.ev_get) {
556 1.1 leo log(LOG_WARNING,
557 1.1 leo "keyboard event queue overflow\n");
558 1.1 leo splx(s);
559 1.1 leo continue;
560 1.1 leo }
561 1.15 leo fe->id = KBD_SCANCODE(code);
562 1.15 leo fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
563 1.15 leo fe->time = time;
564 1.1 leo k->k_events.ev_put = put;
565 1.1 leo EV_WAKEUP(&k->k_events);
566 1.1 leo splx(s);
567 1.1 leo }
568 1.1 leo kbd_rbget = get;
569 1.1 leo }
570 1.1 leo }
571 1.1 leo
572 1.16 leo static u_char sound[] = {
573 1.1 leo 0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
574 1.1 leo 0xF8,0x10,0x10,0x10,0x00,0x20,0x03
575 1.1 leo };
576 1.1 leo
577 1.6 leo void
578 1.1 leo kbdbell()
579 1.1 leo {
580 1.3 leo register int i, sps;
581 1.1 leo
582 1.9 leo sps = splhigh();
583 1.3 leo for (i = 0; i < sizeof(sound); i++) {
584 1.8 leo YM2149->sd_selr = i;
585 1.8 leo YM2149->sd_wdat = sound[i];
586 1.3 leo }
587 1.3 leo splx(sps);
588 1.16 leo }
589 1.16 leo
590 1.16 leo
591 1.16 leo /*
592 1.16 leo * Set the parameters of the 'default' beep.
593 1.16 leo */
594 1.16 leo
595 1.16 leo #define KBDBELLCLOCK 125000 /* 2MHz / 16 */
596 1.16 leo #define KBDBELLDURATION 128 /* 256 / 2MHz */
597 1.16 leo
598 1.16 leo void
599 1.16 leo kbd_bell_gparms(volume, pitch, duration)
600 1.16 leo u_int *volume, *pitch, *duration;
601 1.16 leo {
602 1.16 leo u_int tmp;
603 1.16 leo
604 1.16 leo tmp = sound[11] | (sound[12] << 8);
605 1.16 leo *duration = (tmp * KBDBELLDURATION) / 1000;
606 1.16 leo
607 1.16 leo tmp = sound[0] | (sound[1] << 8);
608 1.16 leo *pitch = KBDBELLCLOCK / tmp;
609 1.16 leo
610 1.16 leo *volume = 0;
611 1.16 leo }
612 1.16 leo
613 1.16 leo void
614 1.16 leo kbd_bell_sparms(volume, pitch, duration)
615 1.16 leo u_int volume, pitch, duration;
616 1.16 leo {
617 1.16 leo u_int f, t;
618 1.16 leo
619 1.16 leo f = pitch > 10 ? pitch : 10; /* minimum pitch */
620 1.16 leo if (f > 20000)
621 1.16 leo f = 20000; /* maximum pitch */
622 1.16 leo
623 1.16 leo f = KBDBELLCLOCK / f;
624 1.16 leo
625 1.16 leo t = (duration * 1000) / KBDBELLDURATION;
626 1.16 leo
627 1.16 leo sound[ 0] = f & 0xff;
628 1.16 leo sound[ 1] = (f >> 8) & 0xf;
629 1.16 leo f -= 1;
630 1.16 leo sound[ 2] = f & 0xff;
631 1.16 leo sound[ 3] = (f >> 8) & 0xf;
632 1.16 leo f += 2;
633 1.16 leo sound[ 4] = f & 0xff;
634 1.16 leo sound[ 5] = (f >> 8) & 0xf;
635 1.16 leo
636 1.16 leo sound[11] = t & 0xff;
637 1.16 leo sound[12] = (t >> 8) & 0xff;
638 1.16 leo
639 1.16 leo sound[13] = 0x03;
640 1.1 leo }
641 1.1 leo
642 1.1 leo int
643 1.1 leo kbdgetcn()
644 1.1 leo {
645 1.1 leo u_char code;
646 1.4 leo int s = spltty();
647 1.4 leo int ints_active;
648 1.1 leo
649 1.4 leo ints_active = 0;
650 1.4 leo if (MFP->mf_imrb & IB_AINT) {
651 1.4 leo ints_active = 1;
652 1.4 leo MFP->mf_imrb &= ~IB_AINT;
653 1.4 leo }
654 1.3 leo for (;;) {
655 1.4 leo while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
656 1.3 leo ; /* Wait for key */
657 1.3 leo if (KBD->ac_cs & (A_OE|A_PE)) {
658 1.3 leo code = KBD->ac_da; /* Silently ignore errors */
659 1.3 leo continue;
660 1.3 leo }
661 1.15 leo code = KBD->ac_da;
662 1.23 thomas #if NITE>0
663 1.15 leo if (!kbd_do_modifier(code))
664 1.23 thomas #endif
665 1.15 leo break;
666 1.3 leo }
667 1.1 leo
668 1.4 leo if (ints_active) {
669 1.18 leo MFP->mf_iprb = (u_int8_t)~IB_AINT;
670 1.4 leo MFP->mf_imrb |= IB_AINT;
671 1.4 leo }
672 1.1 leo
673 1.1 leo splx (s);
674 1.1 leo return code;
675 1.3 leo }
676 1.3 leo
677 1.3 leo /*
678 1.3 leo * Write a command to the keyboard in 'polled' mode.
679 1.3 leo */
680 1.3 leo static int
681 1.4 leo kbd_write_poll(cmd, len)
682 1.3 leo u_char *cmd;
683 1.3 leo int len;
684 1.3 leo {
685 1.3 leo int timeout;
686 1.3 leo
687 1.3 leo while (len-- > 0) {
688 1.3 leo KBD->ac_da = *cmd++;
689 1.3 leo for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
690 1.3 leo delay(10);
691 1.3 leo if (!(KBD->ac_cs & A_TXRDY))
692 1.3 leo return (0);
693 1.3 leo }
694 1.3 leo return (1);
695 1.3 leo }
696 1.3 leo
697 1.3 leo /*
698 1.3 leo * Write a command to the keyboard. Return when command is send.
699 1.3 leo */
700 1.4 leo void
701 1.3 leo kbd_write(cmd, len)
702 1.3 leo u_char *cmd;
703 1.3 leo int len;
704 1.3 leo {
705 1.3 leo struct kbd_softc *k = &kbd_softc;
706 1.3 leo int sps;
707 1.3 leo
708 1.3 leo /*
709 1.3 leo * Get to splhigh, 'real' interrupts arrive at spl6!
710 1.3 leo */
711 1.3 leo sps = splhigh();
712 1.3 leo
713 1.3 leo /*
714 1.3 leo * Make sure any privious write has ended...
715 1.3 leo */
716 1.3 leo while (k->k_sendp != NULL)
717 1.3 leo tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
718 1.3 leo
719 1.3 leo /*
720 1.3 leo * If the KBD-acia is not currently busy, send the first
721 1.3 leo * character now.
722 1.3 leo */
723 1.3 leo KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
724 1.3 leo if (KBD->ac_cs & A_TXRDY) {
725 1.3 leo KBD->ac_da = *cmd++;
726 1.3 leo len--;
727 1.3 leo }
728 1.3 leo
729 1.3 leo /*
730 1.3 leo * If we're not yet done, wait until all characters are send.
731 1.3 leo */
732 1.3 leo if (len > 0) {
733 1.3 leo k->k_sendp = cmd;
734 1.3 leo k->k_send_cnt = len;
735 1.3 leo tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
736 1.3 leo }
737 1.3 leo splx(sps);
738 1.3 leo
739 1.3 leo /*
740 1.3 leo * Wakeup all procs waiting for us.
741 1.3 leo */
742 1.3 leo wakeup((caddr_t)&k->k_sendp);
743 1.3 leo }
744 1.3 leo
745 1.3 leo /*
746 1.3 leo * Setup softc-fields to assemble a keyboard package.
747 1.3 leo */
748 1.3 leo static void
749 1.3 leo kbd_pkg_start(kp, msg_start)
750 1.3 leo struct kbd_softc *kp;
751 1.3 leo u_char msg_start;
752 1.3 leo {
753 1.3 leo kp->k_pkg_idx = 1;
754 1.3 leo kp->k_package[0] = msg_start;
755 1.3 leo switch (msg_start) {
756 1.3 leo case 0xf6:
757 1.9 leo kp->k_pkg_type = KBD_MEM_PKG;
758 1.3 leo kp->k_pkg_size = 8;
759 1.3 leo break;
760 1.3 leo case 0xf7:
761 1.9 leo kp->k_pkg_type = KBD_AMS_PKG;
762 1.3 leo kp->k_pkg_size = 6;
763 1.3 leo break;
764 1.3 leo case 0xf8:
765 1.3 leo case 0xf9:
766 1.3 leo case 0xfa:
767 1.3 leo case 0xfb:
768 1.9 leo kp->k_pkg_type = KBD_RMS_PKG;
769 1.3 leo kp->k_pkg_size = 3;
770 1.3 leo break;
771 1.3 leo case 0xfc:
772 1.9 leo kp->k_pkg_type = KBD_CLK_PKG;
773 1.3 leo kp->k_pkg_size = 7;
774 1.3 leo break;
775 1.3 leo case 0xfe:
776 1.9 leo kp->k_pkg_type = KBD_JOY0_PKG;
777 1.9 leo kp->k_pkg_size = 2;
778 1.9 leo break;
779 1.3 leo case 0xff:
780 1.9 leo kp->k_pkg_type = KBD_JOY1_PKG;
781 1.3 leo kp->k_pkg_size = 2;
782 1.3 leo break;
783 1.3 leo default:
784 1.12 christos printf("kbd: Unknown packet 0x%x\n", msg_start);
785 1.3 leo break;
786 1.3 leo }
787 1.1 leo }
788 1.15 leo
789 1.23 thomas #if NITE>0
790 1.15 leo /*
791 1.15 leo * Modifier processing
792 1.15 leo */
793 1.15 leo static int
794 1.15 leo kbd_do_modifier(code)
795 1.15 leo u_char code;
796 1.15 leo {
797 1.15 leo u_char up, mask;
798 1.15 leo
799 1.15 leo up = KBD_RELEASED(code);
800 1.15 leo mask = 0;
801 1.15 leo
802 1.15 leo switch(KBD_SCANCODE(code)) {
803 1.15 leo case KBD_LEFT_SHIFT:
804 1.15 leo mask = KBD_MOD_LSHIFT;
805 1.15 leo break;
806 1.15 leo case KBD_RIGHT_SHIFT:
807 1.15 leo mask = KBD_MOD_RSHIFT;
808 1.15 leo break;
809 1.15 leo case KBD_CTRL:
810 1.15 leo mask = KBD_MOD_CTRL;
811 1.15 leo break;
812 1.15 leo case KBD_ALT:
813 1.15 leo mask = KBD_MOD_ALT;
814 1.15 leo break;
815 1.15 leo case KBD_CAPS_LOCK:
816 1.15 leo /* CAPSLOCK is a toggle */
817 1.15 leo if(!up)
818 1.15 leo kbd_modifier ^= KBD_MOD_CAPS;
819 1.15 leo return 1;
820 1.15 leo }
821 1.15 leo if(mask) {
822 1.15 leo if(up)
823 1.15 leo kbd_modifier &= ~mask;
824 1.15 leo else
825 1.15 leo kbd_modifier |= mask;
826 1.15 leo return 1;
827 1.15 leo }
828 1.15 leo return 0;
829 1.15 leo }
830 1.23 thomas #endif
831 1.23 thomas
832 1.23 thomas #if NWSKBD>0
833 1.23 thomas /*
834 1.23 thomas * These are the callback functions that are passed to wscons.
835 1.23 thomas * They really don't do anything worth noting, just call the
836 1.23 thomas * other functions above.
837 1.23 thomas */
838 1.23 thomas
839 1.23 thomas static int
840 1.23 thomas kbd_enable(void *c, int on)
841 1.23 thomas {
842 1.23 thomas /* Wonder what this is supposed to do... */
843 1.23 thomas return 0;
844 1.23 thomas }
845 1.23 thomas
846 1.23 thomas static void
847 1.23 thomas kbd_set_leds(void *c, int leds)
848 1.23 thomas {
849 1.23 thomas /* we can not set the leds */
850 1.23 thomas }
851 1.23 thomas
852 1.23 thomas static int
853 1.23 thomas kbd_ioctl(void *c, u_long cmd, caddr_t data, int flag, struct proc *p)
854 1.23 thomas {
855 1.23 thomas struct wskbd_bell_data *kd;
856 1.23 thomas
857 1.23 thomas switch (cmd)
858 1.23 thomas {
859 1.23 thomas case WSKBDIO_COMPLEXBELL:
860 1.23 thomas kd = (struct wskbd_bell_data *)data;
861 1.23 thomas kbd_bell(0, kd->pitch, kd->period, kd->volume);
862 1.23 thomas return 0;
863 1.23 thomas case WSKBDIO_SETLEDS:
864 1.23 thomas return 0;
865 1.23 thomas case WSKBDIO_GETLEDS:
866 1.23 thomas *(int*)data = 0;
867 1.23 thomas return 0;
868 1.23 thomas case WSKBDIO_GTYPE:
869 1.23 thomas *(u_int*)data = WSKBD_TYPE_ATARI;
870 1.23 thomas return 0;
871 1.23 thomas }
872 1.23 thomas
873 1.23 thomas /*
874 1.23 thomas * We are supposed to return EPASSTHROUGH to wscons if we didn't
875 1.23 thomas * understand.
876 1.23 thomas */
877 1.23 thomas return (EPASSTHROUGH);
878 1.23 thomas }
879 1.23 thomas
880 1.23 thomas static void
881 1.23 thomas kbd_getc(void *c, u_int *type, int *data)
882 1.23 thomas {
883 1.23 thomas int key;
884 1.23 thomas key = kbdgetcn();
885 1.23 thomas
886 1.23 thomas *data = KBD_SCANCODE(key);
887 1.23 thomas *type = KBD_RELEASED(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
888 1.23 thomas }
889 1.23 thomas
890 1.23 thomas static void
891 1.23 thomas kbd_pollc(void *c, int on)
892 1.23 thomas {
893 1.23 thomas kbd_softc.k_pollingmode = on;
894 1.23 thomas }
895 1.23 thomas
896 1.23 thomas static void
897 1.23 thomas kbd_bell(void *v, u_int pitch, u_int duration, u_int volume)
898 1.23 thomas {
899 1.23 thomas kbd_bell_sparms(volume, pitch, duration);
900 1.23 thomas kbdbell();
901 1.23 thomas }
902 1.23 thomas #endif /* NWSKBD */
903