2 * Copyright (c) 2003 Jake Burkholder.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/interrupt.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
40 #include <machine/bus.h>
41 #include <machine/resource.h>
45 #include <dev/kbd/kbdreg.h>
47 #include <dev/uart/uart.h>
48 #include <dev/uart/uart_bus.h>
49 #include <dev/uart/uart_cpu.h>
51 #include <dev/uart/uart_kbd_sun.h>
52 #include <dev/uart/uart_kbd_sun_tables.h>
56 #define SUNKBD_BUF_SIZE 128
58 #define TODO printf("%s: unimplemented", __func__)
62 struct uart_softc *sc_uart;
63 struct uart_devinfo *sc_sysdev;
65 struct callout sc_repeat_callout;
75 static int sunkbd_configure(int flags);
76 static int sunkbd_probe_keyboard(struct uart_devinfo *di);
78 static int sunkbd_probe(int unit, void *arg, int flags);
79 static int sunkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags);
80 static int sunkbd_term(keyboard_t *kbd);
81 static int sunkbd_intr(keyboard_t *kbd, void *arg);
82 static int sunkbd_test_if(keyboard_t *kbd);
83 static int sunkbd_enable(keyboard_t *kbd);
84 static int sunkbd_disable(keyboard_t *kbd);
85 static int sunkbd_read(keyboard_t *kbd, int wait);
86 static int sunkbd_check(keyboard_t *kbd);
87 static u_int sunkbd_read_char(keyboard_t *kbd, int wait);
88 static int sunkbd_check_char(keyboard_t *kbd);
89 static int sunkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data);
90 static int sunkbd_lock(keyboard_t *kbd, int lock);
91 static void sunkbd_clear_state(keyboard_t *kbd);
92 static int sunkbd_get_state(keyboard_t *kbd, void *buf, size_t len);
93 static int sunkbd_set_state(keyboard_t *kbd, void *buf, size_t len);
94 static int sunkbd_poll_mode(keyboard_t *kbd, int on);
95 static void sunkbd_diag(keyboard_t *kbd, int level);
97 static void sunkbd_repeat(void *v);
99 static keyboard_switch_t sunkbdsw = {
121 KEYBOARD_DRIVER(sunkbd, sunkbdsw, sunkbd_configure);
123 static struct sunkbd_softc sunkbd_softc;
124 static struct uart_devinfo uart_keyboard;
126 static fkeytab_t fkey_tab[96] = {
127 /* 01-04 */ {"\033[M", 3}, {"\033[N", 3}, {"\033[O", 3}, {"\033[P", 3},
128 /* 05-08 */ {"\033[Q", 3}, {"\033[R", 3}, {"\033[S", 3}, {"\033[T", 3},
129 /* 09-12 */ {"\033[U", 3}, {"\033[V", 3}, {"\033[W", 3}, {"\033[X", 3},
130 /* 13-16 */ {"\033[Y", 3}, {"\033[Z", 3}, {"\033[a", 3}, {"\033[b", 3},
131 /* 17-20 */ {"\033[c", 3}, {"\033[d", 3}, {"\033[e", 3}, {"\033[f", 3},
132 /* 21-24 */ {"\033[g", 3}, {"\033[h", 3}, {"\033[i", 3}, {"\033[j", 3},
133 /* 25-28 */ {"\033[k", 3}, {"\033[l", 3}, {"\033[m", 3}, {"\033[n", 3},
134 /* 29-32 */ {"\033[o", 3}, {"\033[p", 3}, {"\033[q", 3}, {"\033[r", 3},
135 /* 33-36 */ {"\033[s", 3}, {"\033[t", 3}, {"\033[u", 3}, {"\033[v", 3},
136 /* 37-40 */ {"\033[w", 3}, {"\033[x", 3}, {"\033[y", 3}, {"\033[z", 3},
137 /* 41-44 */ {"\033[@", 3}, {"\033[[", 3}, {"\033[\\",3}, {"\033[]", 3},
138 /* 45-48 */ {"\033[^", 3}, {"\033[_", 3}, {"\033[`", 3}, {"\033[{", 3},
139 /* 49-52 */ {"\033[H", 3}, {"\033[A", 3}, {"\033[I", 3}, {"-" , 1},
140 /* 53-56 */ {"\033[D", 3}, {"\033[E", 3}, {"\033[C", 3}, {"+" , 1},
141 /* 57-60 */ {"\033[F", 3}, {"\033[B", 3}, {"\033[G", 3}, {"\033[L", 3},
142 /* 61-64 */ {"\177", 1}, {"\033[J", 3}, {"\033[~", 3}, {"\033[}", 3},
143 /* 65-68 */ {"", 0} , {"", 0} , {"", 0} , {"", 0} ,
144 /* 69-72 */ {"", 0} , {"", 0} , {"", 0} , {"", 0} ,
145 /* 73-76 */ {"", 0} , {"", 0} , {"", 0} , {"", 0} ,
146 /* 77-80 */ {"", 0} , {"", 0} , {"", 0} , {"", 0} ,
147 /* 81-84 */ {"", 0} , {"", 0} , {"", 0} , {"", 0} ,
148 /* 85-88 */ {"", 0} , {"", 0} , {"", 0} , {"", 0} ,
149 /* 89-92 */ {"", 0} , {"", 0} , {"", 0} , {"", 0} ,
150 /* 93-96 */ {"", 0} , {"", 0} , {"", 0} , {"", 0}
154 sunkbd_probe_keyboard(struct uart_devinfo *di)
158 for (tries = 5; tries != 0; tries--) {
161 uart_putc(di, SKBD_CMD_RESET);
162 for (ltries = 1000; ltries != 0; ltries--) {
163 if (uart_poll(di) == SKBD_RSP_RESET)
169 for (ltries = 1000; ltries != 0; ltries--) {
170 if (uart_poll(di) == SKBD_RSP_IDLE)
176 uart_putc(di, SKBD_CMD_LAYOUT);
177 if (uart_getc(di) != SKBD_RSP_LAYOUT)
179 return (uart_getc(di));
184 static int sunkbd_attach(struct uart_softc *sc);
185 static void sunkbd_uart_intr(void *arg);
188 sunkbd_configure(int flags)
190 struct sunkbd_softc *sc;
192 if (uart_cpu_getdev(UART_DEV_KEYBOARD, &uart_keyboard))
194 if (uart_probe(&uart_keyboard))
196 uart_init(&uart_keyboard);
198 uart_keyboard.type = UART_DEV_KEYBOARD;
199 uart_keyboard.attach = sunkbd_attach;
200 uart_add_sysdev(&uart_keyboard);
202 if (sunkbd_probe_keyboard(&uart_keyboard) == -1)
206 callout_init(&sc->sc_repeat_callout, 0);
207 sc->sc_repeat_key = -1;
208 sc->sc_repeating = 0;
210 kbd_init_struct(&sc->sc_kbd, "sunkbd", KB_OTHER, 0, 0, 0, 0);
211 kbd_set_maps(&sc->sc_kbd, &keymap_sun_us_unix_kbd,
212 &accentmap_sun_us_unix_kbd, fkey_tab,
213 sizeof(fkey_tab) / sizeof(fkey_tab[0]));
214 sc->sc_mode = K_XLATE;
215 kbd_register(&sc->sc_kbd);
217 sc->sc_sysdev = &uart_keyboard;
223 sunkbd_attach(struct uart_softc *sc)
227 * Don't attach if we didn't probe the keyboard. Note that
228 * the UART is still marked as a system device in that case.
230 if (sunkbd_softc.sc_sysdev == NULL) {
231 device_printf(sc->sc_dev, "keyboard not present\n");
235 if (sc->sc_sysdev != NULL) {
236 sunkbd_softc.sc_uart = sc;
238 #ifdef KBD_INSTALL_CDEV
239 kbd_attach(&sunkbd_softc.sc_kbd);
241 sunkbd_enable(&sunkbd_softc.sc_kbd);
243 swi_add(&tty_ithd, uart_driver_name, sunkbd_uart_intr,
244 &sunkbd_softc, SWI_TTY, INTR_TYPE_TTY, &sc->sc_softih);
253 sunkbd_uart_intr(void *arg)
255 struct sunkbd_softc *sc = arg;
258 if (sc->sc_uart->sc_leaving)
261 pend = atomic_readandclear_32(&sc->sc_uart->sc_ttypend);
262 if (!(pend & UART_IPEND_MASK))
265 if (pend & UART_IPEND_RXREADY) {
266 if (KBD_IS_ACTIVE(&sc->sc_kbd) && KBD_IS_BUSY(&sc->sc_kbd)) {
267 sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd,
268 KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg);
275 sunkbd_probe(int unit, void *arg, int flags)
282 sunkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
289 sunkbd_term(keyboard_t *kbd)
296 sunkbd_intr(keyboard_t *kbd, void *arg)
303 sunkbd_test_if(keyboard_t *kbd)
310 sunkbd_enable(keyboard_t *kbd)
317 sunkbd_disable(keyboard_t *kbd)
324 sunkbd_read(keyboard_t *kbd, int wait)
331 sunkbd_check(keyboard_t *kbd)
338 sunkbd_read_char(keyboard_t *kbd, int wait)
340 struct sunkbd_softc *sc;
344 sc = (struct sunkbd_softc *)kbd;
345 if (sc->sc_repeating) {
346 sc->sc_repeating = 0;
347 callout_reset(&sc->sc_repeat_callout, hz / 10,
349 key = sc->sc_repeat_key;
350 if (sc->sc_mode == K_RAW)
353 return genkbd_keyaction(kbd, key & 0x7f, key & 0x80,
354 &sc->sc_state, &sc->sc_accents);
360 if (sc->sc_uart != NULL && !uart_rx_empty(sc->sc_uart)) {
361 key = uart_rx_get(sc->sc_uart);
362 } else if (sc->sc_polling != 0 && sc->sc_sysdev != NULL) {
364 key = uart_getc(sc->sc_sysdev);
365 else if ((key = uart_poll(sc->sc_sysdev)) == -1)
377 if ((key & 0x80) == 0) {
378 callout_reset(&sc->sc_repeat_callout, hz / 2,
380 sc->sc_repeat_key = key;
382 if (sc->sc_repeat_key == (key & 0x7f)) {
383 callout_stop(&sc->sc_repeat_callout);
384 sc->sc_repeat_key = -1;
388 if (sc->sc_mode == K_RAW)
391 action = genkbd_keyaction(kbd, key & 0x7f, key & 0x80,
392 &sc->sc_state, &sc->sc_accents);
402 sunkbd_check_char(keyboard_t *kbd)
409 sunkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data)
411 struct sunkbd_softc *sc;
414 sc = (struct sunkbd_softc *)kbd;
418 *(int *)data = sc->sc_mode;
421 switch (*(int *)data) {
423 if (sc->sc_mode != K_XLATE) {
424 /* make lock key state and LED state match */
425 sc->sc_state &= ~LOCK_MASK;
426 sc->sc_state |= KBD_LED_VAL(kbd);
431 if (sc->sc_mode != *(int *)data) {
432 sunkbd_clear_state(kbd);
433 sc->sc_mode = *(int *)data;
442 *(int *)data = KBD_LED_VAL(kbd);
445 if (*(int *)data & ~LOCK_MASK) {
449 if (sc->sc_uart == NULL)
451 sc->sc_uart->sc_txdatasz = 2;
452 sc->sc_uart->sc_txbuf[0] = SKBD_CMD_SETLED;
453 sc->sc_uart->sc_txbuf[1] = 0;
454 if (*(int *)data & CLKED)
455 sc->sc_uart->sc_txbuf[1] |= SKBD_LED_CAPSLOCK;
456 if (*(int *)data & NLKED)
457 sc->sc_uart->sc_txbuf[1] |= SKBD_LED_NUMLOCK;
458 if (*(int *)data & SLKED)
459 sc->sc_uart->sc_txbuf[1] |= SKBD_LED_SCROLLLOCK;
460 UART_TRANSMIT(sc->sc_uart);
461 KBD_LED_VAL(kbd) = *(int *)data;
464 *(int *)data = sc->sc_state & LOCK_MASK;
467 if (*(int *)data & ~LOCK_MASK) {
471 sc->sc_state &= ~LOCK_MASK;
472 sc->sc_state |= *(int *)data;
481 return (genkbd_commonioctl(kbd, cmd, data));
487 sunkbd_lock(keyboard_t *kbd, int lock)
494 sunkbd_clear_state(keyboard_t *kbd)
500 sunkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
507 sunkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
514 sunkbd_poll_mode(keyboard_t *kbd, int on)
516 struct sunkbd_softc *sc;
518 sc = (struct sunkbd_softc *)kbd;
527 sunkbd_diag(keyboard_t *kbd, int level)
533 sunkbd_repeat(void *v)
535 struct sunkbd_softc *sc = v;
537 if (sc->sc_repeat_key != -1) {
538 sc->sc_repeating = 1;
539 sc->sc_kbd.kb_callback.kc_func(&sc->sc_kbd,
540 KBDIO_KEYINPUT, sc->sc_kbd.kb_callback.kc_arg);