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
|
/*
* Copyright (c) 1999 FreeBSD(98) port team.
* 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
*
* $FreeBSD$
*/
#include "opt_kbd.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <sys/kbio.h>
#include <machine/resource.h>
#include <dev/kbd/kbdreg.h>
#include <pc98/pc98/pc98.h>
#include <isa/isavar.h>
#define DRIVER_NAME "pckbd"
/* device configuration flags */
#define KB_CONF_FAIL_IF_NO_KBD (1 << 0) /* don't install if no kbd is found */
static devclass_t pckbd_devclass;
static int pckbdprobe(device_t dev);
static int pckbdattach(device_t dev);
static int pckbdresume(device_t dev);
static void pckbd_isa_intr(void *arg);
static device_method_t pckbd_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, pckbdprobe),
DEVMETHOD(device_attach, pckbdattach),
DEVMETHOD(device_resume, pckbdresume),
{ 0, 0 }
};
static driver_t pckbd_driver = {
DRIVER_NAME,
pckbd_methods,
1,
};
DRIVER_MODULE(pckbd, isa, pckbd_driver, pckbd_devclass, 0, 0);
static bus_addr_t pckbd_iat[] = {0, 2};
static int pckbd_probe_unit(int unit, int port, int irq,
int flags);
static int pckbd_attach_unit(int unit, keyboard_t **kbd,
int port, int irq, int flags);
static timeout_t pckbd_timeout;
static int
pckbdprobe(device_t dev)
{
struct resource *res;
int error, rid;
/* Check isapnp ids */
if (isa_get_vendorid(dev))
return (ENXIO);
device_set_desc(dev, "PC-98 Keyboard");
rid = 0;
res = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, pckbd_iat, 2,
RF_ACTIVE);
if (res == NULL)
return ENXIO;
isa_load_resourcev(res, pckbd_iat, 2);
error = pckbd_probe_unit(device_get_unit(dev),
isa_get_port(dev),
(1 << isa_get_irq(dev)),
device_get_flags(dev));
bus_release_resource(dev, SYS_RES_IOPORT, rid, res);
return (error);
}
static int
pckbdattach(device_t dev)
{
keyboard_t *kbd;
void *ih;
struct resource *res;
int error, rid;
rid = 0;
res = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, pckbd_iat, 2,
RF_ACTIVE);
if (res == NULL)
return ENXIO;
isa_load_resourcev(res, pckbd_iat, 2);
error = pckbd_attach_unit(device_get_unit(dev), &kbd,
isa_get_port(dev),
(1 << isa_get_irq(dev)),
device_get_flags(dev));
rid = 0;
res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1, RF_ACTIVE);
if (res == NULL)
return ENXIO;
BUS_SETUP_INTR(device_get_parent(dev), dev, res, INTR_TYPE_TTY,
pckbd_isa_intr, kbd, &ih);
return 0;
}
static int
pckbdresume(device_t dev)
{
keyboard_t *kbd;
kbd = kbd_get_keyboard(kbd_find_keyboard(DRIVER_NAME,
device_get_unit(dev)));
if (kbd)
(*kbdsw[kbd->kb_index]->clear_state)(kbd);
return (0);
}
static void
pckbd_isa_intr(void *arg)
{
keyboard_t *kbd = arg;
(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
}
static int
pckbd_probe_unit(int unit, int port, int irq, int flags)
{
keyboard_switch_t *sw;
int args[2];
int error;
sw = kbd_get_switch(DRIVER_NAME);
if (sw == NULL)
return ENXIO;
args[0] = port;
args[1] = irq;
error = (*sw->probe)(unit, args, flags);
if (error)
return error;
return 0;
}
static int
pckbd_attach_unit(int unit, keyboard_t **kbd, int port, int irq, int flags)
{
keyboard_switch_t *sw;
int args[2];
int error;
sw = kbd_get_switch(DRIVER_NAME);
if (sw == NULL)
return ENXIO;
/* reset, initialize and enable the device */
args[0] = port;
args[1] = irq;
*kbd = NULL;
error = (*sw->probe)(unit, args, flags);
if (error)
return error;
error = (*sw->init)(unit, kbd, args, flags);
if (error)
return error;
(*sw->enable)(*kbd);
#ifdef KBD_INSTALL_CDEV
/* attach a virtual keyboard cdev */
error = kbd_attach(*kbd);
if (error)
return error;
#endif /* KBD_INSTALL_CDEV */
/*
* This is a kludge to compensate for lost keyboard interrupts.
* A similar code used to be in syscons. See below. XXX
*/
pckbd_timeout(*kbd);
if (bootverbose)
(*sw->diag)(*kbd, bootverbose);
return 0;
}
static void
pckbd_timeout(void *arg)
{
keyboard_t *kbd;
int s;
/* The following comments are extracted from syscons.c (1.287) */
/*
* With release 2.1 of the Xaccel server, the keyboard is left
* hanging pretty often. Apparently an interrupt from the
* keyboard is lost, and I don't know why (yet).
* This ugly hack calls scintr if input is ready for the keyboard
* and conveniently hides the problem. XXX
*/
/*
* Try removing anything stuck in the keyboard controller; whether
* it's a keyboard scan code or mouse data. `scintr()' doesn't
* read the mouse data directly, but `kbdio' routines will, as a
* side effect.
*/
s = spltty();
kbd = (keyboard_t *)arg;
if ((*kbdsw[kbd->kb_index]->lock)(kbd, TRUE)) {
/*
* We have seen the lock flag is not set. Let's reset
* the flag early, otherwise the LED update routine fails
* which may want the lock during the interrupt routine.
*/
(*kbdsw[kbd->kb_index]->lock)(kbd, FALSE);
if ((*kbdsw[kbd->kb_index]->check_char)(kbd))
(*kbdsw[kbd->kb_index]->intr)(kbd, NULL);
}
splx(s);
timeout(pckbd_timeout, arg, hz/10);
}
/* LOW-LEVEL */
#include <machine/limits.h>
#define PC98KBD_DEFAULT 0
typedef caddr_t KBDC;
typedef struct pckbd_state {
KBDC kbdc; /* keyboard controller */
int ks_mode; /* input mode (K_XLATE,K_RAW,K_CODE) */
int ks_flags; /* flags */
#define COMPOSE (1 << 0)
int ks_state; /* shift/lock key state */
int ks_accents; /* accent key index (> 0) */
u_int ks_composed_char; /* composed char code (> 0) */
} pckbd_state_t;
/* keyboard driver declaration */
static int pckbd_configure(int flags);
static kbd_probe_t pckbd_probe;
static kbd_init_t pckbd_init;
static kbd_term_t pckbd_term;
static kbd_intr_t pckbd_intr;
static kbd_test_if_t pckbd_test_if;
static kbd_enable_t pckbd_enable;
static kbd_disable_t pckbd_disable;
static kbd_read_t pckbd_read;
static kbd_check_t pckbd_check;
static kbd_read_char_t pckbd_read_char;
static kbd_check_char_t pckbd_check_char;
static kbd_ioctl_t pckbd_ioctl;
static kbd_lock_t pckbd_lock;
static kbd_clear_state_t pckbd_clear_state;
static kbd_get_state_t pckbd_get_state;
static kbd_set_state_t pckbd_set_state;
static kbd_poll_mode_t pckbd_poll;
keyboard_switch_t pckbdsw = {
pckbd_probe,
pckbd_init,
pckbd_term,
pckbd_intr,
pckbd_test_if,
pckbd_enable,
pckbd_disable,
pckbd_read,
pckbd_check,
pckbd_read_char,
pckbd_check_char,
pckbd_ioctl,
pckbd_lock,
pckbd_clear_state,
pckbd_get_state,
pckbd_set_state,
genkbd_get_fkeystr,
pckbd_poll,
genkbd_diag,
};
KEYBOARD_DRIVER(pckbd, pckbdsw, pckbd_configure);
struct kbdc_softc {
int port; /* base port address */
int lock; /* FIXME: XXX not quite a semaphore... */
};
/* local functions */
static int probe_keyboard(KBDC kbdc, int flags);
static int init_keyboard(KBDC kbdc, int *type, int flags);
static KBDC kbdc_open(int port);
static int kbdc_lock(KBDC kbdc, int lock);
static int kbdc_data_ready(KBDC kbdc);
static int read_kbd_data(KBDC kbdc);
static int read_kbd_data_no_wait(KBDC kbdc);
static int wait_for_kbd_data(struct kbdc_softc *kbdc);
/* local variables */
/* the initial key map, accent map and fkey strings */
#include <dev/kbd/kbdtables.h>
/* structures for the default keyboard */
static keyboard_t default_kbd;
static pckbd_state_t default_kbd_state;
static keymap_t default_keymap;
static accentmap_t default_accentmap;
static fkeytab_t default_fkeytab[NUM_FKEYS];
/*
* The back door to the keyboard driver!
* This function is called by the console driver, via the kbdio module,
* to tickle keyboard drivers when the low-level console is being initialized.
* Almost nothing in the kernel has been initialied yet. Try to probe
* keyboards if possible.
* NOTE: because of the way the low-level conole is initialized, this routine
* may be called more than once!!
*/
static int
pckbd_configure(int flags)
{
keyboard_t *kbd;
int arg[2];
int i;
/* XXX: a kludge to obtain the device configuration flags */
if (resource_int_value(DRIVER_NAME, 0, "flags", &i) == 0) {
flags |= i;
/* if the driver is disabled, unregister the keyboard if any */
if (resource_int_value(DRIVER_NAME, 0, "disabled", &i) == 0
&& i != 0) {
i = kbd_find_keyboard(DRIVER_NAME, PC98KBD_DEFAULT);
if (i >= 0) {
kbd = kbd_get_keyboard(i);
kbd_unregister(kbd);
kbd->kb_flags &= ~KB_REGISTERED;
return 0;
}
}
}
/* probe the default keyboard */
arg[0] = -1;
arg[1] = -1;
kbd = NULL;
if (pckbd_probe(PC98KBD_DEFAULT, arg, flags))
return 0;
if (pckbd_init(PC98KBD_DEFAULT, &kbd, arg, flags))
return 0;
/* return the number of found keyboards */
return 1;
}
/* low-level functions */
/* detect a keyboard */
static int
pckbd_probe(int unit, void *arg, int flags)
{
KBDC kbdc;
int *data = (int *)arg;
if (unit != PC98KBD_DEFAULT)
return ENXIO;
if (KBD_IS_PROBED(&default_kbd))
return 0;
kbdc = kbdc_open(data[0]);
if (kbdc == NULL)
return ENXIO;
if (probe_keyboard(kbdc, flags)) {
if (flags & KB_CONF_FAIL_IF_NO_KBD)
return ENXIO;
}
return 0;
}
/* reset and initialize the device */
static int
pckbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
{
keyboard_t *kbd;
pckbd_state_t *state;
keymap_t *keymap;
accentmap_t *accmap;
fkeytab_t *fkeymap;
int fkeymap_size;
int *data = (int *)arg;
if (unit != PC98KBD_DEFAULT) /* shouldn't happen */
return ENXIO;
*kbdp = kbd = &default_kbd;
state = &default_kbd_state;
if (!KBD_IS_PROBED(kbd)) {
keymap = &default_keymap;
accmap = &default_accentmap;
fkeymap = default_fkeytab;
fkeymap_size =
sizeof(default_fkeytab)/sizeof(default_fkeytab[0]);
state->kbdc = kbdc_open(data[0]);
if (state->kbdc == NULL)
return ENXIO;
kbd_init_struct(kbd, DRIVER_NAME, KB_OTHER, unit, flags,
data[0], IO_KBDSIZE);
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;
if (probe_keyboard(state->kbdc, flags)) {/* shouldn't happen */
if (flags & KB_CONF_FAIL_IF_NO_KBD)
return ENXIO;
} else {
KBD_FOUND_DEVICE(kbd);
}
pckbd_clear_state(kbd);
state->ks_mode = K_XLATE;
KBD_PROBE_DONE(kbd);
}
if (!KBD_IS_INITIALIZED(kbd) && !(flags & KB_CONF_PROBE_ONLY)) {
if (KBD_HAS_DEVICE(kbd)
&& init_keyboard(state->kbdc, &kbd->kb_type, kbd->kb_config)
&& (kbd->kb_config & KB_CONF_FAIL_IF_NO_KBD))
return ENXIO;
pckbd_ioctl(kbd, KDSETLED, (caddr_t)&state->ks_state);
KBD_INIT_DONE(kbd);
}
if (!KBD_IS_CONFIGURED(kbd)) {
if (kbd_register(kbd) < 0)
return ENXIO;
KBD_CONFIG_DONE(kbd);
}
return 0;
}
/* finish using this keyboard */
static int
pckbd_term(keyboard_t *kbd)
{
kbd_unregister(kbd);
return 0;
}
/* keyboard interrupt routine */
static int
pckbd_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 = pckbd_read_char(kbd, FALSE);
} while (c != NOKEY);
}
return 0;
}
/* test the interface to the device */
static int
pckbd_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
pckbd_enable(keyboard_t *kbd)
{
int s;
s = spltty();
KBD_ACTIVATE(kbd);
splx(s);
return 0;
}
/* disallow the access to the device */
static int
pckbd_disable(keyboard_t *kbd)
{
int s;
s = spltty();
KBD_DEACTIVATE(kbd);
splx(s);
return 0;
}
/* read one byte from the keyboard if it's allowed */
static int
pckbd_read(keyboard_t *kbd, int wait)
{
int c;
if (wait)
c = read_kbd_data(((pckbd_state_t *)kbd->kb_data)->kbdc);
else
c = read_kbd_data_no_wait(((pckbd_state_t *)kbd->kb_data)->kbdc);
if (c != -1)
++kbd->kb_count;
return (KBD_IS_ACTIVE(kbd) ? c : -1);
}
/* check if data is waiting */
static int
pckbd_check(keyboard_t *kbd)
{
if (!KBD_IS_ACTIVE(kbd))
return FALSE;
return kbdc_data_ready(((pckbd_state_t *)kbd->kb_data)->kbdc);
}
/* read char from the keyboard */
static u_int
pckbd_read_char(keyboard_t *kbd, int wait)
{
pckbd_state_t *state;
u_int action;
int scancode;
int keycode;
state = (pckbd_state_t *)kbd->kb_data;
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)
return ERRKEY;
return action;
}
/* see if there is something in the keyboard port */
if (wait) {
do {
scancode = read_kbd_data(state->kbdc);
} while (scancode == -1);
} else {
scancode = read_kbd_data_no_wait(state->kbdc);
if (scancode == -1)
return NOKEY;
}
++kbd->kb_count;
#if 0
printf("pckbd_read_char(): scancode:0x%x\n", scancode);
#endif
/* return the byte as is for the K_RAW mode */
if (state->ks_mode == K_RAW)
return scancode;
/* translate the scan code into a keycode */
keycode = scancode & 0x7F;
switch(scancode) {
case 0xF3: /* GRPH (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 0x73: /* GRPH (compose key) pressed */
if (!(state->ks_flags & COMPOSE)) {
state->ks_flags |= COMPOSE;
state->ks_composed_char = 0;
}
break;
}
/* return the key code in the K_CODE mode */
if (state->ks_mode == K_CODE)
return (keycode | (scancode & 0x80));
/* compose a character code */
if (state->ks_flags & COMPOSE) {
switch (scancode) {
/* key pressed, process it */
case 0x42: case 0x43: case 0x44: /* keypad 7,8,9 */
state->ks_composed_char *= 10;
state->ks_composed_char += scancode - 0x3B;
if (state->ks_composed_char > UCHAR_MAX)
return ERRKEY;
goto next_code;
case 0x46: case 0x47: case 0x48: /* keypad 4,5,6 */
state->ks_composed_char *= 10;
state->ks_composed_char += scancode - 0x42;
if (state->ks_composed_char > UCHAR_MAX)
return ERRKEY;
goto next_code;
case 0x4A: case 0x4B: case 0x4C: /* keypad 1,2,3 */
state->ks_composed_char *= 10;
state->ks_composed_char += scancode - 0x49;
if (state->ks_composed_char > UCHAR_MAX)
return ERRKEY;
goto next_code;
case 0x4E: /* keypad 0 */
state->ks_composed_char *= 10;
if (state->ks_composed_char > UCHAR_MAX)
return ERRKEY;
goto next_code;
/* key released, no interest here */
case 0xC2: case 0xC3: case 0xC4: /* keypad 7,8,9 */
case 0xC6: case 0xC7: case 0xC8: /* keypad 4,5,6 */
case 0xCA: case 0xCB: case 0xCC: /* keypad 1,2,3 */
case 0xCE: /* keypad 0 */
goto next_code;
case 0x73: /* GRPH key */
break;
default:
if (state->ks_composed_char > 0) {
state->ks_flags &= ~COMPOSE;
state->ks_composed_char = 0;
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;
else
return action;
}
/* check if char is waiting */
static int
pckbd_check_char(keyboard_t *kbd)
{
pckbd_state_t *state;
if (!KBD_IS_ACTIVE(kbd))
return FALSE;
state = (pckbd_state_t *)kbd->kb_data;
if (!(state->ks_flags & COMPOSE) && (state->ks_composed_char > 0))
return TRUE;
return kbdc_data_ready(state->kbdc);
}
/* some useful control functions */
static int
pckbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
{
pckbd_state_t *state = kbd->kb_data;
int s;
int i;
s = spltty();
switch (cmd) {
case KDGKBMODE: /* get keyboard mode */
*(int *)arg = state->ks_mode;
break;
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);
}
/* FALLTHROUGH */
case K_RAW:
case K_CODE:
if (state->ks_mode != *(int *)arg) {
pckbd_clear_state(kbd);
state->ks_mode = *(int *)arg;
}
break;
default:
splx(s);
return EINVAL;
}
break;
case KDGETLED: /* get keyboard LED */
*(int *)arg = KBD_LED_VAL(kbd);
break;
case KDSETLED: /* set keyboard LED */
/* NOTE: lock key state in ks_state won't be changed */
if (*(int *)arg & ~LOCK_MASK) {
splx(s);
return EINVAL;
}
i = *(int *)arg;
/* replace CAPS LED with ALTGR LED for ALTGR keyboards */
if (kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
if (i & ALKED)
i |= CLKED;
else
i &= ~CLKED;
}
KBD_LED_VAL(kbd) = *(int *)arg;
break;
case KDGKBSTATE: /* get lock key state */
*(int *)arg = state->ks_state & LOCK_MASK;
break;
case KDSKBSTATE: /* set lock key state */
if (*(int *)arg & ~LOCK_MASK) {
splx(s);
return EINVAL;
}
state->ks_state &= ~LOCK_MASK;
state->ks_state |= *(int *)arg;
splx(s);
/* set LEDs and quit */
return pckbd_ioctl(kbd, KDSETLED, arg);
case KDSETRAD: /* set keyboard repeat rate (old interface)*/
break;
case KDSETREPEAT: /* set keyboard repeat rate (new interface) */
break;
case PIO_KEYMAP: /* set keyboard translation table */
case PIO_KEYMAPENT: /* set keyboard translation table entry */
case PIO_DEADKEYMAP: /* set accent key translation table */
state->ks_accents = 0;
/* FALLTHROUGH */
default:
splx(s);
return genkbd_commonioctl(kbd, cmd, arg);
}
splx(s);
return 0;
}
/* lock the access to the keyboard */
static int
pckbd_lock(keyboard_t *kbd, int lock)
{
return kbdc_lock(((pckbd_state_t *)kbd->kb_data)->kbdc, lock);
}
/* clear the internal state of the keyboard */
static void
pckbd_clear_state(keyboard_t *kbd)
{
pckbd_state_t *state;
state = (pckbd_state_t *)kbd->kb_data;
state->ks_flags = 0;
state->ks_state &= LOCK_MASK; /* preserve locking key state */
state->ks_accents = 0;
state->ks_composed_char = 0;
}
/* save the internal state */
static int
pckbd_get_state(keyboard_t *kbd, void *buf, size_t len)
{
if (len == 0)
return sizeof(pckbd_state_t);
if (len < sizeof(pckbd_state_t))
return -1;
bcopy(kbd->kb_data, buf, sizeof(pckbd_state_t));
return 0;
}
/* set the internal state */
static int
pckbd_set_state(keyboard_t *kbd, void *buf, size_t len)
{
if (len < sizeof(pckbd_state_t))
return ENOMEM;
if (((pckbd_state_t *)kbd->kb_data)->kbdc
!= ((pckbd_state_t *)buf)->kbdc)
return ENOMEM;
bcopy(buf, kbd->kb_data, sizeof(pckbd_state_t));
return 0;
}
/* set polling mode */
static int
pckbd_poll(keyboard_t *kbd, int on)
{
return 0;
}
/* local functions */
static int
probe_keyboard(KBDC kbdc, int flags)
{
return 0;
}
static int
init_keyboard(KBDC kbdc, int *type, int flags)
{
*type = KB_OTHER;
return 0;
}
/* keyboard I/O routines */
/* retry count */
#ifndef KBD_MAXRETRY
#define KBD_MAXRETRY 3
#endif
/* timing parameters */
#ifndef KBD_RESETDELAY
#define KBD_RESETDELAY 200 /* wait 200msec after kbd/mouse reset */
#endif
#ifndef KBD_MAXWAIT
#define KBD_MAXWAIT 5 /* wait 5 times at most after reset */
#endif
/* I/O recovery time */
#define KBDC_DELAYTIME 37
#define KBDD_DELAYTIME 37
/* I/O ports */
#define KBD_STATUS_PORT 2 /* status port, read */
#define KBD_DATA_PORT 0 /* data port, read */
/* status bits (KBD_STATUS_PORT) */
#define KBDS_BUFFER_FULL 0x0002
/* macros */
#define kbdcp(p) ((struct kbdc_softc *)(p))
/* local variables */
static struct kbdc_softc kbdc_softc[1] = { { 0 }, };
/* associate a port number with a KBDC */
static KBDC
kbdc_open(int port)
{
if (port <= 0)
port = IO_KBD;
/* PC-98 has only one keyboard I/F */
kbdc_softc[0].port = port;
kbdc_softc[0].lock = FALSE;
return (KBDC)&kbdc_softc[0];
}
/* set/reset polling lock */
static int
kbdc_lock(KBDC p, int lock)
{
int prevlock;
prevlock = kbdcp(p)->lock;
kbdcp(p)->lock = lock;
return (prevlock != lock);
}
/* check if any data is waiting to be processed */
static int
kbdc_data_ready(KBDC p)
{
return (inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL);
}
/* wait for data from the keyboard */
static int
wait_for_kbd_data(struct kbdc_softc *kbdc)
{
/* CPU will stay inside the loop for 200msec at most */
int retry = 10000;
int port = kbdc->port;
while (!(inb(port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL)) {
DELAY(KBDD_DELAYTIME);
DELAY(KBDC_DELAYTIME);
if (--retry < 0)
return 0;
}
DELAY(KBDD_DELAYTIME);
return 1;
}
/* read one byte from the keyboard */
static int
read_kbd_data(KBDC p)
{
if (!wait_for_kbd_data(kbdcp(p)))
return -1; /* timeout */
DELAY(KBDC_DELAYTIME);
return inb(kbdcp(p)->port + KBD_DATA_PORT);
}
/* read one byte from the keyboard, but return immediately if
* no data is waiting
*/
static int
read_kbd_data_no_wait(KBDC p)
{
if (inb(kbdcp(p)->port + KBD_STATUS_PORT) & KBDS_BUFFER_FULL) {
DELAY(KBDD_DELAYTIME);
return inb(kbdcp(p)->port + KBD_DATA_PORT);
}
return -1; /* no data */
}
|