1 /* $NetBSD: hpckbd.c,v 1.10 2002/10/02 16:33:50 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1999-2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: hpckbd.c,v 1.10 2002/10/02 16:33:50 thorpej Exp $"); 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/device.h> 45 46 #include <sys/tty.h> 47 48 #include <machine/bus.h> 49 #include <machine/intr.h> 50 51 #include <machine/config_hook.h> 52 #include <machine/platid.h> 53 #include <machine/platid_mask.h> 54 55 #include "opt_wsdisplay_compat.h" 56 #include "opt_pckbd_layout.h" 57 #include <dev/wscons/wsksymdef.h> 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/pckbc/wskbdmap_mfii.h> 63 #ifdef WSDISPLAY_COMPAT_RAWKBD 64 #include <dev/hpc/pckbd_encode.h> 65 #endif 66 67 #include <dev/hpc/hpckbdvar.h> 68 #include <dev/hpc/hpckbdkeymap.h> 69 70 struct hpckbd_softc; 71 72 #define NEVENTQ 32 73 struct hpckbd_eventq { 74 u_int hq_type; 75 int hq_data; 76 }; 77 78 struct hpckbd_core { 79 struct hpckbd_if hc_if; 80 struct hpckbd_ic_if *hc_ic; 81 const u_int8_t *hc_keymap; 82 const int *hc_special; 83 int hc_polling; 84 int hc_console; 85 #define NEVENTQ 32 86 struct hpckbd_eventq hc_eventq[NEVENTQ]; 87 struct hpckbd_eventq *hc_head, *hc_tail; 88 int hc_nevents; 89 int hc_enabled; 90 struct device *hc_wskbddev; 91 struct hpckbd_softc* hc_sc; /* back link */ 92 #ifdef WSDISPLAY_COMPAT_RAWKBD 93 int hc_rawkbd; 94 #endif 95 }; 96 97 struct hpckbd_softc { 98 struct device sc_dev; 99 struct hpckbd_core *sc_core; 100 struct hpckbd_core sc_coredata; 101 }; 102 103 int hpckbd_match(struct device *, struct cfdata *, void *); 104 void hpckbd_attach(struct device *, struct device *, void *); 105 106 void hpckbd_initcore(struct hpckbd_core *, struct hpckbd_ic_if *, int); 107 void hpckbd_initif(struct hpckbd_core *); 108 int hpckbd_getevent(struct hpckbd_core *, u_int *, int *); 109 int hpckbd_putevent(struct hpckbd_core *, u_int, int); 110 void hpckbd_keymap_lookup(struct hpckbd_core*); 111 void hpckbd_keymap_setup(struct hpckbd_core *, const keysym_t *, int); 112 int __hpckbd_input(void *, int, int); 113 void __hpckbd_input_hook(void*); 114 115 CFATTACH_DECL(hpckbd, sizeof(struct hpckbd_softc), 116 hpckbd_match, hpckbd_attach, NULL, NULL); 117 118 /* wskbd accessopts */ 119 int hpckbd_enable(void *, int); 120 void hpckbd_set_leds(void *, int); 121 int hpckbd_ioctl(void *, u_long, caddr_t, int, struct proc *); 122 123 /* consopts */ 124 struct hpckbd_core hpckbd_consdata; 125 void hpckbd_cngetc(void *, u_int *, int*); 126 void hpckbd_cnpollc(void *, int); 127 128 const struct wskbd_accessops hpckbd_accessops = { 129 hpckbd_enable, 130 hpckbd_set_leds, 131 hpckbd_ioctl, 132 }; 133 134 const struct wskbd_consops hpckbd_consops = { 135 hpckbd_cngetc, 136 hpckbd_cnpollc, 137 }; 138 139 struct wskbd_mapdata hpckbd_keymapdata = { 140 pckbd_keydesctab, 141 #ifdef PCKBD_LAYOUT 142 PCKBD_LAYOUT 143 #else 144 KB_US 145 #endif 146 }; 147 148 int 149 hpckbd_match(struct device *parent, struct cfdata *cf, void *aux) 150 { 151 return (1); 152 } 153 154 void 155 hpckbd_attach(struct device *parent, struct device *self, void *aux) 156 { 157 struct hpckbd_attach_args *haa = aux; 158 struct hpckbd_softc *sc = (void*)self; 159 struct hpckbd_ic_if *ic = haa->haa_ic; 160 struct wskbddev_attach_args wa; 161 162 /* 163 * Initialize core if it isn't console 164 */ 165 if (hpckbd_consdata.hc_ic == ic) { 166 sc->sc_core = &hpckbd_consdata; 167 /* The core has been initialized in hpckbd_cnattach. */ 168 } else { 169 sc->sc_core = &sc->sc_coredata; 170 hpckbd_initcore(sc->sc_core, ic, 0 /* not console */); 171 } 172 173 if (sc->sc_core->hc_keymap == default_keymap) 174 printf(": no keymap."); 175 176 printf("\n"); 177 178 /* 179 * setup hpckbd public interface for parent controller. 180 */ 181 hpckbd_initif(sc->sc_core); 182 183 /* 184 * attach wskbd 185 */ 186 wa.console = sc->sc_core->hc_console; 187 wa.keymap = &hpckbd_keymapdata; 188 wa.accessops = &hpckbd_accessops; 189 wa.accesscookie = sc->sc_core; 190 sc->sc_core->hc_wskbddev = config_found(self, &wa, wskbddevprint); 191 } 192 193 int 194 hpckbd_print(void *aux, const char *pnp) 195 { 196 return (pnp ? QUIET : UNCONF); 197 } 198 199 void 200 hpckbd_initcore(struct hpckbd_core *hc, struct hpckbd_ic_if *ic, int console) 201 { 202 hc->hc_polling = 0; 203 hc->hc_console = console; 204 hc->hc_ic = ic; 205 206 /* setup event queue */ 207 hc->hc_head = hc->hc_tail = hc->hc_eventq; 208 hc->hc_nevents = 0; 209 210 hpckbd_keymap_lookup(hc); 211 } 212 213 void 214 hpckbd_initif(struct hpckbd_core *hc) 215 { 216 struct hpckbd_if *kbdif = &hc->hc_if; 217 218 kbdif->hi_ctx = hc; 219 kbdif->hi_input = __hpckbd_input; 220 kbdif->hi_input_hook = __hpckbd_input_hook; 221 hpckbd_ic_establish(hc->hc_ic, &hc->hc_if); 222 } 223 224 int 225 hpckbd_putevent(struct hpckbd_core* hc, u_int type, int data) 226 { 227 int s = spltty(); 228 229 if (hc->hc_nevents == NEVENTQ) { 230 splx(s); 231 return (0); /* queue is full */ 232 } 233 234 hc->hc_nevents++; 235 hc->hc_tail->hq_type = type; 236 hc->hc_tail->hq_data = data; 237 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_tail) 238 hc->hc_tail = hc->hc_eventq; 239 splx(s); 240 241 return (1); 242 } 243 244 int 245 hpckbd_getevent(struct hpckbd_core* hc, u_int *type, int *data) 246 { 247 int s = spltty(); 248 249 if (hc->hc_nevents == 0) { 250 splx(s); 251 return (0); /* queue is empty */ 252 } 253 254 *type = hc->hc_head->hq_type; 255 *data = hc->hc_head->hq_data; 256 hc->hc_nevents--; 257 if (&hc->hc_eventq[NEVENTQ] <= ++hc->hc_head) 258 hc->hc_head = hc->hc_eventq; 259 splx(s); 260 261 return (1); 262 } 263 264 void 265 hpckbd_keymap_setup(struct hpckbd_core *hc, const keysym_t *map, int mapsize) 266 { 267 int i; 268 struct wscons_keydesc *desc; 269 270 /* fix keydesc table */ 271 desc = (struct wscons_keydesc *)hpckbd_keymapdata.keydesc; 272 for (i = 0; desc[i].name != 0; i++) { 273 if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) { 274 desc[i].map = map; 275 desc[i].map_size = mapsize; 276 } 277 } 278 279 return; 280 } 281 282 void 283 hpckbd_keymap_lookup(struct hpckbd_core *hc) 284 { 285 const struct hpckbd_keymap_table *tab; 286 platid_mask_t mask; 287 288 for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) { 289 290 mask = PLATID_DEREF(tab->ht_platform); 291 292 if (platid_match(&platid, &mask)) { 293 hc->hc_keymap = tab->ht_keymap; 294 hc->hc_special = tab->ht_special; 295 #if !defined(PCKBD_LAYOUT) 296 hpckbd_keymapdata.layout = tab->ht_layout; 297 #endif 298 if (tab->ht_cmdmap.map) { 299 hpckbd_keymap_setup(hc, tab->ht_cmdmap.map, 300 tab->ht_cmdmap.size); 301 #if !defined(PCKBD_LAYOUT) 302 hpckbd_keymapdata.layout |= KB_MACHDEP; 303 #endif 304 } else { 305 hpckbd_keymapdata.layout &= ~KB_MACHDEP; 306 } 307 return; 308 } 309 } 310 311 /* no keymap. use default. */ 312 hc->hc_keymap = default_keymap; 313 hc->hc_special = default_special_keymap; 314 #if !defined(PCKBD_LAYOUT) 315 hpckbd_keymapdata.layout = KB_US; 316 #endif 317 } 318 319 void 320 __hpckbd_input_hook(void *arg) 321 { 322 #if 0 323 struct hpckbd_core *hc = arg; 324 325 if (hc->hc_polling) { 326 hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP; 327 } 328 #endif 329 } 330 331 int 332 __hpckbd_input(void *arg, int flag, int scancode) 333 { 334 struct hpckbd_core *hc = arg; 335 int type, key; 336 337 if (flag) { 338 type = WSCONS_EVENT_KEY_DOWN; 339 } else { 340 type = WSCONS_EVENT_KEY_UP; 341 } 342 343 if ((key = hc->hc_keymap[scancode]) == UNK) { 344 #ifdef DEBUG 345 printf("hpckbd: unknown scan code %#x (%d, %d)\n", 346 scancode, scancode >> 3, 347 scancode - ((scancode >> 3) << 3)); 348 #endif /* DEBUG */ 349 return (0); 350 } 351 352 if (key == IGN) { 353 return (0); 354 } 355 356 if (key == SPL) { 357 if (!flag) 358 return (0); 359 360 if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) { 361 #ifdef DEBUG 362 printf("off button\n"); // XXX notyet -uch 363 #endif 364 } else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) { 365 static int onoff; /* XXX -uch */ 366 config_hook_call(CONFIG_HOOK_BUTTONEVENT, 367 CONFIG_HOOK_BUTTONEVENT_LIGHT, 368 (void *)(onoff ^= 1)); 369 } else { 370 #ifdef DEBUG 371 printf("unknown special key %d\n", scancode); 372 #endif 373 } 374 375 return (0); 376 } 377 378 if (hc->hc_polling) { 379 if (hpckbd_putevent(hc, type, hc->hc_keymap[scancode]) == 0) 380 printf("hpckbd: queue over flow"); 381 } else { 382 #ifdef WSDISPLAY_COMPAT_RAWKBD 383 if (hc->hc_rawkbd) { 384 int n; 385 u_char data[16]; 386 n = pckbd_encode(type, hc->hc_keymap[scancode], data); 387 wskbd_rawinput(hc->hc_wskbddev, data, n); 388 } else 389 #endif 390 wskbd_input(hc->hc_wskbddev, type, hc->hc_keymap[scancode]); 391 } 392 393 return (0); 394 } 395 396 /* 397 * console support routines 398 */ 399 int 400 hpckbd_cnattach(struct hpckbd_ic_if *ic) 401 { 402 struct hpckbd_core *hc = &hpckbd_consdata; 403 404 hpckbd_initcore(hc, ic, 1 /* console */); 405 406 /* attach controller */ 407 hpckbd_initif(hc); 408 409 /* attach wskbd */ 410 wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata); 411 412 return (0); 413 } 414 415 void 416 hpckbd_cngetc(void *arg, u_int *type, int *data) 417 { 418 struct hpckbd_core *hc = arg; 419 420 if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic) 421 return; 422 423 while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */ 424 hpckbd_ic_poll(hc->hc_ic); 425 } 426 427 void 428 hpckbd_cnpollc(void *arg, int on) 429 { 430 struct hpckbd_core *hc = arg; 431 432 hc->hc_polling = on; 433 } 434 435 int 436 hpckbd_enable(void *arg, int on) 437 { 438 struct hpckbd_core *hc = arg; 439 440 if (on) { 441 if (hc->hc_enabled) 442 return (EBUSY); 443 hc->hc_enabled = 1; 444 } else { 445 if (hc->hc_console) 446 return (EBUSY); 447 hc->hc_enabled = 0; 448 } 449 450 return (0); 451 } 452 453 void 454 hpckbd_set_leds(void *arg, int leds) 455 { 456 /* Can you find any LED which tells you about keyboard? */ 457 } 458 459 int 460 hpckbd_ioctl(void *arg, u_long cmd, caddr_t data, int flag, struct proc *p) 461 { 462 #ifdef WSDISPLAY_COMPAT_RAWKBD 463 struct hpckbd_core *hc = arg; 464 #endif 465 switch (cmd) { 466 case WSKBDIO_GTYPE: 467 *(int *)data = WSKBD_TYPE_HPC_KBD; 468 return (0); 469 case WSKBDIO_SETLEDS: 470 return 0; 471 case WSKBDIO_GETLEDS: 472 *(int *)data = 0; /* dummy for wsconsctl(8) */ 473 return (0); 474 #ifdef WSDISPLAY_COMPAT_RAWKBD 475 case WSKBDIO_SETMODE: 476 hc->hc_rawkbd = (*(int *)data == WSKBD_RAW); 477 return (0); 478 #endif 479 } 480 return (EPASSTHROUGH); 481 } 482