xref: /netbsd-src/sys/arch/newsmips/dev/ms_hb.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /*	$NetBSD: ms_hb.c,v 1.8 2003/07/15 02:59:29 lukem 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.8 2003/07/15 02:59:29 lukem 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 	u_char ms_data;
45 	u_char ms_stat;
46 	u_char ms_reset;
47 	u_char ms_init;
48 };
49 
50 struct ms_hb_softc {
51 	struct device sc_dev;
52 	volatile struct msreg *sc_reg;
53 	struct device *sc_wsmousedev;
54 	int sc_ndata;
55 	u_char sc_buf[3];
56 };
57 
58 int ms_hb_match(struct device *, struct cfdata *, void *);
59 void ms_hb_attach(struct device *, struct device *, void *);
60 int ms_hb_intr(void *);
61 
62 int ms_hb_enable(void *);
63 int ms_hb_ioctl(void *, u_long, caddr_t, int, struct proc *);
64 void ms_hb_disable(void *);
65 
66 CFATTACH_DECL(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(parent, cf, aux)
77 	struct device *parent;
78 	struct cfdata *cf;
79 	void *aux;
80 {
81 	struct hb_attach_args *ha = aux;
82 
83 	if (strcmp(ha->ha_name, "ms") == 0)
84 		return 1;
85 
86 	return 0;
87 }
88 
89 void
90 ms_hb_attach(parent, self, aux)
91 	struct device *parent, *self;
92 	void *aux;
93 {
94 	struct ms_hb_softc *sc = (void *)self;
95 	struct hb_attach_args *ha = aux;
96 	volatile struct msreg *reg;
97 	struct wsmousedev_attach_args aa;
98 	int intr;
99 
100 	reg = (struct msreg *)ha->ha_addr;
101 	intr = ha->ha_level;
102 
103 	if (intr == -1)
104 		intr = 2;
105 
106 	sc->sc_reg = reg;
107 
108 	reg->ms_reset = 0x01;
109 	reg->ms_init = 0x80;	/* 1200 bps */
110 
111 	printf(" level %d\n", intr);
112 
113 	hb_intr_establish(intr, INTEN0_MSINT, IPL_TTY, ms_hb_intr, sc);
114 
115 	aa.accessops = &ms_hb_accessops;
116 	aa.accesscookie = sc;
117 	sc->sc_wsmousedev = config_found(self, &aa, wsmousedevprint);
118 }
119 
120 int
121 ms_hb_intr(v)
122 	void *v;
123 {
124 	struct ms_hb_softc *sc = v;
125 	volatile struct msreg *reg = sc->sc_reg;
126 	volatile u_char *ien = (void *)INTEN0;
127 	int code, index, byte0, byte1, byte2;
128 	int button, dx, dy;
129 	int rv = 0;
130 
131 	*ien &= ~RX_MSINTE;
132 
133 	while (reg->ms_stat & RX_MSRDY) {
134 		rv = 1;
135 		code = reg->ms_data;
136 		index = sc->sc_ndata;
137 
138 		if (sc->sc_wsmousedev == NULL)
139 			continue;
140 
141 		if (code & 0x80) {
142 			sc->sc_buf[0] = code;
143 			sc->sc_ndata = 1;
144 			continue;
145 		}
146 
147 		if (index == 1) {
148 			sc->sc_buf[1] = code;
149 			sc->sc_ndata++;
150 			continue;
151 		}
152 
153 		if (index == 2) {
154 			sc->sc_buf[2] = code;
155 			sc->sc_ndata++;
156 
157 			byte0 = sc->sc_buf[0];
158 			byte1 = sc->sc_buf[1];
159 			byte2 = sc->sc_buf[2];
160 
161 			button = 0;
162 			if (byte0 & 0x01)
163 				button |= 1;
164 			if (byte0 & 0x04)
165 				button |= 2;
166 			if (byte0 & 0x02)
167 				button |= 4;
168 
169 			if (byte0 & 0x08)
170 				dx = byte1 - 0x80;
171 			else
172 				dx = byte1;
173 			if (byte0 & 0x10)
174 				dy = byte2 - 0x80;
175 			else
176 				dy = byte2;
177 
178 			wsmouse_input(sc->sc_wsmousedev, button, dx, -dy, 0,
179 			    WSMOUSE_INPUT_DELTA);
180 		}
181 	}
182 
183 	*ien |= RX_MSINTE;
184 	return rv;
185 }
186 
187 int
188 ms_hb_enable(v)
189 	void *v;
190 {
191 	volatile u_char *ien = (void *)INTEN0;
192 
193 	*ien |= RX_MSINTE;
194 	return 0;
195 }
196 
197 void
198 ms_hb_disable(v)
199 	void *v;
200 {
201 	volatile u_char *ien = (void *)INTEN0;
202 
203 	*ien &= ~RX_MSINTE;
204 }
205 
206 int
207 ms_hb_ioctl(v, cmd, data, flag, p)
208 	void *v;
209 	u_long cmd;
210 	caddr_t data;
211 	int flag;
212 	struct proc *p;
213 {
214 	return EPASSTHROUGH;
215 }
216