1 /* $NetBSD: nextkbd.c,v 1.10 2005/12/11 12:18:25 christos Exp $ */ 2 /* 3 * Copyright (c) 1998 Matt DeBergalis 4 * 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. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Matt DeBergalis 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: nextkbd.c,v 1.10 2005/12/11 12:18:25 christos Exp $"); 34 35 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/proc.h> 41 #include <sys/device.h> 42 #include <sys/malloc.h> 43 #include <sys/errno.h> 44 #include <sys/queue.h> 45 #include <sys/lock.h> 46 47 #include <machine/autoconf.h> 48 #include <machine/bus.h> 49 #include <machine/cpu.h> 50 #include <machine/intr.h> 51 52 #include <next68k/dev/nextkbdvar.h> 53 #include <next68k/dev/wskbdmap_next.h> 54 55 #include <dev/wscons/wsconsio.h> 56 #include <dev/wscons/wskbdvar.h> 57 #include <dev/wscons/wsksymdef.h> 58 #include <dev/wscons/wsksymvar.h> 59 60 #include <next68k/next68k/isr.h> 61 62 #include <next68k/dev/intiovar.h> 63 64 struct nextkbd_internal { 65 int num_ints; /* interrupt total */ 66 int polling; 67 int isconsole; 68 69 bus_space_tag_t iot; 70 bus_space_handle_t ioh; 71 struct nextkbd_softc *t_sc; /* back pointer */ 72 u_int32_t mods; 73 }; 74 75 struct mon_regs { 76 u_int32_t mon_csr; 77 u_int32_t mon_1; 78 u_int32_t mon_data; 79 }; 80 81 static int attached = 0; 82 83 int nextkbd_match(struct device *, struct cfdata *, void *); 84 void nextkbd_attach(struct device *, struct device *, void *); 85 86 int nextkbc_cnattach(bus_space_tag_t); 87 88 CFATTACH_DECL(nextkbd, sizeof(struct nextkbd_softc), 89 nextkbd_match, nextkbd_attach, NULL, NULL); 90 91 int nextkbd_enable(void *, int); 92 void nextkbd_set_leds(void *, int); 93 int nextkbd_ioctl(void *, u_long, caddr_t, int, struct lwp *); 94 95 const struct wskbd_accessops nextkbd_accessops = { 96 nextkbd_enable, 97 nextkbd_set_leds, 98 nextkbd_ioctl, 99 }; 100 101 void nextkbd_cngetc(void *, u_int *, int *); 102 void nextkbd_cnpollc(void *, int); 103 104 const struct wskbd_consops nextkbd_consops = { 105 nextkbd_cngetc, 106 nextkbd_cnpollc, 107 }; 108 109 const struct wskbd_mapdata nextkbd_keymapdata = { 110 nextkbd_keydesctab, 111 KB_US, 112 }; 113 114 static int nextkbd_read_data(struct nextkbd_internal *); 115 static int nextkbd_decode(struct nextkbd_internal *, int, u_int *, int *); 116 117 static struct nextkbd_internal nextkbd_consdata; 118 static int nextkbd_is_console(bus_space_tag_t); 119 120 int nextkbdhard(void *); 121 122 static int 123 nextkbd_is_console(bus_space_tag_t bst) 124 { 125 return (nextkbd_consdata.isconsole && (bst == nextkbd_consdata.iot)); 126 } 127 128 int 129 nextkbd_match(struct device *parent, struct cfdata *match, void *aux) 130 { 131 struct intio_attach_args *ia = (struct intio_attach_args *)aux; 132 133 if (attached) 134 return(0); 135 136 ia->ia_addr = (void *)NEXT_P_MON; 137 138 return(1); 139 } 140 141 void 142 nextkbd_attach(struct device *parent, struct device *self, void *aux) 143 { 144 struct nextkbd_softc *sc = (struct nextkbd_softc *)self; 145 struct intio_attach_args *ia = (struct intio_attach_args *)aux; 146 int isconsole; 147 struct wskbddev_attach_args a; 148 149 printf("\n"); 150 151 isconsole = nextkbd_is_console(ia->ia_bst); /* XXX */ 152 153 if (isconsole) { 154 sc->id = &nextkbd_consdata; 155 } else { 156 sc->id = malloc(sizeof(struct nextkbd_internal), 157 M_DEVBUF, M_WAITOK); 158 159 memset(sc->id, 0, sizeof(struct nextkbd_internal)); 160 sc->id->iot = ia->ia_bst; 161 if (bus_space_map(sc->id->iot, NEXT_P_MON, 162 sizeof(struct mon_regs), 163 0, &sc->id->ioh)) { 164 printf("%s: can't map mon status control register\n", 165 sc->sc_dev.dv_xname); 166 return; 167 } 168 } 169 170 sc->id->t_sc = sc; /* set back pointer */ 171 172 isrlink_autovec(nextkbdhard, sc, NEXT_I_IPL(NEXT_I_KYBD_MOUSE), 0, NULL); 173 174 INTR_ENABLE(NEXT_I_KYBD_MOUSE); 175 176 a.console = isconsole; 177 a.keymap = &nextkbd_keymapdata; 178 a.accessops = &nextkbd_accessops; 179 a.accesscookie = sc; 180 181 /* 182 * Attach the wskbd, saving a handle to it. 183 * XXX XXX XXX 184 */ 185 sc->sc_wskbddev = config_found(self, &a, wskbddevprint); 186 187 attached = 1; 188 } 189 190 int 191 nextkbd_enable(void *v, int on) 192 { 193 /* XXX not sure if this should do anything */ 194 /* printf("nextkbd_enable %d\n", on); */ 195 return 0; 196 } 197 198 void 199 nextkbd_set_leds(void *v, int leds) 200 { 201 struct nextkbd_softc *sc = v; 202 uint32_t hw_leds = 0; 203 int s; 204 205 sc->sc_leds &= ~ NEXT_WSKBD_LEDS; 206 sc->sc_leds |= (leds & NEXT_WSKBD_LEDS); 207 208 if (sc->sc_leds & WSKBD_LED_CAPS) { 209 hw_leds |= 0x30000; 210 } 211 212 s = spltty(); 213 bus_space_write_1(sc->id->iot, sc->id->ioh, 3, 0xc5); 214 /* @@@ need to add: 215 if bit 7 of @ioh+0 set: 216 repeat 2 217 wait until bit 6 of @ioh+2 clears 218 */ 219 bus_space_write_4(sc->id->iot, sc->id->ioh, 4, hw_leds); 220 /* @@@ need to add: 221 wait until bit 4 of @ioh+0 (@ioh+2 if bit 7 was set above) 222 clears 223 */ 224 splx(s); 225 226 return; 227 } 228 229 int 230 nextkbd_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l) 231 { 232 struct nextkbd_softc *sc = v; 233 234 switch (cmd) { 235 case WSKBDIO_GTYPE: 236 /* XXX */ 237 *(int *)data = WSKBD_TYPE_NEXT; 238 return (0); 239 case WSKBDIO_SETLEDS: 240 nextkbd_set_leds (sc, *(int *)data); 241 return (0); 242 case WSKBDIO_GETLEDS: 243 *(int *)data = sc->sc_leds & NEXT_WSKBD_LEDS; 244 return (0); 245 case WSKBDIO_COMPLEXBELL: 246 return (0); 247 } 248 return EPASSTHROUGH; 249 } 250 251 int 252 nextkbdhard(void *arg) 253 { 254 struct nextkbd_softc *sc = arg; 255 int type, key, val; 256 257 if (!INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) return 0; 258 259 #define CSR_INT 0x00800000 260 #define CSR_DATA 0x00400000 261 262 #define KD_KEYMASK 0x007f 263 #define KD_DIRECTION 0x0080 /* pressed or released */ 264 #define KD_CNTL 0x0100 265 #define KD_LSHIFT 0x0200 266 #define KD_RSHIFT 0x0400 267 #define KD_LCOMM 0x0800 268 #define KD_RCOMM 0x1000 269 #define KD_LALT 0x2000 270 #define KD_RALT 0x4000 271 #define KD_VALID 0x8000 /* only set for scancode keys ? */ 272 #define KD_MODS 0x4f00 273 274 val = nextkbd_read_data(sc->id); 275 if ((val != -1) && nextkbd_decode(sc->id, val, &type, &key)) { 276 wskbd_input(sc->sc_wskbddev, type, key); 277 } 278 return(1); 279 } 280 281 int 282 nextkbd_cnattach(bus_space_tag_t bst) 283 { 284 bus_space_handle_t bsh; 285 286 if (bus_space_map(bst, NEXT_P_MON, sizeof(struct mon_regs), 287 0, &bsh)) 288 return (ENXIO); 289 290 memset(&nextkbd_consdata, 0, sizeof(nextkbd_consdata)); 291 292 nextkbd_consdata.iot = bst; 293 nextkbd_consdata.ioh = bsh; 294 nextkbd_consdata.isconsole = 1; 295 296 wskbd_cnattach(&nextkbd_consops, &nextkbd_consdata, 297 &nextkbd_keymapdata); 298 299 return (0); 300 } 301 302 void 303 nextkbd_cngetc(void *v, u_int *type, int *data) 304 { 305 struct nextkbd_internal *t = v; 306 int val; 307 308 for (;;) { 309 if (INTR_OCCURRED(NEXT_I_KYBD_MOUSE)) { 310 val = nextkbd_read_data(t); 311 if ((val != -1) && nextkbd_decode(t, val, type, data)) 312 return; 313 } 314 } 315 } 316 317 void 318 nextkbd_cnpollc(void *v, int on) 319 { 320 struct nextkbd_internal *t = v; 321 322 t->polling = on; 323 if (on) { 324 INTR_DISABLE(NEXT_I_KYBD_MOUSE); 325 } else { 326 INTR_ENABLE(NEXT_I_KYBD_MOUSE); 327 } 328 329 } 330 331 static int 332 nextkbd_read_data(struct nextkbd_internal *id) 333 { 334 unsigned char device; 335 struct mon_regs stat; 336 337 bus_space_read_region_4(id->iot, id->ioh, 0, &stat, 3); 338 if ((stat.mon_csr & CSR_INT) && (stat.mon_csr & CSR_DATA)) { 339 stat.mon_csr &= ~CSR_INT; 340 id->num_ints++; 341 bus_space_write_4(id->iot, id->ioh, 0, stat.mon_csr); 342 device = stat.mon_data >> 28; 343 if (device != 1) return (-1); /* XXX: mouse */ 344 return (stat.mon_data & 0xffff); 345 } 346 return (-1); 347 } 348 349 static int 350 nextkbd_decode(struct nextkbd_internal *id, int datain, u_int *type, 351 int *dataout) 352 { 353 /* printf("datain %08x mods %08x\n", datain, id->mods); */ 354 355 if ((datain ^ id->mods) & KD_LSHIFT) { 356 id->mods ^= KD_LSHIFT; 357 *dataout = 90; 358 if (datain & KD_LSHIFT) 359 *type = WSCONS_EVENT_KEY_DOWN; 360 else 361 *type = WSCONS_EVENT_KEY_UP; 362 } else if ((datain ^ id->mods) & KD_RSHIFT) { 363 id->mods ^= KD_RSHIFT; 364 *dataout = 91; 365 if (datain & KD_RSHIFT) 366 *type = WSCONS_EVENT_KEY_DOWN; 367 else 368 *type = WSCONS_EVENT_KEY_UP; 369 } else if ((datain ^ id->mods) & KD_LALT) { 370 id->mods ^= KD_LALT; 371 *dataout = 92; 372 if (datain & KD_LALT) 373 *type = WSCONS_EVENT_KEY_DOWN; 374 else 375 *type = WSCONS_EVENT_KEY_UP; 376 } else if ((datain ^ id->mods) & KD_RALT) { 377 id->mods ^= KD_RALT; 378 *dataout = 93; 379 if (datain & KD_RALT) 380 *type = WSCONS_EVENT_KEY_DOWN; 381 else 382 *type = WSCONS_EVENT_KEY_UP; 383 } else if ((datain ^ id->mods) & KD_CNTL) { 384 id->mods ^= KD_CNTL; 385 *dataout = 94; 386 if (datain & KD_CNTL) 387 *type = WSCONS_EVENT_KEY_DOWN; 388 else 389 *type = WSCONS_EVENT_KEY_UP; 390 } else if ((datain ^ id->mods) & KD_LCOMM) { 391 id->mods ^= KD_LCOMM; 392 *dataout = 95; 393 if (datain & KD_LCOMM) 394 *type = WSCONS_EVENT_KEY_DOWN; 395 else 396 *type = WSCONS_EVENT_KEY_UP; 397 } else if ((datain ^ id->mods) & KD_RCOMM) { 398 id->mods ^= KD_RCOMM; 399 *dataout = 96; 400 if (datain & KD_RCOMM) 401 *type = WSCONS_EVENT_KEY_DOWN; 402 else 403 *type = WSCONS_EVENT_KEY_UP; 404 } else if (datain & KD_KEYMASK) { 405 if (datain & KD_DIRECTION) 406 *type = WSCONS_EVENT_KEY_UP; 407 else 408 *type = WSCONS_EVENT_KEY_DOWN; 409 410 *dataout = (datain & KD_KEYMASK); 411 } else { 412 *dataout = 0; 413 } 414 415 return 1; 416 } 417