xref: /netbsd-src/sys/arch/newsmips/apbus/ms_ap.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: ms_ap.c,v 1.14 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_ap.c,v 1.14 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 #include <newsmips/apbus/apbusvar.h>
41 
42 struct msreg {
43 	volatile uint32_t ms_data;
44 	volatile uint32_t ms_stat;
45 	volatile uint32_t ms_intr_en;
46 	volatile uint32_t ms_reset;
47 	volatile uint32_t ms_speed;
48 };
49 
50 struct ms_ap_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_ap_match(device_t, cfdata_t, void *);
59 void ms_ap_attach(device_t, device_t, void *);
60 int ms_ap_intr(void *);
61 
62 int ms_ap_enable(void *);
63 int ms_ap_ioctl(void *, u_long, void *, int, struct lwp *);
64 void ms_ap_disable(void *);
65 
66 CFATTACH_DECL_NEW(ms_ap, sizeof(struct ms_ap_softc),
67     ms_ap_match, ms_ap_attach, NULL, NULL);
68 
69 struct wsmouse_accessops ms_ap_accessops = {
70 	ms_ap_enable,
71 	ms_ap_ioctl,
72 	ms_ap_disable
73 };
74 
75 int
ms_ap_match(device_t parent,cfdata_t cf,void * aux)76 ms_ap_match(device_t parent, cfdata_t cf, void *aux)
77 {
78 	struct apbus_attach_args *apa = aux;
79 
80 	if (strcmp(apa->apa_name, "ms") == 0)
81 		return 1;
82 
83 	return 0;
84 }
85 
86 void
ms_ap_attach(device_t parent,device_t self,void * aux)87 ms_ap_attach(device_t parent, device_t self, void *aux)
88 {
89 	struct ms_ap_softc *sc = device_private(self);
90 	struct apbus_attach_args *apa = aux;
91 	struct msreg *reg = (void *)apa->apa_hwbase;
92 	struct wsmousedev_attach_args aa;
93 
94 	sc->sc_dev = self;
95 	aprint_normal(" slot%d addr 0x%lx\n", apa->apa_slotno, apa->apa_hwbase);
96 
97 	sc->sc_reg = reg;
98 
99 	reg->ms_reset = 0x03;
100 	reg->ms_speed = 0x01;
101 	reg->ms_intr_en = 0;
102 
103 	apbus_intr_establish(1, NEWS5000_INT1_KBD, 0, ms_ap_intr, sc,
104 	    device_xname(self), apa->apa_ctlnum);
105 
106 	aa.accessops = &ms_ap_accessops;
107 	aa.accesscookie = sc;
108 	sc->sc_wsmousedev = config_found(self, &aa, wsmousedevprint, CFARGS_NONE);
109 }
110 
111 int
ms_ap_intr(void * v)112 ms_ap_intr(void *v)
113 {
114 	struct ms_ap_softc *sc = v;
115 	struct msreg *reg = sc->sc_reg;
116 	int code, index, byte0, byte1, byte2;
117 	int button, dx, dy;
118 	int rv = 0;
119 
120 	reg->ms_intr_en = 0;
121 
122 	while (reg->ms_stat & RX_MSRDY) {
123 		rv = 1;
124 		code = reg->ms_data;
125 		index = sc->sc_ndata;
126 
127 		if (sc->sc_wsmousedev == NULL)
128 			continue;
129 
130 		if (code & 0x80) {
131 			sc->sc_buf[0] = code;
132 			sc->sc_ndata = 1;
133 			continue;
134 		}
135 
136 		if (index == 1) {
137 			sc->sc_buf[1] = code;
138 			sc->sc_ndata++;
139 			continue;
140 		}
141 
142 		if (index == 2) {
143 			sc->sc_buf[2] = code;
144 			sc->sc_ndata++;
145 
146 			byte0 = sc->sc_buf[0];
147 			byte1 = sc->sc_buf[1];
148 			byte2 = sc->sc_buf[2];
149 
150 			button = 0;
151 			if (byte0 & 0x01)
152 				button |= 1;
153 			if (byte0 & 0x04)
154 				button |= 2;
155 			if (byte0 & 0x02)
156 				button |= 4;
157 
158 			if (byte0 & 0x08)
159 				dx = byte1 - 0x80;
160 			else
161 				dx = byte1;
162 			if (byte0 & 0x10)
163 				dy = byte2 - 0x80;
164 			else
165 				dy = byte2;
166 
167 			wsmouse_input(sc->sc_wsmousedev,
168 					button,
169 					dx, -dy, 0, 0,
170 					WSMOUSE_INPUT_DELTA);
171 		}
172 	}
173 
174 	reg->ms_intr_en = 1;
175 	return rv;
176 }
177 
178 int
ms_ap_enable(void * v)179 ms_ap_enable(void *v)
180 {
181 	struct ms_ap_softc *sc = v;
182 	struct msreg *reg = sc->sc_reg;
183 
184 	reg->ms_intr_en = 1;
185 	return 0;
186 }
187 
188 void
ms_ap_disable(void * v)189 ms_ap_disable(void *v)
190 {
191 	struct ms_ap_softc *sc = v;
192 	struct msreg *reg = sc->sc_reg;
193 
194 	reg->ms_intr_en = 0;
195 }
196 
197 int
ms_ap_ioctl(void * v,u_long cmd,void * data,int flag,struct lwp * l)198 ms_ap_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
199 {
200 
201 	return EPASSTHROUGH;
202 }
203