Home | History | Annotate | Line # | Download | only in dev
ms_hb.c revision 1.15
      1  1.15   thorpej /*	$NetBSD: ms_hb.c,v 1.15 2021/04/24 23:36:44 thorpej Exp $	*/
      2   1.1    tsubai 
      3   1.1    tsubai /*-
      4   1.1    tsubai  * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
      5   1.1    tsubai  *
      6   1.1    tsubai  * Redistribution and use in source and binary forms, with or without
      7   1.1    tsubai  * modification, are permitted provided that the following conditions
      8   1.1    tsubai  * are met:
      9   1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     10   1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     11   1.1    tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1    tsubai  *    notice, this list of conditions and the following disclaimer in the
     13   1.1    tsubai  *    documentation and/or other materials provided with the distribution.
     14   1.1    tsubai  * 3. The name of the author may not be used to endorse or promote products
     15   1.1    tsubai  *    derived from this software without specific prior written permission.
     16   1.1    tsubai  *
     17   1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.1    tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.1    tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.1    tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.1    tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.1    tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.1    tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.1    tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.1    tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26   1.1    tsubai  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.1    tsubai  */
     28   1.8     lukem 
     29   1.8     lukem #include <sys/cdefs.h>
     30  1.15   thorpej __KERNEL_RCSID(0, "$NetBSD: ms_hb.c,v 1.15 2021/04/24 23:36:44 thorpej Exp $");
     31   1.1    tsubai 
     32   1.1    tsubai #include <sys/param.h>
     33   1.1    tsubai #include <sys/device.h>
     34   1.1    tsubai #include <sys/systm.h>
     35   1.1    tsubai 
     36   1.1    tsubai #include <dev/wscons/wsconsio.h>
     37   1.1    tsubai #include <dev/wscons/wsmousevar.h>
     38   1.1    tsubai 
     39   1.1    tsubai #include <machine/adrsmap.h>
     40   1.1    tsubai 
     41   1.5   tsutsui #include <newsmips/dev/hbvar.h>
     42   1.5   tsutsui 
     43   1.1    tsubai struct msreg {
     44  1.13   tsutsui 	volatile uint8_t ms_data;
     45  1.13   tsutsui 	volatile uint8_t ms_stat;
     46  1.13   tsutsui 	volatile uint8_t ms_reset;
     47  1.13   tsutsui 	volatile uint8_t ms_init;
     48   1.1    tsubai };
     49   1.1    tsubai 
     50   1.1    tsubai struct ms_hb_softc {
     51  1.13   tsutsui 	device_t sc_dev;
     52  1.13   tsutsui 	struct msreg *sc_reg;
     53  1.14   tsutsui 	device_t sc_wsmousedev;
     54   1.1    tsubai 	int sc_ndata;
     55  1.13   tsutsui 	uint8_t sc_buf[3];
     56   1.1    tsubai };
     57   1.1    tsubai 
     58  1.13   tsutsui int ms_hb_match(device_t, cfdata_t, void *);
     59  1.13   tsutsui void ms_hb_attach(device_t, device_t, void *);
     60   1.1    tsubai int ms_hb_intr(void *);
     61   1.1    tsubai 
     62   1.1    tsubai int ms_hb_enable(void *);
     63  1.12  christos int ms_hb_ioctl(void *, u_long, void *, int, struct lwp *);
     64   1.1    tsubai void ms_hb_disable(void *);
     65   1.1    tsubai 
     66  1.13   tsutsui CFATTACH_DECL_NEW(ms_hb, sizeof(struct ms_hb_softc),
     67   1.4   thorpej     ms_hb_match, ms_hb_attach, NULL, NULL);
     68   1.1    tsubai 
     69   1.1    tsubai struct wsmouse_accessops ms_hb_accessops = {
     70   1.1    tsubai 	ms_hb_enable,
     71   1.1    tsubai 	ms_hb_ioctl,
     72   1.1    tsubai 	ms_hb_disable
     73   1.1    tsubai };
     74   1.1    tsubai 
     75   1.1    tsubai int
     76  1.13   tsutsui ms_hb_match(device_t parent, cfdata_t cf, void *aux)
     77   1.1    tsubai {
     78   1.5   tsutsui 	struct hb_attach_args *ha = aux;
     79   1.1    tsubai 
     80   1.5   tsutsui 	if (strcmp(ha->ha_name, "ms") == 0)
     81   1.1    tsubai 		return 1;
     82   1.1    tsubai 
     83   1.1    tsubai 	return 0;
     84   1.1    tsubai }
     85   1.1    tsubai 
     86   1.1    tsubai void
     87  1.13   tsutsui ms_hb_attach(device_t parent, device_t self, void *aux)
     88   1.1    tsubai {
     89  1.13   tsutsui 	struct ms_hb_softc *sc = device_private(self);
     90   1.5   tsutsui 	struct hb_attach_args *ha = aux;
     91  1.13   tsutsui 	struct msreg *reg;
     92   1.1    tsubai 	struct wsmousedev_attach_args aa;
     93   1.5   tsutsui 	int intr;
     94   1.5   tsutsui 
     95  1.13   tsutsui 	sc->sc_dev = self;
     96  1.13   tsutsui 
     97   1.6   tsutsui 	reg = (struct msreg *)ha->ha_addr;
     98   1.5   tsutsui 	intr = ha->ha_level;
     99   1.1    tsubai 
    100   1.1    tsubai 	if (intr == -1)
    101   1.1    tsubai 		intr = 2;
    102   1.1    tsubai 
    103   1.1    tsubai 	sc->sc_reg = reg;
    104   1.5   tsutsui 
    105   1.1    tsubai 	reg->ms_reset = 0x01;
    106   1.1    tsubai 	reg->ms_init = 0x80;	/* 1200 bps */
    107   1.1    tsubai 
    108  1.13   tsutsui 	aprint_normal(" level %d\n", intr);
    109   1.1    tsubai 
    110   1.7   tsutsui 	hb_intr_establish(intr, INTEN0_MSINT, IPL_TTY, ms_hb_intr, sc);
    111   1.1    tsubai 
    112   1.1    tsubai 	aa.accessops = &ms_hb_accessops;
    113   1.1    tsubai 	aa.accesscookie = sc;
    114  1.15   thorpej 	sc->sc_wsmousedev = config_found(self, &aa, wsmousedevprint, CFARG_EOL);
    115   1.1    tsubai }
    116   1.1    tsubai 
    117   1.1    tsubai int
    118   1.9   tsutsui ms_hb_intr(void *v)
    119   1.1    tsubai {
    120   1.1    tsubai 	struct ms_hb_softc *sc = v;
    121  1.13   tsutsui 	struct msreg *reg = sc->sc_reg;
    122  1.13   tsutsui 	volatile uint8_t *ien = (void *)INTEN0;
    123   1.1    tsubai 	int code, index, byte0, byte1, byte2;
    124   1.1    tsubai 	int button, dx, dy;
    125   1.1    tsubai 	int rv = 0;
    126   1.1    tsubai 
    127   1.1    tsubai 	*ien &= ~RX_MSINTE;
    128   1.1    tsubai 
    129   1.1    tsubai 	while (reg->ms_stat & RX_MSRDY) {
    130   1.1    tsubai 		rv = 1;
    131   1.1    tsubai 		code = reg->ms_data;
    132   1.1    tsubai 		index = sc->sc_ndata;
    133   1.1    tsubai 
    134   1.1    tsubai 		if (sc->sc_wsmousedev == NULL)
    135   1.1    tsubai 			continue;
    136   1.1    tsubai 
    137   1.1    tsubai 		if (code & 0x80) {
    138   1.1    tsubai 			sc->sc_buf[0] = code;
    139   1.1    tsubai 			sc->sc_ndata = 1;
    140   1.1    tsubai 			continue;
    141   1.1    tsubai 		}
    142   1.1    tsubai 
    143   1.1    tsubai 		if (index == 1) {
    144   1.1    tsubai 			sc->sc_buf[1] = code;
    145   1.1    tsubai 			sc->sc_ndata++;
    146   1.1    tsubai 			continue;
    147   1.1    tsubai 		}
    148   1.1    tsubai 
    149   1.1    tsubai 		if (index == 2) {
    150   1.1    tsubai 			sc->sc_buf[2] = code;
    151   1.1    tsubai 			sc->sc_ndata++;
    152   1.1    tsubai 
    153   1.1    tsubai 			byte0 = sc->sc_buf[0];
    154   1.1    tsubai 			byte1 = sc->sc_buf[1];
    155   1.1    tsubai 			byte2 = sc->sc_buf[2];
    156   1.1    tsubai 
    157   1.1    tsubai 			button = 0;
    158   1.1    tsubai 			if (byte0 & 0x01)
    159   1.1    tsubai 				button |= 1;
    160   1.1    tsubai 			if (byte0 & 0x04)
    161   1.1    tsubai 				button |= 2;
    162   1.1    tsubai 			if (byte0 & 0x02)
    163   1.1    tsubai 				button |= 4;
    164   1.1    tsubai 
    165   1.1    tsubai 			if (byte0 & 0x08)
    166   1.1    tsubai 				dx = byte1 - 0x80;
    167   1.1    tsubai 			else
    168   1.1    tsubai 				dx = byte1;
    169   1.1    tsubai 			if (byte0 & 0x10)
    170   1.1    tsubai 				dy = byte2 - 0x80;
    171   1.1    tsubai 			else
    172   1.1    tsubai 				dy = byte2;
    173   1.1    tsubai 
    174  1.11    plunky 			wsmouse_input(sc->sc_wsmousedev,
    175  1.11    plunky 					button,
    176  1.11    plunky 					dx, -dy, 0, 0,
    177  1.11    plunky 					WSMOUSE_INPUT_DELTA);
    178   1.1    tsubai 		}
    179   1.1    tsubai 	}
    180   1.1    tsubai 
    181   1.1    tsubai 	*ien |= RX_MSINTE;
    182   1.1    tsubai 	return rv;
    183   1.1    tsubai }
    184   1.1    tsubai 
    185   1.1    tsubai int
    186   1.9   tsutsui ms_hb_enable(void *v)
    187   1.1    tsubai {
    188  1.13   tsutsui 	volatile uint8_t *ien = (void *)INTEN0;
    189   1.1    tsubai 
    190   1.1    tsubai 	*ien |= RX_MSINTE;
    191   1.1    tsubai 	return 0;
    192   1.1    tsubai }
    193   1.1    tsubai 
    194   1.1    tsubai void
    195   1.9   tsutsui ms_hb_disable(void *v)
    196   1.1    tsubai {
    197  1.13   tsutsui 	volatile uint8_t *ien = (void *)INTEN0;
    198   1.1    tsubai 
    199   1.1    tsubai 	*ien &= ~RX_MSINTE;
    200   1.1    tsubai }
    201   1.1    tsubai 
    202   1.1    tsubai int
    203  1.12  christos ms_hb_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
    204   1.1    tsubai {
    205   1.9   tsutsui 
    206   1.2    atatat 	return EPASSTHROUGH;
    207   1.1    tsubai }
    208