1 /* $NetBSD: hpckbd.c,v 1.16 2006/02/13 16:35:57 uwe 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.16 2006/02/13 16:35:57 uwe 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/pckbport/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 uint8_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 lwp *); 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 /* 272 * XXX The way this is done is really wrong. The __UNCONST() 273 * is a hint as to what is wrong. This actually ends up modifying 274 * initialized data which is marked "const". 275 * The reason we get away with it here is apparently that text 276 * and read-only data gets mapped read/write on the platforms 277 * using this code. 278 */ 279 desc = (struct wscons_keydesc *)__UNCONST(hpckbd_keymapdata.keydesc); 280 for (i = 0; desc[i].name != 0; i++) { 281 if ((desc[i].name & KB_MACHDEP) && desc[i].map == NULL) { 282 desc[i].map = map; 283 desc[i].map_size = mapsize; 284 } 285 } 286 287 return; 288 } 289 290 void 291 hpckbd_keymap_lookup(struct hpckbd_core *hc) 292 { 293 const struct hpckbd_keymap_table *tab; 294 platid_mask_t mask; 295 296 for (tab = hpckbd_keymap_table; tab->ht_platform != NULL; tab++) { 297 298 mask = PLATID_DEREF(tab->ht_platform); 299 300 if (platid_match(&platid, &mask)) { 301 hc->hc_keymap = tab->ht_keymap; 302 hc->hc_special = tab->ht_special; 303 #if !defined(PCKBD_LAYOUT) 304 hpckbd_keymapdata.layout = tab->ht_layout; 305 #endif 306 if (tab->ht_cmdmap.map) { 307 hpckbd_keymap_setup(hc, tab->ht_cmdmap.map, 308 tab->ht_cmdmap.size); 309 #if !defined(PCKBD_LAYOUT) 310 hpckbd_keymapdata.layout |= KB_MACHDEP; 311 #endif 312 } else { 313 hpckbd_keymapdata.layout &= ~KB_MACHDEP; 314 } 315 return; 316 } 317 } 318 319 /* no keymap. use default. */ 320 hc->hc_keymap = default_keymap; 321 hc->hc_special = default_special_keymap; 322 #if !defined(PCKBD_LAYOUT) 323 hpckbd_keymapdata.layout = KB_US; 324 #endif 325 } 326 327 void 328 __hpckbd_input_hook(void *arg) 329 { 330 #if 0 331 struct hpckbd_core *hc = arg; 332 333 if (hc->hc_polling) { 334 hc->hc_type = WSCONS_EVENT_ALL_KEYS_UP; 335 } 336 #endif 337 } 338 339 int 340 __hpckbd_input(void *arg, int flag, int scancode) 341 { 342 struct hpckbd_core *hc = arg; 343 int type, key; 344 345 if (flag) { 346 type = WSCONS_EVENT_KEY_DOWN; 347 } else { 348 type = WSCONS_EVENT_KEY_UP; 349 } 350 351 key = hc->hc_keymap[scancode]; 352 if (key == UNK) { 353 #ifdef DEBUG 354 printf("hpckbd: unknown scan code %#x (%d, %d)\n", 355 scancode, scancode >> 3, 356 scancode - ((scancode >> 3) << 3)); 357 #endif /* DEBUG */ 358 return (0); 359 } 360 361 if (key == IGN) { 362 return (0); 363 } 364 365 if (key == SPL) { 366 if (!flag) 367 return (0); 368 369 if (scancode == hc->hc_special[KEY_SPECIAL_OFF]) { 370 #ifdef DEBUG 371 printf("off button\n"); // XXX notyet -uch 372 #endif 373 } else if (scancode == hc->hc_special[KEY_SPECIAL_LIGHT]) { 374 static int onoff; /* XXX -uch */ 375 config_hook_call(CONFIG_HOOK_BUTTONEVENT, 376 CONFIG_HOOK_BUTTONEVENT_LIGHT, 377 (void *)(onoff ^= 1)); 378 } else { 379 #ifdef DEBUG 380 printf("unknown special key %d\n", scancode); 381 #endif 382 } 383 384 return (0); 385 } 386 387 if (hc->hc_polling) { 388 if (hpckbd_putevent(hc, type, key) == 0) 389 printf("hpckbd: queue over flow\n"); 390 } else { 391 #ifdef WSDISPLAY_COMPAT_RAWKBD 392 if (hc->hc_rawkbd) { 393 int n; 394 u_char data[16]; 395 n = pckbd_encode(type, key, data); 396 wskbd_rawinput(hc->hc_wskbddev, data, n); 397 } else 398 #endif 399 wskbd_input(hc->hc_wskbddev, type, key); 400 } 401 402 return (0); 403 } 404 405 /* 406 * console support routines 407 */ 408 int 409 hpckbd_cnattach(struct hpckbd_ic_if *ic) 410 { 411 struct hpckbd_core *hc = &hpckbd_consdata; 412 413 hpckbd_initcore(hc, ic, 1 /* console */); 414 415 /* attach controller */ 416 hpckbd_initif(hc); 417 418 /* attach wskbd */ 419 wskbd_cnattach(&hpckbd_consops, hc, &hpckbd_keymapdata); 420 421 return (0); 422 } 423 424 void 425 hpckbd_cngetc(void *arg, u_int *type, int *data) 426 { 427 struct hpckbd_core *hc = arg; 428 429 if (!hc->hc_console || !hc->hc_polling || !hc->hc_ic) 430 return; 431 432 while (hpckbd_getevent(hc, type, data) == 0) /* busy loop */ 433 hpckbd_ic_poll(hc->hc_ic); 434 } 435 436 void 437 hpckbd_cnpollc(void *arg, int on) 438 { 439 struct hpckbd_core *hc = arg; 440 441 hc->hc_polling = on; 442 } 443 444 int 445 hpckbd_enable(void *arg, int on) 446 { 447 struct hpckbd_core *hc = arg; 448 449 if (on) { 450 if (hc->hc_enabled) 451 return (EBUSY); 452 hc->hc_enabled = 1; 453 } else { 454 if (hc->hc_console) 455 return (EBUSY); 456 hc->hc_enabled = 0; 457 } 458 459 return (0); 460 } 461 462 void 463 hpckbd_set_leds(void *arg, int leds) 464 { 465 /* Can you find any LED which tells you about keyboard? */ 466 } 467 468 int 469 hpckbd_ioctl(void *arg, u_long cmd, caddr_t data, int flag, struct lwp *l) 470 { 471 #ifdef WSDISPLAY_COMPAT_RAWKBD 472 struct hpckbd_core *hc = arg; 473 #endif 474 switch (cmd) { 475 case WSKBDIO_GTYPE: 476 *(int *)data = WSKBD_TYPE_HPC_KBD; 477 return (0); 478 case WSKBDIO_SETLEDS: 479 return 0; 480 case WSKBDIO_GETLEDS: 481 *(int *)data = 0; /* dummy for wsconsctl(8) */ 482 return (0); 483 #ifdef WSDISPLAY_COMPAT_RAWKBD 484 case WSKBDIO_SETMODE: 485 hc->hc_rawkbd = (*(int *)data == WSKBD_RAW); 486 return (0); 487 #endif 488 } 489 return (EPASSTHROUGH); 490 } 491