uep.c revision 1.10.6.1 1 1.10.6.1 mjf /* $NetBSD: uep.c,v 1.10.6.1 2008/06/02 13:23:54 mjf 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.10.6.1 mjf __KERNEL_RCSID(0, "$NetBSD: uep.c,v 1.10.6.1 2008/06/02 13:23:54 mjf 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.1 tsarna USBBASEDEVICE 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.1 tsarna device_ptr_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.10.6.1 mjf 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.10.6.1 mjf 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.1 tsarna USB_MATCH(uep)
111 1.1 tsarna {
112 1.1 tsarna USB_MATCH_START(uep, uaa);
113 1.1 tsarna
114 1.1 tsarna if ((uaa->vendor == USB_VENDOR_EGALAX) && (
115 1.1 tsarna (uaa->product == USB_PRODUCT_EGALAX_TPANEL)
116 1.1 tsarna || (uaa->product == USB_PRODUCT_EGALAX_TPANEL2)
117 1.1 tsarna ))
118 1.1 tsarna return UMATCH_VENDOR_PRODUCT;
119 1.1 tsarna
120 1.1 tsarna if ((uaa->vendor == USB_VENDOR_EGALAX2)
121 1.1 tsarna && (uaa->product == USB_PRODUCT_EGALAX2_TPANEL))
122 1.1 tsarna return UMATCH_VENDOR_PRODUCT;
123 1.1 tsarna
124 1.1 tsarna
125 1.1 tsarna return UMATCH_NONE;
126 1.1 tsarna }
127 1.1 tsarna
128 1.1 tsarna USB_ATTACH(uep)
129 1.1 tsarna {
130 1.1 tsarna USB_ATTACH_START(uep, sc, uaa);
131 1.1 tsarna usbd_device_handle dev = uaa->device;
132 1.1 tsarna usb_config_descriptor_t *cdesc;
133 1.1 tsarna usb_interface_descriptor_t *id;
134 1.1 tsarna usb_endpoint_descriptor_t *ed;
135 1.1 tsarna struct wsmousedev_attach_args a;
136 1.4 augustss char *devinfop;
137 1.1 tsarna usbd_status err;
138 1.1 tsarna int i, found;
139 1.1 tsarna
140 1.10.6.1 mjf sc->sc_dev = self;
141 1.4 augustss devinfop = usbd_devinfo_alloc(dev, 0);
142 1.1 tsarna USB_ATTACH_SETUP;
143 1.10.6.1 mjf aprint_normal_dev(self, "%s\n", devinfop);
144 1.4 augustss usbd_devinfo_free(devinfop);
145 1.1 tsarna
146 1.1 tsarna sc->sc_udev = dev;
147 1.1 tsarna sc->sc_intr_number = -1;
148 1.1 tsarna sc->sc_intr_pipe = NULL;
149 1.1 tsarna sc->sc_enabled = sc->sc_isize = 0;
150 1.1 tsarna
151 1.1 tsarna /* Move the device into the configured state. */
152 1.1 tsarna err = usbd_set_config_index(dev, 0, 1);
153 1.1 tsarna if (err) {
154 1.10.6.1 mjf aprint_error("\n%s: failed to set configuration, err=%s\n",
155 1.1 tsarna USBDEVNAME(sc->sc_dev), usbd_errstr(err));
156 1.1 tsarna sc->sc_dying = 1;
157 1.1 tsarna USB_ATTACH_ERROR_RETURN;
158 1.1 tsarna }
159 1.1 tsarna
160 1.1 tsarna /* get the config descriptor */
161 1.1 tsarna cdesc = usbd_get_config_descriptor(sc->sc_udev);
162 1.1 tsarna if (cdesc == NULL) {
163 1.10.6.1 mjf aprint_error_dev(self,
164 1.10.6.1 mjf "failed to get configuration descriptor\n");
165 1.1 tsarna sc->sc_dying = 1;
166 1.1 tsarna USB_ATTACH_ERROR_RETURN;
167 1.1 tsarna }
168 1.1 tsarna
169 1.1 tsarna /* get the interface */
170 1.1 tsarna err = usbd_device2interface_handle(dev, 0, &sc->sc_iface);
171 1.1 tsarna if (err) {
172 1.10.6.1 mjf aprint_error("\n%s: failed to get interface, err=%s\n",
173 1.1 tsarna USBDEVNAME(sc->sc_dev), usbd_errstr(err));
174 1.1 tsarna sc->sc_dying = 1;
175 1.1 tsarna USB_ATTACH_ERROR_RETURN;
176 1.1 tsarna }
177 1.1 tsarna
178 1.1 tsarna /* Find the interrupt endpoint */
179 1.1 tsarna id = usbd_get_interface_descriptor(sc->sc_iface);
180 1.1 tsarna sc->sc_iface_number = id->bInterfaceNumber;
181 1.1 tsarna found = 0;
182 1.1 tsarna
183 1.1 tsarna for (i = 0; i < id->bNumEndpoints; i++) {
184 1.1 tsarna ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
185 1.1 tsarna if (ed == NULL) {
186 1.10.6.1 mjf aprint_error_dev(self,
187 1.10.6.1 mjf "no endpoint descriptor for %d\n", i);
188 1.1 tsarna sc->sc_dying = 1;
189 1.1 tsarna USB_ATTACH_ERROR_RETURN;
190 1.1 tsarna }
191 1.1 tsarna
192 1.1 tsarna if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
193 1.1 tsarna UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
194 1.1 tsarna sc->sc_intr_number = ed->bEndpointAddress;
195 1.1 tsarna sc->sc_isize = UGETW(ed->wMaxPacketSize);
196 1.1 tsarna }
197 1.1 tsarna }
198 1.1 tsarna
199 1.1 tsarna if (sc->sc_intr_number== -1) {
200 1.10.6.1 mjf aprint_error_dev(self, "Could not find interrupt in\n");
201 1.1 tsarna sc->sc_dying = 1;
202 1.1 tsarna USB_ATTACH_ERROR_RETURN;
203 1.1 tsarna }
204 1.1 tsarna
205 1.1 tsarna usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
206 1.1 tsarna USBDEV(sc->sc_dev));
207 1.1 tsarna
208 1.1 tsarna a.accessops = &uep_accessops;
209 1.1 tsarna a.accesscookie = sc;
210 1.1 tsarna
211 1.1 tsarna sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
212 1.3 perry
213 1.2 tsarna tpcalib_init(&sc->sc_tpcalib);
214 1.2 tsarna tpcalib_ioctl(&sc->sc_tpcalib, WSMOUSEIO_SCALIBCOORDS,
215 1.8 christos (void *)&default_calib, 0, 0);
216 1.1 tsarna
217 1.1 tsarna USB_ATTACH_SUCCESS_RETURN;
218 1.1 tsarna }
219 1.1 tsarna
220 1.1 tsarna USB_DETACH(uep)
221 1.1 tsarna {
222 1.1 tsarna USB_DETACH_START(uep, sc);
223 1.1 tsarna int rv = 0;
224 1.1 tsarna
225 1.1 tsarna if (sc->sc_intr_pipe != NULL) {
226 1.1 tsarna usbd_abort_pipe(sc->sc_intr_pipe);
227 1.1 tsarna usbd_close_pipe(sc->sc_intr_pipe);
228 1.1 tsarna sc->sc_intr_pipe = NULL;
229 1.1 tsarna }
230 1.1 tsarna
231 1.1 tsarna sc->sc_dying = 1;
232 1.1 tsarna
233 1.2 tsarna /* save current calib as defaults */
234 1.2 tsarna default_calib = sc->sc_tpcalib.sc_saved;
235 1.3 perry
236 1.10 dyoung if (sc->sc_wsmousedev != NULL)
237 1.1 tsarna rv = config_detach(sc->sc_wsmousedev, flags);
238 1.1 tsarna
239 1.1 tsarna usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
240 1.1 tsarna USBDEV(sc->sc_dev));
241 1.1 tsarna
242 1.1 tsarna return rv;
243 1.1 tsarna }
244 1.1 tsarna
245 1.10 dyoung void
246 1.10 dyoung uep_childdet(device_t self, device_t child)
247 1.10 dyoung {
248 1.10 dyoung struct uep_softc *sc = device_private(self);
249 1.10 dyoung
250 1.10 dyoung KASSERT(sc->sc_wsmousedev == child);
251 1.10 dyoung sc->sc_wsmousedev = NULL;
252 1.10 dyoung }
253 1.10 dyoung
254 1.1 tsarna int
255 1.10 dyoung uep_activate(device_t self, enum devact act)
256 1.1 tsarna {
257 1.10 dyoung struct uep_softc *sc = device_private(self);
258 1.1 tsarna int rv = 0;
259 1.1 tsarna
260 1.1 tsarna switch (act) {
261 1.1 tsarna case DVACT_ACTIVATE:
262 1.1 tsarna return EOPNOTSUPP;
263 1.1 tsarna
264 1.1 tsarna case DVACT_DEACTIVATE:
265 1.1 tsarna if (sc->sc_wsmousedev != NULL)
266 1.1 tsarna rv = config_deactivate(sc->sc_wsmousedev);
267 1.1 tsarna sc->sc_dying = 1;
268 1.1 tsarna break;
269 1.1 tsarna }
270 1.1 tsarna
271 1.1 tsarna return rv;
272 1.1 tsarna }
273 1.1 tsarna
274 1.1 tsarna Static int
275 1.1 tsarna uep_enable(void *v)
276 1.1 tsarna {
277 1.1 tsarna struct uep_softc *sc = v;
278 1.1 tsarna int err;
279 1.1 tsarna
280 1.1 tsarna if (sc->sc_dying)
281 1.1 tsarna return EIO;
282 1.1 tsarna
283 1.1 tsarna if (sc->sc_enabled)
284 1.1 tsarna return EBUSY;
285 1.1 tsarna
286 1.1 tsarna if (sc->sc_isize == 0)
287 1.1 tsarna return 0;
288 1.1 tsarna sc->sc_ibuf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
289 1.1 tsarna err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
290 1.1 tsarna USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc, sc->sc_ibuf,
291 1.1 tsarna sc->sc_isize, uep_intr, USBD_DEFAULT_INTERVAL);
292 1.1 tsarna if (err) {
293 1.1 tsarna free(sc->sc_ibuf, M_USBDEV);
294 1.1 tsarna sc->sc_intr_pipe = NULL;
295 1.1 tsarna return EIO;
296 1.1 tsarna }
297 1.1 tsarna
298 1.1 tsarna sc->sc_enabled = 1;
299 1.1 tsarna
300 1.1 tsarna return 0;
301 1.1 tsarna }
302 1.1 tsarna
303 1.1 tsarna Static void
304 1.1 tsarna uep_disable(void *v)
305 1.1 tsarna {
306 1.1 tsarna struct uep_softc *sc = v;
307 1.1 tsarna
308 1.1 tsarna if (!sc->sc_enabled) {
309 1.1 tsarna printf("uep_disable: already disabled!\n");
310 1.1 tsarna return;
311 1.1 tsarna }
312 1.1 tsarna
313 1.1 tsarna /* Disable interrupts. */
314 1.1 tsarna if (sc->sc_intr_pipe != NULL) {
315 1.1 tsarna usbd_abort_pipe(sc->sc_intr_pipe);
316 1.1 tsarna usbd_close_pipe(sc->sc_intr_pipe);
317 1.1 tsarna sc->sc_intr_pipe = NULL;
318 1.1 tsarna }
319 1.1 tsarna
320 1.1 tsarna if (sc->sc_ibuf != NULL) {
321 1.1 tsarna free(sc->sc_ibuf, M_USBDEV);
322 1.1 tsarna sc->sc_ibuf = NULL;
323 1.1 tsarna }
324 1.1 tsarna
325 1.1 tsarna sc->sc_enabled = 0;
326 1.1 tsarna }
327 1.1 tsarna
328 1.1 tsarna Static int
329 1.8 christos uep_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
330 1.1 tsarna {
331 1.2 tsarna struct uep_softc *sc = v;
332 1.2 tsarna struct wsmouse_id *id;
333 1.2 tsarna
334 1.1 tsarna switch (cmd) {
335 1.1 tsarna case WSMOUSEIO_GTYPE:
336 1.1 tsarna *(u_int *)data = WSMOUSE_TYPE_TPANEL;
337 1.2 tsarna return 0;
338 1.2 tsarna
339 1.2 tsarna case WSMOUSEIO_GETID:
340 1.2 tsarna /*
341 1.2 tsarna * return unique ID string
342 1.2 tsarna * "<vendor> <model> <serial number>"
343 1.2 tsarna * unfortunately we have no serial number...
344 1.2 tsarna */
345 1.2 tsarna id = (struct wsmouse_id *)data;
346 1.2 tsarna if (id->type != WSMOUSE_ID_TYPE_UIDSTR)
347 1.2 tsarna return EINVAL;
348 1.3 perry
349 1.2 tsarna strcpy(id->data, UIDSTR);
350 1.2 tsarna id->length = strlen(UIDSTR);
351 1.2 tsarna return 0;
352 1.2 tsarna
353 1.2 tsarna case WSMOUSEIO_SCALIBCOORDS:
354 1.2 tsarna case WSMOUSEIO_GCALIBCOORDS:
355 1.5 christos return tpcalib_ioctl(&sc->sc_tpcalib, cmd, data, flag, l);
356 1.1 tsarna }
357 1.1 tsarna
358 1.1 tsarna return EPASSTHROUGH;
359 1.1 tsarna }
360 1.1 tsarna
361 1.1 tsarna void
362 1.1 tsarna uep_intr(usbd_xfer_handle xfer, usbd_private_handle addr, usbd_status status)
363 1.1 tsarna {
364 1.1 tsarna struct uep_softc *sc = addr;
365 1.1 tsarna u_char *p = sc->sc_ibuf;
366 1.1 tsarna u_int32_t len;
367 1.1 tsarna int x = 0, y = 0, s;
368 1.1 tsarna
369 1.1 tsarna usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
370 1.1 tsarna
371 1.1 tsarna if (status == USBD_CANCELLED)
372 1.1 tsarna return;
373 1.1 tsarna
374 1.1 tsarna if (status != USBD_NORMAL_COMPLETION) {
375 1.10.6.1 mjf aprint_error_dev(sc->sc_dev, "status %d\n", status);
376 1.1 tsarna usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
377 1.1 tsarna return;
378 1.1 tsarna }
379 1.1 tsarna
380 1.1 tsarna if (len != 5) {
381 1.1 tsarna #if 0
382 1.1 tsarna printf("%s: bad input length %d != %d\n",
383 1.1 tsarna USBDEVNAME(sc->sc_dev), len, sc->sc_isize);
384 1.1 tsarna #endif
385 1.1 tsarna return;
386 1.1 tsarna }
387 1.1 tsarna
388 1.1 tsarna if ((p[0] & 0xFE) != 0x80) {
389 1.10.6.1 mjf aprint_error_dev(sc->sc_dev,
390 1.10.6.1 mjf "bad input packet format\n");
391 1.1 tsarna return;
392 1.1 tsarna }
393 1.1 tsarna
394 1.1 tsarna if (sc->sc_wsmousedev != NULL) {
395 1.2 tsarna /*
396 1.2 tsarna * Packet format is 5 bytes:
397 1.2 tsarna *
398 1.2 tsarna * 1000000T
399 1.2 tsarna * 0000AAAA
400 1.2 tsarna * 0AAAAAAA
401 1.2 tsarna * 0000BBBB
402 1.2 tsarna * 0BBBBBBB
403 1.2 tsarna *
404 1.3 perry * T: 1=touched 0=not touched
405 1.2 tsarna * A: bits of axis A position, MSB to LSB
406 1.2 tsarna * B: bits of axis B position, MSB to LSB
407 1.2 tsarna *
408 1.2 tsarna * For the unit I have, A = Y and B = X.
409 1.2 tsarna * I don't know if units exist with A=X and B=Y,
410 1.2 tsarna * if so we'll cross that bridge when we come to it.
411 1.2 tsarna *
412 1.2 tsarna * The controller sends a stream of T=1 events while the
413 1.2 tsarna * panel is touched, followed by a single T=0 event.
414 1.3 perry *
415 1.2 tsarna */
416 1.2 tsarna
417 1.1 tsarna x = (p[3] << 7) | p[4];
418 1.1 tsarna y = (p[1] << 7) | p[2];
419 1.3 perry
420 1.2 tsarna tpcalib_trans(&sc->sc_tpcalib, x, y, &x, &y);
421 1.1 tsarna
422 1.1 tsarna s = spltty();
423 1.7 plunky wsmouse_input(sc->sc_wsmousedev, p[0] & 0x01, x, y, 0, 0,
424 1.1 tsarna WSMOUSE_INPUT_ABSOLUTE_X | WSMOUSE_INPUT_ABSOLUTE_Y);
425 1.1 tsarna splx(s);
426 1.1 tsarna }
427 1.1 tsarna }
428