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