Home | History | Annotate | Line # | Download | only in dev
epockbd.c revision 1.2.2.3
      1  1.2.2.2  tls /*	$NetBSD: epockbd.c,v 1.2.2.3 2014/08/20 00:02:52 tls Exp $	*/
      2  1.2.2.2  tls /*
      3  1.2.2.2  tls  * Copyright (c) 2013 KIYOHARA Takashi
      4  1.2.2.2  tls  * All rights reserved.
      5  1.2.2.2  tls  *
      6  1.2.2.2  tls  * Redistribution and use in source and binary forms, with or without
      7  1.2.2.2  tls  * modification, are permitted provided that the following conditions
      8  1.2.2.2  tls  * are met:
      9  1.2.2.2  tls  * 1. Redistributions of source code must retain the above copyright
     10  1.2.2.2  tls  *    notice, this list of conditions and the following disclaimer.
     11  1.2.2.2  tls  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.2.2.2  tls  *    notice, this list of conditions and the following disclaimer in the
     13  1.2.2.2  tls  *    documentation and/or other materials provided with the distribution.
     14  1.2.2.2  tls  *
     15  1.2.2.2  tls  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.2.2.2  tls  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.2.2.2  tls  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.2.2.2  tls  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  1.2.2.2  tls  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  1.2.2.2  tls  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.2.2.2  tls  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.2.2.2  tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  1.2.2.2  tls  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  1.2.2.2  tls  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.2.2.2  tls  * POSSIBILITY OF SUCH DAMAGE.
     26  1.2.2.2  tls  */
     27  1.2.2.2  tls #include <sys/cdefs.h>
     28  1.2.2.2  tls __KERNEL_RCSID(0, "$NetBSD: epockbd.c,v 1.2.2.3 2014/08/20 00:02:52 tls Exp $");
     29  1.2.2.2  tls 
     30  1.2.2.2  tls #include <sys/param.h>
     31  1.2.2.2  tls #include <sys/bus.h>
     32  1.2.2.2  tls #include <sys/callout.h>
     33  1.2.2.2  tls #include <sys/device.h>
     34  1.2.2.2  tls #include <sys/errno.h>
     35  1.2.2.2  tls #include <sys/kernel.h>
     36  1.2.2.2  tls 
     37  1.2.2.2  tls #include <dev/wscons/wsconsio.h>
     38  1.2.2.2  tls #include <dev/wscons/wskbdvar.h>
     39  1.2.2.2  tls #include <dev/wscons/wsksymdef.h>
     40  1.2.2.2  tls #include <dev/wscons/wsksymvar.h>
     41  1.2.2.2  tls 
     42  1.2.2.2  tls #include <epoc32/dev/epockbdvar.h>
     43  1.2.2.2  tls #include <epoc32/dev/epockbdmap.h>
     44  1.2.2.2  tls 
     45  1.2.2.2  tls #define EPOCKBD_COLUMN0		8
     46  1.2.2.2  tls 
     47  1.2.2.2  tls #define EPOCKBD_SCAN_READ(sc) \
     48  1.2.2.2  tls 	bus_space_read_1((sc)->sc_scant, (sc)->sc_scanh, 0)
     49  1.2.2.2  tls #define EPOCKBD_SCAN_WRITE(sc, cmd) \
     50  1.2.2.2  tls 	bus_space_write_1((sc)->sc_scant, (sc)->sc_scanh, 0, (cmd))
     51  1.2.2.2  tls #define EPOCKBD_DATA_READ(sc) \
     52  1.2.2.2  tls 	bus_space_read_1((sc)->sc_datat, (sc)->sc_datah, 0)
     53  1.2.2.2  tls 
     54  1.2.2.2  tls #define EPOC2WS_KBD_DATA(r, c)	(((c) << 3) + (31 - __builtin_clz(r)) + 1)
     55  1.2.2.2  tls 
     56  1.2.2.2  tls static int epockbd_enable(void *, int);
     57  1.2.2.2  tls static void epockbd_set_leds(void *, int);
     58  1.2.2.2  tls static int epockbd_ioctl(void *, u_long, void *, int, struct lwp *);
     59  1.2.2.2  tls 
     60  1.2.2.2  tls static void epockbd_poll(void *);
     61  1.2.2.2  tls 
     62  1.2.2.2  tls static void epockbd_cngetc(void *, u_int *, int *);
     63  1.2.2.2  tls static void epockbd_cnpollc(void *, int);
     64  1.2.2.2  tls 
     65  1.2.2.2  tls static struct wskbd_consops epockbd_consops = {
     66  1.2.2.2  tls 	epockbd_cngetc,
     67  1.2.2.2  tls 	epockbd_cnpollc,
     68  1.2.2.2  tls 	NULL
     69  1.2.2.2  tls };
     70  1.2.2.2  tls 
     71  1.2.2.2  tls static struct wskbd_accessops epockbd_accessops = {
     72  1.2.2.2  tls 	epockbd_enable,
     73  1.2.2.2  tls 	epockbd_set_leds,
     74  1.2.2.2  tls 	epockbd_ioctl,
     75  1.2.2.2  tls };
     76  1.2.2.2  tls 
     77  1.2.2.2  tls static struct wskbd_mapdata epockbd_mapdata = {
     78  1.2.2.2  tls 	epockbd_keydesctab,
     79  1.2.2.2  tls 	KB_UK,
     80  1.2.2.2  tls };
     81  1.2.2.2  tls 
     82  1.2.2.2  tls static int is_console;
     83  1.2.2.2  tls 
     84  1.2.2.2  tls void
     85  1.2.2.2  tls epockbd_attach(struct epockbd_softc *sc)
     86  1.2.2.2  tls {
     87  1.2.2.2  tls 	struct wskbddev_attach_args aa;
     88  1.2.2.2  tls 
     89  1.2.2.2  tls 	aprint_normal("\n");
     90  1.2.2.2  tls 	aprint_naive("\n");
     91  1.2.2.2  tls 
     92  1.2.2.2  tls 	if (is_console)
     93  1.2.2.2  tls 		wskbd_cnattach(&epockbd_consops, sc, &epockbd_mapdata);
     94  1.2.2.2  tls 
     95  1.2.2.2  tls 	callout_init(&sc->sc_c, 0);
     96  1.2.2.2  tls 	callout_reset(&sc->sc_c, hz, epockbd_poll, sc);
     97  1.2.2.2  tls 	sc->sc_flags |= EPOCKBD_FLAG_ENABLE;
     98  1.2.2.2  tls 
     99  1.2.2.2  tls 	aa.console = is_console;
    100  1.2.2.2  tls 	aa.keymap = &epockbd_mapdata;
    101  1.2.2.2  tls 	aa.accessops = &epockbd_accessops;
    102  1.2.2.2  tls 	aa.accesscookie = sc;
    103  1.2.2.2  tls 	sc->sc_wskbddev = config_found(sc->sc_dev, &aa, wskbddevprint);
    104  1.2.2.2  tls }
    105  1.2.2.2  tls 
    106  1.2.2.2  tls /*
    107  1.2.2.2  tls  * wskbd(9) functions
    108  1.2.2.2  tls  */
    109  1.2.2.2  tls 
    110  1.2.2.2  tls static int
    111  1.2.2.2  tls epockbd_enable(void *v, int on)
    112  1.2.2.2  tls {
    113  1.2.2.2  tls 	struct epockbd_softc *sc = v;
    114  1.2.2.2  tls 
    115  1.2.2.2  tls 	if (on) {
    116  1.2.2.2  tls 		sc->sc_flags |= EPOCKBD_FLAG_ENABLE;
    117  1.2.2.2  tls 		callout_schedule(&sc->sc_c, hz / 20);
    118  1.2.2.2  tls 	} else {
    119  1.2.2.2  tls 		sc->sc_flags &= ~EPOCKBD_FLAG_ENABLE;
    120  1.2.2.2  tls 		callout_stop(&sc->sc_c);
    121  1.2.2.2  tls 	}
    122  1.2.2.2  tls 	return 0;
    123  1.2.2.2  tls }
    124  1.2.2.2  tls 
    125  1.2.2.2  tls static void
    126  1.2.2.2  tls epockbd_set_leds(void *v, int on)
    127  1.2.2.2  tls {
    128  1.2.2.2  tls 	/* Nothing */
    129  1.2.2.2  tls }
    130  1.2.2.2  tls 
    131  1.2.2.2  tls static int
    132  1.2.2.2  tls epockbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    133  1.2.2.2  tls {
    134  1.2.2.2  tls 
    135  1.2.2.2  tls 	switch (cmd) {
    136  1.2.2.2  tls 	case WSKBDIO_GTYPE:
    137  1.2.2.2  tls 		*(int *)data = WSKBD_TYPE_EPOC;
    138  1.2.2.2  tls 		return 0;
    139  1.2.2.2  tls 
    140  1.2.2.2  tls 	case WSKBDIO_SETLEDS:
    141  1.2.2.2  tls 		return 0;
    142  1.2.2.2  tls 
    143  1.2.2.2  tls 	case WSKBDIO_GETLEDS:
    144  1.2.2.2  tls 		*(int *)data = 0;
    145  1.2.2.2  tls 		return 0;
    146  1.2.2.2  tls 
    147  1.2.2.2  tls #ifdef WSDISPLAY_COMPAT_RAWKBD
    148  1.2.2.2  tls 	case WSKBDIO_SETMODE:
    149  1.2.2.2  tls 		*(int *)data == WSKBD_RAW;
    150  1.2.2.2  tls 		sc->sc_flags |= EPOCKBD_FLAG_RAW;
    151  1.2.2.2  tls 
    152  1.2.2.2  tls 		/* Not yet...: return 0; */
    153  1.2.2.2  tls #endif
    154  1.2.2.2  tls 	}
    155  1.2.2.2  tls 	return EPASSTHROUGH;
    156  1.2.2.2  tls }
    157  1.2.2.2  tls 
    158  1.2.2.2  tls static void
    159  1.2.2.2  tls epockbd_poll(void *v)
    160  1.2.2.2  tls {
    161  1.2.2.2  tls 	struct epockbd_softc *sc = v;
    162  1.2.2.2  tls 	u_int type;
    163  1.2.2.2  tls 	int data;
    164  1.2.2.2  tls 
    165  1.2.2.2  tls 	epockbd_cngetc(v, &type, &data);
    166  1.2.2.2  tls 	if (data != 0)
    167  1.2.2.2  tls 		wskbd_input(sc->sc_wskbddev, type, data);
    168  1.2.2.2  tls 
    169  1.2.2.2  tls 	callout_schedule(&sc->sc_c, hz / 20);
    170  1.2.2.2  tls }
    171  1.2.2.2  tls 
    172  1.2.2.2  tls void
    173  1.2.2.2  tls epockbd_cnattach(void)
    174  1.2.2.2  tls {
    175  1.2.2.2  tls 
    176  1.2.2.2  tls 	is_console = 1;
    177  1.2.2.2  tls }
    178  1.2.2.2  tls 
    179  1.2.2.2  tls static void
    180  1.2.2.2  tls epockbd_cngetc(void *conscookie, u_int *type, int *data)
    181  1.2.2.2  tls {
    182  1.2.2.2  tls 	struct epockbd_softc *sc = conscookie;
    183  1.2.2.2  tls 	uint8_t cmd, key, mask;
    184  1.2.2.2  tls 	int column, row;
    185  1.2.2.3  tls #if 1
    186  1.2.2.3  tls 	/*
    187  1.2.2.3  tls 	 * For some machines which return a strange response, it calculates
    188  1.2.2.3  tls 	 * using this variable.
    189  1.2.2.3  tls 	 */
    190  1.2.2.3  tls 	int pc = 0, pr = 0;
    191  1.2.2.3  tls #endif
    192  1.2.2.2  tls 
    193  1.2.2.2  tls 	*type = 0;
    194  1.2.2.2  tls 	*data = 0;
    195  1.2.2.2  tls 	mask = (1 << sc->sc_kbd_nrow) - 1;
    196  1.2.2.2  tls 	for (column = 0; column < sc->sc_kbd_ncolumn; column++) {
    197  1.2.2.2  tls 		delay(16);
    198  1.2.2.2  tls 		cmd = EPOCKBD_SCAN_READ(sc);
    199  1.2.2.2  tls 		cmd &= ~0xf;
    200  1.2.2.2  tls 		cmd |= (EPOCKBD_COLUMN0 + column);
    201  1.2.2.2  tls 		EPOCKBD_SCAN_WRITE(sc, cmd);
    202  1.2.2.2  tls 		delay(16);
    203  1.2.2.2  tls 		key = EPOCKBD_DATA_READ(sc) & mask;
    204  1.2.2.2  tls 		if (sc->sc_state[column] != key) {
    205  1.2.2.2  tls 			row = sc->sc_state[column] ^ key;
    206  1.2.2.2  tls 			sc->sc_state[column] = key;
    207  1.2.2.3  tls #if 1
    208  1.2.2.3  tls 			if (*data == 0 ||
    209  1.2.2.3  tls 			    (row == pr && pc == 0 &&
    210  1.2.2.3  tls 			     column == sc->sc_kbd_ncolumn - 1))
    211  1.2.2.3  tls #else
    212  1.2.2.3  tls 			if (*data == 0)
    213  1.2.2.3  tls #endif
    214  1.2.2.3  tls 			{
    215  1.2.2.2  tls 				if (key & row)
    216  1.2.2.2  tls 					*type = WSCONS_EVENT_KEY_DOWN;
    217  1.2.2.2  tls 				else
    218  1.2.2.2  tls 					*type = WSCONS_EVENT_KEY_UP;
    219  1.2.2.2  tls 				*data = EPOC2WS_KBD_DATA(row, column);
    220  1.2.2.3  tls #if 1
    221  1.2.2.3  tls 				pc = column;
    222  1.2.2.3  tls 				pr = row;
    223  1.2.2.3  tls #endif
    224  1.2.2.2  tls 			}
    225  1.2.2.2  tls 		}
    226  1.2.2.2  tls 	}
    227  1.2.2.2  tls }
    228  1.2.2.2  tls 
    229  1.2.2.2  tls /* ARGSUSED */
    230  1.2.2.2  tls static void
    231  1.2.2.2  tls epockbd_cnpollc(void *conckookie, int on)
    232  1.2.2.2  tls {
    233  1.2.2.2  tls 	/* Nothing */
    234  1.2.2.2  tls }
    235