1 /* $NetBSD: dzkbd.c,v 1.20 2007/12/03 15:34:31 ad Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This software was developed by the Computer Systems Engineering group 8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 9 * contributed to Berkeley. 10 * 11 * All advertising materials mentioning features or use of this software 12 * must display the following acknowledgement: 13 * This product includes software developed by the University of 14 * California, Lawrence Berkeley Laboratory. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 3. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)kbd.c 8.2 (Berkeley) 10/30/93 41 */ 42 43 /* 44 * LK200/LK400 keyboard attached to line 0 of the DZ*-11 45 */ 46 47 #include <sys/cdefs.h> 48 __KERNEL_RCSID(0, "$NetBSD: dzkbd.c,v 1.20 2007/12/03 15:34:31 ad Exp $"); 49 50 #include <sys/param.h> 51 #include <sys/systm.h> 52 #include <sys/device.h> 53 #include <sys/ioctl.h> 54 #include <sys/syslog.h> 55 #include <sys/malloc.h> 56 #include <sys/intr.h> 57 58 #include <dev/wscons/wsconsio.h> 59 #include <dev/wscons/wskbdvar.h> 60 #include <dev/wscons/wsksymdef.h> 61 #include <dev/wscons/wsksymvar.h> 62 #include <dev/dec/wskbdmap_lk201.h> 63 64 #include <sys/bus.h> 65 66 #include <dev/dec/dzreg.h> 67 #include <dev/dec/dzvar.h> 68 #include <dev/dec/dzkbdvar.h> 69 #include <dev/dec/lk201reg.h> 70 #include <dev/dec/lk201var.h> 71 72 #include "locators.h" 73 74 struct dzkbd_internal { 75 struct dz_linestate *dzi_ls; 76 struct lk201_state dzi_ks; 77 }; 78 79 struct dzkbd_internal dzkbd_console_internal; 80 81 struct dzkbd_softc { 82 struct device dzkbd_dev; /* required first: base device */ 83 84 struct dzkbd_internal *sc_itl; 85 86 int sc_enabled; 87 int kbd_type; 88 89 struct device *sc_wskbddev; 90 }; 91 92 static int dzkbd_input(void *, int); 93 94 static int dzkbd_match(struct device *, struct cfdata *, void *); 95 static void dzkbd_attach(struct device *, struct device *, void *); 96 97 CFATTACH_DECL(dzkbd, sizeof(struct dzkbd_softc), 98 dzkbd_match, dzkbd_attach, NULL, NULL); 99 100 static int dzkbd_enable(void *, int); 101 static void dzkbd_set_leds(void *, int); 102 static int dzkbd_ioctl(void *, u_long, void *, int, struct lwp *); 103 104 const struct wskbd_accessops dzkbd_accessops = { 105 dzkbd_enable, 106 dzkbd_set_leds, 107 dzkbd_ioctl, 108 }; 109 110 static void dzkbd_cngetc(void *, u_int *, int *); 111 static void dzkbd_cnpollc(void *, int); 112 113 const struct wskbd_consops dzkbd_consops = { 114 dzkbd_cngetc, 115 dzkbd_cnpollc, 116 }; 117 118 static int dzkbd_sendchar(void *, u_char); 119 120 const struct wskbd_mapdata dzkbd_keymapdata = { 121 lkkbd_keydesctab, 122 #ifdef DZKBD_LAYOUT 123 DZKBD_LAYOUT, 124 #else 125 KB_US | KB_LK401, 126 #endif 127 }; 128 129 /* 130 * kbd_match: how is this dz line configured? 131 */ 132 static int 133 dzkbd_match(struct device *parent, struct cfdata *cf, void *aux) 134 { 135 struct dzkm_attach_args *daa = aux; 136 137 /* Exact match is better than wildcard. */ 138 if (cf->cf_loc[DZCF_LINE] == daa->daa_line) 139 return 2; 140 141 /* This driver accepts wildcard. */ 142 if (cf->cf_loc[DZCF_LINE] == DZCF_LINE_DEFAULT) 143 return 1; 144 145 return 0; 146 } 147 148 static void 149 dzkbd_attach(struct device *parent, struct device *self, void *aux) 150 { 151 struct dz_softc *dz = device_private(parent); 152 struct dzkbd_softc *dzkbd = device_private(self); 153 struct dzkm_attach_args *daa = aux; 154 struct dz_linestate *ls; 155 struct dzkbd_internal *dzi; 156 struct wskbddev_attach_args a; 157 int isconsole; 158 159 dz->sc_dz[daa->daa_line].dz_catch = dzkbd_input; 160 dz->sc_dz[daa->daa_line].dz_private = dzkbd; 161 ls = &dz->sc_dz[daa->daa_line]; 162 163 isconsole = (daa->daa_flags & DZKBD_CONSOLE); 164 165 if (isconsole) { 166 dzi = &dzkbd_console_internal; 167 } else { 168 dzi = malloc(sizeof(struct dzkbd_internal), 169 M_DEVBUF, M_NOWAIT); 170 dzi->dzi_ks.attmt.sendchar = dzkbd_sendchar; 171 dzi->dzi_ks.attmt.cookie = ls; 172 } 173 dzi->dzi_ls = ls; 174 dzkbd->sc_itl = dzi; 175 176 printf("\n"); 177 178 if (!isconsole) { 179 DELAY(100000); 180 lk201_init(&dzi->dzi_ks); 181 } 182 183 /* XXX should identify keyboard ID here XXX */ 184 /* XXX layout and the number of LED is varying XXX */ 185 186 dzkbd->kbd_type = WSKBD_TYPE_LK201; 187 188 dzkbd->sc_enabled = 1; 189 190 a.console = isconsole; 191 a.keymap = &dzkbd_keymapdata; 192 a.accessops = &dzkbd_accessops; 193 a.accesscookie = dzkbd; 194 195 dzkbd->sc_wskbddev = config_found(self, &a, wskbddevprint); 196 } 197 198 int 199 dzkbd_cnattach(ls) 200 struct dz_linestate *ls; 201 { 202 203 dzkbd_console_internal.dzi_ks.attmt.sendchar = dzkbd_sendchar; 204 dzkbd_console_internal.dzi_ks.attmt.cookie = ls; 205 lk201_init(&dzkbd_console_internal.dzi_ks); 206 dzkbd_console_internal.dzi_ls = ls; 207 208 wskbd_cnattach(&dzkbd_consops, &dzkbd_console_internal, 209 &dzkbd_keymapdata); 210 211 return 0; 212 } 213 214 static int 215 dzkbd_enable(v, on) 216 void *v; 217 int on; 218 { 219 struct dzkbd_softc *sc = v; 220 221 sc->sc_enabled = on; 222 return 0; 223 } 224 225 static int 226 dzkbd_sendchar(v, c) 227 void *v; 228 u_char c; 229 { 230 struct dz_linestate *ls = v; 231 int s; 232 233 s = spltty(); 234 dzputc(ls, c); 235 splx(s); 236 return (0); 237 } 238 239 static void 240 dzkbd_cngetc(v, type, data) 241 void *v; 242 u_int *type; 243 int *data; 244 { 245 struct dzkbd_internal *dzi = v; 246 int c; 247 248 do { 249 c = dzgetc(dzi->dzi_ls); 250 } while (!lk201_decode(&dzi->dzi_ks, c, type, data)); 251 } 252 253 static void 254 dzkbd_cnpollc(v, on) 255 void *v; 256 int on; 257 { 258 #if 0 259 struct dzkbd_internal *dzi = v; 260 #endif 261 } 262 263 static void 264 dzkbd_set_leds(v, leds) 265 void *v; 266 int leds; 267 { 268 struct dzkbd_softc *sc = (struct dzkbd_softc *)v; 269 270 //printf("dzkbd_set_leds\n"); 271 lk201_set_leds(&sc->sc_itl->dzi_ks, leds); 272 } 273 274 static int 275 dzkbd_ioctl(v, cmd, data, flag, l) 276 void *v; 277 u_long cmd; 278 void *data; 279 int flag; 280 struct lwp *l; 281 { 282 struct dzkbd_softc *sc = (struct dzkbd_softc *)v; 283 284 switch (cmd) { 285 case WSKBDIO_GTYPE: 286 *(int *)data = sc->kbd_type; 287 return 0; 288 case WSKBDIO_SETLEDS: 289 lk201_set_leds(&sc->sc_itl->dzi_ks, *(int *)data); 290 return 0; 291 case WSKBDIO_GETLEDS: 292 /* XXX don't dig in kbd internals */ 293 *(int *)data = sc->sc_itl->dzi_ks.leds_state; 294 return 0; 295 case WSKBDIO_COMPLEXBELL: 296 lk201_bell(&sc->sc_itl->dzi_ks, 297 (struct wskbd_bell_data *)data); 298 return 0; 299 case WSKBDIO_SETKEYCLICK: 300 lk201_set_keyclick(&sc->sc_itl->dzi_ks, *(int *)data); 301 return 0; 302 case WSKBDIO_GETKEYCLICK: 303 /* XXX don't dig in kbd internals */ 304 *(int *)data = sc->sc_itl->dzi_ks.kcvol; 305 return 0; 306 } 307 return (EPASSTHROUGH); 308 } 309 310 static int 311 dzkbd_input(v, data) 312 void *v; 313 int data; 314 { 315 struct dzkbd_softc *sc = (struct dzkbd_softc *)v; 316 u_int type; 317 int val; 318 319 if (sc->sc_enabled == 0) 320 return(0); 321 322 if (lk201_decode(&sc->sc_itl->dzi_ks, data, &type, &val)) 323 wskbd_input(sc->sc_wskbddev, type, val); 324 return(1); 325 } 326 327