Deleted Added
full compact
mse.c (111748) mse.c (111815)
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 *
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: head/sys/dev/mse/mse.c 111748 2003-03-02 16:54:40Z des $
14 * $FreeBSD: head/sys/dev/mse/mse.c 111815 2003-03-03 12:15:54Z phk $
15 */
16/*
17 * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
18 * the X386 port, courtesy of
19 * Rick Macklem, rick@snowhite.cis.uoguelph.ca
20 * Caveats: The driver currently uses spltty(), but doesn't use any
21 * generic tty code. It could use splmse() (that only masks off the
22 * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
23 * (This may be worth the effort, since the Logitech generates 30/60
24 * interrupts/sec continuously while it is open.)
25 * NB: The ATI has NOT been tested yet!
26 */
27
28/*
29 * Modification history:
30 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
31 * improved probe based on input from Logitech.
32 *
33 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
34 * fixes to make it work with Microsoft InPort busmouse
35 *
36 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
37 * added patches for new "select" interface
38 *
39 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
40 * changed position of some spl()'s in mseread
41 *
42 * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
43 * limit maximum negative x/y value to -127 to work around XFree problem
44 * that causes spurious button pushes.
45 */
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/conf.h>
50#include <sys/kernel.h>
51#include <sys/bus.h>
52#include <sys/poll.h>
53#include <sys/selinfo.h>
54#include <sys/uio.h>
55#include <sys/mouse.h>
56
57#include <machine/bus_pio.h>
58#include <machine/bus.h>
59#include <machine/clock.h>
60#include <machine/resource.h>
61#include <sys/rman.h>
62
63#include <isa/isavar.h>
64
65/* driver configuration flags (config) */
66#define MSE_CONFIG_ACCEL 0x00f0 /* acceleration factor */
67#define MSE_CONFIG_FLAGS (MSE_CONFIG_ACCEL)
68
69/*
70 * Software control structure for mouse. The sc_enablemouse(),
71 * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
72 */
73typedef struct mse_softc {
74 int sc_flags;
75 int sc_mousetype;
76 struct selinfo sc_selp;
77 struct resource *sc_port;
78 struct resource *sc_intr;
79 bus_space_tag_t sc_iot;
80 bus_space_handle_t sc_ioh;
81 void *sc_ih;
82 void (*sc_enablemouse)(bus_space_tag_t t,
83 bus_space_handle_t h);
84 void (*sc_disablemouse)(bus_space_tag_t t,
85 bus_space_handle_t h);
86 void (*sc_getmouse)(bus_space_tag_t t, bus_space_handle_t h,
87 int *dx, int *dy, int *but);
88 int sc_deltax;
89 int sc_deltay;
90 int sc_obuttons;
91 int sc_buttons;
92 int sc_bytesread;
93 u_char sc_bytes[MOUSE_SYS_PACKETSIZE];
94 struct callout_handle sc_callout;
95 int sc_watchdog;
96 dev_t sc_dev;
97 dev_t sc_ndev;
98 mousehw_t hw;
99 mousemode_t mode;
100 mousestatus_t status;
101} mse_softc_t;
102
103static devclass_t mse_devclass;
104
105static int mse_probe(device_t dev);
106static int mse_attach(device_t dev);
107static int mse_detach(device_t dev);
108
109static device_method_t mse_methods[] = {
110 DEVMETHOD(device_probe, mse_probe),
111 DEVMETHOD(device_attach, mse_attach),
112 DEVMETHOD(device_detach, mse_detach),
113 { 0, 0 }
114};
115
116static driver_t mse_driver = {
117 "mse",
118 mse_methods,
119 sizeof(mse_softc_t),
120};
121
122DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
123
124static struct isa_pnp_id mse_ids[] = {
125 { 0x000fd041, "Bus mouse" }, /* PNP0F00 */
126 { 0x020fd041, "InPort mouse" }, /* PNP0F02 */
127 { 0x0d0fd041, "InPort mouse compatible" }, /* PNP0F0D */
128 { 0x110fd041, "Bus mouse compatible" }, /* PNP0F11 */
129 { 0x150fd041, "Logitech bus mouse" }, /* PNP0F15 */
130 { 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */
131 { 0 }
132};
133
134static d_open_t mseopen;
135static d_close_t mseclose;
136static d_read_t mseread;
137static d_ioctl_t mseioctl;
138static d_poll_t msepoll;
139
140#define CDEV_MAJOR 27
141static struct cdevsw mse_cdevsw = {
15 */
16/*
17 * Driver for the Logitech and ATI Inport Bus mice for use with 386bsd and
18 * the X386 port, courtesy of
19 * Rick Macklem, rick@snowhite.cis.uoguelph.ca
20 * Caveats: The driver currently uses spltty(), but doesn't use any
21 * generic tty code. It could use splmse() (that only masks off the
22 * bus mouse interrupt, but that would require hacking in i386/isa/icu.s.
23 * (This may be worth the effort, since the Logitech generates 30/60
24 * interrupts/sec continuously while it is open.)
25 * NB: The ATI has NOT been tested yet!
26 */
27
28/*
29 * Modification history:
30 * Sep 6, 1994 -- Lars Fredriksen(fredriks@mcs.com)
31 * improved probe based on input from Logitech.
32 *
33 * Oct 19, 1992 -- E. Stark (stark@cs.sunysb.edu)
34 * fixes to make it work with Microsoft InPort busmouse
35 *
36 * Jan, 1993 -- E. Stark (stark@cs.sunysb.edu)
37 * added patches for new "select" interface
38 *
39 * May 4, 1993 -- E. Stark (stark@cs.sunysb.edu)
40 * changed position of some spl()'s in mseread
41 *
42 * October 8, 1993 -- E. Stark (stark@cs.sunysb.edu)
43 * limit maximum negative x/y value to -127 to work around XFree problem
44 * that causes spurious button pushes.
45 */
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/conf.h>
50#include <sys/kernel.h>
51#include <sys/bus.h>
52#include <sys/poll.h>
53#include <sys/selinfo.h>
54#include <sys/uio.h>
55#include <sys/mouse.h>
56
57#include <machine/bus_pio.h>
58#include <machine/bus.h>
59#include <machine/clock.h>
60#include <machine/resource.h>
61#include <sys/rman.h>
62
63#include <isa/isavar.h>
64
65/* driver configuration flags (config) */
66#define MSE_CONFIG_ACCEL 0x00f0 /* acceleration factor */
67#define MSE_CONFIG_FLAGS (MSE_CONFIG_ACCEL)
68
69/*
70 * Software control structure for mouse. The sc_enablemouse(),
71 * sc_disablemouse() and sc_getmouse() routines must be called spl'd().
72 */
73typedef struct mse_softc {
74 int sc_flags;
75 int sc_mousetype;
76 struct selinfo sc_selp;
77 struct resource *sc_port;
78 struct resource *sc_intr;
79 bus_space_tag_t sc_iot;
80 bus_space_handle_t sc_ioh;
81 void *sc_ih;
82 void (*sc_enablemouse)(bus_space_tag_t t,
83 bus_space_handle_t h);
84 void (*sc_disablemouse)(bus_space_tag_t t,
85 bus_space_handle_t h);
86 void (*sc_getmouse)(bus_space_tag_t t, bus_space_handle_t h,
87 int *dx, int *dy, int *but);
88 int sc_deltax;
89 int sc_deltay;
90 int sc_obuttons;
91 int sc_buttons;
92 int sc_bytesread;
93 u_char sc_bytes[MOUSE_SYS_PACKETSIZE];
94 struct callout_handle sc_callout;
95 int sc_watchdog;
96 dev_t sc_dev;
97 dev_t sc_ndev;
98 mousehw_t hw;
99 mousemode_t mode;
100 mousestatus_t status;
101} mse_softc_t;
102
103static devclass_t mse_devclass;
104
105static int mse_probe(device_t dev);
106static int mse_attach(device_t dev);
107static int mse_detach(device_t dev);
108
109static device_method_t mse_methods[] = {
110 DEVMETHOD(device_probe, mse_probe),
111 DEVMETHOD(device_attach, mse_attach),
112 DEVMETHOD(device_detach, mse_detach),
113 { 0, 0 }
114};
115
116static driver_t mse_driver = {
117 "mse",
118 mse_methods,
119 sizeof(mse_softc_t),
120};
121
122DRIVER_MODULE(mse, isa, mse_driver, mse_devclass, 0, 0);
123
124static struct isa_pnp_id mse_ids[] = {
125 { 0x000fd041, "Bus mouse" }, /* PNP0F00 */
126 { 0x020fd041, "InPort mouse" }, /* PNP0F02 */
127 { 0x0d0fd041, "InPort mouse compatible" }, /* PNP0F0D */
128 { 0x110fd041, "Bus mouse compatible" }, /* PNP0F11 */
129 { 0x150fd041, "Logitech bus mouse" }, /* PNP0F15 */
130 { 0x180fd041, "Logitech bus mouse compatible" },/* PNP0F18 */
131 { 0 }
132};
133
134static d_open_t mseopen;
135static d_close_t mseclose;
136static d_read_t mseread;
137static d_ioctl_t mseioctl;
138static d_poll_t msepoll;
139
140#define CDEV_MAJOR 27
141static struct cdevsw mse_cdevsw = {
142 /* open */ mseopen,
143 /* close */ mseclose,
144 /* read */ mseread,
145 /* write */ nowrite,
146 /* ioctl */ mseioctl,
147 /* poll */ msepoll,
148 /* mmap */ nommap,
149 /* strategy */ nostrategy,
150 /* name */ "mse",
151 /* maj */ CDEV_MAJOR,
152 /* dump */ nodump,
153 /* psize */ nopsize,
154 /* flags */ 0,
142 .d_open = mseopen,
143 .d_close = mseclose,
144 .d_read = mseread,
145 .d_ioctl = mseioctl,
146 .d_poll = msepoll,
147 .d_name = "mse",
148 .d_maj = CDEV_MAJOR,
155};
156
157static void mseintr(void *);
158static timeout_t msetimeout;
159
160/* Flags */
161#define MSESC_OPEN 0x1
162#define MSESC_WANT 0x2
163
164/* and Mouse Types */
165#define MSE_NONE 0 /* don't move this! */
166#define MSE_LOGITECH 0x1
167#define MSE_ATIINPORT 0x2
168#define MSE_LOGI_SIG 0xA5
169
170#define MSE_PORTA 0
171#define MSE_PORTB 1
172#define MSE_PORTC 2
173#define MSE_PORTD 3
174#define MSE_IOSIZE 4
175
176#define MSE_UNIT(dev) (minor(dev) >> 1)
177#define MSE_NBLOCKIO(dev) (minor(dev) & 0x1)
178
179/*
180 * Logitech bus mouse definitions
181 */
182#define MSE_SETUP 0x91 /* What does this mean? */
183 /* The definition for the control port */
184 /* is as follows: */
185
186 /* D7 = Mode set flag (1 = active) */
187 /* D6,D5 = Mode selection (port A) */
188 /* 00 = Mode 0 = Basic I/O */
189 /* 01 = Mode 1 = Strobed I/O */
190 /* 10 = Mode 2 = Bi-dir bus */
191 /* D4 = Port A direction (1 = input)*/
192 /* D3 = Port C (upper 4 bits) */
193 /* direction. (1 = input) */
194 /* D2 = Mode selection (port B & C) */
195 /* 0 = Mode 0 = Basic I/O */
196 /* 1 = Mode 1 = Strobed I/O */
197 /* D1 = Port B direction (1 = input)*/
198 /* D0 = Port C (lower 4 bits) */
199 /* direction. (1 = input) */
200
201 /* So 91 means Basic I/O on all 3 ports,*/
202 /* Port A is an input port, B is an */
203 /* output port, C is split with upper */
204 /* 4 bits being an output port and lower*/
205 /* 4 bits an input port, and enable the */
206 /* sucker. */
207 /* Courtesy Intel 8255 databook. Lars */
208#define MSE_HOLD 0x80
209#define MSE_RXLOW 0x00
210#define MSE_RXHIGH 0x20
211#define MSE_RYLOW 0x40
212#define MSE_RYHIGH 0x60
213#define MSE_DISINTR 0x10
214#define MSE_INTREN 0x00
215
216static int mse_probelogi(device_t dev, mse_softc_t *sc);
217static void mse_disablelogi(bus_space_tag_t t,
218 bus_space_handle_t h);
219static void mse_getlogi(bus_space_tag_t t, bus_space_handle_t h,
220 int *dx, int *dy, int *but);
221static void mse_enablelogi(bus_space_tag_t t,
222 bus_space_handle_t h);
223
224/*
225 * ATI Inport mouse definitions
226 */
227#define MSE_INPORT_RESET 0x80
228#define MSE_INPORT_STATUS 0x00
229#define MSE_INPORT_DX 0x01
230#define MSE_INPORT_DY 0x02
231#define MSE_INPORT_MODE 0x07
232#define MSE_INPORT_HOLD 0x20
233#define MSE_INPORT_INTREN 0x09
234
235static int mse_probeati(device_t dev, mse_softc_t *sc);
236static void mse_enableati(bus_space_tag_t t, bus_space_handle_t h);
237static void mse_disableati(bus_space_tag_t t, bus_space_handle_t h);
238static void mse_getati(bus_space_tag_t t, bus_space_handle_t h,
239 int *dx, int *dy, int *but);
240
241#define MSEPRI (PZERO + 3)
242
243/*
244 * Table of mouse types.
245 * Keep the Logitech last, since I haven't figured out how to probe it
246 * properly yet. (Someday I'll have the documentation.)
247 */
248static struct mse_types {
249 int m_type; /* Type of bus mouse */
250 int (*m_probe)(device_t dev, mse_softc_t *sc);
251 /* Probe routine to test for it */
252 void (*m_enable)(bus_space_tag_t t, bus_space_handle_t h);
253 /* Start routine */
254 void (*m_disable)(bus_space_tag_t t, bus_space_handle_t h);
255 /* Disable interrupts routine */
256 void (*m_get)(bus_space_tag_t t, bus_space_handle_t h, int *dx,
257 int *dy, int *but);
258 /* and get mouse status */
259 mousehw_t m_hw; /* buttons iftype type model hwid */
260 mousemode_t m_mode; /* proto rate res accel level size mask */
261} mse_types[] = {
262 { MSE_ATIINPORT,
263 mse_probeati, mse_enableati, mse_disableati, mse_getati,
264 { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
265 { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
266 { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
267 { MSE_LOGITECH,
268 mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,
269 { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
270 { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
271 { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
272 { 0, },
273};
274
275static int
276mse_probe(dev)
277 device_t dev;
278{
279 mse_softc_t *sc;
280 int error;
281 int rid;
282 int i;
283
284 /* check PnP IDs */
285 error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
286 if (error == ENXIO)
287 return error;
288
289 sc = device_get_softc(dev);
290 rid = 0;
291 sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
292 MSE_IOSIZE, RF_ACTIVE);
293 if (sc->sc_port == NULL)
294 return ENXIO;
295 sc->sc_iot = rman_get_bustag(sc->sc_port);
296 sc->sc_ioh = rman_get_bushandle(sc->sc_port);
297
298 /*
299 * Check for each mouse type in the table.
300 */
301 i = 0;
302 while (mse_types[i].m_type) {
303 if ((*mse_types[i].m_probe)(dev, sc)) {
304 sc->sc_mousetype = mse_types[i].m_type;
305 sc->sc_enablemouse = mse_types[i].m_enable;
306 sc->sc_disablemouse = mse_types[i].m_disable;
307 sc->sc_getmouse = mse_types[i].m_get;
308 sc->hw = mse_types[i].m_hw;
309 sc->mode = mse_types[i].m_mode;
310 bus_release_resource(dev, SYS_RES_IOPORT, rid,
311 sc->sc_port);
312 device_set_desc(dev, "Bus/InPort Mouse");
313 return 0;
314 }
315 i++;
316 }
317 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
318 return ENXIO;
319}
320
321static int
322mse_attach(dev)
323 device_t dev;
324{
325 mse_softc_t *sc;
326 int flags;
327 int unit;
328 int rid;
329
330 sc = device_get_softc(dev);
331 unit = device_get_unit(dev);
332
333 rid = 0;
334 sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
335 MSE_IOSIZE, RF_ACTIVE);
336 if (sc->sc_port == NULL)
337 return ENXIO;
338 sc->sc_intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
339 RF_ACTIVE);
340 if (sc->sc_intr == NULL) {
341 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
342 return ENXIO;
343 }
344 sc->sc_iot = rman_get_bustag(sc->sc_port);
345 sc->sc_ioh = rman_get_bushandle(sc->sc_port);
346
347 if (BUS_SETUP_INTR(device_get_parent(dev), dev, sc->sc_intr,
348 INTR_TYPE_TTY, mseintr, sc, &sc->sc_ih)) {
349 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
350 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
351 return ENXIO;
352 }
353
354 flags = device_get_flags(dev);
355 sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
356 callout_handle_init(&sc->sc_callout);
357
358 sc->sc_dev = make_dev(&mse_cdevsw, unit << 1, 0, 0, 0600,
359 "mse%d", unit);
360 sc->sc_ndev = make_dev(&mse_cdevsw, (unit<<1)+1, 0, 0, 0600,
361 "nmse%d", unit);
362
363 return 0;
364}
365
366static int
367mse_detach(dev)
368 device_t dev;
369{
370 mse_softc_t *sc;
371 int rid;
372
373 sc = device_get_softc(dev);
374 if (sc->sc_flags & MSESC_OPEN)
375 return EBUSY;
376
377 rid = 0;
378 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
379 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
380 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
381
382 destroy_dev(sc->sc_dev);
383 destroy_dev(sc->sc_ndev);
384
385 return 0;
386}
387
388/*
389 * Exclusive open the mouse, initialize it and enable interrupts.
390 */
391static int
392mseopen(dev, flags, fmt, td)
393 dev_t dev;
394 int flags;
395 int fmt;
396 struct thread *td;
397{
398 mse_softc_t *sc;
399 int s;
400
401 sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
402 if (sc == NULL)
403 return (ENXIO);
404 if (sc->sc_mousetype == MSE_NONE)
405 return (ENXIO);
406 if (sc->sc_flags & MSESC_OPEN)
407 return (EBUSY);
408 sc->sc_flags |= MSESC_OPEN;
409 sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
410 sc->sc_deltax = sc->sc_deltay = 0;
411 sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
412 sc->sc_watchdog = FALSE;
413 sc->sc_callout = timeout(msetimeout, dev, hz*2);
414 sc->mode.level = 0;
415 sc->status.flags = 0;
416 sc->status.button = sc->status.obutton = 0;
417 sc->status.dx = sc->status.dy = sc->status.dz = 0;
418
419 /*
420 * Initialize mouse interface and enable interrupts.
421 */
422 s = spltty();
423 (*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
424 splx(s);
425 return (0);
426}
427
428/*
429 * mseclose: just turn off mouse innterrupts.
430 */
431static int
432mseclose(dev, flags, fmt, td)
433 dev_t dev;
434 int flags;
435 int fmt;
436 struct thread *td;
437{
438 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
439 int s;
440
441 untimeout(msetimeout, dev, sc->sc_callout);
442 callout_handle_init(&sc->sc_callout);
443 s = spltty();
444 (*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
445 sc->sc_flags &= ~MSESC_OPEN;
446 splx(s);
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 */
455static int
456mseread(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, s, error;
463
464 /*
465 * If there are no protocol bytes to be read, set up a new protocol
466 * packet.
467 */
468 s = spltty(); /* 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 splx(s);
474 return (0);
475 }
476 sc->sc_flags |= MSESC_WANT;
477 error = tsleep(sc, MSEPRI | PCATCH,
478 "mseread", 0);
479 if (error) {
480 splx(s);
481 return (error);
482 }
483 }
484
485 /*
486 * Generate protocol bytes.
487 * For some reason X386 expects 5 bytes but never uses
488 * the fourth or fifth?
489 */
490 sc->sc_bytes[0] = sc->mode.syncmask[1]
491 | (sc->sc_buttons & ~sc->mode.syncmask[0]);
492 if (sc->sc_deltax > 127)
493 sc->sc_deltax = 127;
494 if (sc->sc_deltax < -127)
495 sc->sc_deltax = -127;
496 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
497 if (sc->sc_deltay > 127)
498 sc->sc_deltay = 127;
499 if (sc->sc_deltay < -127)
500 sc->sc_deltay = -127;
501 sc->sc_bytes[1] = sc->sc_deltax;
502 sc->sc_bytes[2] = sc->sc_deltay;
503 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
504 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
505 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
506 sc->sc_obuttons = sc->sc_buttons;
507 sc->sc_deltax = sc->sc_deltay = 0;
508 sc->sc_bytesread = 0;
509 }
510 splx(s);
511 xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
512 error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
513 if (error)
514 return (error);
515 sc->sc_bytesread += xfer;
516 return(0);
517}
518
519/*
520 * mseioctl: process ioctl commands.
521 */
522static int
523mseioctl(dev, cmd, addr, flag, td)
524 dev_t dev;
525 u_long cmd;
526 caddr_t addr;
527 int flag;
528 struct thread *td;
529{
530 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
531 mousestatus_t status;
532 int err = 0;
533 int s;
534
535 switch (cmd) {
536
537 case MOUSE_GETHWINFO:
538 s = spltty();
539 *(mousehw_t *)addr = sc->hw;
540 if (sc->mode.level == 0)
541 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
542 splx(s);
543 break;
544
545 case MOUSE_GETMODE:
546 s = spltty();
547 *(mousemode_t *)addr = sc->mode;
548 switch (sc->mode.level) {
549 case 0:
550 break;
551 case 1:
552 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
553 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
554 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
555 break;
556 }
557 splx(s);
558 break;
559
560 case MOUSE_SETMODE:
561 switch (((mousemode_t *)addr)->level) {
562 case 0:
563 case 1:
564 break;
565 default:
566 return (EINVAL);
567 }
568 if (((mousemode_t *)addr)->accelfactor < -1)
569 return (EINVAL);
570 else if (((mousemode_t *)addr)->accelfactor >= 0)
571 sc->mode.accelfactor =
572 ((mousemode_t *)addr)->accelfactor;
573 sc->mode.level = ((mousemode_t *)addr)->level;
574 switch (sc->mode.level) {
575 case 0:
576 sc->sc_bytesread = sc->mode.packetsize
577 = MOUSE_MSC_PACKETSIZE;
578 break;
579 case 1:
580 sc->sc_bytesread = sc->mode.packetsize
581 = MOUSE_SYS_PACKETSIZE;
582 break;
583 }
584 break;
585
586 case MOUSE_GETLEVEL:
587 *(int *)addr = sc->mode.level;
588 break;
589
590 case MOUSE_SETLEVEL:
591 switch (*(int *)addr) {
592 case 0:
593 sc->mode.level = *(int *)addr;
594 sc->sc_bytesread = sc->mode.packetsize
595 = MOUSE_MSC_PACKETSIZE;
596 break;
597 case 1:
598 sc->mode.level = *(int *)addr;
599 sc->sc_bytesread = sc->mode.packetsize
600 = MOUSE_SYS_PACKETSIZE;
601 break;
602 default:
603 return (EINVAL);
604 }
605 break;
606
607 case MOUSE_GETSTATUS:
608 s = spltty();
609 status = sc->status;
610 sc->status.flags = 0;
611 sc->status.obutton = sc->status.button;
612 sc->status.button = 0;
613 sc->status.dx = 0;
614 sc->status.dy = 0;
615 sc->status.dz = 0;
616 splx(s);
617 *(mousestatus_t *)addr = status;
618 break;
619
620 case MOUSE_READSTATE:
621 case MOUSE_READDATA:
622 return (ENODEV);
623
624#if (defined(MOUSE_GETVARS))
625 case MOUSE_GETVARS:
626 case MOUSE_SETVARS:
627 return (ENODEV);
628#endif
629
630 default:
631 return (ENOTTY);
632 }
633 return (err);
634}
635
636/*
637 * msepoll: check for mouse input to be processed.
638 */
639static int
640msepoll(dev, events, td)
641 dev_t dev;
642 int events;
643 struct thread *td;
644{
645 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
646 int s;
647 int revents = 0;
648
649 s = spltty();
650 if (events & (POLLIN | POLLRDNORM)) {
651 if (sc->sc_bytesread != sc->mode.packetsize ||
652 sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
653 (sc->sc_obuttons ^ sc->sc_buttons) != 0)
654 revents |= events & (POLLIN | POLLRDNORM);
655 else {
656 /*
657 * Since this is an exclusive open device, any previous
658 * proc pointer is trash now, so we can just assign it.
659 */
660 selrecord(td, &sc->sc_selp);
661 }
662 }
663 splx(s);
664 return (revents);
665}
666
667/*
668 * msetimeout: watchdog timer routine.
669 */
670static void
671msetimeout(arg)
672 void *arg;
673{
674 dev_t dev;
675 mse_softc_t *sc;
676
677 dev = (dev_t)arg;
678 sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
679 if (sc->sc_watchdog) {
680 if (bootverbose)
681 printf("mse%d: lost interrupt?\n", MSE_UNIT(dev));
682 mseintr(sc);
683 }
684 sc->sc_watchdog = TRUE;
685 sc->sc_callout = timeout(msetimeout, dev, hz);
686}
687
688/*
689 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
690 */
691static void
692mseintr(arg)
693 void *arg;
694{
695 /*
696 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
697 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
698 */
699 static int butmap[8] = {
700 0,
701 MOUSE_BUTTON3DOWN,
702 MOUSE_BUTTON2DOWN,
703 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
704 MOUSE_BUTTON1DOWN,
705 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
706 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
707 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
708 };
709 mse_softc_t *sc = arg;
710 int dx, dy, but;
711 int sign;
712
713#ifdef DEBUG
714 static int mse_intrcnt = 0;
715 if((mse_intrcnt++ % 10000) == 0)
716 printf("mseintr\n");
717#endif /* DEBUG */
718 if ((sc->sc_flags & MSESC_OPEN) == 0)
719 return;
720
721 (*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
722 if (sc->mode.accelfactor > 0) {
723 sign = (dx < 0);
724 dx = dx * dx / sc->mode.accelfactor;
725 if (dx == 0)
726 dx = 1;
727 if (sign)
728 dx = -dx;
729 sign = (dy < 0);
730 dy = dy * dy / sc->mode.accelfactor;
731 if (dy == 0)
732 dy = 1;
733 if (sign)
734 dy = -dy;
735 }
736 sc->sc_deltax += dx;
737 sc->sc_deltay += dy;
738 sc->sc_buttons = but;
739
740 but = butmap[~but & MOUSE_MSC_BUTTONS];
741 sc->status.dx += dx;
742 sc->status.dy += dy;
743 sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
744 | (sc->status.button ^ but);
745 sc->status.button = but;
746
747 sc->sc_watchdog = FALSE;
748
749 /*
750 * If mouse state has changed, wake up anyone wanting to know.
751 */
752 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
753 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
754 if (sc->sc_flags & MSESC_WANT) {
755 sc->sc_flags &= ~MSESC_WANT;
756 wakeup(sc);
757 }
758 selwakeup(&sc->sc_selp);
759 }
760}
761
762/*
763 * Routines for the Logitech mouse.
764 */
765/*
766 * Test for a Logitech bus mouse and return 1 if it is.
767 * (until I know how to use the signature port properly, just disable
768 * interrupts and return 1)
769 */
770static int
771mse_probelogi(dev, sc)
772 device_t dev;
773 mse_softc_t *sc;
774{
775
776 int sig;
777
778 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP);
779 /* set the signature port */
780 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG);
781
782 DELAY(30000); /* 30 ms delay */
783 sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF;
784 if (sig == MSE_LOGI_SIG) {
785 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC,
786 MSE_DISINTR);
787 return(1);
788 } else {
789 if (bootverbose)
790 device_printf(dev, "wrong signature %x\n", sig);
791 return(0);
792 }
793}
794
795/*
796 * Initialize Logitech mouse and enable interrupts.
797 */
798static void
799mse_enablelogi(tag, handle)
800 bus_space_tag_t tag;
801 bus_space_handle_t handle;
802{
803 int dx, dy, but;
804
805 bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP);
806 mse_getlogi(tag, handle, &dx, &dy, &but);
807}
808
809/*
810 * Disable interrupts for Logitech mouse.
811 */
812static void
813mse_disablelogi(tag, handle)
814 bus_space_tag_t tag;
815 bus_space_handle_t handle;
816{
817
818 bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR);
819}
820
821/*
822 * Get the current dx, dy and button up/down state.
823 */
824static void
825mse_getlogi(tag, handle, dx, dy, but)
826 bus_space_tag_t tag;
827 bus_space_handle_t handle;
828 int *dx;
829 int *dy;
830 int *but;
831{
832 register char x, y;
833
834 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
835 x = bus_space_read_1(tag, handle, MSE_PORTA);
836 *but = (x >> 5) & MOUSE_MSC_BUTTONS;
837 x &= 0xf;
838 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
839 x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
840 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
841 y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf);
842 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
843 y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
844 *dx = x;
845 *dy = y;
846 bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN);
847}
848
849/*
850 * Routines for the ATI Inport bus mouse.
851 */
852/*
853 * Test for an ATI Inport bus mouse and return 1 if it is.
854 * (do not enable interrupts)
855 */
856static int
857mse_probeati(dev, sc)
858 device_t dev;
859 mse_softc_t *sc;
860{
861 int i;
862
863 for (i = 0; i < 2; i++)
864 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde)
865 return (1);
866 return (0);
867}
868
869/*
870 * Initialize ATI Inport mouse and enable interrupts.
871 */
872static void
873mse_enableati(tag, handle)
874 bus_space_tag_t tag;
875 bus_space_handle_t handle;
876{
877
878 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET);
879 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
880 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
881}
882
883/*
884 * Disable interrupts for ATI Inport mouse.
885 */
886static void
887mse_disableati(tag, handle)
888 bus_space_tag_t tag;
889 bus_space_handle_t handle;
890{
891
892 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
893 bus_space_write_1(tag, handle, MSE_PORTB, 0);
894}
895
896/*
897 * Get current dx, dy and up/down button state.
898 */
899static void
900mse_getati(tag, handle, dx, dy, but)
901 bus_space_tag_t tag;
902 bus_space_handle_t handle;
903 int *dx;
904 int *dy;
905 int *but;
906{
907 register char byte;
908
909 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
910 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD);
911 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS);
912 *but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS;
913 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX);
914 byte = bus_space_read_1(tag, handle, MSE_PORTB);
915 *dx = byte;
916 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY);
917 byte = bus_space_read_1(tag, handle, MSE_PORTB);
918 *dy = byte;
919 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
920 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
921}
149};
150
151static void mseintr(void *);
152static timeout_t msetimeout;
153
154/* Flags */
155#define MSESC_OPEN 0x1
156#define MSESC_WANT 0x2
157
158/* and Mouse Types */
159#define MSE_NONE 0 /* don't move this! */
160#define MSE_LOGITECH 0x1
161#define MSE_ATIINPORT 0x2
162#define MSE_LOGI_SIG 0xA5
163
164#define MSE_PORTA 0
165#define MSE_PORTB 1
166#define MSE_PORTC 2
167#define MSE_PORTD 3
168#define MSE_IOSIZE 4
169
170#define MSE_UNIT(dev) (minor(dev) >> 1)
171#define MSE_NBLOCKIO(dev) (minor(dev) & 0x1)
172
173/*
174 * Logitech bus mouse definitions
175 */
176#define MSE_SETUP 0x91 /* What does this mean? */
177 /* The definition for the control port */
178 /* is as follows: */
179
180 /* D7 = Mode set flag (1 = active) */
181 /* D6,D5 = Mode selection (port A) */
182 /* 00 = Mode 0 = Basic I/O */
183 /* 01 = Mode 1 = Strobed I/O */
184 /* 10 = Mode 2 = Bi-dir bus */
185 /* D4 = Port A direction (1 = input)*/
186 /* D3 = Port C (upper 4 bits) */
187 /* direction. (1 = input) */
188 /* D2 = Mode selection (port B & C) */
189 /* 0 = Mode 0 = Basic I/O */
190 /* 1 = Mode 1 = Strobed I/O */
191 /* D1 = Port B direction (1 = input)*/
192 /* D0 = Port C (lower 4 bits) */
193 /* direction. (1 = input) */
194
195 /* So 91 means Basic I/O on all 3 ports,*/
196 /* Port A is an input port, B is an */
197 /* output port, C is split with upper */
198 /* 4 bits being an output port and lower*/
199 /* 4 bits an input port, and enable the */
200 /* sucker. */
201 /* Courtesy Intel 8255 databook. Lars */
202#define MSE_HOLD 0x80
203#define MSE_RXLOW 0x00
204#define MSE_RXHIGH 0x20
205#define MSE_RYLOW 0x40
206#define MSE_RYHIGH 0x60
207#define MSE_DISINTR 0x10
208#define MSE_INTREN 0x00
209
210static int mse_probelogi(device_t dev, mse_softc_t *sc);
211static void mse_disablelogi(bus_space_tag_t t,
212 bus_space_handle_t h);
213static void mse_getlogi(bus_space_tag_t t, bus_space_handle_t h,
214 int *dx, int *dy, int *but);
215static void mse_enablelogi(bus_space_tag_t t,
216 bus_space_handle_t h);
217
218/*
219 * ATI Inport mouse definitions
220 */
221#define MSE_INPORT_RESET 0x80
222#define MSE_INPORT_STATUS 0x00
223#define MSE_INPORT_DX 0x01
224#define MSE_INPORT_DY 0x02
225#define MSE_INPORT_MODE 0x07
226#define MSE_INPORT_HOLD 0x20
227#define MSE_INPORT_INTREN 0x09
228
229static int mse_probeati(device_t dev, mse_softc_t *sc);
230static void mse_enableati(bus_space_tag_t t, bus_space_handle_t h);
231static void mse_disableati(bus_space_tag_t t, bus_space_handle_t h);
232static void mse_getati(bus_space_tag_t t, bus_space_handle_t h,
233 int *dx, int *dy, int *but);
234
235#define MSEPRI (PZERO + 3)
236
237/*
238 * Table of mouse types.
239 * Keep the Logitech last, since I haven't figured out how to probe it
240 * properly yet. (Someday I'll have the documentation.)
241 */
242static struct mse_types {
243 int m_type; /* Type of bus mouse */
244 int (*m_probe)(device_t dev, mse_softc_t *sc);
245 /* Probe routine to test for it */
246 void (*m_enable)(bus_space_tag_t t, bus_space_handle_t h);
247 /* Start routine */
248 void (*m_disable)(bus_space_tag_t t, bus_space_handle_t h);
249 /* Disable interrupts routine */
250 void (*m_get)(bus_space_tag_t t, bus_space_handle_t h, int *dx,
251 int *dy, int *but);
252 /* and get mouse status */
253 mousehw_t m_hw; /* buttons iftype type model hwid */
254 mousemode_t m_mode; /* proto rate res accel level size mask */
255} mse_types[] = {
256 { MSE_ATIINPORT,
257 mse_probeati, mse_enableati, mse_disableati, mse_getati,
258 { 2, MOUSE_IF_INPORT, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
259 { MOUSE_PROTO_INPORT, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
260 { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
261 { MSE_LOGITECH,
262 mse_probelogi, mse_enablelogi, mse_disablelogi, mse_getlogi,
263 { 2, MOUSE_IF_BUS, MOUSE_MOUSE, MOUSE_MODEL_GENERIC, 0, },
264 { MOUSE_PROTO_BUS, -1, -1, 0, 0, MOUSE_MSC_PACKETSIZE,
265 { MOUSE_MSC_SYNCMASK, MOUSE_MSC_SYNC, }, }, },
266 { 0, },
267};
268
269static int
270mse_probe(dev)
271 device_t dev;
272{
273 mse_softc_t *sc;
274 int error;
275 int rid;
276 int i;
277
278 /* check PnP IDs */
279 error = ISA_PNP_PROBE(device_get_parent(dev), dev, mse_ids);
280 if (error == ENXIO)
281 return error;
282
283 sc = device_get_softc(dev);
284 rid = 0;
285 sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
286 MSE_IOSIZE, RF_ACTIVE);
287 if (sc->sc_port == NULL)
288 return ENXIO;
289 sc->sc_iot = rman_get_bustag(sc->sc_port);
290 sc->sc_ioh = rman_get_bushandle(sc->sc_port);
291
292 /*
293 * Check for each mouse type in the table.
294 */
295 i = 0;
296 while (mse_types[i].m_type) {
297 if ((*mse_types[i].m_probe)(dev, sc)) {
298 sc->sc_mousetype = mse_types[i].m_type;
299 sc->sc_enablemouse = mse_types[i].m_enable;
300 sc->sc_disablemouse = mse_types[i].m_disable;
301 sc->sc_getmouse = mse_types[i].m_get;
302 sc->hw = mse_types[i].m_hw;
303 sc->mode = mse_types[i].m_mode;
304 bus_release_resource(dev, SYS_RES_IOPORT, rid,
305 sc->sc_port);
306 device_set_desc(dev, "Bus/InPort Mouse");
307 return 0;
308 }
309 i++;
310 }
311 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
312 return ENXIO;
313}
314
315static int
316mse_attach(dev)
317 device_t dev;
318{
319 mse_softc_t *sc;
320 int flags;
321 int unit;
322 int rid;
323
324 sc = device_get_softc(dev);
325 unit = device_get_unit(dev);
326
327 rid = 0;
328 sc->sc_port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0,
329 MSE_IOSIZE, RF_ACTIVE);
330 if (sc->sc_port == NULL)
331 return ENXIO;
332 sc->sc_intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
333 RF_ACTIVE);
334 if (sc->sc_intr == NULL) {
335 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
336 return ENXIO;
337 }
338 sc->sc_iot = rman_get_bustag(sc->sc_port);
339 sc->sc_ioh = rman_get_bushandle(sc->sc_port);
340
341 if (BUS_SETUP_INTR(device_get_parent(dev), dev, sc->sc_intr,
342 INTR_TYPE_TTY, mseintr, sc, &sc->sc_ih)) {
343 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
344 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
345 return ENXIO;
346 }
347
348 flags = device_get_flags(dev);
349 sc->mode.accelfactor = (flags & MSE_CONFIG_ACCEL) >> 4;
350 callout_handle_init(&sc->sc_callout);
351
352 sc->sc_dev = make_dev(&mse_cdevsw, unit << 1, 0, 0, 0600,
353 "mse%d", unit);
354 sc->sc_ndev = make_dev(&mse_cdevsw, (unit<<1)+1, 0, 0, 0600,
355 "nmse%d", unit);
356
357 return 0;
358}
359
360static int
361mse_detach(dev)
362 device_t dev;
363{
364 mse_softc_t *sc;
365 int rid;
366
367 sc = device_get_softc(dev);
368 if (sc->sc_flags & MSESC_OPEN)
369 return EBUSY;
370
371 rid = 0;
372 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, sc->sc_intr, sc->sc_ih);
373 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->sc_intr);
374 bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->sc_port);
375
376 destroy_dev(sc->sc_dev);
377 destroy_dev(sc->sc_ndev);
378
379 return 0;
380}
381
382/*
383 * Exclusive open the mouse, initialize it and enable interrupts.
384 */
385static int
386mseopen(dev, flags, fmt, td)
387 dev_t dev;
388 int flags;
389 int fmt;
390 struct thread *td;
391{
392 mse_softc_t *sc;
393 int s;
394
395 sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
396 if (sc == NULL)
397 return (ENXIO);
398 if (sc->sc_mousetype == MSE_NONE)
399 return (ENXIO);
400 if (sc->sc_flags & MSESC_OPEN)
401 return (EBUSY);
402 sc->sc_flags |= MSESC_OPEN;
403 sc->sc_obuttons = sc->sc_buttons = MOUSE_MSC_BUTTONS;
404 sc->sc_deltax = sc->sc_deltay = 0;
405 sc->sc_bytesread = sc->mode.packetsize = MOUSE_MSC_PACKETSIZE;
406 sc->sc_watchdog = FALSE;
407 sc->sc_callout = timeout(msetimeout, dev, hz*2);
408 sc->mode.level = 0;
409 sc->status.flags = 0;
410 sc->status.button = sc->status.obutton = 0;
411 sc->status.dx = sc->status.dy = sc->status.dz = 0;
412
413 /*
414 * Initialize mouse interface and enable interrupts.
415 */
416 s = spltty();
417 (*sc->sc_enablemouse)(sc->sc_iot, sc->sc_ioh);
418 splx(s);
419 return (0);
420}
421
422/*
423 * mseclose: just turn off mouse innterrupts.
424 */
425static int
426mseclose(dev, flags, fmt, td)
427 dev_t dev;
428 int flags;
429 int fmt;
430 struct thread *td;
431{
432 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
433 int s;
434
435 untimeout(msetimeout, dev, sc->sc_callout);
436 callout_handle_init(&sc->sc_callout);
437 s = spltty();
438 (*sc->sc_disablemouse)(sc->sc_iot, sc->sc_ioh);
439 sc->sc_flags &= ~MSESC_OPEN;
440 splx(s);
441 return(0);
442}
443
444/*
445 * mseread: return mouse info using the MSC serial protocol, but without
446 * using bytes 4 and 5.
447 * (Yes this is cheesy, but it makes the X386 server happy, so...)
448 */
449static int
450mseread(dev, uio, ioflag)
451 dev_t dev;
452 struct uio *uio;
453 int ioflag;
454{
455 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
456 int xfer, s, error;
457
458 /*
459 * If there are no protocol bytes to be read, set up a new protocol
460 * packet.
461 */
462 s = spltty(); /* XXX Should be its own spl, but where is imlXX() */
463 if (sc->sc_bytesread >= sc->mode.packetsize) {
464 while (sc->sc_deltax == 0 && sc->sc_deltay == 0 &&
465 (sc->sc_obuttons ^ sc->sc_buttons) == 0) {
466 if (MSE_NBLOCKIO(dev)) {
467 splx(s);
468 return (0);
469 }
470 sc->sc_flags |= MSESC_WANT;
471 error = tsleep(sc, MSEPRI | PCATCH,
472 "mseread", 0);
473 if (error) {
474 splx(s);
475 return (error);
476 }
477 }
478
479 /*
480 * Generate protocol bytes.
481 * For some reason X386 expects 5 bytes but never uses
482 * the fourth or fifth?
483 */
484 sc->sc_bytes[0] = sc->mode.syncmask[1]
485 | (sc->sc_buttons & ~sc->mode.syncmask[0]);
486 if (sc->sc_deltax > 127)
487 sc->sc_deltax = 127;
488 if (sc->sc_deltax < -127)
489 sc->sc_deltax = -127;
490 sc->sc_deltay = -sc->sc_deltay; /* Otherwise mousey goes wrong way */
491 if (sc->sc_deltay > 127)
492 sc->sc_deltay = 127;
493 if (sc->sc_deltay < -127)
494 sc->sc_deltay = -127;
495 sc->sc_bytes[1] = sc->sc_deltax;
496 sc->sc_bytes[2] = sc->sc_deltay;
497 sc->sc_bytes[3] = sc->sc_bytes[4] = 0;
498 sc->sc_bytes[5] = sc->sc_bytes[6] = 0;
499 sc->sc_bytes[7] = MOUSE_SYS_EXTBUTTONS;
500 sc->sc_obuttons = sc->sc_buttons;
501 sc->sc_deltax = sc->sc_deltay = 0;
502 sc->sc_bytesread = 0;
503 }
504 splx(s);
505 xfer = min(uio->uio_resid, sc->mode.packetsize - sc->sc_bytesread);
506 error = uiomove(&sc->sc_bytes[sc->sc_bytesread], xfer, uio);
507 if (error)
508 return (error);
509 sc->sc_bytesread += xfer;
510 return(0);
511}
512
513/*
514 * mseioctl: process ioctl commands.
515 */
516static int
517mseioctl(dev, cmd, addr, flag, td)
518 dev_t dev;
519 u_long cmd;
520 caddr_t addr;
521 int flag;
522 struct thread *td;
523{
524 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
525 mousestatus_t status;
526 int err = 0;
527 int s;
528
529 switch (cmd) {
530
531 case MOUSE_GETHWINFO:
532 s = spltty();
533 *(mousehw_t *)addr = sc->hw;
534 if (sc->mode.level == 0)
535 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
536 splx(s);
537 break;
538
539 case MOUSE_GETMODE:
540 s = spltty();
541 *(mousemode_t *)addr = sc->mode;
542 switch (sc->mode.level) {
543 case 0:
544 break;
545 case 1:
546 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
547 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
548 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
549 break;
550 }
551 splx(s);
552 break;
553
554 case MOUSE_SETMODE:
555 switch (((mousemode_t *)addr)->level) {
556 case 0:
557 case 1:
558 break;
559 default:
560 return (EINVAL);
561 }
562 if (((mousemode_t *)addr)->accelfactor < -1)
563 return (EINVAL);
564 else if (((mousemode_t *)addr)->accelfactor >= 0)
565 sc->mode.accelfactor =
566 ((mousemode_t *)addr)->accelfactor;
567 sc->mode.level = ((mousemode_t *)addr)->level;
568 switch (sc->mode.level) {
569 case 0:
570 sc->sc_bytesread = sc->mode.packetsize
571 = MOUSE_MSC_PACKETSIZE;
572 break;
573 case 1:
574 sc->sc_bytesread = sc->mode.packetsize
575 = MOUSE_SYS_PACKETSIZE;
576 break;
577 }
578 break;
579
580 case MOUSE_GETLEVEL:
581 *(int *)addr = sc->mode.level;
582 break;
583
584 case MOUSE_SETLEVEL:
585 switch (*(int *)addr) {
586 case 0:
587 sc->mode.level = *(int *)addr;
588 sc->sc_bytesread = sc->mode.packetsize
589 = MOUSE_MSC_PACKETSIZE;
590 break;
591 case 1:
592 sc->mode.level = *(int *)addr;
593 sc->sc_bytesread = sc->mode.packetsize
594 = MOUSE_SYS_PACKETSIZE;
595 break;
596 default:
597 return (EINVAL);
598 }
599 break;
600
601 case MOUSE_GETSTATUS:
602 s = spltty();
603 status = sc->status;
604 sc->status.flags = 0;
605 sc->status.obutton = sc->status.button;
606 sc->status.button = 0;
607 sc->status.dx = 0;
608 sc->status.dy = 0;
609 sc->status.dz = 0;
610 splx(s);
611 *(mousestatus_t *)addr = status;
612 break;
613
614 case MOUSE_READSTATE:
615 case MOUSE_READDATA:
616 return (ENODEV);
617
618#if (defined(MOUSE_GETVARS))
619 case MOUSE_GETVARS:
620 case MOUSE_SETVARS:
621 return (ENODEV);
622#endif
623
624 default:
625 return (ENOTTY);
626 }
627 return (err);
628}
629
630/*
631 * msepoll: check for mouse input to be processed.
632 */
633static int
634msepoll(dev, events, td)
635 dev_t dev;
636 int events;
637 struct thread *td;
638{
639 mse_softc_t *sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
640 int s;
641 int revents = 0;
642
643 s = spltty();
644 if (events & (POLLIN | POLLRDNORM)) {
645 if (sc->sc_bytesread != sc->mode.packetsize ||
646 sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
647 (sc->sc_obuttons ^ sc->sc_buttons) != 0)
648 revents |= events & (POLLIN | POLLRDNORM);
649 else {
650 /*
651 * Since this is an exclusive open device, any previous
652 * proc pointer is trash now, so we can just assign it.
653 */
654 selrecord(td, &sc->sc_selp);
655 }
656 }
657 splx(s);
658 return (revents);
659}
660
661/*
662 * msetimeout: watchdog timer routine.
663 */
664static void
665msetimeout(arg)
666 void *arg;
667{
668 dev_t dev;
669 mse_softc_t *sc;
670
671 dev = (dev_t)arg;
672 sc = devclass_get_softc(mse_devclass, MSE_UNIT(dev));
673 if (sc->sc_watchdog) {
674 if (bootverbose)
675 printf("mse%d: lost interrupt?\n", MSE_UNIT(dev));
676 mseintr(sc);
677 }
678 sc->sc_watchdog = TRUE;
679 sc->sc_callout = timeout(msetimeout, dev, hz);
680}
681
682/*
683 * mseintr: update mouse status. sc_deltax and sc_deltay are accumulative.
684 */
685static void
686mseintr(arg)
687 void *arg;
688{
689 /*
690 * the table to turn MouseSystem button bits (MOUSE_MSC_BUTTON?UP)
691 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
692 */
693 static int butmap[8] = {
694 0,
695 MOUSE_BUTTON3DOWN,
696 MOUSE_BUTTON2DOWN,
697 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
698 MOUSE_BUTTON1DOWN,
699 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
700 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
701 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
702 };
703 mse_softc_t *sc = arg;
704 int dx, dy, but;
705 int sign;
706
707#ifdef DEBUG
708 static int mse_intrcnt = 0;
709 if((mse_intrcnt++ % 10000) == 0)
710 printf("mseintr\n");
711#endif /* DEBUG */
712 if ((sc->sc_flags & MSESC_OPEN) == 0)
713 return;
714
715 (*sc->sc_getmouse)(sc->sc_iot, sc->sc_ioh, &dx, &dy, &but);
716 if (sc->mode.accelfactor > 0) {
717 sign = (dx < 0);
718 dx = dx * dx / sc->mode.accelfactor;
719 if (dx == 0)
720 dx = 1;
721 if (sign)
722 dx = -dx;
723 sign = (dy < 0);
724 dy = dy * dy / sc->mode.accelfactor;
725 if (dy == 0)
726 dy = 1;
727 if (sign)
728 dy = -dy;
729 }
730 sc->sc_deltax += dx;
731 sc->sc_deltay += dy;
732 sc->sc_buttons = but;
733
734 but = butmap[~but & MOUSE_MSC_BUTTONS];
735 sc->status.dx += dx;
736 sc->status.dy += dy;
737 sc->status.flags |= ((dx || dy) ? MOUSE_POSCHANGED : 0)
738 | (sc->status.button ^ but);
739 sc->status.button = but;
740
741 sc->sc_watchdog = FALSE;
742
743 /*
744 * If mouse state has changed, wake up anyone wanting to know.
745 */
746 if (sc->sc_deltax != 0 || sc->sc_deltay != 0 ||
747 (sc->sc_obuttons ^ sc->sc_buttons) != 0) {
748 if (sc->sc_flags & MSESC_WANT) {
749 sc->sc_flags &= ~MSESC_WANT;
750 wakeup(sc);
751 }
752 selwakeup(&sc->sc_selp);
753 }
754}
755
756/*
757 * Routines for the Logitech mouse.
758 */
759/*
760 * Test for a Logitech bus mouse and return 1 if it is.
761 * (until I know how to use the signature port properly, just disable
762 * interrupts and return 1)
763 */
764static int
765mse_probelogi(dev, sc)
766 device_t dev;
767 mse_softc_t *sc;
768{
769
770 int sig;
771
772 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTD, MSE_SETUP);
773 /* set the signature port */
774 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB, MSE_LOGI_SIG);
775
776 DELAY(30000); /* 30 ms delay */
777 sig = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTB) & 0xFF;
778 if (sig == MSE_LOGI_SIG) {
779 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC,
780 MSE_DISINTR);
781 return(1);
782 } else {
783 if (bootverbose)
784 device_printf(dev, "wrong signature %x\n", sig);
785 return(0);
786 }
787}
788
789/*
790 * Initialize Logitech mouse and enable interrupts.
791 */
792static void
793mse_enablelogi(tag, handle)
794 bus_space_tag_t tag;
795 bus_space_handle_t handle;
796{
797 int dx, dy, but;
798
799 bus_space_write_1(tag, handle, MSE_PORTD, MSE_SETUP);
800 mse_getlogi(tag, handle, &dx, &dy, &but);
801}
802
803/*
804 * Disable interrupts for Logitech mouse.
805 */
806static void
807mse_disablelogi(tag, handle)
808 bus_space_tag_t tag;
809 bus_space_handle_t handle;
810{
811
812 bus_space_write_1(tag, handle, MSE_PORTC, MSE_DISINTR);
813}
814
815/*
816 * Get the current dx, dy and button up/down state.
817 */
818static void
819mse_getlogi(tag, handle, dx, dy, but)
820 bus_space_tag_t tag;
821 bus_space_handle_t handle;
822 int *dx;
823 int *dy;
824 int *but;
825{
826 register char x, y;
827
828 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXLOW);
829 x = bus_space_read_1(tag, handle, MSE_PORTA);
830 *but = (x >> 5) & MOUSE_MSC_BUTTONS;
831 x &= 0xf;
832 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RXHIGH);
833 x |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
834 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYLOW);
835 y = (bus_space_read_1(tag, handle, MSE_PORTA) & 0xf);
836 bus_space_write_1(tag, handle, MSE_PORTC, MSE_HOLD | MSE_RYHIGH);
837 y |= (bus_space_read_1(tag, handle, MSE_PORTA) << 4);
838 *dx = x;
839 *dy = y;
840 bus_space_write_1(tag, handle, MSE_PORTC, MSE_INTREN);
841}
842
843/*
844 * Routines for the ATI Inport bus mouse.
845 */
846/*
847 * Test for an ATI Inport bus mouse and return 1 if it is.
848 * (do not enable interrupts)
849 */
850static int
851mse_probeati(dev, sc)
852 device_t dev;
853 mse_softc_t *sc;
854{
855 int i;
856
857 for (i = 0; i < 2; i++)
858 if (bus_space_read_1(sc->sc_iot, sc->sc_ioh, MSE_PORTC) == 0xde)
859 return (1);
860 return (0);
861}
862
863/*
864 * Initialize ATI Inport mouse and enable interrupts.
865 */
866static void
867mse_enableati(tag, handle)
868 bus_space_tag_t tag;
869 bus_space_handle_t handle;
870{
871
872 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_RESET);
873 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
874 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
875}
876
877/*
878 * Disable interrupts for ATI Inport mouse.
879 */
880static void
881mse_disableati(tag, handle)
882 bus_space_tag_t tag;
883 bus_space_handle_t handle;
884{
885
886 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
887 bus_space_write_1(tag, handle, MSE_PORTB, 0);
888}
889
890/*
891 * Get current dx, dy and up/down button state.
892 */
893static void
894mse_getati(tag, handle, dx, dy, but)
895 bus_space_tag_t tag;
896 bus_space_handle_t handle;
897 int *dx;
898 int *dy;
899 int *but;
900{
901 register char byte;
902
903 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
904 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_HOLD);
905 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_STATUS);
906 *but = ~bus_space_read_1(tag, handle, MSE_PORTB) & MOUSE_MSC_BUTTONS;
907 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DX);
908 byte = bus_space_read_1(tag, handle, MSE_PORTB);
909 *dx = byte;
910 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_DY);
911 byte = bus_space_read_1(tag, handle, MSE_PORTB);
912 *dy = byte;
913 bus_space_write_1(tag, handle, MSE_PORTA, MSE_INPORT_MODE);
914 bus_space_write_1(tag, handle, MSE_PORTB, MSE_INPORT_INTREN);
915}