1 /* $NetBSD: zs_ms.c,v 1.6 2007/03/04 06:00:39 christos Exp $ */
4 * Copyright (c) 2004 Steve Rumble
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 * IP12/IP20 serial mouse driver attached to zs channel 1 at 4800bps.
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
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: zs_ms.c,v 1.6 2007/03/04 06:00:39 christos Exp $");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/device.h>
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsmousevar.h>
49 #include <dev/ic/z8530reg.h>
50 #include <machine/machtype.h>
51 #include <machine/z8530var.h>
53 #define ZSMS_BAUD 4800
54 #define ZSMS_RXQ_LEN 64 /* power of 2 */
57 #define ZSMS_SYNC 0x80
58 #define ZSMS_SYNC_MASK 0xf8
59 #define ZSMS_SYNC_BTN_R 0x01
60 #define ZSMS_SYNC_BTN_M 0x02
61 #define ZSMS_SYNC_BTN_L 0x04
62 #define ZSMS_SYNC_BTN_MASK 0x07
67 /* tail-chasing fifo */
68 uint8_t rxq
[ZSMS_RXQ_LEN
];
72 /* 5-byte packet as described above */
73 #define ZSMS_PACKET_SYNC 0
74 #define ZSMS_PACKET_X1 1
75 #define ZSMS_PACKET_Y1 2
76 #define ZSMS_PACKET_X2 3
77 #define ZSMS_PACKET_Y2 4
80 #define ZSMS_STATE_SYNC 0x01
81 #define ZSMS_STATE_X1 0x02
82 #define ZSMS_STATE_Y1 0x04
83 #define ZSMS_STATE_X2 0x08
84 #define ZSMS_STATE_Y2 0x10
89 struct device
*wsmousedev
;
92 static int zsms_match(device_t
, cfdata_t
, void *);
93 static void zsms_attach(device_t
, device_t
, void *);
94 static void zsms_rxint(struct zs_chanstate
*);
95 static void zsms_txint(struct zs_chanstate
*);
96 static void zsms_stint(struct zs_chanstate
*, int);
97 static void zsms_softint(struct zs_chanstate
*);
99 static void zsms_wsmouse_input(struct zsms_softc
*);
100 static int zsms_wsmouse_enable(void *);
101 static void zsms_wsmouse_disable(void *);
102 static int zsms_wsmouse_ioctl(void *, u_long
, void *, int,
105 CFATTACH_DECL_NEW(zsms
, sizeof(struct zsms_softc
),
106 zsms_match
, zsms_attach
, NULL
, NULL
);
108 static struct zsops zsms_zsops
= {
115 static const struct wsmouse_accessops zsms_wsmouse_accessops
= {
122 zsms_match(device_t parent
, cfdata_t cf
, void *aux
)
125 if (mach_type
== MACH_SGI_IP12
|| mach_type
== MACH_SGI_IP20
) {
126 struct zsc_attach_args
*args
= aux
;
128 if (args
->channel
== 1)
136 zsms_attach(device_t parent
, device_t self
, void *aux
)
139 struct zsc_softc
*zsc
;
140 struct zsms_softc
*sc
;
141 struct zsc_attach_args
*args
;
142 struct zs_chanstate
*cs
;
143 struct wsmousedev_attach_args wsmaa
;
145 zsc
= device_private(parent
);
146 sc
= device_private(self
);
150 /* Establish ourself with the MD z8530 driver */
151 channel
= args
->channel
;
152 cs
= zsc
->zsc_cs
[channel
];
153 cs
->cs_ops
= &zsms_zsops
;
159 sc
->state
= ZSMS_STATE_SYNC
;
161 aprint_normal(": baud rate %d\n", ZSMS_BAUD
);
164 zs_write_reg(cs
, 9, (channel
== 0) ? ZSWR9_A_RESET
: ZSWR9_B_RESET
);
165 cs
->cs_preg
[1] = ZSWR1_RIE
;
166 zs_set_speed(cs
, ZSMS_BAUD
);
167 zs_loadchannelregs(cs
);
171 wsmaa
.accessops
= &zsms_wsmouse_accessops
;
172 wsmaa
.accesscookie
= sc
;
173 sc
->wsmousedev
= config_found(self
, &wsmaa
, wsmousedevprint
);
177 zsms_rxint(struct zs_chanstate
*cs
)
180 struct zsms_softc
*sc
= cs
->cs_private
;
183 r
= zs_read_reg(cs
, 1);
184 if (r
& (ZSRR1_FE
| ZSRR1_DO
| ZSRR1_PE
))
185 zs_write_csr(cs
, ZSWR0_RESET_ERRORS
);
187 /* read byte and append to our queue */
188 c
= zs_read_data(cs
);
190 sc
->rxq
[sc
->rxq_tail
] = c
;
191 sc
->rxq_tail
= (sc
->rxq_tail
+ 1) & ~ZSMS_RXQ_LEN
;
196 /* We should never get here. */
198 zsms_txint(struct zs_chanstate
*cs
)
201 zs_write_reg(cs
, 0, ZSWR0_RESET_TXINT
);
203 /* seems like the in thing to do */
208 zsms_stint(struct zs_chanstate
*cs
, int force
)
211 zs_write_csr(cs
, ZSWR0_RESET_STATUS
);
216 zsms_softint(struct zs_chanstate
*cs
)
218 struct zsms_softc
*sc
= cs
->cs_private
;
220 /* No need to keep score if nobody is listening */
222 sc
->rxq_head
= sc
->rxq_tail
;
227 * Here's the real action. Read a full packet and
228 * then let wsmouse know what has happened.
230 while (sc
->rxq_head
!= sc
->rxq_tail
) {
231 int8_t c
= sc
->rxq
[sc
->rxq_head
];
234 case ZSMS_STATE_SYNC
:
235 if ((c
& ZSMS_SYNC_MASK
) == ZSMS_SYNC
) {
236 sc
->packet
[ZSMS_PACKET_SYNC
] = c
;
237 sc
->state
= ZSMS_STATE_X1
;
242 sc
->packet
[ZSMS_PACKET_X1
] = c
;
243 sc
->state
= ZSMS_STATE_Y1
;
247 sc
->packet
[ZSMS_PACKET_Y1
] = c
;
248 sc
->state
= ZSMS_STATE_X2
;
252 sc
->packet
[ZSMS_PACKET_X2
] = c
;
253 sc
->state
= ZSMS_STATE_Y2
;
257 sc
->packet
[ZSMS_PACKET_Y2
] = c
;
260 zsms_wsmouse_input(sc
);
262 sc
->state
= ZSMS_STATE_SYNC
;
265 sc
->rxq_head
= (sc
->rxq_head
+ 1) & ~ZSMS_RXQ_LEN
;
269 /******************************************************************************
271 ******************************************************************************/
274 zsms_wsmouse_input(struct zsms_softc
*sc
)
280 btns
= (uint8_t)sc
->packet
[ZSMS_PACKET_SYNC
] & ZSMS_SYNC_BTN_MASK
;
282 bl
= (btns
& ZSMS_SYNC_BTN_L
) == 0;
283 bm
= (btns
& ZSMS_SYNC_BTN_M
) == 0;
284 br
= (btns
& ZSMS_SYNC_BTN_R
) == 0;
286 /* for wsmouse(4), 1 is down, 0 is up, the most left button is LSB */
287 btns
= (bl
? (1 << 0) : 0) | (bm
? (1 << 1) : 0) | (br
? (1 << 2) : 0);
289 x
= (int)sc
->packet
[ZSMS_PACKET_X1
] + (int)sc
->packet
[ZSMS_PACKET_X2
];
290 y
= (int)sc
->packet
[ZSMS_PACKET_Y1
] + (int)sc
->packet
[ZSMS_PACKET_Y2
];
292 wsmouse_input(sc
->wsmousedev
, btns
, x
, y
, 0, 0, WSMOUSE_INPUT_DELTA
);
296 zsms_wsmouse_enable(void *cookie
)
298 struct zsms_softc
*sc
= cookie
;
303 sc
->state
= ZSMS_STATE_SYNC
;
310 zsms_wsmouse_disable(void *cookie
)
312 struct zsms_softc
*sc
= cookie
;
318 zsms_wsmouse_ioctl(void *cookie
, u_long cmd
,
319 void *data
, int flag
, struct lwp
*l
)
323 case WSMOUSEIO_GTYPE
:
324 *(u_int
*)data
= WSMOUSE_TYPE_SGI
;
329 case WSMOUSEIO_SSCALE
:
330 case WSMOUSEIO_SRATE
:
331 case WSMOUSEIO_SCALIBCOORDS
:
332 case WSMOUSEIO_GCALIBCOORDS
:
333 case WSMOUSEIO_GETID
:
337 return (EPASSTHROUGH
);