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