ce1eb8e474bcb6c2617272d0f90db65dbda9fd41
[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.17 2006/07/28 02:17:36 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/device.h>
52 #include <sys/kernel.h>
53 #include <sys/bus.h>
54 #include <sys/poll.h>
55 #include <sys/selinfo.h>
56 #include <sys/thread2.h>
57 #include <sys/uio.h>
58
59 #include <machine/bus_pio.h>
60 #include <machine/bus.h>
61 #include <machine/clock.h>
62 #include <machine/mouse.h>
63 #include <machine/resource.h>
64 #include <sys/rman.h>
65
66 #include <bus/isa/isavar.h>
67
68 /* driver configuration flags (config) */
69 #define MSE_CONFIG_ACCEL        0x00f0  /* acceleration factor */
70 #define MSE_CONFIG_FLAGS        (MSE_CONFIG_ACCEL)
71
72 /*
73  * Software control structure for mouse. The sc_enablemouse(),
74  * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
75  */
76 typedef struct mse_softc {
77         int             sc_flags;
78         int             sc_mousetype;
79         struct selinfo  sc_selp;
80         struct resource *sc_port;
81         struct resource *sc_intr;
82         bus_space_tag_t sc_iot;
83         bus_space_handle_t sc_ioh;
84         void            *sc_ih;
85         void            (*sc_enablemouse) (bus_space_tag_t t,
86                                                bus_space_handle_t h);
87         void            (*sc_disablemouse) (bus_space_tag_t t,
88                                                 bus_space_handle_t h);
89         void            (*sc_getmouse) (bus_space_tag_t t,
90                                             bus_space_handle_t h,
91                                             int *dx, int *dy, int *but);
92         int             sc_deltax;
93         int             sc_deltay;
94         int             sc_obuttons;
95         int             sc_buttons;
96         int             sc_bytesread;
97         u_char          sc_bytes[MOUSE_SYS_PACKETSIZE];
98         struct          callout sc_callout;
99         int             sc_watchdog;
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 (device_t dev);
108 static  int             mse_attach (device_t dev);
109 static  int             mse_detach (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 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_poll =       msepoll,
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         dev_ops_add(&mse_ops, ~1, unit << 1);
354         make_dev(&mse_ops, unit << 1, 0, 0, 0600, "mse%d", unit);
355         make_dev(&mse_ops, (unit<<1)+1, 0, 0, 0600, "nmse%d", unit);
356
357         return 0;
358 }
359
360 static  int
361 mse_detach(device_t dev)
362 {
363         mse_softc_t *sc;
364         int rid;
365
366         sc = device_get_softc(dev);
367         if (sc->sc_flags & MSESC_OPEN)
368                 return EBUSY;
369
370         rid = 0;
371         BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
372         bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
373         bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
374         dev_ops_remove(&mse_ops, ~1, device_get_unit(dev) << 1);
375
376         return 0;
377 }
378
379 /*
380  * Exclusive open the mouse, initialize it and enable interrupts.
381  */
382 static  int
383 mseopen(struct dev_open_args *ap)
384 {
385         dev_t dev = ap->a_head.a_dev;
386         mse_softc_t *sc;
387
388         sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
389         if (sc == NULL)
390                 return (ENXIO);
391         if (sc->sc_mousetype == MSE_NONE)
392                 return (ENXIO);
393         if (sc->sc_flags & MSESC_OPEN)
394                 return (EBUSY);
395         sc->sc_flags |= MSESC_OPEN;
396         sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
397         sc->sc_deltax = sc->sc_deltay = 0;
398         sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
399         sc->sc_watchdog = FALSE;
400         callout_reset(&sc->sc_callout, hz * 2, msetimeout, dev);
401         sc->mode.level = 0;
402         sc->status.flags = 0;
403         sc->status.button = sc->status.obutton = 0;
404         sc->status.dx = sc->status.dy = sc->status.dz = 0;
405
406         /*
407          * Initialize mouse interface and enable interrupts.
408          */
409         crit_enter();
410         (*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
411         crit_exit();
412         return (0);
413 }
414
415 /*
416  * mseclose: just turn off mouse innterrupts.
417  */
418 static  int
419 mseclose(struct dev_close_args *ap)
420 {
421         dev_t dev = ap->a_head.a_dev;
422         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
423
424         crit_enter();
425         callout_stop(&sc->sc_callout);
426         (*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
427         sc->sc_flags &= ~MSESC_OPEN;
428         crit_exit();
429         return(0);
430 }
431
432 /*
433  * mseread: return mouse info using the MSC serial protocol, but without
434  * using bytes 4 and 5.
435  * (Yes this is cheesy, but it makes the X386 server happy, so...)
436  */
437 static  int
438 mseread(struct dev_read_args *ap)
439 {
440         dev_t dev = ap->a_head.a_dev;
441         struct uio *uio = ap->a_uio;
442         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
443         int xfer, error;
444
445         /*
446          * If there are no protocol bytes to be read, set up a new protocol
447          * packet.
448          */
449         crit_enter(); /* XXX Should be its own spl, but where is imlXX() */
450         if (sc->sc_bytesread >= sc->mode.packetsize) {
451                 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
452                        (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
453                         if (MSE_NBLOCKIO(dev)) {
454                                 crit_exit();
455                                 return (0);
456                         }
457                         sc->sc_flags |= MSESC_WANT;
458                         error = tsleep((caddr_t)sc, PCATCH, "mseread", 0);
459                         if (error) {
460                                 crit_exit();
461                                 return (error);
462                         }
463                 }
464
465                 /*
466                  * Generate protocol bytes.
467                  * For some reason X386 expects 5 bytes but never uses
468                  * the fourth or fifth?
469                  */
470                 sc->sc_bytes[0] = sc->mode.syncmask[1] 
471                     | (sc->sc_buttons & ~sc->mode.syncmask[0]);
472                 if (sc->sc_deltax > 127)
473                         sc->sc_deltax = 127;
474                 if (sc->sc_deltax < -127)
475                         sc->sc_deltax = -127;
476                 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
477                 if (sc->sc_deltay > 127)
478                         sc->sc_deltay = 127;
479                 if (sc->sc_deltay < -127)
480                         sc->sc_deltay = -127;
481                 sc->sc_bytes[1] = sc->sc_deltax;
482                 sc->sc_bytes[2] = sc->sc_deltay;
483                 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
484                 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
485                 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
486                 sc->sc_obuttons = sc->sc_buttons;
487                 sc->sc_deltax = sc->sc_deltay = 0;
488                 sc->sc_bytesread = 0;
489         }
490         crit_exit();
491         xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
492         error = uiomove(&sc->sc_bytes[sc->sc_bytesread], 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         dev_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 /*
612  * msepoll: check for mouse input to be processed.
613  */
614 static  int
615 msepoll(struct dev_poll_args *ap)
616 {
617         dev_t dev = ap->a_head.a_dev;
618         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
619         int revents = 0;
620
621         crit_enter();
622         if (ap->a_events & (POLLIN | POLLRDNORM)) {
623                 if (sc->sc_bytesread != sc->mode.packetsize ||
624                     sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
625                     (sc->sc_obuttons ^ sc->sc_buttons) != 0)
626                         revents |= ap->a_events & (POLLIN | POLLRDNORM);
627                 else {
628                         /*
629                          * Since this is an exclusive open device, any previous
630                          * proc pointer is trash now, so we can just assign it.
631                          */
632                         selrecord(curthread, &sc->sc_selp);
633                 }
634         }
635         crit_exit();
636         ap->a_events = revents;
637         return (0);
638 }
639
640 /*
641  * msetimeout: watchdog timer routine.
642  */
643 static void
644 msetimeout(void *arg)
645 {
646         dev_t dev;
647         mse_softc_t *sc;
648
649         dev = (dev_t)arg;
650         sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
651         if (sc->sc_watchdog) {
652                 if (bootverbose)
653                         printf("mse%d: lost interrupt?\n", MSE_UNIT(dev));
654                 mseintr(sc);
655         }
656         sc->sc_watchdog = TRUE;
657         callout_reset(&sc->sc_callout, hz, msetimeout, dev);
658 }
659
660 /*
661  * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
662  */
663 static void
664 mseintr(void *arg)
665 {
666         /*
667          * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
668          * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
669          */
670         static int butmap[8] = {
671                 0, 
672                 MOUSE_BUTTON3DOWN, 
673                 MOUSE_BUTTON2DOWN, 
674                 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 
675                 MOUSE_BUTTON1DOWN, 
676                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
677                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
678                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
679         };
680         mse_softc_t *sc = arg;
681         int dx, dy, but;
682         int sign;
683
684 #ifdef DEBUG
685         static int mse_intrcnt = 0;
686         if((mse_intrcnt++ % 10000) == 0)
687                 printf("mseintr\n");
688 #endif /* DEBUG */
689         if ((sc->sc_flags & MSESC_OPEN) == 0)
690                 return;
691
692         (*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
693         if (sc->mode.accelfactor > 0) {
694                 sign = (dx < 0);
695                 dx = dx * dx / sc->mode.accelfactor;
696                 if (dx == 0)
697                         dx = 1;
698                 if (sign)
699                         dx = -dx;
700                 sign = (dy < 0);
701                 dy = dy * dy / sc->mode.accelfactor;
702                 if (dy == 0)
703                         dy = 1;
704                 if (sign)
705                         dy = -dy;
706         }
707         sc->sc_deltax += dx;
708         sc->sc_deltay += dy;
709         sc->sc_buttons = but;
710
711         but = butmap[~but & MOUSE_MSC_BUTTONS];
712         sc->status.dx += dx;
713         sc->status.dy += dy;
714         sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
715             | (sc->status.button ^ but);
716         sc->status.button = but;
717
718         sc->sc_watchdog = FALSE;
719
720         /*
721          * If mouse state has changed, wake up anyone wanting to know.
722          */
723         if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
724             (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
725                 if (sc->sc_flags & MSESC_WANT) {
726                         sc->sc_flags &= ~MSESC_WANT;
727                         wakeup((caddr_t)sc);
728                 }
729                 selwakeup(&sc->sc_selp);
730         }
731 }
732
733 /*
734  * Routines for the Logitech mouse.
735  */
736 /*
737  * Test for a Logitech bus mouse and return 1 if it is.
738  * (until I know how to use the signature port properly, just disable
739  *  interrupts and return 1)
740  */
741 static int
742 mse_probelogi(device_t dev, mse_softc_t *sc)
743 {
744
745         int sig;
746
747         bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP);
748                 /* set the signature port */
749         bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG);
750
751         DELAY(30000); /* 30 ms delay */
752         sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF;
753         if (sig == MSE_LOGI_SIG) {
754                 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC,
755                                   MSE_DISINTR);
756                 return(1);
757         } else {
758                 if (bootverbose)
759                         device_printf(dev, "wrong signature %x\n", sig);
760                 return(0);
761         }
762 }
763
764 /*
765  * Initialize Logitech mouse and enable interrupts.
766  */
767 static void
768 mse_enablelogi(bus_space_tag_t tag, bus_space_handle_t handle)
769 {
770         int dx, dy, but;
771
772         bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP);
773         mse_getlogi(tag, handle, &dx, &dy, &but);
774 }
775
776 /*
777  * Disable interrupts for Logitech mouse.
778  */
779 static void
780 mse_disablelogi(bus_space_tag_t tag, bus_space_handle_t handle)
781 {
782
783         bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR);
784 }
785
786 /*
787  * Get the current dx, dy and button up/down state.
788  */
789 static void
790 mse_getlogi(bus_space_tag_t tag, bus_space_handle_t handle, int *dx, int *dy,
791             int *but)
792 {
793         char x, y;
794
795         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
796         x = bus_space_read_1(tag, handle, MSE_PORTA);
797         *but = (x >> 5) & MOUSE_MSC_BUTTONS;
798         x &= 0xf;
799         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
800         x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
801         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
802         y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf);
803         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
804         y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
805         *dx = x;
806         *dy = y;
807         bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN);
808 }
809
810 /*
811  * Routines for the ATI Inport bus mouse.
812  */
813 /*
814  * Test for a ATI Inport bus mouse and return 1 if it is.
815  * (do not enable interrupts)
816  */
817 static int
818 mse_probeati(device_t dev, mse_softc_t *sc)
819 {
820         int i;
821
822         for (i = 0; i < 2; i++)
823                 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde)
824                         return (1);
825         return (0);
826 }
827
828 /*
829  * Initialize ATI Inport mouse and enable interrupts.
830  */
831 static void
832 mse_enableati(bus_space_tag_t tag, bus_space_handle_t handle)
833 {
834
835         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET);
836         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
837         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
838 }
839
840 /*
841  * Disable interrupts for ATI Inport mouse.
842  */
843 static void
844 mse_disableati(bus_space_tag_t tag, bus_space_handle_t handle)
845 {
846
847         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
848         bus_space_write_1(tag, handle, MSE_PORTB, 0);
849 }
850
851 /*
852  * Get current dx, dy and up/down button state.
853  */
854 static void
855 mse_getati(bus_space_tag_t tag, bus_space_handle_t handle, int *dx, int *dy,
856            int *but)
857 {
858         char byte;
859
860         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
861         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD);
862         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS);
863         *but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS;
864         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX);
865         byte = bus_space_read_1(tag, handle, MSE_PORTB);
866         *dx = byte;
867         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY);
868         byte = bus_space_read_1(tag, handle, MSE_PORTB);
869         *dy = byte;
870         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
871         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
872 }