The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/arm/versatile/pl050.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 96d70acc83e6859573b367b6ec10fa2b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.