Home | History | Annotate | Line # | Download | only in apbus
kb_ap.c revision 1.5
      1 /*	$NetBSD: kb_ap.c,v 1.5 2003/07/15 02:59:28 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Tsubai Masanari.  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  * 3. The name of the author may not be used to endorse or promote products
     15  *    derived from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: kb_ap.c,v 1.5 2003/07/15 02:59:28 lukem Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 
     36 #include <dev/wscons/wsconsio.h>
     37 #include <dev/wscons/wskbdvar.h>
     38 #include <dev/wscons/wsksymdef.h>
     39 
     40 #include <machine/adrsmap.h>
     41 #include <newsmips/apbus/apbusvar.h>
     42 
     43 struct kbreg {
     44 	u_int kb_rx_data;
     45 	u_int kb_rx_stat;
     46 	u_int kb_rx_intr_en;
     47 	u_int kb_rx_reset;
     48 	u_int kb_rx_speed;
     49 
     50 	u_int ms_rx_data;
     51 	u_int ms_rx_stat;
     52 	u_int ms_rx_intr_en;
     53 	u_int ms_rx_reset;
     54 	u_int ms_rx_speed;
     55 
     56 	u_int kb_buzzf;
     57 	u_int kb_buzz;
     58 
     59 	u_int kb_tx_data;
     60 	u_int kb_tx_stat;
     61 	u_int kb_tx_intr_en;
     62 	u_int kb_tx_reset;
     63 	u_int kb_tx_speed;
     64 };
     65 
     66 struct kb_ap_softc {
     67 	struct device sc_dev;
     68 	volatile struct kbreg *sc_reg;
     69 	struct device *sc_wskbddev;
     70 };
     71 
     72 int kb_ap_match(struct device *, struct cfdata *, void *);
     73 void kb_ap_attach(struct device *, struct device *, void *);
     74 int kb_ap_intr(void *);
     75 
     76 void kb_ap_cnattach(void);
     77 void kb_ap_cngetc(void *, u_int *, int *);
     78 void kb_ap_cnpollc(void *, int);
     79 
     80 int kb_ap_enable(void *, int);
     81 void kb_ap_setleds(void *, int);
     82 int kb_ap_ioctl(void *, u_long, caddr_t, int, struct proc *);
     83 
     84 extern struct wscons_keydesc newskb_keydesctab[];
     85 
     86 CFATTACH_DECL(kb_ap, sizeof(struct kb_ap_softc),
     87     kb_ap_match, kb_ap_attach, NULL, NULL);
     88 
     89 struct wskbd_accessops kb_ap_accessops = {
     90 	kb_ap_enable,
     91 	kb_ap_setleds,
     92 	kb_ap_ioctl,
     93 };
     94 
     95 struct wskbd_consops kb_ap_consops = {
     96 	kb_ap_cngetc,
     97 	kb_ap_cnpollc,
     98 };
     99 
    100 struct wskbd_mapdata kb_ap_keymapdata = {
    101 	newskb_keydesctab,
    102 	KB_JP,
    103 };
    104 
    105 int
    106 kb_ap_match(parent, cf, aux)
    107 	struct device *parent;
    108 	struct cfdata *cf;
    109 	void *aux;
    110 {
    111 	struct apbus_attach_args *apa = aux;
    112 
    113 	if (strcmp(apa->apa_name, "kb") == 0)
    114 		return 1;
    115 
    116 	return 0;
    117 }
    118 
    119 void
    120 kb_ap_attach(parent, self, aux)
    121 	struct device *parent, *self;
    122 	void *aux;
    123 {
    124 	struct kb_ap_softc *sc = (void *)self;
    125 	struct apbus_attach_args *apa = aux;
    126 	volatile struct kbreg *reg = (void *)apa->apa_hwbase;
    127 	volatile int *dipsw = (void *)NEWS5000_DIP_SWITCH;
    128 	struct wskbddev_attach_args waa;
    129 	int cons = 0;
    130 
    131 	printf(" slot%d addr 0x%lx", apa->apa_slotno, apa->apa_hwbase);
    132 
    133 	sc->sc_reg = reg;
    134 
    135 	if (*dipsw & 7) {
    136 		printf(" (console)");
    137 		cons = 1;
    138 	}
    139 	printf("\n");
    140 
    141 	reg->kb_rx_reset = 0x03;
    142 	reg->kb_tx_reset = 0x03;
    143 
    144 	reg->kb_rx_speed = 0x04;
    145 	reg->kb_tx_speed = 0x04;
    146 
    147 	reg->kb_rx_intr_en = 1;
    148 	reg->kb_tx_intr_en = 0;
    149 
    150 	apbus_intr_establish(1, NEWS5000_INT1_KBD, 0, kb_ap_intr, sc,
    151 	    "", apa->apa_ctlnum);
    152 
    153 	waa.console = cons;
    154 	waa.keymap = &kb_ap_keymapdata;
    155 	waa.accessops = &kb_ap_accessops;
    156 	waa.accesscookie = sc;
    157 
    158 	sc->sc_wskbddev = config_found(self, &waa, wskbddevprint);
    159 }
    160 
    161 int
    162 kb_ap_intr(v)
    163 	void *v;
    164 {
    165 	struct kb_ap_softc *sc = v;
    166 	volatile struct kbreg *reg = sc->sc_reg;
    167 	int key, val, type, release;
    168 	int rv = 0;
    169 
    170 	while (reg->kb_rx_stat & RX_KBRDY) {
    171 		key = reg->kb_rx_data;
    172 		val = key & 0x7f;
    173 		release = key & 0x80;
    174 		type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    175 
    176 		if (sc->sc_wskbddev)
    177 			wskbd_input(sc->sc_wskbddev, type, val);
    178 
    179 		rv = 1;
    180 	}
    181 
    182 	return rv;
    183 }
    184 
    185 void
    186 kb_ap_cnattach()
    187 {
    188 	wskbd_cnattach(&kb_ap_consops, (void *)0xbf900000, &kb_ap_keymapdata);
    189 }
    190 
    191 void
    192 kb_ap_cngetc(v, type, data)
    193 	void *v;
    194 	u_int *type;
    195 	int *data;
    196 {
    197 	volatile struct kbreg *reg = v;
    198 	int key, release, ointr;
    199 
    200 	/* Disable keyboard interrupt. */
    201 	ointr = reg->kb_rx_intr_en;
    202 	reg->kb_rx_intr_en = 0;
    203 
    204 	/* Wait for key data. */
    205 	while ((reg->kb_rx_stat & RX_KBRDY) == 0);
    206 
    207 	key = reg->kb_rx_data;
    208 	release = key & 0x80;
    209 	*data = key & 0x7f;
    210 	*type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
    211 
    212 	reg->kb_rx_intr_en = ointr;
    213 }
    214 
    215 void
    216 kb_ap_cnpollc(v, on)
    217 	void *v;
    218 	int on;
    219 {
    220 }
    221 
    222 int
    223 kb_ap_enable(v, on)
    224 	void *v;
    225 	int on;
    226 {
    227 	return 0;
    228 }
    229 
    230 void
    231 kb_ap_setleds(v, on)
    232 	void *v;
    233 	int on;
    234 {
    235 }
    236 
    237 int
    238 kb_ap_ioctl(v, cmd, data, flag, p)
    239 	void *v;
    240 	u_long cmd;
    241 	caddr_t data;
    242 	int flag;
    243 	struct proc *p;
    244 {
    245 	switch (cmd) {
    246 	case WSKBDIO_GTYPE:
    247 		*(int *)data = 0;	/* XXX */
    248 		return 0;
    249 	case WSKBDIO_SETLEDS:
    250 		return 0;
    251 	case WSKBDIO_GETLEDS:
    252 		*(int *)data = 0;
    253 		return 0;
    254 	}
    255 
    256 	return EPASSTHROUGH;
    257 }
    258