xref: /openbsd-src/sys/dev/usb/uts.c (revision cbb8e2d402387c84ae554cf2009f248cdb0a0db3)
1 /*	$OpenBSD: uts.c,v 1.32 2013/11/07 10:33:43 pirofti Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Robert Nagy <robert@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/sockio.h>
21 #include <sys/mbuf.h>
22 #include <sys/kernel.h>
23 #include <sys/socket.h>
24 #include <sys/systm.h>
25 #include <sys/malloc.h>
26 #include <sys/timeout.h>
27 #include <sys/conf.h>
28 #include <sys/device.h>
29 
30 #include <machine/bus.h>
31 #include <machine/endian.h>
32 #include <machine/intr.h>
33 
34 #include <dev/usb/usb.h>
35 #include <dev/usb/usbdi.h>
36 #include <dev/usb/usbdi_util.h>
37 #include <dev/usb/usbdevs.h>
38 
39 #include <dev/wscons/wsconsio.h>
40 #include <dev/wscons/wsmousevar.h>
41 
42 #ifdef USB_DEBUG
43 #define UTS_DEBUG
44 #endif
45 
46 #ifdef UTS_DEBUG
47 #define DPRINTF(x)		do { printf x; } while (0)
48 #else
49 #define DPRINTF(x)
50 #endif
51 
52 #define UTS_CONFIG_INDEX 0
53 
54 struct tsscale {
55 	int	minx, maxx;
56 	int	miny, maxy;
57 	int	swapxy;
58 	int	resx, resy;
59 } def_scale = {
60 	67, 1931, 102, 1937, 0, 1024, 768
61 };
62 
63 struct uts_softc {
64 	struct device		sc_dev;
65 	struct usbd_device	*sc_udev;
66 	struct usbd_interface	*sc_iface;
67 	int			sc_iface_number;
68 	int			sc_product;
69 	int			sc_vendor;
70 
71 	int			sc_intr_number;
72 	struct usbd_pipe	*sc_intr_pipe;
73 	u_char			*sc_ibuf;
74 	int			sc_isize;
75 	u_int8_t		sc_pkts;
76 
77 	struct device		*sc_wsmousedev;
78 
79 	int	sc_enabled;
80 	int	sc_buttons;
81 	int	sc_oldx;
82 	int	sc_oldy;
83 	int	sc_rawmode;
84 
85 	struct tsscale sc_tsscale;
86 };
87 
88 struct uts_pos {
89 	int	down;
90 	int	x;
91 	int	y;
92 	int	z;	/* touch pressure */
93 };
94 
95 const struct usb_devno uts_devs[] = {
96 	{ USB_VENDOR_FTDI,		USB_PRODUCT_FTDI_ITM_TOUCH },
97 	{ USB_VENDOR_EGALAX,		USB_PRODUCT_EGALAX_TPANEL },
98 	{ USB_VENDOR_EGALAX,		USB_PRODUCT_EGALAX_TPANEL2 },
99 	{ USB_VENDOR_GUNZE,		USB_PRODUCT_GUNZE_TOUCHPANEL }
100 };
101 
102 void uts_intr(struct usbd_xfer *, void *, usbd_status);
103 void uts_get_pos(void *addr, struct uts_pos *tp);
104 
105 int	uts_enable(void *);
106 void	uts_disable(void *);
107 int	uts_ioctl(void *, u_long, caddr_t, int, struct proc *);
108 
109 const struct wsmouse_accessops uts_accessops = {
110 	uts_enable,
111 	uts_ioctl,
112 	uts_disable,
113 };
114 
115 int uts_match(struct device *, void *, void *);
116 void uts_attach(struct device *, struct device *, void *);
117 int uts_detach(struct device *, int);
118 int uts_activate(struct device *, int);
119 
120 struct cfdriver uts_cd = {
121 	NULL, "uts", DV_DULL
122 };
123 
124 const struct cfattach uts_ca = {
125 	sizeof(struct uts_softc),
126 	uts_match,
127 	uts_attach,
128 	uts_detach,
129 	uts_activate,
130 };
131 
132 int
133 uts_match(struct device *parent, void *match, void *aux)
134 {
135 	struct usb_attach_arg *uaa = aux;
136 	usb_interface_descriptor_t *id;
137 
138 	if (uaa->iface == NULL)
139 		return (UMATCH_NONE);
140 
141 	/* Some eGalax touch screens are HID devices. ignore them */
142 	id = usbd_get_interface_descriptor(uaa->iface);
143 	if (id != NULL && id->bInterfaceClass == UICLASS_HID)
144 		return (UMATCH_NONE);
145 
146 	return (usb_lookup(uts_devs, uaa->vendor, uaa->product) != NULL) ?
147 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
148 }
149 
150 void
151 uts_attach(struct device *parent, struct device *self, void *aux)
152 {
153 	struct uts_softc *sc = (struct uts_softc *)self;
154 	struct usb_attach_arg *uaa = aux;
155 	usb_config_descriptor_t *cdesc;
156 	usb_interface_descriptor_t *id;
157 	usb_endpoint_descriptor_t *ed;
158 	struct wsmousedev_attach_args a;
159 	int i;
160 
161 	sc->sc_udev = uaa->device;
162 	sc->sc_product = uaa->product;
163 	sc->sc_vendor = uaa->vendor;
164 	sc->sc_intr_number = -1;
165 	sc->sc_intr_pipe = NULL;
166 	sc->sc_enabled = sc->sc_isize = 0;
167 
168 	/* Copy the default scalue values to each softc */
169 	bcopy(&def_scale, &sc->sc_tsscale, sizeof(sc->sc_tsscale));
170 
171 	/* Move the device into the configured state. */
172 	if (usbd_set_config_index(uaa->device, UTS_CONFIG_INDEX, 1) != 0) {
173 		printf("%s: could not set configuartion no\n",
174 		    sc->sc_dev.dv_xname);
175 		usbd_deactivate(sc->sc_udev);
176 		return;
177 	}
178 
179 	/* get the config descriptor */
180 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
181 	if (cdesc == NULL) {
182 		printf("%s: failed to get configuration descriptor\n",
183 		    sc->sc_dev.dv_xname);
184 		usbd_deactivate(sc->sc_udev);
185 		return;
186 	}
187 
188 	/* get the interface */
189 	if (usbd_device2interface_handle(uaa->device, 0, &sc->sc_iface) != 0) {
190 		printf("%s: failed to get interface\n",
191 		    sc->sc_dev.dv_xname);
192 		usbd_deactivate(sc->sc_udev);
193 		return;
194 	}
195 
196 	/* Find the interrupt endpoint */
197 	id = usbd_get_interface_descriptor(sc->sc_iface);
198 	sc->sc_iface_number = id->bInterfaceNumber;
199 
200 	for (i = 0; i < id->bNumEndpoints; i++) {
201 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
202 		if (ed == NULL) {
203 			printf("%s: no endpoint descriptor for %d\n",
204 			    sc->sc_dev.dv_xname, i);
205 			usbd_deactivate(sc->sc_udev);
206 			return;
207 		}
208 
209 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
210 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
211 			sc->sc_intr_number = ed->bEndpointAddress;
212 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
213 		}
214 	}
215 
216 	if (sc->sc_intr_number== -1) {
217 		printf("%s: Could not find interrupt in\n",
218 		    sc->sc_dev.dv_xname);
219 		usbd_deactivate(sc->sc_udev);
220 		return;
221 	}
222 
223 	a.accessops = &uts_accessops;
224 	a.accesscookie = sc;
225 
226 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
227 }
228 
229 int
230 uts_detach(struct device *self, int flags)
231 {
232 	struct uts_softc *sc = (struct uts_softc *)self;
233 	int rv = 0;
234 
235 	if (sc->sc_intr_pipe != NULL) {
236 		usbd_abort_pipe(sc->sc_intr_pipe);
237 		usbd_close_pipe(sc->sc_intr_pipe);
238 		sc->sc_intr_pipe = NULL;
239 	}
240 
241 	if (sc->sc_wsmousedev != NULL) {
242 		rv = config_detach(sc->sc_wsmousedev, flags);
243 		sc->sc_wsmousedev = NULL;
244 	}
245 
246 	return (rv);
247 }
248 
249 int
250 uts_activate(struct device *self, int act)
251 {
252 	struct uts_softc *sc = (struct uts_softc *)self;
253 	int rv = 0;
254 
255 	switch (act) {
256 	case DVACT_DEACTIVATE:
257 		if (sc->sc_wsmousedev != NULL)
258 			rv = config_deactivate(sc->sc_wsmousedev);
259 		usbd_deactivate(sc->sc_udev);
260 		break;
261 	}
262 
263 	return (rv);
264 }
265 
266 int
267 uts_enable(void *v)
268 {
269 	struct uts_softc *sc = v;
270 	int err;
271 
272 	if (usbd_is_dying(sc->sc_udev))
273 		return (EIO);
274 
275 	if (sc->sc_enabled)
276 		return (EBUSY);
277 
278 	if (sc->sc_isize == 0)
279 		return (0);
280 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
281 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
282 	    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf,
283 	    sc->sc_isize, uts_intr, USBD_DEFAULT_INTERVAL);
284 	if (err) {
285 		free(sc->sc_ibuf, M_USBDEV);
286 		sc->sc_intr_pipe = NULL;
287 		return (EIO);
288 	}
289 
290 	sc->sc_enabled = 1;
291 	sc->sc_buttons = 0;
292 
293 	return (0);
294 }
295 
296 void
297 uts_disable(void *v)
298 {
299 	struct uts_softc *sc = v;
300 
301 	if (!sc->sc_enabled) {
302 		printf("uts_disable: already disabled!\n");
303 		return;
304 	}
305 
306 	/* Disable interrupts. */
307 	if (sc->sc_intr_pipe != NULL) {
308 		usbd_abort_pipe(sc->sc_intr_pipe);
309 		usbd_close_pipe(sc->sc_intr_pipe);
310 		sc->sc_intr_pipe = NULL;
311 	}
312 
313 	if (sc->sc_ibuf != NULL) {
314 		free(sc->sc_ibuf, M_USBDEV);
315 		sc->sc_ibuf = NULL;
316 	}
317 
318 	sc->sc_enabled = 0;
319 }
320 
321 int
322 uts_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *l)
323 {
324 	int error = 0;
325 	struct uts_softc *sc = v;
326 	struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data;
327 
328 	DPRINTF(("uts_ioctl(%d, '%c', %d)\n",
329 	    IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd & 0xff));
330 
331 	switch (cmd) {
332 	case WSMOUSEIO_SCALIBCOORDS:
333 		if (!(wsmc->minx >= 0 && wsmc->maxx >= 0 &&
334 		    wsmc->miny >= 0 && wsmc->maxy >= 0 &&
335 		    wsmc->resx >= 0 && wsmc->resy >= 0 &&
336 		    wsmc->minx < 32768 && wsmc->maxx < 32768 &&
337 		    wsmc->miny < 32768 && wsmc->maxy < 32768 &&
338 		    (wsmc->maxx - wsmc->minx) != 0 &&
339 		    (wsmc->maxy - wsmc->miny) != 0 &&
340 		    wsmc->resx < 32768 && wsmc->resy < 32768 &&
341 		    wsmc->swapxy >= 0 && wsmc->swapxy <= 1 &&
342 		    wsmc->samplelen >= 0 && wsmc->samplelen <= 1))
343 			return (EINVAL);
344 
345 		sc->sc_tsscale.minx = wsmc->minx;
346 		sc->sc_tsscale.maxx = wsmc->maxx;
347 		sc->sc_tsscale.miny = wsmc->miny;
348 		sc->sc_tsscale.maxy = wsmc->maxy;
349 		sc->sc_tsscale.swapxy = wsmc->swapxy;
350 		sc->sc_tsscale.resx = wsmc->resx;
351 		sc->sc_tsscale.resy = wsmc->resy;
352 		sc->sc_rawmode = wsmc->samplelen;
353 		break;
354 	case WSMOUSEIO_GCALIBCOORDS:
355 		wsmc->minx = sc->sc_tsscale.minx;
356 		wsmc->maxx = sc->sc_tsscale.maxx;
357 		wsmc->miny = sc->sc_tsscale.miny;
358 		wsmc->maxy = sc->sc_tsscale.maxy;
359 		wsmc->swapxy = sc->sc_tsscale.swapxy;
360 		wsmc->resx = sc->sc_tsscale.resx;
361 		wsmc->resy = sc->sc_tsscale.resy;
362 		wsmc->samplelen = sc->sc_rawmode;
363 		break;
364 	case WSMOUSEIO_GTYPE:
365 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
366 		break;
367 	default:
368 		error = ENOTTY;
369 		break;
370 	}
371 
372 	return (error);
373 }
374 
375 void
376 uts_get_pos(void *addr, struct uts_pos *tp)
377 {
378 	struct uts_softc *sc = addr;
379 	u_char *p = sc->sc_ibuf;
380 	int down, x, y, z;
381 
382 	switch (sc->sc_product) {
383 	case USB_PRODUCT_FTDI_ITM_TOUCH:
384 		down = (~p[7] & 0x20);
385 		x = ((p[0] & 0x1f) << 7) | (p[3] & 0x7f);
386 		/* Invert the Y coordinate */
387 		y = 0x0fff - abs(((p[1] & 0x1f) << 7) | (p[4] & 0x7f));
388 		z = ((p[2] & 0x1) << 7) | (p[5] & 0x7f);
389 		sc->sc_pkts = 0x8;
390 		break;
391 	case USB_PRODUCT_EGALAX_TPANEL:
392 	case USB_PRODUCT_EGALAX_TPANEL2:
393 		/*
394 		 * eGalax and Gunze USB touch panels have the same device ID,
395 		 * so decide upon the vendor ID.
396 		 */
397 		switch (sc->sc_vendor) {
398 		case USB_VENDOR_EGALAX:
399 			down = (p[0] & 0x01);
400 			/* Invert the X coordinate */
401 			x = 0x07ff - abs(((p[3] & 0x0f) << 7) | (p[4] & 0x7f));
402 			y = ((p[1] & 0x0f) << 7) | (p[2] & 0x7f);
403 			z = down;
404 			sc->sc_pkts = 0x5;
405 			break;
406 		case USB_VENDOR_GUNZE:
407 			down = (~p[7] & 0x20);
408 			/* Invert the X coordinate */
409 			x = 0x0fff - abs(((p[0] & 0x1f) << 7) | (p[2] & 0x7f));
410 			y = ((p[1] & 0x1f) << 7) | (p[3] & 0x7f);
411 			z = (down != 0);
412 			sc->sc_pkts = 0x4;
413 			break;
414 		}
415 		break;
416 	}
417 
418 	DPRINTF(("%s: down = 0x%x, sc->sc_pkts = 0x%x\n",
419 	    sc->sc_dev.dv_xname, down, sc->sc_pkts));
420 
421 	/* x/y values are not reliable if there is no pressure */
422 	if (down) {
423 		if (sc->sc_tsscale.swapxy && !sc->sc_rawmode) {
424 			/* Swap X/Y-Axis */
425 			tp->y = x;
426 			tp->x = y;
427 		} else {
428 			tp->x = x;
429 			tp->y = y;
430 		}
431 		if (!sc->sc_rawmode &&
432 		    (sc->sc_tsscale.maxx - sc->sc_tsscale.minx) != 0 &&
433 		    (sc->sc_tsscale.maxy - sc->sc_tsscale.miny) != 0) {
434 			/* Scale down to the screen resolution. */
435 			tp->x = ((tp->x - sc->sc_tsscale.minx) *
436 			    sc->sc_tsscale.resx) /
437 			    (sc->sc_tsscale.maxx - sc->sc_tsscale.minx);
438 			tp->y = ((tp->y - sc->sc_tsscale.miny) *
439 			    sc->sc_tsscale.resy) /
440 			    (sc->sc_tsscale.maxy - sc->sc_tsscale.miny);
441 		}
442 	} else {
443 		tp->x = sc->sc_oldx;
444 		tp->y = sc->sc_oldy;
445 	}
446 	tp->z = z;
447 	tp->down = down;
448 }
449 
450 void
451 uts_intr(struct usbd_xfer *xfer, void *addr, usbd_status status)
452 {
453 	struct uts_softc *sc = addr;
454 	u_int32_t len;
455 	int s;
456 	struct uts_pos tp;
457 
458 	if (status == USBD_CANCELLED)
459 		return;
460 
461 	if (status != USBD_NORMAL_COMPLETION) {
462 		printf("%s: status %d\n", sc->sc_dev.dv_xname, status);
463 		if (status == USBD_STALLED)
464 			usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
465 		return;
466 	}
467 
468 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
469 
470 	s = spltty();
471 
472 	uts_get_pos(sc, &tp);
473 
474 	if (len != sc->sc_pkts) {
475 		DPRINTF(("%s: bad input length %d != %d\n",
476 		    sc->sc_dev.dv_xname, len, sc->sc_isize));
477 		splx(s);
478 		return;
479 	}
480 
481 	DPRINTF(("%s: tp.down = %d, tp.z = %d, tp.x = %d, tp.y = %d\n",
482 	    sc->sc_dev.dv_xname, tp.down, tp.z, tp.x, tp.y));
483 
484 	wsmouse_input(sc->sc_wsmousedev, tp.down, tp.x, tp.y, tp.z, 0,
485 	    WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y |
486 	    WSMOUSE_INPUT_ABSOLUTE_Z);
487 	sc->sc_oldy = tp.y;
488 	sc->sc_oldx = tp.x;
489 
490 	splx(s);
491 }
492