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