1 /* $NetBSD: lms.c,v 1.54 2008/07/09 20:50:41 joerg Exp $ */
4 * Copyright (c) 1993, 1994 Charles M. Hannum.
5 * Copyright (c) 1992, 1993 Erik Forsberg.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: lms.c,v 1.54 2008/07/09 20:50:41 joerg Exp $");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/ioctl.h>
32 #include <sys/device.h>
34 #include <machine/bus.h>
35 #include <machine/intr.h>
37 #include <dev/isa/isavar.h>
39 #include <dev/wscons/wsconsio.h>
40 #include <dev/wscons/wsmousevar.h>
42 #define LMS_DATA 0 /* offset for data port, read-only */
43 #define LMS_SIGN 1 /* offset for signature port, read-write */
44 #define LMS_INTR 2 /* offset for interrupt port, read-only */
45 #define LMS_CNTRL 2 /* offset for control port, write-only */
46 #define LMS_CONFIG 3 /* for configuration port, read-write */
49 struct lms_softc
{ /* driver status information */
52 bus_space_tag_t sc_iot
; /* bus i/o space identifier */
53 bus_space_handle_t sc_ioh
; /* bus i/o handle */
55 int sc_enabled
; /* device is open */
56 int oldbuttons
; /* mouse button status */
58 device_t sc_wsmousedev
;
61 static int lmsprobe(device_t
, cfdata_t
, void *);
62 static void lmsattach(device_t
, device_t
, void *);
63 static int lmsintr(void *);
65 CFATTACH_DECL_NEW(lms
, sizeof(struct lms_softc
),
66 lmsprobe
, lmsattach
, NULL
, NULL
);
68 static int lms_enable(void *);
69 static int lms_ioctl(void *, u_long
, void *, int, struct lwp
*);
70 static void lms_disable(void *);
72 static const struct wsmouse_accessops lms_accessops
= {
79 lmsprobe(device_t parent
, cfdata_t match
, void *aux
)
81 struct isa_attach_args
*ia
= aux
;
82 bus_space_tag_t iot
= ia
->ia_iot
;
83 bus_space_handle_t ioh
;
91 if (ISA_DIRECT_CONFIG(ia
))
94 /* Disallow wildcarded i/o base. */
95 if (ia
->ia_io
[0].ir_addr
== ISA_UNKNOWN_PORT
)
97 if (ia
->ia_irq
[0].ir_irq
== ISA_UNKNOWN_IRQ
)
100 /* Map the i/o space. */
101 if (bus_space_map(iot
, ia
->ia_io
[0].ir_addr
, LMS_NPORTS
, 0, &ioh
))
106 /* Configure and check for port present. */
107 bus_space_write_1(iot
, ioh
, LMS_CONFIG
, 0x91);
109 bus_space_write_1(iot
, ioh
, LMS_SIGN
, 0x0c);
111 if (bus_space_read_1(iot
, ioh
, LMS_SIGN
) != 0x0c)
113 bus_space_write_1(iot
, ioh
, LMS_SIGN
, 0x50);
115 if (bus_space_read_1(iot
, ioh
, LMS_SIGN
) != 0x50)
118 /* Disable interrupts. */
119 bus_space_write_1(iot
, ioh
, LMS_CNTRL
, 0x10);
123 ia
->ia_io
[0].ir_size
= LMS_NPORTS
;
131 bus_space_unmap(iot
, ioh
, LMS_NPORTS
);
136 lmsattach(device_t parent
, device_t self
, void *aux
)
138 struct lms_softc
*sc
= device_private(self
);
139 struct isa_attach_args
*ia
= aux
;
140 bus_space_tag_t iot
= ia
->ia_iot
;
141 bus_space_handle_t ioh
;
142 struct wsmousedev_attach_args a
;
144 aprint_naive(": Mouse\n");
145 aprint_normal(": Logitech Mouse\n");
147 if (bus_space_map(iot
, ia
->ia_io
[0].ir_addr
, LMS_NPORTS
, 0, &ioh
)) {
148 aprint_error_dev(self
, "can't map i/o space\n");
152 /* Other initialization was done by lmsprobe. */
157 sc
->sc_ih
= isa_intr_establish(ia
->ia_ic
, ia
->ia_irq
[0].ir_irq
,
158 IST_PULSE
, IPL_TTY
, lmsintr
, sc
);
160 a
.accessops
= &lms_accessops
;
164 * Attach the wsmouse, saving a handle to it.
165 * Note that we don't need to check this pointer against NULL
166 * here or in psmintr, because if this fails lms_enable() will
167 * never be called, so lmsintr() will never be called.
169 sc
->sc_wsmousedev
= config_found(self
, &a
, wsmousedevprint
);
175 struct lms_softc
*sc
= v
;
183 /* Enable interrupts. */
184 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, LMS_CNTRL
, 0);
192 struct lms_softc
*sc
= v
;
194 /* Disable interrupts. */
195 bus_space_write_1(sc
->sc_iot
, sc
->sc_ioh
, LMS_CNTRL
, 0x10);
201 lms_ioctl(void *v
, u_long cmd
, void *data
, int flag
,
205 struct lms_softc
*sc
= v
;
209 case WSMOUSEIO_GTYPE
:
210 *(u_int
*)data
= WSMOUSE_TYPE_LMS
;
213 return (EPASSTHROUGH
);
219 struct lms_softc
*sc
= arg
;
220 bus_space_tag_t iot
= sc
->sc_iot
;
221 bus_space_handle_t ioh
= sc
->sc_ioh
;
228 /* Interrupts are not expected. */
231 bus_space_write_1(iot
, ioh
, LMS_CNTRL
, 0xab);
232 hi
= bus_space_read_1(iot
, ioh
, LMS_DATA
);
233 bus_space_write_1(iot
, ioh
, LMS_CNTRL
, 0x90);
234 lo
= bus_space_read_1(iot
, ioh
, LMS_DATA
);
235 dx
= ((hi
& 0x0f) << 4) | (lo
& 0x0f);
236 /* Bounding at -127 avoids a bug in XFree86. */
237 dx
= (dx
== -128) ? -127 : dx
;
239 bus_space_write_1(iot
, ioh
, LMS_CNTRL
, 0xf0);
240 hi
= bus_space_read_1(iot
, ioh
, LMS_DATA
);
241 bus_space_write_1(iot
, ioh
, LMS_CNTRL
, 0xd0);
242 lo
= bus_space_read_1(iot
, ioh
, LMS_DATA
);
243 dy
= ((hi
& 0x0f) << 4) | (lo
& 0x0f);
244 dy
= (dy
== -128) ? 127 : -dy
;
246 bus_space_write_1(iot
, ioh
, LMS_CNTRL
, 0);
248 buttons
= ((hi
& 0x80) ? 0 : 0x1) |
249 ((hi
& 0x40) ? 0 : 0x2) |
250 ((hi
& 0x20) ? 0 : 0x4);
251 changed
= (buttons
^ sc
->oldbuttons
);
252 sc
->oldbuttons
= buttons
;
254 if (dx
|| dy
|| changed
)
255 wsmouse_input(sc
->sc_wsmousedev
,
258 WSMOUSE_INPUT_DELTA
);