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