Home | History | Annotate | Line # | Download | only in dev
ewskbd.c revision 1.8
      1 /*	$NetBSD: ewskbd.c,v 1.8 2008/03/29 19:15:34 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005 Izumi Tsutsui
      5  * Copyright (c) 2004 Steve Rumble
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote products
     17  *    derived from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * EWS4800 serial keyboard driver attached to zs channel 0 at 4800 bps.
     33  * This layer is the parent of wskbd.
     34  *
     35  * This driver is taken from sgimips.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: ewskbd.c,v 1.8 2008/03/29 19:15:34 tsutsui Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/malloc.h>
     43 #include <sys/systm.h>
     44 #include <sys/conf.h>
     45 #include <sys/device.h>
     46 
     47 #include <dev/wscons/wsconsio.h>
     48 #include <dev/wscons/wskbdvar.h>
     49 #include <dev/wscons/wsksymdef.h>
     50 #include <dev/wscons/wsksymvar.h>
     51 
     52 #include <dev/ic/z8530reg.h>
     53 #include <machine/z8530var.h>
     54 
     55 #include <ews4800mips/dev/ews4800keymap.h>
     56 
     57 #define EWSKBD_BAUD		4800
     58 
     59 #define EWSKBD_TXQ_LEN		16		/* power of 2 */
     60 #define EWSKBD_TXQ_LEN_MASK	(EWSKBD_TXQ_LEN - 1)
     61 #define EWSKBD_NEXTTXQ(x)	(((x) + 1) & EWSKBD_TXQ_LEN_MASK)
     62 
     63 #define EWSKBD_RXQ_LEN		64		/* power of 2 */
     64 #define EWSKBD_RXQ_LEN_MASK	(EWSKBD_RXQ_LEN - 1)
     65 #define EWSKBD_NEXTRXQ(x)	(((x) + 1) & EWSKBD_RXQ_LEN_MASK)
     66 
     67 #define EWSKBD_KEY_UP		0x80
     68 #define EWSKBD_KEY_MASK		0x7f
     69 
     70 #ifdef EWSKBD_DEBUG
     71 int ewskbd_debug = 0;
     72 #define DPRINTF(_x) if (ewskbd_debug) printf _x
     73 #else
     74 #define DPRINTF(_x)
     75 #endif
     76 
     77 struct ewskbd_softc {
     78 	device_t sc_dev;
     79 	struct ewskbd_devconfig *sc_dc;
     80 };
     81 
     82 struct ewskbd_devconfig {
     83 	/* transmit tail-chasing fifo */
     84 	uint8_t txq[EWSKBD_TXQ_LEN];
     85 	u_int txq_head;
     86 	u_int txq_tail;
     87 
     88 	/* receive tail-chasing fifo */
     89 	uint8_t rxq[EWSKBD_RXQ_LEN];
     90 	u_int rxq_head;
     91 	u_int rxq_tail;
     92 
     93 	/* state */
     94 	u_int state;
     95 #define TX_READY	0x01
     96 
     97 	/* LED status */
     98 	uint8_t	leds;
     99 #define EWSKBD_SETLEDS	0x90
    100 #define EWSKBD_CAPSLOCK	0x02
    101 #define EWSKBD_KANA	0x04
    102 
    103 	/* wscons glue */
    104 	struct device *wskbddev;
    105 	int enabled;
    106 };
    107 
    108 static int  ewskbd_zsc_match(device_t, cfdata_t, void *);
    109 static void ewskbd_zsc_attach(device_t, device_t, void *);
    110 static int  ewskbd_zsc_init(struct zs_chanstate *);
    111 static void ewskbd_zsc_rxint(struct zs_chanstate *);
    112 static void ewskbd_zsc_stint(struct zs_chanstate *, int);
    113 static void ewskbd_zsc_txint(struct zs_chanstate *);
    114 static void ewskbd_zsc_softint(struct zs_chanstate *);
    115 static void ewskbd_zsc_send(struct zs_chanstate *, uint8_t *, u_int);
    116 
    117 static void ewskbd_wskbd_input(struct zs_chanstate *, u_char);
    118 static int  ewskbd_wskbd_enable(void *, int);
    119 static void ewskbd_wskbd_set_leds(void *, int);
    120 static int  ewskbd_wskbd_get_leds(void *);
    121 static int  ewskbd_wskbd_ioctl(void *, u_long, void *, int, struct lwp *);
    122 
    123 void ewskbd_zsc_cnattach(uint32_t, uint32_t, int);
    124 static void ewskbd_zsc_wskbd_getc(void *, u_int *, int *);
    125 static void ewskbd_wskbd_pollc(void *, int);
    126 static void ewskbd_wskbd_bell(void *, u_int, u_int, u_int);
    127 
    128 CFATTACH_DECL_NEW(ewskbd_zsc, sizeof(struct ewskbd_softc),
    129     ewskbd_zsc_match, ewskbd_zsc_attach, NULL, NULL);
    130 
    131 static struct zsops ewskbd_zsops = {
    132 	ewskbd_zsc_rxint,
    133 	ewskbd_zsc_stint,
    134 	ewskbd_zsc_txint,
    135 	ewskbd_zsc_softint
    136 };
    137 
    138 const struct wskbd_mapdata ews4800kbd_wskbd_keymapdata = {
    139 	ews4800kbd_keydesctab,
    140 	KB_JP
    141 };
    142 
    143 const struct wskbd_accessops ewskbd_wskbd_accessops = {
    144 	ewskbd_wskbd_enable,
    145 	ewskbd_wskbd_set_leds,
    146 	ewskbd_wskbd_ioctl
    147 };
    148 
    149 const struct wskbd_consops ewskbd_wskbd_consops = {
    150 	ewskbd_zsc_wskbd_getc,
    151 	ewskbd_wskbd_pollc,
    152 	ewskbd_wskbd_bell
    153 };
    154 
    155 static struct ewskbd_devconfig ewskbd_console_dc;
    156 static struct zs_chanstate conschan;
    157 static int ewskbd_is_console;
    158 
    159 static int
    160 ewskbd_zsc_match(device_t parent, cfdata_t cf, void *aux)
    161 {
    162 	struct zsc_attach_args *zsc_args = aux;
    163 	struct zsc_softc *zsc = device_private(parent);
    164 
    165 	/* keyboard is on channel B */
    166 	if ((zsc->zsc_flags & 0x0001 /* kbms port */) != 0 &&
    167 	    zsc_args->channel == 1)
    168 		/* prior to generic zstty(4) */
    169 		return 3;
    170 
    171 	return 0;
    172 }
    173 
    174 static void
    175 ewskbd_zsc_attach(struct device *parent, struct device *self, void *aux)
    176 {
    177 	struct ewskbd_softc *sc;
    178 	struct zsc_softc *zsc;
    179 	struct zs_chanstate *cs;
    180 	struct zsc_attach_args *zsc_args;
    181 	struct wskbddev_attach_args wskaa;
    182 	int channel;
    183 
    184 	sc = device_private(self);
    185 	zsc = device_private(parent);
    186 	sc->sc_dev = self;
    187 	zsc_args = aux;
    188 
    189 	/* Establish ourself with the MD z8530 driver */
    190 	channel = zsc_args->channel;
    191 	cs = zsc->zsc_cs[channel];
    192 
    193 	if (ewskbd_is_console) {
    194 		sc->sc_dc = &ewskbd_console_dc;
    195 		wskaa.console = 1;
    196 		sc->sc_dc->enabled = 1;
    197 	} else {
    198 		wskaa.console = 0;
    199 
    200 		sc->sc_dc = malloc(sizeof(struct ewskbd_devconfig), M_DEVBUF,
    201 		    M_WAITOK | M_ZERO);
    202 		if (sc->sc_dc == NULL) {
    203 			printf(": can't allocate memory\n");
    204 			return;
    205 		}
    206 		sc->sc_dc->enabled = 0;
    207 	}
    208 	cs->cs_defspeed = EWSKBD_BAUD;
    209 	cs->cs_ops = &ewskbd_zsops;
    210 	cs->cs_private = sc;
    211 
    212 	sc->sc_dc->txq_head = 0;
    213 	sc->sc_dc->txq_tail = 0;
    214 	sc->sc_dc->rxq_head = 0;
    215 	sc->sc_dc->rxq_tail = 0;
    216 	sc->sc_dc->state = TX_READY;
    217 	sc->sc_dc->leds = 0;
    218 
    219 	ewskbd_zsc_init(cs);
    220 
    221 	/* set default LED */
    222 	ewskbd_wskbd_set_leds(cs, 0);
    223 
    224 	printf(": baud rate %d\n", EWSKBD_BAUD);
    225 
    226 	/* attach wskbd */
    227 	wskaa.keymap = &ews4800kbd_wskbd_keymapdata;
    228 	wskaa.accessops = &ewskbd_wskbd_accessops;
    229 	wskaa.accesscookie = cs;
    230 	sc->sc_dc->wskbddev = config_found(self, &wskaa, wskbddevprint);
    231 }
    232 
    233 static int
    234 ewskbd_zsc_init(struct zs_chanstate *cs)
    235 {
    236 	int s;
    237 
    238 	s = splzs();
    239 
    240 	zs_write_reg(cs, 9, ZSWR9_B_RESET);
    241 	DELAY(100);
    242 	zs_write_reg(cs, 9, ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR);
    243 
    244 	cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE;
    245 	cs->cs_preg[2] = 0;
    246 	cs->cs_preg[3] = ZSWR3_RX_8 | ZSWR3_RX_ENABLE;
    247 	cs->cs_preg[4] = ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_PARENB;
    248 	cs->cs_preg[5] = ZSWR5_TX_8 | ZSWR5_RTS | ZSWR5_TX_ENABLE;
    249 	cs->cs_preg[6] = 0;
    250 	cs->cs_preg[7] = 0;
    251 	cs->cs_preg[8] = 0;
    252 	cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
    253 	cs->cs_preg[10] = 0;
    254 	cs->cs_preg[11] = ZSWR11_RXCLK_BAUD | ZSWR11_TXCLK_BAUD |
    255 	    ZSWR11_TRXC_OUT_ENA | ZSWR11_TRXC_BAUD;
    256 	/* reg[11] and reg[12] are set by zs_set_speed() with cs_brg_clk */
    257 	zs_set_speed(cs, EWSKBD_BAUD);
    258 	cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
    259 	cs->cs_preg[15] = 0;
    260 
    261 	zs_loadchannelregs(cs);
    262 
    263 	splx(s);
    264 
    265 	return 0;
    266 }
    267 
    268 static void
    269 ewskbd_zsc_rxint(struct zs_chanstate *cs)
    270 {
    271 	struct ewskbd_softc *sc;
    272 	struct ewskbd_devconfig *dc;
    273 	uint8_t c, r;
    274 
    275 	sc = cs->cs_private;
    276 	dc = sc->sc_dc;
    277 
    278 	/* clear errors */
    279 	r = zs_read_reg(cs, 1);
    280 	if (r & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE))
    281 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    282 
    283 	/* read byte and append to our queue */
    284 	c = zs_read_data(cs);
    285 
    286 	dc->rxq[dc->rxq_tail] = c;
    287 	dc->rxq_tail = EWSKBD_NEXTRXQ(dc->rxq_tail);
    288 
    289 	cs->cs_softreq = 1;
    290 }
    291 
    292 static void
    293 ewskbd_zsc_stint(struct zs_chanstate *cs, int force)
    294 {
    295 
    296 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    297 	cs->cs_softreq = 1;
    298 }
    299 
    300 static void
    301 ewskbd_zsc_txint(struct zs_chanstate *cs)
    302 {
    303 	struct ewskbd_softc *sc;
    304 
    305 	sc = cs->cs_private;
    306 	zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
    307 	sc->sc_dc->state |= TX_READY;
    308 	cs->cs_softreq = 1;
    309 }
    310 
    311 static void
    312 ewskbd_zsc_softint(struct zs_chanstate *cs)
    313 {
    314 	struct ewskbd_softc *sc;
    315 	struct ewskbd_devconfig *dc;
    316 
    317 	sc = cs->cs_private;
    318 	dc = sc->sc_dc;
    319 
    320 	/* handle pending transmissions */
    321 	if (dc->txq_head != dc->txq_tail && (dc->state & TX_READY)) {
    322 		int s;
    323 
    324 		dc->state &= ~TX_READY;
    325 
    326 		s = splzs();
    327 		zs_write_data(cs, dc->txq[dc->txq_head]);
    328 		splx(s);
    329 
    330 		dc->txq_head = EWSKBD_NEXTTXQ(dc->txq_head);
    331 	}
    332 
    333 	/* don't bother if nobody is listening */
    334 	if (!dc->enabled) {
    335 		dc->rxq_head = dc->rxq_tail;
    336 		return;
    337 	}
    338 
    339 	/* handle incoming keystrokes/config */
    340 	while (dc->rxq_head != dc->rxq_tail) {
    341 		uint8_t key = dc->rxq[dc->rxq_head];
    342 
    343 		/* toss wskbd a bone */
    344 		ewskbd_wskbd_input(cs, key);
    345 
    346 		dc->rxq_head = EWSKBD_NEXTRXQ(dc->rxq_head);
    347 	}
    348 }
    349 
    350 /* expects to be in splzs() */
    351 static void
    352 ewskbd_zsc_send(struct zs_chanstate *cs, uint8_t *c, u_int len)
    353 {
    354 	struct ewskbd_softc *sc;
    355 	struct ewskbd_devconfig *dc;
    356 	int i;
    357 
    358 	sc = cs->cs_private;
    359 	dc = sc->sc_dc;
    360 
    361 	for (i = 0; i < len; i++) {
    362 		if (dc->state & TX_READY) {
    363 			zs_write_data(cs, c[i]);
    364 			dc->state &= ~TX_READY;
    365 		} else {
    366 			dc->txq[dc->txq_tail] = c[i];
    367 			dc->txq_tail = EWSKBD_NEXTTXQ(dc->txq_tail);
    368 			cs->cs_softreq = 1;
    369 		}
    370 	}
    371 }
    372 
    373 /******************************************************************************
    374  * wskbd glue
    375  ******************************************************************************/
    376 
    377 static void
    378 ewskbd_wskbd_input(struct zs_chanstate *cs, uint8_t key)
    379 {
    380 	struct ewskbd_softc *sc;
    381 	u_int type;
    382 
    383 	sc = cs->cs_private;
    384 
    385 	if (key & EWSKBD_KEY_UP)
    386 		type = WSCONS_EVENT_KEY_UP;
    387 	else
    388 		type = WSCONS_EVENT_KEY_DOWN;
    389 
    390 	wskbd_input(sc->sc_dc->wskbddev, type, (key & EWSKBD_KEY_MASK));
    391 
    392 	DPRINTF(("ewskbd_wskbd_input: inputted key 0x%x\n", key));
    393 
    394 #ifdef WSDISPLAY_COMPAT_RAWKBD
    395 	wskbd_rawinput(sc->sc_dc->wskbddev, &key, 1);
    396 #endif
    397 }
    398 
    399 static int
    400 ewskbd_wskbd_enable(void *cookie, int on)
    401 {
    402 	struct zs_chanstate *cs;
    403 	struct ewskbd_softc *sc;
    404 
    405 	cs = cookie;
    406 	sc = cs->cs_private;
    407 
    408 	if (on) {
    409 		if (sc->sc_dc->enabled)
    410 			return EBUSY;
    411 		else
    412 			sc->sc_dc->enabled = 1;
    413 	} else
    414 		sc->sc_dc->enabled = 0;
    415 
    416 	DPRINTF(("ewskbd_wskbd_enable: %s\n", on ? "enabled" : "disabled"));
    417 
    418 	return 0;
    419 }
    420 
    421 static void
    422 ewskbd_wskbd_set_leds(void *cookie, int leds)
    423 {
    424 	struct zs_chanstate *cs;
    425 	struct ewskbd_softc *sc;
    426 	int s;
    427 	uint8_t	cmd;
    428 
    429 	cs = cookie;
    430 	sc = cs->cs_private;
    431 	cmd = 0;
    432 
    433 	if (leds & WSKBD_LED_CAPS)
    434 		cmd |= EWSKBD_CAPSLOCK;
    435 
    436 	sc->sc_dc->leds = cmd;
    437 
    438 	cmd |= EWSKBD_SETLEDS;
    439 
    440 	s = splzs();
    441 	ewskbd_zsc_send(cs, &cmd, 1);
    442 	splx(s);
    443 }
    444 
    445 static int
    446 ewskbd_wskbd_get_leds(void *cookie)
    447 {
    448 	struct zs_chanstate *cs;
    449 	struct ewskbd_softc *sc;
    450 	int leds;
    451 
    452 	cs = cookie;
    453 	sc = cs->cs_private;
    454 	leds = 0;
    455 
    456 	if (sc->sc_dc->leds & EWSKBD_CAPSLOCK)
    457 		leds |= WSKBD_LED_CAPS;
    458 
    459 	return leds;
    460 }
    461 
    462 static int
    463 ewskbd_wskbd_ioctl(void *cookie, u_long cmd, void *data, int flag,
    464     struct lwp *l)
    465 {
    466 
    467 	switch (cmd) {
    468 	case WSKBDIO_GTYPE:
    469 		*(int *)data = WSKBD_TYPE_EWS4800;
    470 		break;
    471 
    472 #ifdef notyet
    473 	case WSKBDIO_BELL:
    474 	case WSKBDIO_COMPLEXBELL:
    475 	case WSKBDIO_SETBELL:
    476 	case WSKBDIO_GETBELL:
    477 	case WSKBDIO_SETDEFAULTBELL:
    478 	case WSKBDIO_GETDEFAULTBELL:
    479 	case WSKBDIO_SETKEYREPEAT:
    480 	case WSKBDIO_GETKEYREPEAT:
    481 	case WSKBDIO_SETDEFAULTKEYREPEAT:
    482 	case WSKBDIO_GETDEFAULTKEYREPEAT:
    483 #endif
    484 
    485 	case WSKBDIO_SETLEDS:
    486 		ewskbd_wskbd_set_leds(cookie, *(int *)data);
    487 		break;
    488 
    489 	case WSKBDIO_GETLEDS:
    490 		*(int *)data = ewskbd_wskbd_get_leds(cookie);
    491 		break;
    492 
    493 #ifdef notyet
    494 	case WSKBDIO_GETMAP:
    495 	case WSKBDIO_SETMAP:
    496 	case WSKBDIO_GETENCODING:
    497 	case WSKBDIO_SETENCODING:
    498 	case WSKBDIO_SETMODE:
    499 	case WSKBDIO_GETMODE:
    500 	case WSKBDIO_SETKEYCLICK:
    501 	case WSKBDIO_GETKEYCLICK:
    502 #endif
    503 
    504 	default:
    505 		return EPASSTHROUGH;
    506 	}
    507 
    508 	return 0;
    509 }
    510 
    511 /*
    512  * console routines
    513  */
    514 void
    515 ewskbd_zsc_cnattach(uint32_t csr, uint32_t data, int pclk)
    516 {
    517 	struct zs_chanstate *cs;
    518 
    519 	cs = &conschan;
    520 
    521 	cs->cs_reg_csr  = (void *)csr;
    522 	cs->cs_reg_data = (void *)data;
    523 	cs->cs_brg_clk =  pclk / 16;
    524 	cs->cs_defspeed = EWSKBD_BAUD;
    525 
    526 	ewskbd_zsc_init(cs);
    527 
    528 	zs_putc(cs, EWSKBD_SETLEDS);
    529 
    530 	wskbd_cnattach(&ewskbd_wskbd_consops, cs, &ews4800kbd_wskbd_keymapdata);
    531 	ewskbd_is_console = 1;
    532 }
    533 
    534 static void
    535 ewskbd_zsc_wskbd_getc(void *cookie, u_int *type, int *data)
    536 {
    537 	int key;
    538 
    539 	key = zs_getc(cookie);
    540 
    541 	if (key & EWSKBD_KEY_UP)
    542 		*type = WSCONS_EVENT_KEY_UP;
    543 	else
    544 		*type = WSCONS_EVENT_KEY_DOWN;
    545 
    546 	*data = key & EWSKBD_KEY_MASK;
    547 }
    548 
    549 static void
    550 ewskbd_wskbd_pollc(void *cookie, int on)
    551 {
    552 
    553 	static bool __polling = false;
    554 	static int s;
    555 
    556 	if (on && !__polling) {
    557 		/* disable interrupt driven I/O */
    558 		s = splhigh();
    559 		__polling = true;
    560 	} else if (!on && __polling) {
    561 		/* enable interrupt driven I/O */
    562 		__polling = false;
    563 		splx(s);
    564 	}
    565 }
    566 
    567 static void
    568 ewskbd_wskbd_bell(void *cookie, u_int pitch, u_int period, u_int volume)
    569 {
    570 
    571 	/* nothing */
    572 }
    573