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