1 /* $NetBSD: kbd_pckbport.c,v 1.4 2008/04/07 13:31:15 tsutsui Exp $ */
4 * Copyright (c) 2002 Valeriy E. Ushakov
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (c) 1998 The NetBSD Foundation, Inc.
32 * All rights reserved.
34 * This code is derived from software contributed to The NetBSD Foundation
35 * by Charles M. Hannum.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
46 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 * POSSIBILITY OF SUCH DAMAGE.
60 * Copyright (c) 1990 The Regents of the University of California.
61 * All rights reserved.
63 * This code is derived from software contributed to Berkeley by
64 * William Jolitz and Don Ahn.
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
69 * 1. Redistributions of source code must retain the above copyright
70 * notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 * notice, this list of conditions and the following disclaimer in the
73 * documentation and/or other materials provided with the distribution.
74 * 3. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
90 * @(#)pccons.c 5.11 (Berkeley) 5/21/91
92 #include <sys/cdefs.h>
93 __KERNEL_RCSID(0, "$NetBSD: kbd_pckbport.c,v 1.4 2008/04/07 13:31:15 tsutsui Exp $");
96 * Serve JavaStation-1 PS/2 keyboard as a Type5 keyboard with US101A
97 * layout. Since stock Xsun(1) knows this layout, JavaStation-1 gets
98 * X for free. When sparc port is switched to wscons and its X server
99 * knows how to talk wskbd, this driver will no longer be necessary,
100 * and we will be able to attach the keyboard with the MI pckbport(4) driver.
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/conf.h>
106 #include <sys/device.h>
107 #include <sys/kernel.h>
108 #include <sys/select.h>
110 #include <machine/autoconf.h>
111 #include <machine/bus.h>
112 #include <machine/intr.h>
114 #include <dev/sun/kbd_reg.h>
115 #include <dev/sun/kbio.h>
117 #include <dev/pckbport/pckbdreg.h>
118 #include <dev/pckbport/pckbportvar.h>
120 #include <dev/sun/event_var.h>
121 #include <dev/sun/kbd_xlate.h>
122 #include <dev/sun/kbdvar.h>
124 #define DPRINTF(args) /* printf args */
127 struct kbd_pckbport_softc
{
128 struct kbd_softc sc_kbd
;
130 /* pckbport attachment */
131 pckbport_tag_t sc_kbctag
;
132 pckbport_slot_t sc_kbcslot
;
140 /* xt scan-codes decoding */
146 static int kbd_pckbport_match(device_t
, cfdata_t
, void *);
147 static void kbd_pckbport_attach(device_t
, device_t
, void *);
149 CFATTACH_DECL_NEW(kbd_pckbport
, sizeof(struct kbd_pckbport_softc
),
150 kbd_pckbport_match
, kbd_pckbport_attach
, NULL
, NULL
);
157 /* callbacks for the upper /dev/kbd layer */
158 static int kbd_pckbport_open(struct kbd_softc
*);
159 static int kbd_pckbport_close(struct kbd_softc
*);
160 static int kbd_pckbport_do_cmd(struct kbd_softc
*, int, int);
161 static int kbd_pckbport_set_leds(struct kbd_softc
*, int, int);
163 static const struct kbd_ops kbd_ops_pckbport
= {
167 kbd_pckbport_set_leds
171 static const uint8_t kbd_pckbport_xt_to_sun
[];
173 static int kbd_pckbport_set_xtscancode(pckbport_tag_t
, pckbport_slot_t
);
174 static void kbd_pckbport_input(void *, int);
175 static int kbd_pckbport_decode(struct kbd_pckbport_softc
*, int, int *);
178 /*********************************************************************
183 kbd_pckbport_match(device_t parent
, cfdata_t cf
, void *aux
)
185 struct pckbport_attach_args
*pa
= aux
;
187 if (pa
->pa_slot
!= PCKBPORT_KBD_SLOT
)
195 kbd_pckbport_attach(device_t parent
, device_t self
, void *aux
)
197 struct kbd_pckbport_softc
*sc
= device_private(self
);
198 struct pckbport_attach_args
*pa
= aux
;
199 struct kbd_softc
*kbd
= &sc
->sc_kbd
;
201 /* save our attachment to pckbport */
202 sc
->sc_kbctag
= pa
->pa_tag
;
203 sc
->sc_kbcslot
= pa
->pa_slot
;
205 /* provide upper layer a link to our middle layer */
207 kbd
->k_ops
= &kbd_ops_pckbport
;
209 /* pre-fill keyboard type/layout */
210 kbd
->k_state
.kbd_id
= KB_SUN4
; /* NB: type5 keyboards actually report type4 */
211 kbd
->k_state
.kbd_layout
= 19; /* US101A */
213 if (1) { /* XXX: pckbport_machdep_cnattach should tell us */
215 * Hookup ourselves as the console input channel
217 extern void kd_attach_input(struct cons_channel
*);
218 struct cons_channel
*cc
;
220 if ((cc
= kbd_cc_alloc(kbd
)) == NULL
)
223 kd_attach_input(cc
); /* XXX ???? */
224 kbd
->k_isconsole
= 1;
225 aprint_normal(": console input");
230 kbd_pckbport_set_xtscancode(sc
->sc_kbctag
, sc
->sc_kbcslot
);
232 /* slow down typematic (can't disable, grrr) */
237 cmd
[0] = KBC_TYPEMATIC
;
238 cmd
[1] = 0x7f; /* 1s, 2/s */
239 res
= pckbport_poll_cmd(sc
->sc_kbctag
, sc
->sc_kbcslot
,
242 aprint_error_dev(self
,
243 "set typematic failed, error %d\n", res
);
247 /* register our callback with pckbport interrupt handler */
248 pckbport_set_inputhandler(sc
->sc_kbctag
, sc
->sc_kbcslot
,
249 kbd_pckbport_input
, sc
, device_xname(self
));
254 kbd_pckbport_set_xtscancode(pckbport_tag_t kbctag
, pckbport_slot_t kbcslot
)
260 * Some keyboard/8042 combinations do not seem to work if the keyboard
261 * is set to table 1; in fact, it would appear that some keyboards just
262 * ignore the command altogether. So by default, we use the AT scan
263 * codes and have the 8042 translate them. Unfortunately, this is
264 * known to not work on some PS/2 machines. We try desparately to deal
265 * with this by checking the (lack of a) translate bit in the 8042 and
266 * attempting to set the keyboard to XT mode. If this all fails, well,
269 * XXX It would perhaps be a better choice to just use AT scan codes
270 * and not bother with this.
272 if (pckbport_xt_translation(kbctag
, kbcslot
, 1)) {
273 /* The 8042 is translating for us; use AT codes. */
274 cmd
[0] = KBC_SETTABLE
;
276 res
= pckbport_poll_cmd(kbctag
, kbcslot
, cmd
, 2, 0, 0, 0);
280 printf("pckbd: error setting scanset 2\n");
283 * XXX at least one keyboard is reported to lock up
284 * if a "set table" is attempted, thus the "reset".
285 * XXX ignore errors, scanset 2 should be
288 resetcmd
[0] = KBC_RESET
;
289 (void)pckbport_poll_cmd(kbctag
, kbcslot
, resetcmd
,
291 pckbport_flush(kbctag
, kbcslot
);
295 /* Stupid 8042; set keyboard to XT codes. */
296 cmd
[0] = KBC_SETTABLE
;
298 res
= pckbport_poll_cmd(kbctag
, kbcslot
, cmd
, 2, 0, 0, 0);
301 printf("pckbd: error setting scanset 1\n");
308 /*********************************************************************
309 * /dev/kbd middle layer
313 * Initialization to be done at first open.
314 * This is called from kbdopen() or kd_cc_open()
315 * Called with user context.
318 kbd_pckbport_open(struct kbd_softc
*kbd
)
320 struct kbd_pckbport_softc
*sc
= (struct kbd_pckbport_softc
*)kbd
;
321 struct kbd_state
*ks
;
325 DPRINTF(("kbd_pckbport_open: kbd == NULL\n"));
331 /* tolerate extra calls */
335 /* open internal device */
337 /* reset the keyboard (and enable interrupts?) */
340 * Initialize the table pointers for this type/layout.
341 * NB: fixed type/layout were preset during attach.
352 kbd_pckbport_close(struct kbd_softc
*kbd
)
355 struct kbd_pckbport_softc
*k
= (struct kbd_pckbport_softc
*)kbd
;
357 return (0); /* nothing to do so far */
362 * Upper layer talks sun keyboard protocol to us.
366 kbd_pckbport_do_cmd(struct kbd_softc
*kbd
, int suncmd
, int isioctl
)
372 case KBD_CMD_BELL
: /* FALLTHROUGH */
373 case KBD_CMD_NOBELL
: /* FALLTHROUGH */
374 case KBD_CMD_CLICK
: /* FALLTHROUGH */
375 case KBD_CMD_NOCLICK
:
376 /* not supported, do nothing */
377 DPRINTF(("%s: ignoring KIOCCMD 0x%02x\n",
378 device_xname(kbd
->k_dev
), suncmd
));
391 kbd_pckbport_set_leds(struct kbd_softc
*kbd
, int sunleds
, int isioctl
)
393 struct kbd_pckbport_softc
*sc
= (struct kbd_pckbport_softc
*)kbd
;
398 /* re-encode sun leds into pckbd leds */
399 /* pckbd: 0 - scroll; 1 - num; 2 - caps */
401 if (sunleds
& LED_SCROLL_LOCK
)
403 if (sunleds
& LED_NUM_LOCK
)
405 if (sunleds
& LED_CAPS_LOCK
)
408 if (pcleds
== sc
->sc_pcleds
)
410 sc
->sc_pcleds
= pcleds
;
411 DPRINTF(("leds: sun %x, pc %x\n", sunleds
, pcleds
));
413 /* request the change */
414 cmd
[0] = KBC_MODEIND
;
416 if (isioctl
) /* user called KIOCSLED */
417 res
= pckbport_enqueue_cmd(sc
->sc_kbctag
, sc
->sc_kbcslot
,
419 else /* console updates leds - called from interrupt handler */
420 res
= pckbport_poll_cmd(sc
->sc_kbctag
, sc
->sc_kbcslot
,
426 /*********************************************************************
427 * /dev/kbd lower layer
431 * Got a receive interrupt - pckbport wants to give us a byte.
434 kbd_pckbport_input(void *vsc
, int data
)
436 struct kbd_pckbport_softc
*sc
= vsc
;
437 struct kbd_softc
*kbd
= &sc
->sc_kbd
;
440 /* convert to sun make/break code */
441 if (!kbd_pckbport_decode(sc
, data
, &sunkey
))
444 kbd_input(kbd
, sunkey
);
449 * Plagiarized from pckbd_decode
452 kbd_pckbport_decode(struct kbd_pckbport_softc
*sc
, int data
, int *sundata
)
458 DPRINTF(("%s: %02x", device_xname(sc
->sc_kbd
.k_dev
), data
));
460 if (data
== KBR_EXTENDED0
) {
464 } else if (data
== KBR_EXTENDED1
) {
465 sc
->sc_extended1
= 2;
470 /* map extended keys to (unused) codes 128-254 */
471 key
= (data
& 0x7f) | (sc
->sc_extended
? 0x80 : 0);
475 * process BREAK key (EXT1 1D 45 EXT1 9D C5):
476 * map to (unused) code 7F
478 if (sc
->sc_extended1
== 2 && (data
== 0x1d || data
== 0x9d)) {
479 sc
->sc_extended1
= 1;
482 } else if (sc
->sc_extended1
== 1
483 && (data
== 0x45 || data
== 0xc5)) {
484 sc
->sc_extended1
= 0;
486 } else if (sc
->sc_extended1
> 0) {
487 sc
->sc_extended1
= 0;
494 /* always ignore typematic keys */
495 if (key
== sc
->sc_lastchar
) {
499 sc
->sc_lastchar
= key
;
503 sunkey
= kbd_pckbport_xt_to_sun
[key
];
505 DPRINTF((" -> xt 0x%02x %s -> %d\n",
506 key
, (up
? "up " : "down"), sunkey
));
515 static const uint8_t kbd_pckbport_xt_to_sun
[256] = {
517 /* 0x01 */ 29, /* Esc */
518 /* 0x02 */ 30, /* 1 */
519 /* 0x03 */ 31, /* 2 */
520 /* 0x04 */ 32, /* 3 */
521 /* 0x05 */ 33, /* 4 */
522 /* 0x06 */ 34, /* 5 */
523 /* 0x07 */ 35, /* 6 */
524 /* 0x08 */ 36, /* 7 */
525 /* 0x09 */ 37, /* 8 */
526 /* 0x0a */ 38, /* 9 */
527 /* 0x0b */ 39, /* 0 */
528 /* 0x0c */ 40, /* minus */
529 /* 0x0d */ 41, /* equal */
530 /* 0x0e */ 43, /* BackSpace */
531 /* 0x0f */ 53, /* Tab */
532 /* 0x10 */ 54, /* Q */
533 /* 0x11 */ 55, /* W */
534 /* 0x12 */ 56, /* E */
535 /* 0x13 */ 57, /* R */
536 /* 0x14 */ 58, /* T */
537 /* 0x15 */ 59, /* Y */
538 /* 0x16 */ 60, /* U */
539 /* 0x17 */ 61, /* I */
540 /* 0x18 */ 62, /* O */
541 /* 0x19 */ 63, /* P */
542 /* 0x1a */ 64, /* [ */
543 /* 0x1b */ 65, /* ] */
544 /* 0x1c */ 89, /* Return */
545 /* 0x1d */ 76, /* Ctrl_L */
546 /* 0x1e */ 77, /* A */
547 /* 0x1f */ 78, /* S */
548 /* 0x20 */ 79, /* D */
549 /* 0x21 */ 80, /* F */
550 /* 0x22 */ 81, /* G */
551 /* 0x23 */ 82, /* H */
552 /* 0x24 */ 83, /* J */
553 /* 0x25 */ 84, /* K */
554 /* 0x26 */ 85, /* L */
555 /* 0x27 */ 86, /* ; */
556 /* 0x28 */ 87, /* apostr. */
557 /* 0x29 */ 42, /* grave/tilde */
558 /* 0x2a */ 99, /* Shift_L */
559 /* 0x2b */ 88, /* backslash */
560 /* 0x2c */ 100, /* Z */
561 /* 0x2d */ 101, /* X */
562 /* 0x2e */ 102, /* C */
563 /* 0x2f */ 103, /* V */
564 /* 0x30 */ 104, /* B */
565 /* 0x31 */ 105, /* N */
566 /* 0x32 */ 106, /* M */
567 /* 0x33 */ 107, /* , */
568 /* 0x34 */ 108, /* . */
569 /* 0x35 */ 109, /* / */
570 /* 0x36 */ 110, /* Shift_R */
571 /* 0x37 */ 47, /* R6/KP_Mult */
572 /* 0x38 */ 120, /* Meta_L */
573 /* 0x39 */ 121, /* SpaceBar */
574 /* 0x3a */ 119, /* CapsLock */
575 /* 0x3b */ 5, /* F1 */
576 /* 0x3c */ 6, /* F2 */
577 /* 0x3d */ 8, /* F3 */
578 /* 0x3e */ 10, /* F4 */
579 /* 0x3f */ 12, /* F5 */
580 /* 0x40 */ 14, /* F6 */
581 /* 0x41 */ 16, /* F7 */
582 /* 0x42 */ 17, /* F8 */
583 /* 0x43 */ 18, /* F9 */
584 /* 0x44 */ 7, /* F10 */
585 /* 0x45 */ 98, /* Num_Lock */
586 /* 0x46 */ 0, /* */ /* scroll lock */
587 /* 0x47 */ 68, /* R7/Home */
588 /* 0x48 */ 69, /* R8/Up */
589 /* 0x49 */ 70, /* R9/PgUp */
590 /* 0x4a */ 71, /* KP_Minus */
591 /* 0x4b */ 91, /* R10/Left */
592 /* 0x4c */ 92, /* R11/KP_5 */
593 /* 0x4d */ 93, /* R12/Right */
594 /* 0x4e */ 125, /* KP_Add */
595 /* 0x4f */ 112, /* R13/End */
596 /* 0x50 */ 113, /* R14/Down */
597 /* 0x51 */ 114, /* R15/PgDn */
598 /* 0x52 */ 94, /* KP_Insert */
599 /* 0x53 */ 50, /* KP_Delete */
603 /* 0x57 */ 9, /* F11 */
604 /* 0x58 */ 11, /* F12 */
643 /* 0x7f */ 23, /* R3/Break */
673 /* 0x9c */ 90, /* KP_Enter */
674 /* 0x9d */ 76, /* Ctrl_L */ /* XXX: Sun kbd has no Ctrl_R */
698 /* 0xb5 */ 46, /* R5/KP_Div */
701 /* 0xb8 */ 122, /* Meta_R */
716 /* 0xc7 */ 52, /* - T5_Home */
717 /* 0xc8 */ 20, /* T5_Up */
718 /* 0xc9 */ 96, /* - T5_PgUp */
720 /* 0xcb */ 24, /* T5_Left */
722 /* 0xcd */ 28, /* T5_Right */
724 /* 0xcf */ 74, /* - T5_End */
725 /* 0xd0 */ 27, /* T5_Down */
726 /* 0xd1 */ 123, /* - T5_PgDn */
727 /* 0xd2 */ 44, /* - T5_Insert */
728 /* 0xd3 */ 66, /* Delete */
736 /* 0xdb */ 19, /* Alt_L */ /* left flying window */
737 /* 0xdc */ 0, /* */ /* right flying window */
738 /* 0xdd */ 0, /* */ /* (right) menu key */
773 }; /* kbd_pckbport_xt_to_sun */