sys/dev/misc/vkbd/vkbd.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 | /* * vkbd.c */ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2004 Maksim Yevmenkin <m_evmenkin@yahoo.com> * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: vkbd.c,v 1.20 2004/11/15 23:53:30 max Exp $ */ #include "opt_kbd.h" #include <sys/param.h> #include <sys/conf.h> #include <sys/devfs.h> #include <sys/device.h> #include <sys/eventhandler.h> #include <sys/fcntl.h> #include <sys/kbio.h> #include <sys/kernel.h> #include <sys/limits.h> #include <sys/lock.h> #include <sys/malloc.h> #include <sys/module.h> #include <sys/mutex.h> #include <sys/poll.h> #include <sys/proc.h> #include <sys/queue.h> #include <sys/systm.h> #include <sys/taskqueue.h> #include <sys/uio.h> #include <dev/misc/kbd/kbdreg.h> #include <dev/misc/kbd/kbdtables.h> #include <dev/misc/vkbd/vkbd_var.h> #define DEVICE_NAME "vkbdctl" #define KEYBOARD_NAME "vkbd" MALLOC_DECLARE(M_VKBD); MALLOC_DEFINE(M_VKBD, KEYBOARD_NAME, "Virtual AT keyboard"); DEVFS_DEFINE_CLONE_BITMAP(vkbd); /***************************************************************************** ***************************************************************************** ** Keyboard state ***************************************************************************** *****************************************************************************/ /* * XXX * For now rely on Giant mutex to protect our data structures. * Just like the rest of keyboard drivers and syscons(4) do. */ #if 0 /* not yet */ #define VKBD_LOCK_DECL struct mtx ks_lock #define VKBD_LOCK_INIT(s) mtx_init(&(s)->ks_lock, "vkbd_lock", NULL, MTX_DEF|MTX_RECURSE) #define VKBD_LOCK_DESTROY(s) mtx_destroy(&(s)->ks_lock) #define VKBD_LOCK(s) mtx_lock(&(s)->ks_lock) #define VKBD_UNLOCK(s) mtx_unlock(&(s)->ks_lock) #define VKBD_LOCK_ASSERT(s, w) mtx_assert(&(s)->ks_lock, w) #define VKBD_SLEEP(s, f, d, t) \ msleep(&(s)->f, &(s)->ks_lock, PCATCH | PZERO, d, t) #else #define VKBD_LOCK_DECL #define VKBD_LOCK_INIT(s) #define VKBD_LOCK_DESTROY(s) #define VKBD_LOCK(s) #define VKBD_UNLOCK(s) #define VKBD_LOCK_ASSERT(s, w) #define VKBD_SLEEP(s, f, d, t) tsleep(&(s)->f, PCATCH | 0, d, t) #endif #define VKBD_KEYBOARD(d) \ kbd_get_keyboard(kbd_find_keyboard(KEYBOARD_NAME, dev2unit(d))) static int kbdelays[] = { 250, 500, 750, 1000 }; static int kbrates[] = { 34, 38, 42, 46, 50, 55, 59, 63, 68, 76, 84, 92, 100, 110, 118, 126, 136, 152, 168, 184, 200, 220, 236, 252, 272, 304, 336, 368, 400, 440, 472, 504 }; /* vkbd queue */ struct vkbd_queue { int q[VKBD_Q_SIZE]; /* queue */ int head; /* index of the first code */ int tail; /* index of the last code */ int cc; /* number of codes in queue */ }; typedef struct vkbd_queue vkbd_queue_t; /* vkbd state */ struct vkbd_state { struct cdev *ks_dev; /* control device */ struct kqinfo ks_rsel; /* select(2) */ struct kqinfo ks_wsel; vkbd_queue_t ks_inq; /* input key codes queue */ struct task ks_task; /* interrupt task */ int ks_flags; /* flags */ #define OPEN (1 << 0) /* control device is open */ #define COMPOSE (1 << 1) /* compose flag */ #define STATUS (1 << 2) /* status has changed */ #define TASK (1 << 3) /* interrupt task queued */ #define READ (1 << 4) /* read pending */ #define WRITE (1 << 5) /* write pending */ int ks_mode; /* K_XLATE, K_RAW, K_CODE */ int ks_polling; /* polling flag */ int ks_state; /* shift/lock key state */ int ks_accents; /* accent key index (> 0) */ u_int ks_composed_char; /* composed char code */ u_char ks_prefix; /* AT scan code prefix */ VKBD_LOCK_DECL; }; typedef struct vkbd_state vkbd_state_t; /***************************************************************************** ***************************************************************************** ** Character device ***************************************************************************** *****************************************************************************/ static int vkbd_dev_clone (struct dev_clone_args *ap); static d_open_t vkbd_dev_open; static d_close_t vkbd_dev_close; static d_read_t vkbd_dev_read; static d_write_t vkbd_dev_write; static d_ioctl_t vkbd_dev_ioctl; #if 0 static d_poll_t vkbd_dev_poll; #endif static d_kqfilter_t vkbd_dev_kqfilter; static void vkbd_dev_intr(void *, int); static void vkbd_status_changed(vkbd_state_t *); static int vkbd_data_ready(vkbd_state_t *); static int vkbd_data_read(vkbd_state_t *, int); static int vkbd_filter_wr_event(struct knote *, long); static int vkbd_filter_rd_event(struct knote *, long); static void vkbd_filter_wr_detach(struct knote *); static void vkbd_filter_rd_detach(struct knote *); static struct dev_ops vkbd_dev_cdevsw = { { "vkbd", 0, 0 }, .d_open = vkbd_dev_open, .d_close = vkbd_dev_close, .d_read = vkbd_dev_read, .d_write = vkbd_dev_write, .d_ioctl = vkbd_dev_ioctl, .d_kqfilter = vkbd_dev_kqfilter, #if 0 .d_poll = vkbd_dev_poll, #endif }; static int vkbd_refcnt = 0; /* module reference count */ #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 static struct clonedevs *vkbd_dev_clones = NULL; /* Clone device */ static void vkbd_dev_clone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev) { int unit; if (*dev != NULL) return; if (strcmp(name, DEVICE_NAME) == 0) unit = -1; else if (dev_stdclone(name, NULL, DEVICE_NAME, &unit) != 1) return; /* don't recognize the name */ /* find any existing device, or allocate new unit number */ if (clone_create(&vkbd_dev_clones, &vkbd_dev_cdevsw, &unit, dev, 0)) *dev = make_dev_credf(MAKEDEV_REF, &vkbd_dev_cdevsw, unit, cred, UID_ROOT, GID_WHEEL, 0600, DEVICE_NAME "%d", unit); else dev_ref(*dev); } #endif static int vkbd_dev_clone(struct dev_clone_args *ap) { int unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(vkbd), 0); ap->a_dev = make_only_dev(&vkbd_dev_cdevsw, unit, UID_ROOT, GID_WHEEL, 0600, "%s%d", DEVICE_NAME, unit); return 0; } /* Open device */ static int vkbd_dev_open(struct dev_open_args *ap) { struct cdev *dev = ap->a_head.a_dev; int unit = dev2unit(dev), error; keyboard_switch_t *sw = NULL; keyboard_t *kbd = NULL; vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; /* XXX FIXME: dev->si_drv1 locking */ if (state == NULL) { if ((sw = kbd_get_switch(KEYBOARD_NAME)) == NULL) return (ENXIO); if ((error = (*sw->probe)(unit, NULL, 0)) != 0 || (error = (*sw->init)(unit, &kbd, NULL, 0)) != 0) return (error); state = (vkbd_state_t *) kbd->kb_data; if ((error = (*sw->enable)(kbd)) != 0) { (*sw->term)(kbd); return (error); } #ifdef KBD_INSTALL_CDEV if ((error = kbd_attach(kbd)) != 0) { (*sw->disable)(kbd); (*sw->term)(kbd); return (error); } #endif /* def KBD_INSTALL_CDEV */ dev->si_drv1 = kbd->kb_data; } VKBD_LOCK(state); if (state->ks_flags & OPEN) { VKBD_UNLOCK(state); return (EBUSY); } state->ks_flags |= OPEN; state->ks_dev = dev; VKBD_UNLOCK(state); vkbd_refcnt++; return (0); } /* Close device */ static int vkbd_dev_close(struct dev_close_args *ap) { vkbd_state_t *state = NULL; struct cdev *dev = ap->a_head.a_dev; keyboard_t *kbd = VKBD_KEYBOARD(dev); if (kbd == NULL) return (ENXIO); if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); /* wait for interrupt task */ while (state->ks_flags & TASK) VKBD_SLEEP(state, ks_task, "vkbdc", 0); /* wakeup poll()ers */ KNOTE(&state->ks_rsel.ki_note, 0); KNOTE(&state->ks_wsel.ki_note, 0); state->ks_flags &= ~OPEN; state->ks_dev = NULL; state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; VKBD_UNLOCK(state); kbd_disable(kbd); #ifdef KBD_INSTALL_CDEV kbd_detach(kbd); #endif /* def KBD_INSTALL_CDEV */ kbd_term(kbd); /* XXX FIXME: dev->si_drv1 locking */ dev->si_drv1 = NULL; destroy_dev(dev); --vkbd_refcnt; if (vkbd_refcnt < 0) vkbd_refcnt = 0; return (0); } /* Read status */ static int vkbd_dev_read(struct dev_read_args *ap) { struct cdev *dev = ap->a_head.a_dev; keyboard_t *kbd = VKBD_KEYBOARD(dev); struct uio *uio = ap->a_uio; vkbd_state_t *state = NULL; vkbd_status_t status; int flag = ap->a_ioflag; int error; if (kbd == NULL) return (ENXIO); if (uio->uio_resid != sizeof(status)) return (EINVAL); if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); if (state->ks_flags & READ) { VKBD_UNLOCK(state); return (EALREADY); } state->ks_flags |= READ; again: if (state->ks_flags & STATUS) { state->ks_flags &= ~STATUS; status.mode = state->ks_mode; status.leds = KBD_LED_VAL(kbd); status.lock = state->ks_state & LOCK_MASK; status.delay = kbd->kb_delay1; status.rate = kbd->kb_delay2; bzero(status.reserved, sizeof(status.reserved)); error = uiomove((caddr_t)&status, sizeof(status), uio); } else { if (flag & O_NONBLOCK) { error = EWOULDBLOCK; goto done; } error = VKBD_SLEEP(state, ks_flags, "vkbdr", 0); if (error != 0) goto done; goto again; } done: state->ks_flags &= ~READ; VKBD_UNLOCK(state); return (error); } /* Write scancodes */ static int vkbd_dev_write(struct dev_write_args *ap) { struct cdev *dev = ap->a_head.a_dev; int flag = ap->a_ioflag; struct uio *uio = ap->a_uio; keyboard_t *kbd = VKBD_KEYBOARD(dev); vkbd_state_t *state = NULL; vkbd_queue_t *q = NULL; int error, avail, bytes; if (kbd == NULL) return (ENXIO); if (uio->uio_resid <= 0) return (EINVAL); if (kbd->kb_data == NULL || kbd->kb_data != dev->si_drv1) panic("%s: kbd->kb_data != dev->si_drv1\n", __func__); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); if (state->ks_flags & WRITE) { VKBD_UNLOCK(state); return (EALREADY); } state->ks_flags |= WRITE; error = 0; q = &state->ks_inq; while (uio->uio_resid >= sizeof(q->q[0])) { if (q->head == q->tail) { if (q->cc == 0) avail = nitems(q->q) - q->head; else avail = 0; /* queue must be full */ } else if (q->head < q->tail) avail = nitems(q->q) - q->tail; else avail = q->head - q->tail; if (avail == 0) { if (flag & O_NONBLOCK) { error = EWOULDBLOCK; break; } error = VKBD_SLEEP(state, ks_inq, "vkbdw", 0); if (error != 0) break; } else { bytes = avail * sizeof(q->q[0]); if (bytes > uio->uio_resid) { avail = uio->uio_resid / sizeof(q->q[0]); bytes = avail * sizeof(q->q[0]); } error = uiomove((caddr_t) &q->q[q->tail], bytes, uio); if (error != 0) break; q->cc += avail; q->tail += avail; if (q->tail == nitems(q->q)) q->tail = 0; /* queue interrupt task if needed */ if (!(state->ks_flags & TASK) && taskqueue_enqueue(taskqueue_swi, &state->ks_task) == 0) state->ks_flags |= TASK; } } state->ks_flags &= ~WRITE; VKBD_UNLOCK(state); return (error); } /* Process ioctl */ static int vkbd_dev_ioctl(struct dev_ioctl_args *ap) { u_long cmd = ap->a_cmd; caddr_t data = ap->a_data; struct cdev *dev = ap->a_head.a_dev; keyboard_t *kbd = VKBD_KEYBOARD(dev); return ((kbd == NULL)? ENXIO : kbd_ioctl(kbd, cmd, data)); } static void vkbd_filter_rd_detach(struct knote *kn) { cdev_t dev = (cdev_t) kn->kn_hook; vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; knote_remove(&state->ks_rsel.ki_note, kn); } static void vkbd_filter_wr_detach(struct knote *kn) { cdev_t dev = (cdev_t) kn->kn_hook; vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; knote_remove(&state->ks_wsel.ki_note, kn); } static int vkbd_filter_rd_event(struct knote *kn, long hint) { cdev_t dev = (cdev_t) kn->kn_hook; vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; if (state->ks_flags & STATUS) return 1; return 0; } static int vkbd_filter_wr_event(struct knote *kn, long hint) { cdev_t dev = (cdev_t) kn->kn_hook; vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; vkbd_queue_t *q = NULL; q = &state->ks_inq; if (q->cc < nitems(q->q)) return 1; return 0; } static struct filterops vkbd_rd_filterops = { .f_detach = vkbd_filter_rd_detach, .f_event = vkbd_filter_rd_event, }; static struct filterops vkbd_wr_filterops = { .f_detach = vkbd_filter_wr_detach, .f_event = vkbd_filter_wr_event, }; static int vkbd_dev_kqfilter(struct dev_kqfilter_args *ap) { cdev_t dev = ap->a_head.a_dev; vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; struct knote *kn = ap->a_kn; struct klist *klist; ap->a_result = 0; switch (kn->kn_filter) { case EVFILT_READ: kn->kn_fop = &vkbd_rd_filterops; kn->kn_hook = (caddr_t)dev; klist = &state->ks_rsel.ki_note; break; case EVFILT_WRITE: kn->kn_fop = &vkbd_wr_filterops; kn->kn_hook = (caddr_t)dev; klist = &state->ks_wsel.ki_note; break; default: ap->a_result = EOPNOTSUPP; return (0); } knote_insert(klist, kn); return (0); } #if 0 /* Poll device */ static int vkbd_dev_poll(struct cdev *dev, int events, struct thread *td) { vkbd_state_t *state = (vkbd_state_t *) dev->si_drv1; vkbd_queue_t *q = NULL; int revents = 0; if (state == NULL) return (ENXIO); VKBD_LOCK(state); q = &state->ks_inq; if (events & (POLLIN | POLLRDNORM)) { if (state->ks_flags & STATUS) revents |= events & (POLLIN | POLLRDNORM); else selrecord(td, &state->ks_rsel); } if (events & (POLLOUT | POLLWRNORM)) { if (q->cc < nitems(q->q)) revents |= events & (POLLOUT | POLLWRNORM); else selrecord(td, &state->ks_wsel); } VKBD_UNLOCK(state); return (revents); } #endif /* Interrupt handler */ void vkbd_dev_intr(void *xkbd, int pending) { keyboard_t *kbd = (keyboard_t *) xkbd; vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; kbd_intr(kbd, NULL); VKBD_LOCK(state); state->ks_flags &= ~TASK; wakeup(&state->ks_task); VKBD_UNLOCK(state); } /* Set status change flags */ static void vkbd_status_changed(vkbd_state_t *state) { VKBD_LOCK_ASSERT(state, MA_OWNED); if (!(state->ks_flags & STATUS)) { state->ks_flags |= STATUS; KNOTE(&state->ks_rsel.ki_note, 0); wakeup(&state->ks_flags); } } /* Check if we have data in the input queue */ static int vkbd_data_ready(vkbd_state_t *state) { VKBD_LOCK_ASSERT(state, MA_OWNED); return (state->ks_inq.cc > 0); } /* Read one code from the input queue */ static int vkbd_data_read(vkbd_state_t *state, int wait) { vkbd_queue_t *q = &state->ks_inq; int c; VKBD_LOCK_ASSERT(state, MA_OWNED); if (q->cc == 0) return (-1); /* get first code from the queue */ q->cc --; c = q->q[q->head ++]; if (q->head == nitems(q->q)) q->head = 0; /* wakeup ks_inq writers/poll()ers */ KNOTE(&state->ks_wsel.ki_note, 0); wakeup(q); return (c); } /**************************************************************************** **************************************************************************** ** Keyboard driver **************************************************************************** ****************************************************************************/ static int vkbd_configure(int flags); static kbd_probe_t vkbd_probe; static kbd_init_t vkbd_init; static kbd_term_t vkbd_term; static kbd_intr_t vkbd_intr; static kbd_test_if_t vkbd_test_if; static kbd_enable_t vkbd_enable; static kbd_disable_t vkbd_disable; static kbd_read_t vkbd_read; static kbd_check_t vkbd_check; static kbd_read_char_t vkbd_read_char; static kbd_check_char_t vkbd_check_char; static kbd_ioctl_t vkbd_ioctl; static kbd_lock_t vkbd_lock; static void vkbd_clear_state_locked(vkbd_state_t *state); static kbd_clear_state_t vkbd_clear_state; static kbd_get_state_t vkbd_get_state; static kbd_set_state_t vkbd_set_state; static kbd_poll_mode_t vkbd_poll; static keyboard_switch_t vkbdsw = { .probe = vkbd_probe, .init = vkbd_init, .term = vkbd_term, .intr = vkbd_intr, .test_if = vkbd_test_if, .enable = vkbd_enable, .disable = vkbd_disable, .read = vkbd_read, .check = vkbd_check, .read_char = vkbd_read_char, .check_char = vkbd_check_char, .ioctl = vkbd_ioctl, .lock = vkbd_lock, .clear_state = vkbd_clear_state, .get_state = vkbd_get_state, .set_state = vkbd_set_state, .poll = vkbd_poll, }; static int typematic(int delay, int rate); static int typematic_delay(int delay); static int typematic_rate(int rate); /* Return the number of found keyboards */ static int vkbd_configure(int flags) { return (1); } /* Detect a keyboard */ static int vkbd_probe(int unit, void *arg, int flags) { return (0); } /* Reset and initialize the keyboard (stolen from atkbd.c) */ static int vkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags) { keyboard_t *kbd = NULL; vkbd_state_t *state = NULL; keymap_t *keymap = NULL; accentmap_t *accmap = NULL; fkeytab_t *fkeymap = NULL; int fkeymap_size, delay[2]; int error, needfree; if (*kbdp == NULL) { *kbdp = kbd = kmalloc(sizeof(*kbd), M_VKBD, M_NOWAIT | M_ZERO); state = kmalloc(sizeof(*state), M_VKBD, M_NOWAIT | M_ZERO); keymap = kmalloc(sizeof(key_map), M_VKBD, M_NOWAIT); accmap = kmalloc(sizeof(accent_map), M_VKBD, M_NOWAIT); fkeymap = kmalloc(sizeof(fkey_tab), M_VKBD, M_NOWAIT); fkeymap_size = sizeof(fkey_tab)/sizeof(fkey_tab[0]); needfree = 1; if ((kbd == NULL) || (state == NULL) || (keymap == NULL) || (accmap == NULL) || (fkeymap == NULL)) { error = ENOMEM; goto bad; } VKBD_LOCK_INIT(state); state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; TASK_INIT(&state->ks_task, 0, vkbd_dev_intr, (void *) kbd); } else if (KBD_IS_INITIALIZED(*kbdp) && KBD_IS_CONFIGURED(*kbdp)) { return (0); } else { kbd = *kbdp; state = (vkbd_state_t *) kbd->kb_data; keymap = kbd->kb_keymap; accmap = kbd->kb_accentmap; fkeymap = kbd->kb_fkeytab; fkeymap_size = kbd->kb_fkeytab_size; needfree = 0; } if (!KBD_IS_PROBED(kbd)) { kbd_init_struct(kbd, KEYBOARD_NAME, KB_OTHER, unit, flags, 0, 0, 0); bcopy(&key_map, keymap, sizeof(key_map)); bcopy(&accent_map, accmap, sizeof(accent_map)); bcopy(fkey_tab, fkeymap, imin(fkeymap_size*sizeof(fkeymap[0]), sizeof(fkey_tab))); kbd_set_maps(kbd, keymap, accmap, fkeymap, fkeymap_size); kbd->kb_data = (void *)state; KBD_FOUND_DEVICE(kbd); KBD_PROBE_DONE(kbd); VKBD_LOCK(state); vkbd_clear_state_locked(state); state->ks_mode = K_XLATE; /* FIXME: set the initial value for lock keys in ks_state */ VKBD_UNLOCK(state); } if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) { kbd->kb_config = flags & ~KB_CONF_PROBE_ONLY; vkbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state); delay[0] = kbd->kb_delay1; delay[1] = kbd->kb_delay2; vkbd_ioctl(kbd, KDSETREPEAT, (caddr_t)delay); KBD_INIT_DONE(kbd); } if (!KBD_IS_CONFIGURED(kbd)) { if (kbd_register(kbd) < 0) { error = ENXIO; goto bad; } KBD_CONFIG_DONE(kbd); } return (0); bad: if (needfree) { if (state != NULL) kfree(state, M_VKBD); if (keymap != NULL) kfree(keymap, M_VKBD); if (accmap != NULL) kfree(accmap, M_VKBD); if (fkeymap != NULL) kfree(fkeymap, M_VKBD); if (kbd != NULL) { kfree(kbd, M_VKBD); *kbdp = NULL; /* insure ref doesn't leak to caller */ } } return (error); } /* Finish using this keyboard */ static int vkbd_term(keyboard_t *kbd) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; kbd_unregister(kbd); VKBD_LOCK_DESTROY(state); bzero(state, sizeof(*state)); kfree(state, M_VKBD); kfree(kbd->kb_keymap, M_VKBD); kfree(kbd->kb_accentmap, M_VKBD); kfree(kbd->kb_fkeytab, M_VKBD); kfree(kbd, M_VKBD); return (0); } /* Keyboard interrupt routine */ static int vkbd_intr(keyboard_t *kbd, void *arg) { int c; if (KBD_IS_ACTIVE(kbd) && KBD_IS_BUSY(kbd)) { /* let the callback function to process the input */ (*kbd->kb_callback.kc_func)(kbd, KBDIO_KEYINPUT, kbd->kb_callback.kc_arg); } else { /* read and discard the input; no one is waiting for input */ do { c = vkbd_read_char(kbd, FALSE); } while (c != NOKEY); } return (0); } /* Test the interface to the device */ static int vkbd_test_if(keyboard_t *kbd) { return (0); } /* * Enable the access to the device; until this function is called, * the client cannot read from the keyboard. */ static int vkbd_enable(keyboard_t *kbd) { KBD_ACTIVATE(kbd); return (0); } /* Disallow the access to the device */ static int vkbd_disable(keyboard_t *kbd) { KBD_DEACTIVATE(kbd); return (0); } /* Read one byte from the keyboard if it's allowed */ static int vkbd_read(keyboard_t *kbd, int wait) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; int c; VKBD_LOCK(state); c = vkbd_data_read(state, wait); VKBD_UNLOCK(state); if (c != -1) kbd->kb_count ++; return (KBD_IS_ACTIVE(kbd)? c : -1); } /* Check if data is waiting */ static int vkbd_check(keyboard_t *kbd) { vkbd_state_t *state = NULL; int ready; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); ready = vkbd_data_ready(state); VKBD_UNLOCK(state); return (ready); } /* Read char from the keyboard (stolen from atkbd.c) */ static u_int vkbd_read_char(keyboard_t *kbd, int wait) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; u_int action; int scancode, keycode; VKBD_LOCK(state); next_code: /* do we have a composed char to return? */ if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) { action = state->ks_composed_char; state->ks_composed_char = 0; if (action > UCHAR_MAX) { VKBD_UNLOCK(state); return (ERRKEY); } VKBD_UNLOCK(state); return (action); } /* see if there is something in the keyboard port */ scancode = vkbd_data_read(state, wait); if (scancode == -1) { VKBD_UNLOCK(state); return (NOKEY); } /* XXX FIXME: check for -1 if wait == 1! */ kbd->kb_count ++; /* return the byte as is for the K_RAW mode */ if (state->ks_mode == K_RAW) { VKBD_UNLOCK(state); return (scancode); } /* translate the scan code into a keycode */ keycode = scancode & 0x7F; switch (state->ks_prefix) { case 0x00: /* normal scancode */ switch(scancode) { case 0xB8: /* left alt (compose key) released */ if (state->ks_flags & COMPOSE) { state->ks_flags &= ~COMPOSE; if (state->ks_composed_char > UCHAR_MAX) state->ks_composed_char = 0; } break; case 0x38: /* left alt (compose key) pressed */ if (!(state->ks_flags & COMPOSE)) { state->ks_flags |= COMPOSE; state->ks_composed_char = 0; } break; case 0xE0: case 0xE1: state->ks_prefix = scancode; goto next_code; } break; case 0xE0: /* 0xE0 prefix */ state->ks_prefix = 0; switch (keycode) { case 0x1C: /* right enter key */ keycode = 0x59; break; case 0x1D: /* right ctrl key */ keycode = 0x5A; break; case 0x35: /* keypad divide key */ keycode = 0x5B; break; case 0x37: /* print scrn key */ keycode = 0x5C; break; case 0x38: /* right alt key (alt gr) */ keycode = 0x5D; break; case 0x46: /* ctrl-pause/break on AT 101 (see below) */ keycode = 0x68; break; case 0x47: /* grey home key */ keycode = 0x5E; break; case 0x48: /* grey up arrow key */ keycode = 0x5F; break; case 0x49: /* grey page up key */ keycode = 0x60; break; case 0x4B: /* grey left arrow key */ keycode = 0x61; break; case 0x4D: /* grey right arrow key */ keycode = 0x62; break; case 0x4F: /* grey end key */ keycode = 0x63; break; case 0x50: /* grey down arrow key */ keycode = 0x64; break; case 0x51: /* grey page down key */ keycode = 0x65; break; case 0x52: /* grey insert key */ keycode = 0x66; break; case 0x53: /* grey delete key */ keycode = 0x67; break; /* the following 3 are only used on the MS "Natural" keyboard */ case 0x5b: /* left Window key */ keycode = 0x69; break; case 0x5c: /* right Window key */ keycode = 0x6a; break; case 0x5d: /* menu key */ keycode = 0x6b; break; case 0x5e: /* power key */ keycode = 0x6d; break; case 0x5f: /* sleep key */ keycode = 0x6e; break; case 0x63: /* wake key */ keycode = 0x6f; break; default: /* ignore everything else */ goto next_code; } break; case 0xE1: /* 0xE1 prefix */ /* * The pause/break key on the 101 keyboard produces: * E1-1D-45 E1-9D-C5 * Ctrl-pause/break produces: * E0-46 E0-C6 (See above.) */ state->ks_prefix = 0; if (keycode == 0x1D) state->ks_prefix = 0x1D; goto next_code; /* NOT REACHED */ case 0x1D: /* pause / break */ state->ks_prefix = 0; if (keycode != 0x45) goto next_code; keycode = 0x68; break; } if (kbd->kb_type == KB_84) { switch (keycode) { case 0x37: /* *(numpad)/print screen */ if (state->ks_flags & SHIFTS) keycode = 0x5c; /* print screen */ break; case 0x45: /* num lock/pause */ if (state->ks_flags & CTLS) keycode = 0x68; /* pause */ break; case 0x46: /* scroll lock/break */ if (state->ks_flags & CTLS) keycode = 0x6c; /* break */ break; } } else if (kbd->kb_type == KB_101) { switch (keycode) { case 0x5c: /* print screen */ if (state->ks_flags & ALTS) keycode = 0x54; /* sysrq */ break; case 0x68: /* pause/break */ if (state->ks_flags & CTLS) keycode = 0x6c; /* break */ break; } } /* return the key code in the K_CODE mode */ if (state->ks_mode == K_CODE) { VKBD_UNLOCK(state); return (keycode | (scancode & 0x80)); } /* compose a character code */ if (state->ks_flags & COMPOSE) { switch (keycode | (scancode & 0x80)) { /* key pressed, process it */ case 0x47: case 0x48: case 0x49: /* keypad 7,8,9 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x40; if (state->ks_composed_char > UCHAR_MAX) { VKBD_UNLOCK(state); return (ERRKEY); } goto next_code; case 0x4B: case 0x4C: case 0x4D: /* keypad 4,5,6 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x47; if (state->ks_composed_char > UCHAR_MAX) { VKBD_UNLOCK(state); return (ERRKEY); } goto next_code; case 0x4F: case 0x50: case 0x51: /* keypad 1,2,3 */ state->ks_composed_char *= 10; state->ks_composed_char += keycode - 0x4E; if (state->ks_composed_char > UCHAR_MAX) { VKBD_UNLOCK(state); return (ERRKEY); } goto next_code; case 0x52: /* keypad 0 */ state->ks_composed_char *= 10; if (state->ks_composed_char > UCHAR_MAX) { VKBD_UNLOCK(state); return (ERRKEY); } goto next_code; /* key released, no interest here */ case 0xC7: case 0xC8: case 0xC9: /* keypad 7,8,9 */ case 0xCB: case 0xCC: case 0xCD: /* keypad 4,5,6 */ case 0xCF: case 0xD0: case 0xD1: /* keypad 1,2,3 */ case 0xD2: /* keypad 0 */ goto next_code; case 0x38: /* left alt key */ break; default: if (state->ks_composed_char > 0) { state->ks_flags &= ~COMPOSE; state->ks_composed_char = 0; VKBD_UNLOCK(state); return (ERRKEY); } break; } } /* keycode to key action */ action = genkbd_keyaction(kbd, keycode, scancode & 0x80, &state->ks_state, &state->ks_accents); if (action == NOKEY) goto next_code; VKBD_UNLOCK(state); return (action); } /* Check if char is waiting */ static int vkbd_check_char(keyboard_t *kbd) { vkbd_state_t *state = NULL; int ready; if (!KBD_IS_ACTIVE(kbd)) return (FALSE); state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0)) ready = TRUE; else ready = vkbd_data_ready(state); VKBD_UNLOCK(state); return (ready); } /* Some useful control functions (stolen from atkbd.c) */ static int vkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; int i; #ifdef COMPAT_FREEBSD6 int ival; #endif VKBD_LOCK(state); switch (cmd) { case KDGKBMODE: /* get keyboard mode */ *(int *)arg = state->ks_mode; break; #ifdef COMPAT_FREEBSD6 case _IO('K', 7): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBMODE: /* set keyboard mode */ switch (*(int *)arg) { case K_XLATE: if (state->ks_mode != K_XLATE) { /* make lock key state and LED state match */ state->ks_state &= ~LOCK_MASK; state->ks_state |= KBD_LED_VAL(kbd); vkbd_status_changed(state); } /* FALLTHROUGH */ case K_RAW: case K_CODE: if (state->ks_mode != *(int *)arg) { vkbd_clear_state_locked(state); state->ks_mode = *(int *)arg; vkbd_status_changed(state); } break; default: VKBD_UNLOCK(state); return (EINVAL); } break; case KDGETLED: /* get keyboard LED */ *(int *)arg = KBD_LED_VAL(kbd); break; #ifdef COMPAT_FREEBSD6 case _IO('K', 66): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSETLED: /* set keyboard LED */ /* NOTE: lock key state in ks_state won't be changed */ if (*(int *)arg & ~LOCK_MASK) { VKBD_UNLOCK(state); return (EINVAL); } i = *(int *)arg; /* replace CAPS LED with ALTGR LED for ALTGR keyboards */ if (state->ks_mode == K_XLATE && kbd->kb_keymap->n_keys > ALTGR_OFFSET) { if (i & ALKED) i |= CLKED; else i &= ~CLKED; } KBD_LED_VAL(kbd) = *(int *)arg; vkbd_status_changed(state); break; case KDGKBSTATE: /* get lock key state */ *(int *)arg = state->ks_state & LOCK_MASK; break; #ifdef COMPAT_FREEBSD6 case _IO('K', 20): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif case KDSKBSTATE: /* set lock key state */ if (*(int *)arg & ~LOCK_MASK) { VKBD_UNLOCK(state); return (EINVAL); } state->ks_state &= ~LOCK_MASK; state->ks_state |= *(int *)arg; vkbd_status_changed(state); VKBD_UNLOCK(state); /* set LEDs and quit */ return (vkbd_ioctl(kbd, KDSETLED, arg)); case KDSETREPEAT: /* set keyboard repeat rate (new interface) */ i = typematic(((int *)arg)[0], ((int *)arg)[1]); kbd->kb_delay1 = typematic_delay(i); kbd->kb_delay2 = typematic_rate(i); vkbd_status_changed(state); break; #ifdef COMPAT_FREEBSD6 case _IO('K', 67): ival = IOCPARM_IVAL(arg); arg = (caddr_t)&ival; /* FALLTHROUGH */ #endif #if 0 case KDSETRAD: /* set keyboard repeat rate (old interface) */ kbd->kb_delay1 = typematic_delay(*(int *)arg); kbd->kb_delay2 = typematic_rate(*(int *)arg); vkbd_status_changed(state); break; #endif case PIO_KEYMAP: /* set keyboard translation table */ case PIO_KEYMAPENT: /* set keyboard translation table entry */ case PIO_DEADKEYMAP: /* set accent key translation table */ #ifdef COMPAT_FREEBSD13 case OPIO_KEYMAP: /* set keyboard translation table (compat) */ case OPIO_DEADKEYMAP: /* set accent key translation table (compat) */ #endif /* COMPAT_FREEBSD13 */ state->ks_accents = 0; /* FALLTHROUGH */ default: VKBD_UNLOCK(state); return (genkbd_commonioctl(kbd, cmd, arg)); } VKBD_UNLOCK(state); return (0); } /* Lock the access to the keyboard */ static int vkbd_lock(keyboard_t *kbd, int lock) { return (1); /* XXX */ } /* Clear the internal state of the keyboard */ static void vkbd_clear_state_locked(vkbd_state_t *state) { VKBD_LOCK_ASSERT(state, MA_OWNED); state->ks_flags &= ~COMPOSE; state->ks_polling = 0; state->ks_state &= LOCK_MASK; /* preserve locking key state */ state->ks_accents = 0; state->ks_composed_char = 0; /* state->ks_prefix = 0; XXX */ /* flush ks_inq and wakeup writers/poll()ers */ state->ks_inq.head = state->ks_inq.tail = state->ks_inq.cc = 0; KNOTE(&state->ks_wsel.ki_note, 0); wakeup(&state->ks_inq); } static void vkbd_clear_state(keyboard_t *kbd) { vkbd_state_t *state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); vkbd_clear_state_locked(state); VKBD_UNLOCK(state); } /* Save the internal state */ static int vkbd_get_state(keyboard_t *kbd, void *buf, size_t len) { if (len == 0) return (sizeof(vkbd_state_t)); if (len < sizeof(vkbd_state_t)) return (-1); bcopy(kbd->kb_data, buf, sizeof(vkbd_state_t)); /* XXX locking? */ return (0); } /* Set the internal state */ static int vkbd_set_state(keyboard_t *kbd, void *buf, size_t len) { if (len < sizeof(vkbd_state_t)) return (ENOMEM); bcopy(buf, kbd->kb_data, sizeof(vkbd_state_t)); /* XXX locking? */ return (0); } /* Set polling */ static int vkbd_poll(keyboard_t *kbd, int on) { vkbd_state_t *state = NULL; state = (vkbd_state_t *) kbd->kb_data; VKBD_LOCK(state); if (on) state->ks_polling ++; else state->ks_polling --; VKBD_UNLOCK(state); return (0); } /* * Local functions */ static int typematic_delay(int i) { return (kbdelays[(i >> 5) & 3]); } static int typematic_rate(int i) { return (kbrates[i & 0x1f]); } static int typematic(int delay, int rate) { int value; int i; for (i = nitems(kbdelays) - 1; i > 0; i--) { if (delay >= kbdelays[i]) break; } value = i << 5; for (i = nitems(kbrates) - 1; i > 0; i--) { if (rate >= kbrates[i]) break; } value |= i; return (value); } /***************************************************************************** ***************************************************************************** ** Module ***************************************************************************** *****************************************************************************/ KEYBOARD_DRIVER(vkbd, vkbdsw, vkbd_configure); static int vkbd_modevent(module_t mod, int type, void *data) { static cdev_t dev = NULL; #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 static eventhandler_tag tag; #endif switch (type) { case MOD_LOAD: #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 clone_setup(&vkbd_dev_clones); tag = EVENTHANDLER_REGISTER(dev_clone, vkbd_dev_clone, 0, 1000); if (tag == NULL) { clone_cleanup(&vkbd_dev_clones); return (ENOMEM); } #endif dev = make_autoclone_dev(&vkbd_dev_cdevsw, &DEVFS_CLONE_BITMAP(vkbd), vkbd_dev_clone, UID_ROOT, GID_WHEEL, 0600, DEVICE_NAME); kbd_add_driver(&vkbd_kbd_driver); break; case MOD_UNLOAD: if (vkbd_refcnt > 0) return (EBUSY); kbd_delete_driver(&vkbd_kbd_driver); #if defined(__FreeBSD__) && __FreeBSD_version >= 500000 EVENTHANDLER_DEREGISTER(dev_clone, tag); clone_cleanup(&vkbd_dev_clones); #endif dev_ops_remove_all(&vkbd_dev_cdevsw); destroy_autoclone_dev(dev, &DEVFS_CLONE_BITMAP(vkbd)); break; default: return (EOPNOTSUPP); } return (0); } DEV_MODULE(vkbd, vkbd_modevent, NULL); |