Home | History | Annotate | Line # | Download | only in dev
optpoint.c revision 1.1
      1 /*	$NetBSD: optpoint.c,v 1.1 2005/05/04 07:54:39 hamajima Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * OptOpint on Telios HC-AJ2
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 
     34 __KERNEL_RCSID(0, "$NetBSD: optpoint.c,v 1.1 2005/05/04 07:54:39 hamajima Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 #include <machine/bus.h>
     40 #include <dev/hpc/hpciovar.h>
     41 #include <dev/wscons/wsconsio.h>
     42 #include <dev/wscons/wsmousevar.h>
     43 #include <hpcmips/tx/tx39var.h>
     44 #include <hpcmips/tx/tx39spivar.h>
     45 #include <hpcmips/tx/tx39icureg.h>
     46 
     47 #undef	OPTPOINTDEBUG
     48 
     49 #ifdef OPTPOINTDEBUG
     50 #define	DPRINTF(arg) printf arg
     51 #else
     52 #define	DPRINTF(arg)
     53 #endif
     54 
     55 struct optpoint_softc {
     56 	struct device sc_dev;
     57 	tx_chipset_tag_t sc_tc;
     58 	struct tx39spi_softc *sc_spi;
     59 	struct hpcio_chip *sc_hc;
     60 	struct device *sc_wsmousedev;
     61 	char packet[4];
     62 	int index;	/* number of bytes received for this packet */
     63 	u_int buttons;	/* mouse button status */
     64 	int enabled;
     65 };
     66 
     67 static int optpoint_match(struct device *, struct cfdata *, void *);
     68 static void optpoint_attach(struct device *, struct device *, void *);
     69 static int optpoint_intr(void *);
     70 static int optpoint_enable(void *);
     71 static void optpoint_disable(void *);
     72 static int optpoint_ioctl(void *, u_long, caddr_t, int, struct proc *);
     73 static int optpoint_initialize(void *);
     74 static void optpoint_send(struct optpoint_softc *, int);
     75 static int optpoint_recv(struct optpoint_softc *);
     76 
     77 #define LBUTMASK 0x01
     78 #define RBUTMASK 0x02
     79 
     80 #define TX39_INTRSTATUS4_OPTPOINTINT	TX39_INTRSTATUS4_CARDIORDNEGINT
     81 #define TX39_IO_MFIO_CARDREG	11
     82 #define TX39_IO_MFIO_CARDIOWR	10
     83 #define TX39_IO_MFIO_CARDIORD	9
     84 #define TELIOS_MFIO_OPTP_T_RDY		TX39_IO_MFIO_CARDREG
     85 #define TELIOS_MFIO_OPTP_C_REQ		TX39_IO_MFIO_CARDIOWR
     86 #define TELIOS_MFIO_OPTP_S_ENB_N	TX39_IO_MFIO_CARDIORD
     87 
     88 CFATTACH_DECL(optpoint, sizeof(struct optpoint_softc),
     89     optpoint_match, optpoint_attach, NULL, NULL);
     90 
     91 const struct wsmouse_accessops optpoint_accessops = {
     92 	optpoint_enable,
     93 	optpoint_ioctl,
     94 	optpoint_disable,
     95 };
     96 
     97 int
     98 optpoint_match(struct device *parent, struct cfdata *cf, void *aux)
     99 {
    100 	return (ATTACH_NORMAL);
    101 }
    102 
    103 void
    104 optpoint_attach(struct device *parent, struct device *self, void *aux)
    105 {
    106 	struct txspi_attach_args *ta = aux;
    107 	struct optpoint_softc *sc = (void*)self;
    108 	struct tx39spi_softc *spi = sc->sc_spi = (void*)parent;
    109 	tx_chipset_tag_t tc = sc->sc_tc = ta->sa_tc;
    110 	struct wsmousedev_attach_args wsmaa;
    111 
    112 	sc->sc_hc = tc->tc_iochip[MFIO];
    113 	sc->enabled = 0;
    114 
    115 	/* Specific SPI settings for OptPoint of HC-AJ2 */
    116 	tx39spi_delayval(spi, 0);
    117 	tx39spi_baudrate(spi, 4);	/* SPICLK Rate = 737.3 kHz */
    118 	tx39spi_word(spi, 0);		/* Use 8bits of data */
    119 	tx39spi_phapol(spi, 0);
    120 	tx39spi_clkpol(spi, 1);
    121 	tx39spi_lsb(spi, 0);		/* MSB first */
    122 
    123 	optpoint_enable(sc);
    124 	printf("\n");
    125 
    126 	wsmaa.accessops = &optpoint_accessops;
    127 	wsmaa.accesscookie = sc;
    128 	/* attach the wsmouse */
    129 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
    130 }
    131 
    132 int
    133 optpoint_intr(void *self)
    134 {
    135 	struct optpoint_softc *sc = (void*)self;
    136 	tx_chipset_tag_t tc = sc->sc_tc;
    137 	char data = optpoint_recv(sc) & 0xff;
    138 
    139 #ifdef DIAGNOSTIC
    140 	if (sc->index >= 3){
    141 		printf("%s: Receive buffer overflow\n", sc->sc_dev.dv_xname);
    142 		sc->index = 0;
    143 		memset(sc->packet, 0, 3);
    144 	}
    145 #endif
    146 	if ((sc->index == 1) && (data & 0xcc) != 0x08){
    147 		DPRINTF(("%s: Bad second byte (0x%02x)\n",
    148 			 sc->sc_dev.dv_xname, data));
    149 		tx_conf_write(tc, TX39_INTRCLEAR4_REG,
    150 				  TX39_INTRSTATUS4_OPTPOINTINT);
    151 		return 0;
    152 	}
    153 
    154 	sc->packet[sc->index++] = data;
    155 	if (sc->index >= 3){
    156 		u_int newbuttons = ((sc->packet[1] & LBUTMASK) ? 0x1 : 0) |
    157 		    ((sc->packet[1] & RBUTMASK) ? 0x2 : 0);
    158 		int dx = sc->packet[2];
    159 		int dy = sc->packet[0];
    160 		u_int changed = (sc->buttons ^ newbuttons);
    161 
    162 		if (dx || dy || changed){
    163 			DPRINTF(("buttons=0x%x, dx=%d, dy=%d\n",
    164 				 newbuttons, dx, dy));
    165 			wsmouse_input(sc->sc_wsmousedev,
    166 				      sc->buttons, dx, dy, 0,
    167 				      WSMOUSE_INPUT_DELTA);
    168 		}
    169 		sc->buttons = newbuttons;
    170 		sc->index = 0;
    171 		memset(sc->packet, 0, 3);
    172 	}
    173 	tx_conf_write(tc, TX39_INTRCLEAR4_REG, TX39_INTRSTATUS4_OPTPOINTINT);
    174 
    175 	return 0;
    176 }
    177 
    178 int
    179 optpoint_enable(void *self)
    180 {
    181 	struct optpoint_softc *sc = (void*)self;
    182 
    183 	if (!sc->enabled){
    184 		tx_chipset_tag_t tc = sc->sc_tc;
    185 		struct hpcio_chip *hc = sc->sc_hc;
    186 		int s = spltty();
    187 
    188 		sc->enabled = 1;
    189 		sc->index = 0;
    190 		sc->buttons = 0;
    191 		sc->packet[0] = 0xf5;	/* Disable */
    192 		sc->packet[1] = 0xf2;	/* Set Stream Mode */
    193 		sc->packet[2] = 0xf8;	/* Stanby */
    194 		sc->packet[3] = 0xf4;	/* Enable */
    195 
    196 		tx39spi_enable(sc->sc_spi, 1);
    197 		tx_intr_establish(tc, MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT),
    198 				  IST_EDGE, IPL_TTY, optpoint_initialize, sc);
    199 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 1);
    200 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
    201 		splx(s);
    202 	}
    203 
    204         return 0;
    205 }
    206 
    207 void
    208 optpoint_disable(void *self)
    209 {
    210 	struct optpoint_softc *sc = (void*)self;
    211 
    212 	if (sc->enabled){
    213 		tx_chipset_tag_t tc = sc->sc_tc;
    214 		struct hpcio_chip *hc = sc->sc_hc;
    215 		int s = spltty();
    216 
    217 		sc->enabled = 0;
    218 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 0);
    219 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 0);
    220 		tx_intr_disestablish(tc,
    221 			    (void *)MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT));
    222 		tx39spi_enable(sc->sc_spi, 0);
    223 		splx(s);
    224 	}
    225 }
    226 
    227 int
    228 optpoint_ioctl(void *self, u_long cmd, caddr_t data, int flag, struct proc *p)
    229 {
    230 	switch (cmd) {
    231 	case WSMOUSEIO_GTYPE:
    232 		*(u_int *)data = WSMOUSE_TYPE_PS2;
    233 		break;
    234 
    235 	default:
    236 		return (EPASSTHROUGH);
    237 	}
    238         return 0;
    239 }
    240 
    241 int
    242 optpoint_initialize(void *self)
    243 {
    244 	struct optpoint_softc *sc = (void*)self;
    245 	struct hpcio_chip *hc = sc->sc_hc;
    246 	tx_chipset_tag_t tc = sc->sc_tc;
    247 
    248 	if (sc->index < 4){
    249 		optpoint_send(sc, sc->packet[sc->index++]);
    250 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 1);
    251 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
    252 	} else {
    253 		tx_intr_disestablish(tc,
    254 			    (void *)MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT));
    255 		sc->index = 0;
    256 		memset(sc->packet, 0, 3);
    257 		tx_intr_establish(tc,
    258 				  MAKEINTR(4, TX39_INTRSTATUS4_OPTPOINTINT),
    259 				  IST_EDGE, IPL_TTY, optpoint_intr, sc);
    260 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_C_REQ, 0);
    261 		(*hc->hc_portwrite)(hc, TELIOS_MFIO_OPTP_T_RDY, 1);
    262 	}
    263 	tx_conf_write(tc, TX39_INTRCLEAR4_REG, TX39_INTRSTATUS4_OPTPOINTINT);
    264 
    265 	return 0;
    266 }
    267 
    268 void
    269 optpoint_send(struct optpoint_softc *sc, int data)
    270 {
    271 	struct hpcio_chip *hc = sc->sc_hc;
    272 
    273 	while ((*hc->hc_portread)(hc, TELIOS_MFIO_OPTP_S_ENB_N))
    274 		;
    275 	tx39spi_put_word(sc->sc_spi, data & 0xff);
    276 }
    277 
    278 int
    279 optpoint_recv(struct optpoint_softc *sc)
    280 {
    281 	struct hpcio_chip *hc = sc->sc_hc;
    282 
    283 	optpoint_send(sc, 0x00);
    284 	while ((*hc->hc_portread)(hc, TELIOS_MFIO_OPTP_S_ENB_N))
    285 		;
    286 	return tx39spi_get_word(sc->sc_spi) & 0xff;
    287 }
    288