xref: /netbsd-src/sys/dev/usb/uts.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /*	$NetBSD: uts.c,v 1.3 2013/01/05 23:34:21 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Pierre Pronchery (khorben@defora.org).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  *  USB generic Touch Screen driver.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: uts.c,v 1.3 2013/01/05 23:34:21 christos Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/device.h>
44 #include <sys/ioctl.h>
45 #include <sys/vnode.h>
46 
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbhid.h>
49 
50 #include <dev/usb/usbdi.h>
51 #include <dev/usb/usbdi_util.h>
52 #include <dev/usb/usbdevs.h>
53 #include <dev/usb/usb_quirks.h>
54 #include <dev/usb/uhidev.h>
55 #include <dev/usb/hid.h>
56 
57 #include <dev/wscons/wsconsio.h>
58 #include <dev/wscons/wsmousevar.h>
59 #include <dev/wscons/tpcalibvar.h>
60 
61 #ifdef UTS_DEBUG
62 #define DPRINTF(x)	if (utsdebug) printf x
63 #define DPRINTFN(n,x)	if (utsdebug>(n)) printf x
64 int	utsdebug = 0;
65 #else
66 #define DPRINTF(x)
67 #define DPRINTFN(n,x)
68 #endif
69 
70 
71 struct uts_softc {
72 	struct uhidev sc_hdev;
73 
74 	struct hid_location sc_loc_x, sc_loc_y, sc_loc_z;
75 	struct hid_location sc_loc_btn;
76 
77 	int sc_enabled;
78 
79 	int flags;		/* device configuration */
80 #define UTS_ABS		0x1	/* absolute position */
81 
82 	u_int32_t		sc_buttons;	/* touchscreen button status */
83 	device_t		sc_wsmousedev;
84 	struct tpcalib_softc	sc_tpcalib;	/* calibration */
85 	struct wsmouse_calibcoords sc_calibcoords;
86 
87 	char sc_dying;
88 };
89 
90 #define TSCREEN_FLAGS_MASK (HIO_CONST|HIO_RELATIVE)
91 
92 Static void	uts_intr(struct uhidev *addr, void *ibuf, u_int len);
93 
94 Static int	uts_enable(void *);
95 Static void	uts_disable(void *);
96 Static int	uts_ioctl(void *, u_long, void *, int, struct lwp *);
97 
98 Static const struct wsmouse_accessops uts_accessops = {
99 	uts_enable,
100 	uts_ioctl,
101 	uts_disable,
102 };
103 
104 Static int	uts_match(device_t, cfdata_t, void *);
105 Static void	uts_attach(device_t, device_t, void *);
106 Static void	uts_childdet(device_t, device_t);
107 Static int	uts_detach(device_t, int);
108 Static int	uts_activate(device_t, enum devact);
109 
110 extern struct cfdriver uts_cd;
111 
112 CFATTACH_DECL2_NEW(uts, sizeof(struct uts_softc), uts_match, uts_attach,
113     uts_detach, uts_activate, NULL, uts_childdet);
114 
115 Static int
116 uts_match(device_t parent, cfdata_t match, void *aux)
117 {
118 	struct uhidev_attach_arg *uha = aux;
119 	int size;
120 	void *desc;
121 
122 	uhidev_get_report_desc(uha->parent, &desc, &size);
123 	if (!hid_is_collection(desc, size, uha->reportid,
124 	    HID_USAGE2(HUP_DIGITIZERS, HUD_TOUCH_SCREEN)) &&
125 	    !hid_is_collection(desc, size, uha->reportid,
126 	    HID_USAGE2(HUP_DIGITIZERS, HUD_FINGER)))
127 		return UMATCH_NONE;
128 
129 	return UMATCH_IFACECLASS;
130 }
131 
132 Static void
133 uts_attach(device_t parent, device_t self, void *aux)
134 {
135 	struct uts_softc *sc = device_private(self);
136 	struct uhidev_attach_arg *uha = aux;
137 	struct wsmousedev_attach_args a;
138 	int size;
139 	void *desc;
140 	u_int32_t flags;
141 	struct hid_data * d;
142 	struct hid_item item;
143 
144 	aprint_naive("\n");
145 
146 	sc->sc_hdev.sc_dev = self;
147 	sc->sc_hdev.sc_intr = uts_intr;
148 	sc->sc_hdev.sc_parent = uha->parent;
149 	sc->sc_hdev.sc_report_id = uha->reportid;
150 
151 	uhidev_get_report_desc(uha->parent, &desc, &size);
152 
153 	if (!pmf_device_register(self, NULL, NULL))
154 		aprint_error_dev(self, "couldn't establish power handler\n");
155 
156 	/* requires HID usage Generic_Desktop:X */
157 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_X),
158 		uha->reportid, hid_input, &sc->sc_loc_x, &flags)) {
159 		aprint_error_dev(sc->sc_hdev.sc_dev,
160 		    "touchscreen has no X report\n");
161 		return;
162 	}
163 	switch (flags & TSCREEN_FLAGS_MASK) {
164 	case 0:
165 		sc->flags |= UTS_ABS;
166 		break;
167 	case HIO_RELATIVE:
168 		break;
169 	default:
170 		aprint_error_dev(sc->sc_hdev.sc_dev,
171 		    "X report 0x%04x not supported\n", flags);
172 		return;
173 	}
174 
175 	/* requires HID usage Generic_Desktop:Y */
176 	if (!hid_locate(desc, size, HID_USAGE2(HUP_GENERIC_DESKTOP, HUG_Y),
177 		uha->reportid, hid_input, &sc->sc_loc_y, &flags)) {
178 		aprint_error_dev(sc->sc_hdev.sc_dev,
179 		    "touchscreen has no Y report\n");
180 		return;
181 	}
182 	switch (flags & TSCREEN_FLAGS_MASK) {
183 	case 0:
184 		sc->flags |= UTS_ABS;
185 		break;
186 	case HIO_RELATIVE:
187 		break;
188 	default:
189 		aprint_error_dev(sc->sc_hdev.sc_dev,
190 		    "Y report 0x%04x not supported\n", flags);
191 		return;
192 	}
193 
194 	/* requires HID usage Digitizer:Tip_Switch */
195 	if (!hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS, HUD_TIP_SWITCH),
196 	    uha->reportid, hid_input, &sc->sc_loc_btn, 0)) {
197 		aprint_error_dev(sc->sc_hdev.sc_dev,
198 		    "touchscreen has no tip switch report\n");
199 		return;
200 	}
201 
202 	/* requires HID usage Digitizer:In_Range */
203 	if (!hid_locate(desc, size, HID_USAGE2(HUP_DIGITIZERS, HUD_IN_RANGE),
204 		uha->reportid, hid_input, &sc->sc_loc_z, &flags)) {
205 		aprint_error_dev(sc->sc_hdev.sc_dev,
206 		    "touchscreen has no range report\n");
207 		return;
208 	}
209 
210 	/* multi-touch support would need HUD_CONTACTID and HUD_CONTACTMAX */
211 
212 #ifdef UTS_DEBUG
213 	DPRINTF(("uts_attach: sc=%p\n", sc));
214 	DPRINTF(("uts_attach: X\t%d/%d\n",
215 		sc->sc_loc_x.pos, sc->sc_loc_x.size));
216 	DPRINTF(("uts_attach: Y\t%d/%d\n",
217 		sc->sc_loc_y.pos, sc->sc_loc_y.size));
218 	DPRINTF(("uts_attach: Z\t%d/%d\n",
219 		sc->sc_loc_z.pos, sc->sc_loc_z.size));
220 #endif
221 
222 	a.accessops = &uts_accessops;
223 	a.accesscookie = sc;
224 
225 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
226 
227 	/* calibrate the touchscreen */
228 	memset(&sc->sc_calibcoords, 0, sizeof(sc->sc_calibcoords));
229 	sc->sc_calibcoords.maxx = 4095;
230 	sc->sc_calibcoords.maxy = 4095;
231 	sc->sc_calibcoords.samplelen = WSMOUSE_CALIBCOORDS_RESET;
232 	d = hid_start_parse(desc, size, hid_input);
233 	while (hid_get_item(d, &item)) {
234 		if (item.kind != hid_input
235 		    || HID_GET_USAGE_PAGE(item.usage) != HUP_GENERIC_DESKTOP
236 		    || item.report_ID != sc->sc_hdev.sc_report_id)
237 			continue;
238 		if (HID_GET_USAGE(item.usage) == HUG_X) {
239 			sc->sc_calibcoords.minx = item.logical_minimum;
240 			sc->sc_calibcoords.maxx = item.logical_maximum;
241 		}
242 		if (HID_GET_USAGE(item.usage) == HUG_Y) {
243 			sc->sc_calibcoords.miny = item.logical_minimum;
244 			sc->sc_calibcoords.maxy = item.logical_maximum;
245 		}
246 	}
247 	hid_end_parse(d);
248 
249 	tpcalib_init(&sc->sc_tpcalib);
250 	tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
251 	    (void *)&sc->sc_calibcoords, 0, 0);
252 
253 	return;
254 }
255 
256 Static int
257 uts_detach(device_t self, int flags)
258 {
259 	struct uts_softc *sc = device_private(self);
260 	int rv = 0;
261 
262 	DPRINTF(("uts_detach: sc=%p flags=%d\n", sc, flags));
263 
264 	if (sc->sc_wsmousedev != NULL)
265 		rv = config_detach(sc->sc_wsmousedev, flags);
266 
267 	pmf_device_deregister(self);
268 
269 	return rv;
270 }
271 
272 Static void
273 uts_childdet(device_t self, device_t child)
274 {
275 	struct uts_softc *sc = device_private(self);
276 
277 	KASSERT(sc->sc_wsmousedev == child);
278 	sc->sc_wsmousedev = NULL;
279 }
280 
281 Static int
282 uts_activate(device_t self, enum devact act)
283 {
284 	struct uts_softc *sc = device_private(self);
285 
286 	switch (act) {
287 	case DVACT_DEACTIVATE:
288 		sc->sc_dying = 1;
289 		return 0;
290 	default:
291 		return EOPNOTSUPP;
292 	}
293 }
294 
295 Static int
296 uts_enable(void *v)
297 {
298 	struct uts_softc *sc = v;
299 
300 	DPRINTFN(1,("uts_enable: sc=%p\n", sc));
301 
302 	if (sc->sc_dying)
303 		return EIO;
304 
305 	if (sc->sc_enabled)
306 		return EBUSY;
307 
308 	sc->sc_enabled = 1;
309 	sc->sc_buttons = 0;
310 
311 	return uhidev_open(&sc->sc_hdev);
312 }
313 
314 Static void
315 uts_disable(void *v)
316 {
317 	struct uts_softc *sc = v;
318 
319 	DPRINTFN(1,("uts_disable: sc=%p\n", sc));
320 #ifdef DIAGNOSTIC
321 	if (!sc->sc_enabled) {
322 		printf("uts_disable: not enabled\n");
323 		return;
324 	}
325 #endif
326 
327 	sc->sc_enabled = 0;
328 	uhidev_close(&sc->sc_hdev);
329 }
330 
331 Static int
332 uts_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
333 {
334 	struct uts_softc *sc = v;
335 
336 	switch (cmd) {
337 	case WSMOUSEIO_GTYPE:
338 		if (sc->flags & UTS_ABS)
339 			*(u_int *)data = WSMOUSE_TYPE_TPANEL;
340 		else
341 			*(u_int *)data = WSMOUSE_TYPE_USB;
342 		return 0;
343 	case WSMOUSEIO_SCALIBCOORDS:
344 	case WSMOUSEIO_GCALIBCOORDS:
345 		return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
346 	}
347 
348 	return EPASSTHROUGH;
349 }
350 
351 Static void
352 uts_intr(struct uhidev *addr, void *ibuf, u_int len)
353 {
354 	struct uts_softc *sc = (struct uts_softc *)addr;
355 	int dx, dy, dz;
356 	u_int32_t buttons = 0;
357 	int flags, s;
358 
359 	DPRINTFN(5,("uts_intr: len=%d\n", len));
360 
361 	flags = WSMOUSE_INPUT_DELTA | WSMOUSE_INPUT_ABSOLUTE_Z;
362 
363 	dx = hid_get_data(ibuf, &sc->sc_loc_x);
364 	if (sc->flags & UTS_ABS) {
365 		flags |= (WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
366 		dy = hid_get_data(ibuf, &sc->sc_loc_y);
367 		tpcalib_trans(&sc->sc_tpcalib, dx, dy, &dx, &dy);
368 	} else
369 		dy = -hid_get_data(ibuf, &sc->sc_loc_y);
370 
371 	dz = hid_get_data(ibuf, &sc->sc_loc_z);
372 
373 	if (hid_get_data(ibuf, &sc->sc_loc_btn))
374 		buttons |= 1;
375 
376 	if (dx != 0 || dy != 0 || dz != 0 || buttons != sc->sc_buttons) {
377 		DPRINTFN(10,("uts_intr: x:%d y:%d z:%d buttons:0x%x\n",
378 		    dx, dy, dz, buttons));
379 		sc->sc_buttons = buttons;
380 		if (sc->sc_wsmousedev != NULL) {
381 			s = spltty();
382 			wsmouse_input(sc->sc_wsmousedev, buttons, dx, dy, dz, 0,
383 			    flags);
384 			splx(s);
385 		}
386 	}
387 }
388