Deleted Added
full compact
mse.c (4259) mse.c (7430)
1/*
2 * Copyright 1992 by the University of Guelph
3 *
4 * Permission to use, copy and modify this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation.
10 * University of Guelph makes no representations about the suitability of
11 * this software for any purpose. It is provided "as is"
12 * without express or implied warranty.
13 *
1/*
2 * Copyright 1992 by the University of Guelph
3 *
4 * Permission to use, copy and modify this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation.
10 * University of Guelph makes no representations about the suitability of
11 * this software for any purpose. It is provided "as is"
12 * without express or implied warranty.
13 *
14 * $Id: mse.c,v 1.8 1994/10/23 21:27:31 wollman Exp $
14 * $Id: mse.c,v 1.9 1994/11/08 05:41:34 jkh Exp $
15 */
16/*
17 * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
18 * the X386 port, courtesy of
19 * Rick Macklem, rick@snowhite.cis.uoguelph.ca
20 * Caveats: The driver currently uses spltty(), but doesn't use any
21 * generic tty code. It could use splmse() (that only masks off the
22 * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
23 * (This may be worth the effort, since the Logitech generates 30/60
24 * interrupts/sec continuously while it is open.)
25 * NB: The ATI has NOT been tested yet!
26 */
27
28/*
29 * Modification history:
30 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
31 * improved probe based on input from Logitech.
32 *
33 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
34 * fixes to make it work with Microsoft InPort busmouse
35 *
36 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
37 * added patches for new "select" interface
38 *
39 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
40 * changed position of some spl()'s in mseread
41 *
42 * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
43 * limit maximum negative x/y value to -127 to work around XFree problem
44 * that causes spurious button pushes.
45 */
46
47#include "mse.h"
48#if NMSE > 0
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/proc.h>
52#include <sys/user.h>
53#include <sys/buf.h>
54#include <sys/kernel.h>
55#include <sys/ioctl.h>
56#include <sys/tty.h>
57#include <sys/uio.h>
58#include <sys/devconf.h>
59
15 */
16/*
17 * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
18 * the X386 port, courtesy of
19 * Rick Macklem, rick@snowhite.cis.uoguelph.ca
20 * Caveats: The driver currently uses spltty(), but doesn't use any
21 * generic tty code. It could use splmse() (that only masks off the
22 * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
23 * (This may be worth the effort, since the Logitech generates 30/60
24 * interrupts/sec continuously while it is open.)
25 * NB: The ATI has NOT been tested yet!
26 */
27
28/*
29 * Modification history:
30 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
31 * improved probe based on input from Logitech.
32 *
33 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
34 * fixes to make it work with Microsoft InPort busmouse
35 *
36 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
37 * added patches for new "select" interface
38 *
39 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
40 * changed position of some spl()'s in mseread
41 *
42 * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
43 * limit maximum negative x/y value to -127 to work around XFree problem
44 * that causes spurious button pushes.
45 */
46
47#include "mse.h"
48#if NMSE > 0
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/proc.h>
52#include <sys/user.h>
53#include <sys/buf.h>
54#include <sys/kernel.h>
55#include <sys/ioctl.h>
56#include <sys/tty.h>
57#include <sys/uio.h>
58#include <sys/devconf.h>
59
60#include <machine/clock.h>
61
60#include <i386/isa/isa_device.h>
61#include <i386/isa/icu.h>
62
63static int mseprobe(struct isa_device *);
64static int mseattach(struct isa_device *);
65void mseintr(int);
66
67struct isa_driver msedriver = {
68 mseprobe, mseattach, "mse"
69};
70
71/*
72 * Software control structure for mouse. The sc_enablemouse(),
73 * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
74 */
75#define PROTOBYTES 5
76struct mse_softc {
77 int sc_flags;
78 int sc_mousetype;
79 struct selinfo sc_selp;
80 u_int sc_port;
81 void (*sc_enablemouse)();
82 void (*sc_disablemouse)();
83 void (*sc_getmouse)();
84 int sc_deltax;
85 int sc_deltay;
86 int sc_obuttons;
87 int sc_buttons;
88 int sc_bytesread;
89 u_char sc_bytes[PROTOBYTES];
90} mse_sc[NMSE];
91
92/* Flags */
93#define MSESC_OPEN 0x1
94#define MSESC_WANT 0x2
95
96/* and Mouse Types */
97#define MSE_LOGITECH 0x1
98#define MSE_ATIINPORT 0x2
99#define MSE_LOGI_SIG 0xA5
100
101#define MSE_PORTA 0
102#define MSE_PORTB 1
103#define MSE_PORTC 2
104#define MSE_PORTD 3
105
106#define MSE_UNIT(dev) (minor(dev) >> 1)
107#define MSE_NBLOCKIO(dev) (minor(dev) & 0x1)
108
109/*
110 * Logitech bus mouse definitions
111 */
112#define MSE_SETUP 0x91 /* What does this mean? */
113 /* The definition for the control port */
114 /* is as follows: */
115
116 /* D7 = Mode set flag (1 = active) */
117 /* D6,D5 = Mode selection (port A) */
118 /* 00 = Mode 0 = Basic I/O */
119 /* 01 = Mode 1 = Strobed I/O */
120 /* 10 = Mode 2 = Bi-dir bus */
121 /* D4 = Port A direction (1 = input)*/
122 /* D3 = Port C (upper 4 bits) */
123 /* direction. (1 = input) */
124 /* D2 = Mode selection (port B & C) */
125 /* 0 = Mode 0 = Basic I/O */
126 /* 1 = Mode 1 = Strobed I/O */
127 /* D1 = Port B direction (1 = input)*/
128 /* D0 = Port C (lower 4 bits) */
129 /* direction. (1 = input) */
130
131 /* So 91 means Basic I/O on all 3 ports,*/
132 /* Port A is an input port, B is an */
133 /* output port, C is split with upper */
134 /* 4 bits being an output port and lower*/
135 /* 4 bits an input port, and enable the */
136 /* sucker. */
137 /* Courtesy Intel 8255 databook. Lars */
138#define MSE_HOLD 0x80
139#define MSE_RXLOW 0x00
140#define MSE_RXHIGH 0x20
141#define MSE_RYLOW 0x40
142#define MSE_RYHIGH 0x60
143#define MSE_DISINTR 0x10
144#define MSE_INTREN 0x00
145
146static int mse_probelogi();
147static void mse_enablelogi(), mse_disablelogi(), mse_getlogi();
148
149/*
150 * ATI Inport mouse definitions
151 */
152#define MSE_INPORT_RESET 0x80
153#define MSE_INPORT_STATUS 0x00
154#define MSE_INPORT_DX 0x01
155#define MSE_INPORT_DY 0x02
156#define MSE_INPORT_MODE 0x07
157#define MSE_INPORT_HOLD 0x20
158#define MSE_INPORT_INTREN 0x09
159
160static int mse_probeati();
161static void mse_enableati(), mse_disableati(), mse_getati();
162
163#define MSEPRI (PZERO + 3)
164
165/*
166 * Table of mouse types.
167 * Keep the Logitech last, since I haven't figured out how to probe it
168 * properly yet. (Someday I'll have the documentation.)
169 */
170struct mse_types {
171 int m_type; /* Type of bus mouse */
172 int (*m_probe)(); /* Probe routine to test for it */
173 void (*m_enable)(); /* Start routine */
174 void (*m_disable)(); /* Disable interrupts routine */
175 void (*m_get)(); /* and get mouse status */
176} mse_types[] = {
177 { MSE_ATIINPORT, mse_probeati, mse_enableati, mse_disableati, mse_getati },
178 { MSE_LOGITECH, mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi },
179 { 0, },
180};
181
182int
183mseprobe(idp)
184 register struct isa_device *idp;
185{
186 register struct mse_softc *sc = &mse_sc[idp->id_unit];
187 register int i;
188
189 /*
190 * Check for each mouse type in the table.
191 */
192 i = 0;
193 while (mse_types[i].m_type) {
194 if ((*mse_types[i].m_probe)(idp)) {
195 sc->sc_mousetype = mse_types[i].m_type;
196 sc->sc_enablemouse = mse_types[i].m_enable;
197 sc->sc_disablemouse = mse_types[i].m_disable;
198 sc->sc_getmouse = mse_types[i].m_get;
199 return (1);
200 }
201 i++;
202 }
203 return (0);
204}
205
206static struct kern_devconf kdc_mse[NMSE] = { {
207 0, 0, 0, /* filled in by dev_attach */
208 "mse", 0, { MDDT_ISA, 0, "tty" },
209 isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
210 &kdc_isa0, /* parent */
211 0, /* parentdata */
212 DC_UNKNOWN, /* not supported */
213 "ATI or Logitech bus mouse adapter"
214} };
215
216static inline void
217mse_registerdev(struct isa_device *id)
218{
219 if(id->id_unit)
220 kdc_mse[id->id_unit] = kdc_mse[0];
221 kdc_mse[id->id_unit].kdc_unit = id->id_unit;
222 kdc_mse[id->id_unit].kdc_isa = id;
223 dev_attach(&kdc_mse[id->id_unit]);
224}
225
226int
227mseattach(idp)
228 struct isa_device *idp;
229{
230 struct mse_softc *sc = &mse_sc[idp->id_unit];
231
232 sc->sc_port = idp->id_iobase;
233 mse_registerdev(idp);
234 return (1);
235}
236
237/*
238 * Exclusive open the mouse, initialize it and enable interrupts.
239 */
240int
241mseopen(dev, flag)
242 dev_t dev;
243 int flag;
244{
245 register struct mse_softc *sc;
246 int s;
247
248 if (MSE_UNIT(dev) >= NMSE)
249 return (ENXIO);
250 sc = &mse_sc[MSE_UNIT(dev)];
251 if (sc->sc_flags & MSESC_OPEN)
252 return (EBUSY);
253 sc->sc_flags |= MSESC_OPEN;
254 sc->sc_obuttons = sc->sc_buttons = 0x7;
255 sc->sc_deltax = sc->sc_deltay = 0;
256 sc->sc_bytesread = PROTOBYTES;
257
258 /*
259 * Initialize mouse interface and enable interrupts.
260 */
261 s = spltty();
262 (*sc->sc_enablemouse)(sc->sc_port);
263 splx(s);
264 return (0);
265}
266
267/*
268 * mseclose: just turn off mouse innterrupts.
269 */
270int
271mseclose(dev, flag)
272 dev_t dev;
273 int flag;
274{
275 struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
276 int s;
277
278 s = spltty();
279 (*sc->sc_disablemouse)(sc->sc_port);
280 sc->sc_flags &= ~MSESC_OPEN;
281 splx(s);
282 return(0);
283}
284
285/*
286 * mseread: return mouse info using the MSC serial protocol, but without
287 * using bytes 4 and 5.
288 * (Yes this is cheesy, but it makes the X386 server happy, so...)
289 */
290int
291mseread(dev, uio)
292 dev_t dev;
293 struct uio *uio;
294{
295 register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
296 int xfer, s, error;
297
298 /*
299 * If there are no protocol bytes to be read, set up a new protocol
300 * packet.
301 */
302 s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
303 if (sc->sc_bytesread >= PROTOBYTES) {
304 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
305 (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
306 if (MSE_NBLOCKIO(dev)) {
307 splx(s);
308 return (0);
309 }
310 sc->sc_flags |= MSESC_WANT;
311 if (error = tsleep((caddr_t)sc, MSEPRI | PCATCH,
312 "mseread", 0)) {
313 splx(s);
314 return (error);
315 }
316 }
317
318 /*
319 * Generate protocol bytes.
320 * For some reason X386 expects 5 bytes but never uses
321 * the fourth or fifth?
322 */
323 sc->sc_bytes[0] = 0x80 | (sc->sc_buttons & ~0xf8);
324 if (sc->sc_deltax > 127)
325 sc->sc_deltax = 127;
326 if (sc->sc_deltax < -127)
327 sc->sc_deltax = -127;
328 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
329 if (sc->sc_deltay > 127)
330 sc->sc_deltay = 127;
331 if (sc->sc_deltay < -127)
332 sc->sc_deltay = -127;
333 sc->sc_bytes[1] = sc->sc_deltax;
334 sc->sc_bytes[2] = sc->sc_deltay;
335 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
336 sc->sc_obuttons = sc->sc_buttons;
337 sc->sc_deltax = sc->sc_deltay = 0;
338 sc->sc_bytesread = 0;
339 }
340 splx(s);
341 xfer = min(uio->uio_resid, PROTOBYTES - sc->sc_bytesread);
342 if (error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio))
343 return (error);
344 sc->sc_bytesread += xfer;
345 return(0);
346}
347
348/*
349 * mseselect: check for mouse input to be processed.
350 */
351int
352mseselect(dev, rw, p)
353 dev_t dev;
354 int rw;
355 struct proc *p;
356{
357 register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
358 int s;
359
360 s = spltty();
361 if (sc->sc_bytesread != PROTOBYTES || sc->sc_deltax != 0 ||
362 sc->sc_deltay != 0 || (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
363 splx(s);
364 return (1);
365 }
366
367 /*
368 * Since this is an exclusive open device, any previous proc.
369 * pointer is trash now, so we can just assign it.
370 */
371 selrecord(p, &sc->sc_selp);
372 splx(s);
373 return (0);
374}
375
376/*
377 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
378 */
379void
380mseintr(unit)
381 int unit;
382{
383 register struct mse_softc *sc = &mse_sc[unit];
384 pid_t p;
385
386#ifdef DEBUG
387 static int mse_intrcnt = 0;
388 if((mse_intrcnt++ % 10000) == 0)
389 printf("mseintr\n");
390#endif /* DEBUG */
391 if ((sc->sc_flags & MSESC_OPEN) == 0)
392 return;
393
394 (*sc->sc_getmouse)(sc->sc_port, &sc->sc_deltax, &sc->sc_deltay, &sc->sc_buttons);
395
396 /*
397 * If mouse state has changed, wake up anyone wanting to know.
398 */
399 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
400 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
401 if (sc->sc_flags & MSESC_WANT) {
402 sc->sc_flags &= ~MSESC_WANT;
403 wakeup((caddr_t)sc);
404 }
405 selwakeup(&sc->sc_selp);
406 }
407}
408
409/*
410 * Routines for the Logitech mouse.
411 */
412/*
413 * Test for a Logitech bus mouse and return 1 if it is.
414 * (until I know how to use the signature port properly, just disable
415 * interrupts and return 1)
416 */
417static int
418mse_probelogi(idp)
419 register struct isa_device *idp;
420{
421
422 int sig;
423
424 outb(idp->id_iobase + MSE_PORTD, MSE_SETUP);
425 /* set the signature port */
426 outb(idp->id_iobase + MSE_PORTB, MSE_LOGI_SIG);
427
428 DELAY(30000); /* 30 ms delay */
429 sig = inb(idp->id_iobase + MSE_PORTB) & 0xFF;
430 if (sig == MSE_LOGI_SIG) {
431 outb(idp->id_iobase + MSE_PORTC, MSE_DISINTR);
432 return(1);
433 } else {
434 printf("mse%d: wrong signature %x\n",idp->id_unit,sig);
435 return(0);
436 }
437}
438
439/*
440 * Initialize Logitech mouse and enable interrupts.
441 */
442static void
443mse_enablelogi(port)
444 register u_int port;
445{
446 int dx, dy, but;
447
448 outb(port + MSE_PORTD, MSE_SETUP);
449 mse_getlogi(port, &dx, &dy, &but);
450}
451
452/*
453 * Disable interrupts for Logitech mouse.
454 */
455static void
456mse_disablelogi(port)
457 register u_int port;
458{
459
460 outb(port + MSE_PORTC, MSE_DISINTR);
461}
462
463/*
464 * Get the current dx, dy and button up/down state.
465 */
466static void
467mse_getlogi(port, dx, dy, but)
468 register u_int port;
469 int *dx;
470 int *dy;
471 int *but;
472{
473 register char x, y;
474
475 outb(port + MSE_PORTC, MSE_HOLD | MSE_RXLOW);
476 x = inb(port + MSE_PORTA);
477 *but = (x >> 5) & 0x7;
478 x &= 0xf;
479 outb(port + MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
480 x |= (inb(port + MSE_PORTA) << 4);
481 outb(port + MSE_PORTC, MSE_HOLD | MSE_RYLOW);
482 y = (inb(port + MSE_PORTA) & 0xf);
483 outb(port + MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
484 y |= (inb(port + MSE_PORTA) << 4);
485 *dx += x;
486 *dy += y;
487 outb(port + MSE_PORTC, MSE_INTREN);
488}
489
490/*
491 * Routines for the ATI Inport bus mouse.
492 */
493/*
494 * Test for a ATI Inport bus mouse and return 1 if it is.
495 * (do not enable interrupts)
496 */
497static int
498mse_probeati(idp)
499 register struct isa_device *idp;
500{
501 int i;
502
503 for (i = 0; i < 2; i++)
504 if (inb(idp->id_iobase + MSE_PORTC) == 0xde)
505 return (1);
506 return (0);
507}
508
509/*
510 * Initialize ATI Inport mouse and enable interrupts.
511 */
512static void
513mse_enableati(port)
514 register u_int port;
515{
516
517 outb(port + MSE_PORTA, MSE_INPORT_RESET);
518 outb(port + MSE_PORTA, MSE_INPORT_MODE);
519 outb(port + MSE_PORTB, MSE_INPORT_INTREN);
520}
521
522/*
523 * Disable interrupts for ATI Inport mouse.
524 */
525static void
526mse_disableati(port)
527 register u_int port;
528{
529
530 outb(port + MSE_PORTA, MSE_INPORT_MODE);
531 outb(port + MSE_PORTB, 0);
532}
533
534/*
535 * Get current dx, dy and up/down button state.
536 */
537static void
538mse_getati(port, dx, dy, but)
539 register u_int port;
540 int *dx;
541 int *dy;
542 int *but;
543{
544 register char byte;
545
546 outb(port + MSE_PORTA, MSE_INPORT_MODE);
547 outb(port + MSE_PORTB, MSE_INPORT_HOLD);
548 outb(port + MSE_PORTA, MSE_INPORT_STATUS);
549 *but = ~(inb(port + MSE_PORTB) & 0x7);
550 outb(port + MSE_PORTA, MSE_INPORT_DX);
551 byte = inb(port + MSE_PORTB);
552 *dx += byte;
553 outb(port + MSE_PORTA, MSE_INPORT_DY);
554 byte = inb(port + MSE_PORTB);
555 *dy += byte;
556 outb(port + MSE_PORTA, MSE_INPORT_MODE);
557 outb(port + MSE_PORTB, MSE_INPORT_INTREN);
558}
559#endif /* NMSE */
62#include <i386/isa/isa_device.h>
63#include <i386/isa/icu.h>
64
65static int mseprobe(struct isa_device *);
66static int mseattach(struct isa_device *);
67void mseintr(int);
68
69struct isa_driver msedriver = {
70 mseprobe, mseattach, "mse"
71};
72
73/*
74 * Software control structure for mouse. The sc_enablemouse(),
75 * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
76 */
77#define PROTOBYTES 5
78struct mse_softc {
79 int sc_flags;
80 int sc_mousetype;
81 struct selinfo sc_selp;
82 u_int sc_port;
83 void (*sc_enablemouse)();
84 void (*sc_disablemouse)();
85 void (*sc_getmouse)();
86 int sc_deltax;
87 int sc_deltay;
88 int sc_obuttons;
89 int sc_buttons;
90 int sc_bytesread;
91 u_char sc_bytes[PROTOBYTES];
92} mse_sc[NMSE];
93
94/* Flags */
95#define MSESC_OPEN 0x1
96#define MSESC_WANT 0x2
97
98/* and Mouse Types */
99#define MSE_LOGITECH 0x1
100#define MSE_ATIINPORT 0x2
101#define MSE_LOGI_SIG 0xA5
102
103#define MSE_PORTA 0
104#define MSE_PORTB 1
105#define MSE_PORTC 2
106#define MSE_PORTD 3
107
108#define MSE_UNIT(dev) (minor(dev) >> 1)
109#define MSE_NBLOCKIO(dev) (minor(dev) & 0x1)
110
111/*
112 * Logitech bus mouse definitions
113 */
114#define MSE_SETUP 0x91 /* What does this mean? */
115 /* The definition for the control port */
116 /* is as follows: */
117
118 /* D7 = Mode set flag (1 = active) */
119 /* D6,D5 = Mode selection (port A) */
120 /* 00 = Mode 0 = Basic I/O */
121 /* 01 = Mode 1 = Strobed I/O */
122 /* 10 = Mode 2 = Bi-dir bus */
123 /* D4 = Port A direction (1 = input)*/
124 /* D3 = Port C (upper 4 bits) */
125 /* direction. (1 = input) */
126 /* D2 = Mode selection (port B & C) */
127 /* 0 = Mode 0 = Basic I/O */
128 /* 1 = Mode 1 = Strobed I/O */
129 /* D1 = Port B direction (1 = input)*/
130 /* D0 = Port C (lower 4 bits) */
131 /* direction. (1 = input) */
132
133 /* So 91 means Basic I/O on all 3 ports,*/
134 /* Port A is an input port, B is an */
135 /* output port, C is split with upper */
136 /* 4 bits being an output port and lower*/
137 /* 4 bits an input port, and enable the */
138 /* sucker. */
139 /* Courtesy Intel 8255 databook. Lars */
140#define MSE_HOLD 0x80
141#define MSE_RXLOW 0x00
142#define MSE_RXHIGH 0x20
143#define MSE_RYLOW 0x40
144#define MSE_RYHIGH 0x60
145#define MSE_DISINTR 0x10
146#define MSE_INTREN 0x00
147
148static int mse_probelogi();
149static void mse_enablelogi(), mse_disablelogi(), mse_getlogi();
150
151/*
152 * ATI Inport mouse definitions
153 */
154#define MSE_INPORT_RESET 0x80
155#define MSE_INPORT_STATUS 0x00
156#define MSE_INPORT_DX 0x01
157#define MSE_INPORT_DY 0x02
158#define MSE_INPORT_MODE 0x07
159#define MSE_INPORT_HOLD 0x20
160#define MSE_INPORT_INTREN 0x09
161
162static int mse_probeati();
163static void mse_enableati(), mse_disableati(), mse_getati();
164
165#define MSEPRI (PZERO + 3)
166
167/*
168 * Table of mouse types.
169 * Keep the Logitech last, since I haven't figured out how to probe it
170 * properly yet. (Someday I'll have the documentation.)
171 */
172struct mse_types {
173 int m_type; /* Type of bus mouse */
174 int (*m_probe)(); /* Probe routine to test for it */
175 void (*m_enable)(); /* Start routine */
176 void (*m_disable)(); /* Disable interrupts routine */
177 void (*m_get)(); /* and get mouse status */
178} mse_types[] = {
179 { MSE_ATIINPORT, mse_probeati, mse_enableati, mse_disableati, mse_getati },
180 { MSE_LOGITECH, mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi },
181 { 0, },
182};
183
184int
185mseprobe(idp)
186 register struct isa_device *idp;
187{
188 register struct mse_softc *sc = &mse_sc[idp->id_unit];
189 register int i;
190
191 /*
192 * Check for each mouse type in the table.
193 */
194 i = 0;
195 while (mse_types[i].m_type) {
196 if ((*mse_types[i].m_probe)(idp)) {
197 sc->sc_mousetype = mse_types[i].m_type;
198 sc->sc_enablemouse = mse_types[i].m_enable;
199 sc->sc_disablemouse = mse_types[i].m_disable;
200 sc->sc_getmouse = mse_types[i].m_get;
201 return (1);
202 }
203 i++;
204 }
205 return (0);
206}
207
208static struct kern_devconf kdc_mse[NMSE] = { {
209 0, 0, 0, /* filled in by dev_attach */
210 "mse", 0, { MDDT_ISA, 0, "tty" },
211 isa_generic_externalize, 0, 0, ISA_EXTERNALLEN,
212 &kdc_isa0, /* parent */
213 0, /* parentdata */
214 DC_UNKNOWN, /* not supported */
215 "ATI or Logitech bus mouse adapter"
216} };
217
218static inline void
219mse_registerdev(struct isa_device *id)
220{
221 if(id->id_unit)
222 kdc_mse[id->id_unit] = kdc_mse[0];
223 kdc_mse[id->id_unit].kdc_unit = id->id_unit;
224 kdc_mse[id->id_unit].kdc_isa = id;
225 dev_attach(&kdc_mse[id->id_unit]);
226}
227
228int
229mseattach(idp)
230 struct isa_device *idp;
231{
232 struct mse_softc *sc = &mse_sc[idp->id_unit];
233
234 sc->sc_port = idp->id_iobase;
235 mse_registerdev(idp);
236 return (1);
237}
238
239/*
240 * Exclusive open the mouse, initialize it and enable interrupts.
241 */
242int
243mseopen(dev, flag)
244 dev_t dev;
245 int flag;
246{
247 register struct mse_softc *sc;
248 int s;
249
250 if (MSE_UNIT(dev) >= NMSE)
251 return (ENXIO);
252 sc = &mse_sc[MSE_UNIT(dev)];
253 if (sc->sc_flags & MSESC_OPEN)
254 return (EBUSY);
255 sc->sc_flags |= MSESC_OPEN;
256 sc->sc_obuttons = sc->sc_buttons = 0x7;
257 sc->sc_deltax = sc->sc_deltay = 0;
258 sc->sc_bytesread = PROTOBYTES;
259
260 /*
261 * Initialize mouse interface and enable interrupts.
262 */
263 s = spltty();
264 (*sc->sc_enablemouse)(sc->sc_port);
265 splx(s);
266 return (0);
267}
268
269/*
270 * mseclose: just turn off mouse innterrupts.
271 */
272int
273mseclose(dev, flag)
274 dev_t dev;
275 int flag;
276{
277 struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
278 int s;
279
280 s = spltty();
281 (*sc->sc_disablemouse)(sc->sc_port);
282 sc->sc_flags &= ~MSESC_OPEN;
283 splx(s);
284 return(0);
285}
286
287/*
288 * mseread: return mouse info using the MSC serial protocol, but without
289 * using bytes 4 and 5.
290 * (Yes this is cheesy, but it makes the X386 server happy, so...)
291 */
292int
293mseread(dev, uio)
294 dev_t dev;
295 struct uio *uio;
296{
297 register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
298 int xfer, s, error;
299
300 /*
301 * If there are no protocol bytes to be read, set up a new protocol
302 * packet.
303 */
304 s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
305 if (sc->sc_bytesread >= PROTOBYTES) {
306 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
307 (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
308 if (MSE_NBLOCKIO(dev)) {
309 splx(s);
310 return (0);
311 }
312 sc->sc_flags |= MSESC_WANT;
313 if (error = tsleep((caddr_t)sc, MSEPRI | PCATCH,
314 "mseread", 0)) {
315 splx(s);
316 return (error);
317 }
318 }
319
320 /*
321 * Generate protocol bytes.
322 * For some reason X386 expects 5 bytes but never uses
323 * the fourth or fifth?
324 */
325 sc->sc_bytes[0] = 0x80 | (sc->sc_buttons & ~0xf8);
326 if (sc->sc_deltax > 127)
327 sc->sc_deltax = 127;
328 if (sc->sc_deltax < -127)
329 sc->sc_deltax = -127;
330 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
331 if (sc->sc_deltay > 127)
332 sc->sc_deltay = 127;
333 if (sc->sc_deltay < -127)
334 sc->sc_deltay = -127;
335 sc->sc_bytes[1] = sc->sc_deltax;
336 sc->sc_bytes[2] = sc->sc_deltay;
337 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
338 sc->sc_obuttons = sc->sc_buttons;
339 sc->sc_deltax = sc->sc_deltay = 0;
340 sc->sc_bytesread = 0;
341 }
342 splx(s);
343 xfer = min(uio->uio_resid, PROTOBYTES - sc->sc_bytesread);
344 if (error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio))
345 return (error);
346 sc->sc_bytesread += xfer;
347 return(0);
348}
349
350/*
351 * mseselect: check for mouse input to be processed.
352 */
353int
354mseselect(dev, rw, p)
355 dev_t dev;
356 int rw;
357 struct proc *p;
358{
359 register struct mse_softc *sc = &mse_sc[MSE_UNIT(dev)];
360 int s;
361
362 s = spltty();
363 if (sc->sc_bytesread != PROTOBYTES || sc->sc_deltax != 0 ||
364 sc->sc_deltay != 0 || (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
365 splx(s);
366 return (1);
367 }
368
369 /*
370 * Since this is an exclusive open device, any previous proc.
371 * pointer is trash now, so we can just assign it.
372 */
373 selrecord(p, &sc->sc_selp);
374 splx(s);
375 return (0);
376}
377
378/*
379 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
380 */
381void
382mseintr(unit)
383 int unit;
384{
385 register struct mse_softc *sc = &mse_sc[unit];
386 pid_t p;
387
388#ifdef DEBUG
389 static int mse_intrcnt = 0;
390 if((mse_intrcnt++ % 10000) == 0)
391 printf("mseintr\n");
392#endif /* DEBUG */
393 if ((sc->sc_flags & MSESC_OPEN) == 0)
394 return;
395
396 (*sc->sc_getmouse)(sc->sc_port, &sc->sc_deltax, &sc->sc_deltay, &sc->sc_buttons);
397
398 /*
399 * If mouse state has changed, wake up anyone wanting to know.
400 */
401 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
402 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
403 if (sc->sc_flags & MSESC_WANT) {
404 sc->sc_flags &= ~MSESC_WANT;
405 wakeup((caddr_t)sc);
406 }
407 selwakeup(&sc->sc_selp);
408 }
409}
410
411/*
412 * Routines for the Logitech mouse.
413 */
414/*
415 * Test for a Logitech bus mouse and return 1 if it is.
416 * (until I know how to use the signature port properly, just disable
417 * interrupts and return 1)
418 */
419static int
420mse_probelogi(idp)
421 register struct isa_device *idp;
422{
423
424 int sig;
425
426 outb(idp->id_iobase + MSE_PORTD, MSE_SETUP);
427 /* set the signature port */
428 outb(idp->id_iobase + MSE_PORTB, MSE_LOGI_SIG);
429
430 DELAY(30000); /* 30 ms delay */
431 sig = inb(idp->id_iobase + MSE_PORTB) & 0xFF;
432 if (sig == MSE_LOGI_SIG) {
433 outb(idp->id_iobase + MSE_PORTC, MSE_DISINTR);
434 return(1);
435 } else {
436 printf("mse%d: wrong signature %x\n",idp->id_unit,sig);
437 return(0);
438 }
439}
440
441/*
442 * Initialize Logitech mouse and enable interrupts.
443 */
444static void
445mse_enablelogi(port)
446 register u_int port;
447{
448 int dx, dy, but;
449
450 outb(port + MSE_PORTD, MSE_SETUP);
451 mse_getlogi(port, &dx, &dy, &but);
452}
453
454/*
455 * Disable interrupts for Logitech mouse.
456 */
457static void
458mse_disablelogi(port)
459 register u_int port;
460{
461
462 outb(port + MSE_PORTC, MSE_DISINTR);
463}
464
465/*
466 * Get the current dx, dy and button up/down state.
467 */
468static void
469mse_getlogi(port, dx, dy, but)
470 register u_int port;
471 int *dx;
472 int *dy;
473 int *but;
474{
475 register char x, y;
476
477 outb(port + MSE_PORTC, MSE_HOLD | MSE_RXLOW);
478 x = inb(port + MSE_PORTA);
479 *but = (x >> 5) & 0x7;
480 x &= 0xf;
481 outb(port + MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
482 x |= (inb(port + MSE_PORTA) << 4);
483 outb(port + MSE_PORTC, MSE_HOLD | MSE_RYLOW);
484 y = (inb(port + MSE_PORTA) & 0xf);
485 outb(port + MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
486 y |= (inb(port + MSE_PORTA) << 4);
487 *dx += x;
488 *dy += y;
489 outb(port + MSE_PORTC, MSE_INTREN);
490}
491
492/*
493 * Routines for the ATI Inport bus mouse.
494 */
495/*
496 * Test for a ATI Inport bus mouse and return 1 if it is.
497 * (do not enable interrupts)
498 */
499static int
500mse_probeati(idp)
501 register struct isa_device *idp;
502{
503 int i;
504
505 for (i = 0; i < 2; i++)
506 if (inb(idp->id_iobase + MSE_PORTC) == 0xde)
507 return (1);
508 return (0);
509}
510
511/*
512 * Initialize ATI Inport mouse and enable interrupts.
513 */
514static void
515mse_enableati(port)
516 register u_int port;
517{
518
519 outb(port + MSE_PORTA, MSE_INPORT_RESET);
520 outb(port + MSE_PORTA, MSE_INPORT_MODE);
521 outb(port + MSE_PORTB, MSE_INPORT_INTREN);
522}
523
524/*
525 * Disable interrupts for ATI Inport mouse.
526 */
527static void
528mse_disableati(port)
529 register u_int port;
530{
531
532 outb(port + MSE_PORTA, MSE_INPORT_MODE);
533 outb(port + MSE_PORTB, 0);
534}
535
536/*
537 * Get current dx, dy and up/down button state.
538 */
539static void
540mse_getati(port, dx, dy, but)
541 register u_int port;
542 int *dx;
543 int *dy;
544 int *but;
545{
546 register char byte;
547
548 outb(port + MSE_PORTA, MSE_INPORT_MODE);
549 outb(port + MSE_PORTB, MSE_INPORT_HOLD);
550 outb(port + MSE_PORTA, MSE_INPORT_STATUS);
551 *but = ~(inb(port + MSE_PORTB) & 0x7);
552 outb(port + MSE_PORTA, MSE_INPORT_DX);
553 byte = inb(port + MSE_PORTB);
554 *dx += byte;
555 outb(port + MSE_PORTA, MSE_INPORT_DY);
556 byte = inb(port + MSE_PORTB);
557 *dy += byte;
558 outb(port + MSE_PORTA, MSE_INPORT_MODE);
559 outb(port + MSE_PORTB, MSE_INPORT_INTREN);
560}
561#endif /* NMSE */