Home | History | Annotate | Line # | Download | only in dev
j6x0tp.c revision 1.4
      1  1.4  uwe /*	$NetBSD: j6x0tp.c,v 1.4 2003/11/05 00:28:34 uwe 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.4  uwe __KERNEL_RCSID(0, "$NetBSD: j6x0tp.c,v 1.4 2003/11/05 00:28:34 uwe 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 #ifdef GPROF
     40  1.1  uwe #include <sys/gmon.h>
     41  1.1  uwe #endif
     42  1.1  uwe 
     43  1.3  uwe #include "opt_j6x0tp.h"
     44  1.3  uwe 
     45  1.1  uwe #include <dev/wscons/wsconsio.h>
     46  1.1  uwe #include <dev/wscons/wsmousevar.h>
     47  1.3  uwe #include <dev/wscons/wskbdvar.h>
     48  1.3  uwe #include <dev/wscons/wsksymvar.h>
     49  1.3  uwe #include <dev/wscons/wsksymdef.h>
     50  1.1  uwe #include <dev/hpc/tpcalibvar.h>
     51  1.1  uwe 
     52  1.1  uwe #include <machine/platid.h>
     53  1.1  uwe #include <machine/platid_mask.h>
     54  1.1  uwe 
     55  1.1  uwe #include <machine/intr.h>
     56  1.1  uwe 
     57  1.1  uwe #include <sh3/exception.h>
     58  1.1  uwe #include <sh3/intcreg.h>
     59  1.1  uwe #include <sh3/pfcreg.h>
     60  1.1  uwe #include <sh3/adcreg.h>
     61  1.1  uwe 
     62  1.2  uwe #include <sh3/dev/adcvar.h>
     63  1.2  uwe 
     64  1.2  uwe 
     65  1.1  uwe #define J6X0TP_DEBUG
     66  1.1  uwe #if 0 /* XXX: disabled in favor of local version that uses printf_nolog */
     67  1.1  uwe #define DPRINTF_ENABLE
     68  1.1  uwe #define DPRINTF_DEBUG	j6x0tp_debug
     69  1.1  uwe #define DPRINTF_LEVEL	0
     70  1.1  uwe #include <machine/debug.h>
     71  1.1  uwe #else
     72  1.1  uwe #ifdef J6X0TP_DEBUG
     73  1.1  uwe volatile int j6x0tp_debug = 0;
     74  1.3  uwe #define DPRINTF_PRINTF		printf_nolog
     75  1.3  uwe #define DPRINTF(arg)		if (j6x0tp_debug) DPRINTF_PRINTF arg
     76  1.3  uwe #define DPRINTFN(n, arg)	if (j6x0tp_debug > (n)) DPRINTF_PRINTF arg
     77  1.1  uwe #else
     78  1.3  uwe #define DPRINTF(arg)		((void)0)
     79  1.1  uwe #define DPRINTFN(n, arg)	((void)0)
     80  1.1  uwe #endif
     81  1.1  uwe #endif
     82  1.1  uwe 
     83  1.1  uwe 
     84  1.3  uwe /*
     85  1.3  uwe  * PFC bits pertinent to Jornada 6x0 touchpanel
     86  1.3  uwe  */
     87  1.1  uwe #define PHDR_TP_PEN_DOWN	0x08
     88  1.1  uwe 
     89  1.1  uwe #define SCPDR_TP_SCAN_ENABLE	0x20
     90  1.1  uwe #define SCPDR_TP_SCAN_Y		0x02
     91  1.1  uwe #define SCPDR_TP_SCAN_X		0x01
     92  1.1  uwe 
     93  1.3  uwe /*
     94  1.4  uwe  * A/D converter channels to get x/y from
     95  1.3  uwe  */
     96  1.1  uwe #define ADC_CHANNEL_TP_Y	1
     97  1.1  uwe #define ADC_CHANNEL_TP_X	2
     98  1.1  uwe 
     99  1.3  uwe /*
    100  1.3  uwe  * Default (read: my device :) raw X/Y values for framebuffer edges.
    101  1.3  uwe  * XXX: defopt these?
    102  1.3  uwe  */
    103  1.3  uwe #define J6X0TP_FB_LEFT		 38
    104  1.3  uwe #define J6X0TP_FB_RIGHT		950
    105  1.3  uwe #define J6X0TP_FB_TOP		 80
    106  1.3  uwe #define J6X0TP_FB_BOTTOM	900
    107  1.3  uwe 
    108  1.3  uwe /*
    109  1.3  uwe  * Bottom of the n'th hard icon (n = 1..4)
    110  1.3  uwe  */
    111  1.3  uwe #define J6X0TP_HARD_ICON_MAX_Y(n) \
    112  1.3  uwe 	(J6X0TP_FB_TOP + ((J6X0TP_FB_BOTTOM - J6X0TP_FB_TOP) / 4) * (n))
    113  1.3  uwe 
    114  1.1  uwe 
    115  1.1  uwe struct j6x0tp_softc {
    116  1.1  uwe 	struct device sc_dev;
    117  1.1  uwe 
    118  1.3  uwe #define J6X0TP_WSMOUSE_ENABLED	0x01
    119  1.3  uwe #define J6X0TP_WSKBD_ENABLED	0x02
    120  1.3  uwe 	int sc_enabled;
    121  1.3  uwe 
    122  1.3  uwe 	int sc_hard_icon;
    123  1.3  uwe 
    124  1.1  uwe 	struct callout sc_touch_ch;
    125  1.1  uwe 
    126  1.1  uwe 	struct device *sc_wsmousedev;
    127  1.3  uwe 	struct device *sc_wskbddev;
    128  1.3  uwe 
    129  1.3  uwe 	struct tpcalib_softc sc_tpcalib; /* calibration info for wsmouse */
    130  1.1  uwe };
    131  1.1  uwe 
    132  1.3  uwe 
    133  1.3  uwe /* config machinery */
    134  1.1  uwe static int	j6x0tp_match(struct device *, struct cfdata *, void *);
    135  1.1  uwe static void	j6x0tp_attach(struct device *, struct device *, void *);
    136  1.3  uwe static int	j6x0tp_wsmouse_submatch(struct device *, struct cfdata *,
    137  1.3  uwe 					void *);
    138  1.3  uwe static int	j6x0tp_wskbd_submatch(struct device *, struct cfdata *,
    139  1.3  uwe 				      void *);
    140  1.3  uwe 
    141  1.3  uwe /* wsmouse accessops */
    142  1.3  uwe static int	j6x0tp_wsmouse_enable(void *);
    143  1.3  uwe static int	j6x0tp_wsmouse_ioctl(void *, u_long, caddr_t, int,
    144  1.3  uwe 				     struct proc *);
    145  1.3  uwe static void	j6x0tp_wsmouse_disable(void *);
    146  1.3  uwe 
    147  1.3  uwe /* wskbd accessops */
    148  1.3  uwe static int	j6x0tp_wskbd_enable(void *, int);
    149  1.3  uwe static void	j6x0tp_wskbd_set_leds(void *, int);
    150  1.3  uwe static int	j6x0tp_wskbd_ioctl(void *, u_long, caddr_t, int,
    151  1.3  uwe 				   struct proc *);
    152  1.3  uwe 
    153  1.3  uwe /* internal driver routines */
    154  1.3  uwe static void	j6x0tp_enable(struct j6x0tp_softc *);
    155  1.3  uwe static void	j6x0tp_disable(struct j6x0tp_softc *);
    156  1.3  uwe static int	j6x0tp_set_enable(struct j6x0tp_softc *, int, int);
    157  1.3  uwe static int	j6x0tp_intr(void *);
    158  1.3  uwe static void	j6x0tp_start_polling(void *);
    159  1.3  uwe static void	j6x0tp_stop_polling(struct j6x0tp_softc *);
    160  1.3  uwe static void	j6x0tp_callout_wsmouse(void *);
    161  1.3  uwe static void	j6x0tp_callout_wskbd(void *);
    162  1.3  uwe static void	j6x0tp_wsmouse_input(struct j6x0tp_softc *, int, int);
    163  1.3  uwe static void	j6x0tp_get_raw_xy(int *, int *);
    164  1.3  uwe static int	j6x0tp_get_hard_icon(int, int);
    165  1.1  uwe 
    166  1.1  uwe 
    167  1.1  uwe const struct wsmouse_accessops j6x0tp_accessops = {
    168  1.3  uwe 	j6x0tp_wsmouse_enable,
    169  1.3  uwe 	j6x0tp_wsmouse_ioctl,
    170  1.3  uwe 	j6x0tp_wsmouse_disable
    171  1.1  uwe };
    172  1.1  uwe 
    173  1.1  uwe static const struct wsmouse_calibcoords j6x0tp_default_calib = {
    174  1.1  uwe 	0, 0, 639, 239,
    175  1.1  uwe 	4,
    176  1.3  uwe 	{{ J6X0TP_FB_LEFT,  J6X0TP_FB_TOP,      0,   0 },
    177  1.3  uwe 	 { J6X0TP_FB_RIGHT, J6X0TP_FB_TOP,    639,   0 },
    178  1.3  uwe 	 { J6X0TP_FB_LEFT,  J6X0TP_FB_BOTTOM,   0, 239 },
    179  1.3  uwe 	 { J6X0TP_FB_RIGHT, J6X0TP_FB_BOTTOM, 639, 239 }}
    180  1.3  uwe };
    181  1.3  uwe 
    182  1.3  uwe const struct wskbd_accessops j6x0tp_wskbd_accessops = {
    183  1.3  uwe 	j6x0tp_wskbd_enable,
    184  1.3  uwe 	j6x0tp_wskbd_set_leds,
    185  1.3  uwe 	j6x0tp_wskbd_ioctl
    186  1.3  uwe };
    187  1.3  uwe 
    188  1.3  uwe 
    189  1.3  uwe #ifndef J6X0TP_SETTINGS_ICON_KEYSYM
    190  1.3  uwe #define J6X0TP_SETTINGS_ICON_KEYSYM	KS_Home
    191  1.3  uwe #endif
    192  1.3  uwe #ifndef J6X0TP_PGUP_ICON_KEYSYM
    193  1.3  uwe #define J6X0TP_PGUP_ICON_KEYSYM		KS_Prior
    194  1.3  uwe #endif
    195  1.3  uwe #ifndef J6X0TP_PGDN_ICON_KEYSYM
    196  1.3  uwe #define J6X0TP_PGDN_ICON_KEYSYM		KS_Next
    197  1.3  uwe #endif
    198  1.3  uwe #ifndef J6X0TP_SWITCH_ICON_KEYSYM
    199  1.3  uwe #define J6X0TP_SWITCH_ICON_KEYSYM	KS_End
    200  1.3  uwe #endif
    201  1.3  uwe 
    202  1.3  uwe static const keysym_t j6x0tp_wskbd_keydesc[] = {
    203  1.3  uwe 	KS_KEYCODE(1), J6X0TP_SETTINGS_ICON_KEYSYM,
    204  1.3  uwe 	KS_KEYCODE(2), J6X0TP_PGUP_ICON_KEYSYM,
    205  1.3  uwe 	KS_KEYCODE(3), J6X0TP_PGDN_ICON_KEYSYM,
    206  1.3  uwe 	KS_KEYCODE(4), J6X0TP_SWITCH_ICON_KEYSYM
    207  1.3  uwe };
    208  1.3  uwe 
    209  1.3  uwe const struct wscons_keydesc j6x0tp_wskbd_keydesctab[] = {
    210  1.3  uwe 	{ KB_US, 0,
    211  1.3  uwe 	  sizeof(j6x0tp_wskbd_keydesc)/sizeof(keysym_t),
    212  1.3  uwe 	  j6x0tp_wskbd_keydesc
    213  1.3  uwe 	},
    214  1.3  uwe 	{0, 0, 0, 0}
    215  1.3  uwe };
    216  1.3  uwe 
    217  1.3  uwe const struct wskbd_mapdata j6x0tp_wskbd_keymapdata = {
    218  1.3  uwe         j6x0tp_wskbd_keydesctab, KB_US
    219  1.1  uwe };
    220  1.1  uwe 
    221  1.1  uwe 
    222  1.3  uwe CFATTACH_DECL(j6x0tp, sizeof(struct j6x0tp_softc),
    223  1.3  uwe     j6x0tp_match, j6x0tp_attach, NULL, NULL);
    224  1.1  uwe 
    225  1.1  uwe 
    226  1.1  uwe static int
    227  1.3  uwe j6x0tp_match(struct device *parent, struct cfdata *cf, void *aux)
    228  1.1  uwe {
    229  1.1  uwe 
    230  1.1  uwe 	/*
    231  1.1  uwe 	 * XXX: does platid_mask_MACH_HP_LX matches _JORNADA_6XX too?
    232  1.1  uwe 	 * Is 620 wired similarly?
    233  1.1  uwe 	 */
    234  1.1  uwe 	if (!platid_match(&platid, &platid_mask_MACH_HP_JORNADA_6XX))
    235  1.1  uwe 		return (0);
    236  1.1  uwe 
    237  1.3  uwe 	if (strcmp(cf->cf_name, "j6x0tp") != 0)
    238  1.1  uwe 		return (0);
    239  1.1  uwe 
    240  1.1  uwe 	return (1);
    241  1.1  uwe }
    242  1.1  uwe 
    243  1.1  uwe 
    244  1.3  uwe /*
    245  1.3  uwe  * Attach the touch panel driver and its ws* children.
    246  1.3  uwe  *
    247  1.3  uwe  * Note that we have to use submatch to distinguish between children
    248  1.3  uwe  * because ws{kbd,mouse}_match match unconditionally.
    249  1.3  uwe  */
    250  1.1  uwe static void
    251  1.1  uwe j6x0tp_attach(struct device *parent, struct device *self, void *aux)
    252  1.1  uwe {
    253  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    254  1.1  uwe 	struct wsmousedev_attach_args wsma;
    255  1.3  uwe 	struct wskbddev_attach_args wska;
    256  1.1  uwe 
    257  1.1  uwe 	printf("\n");
    258  1.1  uwe 
    259  1.1  uwe 	sc->sc_enabled = 0;
    260  1.3  uwe 	sc->sc_hard_icon = 0;
    261  1.1  uwe 
    262  1.3  uwe 	/* touch-panel as a pointing device */
    263  1.1  uwe 	wsma.accessops = &j6x0tp_accessops;
    264  1.1  uwe 	wsma.accesscookie = sc;
    265  1.1  uwe 
    266  1.3  uwe 	sc->sc_wsmousedev = config_found_sm(self, &wsma, wsmousedevprint,
    267  1.3  uwe 					    j6x0tp_wsmouse_submatch);
    268  1.1  uwe 	if (sc->sc_wsmousedev == NULL)
    269  1.1  uwe 		return;
    270  1.1  uwe 
    271  1.3  uwe 	/* on-screen "hard icons" as a keyboard device */
    272  1.3  uwe 	wska.console = 0;
    273  1.3  uwe 	wska.keymap = &j6x0tp_wskbd_keymapdata;
    274  1.3  uwe 	wska.accessops = &j6x0tp_wskbd_accessops;
    275  1.3  uwe 	wska.accesscookie = sc;
    276  1.3  uwe 
    277  1.3  uwe 	sc->sc_wskbddev = config_found_sm(self, &wska, wskbddevprint,
    278  1.3  uwe 					  j6x0tp_wskbd_submatch);
    279  1.3  uwe 
    280  1.4  uwe 	/* init calibration, set default parameters */
    281  1.1  uwe 	tpcalib_init(&sc->sc_tpcalib);
    282  1.1  uwe 	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
    283  1.1  uwe 		      (caddr_t)&j6x0tp_default_calib, 0, 0);
    284  1.1  uwe 
    285  1.3  uwe 	/* used when in polling mode */
    286  1.1  uwe 	callout_init(&sc->sc_touch_ch);
    287  1.1  uwe 
    288  1.3  uwe 	/* establish interrupt handler, but disable until opened */
    289  1.1  uwe 	intc_intr_establish(SH7709_INTEVT2_IRQ3, IST_EDGE, IPL_TTY,
    290  1.1  uwe 			    j6x0tp_intr, sc);
    291  1.3  uwe 	intc_intr_disable(SH7709_INTEVT2_IRQ3);
    292  1.1  uwe }
    293  1.1  uwe 
    294  1.1  uwe 
    295  1.1  uwe static int
    296  1.3  uwe j6x0tp_wsmouse_submatch(struct device *parent, struct cfdata *cf, void *aux)
    297  1.1  uwe {
    298  1.1  uwe 
    299  1.3  uwe 	return (!strcmp(cf->cf_name, "wsmouse"));
    300  1.3  uwe }
    301  1.3  uwe 
    302  1.3  uwe 
    303  1.3  uwe static int
    304  1.3  uwe j6x0tp_wskbd_submatch(struct device *parent, struct cfdata *cf, void *aux)
    305  1.3  uwe {
    306  1.3  uwe 
    307  1.3  uwe 	return (!strcmp(cf->cf_name, "wskbd"));
    308  1.3  uwe }
    309  1.3  uwe 
    310  1.3  uwe 
    311  1.3  uwe /*
    312  1.3  uwe  * Enable touch panel:  we start in interrupt mode.
    313  1.3  uwe  * Must be called as spltty().
    314  1.3  uwe  */
    315  1.3  uwe static void
    316  1.3  uwe j6x0tp_enable(struct j6x0tp_softc *sc)
    317  1.3  uwe {
    318  1.3  uwe 
    319  1.3  uwe 	DPRINTFN(2, ("%s: enable\n", sc->sc_dev.dv_xname));
    320  1.3  uwe 	intc_intr_enable(SH7709_INTEVT2_IRQ3);
    321  1.3  uwe }
    322  1.3  uwe 
    323  1.3  uwe 
    324  1.3  uwe /*
    325  1.3  uwe  * Disable touch panel: disable interrupt, cancel pending callout.
    326  1.3  uwe  * Must be called as spltty().
    327  1.3  uwe  */
    328  1.3  uwe static void
    329  1.3  uwe j6x0tp_disable(struct j6x0tp_softc *sc)
    330  1.3  uwe {
    331  1.3  uwe 
    332  1.3  uwe 	DPRINTFN(2, ("%s: disable\n", sc->sc_dev.dv_xname));
    333  1.3  uwe 	intc_intr_disable(SH7709_INTEVT2_IRQ3);
    334  1.3  uwe 	callout_stop(&sc->sc_touch_ch);
    335  1.3  uwe }
    336  1.3  uwe 
    337  1.1  uwe 
    338  1.3  uwe static int
    339  1.3  uwe j6x0tp_set_enable(struct j6x0tp_softc *sc, int on, int child)
    340  1.3  uwe {
    341  1.3  uwe 	int s = spltty();
    342  1.1  uwe 
    343  1.3  uwe 	if (on) {
    344  1.3  uwe 		if (!sc->sc_enabled)
    345  1.3  uwe 			j6x0tp_enable(sc);
    346  1.3  uwe 		sc->sc_enabled |= child;
    347  1.3  uwe 	} else {
    348  1.3  uwe 		sc->sc_enabled &= ~child;
    349  1.3  uwe 		if (!sc->sc_enabled)
    350  1.3  uwe 			j6x0tp_disable(sc);
    351  1.3  uwe 	}
    352  1.1  uwe 
    353  1.1  uwe 	splx(s);
    354  1.1  uwe 	return (0);
    355  1.1  uwe }
    356  1.1  uwe 
    357  1.1  uwe 
    358  1.3  uwe static int
    359  1.3  uwe j6x0tp_wsmouse_enable(void *self)
    360  1.3  uwe {
    361  1.3  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    362  1.3  uwe 
    363  1.3  uwe 	DPRINTFN(1, ("%s: wsmouse enable\n", sc->sc_dev.dv_xname));
    364  1.3  uwe 	return (j6x0tp_set_enable(sc, 1, J6X0TP_WSMOUSE_ENABLED));
    365  1.3  uwe }
    366  1.3  uwe 
    367  1.3  uwe 
    368  1.1  uwe static void
    369  1.3  uwe j6x0tp_wsmouse_disable(void *self)
    370  1.1  uwe {
    371  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    372  1.1  uwe 
    373  1.3  uwe 	DPRINTFN(1, ("%s: wsmouse disable\n", sc->sc_dev.dv_xname));
    374  1.3  uwe 	j6x0tp_set_enable(sc, 0, J6X0TP_WSMOUSE_ENABLED);
    375  1.3  uwe }
    376  1.1  uwe 
    377  1.1  uwe 
    378  1.3  uwe static int
    379  1.3  uwe j6x0tp_wskbd_enable(void *self, int on)
    380  1.3  uwe {
    381  1.3  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    382  1.1  uwe 
    383  1.3  uwe 	DPRINTFN(1, ("%s: wskbd %sable\n", sc->sc_dev.dv_xname,
    384  1.3  uwe 		     on ? "en" : "dis"));
    385  1.3  uwe 	return (j6x0tp_set_enable(sc, on, J6X0TP_WSKBD_ENABLED));
    386  1.1  uwe }
    387  1.1  uwe 
    388  1.1  uwe 
    389  1.1  uwe static int
    390  1.1  uwe j6x0tp_intr(void *self)
    391  1.1  uwe {
    392  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    393  1.1  uwe 
    394  1.1  uwe 	uint8_t irr0;
    395  1.1  uwe 	uint8_t phdr, touched;
    396  1.1  uwe 	unsigned int steady, tremor_timeout;
    397  1.1  uwe 
    398  1.1  uwe 	irr0 = _reg_read_1(SH7709_IRR0);
    399  1.1  uwe 	if ((irr0 & IRR0_IRQ3) == 0) {
    400  1.1  uwe #ifdef DIAGNOSTIC
    401  1.1  uwe 		printf("%s: irr0 %02x?\n", sc->sc_dev.dv_xname, irr0);
    402  1.1  uwe #endif
    403  1.1  uwe 		return (0);
    404  1.1  uwe 	}
    405  1.1  uwe 
    406  1.3  uwe 	if (!sc->sc_enabled) {
    407  1.3  uwe 		DPRINTFN(1, ("%s: intr: !sc_enabled\n", sc->sc_dev.dv_xname));
    408  1.3  uwe 		intc_intr_disable(SH7709_INTEVT2_IRQ3);
    409  1.3  uwe 		goto served;
    410  1.3  uwe 	}
    411  1.3  uwe 
    412  1.3  uwe 
    413  1.1  uwe 	/*
    414  1.1  uwe 	 * Number of times the "touched" bit should be read
    415  1.1  uwe 	 * consecutively.
    416  1.1  uwe 	 */
    417  1.1  uwe #	define TREMOR_THRESHOLD 0x300
    418  1.1  uwe 
    419  1.1  uwe 	steady = 0;
    420  1.1  uwe 	tremor_timeout = TREMOR_THRESHOLD * 16;	/* XXX: arbitrary */
    421  1.1  uwe 	touched = PHDR_TP_PEN_DOWN;	/* we start with "touched" state */
    422  1.1  uwe 
    423  1.1  uwe 	do {
    424  1.1  uwe 		phdr = _reg_read_1(SH7709_PHDR);
    425  1.1  uwe 
    426  1.1  uwe 		if ((phdr & PHDR_TP_PEN_DOWN) == touched)
    427  1.1  uwe 			++steady;
    428  1.1  uwe 		else {
    429  1.1  uwe 			steady = 0;
    430  1.1  uwe 			touched = phdr & PHDR_TP_PEN_DOWN;
    431  1.1  uwe 		}
    432  1.1  uwe 
    433  1.1  uwe 		if (--tremor_timeout == 0) {
    434  1.1  uwe 			DPRINTF(("%s: tremor timeout!\n",
    435  1.1  uwe 				 sc->sc_dev.dv_xname));
    436  1.1  uwe 			goto served;
    437  1.1  uwe 		}
    438  1.1  uwe 	} while (steady < TREMOR_THRESHOLD);
    439  1.1  uwe 
    440  1.3  uwe 	if (touched) {
    441  1.3  uwe 		intc_intr_disable(SH7709_INTEVT2_IRQ3);
    442  1.3  uwe 
    443  1.3  uwe 		/*
    444  1.3  uwe 		 * ADC readings are not stable yet, so schedule
    445  1.3  uwe 		 * callout instead of accessing ADC from the interrupt
    446  1.3  uwe 		 * handler only to immediately delay().
    447  1.3  uwe 		 */
    448  1.3  uwe 		callout_reset(&sc->sc_touch_ch, hz/32,
    449  1.3  uwe 			      j6x0tp_start_polling, sc);
    450  1.3  uwe 	} else
    451  1.1  uwe 		DPRINTFN(1, ("%s: tremor\n", sc->sc_dev.dv_xname));
    452  1.3  uwe   served:
    453  1.3  uwe 	/* clear the interrupt (XXX: protect access?) */
    454  1.3  uwe 	_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);
    455  1.3  uwe 
    456  1.3  uwe 	return (1);
    457  1.3  uwe }
    458  1.3  uwe 
    459  1.3  uwe 
    460  1.3  uwe /*
    461  1.3  uwe  * Called from the interrupt handler at spltty() upon first touch.
    462  1.3  uwe  * Decide if we are going to report this touch as a mouse click/drag
    463  1.3  uwe  * or as a key press.
    464  1.3  uwe  */
    465  1.3  uwe static void
    466  1.3  uwe j6x0tp_start_polling(void *self)
    467  1.3  uwe {
    468  1.3  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    469  1.3  uwe 	uint8_t phdr;
    470  1.3  uwe 	int do_mouse, do_kbd;
    471  1.3  uwe 	int rawx, rawy;
    472  1.3  uwe 	int icon;
    473  1.3  uwe 
    474  1.3  uwe 	phdr = _reg_read_1(SH7709_PHDR);
    475  1.3  uwe 	if ((phdr & PHDR_TP_PEN_DOWN) == 0) {
    476  1.3  uwe 		DPRINTFN(2, ("%s: start: pen is not down\n",
    477  1.3  uwe 			     sc->sc_dev.dv_xname));
    478  1.3  uwe 		j6x0tp_stop_polling(sc);
    479  1.1  uwe 	}
    480  1.1  uwe 
    481  1.3  uwe 	j6x0tp_get_raw_xy(&rawx, &rawy);
    482  1.3  uwe 	DPRINTFN(2, ("%s: start: %4d %4d -> ",
    483  1.3  uwe 		     sc->sc_dev.dv_xname, rawx, rawy));
    484  1.3  uwe 
    485  1.3  uwe 	do_mouse = sc->sc_enabled & J6X0TP_WSMOUSE_ENABLED;
    486  1.3  uwe #ifdef J6X0TP_WSMOUSE_EXCLUSIVE
    487  1.3  uwe 	if (do_mouse)
    488  1.3  uwe 		do_kbd = 0;
    489  1.3  uwe 	else
    490  1.3  uwe #endif
    491  1.3  uwe 		do_kbd = sc->sc_enabled & J6X0TP_WSKBD_ENABLED;
    492  1.1  uwe 
    493  1.3  uwe 	icon = 0;
    494  1.3  uwe 	if (do_kbd)
    495  1.3  uwe 		icon = j6x0tp_get_hard_icon(rawx, rawy);
    496  1.3  uwe 
    497  1.3  uwe 	if (icon != 0) {
    498  1.3  uwe 		DPRINTFN(2, ("icon %d\n", icon));
    499  1.3  uwe 		sc->sc_hard_icon = icon;
    500  1.3  uwe 		wskbd_input(sc->sc_wskbddev, WSCONS_EVENT_KEY_DOWN, icon);
    501  1.3  uwe 		callout_reset(&sc->sc_touch_ch, hz/32,
    502  1.3  uwe 			      j6x0tp_callout_wskbd, sc);
    503  1.3  uwe 	} else if (do_mouse) {
    504  1.3  uwe 		DPRINTFN(2, ("mouse\n"));
    505  1.3  uwe 		j6x0tp_wsmouse_input(sc, rawx, rawy);
    506  1.1  uwe 		callout_reset(&sc->sc_touch_ch, hz/32,
    507  1.3  uwe 			      j6x0tp_callout_wsmouse, sc);
    508  1.3  uwe 	} else {
    509  1.3  uwe 		DPRINTFN(2, ("ignore\n"));
    510  1.3  uwe 		j6x0tp_stop_polling(sc);
    511  1.3  uwe 	}
    512  1.3  uwe }
    513  1.3  uwe 
    514  1.3  uwe 
    515  1.3  uwe /*
    516  1.3  uwe  * Re-enable touch panel interrupt.
    517  1.3  uwe  * Called as spltty() when polling code detects pen-up.
    518  1.3  uwe  */
    519  1.3  uwe static void
    520  1.3  uwe j6x0tp_stop_polling(struct j6x0tp_softc *sc)
    521  1.3  uwe {
    522  1.3  uwe 	uint8_t irr0;
    523  1.3  uwe 
    524  1.3  uwe 	DPRINTFN(2, ("%s: stop\n", sc->sc_dev.dv_xname));
    525  1.1  uwe 
    526  1.3  uwe 	/* clear pending interrupt signal before re-enabling the interrupt */
    527  1.3  uwe 	irr0 = _reg_read_1(SH7709_IRR0);
    528  1.3  uwe 	if ((irr0 & IRR0_IRQ3) != 0)
    529  1.3  uwe 		_reg_write_1(SH7709_IRR0, irr0 & ~IRR0_IRQ3);
    530  1.1  uwe 
    531  1.3  uwe 	intc_intr_enable(SH7709_INTEVT2_IRQ3);
    532  1.1  uwe }
    533  1.1  uwe 
    534  1.1  uwe 
    535  1.3  uwe /*
    536  1.3  uwe  * We are reporting this touch as a keyboard event.
    537  1.3  uwe  * Poll touch screen waiting for pen-up.
    538  1.3  uwe  */
    539  1.1  uwe static void
    540  1.3  uwe j6x0tp_callout_wskbd(void *self)
    541  1.1  uwe {
    542  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    543  1.3  uwe 	uint8_t phdr;
    544  1.1  uwe 	int s;
    545  1.1  uwe 
    546  1.1  uwe 	s = spltty();
    547  1.1  uwe 
    548  1.1  uwe 	if (!sc->sc_enabled) {
    549  1.3  uwe 		DPRINTFN(1, ("%s: wskbd callout: !sc_enabled\n",
    550  1.1  uwe 			     sc->sc_dev.dv_xname));
    551  1.1  uwe 		splx(s);
    552  1.1  uwe 		return;
    553  1.1  uwe 	}
    554  1.1  uwe 
    555  1.1  uwe 	phdr = _reg_read_1(SH7709_PHDR);
    556  1.3  uwe 	if ((phdr & PHDR_TP_PEN_DOWN) != 0) {
    557  1.3  uwe 		/*
    558  1.3  uwe 		 * Pen is still down, continue polling.  Wskbd's
    559  1.3  uwe 		 * auto-repeat takes care of repeating the key.
    560  1.3  uwe 		 */
    561  1.3  uwe 		callout_schedule(&sc->sc_touch_ch, hz/32);
    562  1.3  uwe 	} else {
    563  1.3  uwe 		wskbd_input(sc->sc_wskbddev,
    564  1.3  uwe 			    WSCONS_EVENT_KEY_UP, sc->sc_hard_icon);
    565  1.3  uwe 		j6x0tp_stop_polling(sc);
    566  1.3  uwe 	}
    567  1.3  uwe 	splx(s);
    568  1.3  uwe }
    569  1.3  uwe 
    570  1.1  uwe 
    571  1.3  uwe /*
    572  1.3  uwe  * We are reporting this touch as a mouse click/drag.
    573  1.3  uwe  */
    574  1.3  uwe static void
    575  1.3  uwe j6x0tp_callout_wsmouse(void *self)
    576  1.3  uwe {
    577  1.3  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    578  1.3  uwe 	uint8_t phdr;
    579  1.3  uwe 	int rawx, rawy;
    580  1.3  uwe 	int s;
    581  1.1  uwe 
    582  1.3  uwe 	s = spltty();
    583  1.1  uwe 
    584  1.3  uwe 	if (!sc->sc_enabled) {
    585  1.3  uwe 		DPRINTFN(1, ("%s: wsmouse callout: !sc_enabled\n",
    586  1.3  uwe 			     sc->sc_dev.dv_xname));
    587  1.1  uwe 		splx(s);
    588  1.1  uwe 		return;
    589  1.1  uwe 	}
    590  1.1  uwe 
    591  1.3  uwe 	phdr = _reg_read_1(SH7709_PHDR);
    592  1.3  uwe 	if ((phdr & PHDR_TP_PEN_DOWN) != 0) {
    593  1.3  uwe 		j6x0tp_get_raw_xy(&rawx, &rawy);
    594  1.3  uwe 		j6x0tp_wsmouse_input(sc, rawx, rawy); /* mouse dragged */
    595  1.3  uwe 		callout_schedule(&sc->sc_touch_ch, hz/32);
    596  1.3  uwe 	} else {
    597  1.3  uwe 		wsmouse_input(sc->sc_wsmousedev, 0, 0, 0, 0, /* button up */
    598  1.3  uwe 			      WSMOUSE_INPUT_DELTA);
    599  1.3  uwe 		j6x0tp_stop_polling(sc);
    600  1.3  uwe 	}
    601  1.3  uwe 	splx(s);
    602  1.3  uwe }
    603  1.3  uwe 
    604  1.3  uwe 
    605  1.3  uwe /*
    606  1.3  uwe  * Report mouse click/drag.
    607  1.3  uwe  */
    608  1.3  uwe static void
    609  1.3  uwe j6x0tp_wsmouse_input(struct j6x0tp_softc *sc, int rawx, int rawy)
    610  1.3  uwe {
    611  1.3  uwe 	int x, y;
    612  1.3  uwe 
    613  1.3  uwe 	tpcalib_trans(&sc->sc_tpcalib, rawx, rawy, &x, &y);
    614  1.3  uwe 
    615  1.3  uwe 	DPRINTFN(3, ("%s: %4d %4d -> %3d %3d\n",
    616  1.3  uwe 		     sc->sc_dev.dv_xname, rawx, rawy, x, y));
    617  1.3  uwe 
    618  1.3  uwe 	wsmouse_input(sc->sc_wsmousedev,
    619  1.3  uwe 		      1,	/* button */
    620  1.3  uwe 		      x, y,
    621  1.3  uwe 		      0,	/* flags */
    622  1.3  uwe 		      WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
    623  1.3  uwe }
    624  1.3  uwe 
    625  1.3  uwe 
    626  1.3  uwe /*
    627  1.3  uwe  * Read raw X/Y coordinates from the ADC.
    628  1.3  uwe  * XXX: protect accesses to SCPDR?
    629  1.3  uwe  */
    630  1.3  uwe static void
    631  1.3  uwe j6x0tp_get_raw_xy(int *rawxp, int *rawyp)
    632  1.3  uwe {
    633  1.3  uwe 	uint8_t scpdr;
    634  1.1  uwe 
    635  1.1  uwe 	/* Y axis */
    636  1.1  uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    637  1.1  uwe 	scpdr |=  SCPDR_TP_SCAN_ENABLE;
    638  1.1  uwe 	scpdr &= ~SCPDR_TP_SCAN_Y; /* pull low to scan */
    639  1.1  uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    640  1.1  uwe 	delay(10);
    641  1.3  uwe 
    642  1.3  uwe 	*rawyp = adc_sample_channel(ADC_CHANNEL_TP_Y);
    643  1.1  uwe 
    644  1.1  uwe 	/* X axis */
    645  1.1  uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    646  1.1  uwe 	scpdr |=  SCPDR_TP_SCAN_Y;
    647  1.1  uwe 	scpdr &= ~SCPDR_TP_SCAN_X; /* pull low to scan */
    648  1.1  uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    649  1.1  uwe 	delay(10);
    650  1.3  uwe 
    651  1.3  uwe 	*rawxp = adc_sample_channel(ADC_CHANNEL_TP_X);
    652  1.1  uwe 
    653  1.1  uwe 	/* restore SCPDR */
    654  1.1  uwe 	scpdr = _reg_read_1(SH7709_SCPDR);
    655  1.1  uwe 	scpdr |=  SCPDR_TP_SCAN_X;
    656  1.1  uwe 	scpdr &= ~SCPDR_TP_SCAN_ENABLE;
    657  1.1  uwe 	_reg_write_1(SH7709_SCPDR, scpdr);
    658  1.3  uwe }
    659  1.1  uwe 
    660  1.1  uwe 
    661  1.3  uwe /*
    662  1.3  uwe  * Check if the (rawx, rawy) is inside one of the 4 hard icons.
    663  1.3  uwe  * Return the icon number 1..4, or 0 if not inside an icon.
    664  1.3  uwe  */
    665  1.3  uwe static int
    666  1.3  uwe j6x0tp_get_hard_icon(int rawx, int rawy)
    667  1.3  uwe {
    668  1.3  uwe 	if (rawx <= J6X0TP_FB_RIGHT)
    669  1.3  uwe 		return (0);
    670  1.1  uwe 
    671  1.3  uwe 	if (rawy < J6X0TP_HARD_ICON_MAX_Y(1))
    672  1.3  uwe 		return (1);
    673  1.3  uwe 	else if (rawy < J6X0TP_HARD_ICON_MAX_Y(2))
    674  1.3  uwe 		return (2);
    675  1.3  uwe 	else if (rawy < J6X0TP_HARD_ICON_MAX_Y(3))
    676  1.3  uwe 		return (3);
    677  1.3  uwe 	else
    678  1.3  uwe 		return (4);
    679  1.1  uwe }
    680  1.1  uwe 
    681  1.1  uwe 
    682  1.1  uwe static int
    683  1.3  uwe j6x0tp_wsmouse_ioctl(void *self, u_long cmd, caddr_t data, int flag,
    684  1.1  uwe 		     struct proc *p)
    685  1.1  uwe {
    686  1.1  uwe 	struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self;
    687  1.1  uwe 
    688  1.1  uwe 	switch (cmd) {
    689  1.1  uwe 	case WSMOUSEIO_GTYPE:
    690  1.1  uwe 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
    691  1.1  uwe 		return (0);
    692  1.1  uwe 
    693  1.1  uwe 	case WSMOUSEIO_SCALIBCOORDS:
    694  1.1  uwe 	case WSMOUSEIO_GCALIBCOORDS:
    695  1.1  uwe 	case WSMOUSEIO_GETID:
    696  1.1  uwe 		return (tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, p));
    697  1.1  uwe 
    698  1.1  uwe 	default:
    699  1.1  uwe 		return (EPASSTHROUGH);
    700  1.1  uwe 	}
    701  1.3  uwe }
    702  1.3  uwe 
    703  1.3  uwe 
    704  1.3  uwe static int
    705  1.3  uwe j6x0tp_wskbd_ioctl(void *self, u_long cmd, caddr_t data, int flag,
    706  1.3  uwe 		     struct proc *p)
    707  1.3  uwe {
    708  1.3  uwe 	/* struct j6x0tp_softc *sc = (struct j6x0tp_softc *)self; */
    709  1.3  uwe 
    710  1.3  uwe 	switch (cmd) {
    711  1.3  uwe 	case WSKBDIO_GTYPE:
    712  1.3  uwe 		*(int *)data = WSKBD_TYPE_HPC_BTN; /* may be use new type? */
    713  1.3  uwe 		return (0);
    714  1.3  uwe 
    715  1.3  uwe 	case WSKBDIO_GETLEDS:
    716  1.3  uwe 		*(int *)data = 0;
    717  1.3  uwe 		return (0);
    718  1.3  uwe 
    719  1.3  uwe 	default:
    720  1.3  uwe 		return (EPASSTHROUGH);
    721  1.3  uwe 	}
    722  1.3  uwe }
    723  1.3  uwe 
    724  1.3  uwe 
    725  1.3  uwe static void
    726  1.3  uwe j6x0tp_wskbd_set_leds(void *self, int leds)
    727  1.3  uwe {
    728  1.3  uwe 
    729  1.3  uwe 	/* nothing to do*/
    730  1.3  uwe 	return;
    731  1.1  uwe }
    732