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