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