Home | History | Annotate | Line # | Download | only in hil
hilms.c revision 1.2.78.1
      1  1.2.78.1  thorpej /*	$NetBSD: hilms.c,v 1.2.78.1 2021/03/21 21:09:11 thorpej Exp $	*/
      2       1.1  tsutsui /*	$OpenBSD: hilms.c,v 1.5 2007/04/10 22:37:17 miod Exp $	*/
      3       1.1  tsutsui /*
      4       1.1  tsutsui  * Copyright (c) 2003, Miodrag Vallat.
      5       1.1  tsutsui  * All rights reserved.
      6       1.1  tsutsui  *
      7       1.1  tsutsui  * Redistribution and use in source and binary forms, with or without
      8       1.1  tsutsui  * modification, are permitted provided that the following conditions
      9       1.1  tsutsui  * are met:
     10       1.1  tsutsui  * 1. Redistributions of source code must retain the above copyright
     11       1.1  tsutsui  *    notice, this list of conditions and the following disclaimer.
     12       1.1  tsutsui  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  tsutsui  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  tsutsui  *    documentation and/or other materials provided with the distribution.
     15       1.1  tsutsui  *
     16       1.1  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17       1.1  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18       1.1  tsutsui  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19       1.1  tsutsui  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     20       1.1  tsutsui  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21       1.1  tsutsui  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22       1.1  tsutsui  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1  tsutsui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     24       1.1  tsutsui  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     25       1.1  tsutsui  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26       1.1  tsutsui  * POSSIBILITY OF SUCH DAMAGE.
     27       1.1  tsutsui  *
     28       1.1  tsutsui  */
     29       1.1  tsutsui 
     30       1.1  tsutsui #include <sys/param.h>
     31       1.1  tsutsui #include <sys/systm.h>
     32       1.1  tsutsui #include <sys/device.h>
     33       1.1  tsutsui #include <sys/ioctl.h>
     34       1.1  tsutsui #include <sys/bus.h>
     35       1.1  tsutsui #include <sys/cpu.h>
     36       1.1  tsutsui 
     37       1.1  tsutsui #include <machine/autoconf.h>
     38       1.1  tsutsui 
     39       1.1  tsutsui #include <dev/hil/hilreg.h>
     40       1.1  tsutsui #include <dev/hil/hilvar.h>
     41       1.1  tsutsui #include <dev/hil/hildevs.h>
     42       1.1  tsutsui 
     43       1.1  tsutsui #include <dev/wscons/wsconsio.h>
     44       1.1  tsutsui #include <dev/wscons/wsmousevar.h>
     45       1.1  tsutsui 
     46       1.1  tsutsui struct hilms_softc {
     47       1.1  tsutsui 	struct hildev_softc sc_hildev;
     48       1.1  tsutsui 
     49       1.1  tsutsui 	int		sc_features;
     50       1.1  tsutsui 	u_int		sc_buttons;
     51       1.1  tsutsui 	u_int		sc_axes;
     52       1.1  tsutsui 	int		sc_enabled;
     53       1.1  tsutsui 	int		sc_buttonstate;
     54       1.1  tsutsui 
     55       1.1  tsutsui 	device_t	sc_wsmousedev;
     56       1.1  tsutsui };
     57       1.1  tsutsui 
     58       1.2  tsutsui static int	hilmsprobe(device_t, cfdata_t, void *);
     59       1.2  tsutsui static void	hilmsattach(device_t, device_t, void *);
     60       1.2  tsutsui static int	hilmsdetach(device_t, int);
     61       1.1  tsutsui 
     62       1.1  tsutsui CFATTACH_DECL_NEW(hilms, sizeof(struct hilms_softc),
     63       1.1  tsutsui     hilmsprobe, hilmsattach, hilmsdetach, NULL);
     64       1.1  tsutsui 
     65       1.2  tsutsui static int	hilms_enable(void *);
     66       1.2  tsutsui static int	hilms_ioctl(void *, u_long, void *, int, struct lwp *);
     67       1.2  tsutsui static void	hilms_disable(void *);
     68       1.1  tsutsui 
     69       1.2  tsutsui static const struct wsmouse_accessops hilms_accessops = {
     70       1.1  tsutsui 	hilms_enable,
     71       1.1  tsutsui 	hilms_ioctl,
     72       1.1  tsutsui 	hilms_disable,
     73       1.1  tsutsui };
     74       1.1  tsutsui 
     75       1.2  tsutsui static void	hilms_callback(struct hildev_softc *, u_int, uint8_t *);
     76       1.1  tsutsui 
     77       1.1  tsutsui int
     78       1.1  tsutsui hilmsprobe(device_t parent, cfdata_t cf, void *aux)
     79       1.1  tsutsui {
     80       1.1  tsutsui 	struct hil_attach_args *ha = aux;
     81       1.1  tsutsui 
     82       1.1  tsutsui 	if (ha->ha_type != HIL_DEVICE_MOUSE)
     83       1.2  tsutsui 		return 0;
     84       1.1  tsutsui 
     85       1.1  tsutsui 	/*
     86       1.1  tsutsui 	 * Reject anything that has only buttons - they are handled as
     87       1.1  tsutsui 	 * keyboards, really.
     88       1.1  tsutsui 	 */
     89       1.1  tsutsui 	if (ha->ha_infolen > 1 && (ha->ha_info[1] & HIL_AXMASK) == 0)
     90       1.2  tsutsui 		return 0;
     91       1.1  tsutsui 
     92       1.2  tsutsui 	return 1;
     93       1.1  tsutsui }
     94       1.1  tsutsui 
     95       1.1  tsutsui void
     96       1.1  tsutsui hilmsattach(device_t parent, device_t self, void *aux)
     97       1.1  tsutsui {
     98       1.1  tsutsui 	struct hilms_softc *sc = device_private(self);
     99       1.1  tsutsui 	struct hil_attach_args *ha = aux;
    100       1.1  tsutsui 	struct wsmousedev_attach_args a;
    101       1.1  tsutsui 	int iob, rx, ry;
    102       1.1  tsutsui 
    103       1.1  tsutsui 	sc->sc_hildev.sc_dev = self;
    104       1.1  tsutsui 	sc->hd_code = ha->ha_code;
    105       1.1  tsutsui 	sc->hd_type = ha->ha_type;
    106       1.1  tsutsui 	sc->hd_infolen = ha->ha_infolen;
    107       1.1  tsutsui 	memcpy(sc->hd_info, ha->ha_info, ha->ha_infolen);
    108       1.1  tsutsui 	sc->hd_fn = hilms_callback;
    109       1.1  tsutsui 
    110       1.1  tsutsui 	/*
    111       1.1  tsutsui 	 * Interpret the identification bytes, if any
    112       1.1  tsutsui 	 */
    113       1.1  tsutsui 	rx = ry = 0;
    114       1.1  tsutsui 	if (ha->ha_infolen > 1) {
    115       1.1  tsutsui 		sc->sc_features = ha->ha_info[1];
    116       1.1  tsutsui 		sc->sc_axes = sc->sc_features & HIL_AXMASK;
    117       1.1  tsutsui 
    118       1.1  tsutsui 		if (sc->sc_features & HIL_IOB) {
    119       1.1  tsutsui 			/* skip resolution bytes */
    120       1.1  tsutsui 			iob = 4;
    121       1.1  tsutsui 			if (sc->sc_features & HIL_ABSOLUTE) {
    122       1.1  tsutsui 				/* skip ranges */
    123       1.1  tsutsui 				rx = ha->ha_info[4] | (ha->ha_info[5] << 8);
    124       1.1  tsutsui 				if (sc->sc_axes > 1)
    125       1.1  tsutsui 					ry = ha->ha_info[6] |
    126       1.1  tsutsui 					    (ha->ha_info[7] << 8);
    127       1.1  tsutsui 				iob += 2 * sc->sc_axes;
    128       1.1  tsutsui 			}
    129       1.1  tsutsui 
    130       1.1  tsutsui 			if (iob >= ha->ha_infolen) {
    131       1.1  tsutsui 				sc->sc_features &= ~(HIL_IOB | HILIOB_PIO);
    132       1.1  tsutsui 			} else {
    133       1.1  tsutsui 				iob = ha->ha_info[iob];
    134       1.1  tsutsui 				sc->sc_buttons = iob & HILIOB_BMASK;
    135       1.1  tsutsui 				sc->sc_features |= (iob & HILIOB_PIO);
    136       1.1  tsutsui 			}
    137       1.1  tsutsui 		}
    138       1.1  tsutsui 	}
    139       1.1  tsutsui 
    140       1.2  tsutsui 	aprint_normal(", %d axes", sc->sc_axes);
    141       1.1  tsutsui 	if (sc->sc_buttons == 1)
    142       1.2  tsutsui 		aprint_normal(", 1 button");
    143       1.1  tsutsui 	else if (sc->sc_buttons > 1)
    144       1.2  tsutsui 		aprint_normal(", %d buttons", sc->sc_buttons);
    145       1.1  tsutsui 	if (sc->sc_features & HILIOB_PIO)
    146       1.2  tsutsui 		aprint_normal(", pressure sensor");
    147       1.1  tsutsui 	if (sc->sc_features & HIL_ABSOLUTE) {
    148       1.2  tsutsui 		aprint_normal("\n");
    149       1.2  tsutsui 		aprint_normal_dev(self, "%d", rx);
    150       1.1  tsutsui 		if (ry != 0)
    151       1.2  tsutsui 			aprint_normal("x%d", ry);
    152       1.1  tsutsui 		else
    153       1.2  tsutsui 			aprint_normal(" linear");
    154       1.2  tsutsui 		aprint_normal(" fixed area");
    155       1.1  tsutsui 	}
    156       1.1  tsutsui 
    157       1.2  tsutsui 	aprint_normal("\n");
    158       1.1  tsutsui 
    159       1.1  tsutsui 	sc->sc_enabled = 0;
    160       1.1  tsutsui 
    161       1.1  tsutsui 	a.accessops = &hilms_accessops;
    162       1.1  tsutsui 	a.accesscookie = sc;
    163       1.1  tsutsui 
    164  1.2.78.1  thorpej 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARG_EOL);
    165       1.1  tsutsui }
    166       1.1  tsutsui 
    167       1.1  tsutsui int
    168       1.1  tsutsui hilmsdetach(device_t self, int flags)
    169       1.1  tsutsui {
    170       1.1  tsutsui 	struct hilms_softc *sc = device_private(self);
    171       1.1  tsutsui 
    172       1.1  tsutsui 	if (sc->sc_wsmousedev != NULL)
    173       1.1  tsutsui 		return config_detach(sc->sc_wsmousedev, flags);
    174       1.1  tsutsui 
    175       1.2  tsutsui 	return 0;
    176       1.1  tsutsui }
    177       1.1  tsutsui 
    178       1.1  tsutsui int
    179       1.1  tsutsui hilms_enable(void *v)
    180       1.1  tsutsui {
    181       1.1  tsutsui 	struct hilms_softc *sc = v;
    182       1.1  tsutsui 
    183       1.1  tsutsui 	if (sc->sc_enabled)
    184       1.1  tsutsui 		return EBUSY;
    185       1.1  tsutsui 
    186       1.1  tsutsui 	sc->sc_enabled = 1;
    187       1.1  tsutsui 	sc->sc_buttonstate = 0;
    188       1.1  tsutsui 
    189       1.2  tsutsui 	return 0;
    190       1.1  tsutsui }
    191       1.1  tsutsui 
    192       1.1  tsutsui void
    193       1.1  tsutsui hilms_disable(void *v)
    194       1.1  tsutsui {
    195       1.1  tsutsui 	struct hilms_softc *sc = v;
    196       1.1  tsutsui 
    197       1.1  tsutsui 	sc->sc_enabled = 0;
    198       1.1  tsutsui }
    199       1.1  tsutsui 
    200       1.1  tsutsui int
    201       1.1  tsutsui hilms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    202       1.1  tsutsui {
    203       1.1  tsutsui #if 0
    204       1.1  tsutsui 	struct hilms_softc *sc = v;
    205       1.1  tsutsui #endif
    206       1.1  tsutsui 
    207       1.1  tsutsui 	switch (cmd) {
    208       1.1  tsutsui 	case WSMOUSEIO_GTYPE:
    209       1.1  tsutsui 		*(int *)data = WSMOUSE_TYPE_HIL;
    210       1.1  tsutsui 		return 0;
    211       1.1  tsutsui 	}
    212       1.1  tsutsui 
    213       1.1  tsutsui 	return EPASSTHROUGH;
    214       1.1  tsutsui }
    215       1.1  tsutsui 
    216       1.1  tsutsui void
    217       1.2  tsutsui hilms_callback(struct hildev_softc *hdsc, u_int buflen, uint8_t *buf)
    218       1.1  tsutsui {
    219       1.1  tsutsui 	struct hilms_softc *sc = device_private(hdsc->sc_dev);
    220       1.1  tsutsui 	int type, flags;
    221       1.1  tsutsui 	int dx, dy, dz, button;
    222       1.1  tsutsui #ifdef DIAGNOSTIC
    223       1.1  tsutsui 	int minlen;
    224       1.1  tsutsui #endif
    225       1.1  tsutsui 
    226       1.1  tsutsui 	/*
    227       1.1  tsutsui 	 * Ignore packet if we don't need it
    228       1.1  tsutsui 	 */
    229       1.1  tsutsui 	if (sc->sc_enabled == 0)
    230       1.1  tsutsui 		return;
    231       1.1  tsutsui 
    232       1.1  tsutsui 	type = *buf++;
    233       1.1  tsutsui 
    234       1.1  tsutsui #ifdef DIAGNOSTIC
    235       1.1  tsutsui 	/*
    236       1.1  tsutsui 	 * Check that the packet contains all the expected data,
    237       1.1  tsutsui 	 * ignore it if too short.
    238       1.1  tsutsui 	 */
    239       1.1  tsutsui 	minlen = 1;
    240       1.1  tsutsui 	if (type & HIL_MOUSEMOTION) {
    241       1.1  tsutsui 		minlen += sc->sc_axes <<
    242       1.1  tsutsui 		    (sc->sc_features & HIL_16_BITS) ? 1 : 0;
    243       1.1  tsutsui 	}
    244       1.1  tsutsui 	if (type & HIL_MOUSEBUTTON)
    245       1.1  tsutsui 		minlen++;
    246       1.1  tsutsui 
    247       1.1  tsutsui 	if (minlen > buflen)
    248       1.1  tsutsui 		return;
    249       1.1  tsutsui #endif
    250       1.1  tsutsui 
    251       1.1  tsutsui 	/*
    252       1.1  tsutsui 	 * The packet can contain both a mouse motion and a button event.
    253       1.1  tsutsui 	 * In this case, the motion data comes first.
    254       1.1  tsutsui 	 */
    255       1.1  tsutsui 
    256       1.1  tsutsui 	if (type & HIL_MOUSEMOTION) {
    257       1.1  tsutsui 		flags = sc->sc_features & HIL_ABSOLUTE ?
    258       1.1  tsutsui 		    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y |
    259       1.1  tsutsui 		    WSMOUSE_INPUT_ABSOLUTE_Z : WSMOUSE_INPUT_DELTA;
    260       1.1  tsutsui 		if (sc->sc_features & HIL_16_BITS) {
    261       1.1  tsutsui 			dx = *buf++;
    262       1.1  tsutsui 			dx |= (*buf++) << 8;
    263       1.1  tsutsui 			if (!(sc->sc_features & HIL_ABSOLUTE))
    264       1.1  tsutsui 				dx = (int16_t)dx;
    265       1.1  tsutsui 		} else {
    266       1.1  tsutsui 			dx = *buf++;
    267       1.1  tsutsui 			if (!(sc->sc_features & HIL_ABSOLUTE))
    268       1.1  tsutsui 				dx = (int8_t)dx;
    269       1.1  tsutsui 		}
    270       1.1  tsutsui 		if (sc->sc_axes > 1) {
    271       1.1  tsutsui 			if (sc->sc_features & HIL_16_BITS) {
    272       1.1  tsutsui 				dy = *buf++;
    273       1.1  tsutsui 				dy |= (*buf++) << 8;
    274       1.1  tsutsui 				if (!(sc->sc_features & HIL_ABSOLUTE))
    275       1.1  tsutsui 					dy = (int16_t)dy;
    276       1.1  tsutsui 			} else {
    277       1.1  tsutsui 				dy = *buf++;
    278       1.1  tsutsui 				if (!(sc->sc_features & HIL_ABSOLUTE))
    279       1.1  tsutsui 					dy = (int8_t)dy;
    280       1.1  tsutsui 			}
    281       1.1  tsutsui 			if (sc->sc_axes > 2) {
    282       1.1  tsutsui 				if (sc->sc_features & HIL_16_BITS) {
    283       1.1  tsutsui 					dz = *buf++;
    284       1.1  tsutsui 					dz |= (*buf++) << 8;
    285       1.1  tsutsui 					if (!(sc->sc_features & HIL_ABSOLUTE))
    286       1.1  tsutsui 						dz = (int16_t)dz;
    287       1.1  tsutsui 				} else {
    288       1.1  tsutsui 					dz = *buf++;
    289       1.1  tsutsui 					if (!(sc->sc_features & HIL_ABSOLUTE))
    290       1.1  tsutsui 						dz = (int8_t)dz;
    291       1.1  tsutsui 				}
    292       1.1  tsutsui 			} else
    293       1.1  tsutsui 				dz = 0;
    294       1.1  tsutsui 		} else
    295       1.1  tsutsui 			dy = dz = 0;
    296       1.1  tsutsui 
    297       1.1  tsutsui 		/*
    298       1.1  tsutsui 		 * Correct Y direction for button boxes.
    299       1.1  tsutsui 		 */
    300       1.1  tsutsui 		if ((sc->sc_features & HIL_ABSOLUTE) == 0 &&
    301       1.1  tsutsui 		    sc->sc_buttons == 0)
    302       1.1  tsutsui 			dy = -dy;
    303       1.1  tsutsui 	} else
    304       1.1  tsutsui 		dx = dy = dz = flags = 0;
    305       1.1  tsutsui 
    306       1.1  tsutsui 	if (type & HIL_MOUSEBUTTON) {
    307       1.1  tsutsui 		button = *buf;
    308       1.1  tsutsui 		/*
    309       1.1  tsutsui 		 * The pressure sensor is very primitive and only has
    310       1.1  tsutsui 		 * a boolean behaviour, as an extra mouse button, which is
    311       1.1  tsutsui 		 * down if there is pressure or the pen is near the tablet,
    312       1.1  tsutsui 		 * and up if there is no pressure or the pen is far from the
    313       1.1  tsutsui 		 * tablet - at least for Tablet id 0x94, P/N 46088B
    314       1.1  tsutsui 		 *
    315       1.1  tsutsui 		 * The corresponding codes are 0x8f and 0x8e. Convert them
    316       1.1  tsutsui 		 * to a pseudo fourth button - even if the tablet never
    317       1.1  tsutsui 		 * has three buttons.
    318       1.1  tsutsui 		 */
    319       1.1  tsutsui 		button = (button - 0x80) >> 1;
    320       1.1  tsutsui 		if (button > 4)
    321       1.1  tsutsui 			button = 4;
    322       1.1  tsutsui 
    323       1.1  tsutsui 		if (*buf & 1) {
    324       1.1  tsutsui 			/* Button released, or no pressure */
    325       1.1  tsutsui 			sc->sc_buttonstate &= ~(1 << button);
    326       1.1  tsutsui 		} else {
    327       1.1  tsutsui 			/* Button pressed, or pressure */
    328       1.1  tsutsui 			sc->sc_buttonstate |= (1 << button);
    329       1.1  tsutsui 		}
    330       1.1  tsutsui 		/* buf++; */
    331       1.1  tsutsui 	}
    332       1.1  tsutsui 
    333       1.1  tsutsui 	if (sc->sc_wsmousedev != NULL)
    334       1.1  tsutsui 		wsmouse_input(sc->sc_wsmousedev,
    335       1.1  tsutsui 		    sc->sc_buttonstate, dx, dy, dz, 0, flags);
    336       1.1  tsutsui }
    337