1 /* $NetBSD: aed.c,v 1.31 2020/12/19 21:54:42 thorpej Exp $ */ 2 3 /* 4 * Copyright (C) 1994 Bradley A. Grantham 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __KERNEL_RCSID(0, "$NetBSD: aed.c,v 1.31 2020/12/19 21:54:42 thorpej Exp $"); 30 31 #include <sys/param.h> 32 #include <sys/device.h> 33 #include <sys/fcntl.h> 34 #include <sys/poll.h> 35 #include <sys/select.h> 36 #include <sys/proc.h> 37 #include <sys/signalvar.h> 38 #include <sys/systm.h> 39 #include <sys/conf.h> 40 41 #include <machine/autoconf.h> 42 #include <machine/cpu.h> 43 #include <machine/keyboard.h> 44 45 #include <macppc/dev/adbvar.h> 46 #include <macppc/dev/aedvar.h> 47 #include <macppc/dev/akbdvar.h> 48 49 #define spladb splhigh 50 51 /* 52 * Function declarations. 53 */ 54 static int aedmatch(device_t, cfdata_t, void *); 55 static void aedattach(device_t, device_t, void *); 56 static void aed_emulate_mouse(adb_event_t *event); 57 static void aed_kbdrpt(void *kstate); 58 static void aed_dokeyupdown(adb_event_t *event); 59 static void aed_handoff(adb_event_t *event); 60 static void aed_enqevent(adb_event_t *event); 61 62 /* 63 * Global variables. 64 */ 65 extern int adb_polling; /* Are we polling? (Debugger mode) */ 66 67 /* 68 * Local variables. 69 */ 70 static struct aed_softc *aed_sc = NULL; 71 static int aed_options = 0; /* | AED_MSEMUL; */ 72 73 /* Driver definition */ 74 CFATTACH_DECL_NEW(aed, sizeof(struct aed_softc), 75 aedmatch, aedattach, NULL, NULL); 76 77 extern struct cfdriver aed_cd; 78 79 dev_type_open(aedopen); 80 dev_type_close(aedclose); 81 dev_type_read(aedread); 82 dev_type_ioctl(aedioctl); 83 dev_type_poll(aedpoll); 84 dev_type_kqfilter(aedkqfilter); 85 86 const struct cdevsw aed_cdevsw = { 87 .d_open = aedopen, 88 .d_close = aedclose, 89 .d_read = aedread, 90 .d_write = nullwrite, 91 .d_ioctl = aedioctl, 92 .d_stop = nostop, 93 .d_tty = notty, 94 .d_poll = aedpoll, 95 .d_mmap = nommap, 96 .d_kqfilter = aedkqfilter, 97 .d_discard = nodiscard, 98 .d_flag = 0 99 }; 100 101 static int 102 aedmatch(device_t parent, cfdata_t cf, void *aux) 103 { 104 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux; 105 static int aed_matched = 0; 106 107 /* Allow only one instance. */ 108 if ((aa_args->origaddr == 0) && (!aed_matched)) { 109 aed_matched = 1; 110 return (1); 111 } else 112 return (0); 113 } 114 115 static void 116 aedattach(device_t parent, device_t self, void *aux) 117 { 118 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux; 119 struct aed_softc *sc = device_private(self); 120 121 callout_init(&sc->sc_repeat_ch, 0); 122 selinit(&sc->sc_selinfo); 123 124 sc->origaddr = aa_args->origaddr; 125 sc->adbaddr = aa_args->adbaddr; 126 sc->handler_id = aa_args->handler_id; 127 128 sc->sc_evq_tail = 0; 129 sc->sc_evq_len = 0; 130 131 sc->sc_rptdelay = 20; 132 sc->sc_rptinterval = 6; 133 sc->sc_repeating = -1; /* not repeating */ 134 135 /* Pull in the options flags. */ 136 sc->sc_options = (device_cfdata(self)->cf_flags | aed_options); 137 138 sc->sc_ioproc = NULL; 139 140 sc->sc_buttons = 0; 141 142 sc->sc_open = 0; 143 144 aed_sc = sc; 145 146 printf("ADB Event device\n"); 147 148 return; 149 } 150 151 /* 152 * Given a keyboard ADB event, record the keycode and call the key 153 * repeat handler, optionally passing the event through the mouse 154 * button emulation handler first. Pass mouse events directly to 155 * the handoff function. 156 */ 157 void 158 aed_input(adb_event_t *event) 159 { 160 adb_event_t new_event = *event; 161 162 switch (event->def_addr) { 163 case ADBADDR_KBD: 164 if (aed_sc->sc_options & AED_MSEMUL) 165 aed_emulate_mouse(&new_event); 166 else 167 aed_dokeyupdown(&new_event); 168 break; 169 case ADBADDR_MS: 170 new_event.u.m.buttons |= aed_sc->sc_buttons; 171 aed_handoff(&new_event); 172 break; 173 default: /* God only knows. */ 174 #ifdef DIAGNOSTIC 175 panic("aed: received event from unsupported device!"); 176 #endif 177 break; 178 } 179 180 } 181 182 /* 183 * Handles mouse button emulation via the keyboard. If the emulation 184 * modifier key is down, left and right arrows will generate 2nd and 185 * 3rd mouse button events while the 1, 2, and 3 keys will generate 186 * the corresponding mouse button event. 187 */ 188 static void 189 aed_emulate_mouse(adb_event_t *event) 190 { 191 static int emulmodkey_down = 0; 192 adb_event_t new_event; 193 194 if (event->u.k.key == ADBK_KEYDOWN(ADBK_OPTION)) { 195 emulmodkey_down = 1; 196 } else if (event->u.k.key == ADBK_KEYUP(ADBK_OPTION)) { 197 /* key up */ 198 emulmodkey_down = 0; 199 if (aed_sc->sc_buttons & 0xfe) { 200 aed_sc->sc_buttons &= 1; 201 new_event.def_addr = ADBADDR_MS; 202 new_event.u.m.buttons = aed_sc->sc_buttons; 203 new_event.u.m.dx = new_event.u.m.dy = 0; 204 microtime(&new_event.timestamp); 205 aed_handoff(&new_event); 206 } 207 } else if (emulmodkey_down) { 208 switch(event->u.k.key) { 209 #ifdef ALTXBUTTONS 210 case ADBK_KEYDOWN(ADBK_1): 211 aed_sc->sc_buttons |= 1; /* left down */ 212 new_event.def_addr = ADBADDR_MS; 213 new_event.u.m.buttons = aed_sc->sc_buttons; 214 new_event.u.m.dx = new_event.u.m.dy = 0; 215 microtime(&new_event.timestamp); 216 aed_handoff(&new_event); 217 break; 218 case ADBK_KEYUP(ADBK_1): 219 aed_sc->sc_buttons &= ~1; /* left up */ 220 new_event.def_addr = ADBADDR_MS; 221 new_event.u.m.buttons = aed_sc->sc_buttons; 222 new_event.u.m.dx = new_event.u.m.dy = 0; 223 microtime(&new_event.timestamp); 224 aed_handoff(&new_event); 225 break; 226 #endif 227 case ADBK_KEYDOWN(ADBK_LEFT): 228 #ifdef ALTXBUTTONS 229 case ADBK_KEYDOWN(ADBK_2): 230 #endif 231 aed_sc->sc_buttons |= 2; /* middle down */ 232 new_event.def_addr = ADBADDR_MS; 233 new_event.u.m.buttons = aed_sc->sc_buttons; 234 new_event.u.m.dx = new_event.u.m.dy = 0; 235 microtime(&new_event.timestamp); 236 aed_handoff(&new_event); 237 break; 238 case ADBK_KEYUP(ADBK_LEFT): 239 #ifdef ALTXBUTTONS 240 case ADBK_KEYUP(ADBK_2): 241 #endif 242 aed_sc->sc_buttons &= ~2; /* middle up */ 243 new_event.def_addr = ADBADDR_MS; 244 new_event.u.m.buttons = aed_sc->sc_buttons; 245 new_event.u.m.dx = new_event.u.m.dy = 0; 246 microtime(&new_event.timestamp); 247 aed_handoff(&new_event); 248 break; 249 case ADBK_KEYDOWN(ADBK_RIGHT): 250 #ifdef ALTXBUTTONS 251 case ADBK_KEYDOWN(ADBK_3): 252 #endif 253 aed_sc->sc_buttons |= 4; /* right down */ 254 new_event.def_addr = ADBADDR_MS; 255 new_event.u.m.buttons = aed_sc->sc_buttons; 256 new_event.u.m.dx = new_event.u.m.dy = 0; 257 microtime(&new_event.timestamp); 258 aed_handoff(&new_event); 259 break; 260 case ADBK_KEYUP(ADBK_RIGHT): 261 #ifdef ALTXBUTTONS 262 case ADBK_KEYUP(ADBK_3): 263 #endif 264 aed_sc->sc_buttons &= ~4; /* right up */ 265 new_event.def_addr = ADBADDR_MS; 266 new_event.u.m.buttons = aed_sc->sc_buttons; 267 new_event.u.m.dx = new_event.u.m.dy = 0; 268 microtime(&new_event.timestamp); 269 aed_handoff(&new_event); 270 break; 271 case ADBK_KEYUP(ADBK_SHIFT): 272 case ADBK_KEYDOWN(ADBK_SHIFT): 273 case ADBK_KEYUP(ADBK_CONTROL): 274 case ADBK_KEYDOWN(ADBK_CONTROL): 275 case ADBK_KEYUP(ADBK_FLOWER): 276 case ADBK_KEYDOWN(ADBK_FLOWER): 277 /* ctrl, shift, cmd */ 278 aed_dokeyupdown(event); 279 break; 280 default: 281 if (event->u.k.key & 0x80) 282 /* ignore keyup */ 283 break; 284 285 /* key down */ 286 new_event = *event; 287 288 /* send option-down */ 289 new_event.u.k.key = ADBK_KEYDOWN(ADBK_OPTION); 290 new_event.bytes[0] = new_event.u.k.key; 291 microtime(&new_event.timestamp); 292 aed_dokeyupdown(&new_event); 293 294 /* send key-down */ 295 new_event.u.k.key = event->bytes[0]; 296 new_event.bytes[0] = new_event.u.k.key; 297 microtime(&new_event.timestamp); 298 aed_dokeyupdown(&new_event); 299 300 /* send key-up */ 301 new_event.u.k.key = 302 ADBK_KEYUP(ADBK_KEYVAL(event->bytes[0])); 303 microtime(&new_event.timestamp); 304 new_event.bytes[0] = new_event.u.k.key; 305 aed_dokeyupdown(&new_event); 306 307 /* send option-up */ 308 new_event.u.k.key = ADBK_KEYUP(ADBK_OPTION); 309 new_event.bytes[0] = new_event.u.k.key; 310 microtime(&new_event.timestamp); 311 aed_dokeyupdown(&new_event); 312 break; 313 } 314 } else { 315 aed_dokeyupdown(event); 316 } 317 } 318 319 /* 320 * Keyboard autorepeat timeout function. Sends key up/down events 321 * for the repeating key and schedules the next call at sc_rptinterval 322 * ticks in the future. 323 */ 324 static void 325 aed_kbdrpt(void *kstate) 326 { 327 struct aed_softc *sc = (struct aed_softc *)kstate; 328 329 sc->sc_rptevent.bytes[0] |= 0x80; 330 microtime(&sc->sc_rptevent.timestamp); 331 aed_handoff(&sc->sc_rptevent); /* do key up */ 332 333 sc->sc_rptevent.bytes[0] &= 0x7f; 334 microtime(&sc->sc_rptevent.timestamp); 335 aed_handoff(&sc->sc_rptevent); /* do key down */ 336 337 if (sc->sc_repeating == sc->sc_rptevent.u.k.key) { 338 callout_reset(&sc->sc_repeat_ch, sc->sc_rptinterval, 339 aed_kbdrpt, kstate); 340 } 341 } 342 343 344 /* 345 * Cancels the currently repeating key event if there is one, schedules 346 * a new repeating key event if needed, and hands the event off to the 347 * appropriate subsystem. 348 */ 349 static void 350 aed_dokeyupdown(adb_event_t *event) 351 { 352 int kbd_key; 353 354 kbd_key = ADBK_KEYVAL(event->u.k.key); 355 if (ADBK_PRESS(event->u.k.key) && keyboard[kbd_key][0] != 0) { 356 /* ignore shift & control */ 357 if (aed_sc->sc_repeating != -1) { 358 callout_stop(&aed_sc->sc_repeat_ch); 359 } 360 aed_sc->sc_rptevent = *event; 361 aed_sc->sc_repeating = kbd_key; 362 callout_reset(&aed_sc->sc_repeat_ch, aed_sc->sc_rptdelay, 363 aed_kbdrpt, (void *)aed_sc); 364 } else { 365 if (aed_sc->sc_repeating != -1) { 366 aed_sc->sc_repeating = -1; 367 callout_stop(&aed_sc->sc_repeat_ch); 368 } 369 aed_sc->sc_rptevent = *event; 370 } 371 aed_handoff(event); 372 } 373 374 /* 375 * Place the event in the event queue if a requesting device is open 376 * and we are not polling. 377 */ 378 static void 379 aed_handoff(adb_event_t *event) 380 { 381 if (aed_sc->sc_open && !adb_polling) 382 aed_enqevent(event); 383 } 384 385 /* 386 * Place the event in the event queue and wakeup any waiting processes. 387 */ 388 static void 389 aed_enqevent(adb_event_t *event) 390 { 391 int s; 392 393 s = spladb(); 394 395 #ifdef DIAGNOSTIC 396 if (aed_sc->sc_evq_tail < 0 || aed_sc->sc_evq_tail >= AED_MAX_EVENTS) 397 panic("adb: event queue tail is out of bounds"); 398 399 if (aed_sc->sc_evq_len < 0 || aed_sc->sc_evq_len > AED_MAX_EVENTS) 400 panic("adb: event queue len is out of bounds"); 401 #endif 402 403 if (aed_sc->sc_evq_len == AED_MAX_EVENTS) { 404 splx(s); 405 return; /* Oh, well... */ 406 } 407 aed_sc->sc_evq[(aed_sc->sc_evq_len + aed_sc->sc_evq_tail) % 408 AED_MAX_EVENTS] = *event; 409 aed_sc->sc_evq_len++; 410 411 selnotify(&aed_sc->sc_selinfo, 0, 0); 412 if (aed_sc->sc_ioproc) 413 psignal(aed_sc->sc_ioproc, SIGIO); 414 415 splx(s); 416 } 417 418 int 419 aedopen(dev_t dev, int flag, int mode, struct lwp *l) 420 { 421 int unit; 422 int error = 0; 423 int s; 424 425 unit = minor(dev); 426 427 if (unit != 0) 428 return (ENXIO); 429 430 s = spladb(); 431 if (aed_sc->sc_open) { 432 splx(s); 433 return (EBUSY); 434 } 435 aed_sc->sc_evq_tail = 0; 436 aed_sc->sc_evq_len = 0; 437 aed_sc->sc_open = 1; 438 aed_sc->sc_ioproc = l->l_proc; 439 splx(s); 440 441 return (error); 442 } 443 444 445 int 446 aedclose(dev_t dev, int flag, int mode, struct lwp *l) 447 { 448 int s = spladb(); 449 450 aed_sc->sc_open = 0; 451 aed_sc->sc_ioproc = NULL; 452 splx(s); 453 454 return (0); 455 } 456 457 458 int 459 aedread(dev_t dev, struct uio *uio, int flag) 460 { 461 int s, error; 462 int willfit; 463 int total; 464 int firstmove; 465 int moremove; 466 467 if (uio->uio_resid < sizeof(adb_event_t)) 468 return (EMSGSIZE); /* close enough. */ 469 470 s = spladb(); 471 if (aed_sc->sc_evq_len == 0) { 472 splx(s); 473 return (0); 474 } 475 willfit = howmany(uio->uio_resid, sizeof(adb_event_t)); 476 total = (aed_sc->sc_evq_len < willfit) ? aed_sc->sc_evq_len : willfit; 477 478 firstmove = (aed_sc->sc_evq_tail + total > AED_MAX_EVENTS) 479 ? (AED_MAX_EVENTS - aed_sc->sc_evq_tail) : total; 480 481 error = uiomove((void *) & aed_sc->sc_evq[aed_sc->sc_evq_tail], 482 firstmove * sizeof(adb_event_t), uio); 483 if (error) { 484 splx(s); 485 return (error); 486 } 487 moremove = total - firstmove; 488 489 if (moremove > 0) { 490 error = uiomove((void *) & aed_sc->sc_evq[0], 491 moremove * sizeof(adb_event_t), uio); 492 if (error) { 493 splx(s); 494 return (error); 495 } 496 } 497 aed_sc->sc_evq_tail = (aed_sc->sc_evq_tail + total) % AED_MAX_EVENTS; 498 aed_sc->sc_evq_len -= total; 499 splx(s); 500 return (0); 501 } 502 503 int 504 aedioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 505 { 506 switch (cmd) { 507 case ADBIOCDEVSINFO: { 508 adb_devinfo_t *di; 509 ADBDataBlock adbdata; 510 int totaldevs; 511 int adbaddr; 512 int i; 513 514 di = (void *)data; 515 516 /* Initialize to no devices */ 517 for (i = 0; i < 16; i++) 518 di->dev[i].addr = -1; 519 520 totaldevs = CountADBs(); 521 for (i = 1; i <= totaldevs; i++) { 522 adbaddr = GetIndADB(&adbdata, i); 523 di->dev[adbaddr].addr = adbaddr; 524 di->dev[adbaddr].default_addr = (int)(adbdata.origADBAddr); 525 di->dev[adbaddr].handler_id = (int)(adbdata.devType); 526 } 527 528 /* Must call ADB Manager to get devices now */ 529 break; 530 } 531 532 case ADBIOCGETREPEAT:{ 533 adb_rptinfo_t *ri; 534 535 ri = (void *)data; 536 ri->delay_ticks = aed_sc->sc_rptdelay; 537 ri->interval_ticks = aed_sc->sc_rptinterval; 538 break; 539 } 540 541 case ADBIOCSETREPEAT:{ 542 adb_rptinfo_t *ri; 543 544 ri = (void *) data; 545 aed_sc->sc_rptdelay = ri->delay_ticks; 546 aed_sc->sc_rptinterval = ri->interval_ticks; 547 break; 548 } 549 550 case ADBIOCRESET: 551 /* Do nothing for now */ 552 break; 553 554 case ADBIOCLISTENCMD:{ 555 adb_listencmd_t *lc; 556 557 lc = (void *)data; 558 } 559 560 default: 561 return (EINVAL); 562 } 563 return (0); 564 } 565 566 567 int 568 aedpoll(dev_t dev, int events, struct lwp *l) 569 { 570 int s, revents; 571 572 revents = events & (POLLOUT | POLLWRNORM); 573 574 if ((events & (POLLIN | POLLRDNORM)) == 0) 575 return (revents); 576 577 s = spladb(); 578 if (aed_sc->sc_evq_len > 0) 579 revents |= events & (POLLIN | POLLRDNORM); 580 else 581 selrecord(l, &aed_sc->sc_selinfo); 582 splx(s); 583 584 return (revents); 585 } 586 587 static void 588 filt_aedrdetach(struct knote *kn) 589 { 590 int s; 591 592 s = spladb(); 593 selremove_knote(&aed_sc->sc_selinfo, kn); 594 splx(s); 595 } 596 597 static int 598 filt_aedread(struct knote *kn, long hint) 599 { 600 601 kn->kn_data = aed_sc->sc_evq_len * sizeof(adb_event_t); 602 return (kn->kn_data > 0); 603 } 604 605 static const struct filterops aedread_filtops = { 606 .f_isfd = 1, 607 .f_attach = NULL, 608 .f_detach = filt_aedrdetach, 609 .f_event = filt_aedread 610 }; 611 612 static const struct filterops aed_seltrue_filtops = { 613 .f_isfd = 1, 614 .f_attach = NULL, 615 .f_detach = filt_aedrdetach, 616 .f_event = filt_seltrue 617 }; 618 619 int 620 aedkqfilter(dev_t dev, struct knote *kn) 621 { 622 int s; 623 624 switch (kn->kn_filter) { 625 case EVFILT_READ: 626 kn->kn_fop = &aedread_filtops; 627 break; 628 629 case EVFILT_WRITE: 630 kn->kn_fop = &aed_seltrue_filtops; 631 break; 632 633 default: 634 return (1); 635 } 636 637 kn->kn_hook = NULL; 638 639 s = spladb(); 640 selrecord_knote(&aed_sc->sc_selinfo, kn); 641 splx(s); 642 643 return (0); 644 } 645