1 /* $NetBSD: kbd.c,v 1.59 2024/09/08 09:36:48 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1995 Leo Weppelman 5 * Copyright (c) 1982, 1986, 1990 The Regents of the University of California. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.59 2024/09/08 09:36:48 rillig Exp $"); 35 36 #include "mouse.h" 37 #include "ite.h" 38 #include "wskbd.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/device.h> 43 #include <sys/ioctl.h> 44 #include <sys/tty.h> 45 #include <sys/proc.h> 46 #include <sys/conf.h> 47 #include <sys/file.h> 48 #include <sys/kernel.h> 49 #include <sys/signalvar.h> 50 #include <sys/syslog.h> 51 #include <sys/rndsource.h> 52 53 #include <dev/cons.h> 54 #include <machine/cpu.h> 55 #include <machine/iomap.h> 56 #include <machine/mfp.h> 57 #include <machine/acia.h> 58 #include <atari/dev/itevar.h> 59 #include <atari/dev/event_var.h> 60 #include <atari/dev/vuid_event.h> 61 #include <atari/dev/ym2149reg.h> 62 #include <atari/dev/kbdreg.h> 63 #include <atari/dev/kbdvar.h> 64 #include <atari/dev/kbdmap.h> 65 #include <atari/dev/msvar.h> 66 67 #if NWSKBD>0 68 #include <dev/wscons/wsconsio.h> 69 #include <dev/wscons/wskbdvar.h> 70 #include <dev/wscons/wsksymdef.h> 71 #include <dev/wscons/wsksymvar.h> 72 #include <atari/dev/wskbdmap_atari.h> 73 #endif 74 75 /* 76 * The ringbuffer is the interface between the hard and soft interrupt handler. 77 * The hard interrupt runs straight from the MFP interrupt. 78 */ 79 #define KBD_RING_SIZE 256 /* Sz of input ring buffer, must be power of 2 */ 80 #define KBD_RING_MASK 255 /* Modulo mask for above */ 81 82 struct kbd_softc { 83 int sc_event_mode; /* if 1, collect events, */ 84 /* else pass to ite */ 85 struct evvar sc_events; /* event queue state */ 86 uint8_t sc_soft_cs; /* control-reg. copy */ 87 uint8_t sc_package[20]; /* XXX package being build */ 88 uint8_t sc_pkg_size; /* Size of the package */ 89 uint8_t sc_pkg_idx; /* Running pkg assembly index */ 90 uint8_t sc_pkg_type; /* Type of package */ 91 const uint8_t *sc_sendp; /* Output pointer */ 92 int sc_send_cnt; /* Chars left for output */ 93 #if NWSKBD > 0 94 device_t sc_wskbddev; /* pointer to wskbd for sending strokes */ 95 int sc_pollingmode; /* polling mode on? whatever it is... */ 96 #endif 97 void *sc_sicookie; /* softint(9) cookie */ 98 krndsource_t sc_rndsource; /* rnd(9) entropy */ 99 }; 100 101 /* WSKBD */ 102 /* 103 * If NWSKBD>0 we try to attach a wskbd device to us. What follows 104 * is definitions of callback functions and structures that are passed 105 * to wscons when initializing. 106 */ 107 108 /* 109 * Now with wscons this driver exhibits some weird behaviour. 110 * It may act both as a driver of its own and the md part of the 111 * wskbd driver. Therefore it can be accessed through /dev/kbd 112 * and /dev/wskbd0 both. 113 * 114 * The data from they keyboard may end up in at least four different 115 * places: 116 * - If this driver has been opened (/dev/kbd) and the 117 * direct mode (TIOCDIRECT) has been set, data goes to 118 * the process who opened the device. Data will transmit itself 119 * as described by the firm_event structure. 120 * - If wskbd support is compiled in and a wskbd driver has been 121 * attached then the data is sent to it. Wskbd in turn may 122 * - Send the data in the wscons_event form to a process that 123 * has opened /dev/wskbd0 124 * - Feed the data to a virtual terminal. 125 * - If an ite is present the data may be fed to it. 126 */ 127 128 uint8_t kbd_modifier; /* Modifier mask */ 129 130 static uint8_t kbd_ring[KBD_RING_SIZE]; 131 static volatile u_int kbd_rbput = 0; /* 'put' index */ 132 static u_int kbd_rbget = 0; /* 'get' index */ 133 134 static struct kbd_softc kbd_softc; /* XXX */ 135 136 /* {b,c}devsw[] function prototypes */ 137 static dev_type_open(kbdopen); 138 static dev_type_close(kbdclose); 139 static dev_type_read(kbdread); 140 static dev_type_ioctl(kbdioctl); 141 static dev_type_poll(kbdpoll); 142 static dev_type_kqfilter(kbdkqfilter); 143 144 static void kbdsoft(void *); 145 static void kbdattach(device_t, device_t, void *); 146 static int kbdmatch(device_t, cfdata_t, void *); 147 #if NITE > 0 148 static int kbd_do_modifier(uint8_t); 149 #endif 150 static int kbd_write_poll(const uint8_t *, int); 151 static void kbd_pkg_start(struct kbd_softc *, uint8_t); 152 153 CFATTACH_DECL_NEW(kbd, 0, 154 kbdmatch, kbdattach, NULL, NULL); 155 156 const struct cdevsw kbd_cdevsw = { 157 .d_open = kbdopen, 158 .d_close = kbdclose, 159 .d_read = kbdread, 160 .d_write = nowrite, 161 .d_ioctl = kbdioctl, 162 .d_stop = nostop, 163 .d_tty = notty, 164 .d_poll = kbdpoll, 165 .d_mmap = nommap, 166 .d_kqfilter = kbdkqfilter, 167 .d_discard = nodiscard, 168 .d_flag = 0 169 }; 170 171 #if NWSKBD>0 172 /* accessops */ 173 static int kbd_enable(void *, int); 174 static void kbd_set_leds(void *, int); 175 static int kbd_ioctl(void *, u_long, void *, int, struct lwp *); 176 177 /* console ops */ 178 static void kbd_getc(void *, u_int *, int *); 179 static void kbd_pollc(void *, int); 180 static void kbd_bell(void *, u_int, u_int, u_int); 181 182 static struct wskbd_accessops kbd_accessops = { 183 kbd_enable, 184 kbd_set_leds, 185 kbd_ioctl 186 }; 187 188 static struct wskbd_consops kbd_consops = { 189 kbd_getc, 190 kbd_pollc, 191 kbd_bell 192 }; 193 194 /* Pointer to keymaps. */ 195 static struct wskbd_mapdata kbd_mapdata = { 196 atarikbd_keydesctab, 197 KB_US 198 }; 199 #endif /* WSKBD */ 200 201 /*ARGSUSED*/ 202 static int 203 kbdmatch(device_t parent, cfdata_t cf, void *aux) 204 { 205 206 if (!strcmp((char *)aux, "kbd")) 207 return 1; 208 return 0; 209 } 210 211 /*ARGSUSED*/ 212 static void 213 kbdattach(device_t parent, device_t self, void *aux) 214 { 215 struct kbd_softc *sc = &kbd_softc; 216 int timeout; 217 const uint8_t kbd_rst[] = { 0x80, 0x01 }; 218 const uint8_t kbd_icmd[] = { 0x12, 0x15 }; 219 220 /* 221 * Disable keyboard interrupts from MFP 222 */ 223 MFP->mf_ierb &= ~IB_AINT; 224 225 /* 226 * Reset ACIA and initialize to: 227 * divide by 16, 8 data, 1 stop, no parity, enable RX interrupts 228 */ 229 KBD->ac_cs = A_RESET; 230 delay(100); /* XXX: enough? */ 231 KBD->ac_cs = sc->sc_soft_cs = KBD_INIT | A_RXINT; 232 233 /* 234 * Clear error conditions 235 */ 236 while ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0) 237 timeout = KBD->ac_da; 238 239 /* 240 * Now send the reset string, and read+ignore its response 241 */ 242 aprint_normal("\n"); 243 if (kbd_write_poll(kbd_rst, 2) == 0) 244 aprint_error_dev(self, "error cannot reset keyboard\n"); 245 for (timeout = 1000; timeout > 0; timeout--) { 246 if ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0) { 247 timeout = KBD->ac_da; 248 timeout = 100; 249 } 250 delay(100); 251 } 252 /* 253 * Send init command: disable mice & joysticks 254 */ 255 kbd_write_poll(kbd_icmd, sizeof(kbd_icmd)); 256 257 sc->sc_sicookie = softint_establish(SOFTINT_SERIAL, kbdsoft, NULL); 258 rnd_attach_source(&sc->sc_rndsource, device_xname(self), 259 RND_TYPE_TTY, RND_FLAG_DEFAULT); 260 261 #if NWSKBD > 0 262 if (self != NULL) { 263 /* 264 * Try to attach the wskbd. 265 */ 266 struct wskbddev_attach_args waa; 267 268 /* Maybe should be done before this?... */ 269 wskbd_cnattach(&kbd_consops, NULL, &kbd_mapdata); 270 271 waa.console = 1; 272 waa.keymap = &kbd_mapdata; 273 waa.accessops = &kbd_accessops; 274 waa.accesscookie = NULL; 275 sc->sc_wskbddev = config_found(self, &waa, wskbddevprint, 276 CFARGS_NONE); 277 278 sc->sc_pollingmode = 0; 279 280 kbdenable(); 281 } 282 #endif /* WSKBD */ 283 } 284 285 void 286 kbdenable(void) 287 { 288 struct kbd_softc *sc = &kbd_softc; 289 int s; 290 291 s = spltty(); 292 293 /* 294 * Clear error conditions... 295 */ 296 while ((KBD->ac_cs & (A_IRQ | A_RXRDY)) != 0) 297 (void)KBD->ac_da; 298 /* 299 * Enable interrupts from MFP 300 */ 301 MFP->mf_iprb = (uint8_t)~IB_AINT; 302 MFP->mf_ierb |= IB_AINT; 303 MFP->mf_imrb |= IB_AINT; 304 305 sc->sc_event_mode = 0; 306 sc->sc_events.ev_io = 0; 307 sc->sc_pkg_size = 0; 308 splx(s); 309 } 310 311 static int 312 kbdopen(dev_t dev, int flags, int mode, struct lwp *l) 313 { 314 struct kbd_softc *sc = &kbd_softc; 315 316 if (sc->sc_events.ev_io) 317 return EBUSY; 318 319 sc->sc_events.ev_io = l->l_proc; 320 ev_init(&sc->sc_events); 321 return 0; 322 } 323 324 static int 325 kbdclose(dev_t dev, int flags, int mode, struct lwp *l) 326 { 327 struct kbd_softc *sc = &kbd_softc; 328 329 /* Turn off event mode, dump the queue */ 330 sc->sc_event_mode = 0; 331 ev_fini(&sc->sc_events); 332 sc->sc_events.ev_io = NULL; 333 return 0; 334 } 335 336 static int 337 kbdread(dev_t dev, struct uio *uio, int flags) 338 { 339 struct kbd_softc *sc = &kbd_softc; 340 341 return ev_read(&sc->sc_events, uio, flags); 342 } 343 344 static int 345 kbdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 346 { 347 struct kbd_softc *sc = &kbd_softc; 348 struct kbdbell *kb; 349 350 switch (cmd) { 351 case KIOCTRANS: 352 if (*(int *)data == TR_UNTRANS_EVENT) 353 return 0; 354 break; 355 356 case KIOCGTRANS: 357 /* 358 * Get translation mode 359 */ 360 *(int *)data = TR_UNTRANS_EVENT; 361 return 0; 362 363 case KIOCSDIRECT: 364 sc->sc_event_mode = *(int *)data; 365 return 0; 366 367 case KIOCRINGBELL: 368 kb = (struct kbdbell *)data; 369 if (kb) 370 kbd_bell_sparms(kb->volume, kb->pitch, 371 kb->duration); 372 kbdbell(); 373 return 0; 374 375 case FIONBIO: /* we will remove this someday (soon???) */ 376 return 0; 377 378 case FIOASYNC: 379 sc->sc_events.ev_async = *(int *)data != 0; 380 return 0; 381 382 case FIOSETOWN: 383 if (-*(int *)data != sc->sc_events.ev_io->p_pgid && 384 *(int *)data != sc->sc_events.ev_io->p_pid) 385 return EPERM; 386 return 0; 387 388 case TIOCSPGRP: 389 if (*(int *)data != sc->sc_events.ev_io->p_pgid) 390 return EPERM; 391 return 0; 392 393 default: 394 return ENOTTY; 395 } 396 397 /* 398 * We identified the ioctl, but we do not handle it. 399 */ 400 return EOPNOTSUPP; /* misuse, but what the heck */ 401 } 402 403 static int 404 kbdpoll(dev_t dev, int events, struct lwp *l) 405 { 406 struct kbd_softc *sc = &kbd_softc; 407 408 return ev_poll(&sc->sc_events, events, l); 409 } 410 411 static int 412 kbdkqfilter(dev_t dev, struct knote *kn) 413 { 414 struct kbd_softc *sc = &kbd_softc; 415 416 return ev_kqfilter(&sc->sc_events, kn); 417 } 418 419 /* 420 * Keyboard interrupt handler called straight from MFP at spl6. 421 */ 422 void 423 kbdintr(int sr) 424 /* sr: sr at time of interrupt */ 425 { 426 struct kbd_softc *sc = &kbd_softc; 427 uint8_t stat, code = 0 /* XXX gcc */; 428 uint32_t rndstat; 429 bool got_char = false; 430 431 /* 432 * There may be multiple keys available. Read them all. 433 */ 434 stat = KBD->ac_cs; 435 rndstat = stat; 436 while ((stat & (A_RXRDY | A_OE | A_PE)) != 0) { 437 got_char = true; 438 if ((KBD->ac_cs & (A_OE | A_PE)) == 0) { 439 code = KBD->ac_da; 440 kbd_ring[kbd_rbput++ & KBD_RING_MASK] = code; 441 } else { 442 /* Silently ignore errors */ 443 code = KBD->ac_da; 444 } 445 stat = KBD->ac_cs; 446 } 447 448 /* 449 * If characters are waiting for transmit, send them. 450 */ 451 if ((sc->sc_soft_cs & A_TXINT) != 0 && 452 (KBD->ac_cs & A_TXRDY) != 0) { 453 if (sc->sc_sendp != NULL) 454 KBD->ac_da = *sc->sc_sendp++; 455 if (--sc->sc_send_cnt <= 0) { 456 /* 457 * The total package has been transmitted, 458 * wakeup anyone waiting for it. 459 */ 460 KBD->ac_cs = (sc->sc_soft_cs &= ~A_TXINT); 461 sc->sc_sendp = NULL; 462 sc->sc_send_cnt = 0; 463 wakeup((void *)&sc->sc_send_cnt); 464 } 465 } 466 467 /* 468 * Activate software-level to handle possible input. 469 * Also add status and data to the rnd(9) pool. 470 */ 471 if (got_char) { 472 softint_schedule(sc->sc_sicookie); 473 rnd_add_uint32(&sc->sc_rndsource, (rndstat << 8) | code); 474 } 475 } 476 477 /* 478 * Keyboard soft interrupt handler 479 */ 480 static void 481 kbdsoft(void *junk1) 482 { 483 struct kbd_softc *sc = &kbd_softc; 484 int s; 485 uint8_t code; 486 struct firm_event *fe; 487 int put, get, n; 488 489 get = kbd_rbget; 490 491 for (;;) { 492 n = kbd_rbput; 493 if (get == n) /* We're done */ 494 break; 495 n -= get; 496 if (n > KBD_RING_SIZE) { /* Ring buffer overflow */ 497 get += n - KBD_RING_SIZE; 498 n = KBD_RING_SIZE; 499 } 500 while (--n >= 0) { 501 code = kbd_ring[get++ & KBD_RING_MASK]; 502 503 /* 504 * If collecting a package, stuff it in and 505 * continue. 506 */ 507 if (sc->sc_pkg_size && 508 (sc->sc_pkg_idx < sc->sc_pkg_size)) { 509 sc->sc_package[sc->sc_pkg_idx++] = code; 510 if (sc->sc_pkg_idx == sc->sc_pkg_size) { 511 /* 512 * Package is complete. 513 */ 514 switch (sc->sc_pkg_type) { 515 #if NMOUSE > 0 516 case KBD_AMS_PKG: 517 case KBD_RMS_PKG: 518 case KBD_JOY1_PKG: 519 mouse_soft( 520 (REL_MOUSE *)sc->sc_package, 521 sc->sc_pkg_size, 522 sc->sc_pkg_type); 523 #endif /* NMOUSE */ 524 } 525 sc->sc_pkg_size = 0; 526 } 527 continue; 528 } 529 /* 530 * If this is a package header, init pkg. handling. 531 */ 532 if (!KBD_IS_KEY(code)) { 533 kbd_pkg_start(sc, code); 534 continue; 535 } 536 #if NWSKBD > 0 537 /* 538 * If we have attached a wskbd and not in polling mode 539 * and nobody has opened us directly, then send the 540 * keystroke to the wskbd. 541 */ 542 543 if (sc->sc_pollingmode == 0 && 544 sc->sc_wskbddev != NULL && 545 sc->sc_event_mode == 0) { 546 wskbd_input(sc->sc_wskbddev, 547 KBD_RELEASED(code) ? 548 WSCONS_EVENT_KEY_UP : 549 WSCONS_EVENT_KEY_DOWN, 550 KBD_SCANCODE(code)); 551 continue; 552 } 553 #endif /* NWSKBD */ 554 #if NITE > 0 555 if (kbd_do_modifier(code) && !sc->sc_event_mode) 556 continue; 557 #endif 558 559 /* 560 * if not in event mode, deliver straight to ite to 561 * process key stroke 562 */ 563 if (!sc->sc_event_mode) { 564 /* Gets to spltty() by itself */ 565 #if NITE > 0 566 ite_filter(code, ITEFILT_TTY); 567 #endif 568 continue; 569 } 570 571 /* 572 * Keyboard is generating events. Turn this keystroke 573 * into an event and put it in the queue. If the queue 574 * is full, the keystroke is lost (sorry!). 575 */ 576 s = spltty(); 577 put = sc->sc_events.ev_put; 578 fe = &sc->sc_events.ev_q[put]; 579 put = (put + 1) % EV_QSIZE; 580 if (put == sc->sc_events.ev_get) { 581 log(LOG_WARNING, 582 "keyboard event queue overflow\n"); 583 splx(s); 584 continue; 585 } 586 fe->id = KBD_SCANCODE(code); 587 fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN; 588 firm_gettime(fe); 589 sc->sc_events.ev_put = put; 590 EV_WAKEUP(&sc->sc_events); 591 splx(s); 592 } 593 kbd_rbget = get; 594 } 595 } 596 597 static uint8_t sound[] = { 598 0xA8, 0x01, 0xA9, 0x01, 0xAA, 0x01, 0x00, 599 0xF8, 0x10, 0x10, 0x10, 0x00, 0x20, 0x03 600 }; 601 602 void 603 kbdbell(void) 604 { 605 int i, s; 606 607 s = splhigh(); 608 for (i = 0; i < sizeof(sound); i++) { 609 YM2149->sd_selr = i; 610 YM2149->sd_wdat = sound[i]; 611 } 612 splx(s); 613 } 614 615 616 /* 617 * Set the parameters of the 'default' beep. 618 */ 619 620 #define KBDBELLCLOCK 125000 /* 2MHz / 16 */ 621 #define KBDBELLDURATION 128 /* 256 / 2MHz */ 622 623 void 624 kbd_bell_gparms(u_int *volume, u_int *pitch, u_int *duration) 625 { 626 u_int tmp; 627 628 tmp = sound[11] | (sound[12] << 8); 629 *duration = (tmp * KBDBELLDURATION) / 1000; 630 631 tmp = sound[0] | (sound[1] << 8); 632 *pitch = KBDBELLCLOCK / tmp; 633 634 *volume = 0; 635 } 636 637 void 638 kbd_bell_sparms(u_int volume, u_int pitch, u_int duration) 639 { 640 u_int f, t; 641 642 f = pitch > 10 ? pitch : 10; /* minimum pitch */ 643 if (f > 20000) 644 f = 20000; /* maximum pitch */ 645 646 f = KBDBELLCLOCK / f; 647 648 t = (duration * 1000) / KBDBELLDURATION; 649 650 sound[ 0] = f & 0xff; 651 sound[ 1] = (f >> 8) & 0xf; 652 f -= 1; 653 sound[ 2] = f & 0xff; 654 sound[ 3] = (f >> 8) & 0xf; 655 f += 2; 656 sound[ 4] = f & 0xff; 657 sound[ 5] = (f >> 8) & 0xf; 658 659 sound[11] = t & 0xff; 660 sound[12] = (t >> 8) & 0xff; 661 662 sound[13] = 0x03; 663 } 664 665 int 666 kbdgetcn(void) 667 { 668 uint8_t code; 669 int s = spltty(); 670 int ints_active; 671 672 ints_active = 0; 673 if ((MFP->mf_imrb & IB_AINT) != 0) { 674 ints_active = 1; 675 MFP->mf_imrb &= ~IB_AINT; 676 } 677 for (;;) { 678 while (!((KBD->ac_cs & (A_IRQ | A_RXRDY)) == (A_IRQ | A_RXRDY))) 679 continue; /* Wait for key */ 680 if ((KBD->ac_cs & (A_OE | A_PE)) != 0) { 681 code = KBD->ac_da; /* Silently ignore errors */ 682 continue; 683 } 684 code = KBD->ac_da; 685 #if NITE>0 686 if (!kbd_do_modifier(code)) 687 #endif 688 break; 689 } 690 691 if (ints_active) { 692 MFP->mf_iprb = (uint8_t)~IB_AINT; 693 MFP->mf_imrb |= IB_AINT; 694 } 695 696 splx(s); 697 return code; 698 } 699 700 /* 701 * Write a command to the keyboard in 'polled' mode. 702 */ 703 static int 704 kbd_write_poll(const uint8_t *cmd, int len) 705 { 706 int timeout; 707 708 while (len-- > 0) { 709 KBD->ac_da = *cmd++; 710 for (timeout = 100; (KBD->ac_cs & A_TXRDY) == 0; timeout--) 711 delay(10); 712 if ((KBD->ac_cs & A_TXRDY) == 0) 713 return 0; 714 } 715 return 1; 716 } 717 718 /* 719 * Write a command to the keyboard. Return when command is send. 720 */ 721 void 722 kbd_write(const uint8_t *cmd, int len) 723 { 724 struct kbd_softc *sc = &kbd_softc; 725 int s; 726 727 /* 728 * Get to splhigh, 'real' interrupts arrive at spl6! 729 */ 730 s = splhigh(); 731 732 /* 733 * Make sure any previous write has ended... 734 */ 735 while (sc->sc_sendp != NULL) 736 tsleep((void *)&sc->sc_sendp, TTOPRI, "kbd_write1", 0); 737 738 /* 739 * If the KBD-acia is not currently busy, send the first 740 * character now. 741 */ 742 KBD->ac_cs = (sc->sc_soft_cs |= A_TXINT); 743 if ((KBD->ac_cs & A_TXRDY) != 0) { 744 KBD->ac_da = *cmd++; 745 len--; 746 } 747 748 /* 749 * If we're not yet done, wait until all characters are send. 750 */ 751 if (len > 0) { 752 sc->sc_sendp = cmd; 753 sc->sc_send_cnt = len; 754 tsleep((void *)&sc->sc_send_cnt, TTOPRI, "kbd_write2", 0); 755 } 756 splx(s); 757 758 /* 759 * Wakeup all procs waiting for us. 760 */ 761 wakeup((void *)&sc->sc_sendp); 762 } 763 764 /* 765 * Setup softc-fields to assemble a keyboard package. 766 */ 767 static void 768 kbd_pkg_start(struct kbd_softc *sc, uint8_t msg_start) 769 { 770 771 sc->sc_pkg_idx = 1; 772 sc->sc_package[0] = msg_start; 773 switch (msg_start) { 774 case 0xf6: 775 sc->sc_pkg_type = KBD_MEM_PKG; 776 sc->sc_pkg_size = 8; 777 break; 778 case 0xf7: 779 sc->sc_pkg_type = KBD_AMS_PKG; 780 sc->sc_pkg_size = 6; 781 break; 782 case 0xf8: 783 case 0xf9: 784 case 0xfa: 785 case 0xfb: 786 sc->sc_pkg_type = KBD_RMS_PKG; 787 sc->sc_pkg_size = 3; 788 break; 789 case 0xfc: 790 sc->sc_pkg_type = KBD_CLK_PKG; 791 sc->sc_pkg_size = 7; 792 break; 793 case 0xfe: 794 sc->sc_pkg_type = KBD_JOY0_PKG; 795 sc->sc_pkg_size = 2; 796 break; 797 case 0xff: 798 sc->sc_pkg_type = KBD_JOY1_PKG; 799 sc->sc_pkg_size = 2; 800 break; 801 default: 802 printf("kbd: Unknown packet 0x%x\n", msg_start); 803 break; 804 } 805 } 806 807 #if NITE > 0 808 /* 809 * Modifier processing 810 */ 811 static int 812 kbd_do_modifier(uint8_t code) 813 { 814 uint8_t up, mask; 815 816 up = KBD_RELEASED(code); 817 mask = 0; 818 819 switch (KBD_SCANCODE(code)) { 820 case KBD_LEFT_SHIFT: 821 mask = KBD_MOD_LSHIFT; 822 break; 823 case KBD_RIGHT_SHIFT: 824 mask = KBD_MOD_RSHIFT; 825 break; 826 case KBD_CTRL: 827 mask = KBD_MOD_CTRL; 828 break; 829 case KBD_ALT: 830 mask = KBD_MOD_ALT; 831 break; 832 case KBD_CAPS_LOCK: 833 /* CAPSLOCK is a toggle */ 834 if (!up) 835 kbd_modifier ^= KBD_MOD_CAPS; 836 return 1; 837 } 838 if (mask) { 839 if (up) 840 kbd_modifier &= ~mask; 841 else 842 kbd_modifier |= mask; 843 return 1; 844 } 845 return 0; 846 } 847 #endif 848 849 #if NWSKBD > 0 850 /* 851 * These are the callback functions that are passed to wscons. 852 * They really don't do anything worth noting, just call the 853 * other functions above. 854 */ 855 856 static int 857 kbd_enable(void *c, int on) 858 { 859 860 /* Wonder what this is supposed to do... */ 861 return 0; 862 } 863 864 static void 865 kbd_set_leds(void *c, int leds) 866 { 867 868 /* we can not set the leds */ 869 } 870 871 static int 872 kbd_ioctl(void *c, u_long cmd, void *data, int flag, struct lwp *p) 873 { 874 struct wskbd_bell_data *kd; 875 876 switch (cmd) { 877 case WSKBDIO_COMPLEXBELL: 878 kd = (struct wskbd_bell_data *)data; 879 kbd_bell(0, kd->pitch, kd->period, kd->volume); 880 return 0; 881 case WSKBDIO_SETLEDS: 882 return 0; 883 case WSKBDIO_GETLEDS: 884 *(int *)data = 0; 885 return 0; 886 case WSKBDIO_GTYPE: 887 *(u_int *)data = WSKBD_TYPE_ATARI; 888 return 0; 889 } 890 891 /* 892 * We are supposed to return EPASSTHROUGH to wscons if we didn't 893 * understand. 894 */ 895 return EPASSTHROUGH; 896 } 897 898 static void 899 kbd_getc(void *c, u_int *type, int *data) 900 { 901 int key; 902 903 key = kbdgetcn(); 904 905 *data = KBD_SCANCODE(key); 906 *type = KBD_RELEASED(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN; 907 } 908 909 static void 910 kbd_pollc(void *c, int on) 911 { 912 struct kbd_softc *sc = &kbd_softc; 913 914 sc->sc_pollingmode = on; 915 } 916 917 static void 918 kbd_bell(void *v, u_int pitch, u_int duration, u_int volume) 919 { 920 921 kbd_bell_sparms(volume, pitch, duration); 922 kbdbell(); 923 } 924 #endif /* NWSKBD */ 925