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