kbd.c revision 1.12 1 /* $NetBSD: kbd.c,v 1.12 1996/10/16 20:43:39 gwr Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
45 */
46
47 /*
48 * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
49 * [yet?]). Translates incoming bytes to ASCII or to `firm_events' and
50 * passes them up to the appropriate reader.
51 */
52
53 /*
54 * Zilog Z8530 Dual UART driver (keyboard interface)
55 *
56 * This is the "slave" driver that will be attached to
57 * the "zsc" driver for a Sun keyboard.
58 */
59
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/proc.h>
63 #include <sys/device.h>
64 #include <sys/conf.h>
65 #include <sys/file.h>
66 #include <sys/ioctl.h>
67 #include <sys/time.h>
68 #include <sys/kernel.h>
69 #include <sys/syslog.h>
70 #include <sys/select.h>
71 #include <sys/poll.h>
72
73 #include <dev/ic/z8530reg.h>
74 #include <machine/z8530var.h>
75 #include <machine/vuid_event.h>
76 #include <machine/kbd.h>
77 #include <machine/kbio.h>
78
79 #include "event_var.h"
80 #include "kbd_xlate.h"
81
82 /*
83 * Ideas:
84 * /dev/kbd is not a tty (plain device)
85 */
86
87 /*
88 * How many input characters we can buffer.
89 * The port-specific var.h may override this.
90 * Note: must be a power of two!
91 */
92 #define KBD_RX_RING_SIZE 256
93 #define KBD_RX_RING_MASK (KBD_RX_RING_SIZE-1)
94 /*
95 * Output buffer. Only need a few chars.
96 */
97 #define KBD_TX_RING_SIZE 16
98 #define KBD_TX_RING_MASK (KBD_TX_RING_SIZE-1)
99 /*
100 * Keyboard serial line speed is fixed at 1200 bps.
101 */
102 #define KBD_BPS 1200
103 #define KBD_RESET_TIMO 1000 /* mS. */
104
105 /*
106 * XXX - Historical comment - no longer quite right...
107 * Keyboard driver state. The ascii and kbd links go up and down and
108 * we just sit in the middle doing translation. Note that it is possible
109 * to get just one of the two links, in which case /dev/kbd is unavailable.
110 * The downlink supplies us with `internal' open and close routines which
111 * will enable dataflow across the downlink. We promise to call open when
112 * we are willing to take keystrokes, and to call close when we are not.
113 * If /dev/kbd is not the console tty input source, we do this whenever
114 * /dev/kbd is in use; otherwise we just leave it open forever.
115 */
116 struct kbd_softc {
117 struct device k_dev; /* required first: base device */
118 struct zs_chanstate *k_cs;
119
120 /* Flags to communicate with kbd_softint() */
121 volatile int k_intr_flags;
122 #define INTR_RX_OVERRUN 1
123 #define INTR_TX_EMPTY 2
124 #define INTR_ST_CHECK 4
125
126 /* Transmit state */
127 volatile int k_txflags;
128 #define K_TXBUSY 1
129 #define K_TXWANT 2
130
131 /*
132 * State of upper interface.
133 */
134 int k_isopen; /* set if open has been done */
135 int k_evmode; /* set if we should produce events */
136 struct evvar k_events; /* event queue state */
137
138 /*
139 * ACSI translation state
140 */
141 int k_repeat_start; /* initial delay */
142 int k_repeat_step; /* inter-char delay */
143 int k_repeatsym; /* repeating symbol */
144 int k_repeating; /* we've called timeout() */
145 struct kbd_state k_state; /* ASCII translation state */
146
147 /*
148 * Magic sequence stuff (L1-A)
149 */
150 char k_isconsole;
151 char k_magic1_down;
152 u_char k_magic1; /* L1 */
153 u_char k_magic2; /* A */
154
155 /*
156 * The transmit ring buffer.
157 */
158 volatile u_int k_tbget; /* transmit buffer `get' index */
159 volatile u_int k_tbput; /* transmit buffer `put' index */
160 u_char k_tbuf[KBD_TX_RING_SIZE]; /* data */
161
162 /*
163 * The receive ring buffer.
164 */
165 u_int k_rbget; /* ring buffer `get' index */
166 volatile u_int k_rbput; /* ring buffer `put' index */
167 u_short k_rbuf[KBD_RX_RING_SIZE]; /* rr1, data pairs */
168
169 };
170
171 /* Prototypes */
172 int kbd_docmd(struct kbd_softc *k, int cmd);
173 int kbd_iopen(int unit);
174 void kbd_new_layout(struct kbd_softc *k);
175 void kbd_output(struct kbd_softc *k, int c);
176 void kbd_repeat(void *arg);
177 void kbd_set_leds(struct kbd_softc *k, int leds);
178 void kbd_start_tx(struct kbd_softc *k);
179 void kbd_update_leds(struct kbd_softc *k);
180 void kbd_was_reset(struct kbd_softc *k);
181
182 extern void kd_input(int ascii);
183
184 cdev_decl(kbd); /* open, close, read, write, ioctl, stop, ... */
185
186 struct zsops zsops_kbd;
187
188 /****************************************************************
189 * Definition of the driver for autoconfig.
190 ****************************************************************/
191
192 static int kbd_match(struct device *, void *, void *);
193 static void kbd_attach(struct device *, struct device *, void *);
194
195 struct cfattach kbd_ca = {
196 sizeof(struct kbd_softc), kbd_match, kbd_attach
197 };
198
199 struct cfdriver kbd_cd = {
200 NULL, "kbd", DV_DULL
201 };
202
203
204 /*
205 * kbd_match: how is this zs channel configured?
206 */
207 int
208 kbd_match(parent, match, aux)
209 struct device *parent;
210 void *match, *aux;
211 {
212 struct cfdata *cf = match;
213 struct zsc_attach_args *args = aux;
214
215 /* Exact match required for keyboard. */
216 if (cf->cf_loc[0] == args->channel)
217 return 2;
218
219 return 0;
220 }
221
222 void
223 kbd_attach(parent, self, aux)
224 struct device *parent, *self;
225 void *aux;
226
227 {
228 struct zsc_softc *zsc = (void *) parent;
229 struct kbd_softc *k = (void *) self;
230 struct zsc_attach_args *args = aux;
231 struct zs_chanstate *cs;
232 struct cfdata *cf;
233 int channel, kbd_unit;
234 int reset, s, tconst;
235
236 cf = k->k_dev.dv_cfdata;
237 kbd_unit = k->k_dev.dv_unit;
238 channel = args->channel;
239 cs = &zsc->zsc_cs[channel];
240 cs->cs_private = k;
241 cs->cs_ops = &zsops_kbd;
242 k->k_cs = cs;
243
244 if (args->hwflags & ZS_HWFLAG_CONSOLE) {
245 k->k_isconsole = 1;
246 printf(" (console)");
247 }
248 printf("\n");
249
250 /* Initialize the speed, etc. */
251 tconst = BPS_TO_TCONST(cs->cs_brg_clk, KBD_BPS);
252 s = splzs();
253 if (k->k_isconsole == 0) {
254 /* Not the console; may need reset. */
255 reset = (channel == 0) ?
256 ZSWR9_A_RESET : ZSWR9_B_RESET;
257 zs_write_reg(cs, 9, reset);
258 }
259 /* These are OK as set by zscc: WR3, WR4, WR5 */
260 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
261 cs->cs_preg[12] = tconst;
262 cs->cs_preg[13] = tconst >> 8;
263 zs_loadchannelregs(cs);
264 splx(s);
265
266 /* Do this before any calls to kbd_rint(). */
267 kbd_xlate_init(&k->k_state);
268
269 /* XXX - Do this in open? */
270 k->k_repeat_start = hz/2;
271 k->k_repeat_step = hz/20;
272
273 /* Magic sequence. */
274 k->k_magic1 = KBD_L1;
275 k->k_magic2 = KBD_A;
276
277 /* Now attach the (kd) pseudo-driver. */
278 kd_init(kbd_unit);
279 }
280
281
282 /****************************************************************
283 * Entry points for /dev/kbd
284 * (open,close,read,write,...)
285 ****************************************************************/
286
287 /*
288 * Open:
289 * Check exclusion, open actual device (_iopen),
290 * setup event channel, clear ASCII repeat stuff.
291 */
292 int
293 kbdopen(dev, flags, mode, p)
294 dev_t dev;
295 int flags, mode;
296 struct proc *p;
297 {
298 struct kbd_softc *k;
299 int error, s, unit;
300
301 unit = minor(dev);
302 if (unit >= kbd_cd.cd_ndevs)
303 return (ENXIO);
304 k = kbd_cd.cd_devs[unit];
305 if (k == NULL)
306 return (ENXIO);
307
308 /* Exclusive open required for /dev/kbd */
309 if (k->k_events.ev_io)
310 return (EBUSY);
311 k->k_events.ev_io = p;
312
313 if ((error = kbd_iopen(unit)) != 0) {
314 k->k_events.ev_io = NULL;
315 return (error);
316 }
317 ev_init(&k->k_events);
318 k->k_evmode = 1; /* XXX: OK? */
319
320 if (k->k_repeating) {
321 k->k_repeating = 0;
322 untimeout(kbd_repeat, k);
323 }
324
325 return (0);
326 }
327
328 /*
329 * Close:
330 * Turn off event mode, dump the queue, and close the keyboard
331 * unless it is supplying console input.
332 */
333 int
334 kbdclose(dev, flags, mode, p)
335 dev_t dev;
336 int flags, mode;
337 struct proc *p;
338 {
339 struct kbd_softc *k;
340
341 k = kbd_cd.cd_devs[minor(dev)];
342 k->k_evmode = 0;
343 ev_fini(&k->k_events);
344 k->k_events.ev_io = NULL;
345 return (0);
346 }
347
348 int
349 kbdread(dev, uio, flags)
350 dev_t dev;
351 struct uio *uio;
352 int flags;
353 {
354 struct kbd_softc *k;
355
356 k = kbd_cd.cd_devs[minor(dev)];
357 return (ev_read(&k->k_events, uio, flags));
358 }
359
360 /* this routine should not exist, but is convenient to write here for now */
361 int
362 kbdwrite(dev, uio, flags)
363 dev_t dev;
364 struct uio *uio;
365 int flags;
366 {
367
368 return (EOPNOTSUPP);
369 }
370
371 int
372 kbdpoll(dev, events, p)
373 dev_t dev;
374 int events;
375 struct proc *p;
376 {
377 struct kbd_softc *k;
378
379 k = kbd_cd.cd_devs[minor(dev)];
380 return (ev_poll(&k->k_events, events, p));
381 }
382
383
384 static int kbd_ioccmd(struct kbd_softc *k, int *data);
385 static int kbd_iockeymap __P((struct kbd_state *ks,
386 u_long cmd, struct kiockeymap *kio));
387
388 static int kbd_iocsled(struct kbd_softc *k, int *data);
389
390 #ifdef KIOCGETKEY
391 static int kbd_oldkeymap __P((struct kbd_state *ks,
392 u_long cmd, struct okiockey *okio));
393 #endif
394
395 int
396 kbdioctl(dev, cmd, data, flag, p)
397 dev_t dev;
398 u_long cmd;
399 register caddr_t data;
400 int flag;
401 struct proc *p;
402 {
403 struct kbd_softc *k;
404 struct kbd_state *ks;
405 int *ip;
406 int error = 0;
407
408 k = kbd_cd.cd_devs[minor(dev)];
409 ks = &k->k_state;
410
411 switch (cmd) {
412
413 case KIOCTRANS: /* Set translation mode */
414 ip = (int *)data;
415 /* We only support "raw" mode on /dev/kbd */
416 if (*ip != TR_UNTRANS_EVENT)
417 error = EINVAL;
418 break;
419
420 case KIOCGTRANS: /* Get translation mode */
421 ip = (int *)data;
422 /* We only support "raw" mode on /dev/kbd */
423 *ip = TR_UNTRANS_EVENT;
424 break;
425
426 #ifdef KIOCGETKEY
427 case KIOCGETKEY: /* Get keymap entry (old format) */
428 error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
429 break;
430 #endif KIOCGETKEY */
431
432 case KIOCSKEY: /* Set keymap entry */
433 /* Don't let just anyone hose the keyboard. */
434 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
435 return (error);
436 /* fallthrough */
437 case KIOCGKEY: /* Get keymap entry */
438 error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
439 break;
440
441 case KIOCCMD: /* Send a command to the keyboard */
442 error = kbd_ioccmd(k, (int *)data);
443 break;
444
445 case KIOCTYPE: /* Get keyboard type */
446 ip = (int *)data;
447 *ip = ks->kbd_id;
448 break;
449
450 case KIOCSDIRECT: /* where to send input */
451 ip = (int *)data;
452 k->k_evmode = *ip;
453 break;
454
455 case KIOCLAYOUT: /* Get keyboard layout */
456 *data = ks->kbd_layout;
457 break;
458
459 case KIOCSLED:
460 error = kbd_iocsled(k, (int *)data);
461 break;
462
463 case KIOCGLED:
464 *(char *)data = ks->kbd_leds;
465 break;
466
467 case FIONBIO: /* we will remove this someday (soon???) */
468 break;
469
470 case FIOASYNC:
471 k->k_events.ev_async = *(int *)data != 0;
472 break;
473
474 case TIOCSPGRP:
475 ip = (int *)data;
476 if (*ip != k->k_events.ev_io->p_pgid)
477 error = EPERM;
478 break;
479
480 }
481
482 return (error);
483 }
484
485 /****************************************************************
486 * ioctl helpers
487 ****************************************************************/
488
489 /*
490 * Get/Set keymap entry
491 */
492 static int
493 kbd_iockeymap(ks, cmd, kio)
494 struct kbd_state *ks;
495 u_long cmd;
496 struct kiockeymap *kio;
497 {
498 struct keymap *km;
499 u_int station;
500
501 switch (kio->kio_tablemask) {
502 case KIOC_NOMASK:
503 km = ks->kbd_k.k_normal;
504 break;
505 case KIOC_SHIFTMASK:
506 km = ks->kbd_k.k_shifted;
507 break;
508 case KIOC_CTRLMASK:
509 km = ks->kbd_k.k_control;
510 break;
511 case KIOC_UPMASK:
512 km = ks->kbd_k.k_release;
513 break;
514 default:
515 /* Silently ignore unsupported masks */
516 return (0);
517 }
518
519 /* Range-check the table position. */
520 station = kio->kio_station;
521 if (station >= KEYMAP_SIZE)
522 return (EINVAL);
523
524 switch (cmd) {
525
526 case KIOCGKEY: /* Get keymap entry */
527 kio->kio_entry = km->keymap[station];
528 break;
529
530 case KIOCSKEY: /* Set keymap entry */
531 km->keymap[station] = kio->kio_entry;
532 break;
533
534 default:
535 return(ENOTTY);
536 }
537 return (0);
538 }
539
540 #ifdef KIOCGETKEY
541 /*
542 * Get/Set keymap entry,
543 * old format (compatibility)
544 */
545 int
546 kbd_oldkeymap(ks, cmd, kio)
547 struct kbd_state *ks;
548 u_long cmd;
549 struct okiockey *kio;
550 {
551 int error = 0;
552
553 switch (cmd) {
554
555 case KIOCGETKEY:
556 if (kio->kio_station == 118) {
557 /*
558 * This is X11 asking if a type 3 keyboard is
559 * really a type 3 keyboard. Say yes, it is,
560 * by reporting key station 118 as a "hole".
561 * Note old (SunOS 3.5) definition of HOLE!
562 */
563 kio->kio_entry = 0xA2;
564 break;
565 }
566 /* fall through */
567
568 default:
569 error = ENOTTY;
570 break;
571 }
572
573 return (error);
574 }
575 #endif /* KIOCGETKEY */
576
577
578 /*
579 * keyboard command ioctl
580 * ``unimplemented commands are ignored'' (blech)
581 */
582 static int
583 kbd_ioccmd(k, data)
584 struct kbd_softc *k;
585 int *data;
586 {
587 struct kbd_state *ks = &k->k_state;
588 int cmd, error, s;
589
590 cmd = *data;
591 switch (cmd) {
592
593 case KBD_CMD_BELL:
594 case KBD_CMD_NOBELL:
595 /* Supported by type 2, 3, and 4 keyboards */
596 break;
597
598 case KBD_CMD_CLICK:
599 case KBD_CMD_NOCLICK:
600 /* Unsupported by type 2 keyboards */
601 if (ks->kbd_id <= KB_SUN2)
602 return (0);
603 ks->kbd_click = (cmd == KBD_CMD_CLICK);
604 break;
605
606 default:
607 return (0);
608 }
609
610 s = spltty();
611
612 error = kbd_drain_tx(k);
613 if (error == 0) {
614 kbd_output(k, cmd);
615 kbd_start_tx(k);
616 }
617
618 splx(s);
619
620 return (error);
621 }
622
623 /*
624 * Set LEDs ioctl.
625 */
626 static int
627 kbd_iocsled(k, data)
628 struct kbd_softc *k;
629 int *data;
630 {
631 struct kbd_state *ks = &k->k_state;
632 int leds, error, s;
633
634 leds = *data;
635
636 s = spltty();
637 error = kbd_drain_tx(k);
638 if (error == 0) {
639 kbd_set_leds(k, leds);
640 }
641 splx(s);
642
643 return (error);
644 }
645
646
647 /****************************************************************
648 * middle layers:
649 * - keysym to ASCII sequence
650 * - raw key codes to keysym
651 ****************************************************************/
652
653
654 /*
655 * Initialization done by either kdcninit or kbd_iopen
656 */
657 void
658 kbd_xlate_init(ks)
659 struct kbd_state *ks;
660 {
661 struct keyboard *ktbls;
662 int id;
663
664 id = ks->kbd_id;
665 if (id < KBD_MIN_TYPE)
666 id = KBD_MIN_TYPE;
667 if (id > kbd_max_type)
668 id = kbd_max_type;
669 ktbls = keyboards[id];
670
671 ks->kbd_k = *ktbls; /* struct assignment */
672 ks->kbd_modbits = 0;
673 }
674
675 /*
676 * Turn keyboard up/down codes into a KEYSYM.
677 * Note that the "kd" driver uses this too!
678 */
679 int
680 kbd_code_to_keysym(ks, c)
681 register struct kbd_state *ks;
682 register int c;
683 {
684 struct keymap *km;
685 int keysym;
686
687 /*
688 * Get keymap pointer. One of these:
689 * release, control, shifted, normal, ...
690 */
691 if (KEY_UP(c))
692 km = ks->kbd_k.k_release;
693 else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
694 km = ks->kbd_k.k_control;
695 else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
696 km = ks->kbd_k.k_shifted;
697 else
698 km = ks->kbd_k.k_normal;
699
700 if (km == NULL) {
701 /*
702 * Do not know how to translate yet.
703 * We will find out when a RESET comes along.
704 */
705 return (KEYSYM_NOP);
706 }
707 keysym = km->keymap[KEY_CODE(c)];
708
709 /*
710 * Post-processing for Caps-lock
711 */
712 if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
713 (KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
714 {
715 if (('a' <= keysym) && (keysym <= 'z'))
716 keysym -= ('a' - 'A');
717 }
718
719 /*
720 * Post-processing for Num-lock
721 */
722 if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
723 (KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
724 {
725 keysym = kbd_numlock_map[keysym & 0x3F];
726 }
727
728 return (keysym);
729 }
730
731 void
732 kbd_input_string(k, str)
733 struct kbd_softc *k;
734 char *str;
735 {
736 while (*str) {
737 kd_input(*str);
738 str++;
739 }
740 }
741
742 void
743 kbd_input_funckey(k, keysym)
744 struct kbd_softc *k;
745 register int keysym;
746 {
747 register int n;
748 char str[12];
749
750 /*
751 * Format the F-key sequence and send as a string.
752 * XXX: Ugly compatibility mappings.
753 */
754 n = 0xC0 + (keysym & 0x3F);
755 sprintf(str, "\033[%dz", n);
756 kbd_input_string(k, str);
757 }
758
759 /*
760 * This is called by kbd_input_raw() or by kb_repeat()
761 * to deliver ASCII input. Called at spltty().
762 */
763 void
764 kbd_input_keysym(k, keysym)
765 struct kbd_softc *k;
766 register int keysym;
767 {
768 struct kbd_state *ks = &k->k_state;
769 register int data;
770
771 switch (KEYSYM_CLASS(keysym)) {
772
773 case KEYSYM_ASCII:
774 data = KEYSYM_DATA(keysym);
775 if (ks->kbd_modbits & KBMOD_META_MASK)
776 data |= 0x80;
777 kd_input(data);
778 break;
779
780 case KEYSYM_STRING:
781 data = keysym & 0xF;
782 kbd_input_string(k, kbd_stringtab[data]);
783 break;
784
785 case KEYSYM_FUNC:
786 kbd_input_funckey(k, keysym);
787 break;
788
789 case KEYSYM_CLRMOD:
790 data = 1 << (keysym & 0x1F);
791 ks->kbd_modbits &= ~data;
792 break;
793
794 case KEYSYM_SETMOD:
795 data = 1 << (keysym & 0x1F);
796 ks->kbd_modbits |= data;
797 break;
798
799 case KEYSYM_INVMOD:
800 data = 1 << (keysym & 0x1F);
801 ks->kbd_modbits ^= data;
802 kbd_update_leds(k);
803 break;
804
805 case KEYSYM_ALL_UP:
806 ks->kbd_modbits &= ~0xFFFF;
807 break;
808
809 case KEYSYM_SPECIAL:
810 if (keysym == KEYSYM_NOP)
811 break;
812 /* fall through */
813 default:
814 log(LOG_WARNING, "%s: unexpected keysym 0x%x\n",
815 k->k_dev.dv_xname, keysym);
816 break;
817 }
818 }
819
820 /*
821 * This is the autorepeat timeout function.
822 * Called at splsoftclock().
823 */
824 void
825 kbd_repeat(void *arg)
826 {
827 struct kbd_softc *k = (struct kbd_softc *)arg;
828 int s = spltty();
829
830 if (k->k_repeating && k->k_repeatsym >= 0) {
831 kbd_input_keysym(k, k->k_repeatsym);
832 timeout(kbd_repeat, k, k->k_repeat_step);
833 }
834 splx(s);
835 }
836
837 /*
838 * Called by our kbd_softint() routine on input,
839 * which passes the raw hardware scan codes.
840 * Called at spltty()
841 */
842 void
843 kbd_input_raw(k, c)
844 struct kbd_softc *k;
845 register int c;
846 {
847 struct kbd_state *ks = &k->k_state;
848 struct firm_event *fe;
849 int put, keysym;
850
851 /* XXX - Input errors already handled. */
852
853 /* Are we expecting special input? */
854 if (ks->kbd_expect) {
855 if (ks->kbd_expect & KBD_EXPECT_IDCODE) {
856 /* We read a KBD_RESET last time. */
857 ks->kbd_id = c;
858 kbd_was_reset(k);
859 }
860 if (ks->kbd_expect & KBD_EXPECT_LAYOUT) {
861 /* We read a KBD_LAYOUT last time. */
862 ks->kbd_layout = c;
863 kbd_new_layout(k);
864 }
865 ks->kbd_expect = 0;
866 return;
867 }
868
869 /* Is this one of the "special" input codes? */
870 if (KBD_SPECIAL(c)) {
871 switch (c) {
872 case KBD_RESET:
873 ks->kbd_expect |= KBD_EXPECT_IDCODE;
874 /* Fake an "all-up" to resync. translation. */
875 c = KBD_IDLE;
876 break;
877
878 case KBD_LAYOUT:
879 ks->kbd_expect |= KBD_EXPECT_LAYOUT;
880 return;
881
882 case KBD_ERROR:
883 log(LOG_WARNING, "%s: received error indicator\n",
884 k->k_dev.dv_xname);
885 return;
886
887 case KBD_IDLE:
888 /* Let this go to the translator. */
889 break;
890 }
891 }
892
893 /*
894 * If /dev/kbd is not connected in event mode,
895 * translate and send upstream (to console).
896 */
897 if (!k->k_evmode) {
898
899 /* Any input stops auto-repeat (i.e. key release). */
900 if (k->k_repeating) {
901 k->k_repeating = 0;
902 untimeout(kbd_repeat, k);
903 }
904
905 /* Translate this code to a keysym */
906 keysym = kbd_code_to_keysym(ks, c);
907
908 /* Pass up to the next layer. */
909 kbd_input_keysym(k, keysym);
910
911 /* Does this symbol get auto-repeat? */
912 if (KEYSYM_NOREPEAT(keysym))
913 return;
914
915 /* Setup for auto-repeat after initial delay. */
916 k->k_repeating = 1;
917 k->k_repeatsym = keysym;
918 timeout(kbd_repeat, k, k->k_repeat_start);
919 return;
920 }
921
922 /*
923 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
924 * This is bad as it means the server will not automatically resync
925 * on all-up IDLEs, but I did not drop them before, and the server
926 * goes crazy when it comes time to blank the screen....
927 */
928 if (c == KBD_IDLE)
929 return;
930
931 /*
932 * Keyboard is generating events. Turn this keystroke into an
933 * event and put it in the queue. If the queue is full, the
934 * keystroke is lost (sorry!).
935 */
936 put = k->k_events.ev_put;
937 fe = &k->k_events.ev_q[put];
938 put = (put + 1) % EV_QSIZE;
939 if (put == k->k_events.ev_get) {
940 log(LOG_WARNING, "%s: event queue overflow\n",
941 k->k_dev.dv_xname); /* ??? */
942 return;
943 }
944 fe->id = KEY_CODE(c);
945 fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
946 fe->time = time;
947 k->k_events.ev_put = put;
948 EV_WAKEUP(&k->k_events);
949 }
950
951 /****************************************************************
952 * Interface to the lower layer (zscc)
953 ****************************************************************/
954
955 static void
956 kbd_rxint(cs)
957 register struct zs_chanstate *cs;
958 {
959 register struct kbd_softc *k;
960 register int put, put_next;
961 register u_char c, rr1;
962
963 k = cs->cs_private;
964 put = k->k_rbput;
965
966 /*
967 * First read the status, because reading the received char
968 * destroys the status of this char.
969 */
970 rr1 = zs_read_reg(cs, 1);
971 c = zs_read_data(cs);
972
973 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
974 /* Clear the receive error. */
975 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
976 }
977
978 /*
979 * Check NOW for a console abort sequence, so that we can
980 * abort even when interrupts are locking up the machine.
981 */
982 if (k->k_magic1_down) {
983 /* The last keycode was "MAGIC1" down. */
984 k->k_magic1_down = 0;
985 if ((c == k->k_magic2) && k->k_isconsole) {
986 /* Magic "L1-A" sequence; enter debugger. */
987 zs_abort();
988 /* Debugger done. Fake L1-up to finish it. */
989 c = k->k_magic1 | KBD_UP;
990 }
991 }
992 if (c == k->k_magic1) {
993 k->k_magic1_down = 1;
994 }
995
996 k->k_rbuf[put] = (c << 8) | rr1;
997 put_next = (put + 1) & KBD_RX_RING_MASK;
998
999 /* Would overrun if increment makes (put==get). */
1000 if (put_next == k->k_rbget) {
1001 k->k_intr_flags |= INTR_RX_OVERRUN;
1002 } else {
1003 /* OK, really increment. */
1004 put = put_next;
1005 }
1006
1007 /* Done reading. */
1008 k->k_rbput = put;
1009
1010 /* Ask for softint() call. */
1011 cs->cs_softreq = 1;
1012 }
1013
1014
1015 static void
1016 kbd_txint(cs)
1017 register struct zs_chanstate *cs;
1018 {
1019 register struct kbd_softc *k;
1020
1021 k = cs->cs_private;
1022 zs_write_csr(cs, ZSWR0_RESET_TXINT);
1023 k->k_intr_flags |= INTR_TX_EMPTY;
1024 /* Ask for softint() call. */
1025 cs->cs_softreq = 1;
1026 }
1027
1028
1029 static void
1030 kbd_stint(cs)
1031 register struct zs_chanstate *cs;
1032 {
1033 register struct kbd_softc *k;
1034 register int rr0;
1035
1036 k = cs->cs_private;
1037
1038 rr0 = zs_read_csr(cs);
1039 zs_write_csr(cs, ZSWR0_RESET_STATUS);
1040
1041 #if 0
1042 if (rr0 & ZSRR0_BREAK) {
1043 /* Keyboard unplugged? */
1044 zs_abort();
1045 return (0);
1046 }
1047 #endif
1048
1049 /*
1050 * We have to accumulate status line changes here.
1051 * Otherwise, if we get multiple status interrupts
1052 * before the softint runs, we could fail to notice
1053 * some status line changes in the softint routine.
1054 * Fix from Bill Studenmund, October 1996.
1055 */
1056 cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
1057 cs->cs_rr0 = rr0;
1058 k->k_intr_flags |= INTR_ST_CHECK;
1059
1060 /* Ask for softint() call. */
1061 cs->cs_softreq = 1;
1062 }
1063
1064 /*
1065 * Get input from the recieve ring and pass it on.
1066 * Note: this is called at splsoftclock()
1067 */
1068 static void
1069 kbd_softint(cs)
1070 struct zs_chanstate *cs;
1071 {
1072 register struct kbd_softc *k;
1073 register int get, c, s;
1074 int intr_flags;
1075 register u_short ring_data;
1076 register u_char rr0, rr1;
1077
1078 k = cs->cs_private;
1079
1080 /* Atomically get and clear flags. */
1081 s = splzs();
1082 intr_flags = k->k_intr_flags;
1083 k->k_intr_flags = 0;
1084
1085 /* Now lower to spltty for the rest. */
1086 (void) spltty();
1087
1088 /*
1089 * Copy data from the receive ring to the event layer.
1090 */
1091 get = k->k_rbget;
1092 while (get != k->k_rbput) {
1093 ring_data = k->k_rbuf[get];
1094 get = (get + 1) & KBD_RX_RING_MASK;
1095
1096 /* low byte of ring_data is rr1 */
1097 c = (ring_data >> 8) & 0xff;
1098
1099 if (ring_data & ZSRR1_DO)
1100 intr_flags |= INTR_RX_OVERRUN;
1101 if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
1102 /*
1103 * After garbage, flush pending input, and
1104 * send a reset to resync key translation.
1105 */
1106 log(LOG_ERR, "%s: input error (0x%x)\n",
1107 k->k_dev.dv_xname, ring_data);
1108 get = k->k_rbput; /* flush */
1109 goto send_reset;
1110 }
1111
1112 /* Pass this up to the "middle" layer. */
1113 kbd_input_raw(k, c);
1114 }
1115 if (intr_flags & INTR_RX_OVERRUN) {
1116 log(LOG_ERR, "%s: input overrun\n",
1117 k->k_dev.dv_xname);
1118 send_reset:
1119 /* Send a reset to resync translation. */
1120 kbd_output(k, KBD_CMD_RESET);
1121 kbd_start_tx(k);
1122 }
1123 k->k_rbget = get;
1124
1125 if (intr_flags & INTR_TX_EMPTY) {
1126 /*
1127 * Transmit done. Try to send more, or
1128 * clear busy and wakeup drain waiters.
1129 */
1130 k->k_txflags &= ~K_TXBUSY;
1131 kbd_start_tx(k);
1132 }
1133
1134 if (intr_flags & INTR_ST_CHECK) {
1135 /*
1136 * Status line change. (Not expected.)
1137 */
1138 log(LOG_ERR, "%s: status interrupt?\n",
1139 k->k_dev.dv_xname);
1140 cs->cs_rr0_delta = 0;
1141 }
1142
1143 splx(s);
1144 }
1145
1146 struct zsops zsops_kbd = {
1147 kbd_rxint, /* receive char available */
1148 kbd_stint, /* external/status */
1149 kbd_txint, /* xmit buffer empty */
1150 kbd_softint, /* process software interrupt */
1151 };
1152
1153 /****************************************************************
1154 * misc...
1155 ****************************************************************/
1156
1157 /*
1158 * Initialization to be done at first open.
1159 * This is called from kbdopen or kdopen (in kd.c)
1160 * Called with user context.
1161 */
1162 int
1163 kbd_iopen(unit)
1164 int unit;
1165 {
1166 struct kbd_softc *k;
1167 struct kbd_state *ks;
1168 int error, s;
1169
1170 if (unit >= kbd_cd.cd_ndevs)
1171 return (ENXIO);
1172 k = kbd_cd.cd_devs[unit];
1173 if (k == NULL)
1174 return (ENXIO);
1175 ks = &k->k_state;
1176 error = 0;
1177
1178 /* Tolerate extra calls. */
1179 if (k->k_isopen)
1180 return (error);
1181
1182 s = spltty();
1183
1184 /* Reset the keyboard and find out its type. */
1185 kbd_output(k, KBD_CMD_RESET);
1186 kbd_start_tx(k);
1187 kbd_drain_tx(k);
1188 /* The wakeup for this is in kbd_was_reset(). */
1189 error = tsleep((caddr_t)&ks->kbd_id,
1190 PZERO | PCATCH, devopn, hz);
1191 if (error == EWOULDBLOCK) { /* no response */
1192 error = 0;
1193 log(LOG_ERR, "%s: reset failed\n",
1194 k->k_dev.dv_xname);
1195 /*
1196 * Allow the open anyway (to keep getty happy)
1197 * but assume the "least common denominator".
1198 */
1199 ks->kbd_id = KB_SUN2;
1200 }
1201
1202 /* Earlier than type 4 does not know "layout". */
1203 if (ks->kbd_id < KB_SUN4)
1204 goto out;
1205
1206 /* Ask for the layout. */
1207 kbd_output(k, KBD_CMD_GETLAYOUT);
1208 kbd_start_tx(k);
1209 kbd_drain_tx(k);
1210 /* The wakeup for this is in kbd_new_layout(). */
1211 error = tsleep((caddr_t)&ks->kbd_layout,
1212 PZERO | PCATCH, devopn, hz);
1213 if (error == EWOULDBLOCK) { /* no response */
1214 error = 0;
1215 log(LOG_ERR, "%s: no response to get_layout\n",
1216 k->k_dev.dv_xname);
1217 ks->kbd_layout = 0;
1218 }
1219
1220 out:
1221 splx(s);
1222
1223 if (error == 0)
1224 k->k_isopen = 1;
1225
1226 return error;
1227 }
1228
1229 /*
1230 * Called by kbd_input_raw, at spltty()
1231 */
1232 void
1233 kbd_was_reset(k)
1234 struct kbd_softc *k;
1235 {
1236 struct kbd_state *ks = &k->k_state;
1237
1238 /*
1239 * On first identification, wake up anyone waiting for type
1240 * and set up the table pointers.
1241 */
1242 wakeup((caddr_t)&ks->kbd_id);
1243
1244 /* Restore keyclick, if necessary */
1245 switch (ks->kbd_id) {
1246
1247 case KB_SUN2:
1248 /* Type 2 keyboards don't support keyclick */
1249 break;
1250
1251 case KB_SUN3:
1252 /* Type 3 keyboards come up with keyclick on */
1253 if (!ks->kbd_click) {
1254 /* turn off the click */
1255 kbd_output(k, KBD_CMD_NOCLICK);
1256 kbd_start_tx(k);
1257 }
1258 break;
1259
1260 case KB_SUN4:
1261 /* Type 4 keyboards come up with keyclick off */
1262 if (ks->kbd_click) {
1263 /* turn on the click */
1264 kbd_output(k, KBD_CMD_CLICK);
1265 kbd_start_tx(k);
1266 }
1267 break;
1268 }
1269
1270 /* LEDs are off after reset. */
1271 ks->kbd_leds = 0;
1272 }
1273
1274 /*
1275 * Called by kbd_input_raw, at spltty()
1276 */
1277 void
1278 kbd_new_layout(k)
1279 struct kbd_softc *k;
1280 {
1281 struct kbd_state *ks = &k->k_state;
1282
1283 /*
1284 * On first identification, wake up anyone waiting for type
1285 * and set up the table pointers.
1286 */
1287 wakeup((caddr_t)&ks->kbd_layout);
1288
1289 /* XXX: switch decoding tables? */
1290 }
1291
1292
1293 /*
1294 * Wait for output to finish.
1295 * Called at spltty(). Has user context.
1296 */
1297 int
1298 kbd_drain_tx(k)
1299 struct kbd_softc *k;
1300 {
1301 int error;
1302
1303 error = 0;
1304
1305 while (k->k_txflags & K_TXBUSY) {
1306 k->k_txflags |= K_TXWANT;
1307 error = tsleep((caddr_t)&k->k_txflags,
1308 PZERO | PCATCH, "kbdout", 0);
1309 }
1310
1311 return (error);
1312 }
1313
1314 /*
1315 * Enqueue some output for the keyboard
1316 * Called at spltty().
1317 */
1318 void
1319 kbd_output(k, c)
1320 struct kbd_softc *k;
1321 int c; /* the data */
1322 {
1323 struct zs_chanstate *cs = k->k_cs;
1324 int put;
1325
1326 put = k->k_tbput;
1327 k->k_tbuf[put] = (u_char)c;
1328 put = (put + 1) & KBD_TX_RING_MASK;
1329
1330 /* Would overrun if increment makes (put==get). */
1331 if (put == k->k_tbget) {
1332 log(LOG_WARNING, "%s: output overrun\n",
1333 k->k_dev.dv_xname);
1334 } else {
1335 /* OK, really increment. */
1336 k->k_tbput = put;
1337 }
1338 }
1339
1340 /*
1341 * Start the sending data from the output queue
1342 * Called at spltty().
1343 */
1344 void
1345 kbd_start_tx(k)
1346 struct kbd_softc *k;
1347 {
1348 struct zs_chanstate *cs = k->k_cs;
1349 int get, s;
1350 u_char c;
1351
1352 if (k->k_txflags & K_TXBUSY)
1353 return;
1354
1355 /* Is there anything to send? */
1356 get = k->k_tbget;
1357 if (get == k->k_tbput) {
1358 /* Nothing to send. Wake drain waiters. */
1359 if (k->k_txflags & K_TXWANT) {
1360 k->k_txflags &= ~K_TXWANT;
1361 wakeup((caddr_t)&k->k_txflags);
1362 }
1363 return;
1364 }
1365
1366 /* Have something to send. */
1367 c = k->k_tbuf[get];
1368 get = (get + 1) & KBD_TX_RING_MASK;
1369 k->k_tbget = get;
1370 k->k_txflags |= K_TXBUSY;
1371
1372 /* Need splzs to avoid interruption of the delay. */
1373 s = splzs();
1374 zs_write_data(cs, c);
1375 splx(s);
1376 }
1377
1378 /*
1379 * Called at spltty by:
1380 * kbd_update_leds, kbd_iocsled
1381 */
1382 void
1383 kbd_set_leds(k, new_leds)
1384 struct kbd_softc *k;
1385 int new_leds;
1386 {
1387 struct kbd_state *ks = &k->k_state;
1388
1389 /* Don't send unless state changes. */
1390 if (ks->kbd_leds == new_leds)
1391 return;
1392
1393 ks->kbd_leds = new_leds;
1394
1395 /* Only type 4 and later has LEDs anyway. */
1396 if (ks->kbd_id < 4)
1397 return;
1398
1399 kbd_output(k, KBD_CMD_SETLED);
1400 kbd_output(k, new_leds);
1401 kbd_start_tx(k);
1402 }
1403
1404 /*
1405 * Called at spltty by:
1406 * kbd_input_keysym
1407 */
1408 void
1409 kbd_update_leds(k)
1410 struct kbd_softc *k;
1411 {
1412 struct kbd_state *ks = &k->k_state;
1413 register char leds;
1414
1415 leds = ks->kbd_leds;
1416 leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
1417
1418 if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
1419 leds |= LED_CAPS_LOCK;
1420 if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
1421 leds |= LED_NUM_LOCK;
1422
1423 kbd_set_leds(k, leds);
1424 }
1425
1426