Home | History | Annotate | Line # | Download | only in dev
nbpkbd.c revision 1.1.68.1
      1  1.1.68.1   thorpej /*	$NetBSD: nbpkbd.c,v 1.1.68.1 2021/03/21 21:09:00 thorpej Exp $ */
      2       1.1  kiyohara /*
      3       1.1  kiyohara  * Copyright (c) 2011 KIYOHARA Takashi
      4       1.1  kiyohara  * All rights reserved.
      5       1.1  kiyohara  *
      6       1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7       1.1  kiyohara  * modification, are permitted provided that the following conditions
      8       1.1  kiyohara  * are met:
      9       1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10       1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11       1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14       1.1  kiyohara  *
     15       1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16       1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19       1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20       1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23       1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24       1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25       1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26       1.1  kiyohara  */
     27       1.1  kiyohara #include <sys/cdefs.h>
     28  1.1.68.1   thorpej __KERNEL_RCSID(0, "$NetBSD: nbpkbd.c,v 1.1.68.1 2021/03/21 21:09:00 thorpej Exp $");
     29       1.1  kiyohara 
     30       1.1  kiyohara #include <sys/param.h>
     31       1.1  kiyohara #include <sys/callout.h>
     32       1.1  kiyohara #include <sys/device.h>
     33       1.1  kiyohara #include <sys/errno.h>
     34       1.1  kiyohara #include <sys/kernel.h>
     35       1.1  kiyohara 
     36       1.1  kiyohara #include <machine/platid.h>
     37       1.1  kiyohara #include <machine/platid_mask.h>
     38       1.1  kiyohara 
     39       1.1  kiyohara #include <arm/xscale/pxa2x0_i2c.h>
     40       1.1  kiyohara 
     41       1.1  kiyohara #include <hpcarm/dev/nbppconvar.h>
     42       1.1  kiyohara 
     43       1.1  kiyohara #include <dev/hpc/hpckbdvar.h>
     44       1.1  kiyohara 
     45       1.1  kiyohara #include <dev/i2c/i2cvar.h>
     46       1.1  kiyohara 
     47       1.1  kiyohara #include "locators.h"
     48       1.1  kiyohara 
     49       1.1  kiyohara #ifdef NBPKBD_DEBUG
     50       1.1  kiyohara #define DPRINTF(x)	printf x
     51       1.1  kiyohara #else
     52       1.1  kiyohara #define DPRINTF(x)
     53       1.1  kiyohara #endif
     54       1.1  kiyohara 
     55       1.1  kiyohara #define IS_KEY_DOWN(x)	(((x) & 0x80) == 0x00)
     56       1.1  kiyohara #define KEY_CODE(x)	((x) & 0x7f)
     57       1.1  kiyohara #define KEY_INITIAL_INTERVAL	(500 * hz / 1000)	/* 500ms */
     58       1.1  kiyohara #define KEY_REPEAT_INTERVAL	(200 * hz / 1000)	/* 200ms */
     59       1.1  kiyohara 
     60       1.1  kiyohara struct nbpkbd_softc {
     61       1.1  kiyohara 	device_t sc_dev;
     62       1.1  kiyohara 	device_t sc_parent;
     63       1.1  kiyohara 	int sc_tag;
     64       1.1  kiyohara 
     65       1.1  kiyohara 	struct hpckbd_ic_if sc_if;
     66       1.1  kiyohara 	struct hpckbd_if *sc_hpckbd;
     67       1.1  kiyohara 
     68       1.1  kiyohara 	int sc_enabled;
     69       1.1  kiyohara 
     70       1.1  kiyohara 	callout_t  sc_callout;
     71       1.1  kiyohara 	uint8_t sc_last_scancode;
     72       1.1  kiyohara };
     73       1.1  kiyohara 
     74       1.1  kiyohara static int nbpkbd_match(device_t, cfdata_t, void *);
     75       1.1  kiyohara static void nbpkbd_attach(device_t, device_t, void *);
     76       1.1  kiyohara 
     77       1.1  kiyohara static int nbpkbd_input_establish(void *, struct hpckbd_if *);
     78       1.1  kiyohara static int nbpkbd_input(void *, int, char *);
     79       1.1  kiyohara static int nbpkbd_poll(void *);
     80       1.1  kiyohara 
     81       1.1  kiyohara static void nbpkbd_key_repeat(void *);
     82       1.1  kiyohara 
     83       1.1  kiyohara CFATTACH_DECL_NEW(nbpkbd, sizeof(struct nbpkbd_softc),
     84       1.1  kiyohara     nbpkbd_match, nbpkbd_attach, NULL, NULL);
     85       1.1  kiyohara 
     86       1.1  kiyohara 
     87       1.1  kiyohara /* ARGSUSED */
     88       1.1  kiyohara static int
     89       1.1  kiyohara nbpkbd_match(device_t parent, cfdata_t match, void *aux)
     90       1.1  kiyohara {
     91       1.1  kiyohara 	struct nbppcon_attach_args *pcon = aux;
     92       1.1  kiyohara 
     93       1.1  kiyohara 	if (strcmp(pcon->aa_name, match->cf_name) ||
     94       1.1  kiyohara 	    pcon->aa_tag == NBPPCONCF_TAG_DEFAULT)
     95       1.1  kiyohara 		return 0;
     96       1.1  kiyohara 
     97       1.1  kiyohara 	return 1;
     98       1.1  kiyohara }
     99       1.1  kiyohara 
    100       1.1  kiyohara static void
    101       1.1  kiyohara nbpkbd_attach(device_t parent, device_t self, void *aux)
    102       1.1  kiyohara {
    103       1.1  kiyohara 	struct nbpkbd_softc *sc = device_private(self);
    104       1.1  kiyohara 	struct nbppcon_attach_args *pcon = aux;
    105       1.1  kiyohara 	struct hpckbd_attach_args haa;
    106       1.1  kiyohara 
    107       1.1  kiyohara 	aprint_normal(": NETBOOK PRO Keyboard\n");
    108       1.1  kiyohara 	aprint_naive("\n");
    109       1.1  kiyohara 
    110       1.1  kiyohara 	sc->sc_dev = self;
    111       1.1  kiyohara 	sc->sc_parent = parent;
    112       1.1  kiyohara 	sc->sc_tag = pcon->aa_tag;
    113       1.1  kiyohara 	sc->sc_if.hii_ctx = sc;
    114       1.1  kiyohara 	sc->sc_if.hii_establish = nbpkbd_input_establish;
    115       1.1  kiyohara 	sc->sc_if.hii_poll = nbpkbd_poll;
    116       1.1  kiyohara 
    117       1.1  kiyohara 	hpckbd_cnattach(&sc->sc_if);
    118       1.1  kiyohara 
    119       1.1  kiyohara 	callout_init(&sc->sc_callout, 0);
    120       1.1  kiyohara 	callout_setfunc(&sc->sc_callout, nbpkbd_key_repeat, sc);
    121       1.1  kiyohara 
    122       1.1  kiyohara 	/* Attach hpckbd */
    123       1.1  kiyohara 	haa.haa_ic = &sc->sc_if;
    124  1.1.68.1   thorpej 	config_found(self, &haa, hpckbd_print, CFARG_EOL);
    125       1.1  kiyohara 
    126       1.1  kiyohara 	if (nbppcon_regist_callback(parent, sc->sc_tag, nbpkbd_input, sc) ==
    127       1.1  kiyohara 	    NULL)
    128       1.1  kiyohara 		aprint_error_dev(self, "callback regist failed\n");
    129       1.1  kiyohara }
    130       1.1  kiyohara 
    131       1.1  kiyohara 
    132       1.1  kiyohara static int
    133       1.1  kiyohara nbpkbd_input_establish(void *arg, struct hpckbd_if *kbdif)
    134       1.1  kiyohara {
    135       1.1  kiyohara 	struct nbpkbd_softc *sc = arg;
    136       1.1  kiyohara 
    137       1.1  kiyohara 	sc->sc_hpckbd = kbdif;
    138       1.1  kiyohara 	sc->sc_enabled = 1;
    139       1.1  kiyohara 
    140       1.1  kiyohara 	return 0;
    141       1.1  kiyohara }
    142       1.1  kiyohara 
    143       1.1  kiyohara static int
    144       1.1  kiyohara nbpkbd_input(void *arg, int buflen, char *buf)
    145       1.1  kiyohara {
    146       1.1  kiyohara 	struct nbpkbd_softc *sc = arg;
    147       1.1  kiyohara 
    148       1.1  kiyohara #ifdef NBPKBD_DEBUG
    149       1.1  kiyohara 	{
    150       1.1  kiyohara 		int i;
    151       1.1  kiyohara 
    152       1.1  kiyohara 		printf("%s:", __func__);
    153       1.1  kiyohara 		for (i = 0; i < buflen; i++)
    154       1.1  kiyohara 			printf(" %02x", buf[i]);
    155       1.1  kiyohara 		printf("\n");
    156       1.1  kiyohara 	}
    157       1.1  kiyohara #endif
    158       1.1  kiyohara 
    159       1.1  kiyohara 	if (buflen != 2) {
    160       1.1  kiyohara 		aprint_error_dev(sc->sc_dev, "ugly length %d\n", buflen);
    161       1.1  kiyohara 		return -1;
    162       1.1  kiyohara 	}
    163       1.1  kiyohara 
    164       1.1  kiyohara 	hpckbd_input(sc->sc_hpckbd, IS_KEY_DOWN(buf[1]), KEY_CODE(buf[1]));
    165       1.1  kiyohara 
    166       1.1  kiyohara 	sc->sc_last_scancode = buf[1];
    167       1.1  kiyohara 	if (IS_KEY_DOWN(buf[1]))
    168       1.1  kiyohara 		callout_schedule(&sc->sc_callout, KEY_INITIAL_INTERVAL);
    169       1.1  kiyohara 	else
    170       1.1  kiyohara 		callout_stop(&sc->sc_callout);
    171       1.1  kiyohara 
    172       1.1  kiyohara 	delay(50);	/* XXXX */
    173       1.1  kiyohara 
    174       1.1  kiyohara 	if (nbppcon_kbd_ready(sc->sc_parent) != 0)
    175       1.1  kiyohara 		aprint_error_dev(sc->sc_dev,
    176       1.1  kiyohara 		    "%s: kbd-ready failed\n", __func__);
    177       1.1  kiyohara 
    178       1.1  kiyohara 	return 0;
    179       1.1  kiyohara }
    180       1.1  kiyohara 
    181       1.1  kiyohara static int
    182       1.1  kiyohara nbpkbd_poll(void *arg)
    183       1.1  kiyohara {
    184       1.1  kiyohara 	struct nbpkbd_softc *sc = arg;
    185       1.1  kiyohara 	int rv, s;
    186       1.1  kiyohara 	char buf[2];
    187       1.1  kiyohara 
    188       1.1  kiyohara 	if (!sc->sc_enabled)
    189       1.1  kiyohara 		return 0;
    190       1.1  kiyohara 
    191       1.1  kiyohara 	s = spltty();
    192       1.1  kiyohara 
    193       1.1  kiyohara 	rv = nbppcon_poll(sc->sc_parent, sc->sc_tag, sizeof(buf), buf);
    194       1.1  kiyohara 
    195       1.1  kiyohara 	DPRINTF(("%s: received %02x %02x\n",
    196       1.1  kiyohara 	    device_xname(sc->sc_dev), buf[0], buf[1]));
    197       1.1  kiyohara 
    198       1.1  kiyohara 	hpckbd_input(sc->sc_hpckbd, IS_KEY_DOWN(buf[1]), KEY_CODE(buf[1]));
    199       1.1  kiyohara 
    200       1.1  kiyohara 	delay(50);	/* XXXX */
    201       1.1  kiyohara 
    202       1.1  kiyohara 	rv = nbppcon_kbd_ready(sc->sc_parent);
    203       1.1  kiyohara 
    204       1.1  kiyohara 	splx(s);
    205       1.1  kiyohara 
    206       1.1  kiyohara 	if (rv != 0)
    207       1.1  kiyohara 		aprint_error_dev(sc->sc_dev,
    208       1.1  kiyohara 		    "%s: kbd-ready failed\n", __func__);
    209       1.1  kiyohara 
    210       1.1  kiyohara 	return 0;
    211       1.1  kiyohara }
    212       1.1  kiyohara 
    213       1.1  kiyohara static void
    214       1.1  kiyohara nbpkbd_key_repeat(void *arg)
    215       1.1  kiyohara {
    216       1.1  kiyohara 	struct nbpkbd_softc *sc = arg;
    217       1.1  kiyohara 
    218       1.1  kiyohara 	hpckbd_input(sc->sc_hpckbd, IS_KEY_DOWN(sc->sc_last_scancode),
    219       1.1  kiyohara 	    KEY_CODE(sc->sc_last_scancode));
    220       1.1  kiyohara 
    221       1.1  kiyohara 	if (IS_KEY_DOWN(sc->sc_last_scancode))
    222       1.1  kiyohara 		callout_schedule(&sc->sc_callout, KEY_REPEAT_INTERVAL);
    223       1.1  kiyohara }
    224