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