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