Home | History | Annotate | Line # | Download | only in g42xxeb
g42xxeb_kmkbd.c revision 1.12.2.2
      1  1.12.2.2      yamt /* $NetBSD: g42xxeb_kmkbd.c,v 1.12.2.2 2012/10/30 17:19:22 yamt Exp $ */
      2       1.1       bsh 
      3       1.1       bsh /*-
      4       1.1       bsh  * Copyright (c) 2002, 2003, 2005 Genetec corp.
      5       1.1       bsh  * All rights reserved.
      6       1.1       bsh  *
      7       1.1       bsh  * 4x5 matrix key switch driver for TWINTAIL.
      8       1.1       bsh  * Written by Hiroyuki Bessho for Genetec corp.
      9       1.1       bsh  *
     10       1.1       bsh  * Redistribution and use in source and binary forms, with or without
     11       1.1       bsh  * modification, are permitted provided that the following conditions
     12       1.1       bsh  * are met:
     13       1.1       bsh  * 1. Redistributions of source code must retain the above copyright
     14       1.1       bsh  *    notice, this list of conditions and the following disclaimer.
     15       1.1       bsh  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1       bsh  *    notice, this list of conditions and the following disclaimer in the
     17       1.1       bsh  *    documentation and/or other materials provided with the distribution.
     18       1.9    martin  * 3. Neither the name of The NetBSD Foundation nor the names of its
     19       1.9    martin  *    contributors may be used to endorse or promote products derived
     20       1.9    martin  *    from this software without specific prior written permission.
     21       1.1       bsh  *
     22       1.1       bsh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23       1.1       bsh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24       1.1       bsh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25       1.1       bsh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26       1.1       bsh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27       1.1       bsh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28       1.1       bsh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29       1.1       bsh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30       1.1       bsh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31       1.1       bsh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32       1.1       bsh  * POSSIBILITY OF SUCH DAMAGE.
     33       1.1       bsh  */
     34       1.1       bsh 
     35       1.1       bsh /*
     36       1.1       bsh  * Use on-board matrix switches as wskbd.
     37       1.1       bsh  */
     38       1.1       bsh 
     39       1.1       bsh #include <sys/cdefs.h>
     40  1.12.2.2      yamt __KERNEL_RCSID(0, "$NetBSD: g42xxeb_kmkbd.c,v 1.12.2.2 2012/10/30 17:19:22 yamt Exp $" );
     41       1.1       bsh 
     42       1.1       bsh #include <sys/param.h>
     43       1.1       bsh #include <sys/systm.h>
     44       1.1       bsh #include <sys/device.h>
     45       1.1       bsh #include <sys/malloc.h>
     46       1.1       bsh #include <sys/ioctl.h>
     47       1.1       bsh #include <sys/callout.h>
     48       1.1       bsh #include <sys/kernel.h>			/* for hz */
     49       1.1       bsh 
     50      1.12    dyoung #include <sys/bus.h>
     51       1.1       bsh 
     52       1.1       bsh #include <dev/wscons/wsconsio.h>
     53       1.1       bsh #include <dev/wscons/wskbdvar.h>
     54       1.1       bsh #include <dev/wscons/wsksymdef.h>
     55       1.1       bsh #include <dev/wscons/wsksymvar.h>
     56       1.1       bsh 
     57       1.1       bsh #include <arch/evbarm/g42xxeb/g42xxeb_var.h>
     58       1.1       bsh 
     59       1.1       bsh #include "locators.h"
     60  1.12.2.1      yamt #include "opt_wsdisplay_compat.h"
     61       1.1       bsh 
     62       1.1       bsh #define DEBOUNCE_TICKS	((hz<=50)?1:hz/50)	/* 20ms */
     63       1.1       bsh #define RELEASE_WATCH_TICKS  (hz/10)	/* 100ms */
     64       1.1       bsh 
     65  1.12.2.1      yamt #define	NUMKEYS	(4*5)	/* the number of keys */
     66  1.12.2.1      yamt 
     67       1.1       bsh struct kmkbd_softc {
     68      1.11       bsh 	device_t dev;
     69       1.1       bsh 
     70  1.12.2.2      yamt 	device_t wskbddev;
     71       1.1       bsh 	void *ih;			/* interrupt handler */
     72       1.1       bsh 	struct callout callout;
     73       1.1       bsh 
     74       1.1       bsh 	uint32_t  notified_bits;	/* reported state of keys */
     75       1.1       bsh 	uint32_t  last_bits;		/* used for debounce */
     76       1.1       bsh 	u_char  debounce_counter;
     77       1.1       bsh #define DEBOUNCE_COUNT  3
     78       1.1       bsh 	u_char  polling;
     79  1.12.2.1      yamt 	u_char	rawkbd;
     80       1.1       bsh 	enum kmkbd_state {
     81       1.1       bsh 		ST_INIT,
     82       1.1       bsh 		ST_DISABLED,
     83       1.1       bsh 		ST_ALL_UP,		/* waiting for interrupt */
     84       1.1       bsh 		ST_DEBOUNCE,		/* doing debounce */
     85       1.1       bsh 		ST_KEY_PRESSED		/* some keys are pressed */
     86       1.1       bsh 	} state;
     87       1.1       bsh };
     88       1.1       bsh 
     89       1.1       bsh 
     90      1.11       bsh int kmkbd_match(device_t, cfdata_t, void *);
     91      1.11       bsh void kmkbd_attach(device_t, device_t, void *);
     92       1.1       bsh 
     93      1.11       bsh CFATTACH_DECL_NEW(kmkbd, sizeof(struct kmkbd_softc),
     94       1.1       bsh     kmkbd_match, kmkbd_attach, NULL, NULL);
     95       1.1       bsh 
     96       1.1       bsh static  int	kmkbd_enable(void *, int);
     97       1.1       bsh static  void	kmkbd_set_leds(void *, int);
     98       1.5  christos static  int	kmkbd_ioctl(void *, u_long, void *, int, struct lwp *);
     99       1.1       bsh 
    100       1.1       bsh const struct wskbd_accessops kmkbd_accessops = {
    101       1.1       bsh 	kmkbd_enable,
    102       1.1       bsh 	kmkbd_set_leds,
    103       1.1       bsh 	kmkbd_ioctl,
    104       1.1       bsh };
    105       1.1       bsh 
    106       1.1       bsh #if 0
    107       1.1       bsh void	kmkbd_cngetc(void *, u_int *, int *);
    108       1.1       bsh void	kmkbd_cnpollc(void *, int);
    109       1.1       bsh void	kmkbd_cnbell(void *, u_int, u_int, u_int);
    110       1.1       bsh 
    111       1.1       bsh const struct wskbd_consops kmkbd_consops = {
    112       1.1       bsh 	kmkbd_cngetc,
    113       1.1       bsh 	kmkbd_cnpollc,
    114       1.1       bsh 	kmkbd_cnbell,
    115       1.1       bsh };
    116       1.1       bsh #endif
    117       1.1       bsh 
    118       1.1       bsh static const keysym_t kmkbd_keydesc_0[] = {
    119       1.1       bsh /*	pos              normal		shifted */
    120       1.1       bsh 	KS_KEYCODE(0),   KS_a,  	KS_A,
    121       1.1       bsh 	KS_KEYCODE(1),   KS_b,  	KS_B,
    122       1.1       bsh 	KS_KEYCODE(2),   KS_c,  	KS_C,
    123       1.1       bsh 	KS_KEYCODE(3),   KS_d,  	KS_D,
    124       1.1       bsh 	KS_KEYCODE(4),   KS_e,  	KS_E,
    125       1.1       bsh 	KS_KEYCODE(5),   KS_f,  	KS_F,
    126       1.1       bsh 	KS_KEYCODE(6),   KS_g,  	KS_G,
    127       1.1       bsh 	KS_KEYCODE(7),   KS_h,  	KS_H,
    128       1.1       bsh 	KS_KEYCODE(8),   KS_i,  	KS_I,
    129       1.1       bsh 	KS_KEYCODE(9),   KS_j,  	KS_J,
    130       1.1       bsh 	KS_KEYCODE(10),   KS_k,  	KS_K,
    131       1.1       bsh 	KS_KEYCODE(11),   KS_l,  	KS_L,
    132       1.1       bsh 	KS_KEYCODE(12),   KS_m,  	KS_M,
    133       1.1       bsh 	KS_KEYCODE(13),   KS_n,  	KS_N,
    134       1.1       bsh 	KS_KEYCODE(14),   KS_o,  	KS_O,
    135       1.1       bsh 	KS_KEYCODE(15),   KS_p,  	KS_P,
    136       1.1       bsh 	KS_KEYCODE(16),   KS_q,  	KS_Q,
    137       1.1       bsh 	KS_KEYCODE(17),   KS_r,  	KS_R,
    138       1.1       bsh 	KS_KEYCODE(18),   '\003',  	'\003',
    139       1.1       bsh 	KS_KEYCODE(19),   KS_Return,  	KS_Linefeed,
    140       1.1       bsh };
    141       1.1       bsh 
    142       1.1       bsh #define KBD_MAP(name, base, map) \
    143       1.1       bsh 			{ name, base, sizeof(map)/sizeof(keysym_t), map }
    144       1.1       bsh 
    145       1.1       bsh static const struct wscons_keydesc kmkbd_keydesctab[] = {
    146       1.1       bsh 	KBD_MAP(KB_MACHDEP, 0, kmkbd_keydesc_0),
    147       1.1       bsh 	{0, 0, 0, 0}
    148       1.1       bsh };
    149       1.1       bsh 
    150       1.1       bsh const struct wskbd_mapdata kmkbd_keymapdata = {
    151       1.1       bsh 	kmkbd_keydesctab,
    152       1.1       bsh #ifdef KMKBD_LAYOUT
    153       1.1       bsh 	KMKBD_LAYOUT,
    154       1.1       bsh #else
    155       1.1       bsh 	KB_MACHDEP,
    156       1.1       bsh #endif
    157       1.1       bsh };
    158       1.1       bsh 
    159       1.1       bsh /*
    160       1.1       bsh  * Hackish support for a bell on the PC Keyboard; when a suitable feeper
    161       1.1       bsh  * is found, it attaches itself into the pckbd driver here.
    162       1.1       bsh  */
    163       1.1       bsh void	(*kmkbd_bell_fn)(void *, u_int, u_int, u_int, int);
    164       1.1       bsh void	*kmkbd_bell_fn_arg;
    165       1.1       bsh 
    166       1.1       bsh void	kmkbd_bell(u_int, u_int, u_int, int);
    167       1.1       bsh void	kmkbd_hookup_bell(void (* fn)(void *, u_int, u_int, u_int, int), void *arg);
    168       1.1       bsh 
    169       1.1       bsh static int 	kmkbd_intr(void *);
    170       1.1       bsh static void 	kmkbd_new_state(struct kmkbd_softc *, enum kmkbd_state);
    171       1.1       bsh 
    172       1.1       bsh /*struct kmkbd_internal kmkbd_consdata;*/
    173       1.1       bsh 
    174       1.1       bsh static int
    175       1.1       bsh kmkbd_is_console(void)
    176       1.1       bsh {
    177       1.1       bsh #if 0
    178       1.1       bsh 	return (kmkbd_consdata.t_isconsole &&
    179       1.1       bsh 		(tag == kmkbd_consdata.t_kbctag) &&
    180       1.1       bsh 		(slot == kmkbd_consdata.t_kbcslot));
    181       1.1       bsh #else
    182       1.1       bsh 	return 0;
    183       1.1       bsh #endif
    184       1.1       bsh }
    185       1.1       bsh 
    186       1.1       bsh int
    187  1.12.2.2      yamt kmkbd_match(device_t parent, cfdata_t cf, void *aux)
    188       1.1       bsh {
    189       1.1       bsh 	return 1;
    190       1.1       bsh }
    191       1.1       bsh 
    192       1.1       bsh void
    193      1.11       bsh kmkbd_attach(device_t parent, device_t self, void *aux)
    194       1.1       bsh {
    195      1.11       bsh 	struct kmkbd_softc *sc = device_private(self);
    196       1.1       bsh 	/*struct obio_attach_args *oa = aux;*/
    197       1.1       bsh 	int state0;
    198       1.1       bsh 	struct wskbddev_attach_args a;
    199      1.11       bsh 	struct obio_softc *osc = device_private(parent);
    200       1.1       bsh 	int s;
    201       1.1       bsh 
    202       1.1       bsh 	printf("\n");
    203       1.1       bsh 
    204      1.11       bsh 	sc->dev = self;
    205       1.1       bsh 	sc->state = ST_INIT;
    206       1.1       bsh 
    207       1.1       bsh 	if (kmkbd_is_console()){
    208       1.1       bsh 		a.console = 1;
    209       1.1       bsh 		state0 = ST_ALL_UP;
    210       1.1       bsh 	} else {
    211       1.1       bsh 		a.console = 0;
    212       1.1       bsh 		state0 = ST_DISABLED;
    213       1.1       bsh 	}
    214       1.1       bsh 
    215  1.12.2.1      yamt #ifdef WSDISPLAY_COMPAT_RAWKBD
    216  1.12.2.1      yamt 	sc->rawkbd = 0;
    217  1.12.2.1      yamt #endif
    218       1.6        ad 	callout_init(&sc->callout, 0);
    219       1.1       bsh 
    220       1.1       bsh 	s = spltty();
    221       1.1       bsh 	sc->ih = obio_intr_establish(osc, G42XXEB_INT_KEY, IPL_TTY,
    222       1.1       bsh 	    IST_EDGE_FALLING, kmkbd_intr, (void *)sc);
    223       1.1       bsh 	kmkbd_new_state(sc, state0);
    224       1.1       bsh 	splx(s);
    225       1.1       bsh 
    226       1.1       bsh 	a.keymap = &kmkbd_keymapdata;
    227       1.1       bsh 
    228       1.1       bsh 	a.accessops = &kmkbd_accessops;
    229       1.1       bsh 	a.accesscookie = sc;
    230       1.1       bsh 
    231       1.1       bsh 
    232       1.1       bsh 	/* Attach the wskbd. */
    233       1.1       bsh 	sc->wskbddev = config_found(self, &a, wskbddevprint);
    234       1.1       bsh 
    235       1.1       bsh }
    236       1.1       bsh 
    237       1.1       bsh static int
    238       1.1       bsh kmkbd_enable(void *v, int on)
    239       1.1       bsh {
    240       1.1       bsh 	struct kmkbd_softc *sc = v;
    241       1.1       bsh 
    242       1.1       bsh 	if (on) {
    243       1.1       bsh 		if (sc->state != ST_DISABLED) {
    244       1.1       bsh #ifdef DIAGNOSTIC
    245       1.1       bsh 			printf("kmkbd_enable: bad enable (state=%d)\n", sc->state);
    246       1.1       bsh #endif
    247       1.1       bsh 			return (EBUSY);
    248       1.1       bsh 		}
    249       1.1       bsh 
    250       1.1       bsh 		kmkbd_new_state(sc, ST_ALL_UP);
    251       1.1       bsh 	} else {
    252       1.1       bsh #if 0
    253       1.1       bsh 		if (sc->id->t_isconsole)
    254       1.1       bsh 			return (EBUSY);
    255       1.1       bsh #endif
    256       1.1       bsh 
    257       1.1       bsh 		kmkbd_new_state(sc, ST_DISABLED);
    258       1.1       bsh 	}
    259       1.1       bsh 
    260       1.1       bsh 	return (0);
    261       1.1       bsh }
    262       1.1       bsh 
    263       1.1       bsh 
    264       1.1       bsh 
    265       1.1       bsh static void
    266       1.1       bsh kmkbd_set_leds(void *v, int leds)
    267       1.1       bsh {
    268       1.1       bsh }
    269       1.1       bsh 
    270       1.1       bsh static int
    271       1.5  christos kmkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    272       1.1       bsh {
    273  1.12.2.1      yamt #ifdef WSDISPLAY_COMPAT_RAWKBD
    274  1.12.2.1      yamt 	struct kmkbd_softc *sc = v;
    275  1.12.2.1      yamt #endif
    276       1.1       bsh 
    277       1.1       bsh 	switch (cmd) {
    278       1.1       bsh 	    case WSKBDIO_GTYPE:
    279       1.1       bsh 		*(int *)data = WSKBD_TYPE_PC_XT; /* XXX */
    280       1.1       bsh 		return 0;
    281       1.1       bsh 	    case WSKBDIO_COMPLEXBELL:
    282       1.1       bsh #define d ((struct wskbd_bell_data *)data)
    283       1.1       bsh 		/*
    284       1.1       bsh 		 * Keyboard can't beep directly; we have an
    285       1.1       bsh 		 * externally-provided global hook to do this.
    286       1.1       bsh 		 */
    287       1.1       bsh 		kmkbd_bell(d->pitch, d->period, d->volume, 0);
    288       1.1       bsh #undef d
    289       1.1       bsh 		return (0);
    290       1.1       bsh #ifdef WSDISPLAY_COMPAT_RAWKBD
    291       1.1       bsh 	    case WSKBDIO_SETMODE:
    292       1.1       bsh 		sc->rawkbd = (*(int *)data == WSKBD_RAW);
    293       1.1       bsh 		return (0);
    294       1.1       bsh #endif
    295       1.1       bsh 
    296       1.1       bsh #if 0
    297       1.1       bsh 	    case WSKBDIO_SETLEDS:
    298       1.1       bsh 	    case WSKBDIO_GETLEDS:
    299       1.1       bsh 		    /* no LED support */
    300       1.1       bsh #endif
    301       1.1       bsh 	}
    302       1.1       bsh 	return EPASSTHROUGH;
    303       1.1       bsh }
    304       1.1       bsh 
    305       1.1       bsh void
    306       1.1       bsh kmkbd_bell(u_int pitch, u_int period, u_int volume, int poll)
    307       1.1       bsh {
    308       1.1       bsh 
    309       1.1       bsh 	if (kmkbd_bell_fn != NULL)
    310       1.1       bsh 		(*kmkbd_bell_fn)(kmkbd_bell_fn_arg, pitch, period,
    311       1.1       bsh 		    volume, poll);
    312       1.1       bsh }
    313       1.1       bsh 
    314       1.1       bsh void
    315       1.1       bsh kmkbd_hookup_bell(void (* fn)(void *, u_int, u_int, u_int, int), void *arg)
    316       1.1       bsh {
    317       1.1       bsh 
    318       1.1       bsh 	if (kmkbd_bell_fn == NULL) {
    319       1.1       bsh 		kmkbd_bell_fn = fn;
    320       1.1       bsh 		kmkbd_bell_fn_arg = arg;
    321       1.1       bsh 	}
    322       1.1       bsh }
    323       1.1       bsh 
    324       1.1       bsh #if 0
    325       1.1       bsh int
    326      1.10       dsl kmkbd_cnattach(pckbc_tag_t kbctag, int kbcslot)
    327       1.1       bsh {
    328       1.1       bsh 	int res;
    329       1.1       bsh 
    330       1.1       bsh 	res = kmkbd_init(&kmkbd_consdata, kbctag, kbcslot, 1);
    331       1.1       bsh 
    332       1.1       bsh 	wskbd_cnattach(&kmkbd_consops, &kmkbd_consdata, &kmkbd_keymapdata);
    333       1.1       bsh 
    334       1.1       bsh 	return (0);
    335       1.1       bsh }
    336       1.1       bsh 
    337       1.1       bsh void
    338       1.1       bsh kmkbd_cngetc(void *v, u_int type, int *data)
    339       1.1       bsh {
    340       1.1       bsh         struct kmkbd_internal *t = v;
    341       1.1       bsh 	int val;
    342       1.1       bsh 
    343       1.1       bsh 	for (;;) {
    344       1.1       bsh 		val = pckbc_poll_data(t->t_kbctag, t->t_kbcslot);
    345       1.1       bsh 		if ((val != -1) && kmkbd_decode(t, val, type, data))
    346       1.1       bsh 			return;
    347       1.1       bsh 	}
    348       1.1       bsh }
    349       1.1       bsh 
    350       1.1       bsh void
    351       1.1       bsh kmkbd_cnpollc(void *v, int on)
    352       1.1       bsh {
    353       1.1       bsh 	struct kmkbd_internal *t = v;
    354       1.1       bsh 
    355       1.1       bsh 	pckbc_set_poll(t->t_kbctag, t->t_kbcslot, on);
    356       1.1       bsh }
    357       1.1       bsh 
    358       1.1       bsh void
    359       1.1       bsh kmkbd_cnbell(void *v, u_int pitch, u_int period, u_int volume)
    360       1.1       bsh {
    361       1.1       bsh 
    362       1.1       bsh 	kmkbd_bell(pitch, period, volume, 1);
    363       1.1       bsh }
    364       1.1       bsh #endif
    365       1.1       bsh 
    366       1.1       bsh 
    367       1.1       bsh /*
    368       1.1       bsh  * low level access to key matrix
    369       1.1       bsh  *
    370       1.1       bsh  * returns bitset of keys being pressed.
    371       1.1       bsh  */
    372       1.1       bsh static u_int
    373       1.1       bsh kmkbd_read_matrix(struct kmkbd_softc *sc)
    374       1.1       bsh {
    375       1.1       bsh 	int i;
    376       1.1       bsh 	u_int ret, data;
    377      1.11       bsh 	struct obio_softc *osc = device_private(device_parent(sc->dev));
    378       1.1       bsh 	bus_space_tag_t iot = osc->sc_iot;
    379       1.1       bsh 	bus_space_handle_t ioh = osc->sc_obioreg_ioh;
    380       1.1       bsh 
    381       1.1       bsh #define KMDELAY()	delay(3)
    382       1.1       bsh 
    383       1.1       bsh 	bus_space_write_2( iot, ioh, G42XXEB_KEYSCAN, 0 );
    384       1.1       bsh 	KMDELAY();
    385       1.1       bsh 
    386       1.1       bsh 	data = KEYSCAN_SENSE_IN &
    387       1.1       bsh 	    bus_space_read_2(iot, ioh, G42XXEB_KEYSCAN);
    388       1.1       bsh 
    389       1.1       bsh 	bus_space_write_2(iot, ioh, G42XXEB_KEYSCAN, KEYSCAN_SCAN_OUT);
    390       1.1       bsh 
    391       1.1       bsh 	if (data == KEYSCAN_SENSE_IN)
    392       1.1       bsh 		return 0;
    393       1.1       bsh 
    394       1.1       bsh 	ret = 0;
    395       1.1       bsh 	for( i=0; i<5; ++i ){
    396       1.1       bsh 		/* scan one line */
    397       1.1       bsh 		bus_space_write_2(iot, ioh, G42XXEB_KEYSCAN, ~(0x0100<<i));
    398       1.1       bsh 		KMDELAY();
    399       1.1       bsh 		data = bus_space_read_2(iot, ioh, G42XXEB_KEYSCAN );
    400       1.1       bsh 
    401       1.1       bsh 		data = ~data & KEYSCAN_SENSE_IN;
    402       1.1       bsh 		ret |= data << (i*4);
    403       1.1       bsh 	}
    404       1.1       bsh 
    405       1.1       bsh 	bus_space_write_2(iot, ioh, G42XXEB_KEYSCAN, KEYSCAN_SCAN_OUT);
    406       1.1       bsh 	return ret;
    407       1.1       bsh 
    408       1.1       bsh #undef KMDELAY
    409       1.1       bsh 
    410       1.1       bsh }
    411       1.1       bsh 
    412       1.1       bsh /*
    413       1.1       bsh  * report key status change to wskbd subsystem.
    414       1.1       bsh  */
    415       1.1       bsh static void
    416       1.1       bsh kmkbd_report(struct kmkbd_softc *sc, u_int bitset)
    417       1.1       bsh {
    418       1.1       bsh 	u_int changed;
    419       1.1       bsh 	int i;
    420       1.1       bsh 
    421       1.1       bsh 	if (bitset == sc->notified_bits)
    422       1.1       bsh 		return;
    423       1.1       bsh 
    424       1.1       bsh 	if (sc->notified_bits && bitset == 0){
    425       1.1       bsh 		wskbd_input(sc->wskbddev, WSCONS_EVENT_ALL_KEYS_UP, 0);
    426       1.1       bsh 		sc->notified_bits = 0;
    427       1.1       bsh 		return;
    428       1.1       bsh 	}
    429       1.1       bsh 
    430       1.1       bsh 	changed = bitset ^ sc->notified_bits;
    431       1.1       bsh 	for( i=0; changed; ++i){
    432       1.1       bsh 		if ((changed & (1<<i)) == 0)
    433       1.1       bsh 			continue;
    434       1.1       bsh 		changed &= ~(1<<i);
    435       1.1       bsh 
    436       1.1       bsh 		wskbd_input(sc->wskbddev,
    437       1.1       bsh 		    (bitset & (1<<i)) ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP,
    438       1.1       bsh 		    i);
    439       1.1       bsh 	}
    440       1.1       bsh 
    441       1.1       bsh 	sc->notified_bits = bitset;
    442       1.1       bsh }
    443       1.1       bsh 
    444  1.12.2.1      yamt #ifdef WSDISPLAY_COMPAT_RAWKBD
    445  1.12.2.1      yamt static void
    446  1.12.2.1      yamt kmkbd_report_raw(struct kmkbd_softc *sc, u_int bitset)
    447  1.12.2.1      yamt {
    448  1.12.2.1      yamt 	int i, nc;
    449  1.12.2.1      yamt 	char cbuf[NUMKEYS];
    450  1.12.2.1      yamt 	u_int changed;
    451  1.12.2.1      yamt 
    452  1.12.2.1      yamt 	if (bitset == sc->notified_bits)
    453  1.12.2.1      yamt 		return;
    454  1.12.2.1      yamt 
    455  1.12.2.1      yamt 	nc = 0;
    456  1.12.2.1      yamt 	changed = bitset ^ sc->notified_bits;
    457  1.12.2.1      yamt 
    458  1.12.2.1      yamt 	while (changed) {
    459  1.12.2.1      yamt 		i = ffs(changed) - 1;
    460  1.12.2.1      yamt 		if (nc < NUMKEYS) {
    461  1.12.2.1      yamt 			cbuf[nc] = i + 1;
    462  1.12.2.1      yamt 			if (0 == (bitset & (1<<i))) {
    463  1.12.2.1      yamt 				/* the key is released */
    464  1.12.2.1      yamt 				cbuf[nc] |= 0x80;
    465  1.12.2.1      yamt 			}
    466  1.12.2.1      yamt 			++nc;
    467  1.12.2.1      yamt 		}
    468  1.12.2.1      yamt 
    469  1.12.2.1      yamt 		changed &= ~(1<<i);
    470  1.12.2.1      yamt 	}
    471  1.12.2.1      yamt 
    472  1.12.2.1      yamt 	wskbd_rawinput(sc->wskbddev, cbuf, nc);
    473  1.12.2.1      yamt 	sc->notified_bits = bitset;
    474  1.12.2.1      yamt }
    475  1.12.2.1      yamt #endif
    476  1.12.2.1      yamt 
    477       1.1       bsh static int
    478       1.1       bsh kmkbd_intr(void *arg)
    479       1.1       bsh {
    480       1.1       bsh 	struct kmkbd_softc *sc = arg;
    481      1.11       bsh 	struct obio_softc *osc = device_private(device_parent(sc->dev));
    482       1.1       bsh 
    483       1.1       bsh 	if ( sc->state != ST_ALL_UP ){
    484       1.1       bsh 		printf("Spurious interrupt from key matrix\n");
    485       1.1       bsh 		obio_intr_mask(osc, sc->ih);
    486       1.1       bsh 		return 1;
    487       1.1       bsh 	}
    488       1.1       bsh 
    489       1.1       bsh 	kmkbd_new_state(sc, ST_DEBOUNCE);
    490       1.1       bsh 
    491       1.1       bsh 	return 1;
    492       1.1       bsh }
    493       1.1       bsh 
    494       1.1       bsh static void
    495       1.1       bsh kmkbd_debounce(void *arg)
    496       1.1       bsh {
    497       1.1       bsh 	struct kmkbd_softc *sc = arg;
    498       1.1       bsh 	u_int newbits;
    499       1.1       bsh 	enum kmkbd_state new_state = ST_DEBOUNCE;
    500       1.1       bsh 	int s = spltty();
    501       1.1       bsh 
    502       1.1       bsh 	newbits = kmkbd_read_matrix(sc);
    503       1.1       bsh 
    504       1.1       bsh 	if (newbits != sc->last_bits){
    505       1.1       bsh 		sc->last_bits = newbits;
    506       1.1       bsh 		sc->debounce_counter = 0;
    507       1.1       bsh 	}
    508       1.1       bsh 	else if( ++(sc->debounce_counter) >= DEBOUNCE_COUNT ){
    509       1.1       bsh 		new_state = newbits == 0 ? ST_ALL_UP : ST_KEY_PRESSED;
    510  1.12.2.1      yamt #ifdef WSDISPLAY_COMPAT_RAWKBD
    511  1.12.2.1      yamt 		if (sc->rawkbd)
    512  1.12.2.1      yamt 			kmkbd_report_raw(sc, newbits);
    513  1.12.2.1      yamt 		else
    514  1.12.2.1      yamt #endif
    515  1.12.2.1      yamt 			kmkbd_report(sc, newbits);
    516       1.1       bsh 	}
    517       1.1       bsh 
    518       1.1       bsh 	kmkbd_new_state(sc, new_state);
    519       1.1       bsh 	splx(s);
    520       1.1       bsh }
    521       1.1       bsh 
    522       1.1       bsh /* callout routine to watch key release */
    523       1.1       bsh static void
    524       1.1       bsh kmkbd_watch(void *arg)
    525       1.1       bsh {
    526       1.1       bsh 	int s = spltty();
    527       1.1       bsh 	struct kmkbd_softc *sc = arg;
    528       1.1       bsh 	u_int newbits;
    529       1.1       bsh 	int new_state = ST_KEY_PRESSED;
    530       1.1       bsh 
    531       1.1       bsh 	newbits = kmkbd_read_matrix(sc);
    532       1.1       bsh 
    533       1.1       bsh 	if (newbits != sc->last_bits){
    534       1.1       bsh 		/* some keys are released or new keys are pressed.
    535       1.1       bsh 		   start debounce */
    536       1.1       bsh 		new_state = ST_DEBOUNCE;
    537       1.1       bsh 		sc->last_bits = newbits;
    538       1.1       bsh 	}
    539       1.1       bsh 
    540       1.1       bsh 	kmkbd_new_state(sc, new_state);
    541       1.1       bsh 	splx(s);
    542       1.1       bsh }
    543       1.1       bsh 
    544       1.1       bsh static void
    545       1.1       bsh kmkbd_new_state(struct kmkbd_softc *sc, enum kmkbd_state new_state)
    546       1.1       bsh {
    547      1.11       bsh 	struct obio_softc *osc = device_private(device_parent(sc->dev));
    548       1.1       bsh 
    549       1.1       bsh 	switch(new_state){
    550       1.1       bsh 	case ST_DISABLED:
    551       1.1       bsh 		if (sc->state != ST_DISABLED){
    552       1.1       bsh 			callout_stop(&sc->callout);
    553       1.1       bsh 			obio_intr_mask(osc,sc->ih);
    554       1.1       bsh 		}
    555       1.1       bsh 		break;
    556       1.1       bsh 	case ST_DEBOUNCE:
    557       1.1       bsh 		if (sc->state == ST_ALL_UP){
    558       1.1       bsh 			obio_intr_mask(osc, sc->ih);
    559       1.1       bsh 			sc->last_bits = kmkbd_read_matrix(sc);
    560       1.1       bsh 		}
    561       1.1       bsh 		if (sc->state != ST_DEBOUNCE)
    562       1.1       bsh 			sc->debounce_counter = 0;
    563       1.1       bsh 
    564       1.1       bsh 		/* start debounce timer */
    565       1.1       bsh 		callout_reset(&sc->callout, DEBOUNCE_TICKS, kmkbd_debounce, sc);
    566       1.1       bsh 		break;
    567       1.1       bsh 	case ST_KEY_PRESSED:
    568       1.1       bsh 		/* start timer to check key release */
    569       1.1       bsh 		callout_reset(&sc->callout, RELEASE_WATCH_TICKS, kmkbd_watch, sc);
    570       1.1       bsh 		break;
    571       1.1       bsh 	case ST_ALL_UP:
    572       1.1       bsh 		if (sc->state != ST_ALL_UP){
    573       1.1       bsh 			bus_space_tag_t iot = osc->sc_iot;
    574       1.1       bsh 			bus_space_handle_t ioh = osc->sc_obioreg_ioh;
    575       1.1       bsh 
    576       1.1       bsh 			obio_intr_unmask(osc, sc->ih);
    577       1.1       bsh 			bus_space_write_2(iot, ioh, G42XXEB_KEYSCAN, 0);
    578       1.1       bsh 		}
    579       1.1       bsh 		break;
    580       1.1       bsh 	case ST_INIT:
    581       1.1       bsh 		; /* Nothing to do */
    582       1.1       bsh 	}
    583       1.1       bsh 
    584       1.1       bsh 	sc->state = new_state;
    585       1.1       bsh }
    586