xref: /netbsd-src/sys/arch/newsmips/dev/ms_hb.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: ms_hb.c,v 1.12 2007/03/04 06:00:26 christos 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.12 2007/03/04 06:00:26 christos 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, void *, int, struct lwp *);
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(struct device *parent, struct cfdata *cf, 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(struct device *parent, struct device *self, void *aux)
88 {
89 	struct ms_hb_softc *sc = (void *)self;
90 	struct hb_attach_args *ha = aux;
91 	volatile struct msreg *reg;
92 	struct wsmousedev_attach_args aa;
93 	int intr;
94 
95 	reg = (struct msreg *)ha->ha_addr;
96 	intr = ha->ha_level;
97 
98 	if (intr == -1)
99 		intr = 2;
100 
101 	sc->sc_reg = reg;
102 
103 	reg->ms_reset = 0x01;
104 	reg->ms_init = 0x80;	/* 1200 bps */
105 
106 	printf(" level %d\n", intr);
107 
108 	hb_intr_establish(intr, INTEN0_MSINT, IPL_TTY, ms_hb_intr, sc);
109 
110 	aa.accessops = &ms_hb_accessops;
111 	aa.accesscookie = sc;
112 	sc->sc_wsmousedev = config_found(self, &aa, wsmousedevprint);
113 }
114 
115 int
116 ms_hb_intr(void *v)
117 {
118 	struct ms_hb_softc *sc = v;
119 	volatile struct msreg *reg = sc->sc_reg;
120 	volatile u_char *ien = (void *)INTEN0;
121 	int code, index, byte0, byte1, byte2;
122 	int button, dx, dy;
123 	int rv = 0;
124 
125 	*ien &= ~RX_MSINTE;
126 
127 	while (reg->ms_stat & RX_MSRDY) {
128 		rv = 1;
129 		code = reg->ms_data;
130 		index = sc->sc_ndata;
131 
132 		if (sc->sc_wsmousedev == NULL)
133 			continue;
134 
135 		if (code & 0x80) {
136 			sc->sc_buf[0] = code;
137 			sc->sc_ndata = 1;
138 			continue;
139 		}
140 
141 		if (index == 1) {
142 			sc->sc_buf[1] = code;
143 			sc->sc_ndata++;
144 			continue;
145 		}
146 
147 		if (index == 2) {
148 			sc->sc_buf[2] = code;
149 			sc->sc_ndata++;
150 
151 			byte0 = sc->sc_buf[0];
152 			byte1 = sc->sc_buf[1];
153 			byte2 = sc->sc_buf[2];
154 
155 			button = 0;
156 			if (byte0 & 0x01)
157 				button |= 1;
158 			if (byte0 & 0x04)
159 				button |= 2;
160 			if (byte0 & 0x02)
161 				button |= 4;
162 
163 			if (byte0 & 0x08)
164 				dx = byte1 - 0x80;
165 			else
166 				dx = byte1;
167 			if (byte0 & 0x10)
168 				dy = byte2 - 0x80;
169 			else
170 				dy = byte2;
171 
172 			wsmouse_input(sc->sc_wsmousedev,
173 					button,
174 					dx, -dy, 0, 0,
175 					WSMOUSE_INPUT_DELTA);
176 		}
177 	}
178 
179 	*ien |= RX_MSINTE;
180 	return rv;
181 }
182 
183 int
184 ms_hb_enable(void *v)
185 {
186 	volatile u_char *ien = (void *)INTEN0;
187 
188 	*ien |= RX_MSINTE;
189 	return 0;
190 }
191 
192 void
193 ms_hb_disable(void *v)
194 {
195 	volatile u_char *ien = (void *)INTEN0;
196 
197 	*ien &= ~RX_MSINTE;
198 }
199 
200 int
201 ms_hb_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
202 {
203 
204 	return EPASSTHROUGH;
205 }
206