]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/versatile/pl050.c
MFV r323530,r323533,r323534: 7431 ZFS Channel Programs, and followups
[FreeBSD/FreeBSD.git] / sys / arm / versatile / pl050.c
1 /*
2  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * All rights reserved.
4  *
5  * Based on dev/usb/input/ukbd.c  
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/rman.h>
39 #include <sys/proc.h>
40 #include <sys/sched.h>
41 #include <sys/kdb.h>
42
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 #include <machine/intr.h>
46
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50
51 #include <sys/ioccom.h>
52 #include <sys/filio.h>
53 #include <sys/tty.h>
54 #include <sys/kbio.h>
55
56 #include <dev/kbd/kbdreg.h>
57
58 #include <machine/bus.h>
59
60 #include <dev/kbd/kbdtables.h>
61
62 #define KMI_LOCK()      mtx_lock(&Giant)
63 #define KMI_UNLOCK()    mtx_unlock(&Giant)
64
65 #ifdef  INVARIANTS
66 /*
67  * Assert that the lock is held in all contexts
68  * where the code can be executed.
69  */
70 #define KMI_LOCK_ASSERT()       mtx_assert(&Giant, MA_OWNED)
71 /*
72  * Assert that the lock is held in the contexts
73  * where it really has to be so.
74  */
75 #define KMI_CTX_LOCK_ASSERT()                           \
76         do {                                            \
77                 if (!kdb_active && panicstr == NULL)    \
78                         mtx_assert(&Giant, MA_OWNED);   \
79         } while (0)
80 #else
81 #define KMI_LOCK_ASSERT()       (void)0
82 #define KMI_CTX_LOCK_ASSERT()   (void)0
83 #endif
84
85 #define KMICR           0x00
86 #define         KMICR_TYPE_NONPS2       (1 << 5)
87 #define         KMICR_RXINTREN          (1 << 4)
88 #define         KMICR_TXINTREN          (1 << 3)
89 #define         KMICR_EN                (1 << 2)
90 #define         KMICR_FKMID             (1 << 1)
91 #define         KMICR_FKMIC             (1 << 0)
92 #define KMISTAT         0x04
93 #define         KMISTAT_TXEMPTY         (1 << 6)
94 #define         KMISTAT_TXBUSY          (1 << 5)
95 #define         KMISTAT_RXFULL          (1 << 4)
96 #define         KMISTAT_RXBUSY          (1 << 3)
97 #define         KMISTAT_RXPARITY        (1 << 2)
98 #define         KMISTAT_KMIC            (1 << 1)
99 #define         KMISTAT_KMID            (1 << 0)
100 #define KMIDATA         0x08
101 #define KMICLKDIV       0x0C
102 #define KMIIR           0x10
103 #define         KMIIR_TXINTR            (1 << 1)
104 #define         KMIIR_RXINTR            (1 << 0)
105
106 #define KMI_DRIVER_NAME          "kmi"
107 #define KMI_NFKEY        (sizeof(fkey_tab)/sizeof(fkey_tab[0])) /* units */
108
109 #define SET_SCANCODE_SET        0xf0
110
111 struct kmi_softc {
112         device_t sc_dev;
113         keyboard_t sc_kbd;
114         keymap_t sc_keymap;
115         accentmap_t sc_accmap;
116         fkeytab_t sc_fkeymap[KMI_NFKEY];
117
118         struct resource*        sc_mem_res;
119         struct resource*        sc_irq_res;
120         void*                   sc_intr_hl;
121
122         int                     sc_mode;                /* input mode (K_XLATE,K_RAW,K_CODE) */
123         int                     sc_state;               /* shift/lock key state */
124         int                     sc_accents;             /* accent key index (> 0) */
125         uint32_t                sc_flags;               /* flags */
126 #define KMI_FLAG_COMPOSE        0x00000001
127 #define KMI_FLAG_POLLING        0x00000002
128
129         struct                  thread *sc_poll_thread;
130 };
131
132 /* Read/Write macros for Timer used as timecounter */
133 #define pl050_kmi_read_4(sc, reg)               \
134         bus_read_4((sc)->sc_mem_res, (reg))
135
136 #define pl050_kmi_write_4(sc, reg, val) \
137         bus_write_4((sc)->sc_mem_res, (reg), (val))
138
139 /* prototypes */
140 static void     kmi_set_leds(struct kmi_softc *, uint8_t);
141 static int      kmi_set_typematic(keyboard_t *, int);
142 static uint32_t kmi_read_char(keyboard_t *, int);
143 static void     kmi_clear_state(keyboard_t *);
144 static int      kmi_ioctl(keyboard_t *, u_long, caddr_t);
145 static int      kmi_enable(keyboard_t *);
146 static int      kmi_disable(keyboard_t *);
147
148 static int      kmi_attached = 0;
149
150 /* early keyboard probe, not supported */
151 static int
152 kmi_configure(int flags)
153 {
154         return (0);
155 }
156
157 /* detect a keyboard, not used */
158 static int
159 kmi_probe(int unit, void *arg, int flags)
160 {
161         return (ENXIO);
162 }
163
164 /* reset and initialize the device, not used */
165 static int
166 kmi_init(int unit, keyboard_t **kbdp, void *arg, int flags)
167 {
168         return (ENXIO);
169 }
170
171 /* test the interface to the device, not used */
172 static int
173 kmi_test_if(keyboard_t *kbd)
174 {
175         return (0);
176 }
177
178 /* finish using this keyboard, not used */
179 static int
180 kmi_term(keyboard_t *kbd)
181 {
182         return (ENXIO);
183 }
184
185 /* keyboard interrupt routine, not used */
186 static int
187 kmi_intr(keyboard_t *kbd, void *arg)
188 {
189
190         return (0);
191 }
192
193 /* lock the access to the keyboard, not used */
194 static int
195 kmi_lock(keyboard_t *kbd, int lock)
196 {
197         return (1);
198 }
199
200 /*
201  * Enable the access to the device; until this function is called,
202  * the client cannot read from the keyboard.
203  */
204 static int
205 kmi_enable(keyboard_t *kbd)
206 {
207
208         KMI_LOCK();
209         KBD_ACTIVATE(kbd);
210         KMI_UNLOCK();
211
212         return (0);
213 }
214
215 /* disallow the access to the device */
216 static int
217 kmi_disable(keyboard_t *kbd)
218 {
219
220         KMI_LOCK();
221         KBD_DEACTIVATE(kbd);
222         KMI_UNLOCK();
223
224         return (0);
225 }
226
227 /* check if data is waiting */
228 static int
229 kmi_check(keyboard_t *kbd)
230 {
231         struct kmi_softc *sc = kbd->kb_data;
232         uint32_t reg;
233
234         KMI_CTX_LOCK_ASSERT();
235
236         if (!KBD_IS_ACTIVE(kbd))
237                 return (0);
238
239         reg = pl050_kmi_read_4(sc, KMIIR);
240         return (reg & KMIIR_RXINTR);
241 }
242
243 /* check if char is waiting */
244 static int
245 kmi_check_char_locked(keyboard_t *kbd)
246 {
247         KMI_CTX_LOCK_ASSERT();
248
249         if (!KBD_IS_ACTIVE(kbd))
250                 return (0);
251
252         return (kmi_check(kbd));
253 }
254
255 static int
256 kmi_check_char(keyboard_t *kbd)
257 {
258         int result;
259
260         KMI_LOCK();
261         result = kmi_check_char_locked(kbd);
262         KMI_UNLOCK();
263
264         return (result);
265 }
266
267 /* read one byte from the keyboard if it's allowed */
268 /* Currently unused. */
269 static int
270 kmi_read(keyboard_t *kbd, int wait)
271 {
272         KMI_CTX_LOCK_ASSERT();
273
274         if (!KBD_IS_ACTIVE(kbd))
275                 return (-1);
276
277         ++(kbd->kb_count);
278         printf("Implement ME: %s\n", __func__);
279         return (0);
280 }
281
282 /* read char from the keyboard */
283 static uint32_t
284 kmi_read_char_locked(keyboard_t *kbd, int wait)
285 {
286         struct kmi_softc *sc = kbd->kb_data;
287         uint32_t reg, data;
288
289         KMI_CTX_LOCK_ASSERT();
290
291         if (!KBD_IS_ACTIVE(kbd))
292                 return (NOKEY);
293
294         reg = pl050_kmi_read_4(sc, KMIIR);
295         if (reg & KMIIR_RXINTR) {
296                 data = pl050_kmi_read_4(sc, KMIDATA);
297                 return (data);
298         }
299
300         ++kbd->kb_count;
301         return (NOKEY);
302 }
303
304 /* Currently wait is always false. */
305 static uint32_t
306 kmi_read_char(keyboard_t *kbd, int wait)
307 {
308         uint32_t keycode;
309
310         KMI_LOCK();
311         keycode = kmi_read_char_locked(kbd, wait);
312         KMI_UNLOCK();
313
314         return (keycode);
315 }
316
317 /* some useful control functions */
318 static int
319 kmi_ioctl_locked(keyboard_t *kbd, u_long cmd, caddr_t arg)
320 {
321         struct kmi_softc *sc = kbd->kb_data;
322         int i;
323 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
324     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
325         int ival;
326
327 #endif
328
329         KMI_LOCK_ASSERT();
330
331         switch (cmd) {
332         case KDGKBMODE:         /* get keyboard mode */
333                 *(int *)arg = sc->sc_mode;
334                 break;
335 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
336     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
337         case _IO('K', 7):
338                 ival = IOCPARM_IVAL(arg);
339                 arg = (caddr_t)&ival;
340                 /* FALLTHROUGH */
341 #endif
342         case KDSKBMODE:         /* set keyboard mode */
343                 switch (*(int *)arg) {
344                 case K_XLATE:
345                         if (sc->sc_mode != K_XLATE) {
346                                 /* make lock key state and LED state match */
347                                 sc->sc_state &= ~LOCK_MASK;
348                                 sc->sc_state |= KBD_LED_VAL(kbd);
349                         }
350                         /* FALLTHROUGH */
351                 case K_RAW:
352                 case K_CODE:
353                         if (sc->sc_mode != *(int *)arg) {
354                                 if ((sc->sc_flags & KMI_FLAG_POLLING) == 0)
355                                         kmi_clear_state(kbd);
356                                 sc->sc_mode = *(int *)arg;
357                         }
358                         break;
359                 default:
360                         return (EINVAL);
361                 }
362                 break;
363
364         case KDGETLED:                  /* get keyboard LED */
365                 *(int *)arg = KBD_LED_VAL(kbd);
366                 break;
367 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
368     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
369         case _IO('K', 66):
370                 ival = IOCPARM_IVAL(arg);
371                 arg = (caddr_t)&ival;
372                 /* FALLTHROUGH */
373 #endif
374         case KDSETLED:                  /* set keyboard LED */
375                 /* NOTE: lock key state in "sc_state" won't be changed */
376                 if (*(int *)arg & ~LOCK_MASK)
377                         return (EINVAL);
378
379                 i = *(int *)arg;
380
381                 /* replace CAPS LED with ALTGR LED for ALTGR keyboards */
382                 if (sc->sc_mode == K_XLATE &&
383                     kbd->kb_keymap->n_keys > ALTGR_OFFSET) {
384                         if (i & ALKED)
385                                 i |= CLKED;
386                         else
387                                 i &= ~CLKED;
388                 }
389                 if (KBD_HAS_DEVICE(kbd))
390                         kmi_set_leds(sc, i);
391
392                 KBD_LED_VAL(kbd) = *(int *)arg;
393                 break;
394         case KDGKBSTATE:                /* get lock key state */
395                 *(int *)arg = sc->sc_state & LOCK_MASK;
396                 break;
397 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
398     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
399         case _IO('K', 20):
400                 ival = IOCPARM_IVAL(arg);
401                 arg = (caddr_t)&ival;
402                 /* FALLTHROUGH */
403 #endif
404         case KDSKBSTATE:                /* set lock key state */
405                 if (*(int *)arg & ~LOCK_MASK) {
406                         return (EINVAL);
407                 }
408                 sc->sc_state &= ~LOCK_MASK;
409                 sc->sc_state |= *(int *)arg;
410
411                 /* set LEDs and quit */
412                 return (kmi_ioctl(kbd, KDSETLED, arg));
413
414         case KDSETREPEAT:               /* set keyboard repeat rate (new
415                                          * interface) */
416                 if (!KBD_HAS_DEVICE(kbd)) {
417                         return (0);
418                 }
419                 if (((int *)arg)[1] < 0) {
420                         return (EINVAL);
421                 }
422                 if (((int *)arg)[0] < 0) {
423                         return (EINVAL);
424                 }
425                 if (((int *)arg)[0] < 200)      /* fastest possible value */
426                         kbd->kb_delay1 = 200;
427                 else
428                         kbd->kb_delay1 = ((int *)arg)[0];
429                 kbd->kb_delay2 = ((int *)arg)[1];
430                 return (0);
431
432 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
433     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
434         case _IO('K', 67):
435                 ival = IOCPARM_IVAL(arg);
436                 arg = (caddr_t)&ival;
437                 /* FALLTHROUGH */
438 #endif
439         case KDSETRAD:                  /* set keyboard repeat rate (old
440                                          * interface) */
441                 return (kmi_set_typematic(kbd, *(int *)arg));
442
443         case PIO_KEYMAP:                /* set keyboard translation table */
444         case OPIO_KEYMAP:               /* set keyboard translation table
445                                          * (compat) */
446         case PIO_KEYMAPENT:             /* set keyboard translation table
447                                          * entry */
448         case PIO_DEADKEYMAP:            /* set accent key translation table */
449                 sc->sc_accents = 0;
450                 /* FALLTHROUGH */
451         default:
452                 return (genkbd_commonioctl(kbd, cmd, arg));
453         }
454
455         return (0);
456 }
457
458 static int
459 kmi_ioctl(keyboard_t *kbd, u_long cmd, caddr_t arg)
460 {
461         int result;
462
463         /*
464          * XXX KDGKBSTATE, KDSKBSTATE and KDSETLED can be called from any
465          * context where printf(9) can be called, which among other things
466          * includes interrupt filters and threads with any kinds of locks
467          * already held.  For this reason it would be dangerous to acquire
468          * the Giant here unconditionally.  On the other hand we have to
469          * have it to handle the ioctl.
470          * So we make our best effort to auto-detect whether we can grab
471          * the Giant or not.  Blame syscons(4) for this.
472          */
473         switch (cmd) {
474         case KDGKBSTATE:
475         case KDSKBSTATE:
476         case KDSETLED:
477                 if (!mtx_owned(&Giant) && !SCHEDULER_STOPPED())
478                         return (EDEADLK);       /* best I could come up with */
479                 /* FALLTHROUGH */
480         default:
481                 KMI_LOCK();
482                 result = kmi_ioctl_locked(kbd, cmd, arg);
483                 KMI_UNLOCK();
484                 return (result);
485         }
486 }
487
488 /* clear the internal state of the keyboard */
489 static void
490 kmi_clear_state(keyboard_t *kbd)
491 {
492         struct kmi_softc *sc = kbd->kb_data;
493
494         KMI_CTX_LOCK_ASSERT();
495
496         sc->sc_flags &= ~(KMI_FLAG_COMPOSE | KMI_FLAG_POLLING);
497         sc->sc_state &= LOCK_MASK;      /* preserve locking key state */
498         sc->sc_accents = 0;
499 }
500
501 /* save the internal state, not used */
502 static int
503 kmi_get_state(keyboard_t *kbd, void *buf, size_t len)
504 {
505         return (len == 0) ? 1 : -1;
506 }
507
508 /* set the internal state, not used */
509 static int
510 kmi_set_state(keyboard_t *kbd, void *buf, size_t len)
511 {
512         return (EINVAL);
513 }
514
515 static int
516 kmi_poll(keyboard_t *kbd, int on)
517 {
518         struct kmi_softc *sc = kbd->kb_data;
519
520         KMI_LOCK();
521         if (on) {
522                 sc->sc_flags |= KMI_FLAG_POLLING;
523                 sc->sc_poll_thread = curthread;
524         } else {
525                 sc->sc_flags &= ~KMI_FLAG_POLLING;
526         }
527         KMI_UNLOCK();
528
529         return (0);
530 }
531
532 /* local functions */
533
534 static void
535 kmi_set_leds(struct kmi_softc *sc, uint8_t leds)
536 {
537
538         KMI_LOCK_ASSERT();
539
540         /* start transfer, if not already started */
541         printf("Implement me: %s\n", __func__);
542 }
543
544 static int
545 kmi_set_typematic(keyboard_t *kbd, int code)
546 {
547         static const int delays[] = {250, 500, 750, 1000};
548         static const int rates[] = {34, 38, 42, 46, 50, 55, 59, 63,
549                 68, 76, 84, 92, 100, 110, 118, 126,
550                 136, 152, 168, 184, 200, 220, 236, 252,
551         272, 304, 336, 368, 400, 440, 472, 504};
552
553         if (code & ~0x7f) {
554                 return (EINVAL);
555         }
556         kbd->kb_delay1 = delays[(code >> 5) & 3];
557         kbd->kb_delay2 = rates[code & 0x1f];
558         return (0);
559 }
560
561 static keyboard_switch_t kmisw = {
562         .probe = &kmi_probe,
563         .init = &kmi_init,
564         .term = &kmi_term,
565         .intr = &kmi_intr,
566         .test_if = &kmi_test_if,
567         .enable = &kmi_enable,
568         .disable = &kmi_disable,
569         .read = &kmi_read,
570         .check = &kmi_check,
571         .read_char = &kmi_read_char,
572         .check_char = &kmi_check_char,
573         .ioctl = &kmi_ioctl,
574         .lock = &kmi_lock,
575         .clear_state = &kmi_clear_state,
576         .get_state = &kmi_get_state,
577         .set_state = &kmi_set_state,
578         .get_fkeystr = &genkbd_get_fkeystr,
579         .poll = &kmi_poll,
580         .diag = &genkbd_diag,
581 };
582
583 KEYBOARD_DRIVER(kmi, kmisw, kmi_configure);
584
585 static void
586 pl050_kmi_intr(void *arg)
587 {
588         struct kmi_softc *sc = arg;
589         uint32_t c;
590
591         KMI_CTX_LOCK_ASSERT();
592
593         if ((sc->sc_flags & KMI_FLAG_POLLING) != 0)
594                 return;
595
596         if (KBD_IS_ACTIVE(&sc->sc_kbd) &&
597             KBD_IS_BUSY(&sc->sc_kbd)) {
598                 /* let the callback function process the input */
599                 (sc->sc_kbd.kb_callback.kc_func) (&sc->sc_kbd, KBDIO_KEYINPUT,
600                     sc->sc_kbd.kb_callback.kc_arg);
601         } else {
602                 /* read and discard the input, no one is waiting for it */
603                 do {
604                         c = kmi_read_char_locked(&sc->sc_kbd, 0);
605                 } while (c != NOKEY);
606         }
607
608 }
609
610 static int
611 pl050_kmi_probe(device_t dev)
612 {
613
614         if (!ofw_bus_status_okay(dev))
615                 return (ENXIO);
616
617         /*
618          * PL050 is plain PS2 port that pushes bytes to/from computer
619          * VersatilePB has two such ports and QEMU simulates keyboard
620          * connected to port #0 and mouse connected to port #1. This
621          * information can't be obtained from device tree so we just
622          * hardcode this knowledge here. We attach keyboard driver to
623          * port #0 and ignore port #1
624          */
625         if (kmi_attached)
626                 return (ENXIO);
627
628         if (ofw_bus_is_compatible(dev, "arm,pl050")) {
629                 device_set_desc(dev, "PL050 Keyboard/Mouse Interface");
630                 return (BUS_PROBE_DEFAULT);
631         }
632
633         return (ENXIO);
634 }
635
636 static int
637 pl050_kmi_attach(device_t dev)
638 {
639         struct kmi_softc *sc = device_get_softc(dev);
640         keyboard_t *kbd;
641         int rid;
642         int i;
643         uint32_t ack;
644
645         sc->sc_dev = dev;
646         kbd = &sc->sc_kbd;
647         rid = 0;
648
649         sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
650         if (sc->sc_mem_res == NULL) {
651                 device_printf(dev, "could not allocate memory resource\n");
652                 return (ENXIO);
653         }
654
655         /* Request the IRQ resources */
656         sc->sc_irq_res =  bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
657         if (sc->sc_irq_res == NULL) {
658                 device_printf(dev, "Error: could not allocate irq resources\n");
659                 return (ENXIO);
660         }
661
662         /* Setup and enable the timer */
663         if (bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_CLK,
664                         NULL, pl050_kmi_intr, sc,
665                         &sc->sc_intr_hl) != 0) {
666                 bus_release_resource(dev, SYS_RES_IRQ, rid,
667                         sc->sc_irq_res);
668                 device_printf(dev, "Unable to setup the clock irq handler.\n");
669                 return (ENXIO);
670         }
671
672         /* TODO: clock & divisor */
673
674         pl050_kmi_write_4(sc, KMICR, KMICR_EN);
675
676         pl050_kmi_write_4(sc, KMIDATA, SET_SCANCODE_SET);
677         /* read out ACK */
678         ack = pl050_kmi_read_4(sc, KMIDATA);
679         /* Set Scan Code set 1 (XT) */
680         pl050_kmi_write_4(sc, KMIDATA, 1);
681         /* read out ACK */
682         ack = pl050_kmi_read_4(sc, KMIDATA);
683
684         pl050_kmi_write_4(sc, KMICR, KMICR_EN | KMICR_RXINTREN);
685
686         kbd_init_struct(kbd, KMI_DRIVER_NAME, KB_OTHER, 
687                         device_get_unit(dev), 0, 0, 0);
688         kbd->kb_data = (void *)sc;
689
690         sc->sc_keymap = key_map;
691         sc->sc_accmap = accent_map;
692         for (i = 0; i < KMI_NFKEY; i++) {
693                 sc->sc_fkeymap[i] = fkey_tab[i];
694         }
695
696         kbd_set_maps(kbd, &sc->sc_keymap, &sc->sc_accmap,
697             sc->sc_fkeymap, KMI_NFKEY);
698
699         KBD_FOUND_DEVICE(kbd);
700         kmi_clear_state(kbd);
701         KBD_PROBE_DONE(kbd);
702
703         KBD_INIT_DONE(kbd);
704
705         if (kbd_register(kbd) < 0) {
706                 goto detach;
707         }
708         KBD_CONFIG_DONE(kbd);
709
710 #ifdef KBD_INSTALL_CDEV
711         if (kbd_attach(kbd)) {
712                 goto detach;
713         }
714 #endif
715
716         if (bootverbose) {
717                 genkbd_diag(kbd, bootverbose);
718         }
719         kmi_attached = 1;
720         return (0);
721
722 detach:
723         return (ENXIO);
724
725 }
726
727 static device_method_t pl050_kmi_methods[] = {
728         DEVMETHOD(device_probe,         pl050_kmi_probe),
729         DEVMETHOD(device_attach,        pl050_kmi_attach),
730         { 0, 0 }
731 };
732
733 static driver_t pl050_kmi_driver = {
734         "kmi",
735         pl050_kmi_methods,
736         sizeof(struct kmi_softc),
737 };
738
739 static devclass_t pl050_kmi_devclass;
740
741 DRIVER_MODULE(pl050_kmi, simplebus, pl050_kmi_driver, pl050_kmi_devclass, 0, 0);