Home | History | Annotate | Line # | Download | only in iomd
qms.c revision 1.14
      1 /*	$NetBSD: qms.c,v 1.14 2008/04/28 20:23:14 martin Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 Reinoud Zandijk
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Reinoud Zandijk
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 /*
     32  * Quadrature mouse driver for the wscons as used in the IOMD
     33  */
     34 
     35 #include <sys/param.h>
     36 
     37 __KERNEL_RCSID(0, "$NetBSD: qms.c,v 1.14 2008/04/28 20:23:14 martin Exp $");
     38 
     39 #include <sys/callout.h>
     40 #include <sys/device.h>
     41 #include <sys/errno.h>
     42 #include <sys/ioctl.h>
     43 #include <sys/kernel.h>
     44 #include <sys/malloc.h>
     45 #include <sys/proc.h>
     46 #include <sys/tty.h>
     47 #include <sys/types.h>
     48 #include <sys/syslog.h>
     49 #include <sys/systm.h>
     50 #include <sys/select.h>
     51 
     52 #include <machine/bus.h>
     53 #include <machine/intr.h>
     54 
     55 #include <arm/iomd/iomdvar.h>
     56 
     57 #include <dev/wscons/wsconsio.h>
     58 #include <dev/wscons/wsmousevar.h>
     59 
     60 struct qms_softc {
     61 	struct device  sc_dev;
     62 	struct device *sc_wsmousedev;
     63 
     64 	bus_space_tag_t sc_iot;		/* bus tag */
     65 	bus_space_handle_t sc_ioh;	/* bus handle for XY */
     66 	bus_space_handle_t sc_butioh;	/* bus handle for buttons */
     67 
     68 	struct callout sc_callout;
     69 
     70 	u_int16_t lastx;
     71 	u_int16_t lasty;
     72 	int lastb;
     73 };
     74 
     75 /* Offsets of hardware registers */
     76 #define QMS_MOUSEX	0		/* 16 bits X register */
     77 #define QMS_MOUSEY	1		/* 16 bits Y register */
     78 #define QMS_BUTTONS	0 		/* mouse buttons in bits 4,5,6 */
     79 
     80 static int  qms_match(struct device *, struct cfdata *, void *);
     81 static void qms_attach(struct device *, struct device *, void *);
     82 
     83 static int qms_enable(void *);
     84 static int qms_ioctl(void *, u_long, void *, int, struct lwp *);
     85 static void qms_disable(void *cookie);
     86 static void qms_intr(void *arg);
     87 
     88 CFATTACH_DECL(qms, sizeof(struct qms_softc),
     89     qms_match, qms_attach, NULL, NULL);
     90 
     91 static struct wsmouse_accessops qms_accessops = {
     92 	qms_enable, qms_ioctl, qms_disable
     93 };
     94 
     95 
     96 static int
     97 qms_match(struct device *parent, struct cfdata *cf, void *aux)
     98 {
     99 	struct qms_attach_args *qa = aux;
    100 
    101 	if (strcmp(qa->qa_name, "qms") == 0)
    102 		return 1;
    103 
    104 	return 0;
    105 }
    106 
    107 
    108 static void
    109 qms_attach(struct device *parent, struct device *self, void *aux)
    110 {
    111 	struct qms_softc *sc = (void *)self;
    112 	struct qms_attach_args *qa = aux;
    113 	struct wsmousedev_attach_args wsmouseargs;
    114 
    115 	sc->sc_iot = qa->qa_iot;
    116 	sc->sc_ioh = qa->qa_ioh;
    117 	sc->sc_butioh = qa->qa_ioh_but;
    118 
    119 	/* set up wsmouse attach arguments */
    120 	wsmouseargs.accessops = &qms_accessops;
    121 	wsmouseargs.accesscookie = sc;
    122 
    123 	printf("\n");
    124 
    125 	sc->sc_wsmousedev =
    126 	    config_found(&sc->sc_dev, &wsmouseargs, wsmousedevprint);
    127 
    128 	callout_init(&sc->sc_callout, 0);
    129 }
    130 
    131 
    132 static int
    133 qms_enable(void *cookie)
    134 {
    135 	struct qms_softc *sc = cookie;
    136 	int b;
    137 
    138 	/* We don't want the mouse to warp on open. */
    139 	sc->lastx = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
    140 	sc->lasty = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
    141 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
    142 
    143 	/* patch up the buttons */
    144 	b >>= 4;
    145 	sc->lastb = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
    146 
    147 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
    148 	return 0;
    149 }
    150 
    151 
    152 static void
    153 qms_disable(void *cookie)
    154 {
    155 	struct qms_softc *sc = cookie;
    156 
    157 	callout_stop(&sc->sc_callout);
    158 }
    159 
    160 
    161 static int
    162 qms_ioctl(void *cookie, u_long cmd, void *data, int flag, struct lwp *l)
    163 {
    164 
    165 	switch (cmd) {
    166 	case WSMOUSEIO_GTYPE:
    167 		*(int *)data = WSMOUSE_TYPE_ARCHIMEDES;
    168 		return 0;
    169 	}
    170 
    171 	return EPASSTHROUGH;
    172 }
    173 
    174 
    175 static void
    176 qms_intr(void *arg)
    177 {
    178 	struct qms_softc *sc = arg;
    179 	int b;
    180 	u_int16_t x, y;
    181 	int16_t dx, dy;
    182 
    183 	x = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX);
    184 	y = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY);
    185 	b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;
    186 
    187 	/* patch up the buttons */
    188 	b >>= 4;
    189 	b = ~( ((b & 1)<<2) | (b & 2) | ((b & 4)>>2));
    190 
    191 	if ((x != sc->lastx) || (y != sc->lasty) || (b != sc->lastb)) {
    192 		/* This assumes that int16_t is two's complement. */
    193 		dx = x - sc->lastx;
    194 		dy = y - sc->lasty;
    195 		wsmouse_input(sc->sc_wsmousedev,
    196 				b,
    197 				dx, dy, 0, 0,
    198 				WSMOUSE_INPUT_DELTA);
    199 
    200 		/* save old values */
    201 		sc->lastx = x;
    202 		sc->lasty = y;
    203 		sc->lastb = b;
    204 	}
    205 	callout_reset(&sc->sc_callout, hz / 100, qms_intr, sc);
    206 }
    207 
    208 
    209 /* end of qms.c */
    210