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