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