Home | History | Annotate | Line # | Download | only in dev
kb_hb.c revision 1.13
      1  1.13   tsutsui /*	$NetBSD: kb_hb.c,v 1.13 2012/10/13 06:25:20 tsutsui Exp $	*/
      2   1.1    tsubai 
      3   1.1    tsubai /*-
      4   1.1    tsubai  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5   1.1    tsubai  *
      6   1.1    tsubai  * Redistribution and use in source and binary forms, with or without
      7   1.1    tsubai  * modification, are permitted provided that the following conditions
      8   1.1    tsubai  * are met:
      9   1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     10   1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     11   1.1    tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1    tsubai  *    notice, this list of conditions and the following disclaimer in the
     13   1.1    tsubai  *    documentation and/or other materials provided with the distribution.
     14   1.1    tsubai  * 3. The name of the author may not be used to endorse or promote products
     15   1.1    tsubai  *    derived from this software without specific prior written permission.
     16   1.1    tsubai  *
     17   1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1    tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1    tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1    tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1    tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1    tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1    tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1    tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1    tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26   1.1    tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1    tsubai  */
     28   1.7     lukem 
     29   1.7     lukem #include <sys/cdefs.h>
     30  1.13   tsutsui __KERNEL_RCSID(0, "$NetBSD: kb_hb.c,v 1.13 2012/10/13 06:25:20 tsutsui Exp $");
     31   1.1    tsubai 
     32   1.1    tsubai #include <sys/param.h>
     33   1.1    tsubai #include <sys/device.h>
     34   1.1    tsubai #include <sys/systm.h>
     35   1.1    tsubai 
     36   1.1    tsubai #include <dev/wscons/wsconsio.h>
     37   1.1    tsubai #include <dev/wscons/wskbdvar.h>
     38   1.1    tsubai #include <dev/wscons/wsksymdef.h>
     39   1.1    tsubai #include <dev/wscons/wsksymvar.h>
     40   1.1    tsubai 
     41   1.1    tsubai #include <machine/adrsmap.h>
     42   1.1    tsubai 
     43   1.5   tsutsui #include <newsmips/dev/hbvar.h>
     44   1.5   tsutsui 
     45   1.1    tsubai struct kbreg {
     46  1.12   tsutsui 	volatile uint8_t kb_data;
     47  1.12   tsutsui 	volatile uint8_t kb_stat;
     48  1.12   tsutsui 	volatile uint8_t kb_reset;
     49  1.12   tsutsui 	volatile uint8_t kb_init;
     50   1.1    tsubai };
     51   1.1    tsubai 
     52   1.1    tsubai struct kb_hb_softc {
     53  1.12   tsutsui 	device_t sc_dev;
     54  1.12   tsutsui 	struct kbreg *sc_reg;
     55  1.13   tsutsui 	device_t sc_wskbddev;
     56   1.1    tsubai };
     57   1.1    tsubai 
     58  1.12   tsutsui int kb_hb_match(device_t, cfdata_t, void *);
     59  1.12   tsutsui void kb_hb_attach(device_t, device_t, void *);
     60   1.1    tsubai int kb_hb_intr(void *);
     61   1.1    tsubai 
     62   1.1    tsubai void kb_hb_cnattach(void);
     63   1.1    tsubai void kb_hb_cngetc(void *, u_int *, int *);
     64   1.1    tsubai void kb_hb_cnpollc(void *, int);
     65   1.1    tsubai 
     66   1.1    tsubai int kb_hb_enable(void *, int);
     67   1.1    tsubai void kb_hb_setleds(void *, int);
     68  1.11  christos int kb_hb_ioctl(void *, u_long, void *, int, struct lwp *);
     69   1.1    tsubai 
     70   1.1    tsubai extern struct wscons_keydesc newskb_keydesctab[];
     71   1.1    tsubai 
     72  1.12   tsutsui CFATTACH_DECL_NEW(kb_hb, sizeof(struct kb_hb_softc),
     73   1.4   thorpej     kb_hb_match, kb_hb_attach, NULL, NULL);
     74   1.1    tsubai 
     75   1.1    tsubai struct wskbd_accessops kb_hb_accessops = {
     76   1.1    tsubai 	kb_hb_enable,
     77   1.1    tsubai 	kb_hb_setleds,
     78   1.1    tsubai 	kb_hb_ioctl
     79   1.1    tsubai };
     80   1.1    tsubai 
     81   1.1    tsubai struct wskbd_consops kb_hb_consops = {
     82   1.1    tsubai 	kb_hb_cngetc,
     83   1.1    tsubai 	kb_hb_cnpollc
     84   1.1    tsubai };
     85   1.1    tsubai 
     86   1.1    tsubai struct wskbd_mapdata kb_hb_keymapdata = {
     87   1.1    tsubai 	newskb_keydesctab,
     88   1.1    tsubai 	KB_JP
     89   1.1    tsubai };
     90   1.1    tsubai 
     91   1.1    tsubai int
     92  1.12   tsutsui kb_hb_match(device_t parent, cfdata_t cf, void *aux)
     93   1.1    tsubai {
     94   1.5   tsutsui 	struct hb_attach_args *ha = aux;
     95   1.1    tsubai 
     96   1.5   tsutsui 	if (strcmp(ha->ha_name, "kb") == 0)
     97   1.1    tsubai 		return 1;
     98   1.1    tsubai 
     99   1.1    tsubai 	return 0;
    100   1.1    tsubai }
    101   1.1    tsubai 
    102   1.1    tsubai void
    103  1.12   tsutsui kb_hb_attach(device_t parent, device_t self, void *aux)
    104   1.1    tsubai {
    105  1.12   tsutsui 	struct kb_hb_softc *sc = device_private(self);
    106   1.5   tsutsui 	struct hb_attach_args *ha = aux;
    107  1.12   tsutsui 	struct kbreg *reg;
    108  1.12   tsutsui 	volatile uint32_t *dipsw = (void *)DIP_SWITCH;
    109   1.1    tsubai 	struct wskbddev_attach_args aa;
    110   1.5   tsutsui 	int intr, cons;
    111   1.5   tsutsui 
    112  1.12   tsutsui 	sc->sc_dev = self;
    113  1.12   tsutsui 
    114   1.5   tsutsui 	reg = (struct kbreg *)ha->ha_addr;
    115   1.5   tsutsui 	intr = ha->ha_level;
    116   1.1    tsubai 
    117   1.1    tsubai 	if (intr == -1)
    118   1.1    tsubai 		intr = 2;
    119   1.1    tsubai 
    120   1.1    tsubai 	sc->sc_reg = reg;
    121   1.1    tsubai 	reg->kb_reset = 0x01;
    122   1.1    tsubai 	reg->kb_init = 0xf0;	/* 9600 bps */
    123   1.1    tsubai 
    124  1.12   tsutsui 	aprint_normal(" level %d", intr);
    125   1.5   tsutsui 	cons = 0;
    126   1.1    tsubai 	if (*dipsw & 7) {
    127   1.1    tsubai 		cons = 1;
    128  1.12   tsutsui 		aprint_normal(" (console)");
    129   1.1    tsubai 	}
    130  1.12   tsutsui 	aprint_normal("\n");
    131   1.1    tsubai 
    132   1.6   tsutsui 	hb_intr_establish(intr, INTEN0_KBDINT, IPL_TTY, kb_hb_intr, sc);
    133   1.1    tsubai 
    134   1.1    tsubai 	aa.console = cons;
    135   1.1    tsubai 	aa.keymap = &kb_hb_keymapdata;
    136   1.1    tsubai 	aa.accessops = &kb_hb_accessops;
    137   1.1    tsubai 	aa.accesscookie = sc;
    138   1.1    tsubai 	sc->sc_wskbddev = config_found(self, &aa, wskbddevprint);
    139   1.1    tsubai }
    140   1.1    tsubai 
    141   1.1    tsubai int
    142   1.8   tsutsui kb_hb_intr(void *v)
    143   1.1    tsubai {
    144   1.1    tsubai 	struct kb_hb_softc *sc = v;
    145  1.12   tsutsui 	struct kbreg *reg = sc->sc_reg;
    146  1.12   tsutsui 	volatile uint8_t *ien = (void *)INTEN0;
    147   1.1    tsubai 	int code, type, release, val;
    148   1.1    tsubai 	int rv = 0;
    149   1.1    tsubai 
    150   1.1    tsubai 	*ien &= ~RX_KBINTE;
    151   1.1    tsubai 
    152   1.1    tsubai 	while (reg->kb_stat & RX_KBRDY) {
    153   1.1    tsubai 		code = reg->kb_data;
    154   1.1    tsubai 		release = code & 0x80;
    155   1.1    tsubai 		val = code & 0x7f;
    156   1.1    tsubai 		type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    157   1.1    tsubai 		if (sc->sc_wskbddev)
    158   1.1    tsubai 			wskbd_input(sc->sc_wskbddev, type, val);
    159   1.1    tsubai 		rv = 1;
    160   1.1    tsubai 	}
    161   1.1    tsubai 
    162   1.1    tsubai 	*ien |= RX_KBINTE;
    163   1.1    tsubai 	return rv;
    164   1.1    tsubai }
    165   1.1    tsubai 
    166   1.1    tsubai void
    167   1.8   tsutsui kb_hb_cnattach(void)
    168   1.1    tsubai {
    169  1.12   tsutsui 	volatile uint32_t *dipsw = (void *)DIP_SWITCH;
    170   1.1    tsubai 
    171   1.1    tsubai 	if (*dipsw & 7)
    172   1.9   tsutsui 		wskbd_cnattach(&kb_hb_consops, (void *)KEYB_DATA,
    173   1.9   tsutsui 		    &kb_hb_keymapdata);
    174   1.1    tsubai }
    175   1.1    tsubai 
    176   1.1    tsubai void
    177   1.8   tsutsui kb_hb_cngetc(void *v, u_int *type, int *data)
    178   1.1    tsubai {
    179  1.12   tsutsui 	struct kbreg *reg = v;
    180  1.12   tsutsui 	volatile uint8_t *ien = (void *)INTEN0;
    181   1.1    tsubai 	int code, release, ointr;
    182   1.1    tsubai 
    183   1.1    tsubai 	ointr = *ien & RX_KBINTE;
    184   1.1    tsubai 	*ien &= ~RX_KBINTE;
    185   1.1    tsubai 
    186   1.1    tsubai 	/* Wait for key data. */
    187   1.1    tsubai 	while ((reg->kb_stat & RX_KBRDY) == 0);
    188   1.1    tsubai 
    189   1.1    tsubai 	code = reg->kb_data;
    190   1.1    tsubai 	release = code & 0x80;
    191   1.1    tsubai 	*data = code & 0x7f;
    192   1.1    tsubai 	*type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    193   1.1    tsubai 
    194   1.1    tsubai 	*ien |= ointr;
    195   1.1    tsubai }
    196   1.1    tsubai 
    197   1.1    tsubai void
    198   1.8   tsutsui kb_hb_cnpollc(void *v, int on)
    199   1.1    tsubai {
    200   1.1    tsubai }
    201   1.1    tsubai 
    202   1.1    tsubai int
    203   1.8   tsutsui kb_hb_enable(void *v, int on)
    204   1.1    tsubai {
    205   1.8   tsutsui 
    206   1.1    tsubai 	return 0;
    207   1.1    tsubai }
    208   1.1    tsubai 
    209   1.1    tsubai void
    210   1.8   tsutsui kb_hb_setleds(void *v, int on)
    211   1.1    tsubai {
    212   1.1    tsubai }
    213   1.1    tsubai 
    214   1.1    tsubai int
    215  1.11  christos kb_hb_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    216   1.1    tsubai {
    217   1.8   tsutsui 
    218   1.1    tsubai 	switch (cmd) {
    219   1.1    tsubai 
    220   1.1    tsubai 	case WSKBDIO_GTYPE:
    221   1.1    tsubai 		*(int *)data = 0;		/* XXX */
    222   1.1    tsubai 		return 0;
    223   1.1    tsubai 	case WSKBDIO_SETLEDS:
    224   1.1    tsubai 		return 0;
    225   1.1    tsubai 	case WSKBDIO_GETLEDS:
    226   1.1    tsubai 		*(int *)data = 0;
    227   1.1    tsubai 		return 0;
    228   1.1    tsubai 	}
    229   1.1    tsubai 
    230   1.2    atatat 	return EPASSTHROUGH;
    231   1.1    tsubai }
    232