hilms.c revision 1.2.8.2 1 1.2.8.2 jruoho /* $NetBSD: hilms.c,v 1.2.8.2 2011/06/06 09:07:49 jruoho Exp $ */
2 1.2.8.2 jruoho /* $OpenBSD: hilms.c,v 1.5 2007/04/10 22:37:17 miod Exp $ */
3 1.2.8.2 jruoho /*
4 1.2.8.2 jruoho * Copyright (c) 2003, Miodrag Vallat.
5 1.2.8.2 jruoho * All rights reserved.
6 1.2.8.2 jruoho *
7 1.2.8.2 jruoho * Redistribution and use in source and binary forms, with or without
8 1.2.8.2 jruoho * modification, are permitted provided that the following conditions
9 1.2.8.2 jruoho * are met:
10 1.2.8.2 jruoho * 1. Redistributions of source code must retain the above copyright
11 1.2.8.2 jruoho * notice, this list of conditions and the following disclaimer.
12 1.2.8.2 jruoho * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.8.2 jruoho * notice, this list of conditions and the following disclaimer in the
14 1.2.8.2 jruoho * documentation and/or other materials provided with the distribution.
15 1.2.8.2 jruoho *
16 1.2.8.2 jruoho * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.2.8.2 jruoho * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.2.8.2 jruoho * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 1.2.8.2 jruoho * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 1.2.8.2 jruoho * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 1.2.8.2 jruoho * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 1.2.8.2 jruoho * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.8.2 jruoho * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 1.2.8.2 jruoho * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 1.2.8.2 jruoho * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.8.2 jruoho * POSSIBILITY OF SUCH DAMAGE.
27 1.2.8.2 jruoho *
28 1.2.8.2 jruoho */
29 1.2.8.2 jruoho
30 1.2.8.2 jruoho #include <sys/param.h>
31 1.2.8.2 jruoho #include <sys/systm.h>
32 1.2.8.2 jruoho #include <sys/device.h>
33 1.2.8.2 jruoho #include <sys/ioctl.h>
34 1.2.8.2 jruoho #include <sys/bus.h>
35 1.2.8.2 jruoho #include <sys/cpu.h>
36 1.2.8.2 jruoho
37 1.2.8.2 jruoho #include <machine/autoconf.h>
38 1.2.8.2 jruoho
39 1.2.8.2 jruoho #include <dev/hil/hilreg.h>
40 1.2.8.2 jruoho #include <dev/hil/hilvar.h>
41 1.2.8.2 jruoho #include <dev/hil/hildevs.h>
42 1.2.8.2 jruoho
43 1.2.8.2 jruoho #include <dev/wscons/wsconsio.h>
44 1.2.8.2 jruoho #include <dev/wscons/wsmousevar.h>
45 1.2.8.2 jruoho
46 1.2.8.2 jruoho struct hilms_softc {
47 1.2.8.2 jruoho struct hildev_softc sc_hildev;
48 1.2.8.2 jruoho
49 1.2.8.2 jruoho int sc_features;
50 1.2.8.2 jruoho u_int sc_buttons;
51 1.2.8.2 jruoho u_int sc_axes;
52 1.2.8.2 jruoho int sc_enabled;
53 1.2.8.2 jruoho int sc_buttonstate;
54 1.2.8.2 jruoho
55 1.2.8.2 jruoho device_t sc_wsmousedev;
56 1.2.8.2 jruoho };
57 1.2.8.2 jruoho
58 1.2.8.2 jruoho static int hilmsprobe(device_t, cfdata_t, void *);
59 1.2.8.2 jruoho static void hilmsattach(device_t, device_t, void *);
60 1.2.8.2 jruoho static int hilmsdetach(device_t, int);
61 1.2.8.2 jruoho
62 1.2.8.2 jruoho CFATTACH_DECL_NEW(hilms, sizeof(struct hilms_softc),
63 1.2.8.2 jruoho hilmsprobe, hilmsattach, hilmsdetach, NULL);
64 1.2.8.2 jruoho
65 1.2.8.2 jruoho static int hilms_enable(void *);
66 1.2.8.2 jruoho static int hilms_ioctl(void *, u_long, void *, int, struct lwp *);
67 1.2.8.2 jruoho static void hilms_disable(void *);
68 1.2.8.2 jruoho
69 1.2.8.2 jruoho static const struct wsmouse_accessops hilms_accessops = {
70 1.2.8.2 jruoho hilms_enable,
71 1.2.8.2 jruoho hilms_ioctl,
72 1.2.8.2 jruoho hilms_disable,
73 1.2.8.2 jruoho };
74 1.2.8.2 jruoho
75 1.2.8.2 jruoho static void hilms_callback(struct hildev_softc *, u_int, uint8_t *);
76 1.2.8.2 jruoho
77 1.2.8.2 jruoho int
78 1.2.8.2 jruoho hilmsprobe(device_t parent, cfdata_t cf, void *aux)
79 1.2.8.2 jruoho {
80 1.2.8.2 jruoho struct hil_attach_args *ha = aux;
81 1.2.8.2 jruoho
82 1.2.8.2 jruoho if (ha->ha_type != HIL_DEVICE_MOUSE)
83 1.2.8.2 jruoho return 0;
84 1.2.8.2 jruoho
85 1.2.8.2 jruoho /*
86 1.2.8.2 jruoho * Reject anything that has only buttons - they are handled as
87 1.2.8.2 jruoho * keyboards, really.
88 1.2.8.2 jruoho */
89 1.2.8.2 jruoho if (ha->ha_infolen > 1 && (ha->ha_info[1] & HIL_AXMASK) == 0)
90 1.2.8.2 jruoho return 0;
91 1.2.8.2 jruoho
92 1.2.8.2 jruoho return 1;
93 1.2.8.2 jruoho }
94 1.2.8.2 jruoho
95 1.2.8.2 jruoho void
96 1.2.8.2 jruoho hilmsattach(device_t parent, device_t self, void *aux)
97 1.2.8.2 jruoho {
98 1.2.8.2 jruoho struct hilms_softc *sc = device_private(self);
99 1.2.8.2 jruoho struct hil_attach_args *ha = aux;
100 1.2.8.2 jruoho struct wsmousedev_attach_args a;
101 1.2.8.2 jruoho int iob, rx, ry;
102 1.2.8.2 jruoho
103 1.2.8.2 jruoho sc->sc_hildev.sc_dev = self;
104 1.2.8.2 jruoho sc->hd_code = ha->ha_code;
105 1.2.8.2 jruoho sc->hd_type = ha->ha_type;
106 1.2.8.2 jruoho sc->hd_infolen = ha->ha_infolen;
107 1.2.8.2 jruoho memcpy(sc->hd_info, ha->ha_info, ha->ha_infolen);
108 1.2.8.2 jruoho sc->hd_fn = hilms_callback;
109 1.2.8.2 jruoho
110 1.2.8.2 jruoho /*
111 1.2.8.2 jruoho * Interpret the identification bytes, if any
112 1.2.8.2 jruoho */
113 1.2.8.2 jruoho rx = ry = 0;
114 1.2.8.2 jruoho if (ha->ha_infolen > 1) {
115 1.2.8.2 jruoho sc->sc_features = ha->ha_info[1];
116 1.2.8.2 jruoho sc->sc_axes = sc->sc_features & HIL_AXMASK;
117 1.2.8.2 jruoho
118 1.2.8.2 jruoho if (sc->sc_features & HIL_IOB) {
119 1.2.8.2 jruoho /* skip resolution bytes */
120 1.2.8.2 jruoho iob = 4;
121 1.2.8.2 jruoho if (sc->sc_features & HIL_ABSOLUTE) {
122 1.2.8.2 jruoho /* skip ranges */
123 1.2.8.2 jruoho rx = ha->ha_info[4] | (ha->ha_info[5] << 8);
124 1.2.8.2 jruoho if (sc->sc_axes > 1)
125 1.2.8.2 jruoho ry = ha->ha_info[6] |
126 1.2.8.2 jruoho (ha->ha_info[7] << 8);
127 1.2.8.2 jruoho iob += 2 * sc->sc_axes;
128 1.2.8.2 jruoho }
129 1.2.8.2 jruoho
130 1.2.8.2 jruoho if (iob >= ha->ha_infolen) {
131 1.2.8.2 jruoho sc->sc_features &= ~(HIL_IOB | HILIOB_PIO);
132 1.2.8.2 jruoho } else {
133 1.2.8.2 jruoho iob = ha->ha_info[iob];
134 1.2.8.2 jruoho sc->sc_buttons = iob & HILIOB_BMASK;
135 1.2.8.2 jruoho sc->sc_features |= (iob & HILIOB_PIO);
136 1.2.8.2 jruoho }
137 1.2.8.2 jruoho }
138 1.2.8.2 jruoho }
139 1.2.8.2 jruoho
140 1.2.8.2 jruoho aprint_normal(", %d axes", sc->sc_axes);
141 1.2.8.2 jruoho if (sc->sc_buttons == 1)
142 1.2.8.2 jruoho aprint_normal(", 1 button");
143 1.2.8.2 jruoho else if (sc->sc_buttons > 1)
144 1.2.8.2 jruoho aprint_normal(", %d buttons", sc->sc_buttons);
145 1.2.8.2 jruoho if (sc->sc_features & HILIOB_PIO)
146 1.2.8.2 jruoho aprint_normal(", pressure sensor");
147 1.2.8.2 jruoho if (sc->sc_features & HIL_ABSOLUTE) {
148 1.2.8.2 jruoho aprint_normal("\n");
149 1.2.8.2 jruoho aprint_normal_dev(self, "%d", rx);
150 1.2.8.2 jruoho if (ry != 0)
151 1.2.8.2 jruoho aprint_normal("x%d", ry);
152 1.2.8.2 jruoho else
153 1.2.8.2 jruoho aprint_normal(" linear");
154 1.2.8.2 jruoho aprint_normal(" fixed area");
155 1.2.8.2 jruoho }
156 1.2.8.2 jruoho
157 1.2.8.2 jruoho aprint_normal("\n");
158 1.2.8.2 jruoho
159 1.2.8.2 jruoho sc->sc_enabled = 0;
160 1.2.8.2 jruoho
161 1.2.8.2 jruoho a.accessops = &hilms_accessops;
162 1.2.8.2 jruoho a.accesscookie = sc;
163 1.2.8.2 jruoho
164 1.2.8.2 jruoho sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
165 1.2.8.2 jruoho }
166 1.2.8.2 jruoho
167 1.2.8.2 jruoho int
168 1.2.8.2 jruoho hilmsdetach(device_t self, int flags)
169 1.2.8.2 jruoho {
170 1.2.8.2 jruoho struct hilms_softc *sc = device_private(self);
171 1.2.8.2 jruoho
172 1.2.8.2 jruoho if (sc->sc_wsmousedev != NULL)
173 1.2.8.2 jruoho return config_detach(sc->sc_wsmousedev, flags);
174 1.2.8.2 jruoho
175 1.2.8.2 jruoho return 0;
176 1.2.8.2 jruoho }
177 1.2.8.2 jruoho
178 1.2.8.2 jruoho int
179 1.2.8.2 jruoho hilms_enable(void *v)
180 1.2.8.2 jruoho {
181 1.2.8.2 jruoho struct hilms_softc *sc = v;
182 1.2.8.2 jruoho
183 1.2.8.2 jruoho if (sc->sc_enabled)
184 1.2.8.2 jruoho return EBUSY;
185 1.2.8.2 jruoho
186 1.2.8.2 jruoho sc->sc_enabled = 1;
187 1.2.8.2 jruoho sc->sc_buttonstate = 0;
188 1.2.8.2 jruoho
189 1.2.8.2 jruoho return 0;
190 1.2.8.2 jruoho }
191 1.2.8.2 jruoho
192 1.2.8.2 jruoho void
193 1.2.8.2 jruoho hilms_disable(void *v)
194 1.2.8.2 jruoho {
195 1.2.8.2 jruoho struct hilms_softc *sc = v;
196 1.2.8.2 jruoho
197 1.2.8.2 jruoho sc->sc_enabled = 0;
198 1.2.8.2 jruoho }
199 1.2.8.2 jruoho
200 1.2.8.2 jruoho int
201 1.2.8.2 jruoho hilms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
202 1.2.8.2 jruoho {
203 1.2.8.2 jruoho #if 0
204 1.2.8.2 jruoho struct hilms_softc *sc = v;
205 1.2.8.2 jruoho #endif
206 1.2.8.2 jruoho
207 1.2.8.2 jruoho switch (cmd) {
208 1.2.8.2 jruoho case WSMOUSEIO_GTYPE:
209 1.2.8.2 jruoho *(int *)data = WSMOUSE_TYPE_HIL;
210 1.2.8.2 jruoho return 0;
211 1.2.8.2 jruoho }
212 1.2.8.2 jruoho
213 1.2.8.2 jruoho return EPASSTHROUGH;
214 1.2.8.2 jruoho }
215 1.2.8.2 jruoho
216 1.2.8.2 jruoho void
217 1.2.8.2 jruoho hilms_callback(struct hildev_softc *hdsc, u_int buflen, uint8_t *buf)
218 1.2.8.2 jruoho {
219 1.2.8.2 jruoho struct hilms_softc *sc = device_private(hdsc->sc_dev);
220 1.2.8.2 jruoho int type, flags;
221 1.2.8.2 jruoho int dx, dy, dz, button;
222 1.2.8.2 jruoho #ifdef DIAGNOSTIC
223 1.2.8.2 jruoho int minlen;
224 1.2.8.2 jruoho #endif
225 1.2.8.2 jruoho
226 1.2.8.2 jruoho /*
227 1.2.8.2 jruoho * Ignore packet if we don't need it
228 1.2.8.2 jruoho */
229 1.2.8.2 jruoho if (sc->sc_enabled == 0)
230 1.2.8.2 jruoho return;
231 1.2.8.2 jruoho
232 1.2.8.2 jruoho type = *buf++;
233 1.2.8.2 jruoho
234 1.2.8.2 jruoho #ifdef DIAGNOSTIC
235 1.2.8.2 jruoho /*
236 1.2.8.2 jruoho * Check that the packet contains all the expected data,
237 1.2.8.2 jruoho * ignore it if too short.
238 1.2.8.2 jruoho */
239 1.2.8.2 jruoho minlen = 1;
240 1.2.8.2 jruoho if (type & HIL_MOUSEMOTION) {
241 1.2.8.2 jruoho minlen += sc->sc_axes <<
242 1.2.8.2 jruoho (sc->sc_features & HIL_16_BITS) ? 1 : 0;
243 1.2.8.2 jruoho }
244 1.2.8.2 jruoho if (type & HIL_MOUSEBUTTON)
245 1.2.8.2 jruoho minlen++;
246 1.2.8.2 jruoho
247 1.2.8.2 jruoho if (minlen > buflen)
248 1.2.8.2 jruoho return;
249 1.2.8.2 jruoho #endif
250 1.2.8.2 jruoho
251 1.2.8.2 jruoho /*
252 1.2.8.2 jruoho * The packet can contain both a mouse motion and a button event.
253 1.2.8.2 jruoho * In this case, the motion data comes first.
254 1.2.8.2 jruoho */
255 1.2.8.2 jruoho
256 1.2.8.2 jruoho if (type & HIL_MOUSEMOTION) {
257 1.2.8.2 jruoho flags = sc->sc_features & HIL_ABSOLUTE ?
258 1.2.8.2 jruoho WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y |
259 1.2.8.2 jruoho WSMOUSE_INPUT_ABSOLUTE_Z : WSMOUSE_INPUT_DELTA;
260 1.2.8.2 jruoho if (sc->sc_features & HIL_16_BITS) {
261 1.2.8.2 jruoho dx = *buf++;
262 1.2.8.2 jruoho dx |= (*buf++) << 8;
263 1.2.8.2 jruoho if (!(sc->sc_features & HIL_ABSOLUTE))
264 1.2.8.2 jruoho dx = (int16_t)dx;
265 1.2.8.2 jruoho } else {
266 1.2.8.2 jruoho dx = *buf++;
267 1.2.8.2 jruoho if (!(sc->sc_features & HIL_ABSOLUTE))
268 1.2.8.2 jruoho dx = (int8_t)dx;
269 1.2.8.2 jruoho }
270 1.2.8.2 jruoho if (sc->sc_axes > 1) {
271 1.2.8.2 jruoho if (sc->sc_features & HIL_16_BITS) {
272 1.2.8.2 jruoho dy = *buf++;
273 1.2.8.2 jruoho dy |= (*buf++) << 8;
274 1.2.8.2 jruoho if (!(sc->sc_features & HIL_ABSOLUTE))
275 1.2.8.2 jruoho dy = (int16_t)dy;
276 1.2.8.2 jruoho } else {
277 1.2.8.2 jruoho dy = *buf++;
278 1.2.8.2 jruoho if (!(sc->sc_features & HIL_ABSOLUTE))
279 1.2.8.2 jruoho dy = (int8_t)dy;
280 1.2.8.2 jruoho }
281 1.2.8.2 jruoho if (sc->sc_axes > 2) {
282 1.2.8.2 jruoho if (sc->sc_features & HIL_16_BITS) {
283 1.2.8.2 jruoho dz = *buf++;
284 1.2.8.2 jruoho dz |= (*buf++) << 8;
285 1.2.8.2 jruoho if (!(sc->sc_features & HIL_ABSOLUTE))
286 1.2.8.2 jruoho dz = (int16_t)dz;
287 1.2.8.2 jruoho } else {
288 1.2.8.2 jruoho dz = *buf++;
289 1.2.8.2 jruoho if (!(sc->sc_features & HIL_ABSOLUTE))
290 1.2.8.2 jruoho dz = (int8_t)dz;
291 1.2.8.2 jruoho }
292 1.2.8.2 jruoho } else
293 1.2.8.2 jruoho dz = 0;
294 1.2.8.2 jruoho } else
295 1.2.8.2 jruoho dy = dz = 0;
296 1.2.8.2 jruoho
297 1.2.8.2 jruoho /*
298 1.2.8.2 jruoho * Correct Y direction for button boxes.
299 1.2.8.2 jruoho */
300 1.2.8.2 jruoho if ((sc->sc_features & HIL_ABSOLUTE) == 0 &&
301 1.2.8.2 jruoho sc->sc_buttons == 0)
302 1.2.8.2 jruoho dy = -dy;
303 1.2.8.2 jruoho } else
304 1.2.8.2 jruoho dx = dy = dz = flags = 0;
305 1.2.8.2 jruoho
306 1.2.8.2 jruoho if (type & HIL_MOUSEBUTTON) {
307 1.2.8.2 jruoho button = *buf;
308 1.2.8.2 jruoho /*
309 1.2.8.2 jruoho * The pressure sensor is very primitive and only has
310 1.2.8.2 jruoho * a boolean behaviour, as an extra mouse button, which is
311 1.2.8.2 jruoho * down if there is pressure or the pen is near the tablet,
312 1.2.8.2 jruoho * and up if there is no pressure or the pen is far from the
313 1.2.8.2 jruoho * tablet - at least for Tablet id 0x94, P/N 46088B
314 1.2.8.2 jruoho *
315 1.2.8.2 jruoho * The corresponding codes are 0x8f and 0x8e. Convert them
316 1.2.8.2 jruoho * to a pseudo fourth button - even if the tablet never
317 1.2.8.2 jruoho * has three buttons.
318 1.2.8.2 jruoho */
319 1.2.8.2 jruoho button = (button - 0x80) >> 1;
320 1.2.8.2 jruoho if (button > 4)
321 1.2.8.2 jruoho button = 4;
322 1.2.8.2 jruoho
323 1.2.8.2 jruoho if (*buf & 1) {
324 1.2.8.2 jruoho /* Button released, or no pressure */
325 1.2.8.2 jruoho sc->sc_buttonstate &= ~(1 << button);
326 1.2.8.2 jruoho } else {
327 1.2.8.2 jruoho /* Button pressed, or pressure */
328 1.2.8.2 jruoho sc->sc_buttonstate |= (1 << button);
329 1.2.8.2 jruoho }
330 1.2.8.2 jruoho /* buf++; */
331 1.2.8.2 jruoho }
332 1.2.8.2 jruoho
333 1.2.8.2 jruoho if (sc->sc_wsmousedev != NULL)
334 1.2.8.2 jruoho wsmouse_input(sc->sc_wsmousedev,
335 1.2.8.2 jruoho sc->sc_buttonstate, dx, dy, dz, 0, flags);
336 1.2.8.2 jruoho }
337