mse_cbus.c revision 140040
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_cbus.c 140040 2005-01-11 06:24:40Z imp $
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/clock.h>
86#include <machine/resource.h>
87#include <sys/rman.h>
88
89#include <isa/isavar.h>
90
91#include <dev/mse/msevar.h>
92
93static	int		mse_probe(device_t dev);
94static	int		mse_attach(device_t dev);
95static	int		mse_detach(device_t dev);
96
97static	device_method_t	mse_methods[] = {
98	DEVMETHOD(device_probe,		mse_probe),
99	DEVMETHOD(device_attach,	mse_attach),
100	DEVMETHOD(device_detach,	mse_detach),
101	{ 0, 0 }
102};
103
104static	driver_t	mse_driver = {
105	"mse",
106	mse_methods,
107	sizeof(mse_softc_t),
108};
109
110DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
111
112static struct isa_pnp_id mse_ids[] = {
113#if 0
114	{ 0x001fa3b8, "PC-98 bus mouse" },		/* NEC1F00 */
115#endif
116	{ 0 }
117};
118
119/*
120 * PC-9801 Bus mouse definitions
121 */
122
123#define	MODE	MSE_PORTD
124#define	HC	MSE_PORTD
125#define	INT	MSE_PORTD
126
127#define	XL	0x00
128#define	XH	0x20
129#define	YL	0x40
130#define	YH	0x60
131
132#define	INT_ENABLE	0x8
133#define	INT_DISABLE	0x9
134#define	HC_NO_CLEAR	0xe
135#define	HC_CLEAR	0xf
136
137static	bus_addr_t	mse_port[] = {0, 2, 4, 6};
138
139static	int		mse_probe98m(device_t dev, mse_softc_t *sc);
140static	void		mse_disable98m(bus_space_tag_t t, bus_space_handle_t h);
141static	void		mse_get98m(bus_space_tag_t t, bus_space_handle_t h,
142			    int *dx, int *dy, int *but);
143static	void		mse_enable98m(bus_space_tag_t t, bus_space_handle_t h);
144
145static struct mse_types mse_types[] = {
146	{ MSE_98BUSMOUSE,
147	  mse_probe98m, mse_enable98m, mse_disable98m, mse_get98m,
148	  { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
149	  { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
150	    { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
151	{ 0, },
152};
153
154static	int
155mse_probe(dev)
156	device_t dev;
157{
158	mse_softc_t *sc;
159	int error;
160	int rid;
161	int i;
162
163	/* check PnP IDs */
164	error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
165	if (error == ENXIO)
166		return error;
167
168	sc = device_get_softc(dev);
169	rid = 0;
170	sc->sc_port = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, mse_port,
171					  MSE_IOSIZE, RF_ACTIVE);
172	if (sc->sc_port == NULL)
173		return ENXIO;
174	if (isa_load_resourcev(sc->sc_port, mse_port, MSE_IOSIZE)) {
175		bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
176		return ENXIO;
177	}
178	sc->sc_iot = rman_get_bustag(sc->sc_port);
179	sc->sc_ioh = rman_get_bushandle(sc->sc_port);
180
181	/*
182	 * Check for each mouse type in the table.
183	 */
184	i = 0;
185	while (mse_types[i].m_type) {
186		if ((*mse_types[i].m_probe)(dev, sc)) {
187			sc->sc_mousetype = mse_types[i].m_type;
188			sc->sc_enablemouse = mse_types[i].m_enable;
189			sc->sc_disablemouse = mse_types[i].m_disable;
190			sc->sc_getmouse = mse_types[i].m_get;
191			sc->hw = mse_types[i].m_hw;
192			sc->mode = mse_types[i].m_mode;
193			bus_release_resource(dev, SYS_RES_IOPORT, rid,
194					     sc->sc_port);
195			device_set_desc(dev, "Bus/InPort Mouse");
196			return 0;
197		}
198		i++;
199	}
200	bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
201	return ENXIO;
202}
203
204static	int
205mse_attach(dev)
206	device_t dev;
207{
208	mse_softc_t *sc;
209	int rid;
210
211	sc = device_get_softc(dev);
212
213	rid = 0;
214	sc->sc_port = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, mse_port,
215					  MSE_IOSIZE, RF_ACTIVE);
216	if (sc->sc_port == NULL)
217		return ENXIO;
218	if (isa_load_resourcev(sc->sc_port, mse_port, MSE_IOSIZE)) {
219		bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
220		return ENXIO;
221	}
222	sc->sc_iot = rman_get_bustag(sc->sc_port);
223	sc->sc_ioh = rman_get_bushandle(sc->sc_port);
224
225	return (mse_common_attach(dev));
226}
227
228static	int
229mse_detach(dev)
230	device_t dev;
231{
232	mse_softc_t *sc;
233	int rid;
234
235	sc = device_get_softc(dev);
236	if (sc->sc_flags & MSESC_OPEN)
237		return EBUSY;
238
239	rid = 0;
240	BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
241	bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
242	bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
243
244	destroy_dev(sc->sc_dev);
245	destroy_dev(sc->sc_ndev);
246
247	return 0;
248}
249
250/*
251 * Routines for the PC98 bus mouse.
252 */
253
254/*
255 * Test for a PC98 bus mouse and return 1 if it is.
256 * (do not enable interrupts)
257 */
258static int
259mse_probe98m(dev, sc)
260	device_t dev;
261	mse_softc_t *sc;
262{
263	/* mode set */
264	bus_space_write_1(sc->sc_iot, sc->sc_ioh, MODE, 0x93);
265
266	/* initialize */
267	/* INT disable */
268	bus_space_write_1(sc->sc_iot, sc->sc_ioh, INT, INT_DISABLE);
269	/* HC = 0 */
270	bus_space_write_1(sc->sc_iot, sc->sc_ioh, HC, HC_NO_CLEAR);
271	/* HC = 1 */
272	bus_space_write_1(sc->sc_iot, sc->sc_ioh, HC, HC_CLEAR);
273
274	return (1);
275}
276
277/*
278 * Initialize PC98 bus mouse and enable interrupts.
279 */
280static void
281mse_enable98m(tag, handle)
282	bus_space_tag_t tag;
283	bus_space_handle_t handle;
284{
285	bus_space_write_1(tag, handle, INT, INT_ENABLE);    /* INT enable */
286	bus_space_write_1(tag, handle, HC, HC_NO_CLEAR);    /* HC = 0 */
287	bus_space_write_1(tag, handle, HC, HC_CLEAR);       /* HC = 1 */
288}
289
290/*
291 * Disable interrupts for PC98 Bus mouse.
292 */
293static void
294mse_disable98m(tag, handle)
295        bus_space_tag_t tag;
296        bus_space_handle_t handle;
297{
298	bus_space_write_1(tag, handle, INT, INT_DISABLE);   /* INT disable */
299	bus_space_write_1(tag, handle, HC, HC_NO_CLEAR);    /* HC = 0 */
300	bus_space_write_1(tag, handle, HC, HC_CLEAR);       /* HC = 1 */
301}
302
303/*
304 * Get current dx, dy and up/down button state.
305 */
306static void
307mse_get98m(tag, handle, dx, dy, but)
308        bus_space_tag_t tag;
309        bus_space_handle_t handle;
310        int *dx;
311        int *dy;
312        int *but;
313{
314	register char x, y;
315
316	bus_space_write_1(tag, handle, INT, INT_DISABLE);   /* INT disable */
317
318	bus_space_write_1(tag, handle, HC, HC_CLEAR);       /* HC = 1 */
319
320	/* X low */
321	bus_space_write_1(tag, handle, MSE_PORTC, 0x90 | XL);
322	x = bus_space_read_1(tag, handle, MSE_PORTA) & 0x0f;
323	/* X high */
324	bus_space_write_1(tag, handle, MSE_PORTC, 0x90 | XH);
325	x |= ((bus_space_read_1(tag, handle, MSE_PORTA)  & 0x0f) << 4);
326
327	/* Y low */
328	bus_space_write_1(tag, handle, MSE_PORTC, 0x90 | YL);
329	y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0x0f);
330	/* Y high */
331	bus_space_write_1(tag, handle, MSE_PORTC, 0x90 | YH);
332	y |= ((bus_space_read_1(tag, handle, MSE_PORTA) & 0x0f) << 4);
333
334	*but = (bus_space_read_1(tag, handle, MSE_PORTA) >> 5) & 7;
335
336	*dx = x;
337	*dy = y;
338
339	bus_space_write_1(tag, handle, HC, HC_NO_CLEAR);    /* HC = 0 */
340
341	bus_space_write_1(tag, handle, INT, INT_ENABLE);    /* INT enable */
342}
343