kbd.c revision 1.48 1 /* $NetBSD: kbd.c,v 1.48 2005/05/17 06:18:31 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.48 2005/05/17 06:18:31 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 kbd_input_wskbd(k, code);
510 return;
511 }
512 #endif
513
514 /*
515 * If /dev/kbd is not connected in event mode, or wskbd mode,
516 * translate and send upstream (to console).
517 */
518 kbd_input_console(k, code);
519 }
520
521
522
523 /****************************************************************
524 * Open/close routines called upon opening /dev/console
525 * if we serve console input.
526 ****************************************************************/
527
528 struct cons_channel *
529 kbd_cc_alloc(k)
530 struct kbd_softc *k;
531 {
532 struct cons_channel *cc;
533
534 if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
535 return (NULL);
536
537 /* our callbacks for the console driver */
538 cc->cc_dev = k;
539 cc->cc_iopen = kbd_cc_open;
540 cc->cc_iclose = kbd_cc_close;
541
542 /* will be provided by the console driver so that we can feed input */
543 cc->cc_upstream = NULL;
544
545 /*
546 * TODO: clean up cons_attach_input() vs kd_attach_input() in
547 * lower layers and move that code here.
548 */
549
550 k->k_cc = cc;
551 return (cc);
552 }
553
554
555 static int
556 kbd_cc_open(cc)
557 struct cons_channel *cc;
558 {
559 struct kbd_softc *k;
560 int ret;
561
562 if (cc == NULL)
563 return (0);
564
565 k = (struct kbd_softc *)cc->cc_dev;
566 if (k == NULL)
567 return (0);
568
569 if (k->k_ops != NULL && k->k_ops->open != NULL)
570 ret = (*k->k_ops->open)(k);
571 else
572 ret = 0;
573
574 /* XXX: verify that callout is not active? */
575 k->k_repeat_start = hz/2;
576 k->k_repeat_step = hz/20;
577 callout_init(&k->k_repeat_ch);
578
579 return (ret);
580 }
581
582
583 static int
584 kbd_cc_close(cc)
585 struct cons_channel *cc;
586 {
587 struct kbd_softc *k;
588 int ret;
589
590 if (cc == NULL)
591 return (0);
592
593 k = (struct kbd_softc *)cc->cc_dev;
594 if (k == NULL)
595 return (0);
596
597 if (k->k_ops != NULL && k->k_ops->close != NULL)
598 ret = (*k->k_ops->close)(k);
599 else
600 ret = 0;
601
602 /* stop any pending auto-repeat */
603 if (k->k_repeating) {
604 k->k_repeating = 0;
605 callout_stop(&k->k_repeat_ch);
606 }
607
608 return (ret);
609 }
610
611
612
613 /****************************************************************
614 * Console input - called by middle layer at spltty().
615 ****************************************************************/
616
617 static void
618 kbd_input_console(k, code)
619 struct kbd_softc *k;
620 int code;
621 {
622 struct kbd_state *ks= &k->k_state;
623 int keysym;
624
625 /* any input stops auto-repeat (i.e. key release) */
626 if (k->k_repeating) {
627 k->k_repeating = 0;
628 callout_stop(&k->k_repeat_ch);
629 }
630
631 keysym = kbd_code_to_keysym(ks, code);
632
633 /* pass to console */
634 if (kbd_input_keysym(k, keysym)) {
635 log(LOG_WARNING, "%s: code=0x%x with mod=0x%x"
636 " produced unexpected keysym 0x%x\n",
637 k->k_dev.dv_xname,
638 code, ks->kbd_modbits, keysym);
639 return; /* no point in auto-repeat here */
640 }
641
642 if (KEYSYM_NOREPEAT(keysym))
643 return;
644
645 /* setup for auto-repeat after initial delay */
646 k->k_repeating = 1;
647 k->k_repeatsym = keysym;
648 callout_reset(&k->k_repeat_ch, k->k_repeat_start,
649 kbd_repeat, k);
650 }
651
652
653 /*
654 * This is the autorepeat callout function scheduled by kbd_input() above.
655 * Called at splsoftclock().
656 */
657 static void
658 kbd_repeat(arg)
659 void *arg;
660 {
661 struct kbd_softc *k = (struct kbd_softc *)arg;
662 int s;
663
664 s = spltty();
665 if (k->k_repeating && k->k_repeatsym >= 0) {
666 /* feed typematic keysym to the console */
667 (void)kbd_input_keysym(k, k->k_repeatsym);
668
669 /* reschedule next repeat */
670 callout_reset(&k->k_repeat_ch, k->k_repeat_step,
671 kbd_repeat, k);
672 }
673 splx(s);
674 }
675
676
677
678 /*
679 * Supply keysym as console input. Convert keysym to character(s) and
680 * pass them up to cons_channel's upstream hook.
681 *
682 * Return zero on success, else the keysym that we could not handle
683 * (so that the caller may complain).
684 */
685 static int
686 kbd_input_keysym(k, keysym)
687 struct kbd_softc *k;
688 int keysym;
689 {
690 struct kbd_state *ks = &k->k_state;
691 int data;
692 /* Check if a recipient has been configured */
693 if (k->k_cc == NULL || k->k_cc->cc_upstream == NULL)
694 return (0);
695
696 switch (KEYSYM_CLASS(keysym)) {
697
698 case KEYSYM_ASCII:
699 data = KEYSYM_DATA(keysym);
700 if (ks->kbd_modbits & KBMOD_META_MASK)
701 data |= 0x80;
702 (*k->k_cc->cc_upstream)(data);
703 break;
704
705 case KEYSYM_STRING:
706 data = keysym & 0xF;
707 kbd_input_string(k, kbd_stringtab[data]);
708 break;
709
710 case KEYSYM_FUNC:
711 kbd_input_funckey(k, keysym);
712 break;
713
714 case KEYSYM_CLRMOD:
715 data = 1 << (keysym & 0x1F);
716 ks->kbd_modbits &= ~data;
717 break;
718
719 case KEYSYM_SETMOD:
720 data = 1 << (keysym & 0x1F);
721 ks->kbd_modbits |= data;
722 break;
723
724 case KEYSYM_INVMOD:
725 data = 1 << (keysym & 0x1F);
726 ks->kbd_modbits ^= data;
727 kbd_update_leds(k);
728 break;
729
730 case KEYSYM_ALL_UP:
731 ks->kbd_modbits &= ~0xFFFF;
732 break;
733
734 case KEYSYM_SPECIAL:
735 if (keysym == KEYSYM_NOP)
736 break;
737 /* FALLTHROUGH */
738 default:
739 /* We could not handle it. */
740 return (keysym);
741 }
742
743 return (0);
744 }
745
746
747 /*
748 * Send string upstream.
749 */
750 static void
751 kbd_input_string(k, str)
752 struct kbd_softc *k;
753 char *str;
754 {
755
756 while (*str) {
757 (*k->k_cc->cc_upstream)(*str);
758 ++str;
759 }
760 }
761
762
763 /*
764 * Format the F-key sequence and send as a string.
765 * XXX: Ugly compatibility mappings.
766 */
767 static void
768 kbd_input_funckey(k, keysym)
769 struct kbd_softc *k;
770 int keysym;
771 {
772 int n;
773 char str[12];
774
775 n = 0xC0 + (keysym & 0x3F);
776 snprintf(str, sizeof(str), "\033[%dz", n);
777 kbd_input_string(k, str);
778 }
779
780
781 /*
782 * Update LEDs to reflect console input state.
783 */
784 static void
785 kbd_update_leds(k)
786 struct kbd_softc *k;
787 {
788 struct kbd_state *ks = &k->k_state;
789 char leds;
790
791 leds = ks->kbd_leds;
792 leds &= ~(LED_CAPS_LOCK|LED_NUM_LOCK);
793
794 if (ks->kbd_modbits & (1 << KBMOD_CAPSLOCK))
795 leds |= LED_CAPS_LOCK;
796 if (ks->kbd_modbits & (1 << KBMOD_NUMLOCK))
797 leds |= LED_NUM_LOCK;
798
799 if (k->k_ops != NULL && k->k_ops->setleds != NULL)
800 (void)(*k->k_ops->setleds)(k, leds, 0);
801 }
802
803
804
805 /****************************************************************
806 * Events input - called by middle layer at spltty().
807 ****************************************************************/
808
809 /*
810 * Supply raw keystrokes when keyboard is open in firm event mode.
811 *
812 * Turn the keystroke into an event and put it in the queue.
813 * If the queue is full, the keystroke is lost (sorry!).
814 */
815 static void
816 kbd_input_event(k, code)
817 struct kbd_softc *k;
818 int code;
819 {
820 struct firm_event *fe;
821 int put;
822
823 #ifdef DIAGNOSTIC
824 if (!k->k_evmode) {
825 printf("%s: kbd_input_event called when not in event mode\n",
826 k->k_dev.dv_xname);
827 return;
828 }
829 #endif
830 put = k->k_events.ev_put;
831 fe = &k->k_events.ev_q[put];
832 put = (put + 1) % EV_QSIZE;
833 if (put == k->k_events.ev_get) {
834 log(LOG_WARNING, "%s: event queue overflow\n",
835 k->k_dev.dv_xname);
836 return;
837 }
838
839 fe->id = KEY_CODE(code);
840 fe->value = KEY_UP(code) ? VKEY_UP : VKEY_DOWN;
841 fe->time = time;
842 k->k_events.ev_put = put;
843 EV_WAKEUP(&k->k_events);
844 }
845
846
847
848 /****************************************************************
849 * Translation stuff declared in kbd_xlate.h
850 ****************************************************************/
851
852 /*
853 * Initialization - called by either lower layer attach or by kdcninit.
854 */
855 void
856 kbd_xlate_init(ks)
857 struct kbd_state *ks;
858 {
859 struct keyboard *ktbls;
860 int id;
861
862 id = ks->kbd_id;
863 if (id < KBD_MIN_TYPE)
864 id = KBD_MIN_TYPE;
865 if (id > kbd_max_type)
866 id = kbd_max_type;
867 ktbls = keyboards[id];
868
869 ks->kbd_k = *ktbls; /* struct assignment */
870 ks->kbd_modbits = 0;
871 }
872
873 /*
874 * Turn keyboard up/down codes into a KEYSYM.
875 * Note that the "kd" driver (on sun3 and sparc64) uses this too!
876 */
877 int
878 kbd_code_to_keysym(ks, c)
879 struct kbd_state *ks;
880 int c;
881 {
882 u_short *km;
883 int keysym;
884
885 /*
886 * Get keymap pointer. One of these:
887 * release, control, shifted, normal, ...
888 */
889 if (KEY_UP(c))
890 km = ks->kbd_k.k_release;
891 else if (ks->kbd_modbits & KBMOD_CTRL_MASK)
892 km = ks->kbd_k.k_control;
893 else if (ks->kbd_modbits & KBMOD_SHIFT_MASK)
894 km = ks->kbd_k.k_shifted;
895 else
896 km = ks->kbd_k.k_normal;
897
898 if (km == NULL) {
899 /*
900 * Do not know how to translate yet.
901 * We will find out when a RESET comes along.
902 */
903 return (KEYSYM_NOP);
904 }
905 keysym = km[KEY_CODE(c)];
906
907 /*
908 * Post-processing for Caps-lock
909 */
910 if ((ks->kbd_modbits & (1 << KBMOD_CAPSLOCK)) &&
911 (KEYSYM_CLASS(keysym) == KEYSYM_ASCII) )
912 {
913 if (('a' <= keysym) && (keysym <= 'z'))
914 keysym -= ('a' - 'A');
915 }
916
917 /*
918 * Post-processing for Num-lock. All "function"
919 * keysyms get indirected through another table.
920 * (XXX: Only if numlock on. Want off also!)
921 */
922 if ((ks->kbd_modbits & (1 << KBMOD_NUMLOCK)) &&
923 (KEYSYM_CLASS(keysym) == KEYSYM_FUNC) )
924 {
925 keysym = kbd_numlock_map[keysym & 0x3F];
926 }
927
928 return (keysym);
929 }
930
931
932 /*
933 * Back door for rcons (fb.c)
934 */
935 void
936 kbd_bell(on)
937 int on;
938 {
939 struct kbd_softc *k = kbd_cd.cd_devs[0]; /* XXX: hardcoded minor */
940
941 if (k == NULL || k->k_ops == NULL || k->k_ops->docmd == NULL)
942 return;
943
944 (void)(*k->k_ops->docmd)(k, on ? KBD_CMD_BELL : KBD_CMD_NOBELL, 0);
945 }
946
947 #if NWSKBD > 0
948 static void
949 kbd_input_wskbd(struct kbd_softc *k, int code)
950 {
951 int type, key;
952
953 #ifdef WSDISPLAY_COMPAT_RAWKBD
954 if (k->k_wsraw) {
955 u_char buf;
956
957 buf = code;
958 wskbd_rawinput(k->k_wskbd, &buf, 1);
959 return;
960 }
961 #endif
962
963 type = KEY_UP(code) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
964 key = KEY_CODE(code);
965 wskbd_input(k->k_wskbd, type, key);
966 }
967
968 int
969 wssunkbd_enable(v, on)
970 void *v;
971 int on;
972 {
973 struct kbd_softc *k = v;
974 if (k->k_wsenabled != on) {
975 k->k_wsenabled = on;
976 if (on) {
977 /* open actual underlying device */
978 if (k->k_ops != NULL && k->k_ops->open != NULL)
979 (*k->k_ops->open)(k);
980 ev_init(&k->k_events);
981 k->k_evmode = 0; /* XXX: OK? */
982 } else {
983 /* close underlying device */
984 if (k->k_ops != NULL && k->k_ops->close != NULL)
985 (*k->k_ops->close)(k);
986 }
987 }
988 return 0;
989 }
990
991 void
992 wssunkbd_set_leds(v, leds)
993 void *v;
994 int leds;
995 {
996 struct kbd_softc *k = v;
997 int l = 0;
998
999 if (leds & WSKBD_LED_CAPS)
1000 l |= LED_CAPS_LOCK;
1001 if (leds & WSKBD_LED_NUM)
1002 l |= LED_NUM_LOCK;
1003 if (leds & WSKBD_LED_SCROLL)
1004 l |= LED_SCROLL_LOCK;
1005 if (leds & WSKBD_LED_COMPOSE)
1006 l |= LED_COMPOSE;
1007 if (k->k_ops != NULL && k->k_ops->setleds != NULL)
1008 (*k->k_ops->setleds)(k, l, 0);
1009 k->k_leds=l;
1010 }
1011
1012 int
1013 wssunkbd_ioctl(v, cmd, data, flag, p)
1014 void *v;
1015 u_long cmd;
1016 caddr_t data;
1017 int flag;
1018 struct proc *p;
1019 {
1020 struct kbd_softc *k = v;
1021
1022 switch (cmd) {
1023 case WSKBDIO_GTYPE:
1024 /* we can't tell 4 from 5 or 6 */
1025 *(int *)data = k->k_state.kbd_id < KB_SUN4 ?
1026 WSKBD_TYPE_SUN : WSKBD_TYPE_SUN5;
1027 return (0);
1028 case WSKBDIO_SETLEDS:
1029 wssunkbd_set_leds(v, *(int *)data);
1030 return (0);
1031 case WSKBDIO_GETLEDS:
1032 *(int *)data = k->k_leds;
1033 return (0);
1034 #ifdef WSDISPLAY_COMPAT_RAWKBD
1035 case WSKBDIO_SETMODE:
1036 k->k_wsraw = *(int *)data == WSKBD_RAW;
1037 return (0);
1038 #endif
1039 }
1040 return EPASSTHROUGH;
1041 }
1042
1043 extern int prom_cngetc(dev_t);
1044
1045 void
1046 sunkbd_wskbd_cngetc(v, type, data)
1047 void *v;
1048 u_int *type;
1049 int *data;
1050 {
1051 /* struct kbd_sun_softc *k = v; */
1052 *data = prom_cngetc(0);
1053 *type = WSCONS_EVENT_ASCII;
1054 }
1055
1056 void
1057 sunkbd_wskbd_cnpollc(v, on)
1058 void *v;
1059 int on;
1060 {
1061 }
1062
1063 static void
1064 sunkbd_bell_off(v)
1065 void *v;
1066 {
1067 struct kbd_softc *k = v;
1068 k->k_ops->docmd(k, KBD_CMD_NOBELL, 0);
1069 }
1070
1071 void
1072 sunkbd_wskbd_cnbell(v, pitch, period, volume)
1073 void *v;
1074 u_int pitch, period, volume;
1075 {
1076 struct kbd_softc *k = v;
1077
1078 callout_reset(&k->k_wsbell, period*1000/hz, sunkbd_bell_off, v);
1079 k->k_ops->docmd(k, KBD_CMD_BELL, 0);
1080 }
1081
1082 void
1083 kbd_enable(dev)
1084 struct device *dev;
1085 {
1086 struct kbd_softc *k=(struct kbd_softc *)dev;
1087 struct wskbddev_attach_args a;
1088
1089 if (k->k_isconsole)
1090 wskbd_cnattach(&sunkbd_wskbd_consops, k,
1091 &sunkbd_wskbd_keymapdata);
1092
1093 a.console = k->k_isconsole;
1094 a.keymap = &sunkbd_wskbd_keymapdata;
1095 a.accessops = &sunkbd_wskbd_accessops;
1096 a.accesscookie = k;
1097
1098 /* XXX why? */
1099 k->k_wsenabled = 0;
1100
1101 /* Attach the wskbd */
1102 k->k_wskbd = config_found(&k->k_dev, &a, wskbddevprint);
1103
1104 callout_init(&k->k_wsbell);
1105
1106 wssunkbd_enable(k,1);
1107
1108 wssunkbd_set_leds(k, WSKBD_LED_SCROLL | WSKBD_LED_NUM | WSKBD_LED_CAPS);
1109 delay(100000);
1110 wssunkbd_set_leds(k, 0);
1111 }
1112
1113 void
1114 kbd_wskbd_attach(k, isconsole)
1115 struct kbd_softc *k;
1116 int isconsole;
1117 {
1118 k->k_isconsole=isconsole;
1119
1120 config_interrupts(&k->k_dev,kbd_enable);
1121 }
1122 #endif
1123