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