summaryrefslogtreecommitdiff
path: root/sys/dev/usb/uintuos.c
blob: 718cf8b2997ae4bf2bbd7ba630701691bbf12c7b (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
391
392
393
394
395
396
397
/*	$NetBSD: uintuos.c,v 1.1 2022/06/30 06:30:44 macallan Exp $	*/

/*
 * Copyright (c) 2019 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Yorick Hardy.
 *
 * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

/*
 *  Wacom Intuos Pen driver.
 *  (partially based on uep.c and ums.c)
 */
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uintuos.c,v 1.1 2022/06/30 06:30:44 macallan Exp $");

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

#include <dev/usb/usb.h>
#include <dev/usb/usbhid.h>

#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/uhidev.h>

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

struct uintuos_softc {
	struct uhidev sc_hdev;

	device_t		sc_wsmousedev;	/* wsmouse device */
	struct tpcalib_softc	sc_tpcalib;	/* calibration */

	u_char sc_enabled;
	u_char sc_dying;
};

Static void uintuos_cth490_intr(struct uhidev *, void *, u_int);
Static void uintuos_ctl6100_intr(struct uhidev *, void *, u_int);

Static int	uintuos_enable(void *);
Static void	uintuos_disable(void *);
Static int	uintuos_ioctl(void *, u_long, void *, int, struct lwp *);

const struct wsmouse_accessops uintuos_accessops = {
	uintuos_enable,
	uintuos_ioctl,
	uintuos_disable,
};

int uintuos_match(device_t, cfdata_t, void *);
void uintuos_attach(device_t, device_t, void *);
void uintuos_childdet(device_t, device_t);
int uintuos_detach(device_t, int);
int uintuos_activate(device_t, enum devact);

CFATTACH_DECL2_NEW(uintuos, sizeof(struct uintuos_softc), uintuos_match, uintuos_attach,
    uintuos_detach, uintuos_activate, NULL, uintuos_childdet);

int
uintuos_match(device_t parent, cfdata_t match, void *aux)
{
	struct uhidev_attach_arg *uha = aux;

	if ((uha->uiaa->uiaa_vendor == USB_VENDOR_WACOM) &&
		(uha->uiaa->uiaa_product == USB_PRODUCT_WACOM_CTH490K0) &&
		(uha->reportid == 16))
		return UMATCH_VENDOR_PRODUCT;

	if ((uha->uiaa->uiaa_vendor == USB_VENDOR_WACOM) &&
		(uha->uiaa->uiaa_product == USB_PRODUCT_WACOM_CTL6100WL) &&
		(uha->reportid == 16))
		return UMATCH_VENDOR_PRODUCT;

	return UMATCH_NONE;
}

void
uintuos_attach(device_t parent, device_t self, void *aux)
{
	struct uintuos_softc *sc = device_private(self);
	struct uhidev_attach_arg *uha = aux;
	struct wsmousedev_attach_args a;
	struct wsmouse_calibcoords default_calib;

	aprint_normal("\n");
	aprint_naive("\n");

	sc->sc_hdev.sc_dev = self;
	sc->sc_hdev.sc_parent = uha->parent;
	sc->sc_hdev.sc_report_id = uha->reportid;

	switch (uha->uiaa->uiaa_product) {
	case USB_PRODUCT_WACOM_CTH490K0:
		default_calib.minx = 0,
		default_calib.miny = 0,
		default_calib.maxx = 7600,
		default_calib.maxy = 4750,
		sc->sc_hdev.sc_intr = uintuos_cth490_intr;
		break;
	case USB_PRODUCT_WACOM_CTL6100WL:
		default_calib.minx = 0,
		default_calib.miny = 0,
		default_calib.maxx = 21600,
		default_calib.maxy = 13471,
		sc->sc_hdev.sc_intr = uintuos_ctl6100_intr;
		break;
	default:
		sc->sc_hdev.sc_intr = uintuos_cth490_intr;
		aprint_error_dev(self, "unsupported product\n");
		break;
	}


	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "couldn't establish power handler\n");

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

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

	default_calib.samplelen = WSMOUSE_CALIBCOORDS_RESET,
	tpcalib_init(&sc->sc_tpcalib);
	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
		(void *)&default_calib, 0, 0);

	return;
}

int
uintuos_detach(device_t self, int flags)
{
	struct uintuos_softc *sc = device_private(self);
	int rv = 0;

	sc->sc_dying = 1;

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

	pmf_device_deregister(self);

	return rv;
}

void
uintuos_childdet(device_t self, device_t child)
{
	struct uintuos_softc *sc = device_private(self);

	KASSERT(sc->sc_wsmousedev == child);
	sc->sc_wsmousedev = NULL;
}

int
uintuos_activate(device_t self, enum devact act)
{
	struct uintuos_softc *sc = device_private(self);

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		return 0;
	default:
		return EOPNOTSUPP;
	}
}

Static int
uintuos_enable(void *v)
{
	struct uintuos_softc *sc = v;
	int error;

	if (sc->sc_dying)
		return EIO;

	if (sc->sc_enabled)
		return EBUSY;

	sc->sc_enabled = 1;

	error = uhidev_open(&sc->sc_hdev);
	if (error)
		sc->sc_enabled = 0;

	return error;
}

Static void
uintuos_disable(void *v)
{
	struct uintuos_softc *sc = v;

	if (!sc->sc_enabled) {
		printf("uintuos_disable: not enabled\n");
		return;
	}

	sc->sc_enabled = 0;
	uhidev_close(&sc->sc_hdev);
}

Static int
uintuos_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{
	struct uintuos_softc *sc = v;
	struct wsmouse_id *id;

	switch (cmd) {
	case WSMOUSEIO_GTYPE:
		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
		return 0;

	case WSMOUSEIO_GETID:
		id = (struct wsmouse_id *)data;
		if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
		 	return EINVAL;

		snprintf(id->data, WSMOUSE_ID_MAXLEN, "%s %s %s",
			sc->sc_hdev.sc_parent->sc_udev->ud_vendor,
			sc->sc_hdev.sc_parent->sc_udev->ud_product,
			sc->sc_hdev.sc_parent->sc_udev->ud_serial);
		id->length = strlen(id->data);
		return 0;

	case WSMOUSEIO_SCALIBCOORDS:
	case WSMOUSEIO_GCALIBCOORDS:
		return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
	}

	return EPASSTHROUGH;
}

void
uintuos_cth490_intr(struct uhidev *addr, void *ibuf, u_int len)
{
	struct uintuos_softc *sc = (struct uintuos_softc *)addr;
	u_char *p = ibuf;
	u_int btns = 0;
	int x = 0, y = 0, z = 0, s;

	if (len != 9) {
		aprint_error_dev(sc->sc_hdev.sc_dev, "wrong report size - ignoring\n");
		return;
	}

	/*
	 * Each report package contains 9 bytes as below (guessed by inspection):
	 *
	 * Byte 0	?VR? ?21T
	 * Byte 1	X coordinate (high byte)
	 * Byte 2	X coordinate (low byte)
	 * Byte 3	Y coordinate (high byte)
	 * Byte 4	Y coordinate (low byte) 
	 * Byte 5	Pressure (high byte)
	 * Byte 6	Pressure (low byte)
	 * Byte 7	zero
	 * Byte 8	quality
	 *
	 * V: 1=valid data, 0=don't use
	 * R: 1=in range, 2=cannot sense
	 * 1: barrel button 1, 1=pressed, 0=not pressed
	 * 2: barrel button 2, 1=pressed, 0=not pressed
	 * T: 1=touched, 0=not touched (unreliable?)
	 * quality: 0 - 255, 255 = most reliable?
	 *
	 */

	/* no valid data or not in range */
	if ((p[0] & 0x40) == 0 || (p[0] & 0x20) == 0)
		return;

	if (sc->sc_wsmousedev != NULL) {
		x = (p[1] << 8) | p[2];
		y = (p[3] << 8) | p[4];
		z = (p[5] << 8) | p[6];

		/*
		 * The "T" bit seems to require a *lot* of pressure to remain "1",
		 * use the pressure value instead (> 255) for button 1
		 *
		 */
		if (p[5] != 0)
			btns |= 1;

		/* barrel button 1 => button 2 */
		if (p[0] & 0x02)
			btns |= 2;

		/* barrel button 2 => button 3 */
		if (p[0] & 0x04)
			btns |= 4;

		tpcalib_trans(&sc->sc_tpcalib, x, y, &x, &y);

		s = spltty();
		wsmouse_input(sc->sc_wsmousedev, btns, x, y, z, 0,
			WSMOUSE_INPUT_ABSOLUTE_X |
			WSMOUSE_INPUT_ABSOLUTE_Y |
			WSMOUSE_INPUT_ABSOLUTE_Z);
		splx(s);
	}
}

void
uintuos_ctl6100_intr(struct uhidev *addr, void *ibuf, u_int len)
{
	struct uintuos_softc *sc = (struct uintuos_softc *)addr;
	u_char *p = ibuf;
	u_int btns = 0;
	int x = 0, y = 0, z = 0, s;

	if (len != 26) {
		aprint_error_dev(sc->sc_hdev.sc_dev, "wrong report size - ignoring\n");
		return;
	}

	/*
	 * Each report package contains 26 bytes as below (guessed by inspection):
	 *
	 * Byte 0	?VR? ?21T
	 * Byte 1	X coordinate (low byte)
	 * Byte 2	X coordinate (high byte)
	 * Byte 3	zero
	 * Byte 4	Y coordinate (low byte)
	 * Byte 5	Y coordinate (high byte) 
	 * Byte 6	zero
	 * Byte 7	Pressure (low byte)
	 * Byte 8	Pressure (high byte)
	 * Byte 9	zero
	 * Byte 10..14	zero
	 * Byte 15	quality?
	 * Byte 16..25	unknown
	 *
	 * V: 1=valid data, 0=don't use
	 * R: 1=in range, 0=cannot sense
	 * 1: barrel button 1, 1=pressed, 0=not pressed
	 * 2: barrel button 2, 1=pressed, 0=not pressed
	 * T: 1=touched, 0=not touched
	 * quality: 0 - 63, 0 = most reliable?
	 *
	 */

	/* no valid data or not in range */
	if ((p[0] & 0x40) == 0 || (p[0] & 0x20) == 0)
		return;

	if (sc->sc_wsmousedev != NULL) {
		x = (p[2] << 8) | p[1];
		y = (p[5] << 8) | p[4];
		z = (p[8] << 8) | p[7];

		btns = p[0] & 0x7;

		tpcalib_trans(&sc->sc_tpcalib, x, y, &x, &y);

		s = spltty();
		wsmouse_input(sc->sc_wsmousedev, btns, x, y, z, 0,
			WSMOUSE_INPUT_ABSOLUTE_X |
			WSMOUSE_INPUT_ABSOLUTE_Y |
			WSMOUSE_INPUT_ABSOLUTE_Z);
		splx(s);
	}
}