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