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