xref: /netbsd/sys/arch/newsmips/apbus/ms_ap.c (revision c4a72b64)
1 /*	$NetBSD: ms_ap.c,v 1.4 2002/10/02 04:27:51 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/param.h>
30 #include <sys/device.h>
31 #include <sys/systm.h>
32 
33 #include <dev/wscons/wsconsio.h>
34 #include <dev/wscons/wsmousevar.h>
35 
36 #include <machine/adrsmap.h>
37 #include <newsmips/apbus/apbusvar.h>
38 
39 struct msreg {
40 	u_int ms_data;
41 	u_int ms_stat;
42 	u_int ms_intr_en;
43 	u_int ms_reset;
44 	u_int ms_speed;
45 };
46 
47 struct ms_ap_softc {
48 	struct device sc_dev;
49 	volatile struct msreg *sc_reg;
50 	struct device *sc_wsmousedev;
51 	int sc_ndata;
52 	u_char sc_buf[3];
53 };
54 
55 int ms_ap_match(struct device *, struct cfdata *, void *);
56 void ms_ap_attach(struct device *, struct device *, void *);
57 int ms_ap_intr(void *);
58 
59 int ms_ap_enable(void *);
60 int ms_ap_ioctl(void *, u_long, caddr_t, int, struct proc *);
61 void ms_ap_disable(void *);
62 
63 CFATTACH_DECL(ms_ap, sizeof(struct ms_ap_softc),
64     ms_ap_match, ms_ap_attach, NULL, NULL);
65 
66 struct wsmouse_accessops ms_ap_accessops = {
67 	ms_ap_enable,
68 	ms_ap_ioctl,
69 	ms_ap_disable
70 };
71 
72 int
73 ms_ap_match(parent, cf, aux)
74 	struct device *parent;
75 	struct cfdata *cf;
76 	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
87 ms_ap_attach(parent, self, aux)
88 	struct device *parent, *self;
89 	void *aux;
90 {
91 	struct ms_ap_softc *sc = (void *)self;
92 	struct apbus_attach_args *apa = aux;
93 	volatile struct msreg *reg = (void *)apa->apa_hwbase;
94 	struct wsmousedev_attach_args aa;
95 
96 	printf(" slot%d addr 0x%lx\n", apa->apa_slotno, apa->apa_hwbase);
97 
98 	sc->sc_reg = reg;
99 
100 	reg->ms_reset = 0x03;
101 	reg->ms_speed = 0x01;
102 	reg->ms_intr_en = 0;
103 
104 	apbus_intr_establish(1, NEWS5000_INT1_KBD, 0, ms_ap_intr, sc,
105 	    "", apa->apa_ctlnum);
106 
107 	aa.accessops = &ms_ap_accessops;
108 	aa.accesscookie = sc;
109 	sc->sc_wsmousedev = config_found(self, &aa, wsmousedevprint);
110 }
111 
112 int
113 ms_ap_intr(v)
114 	void *v;
115 {
116 	struct ms_ap_softc *sc = v;
117 	volatile struct msreg *reg = sc->sc_reg;
118 	int code, index, byte0, byte1, byte2;
119 	int button, dx, dy;
120 	int rv = 0;
121 
122 	reg->ms_intr_en = 0;
123 
124 	while (reg->ms_stat & RX_MSRDY) {
125 		rv = 1;
126 		code = reg->ms_data;
127 		index = sc->sc_ndata;
128 
129 		if (sc->sc_wsmousedev == NULL)
130 			continue;
131 
132 		if (code & 0x80) {
133 			sc->sc_buf[0] = code;
134 			sc->sc_ndata = 1;
135 			continue;
136 		}
137 
138 		if (index == 1) {
139 			sc->sc_buf[1] = code;
140 			sc->sc_ndata++;
141 			continue;
142 		}
143 
144 		if (index == 2) {
145 			sc->sc_buf[2] = code;
146 			sc->sc_ndata++;
147 
148 			byte0 = sc->sc_buf[0];
149 			byte1 = sc->sc_buf[1];
150 			byte2 = sc->sc_buf[2];
151 
152 			button = 0;
153 			if (byte0 & 0x01)
154 				button |= 1;
155 			if (byte0 & 0x04)
156 				button |= 2;
157 			if (byte0 & 0x02)
158 				button |= 4;
159 
160 			if (byte0 & 0x08)
161 				dx = byte1 - 0x80;
162 			else
163 				dx = byte1;
164 			if (byte0 & 0x10)
165 				dy = byte2 - 0x80;
166 			else
167 				dy = byte2;
168 
169 			wsmouse_input(sc->sc_wsmousedev, button, dx, -dy, 0,
170 			    WSMOUSE_INPUT_DELTA);
171 		}
172 	}
173 
174 	reg->ms_intr_en = 1;
175 	return rv;
176 }
177 
178 int
179 ms_ap_enable(v)
180 	void *v;
181 {
182 	struct ms_ap_softc *sc = v;
183 	volatile struct msreg *reg = sc->sc_reg;
184 
185 	reg->ms_intr_en = 1;
186 	return 0;
187 }
188 
189 void
190 ms_ap_disable(v)
191 	void *v;
192 {
193 	struct ms_ap_softc *sc = v;
194 	volatile struct msreg *reg = sc->sc_reg;
195 
196 	reg->ms_intr_en = 0;
197 }
198 
199 int
200 ms_ap_ioctl(v, cmd, data, flag, p)
201 	void *v;
202 	u_long cmd;
203 	caddr_t data;
204 	int flag;
205 	struct proc *p;
206 {
207 	return EPASSTHROUGH;
208 }
209