]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/uart/uart_kbd_sun.c
Make the Z8530 more reliable as low-level console by making use of the
[FreeBSD/FreeBSD.git] / sys / dev / uart / uart_kbd_sun.c
1 /*-
2  * Copyright (c) 2003 Jake Burkholder.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include "opt_kbd.h"
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/interrupt.h>
35 #include <sys/kbio.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/ktr.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42
43 #include <sys/rman.h>
44
45 #include <dev/kbd/kbdreg.h>
46
47 #include <dev/uart/uart.h>
48 #include <dev/uart/uart_bus.h>
49 #include <dev/uart/uart_cpu.h>
50
51 #include <dev/uart/uart_kbd_sun.h>
52 #include <dev/uart/uart_kbd_sun_tables.h>
53
54 #include "uart_if.h"
55
56 #define SUNKBD_BUF_SIZE         128
57
58 #define TODO    printf("%s: unimplemented", __func__)
59
60 struct sunkbd_softc {
61         keyboard_t              sc_kbd;
62         struct uart_softc       *sc_uart;
63         struct uart_devinfo     *sc_sysdev;
64
65         struct callout          sc_repeat_callout;
66         int                     sc_repeat_key;
67
68         int                     sc_accents;
69         int                     sc_mode;
70         int                     sc_polling;
71         int                     sc_repeating;
72         int                     sc_state;
73 };
74
75 static int sunkbd_configure(int flags);
76 static int sunkbd_probe_keyboard(struct uart_devinfo *di);
77
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);
96
97 static void sunkbd_repeat(void *v);
98
99 static keyboard_switch_t sunkbdsw = {
100         sunkbd_probe,
101         sunkbd_init,
102         sunkbd_term,
103         sunkbd_intr,
104         sunkbd_test_if,
105         sunkbd_enable,
106         sunkbd_disable,
107         sunkbd_read,
108         sunkbd_check,
109         sunkbd_read_char,
110         sunkbd_check_char,
111         sunkbd_ioctl,
112         sunkbd_lock,
113         sunkbd_clear_state,
114         sunkbd_get_state,
115         sunkbd_set_state,
116         genkbd_get_fkeystr,
117         sunkbd_poll_mode,
118         sunkbd_diag
119 };
120
121 KEYBOARD_DRIVER(sunkbd, sunkbdsw, sunkbd_configure);
122
123 static struct sunkbd_softc sunkbd_softc;
124 static struct uart_devinfo uart_keyboard;
125
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}
151 };
152
153 static int
154 sunkbd_probe_keyboard(struct uart_devinfo *di)
155 {
156         int tries;
157
158         for (tries = 5; tries != 0; tries--) {
159                 int ltries;
160
161                 uart_putc(di, SKBD_CMD_RESET);
162                 for (ltries = 1000; ltries != 0; ltries--) {
163                         if (uart_poll(di) == SKBD_RSP_RESET)
164                                 break;
165                         DELAY(1000);
166                 }
167                 if (ltries == 0)
168                         continue;
169                 for (ltries = 1000; ltries != 0; ltries--) {
170                         if (uart_poll(di) == SKBD_RSP_IDLE)
171                                 break;
172                         DELAY(1000);
173                 }
174                 if (ltries == 0)
175                         continue;
176                 uart_putc(di, SKBD_CMD_LAYOUT);
177                 if (uart_getc(di) != SKBD_RSP_LAYOUT)
178                         break;
179                 return (uart_getc(di));
180         }
181         return (-1);
182 }
183
184 static int sunkbd_attach(struct uart_softc *sc);
185 static void sunkbd_uart_intr(void *arg);
186
187 static int
188 sunkbd_configure(int flags)
189 {
190         struct sunkbd_softc *sc;
191
192         if (uart_cpu_getdev(UART_DEV_KEYBOARD, &uart_keyboard))
193                 return (0);
194         if (uart_probe(&uart_keyboard))
195                 return (0);
196         uart_init(&uart_keyboard);
197
198         uart_keyboard.type = UART_DEV_KEYBOARD;
199         uart_keyboard.attach = sunkbd_attach;
200         uart_add_sysdev(&uart_keyboard);
201
202         if (sunkbd_probe_keyboard(&uart_keyboard) == -1)
203                 return (0);
204
205         sc = &sunkbd_softc;
206         callout_init(&sc->sc_repeat_callout, 0);
207         sc->sc_repeat_key = -1;
208         sc->sc_repeating = 0;
209
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);
216
217         sc->sc_sysdev = &uart_keyboard;
218
219         return (0);
220 }
221
222 static int
223 sunkbd_attach(struct uart_softc *sc)
224 {
225
226         /*
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.
229          */
230         if (sunkbd_softc.sc_sysdev == NULL) {
231                 device_printf(sc->sc_dev, "keyboard not present\n");
232                 return (0);
233         }
234
235         if (sc->sc_sysdev != NULL) {
236                 sunkbd_softc.sc_uart = sc;
237
238 #ifdef KBD_INSTALL_CDEV
239                 kbd_attach(&sunkbd_softc.sc_kbd);
240 #endif
241                 sunkbd_enable(&sunkbd_softc.sc_kbd);
242
243                 swi_add(&tty_ithd, uart_driver_name, sunkbd_uart_intr,
244                     &sunkbd_softc, SWI_TTY, INTR_TYPE_TTY, &sc->sc_softih);
245
246                 sc->sc_opened = 1;
247         }
248
249         return (0);
250 }
251
252 static void
253 sunkbd_uart_intr(void *arg)
254 {
255         struct sunkbd_softc *sc = arg;
256         int pend;
257
258         if (sc->sc_uart->sc_leaving)
259                 return;
260
261         pend = atomic_readandclear_32(&sc->sc_uart->sc_ttypend);
262         if (!(pend & UART_IPEND_MASK))
263                 return;
264
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);
269                 }
270         }
271
272 }
273
274 static int
275 sunkbd_probe(int unit, void *arg, int flags)
276 {
277         TODO;
278         return (0);
279 }
280
281 static int
282 sunkbd_init(int unit, keyboard_t **kbdp, void *arg, int flags)
283 {
284         TODO;
285         return (0);
286 }
287
288 static int
289 sunkbd_term(keyboard_t *kbd)
290 {
291         TODO;
292         return (0);
293 }
294
295 static int
296 sunkbd_intr(keyboard_t *kbd, void *arg)
297 {
298         TODO;
299         return (0);
300 }
301
302 static int
303 sunkbd_test_if(keyboard_t *kbd)
304 {
305         TODO;
306         return (0);
307 }
308
309 static int
310 sunkbd_enable(keyboard_t *kbd)
311 {
312         KBD_ACTIVATE(kbd);
313         return (0);
314 }
315
316 static int
317 sunkbd_disable(keyboard_t *kbd)
318 {
319         KBD_DEACTIVATE(kbd);
320         return (0);
321 }
322
323 static int
324 sunkbd_read(keyboard_t *kbd, int wait)
325 {
326         TODO;
327         return (0);
328 }
329
330 static int
331 sunkbd_check(keyboard_t *kbd)
332 {
333         TODO;
334         return (0);
335 }
336
337 static u_int
338 sunkbd_read_char(keyboard_t *kbd, int wait)
339 {
340         struct sunkbd_softc *sc;
341         int action;
342         int key;
343
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,
348                     sunkbd_repeat, sc);
349                 key = sc->sc_repeat_key;
350                 if (sc->sc_mode == K_RAW)
351                         return (key);
352                 else
353                         return genkbd_keyaction(kbd, key & 0x7f, key & 0x80,
354                             &sc->sc_state, &sc->sc_accents);
355         }
356
357         for (;;) {
358                 /* XXX compose */
359
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) {
363                         if (wait)
364                                 key = uart_getc(sc->sc_sysdev);
365                         else if ((key = uart_poll(sc->sc_sysdev)) == -1)
366                                 return (NOKEY);
367                 } else {
368                         return (NOKEY);
369                 }
370
371                 switch (key) {
372                 case SKBD_RSP_IDLE:
373                         break;
374                 default:
375                         ++kbd->kb_count;
376
377                         if ((key & 0x80) == 0) {
378                                 callout_reset(&sc->sc_repeat_callout, hz / 2,
379                                     sunkbd_repeat, sc);
380                                 sc->sc_repeat_key = key;
381                         } else {
382                                 if (sc->sc_repeat_key == (key & 0x7f)) {
383                                         callout_stop(&sc->sc_repeat_callout);
384                                         sc->sc_repeat_key = -1;
385                                 }
386                         }
387
388                         if (sc->sc_mode == K_RAW)
389                                 return (key);
390
391                         action = genkbd_keyaction(kbd, key & 0x7f, key & 0x80,
392                             &sc->sc_state, &sc->sc_accents);
393                         if (action != NOKEY)
394                                 return (action);
395                         break;
396                 }
397         }
398         return (0);
399 }
400
401 static int
402 sunkbd_check_char(keyboard_t *kbd)
403 {
404         TODO;
405         return (0);
406 }
407
408 static int
409 sunkbd_ioctl(keyboard_t *kbd, u_long cmd, caddr_t data)
410 {
411         struct sunkbd_softc *sc;
412         int error;
413
414         sc = (struct sunkbd_softc *)kbd;
415         error = 0;
416         switch (cmd) {
417         case KDGKBMODE:
418                 *(int *)data = sc->sc_mode;
419                 break;
420         case KDSKBMODE:
421                 switch (*(int *)data) {
422                 case K_XLATE:
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);
427                         }
428                         /* FALLTHROUGH */
429                 case K_RAW:
430                 case K_CODE:
431                         if (sc->sc_mode != *(int *)data) {
432                                 sunkbd_clear_state(kbd);
433                                 sc->sc_mode = *(int *)data;
434                         }
435                         break;
436                 default:
437                         error = EINVAL;
438                         break;
439                 }
440                 break;
441         case KDGETLED:
442                 *(int *)data = KBD_LED_VAL(kbd);
443                 break;
444         case KDSETLED:
445                 if (*(int *)data & ~LOCK_MASK) {
446                         error = EINVAL;
447                         break;
448                 }
449                 if (sc->sc_uart == NULL)
450                         break;
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;
462                 break;
463         case KDGKBSTATE:
464                 *(int *)data = sc->sc_state & LOCK_MASK;
465                 break;
466         case KDSKBSTATE:
467                 if (*(int *)data & ~LOCK_MASK) {
468                         error = EINVAL;
469                         break;
470                 }
471                 sc->sc_state &= ~LOCK_MASK;
472                 sc->sc_state |= *(int *)data;
473                 break;
474         case KDSETREPEAT:
475         case KDSETRAD:
476                 break;
477         case PIO_KEYMAP:
478         case PIO_KEYMAPENT:
479         case PIO_DEADKEYMAP:
480         default:
481                 return (genkbd_commonioctl(kbd, cmd, data));
482         }
483         return (error);
484 }
485
486 static int
487 sunkbd_lock(keyboard_t *kbd, int lock)
488 {
489         TODO;
490         return (0);
491 }
492
493 static void
494 sunkbd_clear_state(keyboard_t *kbd)
495 {
496         /* TODO; */
497 }
498
499 static int
500 sunkbd_get_state(keyboard_t *kbd, void *buf, size_t len)
501 {
502         TODO;
503         return (0);
504 }
505
506 static int
507 sunkbd_set_state(keyboard_t *kbd, void *buf, size_t len)
508 {
509         TODO;
510         return (0);
511 }
512
513 static int
514 sunkbd_poll_mode(keyboard_t *kbd, int on)
515 {
516         struct sunkbd_softc *sc;
517
518         sc = (struct sunkbd_softc *)kbd;
519         if (on)
520                 sc->sc_polling++;
521         else
522                 sc->sc_polling--;
523         return (0);
524 }
525
526 static void
527 sunkbd_diag(keyboard_t *kbd, int level)
528 {
529         TODO;
530 }
531
532 static void
533 sunkbd_repeat(void *v)
534 {
535         struct sunkbd_softc *sc = v;
536
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);
541         }
542 }