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