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