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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
/* $OpenBSD: ms.c,v 1.4 1997/08/19 23:09:34 kstailey Exp $ */
/* $NetBSD: ms.c,v 1.6 1996/05/17 19:32:09 gwr Exp $ */
/*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
*
* @(#)ms.c 8.1 (Berkeley) 6/11/93
*/
/*
* Mouse driver (/dev/mouse)
*/
/*
* Zilog Z8530 Dual UART driver (mouse interface)
*
* This is the "slave" driver that will be attached to
* the "zsc" driver for a Sun mouse.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/signalvar.h>
#include <sys/syslog.h>
#include <dev/ic/z8530reg.h>
#include <machine/z8530var.h>
#include <machine/vuid_event.h>
#include "../../../dev/sun/event_var.h"
/*
* How many input characters we can buffer.
* The port-specific var.h may override this.
* Note: must be a power of two!
*/
#define MS_RX_RING_SIZE 256
#define MS_RX_RING_MASK (MS_RX_RING_SIZE-1)
/*
* Output buffer. Only need a few chars.
*/
#define MS_TX_RING_SIZE 16
#define MS_TX_RING_MASK (MS_TX_RING_SIZE-1)
/*
* Keyboard serial line speed is fixed at 1200 bps.
*/
#define MS_BPS 1200
/*
* Mouse state. A Mouse Systems mouse is a fairly simple device,
* producing five-byte blobs of the form:
*
* b dx dy dx dy
*
* where b is the button state, encoded as 0x80|(~buttons)---there are
* three buttons (4=left, 2=middle, 1=right)---and dx,dy are X and Y
* delta values, none of which have are in [0x80..0x87]. (This lets
* us sync up with the mouse after an error.)
*/
struct ms_softc {
struct device ms_dev; /* required first: base device */
struct zs_chanstate *ms_cs;
/* Flags to communicate with ms_softintr() */
volatile int ms_intr_flags;
#define INTR_RX_OVERRUN 1
#define INTR_TX_EMPTY 2
#define INTR_ST_CHECK 4
/*
* The receive ring buffer.
*/
u_int ms_rbget; /* ring buffer `get' index */
volatile u_int ms_rbput; /* ring buffer `put' index */
u_short ms_rbuf[MS_RX_RING_SIZE]; /* rr1, data pairs */
/*
* State of input translator
*/
short ms_byteno; /* input byte number, for decode */
char ms_mb; /* mouse button state */
char ms_ub; /* user button state */
int ms_dx; /* delta-x */
int ms_dy; /* delta-y */
/*
* State of upper interface.
*/
volatile int ms_ready; /* event queue is ready */
struct evvar ms_events; /* event queue state */
} ms_softc;
cdev_decl(ms); /* open, close, read, write, ioctl, stop, ... */
struct zsops zsops_ms;
/****************************************************************
* Definition of the driver for autoconfig.
****************************************************************/
static int ms_match __P((struct device *, void *, void *));
static void ms_attach __P((struct device *, struct device *, void *));
static void ms_input __P((register struct ms_softc *, register int));
static void ms_rxint __P((register struct zs_chanstate *));
static void ms_txint __P((register struct zs_chanstate *));
static void ms_stint __P((register struct zs_chanstate *));
static void ms_softint __P((struct zs_chanstate *));
struct cfattach ms_ca = {
sizeof(struct ms_softc), ms_match, ms_attach
};
struct cfdriver ms_cd = {
NULL, "ms", DV_DULL
};
/*
* ms_match: how is this zs channel configured?
*/
int
ms_match(parent, match, aux)
struct device *parent;
void *match, *aux;
{
struct cfdata *cf = match;
struct zsc_attach_args *args = aux;
/* Exact match required for keyboard. */
if (cf->cf_loc[0] == args->channel)
return 2;
return 0;
}
void
ms_attach(parent, self, aux)
struct device *parent, *self;
void *aux;
{
struct zsc_softc *zsc = (void *) parent;
struct ms_softc *ms = (void *) self;
struct zsc_attach_args *args = aux;
struct zs_chanstate *cs;
struct cfdata *cf;
int channel, ms_unit;
int reset, s, tconst;
cf = ms->ms_dev.dv_cfdata;
ms_unit = ms->ms_dev.dv_unit;
channel = args->channel;
cs = &zsc->zsc_cs[channel];
cs->cs_private = ms;
cs->cs_ops = &zsops_ms;
ms->ms_cs = cs;
printf("\n");
/* Initialize the speed, etc. */
tconst = BPS_TO_TCONST(cs->cs_brg_clk, MS_BPS);
s = splzs();
/* May need reset... */
reset = (channel == 0) ?
ZSWR9_A_RESET : ZSWR9_B_RESET;
zs_write_reg(cs, 9, reset);
/* These are OK as set by zscc: WR3, WR4, WR5 */
cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
cs->cs_preg[12] = tconst;
cs->cs_preg[13] = tconst >> 8;
zs_loadchannelregs(cs);
splx(s);
/* Initialize translator. */
ms->ms_byteno = -1;
}
/****************************************************************
* Entry points for /dev/mouse
* (open,close,read,write,...)
****************************************************************/
int
msopen(dev, flags, mode, p)
dev_t dev;
int flags, mode;
struct proc *p;
{
struct ms_softc *ms;
int unit;
unit = minor(dev);
if (unit >= ms_cd.cd_ndevs)
return (ENXIO);
ms = ms_cd.cd_devs[unit];
if (ms == NULL)
return (ENXIO);
/* This is an exclusive open device. */
if (ms->ms_events.ev_io)
return (EBUSY);
ms->ms_events.ev_io = p;
ev_init(&ms->ms_events); /* may cause sleep */
ms->ms_ready = 1; /* start accepting events */
return (0);
}
int
msclose(dev, flags, mode, p)
dev_t dev;
int flags, mode;
struct proc *p;
{
struct ms_softc *ms;
ms = ms_cd.cd_devs[minor(dev)];
ms->ms_ready = 0; /* stop accepting events */
ev_fini(&ms->ms_events);
ms->ms_events.ev_io = NULL;
return (0);
}
int
msread(dev, uio, flags)
dev_t dev;
struct uio *uio;
int flags;
{
struct ms_softc *ms;
ms = ms_cd.cd_devs[minor(dev)];
return (ev_read(&ms->ms_events, uio, flags));
}
/* this routine should not exist, but is convenient to write here for now */
int
mswrite(dev, uio, flags)
dev_t dev;
struct uio *uio;
int flags;
{
return (EOPNOTSUPP);
}
int
msioctl(dev, cmd, data, flag, p)
dev_t dev;
u_long cmd;
register caddr_t data;
int flag;
struct proc *p;
{
struct ms_softc *ms;
ms = ms_cd.cd_devs[minor(dev)];
switch (cmd) {
case FIONBIO: /* we will remove this someday (soon???) */
return (0);
case FIOASYNC:
ms->ms_events.ev_async = *(int *)data != 0;
return (0);
case TIOCSPGRP:
if (*(int *)data != ms->ms_events.ev_io->p_pgid)
return (EPERM);
return (0);
case VUIDGFORMAT:
/* we only do firm_events */
*(int *)data = VUID_FIRM_EVENT;
return (0);
case VUIDSFORMAT:
if (*(int *)data != VUID_FIRM_EVENT)
return (EINVAL);
return (0);
}
return (ENOTTY);
}
int
msselect(dev, rw, p)
dev_t dev;
int rw;
struct proc *p;
{
struct ms_softc *ms;
ms = ms_cd.cd_devs[minor(dev)];
return (ev_select(&ms->ms_events, rw, p));
}
/****************************************************************
* Middle layer (translator)
****************************************************************/
/*
* Called by our ms_softint() routine on input.
*/
void
ms_input(ms, c)
register struct ms_softc *ms;
register int c;
{
register struct firm_event *fe;
register int mb, ub, d, get, put, any;
static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
/*
* Discard input if not ready. Drop sync on parity or framing
* error; gain sync on button byte.
*/
if (ms->ms_ready == 0)
return;
if (c == -1) {
ms->ms_byteno = -1;
return;
}
if ((c & ~7) == 0x80) /* if in 0x80..0x87 */
ms->ms_byteno = 0;
/*
* Run the decode loop, adding to the current information.
* We add, rather than replace, deltas, so that if the event queue
* fills, we accumulate data for when it opens up again.
*/
switch (ms->ms_byteno) {
case -1:
return;
case 0:
/* buttons */
ms->ms_byteno = 1;
ms->ms_mb = (~c) & 0x7;
return;
case 1:
/* first delta-x */
ms->ms_byteno = 2;
ms->ms_dx += (char)c;
return;
case 2:
/* first delta-y */
ms->ms_byteno = 3;
ms->ms_dy += (char)c;
return;
case 3:
/* second delta-x */
ms->ms_byteno = 4;
ms->ms_dx += (char)c;
return;
case 4:
/* second delta-x */
ms->ms_byteno = -1; /* wait for button-byte again */
ms->ms_dy += (char)c;
break;
default:
panic("ms_rint");
/* NOTREACHED */
}
/*
* We have at least one event (mouse button, delta-X, or
* delta-Y; possibly all three, and possibly three separate
* button events). Deliver these events until we are out
* of changes or out of room. As events get delivered,
* mark them `unchanged'.
*/
any = 0;
get = ms->ms_events.ev_get;
put = ms->ms_events.ev_put;
fe = &ms->ms_events.ev_q[put];
/* NEXT prepares to put the next event, backing off if necessary */
#define NEXT \
if ((++put) % EV_QSIZE == get) { \
put--; \
goto out; \
}
/* ADVANCE completes the `put' of the event */
#define ADVANCE \
fe++; \
if (put >= EV_QSIZE) { \
put = 0; \
fe = &ms->ms_events.ev_q[0]; \
} \
any = 1
mb = ms->ms_mb;
ub = ms->ms_ub;
while ((d = mb ^ ub) != 0) {
/*
* Mouse button change. Convert up to three changes
* to the `first' change, and drop it into the event queue.
*/
NEXT;
d = to_one[d - 1]; /* from 1..7 to {1,2,4} */
fe->id = to_id[d - 1]; /* from {1,2,4} to ID */
fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
fe->time = time;
ADVANCE;
ub ^= d;
}
if (ms->ms_dx) {
NEXT;
fe->id = LOC_X_DELTA;
fe->value = ms->ms_dx;
fe->time = time;
ADVANCE;
ms->ms_dx = 0;
}
if (ms->ms_dy) {
NEXT;
fe->id = LOC_Y_DELTA;
fe->value = ms->ms_dy;
fe->time = time;
ADVANCE;
ms->ms_dy = 0;
}
out:
if (any) {
ms->ms_ub = ub;
ms->ms_events.ev_put = put;
EV_WAKEUP(&ms->ms_events);
}
}
/****************************************************************
* Interface to the lower layer (zscc)
****************************************************************/
static void
ms_rxint(cs)
register struct zs_chanstate *cs;
{
register struct ms_softc *ms;
register int put, put_next;
register u_char c, rr1;
ms = cs->cs_private;
put = ms->ms_rbput;
/*
* First read the status, because reading the received char
* destroys the status of this char.
*/
rr1 = zs_read_reg(cs, 1);
c = zs_read_data(cs);
if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
/* Clear the receive error. */
zs_write_csr(cs, ZSWR0_RESET_ERRORS);
}
ms->ms_rbuf[put] = (c << 8) | rr1;
put_next = (put + 1) & MS_RX_RING_MASK;
/* Would overrun if increment makes (put==get). */
if (put_next == ms->ms_rbget) {
ms->ms_intr_flags |= INTR_RX_OVERRUN;
} else {
/* OK, really increment. */
put = put_next;
}
/* Done reading. */
ms->ms_rbput = put;
/* Ask for softint() call. */
cs->cs_softreq = 1;
}
static void
ms_txint(cs)
register struct zs_chanstate *cs;
{
register struct ms_softc *ms;
ms = cs->cs_private;
zs_write_csr(cs, ZSWR0_RESET_TXINT);
ms->ms_intr_flags |= INTR_TX_EMPTY;
/* Ask for softint() call. */
cs->cs_softreq = 1;
}
static void
ms_stint(cs)
register struct zs_chanstate *cs;
{
register struct ms_softc *ms;
register int rr0;
ms = cs->cs_private;
rr0 = zs_read_csr(cs);
zs_write_csr(cs, ZSWR0_RESET_STATUS);
cs->cs_rr0_delta |= (cs->cs_rr0 ^ rr0);
cs->cs_rr0 = rr0;
ms->ms_intr_flags |= INTR_ST_CHECK;
/* Ask for softint() call. */
cs->cs_softreq = 1;
}
static void
ms_softint(cs)
struct zs_chanstate *cs;
{
register struct ms_softc *ms;
register int get, c, s;
int intr_flags;
register u_short ring_data;
ms = cs->cs_private;
/* Atomically get and clear flags. */
s = splzs();
intr_flags = ms->ms_intr_flags;
ms->ms_intr_flags = 0;
/* Now lower to spltty for the rest. */
(void) spltty();
/*
* Copy data from the receive ring to the event layer.
*/
get = ms->ms_rbget;
while (get != ms->ms_rbput) {
ring_data = ms->ms_rbuf[get];
get = (get + 1) & MS_RX_RING_MASK;
/* low byte of ring_data is rr1 */
c = (ring_data >> 8) & 0xff;
if (ring_data & ZSRR1_DO)
intr_flags |= INTR_RX_OVERRUN;
if (ring_data & (ZSRR1_FE | ZSRR1_PE)) {
log(LOG_ERR, "%s: input error (0x%x)\n",
ms->ms_dev.dv_xname, ring_data);
c = -1; /* signal input error */
}
/* Pass this up to the "middle" layer. */
ms_input(ms, c);
}
if (intr_flags & INTR_RX_OVERRUN) {
log(LOG_ERR, "%s: input overrun\n",
ms->ms_dev.dv_xname);
}
ms->ms_rbget = get;
if (intr_flags & INTR_TX_EMPTY) {
/*
* Transmit done. (Not expected.)
*/
log(LOG_ERR, "%s: transmit interrupt?\n",
ms->ms_dev.dv_xname);
}
if (intr_flags & INTR_ST_CHECK) {
/*
* Status line change. (Not expected.)
*/
log(LOG_ERR, "%s: status interrupt?\n",
ms->ms_dev.dv_xname);
cs->cs_rr0_delta = 0;
}
splx(s);
}
struct zsops zsops_ms = {
ms_rxint, /* receive char available */
ms_stint, /* external/status */
ms_txint, /* xmit buffer empty */
ms_softint, /* process software interrupt */
};
|