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