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