Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / misc / mse / mse.c
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  * $FreeBSD: src/sys/i386/isa/mse.c,v 1.49.2.1 2000/03/20 13:58:47 yokota Exp $
15  * $DragonFly: src/sys/dev/misc/mse/mse.c,v 1.2 2003/06/17 04:28:37 dillon Exp $
16  */
17 /*
18  * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
19  * the X386 port, courtesy of
20  * Rick Macklem, rick@snowhite.cis.uoguelph.ca
21  * Caveats: The driver currently uses spltty(), but doesn't use any
22  * generic tty code. It could use splmse() (that only masks off the
23  * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
24  * (This may be worth the effort, since the Logitech generates 30/60
25  * interrupts/sec continuously while it is open.)
26  * NB: The ATI has NOT been tested yet!
27  */
28
29 /*
30  * Modification history:
31  * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
32  *   improved probe based on input from Logitech.
33  *
34  * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
35  *   fixes to make it work with Microsoft InPort busmouse
36  *
37  * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
38  *   added patches for new "select" interface
39  *
40  * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
41  *   changed position of some spl()'s in mseread
42  *
43  * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
44  *   limit maximum negative x/y value to -127 to work around XFree problem
45  *   that causes spurious button pushes.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/kernel.h>
52 #include <sys/bus.h>
53 #include <sys/poll.h>
54 #include <sys/select.h>
55 #include <sys/uio.h>
56
57 #include <machine/bus_pio.h>
58 #include <machine/bus.h>
59 #include <machine/clock.h>
60 #include <machine/mouse.h>
61 #include <machine/resource.h>
62 #include <sys/rman.h>
63
64 #include <isa/isavar.h>
65
66 /* driver configuration flags (config) */
67 #define MSE_CONFIG_ACCEL        0x00f0  /* acceleration factor */
68 #define MSE_CONFIG_FLAGS        (MSE_CONFIG_ACCEL)
69
70 /*
71  * Software control structure for mouse. The sc_enablemouse(),
72  * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
73  */
74 typedef struct mse_softc {
75         int             sc_flags;
76         int             sc_mousetype;
77         struct selinfo  sc_selp;
78         struct resource *sc_port;
79         struct resource *sc_intr;
80         bus_space_tag_t sc_iot;
81         bus_space_handle_t sc_ioh;
82         void            *sc_ih;
83         void            (*sc_enablemouse) __P((bus_space_tag_t t,
84                                                bus_space_handle_t h));
85         void            (*sc_disablemouse) __P((bus_space_tag_t t,
86                                                 bus_space_handle_t h));
87         void            (*sc_getmouse) __P((bus_space_tag_t t,
88                                             bus_space_handle_t h,
89                                             int *dx, int *dy, int *but));
90         int             sc_deltax;
91         int             sc_deltay;
92         int             sc_obuttons;
93         int             sc_buttons;
94         int             sc_bytesread;
95         u_char          sc_bytes[MOUSE_SYS_PACKETSIZE];
96         struct          callout_handle sc_callout;
97         int             sc_watchdog;
98         dev_t           sc_dev;
99         dev_t           sc_ndev;
100         mousehw_t       hw;
101         mousemode_t     mode;
102         mousestatus_t   status;
103 } mse_softc_t;
104
105 static  devclass_t      mse_devclass;
106
107 static  int             mse_probe __P((device_t dev));
108 static  int             mse_attach __P((device_t dev));
109 static  int             mse_detach __P((device_t dev));
110
111 static  device_method_t mse_methods[] = {
112         DEVMETHOD(device_probe,         mse_probe),
113         DEVMETHOD(device_attach,        mse_attach),
114         DEVMETHOD(device_detach,        mse_detach),
115         { 0, 0 }
116 };
117
118 static  driver_t        mse_driver = {
119         "mse",
120         mse_methods,
121         sizeof(mse_softc_t),
122 };
123
124 DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
125
126 static struct isa_pnp_id mse_ids[] = {
127         { 0x000fd041, "Bus mouse" },                    /* PNP0F00 */
128         { 0x020fd041, "InPort mouse" },                 /* PNP0F02 */
129         { 0x0d0fd041, "InPort mouse compatible" },      /* PNP0F0D */
130         { 0x110fd041, "Bus mouse compatible" },         /* PNP0F11 */
131         { 0x150fd041, "Logitech bus mouse" },           /* PNP0F15 */
132         { 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */
133         { 0 }
134 };
135
136 static  d_open_t        mseopen;
137 static  d_close_t       mseclose;
138 static  d_read_t        mseread;
139 static  d_ioctl_t       mseioctl;
140 static  d_poll_t        msepoll;
141
142 #define CDEV_MAJOR 27
143 static struct cdevsw mse_cdevsw = {
144         /* open */      mseopen,
145         /* close */     mseclose,
146         /* read */      mseread,
147         /* write */     nowrite,
148         /* ioctl */     mseioctl,
149         /* poll */      msepoll,
150         /* mmap */      nommap,
151         /* strategy */  nostrategy,
152         /* name */      "mse",
153         /* maj */       CDEV_MAJOR,
154         /* dump */      nodump,
155         /* psize */     nopsize,
156         /* flags */     0,
157         /* bmaj */      -1
158 };
159
160 static  void            mseintr __P((void *));
161 static  timeout_t       msetimeout;
162
163 /* Flags */
164 #define MSESC_OPEN      0x1
165 #define MSESC_WANT      0x2
166
167 /* and Mouse Types */
168 #define MSE_NONE        0       /* don't move this! */
169 #define MSE_LOGITECH    0x1
170 #define MSE_ATIINPORT   0x2
171 #define MSE_LOGI_SIG    0xA5
172
173 #define MSE_PORTA       0
174 #define MSE_PORTB       1
175 #define MSE_PORTC       2
176 #define MSE_PORTD       3
177 #define MSE_IOSIZE      4
178
179 #define MSE_UNIT(dev)           (minor(dev) >> 1)
180 #define MSE_NBLOCKIO(dev)       (minor(dev) & 0x1)
181
182 /*
183  * Logitech bus mouse definitions
184  */
185 #define MSE_SETUP       0x91    /* What does this mean? */
186                                 /* The definition for the control port */
187                                 /* is as follows: */
188
189                                 /* D7    =  Mode set flag (1 = active)  */
190                                 /* D6,D5 =  Mode selection (port A)     */
191                                 /*          00 = Mode 0 = Basic I/O     */
192                                 /*          01 = Mode 1 = Strobed I/O   */
193                                 /*          10 = Mode 2 = Bi-dir bus    */
194                                 /* D4    =  Port A direction (1 = input)*/
195                                 /* D3    =  Port C (upper 4 bits)       */
196                                 /*          direction. (1 = input)      */
197                                 /* D2    =  Mode selection (port B & C) */
198                                 /*          0 = Mode 0 = Basic I/O      */
199                                 /*          1 = Mode 1 = Strobed I/O    */
200                                 /* D1    =  Port B direction (1 = input)*/
201                                 /* D0    =  Port C (lower 4 bits)       */
202                                 /*          direction. (1 = input)      */
203
204                                 /* So 91 means Basic I/O on all 3 ports,*/
205                                 /* Port A is an input port, B is an     */
206                                 /* output port, C is split with upper   */
207                                 /* 4 bits being an output port and lower*/
208                                 /* 4 bits an input port, and enable the */
209                                 /* sucker.                              */
210                                 /* Courtesy Intel 8255 databook. Lars   */
211 #define MSE_HOLD        0x80
212 #define MSE_RXLOW       0x00
213 #define MSE_RXHIGH      0x20
214 #define MSE_RYLOW       0x40
215 #define MSE_RYHIGH      0x60
216 #define MSE_DISINTR     0x10
217 #define MSE_INTREN      0x00
218
219 static  int             mse_probelogi __P((device_t dev, mse_softc_t *sc));
220 static  void            mse_disablelogi __P((bus_space_tag_t t,
221                                              bus_space_handle_t h));
222 static  void            mse_getlogi __P((bus_space_tag_t t,
223                                          bus_space_handle_t h,
224                                          int *dx, int *dy, int *but));
225 static  void            mse_enablelogi __P((bus_space_tag_t t,
226                                             bus_space_handle_t h));
227
228 /*
229  * ATI Inport mouse definitions
230  */
231 #define MSE_INPORT_RESET        0x80
232 #define MSE_INPORT_STATUS       0x00
233 #define MSE_INPORT_DX           0x01
234 #define MSE_INPORT_DY           0x02
235 #define MSE_INPORT_MODE         0x07
236 #define MSE_INPORT_HOLD         0x20
237 #define MSE_INPORT_INTREN       0x09
238
239 static  int             mse_probeati __P((device_t dev, mse_softc_t *sc));
240 static  void            mse_enableati __P((bus_space_tag_t t,
241                                            bus_space_handle_t h));
242 static  void            mse_disableati __P((bus_space_tag_t t,
243                                             bus_space_handle_t h));
244 static  void            mse_getati __P((bus_space_tag_t t,
245                                         bus_space_handle_t h,
246                                         int *dx, int *dy, int *but));
247
248 #define MSEPRI  (PZERO + 3)
249
250 /*
251  * Table of mouse types.
252  * Keep the Logitech last, since I haven't figured out how to probe it
253  * properly yet. (Someday I'll have the documentation.)
254  */
255 static struct mse_types {
256         int     m_type;         /* Type of bus mouse */
257         int     (*m_probe) __P((device_t dev, mse_softc_t *sc));
258                                 /* Probe routine to test for it */
259         void    (*m_enable) __P((bus_space_tag_t t, bus_space_handle_t h));
260                                 /* Start routine */
261         void    (*m_disable) __P((bus_space_tag_t t, bus_space_handle_t h));
262                                 /* Disable interrupts routine */
263         void    (*m_get) __P((bus_space_tag_t t, bus_space_handle_t h,
264                               int *dx, int *dy, int *but));
265                                 /* and get mouse status */
266         mousehw_t   m_hw;       /* buttons iftype type model hwid */
267         mousemode_t m_mode;     /* proto rate res accel level size mask */
268 } mse_types[] = {
269         { MSE_ATIINPORT, 
270           mse_probeati, mse_enableati, mse_disableati, mse_getati,
271           { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
272           { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 
273             { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
274         { MSE_LOGITECH, 
275           mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,
276           { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
277           { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 
278             { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
279         { 0, },
280 };
281
282 static  int
283 mse_probe(dev)
284         device_t dev;
285 {
286         mse_softc_t *sc;
287         int error;
288         int rid;
289         int i;
290
291         /* check PnP IDs */
292         error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
293         if (error == ENXIO)
294                 return ENXIO;
295
296         sc = device_get_softc(dev);
297         rid = 0;
298         sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
299                                          MSE_IOSIZE, RF_ACTIVE);
300         if (sc->sc_port == NULL)
301                 return ENXIO;
302         sc->sc_iot = rman_get_bustag(sc->sc_port);
303         sc->sc_ioh = rman_get_bushandle(sc->sc_port);
304
305         /*
306          * Check for each mouse type in the table.
307          */
308         i = 0;
309         while (mse_types[i].m_type) {
310                 if ((*mse_types[i].m_probe)(dev, sc)) {
311                         sc->sc_mousetype = mse_types[i].m_type;
312                         sc->sc_enablemouse = mse_types[i].m_enable;
313                         sc->sc_disablemouse = mse_types[i].m_disable;
314                         sc->sc_getmouse = mse_types[i].m_get;
315                         sc->hw = mse_types[i].m_hw;
316                         sc->mode = mse_types[i].m_mode;
317                         bus_release_resource(dev, SYS_RES_IOPORT, rid,
318                                              sc->sc_port);
319                         device_set_desc(dev, "Bus/InPort Mouse");
320                         return 0;
321                 }
322                 i++;
323         }
324         bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
325         return ENXIO;
326 }
327
328 static  int
329 mse_attach(dev)
330         device_t dev;
331 {
332         mse_softc_t *sc;
333         int flags;
334         int unit;
335         int rid;
336
337         sc = device_get_softc(dev);
338         unit = device_get_unit(dev);
339
340         rid = 0;
341         sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
342                                          MSE_IOSIZE, RF_ACTIVE);
343         if (sc->sc_port == NULL)
344                 return ENXIO;
345         sc->sc_intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
346                                          RF_ACTIVE);
347         if (sc->sc_intr == NULL) {
348                 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
349                 return ENXIO;
350         }
351         sc->sc_iot = rman_get_bustag(sc->sc_port);
352         sc->sc_ioh = rman_get_bushandle(sc->sc_port);
353
354         if (BUS_SETUP_INTR(device_get_parent(dev), dev, sc->sc_intr,
355                            INTR_TYPE_TTY, mseintr, sc, &sc->sc_ih)) {
356                 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
357                 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
358                 return ENXIO;
359         }
360
361         flags = device_get_flags(dev);
362         sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
363         callout_handle_init(&sc->sc_callout);
364
365         sc->sc_dev = make_dev(&mse_cdevsw, unit << 1, 0, 0, 0600,
366                               "mse%d", unit);
367         sc->sc_ndev = make_dev(&mse_cdevsw, (unit<<1)+1, 0, 0, 0600,
368                                "nmse%d", unit);
369
370         return 0;
371 }
372
373 static  int
374 mse_detach(dev)
375         device_t dev;
376 {
377         mse_softc_t *sc;
378         int rid;
379
380         sc = device_get_softc(dev);
381         if (sc->sc_flags & MSESC_OPEN)
382                 return EBUSY;
383
384         rid = 0;
385         BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
386         bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
387         bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
388
389         destroy_dev(sc->sc_dev);
390         destroy_dev(sc->sc_ndev);
391
392         return 0;
393 }
394
395 /*
396  * Exclusive open the mouse, initialize it and enable interrupts.
397  */
398 static  int
399 mseopen(dev, flags, fmt, p)
400         dev_t dev;
401         int flags;
402         int fmt;
403         struct proc *p;
404 {
405         mse_softc_t *sc;
406         int s;
407
408         sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
409         if (sc == NULL)
410                 return (ENXIO);
411         if (sc->sc_mousetype == MSE_NONE)
412                 return (ENXIO);
413         if (sc->sc_flags & MSESC_OPEN)
414                 return (EBUSY);
415         sc->sc_flags |= MSESC_OPEN;
416         sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
417         sc->sc_deltax = sc->sc_deltay = 0;
418         sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
419         sc->sc_watchdog = FALSE;
420         sc->sc_callout = timeout(msetimeout, dev, hz*2);
421         sc->mode.level = 0;
422         sc->status.flags = 0;
423         sc->status.button = sc->status.obutton = 0;
424         sc->status.dx = sc->status.dy = sc->status.dz = 0;
425
426         /*
427          * Initialize mouse interface and enable interrupts.
428          */
429         s = spltty();
430         (*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
431         splx(s);
432         return (0);
433 }
434
435 /*
436  * mseclose: just turn off mouse innterrupts.
437  */
438 static  int
439 mseclose(dev, flags, fmt, p)
440         dev_t dev;
441         int flags;
442         int fmt;
443         struct proc *p;
444 {
445         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
446         int s;
447
448         untimeout(msetimeout, dev, sc->sc_callout);
449         callout_handle_init(&sc->sc_callout);
450         s = spltty();
451         (*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
452         sc->sc_flags &= ~MSESC_OPEN;
453         splx(s);
454         return(0);
455 }
456
457 /*
458  * mseread: return mouse info using the MSC serial protocol, but without
459  * using bytes 4 and 5.
460  * (Yes this is cheesy, but it makes the X386 server happy, so...)
461  */
462 static  int
463 mseread(dev, uio, ioflag)
464         dev_t dev;
465         struct uio *uio;
466         int ioflag;
467 {
468         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
469         int xfer, s, error;
470
471         /*
472          * If there are no protocol bytes to be read, set up a new protocol
473          * packet.
474          */
475         s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
476         if (sc->sc_bytesread >= sc->mode.packetsize) {
477                 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
478                        (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
479                         if (MSE_NBLOCKIO(dev)) {
480                                 splx(s);
481                                 return (0);
482                         }
483                         sc->sc_flags |= MSESC_WANT;
484                         error = tsleep((caddr_t)sc, MSEPRI | PCATCH,
485                                 "mseread", 0);
486                         if (error) {
487                                 splx(s);
488                                 return (error);
489                         }
490                 }
491
492                 /*
493                  * Generate protocol bytes.
494                  * For some reason X386 expects 5 bytes but never uses
495                  * the fourth or fifth?
496                  */
497                 sc->sc_bytes[0] = sc->mode.syncmask[1] 
498                     | (sc->sc_buttons & ~sc->mode.syncmask[0]);
499                 if (sc->sc_deltax > 127)
500                         sc->sc_deltax = 127;
501                 if (sc->sc_deltax < -127)
502                         sc->sc_deltax = -127;
503                 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
504                 if (sc->sc_deltay > 127)
505                         sc->sc_deltay = 127;
506                 if (sc->sc_deltay < -127)
507                         sc->sc_deltay = -127;
508                 sc->sc_bytes[1] = sc->sc_deltax;
509                 sc->sc_bytes[2] = sc->sc_deltay;
510                 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
511                 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
512                 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
513                 sc->sc_obuttons = sc->sc_buttons;
514                 sc->sc_deltax = sc->sc_deltay = 0;
515                 sc->sc_bytesread = 0;
516         }
517         splx(s);
518         xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
519         error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
520         if (error)
521                 return (error);
522         sc->sc_bytesread += xfer;
523         return(0);
524 }
525
526 /*
527  * mseioctl: process ioctl commands.
528  */
529 static int
530 mseioctl(dev, cmd, addr, flag, p)
531         dev_t dev;
532         u_long cmd;
533         caddr_t addr;
534         int flag;
535         struct proc *p;
536 {
537         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
538         mousestatus_t status;
539         int err = 0;
540         int s;
541
542         switch (cmd) {
543
544         case MOUSE_GETHWINFO:
545                 s = spltty();
546                 *(mousehw_t *)addr = sc->hw;
547                 if (sc->mode.level == 0)
548                         ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
549                 splx(s);
550                 break;
551
552         case MOUSE_GETMODE:
553                 s = spltty();
554                 *(mousemode_t *)addr = sc->mode;
555                 switch (sc->mode.level) {
556                 case 0:
557                         break;
558                 case 1:
559                         ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
560                         ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
561                         ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
562                         break;
563                 }
564                 splx(s);
565                 break;
566
567         case MOUSE_SETMODE:
568                 switch (((mousemode_t *)addr)->level) {
569                 case 0:
570                 case 1:
571                         break;
572                 default:
573                         return (EINVAL);
574                 }
575                 if (((mousemode_t *)addr)->accelfactor < -1)
576                         return (EINVAL);
577                 else if (((mousemode_t *)addr)->accelfactor >= 0)
578                         sc->mode.accelfactor = 
579                             ((mousemode_t *)addr)->accelfactor;
580                 sc->mode.level = ((mousemode_t *)addr)->level;
581                 switch (sc->mode.level) {
582                 case 0:
583                         sc->sc_bytesread = sc->mode.packetsize 
584                             = MOUSE_MSC_PACKETSIZE;
585                         break;
586                 case 1:
587                         sc->sc_bytesread = sc->mode.packetsize 
588                             = MOUSE_SYS_PACKETSIZE;
589                         break;
590                 }
591                 break;
592
593         case MOUSE_GETLEVEL:
594                 *(int *)addr = sc->mode.level;
595                 break;
596
597         case MOUSE_SETLEVEL:
598                 switch (*(int *)addr) {
599                 case 0:
600                         sc->mode.level = *(int *)addr;
601                         sc->sc_bytesread = sc->mode.packetsize 
602                             = MOUSE_MSC_PACKETSIZE;
603                         break;
604                 case 1:
605                         sc->mode.level = *(int *)addr;
606                         sc->sc_bytesread = sc->mode.packetsize 
607                             = MOUSE_SYS_PACKETSIZE;
608                         break;
609                 default:
610                         return (EINVAL);
611                 }
612                 break;
613
614         case MOUSE_GETSTATUS:
615                 s = spltty();
616                 status = sc->status;
617                 sc->status.flags = 0;
618                 sc->status.obutton = sc->status.button;
619                 sc->status.button = 0;
620                 sc->status.dx = 0;
621                 sc->status.dy = 0;
622                 sc->status.dz = 0;
623                 splx(s);
624                 *(mousestatus_t *)addr = status;
625                 break;
626
627         case MOUSE_READSTATE:
628         case MOUSE_READDATA:
629                 return (ENODEV);
630
631 #if (defined(MOUSE_GETVARS))
632         case MOUSE_GETVARS:
633         case MOUSE_SETVARS:
634                 return (ENODEV);
635 #endif
636
637         default:
638                 return (ENOTTY);
639         }
640         return (err);
641 }
642
643 /*
644  * msepoll: check for mouse input to be processed.
645  */
646 static  int
647 msepoll(dev, events, p)
648         dev_t dev;
649         int events;
650         struct proc *p;
651 {
652         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
653         int s;
654         int revents = 0;
655
656         s = spltty();
657         if (events & (POLLIN | POLLRDNORM)) {
658                 if (sc->sc_bytesread != sc->mode.packetsize ||
659                     sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
660                     (sc->sc_obuttons ^ sc->sc_buttons) != 0)
661                         revents |= events & (POLLIN | POLLRDNORM);
662                 else {
663                         /*
664                          * Since this is an exclusive open device, any previous
665                          * proc pointer is trash now, so we can just assign it.
666                          */
667                         selrecord(p, &sc->sc_selp);
668                 }
669         }
670         splx(s);
671         return (revents);
672 }
673
674 /*
675  * msetimeout: watchdog timer routine.
676  */
677 static void
678 msetimeout(arg)
679         void *arg;
680 {
681         dev_t dev;
682         mse_softc_t *sc;
683
684         dev = (dev_t)arg;
685         sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
686         if (sc->sc_watchdog) {
687                 if (bootverbose)
688                         printf("mse%d: lost interrupt?\n", MSE_UNIT(dev));
689                 mseintr(sc);
690         }
691         sc->sc_watchdog = TRUE;
692         sc->sc_callout = timeout(msetimeout, dev, hz);
693 }
694
695 /*
696  * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
697  */
698 static void
699 mseintr(arg)
700         void *arg;
701 {
702         /*
703          * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
704          * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
705          */
706         static int butmap[8] = {
707                 0, 
708                 MOUSE_BUTTON3DOWN, 
709                 MOUSE_BUTTON2DOWN, 
710                 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 
711                 MOUSE_BUTTON1DOWN, 
712                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
713                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
714                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
715         };
716         mse_softc_t *sc = arg;
717         int dx, dy, but;
718         int sign;
719
720 #ifdef DEBUG
721         static int mse_intrcnt = 0;
722         if((mse_intrcnt++ % 10000) == 0)
723                 printf("mseintr\n");
724 #endif /* DEBUG */
725         if ((sc->sc_flags & MSESC_OPEN) == 0)
726                 return;
727
728         (*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
729         if (sc->mode.accelfactor > 0) {
730                 sign = (dx < 0);
731                 dx = dx * dx / sc->mode.accelfactor;
732                 if (dx == 0)
733                         dx = 1;
734                 if (sign)
735                         dx = -dx;
736                 sign = (dy < 0);
737                 dy = dy * dy / sc->mode.accelfactor;
738                 if (dy == 0)
739                         dy = 1;
740                 if (sign)
741                         dy = -dy;
742         }
743         sc->sc_deltax += dx;
744         sc->sc_deltay += dy;
745         sc->sc_buttons = but;
746
747         but = butmap[~but & MOUSE_MSC_BUTTONS];
748         sc->status.dx += dx;
749         sc->status.dy += dy;
750         sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
751             | (sc->status.button ^ but);
752         sc->status.button = but;
753
754         sc->sc_watchdog = FALSE;
755
756         /*
757          * If mouse state has changed, wake up anyone wanting to know.
758          */
759         if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
760             (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
761                 if (sc->sc_flags & MSESC_WANT) {
762                         sc->sc_flags &= ~MSESC_WANT;
763                         wakeup((caddr_t)sc);
764                 }
765                 selwakeup(&sc->sc_selp);
766         }
767 }
768
769 /*
770  * Routines for the Logitech mouse.
771  */
772 /*
773  * Test for a Logitech bus mouse and return 1 if it is.
774  * (until I know how to use the signature port properly, just disable
775  *  interrupts and return 1)
776  */
777 static int
778 mse_probelogi(dev, sc)
779         device_t dev;
780         mse_softc_t *sc;
781 {
782
783         int sig;
784
785         bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP);
786                 /* set the signature port */
787         bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG);
788
789         DELAY(30000); /* 30 ms delay */
790         sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF;
791         if (sig == MSE_LOGI_SIG) {
792                 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC,
793                                   MSE_DISINTR);
794                 return(1);
795         } else {
796                 if (bootverbose)
797                         device_printf(dev, "wrong signature %x\n", sig);
798                 return(0);
799         }
800 }
801
802 /*
803  * Initialize Logitech mouse and enable interrupts.
804  */
805 static void
806 mse_enablelogi(tag, handle)
807         bus_space_tag_t tag;
808         bus_space_handle_t handle;
809 {
810         int dx, dy, but;
811
812         bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP);
813         mse_getlogi(tag, handle, &dx, &dy, &but);
814 }
815
816 /*
817  * Disable interrupts for Logitech mouse.
818  */
819 static void
820 mse_disablelogi(tag, handle)
821         bus_space_tag_t tag;
822         bus_space_handle_t handle;
823 {
824
825         bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR);
826 }
827
828 /*
829  * Get the current dx, dy and button up/down state.
830  */
831 static void
832 mse_getlogi(tag, handle, dx, dy, but)
833         bus_space_tag_t tag;
834         bus_space_handle_t handle;
835         int *dx;
836         int *dy;
837         int *but;
838 {
839         register char x, y;
840
841         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
842         x = bus_space_read_1(tag, handle, MSE_PORTA);
843         *but = (x >> 5) & MOUSE_MSC_BUTTONS;
844         x &= 0xf;
845         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
846         x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
847         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
848         y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf);
849         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
850         y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
851         *dx = x;
852         *dy = y;
853         bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN);
854 }
855
856 /*
857  * Routines for the ATI Inport bus mouse.
858  */
859 /*
860  * Test for a ATI Inport bus mouse and return 1 if it is.
861  * (do not enable interrupts)
862  */
863 static int
864 mse_probeati(dev, sc)
865         device_t dev;
866         mse_softc_t *sc;
867 {
868         int i;
869
870         for (i = 0; i < 2; i++)
871                 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde)
872                         return (1);
873         return (0);
874 }
875
876 /*
877  * Initialize ATI Inport mouse and enable interrupts.
878  */
879 static void
880 mse_enableati(tag, handle)
881         bus_space_tag_t tag;
882         bus_space_handle_t handle;
883 {
884
885         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET);
886         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
887         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
888 }
889
890 /*
891  * Disable interrupts for ATI Inport mouse.
892  */
893 static void
894 mse_disableati(tag, handle)
895         bus_space_tag_t tag;
896         bus_space_handle_t handle;
897 {
898
899         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
900         bus_space_write_1(tag, handle, MSE_PORTB, 0);
901 }
902
903 /*
904  * Get current dx, dy and up/down button state.
905  */
906 static void
907 mse_getati(tag, handle, dx, dy, but)
908         bus_space_tag_t tag;
909         bus_space_handle_t handle;
910         int *dx;
911         int *dy;
912         int *but;
913 {
914         register char byte;
915
916         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
917         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD);
918         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS);
919         *but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS;
920         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX);
921         byte = bus_space_read_1(tag, handle, MSE_PORTB);
922         *dx = byte;
923         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY);
924         byte = bus_space_read_1(tag, handle, MSE_PORTB);
925         *dy = byte;
926         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
927         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
928 }