1 /* 2 * Copyright (c) 1988 University of Utah. 3 * Copyright (c) 1990 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * the Systems Programming Group of the University of Utah Computer 8 * Science Department. 9 * 10 * %sccs.include.redist.c% 11 * 12 * from: Utah $Hdr: hil.c 1.33 89/12/22$ 13 * 14 * @(#)hil.c 7.9 (Berkeley) 06/28/91 15 */ 16 17 #include "sys/param.h" 18 #include "sys/conf.h" 19 #include "sys/proc.h" 20 #include "sys/user.h" 21 #include "sys/ioctl.h" 22 #include "sys/file.h" 23 #include "sys/tty.h" 24 #include "sys/systm.h" 25 #include "sys/uio.h" 26 #include "sys/kernel.h" 27 28 #include "hilreg.h" 29 #include "hilioctl.h" 30 #include "hilvar.h" 31 #include "kbdmap.h" 32 33 #include "machine/cpu.h" 34 35 #include "vm/vm_param.h" 36 #include "vm/vm_map.h" 37 #include "vm/vm_kern.h" 38 #include "vm/vm_page.h" 39 #include "vm/vm_pager.h" 40 41 struct hilloop hil0; 42 struct _hilbell default_bell = { BELLDUR, BELLFREQ }; 43 44 #ifdef DEBUG 45 int hildebug = 0; 46 #define HDB_FOLLOW 0x01 47 #define HDB_MMAP 0x02 48 #define HDB_MASK 0x04 49 #define HDB_CONFIG 0x08 50 #define HDB_KEYBOARD 0x10 51 #define HDB_IDMODULE 0x20 52 #define HDB_EVENTS 0x80 53 #endif 54 55 /* symbolic sleep message strings */ 56 char hilin[] = "hilin"; 57 58 hilinit() 59 { 60 register struct hilloop *hilp = &hil0; /* XXX */ 61 register int i; 62 63 /* 64 * Initialize loop information 65 */ 66 hilp->hl_addr = HILADDR; 67 hilp->hl_cmdending = FALSE; 68 hilp->hl_actdev = hilp->hl_cmddev = 0; 69 hilp->hl_cmddone = FALSE; 70 hilp->hl_cmdbp = hilp->hl_cmdbuf; 71 hilp->hl_pollbp = hilp->hl_pollbuf; 72 hilp->hl_kbddev = 0; 73 hilp->hl_kbdlang = KBD_DEFAULT; 74 hilp->hl_kbdflags = 0; 75 /* 76 * Clear all queues and device associations with queues 77 */ 78 for (i = 0; i < NHILQ; i++) { 79 hilp->hl_queue[i].hq_eventqueue = NULL; 80 hilp->hl_queue[i].hq_procp = NULL; 81 hilp->hl_queue[i].hq_devmask = 0; 82 } 83 for (i = 0; i < NHILD; i++) 84 hilp->hl_device[i].hd_qmask = 0; 85 hilp->hl_device[HILLOOPDEV].hd_flags = (HIL_ALIVE|HIL_PSEUDO); 86 /* 87 * Reset the loop hardware, and collect keyboard/id info 88 */ 89 hilreset(hilp); 90 hilinfo(hilp); 91 kbdenable(); 92 } 93 94 /* ARGSUSED */ 95 hilopen(dev, flags, mode, p) 96 dev_t dev; 97 int flags, mode; 98 struct proc *p; 99 { 100 register struct hilloop *hilp = &hil0; /* XXX */ 101 register struct hilloopdev *dptr; 102 u_char device = HILUNIT(dev); 103 104 #ifdef DEBUG 105 if (hildebug & HDB_FOLLOW) 106 printf("hilopen(%d): device %x\n", p->p_pid, device); 107 #endif 108 109 if ((hilp->hl_device[HILLOOPDEV].hd_flags & HIL_ALIVE) == 0) 110 return(ENXIO); 111 112 dptr = &hilp->hl_device[device]; 113 if ((dptr->hd_flags & HIL_ALIVE) == 0) 114 return(ENODEV); 115 116 /* 117 * Pseudo-devices cannot be read, nothing more to do. 118 */ 119 if (dptr->hd_flags & HIL_PSEUDO) 120 return(0); 121 122 /* 123 * Open semantics: 124 * 1. Open devices have only one of HIL_READIN/HIL_QUEUEIN. 125 * 2. HPUX processes always get read syscall interface and 126 * must have exclusive use of the device. 127 * 3. BSD processes default to shared queue interface. 128 * Multiple processes can open the device. 129 */ 130 if (p->p_flag & SHPUX) { 131 if (dptr->hd_flags & (HIL_READIN|HIL_QUEUEIN)) 132 return(EBUSY); 133 dptr->hd_flags |= HIL_READIN; 134 } else { 135 if (dptr->hd_flags & HIL_READIN) 136 return(EBUSY); 137 dptr->hd_flags |= HIL_QUEUEIN; 138 } 139 if (flags & FNONBLOCK) 140 dptr->hd_flags |= HIL_NOBLOCK; 141 /* 142 * It is safe to flush the read buffer as we are guarenteed 143 * that no one else is using it. 144 */ 145 ndflush(&dptr->hd_queue, dptr->hd_queue.c_cc); 146 147 send_hil_cmd(hilp->hl_addr, HIL_INTON, NULL, 0, NULL); 148 /* 149 * Opened the keyboard, put in raw mode. 150 */ 151 (void) splhil(); 152 if (device == hilp->hl_kbddev) { 153 u_char mask = 0; 154 send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &mask, 1, NULL); 155 hilp->hl_kbdflags |= KBD_RAW; 156 #ifdef DEBUG 157 if (hildebug & HDB_KEYBOARD) 158 printf("hilopen: keyboard %d raw\n", hilp->hl_kbddev); 159 #endif 160 } 161 (void) spl0(); 162 return (0); 163 } 164 165 /* ARGSUSED */ 166 hilclose(dev, flags) 167 dev_t dev; 168 { 169 struct proc *p = curproc; /* XXX */ 170 register struct hilloop *hilp = &hil0; /* XXX */ 171 register struct hilloopdev *dptr; 172 register int i; 173 u_char device = HILUNIT(dev); 174 char mask, lpctrl; 175 176 #ifdef DEBUG 177 if (hildebug & HDB_FOLLOW) 178 printf("hilclose(%d): device %x\n", p->p_pid, device); 179 #endif 180 181 dptr = &hilp->hl_device[device]; 182 if (device && (dptr->hd_flags & HIL_PSEUDO)) 183 return (0); 184 185 if ((p->p_flag & SHPUX) == 0) { 186 /* 187 * If this is the loop device, 188 * free up all queues belonging to this process. 189 */ 190 if (device == 0) { 191 for (i = 0; i < NHILQ; i++) 192 if (hilp->hl_queue[i].hq_procp == p) 193 (void) hilqfree(i); 194 } else { 195 mask = ~hildevmask(device); 196 (void) splhil(); 197 for (i = 0; i < NHILQ; i++) 198 if (hilp->hl_queue[i].hq_procp == p) { 199 dptr->hd_qmask &= ~hilqmask(i); 200 hilp->hl_queue[i].hq_devmask &= mask; 201 } 202 (void) spl0(); 203 } 204 } 205 /* 206 * Always flush the read buffer 207 */ 208 dptr->hd_flags &= ~(HIL_QUEUEIN|HIL_READIN|HIL_NOBLOCK); 209 ndflush(&dptr->hd_queue, dptr->hd_queue.c_cc); 210 /* 211 * Set keyboard back to cooked mode when closed. 212 */ 213 (void) splhil(); 214 if (device && device == hilp->hl_kbddev) { 215 mask = 1 << (hilp->hl_kbddev - 1); 216 send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &mask, 1, NULL); 217 hilp->hl_kbdflags &= ~(KBD_RAW|KBD_AR1|KBD_AR2); 218 /* 219 * XXX: We have had trouble with keyboards remaining raw 220 * after close due to the LPC_KBDCOOK bit getting cleared 221 * somewhere along the line. Hence we check and reset 222 * LPCTRL if necessary. 223 */ 224 send_hil_cmd(hilp->hl_addr, HIL_READLPCTRL, NULL, 0, &lpctrl); 225 if ((lpctrl & LPC_KBDCOOK) == 0) { 226 printf("hilclose: bad LPCTRL %x, reset to %x\n", 227 lpctrl, lpctrl|LPC_KBDCOOK); 228 lpctrl |= LPC_KBDCOOK; 229 send_hil_cmd(hilp->hl_addr, HIL_WRITELPCTRL, 230 &lpctrl, 1, NULL); 231 } 232 #ifdef DEBUG 233 if (hildebug & HDB_KEYBOARD) 234 printf("hilclose: keyboard %d cooked\n", 235 hilp->hl_kbddev); 236 #endif 237 kbdenable(); 238 } 239 (void) spl0(); 240 return (0); 241 } 242 243 /* 244 * Read interface to HIL device. 245 */ 246 hilread(dev, uio) 247 dev_t dev; 248 register struct uio *uio; 249 { 250 struct hilloop *hilp = &hil0; /* XXX */ 251 register struct hilloopdev *dptr; 252 register int cc; 253 u_char device = HILUNIT(dev); 254 char buf[HILBUFSIZE]; 255 int error; 256 257 #if 0 258 /* 259 * XXX: Don't do this since HP-UX doesn't. 260 * 261 * Check device number. 262 * This check is necessary since loop can reconfigure. 263 */ 264 if (device > hilp->hl_maxdev) 265 return(ENODEV); 266 #endif 267 268 dptr = &hilp->hl_device[device]; 269 if ((dptr->hd_flags & HIL_READIN) == 0) 270 return(ENODEV); 271 272 (void) splhil(); 273 while (dptr->hd_queue.c_cc == 0) { 274 if (dptr->hd_flags & HIL_NOBLOCK) { 275 spl0(); 276 return(EWOULDBLOCK); 277 } 278 dptr->hd_flags |= HIL_ASLEEP; 279 if (error = tsleep((caddr_t)dptr, TTIPRI | PCATCH, hilin, 0)) { 280 (void) spl0(); 281 return (error); 282 } 283 } 284 (void) spl0(); 285 286 error = 0; 287 while (uio->uio_resid > 0 && error == 0) { 288 cc = hilq_to_b(&dptr->hd_queue, buf, 289 MIN(uio->uio_resid, HILBUFSIZE)); 290 if (cc <= 0) 291 break; 292 error = uiomove(buf, cc, uio); 293 } 294 return(error); 295 } 296 297 hilioctl(dev, cmd, data, flag, p) 298 dev_t dev; 299 caddr_t data; 300 struct proc *p; 301 { 302 register struct hilloop *hilp = &hil0; /* XXX */ 303 char device = HILUNIT(dev); 304 struct hilloopdev *dptr; 305 register int i; 306 u_char hold; 307 int error; 308 309 #ifdef DEBUG 310 if (hildebug & HDB_FOLLOW) 311 printf("hilioctl(%d): dev %x cmd %x\n", 312 p->p_pid, device, cmd); 313 #endif 314 315 dptr = &hilp->hl_device[device]; 316 if ((dptr->hd_flags & HIL_ALIVE) == 0) 317 return (ENODEV); 318 319 /* 320 * Don't allow hardware ioctls on virtual devices. 321 * Note that though these are the BSD names, they have the same 322 * values as the HP-UX equivalents so we catch them as well. 323 */ 324 if (dptr->hd_flags & HIL_PSEUDO) { 325 switch (cmd) { 326 case HILIOCSC: 327 case HILIOCID: 328 case HILIOCRN: 329 case HILIOCRS: 330 case HILIOCED: 331 return(ENODEV); 332 333 /* 334 * XXX: should also return ENODEV but HP-UX compat 335 * breaks if we do. They work ok right now because 336 * we only recognize one keyboard on the loop. This 337 * will have to change if we remove that restriction. 338 */ 339 case HILIOCAROFF: 340 case HILIOCAR1: 341 case HILIOCAR2: 342 break; 343 344 default: 345 break; 346 } 347 } 348 349 #ifdef HPUXCOMPAT 350 if (p->p_flag & SHPUX) 351 return(hpuxhilioctl(dev, cmd, data, flag)); 352 #endif 353 354 hilp->hl_cmdbp = hilp->hl_cmdbuf; 355 bzero((caddr_t)hilp->hl_cmdbuf, HILBUFSIZE); 356 hilp->hl_cmddev = device; 357 error = 0; 358 switch (cmd) { 359 360 case HILIOCSBP: 361 /* Send four data bytes to the tone gererator. */ 362 send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL); 363 /* Send the trigger beeper command to the 8042. */ 364 send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL); 365 break; 366 367 case HILIOCRRT: 368 /* Transfer the real time to the 8042 data buffer */ 369 send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL); 370 /* Read each byte of the real time */ 371 for (i = 0; i < 5; i++) { 372 send_hil_cmd(hilp->hl_addr, HIL_READTIME + i, NULL, 373 0, &hold); 374 data[4-i] = hold; 375 } 376 break; 377 378 case HILIOCRT: 379 for (i = 0; i < 4; i++) { 380 send_hil_cmd(hilp->hl_addr, (cmd & 0xFF) + i, 381 NULL, 0, &hold); 382 data[i] = hold; 383 } 384 break; 385 386 case HILIOCID: 387 case HILIOCSC: 388 case HILIOCRN: 389 case HILIOCRS: 390 case HILIOCED: 391 send_hildev_cmd(hilp, device, (cmd & 0xFF)); 392 bcopy(hilp->hl_cmdbuf, data, hilp->hl_cmdbp-hilp->hl_cmdbuf); 393 break; 394 395 case HILIOCAROFF: 396 case HILIOCAR1: 397 case HILIOCAR2: 398 if (hilp->hl_kbddev) { 399 hilp->hl_cmddev = hilp->hl_kbddev; 400 send_hildev_cmd(hilp, hilp->hl_kbddev, (cmd & 0xFF)); 401 hilp->hl_kbdflags &= ~(KBD_AR1|KBD_AR2); 402 if (cmd == HILIOCAR1) 403 hilp->hl_kbdflags |= KBD_AR1; 404 else if (cmd == HILIOCAR2) 405 hilp->hl_kbdflags |= KBD_AR2; 406 } 407 break; 408 409 case HILIOCBEEP: 410 hilbeep(hilp, (struct _hilbell *)data); 411 break; 412 413 case FIONBIO: 414 dptr = &hilp->hl_device[device]; 415 if (*(int *)data) 416 dptr->hd_flags |= HIL_NOBLOCK; 417 else 418 dptr->hd_flags &= ~HIL_NOBLOCK; 419 break; 420 421 /* 422 * FIOASYNC must be present for FIONBIO above to work! 423 * (See fcntl in kern_descrip.c). 424 */ 425 case FIOASYNC: 426 break; 427 428 case HILIOCALLOCQ: 429 error = hilqalloc((struct hilqinfo *)data); 430 break; 431 432 case HILIOCFREEQ: 433 error = hilqfree(((struct hilqinfo *)data)->qid); 434 break; 435 436 case HILIOCMAPQ: 437 error = hilqmap(*(int *)data, device); 438 break; 439 440 case HILIOCUNMAPQ: 441 error = hilqunmap(*(int *)data, device); 442 break; 443 444 case HILIOCHPUX: 445 dptr = &hilp->hl_device[device]; 446 dptr->hd_flags |= HIL_READIN; 447 dptr->hd_flags &= ~HIL_QUEUEIN; 448 break; 449 450 case HILIOCRESET: 451 hilreset(hilp); 452 break; 453 454 #ifdef DEBUG 455 case HILIOCTEST: 456 hildebug = *(int *) data; 457 break; 458 #endif 459 460 default: 461 error = EINVAL; 462 break; 463 464 } 465 hilp->hl_cmddev = 0; 466 return(error); 467 } 468 469 #ifdef HPUXCOMPAT 470 /* ARGSUSED */ 471 hpuxhilioctl(dev, cmd, data, flag) 472 dev_t dev; 473 caddr_t data; 474 { 475 register struct hilloop *hilp = &hil0; /* XXX */ 476 char device = HILUNIT(dev); 477 struct hilloopdev *dptr; 478 register int i; 479 u_char hold; 480 481 hilp->hl_cmdbp = hilp->hl_cmdbuf; 482 bzero((caddr_t)hilp->hl_cmdbuf, HILBUFSIZE); 483 hilp->hl_cmddev = device; 484 switch (cmd) { 485 486 case HILSC: 487 case HILID: 488 case HILRN: 489 case HILRS: 490 case HILED: 491 case HILP1: 492 case HILP2: 493 case HILP3: 494 case HILP4: 495 case HILP5: 496 case HILP6: 497 case HILP7: 498 case HILP: 499 case HILA1: 500 case HILA2: 501 case HILA3: 502 case HILA4: 503 case HILA5: 504 case HILA6: 505 case HILA7: 506 case HILA: 507 send_hildev_cmd(hilp, device, (cmd & 0xFF)); 508 bcopy(hilp->hl_cmdbuf, data, hilp->hl_cmdbp-hilp->hl_cmdbuf); 509 break; 510 511 case HILDKR: 512 case HILER1: 513 case HILER2: 514 if (hilp->hl_kbddev) { 515 hilp->hl_cmddev = hilp->hl_kbddev; 516 send_hildev_cmd(hilp, hilp->hl_kbddev, (cmd & 0xFF)); 517 hilp->hl_kbdflags &= ~(KBD_AR1|KBD_AR2); 518 if (cmd == HILIOCAR1) 519 hilp->hl_kbdflags |= KBD_AR1; 520 else if (cmd == HILIOCAR2) 521 hilp->hl_kbdflags |= KBD_AR2; 522 } 523 break; 524 525 case EFTSBP: 526 /* Send four data bytes to the tone gererator. */ 527 send_hil_cmd(hilp->hl_addr, HIL_STARTCMD, data, 4, NULL); 528 /* Send the trigger beeper command to the 8042. */ 529 send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL); 530 break; 531 532 case EFTRRT: 533 /* Transfer the real time to the 8042 data buffer */ 534 send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, NULL); 535 /* Read each byte of the real time */ 536 for (i = 0; i < 5; i++) { 537 send_hil_cmd(hilp->hl_addr, HIL_READTIME + i, NULL, 538 0, &hold); 539 data[4-i] = hold; 540 } 541 break; 542 543 case EFTRT: 544 for (i = 0; i < 4; i++) { 545 send_hil_cmd(hilp->hl_addr, (cmd & 0xFF) + i, 546 NULL, 0, &hold); 547 data[i] = hold; 548 } 549 break; 550 551 case EFTRLC: 552 case EFTRCC: 553 send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), NULL, 0, &hold); 554 *data = hold; 555 break; 556 557 case EFTSRPG: 558 case EFTSRD: 559 case EFTSRR: 560 send_hil_cmd(hilp->hl_addr, (cmd & 0xFF), data, 1, NULL); 561 break; 562 563 case EFTSBI: 564 hilbeep(hilp, (struct _hilbell *)data); 565 break; 566 567 case FIONBIO: 568 dptr = &hilp->hl_device[device]; 569 if (*(int *)data) 570 dptr->hd_flags |= HIL_NOBLOCK; 571 else 572 dptr->hd_flags &= ~HIL_NOBLOCK; 573 break; 574 575 case FIOASYNC: 576 break; 577 578 default: 579 hilp->hl_cmddev = 0; 580 return(EINVAL); 581 } 582 hilp->hl_cmddev = 0; 583 return(0); 584 } 585 #endif 586 587 /* 588 * XXX: the mmap interface for HIL devices should be rethought. 589 * We used it only briefly in conjuntion with shared queues 590 * (instead of HILIOCMAPQ ioctl). Perhaps mmap()ing a device 591 * should give a single queue per process. 592 */ 593 /* ARGSUSED */ 594 hilmap(dev, off, prot) 595 dev_t dev; 596 register int off; 597 { 598 #ifdef MMAP 599 struct proc *p = curproc; /* XXX */ 600 register struct hilloop *hilp = &hil0; /* XXX */ 601 register struct hiliqueue *qp; 602 register int qnum; 603 604 /* 605 * Only allow mmap() on loop device 606 */ 607 if (HILUNIT(dev) != 0 || off >= NHILQ*sizeof(HILQ)) 608 return(-1); 609 /* 610 * Determine which queue we want based on the offset. 611 * Queue must belong to calling process. 612 */ 613 qp = &hilp->hl_queue[off / sizeof(HILQ)]; 614 if (qp->hq_procp != p) 615 return(-1); 616 617 off %= sizeof(HILQ); 618 return(kvtop((u_int)qp->hq_eventqueue + off) >> PGSHIFT); 619 #endif 620 } 621 622 /*ARGSUSED*/ 623 hilselect(dev, rw, p) 624 dev_t dev; 625 struct proc *p; 626 { 627 register struct hilloop *hilp = &hil0; /* XXX */ 628 register struct hilloopdev *dptr; 629 register struct hiliqueue *qp; 630 register int mask; 631 int s, device; 632 633 if (rw == FWRITE) 634 return (1); 635 device = HILUNIT(dev); 636 637 /* 638 * Read interface. 639 * Return 1 if there is something in the queue, 0 ow. 640 */ 641 dptr = &hilp->hl_device[device]; 642 if (dptr->hd_flags & HIL_READIN) { 643 s = splhil(); 644 if (dptr->hd_queue.c_cc) { 645 splx(s); 646 return (1); 647 } 648 if (dptr->hd_selr && 649 dptr->hd_selr->p_wchan == (caddr_t)&selwait) 650 dptr->hd_flags |= HIL_SELCOLL; 651 else 652 dptr->hd_selr = p; 653 splx(s); 654 return (0); 655 } 656 657 /* 658 * Make sure device is alive and real (or the loop device). 659 * Note that we do not do this for the read interface. 660 * This is primarily to be consistant with HP-UX. 661 */ 662 if (device && (dptr->hd_flags & (HIL_ALIVE|HIL_PSEUDO)) != HIL_ALIVE) 663 return (1); 664 665 /* 666 * Select on loop device is special. 667 * Check to see if there are any data for any loop device 668 * provided it is associated with a queue belonging to this user. 669 */ 670 if (device == 0) 671 mask = -1; 672 else 673 mask = hildevmask(device); 674 /* 675 * Must check everybody with interrupts blocked to prevent races. 676 */ 677 s = splhil(); 678 for (qp = hilp->hl_queue; qp < &hilp->hl_queue[NHILQ]; qp++) 679 if (qp->hq_procp == p && (mask & qp->hq_devmask) && 680 qp->hq_eventqueue->hil_evqueue.head != 681 qp->hq_eventqueue->hil_evqueue.tail) { 682 splx(s); 683 return (1); 684 } 685 686 if (dptr->hd_selr && dptr->hd_selr->p_wchan == (caddr_t)&selwait) 687 dptr->hd_flags |= HIL_SELCOLL; 688 else 689 dptr->hd_selr = p; 690 splx(s); 691 return (0); 692 } 693 694 hilint() 695 { 696 struct hilloop *hilp = &hil0; /* XXX */ 697 register struct hil_dev *hildevice = hilp->hl_addr; 698 u_char c, stat; 699 700 stat = hildevice->hil_stat; 701 c = hildevice->hil_data; /* clears interrupt */ 702 hil_process_int(stat, c); 703 } 704 705 #include "ite.h" 706 707 hil_process_int(stat, c) 708 register u_char stat, c; 709 { 710 register struct hilloop *hilp; 711 712 #ifdef DEBUG 713 if (hildebug & HDB_EVENTS) 714 printf("hilint: %x %x\n", stat, c); 715 #endif 716 717 /* the shift enables the compiler to generate a jump table */ 718 switch ((stat>>HIL_SSHIFT) & HIL_SMASK) { 719 720 #if NITE > 0 721 case HIL_KEY: 722 case HIL_SHIFT: 723 case HIL_CTRL: 724 case HIL_CTRLSHIFT: 725 itefilter(stat, c); 726 return; 727 #endif 728 729 case HIL_STATUS: /* The status info. */ 730 hilp = &hil0; /* XXX */ 731 if (c & HIL_ERROR) { 732 hilp->hl_cmddone = TRUE; 733 if (c == HIL_RECONFIG) 734 hilconfig(hilp); 735 break; 736 } 737 if (c & HIL_COMMAND) { 738 if (c & HIL_POLLDATA) /* End of data */ 739 hilevent(hilp); 740 else /* End of command */ 741 hilp->hl_cmdending = TRUE; 742 hilp->hl_actdev = 0; 743 } else { 744 if (c & HIL_POLLDATA) { /* Start of polled data */ 745 if (hilp->hl_actdev != 0) 746 hilevent(hilp); 747 hilp->hl_actdev = (c & HIL_DEVMASK); 748 hilp->hl_pollbp = hilp->hl_pollbuf; 749 } else { /* Start of command */ 750 if (hilp->hl_cmddev == (c & HIL_DEVMASK)) { 751 hilp->hl_cmdbp = hilp->hl_cmdbuf; 752 hilp->hl_actdev = 0; 753 } 754 } 755 } 756 return; 757 758 case HIL_DATA: 759 hilp = &hil0; /* XXX */ 760 if (hilp->hl_actdev != 0) /* Collecting poll data */ 761 *hilp->hl_pollbp++ = c; 762 else if (hilp->hl_cmddev != 0) /* Collecting cmd data */ 763 if (hilp->hl_cmdending) { 764 hilp->hl_cmddone = TRUE; 765 hilp->hl_cmdending = FALSE; 766 } else 767 *hilp->hl_cmdbp++ = c; 768 return; 769 770 case 0: /* force full jump table */ 771 default: 772 return; 773 } 774 } 775 776 #if defined(DEBUG) && !defined(PANICBUTTON) 777 #define PANICBUTTON 778 #endif 779 780 /* 781 * Optimized macro to compute: 782 * eq->head == (eq->tail + 1) % eq->size 783 * i.e. has tail caught up with head. We do this because 32 bit long 784 * remaidering is expensive (a function call with our compiler). 785 */ 786 #define HQFULL(eq) (((eq)->head?(eq)->head:(eq)->size) == (eq)->tail+1) 787 #define HQVALID(eq) \ 788 ((eq)->size == HEVQSIZE && (eq)->tail >= 0 && (eq)->tail < HEVQSIZE) 789 790 hilevent(hilp) 791 struct hilloop *hilp; 792 { 793 register struct hilloopdev *dptr = &hilp->hl_device[hilp->hl_actdev]; 794 register int len, mask, qnum; 795 register u_char *cp, *pp; 796 register HILQ *hq; 797 struct timeval ourtime; 798 hil_packet *proto; 799 int s, len0; 800 long tenths; 801 802 #ifdef PANICBUTTON 803 static int first; 804 extern int panicbutton; 805 806 cp = hilp->hl_pollbuf; 807 if (panicbutton && (*cp & HIL_KBDDATA)) { 808 if (*++cp == 0x4E) 809 first = 1; 810 else if (first && *cp == 0x46 && !panicstr) 811 panic("are we having fun yet?"); 812 else 813 first = 0; 814 } 815 #endif 816 #ifdef DEBUG 817 if (hildebug & HDB_EVENTS) { 818 printf("hilevent: dev %d pollbuf: ", hilp->hl_actdev); 819 printhilpollbuf(hilp); 820 printf("\n"); 821 } 822 #endif 823 824 /* 825 * Note that HIL_READIN effectively "shuts off" any queues 826 * that may have been in use at the time of an HILIOCHPUX call. 827 */ 828 if (dptr->hd_flags & HIL_READIN) { 829 hpuxhilevent(hilp, dptr); 830 return; 831 } 832 833 /* 834 * If this device isn't on any queue or there are no data 835 * in the packet (can this happen?) do nothing. 836 */ 837 if (dptr->hd_qmask == 0 || 838 (len0 = hilp->hl_pollbp - hilp->hl_pollbuf) <= 0) 839 return; 840 841 /* 842 * Everybody gets the same time stamp 843 */ 844 s = splclock(); 845 ourtime = time; 846 splx(s); 847 tenths = (ourtime.tv_sec * 100) + (ourtime.tv_usec / 10000); 848 849 proto = NULL; 850 mask = dptr->hd_qmask; 851 for (qnum = 0; mask; qnum++) { 852 if ((mask & hilqmask(qnum)) == 0) 853 continue; 854 mask &= ~hilqmask(qnum); 855 hq = hilp->hl_queue[qnum].hq_eventqueue; 856 857 /* 858 * Ensure that queue fields that we rely on are valid 859 * and that there is space in the queue. If either 860 * test fails, we just skip this queue. 861 */ 862 if (!HQVALID(&hq->hil_evqueue) || HQFULL(&hq->hil_evqueue)) 863 continue; 864 865 /* 866 * Copy data to queue. 867 * If this is the first queue we construct the packet 868 * with length, timestamp and poll buffer data. 869 * For second and sucessive packets we just duplicate 870 * the first packet. 871 */ 872 pp = (u_char *) &hq->hil_event[hq->hil_evqueue.tail]; 873 if (proto == NULL) { 874 proto = (hil_packet *)pp; 875 cp = hilp->hl_pollbuf; 876 len = len0; 877 *pp++ = len + 6; 878 *pp++ = hilp->hl_actdev; 879 *(long *)pp = tenths; 880 pp += sizeof(long); 881 do *pp++ = *cp++; while (--len); 882 } else 883 *(hil_packet *)pp = *proto; 884 885 if (++hq->hil_evqueue.tail == hq->hil_evqueue.size) 886 hq->hil_evqueue.tail = 0; 887 } 888 889 /* 890 * Wake up anyone selecting on this device or the loop itself 891 */ 892 if (dptr->hd_selr) { 893 selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL); 894 dptr->hd_selr = NULL; 895 dptr->hd_flags &= ~HIL_SELCOLL; 896 } 897 dptr = &hilp->hl_device[HILLOOPDEV]; 898 if (dptr->hd_selr) { 899 selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL); 900 dptr->hd_selr = NULL; 901 dptr->hd_flags &= ~HIL_SELCOLL; 902 } 903 } 904 905 #undef HQFULL 906 907 hpuxhilevent(hilp, dptr) 908 register struct hilloop *hilp; 909 register struct hilloopdev *dptr; 910 { 911 register int len; 912 struct timeval ourtime; 913 long tstamp; 914 int s; 915 916 /* 917 * Everybody gets the same time stamp 918 */ 919 s = splclock(); 920 ourtime = time; 921 splx(s); 922 tstamp = (ourtime.tv_sec * 100) + (ourtime.tv_usec / 10000); 923 924 /* 925 * Each packet that goes into the buffer must be preceded by the 926 * number of bytes in the packet, and the timestamp of the packet. 927 * This adds 5 bytes to the packet size. Make sure there is enough 928 * room in the buffer for it, and if not, toss the packet. 929 */ 930 len = hilp->hl_pollbp - hilp->hl_pollbuf; 931 if (dptr->hd_queue.c_cc <= (HILMAXCLIST - (len+5))) { 932 putc(len+5, &dptr->hd_queue); 933 (void) b_to_q((char *)&tstamp, sizeof tstamp, &dptr->hd_queue); 934 (void) b_to_q((char *)hilp->hl_pollbuf, len, &dptr->hd_queue); 935 } 936 937 /* 938 * Wake up any one blocked on a read or select 939 */ 940 if (dptr->hd_flags & HIL_ASLEEP) { 941 dptr->hd_flags &= ~HIL_ASLEEP; 942 wakeup((caddr_t)dptr); 943 } 944 if (dptr->hd_selr) { 945 selwakeup(dptr->hd_selr, dptr->hd_flags & HIL_SELCOLL); 946 dptr->hd_selr = NULL; 947 dptr->hd_flags &= ~HIL_SELCOLL; 948 } 949 } 950 951 /* 952 * Shared queue manipulation routines 953 */ 954 955 hilqalloc(qip) 956 struct hilqinfo *qip; 957 { 958 struct proc *p = curproc; /* XXX */ 959 960 #ifdef DEBUG 961 if (hildebug & HDB_FOLLOW) 962 printf("hilqalloc(%d): addr %x\n", p->p_pid, qip->addr); 963 #endif 964 return(EINVAL); 965 } 966 967 hilqfree(qnum) 968 register int qnum; 969 { 970 struct proc *p = curproc; /* XXX */ 971 972 #ifdef DEBUG 973 if (hildebug & HDB_FOLLOW) 974 printf("hilqfree(%d): qnum %d\n", p->p_pid, qnum); 975 #endif 976 return(EINVAL); 977 } 978 979 hilqmap(qnum, device) 980 register int qnum, device; 981 { 982 struct proc *p = curproc; /* XXX */ 983 register struct hilloop *hilp = &hil0; /* XXX */ 984 register struct hilloopdev *dptr = &hilp->hl_device[device]; 985 int s; 986 987 #ifdef DEBUG 988 if (hildebug & HDB_FOLLOW) 989 printf("hilqmap(%d): qnum %d device %x\n", 990 p->p_pid, qnum, device); 991 #endif 992 if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != p) 993 return(EINVAL); 994 if ((dptr->hd_flags & HIL_QUEUEIN) == 0) 995 return(EINVAL); 996 if (dptr->hd_qmask && p->p_ucred->cr_uid && 997 p->p_ucred->cr_uid != dptr->hd_uid) 998 return(EPERM); 999 1000 hilp->hl_queue[qnum].hq_devmask |= hildevmask(device); 1001 if (dptr->hd_qmask == 0) 1002 dptr->hd_uid = p->p_ucred->cr_uid; 1003 s = splhil(); 1004 dptr->hd_qmask |= hilqmask(qnum); 1005 splx(s); 1006 #ifdef DEBUG 1007 if (hildebug & HDB_MASK) 1008 printf("hilqmap(%d): devmask %x qmask %x\n", 1009 p->p_pid, hilp->hl_queue[qnum].hq_devmask, 1010 dptr->hd_qmask); 1011 #endif 1012 return(0); 1013 } 1014 1015 hilqunmap(qnum, device) 1016 register int qnum, device; 1017 { 1018 struct proc *p = curproc; /* XXX */ 1019 register struct hilloop *hilp = &hil0; /* XXX */ 1020 int s; 1021 1022 #ifdef DEBUG 1023 if (hildebug & HDB_FOLLOW) 1024 printf("hilqunmap(%d): qnum %d device %x\n", 1025 p->p_pid, qnum, device); 1026 #endif 1027 1028 if (qnum >= NHILQ || hilp->hl_queue[qnum].hq_procp != p) 1029 return(EINVAL); 1030 1031 hilp->hl_queue[qnum].hq_devmask &= ~hildevmask(device); 1032 s = splhil(); 1033 hilp->hl_device[device].hd_qmask &= ~hilqmask(qnum); 1034 splx(s); 1035 #ifdef DEBUG 1036 if (hildebug & HDB_MASK) 1037 printf("hilqunmap(%d): devmask %x qmask %x\n", 1038 p->p_pid, hilp->hl_queue[qnum].hq_devmask, 1039 hilp->hl_device[device].hd_qmask); 1040 #endif 1041 return(0); 1042 } 1043 1044 #include "sys/clist.h" 1045 1046 /* 1047 * This is just a copy of the virgin q_to_b routine with minor 1048 * optimizations for HIL use. It is used because we don't have 1049 * to raise the priority to spltty() for most of the clist manipulations. 1050 */ 1051 hilq_to_b(q, cp, cc) 1052 register struct clist *q; 1053 register char *cp; 1054 { 1055 register struct cblock *bp; 1056 register int nc; 1057 char *acp; 1058 int s; 1059 extern char cwaiting; 1060 1061 if (cc <= 0) 1062 return (0); 1063 s = splhil(); 1064 if (q->c_cc <= 0) { 1065 q->c_cc = 0; 1066 q->c_cf = q->c_cl = NULL; 1067 splx(s); 1068 return (0); 1069 } 1070 acp = cp; 1071 1072 while (cc) { 1073 nc = sizeof (struct cblock) - ((int)q->c_cf & CROUND); 1074 nc = MIN(nc, cc); 1075 nc = MIN(nc, q->c_cc); 1076 (void) bcopy(q->c_cf, cp, (unsigned)nc); 1077 q->c_cf += nc; 1078 q->c_cc -= nc; 1079 cc -= nc; 1080 cp += nc; 1081 if (q->c_cc <= 0) { 1082 bp = (struct cblock *)(q->c_cf - 1); 1083 bp = (struct cblock *)((int)bp & ~CROUND); 1084 q->c_cf = q->c_cl = NULL; 1085 spltty(); 1086 bp->c_next = cfreelist; 1087 cfreelist = bp; 1088 cfreecount += CBSIZE; 1089 if (cwaiting) { 1090 wakeup(&cwaiting); 1091 cwaiting = 0; 1092 } 1093 break; 1094 } 1095 if (((int)q->c_cf & CROUND) == 0) { 1096 bp = (struct cblock *)(q->c_cf); 1097 bp--; 1098 q->c_cf = bp->c_next->c_info; 1099 spltty(); 1100 bp->c_next = cfreelist; 1101 cfreelist = bp; 1102 cfreecount += CBSIZE; 1103 if (cwaiting) { 1104 wakeup(&cwaiting); 1105 cwaiting = 0; 1106 } 1107 splhil(); 1108 } 1109 } 1110 splx(s); 1111 return (cp-acp); 1112 } 1113 1114 /* 1115 * Cooked keyboard functions for ite driver. 1116 * There is only one "cooked" ITE keyboard (the first keyboard found) 1117 * per loop. There may be other keyboards, but they will always be "raw". 1118 */ 1119 1120 kbdbell() 1121 { 1122 struct hilloop *hilp = &hil0; /* XXX */ 1123 1124 hilbeep(hilp, &default_bell); 1125 } 1126 1127 kbdenable() 1128 { 1129 struct hilloop *hilp = &hil0; /* XXX */ 1130 register struct hil_dev *hildevice = hilp->hl_addr; 1131 char db; 1132 1133 /* Set the autorepeat rate register */ 1134 db = ar_format(KBD_ARR); 1135 send_hil_cmd(hildevice, HIL_SETARR, &db, 1, NULL); 1136 1137 /* Set the autorepeat delay register */ 1138 db = ar_format(KBD_ARD); 1139 send_hil_cmd(hildevice, HIL_SETARD, &db, 1, NULL); 1140 1141 /* Enable interrupts */ 1142 send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL); 1143 } 1144 1145 kbddisable() 1146 { 1147 } 1148 1149 /* 1150 * XXX: read keyboard directly and return code. 1151 * Used by console getchar routine. Could really screw up anybody 1152 * reading from the keyboard in the normal, interrupt driven fashion. 1153 */ 1154 kbdgetc(statp) 1155 int *statp; 1156 { 1157 struct hilloop *hilp = &hil0; /* XXX */ 1158 register struct hil_dev *hildevice = hilp->hl_addr; 1159 register int c, stat; 1160 int s; 1161 1162 s = splhil(); 1163 while (((stat = hildevice->hil_stat) & HIL_DATA_RDY) == 0) 1164 ; 1165 c = hildevice->hil_data; 1166 splx(s); 1167 *statp = stat; 1168 return(c); 1169 } 1170 1171 /* 1172 * Recoginize and clear keyboard generated NMIs. 1173 * Returns 1 if it was ours, 0 otherwise. Note that we cannot use 1174 * send_hil_cmd() to issue the clear NMI command as that would actually 1175 * lower the priority to splimp() and it doesn't wait for the completion 1176 * of the command. Either of these conditions could result in the 1177 * interrupt reoccuring. Note that we issue the CNMT command twice. 1178 * This seems to be needed, once is not always enough!?! 1179 */ 1180 kbdnmi() 1181 { 1182 register struct hilloop *hilp = &hil0; /* XXX */ 1183 1184 if ((*KBDNMISTAT & KBDNMI) == 0) 1185 return(0); 1186 HILWAIT(hilp->hl_addr); 1187 hilp->hl_addr->hil_cmd = HIL_CNMT; 1188 HILWAIT(hilp->hl_addr); 1189 hilp->hl_addr->hil_cmd = HIL_CNMT; 1190 HILWAIT(hilp->hl_addr); 1191 return(1); 1192 } 1193 1194 #define HILSECURITY 0x33 1195 #define HILIDENTIFY 0x03 1196 #define HILSCBIT 0x04 1197 1198 /* 1199 * Called at boot time to print out info about interesting devices 1200 */ 1201 hilinfo(hilp) 1202 register struct hilloop *hilp; 1203 { 1204 register int id, len; 1205 register struct kbdmap *km; 1206 1207 /* 1208 * Keyboard info. 1209 */ 1210 if (hilp->hl_kbddev) { 1211 printf("hil%d: ", hilp->hl_kbddev); 1212 for (km = kbd_map; km->kbd_code; km++) 1213 if (km->kbd_code == hilp->hl_kbdlang) { 1214 printf("%s ", km->kbd_desc); 1215 break; 1216 } 1217 printf("keyboard\n"); 1218 } 1219 /* 1220 * ID module. 1221 * Attempt to locate the first ID module and print out its 1222 * security code. Is this a good idea?? 1223 */ 1224 id = hiliddev(hilp); 1225 if (id) { 1226 hilp->hl_cmdbp = hilp->hl_cmdbuf; 1227 hilp->hl_cmddev = id; 1228 send_hildev_cmd(hilp, id, HILSECURITY); 1229 len = hilp->hl_cmdbp - hilp->hl_cmdbuf; 1230 hilp->hl_cmdbp = hilp->hl_cmdbuf; 1231 hilp->hl_cmddev = 0; 1232 printf("hil%d: security code", id); 1233 for (id = 0; id < len; id++) 1234 printf(" %x", hilp->hl_cmdbuf[id]); 1235 while (id++ < 16) 1236 printf(" 0"); 1237 printf("\n"); 1238 } 1239 } 1240 1241 #define HILAR1 0x3E 1242 #define HILAR2 0x3F 1243 1244 /* 1245 * Called after the loop has reconfigured. Here we need to: 1246 * - determine how many devices are on the loop 1247 * (some may have been added or removed) 1248 * - locate the ITE keyboard (if any) and ensure 1249 * that it is in the proper state (raw or cooked) 1250 * and is set to use the proper language mapping table 1251 * - ensure all other keyboards are raw 1252 * Note that our device state is now potentially invalid as 1253 * devices may no longer be where they were. What we should 1254 * do here is either track where the devices went and move 1255 * state around accordingly or, more simply, just mark all 1256 * devices as HIL_DERROR and don't allow any further use until 1257 * they are closed. This is a little too brutal for my tastes, 1258 * we prefer to just assume people won't move things around. 1259 */ 1260 hilconfig(hilp) 1261 register struct hilloop *hilp; 1262 { 1263 u_char db; 1264 int s; 1265 1266 s = splhil(); 1267 #ifdef DEBUG 1268 if (hildebug & HDB_CONFIG) { 1269 printf("hilconfig: reconfigured: "); 1270 send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db); 1271 printf("LPSTAT %x, ", db); 1272 send_hil_cmd(hilp->hl_addr, HIL_READLPCTRL, NULL, 0, &db); 1273 printf("LPCTRL %x, ", db); 1274 send_hil_cmd(hilp->hl_addr, HIL_READKBDSADR, NULL, 0, &db); 1275 printf("KBDSADR %x\n", db); 1276 hilreport(hilp); 1277 } 1278 #endif 1279 /* 1280 * Determine how many devices are on the loop. 1281 * Mark those as alive and real, all others as dead. 1282 */ 1283 db = 0; 1284 send_hil_cmd(hilp->hl_addr, HIL_READLPSTAT, NULL, 0, &db); 1285 hilp->hl_maxdev = db & LPS_DEVMASK; 1286 for (db = 1; db < NHILD; db++) { 1287 if (db <= hilp->hl_maxdev) 1288 hilp->hl_device[db].hd_flags |= HIL_ALIVE; 1289 else 1290 hilp->hl_device[db].hd_flags &= ~HIL_ALIVE; 1291 hilp->hl_device[db].hd_flags &= ~HIL_PSEUDO; 1292 } 1293 #ifdef DEBUG 1294 if (hildebug & (HDB_CONFIG|HDB_KEYBOARD)) 1295 printf("hilconfig: max device %d\n", hilp->hl_maxdev); 1296 #endif 1297 if (hilp->hl_maxdev == 0) { 1298 hilp->hl_kbddev = 0; 1299 splx(s); 1300 return; 1301 } 1302 /* 1303 * Find out where the keyboards are and record the ITE keyboard 1304 * (first one found). If no keyboards found, we are all done. 1305 */ 1306 db = 0; 1307 send_hil_cmd(hilp->hl_addr, HIL_READKBDSADR, NULL, 0, &db); 1308 #ifdef DEBUG 1309 if (hildebug & HDB_KEYBOARD) 1310 printf("hilconfig: keyboard: KBDSADR %x, old %d, new %d\n", 1311 db, hilp->hl_kbddev, ffs((int)db)); 1312 #endif 1313 hilp->hl_kbddev = ffs((int)db); 1314 if (hilp->hl_kbddev == 0) { 1315 splx(s); 1316 return; 1317 } 1318 /* 1319 * Determine if the keyboard should be cooked or raw and configure it. 1320 */ 1321 db = (hilp->hl_kbdflags & KBD_RAW) ? 0 : 1 << (hilp->hl_kbddev - 1); 1322 send_hil_cmd(hilp->hl_addr, HIL_WRITEKBDSADR, &db, 1, NULL); 1323 /* 1324 * Re-enable autorepeat in raw mode, cooked mode AR is not affected. 1325 */ 1326 if (hilp->hl_kbdflags & (KBD_AR1|KBD_AR2)) { 1327 db = (hilp->hl_kbdflags & KBD_AR1) ? HILAR1 : HILAR2; 1328 hilp->hl_cmddev = hilp->hl_kbddev; 1329 send_hildev_cmd(hilp, hilp->hl_kbddev, db); 1330 hilp->hl_cmddev = 0; 1331 } 1332 /* 1333 * Determine the keyboard language configuration, but don't 1334 * override a user-specified setting. 1335 */ 1336 db = 0; 1337 send_hil_cmd(hilp->hl_addr, HIL_READKBDLANG, NULL, 0, &db); 1338 #ifdef DEBUG 1339 if (hildebug & HDB_KEYBOARD) 1340 printf("hilconfig: language: old %x new %x\n", 1341 hilp->hl_kbdlang, db); 1342 #endif 1343 if (hilp->hl_kbdlang != KBD_SPECIAL) { 1344 struct kbdmap *km; 1345 1346 for (km = kbd_map; km->kbd_code; km++) 1347 if (km->kbd_code == db) { 1348 hilp->hl_kbdlang = db; 1349 /* XXX */ 1350 kbd_keymap = km->kbd_keymap; 1351 kbd_shiftmap = km->kbd_shiftmap; 1352 kbd_ctrlmap = km->kbd_ctrlmap; 1353 kbd_ctrlshiftmap = km->kbd_ctrlshiftmap; 1354 kbd_stringmap = km->kbd_stringmap; 1355 } 1356 } 1357 splx(s); 1358 } 1359 1360 hilreset(hilp) 1361 struct hilloop *hilp; 1362 { 1363 register struct hil_dev *hildevice = hilp->hl_addr; 1364 u_char db; 1365 1366 /* 1367 * Initialize the loop: reconfigure, don't report errors, 1368 * cook keyboards, and enable autopolling. 1369 */ 1370 db = LPC_RECONF | LPC_KBDCOOK | LPC_NOERROR | LPC_AUTOPOLL; 1371 send_hil_cmd(hildevice, HIL_WRITELPCTRL, &db, 1, NULL); 1372 /* 1373 * Delay one second for reconfiguration and then read the the 1374 * data register to clear the interrupt (if the loop reconfigured). 1375 */ 1376 DELAY(1000000); 1377 if (hildevice->hil_stat & HIL_DATA_RDY) 1378 db = hildevice->hil_data; 1379 /* 1380 * The HIL loop may have reconfigured. If so we proceed on, 1381 * if not we loop until a successful reconfiguration is reported 1382 * back to us. The HIL loop will continue to attempt forever. 1383 * Probably not very smart. 1384 */ 1385 do { 1386 send_hil_cmd(hildevice, HIL_READLPSTAT, NULL, 0, &db); 1387 } while ((db & (LPS_CONFFAIL|LPS_CONFGOOD)) == 0); 1388 /* 1389 * At this point, the loop should have reconfigured. 1390 * The reconfiguration interrupt has already called hilconfig() 1391 * so the keyboard has been determined. 1392 */ 1393 send_hil_cmd(hildevice, HIL_INTON, NULL, 0, NULL); 1394 } 1395 1396 hilbeep(hilp, bp) 1397 struct hilloop *hilp; 1398 register struct _hilbell *bp; 1399 { 1400 u_char buf[2]; 1401 1402 buf[0] = ~((bp->duration - 10) / 10); 1403 buf[1] = bp->frequency; 1404 send_hil_cmd(hilp->hl_addr, HIL_SETTONE, buf, 2, NULL); 1405 } 1406 1407 /* 1408 * Locate and return the address of the first ID module, 0 if none present. 1409 */ 1410 hiliddev(hilp) 1411 register struct hilloop *hilp; 1412 { 1413 register int i, len; 1414 1415 #ifdef DEBUG 1416 if (hildebug & HDB_IDMODULE) 1417 printf("hiliddev(%x): looking for idmodule...", hilp); 1418 #endif 1419 for (i = 1; i <= hilp->hl_maxdev; i++) { 1420 hilp->hl_cmdbp = hilp->hl_cmdbuf; 1421 hilp->hl_cmddev = i; 1422 send_hildev_cmd(hilp, i, HILIDENTIFY); 1423 /* 1424 * XXX: the final condition checks to ensure that the 1425 * device ID byte is in the range of the ID module (0x30-0x3F) 1426 */ 1427 len = hilp->hl_cmdbp - hilp->hl_cmdbuf; 1428 if (len > 1 && (hilp->hl_cmdbuf[1] & HILSCBIT) && 1429 (hilp->hl_cmdbuf[0] & 0xF0) == 0x30) { 1430 hilp->hl_cmdbp = hilp->hl_cmdbuf; 1431 hilp->hl_cmddev = i; 1432 send_hildev_cmd(hilp, i, HILSECURITY); 1433 break; 1434 } 1435 } 1436 hilp->hl_cmdbp = hilp->hl_cmdbuf; 1437 hilp->hl_cmddev = 0; 1438 #ifdef DEBUG 1439 if (hildebug & HDB_IDMODULE) 1440 if (i <= hilp->hl_maxdev) 1441 printf("found at %d\n", i); 1442 else 1443 printf("not found\n"); 1444 #endif 1445 return(i <= hilp->hl_maxdev ? i : 0); 1446 } 1447 1448 /* 1449 * Low level routines which actually talk to the 8042 chip. 1450 */ 1451 1452 /* 1453 * Send a command to the 8042 with zero or more bytes of data. 1454 * If rdata is non-null, wait for and return a byte of data. 1455 * We run at splimp() to make the transaction as atomic as 1456 * possible without blocking the clock (is this necessary?) 1457 */ 1458 send_hil_cmd(hildevice, cmd, data, dlen, rdata) 1459 register struct hil_dev *hildevice; 1460 u_char cmd, *data, dlen; 1461 u_char *rdata; 1462 { 1463 u_char status; 1464 int s = splimp(); 1465 1466 HILWAIT(hildevice); 1467 hildevice->hil_cmd = cmd; 1468 while (dlen--) { 1469 HILWAIT(hildevice); 1470 hildevice->hil_data = *data++; 1471 } 1472 if (rdata) { 1473 do { 1474 HILDATAWAIT(hildevice); 1475 status = hildevice->hil_stat; 1476 *rdata = hildevice->hil_data; 1477 } while (((status >> HIL_SSHIFT) & HIL_SMASK) != HIL_68K); 1478 } 1479 splx(s); 1480 } 1481 1482 /* 1483 * Send a command to a device on the loop. 1484 * Since only one command can be active on the loop at any time, 1485 * we must ensure that we are not interrupted during this process. 1486 * Hence we mask interrupts to prevent potential access from most 1487 * interrupt routines and turn off auto-polling to disable the 1488 * internally generated poll commands. 1489 * 1490 * splhigh is extremely conservative but insures atomic operation, 1491 * splimp (clock only interrupts) seems to be good enough in practice. 1492 */ 1493 send_hildev_cmd(hilp, device, cmd) 1494 register struct hilloop *hilp; 1495 char device, cmd; 1496 { 1497 register struct hil_dev *hildevice = hilp->hl_addr; 1498 u_char status, c; 1499 int s = splimp(); 1500 1501 polloff(hildevice); 1502 1503 /* 1504 * Transfer the command and device info to the chip 1505 */ 1506 HILWAIT(hildevice); 1507 hildevice->hil_cmd = HIL_STARTCMD; 1508 HILWAIT(hildevice); 1509 hildevice->hil_data = 8 + device; 1510 HILWAIT(hildevice); 1511 hildevice->hil_data = cmd; 1512 HILWAIT(hildevice); 1513 hildevice->hil_data = HIL_TIMEOUT; 1514 /* 1515 * Trigger the command and wait for completion 1516 */ 1517 HILWAIT(hildevice); 1518 hildevice->hil_cmd = HIL_TRIGGER; 1519 hilp->hl_cmddone = FALSE; 1520 do { 1521 HILDATAWAIT(hildevice); 1522 status = hildevice->hil_stat; 1523 c = hildevice->hil_data; 1524 hil_process_int(status, c); 1525 } while (!hilp->hl_cmddone); 1526 1527 pollon(hildevice); 1528 splx(s); 1529 } 1530 1531 /* 1532 * Turn auto-polling off and on. 1533 * Also disables and enable auto-repeat. Why? 1534 */ 1535 polloff(hildevice) 1536 register struct hil_dev *hildevice; 1537 { 1538 register char db; 1539 1540 /* 1541 * Turn off auto repeat 1542 */ 1543 HILWAIT(hildevice); 1544 hildevice->hil_cmd = HIL_SETARR; 1545 HILWAIT(hildevice); 1546 hildevice->hil_data = 0; 1547 /* 1548 * Turn off auto-polling 1549 */ 1550 HILWAIT(hildevice); 1551 hildevice->hil_cmd = HIL_READLPCTRL; 1552 HILDATAWAIT(hildevice); 1553 db = hildevice->hil_data; 1554 db &= ~LPC_AUTOPOLL; 1555 HILWAIT(hildevice); 1556 hildevice->hil_cmd = HIL_WRITELPCTRL; 1557 HILWAIT(hildevice); 1558 hildevice->hil_data = db; 1559 /* 1560 * Must wait til polling is really stopped 1561 */ 1562 do { 1563 HILWAIT(hildevice); 1564 hildevice->hil_cmd = HIL_READBUSY; 1565 HILDATAWAIT(hildevice); 1566 db = hildevice->hil_data; 1567 } while (db & BSY_LOOPBUSY); 1568 } 1569 1570 pollon(hildevice) 1571 register struct hil_dev *hildevice; 1572 { 1573 register char db; 1574 1575 /* 1576 * Turn on auto polling 1577 */ 1578 HILWAIT(hildevice); 1579 hildevice->hil_cmd = HIL_READLPCTRL; 1580 HILDATAWAIT(hildevice); 1581 db = hildevice->hil_data; 1582 db |= LPC_AUTOPOLL; 1583 HILWAIT(hildevice); 1584 hildevice->hil_cmd = HIL_WRITELPCTRL; 1585 HILWAIT(hildevice); 1586 hildevice->hil_data = db; 1587 /* 1588 * Turn on auto repeat 1589 */ 1590 HILWAIT(hildevice); 1591 hildevice->hil_cmd = HIL_SETARR; 1592 HILWAIT(hildevice); 1593 hildevice->hil_data = ar_format(KBD_ARR); 1594 } 1595 1596 #ifdef DEBUG 1597 printhilpollbuf(hilp) 1598 register struct hilloop *hilp; 1599 { 1600 register u_char *cp; 1601 register int i, len; 1602 1603 cp = hilp->hl_pollbuf; 1604 len = hilp->hl_pollbp - cp; 1605 for (i = 0; i < len; i++) 1606 printf("%x ", hilp->hl_pollbuf[i]); 1607 printf("\n"); 1608 } 1609 1610 printhilcmdbuf(hilp) 1611 register struct hilloop *hilp; 1612 { 1613 register u_char *cp; 1614 register int i, len; 1615 1616 cp = hilp->hl_cmdbuf; 1617 len = hilp->hl_cmdbp - cp; 1618 for (i = 0; i < len; i++) 1619 printf("%x ", hilp->hl_cmdbuf[i]); 1620 printf("\n"); 1621 } 1622 1623 hilreport(hilp) 1624 register struct hilloop *hilp; 1625 { 1626 register int i, len; 1627 int s = splhil(); 1628 1629 for (i = 1; i <= hilp->hl_maxdev; i++) { 1630 hilp->hl_cmdbp = hilp->hl_cmdbuf; 1631 hilp->hl_cmddev = i; 1632 send_hildev_cmd(hilp, i, HILIDENTIFY); 1633 printf("hil%d: id: ", i); 1634 printhilcmdbuf(hilp); 1635 len = hilp->hl_cmdbp - hilp->hl_cmdbuf; 1636 if (len > 1 && (hilp->hl_cmdbuf[1] & HILSCBIT)) { 1637 hilp->hl_cmdbp = hilp->hl_cmdbuf; 1638 hilp->hl_cmddev = i; 1639 send_hildev_cmd(hilp, i, HILSECURITY); 1640 printf("hil%d: sc: ", i); 1641 printhilcmdbuf(hilp); 1642 } 1643 } 1644 hilp->hl_cmdbp = hilp->hl_cmdbuf; 1645 hilp->hl_cmddev = 0; 1646 splx(s); 1647 } 1648 #endif 1649