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