Home | History | Annotate | Line # | Download | only in dev
epockbd.c revision 1.1
      1 /*	$NetBSD: epockbd.c,v 1.1 2013/04/28 12:11:25 kiyohara 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.1 2013/04/28 12:11:25 kiyohara 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 }
    105 
    106 /*
    107  * wskbd(9) functions
    108  */
    109 
    110 static int
    111 epockbd_enable(void *v, int on)
    112 {
    113 	struct epockbd_softc *sc = v;
    114 
    115 	if (on) {
    116 		sc->sc_flags |= EPOCKBD_FLAG_ENABLE;
    117 		callout_schedule(&sc->sc_c, hz / 20);
    118 	} else {
    119 		sc->sc_flags &= ~EPOCKBD_FLAG_ENABLE;
    120 		callout_stop(&sc->sc_c);
    121 	}
    122 	return 0;
    123 }
    124 
    125 static void
    126 epockbd_set_leds(void *v, int on)
    127 {
    128 	/* Nothing */
    129 }
    130 
    131 static int
    132 epockbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    133 {
    134 
    135 	switch (cmd) {
    136 	case WSKBDIO_GTYPE:
    137 		*(int *)data = WSKBD_TYPE_EPOC;
    138 		return 0;
    139 
    140 	case WSKBDIO_SETLEDS:
    141 		return 0;
    142 
    143 	case WSKBDIO_GETLEDS:
    144 		*(int *)data = 0;
    145 		return 0;
    146 
    147 #ifdef WSDISPLAY_COMPAT_RAWKBD
    148 	case WSKBDIO_SETMODE:
    149 		*(int *)data == WSKBD_RAW;
    150 		sc->sc_flags |= EPOCKBD_FLAG_RAW;
    151 
    152 		/* Not yet...: return 0; */
    153 #endif
    154 	}
    155 	return EPASSTHROUGH;
    156 }
    157 
    158 static void
    159 epockbd_poll(void *v)
    160 {
    161 	struct epockbd_softc *sc = v;
    162 	u_int type;
    163 	int data;
    164 
    165 	epockbd_cngetc(v, &type, &data);
    166 	if (data != 0)
    167 		wskbd_input(sc->sc_wskbddev, type, data);
    168 
    169 	callout_schedule(&sc->sc_c, hz / 20);
    170 }
    171 
    172 void
    173 epockbd_cnattach(void)
    174 {
    175 
    176 	is_console = 1;
    177 }
    178 
    179 static void
    180 epockbd_cngetc(void *conscookie, u_int *type, int *data)
    181 {
    182 	struct epockbd_softc *sc = conscookie;
    183 	uint8_t cmd, key, mask;
    184 	int column, row;
    185 
    186 	*type = 0;
    187 	*data = 0;
    188 	mask = (1 << sc->sc_kbd_nrow) - 1;
    189 	for (column = 0; column < sc->sc_kbd_ncolumn; column++) {
    190 		delay(16);
    191 		cmd = EPOCKBD_SCAN_READ(sc);
    192 		cmd &= ~0xf;
    193 		cmd |= (EPOCKBD_COLUMN0 + column);
    194 		EPOCKBD_SCAN_WRITE(sc, cmd);
    195 		delay(16);
    196 		key = EPOCKBD_DATA_READ(sc) & mask;
    197 		if (sc->sc_state[column] != key) {
    198 			row = sc->sc_state[column] ^ key;
    199 			sc->sc_state[column] = key;
    200 			if (*data == 0) {
    201 				if (key & row)
    202 					*type = WSCONS_EVENT_KEY_DOWN;
    203 				else
    204 					*type = WSCONS_EVENT_KEY_UP;
    205 				*data = EPOC2WS_KBD_DATA(row, column);
    206 			}
    207 		}
    208 	}
    209 }
    210 
    211 /* ARGSUSED */
    212 static void
    213 epockbd_cnpollc(void *conckookie, int on)
    214 {
    215 	/* Nothing */
    216 }
    217