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