Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / ews4800mips / dev / ewsms.c
bloba28dcc8ea8d5736ea907fda060820dffe513b984
1 /* $NetBSD: ewsms.c,v 1.6 2008/03/29 19:15:34 tsutsui Exp $ */
3 /*
4 * Copyright (c) 2004 Steve Rumble
5 * All rights reserved.
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.
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.
31 * EWS4800 serial mouse driver attached to zs channel 0 at 4800 or 1200bps.
32 * This layer feeds wsmouse.
34 * 5 byte packets: sync, x1, y1, x2, y2
35 * sync format: binary 10000LMR (left, middle, right) 0 is down
37 * This driver is taken from sgimips, but ews4800 has the similar protocol.
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ewsms.c,v 1.6 2008/03/29 19:15:34 tsutsui Exp $");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/conf.h>
46 #include <sys/device.h>
48 #include <dev/wscons/wsconsio.h>
49 #include <dev/wscons/wsmousevar.h>
51 #include <dev/ic/z8530reg.h>
52 #include <machine/z8530var.h>
54 #define EWSMS_BAUD 1200
55 #define EWSMS_BAUD1 4800
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)
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)
71 struct ewsms_softc {
72 device_t sc_dev;
74 /* tail-chasing fifo */
75 uint8_t sc_rxq[EWSMS_RXQ_LEN];
76 u_int sc_rxq_head;
77 u_int sc_rxq_tail;
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
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
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
101 /* wsmouse bits */
102 int sc_enabled;
103 struct device *sc_wsmousedev;
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 *);
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 *);
119 CFATTACH_DECL_NEW(ewsms_zsc, sizeof(struct ewsms_softc),
120 ewsms_zsc_match, ewsms_zsc_attach, NULL, NULL);
122 static struct zsops ewsms_zsops = {
123 ewsms_zsc_rxint,
124 ewsms_zsc_stint,
125 ewsms_zsc_txint,
126 ewsms_zsc_softint
129 static const struct wsmouse_accessops ewsms_wsmouse_accessops = {
130 ewsms_wsmouse_enable,
131 ewsms_wsmouse_ioctl,
132 ewsms_wsmouse_disable
136 ewsms_zsc_match(device_t parent, cfdata_t cf, void *aux)
138 struct zsc_softc *zsc = device_private(parent);
139 struct zsc_attach_args *zsc_args = aux;
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;
147 return 0;
150 void
151 ewsms_zsc_attach(device_t parent, device_t self, void *aux)
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;
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;
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;
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;
179 ewsms_zsc_reset(cs);
181 aprint_normal(": baud rate %d\n", EWSMS_BAUD);
183 /* attach wsmouse */
184 wsmaa.accessops = &ewsms_wsmouse_accessops;
185 wsmaa.accesscookie = sc;
186 sc->sc_wsmousedev = config_found(self, &wsmaa, wsmousedevprint);
190 ewsms_zsc_reset(struct zs_chanstate *cs)
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 };
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);
207 if (sc->sc_flags & EWSMS_F_RSTCMD) {
208 for (i = 0; i < sizeof(cmd); i++)
209 zs_putc(cs, cmd[i]);
212 splx(s);
214 return 0;
217 void
218 ewsms_zsc_rxint(struct zs_chanstate *cs)
220 struct ewsms_softc *sc = cs->cs_private;
221 uint8_t c, r;
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);
228 /* read byte and append to our queue */
229 c = zs_read_data(cs);
231 sc->sc_rxq[sc->sc_rxq_tail] = c;
232 sc->sc_rxq_tail = EWSMS_NEXTRXQ(sc->sc_rxq_tail);
234 cs->cs_softreq = 1;
237 /* We should never get here. */
238 void
239 ewsms_zsc_txint(struct zs_chanstate *cs)
242 zs_write_reg(cs, 0, ZSWR0_RESET_TXINT);
244 /* seems like the in thing to do */
245 cs->cs_softreq = 1;
248 void
249 ewsms_zsc_stint(struct zs_chanstate *cs, int force)
252 zs_write_csr(cs, ZSWR0_RESET_STATUS);
253 cs->cs_softreq = 1;
256 void
257 ewsms_zsc_softint(struct zs_chanstate *cs)
259 struct ewsms_softc *sc = cs->cs_private;
260 uint8_t sync;
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;
269 * Here's the real action. Read a full packet and
270 * then let wsmouse know what has happened.
272 while (sc->sc_rxq_head != sc->sc_rxq_tail) {
273 int8_t c = sc->sc_rxq[sc->sc_rxq_head];
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;
284 break;
286 case EWSMS_STATE_X1:
287 sc->sc_packet[EWSMS_PACKET_X1] = c;
288 sc->sc_state = EWSMS_STATE_Y1;
289 break;
291 case EWSMS_STATE_Y1:
292 sc->sc_packet[EWSMS_PACKET_Y1] = c;
293 sc->sc_state = EWSMS_STATE_X2;
294 break;
296 case EWSMS_STATE_X2:
297 sc->sc_packet[EWSMS_PACKET_X2] = c;
298 sc->sc_state = EWSMS_STATE_Y2;
299 break;
301 case EWSMS_STATE_Y2:
302 sc->sc_packet[EWSMS_PACKET_Y2] = c;
304 /* tweak wsmouse */
305 ewsms_wsmouse_input(sc);
307 sc->sc_state = EWSMS_STATE_SYNC;
310 sc->sc_rxq_head = EWSMS_NEXTRXQ(sc->sc_rxq_head);
314 /******************************************************************************
315 * wsmouse glue
316 ******************************************************************************/
318 static void
319 ewsms_wsmouse_input(struct ewsms_softc *sc)
321 u_int btns;
322 bool bl, bm, br;
323 int dx, dy;
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;
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);
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];
346 wsmouse_input(sc->sc_wsmousedev,
347 btns,
348 dx, dy, 0, 0,
349 WSMOUSE_INPUT_DELTA);
352 static int
353 ewsms_wsmouse_enable(void *cookie)
355 struct ewsms_softc *sc = cookie;
357 if (sc->sc_enabled)
358 return EBUSY;
360 sc->sc_state = EWSMS_STATE_SYNC;
361 sc->sc_enabled = 1;
363 return 0;
366 void
367 ewsms_wsmouse_disable(void *cookie)
369 struct ewsms_softc *sc = cookie;
371 sc->sc_enabled = 0;
374 static int
375 ewsms_wsmouse_ioctl(void *cookie, u_long cmd, void *data, int flag,
376 struct lwp *l)
379 switch (cmd) {
380 case WSMOUSEIO_GTYPE:
381 *(u_int *)data = 0;
382 break;
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
393 default:
394 return EPASSTHROUGH;
397 return 0;