summaryrefslogtreecommitdiff
path: root/sys/arch/vax/dec/vsms_ws.c
blob: 1147175bcc565de8dc77b8c3b7255ecda727592d (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*	$OpenBSD: vsms_ws.c,v 1.4 2008/08/26 19:46:23 miod Exp $	*/
/*	$NetBSD: dzms.c,v 1.1 2000/12/02 17:03:55 ragge Exp $	*/

/*
 * Copyright (c) 2008 Miodrag Vallat.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
/*
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This software was developed by the Computer Systems Engineering group
 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
 * contributed to Berkeley.
 *
 * All advertising materials mentioning features or use of this software
 * must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Lawrence Berkeley Laboratory.
 *
 * 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.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
 *
 *	@(#)ms.c	8.1 (Berkeley) 6/11/93
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/tty.h>

#include <machine/bus.h>

#include <vax/qbus/dzreg.h>
#include <vax/qbus/dzvar.h>

#include <vax/dec/dzkbdvar.h>
#include <vax/dec/vsmsvar.h>

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

int	lkms_handle_error(struct lkms_softc *, int);
void	lkms_input_disabled(struct lkms_softc *, int);
void	lkms_input_mouse(struct lkms_softc *, int);
void	lkms_input_tablet(struct lkms_softc *, int);

#define WSMS_BUTTON(x)	(1 << ((x) - 1))

struct	cfdriver lkms_cd = {
	NULL, "lkms", DV_DULL
};

/*
 * Report the device type and status on attachment, and return nonzero
 * if it is not in working state.
 */
int
lkms_handle_error(struct lkms_softc *sc, int mask)
{
	int error = sc->sc_error;

#ifdef DEBUG
	printf("%s: ", sc->dzms_dev.dv_xname);
#endif

	if (ISSET(sc->sc_flags, MS_TABLET)) {
#ifdef DEBUG
		printf("tablet, ");
#endif
		/*
		 * If we are a tablet, the stylet vs puck information
		 * is returned as a non-fatal status code.  Handle
		 * it there so that this does not get in the way.
		 */
		switch (error) {
		case ERROR_TABLET_NO_POINTER:
			/* i can has cheezpuck? */
#ifdef DEBUG
			printf("neither stylus nor puck connected\n");
#else
			printf("%s: neither stylus nor puck connected\n",
			    sc->dzms_dev.dv_xname);
#endif
			error = ERROR_OK;
			break;
		case ERROR_TABLET_STYLUS:
#ifdef DEBUG
			printf("stylus\n");
#endif
			SET(sc->sc_flags, MS_STYLUS);
			error = ERROR_OK;
			break;
		default:
#ifdef DEBUG
			printf("puck\n");
#endif
			break;
		}
	} else {
#ifdef DEBUG
		printf("mouse\n");
#endif
	}

	switch (error) {
	case ERROR_MEMORY_CKSUM_ERROR:
		printf("%s: memory checksum error\n",
		    sc->dzms_dev.dv_xname);
		break;
	case ERROR_BUTTON_ERROR:
	    {
		int btn;

		/*
		 * Print the list of defective parts
		 */
		if (ISSET(sc->sc_flags, MS_TABLET)) {
			if ((mask & FRAME_T_PR) != 0)
				printf("%s: proximity sensor defective\n",
				    sc->dzms_dev.dv_xname);
		} else
			mask <<= 1;

		for (btn = 1; btn < 4; btn++)
			if ((mask & (1 << btn)) != 0)
				printf("%s: button %d held down or defective\n",
				    sc->dzms_dev.dv_xname, btn);
	    }
		break;
	case ERROR_TABLET_LINK:
		/* how vague this error is... */
		printf("%s: analog or digital error\n",
		    sc->dzms_dev.dv_xname);
		break;
	default:
		printf("%s: %sselftest error %02x\n", sc->dzms_dev.dv_xname,
		    error >= ERROR_FATAL ? "fatal " : "", error);
		break;
	case ERROR_OK:
		break;
	}

	if (error >= ERROR_FATAL)
		return ENXIO;

	return 0;
}

int
lkms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
{
	struct lkms_softc *sc = v;
	struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data;

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

	case WSMOUSEIO_GCALIBCOORDS:
		if (ISSET(sc->sc_flags, MS_TABLET)) {
			/*
			 * The tablet has a usable size of 11 inch on each
			 * axis, with a 200dpi resolution.
			 */
			wsmc->minx = 0;
			wsmc->maxx = 200 * 11;
			wsmc->miny = 0;
			wsmc->maxy = 200 * 11;
			wsmc->swapxy = 0;
			wsmc->resx = wsmc->maxx;	/* anything better? */
			wsmc->resy = wsmc->maxy;	/* anything better? */
			wsmc->samplelen = 0;
			return 0;
		} else
			break;
	}

	return -1;
}

int
lkms_input(void *vsc, int data)
{
	struct lkms_softc *sc = vsc;

	if ((data & FRAME_MASK) != 0) {
		sc->sc_frametype = data & FRAME_TYPE_MASK;
		sc->sc_framepos = 0;
	} else
		sc->sc_framepos++;

	if (ISSET(sc->sc_flags, MS_ENABLED) &&
	    !ISSET(sc->sc_flags, MS_SELFTEST)) {
		switch (sc->sc_frametype) {
		case FRAME_MOUSE:
			lkms_input_mouse(sc, data);
			break;
		case FRAME_TABLET:
			lkms_input_tablet(sc, data);
			break;
		}
	} else
		lkms_input_disabled(sc, data);

	return 1;
}

/*
 * Input processing while the device is disabled.  We only are
 * interested in processing self test frames, so as to identify
 * the device and report its state.
 */
void
lkms_input_disabled(struct lkms_softc *sc, int data)
{
	if (!ISSET(sc->sc_flags, MS_SELFTEST))
		return;

	if (sc->sc_frametype == FRAME_SELFTEST) {
		switch (sc->sc_framepos) {
		case 0:
			break;
		case 1:
			data &= FRAME_ST_DEVICE_MASK;
			if (data == FRAME_ST_DEVICE_TABLET)
				SET(sc->sc_flags, MS_TABLET);
			else if (data != FRAME_ST_DEVICE_MOUSE) {
				printf("%s: unrecognized device type %02x\n",
				    sc->dzms_dev.dv_xname, data);
				goto fail;
			}
			break;
		case 2:
			sc->sc_error = data;
			break;
		case 3:
			if (lkms_handle_error(sc, data) != 0)
				goto fail;

			CLR(sc->sc_flags, MS_SELFTEST);
			goto success;
			break;
		}

		return;
	} /* else goto fail; */

fail:
	/*
	 * Our self test frame has been truncated, or we have received
	 * incorrect data (both could be a cable problem), or the
	 * selftest reported an error.  The device is unusable.
	 */
	CLR(sc->sc_flags, MS_TABLET | MS_STYLUS);

success:
	sc->sc_frametype = 0;
	wakeup(&sc->sc_flags);
}

/*
 * Input processing while the device is enabled, for mouse frames.
 */
void
lkms_input_mouse(struct lkms_softc *sc, int data)
{
	switch (sc->sc_framepos) {
	case 0:
		sc->buttons = 0;
		/* button order is inverted from wscons */
		if ((data & FRAME_MS_B3) != 0)
			sc->buttons |= WSMS_BUTTON(1);
		if ((data & FRAME_MS_B2) != 0)
			sc->buttons |= WSMS_BUTTON(2);
		if ((data & FRAME_MS_B1) != 0)
			sc->buttons |= WSMS_BUTTON(3);

		sc->dx = data & FRAME_MS_X_SIGN;
		sc->dy = data & FRAME_MS_Y_SIGN;
		break;
	case 1:
		if (sc->dx == 0)
			sc->dx = -data;
		else
			sc->dx = data;
		break;
	case 2:
		if (sc->dy == 0)
			sc->dy = -data;
		else
			sc->dy = data;
		wsmouse_input(sc->sc_wsmousedev, sc->buttons,
		    sc->dx, sc->dy, 0, 0, WSMOUSE_INPUT_DELTA);

		sc->sc_frametype = 0;
		break;
	}
}

/*
 * Input processing while the device is enabled, for tablet frames.
 */
void
lkms_input_tablet(struct lkms_softc *sc, int data)
{
	switch (sc->sc_framepos) {
	case 0:
		/*
		 * Button information will depend on the type of positional
		 * device:
		 * - puck buttons get reported as is, as a 4 button mouse.
		 *   Button order is opposite from mouse.
		 * - stylus barrel gets reported as left button, while tip
		 *   gets reported as right button.
		 *   Proximity sensor gets reported as a fictitious fifth
		 *   button.
		 */
		sc->buttons = 0;
		if ((data & FRAME_T_B1) != 0)
			sc->buttons |= WSMS_BUTTON(1);
		if ((data & FRAME_T_B2) != 0) {
			if (ISSET(sc->sc_flags, MS_STYLUS))
				sc->buttons |= WSMS_BUTTON(3);
			else
				sc->buttons |= WSMS_BUTTON(2);
		}
		if ((data & FRAME_T_B3) != 0)
			sc->buttons |= WSMS_BUTTON(3);
		if ((data & FRAME_T_B4) != 0)
			sc->buttons |= WSMS_BUTTON(4);
		if ((data & FRAME_T_PR) == 0)
			sc->buttons |= WSMS_BUTTON(5);
		break;
	case 1:
		sc->dx = data & 0x3f;
		break;
	case 2:
		sc->dx |= (data & 0x3f) << 6;
		break;
	case 3:
		sc->dy = data & 0x3f;
		break;
	case 4:
		sc->dy |= (data & 0x3f) << 6;
		wsmouse_input(sc->sc_wsmousedev, sc->buttons,
		    sc->dx, sc->dy, 0, 0,
		    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);

		sc->sc_frametype = 0;
		break;
	}
}