Home | History | Annotate | Line # | Download | only in dev
ewsms.c revision 1.6
      1 /*	$NetBSD: ewsms.c,v 1.6 2008/03/29 19:15:34 tsutsui Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 Steve Rumble
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*
     31  * EWS4800 serial mouse driver attached to zs channel 0 at 4800 or 1200bps.
     32  * This layer feeds wsmouse.
     33  *
     34  * 5 byte packets: sync, x1, y1, x2, y2
     35  * sync format: binary 10000LMR (left, middle, right) 0 is down
     36  *
     37  * This driver is taken from sgimips, but ews4800 has the similar protocol.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: ewsms.c,v 1.6 2008/03/29 19:15:34 tsutsui Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/conf.h>
     46 #include <sys/device.h>
     47 
     48 #include <dev/wscons/wsconsio.h>
     49 #include <dev/wscons/wsmousevar.h>
     50 
     51 #include <dev/ic/z8530reg.h>
     52 #include <machine/z8530var.h>
     53 
     54 #define EWSMS_BAUD		1200
     55 #define EWSMS_BAUD1		4800
     56 
     57 #define EWSMS_RXQ_LEN		64	/* power of 2 */
     58 #define EWSMS_RXQ_LEN_MASK	(EWSMS_RXQ_LEN - 1)
     59 #define EWSMS_NEXTRXQ(x)	(((x) + 1) & EWSMS_RXQ_LEN_MASK)
     60 
     61 /* protocol */
     62 #define EWSMS_SYNC0		0x80
     63 #define EWSMS_SYNC1		0x88
     64 #define EWSMS_SYNC_MASK		0xf8
     65 #define EWSMS_SYNC_BTN_R	0x01
     66 #define EWSMS_SYNC_BTN_M	0x02
     67 #define EWSMS_SYNC_BTN_L	0x04
     68 #define EWSMS_SYNC_BTN_MASK	\
     69 	(EWSMS_SYNC_BTN_R|EWSMS_SYNC_BTN_M|EWSMS_SYNC_BTN_L)
     70 
     71 struct ewsms_softc {
     72 	device_t sc_dev;
     73 
     74 	/* tail-chasing fifo */
     75 	uint8_t sc_rxq[EWSMS_RXQ_LEN];
     76 	u_int sc_rxq_head;
     77 	u_int sc_rxq_tail;
     78 
     79 	/* 5-byte packet as described above */
     80 	int8_t	sc_packet[5];
     81 #define EWSMS_PACKET_SYNC	0
     82 #define EWSMS_PACKET_X1		1
     83 #define EWSMS_PACKET_Y1		2
     84 #define EWSMS_PACKET_X2		3
     85 #define EWSMS_PACKET_Y2		4
     86 
     87 	u_int sc_state;
     88 #define EWSMS_STATE_SYNC	0x01
     89 #define EWSMS_STATE_X1		0x02
     90 #define EWSMS_STATE_Y1		0x04
     91 #define EWSMS_STATE_X2		0x08
     92 #define EWSMS_STATE_Y2		0x10
     93 
     94 	u_int sc_baud;
     95 	u_int sc_flags;
     96 #define EWSMS_F_FASTBAUD	0x01
     97 #define EWSMS_F_RSTCMD		0x02
     98 #define EWSMS_F_SWAPBTN		0x04
     99 #define EWSMS_F_SYNC1		0x08
    100 
    101 	/* wsmouse bits */
    102 	int sc_enabled;
    103 	struct device *sc_wsmousedev;
    104 };
    105 
    106 static int  ewsms_zsc_match(device_t, cfdata_t, void *);
    107 static void ewsms_zsc_attach(device_t, device_t, void *);
    108 static int  ewsms_zsc_reset(struct zs_chanstate *);
    109 static void ewsms_zsc_rxint(struct zs_chanstate *);
    110 static void ewsms_zsc_txint(struct zs_chanstate *);
    111 static void ewsms_zsc_stint(struct zs_chanstate *, int);
    112 static void ewsms_zsc_softint(struct zs_chanstate *);
    113 
    114 static void ewsms_wsmouse_input(struct ewsms_softc *);
    115 static int  ewsms_wsmouse_enable(void *);
    116 static void ewsms_wsmouse_disable(void *);
    117 static int  ewsms_wsmouse_ioctl(void *, u_long, void *, int, struct lwp *);
    118 
    119 CFATTACH_DECL_NEW(ewsms_zsc, sizeof(struct ewsms_softc),
    120     ewsms_zsc_match, ewsms_zsc_attach, NULL, NULL);
    121 
    122 static struct zsops ewsms_zsops = {
    123 	ewsms_zsc_rxint,
    124 	ewsms_zsc_stint,
    125 	ewsms_zsc_txint,
    126 	ewsms_zsc_softint
    127 };
    128 
    129 static const struct wsmouse_accessops ewsms_wsmouse_accessops = {
    130 	ewsms_wsmouse_enable,
    131 	ewsms_wsmouse_ioctl,
    132 	ewsms_wsmouse_disable
    133 };
    134 
    135 int
    136 ewsms_zsc_match(device_t parent, cfdata_t cf, void *aux)
    137 {
    138 	struct zsc_softc *zsc = device_private(parent);
    139 	struct zsc_attach_args *zsc_args = aux;
    140 
    141 	/* mouse on channel A */
    142 	if ((zsc->zsc_flags & 0x0001 /* kbms port */) != 0 &&
    143 	    zsc_args->channel == 0)
    144 		/* prior to generic zstty(4) */
    145 		return 3;
    146 
    147 	return 0;
    148 }
    149 
    150 void
    151 ewsms_zsc_attach(device_t parent, device_t self, void *aux)
    152 {
    153 	struct ewsms_softc *sc = device_private(self);
    154 	struct zsc_softc *zsc = device_private(parent);
    155 	struct zsc_attach_args *zsc_args = aux;
    156 	struct zs_chanstate *cs;
    157 	struct wsmousedev_attach_args wsmaa;
    158 	int channel;
    159 
    160 	/* Establish ourself with the MD z8530 driver */
    161 	channel = zsc_args->channel;
    162 	cs = zsc->zsc_cs[channel];
    163 	cs->cs_ops = &ewsms_zsops;
    164 	cs->cs_private = sc;
    165 
    166 	sc->sc_dev = self;
    167 	sc->sc_enabled = 0;
    168 	sc->sc_rxq_head = 0;
    169 	sc->sc_rxq_tail = 0;
    170 	sc->sc_state = EWSMS_STATE_SYNC;
    171 
    172 	if (zsc->zsc_flags & 0x0002) {
    173 		sc->sc_flags |= EWSMS_F_RSTCMD;
    174 		sc->sc_flags |= EWSMS_F_FASTBAUD;
    175 		sc->sc_flags |= EWSMS_F_SWAPBTN;
    176 		sc->sc_flags |= EWSMS_F_SYNC1;
    177 	}
    178 
    179 	ewsms_zsc_reset(cs);
    180 
    181 	printf(": baud rate %d\n", EWSMS_BAUD);
    182 
    183 	/* attach wsmouse */
    184 	wsmaa.accessops = &ewsms_wsmouse_accessops;
    185 	wsmaa.accesscookie = sc;
    186 	sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
    187 }
    188 
    189 int
    190 ewsms_zsc_reset(struct zs_chanstate *cs)
    191 {
    192 	struct ewsms_softc *sc = cs->cs_private;
    193 	u_int baud;
    194 	int i, s;
    195 	const static uint8_t cmd[] =
    196 	    { 0x02, 0x52, 0x53, 0x3b, 0x4d, 0x54, 0x2c, 0x36, 0x0d };
    197 
    198 	s = splzs();
    199 	zs_write_reg(cs, 9, ZSWR9_A_RESET);
    200 	cs->cs_preg[1] = ZSWR1_RIE;
    201 	baud = EWSMS_BAUD;
    202 	if (sc->sc_flags & EWSMS_F_FASTBAUD)
    203 		baud = EWSMS_BAUD1;
    204 	zs_set_speed(cs, baud);
    205 	zs_loadchannelregs(cs);
    206 
    207 	if (sc->sc_flags & EWSMS_F_RSTCMD) {
    208 		for (i = 0; i < sizeof(cmd); i++)
    209 			zs_putc(cs, cmd[i]);
    210 	}
    211 
    212 	splx(s);
    213 
    214 	return 0;
    215 }
    216 
    217 void
    218 ewsms_zsc_rxint(struct zs_chanstate *cs)
    219 {
    220 	struct ewsms_softc *sc = cs->cs_private;
    221 	u_char c, r;
    222 
    223 	/* clear errors */
    224 	r = zs_read_reg(cs, 1);
    225 	if (r & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE))
    226 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    227 
    228 	/* read byte and append to our queue */
    229 	c = zs_read_data(cs);
    230 
    231 	sc->sc_rxq[sc->sc_rxq_tail] = c;
    232 	sc->sc_rxq_tail = EWSMS_NEXTRXQ(sc->sc_rxq_tail);
    233 
    234 	cs->cs_softreq = 1;
    235 }
    236 
    237 /* We should never get here. */
    238 void
    239 ewsms_zsc_txint(struct zs_chanstate *cs)
    240 {
    241 	zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
    242 
    243 	/* seems like the in thing to do */
    244 	cs->cs_softreq = 1;
    245 }
    246 
    247 void
    248 ewsms_zsc_stint(struct zs_chanstate *cs, int force)
    249 {
    250 
    251 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    252 	cs->cs_softreq = 1;
    253 }
    254 
    255 void
    256 ewsms_zsc_softint(struct zs_chanstate *cs)
    257 {
    258 	struct ewsms_softc *sc = cs->cs_private;
    259 	uint8_t sync;
    260 
    261 	/* No need to keep score if nobody is listening */
    262 	if (!sc->sc_enabled) {
    263 		sc->sc_rxq_head = sc->sc_rxq_tail;
    264 		return;
    265 	}
    266 
    267 	/*
    268 	 * Here's the real action. Read a full packet and
    269 	 * then let wsmouse know what has happened.
    270 	 */
    271 	while (sc->sc_rxq_head != sc->sc_rxq_tail) {
    272 		int8_t c = sc->sc_rxq[sc->sc_rxq_head];
    273 
    274 		switch (sc->sc_state) {
    275 		case EWSMS_STATE_SYNC:
    276 			sync = EWSMS_SYNC0;
    277 			if (sc->sc_flags & EWSMS_F_SYNC1)
    278 				sync = EWSMS_SYNC1;
    279 			if ((c & EWSMS_SYNC_MASK) == sync) {
    280 				sc->sc_packet[EWSMS_PACKET_SYNC] = c;
    281 				sc->sc_state = EWSMS_STATE_X1;
    282 			}
    283 			break;
    284 
    285 		case EWSMS_STATE_X1:
    286 			sc->sc_packet[EWSMS_PACKET_X1] = c;
    287 			sc->sc_state = EWSMS_STATE_Y1;
    288 			break;
    289 
    290 		case EWSMS_STATE_Y1:
    291 			sc->sc_packet[EWSMS_PACKET_Y1] = c;
    292 			sc->sc_state = EWSMS_STATE_X2;
    293 			break;
    294 
    295 		case EWSMS_STATE_X2:
    296 			sc->sc_packet[EWSMS_PACKET_X2] = c;
    297 			sc->sc_state = EWSMS_STATE_Y2;
    298 			break;
    299 
    300 		case EWSMS_STATE_Y2:
    301 			sc->sc_packet[EWSMS_PACKET_Y2] = c;
    302 
    303 			/* tweak wsmouse */
    304 			ewsms_wsmouse_input(sc);
    305 
    306 			sc->sc_state = EWSMS_STATE_SYNC;
    307 		}
    308 
    309 		sc->sc_rxq_head = EWSMS_NEXTRXQ(sc->sc_rxq_head);
    310 	}
    311 }
    312 
    313 /******************************************************************************
    314  * wsmouse glue
    315  ******************************************************************************/
    316 
    317 static void
    318 ewsms_wsmouse_input(struct ewsms_softc *sc)
    319 {
    320 	u_int btns;
    321 	bool bl, bm, br;
    322 	int dx, dy;
    323 
    324 	btns = (uint8_t)sc->sc_packet[EWSMS_PACKET_SYNC] & EWSMS_SYNC_BTN_MASK;
    325 	if (sc->sc_flags & EWSMS_F_SWAPBTN) {
    326 		bl = (btns & EWSMS_SYNC_BTN_R) == 0;
    327 		bm = (btns & EWSMS_SYNC_BTN_M) == 0;
    328 		br = (btns & EWSMS_SYNC_BTN_L) == 0;
    329 	} else {
    330 		bl = (btns & EWSMS_SYNC_BTN_L) == 0;
    331 		bm = (btns & EWSMS_SYNC_BTN_M) == 0;
    332 		br = (btns & EWSMS_SYNC_BTN_R) == 0;
    333 	}
    334 	/* for wsmouse(4), 1 is down, 0 is up, most left button is LSB */
    335 	btns = (bl ? (1 << 0) : 0) | (bm ? (1 << 1) : 0) | (br ? (1 << 2) : 0);
    336 
    337 	/* delta values are signed */
    338 	/* moving left is minus, moving right is plus */
    339 	dx = (int)sc->sc_packet[EWSMS_PACKET_X1] +
    340 	     (int)sc->sc_packet[EWSMS_PACKET_X2];
    341 	/* moving back is minus, moving front is plus */
    342 	dy = (int)sc->sc_packet[EWSMS_PACKET_Y1] +
    343 	     (int)sc->sc_packet[EWSMS_PACKET_Y2];
    344 
    345 	wsmouse_input(sc->sc_wsmousedev,
    346 			btns,
    347 			dx, dy, 0, 0,
    348 			WSMOUSE_INPUT_DELTA);
    349 }
    350 
    351 static int
    352 ewsms_wsmouse_enable(void *cookie)
    353 {
    354 	struct ewsms_softc *sc = cookie;
    355 
    356 	if (sc->sc_enabled)
    357 		return EBUSY;
    358 
    359 	sc->sc_state = EWSMS_STATE_SYNC;
    360 	sc->sc_enabled = 1;
    361 
    362 	return 0;
    363 }
    364 
    365 void
    366 ewsms_wsmouse_disable(void *cookie)
    367 {
    368 	struct ewsms_softc *sc = cookie;
    369 
    370 	sc->sc_enabled = 0;
    371 }
    372 
    373 static int
    374 ewsms_wsmouse_ioctl(void *cookie, u_long cmd, void *data, int flag,
    375     struct lwp *l)
    376 {
    377 
    378 	switch (cmd) {
    379 	case WSMOUSEIO_GTYPE:
    380 		*(u_int *)data = 0;
    381 		break;
    382 
    383 #ifdef notyet
    384 	case WSMOUSEIO_SRES:
    385 	case WSMOUSEIO_SSCALE:
    386 	case WSMOUSEIO_SRATE:
    387 	case WSMOUSEIO_SCALIBCOORDS:
    388 	case WSMOUSEIO_GCALIBCOORDS:
    389 	case WSMOUSEIO_GETID:
    390 #endif
    391 
    392 	default:
    393 		return EPASSTHROUGH;
    394 	}
    395 
    396 	return 0;
    397 }
    398