vrdsiu_mouse.c revision 1.9.58.1       1       1.1     greg /*
      2       1.6   keihan  * Copyright (c) 2001, 2002 Greg Hughes (greg (at) NetBSD.org). All rights reserved.
      3       1.1     greg  * Copyright (c) 1999 PocketBSD Project. All rights reserved.
      4       1.1     greg  *
      5       1.1     greg  * Redistribution and use in source and binary forms, with or without
      6       1.1     greg  * modification, are permitted provided that the following conditions
      7       1.1     greg  * are met:
      8       1.1     greg  * 1. Redistributions of source code must retain the above copyright
      9       1.1     greg  *    notice, this list of conditions and the following disclaimer.
     10       1.1     greg  * 2. Redistributions in binary form must reproduce the above copyright
     11       1.1     greg  *    notice, this list of conditions and the following disclaimer in the
     12       1.1     greg  *    documentation and/or other materials provided with the distribution.
     13       1.1     greg  *
     14       1.1     greg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15       1.1     greg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16       1.1     greg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17       1.1     greg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18       1.1     greg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19       1.1     greg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20       1.1     greg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21       1.1     greg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22       1.1     greg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23       1.1     greg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24       1.1     greg  * SUCH DAMAGE.
     25       1.1     greg  */
     26       1.1     greg 
     27       1.1     greg /*
     28       1.1     greg  * Wscons mouse driver for DSIU TrackPoint on IBM WorkPad z50 by
     29       1.6   keihan  * Greg Hughes (greg (at) NetBSD.org).
     30       1.1     greg  *
     31       1.1     greg  * Template for interrupt/device registration taken from vrdsu.c.
     32       1.1     greg  */
     33       1.1     greg 
     34       1.4    lukem #include <sys/cdefs.h>
     35  1.9.58.1      jym __KERNEL_RCSID(0, "$NetBSD: vrdsiu_mouse.c,v 1.9.58.1 2009/05/13 17:17:47 jym Exp $");
     36       1.4    lukem 
     37       1.1     greg #include <sys/param.h>
     38       1.1     greg #include <sys/systm.h>
     39       1.1     greg #include <sys/device.h>
     40       1.1     greg 
     41       1.1     greg #include <dev/wscons/wsconsio.h>
     42       1.1     greg #include <dev/wscons/wsmousevar.h>
     43       1.1     greg 
     44       1.1     greg #include <machine/bus.h>
     45       1.1     greg #include <machine/platid.h>
     46       1.1     greg #include <machine/platid_mask.h>
     47       1.1     greg 
     48       1.1     greg #include <hpcmips/vr/vripvar.h>
     49       1.1     greg #include <hpcmips/vr/vrdsiureg.h>
     50       1.1     greg #include <hpcmips/vr/cmureg.h>
     51       1.1     greg 
     52       1.1     greg enum vrdsiu_mouse_stat {
     53       1.1     greg 	VRDSIU_MOUSE_STAT_DISABLE,
     54       1.1     greg 	VRDSIU_MOUSE_STAT_ENABLE
     55       1.1     greg };
     56       1.1     greg 
     57       1.1     greg enum vrdsiu_ps2_input_state {
     58       1.1     greg 	VRDSIU_PS2_INPUT_STATE_BYTE0,
     59       1.1     greg 	VRDSIU_PS2_INPUT_STATE_BYTE1,
     60       1.1     greg 	VRDSIU_PS2_INPUT_STATE_BYTE2
     61       1.1     greg };
     62       1.1     greg 
     63       1.1     greg struct vrdsiu_softc {
     64       1.1     greg 	struct device sc_dev;
     65       1.1     greg 	bus_space_tag_t sc_iot;
     66       1.1     greg 	bus_space_handle_t sc_ioh;
     67       1.1     greg 	int sc_unit;
     68       1.1     greg 	void *sc_handler;
     69       1.1     greg 	vrip_chipset_tag_t sc_vrip;
     70       1.1     greg 
     71       1.1     greg 	enum vrdsiu_mouse_stat sc_mouse_stat;
     72       1.1     greg 
     73       1.1     greg 	struct device *sc_wsmousedev;
     74       1.1     greg };
     75       1.1     greg 
     76       1.1     greg static int asimOld = 0;
     77       1.1     greg 
     78  1.9.58.1      jym static int vrdsiu_match(struct device *, struct cfdata *, void *);
     79  1.9.58.1      jym static void vrdsiu_attach(struct device *, struct device *, void *);
     80       1.1     greg 
     81  1.9.58.1      jym static void vrdsiu_write(struct vrdsiu_softc *, int, unsigned short);
     82  1.9.58.1      jym static unsigned short vrdsiu_read(struct vrdsiu_softc *, int);
     83       1.1     greg 
     84       1.1     greg /* Interrupt handlers */
     85  1.9.58.1      jym static int vrdsiu_intr(void *);
     86  1.9.58.1      jym static void vrdsiu_mouse_intr(struct vrdsiu_softc *);
     87       1.1     greg 
     88       1.1     greg /* Enable/disable DSIU handling */
     89  1.9.58.1      jym static int vrdsiu_mouse_enable(void *);
     90  1.9.58.1      jym static int vrdsiu_mouse_ioctl(void *, u_long, void *, int, struct lwp *);
     91  1.9.58.1      jym static void vrdsiu_mouse_disable(void *);
     92       1.1     greg 
     93       1.1     greg /* wsmouse access ops */
     94       1.1     greg const struct wsmouse_accessops vrdsiu_accessops = {
     95       1.1     greg 	vrdsiu_mouse_enable,
     96       1.1     greg 	vrdsiu_mouse_ioctl,
     97       1.1     greg 	vrdsiu_mouse_disable
     98       1.1     greg };
     99       1.1     greg 
    100       1.3  thorpej CFATTACH_DECL(vrdsiu_mouse, sizeof(struct vrdsiu_softc),
    101       1.3  thorpej     vrdsiu_match, vrdsiu_attach, NULL, NULL);
    102       1.1     greg 
    103       1.1     greg static inline void
    104  1.9.58.1      jym vrdsiu_write(struct vrdsiu_softc *sc, int port, unsigned short val)
    105       1.1     greg {
    106       1.1     greg 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, port, val);
    107       1.1     greg }
    108       1.1     greg 
    109       1.1     greg static inline unsigned short
    110  1.9.58.1      jym vrdsiu_read(struct vrdsiu_softc *sc, int port)
    111       1.1     greg {
    112       1.1     greg 	return bus_space_read_2(sc->sc_iot, sc->sc_ioh, port);
    113       1.1     greg }
    114       1.1     greg 
    115       1.1     greg static int
    116  1.9.58.1      jym vrdsiu_match(struct device *parent, struct cfdata *cf, void *aux)
    117       1.1     greg {
    118       1.1     greg 	return 1;
    119       1.1     greg }
    120       1.1     greg 
    121       1.1     greg static void
    122  1.9.58.1      jym vrdsiu_attach(struct device *parent, struct device *self, void *aux)
    123       1.1     greg {
    124       1.1     greg 	struct vrdsiu_softc *sc = (struct vrdsiu_softc *)self;
    125       1.1     greg 	struct vrip_attach_args *va = aux;
    126       1.1     greg 	struct wsmousedev_attach_args wsmaa;
    127       1.1     greg         int res;
    128       1.1     greg 
    129       1.1     greg 	bus_space_tag_t iot = va->va_iot;
    130       1.1     greg 
    131       1.5       he         if (va->va_parent_ioh != 0)
    132       1.1     greg                 res = bus_space_subregion(iot, va->va_parent_ioh, va->va_addr,
    133       1.1     greg                     va->va_size, &sc->sc_ioh);
    134       1.1     greg         else
    135       1.1     greg                 res = bus_space_map(iot, va->va_addr, va->va_size, 0,
    136       1.1     greg                     &sc->sc_ioh);
    137       1.1     greg 	if (res != 0) {
    138       1.1     greg 		printf(": can't map bus space\n");
    139       1.1     greg 		return;
    140       1.1     greg 	}
    141       1.1     greg 
    142       1.1     greg 	sc->sc_iot = iot;
    143       1.1     greg 	sc->sc_unit = va->va_unit;
    144       1.1     greg 	sc->sc_vrip = va->va_vc;
    145       1.1     greg 
    146       1.1     greg 	/* install interrupt handler and enable interrupt */
    147       1.1     greg 	if (!(sc->sc_handler =
    148       1.1     greg             vrip_intr_establish(sc->sc_vrip, sc->sc_unit, 0, IPL_TTY,
    149       1.1     greg                 vrdsiu_intr, sc))) {
    150       1.1     greg 		printf(": can't map interrupt line\n");
    151       1.1     greg 		return;
    152       1.1     greg 	}
    153       1.1     greg 
    154       1.1     greg 	/* disable the handler initially */
    155       1.1     greg 	vrdsiu_mouse_disable(sc);
    156       1.1     greg 
    157       1.1     greg 	printf("\n");
    158       1.1     greg 
    159       1.1     greg 	/* attach the mouse */
    160       1.1     greg 	wsmaa.accessops = &vrdsiu_accessops;
    161       1.1     greg 	wsmaa.accesscookie = sc;
    162       1.1     greg 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
    163       1.1     greg 
    164       1.1     greg 	/*
    165       1.1     greg 	 * TODO: Initialize the DSIU ourselves.
    166       1.1     greg 	 *       We currently assume WinCE has set up the DSIU properly
    167       1.1     greg 	 */
    168       1.1     greg 
    169       1.1     greg 	asimOld = vrdsiu_read(sc, DSIUASIM00_REG_W);
    170       1.1     greg 
    171       1.1     greg 	/* supply clock to the DSIU */
    172       1.1     greg 	vrip_power(sc->sc_vrip, sc->sc_unit, 1);
    173       1.1     greg }
    174       1.1     greg 
    175       1.1     greg int
    176  1.9.58.1      jym vrdsiu_mouse_enable(void *v)
    177       1.1     greg {
    178       1.1     greg 	struct vrdsiu_softc *sc = v;
    179       1.1     greg 
    180       1.1     greg 	/* ensure the DSIU mouse is currently disabled! */
    181       1.1     greg 	if (sc->sc_mouse_stat != VRDSIU_MOUSE_STAT_DISABLE)
    182       1.1     greg 		return EBUSY;
    183       1.1     greg 
    184       1.1     greg 	/* enable the DSIU mouse */
    185       1.1     greg 	sc->sc_mouse_stat = VRDSIU_MOUSE_STAT_ENABLE;
    186       1.1     greg 
    187       1.1     greg 	/* unmask interrupts */
    188       1.1     greg 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, (1 << 8) | (1 << 9) | (1 << 10), 1);
    189       1.1     greg 	vrip_power(sc->sc_vrip, sc->sc_unit, 1);
    190       1.1     greg 
    191       1.1     greg 	return 0;
    192       1.1     greg }
    193       1.1     greg 
    194       1.1     greg void
    195  1.9.58.1      jym vrdsiu_mouse_disable(void *v)
    196       1.1     greg {
    197       1.1     greg 	struct vrdsiu_softc *sc = v;
    198       1.1     greg 
    199       1.1     greg 	/* mask interrupts */
    200       1.1     greg 	vrip_intr_setmask2(sc->sc_vrip, sc->sc_handler, (1 << 8) | (1 << 9) | (1 << 10), 0);
    201       1.1     greg 
    202       1.1     greg 	/* disable the DSIU mouse */
    203       1.1     greg 	sc->sc_mouse_stat = VRDSIU_MOUSE_STAT_DISABLE;
    204       1.1     greg }
    205       1.1     greg 
    206       1.1     greg int
    207  1.9.58.1      jym vrdsiu_mouse_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    208       1.1     greg {
    209       1.1     greg 	/*struct vrdsiu_softc *sc = v;*/
    210       1.1     greg 
    211       1.1     greg 	switch (cmd)
    212       1.1     greg 	{
    213       1.1     greg 	case WSMOUSEIO_GTYPE:
    214       1.1     greg 		*(u_int *)data = WSMOUSE_TYPE_PS2;
    215       1.1     greg 		break;
    216       1.1     greg 
    217       1.1     greg 	case WSMOUSEIO_SRES:
    218       1.1     greg 		/*
    219       1.1     greg 		 * TODO: Handle setting mouse resolution
    220       1.1     greg 		 */
    221       1.1     greg 		break;
    222       1.1     greg 
    223       1.1     greg 	default:
    224       1.1     greg 		return -1;
    225       1.1     greg 	}
    226       1.1     greg 
    227       1.1     greg 	return 0;
    228       1.1     greg }
    229       1.1     greg 
    230       1.1     greg int
    231  1.9.58.1      jym vrdsiu_intr(void *arg)
    232       1.1     greg {
    233       1.1     greg 	struct vrdsiu_softc *sc = arg;
    234       1.1     greg 
    235       1.1     greg 	vrdsiu_mouse_intr(sc);
    236       1.1     greg 
    237       1.1     greg 	return 0;
    238       1.1     greg }
    239       1.1     greg 
    240       1.1     greg /*
    241       1.1     greg  * PS/2 protocol defines
    242       1.1     greg  */
    243       1.1     greg #define PS2_L_BUTTON_MASK (1 << 0)
    244       1.1     greg #define PS2_R_BUTTON_MASK (1 << 1)
    245       1.1     greg #define PS2_M_BUTTON_MASK (1 << 2)
    246       1.1     greg #define PS2_BYTE0_BIT3_MASK (1 << 3)
    247       1.1     greg #define PS2_DX_SIGN_MASK (1 << 4)
    248       1.1     greg #define PS2_DY_SIGN_MASK (1 << 5)
    249       1.1     greg #define PS2_DX_OVERFLOW_MASK (1 << 6)
    250       1.1     greg #define PS2_DY_OVERFLOW_MASK (1 << 7)
    251       1.1     greg 
    252       1.1     greg /*
    253       1.1     greg  * WSCONS defines
    254       1.1     greg  */
    255       1.1     greg #define WSC_L_BUTTON 0x01
    256       1.1     greg #define WSC_M_BUTTON 0x02
    257       1.1     greg #define WSC_R_BUTTON 0x04
    258       1.1     greg 
    259       1.1     greg void
    260  1.9.58.1      jym vrdsiu_mouse_intr(struct vrdsiu_softc *sc)
    261       1.1     greg {
    262       1.1     greg 	u_int intrReason;
    263       1.1     greg 	unsigned char b;
    264       1.1     greg 
    265       1.1     greg 	static int dx;
    266       1.1     greg 	static int dy;
    267       1.1     greg 	static u_char buttons;
    268       1.1     greg 	static enum vrdsiu_ps2_input_state ps2_state = 0;
    269       1.1     greg 
    270       1.1     greg 	/* What caused the interrupt? */
    271       1.1     greg 	intrReason = vrdsiu_read(sc, DSIUINTR0_REG_W);
    272       1.1     greg 
    273       1.1     greg 	/*
    274       1.1     greg 	 * TODO: Check for error conditions; specifically need to handle
    275       1.1     greg 	 *       overruns (which currently mess up the mouse).
    276       1.1     greg 	 */
    277       1.1     greg 
    278       1.1     greg 	/* disable reception */
    279       1.1     greg 	vrdsiu_write(sc, DSIUASIM00_REG_W, asimOld & ~DSIUASIM00_RXE0);
    280       1.1     greg 
    281       1.1     greg 	if (intrReason & DSIUINTR0_INTSER0)
    282       1.1     greg 		ps2_state = 0;
    283       1.1     greg 
    284       1.1     greg 	/* Ignore everything except receive notifications */
    285       1.1     greg 	if ((intrReason & DSIUINTR0_INTSR0) == 0)
    286       1.1     greg 		goto done;
    287       1.1     greg 
    288       1.1     greg 	/* read from DSIU */
    289       1.1     greg 	b = (unsigned char)vrdsiu_read(sc, DSIURXB0L_REG_W);
    290       1.1     greg 
    291       1.1     greg 	/* check if the DSIU is enabled */
    292       1.1     greg 	if (sc->sc_mouse_stat == VRDSIU_MOUSE_STAT_DISABLE)
    293       1.1     greg 	{
    294       1.1     greg 		/* Throw away input to keep the DSIU's buffer clean */
    295       1.1     greg 		goto done;
    296       1.1     greg 	}
    297       1.1     greg 
    298       1.1     greg 	/*
    299       1.1     greg 	 * PS/2 protocol interpretation
    300       1.1     greg 	 *
    301       1.1     greg 	 * A PS/2 packet consists of 3 bytes.  We read them in, in order, and
    302       1.1     greg 	 * piece together the mouse info.
    303       1.1     greg 	 *
    304       1.1     greg 	 * Byte 0 contains button info and dx/dy signedness
    305       1.1     greg 	 * Byte 1 contains dx info
    306       1.1     greg 	 * Byte 2 contains dy info
    307       1.1     greg 	 *
    308       1.1     greg 	 * Please see PS/2 specs for detailed information; brief descriptions
    309       1.1     greg 	 * are provided below.
    310       1.1     greg 	 *
    311       1.1     greg 	 * PS/2 mouse specs for the TrackPoint from IBM's TrackPoint Engineering
    312       1.1     greg 	 * Specification Version 4.0.
    313       1.1     greg 	 */
    314       1.1     greg 	switch (ps2_state)
    315       1.1     greg 	{
    316       1.1     greg 	case VRDSIU_PS2_INPUT_STATE_BYTE0:
    317       1.1     greg 		/* Bit 3 of byte 0 is always 1; we use that info to sync input */
    318       1.1     greg 		if ((b & PS2_BYTE0_BIT3_MASK) == 0)
    319       1.1     greg 			goto done;
    320       1.1     greg 
    321       1.1     greg 		if (b & (PS2_M_BUTTON_MASK | PS2_DX_OVERFLOW_MASK |
    322       1.1     greg 			PS2_DY_OVERFLOW_MASK))
    323       1.1     greg 			goto done;
    324       1.1     greg 
    325       1.1     greg 		/* Extract button state */
    326       1.1     greg 		buttons = ((b & PS2_L_BUTTON_MASK) ? WSC_L_BUTTON : 0)
    327       1.1     greg 			| ((b & PS2_M_BUTTON_MASK) ? WSC_M_BUTTON : 0)
    328       1.1     greg 			| ((b & PS2_R_BUTTON_MASK) ? WSC_R_BUTTON : 0);
    329       1.1     greg 
    330       1.1     greg 		/* Extract dx/dy signedness -- 9-bit 2's comp */
    331       1.1     greg 		/* dx = (b & PS2_DX_SIGN_MASK) ? 0xFFFFFFFF : 0;
    332       1.1     greg 		dy = (b & PS2_DY_SIGN_MASK) ? 0xFFFFFFFF : 0; */
    333       1.1     greg 
    334       1.1     greg 		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE1;
    335       1.1     greg 		break;
    336       1.1     greg 
    337       1.1     greg 	case VRDSIU_PS2_INPUT_STATE_BYTE1:
    338       1.1     greg 		/* put in the lower 8 bits of dx */
    339       1.1     greg 		dx = (signed char)b;
    340       1.1     greg 		if (dx == -128)
    341       1.1     greg 			dx = -127;
    342       1.1     greg 
    343       1.1     greg 		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE2;
    344       1.1     greg 		break;
    345       1.1     greg 
    346       1.1     greg 	case VRDSIU_PS2_INPUT_STATE_BYTE2:
    347       1.1     greg 		/* put in the lower 8 bits of dy */
    348       1.1     greg 		dy = (signed char)b;
    349       1.1     greg 		if (dy == -128)
    350       1.1     greg 			dy = -127;
    351       1.1     greg 
    352       1.1     greg 		/* We now have a complete packet; send to wscons */
    353       1.1     greg 		wsmouse_input(sc->sc_wsmousedev,
    354       1.8   plunky 				buttons,
    355       1.8   plunky 				dx, dy, 0, 0,
    356       1.8   plunky 				WSMOUSE_INPUT_DELTA);
    357       1.1     greg 
    358       1.1     greg 		ps2_state = VRDSIU_PS2_INPUT_STATE_BYTE0;
    359       1.1     greg 		break;
    360       1.1     greg 	}
    361       1.1     greg 
    362       1.1     greg done:
    363       1.1     greg 	/* clear the interrupt */
    364       1.1     greg 	vrdsiu_write(sc, DSIUINTR0_REG_W, intrReason);
    365       1.1     greg 
    366       1.1     greg 	/* enable reception */
    367       1.1     greg 	vrdsiu_write(sc, DSIUASIM00_REG_W, asimOld | DSIUASIM00_RXE0);
    368       1.1     greg }
    369