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