summaryrefslogtreecommitdiff
path: root/sys/arch/newsmips/dev/ms_hb.c
blob: c893f6c48bb29c3bdc4d83101d6ad1c9904a0f5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*	$NetBSD: ms_hb.c,v 1.16 2021/08/07 16:19:01 thorpej Exp $	*/

/*-
 * Copyright (c) 2000 Tsubai Masanari.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ms_hb.c,v 1.16 2021/08/07 16:19:01 thorpej Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>

#include <machine/adrsmap.h>

#include <newsmips/dev/hbvar.h>

struct msreg {
	volatile uint8_t ms_data;
	volatile uint8_t ms_stat;
	volatile uint8_t ms_reset;
	volatile uint8_t ms_init;
};

struct ms_hb_softc {
	device_t sc_dev;
	struct msreg *sc_reg;
	device_t sc_wsmousedev;
	int sc_ndata;
	uint8_t sc_buf[3];
};

int ms_hb_match(device_t, cfdata_t, void *);
void ms_hb_attach(device_t, device_t, void *);
int ms_hb_intr(void *);

int ms_hb_enable(void *);
int ms_hb_ioctl(void *, u_long, void *, int, struct lwp *);
void ms_hb_disable(void *);

CFATTACH_DECL_NEW(ms_hb, sizeof(struct ms_hb_softc),
    ms_hb_match, ms_hb_attach, NULL, NULL);

struct wsmouse_accessops ms_hb_accessops = {
	ms_hb_enable,
	ms_hb_ioctl,
	ms_hb_disable
};

int
ms_hb_match(device_t parent, cfdata_t cf, void *aux)
{
	struct hb_attach_args *ha = aux;

	if (strcmp(ha->ha_name, "ms") == 0)
		return 1;

	return 0;
}

void
ms_hb_attach(device_t parent, device_t self, void *aux)
{
	struct ms_hb_softc *sc = device_private(self);
	struct hb_attach_args *ha = aux;
	struct msreg *reg;
	struct wsmousedev_attach_args aa;
	int intr;

	sc->sc_dev = self;

	reg = (struct msreg *)ha->ha_addr;
	intr = ha->ha_level;

	if (intr == -1)
		intr = 2;

	sc->sc_reg = reg;

	reg->ms_reset = 0x01;
	reg->ms_init = 0x80;	/* 1200 bps */

	aprint_normal(" level %d\n", intr);

	hb_intr_establish(intr, INTEN0_MSINT, IPL_TTY, ms_hb_intr, sc);

	aa.accessops = &ms_hb_accessops;
	aa.accesscookie = sc;
	sc->sc_wsmousedev = config_found(self, &aa, wsmousedevprint, CFARGS_NONE);
}

int
ms_hb_intr(void *v)
{
	struct ms_hb_softc *sc = v;
	struct msreg *reg = sc->sc_reg;
	volatile uint8_t *ien = (void *)INTEN0;
	int code, index, byte0, byte1, byte2;
	int button, dx, dy;
	int rv = 0;

	*ien &= ~RX_MSINTE;

	while (reg->ms_stat & RX_MSRDY) {
		rv = 1;
		code = reg->ms_data;
		index = sc->sc_ndata;

		if (sc->sc_wsmousedev == NULL)
			continue;

		if (code & 0x80) {
			sc->sc_buf[0] = code;
			sc->sc_ndata = 1;
			continue;
		}

		if (index == 1) {
			sc->sc_buf[1] = code;
			sc->sc_ndata++;
			continue;
		}

		if (index == 2) {
			sc->sc_buf[2] = code;
			sc->sc_ndata++;

			byte0 = sc->sc_buf[0];
			byte1 = sc->sc_buf[1];
			byte2 = sc->sc_buf[2];

			button = 0;
			if (byte0 & 0x01)
				button |= 1;
			if (byte0 & 0x04)
				button |= 2;
			if (byte0 & 0x02)
				button |= 4;

			if (byte0 & 0x08)
				dx = byte1 - 0x80;
			else
				dx = byte1;
			if (byte0 & 0x10)
				dy = byte2 - 0x80;
			else
				dy = byte2;

			wsmouse_input(sc->sc_wsmousedev,
					button,
					dx, -dy, 0, 0,
					WSMOUSE_INPUT_DELTA);
		}
	}

	*ien |= RX_MSINTE;
	return rv;
}

int
ms_hb_enable(void *v)
{
	volatile uint8_t *ien = (void *)INTEN0;

	*ien |= RX_MSINTE;
	return 0;
}

void
ms_hb_disable(void *v)
{
	volatile uint8_t *ien = (void *)INTEN0;

	*ien &= ~RX_MSINTE;
}

int
ms_hb_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{

	return EPASSTHROUGH;
}