summaryrefslogtreecommitdiff
path: root/sys/dev/hil/hilms.c
blob: c13d1bd4536e7239b18db55e34219587d4f1dd6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*	$OpenBSD: hilms.c,v 1.7 2022/04/06 18:59:28 naddy Exp $	*/
/*
 * Copyright (c) 2003, Miodrag Vallat.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/ioctl.h>

#include <machine/autoconf.h>
#include <machine/bus.h>
#include <machine/cpu.h>

#include <dev/hil/hilreg.h>
#include <dev/hil/hilvar.h>
#include <dev/hil/hildevs.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>

struct hilms_softc {
	struct hildev_softc sc_hildev;

	int		sc_features;
	u_int		sc_buttons;
	u_int		sc_axes;
	int		sc_enabled;
	int		sc_buttonstate;

	struct device	*sc_wsmousedev;
};

int	hilmsprobe(struct device *, void *, void *);
void	hilmsattach(struct device *, struct device *, void *);
int	hilmsdetach(struct device *, int);

struct cfdriver hilms_cd = {
	NULL, "hilms", DV_DULL
};

const struct cfattach hilms_ca = {
	sizeof(struct hilms_softc), hilmsprobe, hilmsattach, hilmsdetach,
};

int	hilms_enable(void *);
int	hilms_ioctl(void *, u_long, caddr_t, int, struct proc *);
void	hilms_disable(void *);

const struct wsmouse_accessops hilms_accessops = {
	hilms_enable,
	hilms_ioctl,
	hilms_disable,
};

void	hilms_callback(struct hildev_softc *, u_int, u_int8_t *);

int
hilmsprobe(struct device *parent, void *match, void *aux)
{
	struct hil_attach_args *ha = aux;

	if (ha->ha_type != HIL_DEVICE_MOUSE)
		return (0);

	/*
	 * Reject anything that has only buttons - they are handled as
	 * keyboards, really.
	 */
	if (ha->ha_infolen > 1 && (ha->ha_info[1] & HIL_AXMASK) == 0)
		return (0);

	return (1);
}

void
hilmsattach(struct device *parent, struct device *self, void *aux)
{
	struct hilms_softc *sc = (void *)self;
	struct hil_attach_args *ha = aux;
	struct wsmousedev_attach_args a;
	int iob, rx, ry;

	sc->hd_code = ha->ha_code;
	sc->hd_type = ha->ha_type;
	sc->hd_infolen = ha->ha_infolen;
	bcopy(ha->ha_info, sc->hd_info, ha->ha_infolen);
	sc->hd_fn = hilms_callback;

	/*
	 * Interpret the identification bytes, if any
	 */
	rx = ry = 0;
	if (ha->ha_infolen > 1) {
		sc->sc_features = ha->ha_info[1];
		sc->sc_axes = sc->sc_features & HIL_AXMASK;

		if (sc->sc_features & HIL_IOB) {
			/* skip resolution bytes */
			iob = 4;
			if (sc->sc_features & HIL_ABSOLUTE) {
				/* skip ranges */
				rx = ha->ha_info[4] | (ha->ha_info[5] << 8);
				if (sc->sc_axes > 1)
					ry = ha->ha_info[6] |
					    (ha->ha_info[7] << 8);
				iob += 2 * sc->sc_axes;
			}

			if (iob >= ha->ha_infolen) {
				sc->sc_features &= ~(HIL_IOB | HILIOB_PIO);
			} else {
				iob = ha->ha_info[iob];
				sc->sc_buttons = iob & HILIOB_BMASK;
				sc->sc_features |= (iob & HILIOB_PIO);
			}
		}
	}

	printf(", %d axes", sc->sc_axes);
	if (sc->sc_buttons == 1)
		printf(", 1 button");
	else if (sc->sc_buttons > 1)
		printf(", %d buttons", sc->sc_buttons);
	if (sc->sc_features & HILIOB_PIO)
		printf(", pressure sensor");
	if (sc->sc_features & HIL_ABSOLUTE) {
		printf ("\n%s: %d", self->dv_xname, rx);
		if (ry != 0)
			printf("x%d", ry);
		else
			printf(" linear");
		printf(" fixed area");
	}

	printf("\n");

	sc->sc_enabled = 0;

	a.accessops = &hilms_accessops;
	a.accesscookie = sc;

	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
}

int
hilmsdetach(struct device *self, int flags)
{
	struct hilms_softc *sc = (void *)self;

	if (sc->sc_wsmousedev != NULL)
		return config_detach(sc->sc_wsmousedev, flags);

	return (0);
}

int
hilms_enable(void *v)
{
	struct hilms_softc *sc = v;

	if (sc->sc_enabled)
		return EBUSY;

	sc->sc_enabled = 1;
	sc->sc_buttonstate = 0;

	return (0);
}

void
hilms_disable(void *v)
{
	struct hilms_softc *sc = v;

	sc->sc_enabled = 0;
}

int
hilms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
{
#if 0
	struct hilms_softc *sc = v;
#endif

	switch (cmd) {
	case WSMOUSEIO_GTYPE:
		*(int *)data = WSMOUSE_TYPE_HIL;
		return 0;
	}

	return -1;
}

void
hilms_callback(struct hildev_softc *dev, u_int buflen, u_int8_t *buf)
{
	struct hilms_softc *sc = (struct hilms_softc *)dev;
	int type;
	int dx, dy, dz, button;
#ifdef DIAGNOSTIC
	int minlen;
#endif

	/*
	 * Ignore packet if we don't need it
	 */
	if (sc->sc_enabled == 0)
		return;

	type = *buf++;

#ifdef DIAGNOSTIC
	/*
	 * Check that the packet contains all the expected data,
	 * ignore it if too short.
	 */
	minlen = 1;
	if (type & HIL_MOUSEMOTION) {
		minlen += sc->sc_axes <<
		    (sc->sc_features & HIL_16_BITS) ? 1 : 0;
	}
	if (type & HIL_MOUSEBUTTON)
		minlen++;

	if (minlen > buflen)
		return;
#endif

	/*
	 * The packet can contain both a mouse motion and a button event.
	 * In this case, the motion data comes first.
	 */

	if (type & HIL_MOUSEMOTION) {
		if (sc->sc_features & HIL_16_BITS) {
			dx = *buf++;
			dx |= (*buf++) << 8;
			if (!(sc->sc_features & HIL_ABSOLUTE))
				dx = (int16_t)dx;
		} else {
			dx = *buf++;
			if (!(sc->sc_features & HIL_ABSOLUTE))
				dx = (int8_t)dx;
		}
		if (sc->sc_axes > 1) {
			if (sc->sc_features & HIL_16_BITS) {
				dy = *buf++;
				dy |= (*buf++) << 8;
				if (!(sc->sc_features & HIL_ABSOLUTE))
					dy = (int16_t)dy;
			} else {
				dy = *buf++;
				if (!(sc->sc_features & HIL_ABSOLUTE))
					dy = (int8_t)dy;
			}
			if (sc->sc_axes > 2) {
				if (sc->sc_features & HIL_16_BITS) {
					dz = *buf++;
					dz |= (*buf++) << 8;
					if (!(sc->sc_features & HIL_ABSOLUTE))
						dz = (int16_t)dz;
				} else {
					dz = *buf++;
					if (!(sc->sc_features & HIL_ABSOLUTE))
						dz = (int8_t)dz;
				}
			} else
				dz = 0;
		} else
			dy = dz = 0;

		/*
		 * Correct Y direction for button boxes.
		 */
		if ((sc->sc_features & HIL_ABSOLUTE) == 0 &&
		    sc->sc_buttons == 0)
			dy = -dy;
	}

	if (type & HIL_MOUSEBUTTON) {
		button = *buf;
		/*
		 * The pressure sensor is very primitive and only has
		 * a boolean behaviour, as an extra mouse button, which is
		 * down if there is pressure or the pen is near the tablet,
		 * and up if there is no pressure or the pen is far from the
		 * tablet - at least for Tablet id 0x94, P/N 46088B
		 *
		 * The corresponding codes are 0x8f and 0x8e. Convert them
		 * to a pseudo fourth button - even if the tablet never
		 * has three buttons.
		 */
		button = (button - 0x80) >> 1;
		if (button > 4)
			button = 4;

		if (*buf & 1) {
			/* Button released, or no pressure */
			sc->sc_buttonstate &= ~(1 << button);
		} else {
			/* Button pressed, or pressure */
			sc->sc_buttonstate |= (1 << button);
		}
		/* buf++; */
	}
	
	if (sc->sc_wsmousedev == NULL)
		return;

	wsmouse_buttons(sc->sc_wsmousedev, sc->sc_buttonstate);
	if (type & HIL_MOUSEMOTION) {
		if ((sc->sc_features & HIL_ABSOLUTE) == 0) {
			wsmouse_motion(sc->sc_wsmousedev, dx, dy, dz, 0);
		} else {
			wsmouse_position(sc->sc_wsmousedev, dx, dy);
			if (sc->sc_axes > 2)
				wsmouse_touch(sc->sc_wsmousedev, dz, 0);
		}
	}
	wsmouse_input_sync(sc->sc_wsmousedev);
}