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