xref: /netbsd-src/sys/arch/newsmips/dev/ms_hb.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: ms_hb.c,v 1.7 2003/05/10 09:46:25 tsutsui 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 
38 #include <newsmips/dev/hbvar.h>
39 
40 struct msreg {
41 	u_char ms_data;
42 	u_char ms_stat;
43 	u_char ms_reset;
44 	u_char ms_init;
45 };
46 
47 struct ms_hb_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_hb_match(struct device *, struct cfdata *, void *);
56 void ms_hb_attach(struct device *, struct device *, void *);
57 int ms_hb_intr(void *);
58 
59 int ms_hb_enable(void *);
60 int ms_hb_ioctl(void *, u_long, caddr_t, int, struct proc *);
61 void ms_hb_disable(void *);
62 
63 CFATTACH_DECL(ms_hb, sizeof(struct ms_hb_softc),
64     ms_hb_match, ms_hb_attach, NULL, NULL);
65 
66 struct wsmouse_accessops ms_hb_accessops = {
67 	ms_hb_enable,
68 	ms_hb_ioctl,
69 	ms_hb_disable
70 };
71 
72 int
73 ms_hb_match(parent, cf, aux)
74 	struct device *parent;
75 	struct cfdata *cf;
76 	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(parent, self, aux)
88 	struct device *parent, *self;
89 	void *aux;
90 {
91 	struct ms_hb_softc *sc = (void *)self;
92 	struct hb_attach_args *ha = aux;
93 	volatile struct msreg *reg;
94 	struct wsmousedev_attach_args aa;
95 	int intr;
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 	printf(" 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);
115 }
116 
117 int
118 ms_hb_intr(v)
119 	void *v;
120 {
121 	struct ms_hb_softc *sc = v;
122 	volatile struct msreg *reg = sc->sc_reg;
123 	volatile u_char *ien = (void *)INTEN0;
124 	int code, index, byte0, byte1, byte2;
125 	int button, dx, dy;
126 	int rv = 0;
127 
128 	*ien &= ~RX_MSINTE;
129 
130 	while (reg->ms_stat & RX_MSRDY) {
131 		rv = 1;
132 		code = reg->ms_data;
133 		index = sc->sc_ndata;
134 
135 		if (sc->sc_wsmousedev == NULL)
136 			continue;
137 
138 		if (code & 0x80) {
139 			sc->sc_buf[0] = code;
140 			sc->sc_ndata = 1;
141 			continue;
142 		}
143 
144 		if (index == 1) {
145 			sc->sc_buf[1] = code;
146 			sc->sc_ndata++;
147 			continue;
148 		}
149 
150 		if (index == 2) {
151 			sc->sc_buf[2] = code;
152 			sc->sc_ndata++;
153 
154 			byte0 = sc->sc_buf[0];
155 			byte1 = sc->sc_buf[1];
156 			byte2 = sc->sc_buf[2];
157 
158 			button = 0;
159 			if (byte0 & 0x01)
160 				button |= 1;
161 			if (byte0 & 0x04)
162 				button |= 2;
163 			if (byte0 & 0x02)
164 				button |= 4;
165 
166 			if (byte0 & 0x08)
167 				dx = byte1 - 0x80;
168 			else
169 				dx = byte1;
170 			if (byte0 & 0x10)
171 				dy = byte2 - 0x80;
172 			else
173 				dy = byte2;
174 
175 			wsmouse_input(sc->sc_wsmousedev, button, dx, -dy, 0,
176 			    WSMOUSE_INPUT_DELTA);
177 		}
178 	}
179 
180 	*ien |= RX_MSINTE;
181 	return rv;
182 }
183 
184 int
185 ms_hb_enable(v)
186 	void *v;
187 {
188 	volatile u_char *ien = (void *)INTEN0;
189 
190 	*ien |= RX_MSINTE;
191 	return 0;
192 }
193 
194 void
195 ms_hb_disable(v)
196 	void *v;
197 {
198 	volatile u_char *ien = (void *)INTEN0;
199 
200 	*ien &= ~RX_MSINTE;
201 }
202 
203 int
204 ms_hb_ioctl(v, cmd, data, flag, p)
205 	void *v;
206 	u_long cmd;
207 	caddr_t data;
208 	int flag;
209 	struct proc *p;
210 {
211 	return EPASSTHROUGH;
212 }
213