xref: /openbsd-src/sys/dev/usb/uts.c (revision de5d9ff015fd30fc7ebf280f589fddecd89ee872)
1 /*	$OpenBSD: uts.c,v 1.8 2007/05/27 04:00:25 jsg 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 
70 	int			sc_intr_number;
71 	usbd_pipe_handle	sc_intr_pipe;
72 	u_char			*sc_ibuf;
73 	int			sc_isize;
74 	u_int8_t		sc_pkts;
75 
76 	device_ptr_t		sc_wsmousedev;
77 
78 	int	sc_enabled;
79 	int	sc_buttons;
80 	int	sc_dying;
81 	int	sc_oldx;
82 	int	sc_oldy;
83 	int	sc_rawmode;
84 
85 	struct tsscale sc_tsscale;
86 };
87 
88 struct uts_pos {
89 	int	x;
90 	int	y;
91 	int	z;	/* touch pressure */
92 };
93 
94 Static const struct usb_devno uts_devs[] = {
95 	{ USB_VENDOR_FTDI,		USB_PRODUCT_FTDI_ITM_TOUCH },
96 	{ USB_VENDOR_EGALAX,		USB_PRODUCT_EGALAX_TPANEL },
97 	{ USB_VENDOR_EGALAX,		USB_PRODUCT_EGALAX_TPANEL2 }
98 };
99 
100 Static void uts_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
101 struct uts_pos uts_get_pos(usbd_private_handle addr, struct uts_pos tp);
102 
103 Static int	uts_enable(void *);
104 Static void	uts_disable(void *);
105 Static int	uts_ioctl(void *, u_long, caddr_t, int, struct proc *);
106 
107 const struct wsmouse_accessops uts_accessops = {
108 	uts_enable,
109 	uts_ioctl,
110 	uts_disable,
111 };
112 
113 USB_DECLARE_DRIVER(uts);
114 
115 int
116 uts_match(struct device *parent, void *match, void *aux)
117 {
118 	struct usb_attach_arg *uaa = aux;
119 
120 	if (uaa->iface == NULL)
121 		return UMATCH_NONE;
122 
123 	return (usb_lookup(uts_devs, uaa->vendor, uaa->product) != NULL) ?
124 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
125 }
126 
127 void
128 uts_attach(struct device *parent, struct device *self, void *aux)
129 {
130 	struct uts_softc *sc = (struct uts_softc *)self;
131 	struct usb_attach_arg *uaa = aux;
132 	usb_config_descriptor_t *cdesc;
133 	usb_interface_descriptor_t *id;
134 	usb_endpoint_descriptor_t *ed;
135 	struct wsmousedev_attach_args a;
136 	char *devinfop;
137 	int i, found;
138 
139 	sc->sc_udev = uaa->device;
140 	sc->sc_product = uaa->product;
141 	sc->sc_intr_number = -1;
142 	sc->sc_intr_pipe = NULL;
143 	sc->sc_enabled = sc->sc_isize = 0;
144 
145 	/* Copy the default scalue values to each softc */
146 	bcopy(&def_scale, &sc->sc_tsscale, sizeof(sc->sc_tsscale));
147 
148 	/* Display device info string */
149 	printf("\n");
150 	if ((devinfop = usbd_devinfo_alloc(uaa->device, 0)) != NULL) {
151 		printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
152 		usbd_devinfo_free(devinfop);
153 	}
154 
155 	/* Move the device into the configured state. */
156 	if (usbd_set_config_index(uaa->device, UTS_CONFIG_INDEX, 1) != 0) {
157 		printf("%s: could not set configuartion no\n",
158 			USBDEVNAME(sc->sc_dev));
159 		sc->sc_dying = 1;
160 		return;
161 	}
162 
163 	/* get the config descriptor */
164 	cdesc = usbd_get_config_descriptor(sc->sc_udev);
165 	if (cdesc == NULL) {
166 		printf("%s: failed to get configuration descriptor\n",
167 			USBDEVNAME(sc->sc_dev));
168 		sc->sc_dying = 1;
169 		return;
170 	}
171 
172 	/* get the interface */
173 	if (usbd_device2interface_handle(uaa->device, 0, &sc->sc_iface) != 0) {
174 		printf("%s: failed to get interface\n",
175 			USBDEVNAME(sc->sc_dev));
176 		sc->sc_dying = 1;
177 		return;
178 	}
179 
180 	/* Find the interrupt endpoint */
181 	id = usbd_get_interface_descriptor(sc->sc_iface);
182 	sc->sc_iface_number = id->bInterfaceNumber;
183 	found = 0;
184 
185 	for (i = 0; i < id->bNumEndpoints; i++) {
186 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
187 		if (ed == NULL) {
188 			printf("%s: no endpoint descriptor for %d\n",
189 				USBDEVNAME(sc->sc_dev), i);
190 			sc->sc_dying = 1;
191 			return;
192 		}
193 
194 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
195 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
196 			sc->sc_intr_number = ed->bEndpointAddress;
197 			sc->sc_isize = UGETW(ed->wMaxPacketSize);
198 		}
199 	}
200 
201 	if (sc->sc_intr_number== -1) {
202 		printf("%s: Could not find interrupt in\n",
203 			USBDEVNAME(sc->sc_dev));
204 		sc->sc_dying = 1;
205 		return;
206 	}
207 
208 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
209 			   USBDEV(sc->sc_dev));
210 
211 	a.accessops = &uts_accessops;
212 	a.accesscookie = sc;
213 
214 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
215 }
216 
217 int
218 uts_detach(struct device *self, int flags)
219 {
220 	struct uts_softc *sc = (struct uts_softc *)self;
221 	int rv = 0;
222 
223 	if (sc->sc_intr_pipe != NULL) {
224 		usbd_abort_pipe(sc->sc_intr_pipe);
225 		usbd_close_pipe(sc->sc_intr_pipe);
226 		sc->sc_intr_pipe = NULL;
227 	}
228 
229 	sc->sc_dying = 1;
230 
231 	if (sc->sc_wsmousedev != NULL) {
232 		rv = config_detach(sc->sc_wsmousedev, flags);
233 		sc->sc_wsmousedev = NULL;
234 	}
235 
236 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
237 			   USBDEV(sc->sc_dev));
238 
239 	return (rv);
240 }
241 
242 int
243 uts_activate(device_ptr_t self, enum devact act)
244 {
245 	struct uts_softc *sc = (struct uts_softc *)self;
246 	int rv = 0;
247 
248 	switch (act) {
249 	case DVACT_ACTIVATE:
250 		break;
251 
252 	case DVACT_DEACTIVATE:
253 		if (sc->sc_wsmousedev != NULL)
254 			rv = config_deactivate(sc->sc_wsmousedev);
255 		sc->sc_dying = 1;
256 		break;
257 	}
258 
259 	return (rv);
260 }
261 
262 Static int
263 uts_enable(void *v)
264 {
265 	struct uts_softc *sc = v;
266 	int err;
267 
268 	if (sc->sc_dying)
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);
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 Static 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);
311 		sc->sc_ibuf = NULL;
312 	}
313 
314 	sc->sc_enabled = 0;
315 }
316 
317 Static 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->resx < 32768 && wsmc->resy < 32768 &&
335 		    wsmc->swapxy >= 0 && wsmc->swapxy <= 1 &&
336 		    wsmc->samplelen >= 0 && wsmc->samplelen <= 1))
337 			return (EINVAL);
338 
339 		sc->sc_tsscale.minx = wsmc->minx;
340 		sc->sc_tsscale.maxx = wsmc->maxx;
341 		sc->sc_tsscale.miny = wsmc->miny;
342 		sc->sc_tsscale.maxy = wsmc->maxy;
343 		sc->sc_tsscale.swapxy = wsmc->swapxy;
344 		sc->sc_tsscale.resx = wsmc->resx;
345 		sc->sc_tsscale.resy = wsmc->resy;
346 		sc->sc_rawmode = wsmc->samplelen;
347 		break;
348 	case WSMOUSEIO_GCALIBCOORDS:
349 		wsmc->minx = sc->sc_tsscale.minx;
350 		wsmc->maxx = sc->sc_tsscale.maxx;
351 		wsmc->miny = sc->sc_tsscale.miny;
352 		wsmc->maxy = sc->sc_tsscale.maxy;
353 		wsmc->swapxy = sc->sc_tsscale.swapxy;
354 		wsmc->resx = sc->sc_tsscale.resx;
355 		wsmc->resy = sc->sc_tsscale.resy;
356 		wsmc->samplelen = sc->sc_rawmode;
357 		break;
358 	case WSMOUSEIO_GTYPE:
359 		*(u_int *)data = WSMOUSE_TYPE_TPANEL;
360 		break;
361 	default:
362 		error = ENOTTY;
363 		break;
364 	}
365 
366 	return (error);
367 }
368 
369 struct uts_pos
370 uts_get_pos(usbd_private_handle addr, struct uts_pos tp)
371 {
372 	struct uts_softc *sc = addr;
373 	u_char *p = sc->sc_ibuf;
374 	int down, x, y;
375 
376 	switch (sc->sc_product) {
377 	case USB_PRODUCT_FTDI_ITM_TOUCH:
378 		down = (~p[7] & 0x20);
379 		x = ((p[0] & 0x1f) << 7) | (p[3] & 0x7f);
380 		/* Invert the Y coordinate */
381 		y = 0x0fff - abs(((p[1] & 0x1f) << 7) | (p[4] & 0x7f));
382 		sc->sc_pkts = 0x8;
383 		break;
384 	case USB_PRODUCT_EGALAX_TPANEL:
385 	case USB_PRODUCT_EGALAX_TPANEL2:
386 		down = (p[0] & 0x01);
387 		/* Invert the X coordiate */
388 		x = 0x07ff - abs(((p[3] & 0x0f) << 7) | (p[4] & 0x7f));
389 		y = ((p[1] & 0x0f) << 7) | (p[2] & 0x7f);
390 		sc->sc_pkts = 0x5;
391 		break;
392 	}
393 
394 	DPRINTF(("%s: down = 0x%x, sc->sc_pkts = 0x%x\n",
395 	    USBDEVNAME(sc->sc_dev), down, sc->sc_pkts));
396 
397 	/* x/y values are not reliable if there is no pressure */
398 	if (down) {
399 		if (sc->sc_tsscale.swapxy) {	/* Swap X/Y-Axis */
400 			tp.y = x;
401 			tp.x = y;
402 		} else {
403 			tp.x = x;
404 			tp.y = y;
405 		}
406 
407 		if (!sc->sc_rawmode) {
408 			/* Scale down to the screen resolution. */
409 			tp.x = ((tp.x - sc->sc_tsscale.minx) *
410 			    sc->sc_tsscale.resx) /
411 			    (sc->sc_tsscale.maxx - sc->sc_tsscale.minx);
412 			tp.y = ((tp.y - sc->sc_tsscale.miny) *
413 			    sc->sc_tsscale.resy) /
414 			    (sc->sc_tsscale.maxy - sc->sc_tsscale.miny);
415 		}
416 		tp.z = 1;
417 	} else {
418 		tp.x = sc->sc_oldx;
419 		tp.y = sc->sc_oldy;
420 		tp.z = 0;
421 	}
422 
423 	return (tp);
424 }
425 
426 Static void
427 uts_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
428 {
429 	struct uts_softc *sc = addr;
430 	u_int32_t len;
431 	int s;
432 	struct uts_pos tp;
433 
434 	usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
435 
436 	s = spltty();
437 
438 	if (status == USBD_CANCELLED)
439 		return;
440 
441 	if (status != USBD_NORMAL_COMPLETION) {
442 		printf("%s: status %d\n", USBDEVNAME(sc->sc_dev), status);
443 		usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
444 		return;
445 	}
446 
447 	tp = uts_get_pos(sc, tp);
448 
449 	if (len != sc->sc_pkts) {
450 		DPRINTF(("%s: bad input length %d != %d\n",
451 			USBDEVNAME(sc->sc_dev), len, sc->sc_isize));
452 		return;
453 	}
454 
455 	DPRINTF(("%s: tp.z = %d, tp.x = %d, tp.y = %d\n",
456 	    USBDEVNAME(sc->sc_dev), tp.z, tp.x, tp.y));
457 
458 	wsmouse_input(sc->sc_wsmousedev, tp.z, tp.x, tp.y, 0, 0,
459 		WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y |
460 		WSMOUSE_INPUT_ABSOLUTE_Z);
461 	sc->sc_oldy = tp.y;
462 	sc->sc_oldx = tp.x;
463 
464 	splx(s);
465 }
466