]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mse/mse_isa.c
sys/dev: further adoption of SPDX licensing ID tags.
[FreeBSD/FreeBSD.git] / sys / dev / mse / mse_isa.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 M. Warner Losh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 /*-
32  * Copyright 1992 by the University of Guelph
33  *
34  * Permission to use, copy and modify this
35  * software and its documentation for any purpose and without
36  * fee is hereby granted, provided that the above copyright
37  * notice appear in all copies and that both that copyright
38  * notice and this permission notice appear in supporting
39  * documentation.
40  * University of Guelph makes no representations about the suitability of
41  * this software for any purpose.  It is provided "as is"
42  * without express or implied warranty.
43  */
44 /*
45  * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
46  * the X386 port, courtesy of
47  * Rick Macklem, rick@snowhite.cis.uoguelph.ca
48  * Caveats: The driver currently uses spltty(), but doesn't use any
49  * generic tty code. It could use splmse() (that only masks off the
50  * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
51  * (This may be worth the effort, since the Logitech generates 30/60
52  * interrupts/sec continuously while it is open.)
53  * NB: The ATI has NOT been tested yet!
54  */
55
56 /*
57  * Modification history:
58  * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
59  *   improved probe based on input from Logitech.
60  *
61  * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
62  *   fixes to make it work with Microsoft InPort busmouse
63  *
64  * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
65  *   added patches for new "select" interface
66  *
67  * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
68  *   changed position of some spl()'s in mseread
69  *
70  * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
71  *   limit maximum negative x/y value to -127 to work around XFree problem
72  *   that causes spurious button pushes.
73  */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/conf.h>
78 #include <sys/kernel.h>
79 #include <sys/module.h>
80 #include <sys/bus.h>
81 #include <sys/poll.h>
82 #include <sys/selinfo.h>
83 #include <sys/uio.h>
84 #include <sys/mouse.h>
85
86 #include <machine/bus.h>
87 #include <machine/resource.h>
88 #include <sys/rman.h>
89
90 #include <isa/isavar.h>
91
92 #include <dev/mse/msevar.h>
93
94 static  int             mse_isa_probe(device_t dev);
95 static  int             mse_isa_attach(device_t dev);
96
97 static  device_method_t mse_methods[] = {
98         DEVMETHOD(device_probe,         mse_isa_probe),
99         DEVMETHOD(device_attach,        mse_isa_attach),
100         DEVMETHOD(device_detach,        mse_detach),
101         { 0, 0 }
102 };
103
104 static  driver_t        mse_driver = {
105         "mse",
106         mse_methods,
107         sizeof(mse_softc_t),
108 };
109
110 DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
111
112 static struct isa_pnp_id mse_ids[] = {
113         { 0x000fd041, "Bus mouse" },                    /* PNP0F00 */
114         { 0x020fd041, "InPort mouse" },                 /* PNP0F02 */
115         { 0x0d0fd041, "InPort mouse compatible" },      /* PNP0F0D */
116         { 0x110fd041, "Bus mouse compatible" },         /* PNP0F11 */
117         { 0x150fd041, "Logitech bus mouse" },           /* PNP0F15 */
118         { 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */
119         { 0 }
120 };
121
122 /*
123  * Logitech bus mouse definitions
124  */
125 #define MSE_SETUP       0x91    /* What does this mean? */
126                                 /* The definition for the control port */
127                                 /* is as follows: */
128
129                                 /* D7    =  Mode set flag (1 = active)  */
130                                 /* D6,D5 =  Mode selection (port A)     */
131                                 /*          00 = Mode 0 = Basic I/O     */
132                                 /*          01 = Mode 1 = Strobed I/O   */
133                                 /*          10 = Mode 2 = Bi-dir bus    */
134                                 /* D4    =  Port A direction (1 = input)*/
135                                 /* D3    =  Port C (upper 4 bits)       */
136                                 /*          direction. (1 = input)      */
137                                 /* D2    =  Mode selection (port B & C) */
138                                 /*          0 = Mode 0 = Basic I/O      */
139                                 /*          1 = Mode 1 = Strobed I/O    */
140                                 /* D1    =  Port B direction (1 = input)*/
141                                 /* D0    =  Port C (lower 4 bits)       */
142                                 /*          direction. (1 = input)      */
143
144                                 /* So 91 means Basic I/O on all 3 ports,*/
145                                 /* Port A is an input port, B is an     */
146                                 /* output port, C is split with upper   */
147                                 /* 4 bits being an output port and lower*/
148                                 /* 4 bits an input port, and enable the */
149                                 /* sucker.                              */
150                                 /* Courtesy Intel 8255 databook. Lars   */
151 #define MSE_HOLD        0x80
152 #define MSE_RXLOW       0x00
153 #define MSE_RXHIGH      0x20
154 #define MSE_RYLOW       0x40
155 #define MSE_RYHIGH      0x60
156 #define MSE_DISINTR     0x10
157 #define MSE_INTREN      0x00
158
159 static  int             mse_probelogi(device_t dev, mse_softc_t *sc);
160 static  void            mse_disablelogi(struct resource *port);
161 static  void            mse_getlogi(struct resource *port, int *dx, int *dy,
162                             int *but);
163 static  void            mse_enablelogi(struct resource *port);
164
165 /*
166  * ATI Inport mouse definitions
167  */
168 #define MSE_INPORT_RESET        0x80
169 #define MSE_INPORT_STATUS       0x00
170 #define MSE_INPORT_DX           0x01
171 #define MSE_INPORT_DY           0x02
172 #define MSE_INPORT_MODE         0x07
173 #define MSE_INPORT_HOLD         0x20
174 #define MSE_INPORT_INTREN       0x09
175
176 static  int             mse_probeati(device_t dev, mse_softc_t *sc);
177 static  void            mse_enableati(struct resource *port);
178 static  void            mse_disableati(struct resource *port);
179 static  void            mse_getati(struct resource *port, int *dx, int *dy,
180                             int *but);
181
182 static struct mse_types mse_types[] = {
183         { MSE_ATIINPORT, 
184           mse_probeati, mse_enableati, mse_disableati, mse_getati,
185           { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
186           { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 
187             { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
188         { MSE_LOGITECH, 
189           mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,
190           { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
191           { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE, 
192             { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
193         { 0, },
194 };
195
196 static  int
197 mse_isa_probe(device_t dev)
198 {
199         mse_softc_t *sc;
200         int error;
201         int rid;
202         int i;
203
204         /* check PnP IDs */
205         error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
206         if (error == ENXIO)
207                 return error;
208
209         sc = device_get_softc(dev);
210         rid = 0;
211         sc->sc_port = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
212                                                   MSE_IOSIZE, RF_ACTIVE);
213         if (sc->sc_port == NULL)
214                 return ENXIO;
215
216         /*
217          * Check for each mouse type in the table.
218          */
219         i = 0;
220         while (mse_types[i].m_type) {
221                 if ((*mse_types[i].m_probe)(dev, sc)) {
222                         sc->sc_mousetype = mse_types[i].m_type;
223                         sc->sc_enablemouse = mse_types[i].m_enable;
224                         sc->sc_disablemouse = mse_types[i].m_disable;
225                         sc->sc_getmouse = mse_types[i].m_get;
226                         sc->hw = mse_types[i].m_hw;
227                         sc->mode = mse_types[i].m_mode;
228                         bus_release_resource(dev, SYS_RES_IOPORT, rid,
229                                              sc->sc_port);
230                         device_set_desc(dev, "Bus/InPort Mouse");
231                         return 0;
232                 }
233                 i++;
234         }
235         bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
236         return ENXIO;
237 }
238
239 static  int
240 mse_isa_attach(device_t dev)
241 {
242         mse_softc_t *sc;
243         int rid;
244
245         sc = device_get_softc(dev);
246
247         rid = 0;
248         sc->sc_port = bus_alloc_resource_anywhere(dev, SYS_RES_IOPORT, &rid,
249                                                   MSE_IOSIZE, RF_ACTIVE);
250         if (sc->sc_port == NULL)
251                 return ENXIO;
252
253         return (mse_common_attach(dev));
254 }
255
256 /*
257  * Routines for the Logitech mouse.
258  */
259 /*
260  * Test for a Logitech bus mouse and return 1 if it is.
261  * (until I know how to use the signature port properly, just disable
262  *  interrupts and return 1)
263  */
264 static int
265 mse_probelogi(device_t dev, mse_softc_t *sc)
266 {
267
268         int sig;
269
270         bus_write_1(sc->sc_port, MSE_PORTD, MSE_SETUP);
271                 /* set the signature port */
272         bus_write_1(sc->sc_port, MSE_PORTB, MSE_LOGI_SIG);
273
274         DELAY(30000); /* 30 ms delay */
275         sig = bus_read_1(sc->sc_port, MSE_PORTB) & 0xFF;
276         if (sig == MSE_LOGI_SIG) {
277                 bus_write_1(sc->sc_port, MSE_PORTC, MSE_DISINTR);
278                 return(1);
279         } else {
280                 if (bootverbose)
281                         device_printf(dev, "wrong signature %x\n", sig);
282                 return(0);
283         }
284 }
285
286 /*
287  * Initialize Logitech mouse and enable interrupts.
288  */
289 static void
290 mse_enablelogi(struct resource *port)
291 {
292         int dx, dy, but;
293
294         bus_write_1(port, MSE_PORTD, MSE_SETUP);
295         mse_getlogi(port, &dx, &dy, &but);
296 }
297
298 /*
299  * Disable interrupts for Logitech mouse.
300  */
301 static void
302 mse_disablelogi(struct resource *port)
303 {
304
305         bus_write_1(port, MSE_PORTC, MSE_DISINTR);
306 }
307
308 /*
309  * Get the current dx, dy and button up/down state.
310  */
311 static void
312 mse_getlogi(struct resource *port, int *dx, int *dy, int *but)
313 {
314         char x, y;
315
316         bus_write_1(port, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
317         x = bus_read_1(port, MSE_PORTA);
318         *but = (x >> 5) & MOUSE_MSC_BUTTONS;
319         x &= 0xf;
320         bus_write_1(port, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
321         x |= (bus_read_1(port, MSE_PORTA) << 4);
322         bus_write_1(port, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
323         y = (bus_read_1(port, MSE_PORTA) & 0xf);
324         bus_write_1(port, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
325         y |= (bus_read_1(port, MSE_PORTA) << 4);
326         *dx = x;
327         *dy = y;
328         bus_write_1(port, MSE_PORTC, MSE_INTREN);
329 }
330
331 /*
332  * Routines for the ATI Inport bus mouse.
333  */
334 /*
335  * Test for an ATI Inport bus mouse and return 1 if it is.
336  * (do not enable interrupts)
337  */
338 static int
339 mse_probeati(device_t dev, mse_softc_t *sc)
340 {
341         int i;
342
343         for (i = 0; i < 2; i++)
344                 if (bus_read_1(sc->sc_port, MSE_PORTC) == 0xde)
345                         return (1);
346         return (0);
347 }
348
349 /*
350  * Initialize ATI Inport mouse and enable interrupts.
351  */
352 static void
353 mse_enableati(struct resource *port)
354 {
355
356         bus_write_1(port, MSE_PORTA, MSE_INPORT_RESET);
357         bus_write_1(port, MSE_PORTA, MSE_INPORT_MODE);
358         bus_write_1(port, MSE_PORTB, MSE_INPORT_INTREN);
359 }
360
361 /*
362  * Disable interrupts for ATI Inport mouse.
363  */
364 static void
365 mse_disableati(struct resource *port)
366 {
367
368         bus_write_1(port, MSE_PORTA, MSE_INPORT_MODE);
369         bus_write_1(port, MSE_PORTB, 0);
370 }
371
372 /*
373  * Get current dx, dy and up/down button state.
374  */
375 static void
376 mse_getati(struct resource *port, int *dx, int *dy, int *but)
377 {
378         char byte;
379
380         bus_write_1(port, MSE_PORTA, MSE_INPORT_MODE);
381         bus_write_1(port, MSE_PORTB, MSE_INPORT_HOLD);
382         bus_write_1(port, MSE_PORTA, MSE_INPORT_STATUS);
383         *but = ~bus_read_1(port, MSE_PORTB) & MOUSE_MSC_BUTTONS;
384         bus_write_1(port, MSE_PORTA, MSE_INPORT_DX);
385         byte = bus_read_1(port, MSE_PORTB);
386         *dx = byte;
387         bus_write_1(port, MSE_PORTA, MSE_INPORT_DY);
388         byte = bus_read_1(port, MSE_PORTB);
389         *dy = byte;
390         bus_write_1(port, MSE_PORTA, MSE_INPORT_MODE);
391         bus_write_1(port, MSE_PORTB, MSE_INPORT_INTREN);
392 }