1 /* $NetBSD: ewsms.c,v 1.8 2012/10/13 06:08:30 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.8 2012/10/13 06:08:30 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 device_t 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 aprint_normal(": 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 static const 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 uint8_t 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 242 zs_write_reg(cs, 0, ZSWR0_RESET_TXINT); 243 244 /* seems like the in thing to do */ 245 cs->cs_softreq = 1; 246 } 247 248 void 249 ewsms_zsc_stint(struct zs_chanstate *cs, int force) 250 { 251 252 zs_write_csr(cs, ZSWR0_RESET_STATUS); 253 cs->cs_softreq = 1; 254 } 255 256 void 257 ewsms_zsc_softint(struct zs_chanstate *cs) 258 { 259 struct ewsms_softc *sc = cs->cs_private; 260 uint8_t sync; 261 262 /* No need to keep score if nobody is listening */ 263 if (!sc->sc_enabled) { 264 sc->sc_rxq_head = sc->sc_rxq_tail; 265 return; 266 } 267 268 /* 269 * Here's the real action. Read a full packet and 270 * then let wsmouse know what has happened. 271 */ 272 while (sc->sc_rxq_head != sc->sc_rxq_tail) { 273 int8_t c = sc->sc_rxq[sc->sc_rxq_head]; 274 275 switch (sc->sc_state) { 276 case EWSMS_STATE_SYNC: 277 sync = EWSMS_SYNC0; 278 if (sc->sc_flags & EWSMS_F_SYNC1) 279 sync = EWSMS_SYNC1; 280 if ((c & EWSMS_SYNC_MASK) == sync) { 281 sc->sc_packet[EWSMS_PACKET_SYNC] = c; 282 sc->sc_state = EWSMS_STATE_X1; 283 } 284 break; 285 286 case EWSMS_STATE_X1: 287 sc->sc_packet[EWSMS_PACKET_X1] = c; 288 sc->sc_state = EWSMS_STATE_Y1; 289 break; 290 291 case EWSMS_STATE_Y1: 292 sc->sc_packet[EWSMS_PACKET_Y1] = c; 293 sc->sc_state = EWSMS_STATE_X2; 294 break; 295 296 case EWSMS_STATE_X2: 297 sc->sc_packet[EWSMS_PACKET_X2] = c; 298 sc->sc_state = EWSMS_STATE_Y2; 299 break; 300 301 case EWSMS_STATE_Y2: 302 sc->sc_packet[EWSMS_PACKET_Y2] = c; 303 304 /* tweak wsmouse */ 305 ewsms_wsmouse_input(sc); 306 307 sc->sc_state = EWSMS_STATE_SYNC; 308 } 309 310 sc->sc_rxq_head = EWSMS_NEXTRXQ(sc->sc_rxq_head); 311 } 312 } 313 314 /****************************************************************************** 315 * wsmouse glue 316 ******************************************************************************/ 317 318 static void 319 ewsms_wsmouse_input(struct ewsms_softc *sc) 320 { 321 u_int btns; 322 bool bl, bm, br; 323 int dx, dy; 324 325 btns = (uint8_t)sc->sc_packet[EWSMS_PACKET_SYNC] & EWSMS_SYNC_BTN_MASK; 326 if (sc->sc_flags & EWSMS_F_SWAPBTN) { 327 bl = (btns & EWSMS_SYNC_BTN_R) == 0; 328 bm = (btns & EWSMS_SYNC_BTN_M) == 0; 329 br = (btns & EWSMS_SYNC_BTN_L) == 0; 330 } else { 331 bl = (btns & EWSMS_SYNC_BTN_L) == 0; 332 bm = (btns & EWSMS_SYNC_BTN_M) == 0; 333 br = (btns & EWSMS_SYNC_BTN_R) == 0; 334 } 335 /* for wsmouse(4), 1 is down, 0 is up, most left button is LSB */ 336 btns = (bl ? (1 << 0) : 0) | (bm ? (1 << 1) : 0) | (br ? (1 << 2) : 0); 337 338 /* delta values are signed */ 339 /* moving left is minus, moving right is plus */ 340 dx = (int)sc->sc_packet[EWSMS_PACKET_X1] + 341 (int)sc->sc_packet[EWSMS_PACKET_X2]; 342 /* moving back is minus, moving front is plus */ 343 dy = (int)sc->sc_packet[EWSMS_PACKET_Y1] + 344 (int)sc->sc_packet[EWSMS_PACKET_Y2]; 345 346 wsmouse_input(sc->sc_wsmousedev, 347 btns, 348 dx, dy, 0, 0, 349 WSMOUSE_INPUT_DELTA); 350 } 351 352 static int 353 ewsms_wsmouse_enable(void *cookie) 354 { 355 struct ewsms_softc *sc = cookie; 356 357 if (sc->sc_enabled) 358 return EBUSY; 359 360 sc->sc_state = EWSMS_STATE_SYNC; 361 sc->sc_enabled = 1; 362 363 return 0; 364 } 365 366 void 367 ewsms_wsmouse_disable(void *cookie) 368 { 369 struct ewsms_softc *sc = cookie; 370 371 sc->sc_enabled = 0; 372 } 373 374 static int 375 ewsms_wsmouse_ioctl(void *cookie, u_long cmd, void *data, int flag, 376 struct lwp *l) 377 { 378 379 switch (cmd) { 380 case WSMOUSEIO_GTYPE: 381 *(u_int *)data = 0; 382 break; 383 384 #ifdef notyet 385 case WSMOUSEIO_SRES: 386 case WSMOUSEIO_SSCALE: 387 case WSMOUSEIO_SRATE: 388 case WSMOUSEIO_SCALIBCOORDS: 389 case WSMOUSEIO_GCALIBCOORDS: 390 case WSMOUSEIO_GETID: 391 #endif 392 393 default: 394 return EPASSTHROUGH; 395 } 396 397 return 0; 398 } 399