FreeBSD/Linux Kernel Cross Reference
sys/isa/psm.c
1 /*-
2 * Copyright (c) 1992, 1993 Erik Forsberg.
3 * Copyright (c) 1996, 1997 Kazutaka YOKOTA.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * THIS SOFTWARE IS PROVIDED BY ``AS IS'' AND ANY EXPRESS OR IMPLIED
13 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
15 * NO EVENT SHALL I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 */
23 /*
24 * Ported to 386bsd Oct 17, 1992
25 * Sandi Donno, Computer Science, University of Cape Town, South Africa
26 * Please send bug reports to sandi@cs.uct.ac.za
27 *
28 * Thanks are also due to Rick Macklem, rick@snowhite.cis.uoguelph.ca -
29 * although I was only partially successful in getting the alpha release
30 * of his "driver for the Logitech and ATI Inport Bus mice for use with
31 * 386bsd and the X386 port" to work with my Microsoft mouse, I nevertheless
32 * found his code to be an invaluable reference when porting this driver
33 * to 386bsd.
34 *
35 * Further modifications for latest 386BSD+patchkit and port to NetBSD,
36 * Andrew Herbert <andrew@werple.apana.org.au> - 8 June 1993
37 *
38 * Cloned from the Microsoft Bus Mouse driver, also by Erik Forsberg, by
39 * Andrew Herbert - 12 June 1993
40 *
41 * Modified for PS/2 mouse by Charles Hannum <mycroft@ai.mit.edu>
42 * - 13 June 1993
43 *
44 * Modified for PS/2 AUX mouse by Shoji Yuen <yuen@nuie.nagoya-u.ac.jp>
45 * - 24 October 1993
46 *
47 * Hardware access routines and probe logic rewritten by
48 * Kazutaka Yokota <yokota@zodiac.mech.utsunomiya-u.ac.jp>
49 * - 3, 14, 22 October 1996.
50 * - 12 November 1996. IOCTLs and rearranging `psmread', `psmioctl'...
51 * - 14, 30 November 1996. Uses `kbdio.c'.
52 * - 13 December 1996. Uses queuing version of `kbdio.c'.
53 * - January/February 1997. Tweaked probe logic for
54 * HiNote UltraII/Latitude/Armada laptops.
55 * - 30 July 1997. Added APM support.
56 * - 5 March 1997. Defined driver configuration flags (PSM_CONFIG_XXX).
57 * Improved sync check logic.
58 * Vendor specific support routines.
59 */
60
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD: releng/5.2/sys/isa/psm.c 122352 2003-11-09 09:17:26Z tanimura $");
63
64 #include "opt_psm.h"
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/module.h>
70 #include <sys/bus.h>
71 #include <sys/conf.h>
72 #include <sys/poll.h>
73 #include <sys/syslog.h>
74 #include <machine/bus.h>
75 #include <sys/rman.h>
76 #include <sys/selinfo.h>
77 #include <sys/time.h>
78 #include <sys/uio.h>
79
80 #include <sys/limits.h>
81 #include <sys/mouse.h>
82 #include <machine/resource.h>
83
84 #include <isa/isavar.h>
85 #include <dev/kbd/atkbdcreg.h>
86
87 /*
88 * Driver specific options: the following options may be set by
89 * `options' statements in the kernel configuration file.
90 */
91
92 /* debugging */
93 #ifndef PSM_DEBUG
94 #define PSM_DEBUG 0 /* logging: 0: none, 1: brief, 2: verbose */
95 #endif
96
97 #ifndef PSM_SYNCERR_THRESHOLD1
98 #define PSM_SYNCERR_THRESHOLD1 20
99 #endif
100
101 #ifndef PSM_INPUT_TIMEOUT
102 #define PSM_INPUT_TIMEOUT 2000000 /* 2 sec */
103 #endif
104
105 /* end of driver specific options */
106
107 #define PSM_DRIVER_NAME "psm"
108 #define PSMCPNP_DRIVER_NAME "psmcpnp"
109
110 /* input queue */
111 #define PSM_BUFSIZE 960
112 #define PSM_SMALLBUFSIZE 240
113
114 /* operation levels */
115 #define PSM_LEVEL_BASE 0
116 #define PSM_LEVEL_STANDARD 1
117 #define PSM_LEVEL_NATIVE 2
118 #define PSM_LEVEL_MIN PSM_LEVEL_BASE
119 #define PSM_LEVEL_MAX PSM_LEVEL_NATIVE
120
121 /* Logitech PS2++ protocol */
122 #define MOUSE_PS2PLUS_CHECKBITS(b) \
123 ((((b[2] & 0x03) << 2) | 0x02) == (b[1] & 0x0f))
124 #define MOUSE_PS2PLUS_PACKET_TYPE(b) \
125 (((b[0] & 0x30) >> 2) | ((b[1] & 0x30) >> 4))
126
127 /* some macros */
128 #define PSM_UNIT(dev) (minor(dev) >> 1)
129 #define PSM_NBLOCKIO(dev) (minor(dev) & 1)
130 #define PSM_MKMINOR(unit,block) (((unit) << 1) | ((block) ? 0:1))
131
132 /* ring buffer */
133 typedef struct ringbuf {
134 int count; /* # of valid elements in the buffer */
135 int head; /* head pointer */
136 int tail; /* tail poiner */
137 unsigned char buf[PSM_BUFSIZE];
138 } ringbuf_t;
139
140 /* driver control block */
141 struct psm_softc { /* Driver status information */
142 int unit;
143 struct selinfo rsel; /* Process selecting for Input */
144 unsigned char state; /* Mouse driver state */
145 int config; /* driver configuration flags */
146 int flags; /* other flags */
147 KBDC kbdc; /* handle to access the keyboard controller */
148 struct resource *intr; /* IRQ resource */
149 void *ih; /* interrupt handle */
150 mousehw_t hw; /* hardware information */
151 mousemode_t mode; /* operation mode */
152 mousemode_t dflt_mode; /* default operation mode */
153 mousestatus_t status; /* accumulated mouse movement */
154 ringbuf_t queue; /* mouse status queue */
155 unsigned char ipacket[16]; /* interim input buffer */
156 int inputbytes; /* # of bytes in the input buffer */
157 int button; /* the latest button state */
158 int xold; /* previous absolute X position */
159 int yold; /* previous absolute Y position */
160 int syncerrors;
161 struct timeval inputtimeout;
162 int watchdog; /* watchdog timer flag */
163 struct callout_handle callout; /* watchdog timer call out */
164 dev_t dev;
165 dev_t bdev;
166 };
167 static devclass_t psm_devclass;
168 #define PSM_SOFTC(unit) ((struct psm_softc*)devclass_get_softc(psm_devclass, unit))
169
170 /* driver state flags (state) */
171 #define PSM_VALID 0x80
172 #define PSM_OPEN 1 /* Device is open */
173 #define PSM_ASLP 2 /* Waiting for mouse data */
174
175 /* driver configuration flags (config) */
176 #define PSM_CONFIG_RESOLUTION 0x000f /* resolution */
177 #define PSM_CONFIG_ACCEL 0x00f0 /* acceleration factor */
178 #define PSM_CONFIG_NOCHECKSYNC 0x0100 /* disable sync. test */
179 #define PSM_CONFIG_NOIDPROBE 0x0200 /* disable mouse model probe */
180 #define PSM_CONFIG_NORESET 0x0400 /* don't reset the mouse */
181 #define PSM_CONFIG_FORCETAP 0x0800 /* assume `tap' action exists */
182 #define PSM_CONFIG_IGNPORTERROR 0x1000 /* ignore error in aux port test */
183 #define PSM_CONFIG_HOOKRESUME 0x2000 /* hook the system resume event */
184 #define PSM_CONFIG_INITAFTERSUSPEND 0x4000 /* init the device at the resume event */
185 #define PSM_CONFIG_SYNCHACK 0x8000 /* enable `out-of-sync' hack */
186
187 #define PSM_CONFIG_FLAGS (PSM_CONFIG_RESOLUTION \
188 | PSM_CONFIG_ACCEL \
189 | PSM_CONFIG_NOCHECKSYNC \
190 | PSM_CONFIG_SYNCHACK \
191 | PSM_CONFIG_NOIDPROBE \
192 | PSM_CONFIG_NORESET \
193 | PSM_CONFIG_FORCETAP \
194 | PSM_CONFIG_IGNPORTERROR \
195 | PSM_CONFIG_HOOKRESUME \
196 | PSM_CONFIG_INITAFTERSUSPEND)
197
198 /* other flags (flags) */
199 #define PSM_FLAGS_FINGERDOWN 0x0001 /* VersaPad finger down */
200
201 /* for backward compatibility */
202 #define OLD_MOUSE_GETHWINFO _IOR('M', 1, old_mousehw_t)
203 #define OLD_MOUSE_GETMODE _IOR('M', 2, old_mousemode_t)
204 #define OLD_MOUSE_SETMODE _IOW('M', 3, old_mousemode_t)
205
206 typedef struct old_mousehw {
207 int buttons;
208 int iftype;
209 int type;
210 int hwid;
211 } old_mousehw_t;
212
213 typedef struct old_mousemode {
214 int protocol;
215 int rate;
216 int resolution;
217 int accelfactor;
218 } old_mousemode_t;
219
220 /* packet formatting function */
221 typedef int packetfunc_t(struct psm_softc *, unsigned char *,
222 int *, int, mousestatus_t *);
223
224 /* function prototypes */
225 static void psmidentify(driver_t *, device_t);
226 static int psmprobe(device_t);
227 static int psmattach(device_t);
228 static int psmdetach(device_t);
229 static int psmresume(device_t);
230
231 static d_open_t psmopen;
232 static d_close_t psmclose;
233 static d_read_t psmread;
234 static d_ioctl_t psmioctl;
235 static d_poll_t psmpoll;
236
237 static int enable_aux_dev(KBDC);
238 static int disable_aux_dev(KBDC);
239 static int get_mouse_status(KBDC, int *, int, int);
240 static int get_aux_id(KBDC);
241 static int set_mouse_sampling_rate(KBDC, int);
242 static int set_mouse_scaling(KBDC, int);
243 static int set_mouse_resolution(KBDC, int);
244 static int set_mouse_mode(KBDC);
245 static int get_mouse_buttons(KBDC);
246 static int is_a_mouse(int);
247 static void recover_from_error(KBDC);
248 static int restore_controller(KBDC, int);
249 static int doinitialize(struct psm_softc *, mousemode_t *);
250 static int doopen(struct psm_softc *, int);
251 static int reinitialize(struct psm_softc *, int);
252 static char *model_name(int);
253 static void psmintr(void *);
254 static void psmtimeout(void *);
255
256 /* vendor specific features */
257 typedef int probefunc_t(struct psm_softc *);
258
259 static int mouse_id_proc1(KBDC, int, int, int *);
260 static int mouse_ext_command(KBDC, int);
261 static probefunc_t enable_groller;
262 static probefunc_t enable_gmouse;
263 static probefunc_t enable_aglide;
264 static probefunc_t enable_kmouse;
265 static probefunc_t enable_msexplorer;
266 static probefunc_t enable_msintelli;
267 static probefunc_t enable_4dmouse;
268 static probefunc_t enable_4dplus;
269 static probefunc_t enable_mmanplus;
270 static probefunc_t enable_versapad;
271 static int tame_mouse(struct psm_softc *, mousestatus_t *, unsigned char *);
272
273 static struct {
274 int model;
275 unsigned char syncmask;
276 int packetsize;
277 probefunc_t *probefunc;
278 } vendortype[] = {
279 /*
280 * WARNING: the order of probe is very important. Don't mess it
281 * unless you know what you are doing.
282 */
283 { MOUSE_MODEL_NET, /* Genius NetMouse */
284 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_gmouse, },
285 { MOUSE_MODEL_NETSCROLL, /* Genius NetScroll */
286 0xc8, 6, enable_groller, },
287 { MOUSE_MODEL_MOUSEMANPLUS, /* Logitech MouseMan+ */
288 0x08, MOUSE_PS2_PACKETSIZE, enable_mmanplus, },
289 { MOUSE_MODEL_EXPLORER, /* Microsoft IntelliMouse Explorer */
290 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msexplorer, },
291 { MOUSE_MODEL_4D, /* A4 Tech 4D Mouse */
292 0x08, MOUSE_4D_PACKETSIZE, enable_4dmouse, },
293 { MOUSE_MODEL_4DPLUS, /* A4 Tech 4D+ Mouse */
294 0xc8, MOUSE_4DPLUS_PACKETSIZE, enable_4dplus, },
295 { MOUSE_MODEL_INTELLI, /* Microsoft IntelliMouse */
296 0x08, MOUSE_PS2INTELLI_PACKETSIZE, enable_msintelli, },
297 { MOUSE_MODEL_GLIDEPOINT, /* ALPS GlidePoint */
298 0xc0, MOUSE_PS2_PACKETSIZE, enable_aglide, },
299 { MOUSE_MODEL_THINK, /* Kensignton ThinkingMouse */
300 0x80, MOUSE_PS2_PACKETSIZE, enable_kmouse, },
301 { MOUSE_MODEL_VERSAPAD, /* Interlink electronics VersaPad */
302 0xe8, MOUSE_PS2VERSA_PACKETSIZE, enable_versapad, },
303 { MOUSE_MODEL_GENERIC,
304 0xc0, MOUSE_PS2_PACKETSIZE, NULL, },
305 };
306 #define GENERIC_MOUSE_ENTRY ((sizeof(vendortype) / sizeof(*vendortype)) - 1)
307
308 /* device driver declarateion */
309 static device_method_t psm_methods[] = {
310 /* Device interface */
311 DEVMETHOD(device_identify, psmidentify),
312 DEVMETHOD(device_probe, psmprobe),
313 DEVMETHOD(device_attach, psmattach),
314 DEVMETHOD(device_detach, psmdetach),
315 DEVMETHOD(device_resume, psmresume),
316
317 { 0, 0 }
318 };
319
320 static driver_t psm_driver = {
321 PSM_DRIVER_NAME,
322 psm_methods,
323 sizeof(struct psm_softc),
324 };
325
326 #define CDEV_MAJOR 21
327
328 static struct cdevsw psm_cdevsw = {
329 .d_open = psmopen,
330 .d_close = psmclose,
331 .d_read = psmread,
332 .d_ioctl = psmioctl,
333 .d_poll = psmpoll,
334 .d_name = PSM_DRIVER_NAME,
335 .d_maj = CDEV_MAJOR,
336 };
337
338 /* debug message level */
339 static int verbose = PSM_DEBUG;
340
341 /* device I/O routines */
342 static int
343 enable_aux_dev(KBDC kbdc)
344 {
345 int res;
346
347 res = send_aux_command(kbdc, PSMC_ENABLE_DEV);
348 if (verbose >= 2)
349 log(LOG_DEBUG, "psm: ENABLE_DEV return code:%04x\n", res);
350
351 return (res == PSM_ACK);
352 }
353
354 static int
355 disable_aux_dev(KBDC kbdc)
356 {
357 int res;
358
359 res = send_aux_command(kbdc, PSMC_DISABLE_DEV);
360 if (verbose >= 2)
361 log(LOG_DEBUG, "psm: DISABLE_DEV return code:%04x\n", res);
362
363 return (res == PSM_ACK);
364 }
365
366 static int
367 get_mouse_status(KBDC kbdc, int *status, int flag, int len)
368 {
369 int cmd;
370 int res;
371 int i;
372
373 switch (flag) {
374 case 0:
375 default:
376 cmd = PSMC_SEND_DEV_STATUS;
377 break;
378 case 1:
379 cmd = PSMC_SEND_DEV_DATA;
380 break;
381 }
382 empty_aux_buffer(kbdc, 5);
383 res = send_aux_command(kbdc, cmd);
384 if (verbose >= 2)
385 log(LOG_DEBUG, "psm: SEND_AUX_DEV_%s return code:%04x\n",
386 (flag == 1) ? "DATA" : "STATUS", res);
387 if (res != PSM_ACK)
388 return 0;
389
390 for (i = 0; i < len; ++i) {
391 status[i] = read_aux_data(kbdc);
392 if (status[i] < 0)
393 break;
394 }
395
396 if (verbose) {
397 log(LOG_DEBUG, "psm: %s %02x %02x %02x\n",
398 (flag == 1) ? "data" : "status", status[0], status[1], status[2]);
399 }
400
401 return i;
402 }
403
404 static int
405 get_aux_id(KBDC kbdc)
406 {
407 int res;
408 int id;
409
410 empty_aux_buffer(kbdc, 5);
411 res = send_aux_command(kbdc, PSMC_SEND_DEV_ID);
412 if (verbose >= 2)
413 log(LOG_DEBUG, "psm: SEND_DEV_ID return code:%04x\n", res);
414 if (res != PSM_ACK)
415 return (-1);
416
417 /* 10ms delay */
418 DELAY(10000);
419
420 id = read_aux_data(kbdc);
421 if (verbose >= 2)
422 log(LOG_DEBUG, "psm: device ID: %04x\n", id);
423
424 return id;
425 }
426
427 static int
428 set_mouse_sampling_rate(KBDC kbdc, int rate)
429 {
430 int res;
431
432 res = send_aux_command_and_data(kbdc, PSMC_SET_SAMPLING_RATE, rate);
433 if (verbose >= 2)
434 log(LOG_DEBUG, "psm: SET_SAMPLING_RATE (%d) %04x\n", rate, res);
435
436 return ((res == PSM_ACK) ? rate : -1);
437 }
438
439 static int
440 set_mouse_scaling(KBDC kbdc, int scale)
441 {
442 int res;
443
444 switch (scale) {
445 case 1:
446 default:
447 scale = PSMC_SET_SCALING11;
448 break;
449 case 2:
450 scale = PSMC_SET_SCALING21;
451 break;
452 }
453 res = send_aux_command(kbdc, scale);
454 if (verbose >= 2)
455 log(LOG_DEBUG, "psm: SET_SCALING%s return code:%04x\n",
456 (scale == PSMC_SET_SCALING21) ? "21" : "11", res);
457
458 return (res == PSM_ACK);
459 }
460
461 /* `val' must be 0 through PSMD_MAX_RESOLUTION */
462 static int
463 set_mouse_resolution(KBDC kbdc, int val)
464 {
465 int res;
466
467 res = send_aux_command_and_data(kbdc, PSMC_SET_RESOLUTION, val);
468 if (verbose >= 2)
469 log(LOG_DEBUG, "psm: SET_RESOLUTION (%d) %04x\n", val, res);
470
471 return ((res == PSM_ACK) ? val : -1);
472 }
473
474 /*
475 * NOTE: once `set_mouse_mode()' is called, the mouse device must be
476 * re-enabled by calling `enable_aux_dev()'
477 */
478 static int
479 set_mouse_mode(KBDC kbdc)
480 {
481 int res;
482
483 res = send_aux_command(kbdc, PSMC_SET_STREAM_MODE);
484 if (verbose >= 2)
485 log(LOG_DEBUG, "psm: SET_STREAM_MODE return code:%04x\n", res);
486
487 return (res == PSM_ACK);
488 }
489
490 static int
491 get_mouse_buttons(KBDC kbdc)
492 {
493 int c = 2; /* assume two buttons by default */
494 int status[3];
495
496 /*
497 * NOTE: a special sequence to obtain Logitech Mouse specific
498 * information: set resolution to 25 ppi, set scaling to 1:1, set
499 * scaling to 1:1, set scaling to 1:1. Then the second byte of the
500 * mouse status bytes is the number of available buttons.
501 * Some manufactures also support this sequence.
502 */
503 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
504 return c;
505 if (set_mouse_scaling(kbdc, 1) && set_mouse_scaling(kbdc, 1)
506 && set_mouse_scaling(kbdc, 1)
507 && (get_mouse_status(kbdc, status, 0, 3) >= 3)) {
508 if (status[1] != 0)
509 return status[1];
510 }
511 return c;
512 }
513
514 /* misc subroutines */
515 /*
516 * Someday, I will get the complete list of valid pointing devices and
517 * their IDs... XXX
518 */
519 static int
520 is_a_mouse(int id)
521 {
522 #if 0
523 static int valid_ids[] = {
524 PSM_MOUSE_ID, /* mouse */
525 PSM_BALLPOINT_ID, /* ballpoint device */
526 PSM_INTELLI_ID, /* Intellimouse */
527 PSM_EXPLORER_ID, /* Intellimouse Explorer */
528 -1 /* end of table */
529 };
530 int i;
531
532 for (i = 0; valid_ids[i] >= 0; ++i)
533 if (valid_ids[i] == id)
534 return TRUE;
535 return FALSE;
536 #else
537 return TRUE;
538 #endif
539 }
540
541 static char *
542 model_name(int model)
543 {
544 static struct {
545 int model_code;
546 char *model_name;
547 } models[] = {
548 { MOUSE_MODEL_NETSCROLL, "NetScroll" },
549 { MOUSE_MODEL_NET, "NetMouse/NetScroll Optical" },
550 { MOUSE_MODEL_GLIDEPOINT, "GlidePoint" },
551 { MOUSE_MODEL_THINK, "ThinkingMouse" },
552 { MOUSE_MODEL_INTELLI, "IntelliMouse" },
553 { MOUSE_MODEL_MOUSEMANPLUS, "MouseMan+" },
554 { MOUSE_MODEL_VERSAPAD, "VersaPad" },
555 { MOUSE_MODEL_EXPLORER, "IntelliMouse Explorer" },
556 { MOUSE_MODEL_4D, "4D Mouse" },
557 { MOUSE_MODEL_4DPLUS, "4D+ Mouse" },
558 { MOUSE_MODEL_GENERIC, "Generic PS/2 mouse" },
559 { MOUSE_MODEL_UNKNOWN, NULL },
560 };
561 int i;
562
563 for (i = 0; models[i].model_code != MOUSE_MODEL_UNKNOWN; ++i) {
564 if (models[i].model_code == model)
565 return models[i].model_name;
566 }
567 return "Unknown";
568 }
569
570 static void
571 recover_from_error(KBDC kbdc)
572 {
573 /* discard anything left in the output buffer */
574 empty_both_buffers(kbdc, 10);
575
576 #if 0
577 /*
578 * NOTE: KBDC_RESET_KBD may not restore the communication between the
579 * keyboard and the controller.
580 */
581 reset_kbd(kbdc);
582 #else
583 /*
584 * NOTE: somehow diagnostic and keyboard port test commands bring the
585 * keyboard back.
586 */
587 if (!test_controller(kbdc))
588 log(LOG_ERR, "psm: keyboard controller failed.\n");
589 /* if there isn't a keyboard in the system, the following error is OK */
590 if (test_kbd_port(kbdc) != 0) {
591 if (verbose)
592 log(LOG_ERR, "psm: keyboard port failed.\n");
593 }
594 #endif
595 }
596
597 static int
598 restore_controller(KBDC kbdc, int command_byte)
599 {
600 empty_both_buffers(kbdc, 10);
601
602 if (!set_controller_command_byte(kbdc, 0xff, command_byte)) {
603 log(LOG_ERR, "psm: failed to restore the keyboard controller "
604 "command byte.\n");
605 empty_both_buffers(kbdc, 10);
606 return FALSE;
607 } else {
608 empty_both_buffers(kbdc, 10);
609 return TRUE;
610 }
611 }
612
613 /*
614 * Re-initialize the aux port and device. The aux port must be enabled
615 * and its interrupt must be disabled before calling this routine.
616 * The aux device will be disabled before returning.
617 * The keyboard controller must be locked via `kbdc_lock()' before
618 * calling this routine.
619 */
620 static int
621 doinitialize(struct psm_softc *sc, mousemode_t *mode)
622 {
623 KBDC kbdc = sc->kbdc;
624 int stat[3];
625 int i;
626
627 switch((i = test_aux_port(kbdc))) {
628 case 1: /* ignore this error */
629 case PSM_ACK:
630 if (verbose)
631 log(LOG_DEBUG, "psm%d: strange result for test aux port (%d).\n",
632 sc->unit, i);
633 /* FALLTHROUGH */
634 case 0: /* no error */
635 break;
636 case -1: /* time out */
637 default: /* error */
638 recover_from_error(kbdc);
639 if (sc->config & PSM_CONFIG_IGNPORTERROR)
640 break;
641 log(LOG_ERR, "psm%d: the aux port is not functioning (%d).\n",
642 sc->unit, i);
643 return FALSE;
644 }
645
646 if (sc->config & PSM_CONFIG_NORESET) {
647 /*
648 * Don't try to reset the pointing device. It may possibly be
649 * left in the unknown state, though...
650 */
651 } else {
652 /*
653 * NOTE: some controllers appears to hang the `keyboard' when
654 * the aux port doesn't exist and `PSMC_RESET_DEV' is issued.
655 */
656 if (!reset_aux_dev(kbdc)) {
657 recover_from_error(kbdc);
658 log(LOG_ERR, "psm%d: failed to reset the aux device.\n", sc->unit);
659 return FALSE;
660 }
661 }
662
663 /*
664 * both the aux port and the aux device is functioning, see
665 * if the device can be enabled.
666 */
667 if (!enable_aux_dev(kbdc) || !disable_aux_dev(kbdc)) {
668 log(LOG_ERR, "psm%d: failed to enable the aux device.\n", sc->unit);
669 return FALSE;
670 }
671 empty_both_buffers(kbdc, 10); /* remove stray data if any */
672
673 if (sc->config & PSM_CONFIG_NOIDPROBE) {
674 i = GENERIC_MOUSE_ENTRY;
675 } else {
676 /* FIXME: hardware ID, mouse buttons? */
677
678 /* other parameters */
679 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
680 if ((*vendortype[i].probefunc)(sc)) {
681 if (verbose >= 2)
682 log(LOG_ERR, "psm%d: found %s\n",
683 sc->unit, model_name(vendortype[i].model));
684 break;
685 }
686 }
687 }
688
689 sc->hw.model = vendortype[i].model;
690 sc->mode.packetsize = vendortype[i].packetsize;
691
692 /* set mouse parameters */
693 if (mode != (mousemode_t *)NULL) {
694 if (mode->rate > 0)
695 mode->rate = set_mouse_sampling_rate(kbdc, mode->rate);
696 if (mode->resolution >= 0)
697 mode->resolution = set_mouse_resolution(kbdc, mode->resolution);
698 set_mouse_scaling(kbdc, 1);
699 set_mouse_mode(kbdc);
700 }
701
702 /* request a data packet and extract sync. bits */
703 if (get_mouse_status(kbdc, stat, 1, 3) < 3) {
704 log(LOG_DEBUG, "psm%d: failed to get data (doinitialize).\n",
705 sc->unit);
706 sc->mode.syncmask[0] = 0;
707 } else {
708 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
709 /* the NetScroll Mouse will send three more bytes... Ignore them */
710 empty_aux_buffer(kbdc, 5);
711 }
712
713 /* just check the status of the mouse */
714 if (get_mouse_status(kbdc, stat, 0, 3) < 3)
715 log(LOG_DEBUG, "psm%d: failed to get status (doinitialize).\n",
716 sc->unit);
717
718 return TRUE;
719 }
720
721 static int
722 doopen(struct psm_softc *sc, int command_byte)
723 {
724 int stat[3];
725
726 /* enable the mouse device */
727 if (!enable_aux_dev(sc->kbdc)) {
728 /* MOUSE ERROR: failed to enable the mouse because:
729 * 1) the mouse is faulty,
730 * 2) the mouse has been removed(!?)
731 * In the latter case, the keyboard may have hung, and need
732 * recovery procedure...
733 */
734 recover_from_error(sc->kbdc);
735 #if 0
736 /* FIXME: we could reset the mouse here and try to enable
737 * it again. But it will take long time and it's not a good
738 * idea to disable the keyboard that long...
739 */
740 if (!doinitialize(sc, &sc->mode) || !enable_aux_dev(sc->kbdc)) {
741 recover_from_error(sc->kbdc);
742 #else
743 {
744 #endif
745 restore_controller(sc->kbdc, command_byte);
746 /* mark this device is no longer available */
747 sc->state &= ~PSM_VALID;
748 log(LOG_ERR, "psm%d: failed to enable the device (doopen).\n",
749 sc->unit);
750 return (EIO);
751 }
752 }
753
754 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
755 log(LOG_DEBUG, "psm%d: failed to get status (doopen).\n", sc->unit);
756
757 /* enable the aux port and interrupt */
758 if (!set_controller_command_byte(sc->kbdc,
759 kbdc_get_device_mask(sc->kbdc),
760 (command_byte & KBD_KBD_CONTROL_BITS)
761 | KBD_ENABLE_AUX_PORT | KBD_ENABLE_AUX_INT)) {
762 /* CONTROLLER ERROR */
763 disable_aux_dev(sc->kbdc);
764 restore_controller(sc->kbdc, command_byte);
765 log(LOG_ERR, "psm%d: failed to enable the aux interrupt (doopen).\n",
766 sc->unit);
767 return (EIO);
768 }
769
770 /* start the watchdog timer */
771 sc->watchdog = FALSE;
772 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz*2);
773
774 return (0);
775 }
776
777 static int
778 reinitialize(struct psm_softc *sc, int doinit)
779 {
780 int err;
781 int c;
782 int s;
783
784 /* don't let anybody mess with the aux device */
785 if (!kbdc_lock(sc->kbdc, TRUE))
786 return (EIO);
787 s = spltty();
788
789 /* block our watchdog timer */
790 sc->watchdog = FALSE;
791 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
792 callout_handle_init(&sc->callout);
793
794 /* save the current controller command byte */
795 empty_both_buffers(sc->kbdc, 10);
796 c = get_controller_command_byte(sc->kbdc);
797 if (verbose >= 2)
798 log(LOG_DEBUG, "psm%d: current command byte: %04x (reinitialize).\n",
799 sc->unit, c);
800
801 /* enable the aux port but disable the aux interrupt and the keyboard */
802 if ((c == -1) || !set_controller_command_byte(sc->kbdc,
803 kbdc_get_device_mask(sc->kbdc),
804 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
805 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
806 /* CONTROLLER ERROR */
807 splx(s);
808 kbdc_lock(sc->kbdc, FALSE);
809 log(LOG_ERR, "psm%d: unable to set the command byte (reinitialize).\n",
810 sc->unit);
811 return (EIO);
812 }
813
814 /* flush any data */
815 if (sc->state & PSM_VALID) {
816 disable_aux_dev(sc->kbdc); /* this may fail; but never mind... */
817 empty_aux_buffer(sc->kbdc, 10);
818 }
819 sc->inputbytes = 0;
820 sc->syncerrors = 0;
821
822 /* try to detect the aux device; are you still there? */
823 err = 0;
824 if (doinit) {
825 if (doinitialize(sc, &sc->mode)) {
826 /* yes */
827 sc->state |= PSM_VALID;
828 } else {
829 /* the device has gone! */
830 restore_controller(sc->kbdc, c);
831 sc->state &= ~PSM_VALID;
832 log(LOG_ERR, "psm%d: the aux device has gone! (reinitialize).\n",
833 sc->unit);
834 err = ENXIO;
835 }
836 }
837 splx(s);
838
839 /* restore the driver state */
840 if ((sc->state & PSM_OPEN) && (err == 0)) {
841 /* enable the aux device and the port again */
842 err = doopen(sc, c);
843 if (err != 0)
844 log(LOG_ERR, "psm%d: failed to enable the device (reinitialize).\n",
845 sc->unit);
846 } else {
847 /* restore the keyboard port and disable the aux port */
848 if (!set_controller_command_byte(sc->kbdc,
849 kbdc_get_device_mask(sc->kbdc),
850 (c & KBD_KBD_CONTROL_BITS)
851 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
852 /* CONTROLLER ERROR */
853 log(LOG_ERR, "psm%d: failed to disable the aux port (reinitialize).\n",
854 sc->unit);
855 err = EIO;
856 }
857 }
858
859 kbdc_lock(sc->kbdc, FALSE);
860 return (err);
861 }
862
863 /* psm driver entry points */
864
865 static void
866 psmidentify(driver_t *driver, device_t parent)
867 {
868 device_t psmc;
869 device_t psm;
870 u_long irq;
871 int unit;
872
873 unit = device_get_unit(parent);
874
875 /* always add at least one child */
876 psm = BUS_ADD_CHILD(parent, KBDC_RID_AUX, driver->name, unit);
877 if (psm == NULL)
878 return;
879
880 irq = bus_get_resource_start(psm, SYS_RES_IRQ, KBDC_RID_AUX);
881 if (irq > 0)
882 return;
883
884 /*
885 * If the PS/2 mouse device has already been reported by ACPI or
886 * PnP BIOS, obtain the IRQ resource from it.
887 * (See psmcpnp_attach() below.)
888 */
889 psmc = device_find_child(device_get_parent(parent),
890 PSMCPNP_DRIVER_NAME, unit);
891 if (psmc == NULL)
892 return;
893 irq = bus_get_resource_start(psmc, SYS_RES_IRQ, 0);
894 if (irq <= 0)
895 return;
896 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
897 }
898
899 #define endprobe(v) do { if (bootverbose) \
900 --verbose; \
901 kbdc_set_device_mask(sc->kbdc, mask); \
902 kbdc_lock(sc->kbdc, FALSE); \
903 return (v); \
904 } while (0)
905
906 static int
907 psmprobe(device_t dev)
908 {
909 int unit = device_get_unit(dev);
910 struct psm_softc *sc = device_get_softc(dev);
911 int stat[3];
912 int command_byte;
913 int mask;
914 int rid;
915 int i;
916
917 #if 0
918 kbdc_debug(TRUE);
919 #endif
920
921 /* see if IRQ is available */
922 rid = KBDC_RID_AUX;
923 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
924 RF_SHAREABLE | RF_ACTIVE);
925 if (sc->intr == NULL) {
926 if (bootverbose)
927 device_printf(dev, "unable to allocate IRQ\n");
928 return (ENXIO);
929 }
930 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
931
932 sc->unit = unit;
933 sc->kbdc = atkbdc_open(device_get_unit(device_get_parent(dev)));
934 sc->config = device_get_flags(dev) & PSM_CONFIG_FLAGS;
935 /* XXX: for backward compatibility */
936 #if defined(PSM_HOOKRESUME) || defined(PSM_HOOKAPM)
937 sc->config |=
938 #ifdef PSM_RESETAFTERSUSPEND
939 PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
940 #else
941 PSM_CONFIG_HOOKRESUME;
942 #endif
943 #endif /* PSM_HOOKRESUME | PSM_HOOKAPM */
944 sc->flags = 0;
945 if (bootverbose)
946 ++verbose;
947
948 device_set_desc(dev, "PS/2 Mouse");
949
950 if (!kbdc_lock(sc->kbdc, TRUE)) {
951 printf("psm%d: unable to lock the controller.\n", unit);
952 if (bootverbose)
953 --verbose;
954 return (ENXIO);
955 }
956
957 /*
958 * NOTE: two bits in the command byte controls the operation of the
959 * aux port (mouse port): the aux port disable bit (bit 5) and the aux
960 * port interrupt (IRQ 12) enable bit (bit 2).
961 */
962
963 /* discard anything left after the keyboard initialization */
964 empty_both_buffers(sc->kbdc, 10);
965
966 /* save the current command byte; it will be used later */
967 mask = kbdc_get_device_mask(sc->kbdc) & ~KBD_AUX_CONTROL_BITS;
968 command_byte = get_controller_command_byte(sc->kbdc);
969 if (verbose)
970 printf("psm%d: current command byte:%04x\n", unit, command_byte);
971 if (command_byte == -1) {
972 /* CONTROLLER ERROR */
973 printf("psm%d: unable to get the current command byte value.\n",
974 unit);
975 endprobe(ENXIO);
976 }
977
978 /*
979 * disable the keyboard port while probing the aux port, which must be
980 * enabled during this routine
981 */
982 if (!set_controller_command_byte(sc->kbdc,
983 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
984 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
985 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
986 /*
987 * this is CONTROLLER ERROR; I don't know how to recover
988 * from this error...
989 */
990 restore_controller(sc->kbdc, command_byte);
991 printf("psm%d: unable to set the command byte.\n", unit);
992 endprobe(ENXIO);
993 }
994 write_controller_command(sc->kbdc, KBDC_ENABLE_AUX_PORT);
995
996 /*
997 * NOTE: `test_aux_port()' is designed to return with zero if the aux
998 * port exists and is functioning. However, some controllers appears
999 * to respond with zero even when the aux port doesn't exist. (It may
1000 * be that this is only the case when the controller DOES have the aux
1001 * port but the port is not wired on the motherboard.) The keyboard
1002 * controllers without the port, such as the original AT, are
1003 * supporsed to return with an error code or simply time out. In any
1004 * case, we have to continue probing the port even when the controller
1005 * passes this test.
1006 *
1007 * XXX: some controllers erroneously return the error code 1 when
1008 * it has the perfectly functional aux port. We have to ignore this
1009 * error code. Even if the controller HAS error with the aux port,
1010 * it will be detected later...
1011 * XXX: another incompatible controller returns PSM_ACK (0xfa)...
1012 */
1013 switch ((i = test_aux_port(sc->kbdc))) {
1014 case 1: /* ignore this error */
1015 case PSM_ACK:
1016 if (verbose)
1017 printf("psm%d: strange result for test aux port (%d).\n",
1018 unit, i);
1019 /* FALLTHROUGH */
1020 case 0: /* no error */
1021 break;
1022 case -1: /* time out */
1023 default: /* error */
1024 recover_from_error(sc->kbdc);
1025 if (sc->config & PSM_CONFIG_IGNPORTERROR)
1026 break;
1027 restore_controller(sc->kbdc, command_byte);
1028 if (verbose)
1029 printf("psm%d: the aux port is not functioning (%d).\n",
1030 unit, i);
1031 endprobe(ENXIO);
1032 }
1033
1034 if (sc->config & PSM_CONFIG_NORESET) {
1035 /*
1036 * Don't try to reset the pointing device. It may possibly be
1037 * left in the unknown state, though...
1038 */
1039 } else {
1040 /*
1041 * NOTE: some controllers appears to hang the `keyboard' when the aux
1042 * port doesn't exist and `PSMC_RESET_DEV' is issued.
1043 *
1044 * Attempt to reset the controller twice -- this helps
1045 * pierce through some KVM switches. The second reset
1046 * is non-fatal.
1047 */
1048 if (!reset_aux_dev(sc->kbdc)) {
1049 recover_from_error(sc->kbdc);
1050 restore_controller(sc->kbdc, command_byte);
1051 if (verbose)
1052 printf("psm%d: failed to reset the aux device.\n", unit);
1053 endprobe(ENXIO);
1054 } else if (!reset_aux_dev(sc->kbdc)) {
1055 recover_from_error(sc->kbdc);
1056 if (verbose >= 2)
1057 printf("psm%d: failed to reset the aux device (2).\n",
1058 unit);
1059 }
1060 }
1061
1062 /*
1063 * both the aux port and the aux device is functioning, see if the
1064 * device can be enabled. NOTE: when enabled, the device will start
1065 * sending data; we shall immediately disable the device once we know
1066 * the device can be enabled.
1067 */
1068 if (!enable_aux_dev(sc->kbdc) || !disable_aux_dev(sc->kbdc)) {
1069 /* MOUSE ERROR */
1070 recover_from_error(sc->kbdc);
1071 restore_controller(sc->kbdc, command_byte);
1072 if (verbose)
1073 printf("psm%d: failed to enable the aux device.\n", unit);
1074 endprobe(ENXIO);
1075 }
1076
1077 /* save the default values after reset */
1078 if (get_mouse_status(sc->kbdc, stat, 0, 3) >= 3) {
1079 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1080 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1081 } else {
1082 sc->dflt_mode.rate = sc->mode.rate = -1;
1083 sc->dflt_mode.resolution = sc->mode.resolution = -1;
1084 }
1085
1086 /* hardware information */
1087 sc->hw.iftype = MOUSE_IF_PS2;
1088
1089 /* verify the device is a mouse */
1090 sc->hw.hwid = get_aux_id(sc->kbdc);
1091 if (!is_a_mouse(sc->hw.hwid)) {
1092 restore_controller(sc->kbdc, command_byte);
1093 if (verbose)
1094 printf("psm%d: unknown device type (%d).\n", unit, sc->hw.hwid);
1095 endprobe(ENXIO);
1096 }
1097 switch (sc->hw.hwid) {
1098 case PSM_BALLPOINT_ID:
1099 sc->hw.type = MOUSE_TRACKBALL;
1100 break;
1101 case PSM_MOUSE_ID:
1102 case PSM_INTELLI_ID:
1103 case PSM_EXPLORER_ID:
1104 case PSM_4DMOUSE_ID:
1105 case PSM_4DPLUS_ID:
1106 sc->hw.type = MOUSE_MOUSE;
1107 break;
1108 default:
1109 sc->hw.type = MOUSE_UNKNOWN;
1110 break;
1111 }
1112
1113 if (sc->config & PSM_CONFIG_NOIDPROBE) {
1114 sc->hw.buttons = 2;
1115 i = GENERIC_MOUSE_ENTRY;
1116 } else {
1117 /* # of buttons */
1118 sc->hw.buttons = get_mouse_buttons(sc->kbdc);
1119
1120 /* other parameters */
1121 for (i = 0; vendortype[i].probefunc != NULL; ++i) {
1122 if ((*vendortype[i].probefunc)(sc)) {
1123 if (verbose >= 2)
1124 printf("psm%d: found %s\n",
1125 unit, model_name(vendortype[i].model));
1126 break;
1127 }
1128 }
1129 }
1130
1131 sc->hw.model = vendortype[i].model;
1132
1133 sc->dflt_mode.level = PSM_LEVEL_BASE;
1134 sc->dflt_mode.packetsize = MOUSE_PS2_PACKETSIZE;
1135 sc->dflt_mode.accelfactor = (sc->config & PSM_CONFIG_ACCEL) >> 4;
1136 if (sc->config & PSM_CONFIG_NOCHECKSYNC)
1137 sc->dflt_mode.syncmask[0] = 0;
1138 else
1139 sc->dflt_mode.syncmask[0] = vendortype[i].syncmask;
1140 if (sc->config & PSM_CONFIG_FORCETAP)
1141 sc->mode.syncmask[0] &= ~MOUSE_PS2_TAP;
1142 sc->dflt_mode.syncmask[1] = 0; /* syncbits */
1143 sc->mode = sc->dflt_mode;
1144 sc->mode.packetsize = vendortype[i].packetsize;
1145
1146 /* set mouse parameters */
1147 #if 0
1148 /*
1149 * A version of Logitech FirstMouse+ won't report wheel movement,
1150 * if SET_DEFAULTS is sent... Don't use this command.
1151 * This fix was found by Takashi Nishida.
1152 */
1153 i = send_aux_command(sc->kbdc, PSMC_SET_DEFAULTS);
1154 if (verbose >= 2)
1155 printf("psm%d: SET_DEFAULTS return code:%04x\n", unit, i);
1156 #endif
1157 if (sc->config & PSM_CONFIG_RESOLUTION) {
1158 sc->mode.resolution
1159 = set_mouse_resolution(sc->kbdc,
1160 (sc->config & PSM_CONFIG_RESOLUTION) - 1);
1161 } else if (sc->mode.resolution >= 0) {
1162 sc->mode.resolution
1163 = set_mouse_resolution(sc->kbdc, sc->dflt_mode.resolution);
1164 }
1165 if (sc->mode.rate > 0) {
1166 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, sc->dflt_mode.rate);
1167 }
1168 set_mouse_scaling(sc->kbdc, 1);
1169
1170 /* request a data packet and extract sync. bits */
1171 if (get_mouse_status(sc->kbdc, stat, 1, 3) < 3) {
1172 printf("psm%d: failed to get data.\n", unit);
1173 sc->mode.syncmask[0] = 0;
1174 } else {
1175 sc->mode.syncmask[1] = stat[0] & sc->mode.syncmask[0]; /* syncbits */
1176 /* the NetScroll Mouse will send three more bytes... Ignore them */
1177 empty_aux_buffer(sc->kbdc, 5);
1178 }
1179
1180 /* just check the status of the mouse */
1181 /*
1182 * NOTE: XXX there are some arcane controller/mouse combinations out
1183 * there, which hung the controller unless there is data transmission
1184 * after ACK from the mouse.
1185 */
1186 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3) {
1187 printf("psm%d: failed to get status.\n", unit);
1188 } else {
1189 /*
1190 * When in its native mode, some mice operate with different
1191 * default parameters than in the PS/2 compatible mode.
1192 */
1193 sc->dflt_mode.rate = sc->mode.rate = stat[2];
1194 sc->dflt_mode.resolution = sc->mode.resolution = stat[1];
1195 }
1196
1197 /* disable the aux port for now... */
1198 if (!set_controller_command_byte(sc->kbdc,
1199 KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS,
1200 (command_byte & KBD_KBD_CONTROL_BITS)
1201 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1202 /*
1203 * this is CONTROLLER ERROR; I don't know the proper way to
1204 * recover from this error...
1205 */
1206 restore_controller(sc->kbdc, command_byte);
1207 printf("psm%d: unable to set the command byte.\n", unit);
1208 endprobe(ENXIO);
1209 }
1210
1211 /* done */
1212 kbdc_set_device_mask(sc->kbdc, mask | KBD_AUX_CONTROL_BITS);
1213 kbdc_lock(sc->kbdc, FALSE);
1214 return (0);
1215 }
1216
1217 static int
1218 psmattach(device_t dev)
1219 {
1220 int unit = device_get_unit(dev);
1221 struct psm_softc *sc = device_get_softc(dev);
1222 int error;
1223 int rid;
1224
1225 if (sc == NULL) /* shouldn't happen */
1226 return (ENXIO);
1227
1228 /* Setup initial state */
1229 sc->state = PSM_VALID;
1230 callout_handle_init(&sc->callout);
1231
1232 /* Setup our interrupt handler */
1233 rid = KBDC_RID_AUX;
1234 sc->intr = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1235 RF_SHAREABLE | RF_ACTIVE);
1236 if (sc->intr == NULL)
1237 return (ENXIO);
1238 error = bus_setup_intr(dev, sc->intr, INTR_TYPE_TTY, psmintr, sc, &sc->ih);
1239 if (error) {
1240 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1241 return (error);
1242 }
1243
1244 /* Done */
1245 sc->dev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, FALSE), 0, 0, 0666,
1246 "psm%d", unit);
1247 sc->bdev = make_dev(&psm_cdevsw, PSM_MKMINOR(unit, TRUE), 0, 0, 0666,
1248 "bpsm%d", unit);
1249
1250 if (!verbose) {
1251 printf("psm%d: model %s, device ID %d\n",
1252 unit, model_name(sc->hw.model), sc->hw.hwid & 0x00ff);
1253 } else {
1254 printf("psm%d: model %s, device ID %d-%02x, %d buttons\n",
1255 unit, model_name(sc->hw.model),
1256 sc->hw.hwid & 0x00ff, sc->hw.hwid >> 8, sc->hw.buttons);
1257 printf("psm%d: config:%08x, flags:%08x, packet size:%d\n",
1258 unit, sc->config, sc->flags, sc->mode.packetsize);
1259 printf("psm%d: syncmask:%02x, syncbits:%02x\n",
1260 unit, sc->mode.syncmask[0], sc->mode.syncmask[1]);
1261 }
1262
1263 if (bootverbose)
1264 --verbose;
1265
1266 return (0);
1267 }
1268
1269 static int
1270 psmdetach(device_t dev)
1271 {
1272 struct psm_softc *sc;
1273 int rid;
1274
1275 sc = device_get_softc(dev);
1276 if (sc->state & PSM_OPEN)
1277 return EBUSY;
1278
1279 rid = KBDC_RID_AUX;
1280 bus_teardown_intr(dev, sc->intr, sc->ih);
1281 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->intr);
1282
1283 destroy_dev(sc->dev);
1284 destroy_dev(sc->bdev);
1285
1286 return 0;
1287 }
1288
1289 static int
1290 psmopen(dev_t dev, int flag, int fmt, struct thread *td)
1291 {
1292 int unit = PSM_UNIT(dev);
1293 struct psm_softc *sc;
1294 int command_byte;
1295 int err;
1296 int s;
1297
1298 /* Get device data */
1299 sc = PSM_SOFTC(unit);
1300 if ((sc == NULL) || (sc->state & PSM_VALID) == 0)
1301 /* the device is no longer valid/functioning */
1302 return (ENXIO);
1303
1304 /* Disallow multiple opens */
1305 if (sc->state & PSM_OPEN)
1306 return (EBUSY);
1307
1308 device_busy(devclass_get_device(psm_devclass, unit));
1309
1310 /* Initialize state */
1311 sc->mode.level = sc->dflt_mode.level;
1312 sc->mode.protocol = sc->dflt_mode.protocol;
1313 sc->watchdog = FALSE;
1314
1315 /* flush the event queue */
1316 sc->queue.count = 0;
1317 sc->queue.head = 0;
1318 sc->queue.tail = 0;
1319 sc->status.flags = 0;
1320 sc->status.button = 0;
1321 sc->status.obutton = 0;
1322 sc->status.dx = 0;
1323 sc->status.dy = 0;
1324 sc->status.dz = 0;
1325 sc->button = 0;
1326
1327 /* empty input buffer */
1328 bzero(sc->ipacket, sizeof(sc->ipacket));
1329 sc->inputbytes = 0;
1330 sc->syncerrors = 0;
1331
1332 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1333 if (!kbdc_lock(sc->kbdc, TRUE))
1334 return (EIO);
1335
1336 /* save the current controller command byte */
1337 s = spltty();
1338 command_byte = get_controller_command_byte(sc->kbdc);
1339
1340 /* enable the aux port and temporalily disable the keyboard */
1341 if ((command_byte == -1)
1342 || !set_controller_command_byte(sc->kbdc,
1343 kbdc_get_device_mask(sc->kbdc),
1344 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1345 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1346 /* CONTROLLER ERROR; do you know how to get out of this? */
1347 kbdc_lock(sc->kbdc, FALSE);
1348 splx(s);
1349 log(LOG_ERR, "psm%d: unable to set the command byte (psmopen).\n",
1350 unit);
1351 return (EIO);
1352 }
1353 /*
1354 * Now that the keyboard controller is told not to generate
1355 * the keyboard and mouse interrupts, call `splx()' to allow
1356 * the other tty interrupts. The clock interrupt may also occur,
1357 * but timeout routines will be blocked by the poll flag set
1358 * via `kbdc_lock()'
1359 */
1360 splx(s);
1361
1362 /* enable the mouse device */
1363 err = doopen(sc, command_byte);
1364
1365 /* done */
1366 if (err == 0)
1367 sc->state |= PSM_OPEN;
1368 kbdc_lock(sc->kbdc, FALSE);
1369 return (err);
1370 }
1371
1372 static int
1373 psmclose(dev_t dev, int flag, int fmt, struct thread *td)
1374 {
1375 int unit = PSM_UNIT(dev);
1376 struct psm_softc *sc = PSM_SOFTC(unit);
1377 int stat[3];
1378 int command_byte;
1379 int s;
1380
1381 /* don't let timeout routines in the keyboard driver to poll the kbdc */
1382 if (!kbdc_lock(sc->kbdc, TRUE))
1383 return (EIO);
1384
1385 /* save the current controller command byte */
1386 s = spltty();
1387 command_byte = get_controller_command_byte(sc->kbdc);
1388 if (command_byte == -1) {
1389 kbdc_lock(sc->kbdc, FALSE);
1390 splx(s);
1391 return (EIO);
1392 }
1393
1394 /* disable the aux interrupt and temporalily disable the keyboard */
1395 if (!set_controller_command_byte(sc->kbdc,
1396 kbdc_get_device_mask(sc->kbdc),
1397 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1398 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1399 log(LOG_ERR, "psm%d: failed to disable the aux int (psmclose).\n",
1400 unit);
1401 /* CONTROLLER ERROR;
1402 * NOTE: we shall force our way through. Because the only
1403 * ill effect we shall see is that we may not be able
1404 * to read ACK from the mouse, and it doesn't matter much
1405 * so long as the mouse will accept the DISABLE command.
1406 */
1407 }
1408 splx(s);
1409
1410 /* stop the watchdog timer */
1411 untimeout(psmtimeout, (void *)(uintptr_t)sc, sc->callout);
1412 callout_handle_init(&sc->callout);
1413
1414 /* remove anything left in the output buffer */
1415 empty_aux_buffer(sc->kbdc, 10);
1416
1417 /* disable the aux device, port and interrupt */
1418 if (sc->state & PSM_VALID) {
1419 if (!disable_aux_dev(sc->kbdc)) {
1420 /* MOUSE ERROR;
1421 * NOTE: we don't return error and continue, pretending
1422 * we have successfully disabled the device. It's OK because
1423 * the interrupt routine will discard any data from the mouse
1424 * hereafter.
1425 */
1426 log(LOG_ERR, "psm%d: failed to disable the device (psmclose).\n",
1427 unit);
1428 }
1429
1430 if (get_mouse_status(sc->kbdc, stat, 0, 3) < 3)
1431 log(LOG_DEBUG, "psm%d: failed to get status (psmclose).\n",
1432 unit);
1433 }
1434
1435 if (!set_controller_command_byte(sc->kbdc,
1436 kbdc_get_device_mask(sc->kbdc),
1437 (command_byte & KBD_KBD_CONTROL_BITS)
1438 | KBD_DISABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1439 /* CONTROLLER ERROR;
1440 * we shall ignore this error; see the above comment.
1441 */
1442 log(LOG_ERR, "psm%d: failed to disable the aux port (psmclose).\n",
1443 unit);
1444 }
1445
1446 /* remove anything left in the output buffer */
1447 empty_aux_buffer(sc->kbdc, 10);
1448
1449 /* close is almost always successful */
1450 sc->state &= ~PSM_OPEN;
1451 kbdc_lock(sc->kbdc, FALSE);
1452 device_unbusy(devclass_get_device(psm_devclass, unit));
1453 return (0);
1454 }
1455
1456 static int
1457 tame_mouse(struct psm_softc *sc, mousestatus_t *status, unsigned char *buf)
1458 {
1459 static unsigned char butmapps2[8] = {
1460 0,
1461 MOUSE_PS2_BUTTON1DOWN,
1462 MOUSE_PS2_BUTTON2DOWN,
1463 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN,
1464 MOUSE_PS2_BUTTON3DOWN,
1465 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON3DOWN,
1466 MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1467 MOUSE_PS2_BUTTON1DOWN | MOUSE_PS2_BUTTON2DOWN | MOUSE_PS2_BUTTON3DOWN,
1468 };
1469 static unsigned char butmapmsc[8] = {
1470 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1471 MOUSE_MSC_BUTTON2UP | MOUSE_MSC_BUTTON3UP,
1472 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON3UP,
1473 MOUSE_MSC_BUTTON3UP,
1474 MOUSE_MSC_BUTTON1UP | MOUSE_MSC_BUTTON2UP,
1475 MOUSE_MSC_BUTTON2UP,
1476 MOUSE_MSC_BUTTON1UP,
1477 0,
1478 };
1479 int mapped;
1480 int i;
1481
1482 if (sc->mode.level == PSM_LEVEL_BASE) {
1483 mapped = status->button & ~MOUSE_BUTTON4DOWN;
1484 if (status->button & MOUSE_BUTTON4DOWN)
1485 mapped |= MOUSE_BUTTON1DOWN;
1486 status->button = mapped;
1487 buf[0] = MOUSE_PS2_SYNC | butmapps2[mapped & MOUSE_STDBUTTONS];
1488 i = imax(imin(status->dx, 255), -256);
1489 if (i < 0)
1490 buf[0] |= MOUSE_PS2_XNEG;
1491 buf[1] = i;
1492 i = imax(imin(status->dy, 255), -256);
1493 if (i < 0)
1494 buf[0] |= MOUSE_PS2_YNEG;
1495 buf[2] = i;
1496 return MOUSE_PS2_PACKETSIZE;
1497 } else if (sc->mode.level == PSM_LEVEL_STANDARD) {
1498 buf[0] = MOUSE_MSC_SYNC | butmapmsc[status->button & MOUSE_STDBUTTONS];
1499 i = imax(imin(status->dx, 255), -256);
1500 buf[1] = i >> 1;
1501 buf[3] = i - buf[1];
1502 i = imax(imin(status->dy, 255), -256);
1503 buf[2] = i >> 1;
1504 buf[4] = i - buf[2];
1505 i = imax(imin(status->dz, 127), -128);
1506 buf[5] = (i >> 1) & 0x7f;
1507 buf[6] = (i - (i >> 1)) & 0x7f;
1508 buf[7] = (~status->button >> 3) & 0x7f;
1509 return MOUSE_SYS_PACKETSIZE;
1510 }
1511 return sc->inputbytes;
1512 }
1513
1514 static int
1515 psmread(dev_t dev, struct uio *uio, int flag)
1516 {
1517 register struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1518 unsigned char buf[PSM_SMALLBUFSIZE];
1519 int error = 0;
1520 int s;
1521 int l;
1522
1523 if ((sc->state & PSM_VALID) == 0)
1524 return EIO;
1525
1526 /* block until mouse activity occured */
1527 s = spltty();
1528 while (sc->queue.count <= 0) {
1529 if (PSM_NBLOCKIO(dev)) {
1530 splx(s);
1531 return EWOULDBLOCK;
1532 }
1533 sc->state |= PSM_ASLP;
1534 error = tsleep( sc, PZERO | PCATCH, "psmrea", 0);
1535 sc->state &= ~PSM_ASLP;
1536 if (error) {
1537 splx(s);
1538 return error;
1539 } else if ((sc->state & PSM_VALID) == 0) {
1540 /* the device disappeared! */
1541 splx(s);
1542 return EIO;
1543 }
1544 }
1545 splx(s);
1546
1547 /* copy data to the user land */
1548 while ((sc->queue.count > 0) && (uio->uio_resid > 0)) {
1549 s = spltty();
1550 l = imin(sc->queue.count, uio->uio_resid);
1551 if (l > sizeof(buf))
1552 l = sizeof(buf);
1553 if (l > sizeof(sc->queue.buf) - sc->queue.head) {
1554 bcopy(&sc->queue.buf[sc->queue.head], &buf[0],
1555 sizeof(sc->queue.buf) - sc->queue.head);
1556 bcopy(&sc->queue.buf[0],
1557 &buf[sizeof(sc->queue.buf) - sc->queue.head],
1558 l - (sizeof(sc->queue.buf) - sc->queue.head));
1559 } else {
1560 bcopy(&sc->queue.buf[sc->queue.head], &buf[0], l);
1561 }
1562 sc->queue.count -= l;
1563 sc->queue.head = (sc->queue.head + l) % sizeof(sc->queue.buf);
1564 splx(s);
1565 error = uiomove(buf, l, uio);
1566 if (error)
1567 break;
1568 }
1569
1570 return error;
1571 }
1572
1573 static int
1574 block_mouse_data(struct psm_softc *sc, int *c)
1575 {
1576 int s;
1577
1578 if (!kbdc_lock(sc->kbdc, TRUE))
1579 return EIO;
1580
1581 s = spltty();
1582 *c = get_controller_command_byte(sc->kbdc);
1583 if ((*c == -1)
1584 || !set_controller_command_byte(sc->kbdc,
1585 kbdc_get_device_mask(sc->kbdc),
1586 KBD_DISABLE_KBD_PORT | KBD_DISABLE_KBD_INT
1587 | KBD_ENABLE_AUX_PORT | KBD_DISABLE_AUX_INT)) {
1588 /* this is CONTROLLER ERROR */
1589 splx(s);
1590 kbdc_lock(sc->kbdc, FALSE);
1591 return EIO;
1592 }
1593
1594 /*
1595 * The device may be in the middle of status data transmission.
1596 * The transmission will be interrupted, thus, incomplete status
1597 * data must be discarded. Although the aux interrupt is disabled
1598 * at the keyboard controller level, at most one aux interrupt
1599 * may have already been pending and a data byte is in the
1600 * output buffer; throw it away. Note that the second argument
1601 * to `empty_aux_buffer()' is zero, so that the call will just
1602 * flush the internal queue.
1603 * `psmintr()' will be invoked after `splx()' if an interrupt is
1604 * pending; it will see no data and returns immediately.
1605 */
1606 empty_aux_buffer(sc->kbdc, 0); /* flush the queue */
1607 read_aux_data_no_wait(sc->kbdc); /* throw away data if any */
1608 sc->inputbytes = 0;
1609 splx(s);
1610
1611 return 0;
1612 }
1613
1614 static int
1615 unblock_mouse_data(struct psm_softc *sc, int c)
1616 {
1617 int error = 0;
1618
1619 /*
1620 * We may have seen a part of status data during `set_mouse_XXX()'.
1621 * they have been queued; flush it.
1622 */
1623 empty_aux_buffer(sc->kbdc, 0);
1624
1625 /* restore ports and interrupt */
1626 if (!set_controller_command_byte(sc->kbdc,
1627 kbdc_get_device_mask(sc->kbdc),
1628 c & (KBD_KBD_CONTROL_BITS | KBD_AUX_CONTROL_BITS))) {
1629 /* CONTROLLER ERROR; this is serious, we may have
1630 * been left with the inaccessible keyboard and
1631 * the disabled mouse interrupt.
1632 */
1633 error = EIO;
1634 }
1635
1636 kbdc_lock(sc->kbdc, FALSE);
1637 return error;
1638 }
1639
1640 static int
1641 psmioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1642 {
1643 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
1644 mousemode_t mode;
1645 mousestatus_t status;
1646 #if (defined(MOUSE_GETVARS))
1647 mousevar_t *var;
1648 #endif
1649 mousedata_t *data;
1650 int stat[3];
1651 int command_byte;
1652 int error = 0;
1653 int s;
1654
1655 /* Perform IOCTL command */
1656 switch (cmd) {
1657
1658 case OLD_MOUSE_GETHWINFO:
1659 s = spltty();
1660 ((old_mousehw_t *)addr)->buttons = sc->hw.buttons;
1661 ((old_mousehw_t *)addr)->iftype = sc->hw.iftype;
1662 ((old_mousehw_t *)addr)->type = sc->hw.type;
1663 ((old_mousehw_t *)addr)->hwid = sc->hw.hwid & 0x00ff;
1664 splx(s);
1665 break;
1666
1667 case MOUSE_GETHWINFO:
1668 s = spltty();
1669 *(mousehw_t *)addr = sc->hw;
1670 if (sc->mode.level == PSM_LEVEL_BASE)
1671 ((mousehw_t *)addr)->model = MOUSE_MODEL_GENERIC;
1672 splx(s);
1673 break;
1674
1675 case OLD_MOUSE_GETMODE:
1676 s = spltty();
1677 switch (sc->mode.level) {
1678 case PSM_LEVEL_BASE:
1679 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1680 break;
1681 case PSM_LEVEL_STANDARD:
1682 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1683 break;
1684 case PSM_LEVEL_NATIVE:
1685 ((old_mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1686 break;
1687 }
1688 ((old_mousemode_t *)addr)->rate = sc->mode.rate;
1689 ((old_mousemode_t *)addr)->resolution = sc->mode.resolution;
1690 ((old_mousemode_t *)addr)->accelfactor = sc->mode.accelfactor;
1691 splx(s);
1692 break;
1693
1694 case MOUSE_GETMODE:
1695 s = spltty();
1696 *(mousemode_t *)addr = sc->mode;
1697 ((mousemode_t *)addr)->resolution =
1698 MOUSE_RES_LOW - sc->mode.resolution;
1699 switch (sc->mode.level) {
1700 case PSM_LEVEL_BASE:
1701 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1702 ((mousemode_t *)addr)->packetsize = MOUSE_PS2_PACKETSIZE;
1703 break;
1704 case PSM_LEVEL_STANDARD:
1705 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_SYSMOUSE;
1706 ((mousemode_t *)addr)->packetsize = MOUSE_SYS_PACKETSIZE;
1707 ((mousemode_t *)addr)->syncmask[0] = MOUSE_SYS_SYNCMASK;
1708 ((mousemode_t *)addr)->syncmask[1] = MOUSE_SYS_SYNC;
1709 break;
1710 case PSM_LEVEL_NATIVE:
1711 /* FIXME: this isn't quite correct... XXX */
1712 ((mousemode_t *)addr)->protocol = MOUSE_PROTO_PS2;
1713 break;
1714 }
1715 splx(s);
1716 break;
1717
1718 case OLD_MOUSE_SETMODE:
1719 case MOUSE_SETMODE:
1720 if (cmd == OLD_MOUSE_SETMODE) {
1721 mode.rate = ((old_mousemode_t *)addr)->rate;
1722 /*
1723 * resolution old I/F new I/F
1724 * default 0 0
1725 * low 1 -2
1726 * medium low 2 -3
1727 * medium high 3 -4
1728 * high 4 -5
1729 */
1730 if (((old_mousemode_t *)addr)->resolution > 0)
1731 mode.resolution = -((old_mousemode_t *)addr)->resolution - 1;
1732 mode.accelfactor = ((old_mousemode_t *)addr)->accelfactor;
1733 mode.level = -1;
1734 } else {
1735 mode = *(mousemode_t *)addr;
1736 }
1737
1738 /* adjust and validate parameters. */
1739 if (mode.rate > UCHAR_MAX)
1740 return EINVAL;
1741 if (mode.rate == 0)
1742 mode.rate = sc->dflt_mode.rate;
1743 else if (mode.rate == -1)
1744 /* don't change the current setting */
1745 ;
1746 else if (mode.rate < 0)
1747 return EINVAL;
1748 if (mode.resolution >= UCHAR_MAX)
1749 return EINVAL;
1750 if (mode.resolution >= 200)
1751 mode.resolution = MOUSE_RES_HIGH;
1752 else if (mode.resolution >= 100)
1753 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1754 else if (mode.resolution >= 50)
1755 mode.resolution = MOUSE_RES_MEDIUMLOW;
1756 else if (mode.resolution > 0)
1757 mode.resolution = MOUSE_RES_LOW;
1758 if (mode.resolution == MOUSE_RES_DEFAULT)
1759 mode.resolution = sc->dflt_mode.resolution;
1760 else if (mode.resolution == -1)
1761 /* don't change the current setting */
1762 ;
1763 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1764 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1765 if (mode.level == -1)
1766 /* don't change the current setting */
1767 mode.level = sc->mode.level;
1768 else if ((mode.level < PSM_LEVEL_MIN) || (mode.level > PSM_LEVEL_MAX))
1769 return EINVAL;
1770 if (mode.accelfactor == -1)
1771 /* don't change the current setting */
1772 mode.accelfactor = sc->mode.accelfactor;
1773 else if (mode.accelfactor < 0)
1774 return EINVAL;
1775
1776 /* don't allow anybody to poll the keyboard controller */
1777 error = block_mouse_data(sc, &command_byte);
1778 if (error)
1779 return error;
1780
1781 /* set mouse parameters */
1782 if (mode.rate > 0)
1783 mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1784 if (mode.resolution >= 0)
1785 mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1786 set_mouse_scaling(sc->kbdc, 1);
1787 get_mouse_status(sc->kbdc, stat, 0, 3);
1788
1789 s = spltty();
1790 sc->mode.rate = mode.rate;
1791 sc->mode.resolution = mode.resolution;
1792 sc->mode.accelfactor = mode.accelfactor;
1793 sc->mode.level = mode.level;
1794 splx(s);
1795
1796 unblock_mouse_data(sc, command_byte);
1797 break;
1798
1799 case MOUSE_GETLEVEL:
1800 *(int *)addr = sc->mode.level;
1801 break;
1802
1803 case MOUSE_SETLEVEL:
1804 if ((*(int *)addr < PSM_LEVEL_MIN) || (*(int *)addr > PSM_LEVEL_MAX))
1805 return EINVAL;
1806 sc->mode.level = *(int *)addr;
1807 break;
1808
1809 case MOUSE_GETSTATUS:
1810 s = spltty();
1811 status = sc->status;
1812 sc->status.flags = 0;
1813 sc->status.obutton = sc->status.button;
1814 sc->status.button = 0;
1815 sc->status.dx = 0;
1816 sc->status.dy = 0;
1817 sc->status.dz = 0;
1818 splx(s);
1819 *(mousestatus_t *)addr = status;
1820 break;
1821
1822 #if (defined(MOUSE_GETVARS))
1823 case MOUSE_GETVARS:
1824 var = (mousevar_t *)addr;
1825 bzero(var, sizeof(*var));
1826 s = spltty();
1827 var->var[0] = MOUSE_VARS_PS2_SIG;
1828 var->var[1] = sc->config;
1829 var->var[2] = sc->flags;
1830 splx(s);
1831 break;
1832
1833 case MOUSE_SETVARS:
1834 return ENODEV;
1835 #endif /* MOUSE_GETVARS */
1836
1837 case MOUSE_READSTATE:
1838 case MOUSE_READDATA:
1839 data = (mousedata_t *)addr;
1840 if (data->len > sizeof(data->buf)/sizeof(data->buf[0]))
1841 return EINVAL;
1842
1843 error = block_mouse_data(sc, &command_byte);
1844 if (error)
1845 return error;
1846 if ((data->len = get_mouse_status(sc->kbdc, data->buf,
1847 (cmd == MOUSE_READDATA) ? 1 : 0, data->len)) <= 0)
1848 error = EIO;
1849 unblock_mouse_data(sc, command_byte);
1850 break;
1851
1852 #if (defined(MOUSE_SETRESOLUTION))
1853 case MOUSE_SETRESOLUTION:
1854 mode.resolution = *(int *)addr;
1855 if (mode.resolution >= UCHAR_MAX)
1856 return EINVAL;
1857 else if (mode.resolution >= 200)
1858 mode.resolution = MOUSE_RES_HIGH;
1859 else if (mode.resolution >= 100)
1860 mode.resolution = MOUSE_RES_MEDIUMHIGH;
1861 else if (mode.resolution >= 50)
1862 mode.resolution = MOUSE_RES_MEDIUMLOW;
1863 else if (mode.resolution > 0)
1864 mode.resolution = MOUSE_RES_LOW;
1865 if (mode.resolution == MOUSE_RES_DEFAULT)
1866 mode.resolution = sc->dflt_mode.resolution;
1867 else if (mode.resolution == -1)
1868 mode.resolution = sc->mode.resolution;
1869 else if (mode.resolution < 0) /* MOUSE_RES_LOW/MEDIUM/HIGH */
1870 mode.resolution = MOUSE_RES_LOW - mode.resolution;
1871
1872 error = block_mouse_data(sc, &command_byte);
1873 if (error)
1874 return error;
1875 sc->mode.resolution = set_mouse_resolution(sc->kbdc, mode.resolution);
1876 if (sc->mode.resolution != mode.resolution)
1877 error = EIO;
1878 unblock_mouse_data(sc, command_byte);
1879 break;
1880 #endif /* MOUSE_SETRESOLUTION */
1881
1882 #if (defined(MOUSE_SETRATE))
1883 case MOUSE_SETRATE:
1884 mode.rate = *(int *)addr;
1885 if (mode.rate > UCHAR_MAX)
1886 return EINVAL;
1887 if (mode.rate == 0)
1888 mode.rate = sc->dflt_mode.rate;
1889 else if (mode.rate < 0)
1890 mode.rate = sc->mode.rate;
1891
1892 error = block_mouse_data(sc, &command_byte);
1893 if (error)
1894 return error;
1895 sc->mode.rate = set_mouse_sampling_rate(sc->kbdc, mode.rate);
1896 if (sc->mode.rate != mode.rate)
1897 error = EIO;
1898 unblock_mouse_data(sc, command_byte);
1899 break;
1900 #endif /* MOUSE_SETRATE */
1901
1902 #if (defined(MOUSE_SETSCALING))
1903 case MOUSE_SETSCALING:
1904 if ((*(int *)addr <= 0) || (*(int *)addr > 2))
1905 return EINVAL;
1906
1907 error = block_mouse_data(sc, &command_byte);
1908 if (error)
1909 return error;
1910 if (!set_mouse_scaling(sc->kbdc, *(int *)addr))
1911 error = EIO;
1912 unblock_mouse_data(sc, command_byte);
1913 break;
1914 #endif /* MOUSE_SETSCALING */
1915
1916 #if (defined(MOUSE_GETHWID))
1917 case MOUSE_GETHWID:
1918 error = block_mouse_data(sc, &command_byte);
1919 if (error)
1920 return error;
1921 sc->hw.hwid &= ~0x00ff;
1922 sc->hw.hwid |= get_aux_id(sc->kbdc);
1923 *(int *)addr = sc->hw.hwid & 0x00ff;
1924 unblock_mouse_data(sc, command_byte);
1925 break;
1926 #endif /* MOUSE_GETHWID */
1927
1928 default:
1929 return ENOTTY;
1930 }
1931
1932 return error;
1933 }
1934
1935 static void
1936 psmtimeout(void *arg)
1937 {
1938 struct psm_softc *sc;
1939 int s;
1940
1941 sc = (struct psm_softc *)arg;
1942 s = spltty();
1943 if (sc->watchdog && kbdc_lock(sc->kbdc, TRUE)) {
1944 if (verbose >= 4)
1945 log(LOG_DEBUG, "psm%d: lost interrupt?\n", sc->unit);
1946 psmintr(sc);
1947 kbdc_lock(sc->kbdc, FALSE);
1948 }
1949 sc->watchdog = TRUE;
1950 splx(s);
1951 sc->callout = timeout(psmtimeout, (void *)(uintptr_t)sc, hz);
1952 }
1953
1954 static void
1955 psmintr(void *arg)
1956 {
1957 /*
1958 * the table to turn PS/2 mouse button bits (MOUSE_PS2_BUTTON?DOWN)
1959 * into `mousestatus' button bits (MOUSE_BUTTON?DOWN).
1960 */
1961 static int butmap[8] = {
1962 0,
1963 MOUSE_BUTTON1DOWN,
1964 MOUSE_BUTTON3DOWN,
1965 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1966 MOUSE_BUTTON2DOWN,
1967 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN,
1968 MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN,
1969 MOUSE_BUTTON1DOWN | MOUSE_BUTTON2DOWN | MOUSE_BUTTON3DOWN
1970 };
1971 static int butmap_versapad[8] = {
1972 0,
1973 MOUSE_BUTTON3DOWN,
1974 0,
1975 MOUSE_BUTTON3DOWN,
1976 MOUSE_BUTTON1DOWN,
1977 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN,
1978 MOUSE_BUTTON1DOWN,
1979 MOUSE_BUTTON1DOWN | MOUSE_BUTTON3DOWN
1980 };
1981 register struct psm_softc *sc = arg;
1982 mousestatus_t ms;
1983 struct timeval tv;
1984 int x, y, z;
1985 int c;
1986 int l;
1987 int x0, y0;
1988
1989 /* read until there is nothing to read */
1990 while((c = read_aux_data_no_wait(sc->kbdc)) != -1) {
1991
1992 /* discard the byte if the device is not open */
1993 if ((sc->state & PSM_OPEN) == 0)
1994 continue;
1995
1996 getmicrouptime(&tv);
1997 if ((sc->inputbytes > 0) && timevalcmp(&tv, &sc->inputtimeout, >)) {
1998 log(LOG_DEBUG, "psmintr: delay too long; resetting byte count\n");
1999 sc->inputbytes = 0;
2000 sc->syncerrors = 0;
2001 }
2002 sc->inputtimeout.tv_sec = PSM_INPUT_TIMEOUT/1000000;
2003 sc->inputtimeout.tv_usec = PSM_INPUT_TIMEOUT%1000000;
2004 timevaladd(&sc->inputtimeout, &tv);
2005
2006 sc->ipacket[sc->inputbytes++] = c;
2007 if (sc->inputbytes < sc->mode.packetsize)
2008 continue;
2009
2010 #if 0
2011 log(LOG_DEBUG, "psmintr: %02x %02x %02x %02x %02x %02x\n",
2012 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2],
2013 sc->ipacket[3], sc->ipacket[4], sc->ipacket[5]);
2014 #endif
2015
2016 c = sc->ipacket[0];
2017
2018 if ((c & sc->mode.syncmask[0]) != sc->mode.syncmask[1]) {
2019 log(LOG_DEBUG, "psmintr: out of sync (%04x != %04x).\n",
2020 c & sc->mode.syncmask[0], sc->mode.syncmask[1]);
2021 ++sc->syncerrors;
2022 if (sc->syncerrors < sc->mode.packetsize) {
2023 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2024 --sc->inputbytes;
2025 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2026 } else if (sc->syncerrors == sc->mode.packetsize) {
2027 log(LOG_DEBUG, "psmintr: re-enable the mouse.\n");
2028 sc->inputbytes = 0;
2029 disable_aux_dev(sc->kbdc);
2030 enable_aux_dev(sc->kbdc);
2031 } else if (sc->syncerrors < PSM_SYNCERR_THRESHOLD1) {
2032 log(LOG_DEBUG, "psmintr: discard a byte (%d).\n", sc->syncerrors);
2033 --sc->inputbytes;
2034 bcopy(&sc->ipacket[1], &sc->ipacket[0], sc->inputbytes);
2035 } else if (sc->syncerrors >= PSM_SYNCERR_THRESHOLD1) {
2036 log(LOG_DEBUG, "psmintr: reset the mouse.\n");
2037 reinitialize(sc, TRUE);
2038 }
2039 continue;
2040 }
2041
2042 /*
2043 * A kludge for Kensington device!
2044 * The MSB of the horizontal count appears to be stored in
2045 * a strange place.
2046 */
2047 if (sc->hw.model == MOUSE_MODEL_THINK)
2048 sc->ipacket[1] |= (c & MOUSE_PS2_XOVERFLOW) ? 0x80 : 0;
2049
2050 /* ignore the overflow bits... */
2051 x = (c & MOUSE_PS2_XNEG) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2052 y = (c & MOUSE_PS2_YNEG) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2053 z = 0;
2054 ms.obutton = sc->button; /* previous button state */
2055 ms.button = butmap[c & MOUSE_PS2_BUTTONS];
2056 /* `tapping' action */
2057 if (sc->config & PSM_CONFIG_FORCETAP)
2058 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2059
2060 switch (sc->hw.model) {
2061
2062 case MOUSE_MODEL_EXPLORER:
2063 /*
2064 * b7 b6 b5 b4 b3 b2 b1 b0
2065 * byte 1: oy ox sy sx 1 M R L
2066 * byte 2: x x x x x x x x
2067 * byte 3: y y y y y y y y
2068 * byte 4: * * S2 S1 s d2 d1 d0
2069 *
2070 * L, M, R, S1, S2: left, middle, right and side buttons
2071 * s: wheel data sign bit
2072 * d2-d0: wheel data
2073 */
2074 z = (sc->ipacket[3] & MOUSE_EXPLORER_ZNEG)
2075 ? (sc->ipacket[3] & 0x0f) - 16 : (sc->ipacket[3] & 0x0f);
2076 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON4DOWN)
2077 ? MOUSE_BUTTON4DOWN : 0;
2078 ms.button |= (sc->ipacket[3] & MOUSE_EXPLORER_BUTTON5DOWN)
2079 ? MOUSE_BUTTON5DOWN : 0;
2080 break;
2081
2082 case MOUSE_MODEL_INTELLI:
2083 case MOUSE_MODEL_NET:
2084 /* wheel data is in the fourth byte */
2085 z = (char)sc->ipacket[3];
2086 /* some mice may send 7 when there is no Z movement?! XXX */
2087 if ((z >= 7) || (z <= -7))
2088 z = 0;
2089 /* some compatible mice have additional buttons */
2090 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON4DOWN)
2091 ? MOUSE_BUTTON4DOWN : 0;
2092 ms.button |= (c & MOUSE_PS2INTELLI_BUTTON5DOWN)
2093 ? MOUSE_BUTTON5DOWN : 0;
2094 break;
2095
2096 case MOUSE_MODEL_MOUSEMANPLUS:
2097 /*
2098 * PS2++ protocl packet
2099 *
2100 * b7 b6 b5 b4 b3 b2 b1 b0
2101 * byte 1: * 1 p3 p2 1 * * *
2102 * byte 2: c1 c2 p1 p0 d1 d0 1 0
2103 *
2104 * p3-p0: packet type
2105 * c1, c2: c1 & c2 == 1, if p2 == 0
2106 * c1 & c2 == 0, if p2 == 1
2107 *
2108 * packet type: 0 (device type)
2109 * See comments in enable_mmanplus() below.
2110 *
2111 * packet type: 1 (wheel data)
2112 *
2113 * b7 b6 b5 b4 b3 b2 b1 b0
2114 * byte 3: h * B5 B4 s d2 d1 d0
2115 *
2116 * h: 1, if horizontal roller data
2117 * 0, if vertical roller data
2118 * B4, B5: button 4 and 5
2119 * s: sign bit
2120 * d2-d0: roller data
2121 *
2122 * packet type: 2 (reserved)
2123 */
2124 if (((c & MOUSE_PS2PLUS_SYNCMASK) == MOUSE_PS2PLUS_SYNC)
2125 && (abs(x) > 191)
2126 && MOUSE_PS2PLUS_CHECKBITS(sc->ipacket)) {
2127 /* the extended data packet encodes button and wheel events */
2128 switch (MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket)) {
2129 case 1:
2130 /* wheel data packet */
2131 x = y = 0;
2132 if (sc->ipacket[2] & 0x80) {
2133 /* horizontal roller count - ignore it XXX*/
2134 } else {
2135 /* vertical roller count */
2136 z = (sc->ipacket[2] & MOUSE_PS2PLUS_ZNEG)
2137 ? (sc->ipacket[2] & 0x0f) - 16
2138 : (sc->ipacket[2] & 0x0f);
2139 }
2140 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON4DOWN)
2141 ? MOUSE_BUTTON4DOWN : 0;
2142 ms.button |= (sc->ipacket[2] & MOUSE_PS2PLUS_BUTTON5DOWN)
2143 ? MOUSE_BUTTON5DOWN : 0;
2144 break;
2145 case 2:
2146 /* this packet type is reserved by Logitech... */
2147 /*
2148 * IBM ScrollPoint Mouse uses this packet type to
2149 * encode both vertical and horizontal scroll movement.
2150 */
2151 x = y = 0;
2152 /* horizontal count */
2153 if (sc->ipacket[2] & 0x0f)
2154 z = (sc->ipacket[2] & MOUSE_SPOINT_WNEG) ? -2 : 2;
2155 /* vertical count */
2156 if (sc->ipacket[2] & 0xf0)
2157 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG) ? -1 : 1;
2158 #if 0
2159 /* vertical count */
2160 z = (sc->ipacket[2] & MOUSE_SPOINT_ZNEG)
2161 ? ((sc->ipacket[2] >> 4) & 0x0f) - 16
2162 : ((sc->ipacket[2] >> 4) & 0x0f);
2163 /* horizontal count */
2164 w = (sc->ipacket[2] & MOUSE_SPOINT_WNEG)
2165 ? (sc->ipacket[2] & 0x0f) - 16
2166 : (sc->ipacket[2] & 0x0f);
2167 #endif
2168 break;
2169 case 0:
2170 /* device type packet - shouldn't happen */
2171 /* FALLTHROUGH */
2172 default:
2173 x = y = 0;
2174 ms.button = ms.obutton;
2175 if (bootverbose)
2176 log(LOG_DEBUG, "psmintr: unknown PS2++ packet type %d: "
2177 "0x%02x 0x%02x 0x%02x\n",
2178 MOUSE_PS2PLUS_PACKET_TYPE(sc->ipacket),
2179 sc->ipacket[0], sc->ipacket[1], sc->ipacket[2]);
2180 break;
2181 }
2182 } else {
2183 /* preserve button states */
2184 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2185 }
2186 break;
2187
2188 case MOUSE_MODEL_GLIDEPOINT:
2189 /* `tapping' action */
2190 ms.button |= ((c & MOUSE_PS2_TAP)) ? 0 : MOUSE_BUTTON4DOWN;
2191 break;
2192
2193 case MOUSE_MODEL_NETSCROLL:
2194 /* three addtional bytes encode buttons and wheel events */
2195 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON3DOWN)
2196 ? MOUSE_BUTTON4DOWN : 0;
2197 ms.button |= (sc->ipacket[3] & MOUSE_PS2_BUTTON1DOWN)
2198 ? MOUSE_BUTTON5DOWN : 0;
2199 z = (sc->ipacket[3] & MOUSE_PS2_XNEG)
2200 ? sc->ipacket[4] - 256 : sc->ipacket[4];
2201 break;
2202
2203 case MOUSE_MODEL_THINK:
2204 /* the fourth button state in the first byte */
2205 ms.button |= (c & MOUSE_PS2_TAP) ? MOUSE_BUTTON4DOWN : 0;
2206 break;
2207
2208 case MOUSE_MODEL_VERSAPAD:
2209 /* VersaPad PS/2 absolute mode message format
2210 *
2211 * [packet1] 7 6 5 4 3 2 1 0(LSB)
2212 * ipacket[0]: 1 1 0 A 1 L T R
2213 * ipacket[1]: H7 H6 H5 H4 H3 H2 H1 H0
2214 * ipacket[2]: V7 V6 V5 V4 V3 V2 V1 V0
2215 * ipacket[3]: 1 1 1 A 1 L T R
2216 * ipacket[4]:V11 V10 V9 V8 H11 H10 H9 H8
2217 * ipacket[5]: 0 P6 P5 P4 P3 P2 P1 P0
2218 *
2219 * [note]
2220 * R: right physical mouse button (1=on)
2221 * T: touch pad virtual button (1=tapping)
2222 * L: left physical mouse button (1=on)
2223 * A: position data is valid (1=valid)
2224 * H: horizontal data (12bit signed integer. H11 is sign bit.)
2225 * V: vertical data (12bit signed integer. V11 is sign bit.)
2226 * P: pressure data
2227 *
2228 * Tapping is mapped to MOUSE_BUTTON4.
2229 */
2230 ms.button = butmap_versapad[c & MOUSE_PS2VERSA_BUTTONS];
2231 ms.button |= (c & MOUSE_PS2VERSA_TAP) ? MOUSE_BUTTON4DOWN : 0;
2232 x = y = 0;
2233 if (c & MOUSE_PS2VERSA_IN_USE) {
2234 x0 = sc->ipacket[1] | (((sc->ipacket[4]) & 0x0f) << 8);
2235 y0 = sc->ipacket[2] | (((sc->ipacket[4]) & 0xf0) << 4);
2236 if (x0 & 0x800)
2237 x0 -= 0x1000;
2238 if (y0 & 0x800)
2239 y0 -= 0x1000;
2240 if (sc->flags & PSM_FLAGS_FINGERDOWN) {
2241 x = sc->xold - x0;
2242 y = y0 - sc->yold;
2243 if (x < 0) /* XXX */
2244 x++;
2245 else if (x)
2246 x--;
2247 if (y < 0)
2248 y++;
2249 else if (y)
2250 y--;
2251 } else {
2252 sc->flags |= PSM_FLAGS_FINGERDOWN;
2253 }
2254 sc->xold = x0;
2255 sc->yold = y0;
2256 } else {
2257 sc->flags &= ~PSM_FLAGS_FINGERDOWN;
2258 }
2259 c = ((x < 0) ? MOUSE_PS2_XNEG : 0)
2260 | ((y < 0) ? MOUSE_PS2_YNEG : 0);
2261 break;
2262
2263 case MOUSE_MODEL_4D:
2264 /*
2265 * b7 b6 b5 b4 b3 b2 b1 b0
2266 * byte 1: s2 d2 s1 d1 1 M R L
2267 * byte 2: sx x x x x x x x
2268 * byte 3: sy y y y y y y y
2269 *
2270 * s1: wheel 1 direction
2271 * d1: wheel 1 data
2272 * s2: wheel 2 direction
2273 * d2: wheel 2 data
2274 */
2275 x = (sc->ipacket[1] & 0x80) ? sc->ipacket[1] - 256 : sc->ipacket[1];
2276 y = (sc->ipacket[2] & 0x80) ? sc->ipacket[2] - 256 : sc->ipacket[2];
2277 switch (c & MOUSE_4D_WHEELBITS) {
2278 case 0x10:
2279 z = 1;
2280 break;
2281 case 0x30:
2282 z = -1;
2283 break;
2284 case 0x40: /* 2nd wheel turning right XXX */
2285 z = 2;
2286 break;
2287 case 0xc0: /* 2nd wheel turning left XXX */
2288 z = -2;
2289 break;
2290 }
2291 break;
2292
2293 case MOUSE_MODEL_4DPLUS:
2294 if ((x < 16 - 256) && (y < 16 - 256)) {
2295 /*
2296 * b7 b6 b5 b4 b3 b2 b1 b0
2297 * byte 1: 0 0 1 1 1 M R L
2298 * byte 2: 0 0 0 0 1 0 0 0
2299 * byte 3: 0 0 0 0 S s d1 d0
2300 *
2301 * L, M, R, S: left, middle, right and side buttons
2302 * s: wheel data sign bit
2303 * d1-d0: wheel data
2304 */
2305 x = y = 0;
2306 if (sc->ipacket[2] & MOUSE_4DPLUS_BUTTON4DOWN)
2307 ms.button |= MOUSE_BUTTON4DOWN;
2308 z = (sc->ipacket[2] & MOUSE_4DPLUS_ZNEG)
2309 ? ((sc->ipacket[2] & 0x07) - 8)
2310 : (sc->ipacket[2] & 0x07) ;
2311 } else {
2312 /* preserve previous button states */
2313 ms.button |= ms.obutton & MOUSE_EXTBUTTONS;
2314 }
2315 break;
2316
2317 case MOUSE_MODEL_GENERIC:
2318 default:
2319 break;
2320 }
2321
2322 /* scale values */
2323 if (sc->mode.accelfactor >= 1) {
2324 if (x != 0) {
2325 x = x * x / sc->mode.accelfactor;
2326 if (x == 0)
2327 x = 1;
2328 if (c & MOUSE_PS2_XNEG)
2329 x = -x;
2330 }
2331 if (y != 0) {
2332 y = y * y / sc->mode.accelfactor;
2333 if (y == 0)
2334 y = 1;
2335 if (c & MOUSE_PS2_YNEG)
2336 y = -y;
2337 }
2338 }
2339
2340 ms.dx = x;
2341 ms.dy = y;
2342 ms.dz = z;
2343 ms.flags = ((x || y || z) ? MOUSE_POSCHANGED : 0)
2344 | (ms.obutton ^ ms.button);
2345
2346 if (sc->mode.level < PSM_LEVEL_NATIVE)
2347 sc->inputbytes = tame_mouse(sc, &ms, sc->ipacket);
2348
2349 sc->status.flags |= ms.flags;
2350 sc->status.dx += ms.dx;
2351 sc->status.dy += ms.dy;
2352 sc->status.dz += ms.dz;
2353 sc->status.button = ms.button;
2354 sc->button = ms.button;
2355
2356 sc->watchdog = FALSE;
2357
2358 /* queue data */
2359 if (sc->queue.count + sc->inputbytes < sizeof(sc->queue.buf)) {
2360 l = imin(sc->inputbytes, sizeof(sc->queue.buf) - sc->queue.tail);
2361 bcopy(&sc->ipacket[0], &sc->queue.buf[sc->queue.tail], l);
2362 if (sc->inputbytes > l)
2363 bcopy(&sc->ipacket[l], &sc->queue.buf[0], sc->inputbytes - l);
2364 sc->queue.tail =
2365 (sc->queue.tail + sc->inputbytes) % sizeof(sc->queue.buf);
2366 sc->queue.count += sc->inputbytes;
2367 }
2368 sc->inputbytes = 0;
2369
2370 if (sc->state & PSM_ASLP) {
2371 sc->state &= ~PSM_ASLP;
2372 wakeup( sc);
2373 }
2374 selwakeuppri(&sc->rsel, PZERO);
2375 }
2376 }
2377
2378 static int
2379 psmpoll(dev_t dev, int events, struct thread *td)
2380 {
2381 struct psm_softc *sc = PSM_SOFTC(PSM_UNIT(dev));
2382 int s;
2383 int revents = 0;
2384
2385 /* Return true if a mouse event available */
2386 s = spltty();
2387 if (events & (POLLIN | POLLRDNORM)) {
2388 if (sc->queue.count > 0)
2389 revents |= events & (POLLIN | POLLRDNORM);
2390 else
2391 selrecord(td, &sc->rsel);
2392 }
2393 splx(s);
2394
2395 return (revents);
2396 }
2397
2398 /* vendor/model specific routines */
2399
2400 static int mouse_id_proc1(KBDC kbdc, int res, int scale, int *status)
2401 {
2402 if (set_mouse_resolution(kbdc, res) != res)
2403 return FALSE;
2404 if (set_mouse_scaling(kbdc, scale)
2405 && set_mouse_scaling(kbdc, scale)
2406 && set_mouse_scaling(kbdc, scale)
2407 && (get_mouse_status(kbdc, status, 0, 3) >= 3))
2408 return TRUE;
2409 return FALSE;
2410 }
2411
2412 static int
2413 mouse_ext_command(KBDC kbdc, int command)
2414 {
2415 int c;
2416
2417 c = (command >> 6) & 0x03;
2418 if (set_mouse_resolution(kbdc, c) != c)
2419 return FALSE;
2420 c = (command >> 4) & 0x03;
2421 if (set_mouse_resolution(kbdc, c) != c)
2422 return FALSE;
2423 c = (command >> 2) & 0x03;
2424 if (set_mouse_resolution(kbdc, c) != c)
2425 return FALSE;
2426 c = (command >> 0) & 0x03;
2427 if (set_mouse_resolution(kbdc, c) != c)
2428 return FALSE;
2429 return TRUE;
2430 }
2431
2432 #if notyet
2433 /* Logitech MouseMan Cordless II */
2434 static int
2435 enable_lcordless(struct psm_softc *sc)
2436 {
2437 int status[3];
2438 int ch;
2439
2440 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 2, status))
2441 return FALSE;
2442 if (status[1] == PSMD_RES_HIGH)
2443 return FALSE;
2444 ch = (status[0] & 0x07) - 1; /* channel # */
2445 if ((ch <= 0) || (ch > 4))
2446 return FALSE;
2447 /*
2448 * status[1]: always one?
2449 * status[2]: battery status? (0-100)
2450 */
2451 return TRUE;
2452 }
2453 #endif /* notyet */
2454
2455 /* Genius NetScroll Mouse, MouseSystems SmartScroll Mouse */
2456 static int
2457 enable_groller(struct psm_softc *sc)
2458 {
2459 int status[3];
2460
2461 /*
2462 * The special sequence to enable the fourth button and the
2463 * roller. Immediately after this sequence check status bytes.
2464 * if the mouse is NetScroll, the second and the third bytes are
2465 * '3' and 'D'.
2466 */
2467
2468 /*
2469 * If the mouse is an ordinary PS/2 mouse, the status bytes should
2470 * look like the following.
2471 *
2472 * byte 1 bit 7 always 0
2473 * bit 6 stream mode (0)
2474 * bit 5 disabled (0)
2475 * bit 4 1:1 scaling (0)
2476 * bit 3 always 0
2477 * bit 0-2 button status
2478 * byte 2 resolution (PSMD_RES_HIGH)
2479 * byte 3 report rate (?)
2480 */
2481
2482 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2483 return FALSE;
2484 if ((status[1] != '3') || (status[2] != 'D'))
2485 return FALSE;
2486 /* FIXME: SmartScroll Mouse has 5 buttons! XXX */
2487 sc->hw.buttons = 4;
2488 return TRUE;
2489 }
2490
2491 /* Genius NetMouse/NetMouse Pro, ASCII Mie Mouse, NetScroll Optical */
2492 static int
2493 enable_gmouse(struct psm_softc *sc)
2494 {
2495 int status[3];
2496
2497 /*
2498 * The special sequence to enable the middle, "rubber" button.
2499 * Immediately after this sequence check status bytes.
2500 * if the mouse is NetMouse, NetMouse Pro, or ASCII MIE Mouse,
2501 * the second and the third bytes are '3' and 'U'.
2502 * NOTE: NetMouse reports that it has three buttons although it has
2503 * two buttons and a rubber button. NetMouse Pro and MIE Mouse
2504 * say they have three buttons too and they do have a button on the
2505 * side...
2506 */
2507 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_HIGH, 1, status))
2508 return FALSE;
2509 if ((status[1] != '3') || (status[2] != 'U'))
2510 return FALSE;
2511 return TRUE;
2512 }
2513
2514 /* ALPS GlidePoint */
2515 static int
2516 enable_aglide(struct psm_softc *sc)
2517 {
2518 int status[3];
2519
2520 /*
2521 * The special sequence to obtain ALPS GlidePoint specific
2522 * information. Immediately after this sequence, status bytes will
2523 * contain something interesting.
2524 * NOTE: ALPS produces several models of GlidePoint. Some of those
2525 * do not respond to this sequence, thus, cannot be detected this way.
2526 */
2527 if (set_mouse_sampling_rate(sc->kbdc, 100) != 100)
2528 return FALSE;
2529 if (!mouse_id_proc1(sc->kbdc, PSMD_RES_LOW, 2, status))
2530 return FALSE;
2531 if ((status[1] == PSMD_RES_LOW) || (status[2] == 100))
2532 return FALSE;
2533 return TRUE;
2534 }
2535
2536 /* Kensington ThinkingMouse/Trackball */
2537 static int
2538 enable_kmouse(struct psm_softc *sc)
2539 {
2540 static unsigned char rate[] = { 20, 60, 40, 20, 20, 60, 40, 20, 20 };
2541 KBDC kbdc = sc->kbdc;
2542 int status[3];
2543 int id1;
2544 int id2;
2545 int i;
2546
2547 id1 = get_aux_id(kbdc);
2548 if (set_mouse_sampling_rate(kbdc, 10) != 10)
2549 return FALSE;
2550 /*
2551 * The device is now in the native mode? It returns a different
2552 * ID value...
2553 */
2554 id2 = get_aux_id(kbdc);
2555 if ((id1 == id2) || (id2 != 2))
2556 return FALSE;
2557
2558 if (set_mouse_resolution(kbdc, PSMD_RES_LOW) != PSMD_RES_LOW)
2559 return FALSE;
2560 #if PSM_DEBUG >= 2
2561 /* at this point, resolution is LOW, sampling rate is 10/sec */
2562 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2563 return FALSE;
2564 #endif
2565
2566 /*
2567 * The special sequence to enable the third and fourth buttons.
2568 * Otherwise they behave like the first and second buttons.
2569 */
2570 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2571 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2572 return FALSE;
2573 }
2574
2575 /*
2576 * At this point, the device is using default resolution and
2577 * sampling rate for the native mode.
2578 */
2579 if (get_mouse_status(kbdc, status, 0, 3) < 3)
2580 return FALSE;
2581 if ((status[1] == PSMD_RES_LOW) || (status[2] == rate[i - 1]))
2582 return FALSE;
2583
2584 /* the device appears be enabled by this sequence, diable it for now */
2585 disable_aux_dev(kbdc);
2586 empty_aux_buffer(kbdc, 5);
2587
2588 return TRUE;
2589 }
2590
2591 /* Logitech MouseMan+/FirstMouse+, IBM ScrollPoint Mouse */
2592 static int
2593 enable_mmanplus(struct psm_softc *sc)
2594 {
2595 KBDC kbdc = sc->kbdc;
2596 int data[3];
2597
2598 /* the special sequence to enable the fourth button and the roller. */
2599 /*
2600 * NOTE: for ScrollPoint to respond correctly, the SET_RESOLUTION
2601 * must be called exactly three times since the last RESET command
2602 * before this sequence. XXX
2603 */
2604 if (!set_mouse_scaling(kbdc, 1))
2605 return FALSE;
2606 if (!mouse_ext_command(kbdc, 0x39) || !mouse_ext_command(kbdc, 0xdb))
2607 return FALSE;
2608 if (get_mouse_status(kbdc, data, 1, 3) < 3)
2609 return FALSE;
2610
2611 /*
2612 * PS2++ protocl, packet type 0
2613 *
2614 * b7 b6 b5 b4 b3 b2 b1 b0
2615 * byte 1: * 1 p3 p2 1 * * *
2616 * byte 2: 1 1 p1 p0 m1 m0 1 0
2617 * byte 3: m7 m6 m5 m4 m3 m2 m1 m0
2618 *
2619 * p3-p0: packet type: 0
2620 * m7-m0: model ID: MouseMan+:0x50, FirstMouse+:0x51, ScrollPoint:0x58...
2621 */
2622 /* check constant bits */
2623 if ((data[0] & MOUSE_PS2PLUS_SYNCMASK) != MOUSE_PS2PLUS_SYNC)
2624 return FALSE;
2625 if ((data[1] & 0xc3) != 0xc2)
2626 return FALSE;
2627 /* check d3-d0 in byte 2 */
2628 if (!MOUSE_PS2PLUS_CHECKBITS(data))
2629 return FALSE;
2630 /* check p3-p0 */
2631 if (MOUSE_PS2PLUS_PACKET_TYPE(data) != 0)
2632 return FALSE;
2633
2634 sc->hw.hwid &= 0x00ff;
2635 sc->hw.hwid |= data[2] << 8; /* save model ID */
2636
2637 /*
2638 * MouseMan+ (or FirstMouse+) is now in its native mode, in which
2639 * the wheel and the fourth button events are encoded in the
2640 * special data packet. The mouse may be put in the IntelliMouse mode
2641 * if it is initialized by the IntelliMouse's method.
2642 */
2643 return TRUE;
2644 }
2645
2646 /* MS IntelliMouse Explorer */
2647 static int
2648 enable_msexplorer(struct psm_softc *sc)
2649 {
2650 static unsigned char rate0[] = { 200, 100, 80, };
2651 static unsigned char rate1[] = { 200, 200, 80, };
2652 KBDC kbdc = sc->kbdc;
2653 int id;
2654 int i;
2655
2656 /* the special sequence to enable the extra buttons and the roller. */
2657 for (i = 0; i < sizeof(rate1)/sizeof(rate1[0]); ++i) {
2658 if (set_mouse_sampling_rate(kbdc, rate1[i]) != rate1[i])
2659 return FALSE;
2660 }
2661 /* the device will give the genuine ID only after the above sequence */
2662 id = get_aux_id(kbdc);
2663 if (id != PSM_EXPLORER_ID)
2664 return FALSE;
2665
2666 sc->hw.hwid = id;
2667 sc->hw.buttons = 5; /* IntelliMouse Explorer XXX */
2668
2669 /*
2670 * XXX: this is a kludge to fool some KVM switch products
2671 * which think they are clever enough to know the 4-byte IntelliMouse
2672 * protocol, and assume any other protocols use 3-byte packets.
2673 * They don't convey 4-byte data packets from the IntelliMouse Explorer
2674 * correctly to the host computer because of this!
2675 * The following sequence is actually IntelliMouse's "wake up"
2676 * sequence; it will make the KVM think the mouse is IntelliMouse
2677 * when it is in fact IntelliMouse Explorer.
2678 */
2679 for (i = 0; i < sizeof(rate0)/sizeof(rate0[0]); ++i) {
2680 if (set_mouse_sampling_rate(kbdc, rate0[i]) != rate0[i])
2681 break;
2682 }
2683 id = get_aux_id(kbdc);
2684
2685 return TRUE;
2686 }
2687
2688 /* MS IntelliMouse */
2689 static int
2690 enable_msintelli(struct psm_softc *sc)
2691 {
2692 /*
2693 * Logitech MouseMan+ and FirstMouse+ will also respond to this
2694 * probe routine and act like IntelliMouse.
2695 */
2696
2697 static unsigned char rate[] = { 200, 100, 80, };
2698 KBDC kbdc = sc->kbdc;
2699 int id;
2700 int i;
2701
2702 /* the special sequence to enable the third button and the roller. */
2703 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2704 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2705 return FALSE;
2706 }
2707 /* the device will give the genuine ID only after the above sequence */
2708 id = get_aux_id(kbdc);
2709 if (id != PSM_INTELLI_ID)
2710 return FALSE;
2711
2712 sc->hw.hwid = id;
2713 sc->hw.buttons = 3;
2714
2715 return TRUE;
2716 }
2717
2718 /* A4 Tech 4D Mouse */
2719 static int
2720 enable_4dmouse(struct psm_softc *sc)
2721 {
2722 /*
2723 * Newer wheel mice from A4 Tech may use the 4D+ protocol.
2724 */
2725
2726 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2727 KBDC kbdc = sc->kbdc;
2728 int id;
2729 int i;
2730
2731 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2732 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2733 return FALSE;
2734 }
2735 id = get_aux_id(kbdc);
2736 /*
2737 * WinEasy 4D, 4 Way Scroll 4D: 6
2738 * Cable-Free 4D: 8 (4DPLUS)
2739 * WinBest 4D+, 4 Way Scroll 4D+: 8 (4DPLUS)
2740 */
2741 if (id != PSM_4DMOUSE_ID)
2742 return FALSE;
2743
2744 sc->hw.hwid = id;
2745 sc->hw.buttons = 3; /* XXX some 4D mice have 4? */
2746
2747 return TRUE;
2748 }
2749
2750 /* A4 Tech 4D+ Mouse */
2751 static int
2752 enable_4dplus(struct psm_softc *sc)
2753 {
2754 /*
2755 * Newer wheel mice from A4 Tech seem to use this protocol.
2756 * Older models are recognized as either 4D Mouse or IntelliMouse.
2757 */
2758 KBDC kbdc = sc->kbdc;
2759 int id;
2760
2761 /*
2762 * enable_4dmouse() already issued the following ID sequence...
2763 static unsigned char rate[] = { 200, 100, 80, 60, 40, 20 };
2764 int i;
2765
2766 for (i = 0; i < sizeof(rate)/sizeof(rate[0]); ++i) {
2767 if (set_mouse_sampling_rate(kbdc, rate[i]) != rate[i])
2768 return FALSE;
2769 }
2770 */
2771
2772 id = get_aux_id(kbdc);
2773 switch (id) {
2774 case PSM_4DPLUS_ID:
2775 sc->hw.buttons = 4;
2776 break;
2777 case PSM_4DPLUS_RFSW35_ID:
2778 sc->hw.buttons = 3;
2779 break;
2780 default:
2781 return FALSE;
2782 }
2783
2784 sc->hw.hwid = id;
2785
2786 return TRUE;
2787 }
2788
2789 /* Interlink electronics VersaPad */
2790 static int
2791 enable_versapad(struct psm_softc *sc)
2792 {
2793 KBDC kbdc = sc->kbdc;
2794 int data[3];
2795
2796 set_mouse_resolution(kbdc, PSMD_RES_MEDIUM_HIGH); /* set res. 2 */
2797 set_mouse_sampling_rate(kbdc, 100); /* set rate 100 */
2798 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2799 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2800 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2801 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2802 if (get_mouse_status(kbdc, data, 0, 3) < 3) /* get status */
2803 return FALSE;
2804 if (data[2] != 0xa || data[1] != 0 ) /* rate == 0xa && res. == 0 */
2805 return FALSE;
2806 set_mouse_scaling(kbdc, 1); /* set scale 1:1 */
2807
2808 sc->config |= PSM_CONFIG_HOOKRESUME | PSM_CONFIG_INITAFTERSUSPEND;
2809
2810 return TRUE; /* PS/2 absolute mode */
2811 }
2812
2813 static int
2814 psmresume(device_t dev)
2815 {
2816 struct psm_softc *sc = device_get_softc(dev);
2817 int unit = device_get_unit(dev);
2818 int err;
2819
2820 if (verbose >= 2)
2821 log(LOG_NOTICE, "psm%d: system resume hook called.\n", unit);
2822
2823 if (!(sc->config & PSM_CONFIG_HOOKRESUME))
2824 return (0);
2825
2826 err = reinitialize(sc, sc->config & PSM_CONFIG_INITAFTERSUSPEND);
2827
2828 if ((sc->state & PSM_ASLP) && !(sc->state & PSM_VALID)) {
2829 /*
2830 * Release the blocked process; it must be notified that the device
2831 * cannot be accessed anymore.
2832 */
2833 sc->state &= ~PSM_ASLP;
2834 wakeup(sc);
2835 }
2836
2837 if (verbose >= 2)
2838 log(LOG_DEBUG, "psm%d: system resume hook exiting.\n", unit);
2839
2840 return (err);
2841 }
2842
2843 DRIVER_MODULE(psm, atkbdc, psm_driver, psm_devclass, 0, 0);
2844
2845 /*
2846 * This sucks up assignments from PNPBIOS and ACPI.
2847 */
2848
2849 /*
2850 * When the PS/2 mouse device is reported by ACPI or PnP BIOS, it may
2851 * appear BEFORE the AT keyboard controller. As the PS/2 mouse device
2852 * can be probed and attached only after the AT keyboard controller is
2853 * attached, we shall quietly reserve the IRQ resource for later use.
2854 * If the PS/2 mouse device is reported to us AFTER the keyboard controller,
2855 * copy the IRQ resource to the PS/2 mouse device instance hanging
2856 * under the keyboard controller, then probe and attach it.
2857 */
2858
2859 static devclass_t psmcpnp_devclass;
2860
2861 static device_probe_t psmcpnp_probe;
2862 static device_attach_t psmcpnp_attach;
2863
2864 static device_method_t psmcpnp_methods[] = {
2865 DEVMETHOD(device_probe, psmcpnp_probe),
2866 DEVMETHOD(device_attach, psmcpnp_attach),
2867
2868 { 0, 0 }
2869 };
2870
2871 static driver_t psmcpnp_driver = {
2872 PSMCPNP_DRIVER_NAME,
2873 psmcpnp_methods,
2874 1, /* no softc */
2875 };
2876
2877 static struct isa_pnp_id psmcpnp_ids[] = {
2878 { 0x030fd041, "PS/2 mouse port" }, /* PNP0F03 */
2879 { 0x130fd041, "PS/2 mouse port" }, /* PNP0F13 */
2880 { 0x1303d041, "PS/2 port" }, /* PNP0313, XXX */
2881 { 0x02002e4f, "Dell PS/2 mouse port" }, /* Lat. X200, Dell */
2882 { 0x80374d24, "IBM PS/2 mouse port" }, /* IBM3780, ThinkPad */
2883 { 0x81374d24, "IBM PS/2 mouse port" }, /* IBM3781, ThinkPad */
2884 { 0x0190d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9001, Vaio */
2885 { 0x0290d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9002, Vaio */
2886 { 0x0390d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9003, Vaio */
2887 { 0x0490d94d, "SONY VAIO PS/2 mouse port"}, /* SNY9004, Vaio */
2888 { 0 }
2889 };
2890
2891 static int
2892 create_a_copy(device_t atkbdc, device_t me)
2893 {
2894 device_t psm;
2895 u_long irq;
2896
2897 /* find the PS/2 mouse device instance under the keyboard controller */
2898 psm = device_find_child(atkbdc, PSM_DRIVER_NAME,
2899 device_get_unit(atkbdc));
2900 if (psm == NULL)
2901 return ENXIO;
2902 if (device_get_state(psm) != DS_NOTPRESENT)
2903 return 0;
2904
2905 /* move our resource to the found device */
2906 irq = bus_get_resource_start(me, SYS_RES_IRQ, 0);
2907 bus_set_resource(psm, SYS_RES_IRQ, KBDC_RID_AUX, irq, 1);
2908
2909 /* ...then probe and attach it */
2910 return device_probe_and_attach(psm);
2911 }
2912
2913 static int
2914 psmcpnp_probe(device_t dev)
2915 {
2916 struct resource *res;
2917 u_long irq;
2918 int rid;
2919
2920 if (ISA_PNP_PROBE(device_get_parent(dev), dev, psmcpnp_ids))
2921 return ENXIO;
2922
2923 /*
2924 * The PnP BIOS and ACPI are supposed to assign an IRQ (12)
2925 * to the PS/2 mouse device node. But, some buggy PnP BIOS
2926 * declares the PS/2 mouse device node without an IRQ resource!
2927 * If this happens, we shall refer to device hints.
2928 * If we still don't find it there, use a hardcoded value... XXX
2929 */
2930 rid = 0;
2931 irq = bus_get_resource_start(dev, SYS_RES_IRQ, rid);
2932 if (irq <= 0) {
2933 if (resource_long_value(PSM_DRIVER_NAME,
2934 device_get_unit(dev), "irq", &irq) != 0)
2935 irq = 12; /* XXX */
2936 device_printf(dev, "irq resource info is missing; "
2937 "assuming irq %ld\n", irq);
2938 bus_set_resource(dev, SYS_RES_IRQ, rid, irq, 1);
2939 }
2940 res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2941 RF_SHAREABLE);
2942 bus_release_resource(dev, SYS_RES_IRQ, rid, res);
2943
2944 /* keep quiet */
2945 if (!bootverbose)
2946 device_quiet(dev);
2947
2948 return ((res == NULL) ? ENXIO : 0);
2949 }
2950
2951 static int
2952 psmcpnp_attach(device_t dev)
2953 {
2954 device_t atkbdc;
2955 int rid;
2956
2957 /* find the keyboard controller, which may be on acpi* or isa* bus */
2958 atkbdc = devclass_get_device(devclass_find(ATKBDC_DRIVER_NAME),
2959 device_get_unit(dev));
2960 if ((atkbdc != NULL) && (device_get_state(atkbdc) == DS_ATTACHED)) {
2961 create_a_copy(atkbdc, dev);
2962 } else {
2963 /*
2964 * If we don't have the AT keyboard controller yet,
2965 * just reserve the IRQ for later use...
2966 * (See psmidentify() above.)
2967 */
2968 rid = 0;
2969 bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
2970 RF_SHAREABLE);
2971 }
2972
2973 return 0;
2974 }
2975
2976 DRIVER_MODULE(psmcpnp, isa, psmcpnp_driver, psmcpnp_devclass, 0, 0);
2977 DRIVER_MODULE(psmcpnp, acpi, psmcpnp_driver, psmcpnp_devclass, 0, 0);
Cache object: 71a62eee866b624299628f5451ba59ca
|