1 /* $NetBSD: hpf1275a_tty.c,v 1.10 2006/05/22 20:40:07 uwe Exp $ */ 2 3 /* 4 * Copyright (c) 2004 Valeriy E. Ushakov 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: hpf1275a_tty.c,v 1.10 2006/05/22 20:40:07 uwe Exp $"); 32 33 #include "opt_wsdisplay_compat.h" 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/conf.h> 38 #include <sys/device.h> 39 #include <sys/tty.h> 40 #include <sys/fcntl.h> 41 #include <sys/proc.h> /* XXX: for curproc */ 42 #include <sys/systm.h> 43 #include <sys/kauth.h> 44 #ifdef GPROF 45 #include <sys/gmon.h> 46 #endif 47 48 #include <dev/wscons/wsksymvar.h> 49 #include <dev/pckbport/wskbdmap_mfii.h> 50 #ifdef WSDISPLAY_COMPAT_RAWKBD 51 #include <dev/hpc/pckbd_encode.h> 52 #endif 53 54 #include <dev/wscons/wsconsio.h> 55 #include <dev/wscons/wskbdvar.h> 56 #include <dev/wscons/wsksymdef.h> 57 #include <dev/wscons/wsksymvar.h> 58 59 60 extern struct cfdriver hpf1275a_cd; 61 62 struct hpf1275a_softc { 63 struct device sc_dev; 64 65 struct tty *sc_tp; /* back reference to the tty */ 66 struct device *sc_wskbd; /* wskbd child */ 67 int sc_enabled; 68 #ifdef WSDISPLAY_COMPAT_RAWKBD 69 int sc_rawkbd; 70 #endif 71 }; 72 73 74 /* pseudo-device initialization */ 75 extern void hpf1275aattach(int); 76 77 /* line discipline methods */ 78 static int hpf1275a_open(dev_t, struct tty *); 79 static int hpf1275a_close(struct tty *, int); 80 static int hpf1275a_input(int, struct tty *); 81 82 /* autoconf(9) methods */ 83 static int hpf1275a_match(struct device *, struct cfdata *, void *); 84 static void hpf1275a_attach(struct device *, struct device *, void *); 85 static int hpf1275a_detach(struct device *, int); 86 87 /* wskbd(4) accessops */ 88 static int hpf1275a_wskbd_enable(void *, int); 89 static void hpf1275a_wskbd_set_leds(void *, int); 90 static int hpf1275a_wskbd_ioctl(void *, u_long, caddr_t, int, 91 struct lwp *); 92 93 94 CFATTACH_DECL(hpf1275a, sizeof(struct hpf1275a_softc), 95 hpf1275a_match, hpf1275a_attach, hpf1275a_detach, NULL); 96 97 98 static struct linesw hpf1275a_disc = { 99 .l_name = "hpf1275a", 100 .l_open = hpf1275a_open, 101 .l_close = hpf1275a_close, 102 .l_read = ttyerrio, 103 .l_write = ttyerrio, 104 .l_ioctl = ttynullioctl, 105 .l_rint = hpf1275a_input, 106 .l_start = ttstart, 107 .l_modem = nullmodem, 108 .l_poll = ttpoll 109 }; 110 111 112 static const struct wskbd_accessops hpf1275a_wskbd_accessops = { 113 hpf1275a_wskbd_enable, 114 hpf1275a_wskbd_set_leds, 115 hpf1275a_wskbd_ioctl 116 }; 117 118 119 static struct wskbd_mapdata hpf1275a_wskbd_keymapdata = { 120 pckbd_keydesctab, KB_US 121 }; 122 123 124 /* F1275A scancodes -> XT scancodes so that we can use pckbd_keydesctab. */ 125 static const uint8_t hpf1275a_to_xtscan[128] = { 126 [0x04] = 30, /* a */ 127 [0x05] = 48, /* b */ 128 [0x06] = 46, /* c */ 129 [0x07] = 32, /* d */ 130 [0x08] = 18, /* e */ 131 [0x09] = 33, /* f */ 132 [0x0a] = 34, /* g */ 133 [0x0b] = 35, /* h */ 134 [0x0c] = 23, /* i */ 135 [0x0d] = 36, /* j */ 136 [0x0e] = 37, /* k */ 137 [0x0f] = 38, /* l */ 138 [0x10] = 50, /* m */ 139 [0x11] = 49, /* n */ 140 [0x12] = 24, /* o */ 141 [0x13] = 25, /* p */ 142 [0x14] = 16, /* q */ 143 [0x15] = 19, /* r */ 144 [0x16] = 31, /* s */ 145 [0x17] = 20, /* t */ 146 [0x18] = 22, /* u */ 147 [0x19] = 47, /* v */ 148 [0x1a] = 17, /* w */ 149 [0x1b] = 45, /* x */ 150 [0x1c] = 21, /* y */ 151 [0x1d] = 44, /* z */ 152 153 [0x1e] = 2, /* 1 */ 154 [0x1f] = 3, /* 2 */ 155 [0x20] = 4, /* 3 */ 156 [0x21] = 5, /* 4 */ 157 [0x22] = 6, /* 5 */ 158 [0x23] = 7, /* 6 */ 159 [0x24] = 8, /* 7 */ 160 [0x25] = 9, /* 8 */ 161 [0x26] = 10, /* 9 */ 162 [0x27] = 11, /* 0 */ 163 164 [0x28] = 28, /* Enter */ 165 166 [0x29] = 1, /* ESC */ 167 [0x2a] = 14, /* Backspace */ 168 [0x2b] = 15, /* Tab */ 169 [0x2c] = 57, /* Space */ 170 171 [0x2d] = 12, /* - */ 172 [0x2e] = 13, /* = */ 173 [0x2f] = 26, /* [ */ 174 [0x30] = 27, /* ] */ 175 [0x31] = 43, /* \ */ 176 177 [0x33] = 39, /* ; */ 178 [0x34] = 40, /* ' */ 179 [0x35] = 41, /* ` */ 180 [0x36] = 51, /* , */ 181 [0x37] = 52, /* . */ 182 [0x38] = 53, /* / */ 183 184 [0x3a] = 59, /* F1 */ 185 [0x3b] = 60, /* F2 */ 186 [0x3c] = 61, /* F3 */ 187 [0x3d] = 62, /* F4 */ 188 [0x3e] = 63, /* F5 */ 189 [0x3f] = 64, /* F6 */ 190 [0x40] = 65, /* F7 */ 191 [0x41] = 66, /* F8 */ 192 193 [0x42] = 68, /* "OK" -> F10 */ 194 [0x43] = 87, /* "Cancel" -> F11 */ 195 196 [0x4c] = 211, /* Del */ 197 198 [0x4f] = 205, /* Right */ 199 [0x50] = 203, /* Left */ 200 [0x51] = 208, /* Down */ 201 [0x52] = 200, /* Up */ 202 203 [0x53] = 67, /* "task switch" -> F9 */ 204 205 [0x65] = 221, /* windows */ 206 [0x66] = 88, /* "keyboard" -> F12 */ 207 208 [0x74] = 42, /* Shift (left) */ 209 [0x75] = 54, /* Shift (right) */ 210 [0x76] = 56, /* Alt (left) */ 211 [0x77] = 184, /* Fn -> AltGr == Mode Switch */ 212 [0x78] = 29, /* Control (left) */ 213 }; 214 215 216 /* 217 * Pseudo-device initialization routine called from main(). 218 */ 219 void 220 hpf1275aattach(int n) 221 { 222 int error; 223 224 error = ttyldisc_attach(&hpf1275a_disc); 225 if (error) { 226 printf("%s: unable to register line discipline, error = %d\n", 227 hpf1275a_cd.cd_name, error); 228 return; 229 } 230 231 error = config_cfattach_attach(hpf1275a_cd.cd_name, &hpf1275a_ca); 232 if (error) { 233 printf("%s: unable to register cfattach, error = %d\n", 234 hpf1275a_cd.cd_name, error); 235 config_cfdriver_detach(&hpf1275a_cd); 236 (void) ttyldisc_detach(&hpf1275a_disc); 237 } 238 } 239 240 241 /* 242 * Autoconf match routine. 243 * 244 * XXX: unused: config_attach_pseudo(9) does not call ca_match. 245 */ 246 static int 247 hpf1275a_match(struct device *self, struct cfdata *cfdata, void *arg) 248 { 249 250 /* pseudo-device; always present */ 251 return (1); 252 } 253 254 255 /* 256 * Autoconf attach routine. Called by config_attach_pseudo(9) when we 257 * open the line discipline. 258 */ 259 static void 260 hpf1275a_attach(struct device *parent, struct device *self, void *aux) 261 { 262 struct hpf1275a_softc *sc = device_private(self); 263 struct wskbddev_attach_args wska; 264 265 wska.console = 0; 266 wska.keymap = &hpf1275a_wskbd_keymapdata; 267 wska.accessops = &hpf1275a_wskbd_accessops; 268 wska.accesscookie = sc; 269 270 sc->sc_enabled = 0; 271 #ifdef WSDISPLAY_COMPAT_RAWKBD 272 sc->sc_rawkbd = 0; 273 #endif 274 sc->sc_wskbd = config_found(self, &wska, wskbddevprint); 275 } 276 277 278 /* 279 * Autoconf detach routine. Called when we close the line discipline. 280 */ 281 static int 282 hpf1275a_detach(struct device *self, int flags) 283 { 284 struct hpf1275a_softc *sc = device_private(self); 285 int error; 286 287 if (sc->sc_wskbd == NULL) 288 return (0); 289 290 error = config_detach(sc->sc_wskbd, 0); 291 292 return (error); 293 } 294 295 296 /* 297 * Line discipline open routine. 298 */ 299 int 300 hpf1275a_open(dev_t dev, struct tty *tp) 301 { 302 static struct cfdata hpf1275a_cfdata = { 303 .cf_name = "hpf1275a", 304 .cf_atname = "hpf1275a", 305 .cf_unit = DVUNIT_ANY, 306 .cf_fstate = FSTATE_STAR, 307 }; 308 struct proc *p = curproc; /* XXX */ 309 struct hpf1275a_softc *sc; 310 int error, s; 311 312 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, 313 &p->p_acflag)) != 0) 314 return (error); 315 316 s = spltty(); 317 318 sc = (struct hpf1275a_softc *) config_attach_pseudo(&hpf1275a_cfdata); 319 if (sc == NULL) { 320 splx(s); 321 return (EIO); 322 } 323 324 tp->t_sc = sc; 325 sc->sc_tp = tp; 326 327 splx(s); 328 return (0); 329 } 330 331 332 /* 333 * Line discipline close routine. 334 */ 335 int 336 hpf1275a_close(struct tty *tp, int flag) 337 { 338 struct hpf1275a_softc *sc = (struct hpf1275a_softc *)tp->t_sc; 339 int s; 340 341 s = spltty(); 342 ttyflush(tp, FREAD | FWRITE); 343 ttyldisc_release(tp->t_linesw); 344 tp->t_linesw = ttyldisc_default(); 345 if (sc != NULL) { 346 tp->t_sc = NULL; 347 if (sc->sc_tp == tp) 348 config_detach(&sc->sc_dev, 0); 349 } 350 splx(s); 351 return (0); 352 } 353 354 355 /* 356 * Feed input from the keyboard to wskbd(4). 357 */ 358 int 359 hpf1275a_input(int c, struct tty *tp) 360 { 361 struct hpf1275a_softc *sc = (struct hpf1275a_softc *)tp->t_sc; 362 int code; 363 u_int type; 364 int xtscan; 365 366 if (!sc->sc_enabled) 367 return (0); 368 369 if (c & TTY_ERRORMASK) 370 return (0); /* TODO? */ 371 372 code = c & TTY_CHARMASK; 373 if (code & 0x80) { 374 type = WSCONS_EVENT_KEY_UP; 375 code &= ~0x80; 376 } else 377 type = WSCONS_EVENT_KEY_DOWN; 378 379 xtscan = hpf1275a_to_xtscan[code]; 380 if (xtscan == 0) { 381 printf("%s: unknown code 0x%x\n", sc->sc_dev.dv_xname, code); 382 return (0); 383 } 384 385 KASSERT(sc->sc_wskbd != NULL); 386 387 #ifdef WSDISPLAY_COMPAT_RAWKBD 388 if (sc->sc_rawkbd) { 389 u_char data[16]; 390 int n; 391 392 n = pckbd_encode(type, xtscan, data); 393 wskbd_rawinput(sc->sc_wskbd, data, n); 394 } else 395 #endif 396 wskbd_input(sc->sc_wskbd, type, xtscan); 397 398 return (0); 399 } 400 401 402 static int 403 hpf1275a_wskbd_enable(void *self, int on) 404 { 405 struct hpf1275a_softc *sc = self; 406 407 sc->sc_enabled = on; 408 return (0); 409 } 410 411 412 /* ARGSUSED */ 413 static void 414 hpf1275a_wskbd_set_leds(void *self, int leds) 415 { 416 417 /* this keyboard has no leds; nothing to do */ 418 return; 419 } 420 421 422 static int 423 hpf1275a_wskbd_ioctl(void *self, u_long cmd, caddr_t data, int flag, 424 struct lwp *l) 425 { 426 #ifdef WSDISPLAY_COMPAT_RAWKBD 427 struct hpf1275a_softc *sc = self; 428 #endif 429 430 switch (cmd) { 431 case WSKBDIO_GTYPE: 432 *(int *)data = WSKBD_TYPE_HPC_KBD; /* may be use new type? */ 433 return (0); 434 435 case WSKBDIO_GETLEDS: 436 *(int *)data = 0; /* this keyboard has no leds */ 437 return (0); 438 439 #ifdef WSDISPLAY_COMPAT_RAWKBD 440 case WSKBDIO_SETMODE: 441 sc->sc_rawkbd = (*(int *)data == WSKBD_RAW); 442 return (0); 443 #endif 444 445 default: 446 return (EPASSTHROUGH); 447 } 448 } 449