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