xref: /openbsd-src/sys/dev/usb/uts.c (revision 7303018685d384bf9e330b12e17cfdd5cc1c71a0)
1 /*	$OpenBSD: uts.c,v 1.15 2007/06/12 16:26:37 mbalmer 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	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(usbd_xfer_handle, usbd_private_handle, usbd_status);
103 struct uts_pos uts_get_pos(usbd_private_handle 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 USB_DECLARE_DRIVER(uts);
116 
117 int
118 uts_match(struct device *parent, void *match, void *aux)
119 {
120 	struct usb_attach_arg *uaa = aux;
121 
122 	if (uaa->iface == NULL)
123 		return UMATCH_NONE;
124 
125 	return (usb_lookup(uts_devs, uaa->vendor, uaa->product) != NULL) ?
126 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
127 }
128 
129 void
130 uts_attach(struct device *parent, struct device *self, void *aux)
131 {
132 	struct uts_softc *sc = (struct uts_softc *)self;
133 	struct usb_attach_arg *uaa = aux;
134 	usb_config_descriptor_t *cdesc;
135 	usb_interface_descriptor_t *id;
136 	usb_endpoint_descriptor_t *ed;
137 	struct wsmousedev_attach_args a;
138 	char *devinfop;
139 	int i, found;
140 
141 	sc->sc_udev = uaa->device;
142 	sc->sc_product = uaa->product;
143 	sc->sc_vendor = uaa->vendor;
144 	sc->sc_intr_number = -1;
145 	sc->sc_intr_pipe = NULL;
146 	sc->sc_enabled = sc->sc_isize = 0;
147 
148 	/* Copy the default scalue values to each softc */
149 	bcopy(&def_scale, &sc->sc_tsscale, sizeof(sc->sc_tsscale));
150 
151 	/* Display device info string */
152 	printf("\n");
153 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
154 	printf("%s: %s\n", sc->sc_dev.dv_xname, devinfop);
155 	usbd_devinfo_free(devinfop);
156 
157 	/* Move the device into the configured state. */
158 	if (usbd_set_config_index(uaa->device, UTS_CONFIG_INDEX, 1) != 0) {
159 		printf("%s: could not set configuartion no\n",
160 			sc->sc_dev.dv_xname);
161 		sc->sc_dying = 1;
162 		return;
163 	}
164 
165 	/* get the config descriptor */
166 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
167 	if (cdesc == NULL) {
168 		printf("%s: failed to get configuration descriptor\n",
169 			sc->sc_dev.dv_xname);
170 		sc->sc_dying = 1;
171 		return;
172 	}
173 
174 	/* get the interface */
175 	if (usbd_device2interface_handle(uaa->device, 0, &sc->sc_iface) != 0) {
176 		printf("%s: failed to get interface\n",
177 			sc->sc_dev.dv_xname);
178 		sc->sc_dying = 1;
179 		return;
180 	}
181 
182 	/* Find the interrupt endpoint */
183 	id = usbd_get_interface_descriptor(sc->sc_iface);
184 	sc->sc_iface_number = id->bInterfaceNumber;
185 	found = 0;
186 
187 	for (i = 0; i < id->bNumEndpoints; i++) {
188 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
189 		if (ed == NULL) {
190 			printf("%s: no endpoint descriptor for %d\n",
191 				sc->sc_dev.dv_xname, i);
192 			sc->sc_dying = 1;
193 			return;
194 		}
195 
196 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
197 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
198 			sc->sc_intr_number = ed->bEndpointAddress;
199 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
200 		}
201 	}
202 
203 	if (sc->sc_intr_number== -1) {
204 		printf("%s: Could not find interrupt in\n",
205 			sc->sc_dev.dv_xname);
206 		sc->sc_dying = 1;
207 		return;
208 	}
209 
210 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
211 			   &sc->sc_dev);
212 
213 	a.accessops = &uts_accessops;
214 	a.accesscookie = sc;
215 
216 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
217 }
218 
219 int
220 uts_detach(struct device *self, int flags)
221 {
222 	struct uts_softc *sc = (struct uts_softc *)self;
223 	int rv = 0;
224 
225 	if (sc->sc_intr_pipe != NULL) {
226 		usbd_abort_pipe(sc->sc_intr_pipe);
227 		usbd_close_pipe(sc->sc_intr_pipe);
228 		sc->sc_intr_pipe = NULL;
229 	}
230 
231 	sc->sc_dying = 1;
232 
233 	if (sc->sc_wsmousedev != NULL) {
234 		rv = config_detach(sc->sc_wsmousedev, flags);
235 		sc->sc_wsmousedev = NULL;
236 	}
237 
238 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
239 			   &sc->sc_dev);
240 
241 	return (rv);
242 }
243 
244 int
245 uts_activate(struct device *self, enum devact act)
246 {
247 	struct uts_softc *sc = (struct uts_softc *)self;
248 	int rv = 0;
249 
250 	switch (act) {
251 	case DVACT_ACTIVATE:
252 		break;
253 
254 	case DVACT_DEACTIVATE:
255 		if (sc->sc_wsmousedev != NULL)
256 			rv = config_deactivate(sc->sc_wsmousedev);
257 		sc->sc_dying = 1;
258 		break;
259 	}
260 
261 	return (rv);
262 }
263 
264 int
265 uts_enable(void *v)
266 {
267 	struct uts_softc *sc = v;
268 	int err;
269 
270 	if (sc->sc_dying)
271 		return (EIO);
272 
273 	if (sc->sc_enabled)
274 		return (EBUSY);
275 
276 	if (sc->sc_isize == 0)
277 		return 0;
278 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
279 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
280 		USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf,
281 		sc->sc_isize, uts_intr, USBD_DEFAULT_INTERVAL);
282 	if (err) {
283 		free(sc->sc_ibuf, M_USBDEV);
284 		sc->sc_intr_pipe = NULL;
285 		return EIO;
286 	}
287 
288 	sc->sc_enabled = 1;
289 	sc->sc_buttons = 0;
290 
291 	return (0);
292 }
293 
294 void
295 uts_disable(void *v)
296 {
297 	struct uts_softc *sc = v;
298 
299 	if (!sc->sc_enabled) {
300 		printf("uts_disable: already disabled!\n");
301 		return;
302 	}
303 
304 	/* Disable interrupts. */
305 	if (sc->sc_intr_pipe != NULL) {
306 		usbd_abort_pipe(sc->sc_intr_pipe);
307 		usbd_close_pipe(sc->sc_intr_pipe);
308 		sc->sc_intr_pipe = NULL;
309 	}
310 
311 	if (sc->sc_ibuf != NULL) {
312 		free(sc->sc_ibuf, M_USBDEV);
313 		sc->sc_ibuf = NULL;
314 	}
315 
316 	sc->sc_enabled = 0;
317 }
318 
319 int
320 uts_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *l)
321 {
322 	int error = 0;
323 	struct uts_softc *sc = v;
324 	struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data;
325 
326 	DPRINTF(("uts_ioctl(%d, '%c', %d)\n",
327 	    IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd & 0xff));
328 
329 	switch (cmd) {
330 	case WSMOUSEIO_SCALIBCOORDS:
331 		if (!(wsmc->minx >= 0 && wsmc->maxx >= 0 &&
332 		    wsmc->miny >= 0 && wsmc->maxy >= 0 &&
333 		    wsmc->resx >= 0 && wsmc->resy >= 0 &&
334 		    wsmc->minx < 32768 && wsmc->maxx < 32768 &&
335 		    wsmc->miny < 32768 && wsmc->maxy < 32768 &&
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 struct uts_pos
372 uts_get_pos(usbd_private_handle addr, struct uts_pos tp)
373 {
374 	struct uts_softc *sc = addr;
375 	u_char *p = sc->sc_ibuf;
376 	int down, x, y;
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 		sc->sc_pkts = 0x8;
385 		break;
386 	case USB_PRODUCT_EGALAX_TPANEL:
387 	case USB_PRODUCT_EGALAX_TPANEL2:
388 		/*
389 		 * eGalax and Gunze USB touch panels have the same device ID,
390 		 * so decide upon the vendor ID.
391 		 */
392 		switch (sc->sc_vendor) {
393 		case USB_VENDOR_EGALAX:
394 			down = (p[0] & 0x01);
395 			/* Invert the X coordiate */
396 			x = 0x07ff - abs(((p[3] & 0x0f) << 7) | (p[4] & 0x7f));
397 			y = ((p[1] & 0x0f) << 7) | (p[2] & 0x7f);
398 			sc->sc_pkts = 0x5;
399 			break;
400 		case USB_VENDOR_GUNZE:
401 			down = (~p[7] & 0x20);
402 			/* Invert the X coordinate */
403 			x = 0x0fff - abs(((p[0] & 0x1f) << 7) | (p[2] & 0x7f));
404 			y = ((p[1] & 0x1f) << 7) | (p[3] & 0x7f);
405 			sc->sc_pkts = 0x4;
406 			break;
407 		}
408 		break;
409 	}
410 
411 	DPRINTF(("%s: down = 0x%x, sc->sc_pkts = 0x%x\n",
412 	    sc->sc_dev.dv_xname, down, sc->sc_pkts));
413 
414 	/* x/y values are not reliable if there is no pressure */
415 	if (down) {
416 		if (sc->sc_tsscale.swapxy) {	/* Swap X/Y-Axis */
417 			tp.y = x;
418 			tp.x = y;
419 		} else {
420 			tp.x = x;
421 			tp.y = y;
422 		}
423 
424 		if (!sc->sc_rawmode) {
425 			/* Scale down to the screen resolution. */
426 			tp.x = ((tp.x - sc->sc_tsscale.minx) *
427 			    sc->sc_tsscale.resx) /
428 			    (sc->sc_tsscale.maxx - sc->sc_tsscale.minx);
429 			tp.y = ((tp.y - sc->sc_tsscale.miny) *
430 			    sc->sc_tsscale.resy) /
431 			    (sc->sc_tsscale.maxy - sc->sc_tsscale.miny);
432 		}
433 		tp.z = 1;
434 	} else {
435 		tp.x = sc->sc_oldx;
436 		tp.y = sc->sc_oldy;
437 		tp.z = 0;
438 	}
439 
440 	return (tp);
441 }
442 
443 void
444 uts_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
445 {
446 	struct uts_softc *sc = addr;
447 	u_int32_t len;
448 	int s;
449 	struct uts_pos tp;
450 
451 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
452 
453 	s = spltty();
454 
455 	if (status == USBD_CANCELLED)
456 		return;
457 
458 	if (status != USBD_NORMAL_COMPLETION) {
459 		printf("%s: status %d\n", sc->sc_dev.dv_xname, status);
460 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
461 		return;
462 	}
463 
464 	tp = uts_get_pos(sc, tp);
465 
466 	if (len != sc->sc_pkts) {
467 		DPRINTF(("%s: bad input length %d != %d\n",
468 			sc->sc_dev.dv_xname, len, sc->sc_isize));
469 		return;
470 	}
471 
472 	DPRINTF(("%s: tp.z = %d, tp.x = %d, tp.y = %d\n",
473 	    sc->sc_dev.dv_xname, tp.z, tp.x, tp.y));
474 
475 	wsmouse_input(sc->sc_wsmousedev, tp.z, tp.x, tp.y, 0, 0,
476 		WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y |
477 		WSMOUSE_INPUT_ABSOLUTE_Z);
478 	sc->sc_oldy = tp.y;
479 	sc->sc_oldx = tp.x;
480 
481 	splx(s);
482 }
483