1 /* $NetBSD: kb_hb.c,v 1.4 2002/10/02 04:27:52 thorpej 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/param.h> 30 #include <sys/device.h> 31 #include <sys/systm.h> 32 33 #include <dev/wscons/wsconsio.h> 34 #include <dev/wscons/wskbdvar.h> 35 #include <dev/wscons/wsksymdef.h> 36 #include <dev/wscons/wsksymvar.h> 37 38 #include <machine/autoconf.h> 39 #include <machine/adrsmap.h> 40 41 struct kbreg { 42 u_char kb_data; 43 u_char kb_stat; 44 u_char kb_reset; 45 u_char kb_init; 46 }; 47 48 struct kb_hb_softc { 49 struct device sc_dev; 50 volatile struct kbreg *sc_reg; 51 struct device *sc_wskbddev; 52 }; 53 54 int kb_hb_match(struct device *, struct cfdata *, void *); 55 void kb_hb_attach(struct device *, struct device *, void *); 56 int kb_hb_intr(void *); 57 58 void kb_hb_cnattach(void); 59 void kb_hb_cngetc(void *, u_int *, int *); 60 void kb_hb_cnpollc(void *, int); 61 62 int kb_hb_enable(void *, int); 63 void kb_hb_setleds(void *, int); 64 int kb_hb_ioctl(void *, u_long, caddr_t, int, struct proc *); 65 66 extern struct wscons_keydesc newskb_keydesctab[]; 67 68 CFATTACH_DECL(kb_hb, sizeof(struct kb_hb_softc), 69 kb_hb_match, kb_hb_attach, NULL, NULL); 70 71 struct wskbd_accessops kb_hb_accessops = { 72 kb_hb_enable, 73 kb_hb_setleds, 74 kb_hb_ioctl 75 }; 76 77 struct wskbd_consops kb_hb_consops = { 78 kb_hb_cngetc, 79 kb_hb_cnpollc 80 }; 81 82 struct wskbd_mapdata kb_hb_keymapdata = { 83 newskb_keydesctab, 84 KB_JP 85 }; 86 87 int 88 kb_hb_match(parent, cf, aux) 89 struct device *parent; 90 struct cfdata *cf; 91 void *aux; 92 { 93 struct confargs *ca = aux; 94 95 if (strcmp(ca->ca_name, "kb") == 0) 96 return 1; 97 98 return 0; 99 } 100 101 void 102 kb_hb_attach(parent, self, aux) 103 struct device *parent, *self; 104 void *aux; 105 { 106 struct kb_hb_softc *sc = (void *)self; 107 volatile struct kbreg *reg = (void *)self->dv_cfdata->cf_addr; 108 int intr = self->dv_cfdata->cf_level; 109 volatile int *dipsw = (void *)DIP_SWITCH; 110 struct wskbddev_attach_args aa; 111 int cons = 0; 112 113 if (intr == -1) 114 intr = 2; 115 116 sc->sc_reg = reg; 117 reg->kb_reset = 0x01; 118 reg->kb_init = 0xf0; /* 9600 bps */ 119 120 printf(" level %d", intr); 121 if (*dipsw & 7) { 122 cons = 1; 123 printf(" (console)"); 124 } 125 printf("\n"); 126 127 hb_intr_establish(intr, IPL_TTY, kb_hb_intr, sc); 128 129 aa.console = cons; 130 aa.keymap = &kb_hb_keymapdata; 131 aa.accessops = &kb_hb_accessops; 132 aa.accesscookie = sc; 133 sc->sc_wskbddev = config_found(self, &aa, wskbddevprint); 134 } 135 136 int 137 kb_hb_intr(v) 138 void *v; 139 { 140 struct kb_hb_softc *sc = v; 141 volatile struct kbreg *reg = sc->sc_reg; 142 volatile u_char *ien = (void *)INTEN0; 143 int code, type, release, val; 144 int rv = 0; 145 146 *ien &= ~RX_KBINTE; 147 148 while (reg->kb_stat & RX_KBRDY) { 149 code = reg->kb_data; 150 release = code & 0x80; 151 val = code & 0x7f; 152 type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 153 if (sc->sc_wskbddev) 154 wskbd_input(sc->sc_wskbddev, type, val); 155 rv = 1; 156 } 157 158 *ien |= RX_KBINTE; 159 return rv; 160 } 161 162 void 163 kb_hb_cnattach() 164 { 165 volatile int *dipsw = (void *)DIP_SWITCH; 166 volatile struct kbreg *reg = (void *)KEYB_DATA; 167 168 if (*dipsw & 7) 169 wskbd_cnattach(&kb_hb_consops, (void *)reg, &kb_hb_keymapdata); 170 } 171 172 void 173 kb_hb_cngetc(v, type, data) 174 void *v; 175 u_int *type; 176 int *data; 177 { 178 volatile struct kbreg *reg = v; 179 volatile u_char *ien = (void *)INTEN0; 180 int code, release, ointr; 181 182 ointr = *ien & RX_KBINTE; 183 *ien &= ~RX_KBINTE; 184 185 /* Wait for key data. */ 186 while ((reg->kb_stat & RX_KBRDY) == 0); 187 188 code = reg->kb_data; 189 release = code & 0x80; 190 *data = code & 0x7f; 191 *type = release ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 192 193 *ien |= ointr; 194 } 195 196 void 197 kb_hb_cnpollc(v, on) 198 void *v; 199 int on; 200 { 201 } 202 203 int 204 kb_hb_enable(v, on) 205 void *v; 206 int on; 207 { 208 return 0; 209 } 210 211 void 212 kb_hb_setleds(v, on) 213 void *v; 214 int on; 215 { 216 } 217 218 int 219 kb_hb_ioctl(v, cmd, data, flag, p) 220 void *v; 221 u_long cmd; 222 caddr_t data; 223 int flag; 224 struct proc *p; 225 { 226 switch (cmd) { 227 228 case WSKBDIO_GTYPE: 229 *(int *)data = 0; /* XXX */ 230 return 0; 231 case WSKBDIO_SETLEDS: 232 return 0; 233 case WSKBDIO_GETLEDS: 234 *(int *)data = 0; 235 return 0; 236 } 237 238 return EPASSTHROUGH; 239 } 240