uep.c revision 1.15 1 1.15 dyoung /* $NetBSD: uep.c,v 1.15 2010/11/03 22:34:23 dyoung Exp $ */
2 1.1 tsarna
3 1.1 tsarna /*
4 1.1 tsarna * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 tsarna * All rights reserved.
6 1.1 tsarna *
7 1.1 tsarna * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsarna * by Tyler C. Sarna (tsarna (at) netbsd.org).
9 1.1 tsarna *
10 1.1 tsarna * Redistribution and use in source and binary forms, with or without
11 1.1 tsarna * modification, are permitted provided that the following conditions
12 1.1 tsarna * are met:
13 1.1 tsarna * 1. Redistributions of source code must retain the above copyright
14 1.1 tsarna * notice, this list of conditions and the following disclaimer.
15 1.1 tsarna * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsarna * notice, this list of conditions and the following disclaimer in the
17 1.1 tsarna * documentation and/or other materials provided with the distribution.
18 1.1 tsarna *
19 1.1 tsarna * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tsarna * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tsarna * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tsarna * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tsarna * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tsarna * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tsarna * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tsarna * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tsarna * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tsarna * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tsarna * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tsarna */
31 1.1 tsarna
32 1.2 tsarna /*
33 1.2 tsarna * eGalax USB touchpanel controller driver.
34 1.2 tsarna *
35 1.2 tsarna * For Programming Documentation, see:
36 1.2 tsarna *
37 1.2 tsarna * http://www.egalax.com/SoftwareProgrammingGuide_1.1.pdf
38 1.2 tsarna */
39 1.3 perry
40 1.1 tsarna #include <sys/cdefs.h>
41 1.15 dyoung __KERNEL_RCSID(0, "$NetBSD: uep.c,v 1.15 2010/11/03 22:34:23 dyoung Exp $");
42 1.1 tsarna
43 1.1 tsarna #include <sys/param.h>
44 1.1 tsarna #include <sys/systm.h>
45 1.1 tsarna #include <sys/kernel.h>
46 1.1 tsarna #include <sys/malloc.h>
47 1.1 tsarna #include <sys/device.h>
48 1.1 tsarna #include <sys/ioctl.h>
49 1.1 tsarna #include <sys/vnode.h>
50 1.1 tsarna
51 1.1 tsarna #include <dev/usb/usb.h>
52 1.1 tsarna #include <dev/usb/usbdi.h>
53 1.1 tsarna #include <dev/usb/usbdi_util.h>
54 1.1 tsarna #include <dev/usb/usbdevs.h>
55 1.1 tsarna #include <dev/usb/usb_quirks.h>
56 1.1 tsarna
57 1.1 tsarna #include <dev/wscons/wsconsio.h>
58 1.1 tsarna #include <dev/wscons/wsmousevar.h>
59 1.2 tsarna #include <dev/wscons/tpcalibvar.h>
60 1.2 tsarna
61 1.2 tsarna #define UIDSTR "eGalax USB SN000000"
62 1.1 tsarna
63 1.1 tsarna struct uep_softc {
64 1.15 dyoung device_t sc_dev;
65 1.1 tsarna usbd_device_handle sc_udev; /* device */
66 1.1 tsarna usbd_interface_handle sc_iface; /* interface */
67 1.1 tsarna int sc_iface_number;
68 1.1 tsarna
69 1.1 tsarna int sc_intr_number; /* interrupt number */
70 1.1 tsarna usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
71 1.1 tsarna u_char *sc_ibuf;
72 1.1 tsarna int sc_isize;
73 1.1 tsarna
74 1.15 dyoung device_t sc_wsmousedev; /* wsmouse device */
75 1.2 tsarna struct tpcalib_softc sc_tpcalib; /* calibration */
76 1.1 tsarna
77 1.1 tsarna u_char sc_enabled;
78 1.1 tsarna u_char sc_dying;
79 1.1 tsarna };
80 1.1 tsarna
81 1.2 tsarna static struct wsmouse_calibcoords default_calib = {
82 1.6 christos .minx = 0,
83 1.6 christos .miny = 0,
84 1.6 christos .maxx = 2047,
85 1.6 christos .maxy = 2047,
86 1.6 christos .samplelen = WSMOUSE_CALIBCOORDS_RESET,
87 1.2 tsarna };
88 1.2 tsarna
89 1.1 tsarna Static void uep_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
90 1.1 tsarna
91 1.1 tsarna Static int uep_enable(void *);
92 1.1 tsarna Static void uep_disable(void *);
93 1.8 christos Static int uep_ioctl(void *, u_long, void *, int, struct lwp *);
94 1.1 tsarna
95 1.1 tsarna const struct wsmouse_accessops uep_accessops = {
96 1.1 tsarna uep_enable,
97 1.1 tsarna uep_ioctl,
98 1.1 tsarna uep_disable,
99 1.1 tsarna };
100 1.1 tsarna
101 1.12 cube int uep_match(device_t, cfdata_t, void *);
102 1.10 dyoung void uep_attach(device_t, device_t, void *);
103 1.10 dyoung void uep_childdet(device_t, device_t);
104 1.10 dyoung int uep_detach(device_t, int);
105 1.10 dyoung int uep_activate(device_t, enum devact);
106 1.10 dyoung extern struct cfdriver uep_cd;
107 1.12 cube CFATTACH_DECL2_NEW(uep, sizeof(struct uep_softc), uep_match, uep_attach,
108 1.10 dyoung uep_detach, uep_activate, NULL, uep_childdet);
109 1.1 tsarna
110 1.15 dyoung int
111 1.15 dyoung uep_match(device_t parent, cfdata_t match, void *aux)
112 1.1 tsarna {
113 1.15 dyoung struct usb_attach_arg *uaa = aux;
114 1.1 tsarna
115 1.1 tsarna if ((uaa->vendor == USB_VENDOR_EGALAX) && (
116 1.1 tsarna (uaa->product == USB_PRODUCT_EGALAX_TPANEL)
117 1.1 tsarna || (uaa->product == USB_PRODUCT_EGALAX_TPANEL2)
118 1.1 tsarna ))
119 1.1 tsarna return UMATCH_VENDOR_PRODUCT;
120 1.1 tsarna
121 1.1 tsarna if ((uaa->vendor == USB_VENDOR_EGALAX2)
122 1.1 tsarna && (uaa->product == USB_PRODUCT_EGALAX2_TPANEL))
123 1.1 tsarna return UMATCH_VENDOR_PRODUCT;
124 1.1 tsarna
125 1.1 tsarna
126 1.1 tsarna return UMATCH_NONE;
127 1.1 tsarna }
128 1.1 tsarna
129 1.15 dyoung void
130 1.15 dyoung uep_attach(device_t parent, device_t self, void *aux)
131 1.1 tsarna {
132 1.15 dyoung struct uep_softc *sc = device_private(self);
133 1.15 dyoung struct usb_attach_arg *uaa = aux;
134 1.1 tsarna usbd_device_handle dev = uaa->device;
135 1.1 tsarna usb_config_descriptor_t *cdesc;
136 1.1 tsarna usb_interface_descriptor_t *id;
137 1.1 tsarna usb_endpoint_descriptor_t *ed;
138 1.1 tsarna struct wsmousedev_attach_args a;
139 1.4 augustss char *devinfop;
140 1.1 tsarna usbd_status err;
141 1.1 tsarna int i, found;
142 1.1 tsarna
143 1.12 cube sc->sc_dev = self;
144 1.13 plunky
145 1.13 plunky aprint_naive("\n");
146 1.13 plunky aprint_normal("\n");
147 1.13 plunky
148 1.4 augustss devinfop = usbd_devinfo_alloc(dev, 0);
149 1.12 cube aprint_normal_dev(self, "%s\n", devinfop);
150 1.4 augustss usbd_devinfo_free(devinfop);
151 1.1 tsarna
152 1.1 tsarna sc->sc_udev = dev;
153 1.1 tsarna sc->sc_intr_number = -1;
154 1.1 tsarna sc->sc_intr_pipe = NULL;
155 1.1 tsarna sc->sc_enabled = sc->sc_isize = 0;
156 1.1 tsarna
157 1.1 tsarna /* Move the device into the configured state. */
158 1.1 tsarna err = usbd_set_config_index(dev, 0, 1);
159 1.1 tsarna if (err) {
160 1.12 cube aprint_error("\n%s: failed to set configuration, err=%s\n",
161 1.15 dyoung device_xname(sc->sc_dev), usbd_errstr(err));
162 1.1 tsarna sc->sc_dying = 1;
163 1.15 dyoung return;
164 1.1 tsarna }
165 1.1 tsarna
166 1.1 tsarna /* get the config descriptor */
167 1.1 tsarna cdesc = usbd_get_config_descriptor(sc->sc_udev);
168 1.1 tsarna if (cdesc == NULL) {
169 1.12 cube aprint_error_dev(self,
170 1.12 cube "failed to get configuration descriptor\n");
171 1.1 tsarna sc->sc_dying = 1;
172 1.15 dyoung return;
173 1.1 tsarna }
174 1.1 tsarna
175 1.1 tsarna /* get the interface */
176 1.1 tsarna err = usbd_device2interface_handle(dev, 0, &sc->sc_iface);
177 1.1 tsarna if (err) {
178 1.12 cube aprint_error("\n%s: failed to get interface, err=%s\n",
179 1.15 dyoung device_xname(sc->sc_dev), usbd_errstr(err));
180 1.1 tsarna sc->sc_dying = 1;
181 1.15 dyoung return;
182 1.1 tsarna }
183 1.1 tsarna
184 1.1 tsarna /* Find the interrupt endpoint */
185 1.1 tsarna id = usbd_get_interface_descriptor(sc->sc_iface);
186 1.1 tsarna sc->sc_iface_number = id->bInterfaceNumber;
187 1.1 tsarna found = 0;
188 1.1 tsarna
189 1.1 tsarna for (i = 0; i < id->bNumEndpoints; i++) {
190 1.1 tsarna ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
191 1.1 tsarna if (ed == NULL) {
192 1.12 cube aprint_error_dev(self,
193 1.12 cube "no endpoint descriptor for %d\n", i);
194 1.1 tsarna sc->sc_dying = 1;
195 1.15 dyoung return;
196 1.1 tsarna }
197 1.1 tsarna
198 1.1 tsarna if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
199 1.1 tsarna UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
200 1.1 tsarna sc->sc_intr_number = ed->bEndpointAddress;
201 1.1 tsarna sc->sc_isize = UGETW(ed->wMaxPacketSize);
202 1.1 tsarna }
203 1.1 tsarna }
204 1.1 tsarna
205 1.1 tsarna if (sc->sc_intr_number== -1) {
206 1.12 cube aprint_error_dev(self, "Could not find interrupt in\n");
207 1.1 tsarna sc->sc_dying = 1;
208 1.15 dyoung return;
209 1.1 tsarna }
210 1.1 tsarna
211 1.1 tsarna usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
212 1.15 dyoung sc->sc_dev);
213 1.1 tsarna
214 1.1 tsarna a.accessops = &uep_accessops;
215 1.1 tsarna a.accesscookie = sc;
216 1.1 tsarna
217 1.1 tsarna sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
218 1.3 perry
219 1.2 tsarna tpcalib_init(&sc->sc_tpcalib);
220 1.2 tsarna tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
221 1.8 christos (void *)&default_calib, 0, 0);
222 1.1 tsarna
223 1.15 dyoung return;
224 1.1 tsarna }
225 1.1 tsarna
226 1.15 dyoung int
227 1.15 dyoung uep_detach(device_t self, int flags)
228 1.1 tsarna {
229 1.15 dyoung struct uep_softc *sc = device_private(self);
230 1.1 tsarna int rv = 0;
231 1.1 tsarna
232 1.1 tsarna if (sc->sc_intr_pipe != NULL) {
233 1.1 tsarna usbd_abort_pipe(sc->sc_intr_pipe);
234 1.1 tsarna usbd_close_pipe(sc->sc_intr_pipe);
235 1.1 tsarna sc->sc_intr_pipe = NULL;
236 1.1 tsarna }
237 1.1 tsarna
238 1.1 tsarna sc->sc_dying = 1;
239 1.1 tsarna
240 1.2 tsarna /* save current calib as defaults */
241 1.2 tsarna default_calib = sc->sc_tpcalib.sc_saved;
242 1.3 perry
243 1.10 dyoung if (sc->sc_wsmousedev != NULL)
244 1.1 tsarna rv = config_detach(sc->sc_wsmousedev, flags);
245 1.1 tsarna
246 1.1 tsarna usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
247 1.15 dyoung sc->sc_dev);
248 1.1 tsarna
249 1.1 tsarna return rv;
250 1.1 tsarna }
251 1.1 tsarna
252 1.10 dyoung void
253 1.10 dyoung uep_childdet(device_t self, device_t child)
254 1.10 dyoung {
255 1.10 dyoung struct uep_softc *sc = device_private(self);
256 1.10 dyoung
257 1.10 dyoung KASSERT(sc->sc_wsmousedev == child);
258 1.10 dyoung sc->sc_wsmousedev = NULL;
259 1.10 dyoung }
260 1.10 dyoung
261 1.1 tsarna int
262 1.10 dyoung uep_activate(device_t self, enum devact act)
263 1.1 tsarna {
264 1.10 dyoung struct uep_softc *sc = device_private(self);
265 1.1 tsarna
266 1.1 tsarna switch (act) {
267 1.1 tsarna case DVACT_DEACTIVATE:
268 1.1 tsarna sc->sc_dying = 1;
269 1.14 dyoung return 0;
270 1.14 dyoung default:
271 1.14 dyoung return EOPNOTSUPP;
272 1.1 tsarna }
273 1.1 tsarna }
274 1.1 tsarna
275 1.1 tsarna Static int
276 1.1 tsarna uep_enable(void *v)
277 1.1 tsarna {
278 1.1 tsarna struct uep_softc *sc = v;
279 1.1 tsarna int err;
280 1.1 tsarna
281 1.1 tsarna if (sc->sc_dying)
282 1.1 tsarna return EIO;
283 1.1 tsarna
284 1.1 tsarna if (sc->sc_enabled)
285 1.1 tsarna return EBUSY;
286 1.1 tsarna
287 1.1 tsarna if (sc->sc_isize == 0)
288 1.1 tsarna return 0;
289 1.1 tsarna sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
290 1.1 tsarna err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
291 1.1 tsarna USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf,
292 1.1 tsarna sc->sc_isize, uep_intr, USBD_DEFAULT_INTERVAL);
293 1.1 tsarna if (err) {
294 1.1 tsarna free(sc->sc_ibuf, M_USBDEV);
295 1.1 tsarna sc->sc_intr_pipe = NULL;
296 1.1 tsarna return EIO;
297 1.1 tsarna }
298 1.1 tsarna
299 1.1 tsarna sc->sc_enabled = 1;
300 1.1 tsarna
301 1.1 tsarna return 0;
302 1.1 tsarna }
303 1.1 tsarna
304 1.1 tsarna Static void
305 1.1 tsarna uep_disable(void *v)
306 1.1 tsarna {
307 1.1 tsarna struct uep_softc *sc = v;
308 1.1 tsarna
309 1.1 tsarna if (!sc->sc_enabled) {
310 1.1 tsarna printf("uep_disable: already disabled!\n");
311 1.1 tsarna return;
312 1.1 tsarna }
313 1.1 tsarna
314 1.1 tsarna /* Disable interrupts. */
315 1.1 tsarna if (sc->sc_intr_pipe != NULL) {
316 1.1 tsarna usbd_abort_pipe(sc->sc_intr_pipe);
317 1.1 tsarna usbd_close_pipe(sc->sc_intr_pipe);
318 1.1 tsarna sc->sc_intr_pipe = NULL;
319 1.1 tsarna }
320 1.1 tsarna
321 1.1 tsarna if (sc->sc_ibuf != NULL) {
322 1.1 tsarna free(sc->sc_ibuf, M_USBDEV);
323 1.1 tsarna sc->sc_ibuf = NULL;
324 1.1 tsarna }
325 1.1 tsarna
326 1.1 tsarna sc->sc_enabled = 0;
327 1.1 tsarna }
328 1.1 tsarna
329 1.1 tsarna Static int
330 1.8 christos uep_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
331 1.1 tsarna {
332 1.2 tsarna struct uep_softc *sc = v;
333 1.2 tsarna struct wsmouse_id *id;
334 1.2 tsarna
335 1.1 tsarna switch (cmd) {
336 1.1 tsarna case WSMOUSEIO_GTYPE:
337 1.1 tsarna *(u_int *)data = WSMOUSE_TYPE_TPANEL;
338 1.2 tsarna return 0;
339 1.2 tsarna
340 1.2 tsarna case WSMOUSEIO_GETID:
341 1.2 tsarna /*
342 1.2 tsarna * return unique ID string
343 1.2 tsarna * "<vendor> <model> <serial number>"
344 1.2 tsarna * unfortunately we have no serial number...
345 1.2 tsarna */
346 1.2 tsarna id = (struct wsmouse_id *)data;
347 1.2 tsarna if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
348 1.2 tsarna return EINVAL;
349 1.3 perry
350 1.2 tsarna strcpy(id->data, UIDSTR);
351 1.2 tsarna id->length = strlen(UIDSTR);
352 1.2 tsarna return 0;
353 1.2 tsarna
354 1.2 tsarna case WSMOUSEIO_SCALIBCOORDS:
355 1.2 tsarna case WSMOUSEIO_GCALIBCOORDS:
356 1.5 christos return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
357 1.1 tsarna }
358 1.1 tsarna
359 1.1 tsarna return EPASSTHROUGH;
360 1.1 tsarna }
361 1.1 tsarna
362 1.1 tsarna void
363 1.1 tsarna uep_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
364 1.1 tsarna {
365 1.1 tsarna struct uep_softc *sc = addr;
366 1.1 tsarna u_char *p = sc->sc_ibuf;
367 1.1 tsarna u_int32_t len;
368 1.1 tsarna int x = 0, y = 0, s;
369 1.1 tsarna
370 1.1 tsarna usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
371 1.1 tsarna
372 1.1 tsarna if (status == USBD_CANCELLED)
373 1.1 tsarna return;
374 1.1 tsarna
375 1.1 tsarna if (status != USBD_NORMAL_COMPLETION) {
376 1.12 cube aprint_error_dev(sc->sc_dev, "status %d\n", status);
377 1.1 tsarna usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
378 1.1 tsarna return;
379 1.1 tsarna }
380 1.1 tsarna
381 1.1 tsarna if (len != 5) {
382 1.1 tsarna #if 0
383 1.1 tsarna printf("%s: bad input length %d != %d\n",
384 1.15 dyoung device_xname(sc->sc_dev), len, sc->sc_isize);
385 1.1 tsarna #endif
386 1.1 tsarna return;
387 1.1 tsarna }
388 1.1 tsarna
389 1.1 tsarna if ((p[0] & 0xFE) != 0x80) {
390 1.12 cube aprint_error_dev(sc->sc_dev,
391 1.12 cube "bad input packet format\n");
392 1.1 tsarna return;
393 1.1 tsarna }
394 1.1 tsarna
395 1.1 tsarna if (sc->sc_wsmousedev != NULL) {
396 1.2 tsarna /*
397 1.2 tsarna * Packet format is 5 bytes:
398 1.2 tsarna *
399 1.2 tsarna * 1000000T
400 1.2 tsarna * 0000AAAA
401 1.2 tsarna * 0AAAAAAA
402 1.2 tsarna * 0000BBBB
403 1.2 tsarna * 0BBBBBBB
404 1.2 tsarna *
405 1.3 perry * T: 1=touched 0=not touched
406 1.2 tsarna * A: bits of axis A position, MSB to LSB
407 1.2 tsarna * B: bits of axis B position, MSB to LSB
408 1.2 tsarna *
409 1.2 tsarna * For the unit I have, A = Y and B = X.
410 1.2 tsarna * I don't know if units exist with A=X and B=Y,
411 1.2 tsarna * if so we'll cross that bridge when we come to it.
412 1.2 tsarna *
413 1.2 tsarna * The controller sends a stream of T=1 events while the
414 1.2 tsarna * panel is touched, followed by a single T=0 event.
415 1.3 perry *
416 1.2 tsarna */
417 1.2 tsarna
418 1.1 tsarna x = (p[3] << 7) | p[4];
419 1.1 tsarna y = (p[1] << 7) | p[2];
420 1.3 perry
421 1.2 tsarna tpcalib_trans(&sc->sc_tpcalib, x, y, &x, &y);
422 1.1 tsarna
423 1.1 tsarna s = spltty();
424 1.7 plunky wsmouse_input(sc->sc_wsmousedev, p[0] & 0x01, x, y, 0, 0,
425 1.1 tsarna WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
426 1.1 tsarna splx(s);
427 1.1 tsarna }
428 1.1 tsarna }
429