xref: /openbsd-src/sys/dev/usb/uts.c (revision dcff4970b11322ee6a6c83bbd7ec5fd1248cc105)
1 /*	$OpenBSD: uts.c,v 1.9 2007/05/31 02:56:03 robert 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 	USBBASEDEVICE		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 	device_ptr_t		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 Static 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 Static 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 Static int	uts_enable(void *);
106 Static void	uts_disable(void *);
107 Static 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 	if ((devinfop = usbd_devinfo_alloc(uaa->device, 0)) != NULL) {
154 		printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
155 		usbd_devinfo_free(devinfop);
156 	}
157 
158 	/* Move the device into the configured state. */
159 	if (usbd_set_config_index(uaa->device, UTS_CONFIG_INDEX, 1) != 0) {
160 		printf("%s: could not set configuartion no\n",
161 			USBDEVNAME(sc->sc_dev));
162 		sc->sc_dying = 1;
163 		return;
164 	}
165 
166 	/* get the config descriptor */
167 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
168 	if (cdesc == NULL) {
169 		printf("%s: failed to get configuration descriptor\n",
170 			USBDEVNAME(sc->sc_dev));
171 		sc->sc_dying = 1;
172 		return;
173 	}
174 
175 	/* get the interface */
176 	if (usbd_device2interface_handle(uaa->device, 0, &sc->sc_iface) != 0) {
177 		printf("%s: failed to get interface\n",
178 			USBDEVNAME(sc->sc_dev));
179 		sc->sc_dying = 1;
180 		return;
181 	}
182 
183 	/* Find the interrupt endpoint */
184 	id = usbd_get_interface_descriptor(sc->sc_iface);
185 	sc->sc_iface_number = id->bInterfaceNumber;
186 	found = 0;
187 
188 	for (i = 0; i < id->bNumEndpoints; i++) {
189 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
190 		if (ed == NULL) {
191 			printf("%s: no endpoint descriptor for %d\n",
192 				USBDEVNAME(sc->sc_dev), i);
193 			sc->sc_dying = 1;
194 			return;
195 		}
196 
197 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
198 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
199 			sc->sc_intr_number = ed->bEndpointAddress;
200 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
201 		}
202 	}
203 
204 	if (sc->sc_intr_number== -1) {
205 		printf("%s: Could not find interrupt in\n",
206 			USBDEVNAME(sc->sc_dev));
207 		sc->sc_dying = 1;
208 		return;
209 	}
210 
211 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
212 			   USBDEV(sc->sc_dev));
213 
214 	a.accessops = &uts_accessops;
215 	a.accesscookie = sc;
216 
217 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
218 }
219 
220 int
221 uts_detach(struct device *self, int flags)
222 {
223 	struct uts_softc *sc = (struct uts_softc *)self;
224 	int rv = 0;
225 
226 	if (sc->sc_intr_pipe != NULL) {
227 		usbd_abort_pipe(sc->sc_intr_pipe);
228 		usbd_close_pipe(sc->sc_intr_pipe);
229 		sc->sc_intr_pipe = NULL;
230 	}
231 
232 	sc->sc_dying = 1;
233 
234 	if (sc->sc_wsmousedev != NULL) {
235 		rv = config_detach(sc->sc_wsmousedev, flags);
236 		sc->sc_wsmousedev = NULL;
237 	}
238 
239 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
240 			   USBDEV(sc->sc_dev));
241 
242 	return (rv);
243 }
244 
245 int
246 uts_activate(device_ptr_t self, enum devact act)
247 {
248 	struct uts_softc *sc = (struct uts_softc *)self;
249 	int rv = 0;
250 
251 	switch (act) {
252 	case DVACT_ACTIVATE:
253 		break;
254 
255 	case DVACT_DEACTIVATE:
256 		if (sc->sc_wsmousedev != NULL)
257 			rv = config_deactivate(sc->sc_wsmousedev);
258 		sc->sc_dying = 1;
259 		break;
260 	}
261 
262 	return (rv);
263 }
264 
265 Static int
266 uts_enable(void *v)
267 {
268 	struct uts_softc *sc = v;
269 	int err;
270 
271 	if (sc->sc_dying)
272 		return (EIO);
273 
274 	if (sc->sc_enabled)
275 		return (EBUSY);
276 
277 	if (sc->sc_isize == 0)
278 		return 0;
279 	sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
280 	err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
281 		USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf,
282 		sc->sc_isize, uts_intr, USBD_DEFAULT_INTERVAL);
283 	if (err) {
284 		free(sc->sc_ibuf, M_USBDEV);
285 		sc->sc_intr_pipe = NULL;
286 		return EIO;
287 	}
288 
289 	sc->sc_enabled = 1;
290 	sc->sc_buttons = 0;
291 
292 	return (0);
293 }
294 
295 Static void
296 uts_disable(void *v)
297 {
298 	struct uts_softc *sc = v;
299 
300 	if (!sc->sc_enabled) {
301 		printf("uts_disable: already disabled!\n");
302 		return;
303 	}
304 
305 	/* Disable interrupts. */
306 	if (sc->sc_intr_pipe != NULL) {
307 		usbd_abort_pipe(sc->sc_intr_pipe);
308 		usbd_close_pipe(sc->sc_intr_pipe);
309 		sc->sc_intr_pipe = NULL;
310 	}
311 
312 	if (sc->sc_ibuf != NULL) {
313 		free(sc->sc_ibuf, M_USBDEV);
314 		sc->sc_ibuf = NULL;
315 	}
316 
317 	sc->sc_enabled = 0;
318 }
319 
320 Static int
321 uts_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *l)
322 {
323 	int error = 0;
324 	struct uts_softc *sc = v;
325 	struct wsmouse_calibcoords *wsmc = (struct wsmouse_calibcoords *)data;
326 
327 	DPRINTF(("uts_ioctl(%d, '%c', %d)\n",
328 	    IOCPARM_LEN(cmd), IOCGROUP(cmd), cmd & 0xff));
329 
330 	switch (cmd) {
331 	case WSMOUSEIO_SCALIBCOORDS:
332 		if (!(wsmc->minx >= 0 && wsmc->maxx >= 0 &&
333 		    wsmc->miny >= 0 && wsmc->maxy >= 0 &&
334 		    wsmc->resx >= 0 && wsmc->resy >= 0 &&
335 		    wsmc->minx < 32768 && wsmc->maxx < 32768 &&
336 		    wsmc->miny < 32768 && wsmc->maxy < 32768 &&
337 		    wsmc->resx < 32768 && wsmc->resy < 32768 &&
338 		    wsmc->swapxy >= 0 && wsmc->swapxy <= 1 &&
339 		    wsmc->samplelen >= 0 && wsmc->samplelen <= 1))
340 			return (EINVAL);
341 
342 		sc->sc_tsscale.minx = wsmc->minx;
343 		sc->sc_tsscale.maxx = wsmc->maxx;
344 		sc->sc_tsscale.miny = wsmc->miny;
345 		sc->sc_tsscale.maxy = wsmc->maxy;
346 		sc->sc_tsscale.swapxy = wsmc->swapxy;
347 		sc->sc_tsscale.resx = wsmc->resx;
348 		sc->sc_tsscale.resy = wsmc->resy;
349 		sc->sc_rawmode = wsmc->samplelen;
350 		break;
351 	case WSMOUSEIO_GCALIBCOORDS:
352 		wsmc->minx = sc->sc_tsscale.minx;
353 		wsmc->maxx = sc->sc_tsscale.maxx;
354 		wsmc->miny = sc->sc_tsscale.miny;
355 		wsmc->maxy = sc->sc_tsscale.maxy;
356 		wsmc->swapxy = sc->sc_tsscale.swapxy;
357 		wsmc->resx = sc->sc_tsscale.resx;
358 		wsmc->resy = sc->sc_tsscale.resy;
359 		wsmc->samplelen = sc->sc_rawmode;
360 		break;
361 	case WSMOUSEIO_GTYPE:
362 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
363 		break;
364 	default:
365 		error = ENOTTY;
366 		break;
367 	}
368 
369 	return (error);
370 }
371 
372 struct uts_pos
373 uts_get_pos(usbd_private_handle addr, struct uts_pos tp)
374 {
375 	struct uts_softc *sc = addr;
376 	u_char *p = sc->sc_ibuf;
377 	int down, x, y;
378 
379 	switch (sc->sc_product) {
380 	case USB_PRODUCT_FTDI_ITM_TOUCH:
381 		down = (~p[7] & 0x20);
382 		x = ((p[0] & 0x1f) << 7) | (p[3] & 0x7f);
383 		/* Invert the Y coordinate */
384 		y = 0x0fff - abs(((p[1] & 0x1f) << 7) | (p[4] & 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 coordiate */
397 			x = 0x07ff - abs(((p[3] & 0x0f) << 7) | (p[4] & 0x7f));
398 			y = ((p[1] & 0x0f) << 7) | (p[2] & 0x7f);
399 			sc->sc_pkts = 0x5;
400 			break;
401 		case USB_VENDOR_GUNZE:
402 			down = (~p[7] & 0x20);
403 			/* Invert the X coordinate */
404 			x = 0x0fff - abs(((p[0] & 0x1f) << 7) | (p[2] & 0x7f));
405 			y = ((p[1] & 0x1f) << 7) | (p[3] & 0x7f);
406 			sc->sc_pkts = 0x4;
407 			break;
408 		}
409 		break;
410 	}
411 
412 	DPRINTF(("%s: down = 0x%x, sc->sc_pkts = 0x%x\n",
413 	    USBDEVNAME(sc->sc_dev), down, sc->sc_pkts));
414 
415 	/* x/y values are not reliable if there is no pressure */
416 	if (down) {
417 		if (sc->sc_tsscale.swapxy) {	/* Swap X/Y-Axis */
418 			tp.y = x;
419 			tp.x = y;
420 		} else {
421 			tp.x = x;
422 			tp.y = y;
423 		}
424 
425 		if (!sc->sc_rawmode) {
426 			/* Scale down to the screen resolution. */
427 			tp.x = ((tp.x - sc->sc_tsscale.minx) *
428 			    sc->sc_tsscale.resx) /
429 			    (sc->sc_tsscale.maxx - sc->sc_tsscale.minx);
430 			tp.y = ((tp.y - sc->sc_tsscale.miny) *
431 			    sc->sc_tsscale.resy) /
432 			    (sc->sc_tsscale.maxy - sc->sc_tsscale.miny);
433 		}
434 		tp.z = 1;
435 	} else {
436 		tp.x = sc->sc_oldx;
437 		tp.y = sc->sc_oldy;
438 		tp.z = 0;
439 	}
440 
441 	return (tp);
442 }
443 
444 Static void
445 uts_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
446 {
447 	struct uts_softc *sc = addr;
448 	u_int32_t len;
449 	int s;
450 	struct uts_pos tp;
451 
452 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
453 
454 	s = spltty();
455 
456 	if (status == USBD_CANCELLED)
457 		return;
458 
459 	if (status != USBD_NORMAL_COMPLETION) {
460 		printf("%s: status %d\n", USBDEVNAME(sc->sc_dev), status);
461 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
462 		return;
463 	}
464 
465 	tp = uts_get_pos(sc, tp);
466 
467 	if (len != sc->sc_pkts) {
468 		DPRINTF(("%s: bad input length %d != %d\n",
469 			USBDEVNAME(sc->sc_dev), len, sc->sc_isize));
470 		return;
471 	}
472 
473 	DPRINTF(("%s: tp.z = %d, tp.x = %d, tp.y = %d\n",
474 	    USBDEVNAME(sc->sc_dev), tp.z, tp.x, tp.y));
475 
476 	wsmouse_input(sc->sc_wsmousedev, tp.z, tp.x, tp.y, 0, 0,
477 		WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y |
478 		WSMOUSE_INPUT_ABSOLUTE_Z);
479 	sc->sc_oldy = tp.y;
480 	sc->sc_oldx = tp.x;
481 
482 	splx(s);
483 }
484