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
|
/*-
* Copyright (c) 1992, 1993 Erik Forsberg.
* 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.
*
* THIS SOFTWARE IS PROVIDED BY ``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 I 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.
*/
/*
* Ported to 386bsd Oct 17, 1992
* Sandi Donno, Computer Science, University of Cape Town, South Africa
* Please send bug reports to sandi@cs.uct.ac.za
*
* Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
* although I was only partially successful in getting the alpha release
* of his "driver for the Logitech and ATI Inport Bus mice for use with
* 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
* found his code to be an invaluable reference when porting this driver
* to 386bsd.
*
* Further modifications for latest 386BSD+patchkit and port to NetBSD,
* Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
*
* Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
* Andrew Herbert - 12 June 1993
*
* Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
* - 13 June 1993
*
* Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
* - 24 October 1993
*/
#include "psm.h"
#if NPSM > 0
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/proc.h>
#include <sys/conf.h>
#ifdef DEVFS
#include <sys/devfsext.h>
#endif /*DEVFS*/
#include <machine/mouse.h>
#include <i386/isa/isa_device.h>
#define DATA 0 /* Offset for data port, read-write */
#define CNTRL 4 /* Offset for control port, write-only */
#define STATUS 4 /* Offset for status port, read-only */
/* status bits */
#define PSM_OUTPUT_ACK 0x02 /* output acknowledge */
/* controller commands */
#define PSM_ENABLE 0xa8 /* enable auxiliary port */
#define PSM_DISABLE 0xa7 /* disable auxiliary port */
#define PSM_INT_ENABLE 0x47 /* enable controller interrupts */
#define PSM_INT_DISABLE 0x65 /* disable controller interrupts */
/* m+use commands */
#define PSM_SET_SCALE11 0xe6 /* set 1:1 scaling */
#define PSM_SET_SCALE21 0xe7 /* set 2:1 scaling */
#define PSM_SET_RES 0xe8 /* set resolution */
#define PSM_GET_SCALE 0xe9 /* set scaling factor */
#define PSM_SET_STREAM 0xea /* set streaming mode */
#define PSM_SET_SAMPLE 0xf3 /* set sampling rate */
#define PSM_DEV_ENABLE 0xf4 /* mouse on */
#define PSM_DEV_DISABLE 0xf5 /* mouse off */
#define PSM_RESET 0xff /* reset */
#define PSMUNIT(dev) (minor(dev) >> 1)
#ifndef min
#define min(x,y) (x < y ? x : y)
#endif min
static int psmprobe (struct isa_device *);
static int psmattach (struct isa_device *);
static void psm_poll_status(void);
static int psmaddr[NPSM]; /* Base I/O port addresses per unit */
#define MSBSZ 1024 /* Output queue size (pwr of 2 is best) */
struct ringbuf {
int count, first, last;
char queue[MSBSZ];
};
static struct psm_softc { /* Driver status information */
struct ringbuf inq; /* Input queue */
struct selinfo rsel; /* Process selecting for Input */
unsigned char state; /* Mouse driver state */
unsigned char status; /* Mouse button status */
unsigned char button; /* Previous mouse button status bits */
int x, y; /* accumulated motion in the X,Y axis */
#ifdef DEVFS
void *devfs_token;
void *n_devfs_token;
#endif
} psm_softc[NPSM];
#define OPEN 1 /* Device is open */
#define ASLP 2 /* Waiting for mouse data */
struct isa_driver psmdriver = { psmprobe, psmattach, "psm" };
static d_open_t psmopen;
static d_close_t psmclose;
static d_read_t psmread;
static d_ioctl_t psmioctl;
static d_select_t psmselect;
#define CDEV_MAJOR 21
static struct cdevsw psm_cdevsw =
{ psmopen, psmclose, psmread, nowrite, /*21*/
psmioctl, nostop, nullreset, nodevtotty,
psmselect, nommap, NULL, "psm", NULL, -1 };
#define AUX_PORT 0x60 /* AUX_PORT base (S.Yuen) */
static void psm_write_dev(int inport, u_char value)
{
psm_poll_status();
outb(inport+CNTRL, 0xd4);
psm_poll_status();
outb(inport+DATA,value);
}
static inline void psm_command(int ioport, u_char value)
{
psm_poll_status();
outb(ioport+CNTRL, 0x60);
psm_poll_status();
outb(ioport+DATA, value);
}
static int
psmprobe(struct isa_device *dvp)
{
/* XXX: Needs a real probe routine. */
int ioport,c,unit;
ioport=dvp->id_iobase;
unit=dvp->id_unit;
#ifndef PSM_NO_RESET
psm_write_dev(ioport,0xff); /* Reset aux device */
psm_poll_status();
#endif
outb(ioport+CNTRL,0xa9);
psm_poll_status();
outb(ioport+CNTRL,0xaa);
c = inb(ioport+DATA);
if(c&0x04) {
/* printf("PS/2 AUX mouse is not found\n");*/
psm_command(ioport,0x65);
psmaddr[unit] = 0; /* Device not found */
return(0);}
/* printf("PS/2 AUX mouse found. Installing driver\n");*/
return (4);
}
static int
psmattach(struct isa_device *dvp)
{
int unit = dvp->id_unit;
int ioport = dvp->id_iobase;
struct psm_softc *sc = &psm_softc[unit];
/* Save I/O base address */
psmaddr[unit] = ioport;
/* Disable mouse interrupts */
psm_poll_status();
outb(ioport+CNTRL, PSM_ENABLE);
#ifdef 0
psm_write(ioport, PSM_SET_RES);
psm_write(ioport, 0x03); /* 8 counts/mm */
psm_write(ioport, PSM_SET_SCALE);
psm_write(ioport, 0x02); /* 2:1 */
psm_write(ioport, PSM_SET_SCALE21);
psm_write(ioport, PSM_SET_SAMPLE);
psm_write(ioport, 0x64); /* 100 samples/sec */
psm_write(ioport, PSM_SET_STREAM);
#endif
psm_poll_status();
outb(ioport+CNTRL, PSM_DISABLE);
psm_command(ioport, PSM_INT_DISABLE);
/* Setup initial state */
sc->state = 0;
/* Done */
return(0); /* XXX eh? usually 1 indicates success */
}
static int
psmopen(dev_t dev, int flag, int fmt, struct proc *p)
{
int unit = PSMUNIT(dev);
struct psm_softc *sc;
int ioport;
char name[32];
/* Validate unit number */
if (unit >= NPSM)
return(ENXIO);
/* Get device data */
sc = &psm_softc[unit];
ioport = psmaddr[unit];
/* If device does not exist */
if (ioport == 0)
return(ENXIO);
/* Disallow multiple opens */
if (sc->state & OPEN)
return(EBUSY);
/* Initialize state */
sc->state |= OPEN;
sc->rsel.si_flags = 0;
sc->rsel.si_pid = 0;
sc->status = 0;
sc->button = 0;
sc->x = 0;
sc->y = 0;
/* Allocate and initialize a ring buffer */
sc->inq.count = sc->inq.first = sc->inq.last = 0;
/* Enable Bus Mouse interrupts */
psm_write_dev(ioport, PSM_DEV_ENABLE);
psm_poll_status();
outb(ioport+CNTRL, PSM_ENABLE);
psm_command(ioport, PSM_INT_ENABLE);
/* Successful open */
#ifdef DEVFS
sprintf(name,"psm%d", unit);
/* path name devsw minor */
sc->devfs_token = devfs_add_devsw( "/", name, &psm_cdevsw, unit << 1,
/*type uid gid perm*/
DV_CHR, 0, 0, 0666);
sprintf(name,"npsm%d", unit);
/* path name devsw minor */
sc->n_devfs_token = devfs_add_devsw("/", name, &psm_cdevsw, (unit<<1)+1,
/*type uid gid perm*/
DV_CHR, 0, 0, 0666);
#endif
return(0);
}
static void
psm_poll_status(void)
{
while(inb(AUX_PORT+STATUS)&0x03) {
if(inb(AUX_PORT+STATUS) & 0x2 == 0x2)
inb(AUX_PORT+DATA);}
return;
}
static int
psmclose(dev_t dev, int flag, int fmt, struct proc *p)
{
int unit, ioport;
struct psm_softc *sc;
/* Get unit and associated info */
unit = PSMUNIT(dev);
sc = &psm_softc[unit];
ioport = psmaddr[unit];
/* Disable further mouse interrupts */
psm_command(ioport,PSM_INT_DISABLE);
psm_poll_status();
outb(ioport+CNTRL,PSM_DISABLE );
/* Complete the close */
sc->state &= ~OPEN;
/* close is almost always successful */
return(0);
}
static int
psmread(dev_t dev, struct uio *uio, int flag)
{
int s;
int error = 0; /* keep compiler quiet, even though initialisation
is unnecessary */
unsigned length;
struct psm_softc *sc;
unsigned char buffer[100];
/* Get device information */
sc = &psm_softc[PSMUNIT(dev)];
/* Block until mouse activity occured */
s = spltty();
while (sc->inq.count == 0) {
if (minor(dev) & 0x1) {
splx(s);
return(EWOULDBLOCK);
}
sc->state |= ASLP;
error = tsleep((caddr_t)sc, PZERO | PCATCH, "psmrea", 0);
if (error != 0) {
splx(s);
return(error);
}
}
/* Transfer as many chunks as possible */
while (sc->inq.count > 0 && uio->uio_resid > 0) {
length = min(sc->inq.count, uio->uio_resid);
if (length > sizeof(buffer))
length = sizeof(buffer);
/* Remove a small chunk from input queue */
if (sc->inq.first + length >= MSBSZ) {
bcopy(&sc->inq.queue[sc->inq.first],
buffer, MSBSZ - sc->inq.first);
bcopy(sc->inq.queue, &buffer[MSBSZ-sc->inq.first],
length - (MSBSZ - sc->inq.first));
}
else
bcopy(&sc->inq.queue[sc->inq.first], buffer, length);
sc->inq.first = (sc->inq.first + length) % MSBSZ;
sc->inq.count -= length;
/* Copy data to user process */
error = uiomove(buffer, length, uio);
if (error)
break;
}
sc->x = sc->y = 0;
/* Allow interrupts again */
splx(s);
return(error);
}
static int
psmioctl(dev_t dev, int cmd, caddr_t addr, int flag, struct proc *p)
{
struct psm_softc *sc;
struct mouseinfo info;
int s, error;
/* Get device information */
sc = &psm_softc[PSMUNIT(dev)];
/* Perform IOCTL command */
switch (cmd) {
case MOUSEIOCREAD:
/* Don't modify info while calculating */
s = spltty();
/* Build mouse status octet */
info.status = sc->status;
if (sc->x || sc->y)
info.status |= MOVEMENT;
/* Encode X and Y motion as good as we can */
if (sc->x > 127)
info.xmotion = 127;
else if (sc->x < -128)
info.xmotion = -128;
else
info.xmotion = sc->x;
if (sc->y > 127)
info.ymotion = 127;
else if (sc->y < -128)
info.ymotion = -128;
else
info.ymotion = sc->y;
/* Reset historical information */
sc->x = 0;
sc->y = 0;
sc->status &= ~BUTCHNGMASK;
/* Allow interrupts and copy result buffer */
splx(s);
error = copyout(&info, addr, sizeof(struct mouseinfo));
break;
default:
error = EINVAL;
break;
}
/* Return error code */
return(error);
}
void psmintr(unit)
int unit;
{
struct psm_softc *sc = &psm_softc[unit];
int ioport = psmaddr[unit];
sc->inq.queue[sc->inq.last++ % MSBSZ] = inb(ioport+DATA);
sc->inq.count++;
if (sc -> state & ASLP) {
sc->state &= ~ASLP;
wakeup((caddr_t)sc);
}
selwakeup(&sc->rsel);
}
static int
psmselect(dev_t dev, int rw, struct proc *p)
{
int s, ret;
struct psm_softc *sc = &psm_softc[PSMUNIT(dev)];
/* Silly to select for output */
if (rw == FWRITE)
return(0);
/* Return true if a mouse event available */
s = spltty();
if (sc->inq.count)
ret = 1;
else {
selrecord(p, &sc->rsel);
ret = 0;
}
splx(s);
return(ret);
}
static psm_devsw_installed = 0;
static void psm_drvinit(void *unused)
{
dev_t dev;
if( ! psm_devsw_installed ) {
dev = makedev(CDEV_MAJOR, 0);
cdevsw_add(&dev,&psm_cdevsw, NULL);
psm_devsw_installed = 1;
}
}
SYSINIT(psmdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,psm_drvinit,NULL)
#endif
|