Home | History | Annotate | Line # | Download | only in dev
j6x0tp.c revision 1.21.16.1
      1  1.21.16.1       mjf /*	$NetBSD: j6x0tp.c,v 1.21.16.1 2008/04/03 12:42:17 mjf Exp $ */
      2        1.1       uwe 
      3        1.1       uwe /*
      4        1.1       uwe  * Copyright (c) 2003 Valeriy E. Ushakov
      5        1.1       uwe  * All rights reserved.
      6        1.1       uwe  *
      7        1.1       uwe  * Redistribution and use in source and binary forms, with or without
      8        1.1       uwe  * modification, are permitted provided that the following conditions
      9        1.1       uwe  * are met:
     10        1.1       uwe  * 1. Redistributions of source code must retain the above copyright
     11        1.1       uwe  *    notice, this list of conditions and the following disclaimer.
     12        1.1       uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       uwe  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       uwe  *    documentation and/or other materials provided with the distribution.
     15        1.1       uwe  * 3. The name of the author may not be used to endorse or promote products
     16        1.1       uwe  *    derived from this software without specific prior written permission
     17        1.1       uwe  *
     18        1.1       uwe  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19        1.1       uwe  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20        1.1       uwe  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21        1.1       uwe  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22        1.1       uwe  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23        1.1       uwe  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24        1.1       uwe  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25        1.1       uwe  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26        1.1       uwe  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27        1.1       uwe  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28        1.1       uwe  */
     29        1.1       uwe 
     30        1.1       uwe #include <sys/cdefs.h>
     31  1.21.16.1       mjf __KERNEL_RCSID(0, "$NetBSD: j6x0tp.c,v 1.21.16.1 2008/04/03 12:42:17 mjf Exp $");
     32        1.1       uwe 
     33        1.1       uwe #include <sys/param.h>
     34        1.1       uwe #include <sys/kernel.h>
     35        1.1       uwe #include <sys/device.h>
     36        1.1       uwe #include <sys/malloc.h>
     37        1.1       uwe #include <sys/systm.h>
     38        1.1       uwe #include <sys/callout.h>
     39        1.1       uwe 
     40        1.3       uwe #include "opt_j6x0tp.h"
     41        1.3       uwe 
     42        1.1       uwe #include <dev/wscons/wsconsio.h>
     43        1.1       uwe #include <dev/wscons/wsmousevar.h>
     44        1.3       uwe #include <dev/wscons/wskbdvar.h>
     45        1.3       uwe #include <dev/wscons/wsksymvar.h>
     46        1.3       uwe #include <dev/wscons/wsksymdef.h>
     47        1.5    tsarna #include <dev/hpc/hpctpanelvar.h>
     48        1.1       uwe 
     49        1.1       uwe #include <machine/platid.h>
     50        1.1       uwe #include <machine/platid_mask.h>
     51        1.1       uwe 
     52        1.1       uwe #include <machine/intr.h>
     53        1.1       uwe 
     54        1.1       uwe #include <sh3/exception.h>
     55        1.1       uwe #include <sh3/intcreg.h>
     56        1.1       uwe #include <sh3/pfcreg.h>
     57        1.1       uwe #include <sh3/adcreg.h>
     58        1.1       uwe 
     59        1.2       uwe #include <sh3/dev/adcvar.h>
     60        1.2       uwe 
     61        1.2       uwe 
     62        1.1       uwe #if 0 /* XXX: disabled in favor of local version that uses printf_nolog */
     63        1.1       uwe #define DPRINTF_ENABLE
     64        1.1       uwe #define DPRINTF_DEBUG	j6x0tp_debug
     65        1.1       uwe #define DPRINTF_LEVEL	0
     66        1.1       uwe #include <machine/debug.h>
     67        1.1       uwe #else
     68        1.1       uwe #ifdef J6X0TP_DEBUG
     69        1.1       uwe volatile int j6x0tp_debug = 0;
     70        1.3       uwe #define DPRINTF_PRINTF		printf_nolog
     71        1.3       uwe #define DPRINTF(arg)		if (j6x0tp_debug) DPRINTF_PRINTF arg
     72        1.3       uwe #define DPRINTFN(n, arg)	if (j6x0tp_debug > (n)) DPRINTF_PRINTF arg
     73        1.1       uwe #else
     74        1.3       uwe #define DPRINTF(arg)		((void)0)
     75        1.1       uwe #define DPRINTFN(n, arg)	((void)0)
     76        1.1       uwe #endif
     77        1.1       uwe #endif
     78        1.1       uwe 
     79        1.1       uwe 
     80        1.3       uwe /*
     81        1.3       uwe  * PFC bits pertinent to Jornada 6x0 touchpanel
     82        1.3       uwe  */
     83        1.1       uwe #define PHDR_TP_PEN_DOWN	0x08
     84        1.1       uwe 
     85        1.1       uwe #define SCPDR_TP_SCAN_ENABLE	0x20
     86        1.1       uwe #define SCPDR_TP_SCAN_Y		0x02
     87        1.1       uwe #define SCPDR_TP_SCAN_X		0x01
     88        1.1       uwe 
     89        1.3       uwe /*
     90        1.4       uwe  * A/D converter channels to get x/y from
     91        1.3       uwe  */
     92        1.1       uwe #define ADC_CHANNEL_TP_Y	1
     93        1.1       uwe #define ADC_CHANNEL_TP_X	2
     94        1.1       uwe 
     95        1.3       uwe /*
     96        1.3       uwe  * Default (read: my device :) raw X/Y values for framebuffer edges.
     97        1.3       uwe  * XXX: defopt these?
     98        1.3       uwe  */
     99        1.3       uwe #define J6X0TP_FB_LEFT		 38
    100        1.3       uwe #define J6X0TP_FB_RIGHT		950
    101        1.3       uwe #define J6X0TP_FB_TOP		 80
    102        1.3       uwe #define J6X0TP_FB_BOTTOM	900
    103        1.3       uwe 
    104        1.3       uwe /*
    105        1.3       uwe  * Bottom of the n'th hard icon (n = 1..4)
    106        1.3       uwe  */
    107        1.3       uwe #define J6X0TP_HARD_ICON_MAX_Y(n) \
    108        1.3       uwe 	(J6X0TP_FB_TOP + ((J6X0TP_FB_BOTTOM - J6X0TP_FB_TOP) / 4) * (n))
    109        1.3       uwe 
    110        1.1       uwe 
    111        1.1       uwe struct j6x0tp_softc {
    112  1.21.16.1       mjf 	device_t sc_dev;
    113        1.1       uwe 
    114        1.3       uwe #define J6X0TP_WSMOUSE_ENABLED	0x01
    115        1.3       uwe #define J6X0TP_WSKBD_ENABLED	0x02
    116        1.3       uwe 	int sc_enabled;
    117        1.3       uwe 
    118        1.3       uwe 	int sc_hard_icon;
    119        1.3       uwe 
    120        1.1       uwe 	struct callout sc_touch_ch;
    121        1.1       uwe 
    122  1.21.16.1       mjf 	device_t sc_wsmousedev;
    123  1.21.16.1       mjf 	device_t sc_wskbddev;
    124        1.3       uwe 
    125        1.3       uwe 	struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */
    126        1.1       uwe };
    127        1.1       uwe 
    128        1.3       uwe 
    129        1.3       uwe /* config machinery */
    130  1.21.16.1       mjf static int	j6x0tp_match(device_t, cfdata_t, void *);
    131  1.21.16.1       mjf static void	j6x0tp_attach(device_t, device_t, void *);
    132        1.3       uwe 
    133        1.3       uwe /* wsmouse accessops */
    134        1.3       uwe static int	j6x0tp_wsmouse_enable(void *);
    135  1.21.16.1       mjf static int	j6x0tp_wsmouse_ioctl(void *, u_long, void *, int, lwp_t *);
    136        1.3       uwe static void	j6x0tp_wsmouse_disable(void *);
    137        1.3       uwe 
    138        1.3       uwe /* wskbd accessops */
    139        1.3       uwe static int	j6x0tp_wskbd_enable(void *, int);
    140        1.3       uwe static void	j6x0tp_wskbd_set_leds(void *, int);
    141  1.21.16.1       mjf static int	j6x0tp_wskbd_ioctl(void *, u_long, void *, int, lwp_t *);
    142        1.3       uwe 
    143        1.3       uwe /* internal driver routines */
    144        1.3       uwe static void	j6x0tp_enable(struct j6x0tp_softc *);
    145        1.3       uwe static void	j6x0tp_disable(struct j6x0tp_softc *);
    146       1.18       uwe static int	j6x0tp_enable_child(struct j6x0tp_softc *, int, int);
    147        1.3       uwe static int	j6x0tp_intr(void *);
    148        1.3       uwe static void	j6x0tp_start_polling(void *);
    149        1.3       uwe static void	j6x0tp_stop_polling(struct j6x0tp_softc *);
    150        1.3       uwe static void	j6x0tp_callout_wsmouse(void *);
    151        1.3       uwe static void	j6x0tp_callout_wskbd(void *);
    152        1.3       uwe static void	j6x0tp_wsmouse_input(struct j6x0tp_softc *, int, int);
    153        1.3       uwe static void	j6x0tp_get_raw_xy(int *, int *);
    154        1.3       uwe static int	j6x0tp_get_hard_icon(int, int);
    155        1.1       uwe 
    156        1.1       uwe 
    157       1.11       uwe static const struct wsmouse_accessops j6x0tp_accessops = {
    158        1.3       uwe 	j6x0tp_wsmouse_enable,
    159        1.3       uwe 	j6x0tp_wsmouse_ioctl,
    160        1.3       uwe 	j6x0tp_wsmouse_disable
    161        1.1       uwe };
    162        1.1       uwe 
    163        1.1       uwe static const struct wsmouse_calibcoords j6x0tp_default_calib = {
    164        1.1       uwe 	0, 0, 639, 239,
    165        1.1       uwe 	4,
    166        1.3       uwe 	{{ J6X0TP_FB_LEFT,  J6X0TP_FB_TOP,      0,   0 },
    167        1.3       uwe 	 { J6X0TP_FB_RIGHT, J6X0TP_FB_TOP,    639,   0 },
    168        1.3       uwe 	 { J6X0TP_FB_LEFT,  J6X0TP_FB_BOTTOM,   0, 239 },
    169        1.3       uwe 	 { J6X0TP_FB_RIGHT, J6X0TP_FB_BOTTOM, 639, 239 }}
    170        1.3       uwe };
    171        1.3       uwe 
    172       1.11       uwe static const struct wskbd_accessops j6x0tp_wskbd_accessops = {
    173        1.3       uwe 	j6x0tp_wskbd_enable,
    174        1.3       uwe 	j6x0tp_wskbd_set_leds,
    175        1.3       uwe 	j6x0tp_wskbd_ioctl
    176        1.3       uwe };
    177        1.3       uwe 
    178        1.3       uwe 
    179        1.3       uwe #ifndef J6X0TP_SETTINGS_ICON_KEYSYM
    180        1.3       uwe #define J6X0TP_SETTINGS_ICON_KEYSYM	KS_Home
    181        1.3       uwe #endif
    182        1.3       uwe #ifndef J6X0TP_PGUP_ICON_KEYSYM
    183        1.3       uwe #define J6X0TP_PGUP_ICON_KEYSYM		KS_Prior
    184        1.3       uwe #endif
    185        1.3       uwe #ifndef J6X0TP_PGDN_ICON_KEYSYM
    186        1.3       uwe #define J6X0TP_PGDN_ICON_KEYSYM		KS_Next
    187        1.3       uwe #endif
    188        1.3       uwe #ifndef J6X0TP_SWITCH_ICON_KEYSYM
    189        1.3       uwe #define J6X0TP_SWITCH_ICON_KEYSYM	KS_End
    190        1.3       uwe #endif
    191        1.3       uwe 
    192        1.3       uwe static const keysym_t j6x0tp_wskbd_keydesc[] = {
    193        1.3       uwe 	KS_KEYCODE(1), J6X0TP_SETTINGS_ICON_KEYSYM,
    194        1.3       uwe 	KS_KEYCODE(2), J6X0TP_PGUP_ICON_KEYSYM,
    195        1.3       uwe 	KS_KEYCODE(3), J6X0TP_PGDN_ICON_KEYSYM,
    196        1.3       uwe 	KS_KEYCODE(4), J6X0TP_SWITCH_ICON_KEYSYM
    197        1.3       uwe };
    198        1.3       uwe 
    199       1.11       uwe static const struct wscons_keydesc j6x0tp_wskbd_keydesctab[] = {
    200        1.3       uwe 	{ KB_US, 0,
    201        1.3       uwe 	  sizeof(j6x0tp_wskbd_keydesc)/sizeof(keysym_t),
    202        1.3       uwe 	  j6x0tp_wskbd_keydesc
    203        1.3       uwe 	},
    204        1.3       uwe 	{0, 0, 0, 0}
    205        1.3       uwe };
    206        1.3       uwe 
    207       1.11       uwe static const struct wskbd_mapdata j6x0tp_wskbd_keymapdata = {
    208        1.3       uwe         j6x0tp_wskbd_keydesctab, KB_US
    209        1.1       uwe };
    210        1.1       uwe 
    211        1.1       uwe 
    212  1.21.16.1       mjf CFATTACH_DECL_NEW(j6x0tp, sizeof(struct j6x0tp_softc),
    213        1.3       uwe     j6x0tp_match, j6x0tp_attach, NULL, NULL);
    214        1.1       uwe 
    215        1.1       uwe 
    216        1.1       uwe static int
    217  1.21.16.1       mjf j6x0tp_match(device_t parent, cfdata_t cf, void *aux)
    218        1.1       uwe {
    219        1.1       uwe 
    220        1.1       uwe 	/*
    221        1.8       uwe 	 * XXX: platid_mask_MACH_HP_LX also matches 360LX.  It's not
    222        1.8       uwe 	 * confirmed whether touch panel in 360LX is connected this
    223        1.8       uwe 	 * way.  We may need to regroup platid masks.
    224        1.1       uwe 	 */
    225        1.8       uwe 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX)
    226        1.8       uwe 	    && !platid_match(&platid, &platid_mask_MACH_HP_LX))
    227        1.1       uwe 		return (0);
    228        1.1       uwe 
    229        1.3       uwe 	if (strcmp(cf->cf_name, "j6x0tp") != 0)
    230        1.1       uwe 		return (0);
    231        1.1       uwe 
    232        1.1       uwe 	return (1);
    233        1.1       uwe }
    234        1.1       uwe 
    235        1.1       uwe 
    236        1.1       uwe static void
    237  1.21.16.1       mjf j6x0tp_attach(device_t parent, device_t self, void *aux)
    238        1.1       uwe {
    239  1.21.16.1       mjf 	struct j6x0tp_softc *sc;
    240        1.1       uwe 	struct wsmousedev_attach_args wsma;
    241        1.3       uwe 	struct wskbddev_attach_args wska;
    242        1.1       uwe 
    243  1.21.16.1       mjf 	aprint_naive("\n");
    244  1.21.16.1       mjf 	aprint_normal("\n");
    245  1.21.16.1       mjf 
    246  1.21.16.1       mjf 	sc = device_private(self);
    247  1.21.16.1       mjf 	sc->sc_dev = self;
    248        1.1       uwe 
    249        1.1       uwe 	sc->sc_enabled = 0;
    250        1.3       uwe 	sc->sc_hard_icon = 0;
    251        1.1       uwe 
    252        1.3       uwe 	/* touch-panel as a pointing device */
    253        1.1       uwe 	wsma.accessops = &j6x0tp_accessops;
    254        1.1       uwe 	wsma.accesscookie = sc;
    255        1.1       uwe 
    256        1.7  drochner 	sc->sc_wsmousedev = config_found_ia(self, "wsmousedev", &wsma,
    257        1.7  drochner 					    wsmousedevprint);
    258        1.1       uwe 	if (sc->sc_wsmousedev == NULL)
    259        1.1       uwe 		return;
    260        1.1       uwe 
    261        1.3       uwe 	/* on-screen "hard icons" as a keyboard device */
    262        1.3       uwe 	wska.console = 0;
    263        1.3       uwe 	wska.keymap = &j6x0tp_wskbd_keymapdata;
    264        1.3       uwe 	wska.accessops = &j6x0tp_wskbd_accessops;
    265        1.3       uwe 	wska.accesscookie = sc;
    266        1.3       uwe 
    267        1.9       uwe 	sc->sc_wskbddev = config_found_ia(self, "wskbddev", &wska,
    268        1.7  drochner 					  wskbddevprint);
    269        1.3       uwe 
    270        1.4       uwe 	/* init calibration, set default parameters */
    271        1.1       uwe 	tpcalib_init(&sc->sc_tpcalib);
    272        1.1       uwe 	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    273       1.16  christos 		      (void *)__UNCONST(&j6x0tp_default_calib), 0, 0);
    274        1.1       uwe 
    275        1.3       uwe 	/* used when in polling mode */
    276       1.19       uwe 	callout_init(&sc->sc_touch_ch, 0);
    277        1.1       uwe 
    278        1.3       uwe 	/* establish interrupt handler, but disable until opened */
    279        1.1       uwe 	intc_intr_establish(SH7709_INTEVT2_IRQ3, IST_EDGE, IPL_TTY,
    280        1.1       uwe 			    j6x0tp_intr, sc);
    281        1.3       uwe 	intc_intr_disable(SH7709_INTEVT2_IRQ3);
    282        1.1       uwe }
    283        1.1       uwe 
    284        1.1       uwe 
    285        1.3       uwe /*
    286        1.3       uwe  * Enable touch panel:  we start in interrupt mode.
    287        1.3       uwe  * Must be called as spltty().
    288        1.3       uwe  */
    289        1.3       uwe static void
    290        1.3       uwe j6x0tp_enable(struct j6x0tp_softc *sc)
    291        1.3       uwe {
    292        1.3       uwe 
    293  1.21.16.1       mjf 	DPRINTFN(2, ("%s: enable\n", device_xname(sc->sc_dev)));
    294        1.3       uwe 	intc_intr_enable(SH7709_INTEVT2_IRQ3);
    295        1.3       uwe }
    296        1.3       uwe 
    297        1.3       uwe 
    298        1.3       uwe /*
    299        1.3       uwe  * Disable touch panel: disable interrupt, cancel pending callout.
    300        1.3       uwe  * Must be called as spltty().
    301        1.3       uwe  */
    302        1.3       uwe static void
    303        1.3       uwe j6x0tp_disable(struct j6x0tp_softc *sc)
    304        1.3       uwe {
    305        1.3       uwe 
    306  1.21.16.1       mjf 	DPRINTFN(2, ("%s: disable\n", device_xname(sc->sc_dev)));
    307        1.3       uwe 	intc_intr_disable(SH7709_INTEVT2_IRQ3);
    308        1.3       uwe 	callout_stop(&sc->sc_touch_ch);
    309        1.3       uwe }
    310        1.3       uwe 
    311        1.1       uwe 
    312        1.3       uwe static int
    313       1.18       uwe j6x0tp_enable_child(struct j6x0tp_softc *sc, int child, int on)
    314        1.3       uwe {
    315        1.3       uwe 	int s = spltty();
    316        1.1       uwe 
    317        1.3       uwe 	if (on) {
    318        1.3       uwe 		if (!sc->sc_enabled)
    319        1.3       uwe 			j6x0tp_enable(sc);
    320        1.3       uwe 		sc->sc_enabled |= child;
    321        1.3       uwe 	} else {
    322        1.3       uwe 		sc->sc_enabled &= ~child;
    323        1.3       uwe 		if (!sc->sc_enabled)
    324        1.3       uwe 			j6x0tp_disable(sc);
    325        1.3       uwe 	}
    326        1.1       uwe 
    327        1.1       uwe 	splx(s);
    328        1.1       uwe 	return (0);
    329        1.1       uwe }
    330        1.1       uwe 
    331        1.1       uwe 
    332        1.3       uwe static int
    333  1.21.16.1       mjf j6x0tp_wsmouse_enable(void *arg)
    334        1.3       uwe {
    335  1.21.16.1       mjf 	struct j6x0tp_softc *sc = arg;
    336        1.3       uwe 
    337  1.21.16.1       mjf 	DPRINTFN(1, ("%s: wsmouse enable\n", device_xname(sc->sc_dev)));
    338       1.18       uwe 	return (j6x0tp_enable_child(sc, J6X0TP_WSMOUSE_ENABLED, 1));
    339        1.3       uwe }
    340        1.3       uwe 
    341        1.3       uwe 
    342        1.1       uwe static void
    343  1.21.16.1       mjf j6x0tp_wsmouse_disable(void *arg)
    344        1.1       uwe {
    345  1.21.16.1       mjf 	struct j6x0tp_softc *sc = arg;
    346        1.1       uwe 
    347  1.21.16.1       mjf 	DPRINTFN(1, ("%s: wsmouse disable\n", device_xname(sc->sc_dev)));
    348       1.18       uwe 	j6x0tp_enable_child(sc, J6X0TP_WSMOUSE_ENABLED, 0);
    349        1.3       uwe }
    350        1.1       uwe 
    351        1.1       uwe 
    352        1.3       uwe static int
    353  1.21.16.1       mjf j6x0tp_wskbd_enable(void *arg, int on)
    354        1.3       uwe {
    355  1.21.16.1       mjf 	struct j6x0tp_softc *sc = arg;
    356        1.1       uwe 
    357  1.21.16.1       mjf 	DPRINTFN(1, ("%s: wskbd %sable\n", device_xname(sc->sc_dev),
    358        1.3       uwe 		     on ? "en" : "dis"));
    359       1.18       uwe 	return (j6x0tp_enable_child(sc, J6X0TP_WSKBD_ENABLED, on));
    360        1.1       uwe }
    361        1.1       uwe 
    362        1.1       uwe 
    363        1.1       uwe static int
    364  1.21.16.1       mjf j6x0tp_intr(void *arg)
    365        1.1       uwe {
    366  1.21.16.1       mjf 	struct j6x0tp_softc *sc = arg;
    367        1.1       uwe 
    368        1.1       uwe 	uint8_t irr0;
    369        1.1       uwe 	uint8_t phdr, touched;
    370        1.1       uwe 	unsigned int steady, tremor_timeout;
    371        1.1       uwe 
    372        1.1       uwe 	irr0 = _reg_read_1(SH7709_IRR0);
    373        1.1       uwe 	if ((irr0 & IRR0_IRQ3) == 0) {
    374        1.1       uwe #ifdef DIAGNOSTIC
    375  1.21.16.1       mjf 		printf("%s: irr0 %02x?\n", device_xname(sc->sc_dev), irr0);
    376        1.1       uwe #endif
    377        1.1       uwe 		return (0);
    378        1.1       uwe 	}
    379        1.1       uwe 
    380        1.3       uwe 	if (!sc->sc_enabled) {
    381       1.17       uwe 		DPRINTFN(1, ("%s: intr: !sc_enabled\n",
    382  1.21.16.1       mjf 			     device_xname(sc->sc_dev)));
    383        1.3       uwe 		intc_intr_disable(SH7709_INTEVT2_IRQ3);
    384        1.3       uwe 		goto served;
    385        1.3       uwe 	}
    386        1.3       uwe 
    387        1.3       uwe 
    388        1.1       uwe 	/*
    389        1.1       uwe 	 * Number of times the "touched" bit should be read
    390        1.1       uwe 	 * consecutively.
    391        1.1       uwe 	 */
    392        1.1       uwe #	define TREMOR_THRESHOLD 0x300
    393        1.1       uwe 
    394        1.1       uwe 	steady = 0;
    395        1.1       uwe 	tremor_timeout = TREMOR_THRESHOLD * 16;	/* XXX: arbitrary */
    396        1.1       uwe 	touched = PHDR_TP_PEN_DOWN;	/* we start with "touched" state */
    397        1.1       uwe 
    398        1.1       uwe 	do {
    399        1.1       uwe 		phdr = _reg_read_1(SH7709_PHDR);
    400        1.1       uwe 
    401        1.1       uwe 		if ((phdr & PHDR_TP_PEN_DOWN) == touched)
    402        1.1       uwe 			++steady;
    403        1.1       uwe 		else {
    404        1.1       uwe 			steady = 0;
    405        1.1       uwe 			touched = phdr & PHDR_TP_PEN_DOWN;
    406        1.1       uwe 		}
    407        1.1       uwe 
    408        1.1       uwe 		if (--tremor_timeout == 0) {
    409        1.1       uwe 			DPRINTF(("%s: tremor timeout!\n",
    410  1.21.16.1       mjf 				 device_xname(sc->sc_dev)));
    411        1.1       uwe 			goto served;
    412        1.1       uwe 		}
    413        1.1       uwe 	} while (steady < TREMOR_THRESHOLD);
    414        1.1       uwe 
    415        1.3       uwe 	if (touched) {
    416        1.3       uwe 		intc_intr_disable(SH7709_INTEVT2_IRQ3);
    417        1.3       uwe 
    418        1.3       uwe 		/*
    419        1.3       uwe 		 * ADC readings are not stable yet, so schedule
    420        1.3       uwe 		 * callout instead of accessing ADC from the interrupt
    421        1.3       uwe 		 * handler only to immediately delay().
    422        1.3       uwe 		 */
    423        1.3       uwe 		callout_reset(&sc->sc_touch_ch, hz/32,
    424        1.3       uwe 			      j6x0tp_start_polling, sc);
    425        1.3       uwe 	} else
    426  1.21.16.1       mjf 		DPRINTFN(1, ("%s: tremor\n", device_xname(sc->sc_dev)));
    427        1.3       uwe   served:
    428        1.3       uwe 	/* clear the interrupt (XXX: protect access?) */
    429        1.3       uwe 	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);
    430        1.3       uwe 
    431        1.3       uwe 	return (1);
    432        1.3       uwe }
    433        1.3       uwe 
    434        1.3       uwe 
    435        1.3       uwe /*
    436        1.3       uwe  * Called from the interrupt handler at spltty() upon first touch.
    437        1.3       uwe  * Decide if we are going to report this touch as a mouse click/drag
    438        1.3       uwe  * or as a key press.
    439        1.3       uwe  */
    440        1.3       uwe static void
    441  1.21.16.1       mjf j6x0tp_start_polling(void *arg)
    442        1.3       uwe {
    443  1.21.16.1       mjf 	struct j6x0tp_softc *sc = arg;
    444        1.3       uwe 	uint8_t phdr;
    445        1.3       uwe 	int do_mouse, do_kbd;
    446        1.3       uwe 	int rawx, rawy;
    447        1.3       uwe 	int icon;
    448        1.3       uwe 
    449        1.3       uwe 	phdr = _reg_read_1(SH7709_PHDR);
    450        1.3       uwe 	if ((phdr & PHDR_TP_PEN_DOWN) == 0) {
    451        1.3       uwe 		DPRINTFN(2, ("%s: start: pen is not down\n",
    452  1.21.16.1       mjf 			     device_xname(sc->sc_dev)));
    453        1.3       uwe 		j6x0tp_stop_polling(sc);
    454        1.1       uwe 	}
    455        1.1       uwe 
    456        1.3       uwe 	j6x0tp_get_raw_xy(&rawx, &rawy);
    457        1.3       uwe 	DPRINTFN(2, ("%s: start: %4d %4d -> ",
    458  1.21.16.1       mjf 		     device_xname(sc->sc_dev), rawx, rawy));
    459        1.3       uwe 
    460        1.3       uwe 	do_mouse = sc->sc_enabled & J6X0TP_WSMOUSE_ENABLED;
    461        1.3       uwe #ifdef J6X0TP_WSMOUSE_EXCLUSIVE
    462        1.3       uwe 	if (do_mouse)
    463        1.3       uwe 		do_kbd = 0;
    464        1.3       uwe 	else
    465        1.3       uwe #endif
    466        1.3       uwe 		do_kbd = sc->sc_enabled & J6X0TP_WSKBD_ENABLED;
    467        1.1       uwe 
    468        1.3       uwe 	icon = 0;
    469        1.3       uwe 	if (do_kbd)
    470        1.3       uwe 		icon = j6x0tp_get_hard_icon(rawx, rawy);
    471        1.3       uwe 
    472        1.3       uwe 	if (icon != 0) {
    473        1.3       uwe 		DPRINTFN(2, ("icon %d\n", icon));
    474        1.3       uwe 		sc->sc_hard_icon = icon;
    475        1.3       uwe 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, icon);
    476        1.3       uwe 		callout_reset(&sc->sc_touch_ch, hz/32,
    477        1.3       uwe 			      j6x0tp_callout_wskbd, sc);
    478        1.3       uwe 	} else if (do_mouse) {
    479        1.3       uwe 		DPRINTFN(2, ("mouse\n"));
    480        1.3       uwe 		j6x0tp_wsmouse_input(sc, rawx, rawy);
    481        1.1       uwe 		callout_reset(&sc->sc_touch_ch, hz/32,
    482        1.3       uwe 			      j6x0tp_callout_wsmouse, sc);
    483        1.3       uwe 	} else {
    484        1.3       uwe 		DPRINTFN(2, ("ignore\n"));
    485        1.3       uwe 		j6x0tp_stop_polling(sc);
    486        1.3       uwe 	}
    487        1.3       uwe }
    488        1.3       uwe 
    489        1.3       uwe 
    490        1.3       uwe /*
    491        1.3       uwe  * Re-enable touch panel interrupt.
    492        1.3       uwe  * Called as spltty() when polling code detects pen-up.
    493        1.3       uwe  */
    494        1.3       uwe static void
    495        1.3       uwe j6x0tp_stop_polling(struct j6x0tp_softc *sc)
    496        1.3       uwe {
    497        1.3       uwe 	uint8_t irr0;
    498        1.3       uwe 
    499  1.21.16.1       mjf 	DPRINTFN(2, ("%s: stop\n", device_xname(sc->sc_dev)));
    500        1.1       uwe 
    501        1.3       uwe 	/* clear pending interrupt signal before re-enabling the interrupt */
    502        1.3       uwe 	irr0 = _reg_read_1(SH7709_IRR0);
    503        1.3       uwe 	if ((irr0 & IRR0_IRQ3) != 0)
    504        1.3       uwe 		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);
    505        1.1       uwe 
    506        1.3       uwe 	intc_intr_enable(SH7709_INTEVT2_IRQ3);
    507        1.1       uwe }
    508        1.1       uwe 
    509        1.1       uwe 
    510        1.3       uwe /*
    511        1.3       uwe  * We are reporting this touch as a keyboard event.
    512        1.3       uwe  * Poll touch screen waiting for pen-up.
    513        1.3       uwe  */
    514        1.1       uwe static void
    515  1.21.16.1       mjf j6x0tp_callout_wskbd(void *arg)
    516        1.1       uwe {
    517  1.21.16.1       mjf 	struct j6x0tp_softc *sc = arg;
    518        1.3       uwe 	uint8_t phdr;
    519        1.1       uwe 	int s;
    520        1.1       uwe 
    521        1.1       uwe 	s = spltty();
    522        1.1       uwe 
    523        1.1       uwe 	if (!sc->sc_enabled) {
    524        1.3       uwe 		DPRINTFN(1, ("%s: wskbd callout: !sc_enabled\n",
    525  1.21.16.1       mjf 			     device_xname(sc->sc_dev)));
    526        1.1       uwe 		splx(s);
    527        1.1       uwe 		return;
    528        1.1       uwe 	}
    529        1.1       uwe 
    530        1.1       uwe 	phdr = _reg_read_1(SH7709_PHDR);
    531        1.3       uwe 	if ((phdr & PHDR_TP_PEN_DOWN) != 0) {
    532        1.3       uwe 		/*
    533        1.3       uwe 		 * Pen is still down, continue polling.  Wskbd's
    534        1.3       uwe 		 * auto-repeat takes care of repeating the key.
    535        1.3       uwe 		 */
    536        1.3       uwe 		callout_schedule(&sc->sc_touch_ch, hz/32);
    537        1.3       uwe 	} else {
    538        1.3       uwe 		wskbd_input(sc->sc_wskbddev,
    539        1.3       uwe 			    WSCONS_EVENT_KEY_UP, sc->sc_hard_icon);
    540        1.3       uwe 		j6x0tp_stop_polling(sc);
    541        1.3       uwe 	}
    542        1.3       uwe 	splx(s);
    543        1.3       uwe }
    544        1.3       uwe 
    545        1.1       uwe 
    546        1.3       uwe /*
    547        1.3       uwe  * We are reporting this touch as a mouse click/drag.
    548        1.3       uwe  */
    549        1.3       uwe static void
    550  1.21.16.1       mjf j6x0tp_callout_wsmouse(void *arg)
    551        1.3       uwe {
    552  1.21.16.1       mjf 	struct j6x0tp_softc *sc = arg;
    553        1.3       uwe 	uint8_t phdr;
    554        1.3       uwe 	int rawx, rawy;
    555        1.3       uwe 	int s;
    556        1.1       uwe 
    557        1.3       uwe 	s = spltty();
    558        1.1       uwe 
    559        1.3       uwe 	if (!sc->sc_enabled) {
    560        1.3       uwe 		DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n",
    561  1.21.16.1       mjf 			     device_xname(sc->sc_dev)));
    562        1.1       uwe 		splx(s);
    563        1.1       uwe 		return;
    564        1.1       uwe 	}
    565        1.1       uwe 
    566        1.3       uwe 	phdr = _reg_read_1(SH7709_PHDR);
    567        1.3       uwe 	if ((phdr & PHDR_TP_PEN_DOWN) != 0) {
    568        1.3       uwe 		j6x0tp_get_raw_xy(&rawx, &rawy);
    569        1.3       uwe 		j6x0tp_wsmouse_input(sc, rawx, rawy); /* mouse dragged */
    570        1.3       uwe 		callout_schedule(&sc->sc_touch_ch, hz/32);
    571        1.3       uwe 	} else {
    572       1.15    plunky 		wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, 0, /* button up */
    573        1.3       uwe 			      WSMOUSE_INPUT_DELTA);
    574        1.3       uwe 		j6x0tp_stop_polling(sc);
    575        1.3       uwe 	}
    576        1.3       uwe 	splx(s);
    577        1.3       uwe }
    578        1.3       uwe 
    579        1.3       uwe 
    580        1.3       uwe /*
    581        1.3       uwe  * Report mouse click/drag.
    582        1.3       uwe  */
    583        1.3       uwe static void
    584        1.3       uwe j6x0tp_wsmouse_input(struct j6x0tp_softc *sc, int rawx, int rawy)
    585        1.3       uwe {
    586        1.3       uwe 	int x, y;
    587        1.3       uwe 
    588        1.3       uwe 	tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
    589        1.3       uwe 
    590        1.3       uwe 	DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n",
    591  1.21.16.1       mjf 		     device_xname(sc->sc_dev), rawx, rawy, x, y));
    592        1.3       uwe 
    593        1.3       uwe 	wsmouse_input(sc->sc_wsmousedev,
    594       1.15    plunky 			1,	/* button */
    595       1.15    plunky 			x, y, 0, 0,
    596       1.15    plunky 			WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    597        1.3       uwe }
    598        1.3       uwe 
    599        1.3       uwe 
    600        1.3       uwe /*
    601        1.3       uwe  * Read raw X/Y coordinates from the ADC.
    602        1.3       uwe  * XXX: protect accesses to SCPDR?
    603        1.3       uwe  */
    604        1.3       uwe static void
    605        1.3       uwe j6x0tp_get_raw_xy(int *rawxp, int *rawyp)
    606        1.3       uwe {
    607        1.3       uwe 	uint8_t scpdr;
    608        1.1       uwe 
    609        1.1       uwe 	/* Y axis */
    610        1.1       uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    611        1.1       uwe 	scpdr |=  SCPDR_TP_SCAN_ENABLE;
    612        1.1       uwe 	scpdr &= ~SCPDR_TP_SCAN_Y; /* pull low to scan */
    613        1.1       uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    614        1.1       uwe 	delay(10);
    615        1.3       uwe 
    616        1.3       uwe 	*rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y);
    617        1.1       uwe 
    618        1.1       uwe 	/* X axis */
    619        1.1       uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    620        1.1       uwe 	scpdr |=  SCPDR_TP_SCAN_Y;
    621        1.1       uwe 	scpdr &= ~SCPDR_TP_SCAN_X; /* pull low to scan */
    622        1.1       uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    623        1.1       uwe 	delay(10);
    624        1.3       uwe 
    625        1.3       uwe 	*rawxp = adc_sample_channel(ADC_CHANNEL_TP_X);
    626        1.1       uwe 
    627        1.1       uwe 	/* restore SCPDR */
    628        1.1       uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    629        1.1       uwe 	scpdr |=  SCPDR_TP_SCAN_X;
    630        1.1       uwe 	scpdr &= ~SCPDR_TP_SCAN_ENABLE;
    631        1.1       uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    632        1.3       uwe }
    633        1.1       uwe 
    634        1.1       uwe 
    635        1.3       uwe /*
    636        1.3       uwe  * Check if the (rawx, rawy) is inside one of the 4 hard icons.
    637        1.3       uwe  * Return the icon number 1..4, or 0 if not inside an icon.
    638        1.3       uwe  */
    639        1.3       uwe static int
    640        1.3       uwe j6x0tp_get_hard_icon(int rawx, int rawy)
    641        1.3       uwe {
    642        1.3       uwe 	if (rawx <= J6X0TP_FB_RIGHT)
    643        1.3       uwe 		return (0);
    644        1.1       uwe 
    645        1.3       uwe 	if (rawy < J6X0TP_HARD_ICON_MAX_Y(1))
    646        1.3       uwe 		return (1);
    647        1.3       uwe 	else if (rawy < J6X0TP_HARD_ICON_MAX_Y(2))
    648        1.3       uwe 		return (2);
    649        1.3       uwe 	else if (rawy < J6X0TP_HARD_ICON_MAX_Y(3))
    650        1.3       uwe 		return (3);
    651        1.3       uwe 	else
    652        1.3       uwe 		return (4);
    653        1.1       uwe }
    654        1.1       uwe 
    655        1.1       uwe 
    656        1.1       uwe static int
    657  1.21.16.1       mjf j6x0tp_wsmouse_ioctl(void *arg, u_long cmd, void *data, int flag, lwp_t *l)
    658        1.1       uwe {
    659  1.21.16.1       mjf 	struct j6x0tp_softc *sc = arg;
    660        1.1       uwe 
    661       1.10  christos 	return hpc_tpanel_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
    662        1.3       uwe }
    663        1.3       uwe 
    664        1.3       uwe 
    665        1.3       uwe static int
    666  1.21.16.1       mjf j6x0tp_wskbd_ioctl(void *arg, u_long cmd, void *data, int flag, lwp_t *l)
    667        1.3       uwe {
    668  1.21.16.1       mjf 	/* struct j6x0tp_softc *sc = arg; */
    669        1.3       uwe 
    670        1.3       uwe 	switch (cmd) {
    671        1.3       uwe 	case WSKBDIO_GTYPE:
    672        1.3       uwe 		*(int *)data = WSKBD_TYPE_HPC_BTN; /* may be use new type? */
    673        1.3       uwe 		return (0);
    674        1.3       uwe 
    675        1.3       uwe 	case WSKBDIO_GETLEDS:
    676        1.3       uwe 		*(int *)data = 0;
    677        1.3       uwe 		return (0);
    678        1.3       uwe 
    679        1.3       uwe 	default:
    680        1.3       uwe 		return (EPASSTHROUGH);
    681        1.3       uwe 	}
    682        1.3       uwe }
    683        1.3       uwe 
    684        1.3       uwe 
    685        1.3       uwe static void
    686  1.21.16.1       mjf j6x0tp_wskbd_set_leds(void *arg, int leds)
    687        1.3       uwe {
    688        1.3       uwe 
    689        1.3       uwe 	/* nothing to do*/
    690        1.3       uwe 	return;
    691        1.1       uwe }
    692