Merge from vendor branch GCC:
[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.13 2005/06/16 16:39:54 joerg Exp $
16  */
17 /*
18  * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
19  * the X386 port, courtesy of
20  * Rick Macklem, rick@snowhite.cis.uoguelph.ca
21  * Caveats: The driver currently uses spltty(), but doesn't use any
22  * generic tty code. It could use splmse() (that only masks off the
23  * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
24  * (This may be worth the effort, since the Logitech generates 30/60
25  * interrupts/sec continuously while it is open.)
26  * NB: The ATI has NOT been tested yet!
27  */
28
29 /*
30  * Modification history:
31  * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
32  *   improved probe based on input from Logitech.
33  *
34  * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
35  *   fixes to make it work with Microsoft InPort busmouse
36  *
37  * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
38  *   added patches for new "select" interface
39  *
40  * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
41  *   changed position of some spl()'s in mseread
42  *
43  * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
44  *   limit maximum negative x/y value to -127 to work around XFree problem
45  *   that causes spurious button pushes.
46  */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/conf.h>
51 #include <sys/kernel.h>
52 #include <sys/bus.h>
53 #include <sys/poll.h>
54 #include <sys/select.h>
55 #include <sys/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(dev)
283         device_t dev;
284 {
285         mse_softc_t *sc;
286         int error;
287         int rid;
288         int i;
289
290         /* check PnP IDs */
291         error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
292         if (error == ENXIO)
293                 return ENXIO;
294
295         sc = device_get_softc(dev);
296         rid = 0;
297         sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
298                                          MSE_IOSIZE, RF_ACTIVE);
299         if (sc->sc_port == NULL)
300                 return ENXIO;
301         sc->sc_iot = rman_get_bustag(sc->sc_port);
302         sc->sc_ioh = rman_get_bushandle(sc->sc_port);
303
304         /*
305          * Check for each mouse type in the table.
306          */
307         i = 0;
308         while (mse_types[i].m_type) {
309                 if ((*mse_types[i].m_probe)(dev, sc)) {
310                         sc->sc_mousetype = mse_types[i].m_type;
311                         sc->sc_enablemouse = mse_types[i].m_enable;
312                         sc->sc_disablemouse = mse_types[i].m_disable;
313                         sc->sc_getmouse = mse_types[i].m_get;
314                         sc->hw = mse_types[i].m_hw;
315                         sc->mode = mse_types[i].m_mode;
316                         bus_release_resource(dev, SYS_RES_IOPORT, rid,
317                                              sc->sc_port);
318                         device_set_desc(dev, "Bus/InPort Mouse");
319                         return 0;
320                 }
321                 i++;
322         }
323         bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
324         return ENXIO;
325 }
326
327 static  int
328 mse_attach(dev)
329         device_t dev;
330 {
331         mse_softc_t *sc;
332         int flags;
333         int unit;
334         int rid;
335
336         sc = device_get_softc(dev);
337         unit = device_get_unit(dev);
338
339         rid = 0;
340         sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
341                                          MSE_IOSIZE, RF_ACTIVE);
342         if (sc->sc_port == NULL)
343                 return ENXIO;
344         sc->sc_intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
345                                          RF_ACTIVE);
346         if (sc->sc_intr == NULL) {
347                 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
348                 return ENXIO;
349         }
350         sc->sc_iot = rman_get_bustag(sc->sc_port);
351         sc->sc_ioh = rman_get_bushandle(sc->sc_port);
352
353         if (BUS_SETUP_INTR(device_get_parent(dev), dev, sc->sc_intr,
354                            INTR_TYPE_TTY, mseintr, sc, &sc->sc_ih, NULL)) {
355                 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
356                 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
357                 return ENXIO;
358         }
359
360         flags = device_get_flags(dev);
361         sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
362         callout_init(&sc->sc_callout);
363
364         cdevsw_add(&mse_cdevsw, ~1, unit << 1);
365         make_dev(&mse_cdevsw, unit << 1, 0, 0, 0600, "mse%d", unit);
366         make_dev(&mse_cdevsw, (unit<<1)+1, 0, 0, 0600, "nmse%d", unit);
367
368         return 0;
369 }
370
371 static  int
372 mse_detach(dev)
373         device_t dev;
374 {
375         mse_softc_t *sc;
376         int rid;
377
378         sc = device_get_softc(dev);
379         if (sc->sc_flags & MSESC_OPEN)
380                 return EBUSY;
381
382         rid = 0;
383         BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
384         bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
385         bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
386         cdevsw_remove(&mse_cdevsw, ~1, device_get_unit(dev) << 1);
387
388         return 0;
389 }
390
391 /*
392  * Exclusive open the mouse, initialize it and enable interrupts.
393  */
394 static  int
395 mseopen(dev, flags, fmt, td)
396         dev_t dev;
397         int flags;
398         int fmt;
399         struct thread *td;
400 {
401         mse_softc_t *sc;
402
403         sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
404         if (sc == NULL)
405                 return (ENXIO);
406         if (sc->sc_mousetype == MSE_NONE)
407                 return (ENXIO);
408         if (sc->sc_flags & MSESC_OPEN)
409                 return (EBUSY);
410         sc->sc_flags |= MSESC_OPEN;
411         sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
412         sc->sc_deltax = sc->sc_deltay = 0;
413         sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
414         sc->sc_watchdog = FALSE;
415         callout_reset(&sc->sc_callout, hz * 2, msetimeout, dev);
416         sc->mode.level = 0;
417         sc->status.flags = 0;
418         sc->status.button = sc->status.obutton = 0;
419         sc->status.dx = sc->status.dy = sc->status.dz = 0;
420
421         /*
422          * Initialize mouse interface and enable interrupts.
423          */
424         crit_enter();
425         (*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
426         crit_exit();
427         return (0);
428 }
429
430 /*
431  * mseclose: just turn off mouse innterrupts.
432  */
433 static  int
434 mseclose(dev, flags, fmt, td)
435         dev_t dev;
436         int flags;
437         int fmt;
438         struct thread *td;
439 {
440         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
441
442         crit_enter();
443         callout_stop(&sc->sc_callout);
444         (*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
445         sc->sc_flags &= ~MSESC_OPEN;
446         crit_exit();
447         return(0);
448 }
449
450 /*
451  * mseread: return mouse info using the MSC serial protocol, but without
452  * using bytes 4 and 5.
453  * (Yes this is cheesy, but it makes the X386 server happy, so...)
454  */
455 static  int
456 mseread(dev, uio, ioflag)
457         dev_t dev;
458         struct uio *uio;
459         int ioflag;
460 {
461         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
462         int xfer, error;
463
464         /*
465          * If there are no protocol bytes to be read, set up a new protocol
466          * packet.
467          */
468         crit_enter(); /* XXX Should be its own spl, but where is imlXX() */
469         if (sc->sc_bytesread >= sc->mode.packetsize) {
470                 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
471                        (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
472                         if (MSE_NBLOCKIO(dev)) {
473                                 crit_exit();
474                                 return (0);
475                         }
476                         sc->sc_flags |= MSESC_WANT;
477                         error = tsleep((caddr_t)sc, PCATCH, "mseread", 0);
478                         if (error) {
479                                 crit_exit();
480                                 return (error);
481                         }
482                 }
483
484                 /*
485                  * Generate protocol bytes.
486                  * For some reason X386 expects 5 bytes but never uses
487                  * the fourth or fifth?
488                  */
489                 sc->sc_bytes[0] = sc->mode.syncmask[1] 
490                     | (sc->sc_buttons & ~sc->mode.syncmask[0]);
491                 if (sc->sc_deltax > 127)
492                         sc->sc_deltax = 127;
493                 if (sc->sc_deltax < -127)
494                         sc->sc_deltax = -127;
495                 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
496                 if (sc->sc_deltay > 127)
497                         sc->sc_deltay = 127;
498                 if (sc->sc_deltay < -127)
499                         sc->sc_deltay = -127;
500                 sc->sc_bytes[1] = sc->sc_deltax;
501                 sc->sc_bytes[2] = sc->sc_deltay;
502                 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
503                 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
504                 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
505                 sc->sc_obuttons = sc->sc_buttons;
506                 sc->sc_deltax = sc->sc_deltay = 0;
507                 sc->sc_bytesread = 0;
508         }
509         crit_exit();
510         xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
511         error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
512         if (error)
513                 return (error);
514         sc->sc_bytesread += xfer;
515         return(0);
516 }
517
518 /*
519  * mseioctl: process ioctl commands.
520  */
521 static int
522 mseioctl(dev, cmd, addr, flag, td)
523         dev_t dev;
524         u_long cmd;
525         caddr_t addr;
526         int flag;
527         struct thread *td;
528 {
529         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
530         mousestatus_t status;
531         int err = 0;
532
533         switch (cmd) {
534
535         case MOUSE_GETHWINFO:
536                 crit_enter();
537                 *(mousehw_t *)addr = sc->hw;
538                 if (sc->mode.level == 0)
539                         ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
540                 crit_exit();
541                 break;
542
543         case MOUSE_GETMODE:
544                 crit_enter();
545                 *(mousemode_t *)addr = sc->mode;
546                 switch (sc->mode.level) {
547                 case 0:
548                         break;
549                 case 1:
550                         ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
551                         ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
552                         ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
553                         break;
554                 }
555                 crit_exit();
556                 break;
557
558         case MOUSE_SETMODE:
559                 switch (((mousemode_t *)addr)->level) {
560                 case 0:
561                 case 1:
562                         break;
563                 default:
564                         return (EINVAL);
565                 }
566                 if (((mousemode_t *)addr)->accelfactor < -1)
567                         return (EINVAL);
568                 else if (((mousemode_t *)addr)->accelfactor >= 0)
569                         sc->mode.accelfactor = 
570                             ((mousemode_t *)addr)->accelfactor;
571                 sc->mode.level = ((mousemode_t *)addr)->level;
572                 switch (sc->mode.level) {
573                 case 0:
574                         sc->sc_bytesread = sc->mode.packetsize 
575                             = MOUSE_MSC_PACKETSIZE;
576                         break;
577                 case 1:
578                         sc->sc_bytesread = sc->mode.packetsize 
579                             = MOUSE_SYS_PACKETSIZE;
580                         break;
581                 }
582                 break;
583
584         case MOUSE_GETLEVEL:
585                 *(int *)addr = sc->mode.level;
586                 break;
587
588         case MOUSE_SETLEVEL:
589                 switch (*(int *)addr) {
590                 case 0:
591                         sc->mode.level = *(int *)addr;
592                         sc->sc_bytesread = sc->mode.packetsize 
593                             = MOUSE_MSC_PACKETSIZE;
594                         break;
595                 case 1:
596                         sc->mode.level = *(int *)addr;
597                         sc->sc_bytesread = sc->mode.packetsize 
598                             = MOUSE_SYS_PACKETSIZE;
599                         break;
600                 default:
601                         return (EINVAL);
602                 }
603                 break;
604
605         case MOUSE_GETSTATUS:
606                 crit_enter();
607                 status = sc->status;
608                 sc->status.flags = 0;
609                 sc->status.obutton = sc->status.button;
610                 sc->status.button = 0;
611                 sc->status.dx = 0;
612                 sc->status.dy = 0;
613                 sc->status.dz = 0;
614                 crit_exit();
615                 *(mousestatus_t *)addr = status;
616                 break;
617
618         case MOUSE_READSTATE:
619         case MOUSE_READDATA:
620                 return (ENODEV);
621
622 #if (defined(MOUSE_GETVARS))
623         case MOUSE_GETVARS:
624         case MOUSE_SETVARS:
625                 return (ENODEV);
626 #endif
627
628         default:
629                 return (ENOTTY);
630         }
631         return (err);
632 }
633
634 /*
635  * msepoll: check for mouse input to be processed.
636  */
637 static  int
638 msepoll(dev, events, td)
639         dev_t dev;
640         int events;
641         struct thread *td;
642 {
643         mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
644         int revents = 0;
645
646         crit_enter();
647         if (events & (POLLIN | POLLRDNORM)) {
648                 if (sc->sc_bytesread != sc->mode.packetsize ||
649                     sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
650                     (sc->sc_obuttons ^ sc->sc_buttons) != 0)
651                         revents |= events & (POLLIN | POLLRDNORM);
652                 else {
653                         /*
654                          * Since this is an exclusive open device, any previous
655                          * proc pointer is trash now, so we can just assign it.
656                          */
657                         selrecord(td, &sc->sc_selp);
658                 }
659         }
660         crit_exit();
661         return (revents);
662 }
663
664 /*
665  * msetimeout: watchdog timer routine.
666  */
667 static void
668 msetimeout(arg)
669         void *arg;
670 {
671         dev_t dev;
672         mse_softc_t *sc;
673
674         dev = (dev_t)arg;
675         sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
676         if (sc->sc_watchdog) {
677                 if (bootverbose)
678                         printf("mse%d: lost interrupt?\n", MSE_UNIT(dev));
679                 mseintr(sc);
680         }
681         sc->sc_watchdog = TRUE;
682         callout_reset(&sc->sc_callout, hz, msetimeout, dev);
683 }
684
685 /*
686  * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
687  */
688 static void
689 mseintr(arg)
690         void *arg;
691 {
692         /*
693          * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
694          * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
695          */
696         static int butmap[8] = {
697                 0, 
698                 MOUSE_BUTTON3DOWN, 
699                 MOUSE_BUTTON2DOWN, 
700                 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN, 
701                 MOUSE_BUTTON1DOWN, 
702                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN, 
703                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
704                 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
705         };
706         mse_softc_t *sc = arg;
707         int dx, dy, but;
708         int sign;
709
710 #ifdef DEBUG
711         static int mse_intrcnt = 0;
712         if((mse_intrcnt++ % 10000) == 0)
713                 printf("mseintr\n");
714 #endif /* DEBUG */
715         if ((sc->sc_flags & MSESC_OPEN) == 0)
716                 return;
717
718         (*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
719         if (sc->mode.accelfactor > 0) {
720                 sign = (dx < 0);
721                 dx = dx * dx / sc->mode.accelfactor;
722                 if (dx == 0)
723                         dx = 1;
724                 if (sign)
725                         dx = -dx;
726                 sign = (dy < 0);
727                 dy = dy * dy / sc->mode.accelfactor;
728                 if (dy == 0)
729                         dy = 1;
730                 if (sign)
731                         dy = -dy;
732         }
733         sc->sc_deltax += dx;
734         sc->sc_deltay += dy;
735         sc->sc_buttons = but;
736
737         but = butmap[~but & MOUSE_MSC_BUTTONS];
738         sc->status.dx += dx;
739         sc->status.dy += dy;
740         sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
741             | (sc->status.button ^ but);
742         sc->status.button = but;
743
744         sc->sc_watchdog = FALSE;
745
746         /*
747          * If mouse state has changed, wake up anyone wanting to know.
748          */
749         if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
750             (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
751                 if (sc->sc_flags & MSESC_WANT) {
752                         sc->sc_flags &= ~MSESC_WANT;
753                         wakeup((caddr_t)sc);
754                 }
755                 selwakeup(&sc->sc_selp);
756         }
757 }
758
759 /*
760  * Routines for the Logitech mouse.
761  */
762 /*
763  * Test for a Logitech bus mouse and return 1 if it is.
764  * (until I know how to use the signature port properly, just disable
765  *  interrupts and return 1)
766  */
767 static int
768 mse_probelogi(dev, sc)
769         device_t dev;
770         mse_softc_t *sc;
771 {
772
773         int sig;
774
775         bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP);
776                 /* set the signature port */
777         bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG);
778
779         DELAY(30000); /* 30 ms delay */
780         sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF;
781         if (sig == MSE_LOGI_SIG) {
782                 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC,
783                                   MSE_DISINTR);
784                 return(1);
785         } else {
786                 if (bootverbose)
787                         device_printf(dev, "wrong signature %x\n", sig);
788                 return(0);
789         }
790 }
791
792 /*
793  * Initialize Logitech mouse and enable interrupts.
794  */
795 static void
796 mse_enablelogi(tag, handle)
797         bus_space_tag_t tag;
798         bus_space_handle_t handle;
799 {
800         int dx, dy, but;
801
802         bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP);
803         mse_getlogi(tag, handle, &dx, &dy, &but);
804 }
805
806 /*
807  * Disable interrupts for Logitech mouse.
808  */
809 static void
810 mse_disablelogi(tag, handle)
811         bus_space_tag_t tag;
812         bus_space_handle_t handle;
813 {
814
815         bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR);
816 }
817
818 /*
819  * Get the current dx, dy and button up/down state.
820  */
821 static void
822 mse_getlogi(tag, handle, dx, dy, but)
823         bus_space_tag_t tag;
824         bus_space_handle_t handle;
825         int *dx;
826         int *dy;
827         int *but;
828 {
829         char x, y;
830
831         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
832         x = bus_space_read_1(tag, handle, MSE_PORTA);
833         *but = (x >> 5) & MOUSE_MSC_BUTTONS;
834         x &= 0xf;
835         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
836         x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
837         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
838         y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf);
839         bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
840         y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
841         *dx = x;
842         *dy = y;
843         bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN);
844 }
845
846 /*
847  * Routines for the ATI Inport bus mouse.
848  */
849 /*
850  * Test for a ATI Inport bus mouse and return 1 if it is.
851  * (do not enable interrupts)
852  */
853 static int
854 mse_probeati(dev, sc)
855         device_t dev;
856         mse_softc_t *sc;
857 {
858         int i;
859
860         for (i = 0; i < 2; i++)
861                 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde)
862                         return (1);
863         return (0);
864 }
865
866 /*
867  * Initialize ATI Inport mouse and enable interrupts.
868  */
869 static void
870 mse_enableati(tag, handle)
871         bus_space_tag_t tag;
872         bus_space_handle_t handle;
873 {
874
875         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET);
876         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
877         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
878 }
879
880 /*
881  * Disable interrupts for ATI Inport mouse.
882  */
883 static void
884 mse_disableati(tag, handle)
885         bus_space_tag_t tag;
886         bus_space_handle_t handle;
887 {
888
889         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
890         bus_space_write_1(tag, handle, MSE_PORTB, 0);
891 }
892
893 /*
894  * Get current dx, dy and up/down button state.
895  */
896 static void
897 mse_getati(tag, handle, dx, dy, but)
898         bus_space_tag_t tag;
899         bus_space_handle_t handle;
900         int *dx;
901         int *dy;
902         int *but;
903 {
904         char byte;
905
906         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
907         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD);
908         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS);
909         *but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS;
910         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX);
911         byte = bus_space_read_1(tag, handle, MSE_PORTB);
912         *dx = byte;
913         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY);
914         byte = bus_space_read_1(tag, handle, MSE_PORTB);
915         *dy = byte;
916         bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
917         bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
918 }