1 /* $NetBSD: lunaws.c,v 1.19 2009/03/18 17:06:45 cegger Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Tohru Nishimura. 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 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 33 34 __KERNEL_RCSID(0, "$NetBSD: lunaws.c,v 1.19 2009/03/18 17:06:45 cegger Exp $"); 35 36 #include "wsmouse.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/conf.h> 41 #include <sys/device.h> 42 43 #include <dev/wscons/wsconsio.h> 44 #include <dev/wscons/wskbdvar.h> 45 #include <dev/wscons/wsksymdef.h> 46 #include <dev/wscons/wsksymvar.h> 47 #include <dev/wscons/wsmousevar.h> 48 49 #include <luna68k/dev/sioreg.h> 50 #include <luna68k/dev/siovar.h> 51 52 static const u_int8_t ch1_regs[6] = { 53 WR0_RSTINT, /* Reset E/S Interrupt */ 54 WR1_RXALLS, /* Rx per char, No Tx */ 55 0, /* */ 56 WR3_RX8BIT | WR3_RXENBL, /* Rx */ 57 WR4_BAUD96 | WR4_STOP1 | WR4_NPARITY, /* Tx/Rx */ 58 WR5_TX8BIT | WR5_TXENBL, /* Tx */ 59 }; 60 61 struct ws_softc { 62 struct device sc_dv; 63 struct sioreg *sc_ctl; 64 u_int8_t sc_wr[6]; 65 struct device *sc_wskbddev; 66 #if NWSMOUSE > 0 67 struct device *sc_wsmousedev; 68 int sc_msreport; 69 int buttons, dx, dy; 70 #endif 71 }; 72 73 static void omkbd_input(void *, int); 74 static int omkbd_decode(void *, int, u_int *, int *); 75 static int omkbd_enable(void *, int); 76 static void omkbd_set_leds(void *, int); 77 static int omkbd_ioctl(void *, u_long, void *, int, struct lwp *); 78 79 struct wscons_keydesc omkbd_keydesctab[]; 80 81 static const struct wskbd_mapdata omkbd_keymapdata = { 82 omkbd_keydesctab, 83 KB_JP, 84 }; 85 static const struct wskbd_accessops omkbd_accessops = { 86 omkbd_enable, 87 omkbd_set_leds, 88 omkbd_ioctl, 89 }; 90 91 void ws_cnattach(void); 92 static void ws_cngetc(void *, u_int *, int *); 93 static void ws_cnpollc(void *, int); 94 static const struct wskbd_consops ws_consops = { 95 ws_cngetc, 96 ws_cnpollc, 97 }; 98 99 #if NWSMOUSE > 0 100 static int omms_enable(void *); 101 static int omms_ioctl(void *, u_long, void *, int, struct lwp *); 102 static void omms_disable(void *); 103 104 static const struct wsmouse_accessops omms_accessops = { 105 omms_enable, 106 omms_ioctl, 107 omms_disable, 108 }; 109 #endif 110 111 static void wsintr(int); 112 113 static int wsmatch(struct device *, struct cfdata *, void *); 114 static void wsattach(struct device *, struct device *, void *); 115 116 CFATTACH_DECL(ws, sizeof(struct ws_softc), 117 wsmatch, wsattach, NULL, NULL); 118 extern struct cfdriver ws_cd; 119 120 extern int syscngetc(dev_t); 121 extern void syscnputc(dev_t, int); 122 123 static int 124 wsmatch(struct device *parent, struct cfdata *match, void *aux) 125 { 126 struct sio_attach_args *args = aux; 127 128 if (args->channel != 1) 129 return 0; 130 return 1; 131 } 132 133 static void 134 wsattach(struct device *parent, struct device *self, void *aux) 135 { 136 struct ws_softc *sc = (struct ws_softc *)self; 137 struct sio_softc *scp = (struct sio_softc *)parent; 138 struct sio_attach_args *args = aux; 139 struct wskbddev_attach_args a; 140 141 sc->sc_ctl = (struct sioreg *)scp->scp_ctl + 1; 142 memcpy( sc->sc_wr, ch1_regs, sizeof(ch1_regs)); 143 scp->scp_intr[1] = wsintr; 144 145 setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]); 146 setsioreg(sc->sc_ctl, WR4, sc->sc_wr[WR4]); 147 setsioreg(sc->sc_ctl, WR3, sc->sc_wr[WR3]); 148 setsioreg(sc->sc_ctl, WR5, sc->sc_wr[WR5]); 149 setsioreg(sc->sc_ctl, WR0, sc->sc_wr[WR0]); 150 setsioreg(sc->sc_ctl, WR1, sc->sc_wr[WR1]); 151 152 syscnputc((dev_t)1, 0x20); /* keep quiet mouse */ 153 154 printf("\n"); 155 156 a.console = (args->hwflags == 1); 157 a.keymap = &omkbd_keymapdata; 158 a.accessops = &omkbd_accessops; 159 a.accesscookie = (void *)sc; 160 sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, 161 wskbddevprint); 162 163 #if NWSMOUSE > 0 164 { 165 struct wsmousedev_attach_args b; 166 b.accessops = &omms_accessops; 167 b.accesscookie = (void *)sc; 168 sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &b, 169 wsmousedevprint); 170 sc->sc_msreport = 0; 171 } 172 #endif 173 } 174 175 /*ARGSUSED*/ 176 static void 177 wsintr(int chan) 178 { 179 struct ws_softc *sc = device_lookup_private(&ws_cd, 0); 180 struct sioreg *sio = sc->sc_ctl; 181 u_int code; 182 int rr; 183 184 rr = getsiocsr(sio); 185 if (rr & RR_RXRDY) { 186 do { 187 code = sio->sio_data; 188 if (rr & (RR_FRAMING | RR_OVERRUN | RR_PARITY)) { 189 sio->sio_cmd = WR0_ERRRST; 190 continue; 191 } 192 #if NWSMOUSE > 0 193 /* 194 * if (code >= 0x80 && code <= 0x87), then 195 * it's the first byte of 3 byte long mouse report 196 * code[0] & 07 -> LMR button condition 197 * code[1], [2] -> x,y delta 198 * otherwise, key press or release event. 199 */ 200 if (sc->sc_msreport == 0) { 201 if (code < 0x80 || code > 0x87) { 202 omkbd_input(sc, code); 203 continue; 204 } 205 code = (code & 07) ^ 07; 206 /* LMR->RML: wsevent counts 0 for leftmost */ 207 sc->buttons = (code & 02); 208 if (code & 01) 209 sc->buttons |= 04; 210 if (code & 04) 211 sc->buttons |= 01; 212 sc->sc_msreport = 1; 213 } 214 else if (sc->sc_msreport == 1) { 215 sc->dx = (signed char)code; 216 sc->sc_msreport = 2; 217 } 218 else if (sc->sc_msreport == 2) { 219 sc->dy = (signed char)code; 220 wsmouse_input(sc->sc_wsmousedev, 221 sc->buttons, 222 sc->dx, sc->dy, 0, 0, 223 WSMOUSE_INPUT_DELTA); 224 225 sc->sc_msreport = 0; 226 } 227 #else 228 omkbd_input(sc, code); 229 #endif 230 } while ((rr = getsiocsr(sio)) & RR_RXRDY); 231 } 232 if (rr && RR_TXRDY) 233 sio->sio_cmd = WR0_RSTPEND; 234 /* not capable of transmit, yet */ 235 } 236 237 static void 238 omkbd_input(void *v, int data) 239 { 240 struct ws_softc *sc = v; 241 u_int type; 242 int key; 243 244 if (omkbd_decode(v, data, &type, &key)) 245 wskbd_input(sc->sc_wskbddev, type, key); 246 } 247 248 static int 249 omkbd_decode(void *v, int datain, u_int *type, int *dataout) 250 { 251 *type = (datain & 0x80) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 252 *dataout = datain & 0x7f; 253 return 1; 254 } 255 256 #define KC(n) KS_KEYCODE(n) 257 258 static const keysym_t omkbd_keydesc_1[] = { 259 /* pos command normal shifted */ 260 KC(0x9), KS_Tab, 261 KC(0xa), KS_Control_L, 262 KC(0xb), KS_Mode_switch, /* Kana */ 263 KC(0xc), KS_Shift_R, 264 KC(0xd), KS_Shift_L, 265 KC(0xe), KS_Caps_Lock, 266 KC(0xf), KS_Meta_L, /* Zenmen */ 267 KC(0x10), KS_Escape, 268 KC(0x11), KS_BackSpace, 269 KC(0x12), KS_Return, 270 KC(0x14), KS_space, 271 KC(0x15), KS_Delete, 272 KC(0x16), KS_Alt_L, /* Henkan */ 273 KC(0x17), KS_Alt_R, /* Kakutei */ 274 KC(0x18), KS_f11, /* Shokyo */ 275 KC(0x19), KS_f12, /* Yobidashi */ 276 KC(0x1a), KS_f13, /* Bunsetsu L */ 277 KC(0x1b), KS_f14, /* Bunsetsu R */ 278 KC(0x1c), KS_KP_Up, 279 KC(0x1d), KS_KP_Left, 280 KC(0x1e), KS_KP_Right, 281 KC(0x1f), KS_KP_Down, 282 /* KC(0x20), KS_f11, */ 283 /* KC(0x21), KS_f12, */ 284 KC(0x22), KS_1, KS_exclam, 285 KC(0x23), KS_2, KS_quotedbl, 286 KC(0x24), KS_3, KS_numbersign, 287 KC(0x25), KS_4, KS_dollar, 288 KC(0x26), KS_5, KS_percent, 289 KC(0x27), KS_6, KS_ampersand, 290 KC(0x28), KS_7, KS_apostrophe, 291 KC(0x29), KS_8, KS_parenleft, 292 KC(0x2a), KS_9, KS_parenright, 293 KC(0x2b), KS_0, 294 KC(0x2c), KS_minus, KS_equal, 295 KC(0x2d), KS_asciicircum, KS_asciitilde, 296 KC(0x2e), KS_backslash, KS_bar, 297 /* KC(0x30), KS_f13, */ 298 /* KC(0x31), KS_f14, */ 299 KC(0x32), KS_q, 300 KC(0x33), KS_w, 301 KC(0x34), KS_e, 302 KC(0x35), KS_r, 303 KC(0x36), KS_t, 304 KC(0x37), KS_y, 305 KC(0x38), KS_u, 306 KC(0x39), KS_i, 307 KC(0x3a), KS_o, 308 KC(0x3b), KS_p, 309 KC(0x3c), KS_at, KS_grave, 310 KC(0x3d), KS_bracketleft, KS_braceleft, 311 KC(0x42), KS_a, 312 KC(0x43), KS_s, 313 KC(0x44), KS_d, 314 KC(0x45), KS_f, 315 KC(0x46), KS_g, 316 KC(0x47), KS_h, 317 KC(0x48), KS_j, 318 KC(0x49), KS_k, 319 KC(0x4a), KS_l, 320 KC(0x4b), KS_semicolon, KS_plus, 321 KC(0x4c), KS_colon, KS_asterisk, 322 KC(0x4d), KS_bracketright, KS_braceright, 323 KC(0x52), KS_z, 324 KC(0x53), KS_x, 325 KC(0x54), KS_c, 326 KC(0x55), KS_v, 327 KC(0x56), KS_b, 328 KC(0x57), KS_n, 329 KC(0x58), KS_m, 330 KC(0x59), KS_comma, KS_less, 331 KC(0x5a), KS_period, KS_greater, 332 KC(0x5b), KS_slash, KS_question, 333 KC(0x5c), KS_underscore, 334 KC(0x60), KS_KP_Delete, 335 KC(0x61), KS_KP_Add, 336 KC(0x62), KS_KP_Subtract, 337 KC(0x63), KS_KP_7, 338 KC(0x64), KS_KP_8, 339 KC(0x65), KS_KP_9, 340 KC(0x66), KS_KP_4, 341 KC(0x67), KS_KP_5, 342 KC(0x68), KS_KP_6, 343 KC(0x69), KS_KP_1, 344 KC(0x6a), KS_KP_2, 345 KC(0x6b), KS_KP_3, 346 KC(0x6c), KS_KP_0, 347 KC(0x6d), KS_KP_Decimal, 348 KC(0x6e), KS_KP_Enter, 349 KC(0x72), KS_f1, 350 KC(0x73), KS_f2, 351 KC(0x74), KS_f3, 352 KC(0x75), KS_f4, 353 KC(0x76), KS_f5, 354 KC(0x77), KS_f6, 355 KC(0x78), KS_f7, 356 KC(0x79), KS_f8, 357 KC(0x7a), KS_f9, 358 KC(0x7b), KS_f10, 359 KC(0x7c), KS_KP_Multiply, 360 KC(0x7d), KS_KP_Divide, 361 KC(0x7e), KS_KP_Equal, 362 KC(0x7f), KS_KP_Separator, 363 }; 364 365 #define SIZE(map) (sizeof(map)/sizeof(keysym_t)) 366 367 struct wscons_keydesc omkbd_keydesctab[] = { 368 { KB_JP, 0, SIZE(omkbd_keydesc_1), omkbd_keydesc_1, }, 369 { 0, 0, 0, 0 }, 370 }; 371 372 static void 373 ws_cngetc(void *v, u_int *type, int *data) 374 { 375 int code; 376 377 do { 378 code = syscngetc((dev_t)1); 379 } while (!omkbd_decode(v, code, type, data)); 380 } 381 382 static void 383 ws_cnpollc(void *v, int on) 384 { 385 } 386 387 /* EXPORT */ void 388 ws_cnattach(void) 389 { 390 static int voidfill; 391 392 /* XXX need CH.B initialization XXX */ 393 394 wskbd_cnattach(&ws_consops, &voidfill, &omkbd_keymapdata); 395 } 396 397 static int 398 omkbd_enable(void *v, int on) 399 { 400 return 0; 401 } 402 403 static void 404 omkbd_set_leds(void *v, int leds) 405 { 406 #if 0 407 syscnputc((dev_t)1, 0x10); /* kana LED on */ 408 syscnputc((dev_t)1, 0x00); /* kana LED off */ 409 syscnputc((dev_t)1, 0x11); /* caps LED on */ 410 syscnputc((dev_t)1, 0x01); /* caps LED off */ 411 #endif 412 } 413 414 static int 415 omkbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 416 { 417 switch (cmd) { 418 case WSKBDIO_GTYPE: 419 *(int *)data = 0x19991005 /* XXX */; 420 return 0; 421 case WSKBDIO_SETLEDS: 422 case WSKBDIO_GETLEDS: 423 case WSKBDIO_COMPLEXBELL: /* XXX capable of complex bell */ 424 return 0; 425 } 426 return EPASSTHROUGH; 427 } 428 429 #if NWSMOUSE > 0 430 431 static int 432 omms_enable(void *v) 433 { 434 struct ws_softc *sc = v; 435 436 syscnputc((dev_t)1, 0x60); /* enable 3 byte long mouse reporting */ 437 sc->sc_msreport = 0; 438 return 0; 439 } 440 441 /*ARGUSED*/ 442 static int 443 omms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l) 444 { 445 if (cmd == WSMOUSEIO_GTYPE) { 446 *(u_int *)data = 0x19991005; /* XXX */ 447 return 0; 448 } 449 return EPASSTHROUGH; 450 } 451 452 static void 453 omms_disable(void *v) 454 { 455 struct ws_softc *sc = v; 456 457 syscnputc((dev_t)1, 0x20); /* quiet mouse */ 458 sc->sc_msreport = 0; 459 } 460 #endif 461