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