ms_hb.c revision 1.8 1 1.8 lukem /* $NetBSD: ms_hb.c,v 1.8 2003/07/15 02:59:29 lukem Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*-
4 1.1 tsubai * Copyright (c) 2000 Tsubai Masanari. All rights reserved.
5 1.1 tsubai *
6 1.1 tsubai * Redistribution and use in source and binary forms, with or without
7 1.1 tsubai * modification, are permitted provided that the following conditions
8 1.1 tsubai * are met:
9 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
10 1.1 tsubai * notice, this list of conditions and the following disclaimer.
11 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
13 1.1 tsubai * documentation and/or other materials provided with the distribution.
14 1.1 tsubai * 3. The name of the author may not be used to endorse or promote products
15 1.1 tsubai * derived from this software without specific prior written permission.
16 1.1 tsubai *
17 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 tsubai */
28 1.8 lukem
29 1.8 lukem #include <sys/cdefs.h>
30 1.8 lukem __KERNEL_RCSID(0, "$NetBSD: ms_hb.c,v 1.8 2003/07/15 02:59:29 lukem Exp $");
31 1.1 tsubai
32 1.1 tsubai #include <sys/param.h>
33 1.1 tsubai #include <sys/device.h>
34 1.1 tsubai #include <sys/systm.h>
35 1.1 tsubai
36 1.1 tsubai #include <dev/wscons/wsconsio.h>
37 1.1 tsubai #include <dev/wscons/wsmousevar.h>
38 1.1 tsubai
39 1.1 tsubai #include <machine/adrsmap.h>
40 1.1 tsubai
41 1.5 tsutsui #include <newsmips/dev/hbvar.h>
42 1.5 tsutsui
43 1.1 tsubai struct msreg {
44 1.1 tsubai u_char ms_data;
45 1.1 tsubai u_char ms_stat;
46 1.1 tsubai u_char ms_reset;
47 1.1 tsubai u_char ms_init;
48 1.1 tsubai };
49 1.1 tsubai
50 1.1 tsubai struct ms_hb_softc {
51 1.1 tsubai struct device sc_dev;
52 1.1 tsubai volatile struct msreg *sc_reg;
53 1.1 tsubai struct device *sc_wsmousedev;
54 1.1 tsubai int sc_ndata;
55 1.1 tsubai u_char sc_buf[3];
56 1.1 tsubai };
57 1.1 tsubai
58 1.1 tsubai int ms_hb_match(struct device *, struct cfdata *, void *);
59 1.1 tsubai void ms_hb_attach(struct device *, struct device *, void *);
60 1.1 tsubai int ms_hb_intr(void *);
61 1.1 tsubai
62 1.1 tsubai int ms_hb_enable(void *);
63 1.1 tsubai int ms_hb_ioctl(void *, u_long, caddr_t, int, struct proc *);
64 1.1 tsubai void ms_hb_disable(void *);
65 1.1 tsubai
66 1.4 thorpej CFATTACH_DECL(ms_hb, sizeof(struct ms_hb_softc),
67 1.4 thorpej ms_hb_match, ms_hb_attach, NULL, NULL);
68 1.1 tsubai
69 1.1 tsubai struct wsmouse_accessops ms_hb_accessops = {
70 1.1 tsubai ms_hb_enable,
71 1.1 tsubai ms_hb_ioctl,
72 1.1 tsubai ms_hb_disable
73 1.1 tsubai };
74 1.1 tsubai
75 1.1 tsubai int
76 1.1 tsubai ms_hb_match(parent, cf, aux)
77 1.1 tsubai struct device *parent;
78 1.1 tsubai struct cfdata *cf;
79 1.1 tsubai void *aux;
80 1.1 tsubai {
81 1.5 tsutsui struct hb_attach_args *ha = aux;
82 1.1 tsubai
83 1.5 tsutsui if (strcmp(ha->ha_name, "ms") == 0)
84 1.1 tsubai return 1;
85 1.1 tsubai
86 1.1 tsubai return 0;
87 1.1 tsubai }
88 1.1 tsubai
89 1.1 tsubai void
90 1.1 tsubai ms_hb_attach(parent, self, aux)
91 1.1 tsubai struct device *parent, *self;
92 1.1 tsubai void *aux;
93 1.1 tsubai {
94 1.1 tsubai struct ms_hb_softc *sc = (void *)self;
95 1.5 tsutsui struct hb_attach_args *ha = aux;
96 1.5 tsutsui volatile struct msreg *reg;
97 1.1 tsubai struct wsmousedev_attach_args aa;
98 1.5 tsutsui int intr;
99 1.5 tsutsui
100 1.6 tsutsui reg = (struct msreg *)ha->ha_addr;
101 1.5 tsutsui intr = ha->ha_level;
102 1.1 tsubai
103 1.1 tsubai if (intr == -1)
104 1.1 tsubai intr = 2;
105 1.1 tsubai
106 1.1 tsubai sc->sc_reg = reg;
107 1.5 tsutsui
108 1.1 tsubai reg->ms_reset = 0x01;
109 1.1 tsubai reg->ms_init = 0x80; /* 1200 bps */
110 1.1 tsubai
111 1.1 tsubai printf(" level %d\n", intr);
112 1.1 tsubai
113 1.7 tsutsui hb_intr_establish(intr, INTEN0_MSINT, IPL_TTY, ms_hb_intr, sc);
114 1.1 tsubai
115 1.1 tsubai aa.accessops = &ms_hb_accessops;
116 1.1 tsubai aa.accesscookie = sc;
117 1.1 tsubai sc->sc_wsmousedev = config_found(self, &aa, wsmousedevprint);
118 1.1 tsubai }
119 1.1 tsubai
120 1.1 tsubai int
121 1.1 tsubai ms_hb_intr(v)
122 1.1 tsubai void *v;
123 1.1 tsubai {
124 1.1 tsubai struct ms_hb_softc *sc = v;
125 1.1 tsubai volatile struct msreg *reg = sc->sc_reg;
126 1.1 tsubai volatile u_char *ien = (void *)INTEN0;
127 1.1 tsubai int code, index, byte0, byte1, byte2;
128 1.1 tsubai int button, dx, dy;
129 1.1 tsubai int rv = 0;
130 1.1 tsubai
131 1.1 tsubai *ien &= ~RX_MSINTE;
132 1.1 tsubai
133 1.1 tsubai while (reg->ms_stat & RX_MSRDY) {
134 1.1 tsubai rv = 1;
135 1.1 tsubai code = reg->ms_data;
136 1.1 tsubai index = sc->sc_ndata;
137 1.1 tsubai
138 1.1 tsubai if (sc->sc_wsmousedev == NULL)
139 1.1 tsubai continue;
140 1.1 tsubai
141 1.1 tsubai if (code & 0x80) {
142 1.1 tsubai sc->sc_buf[0] = code;
143 1.1 tsubai sc->sc_ndata = 1;
144 1.1 tsubai continue;
145 1.1 tsubai }
146 1.1 tsubai
147 1.1 tsubai if (index == 1) {
148 1.1 tsubai sc->sc_buf[1] = code;
149 1.1 tsubai sc->sc_ndata++;
150 1.1 tsubai continue;
151 1.1 tsubai }
152 1.1 tsubai
153 1.1 tsubai if (index == 2) {
154 1.1 tsubai sc->sc_buf[2] = code;
155 1.1 tsubai sc->sc_ndata++;
156 1.1 tsubai
157 1.1 tsubai byte0 = sc->sc_buf[0];
158 1.1 tsubai byte1 = sc->sc_buf[1];
159 1.1 tsubai byte2 = sc->sc_buf[2];
160 1.1 tsubai
161 1.1 tsubai button = 0;
162 1.1 tsubai if (byte0 & 0x01)
163 1.1 tsubai button |= 1;
164 1.1 tsubai if (byte0 & 0x04)
165 1.1 tsubai button |= 2;
166 1.1 tsubai if (byte0 & 0x02)
167 1.1 tsubai button |= 4;
168 1.1 tsubai
169 1.1 tsubai if (byte0 & 0x08)
170 1.1 tsubai dx = byte1 - 0x80;
171 1.1 tsubai else
172 1.1 tsubai dx = byte1;
173 1.1 tsubai if (byte0 & 0x10)
174 1.1 tsubai dy = byte2 - 0x80;
175 1.1 tsubai else
176 1.1 tsubai dy = byte2;
177 1.1 tsubai
178 1.1 tsubai wsmouse_input(sc->sc_wsmousedev, button, dx, -dy, 0,
179 1.1 tsubai WSMOUSE_INPUT_DELTA);
180 1.1 tsubai }
181 1.1 tsubai }
182 1.1 tsubai
183 1.1 tsubai *ien |= RX_MSINTE;
184 1.1 tsubai return rv;
185 1.1 tsubai }
186 1.1 tsubai
187 1.1 tsubai int
188 1.1 tsubai ms_hb_enable(v)
189 1.1 tsubai void *v;
190 1.1 tsubai {
191 1.1 tsubai volatile u_char *ien = (void *)INTEN0;
192 1.1 tsubai
193 1.1 tsubai *ien |= RX_MSINTE;
194 1.1 tsubai return 0;
195 1.1 tsubai }
196 1.1 tsubai
197 1.1 tsubai void
198 1.1 tsubai ms_hb_disable(v)
199 1.1 tsubai void *v;
200 1.1 tsubai {
201 1.1 tsubai volatile u_char *ien = (void *)INTEN0;
202 1.1 tsubai
203 1.1 tsubai *ien &= ~RX_MSINTE;
204 1.1 tsubai }
205 1.1 tsubai
206 1.1 tsubai int
207 1.1 tsubai ms_hb_ioctl(v, cmd, data, flag, p)
208 1.1 tsubai void *v;
209 1.1 tsubai u_long cmd;
210 1.1 tsubai caddr_t data;
211 1.1 tsubai int flag;
212 1.1 tsubai struct proc *p;
213 1.1 tsubai {
214 1.2 atatat return EPASSTHROUGH;
215 1.1 tsubai }
216