xref: /openbsd/sys/dev/usb/uts.c (revision 404b540a)
1 /*	$OpenBSD: uts.c,v 1.25 2009/10/13 19:33:19 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 	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 
138 	if (uaa->iface == NULL)
139 		return (UMATCH_NONE);
140 
141 	return (usb_lookup(uts_devs, uaa->vendor, uaa->product) != NULL) ?
142 	    UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
143 }
144 
145 void
146 uts_attach(struct device *parent, struct device *self, void *aux)
147 {
148 	struct uts_softc *sc = (struct uts_softc *)self;
149 	struct usb_attach_arg *uaa = aux;
150 	usb_config_descriptor_t *cdesc;
151 	usb_interface_descriptor_t *id;
152 	usb_endpoint_descriptor_t *ed;
153 	struct wsmousedev_attach_args a;
154 	int i;
155 
156 	sc->sc_udev = uaa->device;
157 	sc->sc_product = uaa->product;
158 	sc->sc_vendor = uaa->vendor;
159 	sc->sc_intr_number = -1;
160 	sc->sc_intr_pipe = NULL;
161 	sc->sc_enabled = sc->sc_isize = 0;
162 
163 	/* Copy the default scalue values to each softc */
164 	bcopy(&def_scale, &sc->sc_tsscale, sizeof(sc->sc_tsscale));
165 
166 	/* Move the device into the configured state. */
167 	if (usbd_set_config_index(uaa->device, UTS_CONFIG_INDEX, 1) != 0) {
168 		printf("%s: could not set configuartion no\n",
169 		    sc->sc_dev.dv_xname);
170 		sc->sc_dying = 1;
171 		return;
172 	}
173 
174 	/* get the config descriptor */
175 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
176 	if (cdesc == NULL) {
177 		printf("%s: failed to get configuration descriptor\n",
178 		    sc->sc_dev.dv_xname);
179 		sc->sc_dying = 1;
180 		return;
181 	}
182 
183 	/* get the interface */
184 	if (usbd_device2interface_handle(uaa->device, 0, &sc->sc_iface) != 0) {
185 		printf("%s: failed to get interface\n",
186 		    sc->sc_dev.dv_xname);
187 		sc->sc_dying = 1;
188 		return;
189 	}
190 
191 	/* Find the interrupt endpoint */
192 	id = usbd_get_interface_descriptor(sc->sc_iface);
193 	sc->sc_iface_number = id->bInterfaceNumber;
194 
195 	for (i = 0; i < id->bNumEndpoints; i++) {
196 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
197 		if (ed == NULL) {
198 			printf("%s: no endpoint descriptor for %d\n",
199 			    sc->sc_dev.dv_xname, i);
200 			sc->sc_dying = 1;
201 			return;
202 		}
203 
204 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
205 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
206 			sc->sc_intr_number = ed->bEndpointAddress;
207 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
208 		}
209 	}
210 
211 	if (sc->sc_intr_number== -1) {
212 		printf("%s: Could not find interrupt in\n",
213 		    sc->sc_dev.dv_xname);
214 		sc->sc_dying = 1;
215 		return;
216 	}
217 
218 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, &sc->sc_dev);
219 
220 	a.accessops = &uts_accessops;
221 	a.accesscookie = sc;
222 
223 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
224 }
225 
226 int
227 uts_detach(struct device *self, int flags)
228 {
229 	struct uts_softc *sc = (struct uts_softc *)self;
230 	int rv = 0;
231 
232 	if (sc->sc_intr_pipe != NULL) {
233 		usbd_abort_pipe(sc->sc_intr_pipe);
234 		usbd_close_pipe(sc->sc_intr_pipe);
235 		sc->sc_intr_pipe = NULL;
236 	}
237 
238 	sc->sc_dying = 1;
239 
240 	if (sc->sc_wsmousedev != NULL) {
241 		rv = config_detach(sc->sc_wsmousedev, flags);
242 		sc->sc_wsmousedev = NULL;
243 	}
244 
245 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, &sc->sc_dev);
246 
247 	return (rv);
248 }
249 
250 int
251 uts_activate(struct device *self, int act)
252 {
253 	struct uts_softc *sc = (struct uts_softc *)self;
254 	int rv = 0;
255 
256 	switch (act) {
257 	case DVACT_ACTIVATE:
258 		break;
259 
260 	case DVACT_DEACTIVATE:
261 		if (sc->sc_wsmousedev != NULL)
262 			rv = config_deactivate(sc->sc_wsmousedev);
263 		sc->sc_dying = 1;
264 		break;
265 	}
266 
267 	return (rv);
268 }
269 
270 int
271 uts_enable(void *v)
272 {
273 	struct uts_softc *sc = v;
274 	int err;
275 
276 	if (sc->sc_dying)
277 		return (EIO);
278 
279 	if (sc->sc_enabled)
280 		return (EBUSY);
281 
282 	if (sc->sc_isize == 0)
283 		return (0);
284 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
285 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
286 	    USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf,
287 	    sc->sc_isize, uts_intr, USBD_DEFAULT_INTERVAL);
288 	if (err) {
289 		free(sc->sc_ibuf, M_USBDEV);
290 		sc->sc_intr_pipe = NULL;
291 		return (EIO);
292 	}
293 
294 	sc->sc_enabled = 1;
295 	sc->sc_buttons = 0;
296 
297 	return (0);
298 }
299 
300 void
301 uts_disable(void *v)
302 {
303 	struct uts_softc *sc = v;
304 
305 	if (!sc->sc_enabled) {
306 		printf("uts_disable: already disabled!\n");
307 		return;
308 	}
309 
310 	/* Disable interrupts. */
311 	if (sc->sc_intr_pipe != NULL) {
312 		usbd_abort_pipe(sc->sc_intr_pipe);
313 		usbd_close_pipe(sc->sc_intr_pipe);
314 		sc->sc_intr_pipe = NULL;
315 	}
316 
317 	if (sc->sc_ibuf != NULL) {
318 		free(sc->sc_ibuf, M_USBDEV);
319 		sc->sc_ibuf = NULL;
320 	}
321 
322 	sc->sc_enabled = 0;
323 }
324 
325 int
326 uts_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *l)
327 {
328 	int error = 0;
329 	struct uts_softc *sc = v;
330 	struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data;
331 
332 	DPRINTF(("uts_ioctl(%d, '%c', %d)\n",
333 	    IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd & 0xff));
334 
335 	switch (cmd) {
336 	case WSMOUSEIO_SCALIBCOORDS:
337 		if (!(wsmc->minx >= 0 && wsmc->maxx >= 0 &&
338 		    wsmc->miny >= 0 && wsmc->maxy >= 0 &&
339 		    wsmc->resx >= 0 && wsmc->resy >= 0 &&
340 		    wsmc->minx < 32768 && wsmc->maxx < 32768 &&
341 		    wsmc->miny < 32768 && wsmc->maxy < 32768 &&
342 		    wsmc->resx < 32768 && wsmc->resy < 32768 &&
343 		    wsmc->swapxy >= 0 && wsmc->swapxy <= 1 &&
344 		    wsmc->samplelen >= 0 && wsmc->samplelen <= 1))
345 			return (EINVAL);
346 
347 		sc->sc_tsscale.minx = wsmc->minx;
348 		sc->sc_tsscale.maxx = wsmc->maxx;
349 		sc->sc_tsscale.miny = wsmc->miny;
350 		sc->sc_tsscale.maxy = wsmc->maxy;
351 		sc->sc_tsscale.swapxy = wsmc->swapxy;
352 		sc->sc_tsscale.resx = wsmc->resx;
353 		sc->sc_tsscale.resy = wsmc->resy;
354 		sc->sc_rawmode = wsmc->samplelen;
355 		break;
356 	case WSMOUSEIO_GCALIBCOORDS:
357 		wsmc->minx = sc->sc_tsscale.minx;
358 		wsmc->maxx = sc->sc_tsscale.maxx;
359 		wsmc->miny = sc->sc_tsscale.miny;
360 		wsmc->maxy = sc->sc_tsscale.maxy;
361 		wsmc->swapxy = sc->sc_tsscale.swapxy;
362 		wsmc->resx = sc->sc_tsscale.resx;
363 		wsmc->resy = sc->sc_tsscale.resy;
364 		wsmc->samplelen = sc->sc_rawmode;
365 		break;
366 	case WSMOUSEIO_GTYPE:
367 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
368 		break;
369 	default:
370 		error = ENOTTY;
371 		break;
372 	}
373 
374 	return (error);
375 }
376 
377 void
378 uts_get_pos(usbd_private_handle addr, struct uts_pos *tp)
379 {
380 	struct uts_softc *sc = addr;
381 	u_char *p = sc->sc_ibuf;
382 	int down, x, y, z;
383 
384 	switch (sc->sc_product) {
385 	case USB_PRODUCT_FTDI_ITM_TOUCH:
386 		down = (~p[7] & 0x20);
387 		x = ((p[0] & 0x1f) << 7) | (p[3] & 0x7f);
388 		/* Invert the Y coordinate */
389 		y = 0x0fff - abs(((p[1] & 0x1f) << 7) | (p[4] & 0x7f));
390 		z = ((p[2] & 0x1) << 7) | (p[5] & 0x7f);
391 		sc->sc_pkts = 0x8;
392 		break;
393 	case USB_PRODUCT_EGALAX_TPANEL:
394 	case USB_PRODUCT_EGALAX_TPANEL2:
395 		/*
396 		 * eGalax and Gunze USB touch panels have the same device ID,
397 		 * so decide upon the vendor ID.
398 		 */
399 		switch (sc->sc_vendor) {
400 		case USB_VENDOR_EGALAX:
401 			down = (p[0] & 0x01);
402 			/* Invert the X coordinate */
403 			x = 0x07ff - abs(((p[3] & 0x0f) << 7) | (p[4] & 0x7f));
404 			y = ((p[1] & 0x0f) << 7) | (p[2] & 0x7f);
405 			z = down;
406 			sc->sc_pkts = 0x5;
407 			break;
408 		case USB_VENDOR_GUNZE:
409 			down = (~p[7] & 0x20);
410 			/* Invert the X coordinate */
411 			x = 0x0fff - abs(((p[0] & 0x1f) << 7) | (p[2] & 0x7f));
412 			y = ((p[1] & 0x1f) << 7) | (p[3] & 0x7f);
413 			z = (down != 0);
414 			sc->sc_pkts = 0x4;
415 			break;
416 		}
417 		break;
418 	}
419 
420 	DPRINTF(("%s: down = 0x%x, sc->sc_pkts = 0x%x\n",
421 	    sc->sc_dev.dv_xname, down, sc->sc_pkts));
422 
423 	/* x/y values are not reliable if there is no pressure */
424 	if (down) {
425 		if (sc->sc_tsscale.swapxy && !sc->sc_rawmode) {
426 			/* Swap X/Y-Axis */
427 			tp->y = x;
428 			tp->x = y;
429 		} else {
430 			tp->x = x;
431 			tp->y = y;
432 		}
433 		if (!sc->sc_rawmode) {
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(usbd_xfer_handle xfer, usbd_private_handle 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