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
|
/*-
* 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 as
* the first lines of this file unmodified.
* 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 AUTHORS ``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 AUTHORS 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: kbd.c,v 1.7 1999/05/30 14:55:24 phk Exp $
*/
#include "kbd.h"
#include "opt_kbd.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/tty.h>
#include <sys/poll.h>
#include <sys/vnode.h>
#include <sys/uio.h>
#include <machine/console.h>
#include <dev/kbd/kbdreg.h>
/* local arrays */
/*
* We need at least one entry each in order to initialize a keyboard
* for the kernel console. The arrays will be increased dynamically
* when necessary.
*/
static int keyboards = 1;
static keyboard_t *kbd_ini;
static keyboard_t **keyboard = &kbd_ini;
static keyboard_switch_t *kbdsw_ini;
keyboard_switch_t **kbdsw = &kbdsw_ini;
#ifdef KBD_INSTALL_CDEV
static struct cdevsw *kbdcdevsw_ini;
static struct cdevsw **kbdcdevsw = &kbdcdevsw_ini;
#endif
#define ARRAY_DELTA 4
static int
kbd_realloc_array(void)
{
keyboard_t **new_kbd;
keyboard_switch_t **new_kbdsw;
#ifdef KBD_INSTALL_CDEV
struct cdevsw **new_cdevsw;
#endif
int newsize;
int s;
s = spltty();
newsize = ((keyboards + ARRAY_DELTA)/ARRAY_DELTA)*ARRAY_DELTA;
new_kbd = malloc(sizeof(*new_kbd)*newsize, M_DEVBUF, M_NOWAIT);
if (new_kbd == NULL) {
splx(s);
return ENOMEM;
}
new_kbdsw = malloc(sizeof(*new_kbdsw)*newsize, M_DEVBUF, M_NOWAIT);
if (new_kbdsw == NULL) {
free(new_kbd, M_DEVBUF);
splx(s);
return ENOMEM;
}
#ifdef KBD_INSTALL_CDEV
new_cdevsw = malloc(sizeof(*new_cdevsw)*newsize, M_DEVBUF, M_NOWAIT);
if (new_cdevsw == NULL) {
free(new_kbd, M_DEVBUF);
free(new_kbdsw, M_DEVBUF);
splx(s);
return ENOMEM;
}
#endif
bzero(new_kbd, sizeof(*new_kbd)*newsize);
bzero(new_kbdsw, sizeof(*new_kbdsw)*newsize);
bcopy(keyboard, new_kbd, sizeof(*keyboard)*keyboards);
bcopy(kbdsw, new_kbdsw, sizeof(*kbdsw)*keyboards);
#ifdef KBD_INSTALL_CDEV
bzero(new_cdevsw, sizeof(*new_cdevsw)*newsize);
bcopy(kbdcdevsw, new_cdevsw, sizeof(*kbdcdevsw)*keyboards);
#endif
if (keyboards > 1) {
free(keyboard, M_DEVBUF);
free(kbdsw, M_DEVBUF);
#ifdef KBD_INSTALL_CDEV
free(kbdcdevsw, M_DEVBUF);
#endif
}
keyboard = new_kbd;
kbdsw = new_kbdsw;
#ifdef KBD_INSTALL_CDEV
kbdcdevsw = new_cdevsw;
#endif
keyboards = newsize;
splx(s);
if (bootverbose)
printf("kbd: new array size %d\n", keyboards);
return 0;
}
/*
* Low-level keyboard driver functions
* Keyboard subdrivers, such as the AT keyboard driver and the USB keyboard
* driver, call these functions to initialize the keyboard_t structure
* and register it to the virtual keyboard driver `kbd'.
*/
/* initialize the keyboard_t structure */
void
kbd_init_struct(keyboard_t *kbd, char *name, int type, int unit, int config,
int port, int port_size)
{
kbd->kb_flags = KB_NO_DEVICE; /* device has not been found */
kbd->kb_name = name;
kbd->kb_type = type;
kbd->kb_unit = unit;
kbd->kb_config = config & ~KB_CONF_PROBE_ONLY;
kbd->kb_led = 0; /* unknown */
kbd->kb_io_base = port;
kbd->kb_io_size = port_size;
kbd->kb_data = NULL;
kbd->kb_keymap = NULL;
kbd->kb_accentmap = NULL;
kbd->kb_fkeytab = NULL;
kbd->kb_fkeytab_size = 0;
kbd->kb_delay1 = KB_DELAY1; /* these values are advisory only */
kbd->kb_delay2 = KB_DELAY2;
}
void
kbd_set_maps(keyboard_t *kbd, keymap_t *keymap, accentmap_t *accmap,
fkeytab_t *fkeymap, int fkeymap_size)
{
kbd->kb_keymap = keymap;
kbd->kb_accentmap = accmap;
kbd->kb_fkeytab = fkeymap;
kbd->kb_fkeytab_size = fkeymap_size;
}
/* register a keyboard and associate it with a function table */
int
kbd_register(keyboard_t *kbd)
{
const keyboard_driver_t **list;
const keyboard_driver_t *p;
int index;
for (index = 0; index < keyboards; ++index) {
if (keyboard[index] == NULL)
break;
}
if (index >= keyboards) {
if (kbd_realloc_array())
return -1;
}
kbd->kb_index = index;
KBD_UNBUSY(kbd);
KBD_VALID(kbd);
kbd->kb_active = 0; /* disabled until someone calls kbd_enable() */
kbd->kb_token = NULL;
kbd->kb_callback.kc_func = NULL;
kbd->kb_callback.kc_arg = NULL;
list = (const keyboard_driver_t **)kbddriver_set.ls_items;
while ((p = *list++) != NULL) {
if (strcmp(p->name, kbd->kb_name) == 0) {
keyboard[index] = kbd;
kbdsw[index] = p->kbdsw;
return index;
}
}
return -1;
}
int
kbd_unregister(keyboard_t *kbd)
{
int error;
int s;
if ((kbd->kb_index < 0) || (kbd->kb_index >= keyboards))
return ENOENT;
if (keyboard[kbd->kb_index] != kbd)
return ENOENT;
s = spltty();
if (KBD_IS_BUSY(kbd)) {
error = (*kbd->kb_callback.kc_func)(kbd, KBDIO_UNLOADING,
kbd->kb_callback.kc_arg);
if (error) {
splx(s);
return error;
}
if (KBD_IS_BUSY(kbd)) {
splx(s);
return EBUSY;
}
}
KBD_INVALID(kbd);
keyboard[kbd->kb_index] = NULL;
kbdsw[kbd->kb_index] = NULL;
splx(s);
return 0;
}
/* find a funciton table by the driver name */
keyboard_switch_t
*kbd_get_switch(char *driver)
{
const keyboard_driver_t **list;
const keyboard_driver_t *p;
list = (const keyboard_driver_t **)kbddriver_set.ls_items;
while ((p = *list++) != NULL) {
if (strcmp(p->name, driver) == 0)
return p->kbdsw;
}
return NULL;
}
/*
* Keyboard client functions
* Keyboard clients, such as the console driver `syscons' and the keyboard
* cdev driver, use these functions to claim and release a keyboard for
* exclusive use.
*/
/* find the keyboard specified by a driver name and a unit number */
int
kbd_find_keyboard(char *driver, int unit)
{
int i;
for (i = 0; i < keyboards; ++i) {
if (keyboard[i] == NULL)
continue;
if (!KBD_IS_VALID(keyboard[i]))
continue;
if (strcmp("*", driver) && strcmp(keyboard[i]->kb_name, driver))
continue;
if ((unit != -1) && (keyboard[i]->kb_unit != unit))
continue;
return i;
}
return -1;
}
/* allocate a keyboard */
int
kbd_allocate(char *driver, int unit, void *id, kbd_callback_func_t *func,
void *arg)
{
int index;
int s;
if (func == NULL)
return -1;
s = spltty();
index = kbd_find_keyboard(driver, unit);
if (index >= 0) {
if (KBD_IS_BUSY(keyboard[index])) {
splx(s);
return -1;
}
keyboard[index]->kb_token = id;
KBD_BUSY(keyboard[index]);
keyboard[index]->kb_callback.kc_func = func;
keyboard[index]->kb_callback.kc_arg = arg;
(*kbdsw[index]->clear_state)(keyboard[index]);
}
splx(s);
return index;
}
int
kbd_release(keyboard_t *kbd, void *id)
{
int error;
int s;
s = spltty();
if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
error = EINVAL;
} else if (kbd->kb_token != id) {
error = EPERM;
} else {
kbd->kb_token = NULL;
KBD_UNBUSY(kbd);
kbd->kb_callback.kc_func = NULL;
kbd->kb_callback.kc_arg = NULL;
(*kbdsw[kbd->kb_index]->clear_state)(kbd);
error = 0;
}
splx(s);
return error;
}
int
kbd_change_callback(keyboard_t *kbd, void *id, kbd_callback_func_t *func,
void *arg)
{
int error;
int s;
s = spltty();
if (!KBD_IS_VALID(kbd) || !KBD_IS_BUSY(kbd)) {
error = EINVAL;
} else if (kbd->kb_token != id) {
error = EPERM;
} else if (func == NULL) {
error = EINVAL;
} else {
kbd->kb_callback.kc_func = func;
kbd->kb_callback.kc_arg = arg;
error = 0;
}
splx(s);
return error;
}
/* get a keyboard structure */
keyboard_t
*kbd_get_keyboard(int index)
{
if ((index < 0) || (index >= keyboards))
return NULL;
if (!KBD_IS_VALID(keyboard[index]))
return NULL;
return keyboard[index];
}
/*
* The back door for the console driver; configure keyboards
* This function is for the kernel console to initialize keyboards
* at very early stage.
*/
int
kbd_configure(int flags)
{
const keyboard_driver_t **list;
const keyboard_driver_t *p;
list = (const keyboard_driver_t **)kbddriver_set.ls_items;
while ((p = *list++) != NULL) {
if (p->configure != NULL)
(*p->configure)(flags);
}
return 0;
}
#ifdef KBD_INSTALL_CDEV
/*
* Virtual keyboard cdev driver functions
* The virtual keyboard driver dispatches driver functions to
* appropriate subdrivers.
*/
#define KBD_UNIT(dev) minor(dev)
static d_open_t kbdopen;
static d_close_t kbdclose;
static d_read_t kbdread;
static d_write_t kbdwrite;
static d_ioctl_t kbdioctl;
static d_devtotty_t kbddevtotty;
static d_poll_t kbdpoll;
static d_mmap_t kbdmmap;
#define CDEV_MAJOR 112
static struct cdevsw kbd_cdevsw = {
/* open */ kbdopen,
/* close */ kbdclose,
/* read */ kbdread,
/* write */ kbdwrite,
/* ioctl */ kbdioctl,
/* stop */ nostop,
/* reset */ noreset,
/* devtotty */ kbddevtotty,
/* poll */ kbdpoll,
/* mmap */ kbdmmap,
/* strategy */ nostrategy,
/* name */ "kbd",
/* parms */ noparms,
/* maj */ CDEV_MAJOR,
/* dump */ nodump,
/* psize */ nopsize,
/* flags */ 0,
/* maxio */ 0,
/* bmaj */ -1
};
static void
vkbdattach(void *arg)
{
static int kbd_devsw_installed = FALSE;
dev_t dev;
if (!kbd_devsw_installed) {
dev = makedev(CDEV_MAJOR, 0);
cdevsw_add(&dev, &kbd_cdevsw, NULL);
kbd_devsw_installed = TRUE;
}
}
PSEUDO_SET(vkbdattach, kbd);
int
kbd_attach(dev_t dev, keyboard_t *kbd, struct cdevsw *cdevsw)
{
int s;
if (kbd->kb_index >= keyboards)
return EINVAL;
if (keyboard[kbd->kb_index] != kbd)
return EINVAL;
s = spltty();
kbd->kb_minor = minor(dev);
kbdcdevsw[kbd->kb_index] = cdevsw;
splx(s);
/* XXX: DEVFS? */
printf("kbd%d at %s%d\n", kbd->kb_index, kbd->kb_name, kbd->kb_unit);
return 0;
}
int
kbd_detach(dev_t dev, keyboard_t *kbd, struct cdevsw *cdevsw)
{
int s;
if (kbd->kb_index >= keyboards)
return EINVAL;
if (keyboard[kbd->kb_index] != kbd)
return EINVAL;
if (kbdcdevsw[kbd->kb_index] != cdevsw)
return EINVAL;
s = spltty();
kbdcdevsw[kbd->kb_index] = NULL;
splx(s);
return 0;
}
static int
kbdopen(dev_t dev, int flag, int mode, struct proc *p)
{
int unit;
unit = KBD_UNIT(dev);
if (unit >= keyboards)
return ENXIO;
if (kbdcdevsw[unit] == NULL)
return ENXIO;
if (KBD_IS_BUSY(keyboard[unit]))
return EBUSY;
return (*kbdcdevsw[unit]->d_open)(makedev(0, keyboard[unit]->kb_minor),
flag, mode, p);
}
static int
kbdclose(dev_t dev, int flag, int mode, struct proc *p)
{
int unit;
unit = KBD_UNIT(dev);
if (kbdcdevsw[unit] == NULL)
return ENXIO;
return (*kbdcdevsw[unit]->d_close)(makedev(0, keyboard[unit]->kb_minor),
flag, mode, p);
}
static int
kbdread(dev_t dev, struct uio *uio, int flag)
{
int unit;
unit = KBD_UNIT(dev);
if (kbdcdevsw[unit] == NULL)
return ENXIO;
return (*kbdcdevsw[unit]->d_read)(makedev(0, keyboard[unit]->kb_minor),
uio, flag);
}
static int
kbdwrite(dev_t dev, struct uio *uio, int flag)
{
int unit;
unit = KBD_UNIT(dev);
if (kbdcdevsw[unit] == NULL)
return ENXIO;
return (*kbdcdevsw[unit]->d_write)(makedev(0, keyboard[unit]->kb_minor),
uio, flag);
}
static int
kbdioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
{
int unit;
unit = KBD_UNIT(dev);
if (kbdcdevsw[unit] == NULL)
return ENXIO;
return (*kbdcdevsw[unit]->d_ioctl)(makedev(0, keyboard[unit]->kb_minor),
cmd, arg, flag, p);
}
static struct tty
*kbddevtotty(dev_t dev)
{
int unit;
unit = KBD_UNIT(dev);
if (kbdcdevsw[unit] == NULL)
return NULL;
return (*kbdcdevsw[unit]->d_devtotty)(makedev(0, keyboard[unit]->kb_minor));
}
static int
kbdpoll(dev_t dev, int event, struct proc *p)
{
int unit;
unit = KBD_UNIT(dev);
if (kbdcdevsw[unit] == NULL)
return ENXIO;
return (*kbdcdevsw[unit]->d_poll)(makedev(0, keyboard[unit]->kb_minor),
event, p);
}
static int
kbdmmap(dev_t dev, vm_offset_t offset, int nprot)
{
int unit;
unit = KBD_UNIT(dev);
if (kbdcdevsw[unit] == NULL)
return ENXIO;
return (*kbdcdevsw[unit]->d_mmap)(makedev(0, keyboard[unit]->kb_minor),
offset, nprot);
}
/*
* Generic keyboard cdev driver functions
* Keyboard subdrivers may call these functions to implement common
* driver functions.
*/
#define KB_QSIZE 512
#define KB_BUFSIZE 64
static kbd_callback_func_t genkbd_event;
int
genkbdopen(genkbd_softc_t *sc, keyboard_t *kbd, int mode, int flag,
struct proc *p)
{
int s;
int i;
s = spltty();
if (!KBD_IS_VALID(kbd)) {
splx(s);
return ENXIO;
}
i = kbd_allocate(kbd->kb_name, kbd->kb_unit, sc,
genkbd_event, (void *)sc);
if (i < 0) {
splx(s);
return EBUSY;
}
/* assert(i == kbd->kb_index) */
/* assert(kbd == kbd_get_keyboard(i)) */
/*
* NOTE: even when we have successfully claimed a keyboard,
* the device may still be missing (!KBD_HAS_DEVICE(kbd)).
*/
#if 0
bzero(&sc->gkb_q, sizeof(sc->gkb_q));
#endif
clist_alloc_cblocks(&sc->gkb_q, KB_QSIZE, KB_QSIZE/2); /* XXX */
sc->gkb_rsel.si_flags = 0;
sc->gkb_rsel.si_pid = 0;
splx(s);
return 0;
}
int
genkbdclose(genkbd_softc_t *sc, keyboard_t *kbd, int mode, int flag,
struct proc *p)
{
int s;
/*
* NOTE: the device may have already become invalid.
* !KBD_IS_VALID(kbd)
*/
s = spltty();
kbd_release(kbd, (void *)sc);
#if 0
clist_free_cblocks(&sc->gkb_q);
#endif
splx(s);
return 0;
}
int
genkbdread(genkbd_softc_t *sc, keyboard_t *kbd, struct uio *uio, int flag)
{
u_char buffer[KB_BUFSIZE];
int len;
int error;
int s;
/* wait for input */
s = spltty();
while (sc->gkb_q.c_cc == 0) {
if (!KBD_IS_VALID(kbd)) {
splx(s);
return EIO;
}
if (flag & IO_NDELAY) {
splx(s);
return EWOULDBLOCK;
}
sc->gkb_flags |= KB_ASLEEP;
error = tsleep((caddr_t)sc, PZERO | PCATCH, "kbdrea", 0);
if (error) {
sc->gkb_flags &= ~KB_ASLEEP;
splx(s);
return error;
}
}
splx(s);
/* copy as much input as possible */
error = 0;
while (uio->uio_resid > 0) {
len = imin(uio->uio_resid, sizeof(buffer));
len = q_to_b(&sc->gkb_q, buffer, len);
if (len <= 0)
break;
error = uiomove(buffer, len, uio);
if (error)
break;
}
return error;
}
int
genkbdwrite(genkbd_softc_t *sc, keyboard_t *kbd, struct uio *uio, int flag)
{
if (!KBD_IS_VALID(kbd))
return ENXIO;
return ENODEV;
}
int
genkbdioctl(genkbd_softc_t *sc, keyboard_t *kbd, u_long cmd, caddr_t arg,
int flag, struct proc *p)
{
int error;
if (kbd == NULL) /* XXX */
return ENXIO;
if (!KBD_IS_VALID(kbd))
return ENXIO;
error = (*kbdsw[kbd->kb_index]->ioctl)(kbd, cmd, arg);
if (error == ENOIOCTL)
error = ENODEV;
return error;
}
int
genkbdpoll(genkbd_softc_t *sc, keyboard_t *kbd, int events, struct proc *p)
{
int revents;
int s;
revents = 0;
s = spltty();
if (events & (POLLIN | POLLRDNORM)) {
if ((sc->gkb_q.c_cc > 0) || !KBD_IS_VALID(kbd))
revents |= (POLLIN | POLLRDNORM);
else
selrecord(p, &sc->gkb_rsel);
}
splx(s);
return revents;
}
static int
genkbd_event(keyboard_t *kbd, int event, void *arg)
{
genkbd_softc_t *sc;
size_t len;
u_char *cp;
int mode;
int c;
/* assert(KBD_IS_VALID(kbd)) */
sc = (genkbd_softc_t *)arg;
switch (event) {
case KBDIO_KEYINPUT:
break;
case KBDIO_UNLOADING:
/* the keyboard is going... */
kbd_release(kbd, (void *)sc);
return 0;
default:
return EINVAL;
}
/* obtain the current key input mode */
if ((*kbdsw[kbd->kb_index]->ioctl)(kbd, KDGKBMODE, (caddr_t)&mode))
mode = K_XLATE;
/* read all pending input */
while ((*kbdsw[kbd->kb_index]->check_char)(kbd)) {
c = (*kbdsw[kbd->kb_index]->read_char)(kbd, FALSE);
if (c == NOKEY)
continue;
if (c == ERRKEY) /* XXX: ring bell? */
continue;
if (!KBD_IS_BUSY(kbd))
/* the device is not open, discard the input */
continue;
/* store the byte as is for K_RAW and K_CODE modes */
if (mode != K_XLATE) {
putc(KEYCHAR(c), &sc->gkb_q);
continue;
}
/* K_XLATE */
if (c & RELKEY) /* key release is ignored */
continue;
/* process special keys; most of them are just ignored... */
if (c & SPCLKEY) {
switch (KEYCHAR(c)) {
/* locking keys */
case NLK: case CLK: case SLK: case ALK:
/* shift keys */
case LSH: case RSH: case LCTR: case RCTR:
case LALT: case RALT: case ASH: case META:
/* other special keys */
case NOP: case SPSC: case RBT: case SUSP:
case STBY: case DBG: case NEXT:
/* ignore them... */
continue;
case BTAB: /* a backtab: ESC [ Z */
putc(0x1b, &sc->gkb_q);
putc('[', &sc->gkb_q);
putc('Z', &sc->gkb_q);
continue;
}
}
/* normal chars, normal chars with the META, function keys */
switch (KEYFLAGS(c)) {
case 0: /* a normal char */
putc(KEYCHAR(c), &sc->gkb_q);
break;
case MKEY: /* the META flag: prepend ESC */
putc(0x1b, &sc->gkb_q);
putc(KEYCHAR(c), &sc->gkb_q);
break;
case FKEY | SPCLKEY: /* a function key, return string */
cp = (*kbdsw[kbd->kb_index]->get_fkeystr)(kbd,
KEYCHAR(c), &len);
if (cp != NULL) {
while (len-- > 0)
putc(*cp++, &sc->gkb_q);
}
break;
}
}
/* wake up sleeping/polling processes */
if (sc->gkb_q.c_cc > 0) {
if (sc->gkb_flags & KB_ASLEEP) {
sc->gkb_flags &= ~KB_ASLEEP;
wakeup((caddr_t)sc);
}
selwakeup(&sc->gkb_rsel);
}
return 0;
}
#endif /* KBD_INSTALL_CDEV */
/*
* Generic low-level keyboard functions
* The low-level functions in the keyboard subdriver may use these
* functions.
*/
int
genkbd_commonioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
{
keyarg_t *keyp;
fkeyarg_t *fkeyp;
int s;
int i;
s = spltty();
switch (cmd) {
case KDGKBINFO: /* get keyboard information */
((keyboard_info_t *)arg)->kb_index = kbd->kb_index;
i = imin(strlen(kbd->kb_name) + 1,
sizeof(((keyboard_info_t *)arg)->kb_name));
bcopy(kbd->kb_name, ((keyboard_info_t *)arg)->kb_name, i);
((keyboard_info_t *)arg)->kb_unit = kbd->kb_unit;
((keyboard_info_t *)arg)->kb_type = kbd->kb_type;
((keyboard_info_t *)arg)->kb_config = kbd->kb_config;
((keyboard_info_t *)arg)->kb_flags = kbd->kb_flags;
break;
case KDGKBTYPE: /* get keyboard type */
*(int *)arg = kbd->kb_type;
break;
case GIO_KEYMAP: /* get keyboard translation table */
bcopy(kbd->kb_keymap, arg, sizeof(*kbd->kb_keymap));
break;
case PIO_KEYMAP: /* set keyboard translation table */
#ifndef KBD_DISABLE_KEYMAP_LOAD
bzero(kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
bcopy(arg, kbd->kb_keymap, sizeof(*kbd->kb_keymap));
break;
#else
splx(s);
return ENODEV;
#endif
case GIO_KEYMAPENT: /* get keyboard translation table entry */
keyp = (keyarg_t *)arg;
if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
/sizeof(kbd->kb_keymap->key[0])) {
splx(s);
return EINVAL;
}
bcopy(&kbd->kb_keymap->key[keyp->keynum], &keyp->key,
sizeof(keyp->key));
break;
case PIO_KEYMAPENT: /* set keyboard translation table entry */
#ifndef KBD_DISABLE_KEYMAP_LOAD
keyp = (keyarg_t *)arg;
if (keyp->keynum >= sizeof(kbd->kb_keymap->key)
/sizeof(kbd->kb_keymap->key[0])) {
splx(s);
return EINVAL;
}
bcopy(&keyp->key, &kbd->kb_keymap->key[keyp->keynum],
sizeof(keyp->key));
break;
#else
splx(s);
return ENODEV;
#endif
case GIO_DEADKEYMAP: /* get accent key translation table */
bcopy(kbd->kb_accentmap, arg, sizeof(*kbd->kb_accentmap));
break;
case PIO_DEADKEYMAP: /* set accent key translation table */
#ifndef KBD_DISABLE_KEYMAP_LOAD
bcopy(arg, kbd->kb_accentmap, sizeof(*kbd->kb_accentmap));
break;
#else
splx(s);
return ENODEV;
#endif
case GETFKEY: /* get functionkey string */
fkeyp = (fkeyarg_t *)arg;
if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
splx(s);
return EINVAL;
}
bcopy(kbd->kb_fkeytab[fkeyp->keynum].str, fkeyp->keydef,
kbd->kb_fkeytab[fkeyp->keynum].len);
fkeyp->flen = kbd->kb_fkeytab[fkeyp->keynum].len;
break;
case SETFKEY: /* set functionkey string */
#ifndef KBD_DISABLE_KEYMAP_LOAD
fkeyp = (fkeyarg_t *)arg;
if (fkeyp->keynum >= kbd->kb_fkeytab_size) {
splx(s);
return EINVAL;
}
kbd->kb_fkeytab[fkeyp->keynum].len = imin(fkeyp->flen, MAXFK);
bcopy(fkeyp->keydef, kbd->kb_fkeytab[fkeyp->keynum].str,
kbd->kb_fkeytab[fkeyp->keynum].len);
break;
#else
splx(s);
return ENODEV;
#endif
default:
splx(s);
return ENOIOCTL;
}
splx(s);
return 0;
}
/* get a pointer to the string associated with the given function key */
u_char
*genkbd_get_fkeystr(keyboard_t *kbd, int fkey, size_t *len)
{
if (kbd == NULL)
return NULL;
fkey -= F_FN;
if (fkey > kbd->kb_fkeytab_size)
return NULL;
*len = kbd->kb_fkeytab[fkey].len;
return kbd->kb_fkeytab[fkey].str;
}
/* diagnostic dump */
static char
*get_kbd_type_name(int type)
{
static struct {
int type;
char *name;
} name_table[] = {
{ KB_84, "AT 84" },
{ KB_101, "AT 101/102" },
{ KB_OTHER, "generic" },
};
int i;
for (i = 0; i < sizeof(name_table)/sizeof(name_table[0]); ++i) {
if (type == name_table[i].type)
return name_table[i].name;
}
return "unknown";
}
void
genkbd_diag(keyboard_t *kbd, int level)
{
if (level > 0) {
printf("kbd%d: %s%d, %s (%d), config:0x%x, flags:0x%x",
kbd->kb_index, kbd->kb_name, kbd->kb_unit,
get_kbd_type_name(kbd->kb_type), kbd->kb_type,
kbd->kb_config, kbd->kb_flags);
if (kbd->kb_io_base > 0)
printf(", port:0x%x-0x%x", kbd->kb_io_base,
kbd->kb_io_base + kbd->kb_io_size - 1);
printf("\n");
}
}
#define set_lockkey_state(k, s, l) \
if (!((s) & l ## DOWN)) { \
int i; \
(s) |= l ## DOWN; \
(s) ^= l ## ED; \
i = (s) & LOCK_MASK; \
(*kbdsw[(k)->kb_index]->ioctl)((k), KDSETLED, (caddr_t)&i); \
}
static u_int
save_accent_key(keyboard_t *kbd, u_int key, int *accents)
{
int i;
/* make an index into the accent map */
i = key - F_ACC + 1;
if ((i > kbd->kb_accentmap->n_accs)
|| (kbd->kb_accentmap->acc[i - 1].accchar == 0)) {
/* the index is out of range or pointing to an empty entry */
*accents = 0;
return ERRKEY;
}
/*
* If the same accent key has been hit twice, produce the accent char
* itself.
*/
if (i == *accents) {
key = kbd->kb_accentmap->acc[i - 1].accchar;
*accents = 0;
return key;
}
/* remember the index and wait for the next key */
*accents = i;
return NOKEY;
}
static u_int
make_accent_char(keyboard_t *kbd, u_int ch, int *accents)
{
struct acc_t *acc;
int i;
acc = &kbd->kb_accentmap->acc[*accents - 1];
*accents = 0;
/*
* If the accent key is followed by the space key,
* produce the accent char itself.
*/
if (ch == ' ')
return acc->accchar;
/* scan the accent map */
for (i = 0; i < NUM_ACCENTCHARS; ++i) {
if (acc->map[i][0] == 0) /* end of table */
break;
if (acc->map[i][0] == ch)
return acc->map[i][1];
}
/* this char cannot be accented... */
return ERRKEY;
}
int
genkbd_keyaction(keyboard_t *kbd, int keycode, int up, int *shiftstate,
int *accents)
{
struct keyent_t *key;
int state = *shiftstate;
int action;
int f;
int i;
f = state & (AGRS | ALKED);
if ((f == AGRS1) || (f == AGRS2) || (f == ALKED))
keycode += ALTGR_OFFSET;
key = &kbd->kb_keymap->key[keycode];
i = ((state & SHIFTS) ? 1 : 0)
| ((state & CTLS) ? 2 : 0)
| ((state & ALTS) ? 4 : 0);
if (((key->flgs & FLAG_LOCK_C) && (state & CLKED))
|| ((key->flgs & FLAG_LOCK_N) && (state & NLKED)) )
i ^= 1;
action = key->map[i];
if (up) { /* break: key released */
if (key->spcl & (0x80 >> i)) {
/* special keys */
switch (action) {
case LSH:
state &= ~SHIFTS1;
break;
case RSH:
state &= ~SHIFTS2;
break;
case LCTR:
state &= ~CTLS1;
break;
case RCTR:
state &= ~CTLS2;
break;
case LALT:
state &= ~ALTS1;
break;
case RALT:
state &= ~ALTS2;
break;
case ASH:
state &= ~AGRS1;
break;
case META:
state &= ~METAS1;
break;
case NLK:
state &= ~NLKDOWN;
break;
case CLK:
#ifndef PC98
state &= ~CLKDOWN;
#else
state &= ~CLKED;
i = state & LOCK_MASK;
(*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
(caddr_t)&i);
#endif
break;
case SLK:
state &= ~SLKDOWN;
break;
case ALK:
state &= ~ALKDOWN;
break;
}
*shiftstate = state;
return (SPCLKEY | RELKEY | action);
}
/* release events of regular keys are not reported */
return NOKEY;
} else { /* make: key pressed */
if (key->spcl & (0x80 >> i)) {
/* special keys */
switch (action) {
/* LOCKING KEYS */
case NLK:
set_lockkey_state(kbd, state, NLK);
break;
case CLK:
#ifndef PC98
set_lockkey_state(kbd, state, CLK);
#else
state |= CLKED;
i = state & LOCK_MASK;
(*kbdsw[kbd->kb_index]->ioctl)(kbd, KDSETLED,
(caddr_t)&i);
#endif
break;
case SLK:
set_lockkey_state(kbd, state, SLK);
break;
case ALK:
set_lockkey_state(kbd, state, ALK);
break;
/* NON-LOCKING KEYS */
case SPSC: case RBT: case SUSP: case STBY:
case DBG: case NEXT:
*accents = 0;
break;
case BTAB:
*accents = 0;
action |= BKEY;
break;
case LSH:
state |= SHIFTS1;
break;
case RSH:
state |= SHIFTS2;
break;
case LCTR:
state |= CTLS1;
break;
case RCTR:
state |= CTLS2;
break;
case LALT:
state |= ALTS1;
break;
case RALT:
state |= ALTS2;
break;
case ASH:
state |= AGRS1;
break;
case META:
state |= METAS1;
break;
default:
/* is this an accent (dead) key? */
if (action >= F_ACC && action <= L_ACC) {
action = save_accent_key(kbd, action,
accents);
switch (action) {
case NOKEY:
case ERRKEY:
return action;
default:
if (state & METAS)
return (action | MKEY);
else
return action;
}
/* NOT REACHED */
}
/* other special keys */
if (*accents > 0) {
*accents = 0;
return ERRKEY;
}
if (action >= F_FN && action <= L_FN)
action |= FKEY;
/* XXX: return fkey string for the FKEY? */
}
*shiftstate = state;
return (SPCLKEY | action);
} else {
/* regular keys */
if (*accents > 0) {
/* make an accented char */
action = make_accent_char(kbd, action, accents);
if (action == ERRKEY)
return action;
}
if (state & METAS)
action |= MKEY;
return action;
}
}
/* NOT REACHED */
}
|