Home | History | Annotate | Line # | Download | only in dev
kbd.c revision 1.45
      1  1.45       agc /*	$NetBSD: kbd.c,v 1.45 2003/08/07 16:26:42 agc Exp $ */
      2  1.11       cgd 
      3   1.1        mw /*
      4   1.1        mw  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
      5   1.1        mw  * All rights reserved.
      6   1.1        mw  *
      7   1.1        mw  * Redistribution and use in source and binary forms, with or without
      8   1.1        mw  * modification, are permitted provided that the following conditions
      9   1.1        mw  * are met:
     10   1.1        mw  * 1. Redistributions of source code must retain the above copyright
     11   1.1        mw  *    notice, this list of conditions and the following disclaimer.
     12   1.1        mw  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1        mw  *    notice, this list of conditions and the following disclaimer in the
     14   1.1        mw  *    documentation and/or other materials provided with the distribution.
     15  1.45       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1        mw  *    may be used to endorse or promote products derived from this software
     17   1.1        mw  *    without specific prior written permission.
     18   1.1        mw  *
     19   1.1        mw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1        mw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1        mw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1        mw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1        mw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1        mw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1        mw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1        mw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1        mw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1        mw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1        mw  * SUCH DAMAGE.
     30   1.1        mw  *
     31   1.4        mw  *	kbd.c
     32   1.1        mw  */
     33  1.38   aymeric 
     34  1.38   aymeric #include <sys/cdefs.h>
     35  1.45       agc __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.45 2003/08/07 16:26:42 agc Exp $");
     36  1.38   aymeric 
     37   1.7    chopps #include <sys/param.h>
     38   1.7    chopps #include <sys/systm.h>
     39   1.9    chopps #include <sys/device.h>
     40   1.7    chopps #include <sys/ioctl.h>
     41   1.7    chopps #include <sys/tty.h>
     42   1.7    chopps #include <sys/proc.h>
     43   1.7    chopps #include <sys/file.h>
     44   1.7    chopps #include <sys/kernel.h>
     45   1.7    chopps #include <sys/syslog.h>
     46  1.18     veego #include <sys/signalvar.h>
     47  1.40   gehenna #include <sys/conf.h>
     48   1.8    chopps #include <dev/cons.h>
     49   1.7    chopps #include <machine/cpu.h>
     50   1.9    chopps #include <amiga/amiga/device.h>
     51   1.7    chopps #include <amiga/amiga/custom.h>
     52  1.19        is #ifdef DRACO
     53  1.34        is #include <m68k/asm_single.h>
     54  1.19        is #include <amiga/amiga/drcustom.h>
     55  1.19        is #endif
     56   1.7    chopps #include <amiga/amiga/cia.h>
     57   1.9    chopps #include <amiga/dev/itevar.h>
     58   1.9    chopps #include <amiga/dev/kbdreg.h>
     59  1.18     veego #include <amiga/dev/kbdmap.h>
     60   1.7    chopps #include <amiga/dev/event_var.h>
     61   1.7    chopps #include <amiga/dev/vuid_event.h>
     62  1.35        is 
     63   1.9    chopps #include "kbd.h"
     64  1.35        is #include "ite.h"
     65  1.35        is 
     66  1.35        is /* WSKBD */
     67  1.35        is 
     68  1.37   aymeric /*
     69  1.35        is  * If NWSKBD>0 we try to attach an wskbd device to us. What follows
     70  1.35        is  * is definitions of callback functions and structures that are passed
     71  1.35        is  * to wscons when initializing.
     72  1.35        is  */
     73  1.35        is 
     74  1.36  jandberg /*
     75  1.36  jandberg  * Now with wscons this driver exhibits some weird behaviour.
     76  1.36  jandberg  * It may act both as a driver of its own and the md part of the
     77  1.36  jandberg  * wskbd driver. Therefore it can be accessed through /dev/kbd
     78  1.36  jandberg  * and /dev/wskbd0 both.
     79  1.36  jandberg  *
     80  1.36  jandberg  * The data from they keyboard may end up in at least four different
     81  1.36  jandberg  * places:
     82  1.37   aymeric  * - If this driver has been opened (/dev/kbd) and the
     83  1.37   aymeric  *   direct mode (TIOCDIRECT) has been set, data goes to
     84  1.36  jandberg  *   the process who opened the device. Data will transmit itself
     85  1.36  jandberg  *   as described by the firm_event structure.
     86  1.36  jandberg  * - If wskbd support is compiled in and a wskbd driver has been
     87  1.36  jandberg  *   attached then the data is sent to it. Wskbd in turn may
     88  1.36  jandberg  *   - Send the data in the wscons_event form to a process that
     89  1.36  jandberg  *     has opened /dev/wskbd0
     90  1.36  jandberg  *   - Feed the data to a virtual terminal.
     91  1.36  jandberg  * - If an ite is present the data may be fed to it.
     92  1.36  jandberg  */
     93  1.36  jandberg 
     94  1.35        is #include "wskbd.h"
     95  1.35        is 
     96  1.35        is #if NWSKBD>0
     97  1.35        is #include <dev/wscons/wsconsio.h>
     98  1.35        is #include <dev/wscons/wskbdvar.h>
     99  1.35        is #include <dev/wscons/wsksymdef.h>
    100  1.35        is #include <dev/wscons/wsksymvar.h>
    101  1.35        is #include <amiga/dev/wskbdmap_amiga.h>
    102  1.35        is 
    103  1.35        is /* accessops */
    104  1.37   aymeric int     kbd_enable(void *, int);
    105  1.37   aymeric void    kbd_set_leds(void *, int);
    106  1.37   aymeric int     kbd_ioctl(void *, u_long, caddr_t, int, struct proc *);
    107  1.35        is 
    108  1.35        is /* console ops */
    109  1.37   aymeric void    kbd_getc(void *, u_int *, int *);
    110  1.37   aymeric void    kbd_pollc(void *, int);
    111  1.37   aymeric void    kbd_bell(void *, u_int, u_int, u_int);
    112  1.35        is 
    113  1.35        is static struct wskbd_accessops kbd_accessops = {
    114  1.35        is 	kbd_enable,
    115  1.35        is 	kbd_set_leds,
    116  1.35        is 	kbd_ioctl
    117  1.35        is };
    118  1.35        is 
    119  1.35        is static struct wskbd_consops kbd_consops = {
    120  1.35        is 	kbd_getc,
    121  1.35        is 	kbd_pollc,
    122  1.35        is 	kbd_bell
    123  1.35        is };
    124  1.35        is 
    125  1.35        is /*
    126  1.36  jandberg  * Pointer to keymaps. They are defined in wskbdmap_amiga.c.
    127  1.35        is  */
    128  1.35        is static struct wskbd_mapdata kbd_mapdata = {
    129  1.35        is 	amigakbd_keydesctab,
    130  1.35        is 	KB_US
    131  1.35        is };
    132  1.35        is 
    133  1.35        is #endif /* WSKBD */
    134  1.35        is 
    135   1.5        mw struct kbd_softc {
    136   1.9    chopps 	int k_event_mode;	/* if true, collect events, else pass to ite */
    137   1.9    chopps 	struct evvar k_events;	/* event queue state */
    138  1.19        is #ifdef DRACO
    139  1.19        is 	u_char k_rlprfx;	/* MF-II rel. prefix has been seen */
    140  1.31        is 	u_char k_mf2;
    141  1.19        is #endif
    142  1.35        is 
    143  1.35        is #if NWSKBD>0
    144  1.35        is 	struct device *k_wskbddev; /* pointer to wskbd for sending strokes */
    145  1.35        is 	int k_pollingmode;         /* polling mode on? whatever it isss... */
    146  1.35        is #endif
    147   1.9    chopps };
    148   1.9    chopps struct kbd_softc kbd_softc;
    149   1.9    chopps 
    150  1.37   aymeric int kbdmatch(struct device *, struct cfdata *, void *);
    151  1.37   aymeric void kbdattach(struct device *, struct device *, void *);
    152  1.37   aymeric void kbdintr(int);
    153  1.37   aymeric void kbdstuffchar(u_char);
    154  1.37   aymeric 
    155  1.37   aymeric int drkbdgetc(void);
    156  1.37   aymeric int drkbdrputc(u_int8_t);
    157  1.37   aymeric int drkbdputc(u_int8_t);
    158  1.37   aymeric int drkbdputc2(u_int8_t, u_int8_t);
    159  1.37   aymeric int drkbdwaitfor(int);
    160  1.31        is 
    161  1.42   thorpej CFATTACH_DECL(kbd, sizeof(struct device),
    162  1.42   thorpej     kbdmatch, kbdattach, NULL, NULL);
    163  1.40   gehenna 
    164  1.40   gehenna dev_type_open(kbdopen);
    165  1.40   gehenna dev_type_close(kbdclose);
    166  1.40   gehenna dev_type_read(kbdread);
    167  1.40   gehenna dev_type_ioctl(kbdioctl);
    168  1.40   gehenna dev_type_poll(kbdpoll);
    169  1.43  jdolecek dev_type_kqfilter(kbdkqfilter);
    170  1.40   gehenna 
    171  1.40   gehenna const struct cdevsw kbd_cdevsw = {
    172  1.40   gehenna 	kbdopen, kbdclose, kbdread, nowrite, kbdioctl,
    173  1.43  jdolecek 	nostop, notty, kbdpoll, nommap, kbdkqfilter,
    174  1.16   thorpej };
    175   1.9    chopps 
    176   1.9    chopps /*ARGSUSED*/
    177   1.9    chopps int
    178  1.37   aymeric kbdmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
    179   1.9    chopps {
    180  1.16   thorpej 
    181   1.9    chopps 	if (matchname((char *)auxp, "kbd"))
    182   1.9    chopps 		return(1);
    183   1.9    chopps 	return(0);
    184   1.9    chopps }
    185   1.9    chopps 
    186   1.9    chopps /*ARGSUSED*/
    187   1.9    chopps void
    188  1.37   aymeric kbdattach(struct device *pdp, struct device *dp, void *auxp)
    189   1.9    chopps {
    190  1.19        is #ifdef DRACO
    191  1.31        is 	kbdenable();
    192  1.31        is 	if (kbd_softc.k_mf2)
    193  1.31        is 		printf(": QuickLogic type MF-II\n");
    194  1.31        is 	else
    195  1.31        is 		printf(": CIA A type Amiga\n");
    196  1.19        is #else
    197  1.26  christos 	printf(": CIA A type Amiga\n");
    198  1.19        is #endif
    199  1.19        is 
    200  1.35        is #if NWSKBD>0
    201  1.35        is 	if (dp != NULL) {
    202  1.35        is 		/*
    203  1.35        is 		 * Try to attach the wskbd.
    204  1.35        is 		 */
    205  1.35        is 		struct wskbddev_attach_args waa;
    206  1.35        is 
    207  1.35        is 		/* Maybe should be done before this?... */
    208  1.35        is 		wskbd_cnattach(&kbd_consops, NULL, &kbd_mapdata);
    209  1.35        is 
    210  1.35        is 		waa.console = 1;
    211  1.35        is 		waa.keymap = &kbd_mapdata;
    212  1.35        is 		waa.accessops = &kbd_accessops;
    213  1.35        is 		waa.accesscookie = NULL;
    214  1.35        is 		kbd_softc.k_wskbddev = config_found(dp, &waa, wskbddevprint);
    215  1.35        is 
    216  1.35        is 		kbd_softc.k_pollingmode = 0;
    217  1.35        is 	}
    218  1.35        is 	kbdenable();
    219  1.35        is #endif /* WSKBD */
    220   1.9    chopps }
    221   1.5        mw 
    222   1.5        mw /* definitions for amiga keyboard encoding. */
    223   1.5        mw #define KEY_CODE(c)  ((c) & 0x7f)
    224   1.5        mw #define KEY_UP(c)    ((c) & 0x80)
    225   1.5        mw 
    226  1.31        is #define DATLO single_inst_bclr_b(draco_ioct->io_control, DRCNTRL_KBDDATOUT)
    227  1.31        is #define DATHI single_inst_bset_b(draco_ioct->io_control, DRCNTRL_KBDDATOUT)
    228  1.31        is 
    229  1.31        is #define CLKLO single_inst_bclr_b(draco_ioct->io_control, DRCNTRL_KBDCLKOUT)
    230  1.31        is #define CLKHI single_inst_bset_b(draco_ioct->io_control, DRCNTRL_KBDCLKOUT)
    231  1.31        is 
    232   1.1        mw void
    233  1.37   aymeric kbdenable(void)
    234   1.1        mw {
    235  1.30        is 	static int kbd_inited = 0;
    236  1.30        is 
    237   1.9    chopps 	int s;
    238  1.22        is 
    239  1.21        is #ifdef DRACO
    240  1.31        is 	int id;
    241  1.21        is #endif
    242   1.9    chopps 	/*
    243   1.9    chopps 	 * collides with external ints from SCSI, watch out for this when
    244   1.9    chopps 	 * enabling/disabling interrupts there !!
    245   1.9    chopps 	 */
    246  1.30        is 	s = splhigh();	/* don't lower; might be called from early ddb */
    247  1.30        is 	if (kbd_inited) {
    248  1.30        is 		splx(s);
    249  1.30        is 		return;
    250  1.30        is 	}
    251  1.30        is 	kbd_inited = 1;
    252  1.19        is #ifdef DRACO
    253  1.31        is 	if (is_draco()) {
    254  1.31        is 
    255  1.31        is 		CLKLO;
    256  1.31        is 		delay(5000);
    257  1.31        is 		draco_ioct->io_kbdrst = 0;
    258  1.31        is 
    259  1.31        is 		if (drkbdputc(0xf2))
    260  1.31        is 			goto LnoMFII;
    261  1.31        is 
    262  1.31        is 		id = drkbdgetc() << 8;
    263  1.31        is 		id |= drkbdgetc();
    264  1.31        is 
    265  1.31        is 		if (id != 0xab83)
    266  1.31        is 			goto LnoMFII;
    267  1.19        is 
    268  1.31        is 		if (drkbdputc2(0xf0, 3))	/* mode 3 */
    269  1.31        is 			goto LnoMFII;
    270  1.31        is 
    271  1.31        is 		if (drkbdputc(0xf8))		/* make/break, no typematic */
    272  1.31        is 			goto LnoMFII;
    273  1.31        is 
    274  1.31        is 		if (drkbdputc(0xf4))		/* enable */
    275  1.31        is 			goto LnoMFII;
    276  1.31        is 		kbd_softc.k_mf2 = 1;
    277  1.33        is 		single_inst_bclr_b(draco_ioct->io_control, DRCNTRL_KBDINTENA);
    278  1.19        is 
    279  1.31        is 		ciaa.icr = CIA_ICR_SP;  /* CIA SP interrupt disable */
    280  1.31        is 		ciaa.cra &= ~(1<<6);	/* serial line == input */
    281  1.31        is 		splx(s);
    282  1.31        is 		return;
    283  1.19        is 
    284  1.31        is 	LnoMFII:
    285  1.31        is 		kbd_softc.k_mf2 = 0;
    286  1.33        is 		single_inst_bset_b(*draco_intena, DRIRQ_INT2);
    287  1.31        is 		ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;
    288  1.31        is 					/* SP interrupt enable */
    289  1.31        is 		ciaa.cra &= ~(1<<6);	/* serial line == input */
    290  1.31        is 		splx(s);
    291  1.31        is 		return;
    292  1.37   aymeric 
    293  1.31        is 	} else {
    294  1.31        is #endif
    295   1.9    chopps 	custom.intena = INTF_SETCLR | INTF_PORTS;
    296   1.9    chopps 	ciaa.icr = CIA_ICR_IR_SC | CIA_ICR_SP;  /* SP interrupt enable */
    297   1.9    chopps 	ciaa.cra &= ~(1<<6);		/* serial line == input */
    298  1.31        is #ifdef DRACO
    299  1.31        is 	}
    300  1.19        is #endif
    301   1.9    chopps 	kbd_softc.k_event_mode = 0;
    302   1.9    chopps 	kbd_softc.k_events.ev_io = 0;
    303   1.9    chopps 	splx(s);
    304   1.1        mw }
    305   1.1        mw 
    306  1.32        is #ifdef DRACO
    307  1.31        is /*
    308  1.44       wiz  * call this with kbd interrupt blocked
    309  1.31        is  */
    310  1.31        is 
    311  1.31        is int
    312  1.37   aymeric drkbdgetc(void)
    313  1.31        is {
    314  1.31        is 	u_int8_t in;
    315  1.31        is 
    316  1.31        is 	while ((draco_ioct->io_status & DRSTAT_KBDRECV) == 0);
    317  1.31        is 	in = draco_ioct->io_kbddata;
    318  1.31        is 	draco_ioct->io_kbdrst = 0;
    319  1.31        is 
    320  1.31        is 	return in;
    321  1.31        is }
    322  1.31        is 
    323  1.31        is #define WAIT0 if (drkbdwaitfor(0)) goto Ltimeout
    324  1.31        is #define WAIT1 if (drkbdwaitfor(DRSTAT_KBDCLKIN)) goto Ltimeout
    325  1.31        is 
    326  1.31        is int
    327  1.37   aymeric drkbdwaitfor(int bit)
    328  1.31        is {
    329  1.31        is 	int i;
    330  1.31        is 
    331  1.31        is 
    332  1.31        is 
    333  1.31        is 	i = 60000;	/* about 50 ms max */
    334  1.31        is 
    335  1.31        is 	do {
    336  1.31        is 		if ((draco_ioct->io_status & DRSTAT_KBDCLKIN) == bit)
    337  1.31        is 			return 0;
    338  1.31        is 
    339  1.31        is 	} while (--i >= 0);
    340  1.31        is 
    341  1.31        is 	return 1;
    342  1.31        is }
    343  1.31        is 
    344  1.31        is /*
    345  1.31        is  * Output a raw byte to the keyboard (+ parity and stop bit).
    346  1.31        is  * return 0 on success, 1 on timeout.
    347  1.31        is  */
    348  1.31        is int
    349  1.37   aymeric drkbdrputc(u_int8_t c)
    350  1.31        is {
    351  1.31        is 	u_int8_t parity;
    352  1.31        is 	int bitcnt;
    353  1.31        is 
    354  1.31        is 	DATLO; CLKHI; WAIT1;
    355  1.31        is 	parity = 0;
    356  1.31        is 
    357  1.31        is 	for (bitcnt=7; bitcnt >= 0; bitcnt--) {
    358  1.31        is 		WAIT0;
    359  1.31        is 		if (c & 1) {
    360  1.31        is 			DATHI;
    361  1.31        is 		} else {
    362  1.31        is 			++parity;
    363  1.31        is 			DATLO;
    364  1.31        is 		}
    365  1.31        is 		c >>= 1;
    366  1.31        is 		WAIT1;
    367  1.31        is 	}
    368  1.31        is 	WAIT0;
    369  1.31        is 	/* parity bit */
    370  1.31        is 	if (parity & 1) {
    371  1.31        is 		DATLO;
    372  1.31        is 	} else {
    373  1.31        is 		DATHI;
    374  1.31        is 	}
    375  1.31        is 	WAIT1;
    376  1.31        is 	/* stop bit */
    377  1.31        is 	WAIT0; DATHI; WAIT1;
    378  1.31        is 
    379  1.31        is 	WAIT0; /* XXX should check the ack bit here... */
    380  1.31        is 	WAIT1;
    381  1.31        is 	draco_ioct->io_kbdrst = 0;
    382  1.31        is 	return 0;
    383  1.31        is 
    384  1.31        is Ltimeout:
    385  1.31        is 	DATHI;
    386  1.31        is 	draco_ioct->io_kbdrst = 0;
    387  1.31        is 	return 1;
    388  1.31        is }
    389  1.31        is 
    390  1.31        is /*
    391  1.31        is  * Output one cooked byte to the keyboard, with wait for ACK or RESEND,
    392  1.31        is  * and retry if necessary. 0 == success, 1 == timeout
    393  1.31        is  */
    394  1.31        is int
    395  1.37   aymeric drkbdputc(u_int8_t c)
    396  1.31        is {
    397  1.31        is 	int rc;
    398  1.31        is 
    399  1.31        is 	do {
    400  1.31        is 		if (drkbdrputc(c))
    401  1.31        is 			return(-1);
    402  1.31        is 
    403  1.31        is 		rc = drkbdgetc();
    404  1.31        is 	} while (rc == 0xfe);
    405  1.31        is 	return (!(rc == 0xfa));
    406  1.31        is }
    407  1.31        is 
    408  1.31        is /*
    409  1.31        is  * same for twobyte sequence
    410  1.31        is  */
    411  1.31        is 
    412  1.31        is int
    413  1.37   aymeric drkbdputc2(u_int8_t c1, u_int8_t c2)
    414  1.31        is {
    415  1.31        is 	int rc;
    416  1.31        is 
    417  1.31        is 	do {
    418  1.31        is 		do {
    419  1.31        is 			if (drkbdrputc(c1))
    420  1.31        is 				return(-1);
    421  1.31        is 
    422  1.31        is 			rc = drkbdgetc();
    423  1.31        is 		} while (rc == 0xfe);
    424  1.31        is 		if (rc != 0xfa)
    425  1.31        is 			return (-1);
    426  1.31        is 
    427  1.31        is 		if (drkbdrputc(c2))
    428  1.31        is 			return(-1);
    429  1.31        is 
    430  1.31        is 		rc = drkbdgetc();
    431  1.31        is 	} while (rc == 0xfe);
    432  1.31        is 	return (!(rc == 0xfa));
    433  1.31        is }
    434  1.32        is #endif
    435   1.1        mw 
    436   1.1        mw int
    437  1.37   aymeric kbdopen(dev_t dev, int flags, int mode, struct proc *p)
    438   1.5        mw {
    439   1.5        mw 
    440  1.30        is 	kbdenable();
    441  1.18     veego 	if (kbd_softc.k_events.ev_io)
    442  1.18     veego 		return EBUSY;
    443   1.5        mw 
    444  1.18     veego 	kbd_softc.k_events.ev_io = p;
    445  1.18     veego 	ev_init(&kbd_softc.k_events);
    446  1.18     veego 	return (0);
    447   1.5        mw }
    448   1.5        mw 
    449   1.5        mw int
    450  1.37   aymeric kbdclose(dev_t dev, int flags, int mode, struct proc *p)
    451   1.5        mw {
    452  1.18     veego 
    453  1.18     veego 	/* Turn off event mode, dump the queue */
    454  1.18     veego 	kbd_softc.k_event_mode = 0;
    455  1.18     veego 	ev_fini(&kbd_softc.k_events);
    456  1.18     veego 	kbd_softc.k_events.ev_io = NULL;
    457  1.18     veego 	return (0);
    458   1.5        mw }
    459   1.5        mw 
    460   1.5        mw int
    461  1.37   aymeric kbdread(dev_t dev, struct uio *uio, int flags)
    462   1.5        mw {
    463  1.18     veego 	return ev_read (&kbd_softc.k_events, uio, flags);
    464   1.5        mw }
    465   1.5        mw 
    466   1.5        mw int
    467  1.37   aymeric kbdioctl(dev_t dev, u_long cmd, register caddr_t data, int flag,
    468  1.37   aymeric          struct proc *p)
    469   1.5        mw {
    470  1.18     veego 	register struct kbd_softc *k = &kbd_softc;
    471  1.18     veego 
    472  1.18     veego 	switch (cmd) {
    473  1.18     veego 		case KIOCTRANS:
    474  1.18     veego 			if (*(int *)data == TR_UNTRANS_EVENT)
    475  1.18     veego 				return 0;
    476  1.18     veego 			break;
    477  1.18     veego 
    478  1.18     veego 		case KIOCGTRANS:
    479  1.18     veego 			/* Get translation mode */
    480  1.18     veego 			*(int *)data = TR_UNTRANS_EVENT;
    481  1.18     veego 			return 0;
    482  1.18     veego 
    483  1.18     veego 		case KIOCSDIRECT:
    484  1.18     veego 			k->k_event_mode = *(int *)data;
    485  1.18     veego 			return 0;
    486  1.18     veego 
    487  1.18     veego 		case FIONBIO:	/* we will remove this someday (soon???) */
    488  1.18     veego 			return 0;
    489  1.18     veego 
    490  1.18     veego 		case FIOASYNC:
    491  1.18     veego 			k->k_events.ev_async = *(int *)data != 0;
    492  1.18     veego 			return 0;
    493  1.18     veego 
    494  1.18     veego 		case TIOCSPGRP:
    495  1.18     veego 			if (*(int *)data != k->k_events.ev_io->p_pgid)
    496  1.18     veego 				return EPERM;
    497  1.18     veego 			return 0;
    498  1.18     veego 
    499  1.18     veego 		default:
    500  1.18     veego 			return ENOTTY;
    501  1.18     veego 	}
    502   1.5        mw 
    503  1.18     veego 	/* We identified the ioctl, but we do not handle it. */
    504  1.18     veego 	return EOPNOTSUPP;	/* misuse, but what the heck */
    505   1.5        mw }
    506   1.5        mw 
    507   1.5        mw int
    508  1.37   aymeric kbdpoll(dev_t dev, int events, struct proc *p)
    509   1.5        mw {
    510  1.25    mhitch 	return ev_poll (&kbd_softc.k_events, events, p);
    511   1.5        mw }
    512   1.5        mw 
    513  1.43  jdolecek int
    514  1.43  jdolecek kbdkqfilter(dev, kn)
    515  1.43  jdolecek 	dev_t dev;
    516  1.43  jdolecek 	struct knote *kn;
    517  1.43  jdolecek {
    518  1.43  jdolecek 
    519  1.43  jdolecek 	return (ev_kqfilter(&kbd_softc.k_events, kn));
    520  1.43  jdolecek }
    521   1.5        mw 
    522  1.18     veego void
    523  1.37   aymeric kbdintr(int mask)
    524   1.1        mw {
    525  1.18     veego 	u_char c;
    526  1.13    chopps #ifdef KBDRESET
    527  1.18     veego 	static int reset_warn;
    528  1.13    chopps #endif
    529  1.37   aymeric 
    530  1.37   aymeric 	/*
    531  1.19        is 	 * now only invoked from generic CIA interrupt handler if there *is*
    532  1.18     veego 	 * a keyboard interrupt pending
    533  1.18     veego 	 */
    534  1.37   aymeric 
    535  1.18     veego 	c = ~ciaa.sdr;	/* keyboard data is inverted */
    536  1.18     veego 	/* ack */
    537  1.18     veego 	ciaa.cra |= (1 << 6);	/* serial line output */
    538  1.13    chopps #ifdef KBDRESET
    539  1.18     veego 	if (reset_warn && c == 0xf0) {
    540  1.13    chopps #ifdef DEBUG
    541  1.26  christos 		printf ("kbdintr: !!!! Reset Warning !!!!\n");
    542  1.13    chopps #endif
    543  1.18     veego 		bootsync();
    544  1.18     veego 		reset_warn = 0;
    545  1.18     veego 		DELAY(30000000);
    546  1.18     veego 	}
    547  1.13    chopps #endif
    548  1.18     veego 	/* wait 200 microseconds (for bloody Cherry keyboards..) */
    549  1.18     veego 	DELAY(2000);			/* fudge delay a bit for some keyboards */
    550  1.18     veego 	ciaa.cra &= ~(1 << 6);
    551  1.18     veego 
    552  1.18     veego 	/* process the character */
    553  1.18     veego 	c = (c >> 1) | (c << 7);	/* rotate right once */
    554   1.1        mw 
    555  1.13    chopps #ifdef KBDRESET
    556  1.18     veego 	if (c == 0x78) {
    557  1.13    chopps #ifdef DEBUG
    558  1.26  christos 		printf ("kbdintr: Reset Warning started\n");
    559  1.13    chopps #endif
    560  1.18     veego 		++reset_warn;
    561  1.18     veego 		return;
    562  1.18     veego 	}
    563  1.13    chopps #endif
    564  1.19        is 	kbdstuffchar(c);
    565  1.19        is }
    566  1.37   aymeric 
    567  1.19        is #ifdef DRACO
    568  1.19        is /* maps MF-II keycodes to Amiga keycodes */
    569  1.19        is 
    570  1.27        is const u_char drkbdtab[] = {
    571  1.19        is 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x50,
    572  1.19        is 	0x45, 0xff, 0xff, 0xff, 0xff, 0x42, 0x00, 0x51,
    573  1.19        is 
    574  1.19        is 	0xff, 0x64, 0x60, 0x30, 0x63, 0x10, 0x01, 0x52,
    575  1.19        is 	0xff, 0x66, 0x31, 0x21, 0x20, 0x11, 0x02, 0x53,
    576  1.19        is 
    577  1.19        is 	0xff, 0x33, 0x32, 0x22, 0x12, 0x04, 0x03, 0x54,
    578  1.19        is 	0xff, 0x40, 0x34, 0x23, 0x14, 0x13, 0x05, 0x55,
    579  1.19        is 
    580  1.19        is 	0xff, 0x36, 0x35, 0x25, 0x24, 0x15, 0x06, 0x56,
    581  1.19        is 	0xff, 0x67, 0x37, 0x26, 0x16, 0x07, 0x08, 0x57,
    582  1.19        is 	/* --- */
    583  1.19        is 	0xff, 0x38, 0x27, 0x17, 0x18, 0x0a, 0x09, 0x58,
    584  1.19        is 	0xff, 0x39, 0x3a, 0x28, 0x29, 0x19, 0x0b, 0x59,
    585  1.19        is 
    586  1.19        is 	0xff, 0xff, 0x2a, 0x2b, 0x1a, 0x0c, 0x4b, 0xff,
    587  1.19        is 	0x65, 0x61, 0x44, 0x1b, 0xff, 0xff, 0x6f, 0xff,
    588  1.19        is 
    589  1.19        is 	0x4d, 0x4f, 0xff, 0x4c, 0x0d, 0xff, 0x41, 0x46,
    590  1.19        is 	0xff, 0x1d, 0x4e, 0x2d, 0x3d, 0x4a, 0x5f, 0x62,
    591  1.19        is 
    592  1.19        is 	0x0f, 0x3c, 0x1e, 0x2e, 0x2f, 0x3e, 0x5a, 0x5b,
    593  1.19        is 	0xff, 0x43, 0x1f, 0xff, 0x5e, 0x3f, 0x5c, 0xff,
    594  1.19        is 	/* --- */
    595  1.19        is 	0xff, 0xff, 0xff, 0xff, 0x5d
    596  1.19        is };
    597  1.19        is #endif
    598  1.19        is 
    599  1.19        is 
    600  1.19        is int
    601  1.37   aymeric kbdgetcn(void)
    602  1.19        is {
    603  1.19        is 	int s;
    604  1.19        is 	u_char ints, mask, c, in;
    605  1.19        is 
    606  1.19        is #ifdef DRACO
    607  1.31        is 	if (is_draco() && kbd_softc.k_mf2) {
    608  1.31        is 		do {
    609  1.31        is 			c = 0;
    610  1.31        is 			s = spltty ();
    611  1.19        is 			while ((draco_ioct->io_status & DRSTAT_KBDRECV) == 0);
    612  1.19        is 			in = draco_ioct->io_kbddata;
    613  1.19        is 			draco_ioct->io_kbdrst = 0;
    614  1.31        is 			if (in == 0xF0) { /* release prefix */
    615  1.31        is 				c = 0x80;
    616  1.33        is 				while ((draco_ioct->io_status &
    617  1.33        is 				    DRSTAT_KBDRECV) == 0);
    618  1.31        is 				in = draco_ioct->io_kbddata;
    619  1.31        is 				draco_ioct->io_kbdrst = 0;
    620  1.31        is 			}
    621  1.31        is 			splx(s);
    622  1.19        is #ifdef DRACORAWKEYDEBUG
    623  1.31        is 			printf("<%02x>", in);
    624  1.19        is #endif
    625  1.31        is 			c |= in>=sizeof(drkbdtab) ? 0xff : drkbdtab[in];
    626  1.31        is 		} while (c == 0xff);
    627  1.31        is 		return (c);
    628  1.19        is 	}
    629  1.19        is #endif
    630  1.19        is 	s = spltty();
    631  1.37   aymeric 	for (ints = 0; ! ((mask = ciaa.icr) & CIA_ICR_SP);
    632  1.19        is 	    ints |= mask) ;
    633  1.19        is 
    634  1.19        is 	in = ciaa.sdr;
    635  1.19        is 	c = ~in;
    636  1.37   aymeric 
    637  1.19        is 	/* ack */
    638  1.19        is 	ciaa.cra |= (1 << 6);	/* serial line output */
    639  1.19        is 	ciaa.sdr = 0xff;	/* ack */
    640  1.19        is 	/* wait 200 microseconds */
    641  1.37   aymeric 	DELAY(2000);	/* XXXX only works as long as DELAY doesn't
    642  1.19        is 			 * use a timer and waits.. */
    643  1.19        is 	ciaa.cra &= ~(1 << 6);
    644  1.19        is 	ciaa.sdr = in;
    645  1.19        is 
    646  1.19        is 	splx (s);
    647  1.19        is 	c = (c >> 1) | (c << 7);
    648  1.19        is 
    649  1.19        is 	/* take care that no CIA-interrupts are lost */
    650  1.19        is 	if (ints)
    651  1.19        is 		dispatch_cia_ints (0, ints);
    652  1.19        is 
    653  1.19        is 	return c;
    654  1.19        is }
    655  1.19        is 
    656  1.19        is void
    657  1.37   aymeric kbdstuffchar(u_char c)
    658  1.19        is {
    659  1.19        is 	struct firm_event *fe;
    660  1.19        is 	struct kbd_softc *k = &kbd_softc;
    661  1.19        is 	int put;
    662  1.19        is 
    663  1.35        is #if NWSKBD>0
    664  1.37   aymeric 	/*
    665  1.35        is 	 * If we have attached a wskbd and not in polling mode and
    666  1.35        is 	 * nobody has opened us directly, then send the keystroke
    667  1.35        is 	 * to the wskbd.
    668  1.35        is 	 */
    669  1.35        is 
    670  1.37   aymeric 	if (kbd_softc.k_pollingmode == 0
    671  1.35        is 	    && kbd_softc.k_wskbddev != NULL
    672  1.35        is 	    && k->k_event_mode == 0) {
    673  1.35        is 		wskbd_input(kbd_softc.k_wskbddev,
    674  1.37   aymeric 			    KEY_UP(c) ?
    675  1.37   aymeric 			    WSCONS_EVENT_KEY_UP :
    676  1.35        is 			    WSCONS_EVENT_KEY_DOWN,
    677  1.35        is 			    KEY_CODE(c));
    678  1.35        is 		return;
    679  1.35        is 	}
    680  1.35        is 
    681  1.35        is #endif /* NWSKBD */
    682  1.35        is 
    683  1.37   aymeric 	/*
    684  1.37   aymeric 	 * If not in event mode, deliver straight to ite to process
    685  1.37   aymeric 	 * key stroke
    686  1.19        is 	 */
    687  1.19        is 
    688  1.18     veego 	if (! k->k_event_mode) {
    689  1.35        is #if NITE>0
    690  1.18     veego 		ite_filter (c, ITEFILT_TTY);
    691  1.35        is #endif
    692  1.18     veego 		return;
    693  1.18     veego 	}
    694  1.18     veego 
    695  1.37   aymeric 	/*
    696  1.19        is 	 * Keyboard is generating events. Turn this keystroke into an
    697  1.19        is 	 * event and put it in the queue. If the queue is full, the
    698  1.18     veego 	 * keystroke is lost (sorry!).
    699  1.18     veego 	 */
    700  1.37   aymeric 
    701  1.18     veego 	put = k->k_events.ev_put;
    702  1.18     veego 	fe = &k->k_events.ev_q[put];
    703  1.18     veego 	put = (put + 1) % EV_QSIZE;
    704  1.18     veego 	if (put == k->k_events.ev_get) {
    705  1.19        is 		log(LOG_WARNING, "keyboard event queue overflow\n");
    706  1.19        is 			/* ??? */
    707  1.18     veego 		return;
    708  1.18     veego 	}
    709  1.18     veego 	fe->id = KEY_CODE(c);
    710  1.18     veego 	fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
    711  1.18     veego 	fe->time = time;
    712  1.18     veego 	k->k_events.ev_put = put;
    713  1.18     veego 	EV_WAKEUP(&k->k_events);
    714   1.1        mw }
    715   1.1        mw 
    716   1.1        mw 
    717  1.19        is #ifdef DRACO
    718  1.19        is void
    719  1.37   aymeric drkbdintr(void)
    720   1.1        mw {
    721  1.19        is 	u_char in;
    722  1.19        is 	struct kbd_softc *k = &kbd_softc;
    723  1.18     veego 
    724  1.19        is 	in = draco_ioct->io_kbddata;
    725  1.19        is 	draco_ioct->io_kbdrst = 0;
    726   1.1        mw 
    727  1.37   aymeric 	if (in == 0xF0)
    728  1.19        is 		k->k_rlprfx = 0x80;
    729  1.19        is 	else {
    730  1.19        is 		kbdstuffchar(in>=sizeof(drkbdtab) ? 0xff :
    731  1.19        is 		    drkbdtab[in] | k->k_rlprfx);
    732  1.19        is 		k->k_rlprfx = 0;
    733  1.19        is 	}
    734  1.19        is }
    735   1.1        mw 
    736  1.19        is #endif
    737  1.35        is 
    738  1.35        is 
    739  1.35        is #if NWSKBD>0
    740  1.35        is /*
    741  1.35        is  * These are the callback functions that are passed to wscons.
    742  1.35        is  * They really don't do anything worth noting, just call the
    743  1.35        is  * other functions above.
    744  1.35        is  */
    745  1.35        is 
    746  1.35        is int
    747  1.37   aymeric kbd_enable(void *c, int on)
    748  1.35        is {
    749  1.35        is 	/* Wonder what this is supposed to do... */
    750  1.35        is 	return (0);
    751  1.35        is }
    752  1.35        is 
    753  1.35        is void
    754  1.37   aymeric kbd_set_leds(void *c, int leds)
    755  1.35        is {
    756  1.35        is }
    757  1.35        is 
    758  1.37   aymeric int
    759  1.37   aymeric kbd_ioctl(void *c, u_long cmd, caddr_t data, int flag, struct proc *p)
    760  1.35        is {
    761  1.35        is 	switch (cmd)
    762  1.35        is 	{
    763  1.35        is 	case WSKBDIO_COMPLEXBELL:
    764  1.35        is 		return 0;
    765  1.35        is 	case WSKBDIO_SETLEDS:
    766  1.35        is 		return 0;
    767  1.35        is 	case WSKBDIO_GETLEDS:
    768  1.35        is 		*(int*)data = 0;
    769  1.35        is 		return 0;
    770  1.35        is 	case WSKBDIO_GTYPE:
    771  1.37   aymeric 		*(u_int*)data = WSKBD_TYPE_AMIGA;
    772  1.35        is 		return 0;
    773  1.35        is 	}
    774  1.35        is 
    775  1.39    atatat 	/*
    776  1.39    atatat 	 * We are supposed to return EPASSTHROUGH to wscons if we didn't
    777  1.39    atatat 	 * understand.
    778  1.39    atatat 	 */
    779  1.39    atatat 	return (EPASSTHROUGH);
    780  1.35        is }
    781  1.35        is 
    782  1.35        is void
    783  1.37   aymeric kbd_getc(void *c, u_int *type, int *data)
    784  1.35        is {
    785  1.35        is 	int key;
    786  1.35        is 
    787  1.35        is 	key = kbdgetcn();
    788  1.35        is 
    789  1.35        is 	*data = KEY_CODE(key);
    790  1.35        is 	*type = KEY_UP(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    791  1.35        is }
    792  1.35        is 
    793  1.35        is void
    794  1.37   aymeric kbd_pollc(void *c, int on)
    795  1.35        is {
    796  1.35        is 	kbd_softc.k_pollingmode = on;
    797  1.35        is }
    798  1.35        is 
    799  1.35        is void
    800  1.37   aymeric kbd_bell(void *c, u_int x, u_int y, u_int z)
    801  1.35        is {
    802  1.35        is }
    803  1.35        is #endif /* WSKBD */
    804