Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.19
      1  1.19   gehenna /*	$NetBSD: kbd.c,v 1.19 2002/09/06 13:18:43 gehenna 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.9       leo #include <sys/signalvar.h>
     47   1.1       leo #include <sys/syslog.h>
     48   1.1       leo #include <dev/cons.h>
     49   1.1       leo #include <machine/cpu.h>
     50   1.1       leo #include <machine/iomap.h>
     51   1.1       leo #include <machine/mfp.h>
     52   1.1       leo #include <machine/acia.h>
     53   1.1       leo #include <atari/dev/itevar.h>
     54   1.1       leo #include <atari/dev/event_var.h>
     55   1.1       leo #include <atari/dev/vuid_event.h>
     56   1.9       leo #include <atari/dev/ym2149reg.h>
     57   1.9       leo #include <atari/dev/kbdreg.h>
     58   1.9       leo #include <atari/dev/kbdvar.h>
     59  1.15       leo #include <atari/dev/kbdmap.h>
     60   1.9       leo #include <atari/dev/msvar.h>
     61   1.1       leo 
     62   1.5       leo #include "mouse.h"
     63   1.5       leo 
     64  1.15       leo u_char			kbd_modifier;	/* Modifier mask		*/
     65  1.15       leo 
     66   1.1       leo static u_char		kbd_ring[KBD_RING_SIZE];
     67   1.1       leo static volatile u_int	kbd_rbput = 0;	/* 'put' index			*/
     68   1.1       leo static u_int		kbd_rbget = 0;	/* 'get' index			*/
     69   1.1       leo static u_char		kbd_soft  = 0;	/* 1: Softint has been scheduled*/
     70   1.1       leo 
     71   1.1       leo static struct kbd_softc kbd_softc;
     72   1.1       leo 
     73   1.6       leo /* {b,c}devsw[] function prototypes */
     74   1.6       leo dev_type_open(kbdopen);
     75   1.6       leo dev_type_close(kbdclose);
     76   1.6       leo dev_type_read(kbdread);
     77   1.6       leo dev_type_ioctl(kbdioctl);
     78  1.13       leo dev_type_poll(kbdpoll);
     79   1.6       leo 
     80   1.6       leo /* Interrupt handler */
     81   1.6       leo void	kbdintr __P((int));
     82   1.6       leo 
     83  1.10       leo static void kbdsoft __P((void *, void *));
     84   1.1       leo static void kbdattach __P((struct device *, struct device *, void *));
     85  1.14       leo static int  kbdmatch __P((struct device *, struct cfdata *, void *));
     86  1.15       leo static int  kbd_do_modifier __P((u_char));
     87   1.4       leo static int  kbd_write_poll __P((u_char *, int));
     88   1.3       leo static void kbd_pkg_start __P((struct kbd_softc *, u_char));
     89   1.1       leo 
     90   1.7   thorpej struct cfattach kbd_ca = {
     91   1.7   thorpej 	sizeof(struct device), kbdmatch, kbdattach
     92  1.19   gehenna };
     93  1.19   gehenna 
     94  1.19   gehenna const struct cdevsw kbd_cdevsw = {
     95  1.19   gehenna 	kbdopen, kbdclose, kbdread, nowrite, kbdioctl,
     96  1.19   gehenna 	nostop, notty, kbdpoll, nommap,
     97   1.7   thorpej };
     98   1.9       leo 
     99   1.1       leo /*ARGSUSED*/
    100   1.1       leo static	int
    101  1.14       leo kbdmatch(pdp, cfp, auxp)
    102  1.14       leo struct	device	*pdp;
    103  1.14       leo struct	cfdata	*cfp;
    104  1.14       leo void		*auxp;
    105   1.1       leo {
    106   1.3       leo 	if (!strcmp((char *)auxp, "kbd"))
    107   1.3       leo 		return (1);
    108   1.3       leo 	return (0);
    109   1.1       leo }
    110   1.1       leo 
    111   1.1       leo /*ARGSUSED*/
    112   1.1       leo static void
    113   1.1       leo kbdattach(pdp, dp, auxp)
    114   1.1       leo struct	device *pdp, *dp;
    115   1.1       leo void	*auxp;
    116   1.1       leo {
    117   1.3       leo 	int	timeout;
    118   1.4       leo 	u_char	kbd_rst[]  = { 0x80, 0x01 };
    119   1.4       leo 	u_char	kbd_icmd[] = { 0x12, 0x15 };
    120   1.3       leo 
    121   1.3       leo 	/*
    122   1.3       leo 	 * Disable keyboard interrupts from MFP
    123   1.3       leo 	 */
    124   1.3       leo 	MFP->mf_ierb &= ~IB_AINT;
    125   1.3       leo 
    126   1.3       leo 	/*
    127   1.3       leo 	 * Reset ACIA and intialize to:
    128   1.3       leo 	 *    divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
    129   1.3       leo 	 */
    130   1.3       leo 	KBD->ac_cs = A_RESET;
    131   1.3       leo 	delay(100);	/* XXX: enough? */
    132   1.3       leo 	KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
    133   1.3       leo 
    134   1.3       leo 	/*
    135   1.3       leo 	 * Clear error conditions
    136   1.3       leo 	 */
    137   1.3       leo 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
    138   1.3       leo 		timeout = KBD->ac_da;
    139   1.3       leo 
    140   1.3       leo 	/*
    141   1.3       leo 	 * Now send the reset string, and read+ignore it's response
    142   1.3       leo 	 */
    143   1.4       leo 	if (!kbd_write_poll(kbd_rst, 2))
    144  1.12  christos 		printf("kbd: error cannot reset keyboard\n");
    145   1.3       leo 	for (timeout = 1000; timeout > 0; timeout--) {
    146   1.3       leo 		if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
    147   1.3       leo 			timeout = KBD->ac_da;
    148   1.3       leo 			timeout = 100;
    149   1.3       leo 		}
    150   1.3       leo 		delay(100);
    151   1.3       leo 	}
    152   1.4       leo 	/*
    153   1.4       leo 	 * Send init command: disable mice & joysticks
    154   1.4       leo 	 */
    155   1.4       leo 	kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
    156   1.3       leo 
    157  1.12  christos 	printf("\n");
    158   1.1       leo }
    159   1.1       leo 
    160   1.1       leo void
    161   1.1       leo kbdenable()
    162   1.1       leo {
    163   1.3       leo 	int	s, code;
    164   1.1       leo 
    165   1.1       leo 	s = spltty();
    166   1.3       leo 
    167   1.1       leo 	/*
    168   1.3       leo 	 * Clear error conditions...
    169   1.1       leo 	 */
    170   1.3       leo 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
    171   1.3       leo 		code = KBD->ac_da;
    172   1.1       leo 	/*
    173   1.1       leo 	 * Enable interrupts from MFP
    174   1.1       leo 	 */
    175  1.18       leo 	MFP->mf_iprb  = (u_int8_t)~IB_AINT;
    176   1.1       leo 	MFP->mf_ierb |= IB_AINT;
    177   1.1       leo 	MFP->mf_imrb |= IB_AINT;
    178   1.1       leo 
    179   1.1       leo 	kbd_softc.k_event_mode   = 0;
    180   1.1       leo 	kbd_softc.k_events.ev_io = 0;
    181   1.3       leo 	kbd_softc.k_pkg_size     = 0;
    182   1.1       leo 	splx(s);
    183   1.1       leo }
    184   1.1       leo 
    185   1.1       leo int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
    186   1.1       leo {
    187   1.3       leo 	if (kbd_softc.k_events.ev_io)
    188   1.1       leo 		return EBUSY;
    189   1.1       leo 
    190   1.1       leo 	kbd_softc.k_events.ev_io = p;
    191   1.1       leo 	ev_init(&kbd_softc.k_events);
    192   1.1       leo 	return (0);
    193   1.1       leo }
    194   1.1       leo 
    195   1.1       leo int
    196   1.1       leo kbdclose(dev_t dev, int flags, int mode, struct proc *p)
    197   1.1       leo {
    198   1.1       leo 	/* Turn off event mode, dump the queue */
    199   1.1       leo 	kbd_softc.k_event_mode = 0;
    200   1.1       leo 	ev_fini(&kbd_softc.k_events);
    201   1.1       leo 	kbd_softc.k_events.ev_io = NULL;
    202   1.1       leo 	return (0);
    203   1.1       leo }
    204   1.1       leo 
    205   1.1       leo int
    206   1.1       leo kbdread(dev_t dev, struct uio *uio, int flags)
    207   1.1       leo {
    208   1.1       leo 	return ev_read(&kbd_softc.k_events, uio, flags);
    209   1.1       leo }
    210   1.1       leo 
    211   1.1       leo int
    212   1.1       leo kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
    213   1.1       leo {
    214   1.1       leo 	register struct kbd_softc *k = &kbd_softc;
    215  1.16       leo 	struct kbdbell	*kb;
    216   1.1       leo 
    217   1.1       leo 	switch (cmd) {
    218   1.1       leo 		case KIOCTRANS:
    219   1.3       leo 			if (*(int *)data == TR_UNTRANS_EVENT)
    220   1.1       leo 				return 0;
    221   1.1       leo 			break;
    222   1.1       leo 
    223   1.1       leo 		case KIOCGTRANS:
    224   1.1       leo 			/*
    225   1.1       leo 			 * Get translation mode
    226   1.1       leo 			 */
    227   1.1       leo 			*(int *)data = TR_UNTRANS_EVENT;
    228   1.1       leo 			return 0;
    229   1.1       leo 
    230   1.1       leo 		case KIOCSDIRECT:
    231   1.1       leo 			k->k_event_mode = *(int *)data;
    232   1.1       leo 			return 0;
    233  1.16       leo 
    234  1.16       leo 		case KIOCRINGBELL:
    235  1.16       leo 			kb = (struct kbdbell *)data;
    236  1.16       leo 			if (kb)
    237  1.16       leo 				kbd_bell_sparms(kb->volume, kb->pitch,
    238  1.16       leo 							kb->duration);
    239  1.16       leo 			kbdbell();
    240  1.16       leo 			return 0;
    241   1.1       leo 
    242   1.1       leo 		case FIONBIO:	/* we will remove this someday (soon???) */
    243   1.1       leo 			return 0;
    244   1.1       leo 
    245   1.1       leo 		case FIOASYNC:
    246   1.1       leo 			k->k_events.ev_async = *(int *)data != 0;
    247   1.1       leo 				return 0;
    248   1.1       leo 
    249   1.1       leo 		case TIOCSPGRP:
    250   1.3       leo 			if (*(int *)data != k->k_events.ev_io->p_pgid)
    251   1.1       leo 				return EPERM;
    252   1.1       leo 			return 0;
    253   1.1       leo 
    254   1.1       leo 		default:
    255   1.1       leo 			return ENOTTY;
    256   1.1       leo 	}
    257   1.1       leo 
    258   1.1       leo 	/*
    259   1.1       leo 	 * We identified the ioctl, but we do not handle it.
    260   1.1       leo 	 */
    261   1.1       leo 	return EOPNOTSUPP;		/* misuse, but what the heck */
    262   1.1       leo }
    263   1.1       leo 
    264   1.1       leo int
    265  1.13       leo kbdpoll (dev_t dev, int events, struct proc *p)
    266   1.1       leo {
    267  1.13       leo   return ev_poll (&kbd_softc.k_events, events, p);
    268   1.1       leo }
    269   1.1       leo 
    270   1.1       leo /*
    271   1.3       leo  * Keyboard interrupt handler called straight from MFP at spl6.
    272   1.1       leo  */
    273   1.6       leo void
    274   1.1       leo kbdintr(sr)
    275   1.1       leo int sr;	/* sr at time of interrupt	*/
    276   1.1       leo {
    277   1.1       leo 	int	code;
    278   1.3       leo 	int	got_char = 0;
    279   1.1       leo 
    280   1.1       leo 	/*
    281   1.1       leo 	 * There may be multiple keys available. Read them all.
    282   1.1       leo 	 */
    283   1.3       leo 	while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
    284   1.3       leo 		got_char = 1;
    285   1.3       leo 		if (KBD->ac_cs & (A_OE|A_PE)) {
    286   1.3       leo 			code = KBD->ac_da;	/* Silently ignore errors */
    287   1.1       leo 			continue;
    288   1.1       leo 		}
    289   1.1       leo 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
    290   1.1       leo 	}
    291   1.3       leo 
    292   1.3       leo 	/*
    293   1.3       leo 	 * If characters are waiting for transmit, send them.
    294   1.3       leo 	 */
    295   1.3       leo 	if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
    296   1.3       leo 		if (kbd_softc.k_sendp != NULL)
    297   1.3       leo 			KBD->ac_da = *kbd_softc.k_sendp++;
    298   1.3       leo 		if (--kbd_softc.k_send_cnt <= 0) {
    299   1.3       leo 			/*
    300   1.3       leo 			 * The total package has been transmitted,
    301   1.3       leo 			 * wakeup anyone waiting for it.
    302   1.3       leo 			 */
    303   1.3       leo 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
    304   1.3       leo 			kbd_softc.k_sendp    = NULL;
    305   1.3       leo 			kbd_softc.k_send_cnt = 0;
    306   1.3       leo 			wakeup((caddr_t)&kbd_softc.k_send_cnt);
    307   1.3       leo 		}
    308   1.3       leo 	}
    309   1.3       leo 
    310   1.3       leo 	/*
    311   1.3       leo 	 * Activate software-level to handle possible input.
    312   1.3       leo 	 */
    313   1.3       leo 	if (got_char) {
    314   1.3       leo 		if (!BASEPRI(sr)) {
    315   1.3       leo 			if (!kbd_soft++)
    316   1.3       leo 				add_sicallback(kbdsoft, 0, 0);
    317   1.3       leo 		} else {
    318   1.3       leo 			spl1();
    319  1.10       leo 			kbdsoft(NULL, NULL);
    320   1.3       leo 		}
    321   1.1       leo 	}
    322   1.1       leo }
    323   1.1       leo 
    324   1.1       leo /*
    325   1.1       leo  * Keyboard soft interrupt handler
    326   1.1       leo  */
    327   1.1       leo void
    328  1.10       leo kbdsoft(junk1, junk2)
    329  1.10       leo void	*junk1, *junk2;
    330   1.1       leo {
    331   1.1       leo 	int			s;
    332   1.1       leo 	u_char			code;
    333   1.1       leo 	struct kbd_softc	*k = &kbd_softc;
    334   1.1       leo 	struct firm_event	*fe;
    335   1.1       leo 	int			put;
    336   1.1       leo 	int			n, get;
    337   1.1       leo 
    338   1.1       leo 	kbd_soft = 0;
    339   1.1       leo 	get      = kbd_rbget;
    340   1.1       leo 
    341   1.3       leo 	for (;;) {
    342   1.1       leo 		n = kbd_rbput;
    343   1.3       leo 		if (get == n) /* We're done	*/
    344   1.1       leo 			break;
    345   1.1       leo 		n -= get;
    346   1.3       leo 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
    347   1.1       leo 			get += n - KBD_RING_SIZE;
    348   1.1       leo 			n    = KBD_RING_SIZE;
    349   1.1       leo 		}
    350   1.3       leo 		while (--n >= 0) {
    351   1.1       leo 			code = kbd_ring[get++ & KBD_RING_MASK];
    352   1.1       leo 
    353   1.1       leo 			/*
    354   1.3       leo 			 * If collecting a package, stuff it in and
    355   1.3       leo 			 * continue.
    356   1.3       leo 			 */
    357   1.3       leo 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
    358   1.9       leo 			    k->k_package[k->k_pkg_idx++] = code;
    359   1.9       leo 			    if (k->k_pkg_idx == k->k_pkg_size) {
    360   1.9       leo 				/*
    361   1.9       leo 				 * Package is complete.
    362   1.9       leo 				 */
    363   1.9       leo 				switch(k->k_pkg_type) {
    364   1.5       leo #if NMOUSE > 0
    365   1.9       leo 				    case KBD_AMS_PKG:
    366   1.9       leo 				    case KBD_RMS_PKG:
    367   1.9       leo 				    case KBD_JOY1_PKG:
    368   1.9       leo 			 		mouse_soft((REL_MOUSE *)k->k_package,
    369   1.9       leo 						k->k_pkg_size, k->k_pkg_type);
    370   1.5       leo #endif /* NMOUSE */
    371   1.3       leo 				}
    372   1.9       leo 				k->k_pkg_size = 0;
    373   1.9       leo 			    }
    374   1.9       leo 			    continue;
    375   1.3       leo 			}
    376   1.3       leo 			/*
    377   1.3       leo 			 * If this is a package header, init pkg. handling.
    378   1.3       leo 			 */
    379  1.15       leo 			if (!KBD_IS_KEY(code)) {
    380   1.3       leo 				kbd_pkg_start(k, code);
    381   1.3       leo 				continue;
    382   1.3       leo 			}
    383  1.15       leo 			if (kbd_do_modifier(code) && !k->k_event_mode)
    384  1.15       leo 				continue;
    385  1.15       leo 
    386   1.3       leo 			/*
    387   1.1       leo 			 * if not in event mode, deliver straight to ite to
    388   1.1       leo 			 * process key stroke
    389   1.1       leo 			 */
    390   1.3       leo 			if (!k->k_event_mode) {
    391   1.1       leo 				/* Gets to spltty() by itself	*/
    392   1.1       leo 				ite_filter(code, ITEFILT_TTY);
    393   1.1       leo 				continue;
    394   1.1       leo 			}
    395   1.1       leo 
    396   1.1       leo 			/*
    397   1.1       leo 			 * Keyboard is generating events.  Turn this keystroke
    398   1.1       leo 			 * into an event and put it in the queue.  If the queue
    399   1.1       leo 			 * is full, the keystroke is lost (sorry!).
    400   1.1       leo 			 */
    401   1.1       leo 			s = spltty();
    402   1.1       leo 			put = k->k_events.ev_put;
    403   1.1       leo 			fe  = &k->k_events.ev_q[put];
    404   1.1       leo 			put = (put + 1) % EV_QSIZE;
    405   1.3       leo 			if (put == k->k_events.ev_get) {
    406   1.1       leo 				log(LOG_WARNING,
    407   1.1       leo 					"keyboard event queue overflow\n");
    408   1.1       leo 				splx(s);
    409   1.1       leo 				continue;
    410   1.1       leo 			}
    411  1.15       leo 			fe->id    = KBD_SCANCODE(code);
    412  1.15       leo 			fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
    413  1.15       leo 			fe->time  = time;
    414   1.1       leo 			k->k_events.ev_put = put;
    415   1.1       leo 			EV_WAKEUP(&k->k_events);
    416   1.1       leo 			splx(s);
    417   1.1       leo 		}
    418   1.1       leo 		kbd_rbget = get;
    419   1.1       leo 	}
    420   1.1       leo }
    421   1.1       leo 
    422  1.16       leo static	u_char sound[] = {
    423   1.1       leo 	0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
    424   1.1       leo 	0xF8,0x10,0x10,0x10,0x00,0x20,0x03
    425   1.1       leo };
    426   1.1       leo 
    427   1.6       leo void
    428   1.1       leo kbdbell()
    429   1.1       leo {
    430   1.3       leo 	register int	i, sps;
    431   1.1       leo 
    432   1.9       leo 	sps = splhigh();
    433   1.3       leo 	for (i = 0; i < sizeof(sound); i++) {
    434   1.8       leo 		YM2149->sd_selr = i;
    435   1.8       leo 		YM2149->sd_wdat = sound[i];
    436   1.3       leo 	}
    437   1.3       leo 	splx(sps);
    438  1.16       leo }
    439  1.16       leo 
    440  1.16       leo 
    441  1.16       leo /*
    442  1.16       leo  * Set the parameters of the 'default' beep.
    443  1.16       leo  */
    444  1.16       leo 
    445  1.16       leo #define KBDBELLCLOCK	125000	/* 2MHz / 16 */
    446  1.16       leo #define KBDBELLDURATION	128	/* 256 / 2MHz */
    447  1.16       leo 
    448  1.16       leo void
    449  1.16       leo kbd_bell_gparms(volume, pitch, duration)
    450  1.16       leo 	u_int	*volume, *pitch, *duration;
    451  1.16       leo {
    452  1.16       leo 	u_int	tmp;
    453  1.16       leo 
    454  1.16       leo 	tmp = sound[11] | (sound[12] << 8);
    455  1.16       leo 	*duration = (tmp * KBDBELLDURATION) / 1000;
    456  1.16       leo 
    457  1.16       leo 	tmp = sound[0] | (sound[1] << 8);
    458  1.16       leo 	*pitch = KBDBELLCLOCK / tmp;
    459  1.16       leo 
    460  1.16       leo 	*volume = 0;
    461  1.16       leo }
    462  1.16       leo 
    463  1.16       leo void
    464  1.16       leo kbd_bell_sparms(volume, pitch, duration)
    465  1.16       leo 	u_int	volume, pitch, duration;
    466  1.16       leo {
    467  1.16       leo 	u_int	f, t;
    468  1.16       leo 
    469  1.16       leo 	f = pitch > 10 ? pitch : 10;	/* minimum pitch */
    470  1.16       leo 	if (f > 20000)
    471  1.16       leo 		f = 20000;		/* maximum pitch */
    472  1.16       leo 
    473  1.16       leo 	f = KBDBELLCLOCK / f;
    474  1.16       leo 
    475  1.16       leo 	t = (duration * 1000) / KBDBELLDURATION;
    476  1.16       leo 
    477  1.16       leo 	sound[ 0] = f & 0xff;
    478  1.16       leo 	sound[ 1] = (f >> 8) & 0xf;
    479  1.16       leo 	f -= 1;
    480  1.16       leo 	sound[ 2] = f & 0xff;
    481  1.16       leo 	sound[ 3] = (f >> 8) & 0xf;
    482  1.16       leo 	f += 2;
    483  1.16       leo 	sound[ 4] = f & 0xff;
    484  1.16       leo 	sound[ 5] = (f >> 8) & 0xf;
    485  1.16       leo 
    486  1.16       leo 	sound[11] = t & 0xff;
    487  1.16       leo 	sound[12] = (t >> 8) & 0xff;
    488  1.16       leo 
    489  1.16       leo 	sound[13] = 0x03;
    490   1.1       leo }
    491   1.1       leo 
    492   1.1       leo int
    493   1.1       leo kbdgetcn()
    494   1.1       leo {
    495   1.1       leo 	u_char	code;
    496   1.4       leo 	int	s = spltty();
    497   1.4       leo 	int	ints_active;
    498   1.1       leo 
    499   1.4       leo 	ints_active = 0;
    500   1.4       leo 	if (MFP->mf_imrb & IB_AINT) {
    501   1.4       leo 		ints_active   = 1;
    502   1.4       leo 		MFP->mf_imrb &= ~IB_AINT;
    503   1.4       leo 	}
    504   1.3       leo 	for (;;) {
    505   1.4       leo 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
    506   1.3       leo 			;	/* Wait for key	*/
    507   1.3       leo 		if (KBD->ac_cs & (A_OE|A_PE)) {
    508   1.3       leo 			code = KBD->ac_da;	/* Silently ignore errors */
    509   1.3       leo 			continue;
    510   1.3       leo 		}
    511  1.15       leo 		code = KBD->ac_da;
    512  1.15       leo 		if (!kbd_do_modifier(code))
    513  1.15       leo 			break;
    514   1.3       leo 	}
    515   1.1       leo 
    516   1.4       leo 	if (ints_active) {
    517  1.18       leo 		MFP->mf_iprb  = (u_int8_t)~IB_AINT;
    518   1.4       leo 		MFP->mf_imrb |=  IB_AINT;
    519   1.4       leo 	}
    520   1.1       leo 
    521   1.1       leo 	splx (s);
    522   1.1       leo 	return code;
    523   1.3       leo }
    524   1.3       leo 
    525   1.3       leo /*
    526   1.3       leo  * Write a command to the keyboard in 'polled' mode.
    527   1.3       leo  */
    528   1.3       leo static int
    529   1.4       leo kbd_write_poll(cmd, len)
    530   1.3       leo u_char	*cmd;
    531   1.3       leo int	len;
    532   1.3       leo {
    533   1.3       leo 	int	timeout;
    534   1.3       leo 
    535   1.3       leo 	while (len-- > 0) {
    536   1.3       leo 		KBD->ac_da = *cmd++;
    537   1.3       leo 		for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
    538   1.3       leo 			delay(10);
    539   1.3       leo 		if (!(KBD->ac_cs & A_TXRDY))
    540   1.3       leo 			return (0);
    541   1.3       leo 	}
    542   1.3       leo 	return (1);
    543   1.3       leo }
    544   1.3       leo 
    545   1.3       leo /*
    546   1.3       leo  * Write a command to the keyboard. Return when command is send.
    547   1.3       leo  */
    548   1.4       leo void
    549   1.3       leo kbd_write(cmd, len)
    550   1.3       leo u_char	*cmd;
    551   1.3       leo int	len;
    552   1.3       leo {
    553   1.3       leo 	struct kbd_softc	*k = &kbd_softc;
    554   1.3       leo 	int			sps;
    555   1.3       leo 
    556   1.3       leo 	/*
    557   1.3       leo 	 * Get to splhigh, 'real' interrupts arrive at spl6!
    558   1.3       leo 	 */
    559   1.3       leo 	sps = splhigh();
    560   1.3       leo 
    561   1.3       leo 	/*
    562   1.3       leo 	 * Make sure any privious write has ended...
    563   1.3       leo 	 */
    564   1.3       leo 	while (k->k_sendp != NULL)
    565   1.3       leo 		tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
    566   1.3       leo 
    567   1.3       leo 	/*
    568   1.3       leo 	 * If the KBD-acia is not currently busy, send the first
    569   1.3       leo 	 * character now.
    570   1.3       leo 	 */
    571   1.3       leo 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
    572   1.3       leo 	if (KBD->ac_cs & A_TXRDY) {
    573   1.3       leo 		KBD->ac_da = *cmd++;
    574   1.3       leo 		len--;
    575   1.3       leo 	}
    576   1.3       leo 
    577   1.3       leo 	/*
    578   1.3       leo 	 * If we're not yet done, wait until all characters are send.
    579   1.3       leo 	 */
    580   1.3       leo 	if (len > 0) {
    581   1.3       leo 		k->k_sendp    = cmd;
    582   1.3       leo 		k->k_send_cnt = len;
    583   1.3       leo 		tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
    584   1.3       leo 	}
    585   1.3       leo 	splx(sps);
    586   1.3       leo 
    587   1.3       leo 	/*
    588   1.3       leo 	 * Wakeup all procs waiting for us.
    589   1.3       leo 	 */
    590   1.3       leo 	wakeup((caddr_t)&k->k_sendp);
    591   1.3       leo }
    592   1.3       leo 
    593   1.3       leo /*
    594   1.3       leo  * Setup softc-fields to assemble a keyboard package.
    595   1.3       leo  */
    596   1.3       leo static void
    597   1.3       leo kbd_pkg_start(kp, msg_start)
    598   1.3       leo struct kbd_softc *kp;
    599   1.3       leo u_char		 msg_start;
    600   1.3       leo {
    601   1.3       leo 	kp->k_pkg_idx    = 1;
    602   1.3       leo 	kp->k_package[0] = msg_start;
    603   1.3       leo 	switch (msg_start) {
    604   1.3       leo 		case 0xf6:
    605   1.9       leo 			kp->k_pkg_type = KBD_MEM_PKG;
    606   1.3       leo 			kp->k_pkg_size = 8;
    607   1.3       leo 			break;
    608   1.3       leo 		case 0xf7:
    609   1.9       leo 			kp->k_pkg_type = KBD_AMS_PKG;
    610   1.3       leo 			kp->k_pkg_size = 6;
    611   1.3       leo 			break;
    612   1.3       leo 		case 0xf8:
    613   1.3       leo 		case 0xf9:
    614   1.3       leo 		case 0xfa:
    615   1.3       leo 		case 0xfb:
    616   1.9       leo 			kp->k_pkg_type = KBD_RMS_PKG;
    617   1.3       leo 			kp->k_pkg_size = 3;
    618   1.3       leo 			break;
    619   1.3       leo 		case 0xfc:
    620   1.9       leo 			kp->k_pkg_type = KBD_CLK_PKG;
    621   1.3       leo 			kp->k_pkg_size = 7;
    622   1.3       leo 			break;
    623   1.3       leo 		case 0xfe:
    624   1.9       leo 			kp->k_pkg_type = KBD_JOY0_PKG;
    625   1.9       leo 			kp->k_pkg_size = 2;
    626   1.9       leo 			break;
    627   1.3       leo 		case 0xff:
    628   1.9       leo 			kp->k_pkg_type = KBD_JOY1_PKG;
    629   1.3       leo 			kp->k_pkg_size = 2;
    630   1.3       leo 			break;
    631   1.3       leo 		default:
    632  1.12  christos 			printf("kbd: Unknown packet 0x%x\n", msg_start);
    633   1.3       leo 			break;
    634   1.3       leo 	}
    635   1.1       leo }
    636  1.15       leo 
    637  1.15       leo /*
    638  1.15       leo  * Modifier processing
    639  1.15       leo  */
    640  1.15       leo static int
    641  1.15       leo kbd_do_modifier(code)
    642  1.15       leo u_char	code;
    643  1.15       leo {
    644  1.15       leo 	u_char	up, mask;
    645  1.15       leo 
    646  1.15       leo 	up   = KBD_RELEASED(code);
    647  1.15       leo 	mask = 0;
    648  1.15       leo 
    649  1.15       leo 	switch(KBD_SCANCODE(code)) {
    650  1.15       leo 		case KBD_LEFT_SHIFT:
    651  1.15       leo 			mask = KBD_MOD_LSHIFT;
    652  1.15       leo 			break;
    653  1.15       leo 		case KBD_RIGHT_SHIFT:
    654  1.15       leo 			mask = KBD_MOD_RSHIFT;
    655  1.15       leo 			break;
    656  1.15       leo 		case KBD_CTRL:
    657  1.15       leo 			mask = KBD_MOD_CTRL;
    658  1.15       leo 			break;
    659  1.15       leo 		case KBD_ALT:
    660  1.15       leo 			mask = KBD_MOD_ALT;
    661  1.15       leo 			break;
    662  1.15       leo 		case KBD_CAPS_LOCK:
    663  1.15       leo 			/* CAPSLOCK is a toggle */
    664  1.15       leo 			if(!up)
    665  1.15       leo 				kbd_modifier ^= KBD_MOD_CAPS;
    666  1.15       leo 			return 1;
    667  1.15       leo 	}
    668  1.15       leo 	if(mask) {
    669  1.15       leo 		if(up)
    670  1.15       leo 			kbd_modifier &= ~mask;
    671  1.15       leo 		else
    672  1.15       leo 			kbd_modifier |= mask;
    673  1.15       leo 		return 1;
    674  1.15       leo 	}
    675  1.15       leo 	return 0;
    676  1.15       leo }
    677