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