kbd.c revision 1.38 1 /* $NetBSD: kbd.c,v 1.38 2003/08/07 16:31:24 agc 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. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)kbd.c 8.2 (Berkeley) 10/30/93
41 */
42
43 /*
44 * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
45 * [yet?]). Translates incoming bytes to ASCII or to `firm_events' and
46 * passes them up to the appropriate reader.
47 */
48
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.38 2003/08/07 16:31:24 agc Exp $");
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/conf.h>
55 #include <sys/device.h>
56 #include <sys/ioctl.h>
57 #include <sys/kernel.h>
58 #include <sys/proc.h>
59 #include <sys/malloc.h>
60 #include <sys/signal.h>
61 #include <sys/signalvar.h>
62 #include <sys/time.h>
63 #include <sys/syslog.h>
64 #include <sys/select.h>
65 #include <sys/poll.h>
66 #include <sys/file.h>
67
68 #include <dev/wscons/wsksymdef.h>
69
70 #include <dev/sun/kbd_reg.h>
71 #include <dev/sun/kbio.h>
72 #include <dev/sun/vuid_event.h>
73 #include <dev/sun/event_var.h>
74 #include <dev/sun/kbd_xlate.h>
75 #include <dev/sun/kbdvar.h>
76
77 #include "locators.h"
78
79 extern struct cfdriver kbd_cd;
80
81 dev_type_open(kbdopen);
82 dev_type_close(kbdclose);
83 dev_type_read(kbdread);
84 dev_type_ioctl(kbdioctl);
85 dev_type_poll(kbdpoll);
86 dev_type_kqfilter(kbdkqfilter);
87
88 const struct cdevsw kbd_cdevsw = {
89 kbdopen, kbdclose, kbdread, nowrite, kbdioctl,
90 nostop, notty, kbdpoll, nommap, kbdkqfilter
91 };
92
93 #if NWSKBD > 0
94 int wssunkbd_enable __P((void *, int));
95 void wssunkbd_set_leds __P((void *, int));
96 int wssunkbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
97
98 void sunkbd_wskbd_cngetc __P((void *, u_int *, int *));
99 void sunkbd_wskbd_cnpollc __P((void *, int));
100 void sunkbd_wskbd_cnbell __P((void *, u_int, u_int, u_int));
101 static void sunkbd_bell_off(void *v);
102
103 const struct wskbd_accessops sunkbd_wskbd_accessops = {
104 wssunkbd_enable,
105 wssunkbd_set_leds,
106 wssunkbd_ioctl,
107 };
108
109 extern const struct wscons_keydesc wssun_keydesctab[];
110 const struct wskbd_mapdata sunkbd_wskbd_keymapdata = {
111 wssun_keydesctab,
112 #ifdef SUNKBD_LAYOUT
113 SUNKBD_LAYOUT,
114 #else
115 KB_US,
116 #endif
117 };
118
119 const struct wskbd_consops sunkbd_wskbd_consops = {
120 sunkbd_wskbd_cngetc,
121 sunkbd_wskbd_cnpollc,
122 sunkbd_wskbd_cnbell,
123 };
124
125 void kbd_wskbd_attach(struct kbd_softc *k, int isconsole);
126 #endif
127
128 /* ioctl helpers */
129 static int kbd_iockeymap(struct kbd_state *ks,
130 u_long cmd, struct kiockeymap *kio);
131 #ifdef KIOCGETKEY
132 static int kbd_oldkeymap(struct kbd_state *ks,
133 u_long cmd, struct okiockey *okio);
134 #endif
135
136
137 /* callbacks for console driver */
138 static int kbd_cc_open(struct cons_channel *);
139 static int kbd_cc_close(struct cons_channel *);
140
141 /* console input */
142 static void kbd_input_console(struct kbd_softc *, int);
143 static void kbd_repeat(void *);
144 static int kbd_input_keysym(struct kbd_softc *, int);
145 static void kbd_input_string(struct kbd_softc *, char *);
146 static void kbd_input_funckey(struct kbd_softc *, int);
147 static void kbd_update_leds(struct kbd_softc *);
148
149 #if NWSKBD > 0
150 static void kbd_input_wskbd(struct kbd_softc *, int);
151 #endif
152
153 /* firm events input */
154 static void kbd_input_event(struct kbd_softc *, int);
155
156
157
158 /****************************************************************
159 * Entry points for /dev/kbd
160 * (open,close,read,write,...)
161 ****************************************************************/
162
163 /*
164 * Open:
165 * Check exclusion, open actual device (_iopen),
166 * setup event channel, clear ASCII repeat stuff.
167 */
168 int
169 kbdopen(dev, flags, mode, p)
170 dev_t dev;
171 int flags, mode;
172 struct proc *p;
173 {
174 struct kbd_softc *k;
175 int error, unit;
176
177 /* locate device */
178 unit = minor(dev);
179 if (unit >= kbd_cd.cd_ndevs)
180 return (ENXIO);
181 k = kbd_cd.cd_devs[unit];
182 if (k == NULL)
183 return (ENXIO);
184
185 /*
186 * NB: wscons support: while we can track if wskbd has called
187 * enable(), we can't tell if that's for console input or for
188 * events input, so we should probably just let the open to
189 * always succeed regardless (e.g. Xsun opening /dev/kbd).
190 */
191
192 /* exclusive open required for /dev/kbd */
193 if (k->k_events.ev_io)
194 return (EBUSY);
195 k->k_events.ev_io = p;
196
197 /* stop pending autorepeat of console input */
198 if (k->k_repeating) {
199 k->k_repeating = 0;
200 callout_stop(&k->k_repeat_ch);
201 }
202
203 /* open actual underlying device */
204 if (k->k_ops != NULL && k->k_ops->open != NULL)
205 if ((error = (*k->k_ops->open)(k)) != 0) {
206 k->k_events.ev_io = NULL;
207 return (error);
208 }
209
210 ev_init(&k->k_events);
211 k->k_evmode = 0; /* XXX: OK? */
212
213 return (0);
214 }
215
216
217 /*
218 * Close:
219 * Turn off event mode, dump the queue, and close the keyboard
220 * unless it is supplying console input.
221 */
222 int
223 kbdclose(dev, flags, mode, p)
224 dev_t dev;
225 int flags, mode;
226 struct proc *p;
227 {
228 struct kbd_softc *k;
229
230 k = kbd_cd.cd_devs[minor(dev)];
231 k->k_evmode = 0;
232 ev_fini(&k->k_events);
233 k->k_events.ev_io = NULL;
234
235 if (k->k_ops != NULL && k->k_ops->close != NULL) {
236 int error;
237 if ((error = (*k->k_ops->close)(k)) != 0)
238 return (error);
239 }
240 return (0);
241 }
242
243
244 int
245 kbdread(dev, uio, flags)
246 dev_t dev;
247 struct uio *uio;
248 int flags;
249 {
250 struct kbd_softc *k;
251
252 k = kbd_cd.cd_devs[minor(dev)];
253 return (ev_read(&k->k_events, uio, flags));
254 }
255
256
257 int
258 kbdpoll(dev, events, p)
259 dev_t dev;
260 int events;
261 struct proc *p;
262 {
263 struct kbd_softc *k;
264
265 k = kbd_cd.cd_devs[minor(dev)];
266 return (ev_poll(&k->k_events, events, p));
267 }
268
269 int
270 kbdkqfilter(dev, kn)
271 dev_t dev;
272 struct knote *kn;
273 {
274 struct kbd_softc *k;
275
276 k = kbd_cd.cd_devs[minor(dev)];
277 return (ev_kqfilter(&k->k_events, kn));
278 }
279
280 int
281 kbdioctl(dev, cmd, data, flag, p)
282 dev_t dev;
283 u_long cmd;
284 caddr_t data;
285 int flag;
286 struct proc *p;
287 {
288 struct kbd_softc *k;
289 struct kbd_state *ks;
290 int error = 0;
291
292 k = kbd_cd.cd_devs[minor(dev)];
293 ks = &k->k_state;
294
295 switch (cmd) {
296
297 case KIOCTRANS: /* Set translation mode */
298 /* We only support "raw" mode on /dev/kbd */
299 if (*(int *)data != TR_UNTRANS_EVENT)
300 error = EINVAL;
301 break;
302
303 case KIOCGTRANS: /* Get translation mode */
304 /* We only support "raw" mode on /dev/kbd */
305 *(int *)data = TR_UNTRANS_EVENT;
306 break;
307
308 #ifdef KIOCGETKEY
309 case KIOCGETKEY: /* Get keymap entry (old format) */
310 error = kbd_oldkeymap(ks, cmd, (struct okiockey *)data);
311 break;
312 #endif /* KIOCGETKEY */
313
314 case KIOCSKEY: /* Set keymap entry */
315 /* FALLTHROUGH */
316 case KIOCGKEY: /* Get keymap entry */
317 error = kbd_iockeymap(ks, cmd, (struct kiockeymap *)data);
318 break;
319
320 case KIOCCMD: /* Send a command to the keyboard */
321 /* pass it to the middle layer */
322 if (k->k_ops != NULL && k->k_ops->docmd != NULL)
323 error = (*k->k_ops->docmd)(k, *(int *)data, 1);
324 break;
325
326 case KIOCTYPE: /* Get keyboard type */
327 *(int *)data = ks->kbd_id;
328 break;
329
330 case KIOCSDIRECT: /* Where to send input */
331 k->k_evmode = *(int *)data;
332 break;
333
334 case KIOCLAYOUT: /* Get keyboard layout */
335 *(int *)data = ks->kbd_layout;
336 break;
337
338 case KIOCSLED: /* Set keyboard LEDs */
339 /* pass the request to the middle layer */
340 if (k->k_ops != NULL && k->k_ops->setleds != NULL)
341 error = (*k->k_ops->setleds)(k, *(char *)data, 1);
342 break;
343
344 case KIOCGLED: /* Get keyboard LEDs */
345 *(char *)data = ks->kbd_leds;
346 break;
347
348 case FIONBIO: /* we will remove this someday (soon???) */
349 break;
350
351 case FIOASYNC:
352 k->k_events.ev_async = (*(int *)data != 0);
353 break;
354
355 case TIOCSPGRP:
356 if (*(int *)data != k->k_events.ev_io->p_pgid)
357 error = EPERM;
358 break;
359
360 default:
361 error = ENOTTY;
362 break;
363 }
364
365 return (error);
366 }
367
368
369 /****************************************************************
370 * ioctl helpers
371 ****************************************************************/
372
373 /*
374 * Get/Set keymap entry
375 */
376 static int
377 kbd_iockeymap(ks, cmd, kio)
378 struct kbd_state *ks;
379 u_long cmd;
380 struct kiockeymap *kio;
381 {
382 u_short *km;
383 u_int station;
384
385 switch (kio->kio_tablemask) {
386 case KIOC_NOMASK:
387 km = ks->kbd_k.k_normal;
388 break;
389 case KIOC_SHIFTMASK:
390 km = ks->kbd_k.k_shifted;
391 break;
392 case KIOC_CTRLMASK:
393 km = ks->kbd_k.k_control;
394 break;
395 case KIOC_UPMASK:
396 km = ks->kbd_k.k_release;
397 break;
398 default:
399 /* Silently ignore unsupported masks */
400 return (0);
401 }
402
403 /* Range-check the table position. */
404 station = kio->kio_station;
405 if (station >= KEYMAP_SIZE)
406 return (EINVAL);
407
408 switch (cmd) {
409
410 case KIOCGKEY: /* Get keymap entry */
411 kio->kio_entry = km[station];
412 break;
413
414 case KIOCSKEY: /* Set keymap entry */
415 km[station] = kio->kio_entry;
416 break;
417
418 default:
419 return(ENOTTY);
420 }
421 return (0);
422 }
423
424
425 #ifdef KIOCGETKEY
426 /*
427 * Get/Set keymap entry,
428 * old format (compatibility)
429 */
430 int
431 kbd_oldkeymap(ks, cmd, kio)
432 struct kbd_state *ks;
433 u_long cmd;
434 struct okiockey *kio;
435 {
436 int error = 0;
437
438 switch (cmd) {
439
440 case KIOCGETKEY:
441 if (kio->kio_station == 118) {
442 /*
443 * This is X11 asking if a type 3 keyboard is
444 * really a type 3 keyboard. Say yes, it is,
445 * by reporting key station 118 as a "hole".
446 * Note old (SunOS 3.5) definition of HOLE!
447 */
448 kio->kio_entry = 0xA2;
449 break;
450 }
451 /* fall through */
452
453 default:
454 error = ENOTTY;
455 break;
456 }
457
458 return (error);
459 }
460 #endif /* KIOCGETKEY */
461
462
463
464 /****************************************************************
465 * Keyboard input - called by middle layer at spltty().
466 ****************************************************************/
467
468 void
469 kbd_input(k, code)
470 struct kbd_softc *k;
471 int code;
472 {
473 if (k->k_evmode) {
474
475 #ifdef KBD_IDLE_EVENTS
476 /*
477 * XXX: is this still true?
478 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
479 * This is bad as it means the server will not automatically resync
480 * on all-up IDLEs, but I did not drop them before, and the server
481 * goes crazy when it comes time to blank the screen....
482 */
483 if (code == KBD_IDLE)
484 return;
485 #endif
486
487 /*
488 * Keyboard is generating firm events. Turn this keystroke
489 * into an event and put it in the queue.
490 */
491 kbd_input_event(k, code);
492 return;
493 }
494
495 #if NWSKBD > 0
496 if (k->k_wskbd != NULL && k->k_wsenabled) {
497 /*
498 * We are using wskbd input mode, pass the event up.
499 */
500 kbd_input_wskbd(k, code);
501 return;
502 }
503 #endif
504
505 /*
506 * If /dev/kbd is not connected in event mode, or wskbd mode,
507 * translate and send upstream (to console).
508 */
509 kbd_input_console(k, code);
510 }
511
512
513
514 /****************************************************************
515 * Open/close routines called upon opening /dev/console
516 * if we serve console input.
517 ****************************************************************/
518
519 struct cons_channel *
520 kbd_cc_alloc(k)
521 struct kbd_softc *k;
522 {
523 struct cons_channel *cc;
524
525 if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
526 return (NULL);
527
528 /* our callbacks for the console driver */
529 cc->cc_dev = k;
530 cc->cc_iopen = kbd_cc_open;
531 cc->cc_iclose = kbd_cc_close;
532
533 /* will be provided by the console driver so that we can feed input */
534 cc->cc_upstream = NULL;
535
536 /*
537 * TODO: clean up cons_attach_input() vs kd_attach_input() in
538 * lower layers and move that code here.
539 */
540
541 k->k_cc = cc;
542 return (cc);
543 }
544
545
546 static int
547 kbd_cc_open(cc)
548 struct cons_channel *cc;
549 {
550 struct kbd_softc *k;
551 int ret;
552
553 if (cc == NULL)
554 return (0);
555
556 k = (struct kbd_softc *)cc->cc_dev;
557 if (k == NULL)
558 return (0);
559
560 if (k->k_ops != NULL && k->k_ops->open != NULL)
561 ret = (*k->k_ops->open)(k);
562 else
563 ret = 0;
564
565 /* XXX: verify that callout is not active? */
566 k->k_repeat_start = hz/2;
567 k->k_repeat_step = hz/20;
568 callout_init(&k->k_repeat_ch);
569
570 return (ret);
571 }
572
573
574 static int
575 kbd_cc_close(cc)
576 struct cons_channel *cc;
577 {
578 struct kbd_softc *k;
579 int ret;
580
581 if (cc == NULL)
582 return (0);
583
584 k = (struct kbd_softc *)cc->cc_dev;
585 if (k == NULL)
586 return (0);
587
588 if (k->k_ops != NULL && k->k_ops->close != NULL)
589 ret = (*k->k_ops->close)(k);
590 else
591 ret = 0;
592
593 /* stop any pending auto-repeat */
594 if (k->k_repeating) {
595 k->k_repeating = 0;
596 callout_stop(&k->k_repeat_ch);
597 }
598
599 return (ret);
600 }
601
602
603
604 /****************************************************************
605 * Console input - called by middle layer at spltty().
606 ****************************************************************/
607
608 static void
609 kbd_input_console(k, code)
610 struct kbd_softc *k;
611 int code;
612 {
613 struct kbd_state *ks= &k->k_state;
614 int keysym;
615
616 /* any input stops auto-repeat (i.e. key release) */
617 if (k->k_repeating) {
618 k->k_repeating = 0;
619 callout_stop(&k->k_repeat_ch);
620 }
621
622 keysym = kbd_code_to_keysym(ks, code);
623
624 /* pass to console */
625 if (kbd_input_keysym(k, keysym)) {
626 log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
627 " produced unexpected keysym 0x%x\n",
628 k->k_dev.dv_xname,
629 code, ks->kbd_modbits, keysym);
630 return; /* no point in auto-repeat here */
631 }
632
633 if (KEYSYM_NOREPEAT(keysym))
634 return;
635
636 /* setup for auto-repeat after initial delay */
637 k->k_repeating = 1;
638 k->k_repeatsym = keysym;
639 callout_reset(&k->k_repeat_ch, k->k_repeat_start,
640 kbd_repeat, k);
641 }
642
643
644 /*
645 * This is the autorepeat callout function scheduled by kbd_input() above.
646 * Called at splsoftclock().
647 */
648 static void
649 kbd_repeat(arg)
650 void *arg;
651 {
652 struct kbd_softc *k = (struct kbd_softc *)arg;
653 int s;
654
655 s = spltty();
656 if (k->k_repeating && k->k_repeatsym >= 0) {
657 /* feed typematic keysym to the console */
658 (void)kbd_input_keysym(k, k->k_repeatsym);
659
660 /* reschedule next repeat */
661 callout_reset(&k->k_repeat_ch, k->k_repeat_step,
662 kbd_repeat, k);
663 }
664 splx(s);
665 }
666
667
668
669 /*
670 * Supply keysym as console input. Convert keysym to character(s) and
671 * pass them up to cons_channel's upstream hook.
672 *
673 * Return zero on success, else the keysym that we could not handle
674 * (so that the caller may complain).
675 */
676 static int
677 kbd_input_keysym(k, keysym)
678 struct kbd_softc *k;
679 int keysym;
680 {
681 struct kbd_state *ks = &k->k_state;
682 int data;
683
684 /* Check if a recipient has been configured */
685 if (k->k_cc == NULL || k->k_cc->cc_upstream == NULL)
686 return (0);
687
688 switch (KEYSYM_CLASS(keysym)) {
689
690 case KEYSYM_ASCII:
691 data = KEYSYM_DATA(keysym);
692 if (ks->kbd_modbits & KBMOD_META_MASK)
693 data |= 0x80;
694 (*k->k_cc->cc_upstream)(data);
695 break;
696
697 case KEYSYM_STRING:
698 data = keysym & 0xF;
699 kbd_input_string(k, kbd_stringtab[data]);
700 break;
701
702 case KEYSYM_FUNC:
703 kbd_input_funckey(k, keysym);
704 break;
705
706 case KEYSYM_CLRMOD:
707 data = 1 << (keysym & 0x1F);
708 ks->kbd_modbits &= ~data;
709 break;
710
711 case KEYSYM_SETMOD:
712 data = 1 << (keysym & 0x1F);
713 ks->kbd_modbits |= data;
714 break;
715
716 case KEYSYM_INVMOD:
717 data = 1 << (keysym & 0x1F);
718 ks->kbd_modbits ^= data;
719 kbd_update_leds(k);
720 break;
721
722 case KEYSYM_ALL_UP:
723 ks->kbd_modbits &= ~0xFFFF;
724 break;
725
726 case KEYSYM_SPECIAL:
727 if (keysym == KEYSYM_NOP)
728 break;
729 /* FALLTHROUGH */
730 default:
731 /* We could not handle it. */
732 return (keysym);
733 }
734
735 return (0);
736 }
737
738
739 /*
740 * Send string upstream.
741 */
742 static void
743 kbd_input_string(k, str)
744 struct kbd_softc *k;
745 char *str;
746 {
747
748 while (*str) {
749 (*k->k_cc->cc_upstream)(*str);
750 ++str;
751 }
752 }
753
754
755 /*
756 * Format the F-key sequence and send as a string.
757 * XXX: Ugly compatibility mappings.
758 */
759 static void
760 kbd_input_funckey(k, keysym)
761 struct kbd_softc *k;
762 int keysym;
763 {
764 int n;
765 char str[12];
766
767 n = 0xC0 + (keysym & 0x3F);
768 sprintf(str, "\033[%dz", n);
769 kbd_input_string(k, str);
770 }
771
772
773 /*
774 * Update LEDs to reflect console input state.
775 */
776 static void
777 kbd_update_leds(k)
778 struct kbd_softc *k;
779 {
780 struct kbd_state *ks = &k->k_state;
781 char leds;
782
783 leds = ks->kbd_leds;
784 leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
785
786 if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
787 leds |= LED_CAPS_LOCK;
788 if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
789 leds |= LED_NUM_LOCK;
790
791 if (k->k_ops != NULL && k->k_ops->setleds != NULL)
792 (void)(*k->k_ops->setleds)(k, leds, 0);
793 }
794
795
796
797 /****************************************************************
798 * Events input - called by middle layer at spltty().
799 ****************************************************************/
800
801 /*
802 * Supply raw keystrokes when keyboard is open in firm event mode.
803 *
804 * Turn the keystroke into an event and put it in the queue.
805 * If the queue is full, the keystroke is lost (sorry!).
806 */
807 static void
808 kbd_input_event(k, code)
809 struct kbd_softc *k;
810 int code;
811 {
812 struct firm_event *fe;
813 int put;
814
815 #ifdef DIAGNOSTIC
816 if (!k->k_evmode) {
817 printf("%s: kbd_input_event called when not in event mode\n",
818 k->k_dev.dv_xname);
819 return;
820 }
821 #endif
822 put = k->k_events.ev_put;
823 fe = &k->k_events.ev_q[put];
824 put = (put + 1) % EV_QSIZE;
825 if (put == k->k_events.ev_get) {
826 log(LOG_WARNING, "%s: event queue overflow\n",
827 k->k_dev.dv_xname);
828 return;
829 }
830
831 fe->id = KEY_CODE(code);
832 fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
833 fe->time = time;
834 k->k_events.ev_put = put;
835 EV_WAKEUP(&k->k_events);
836 }
837
838
839
840 /****************************************************************
841 * Translation stuff declared in kbd_xlate.h
842 ****************************************************************/
843
844 /*
845 * Initialization - called by either lower layer attach or by kdcninit.
846 */
847 void
848 kbd_xlate_init(ks)
849 struct kbd_state *ks;
850 {
851 struct keyboard *ktbls;
852 int id;
853
854 id = ks->kbd_id;
855 if (id < KBD_MIN_TYPE)
856 id = KBD_MIN_TYPE;
857 if (id > kbd_max_type)
858 id = kbd_max_type;
859 ktbls = keyboards[id];
860
861 ks->kbd_k = *ktbls; /* struct assignment */
862 ks->kbd_modbits = 0;
863 }
864
865 /*
866 * Turn keyboard up/down codes into a KEYSYM.
867 * Note that the "kd" driver (on sun3 and sparc64) uses this too!
868 */
869 int
870 kbd_code_to_keysym(ks, c)
871 struct kbd_state *ks;
872 int c;
873 {
874 u_short *km;
875 int keysym;
876
877 /*
878 * Get keymap pointer. One of these:
879 * release, control, shifted, normal, ...
880 */
881 if (KEY_UP(c))
882 km = ks->kbd_k.k_release;
883 else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
884 km = ks->kbd_k.k_control;
885 else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
886 km = ks->kbd_k.k_shifted;
887 else
888 km = ks->kbd_k.k_normal;
889
890 if (km == NULL) {
891 /*
892 * Do not know how to translate yet.
893 * We will find out when a RESET comes along.
894 */
895 return (KEYSYM_NOP);
896 }
897 keysym = km[KEY_CODE(c)];
898
899 /*
900 * Post-processing for Caps-lock
901 */
902 if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
903 (KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
904 {
905 if (('a' <= keysym) && (keysym <= 'z'))
906 keysym -= ('a' - 'A');
907 }
908
909 /*
910 * Post-processing for Num-lock. All "function"
911 * keysyms get indirected through another table.
912 * (XXX: Only if numlock on. Want off also!)
913 */
914 if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
915 (KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
916 {
917 keysym = kbd_numlock_map[keysym & 0x3F];
918 }
919
920 return (keysym);
921 }
922
923
924 /*
925 * Back door for rcons (fb.c)
926 */
927 void
928 kbd_bell(on)
929 int on;
930 {
931 struct kbd_softc *k = kbd_cd.cd_devs[0]; /* XXX: hardcoded minor */
932
933 if (k == NULL || k->k_ops == NULL || k->k_ops->docmd == NULL)
934 return;
935
936 (void)(*k->k_ops->docmd)(k, on ? KBD_CMD_BELL : KBD_CMD_NOBELL, 0);
937 }
938
939 #if NWSKBD > 0
940 static void
941 kbd_input_wskbd(struct kbd_softc *k, int code)
942 {
943 int type, key;
944
945 type = KEY_UP(code) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
946 key = KEY_CODE(code);
947 wskbd_input(k->k_wskbd, type, key);
948 }
949
950 int
951 wssunkbd_enable(v, on)
952 void *v;
953 int on;
954 {
955 struct kbd_softc *k = v;
956
957 k->k_wsenabled = on;
958 if (on) {
959 /* open actual underlying device */
960 if (k->k_ops != NULL && k->k_ops->open != NULL)
961 (*k->k_ops->open)(k);
962 ev_init(&k->k_events);
963 k->k_evmode = 0; /* XXX: OK? */
964 } else {
965 /* close underlying device */
966 if (k->k_ops != NULL && k->k_ops->close != NULL)
967 (*k->k_ops->close)(k);
968 }
969
970 return 0;
971 }
972
973 void
974 wssunkbd_set_leds(v, leds)
975 void *v;
976 int leds;
977 {
978 struct kbd_softc *k = v;
979 int l = 0;
980
981 if (leds & WSKBD_LED_CAPS)
982 l |= LED_CAPS_LOCK;
983 if (leds & WSKBD_LED_NUM)
984 l |= LED_NUM_LOCK;
985 if (leds & WSKBD_LED_SCROLL)
986 l |= LED_SCROLL_LOCK;
987 if (leds & WSKBD_LED_COMPOSE)
988 l |= LED_COMPOSE;
989 if (k->k_ops != NULL && k->k_ops->setleds != NULL)
990 (*k->k_ops->setleds)(k, l, 0);
991 }
992
993 int
994 wssunkbd_ioctl(v, cmd, data, flag, p)
995 void *v;
996 u_long cmd;
997 caddr_t data;
998 int flag;
999 struct proc *p;
1000 {
1001 return EPASSTHROUGH;
1002 }
1003
1004 void
1005 sunkbd_wskbd_cngetc(v, type, data)
1006 void *v;
1007 u_int *type;
1008 int *data;
1009 {
1010 /* struct kbd_sun_softc *k = v; */
1011 }
1012
1013 void
1014 sunkbd_wskbd_cnpollc(v, on)
1015 void *v;
1016 int on;
1017 {
1018 }
1019
1020 static void
1021 sunkbd_bell_off(v)
1022 void *v;
1023 {
1024 struct kbd_softc *k = v;
1025 k->k_ops->docmd(k, KBD_CMD_NOBELL, 0);
1026 }
1027
1028 void
1029 sunkbd_wskbd_cnbell(v, pitch, period, volume)
1030 void *v;
1031 u_int pitch, period, volume;
1032 {
1033 struct kbd_softc *k = v;
1034
1035 callout_reset(&k->k_wsbell, period*1000/hz, sunkbd_bell_off, v);
1036 k->k_ops->docmd(k, KBD_CMD_BELL, 0);
1037 }
1038
1039 void
1040 kbd_wskbd_attach(k, isconsole)
1041 struct kbd_softc *k;
1042 int isconsole;
1043 {
1044 struct wskbddev_attach_args a;
1045
1046 a.console = isconsole;
1047
1048 if (a.console)
1049 wskbd_cnattach(&sunkbd_wskbd_consops, k, &sunkbd_wskbd_keymapdata);
1050
1051 a.keymap = &sunkbd_wskbd_keymapdata;
1052
1053 a.accessops = &sunkbd_wskbd_accessops;
1054 a.accesscookie = k;
1055
1056 /* Attach the wskbd */
1057 k->k_wskbd = config_found(&k->k_dev, &a, wskbddevprint);
1058 k->k_wsenabled = 0;
1059 callout_init(&k->k_wsbell);
1060 }
1061 #endif
1062