1 /*
2 * Copyright (c) 2014 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com> and was subsequently ported,
6 * modified and enhanced for FreeBSD by Michael Gmelin <freebsd@grem.de>.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 /*
38 * CYAPA - Cypress APA trackpad with I2C Interface driver
39 *
40 * Based on DragonFlyBSD's cyapa driver, which referenced the linux
41 * cyapa.c driver to figure out the bootstrapping and commands.
42 *
43 * Unable to locate any datasheet for the device.
44 *
45 *
46 * Trackpad layout:
47 *
48 * 2/3 1/3
49 * +--------------------+------------+
50 * | | Middle |
51 * | | Button |
52 * | Left | |
53 * | Button +------------+
54 * | | Right |
55 * | | Button |
56 * +--------------------+............|
57 * | Thumb/Button Area | 15%
58 * +---------------------------------+
59 *
60 *
61 * FEATURES
62 *
63 * IMPS/2 emulation - Emulates the IntelliMouse protocol.
64 *
65 * Jitter supression - Implements 2-pixel hysteresis with memory.
66 *
67 * Jump detecion - Detect jumps caused by touchpad.
68 *
69 * Two finger scrolling - Use two fingers for Z axis scrolling.
70 *
71 * Button down/2nd finger - While one finger clicks and holds down the
72 * touchpad, the second one can be used to move
73 * the mouse cursor. Useful for drawing or
74 * selecting text.
75 *
76 * Thumb/Button Area - The lower 15%* of the trackpad will not affect
77 * the mouse cursor position. This allows for high
78 * precision clicking, by controlling the cursor
79 * with the index finger and pushing/holding the
80 * pad down with the thumb.
81 * * can be changed using sysctl
82 *
83 * Track-pad button - Push physical button. Left 2/3rds of the pad
84 * will issue a LEFT button event, upper right
85 * corner will issue a MIDDLE button event,
86 * lower right corner will issue a RIGHT button
87 * event. Optional tap support can be enabled
88 * and configured using sysctl.
89 *
90 * WARNINGS
91 *
92 * These trackpads get confused when three or more fingers are down on the
93 * same horizontal axis and will start to glitch the finger detection.
94 * Removing your hand for a few seconds will allow the trackpad to
95 * recalibrate. Generally speaking, when using three or more fingers
96 * please try to place at least one finger off-axis (a little above or
97 * below) the other two.
98 */
99
100 #include "opt_evdev.h"
101
102 #include <sys/param.h>
103 #include <sys/bus.h>
104 #include <sys/conf.h>
105 #include <sys/event.h>
106 #include <sys/fcntl.h>
107 #include <sys/kernel.h>
108 #include <sys/kthread.h>
109 #include <sys/lock.h>
110 #include <sys/lockmgr.h>
111 #include <sys/malloc.h>
112 #include <sys/mbuf.h>
113 #include <sys/module.h>
114 #include <sys/mouse.h>
115 #include <sys/mutex.h>
116 #include <sys/poll.h>
117 #include <sys/selinfo.h>
118 #include <sys/sysctl.h>
119 #include <sys/sysctl.h>
120 #include <sys/systm.h>
121 #include <sys/systm.h>
122 #include <sys/uio.h>
123 #include <sys/vnode.h>
124
125 #include <dev/iicbus/iiconf.h>
126 #include <dev/iicbus/iicbus.h>
127 #include <dev/cyapa/cyapa.h>
128
129 #ifdef EVDEV_SUPPORT
130 #include <dev/evdev/input.h>
131 #include <dev/evdev/evdev.h>
132 #endif
133
134 #include "iicbus_if.h"
135 #include "bus_if.h"
136 #include "device_if.h"
137
138 #define CYAPA_BUFSIZE 128 /* power of 2 */
139 #define CYAPA_BUFMASK (CYAPA_BUFSIZE - 1)
140
141 #define ZSCALE 15
142
143 #define TIME_TO_IDLE (hz * 10)
144 #define TIME_TO_RESET (hz * 3)
145
146 static MALLOC_DEFINE(M_CYAPA, "cyapa", "CYAPA device data");
147
148 struct cyapa_fifo {
149 int rindex;
150 int windex;
151 char buf[CYAPA_BUFSIZE];
152 };
153
154 struct cyapa_softc {
155 device_t dev;
156 int count; /* >0 if device opened */
157 struct cdev *devnode;
158 struct selinfo selinfo;
159 struct mtx mutex;
160 struct intr_config_hook intr_hook;
161 #ifdef EVDEV_SUPPORT
162 struct evdev_dev *evdev;
163 #endif
164
165 int cap_resx;
166 int cap_resy;
167 int cap_phyx;
168 int cap_phyy;
169 uint8_t cap_buttons;
170
171 int detaching; /* driver is detaching */
172 int poll_thread_running; /* poll thread is running */
173
174 /* PS/2 mouse emulation */
175 int track_x; /* current tracking */
176 int track_y;
177 int track_z;
178 int track_z_ticks;
179 uint16_t track_but;
180 char track_id; /* first finger id */
181 int track_nfingers;
182 int delta_x; /* accumulation -> report */
183 int delta_y;
184 int delta_z;
185 int fuzz_x;
186 int fuzz_y;
187 int fuzz_z;
188 int touch_x; /* touch down coordinates */
189 int touch_y;
190 int touch_z;
191 int finger1_ticks;
192 int finger2_ticks;
193 int finger3_ticks;
194 uint16_t reported_but;
195
196 struct cyapa_fifo rfifo; /* device->host */
197 struct cyapa_fifo wfifo; /* host->device */
198 uint8_t ps2_cmd; /* active p2_cmd waiting for data */
199 uint8_t ps2_acked;
200 int active_tick;
201 int data_signal;
202 int blocked;
203 int isselect;
204 int reporting_mode; /* 0=disabled 1=enabled */
205 int scaling_mode; /* 0=1:1 1=2:1 */
206 int remote_mode; /* 0 for streaming mode */
207 int zenabled; /* z-axis enabled (mode 1 or 2) */
208 mousehw_t hw; /* hardware information */
209 mousemode_t mode; /* mode */
210 int poll_ticks;
211 };
212
213 struct cyapa_cdevpriv {
214 struct cyapa_softc *sc;
215 };
216
217 #define CYPOLL_SHUTDOWN 0x0001
218
219 static void cyapa_poll_thread(void *arg);
220 static int cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs,
221 int freq);
222 static void cyapa_set_power_mode(struct cyapa_softc *sc, int mode);
223
224 static int fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo);
225 static size_t fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo);
226 static char *fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo,
227 size_t n);
228 static char *fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo,
229 size_t n);
230 static uint8_t fifo_read_char(struct cyapa_softc *sc,
231 struct cyapa_fifo *fifo);
232 static void fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo,
233 uint8_t c);
234 static size_t fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo);
235 static void fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo);
236
237 static int cyapa_fuzz(int delta, int *fuzz);
238
239 static int cyapa_idle_freq = 1;
240 SYSCTL_INT(_debug, OID_AUTO, cyapa_idle_freq, CTLFLAG_RW,
241 &cyapa_idle_freq, 0, "Scan frequency in idle mode");
242 static int cyapa_slow_freq = 20;
243 SYSCTL_INT(_debug, OID_AUTO, cyapa_slow_freq, CTLFLAG_RW,
244 &cyapa_slow_freq, 0, "Scan frequency in slow mode ");
245 static int cyapa_norm_freq = 100;
246 SYSCTL_INT(_debug, OID_AUTO, cyapa_norm_freq, CTLFLAG_RW,
247 &cyapa_norm_freq, 0, "Normal scan frequency");
248 static int cyapa_minpressure = 12;
249 SYSCTL_INT(_debug, OID_AUTO, cyapa_minpressure, CTLFLAG_RW,
250 &cyapa_minpressure, 0, "Minimum pressure to detect finger");
251 static int cyapa_enable_tapclick = 0;
252 SYSCTL_INT(_debug, OID_AUTO, cyapa_enable_tapclick, CTLFLAG_RW,
253 &cyapa_enable_tapclick, 0, "Enable tap to click");
254 static int cyapa_tapclick_min_ticks = 1;
255 SYSCTL_INT(_debug, OID_AUTO, cyapa_tapclick_min_ticks, CTLFLAG_RW,
256 &cyapa_tapclick_min_ticks, 0, "Minimum tap duration for click");
257 static int cyapa_tapclick_max_ticks = 8;
258 SYSCTL_INT(_debug, OID_AUTO, cyapa_tapclick_max_ticks, CTLFLAG_RW,
259 &cyapa_tapclick_max_ticks, 0, "Maximum tap duration for click");
260 static int cyapa_move_min_ticks = 4;
261 SYSCTL_INT(_debug, OID_AUTO, cyapa_move_min_ticks, CTLFLAG_RW,
262 &cyapa_move_min_ticks, 0,
263 "Minimum ticks before cursor position is changed");
264 static int cyapa_scroll_wait_ticks = 0;
265 SYSCTL_INT(_debug, OID_AUTO, cyapa_scroll_wait_ticks, CTLFLAG_RW,
266 &cyapa_scroll_wait_ticks, 0,
267 "Wait N ticks before starting to scroll");
268 static int cyapa_scroll_stick_ticks = 15;
269 SYSCTL_INT(_debug, OID_AUTO, cyapa_scroll_stick_ticks, CTLFLAG_RW,
270 &cyapa_scroll_stick_ticks, 0,
271 "Prevent cursor move on single finger for N ticks after scroll");
272 static int cyapa_thumbarea_percent = 15;
273 SYSCTL_INT(_debug, OID_AUTO, cyapa_thumbarea_percent, CTLFLAG_RW,
274 &cyapa_thumbarea_percent, 0,
275 "Size of bottom thumb area in percent");
276
277 static int cyapa_debug = 0;
278 SYSCTL_INT(_debug, OID_AUTO, cyapa_debug, CTLFLAG_RW,
279 &cyapa_debug, 0, "Enable debugging");
280 static int cyapa_reset = 0;
281 SYSCTL_INT(_debug, OID_AUTO, cyapa_reset, CTLFLAG_RW,
282 &cyapa_reset, 0, "Reset track pad");
283
284 static int
cyapa_read_bytes(device_t dev,uint8_t reg,uint8_t * val,int cnt)285 cyapa_read_bytes(device_t dev, uint8_t reg, uint8_t *val, int cnt)
286 {
287 uint16_t addr = iicbus_get_addr(dev);
288 struct iic_msg msgs[] = {
289 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
290 { addr, IIC_M_RD, cnt, val },
291 };
292
293 return (iicbus_transfer(dev, msgs, nitems(msgs)));
294 }
295
296 static int
cyapa_write_bytes(device_t dev,uint8_t reg,const uint8_t * val,int cnt)297 cyapa_write_bytes(device_t dev, uint8_t reg, const uint8_t *val, int cnt)
298 {
299 uint16_t addr = iicbus_get_addr(dev);
300 struct iic_msg msgs[] = {
301 { addr, IIC_M_WR | IIC_M_NOSTOP, 1, ® },
302 { addr, IIC_M_WR | IIC_M_NOSTART, cnt, __DECONST(uint8_t *, val) },
303 };
304
305 return (iicbus_transfer(dev, msgs, nitems(msgs)));
306 }
307
308 static void
cyapa_lock(struct cyapa_softc * sc)309 cyapa_lock(struct cyapa_softc *sc)
310 {
311
312 mtx_lock(&sc->mutex);
313 }
314
315 static void
cyapa_unlock(struct cyapa_softc * sc)316 cyapa_unlock(struct cyapa_softc *sc)
317 {
318
319 mtx_unlock(&sc->mutex);
320 }
321
322 #define CYAPA_LOCK_ASSERT(sc) mtx_assert(&(sc)->mutex, MA_OWNED);
323
324 /*
325 * Notify if possible receive data ready. Must be called
326 * with sc->mutex held (cyapa_lock(sc)).
327 */
328 static void
cyapa_notify(struct cyapa_softc * sc)329 cyapa_notify(struct cyapa_softc *sc)
330 {
331
332 CYAPA_LOCK_ASSERT(sc);
333
334 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo)) {
335 KNOTE_LOCKED(&sc->selinfo.si_note, 0);
336 if (sc->blocked || sc->isselect) {
337 if (sc->blocked) {
338 sc->blocked = 0;
339 wakeup(&sc->blocked);
340 }
341 if (sc->isselect) {
342 sc->isselect = 0;
343 selwakeup(&sc->selinfo);
344 }
345 }
346 }
347 }
348
349 /*
350 * Initialize the device
351 */
352 static int
init_device(device_t dev,struct cyapa_cap * cap,int probe)353 init_device(device_t dev, struct cyapa_cap *cap, int probe)
354 {
355 static char bl_exit[] = {
356 0x00, 0xff, 0xa5, 0x00, 0x01,
357 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
358 static char bl_deactivate[] = {
359 0x00, 0xff, 0x3b, 0x00, 0x01,
360 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
361 struct cyapa_boot_regs boot;
362 int error;
363 int retries;
364
365 /* Get status */
366 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
367 (void *)&boot, sizeof(boot));
368 if (error)
369 goto done;
370
371 /*
372 * Bootstrap the device if necessary. It can take up to 2 seconds
373 * for the device to fully initialize.
374 */
375 retries = 20;
376 while ((boot.stat & CYAPA_STAT_RUNNING) == 0 && retries > 0) {
377 if (boot.boot & CYAPA_BOOT_BUSY) {
378 /* Busy, wait loop. */
379 } else if (boot.error & CYAPA_ERROR_BOOTLOADER) {
380 /* Magic */
381 error = cyapa_write_bytes(dev, CMD_BOOT_STATUS,
382 bl_deactivate, sizeof(bl_deactivate));
383 if (error)
384 goto done;
385 } else {
386 /* Magic */
387 error = cyapa_write_bytes(dev, CMD_BOOT_STATUS,
388 bl_exit, sizeof(bl_exit));
389 if (error)
390 goto done;
391 }
392 pause("cyapab1", (hz * 2) / 10);
393 --retries;
394 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
395 (void *)&boot, sizeof(boot));
396 if (error)
397 goto done;
398 }
399
400 if (retries == 0) {
401 device_printf(dev, "Unable to bring device out of bootstrap\n");
402 error = ENXIO;
403 goto done;
404 }
405
406 /* Check identity */
407 if (cap) {
408 error = cyapa_read_bytes(dev, CMD_QUERY_CAPABILITIES,
409 (void *)cap, sizeof(*cap));
410
411 if (strncmp(cap->prod_ida, "CYTRA", 5) != 0) {
412 device_printf(dev, "Product ID \"%5.5s\" mismatch\n",
413 cap->prod_ida);
414 error = ENXIO;
415 }
416 }
417 error = cyapa_read_bytes(dev, CMD_BOOT_STATUS,
418 (void *)&boot, sizeof(boot));
419
420 if (probe == 0) /* official init */
421 device_printf(dev, "cyapa init status %02x\n", boot.stat);
422 else if (probe == 2)
423 device_printf(dev, "cyapa reset status %02x\n", boot.stat);
424
425 done:
426 if (error)
427 device_printf(dev, "Unable to initialize\n");
428 return (error);
429 }
430
431 /*
432 * Start the polling thread
433 */
434 static void
cyapa_start(void * xdev)435 cyapa_start(void *xdev)
436 {
437 struct cyapa_softc *sc;
438 device_t dev = xdev;
439
440 sc = device_get_softc(dev);
441
442 config_intrhook_disestablish(&sc->intr_hook);
443
444 /* Setup input event tracking */
445 cyapa_set_power_mode(sc, CMD_POWER_MODE_IDLE);
446
447 /* Start the polling thread */
448 kthread_add(cyapa_poll_thread, sc, NULL, NULL,
449 0, 0, "cyapa-poll");
450 }
451
452 static int cyapa_probe(device_t);
453 static int cyapa_attach(device_t);
454 static int cyapa_detach(device_t);
455 static void cyapa_cdevpriv_dtor(void*);
456
457 static device_method_t cyapa_methods[] = {
458 /* device interface */
459 DEVMETHOD(device_probe, cyapa_probe),
460 DEVMETHOD(device_attach, cyapa_attach),
461 DEVMETHOD(device_detach, cyapa_detach),
462
463 DEVMETHOD_END
464 };
465
466 static driver_t cyapa_driver = {
467 "cyapa",
468 cyapa_methods,
469 sizeof(struct cyapa_softc),
470 };
471
472 static d_open_t cyapaopen;
473 static d_ioctl_t cyapaioctl;
474 static d_read_t cyaparead;
475 static d_write_t cyapawrite;
476 static d_kqfilter_t cyapakqfilter;
477 static d_poll_t cyapapoll;
478
479 static struct cdevsw cyapa_cdevsw = {
480 .d_version = D_VERSION,
481 .d_open = cyapaopen,
482 .d_ioctl = cyapaioctl,
483 .d_read = cyaparead,
484 .d_write = cyapawrite,
485 .d_kqfilter = cyapakqfilter,
486 .d_poll = cyapapoll,
487 };
488
489 static int
cyapa_probe(device_t dev)490 cyapa_probe(device_t dev)
491 {
492 struct cyapa_cap cap;
493 int addr;
494 int error;
495
496 addr = iicbus_get_addr(dev);
497
498 /*
499 * 0x67 - cypress trackpad on the acer c720
500 * (other devices might use other ids).
501 */
502 if (addr != 0xce)
503 return (ENXIO);
504
505 error = init_device(dev, &cap, 1);
506 if (error != 0)
507 return (ENXIO);
508
509 device_set_desc(dev, "Cypress APA I2C Trackpad");
510
511 return (BUS_PROBE_VENDOR);
512 }
513
514 static int
cyapa_attach(device_t dev)515 cyapa_attach(device_t dev)
516 {
517 struct cyapa_softc *sc;
518 struct cyapa_cap cap;
519 int unit;
520 int addr;
521
522 sc = device_get_softc(dev);
523 sc->reporting_mode = 1;
524
525 unit = device_get_unit(dev);
526 addr = iicbus_get_addr(dev);
527
528 if (init_device(dev, &cap, 0))
529 return (ENXIO);
530
531 mtx_init(&sc->mutex, "cyapa", NULL, MTX_DEF);
532
533 sc->dev = dev;
534
535 knlist_init_mtx(&sc->selinfo.si_note, &sc->mutex);
536
537 sc->cap_resx = ((cap.max_abs_xy_high << 4) & 0x0F00) |
538 cap.max_abs_x_low;
539 sc->cap_resy = ((cap.max_abs_xy_high << 8) & 0x0F00) |
540 cap.max_abs_y_low;
541 sc->cap_phyx = ((cap.phy_siz_xy_high << 4) & 0x0F00) |
542 cap.phy_siz_x_low;
543 sc->cap_phyy = ((cap.phy_siz_xy_high << 8) & 0x0F00) |
544 cap.phy_siz_y_low;
545 sc->cap_buttons = cap.buttons >> 3 &
546 (CYAPA_FNGR_LEFT | CYAPA_FNGR_RIGHT | CYAPA_FNGR_MIDDLE);
547
548 device_printf(dev, "%5.5s-%6.6s-%2.2s buttons=%c%c%c res=%dx%d\n",
549 cap.prod_ida, cap.prod_idb, cap.prod_idc,
550 ((sc->cap_buttons & CYAPA_FNGR_LEFT) ? 'L' : '-'),
551 ((sc->cap_buttons & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
552 ((sc->cap_buttons & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
553 sc->cap_resx, sc->cap_resy);
554
555 sc->hw.buttons = 5;
556 sc->hw.iftype = MOUSE_IF_PS2;
557 sc->hw.type = MOUSE_MOUSE;
558 sc->hw.model = MOUSE_MODEL_INTELLI;
559 sc->hw.hwid = addr;
560
561 sc->mode.protocol = MOUSE_PROTO_PS2;
562 sc->mode.rate = 100;
563 sc->mode.resolution = 4;
564 sc->mode.accelfactor = 1;
565 sc->mode.level = 0;
566 sc->mode.packetsize = MOUSE_PS2_PACKETSIZE;
567
568 sc->intr_hook.ich_func = cyapa_start;
569 sc->intr_hook.ich_arg = sc->dev;
570
571 #ifdef EVDEV_SUPPORT
572 sc->evdev = evdev_alloc();
573 evdev_set_name(sc->evdev, device_get_desc(sc->dev));
574 evdev_set_phys(sc->evdev, device_get_nameunit(sc->dev));
575 evdev_set_id(sc->evdev, BUS_I2C, 0, 0, 1);
576 evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_STCOMPAT);
577 evdev_set_flag(sc->evdev, EVDEV_FLAG_MT_AUTOREL);
578
579 evdev_support_event(sc->evdev, EV_SYN);
580 evdev_support_event(sc->evdev, EV_ABS);
581 evdev_support_event(sc->evdev, EV_KEY);
582 evdev_support_prop(sc->evdev, INPUT_PROP_POINTER);
583 if (sc->cap_buttons & CYAPA_FNGR_LEFT)
584 evdev_support_key(sc->evdev, BTN_LEFT);
585 if (sc->cap_buttons & CYAPA_FNGR_RIGHT)
586 evdev_support_key(sc->evdev, BTN_RIGHT);
587 if (sc->cap_buttons & CYAPA_FNGR_MIDDLE)
588 evdev_support_key(sc->evdev, BTN_MIDDLE);
589 if (sc->cap_buttons == CYAPA_FNGR_LEFT)
590 evdev_support_prop(sc->evdev, INPUT_PROP_BUTTONPAD);
591
592 evdev_support_abs(sc->evdev, ABS_MT_SLOT,
593 0, CYAPA_MAX_MT - 1, 0, 0, 0);
594 evdev_support_abs(sc->evdev, ABS_MT_TRACKING_ID, -1, 15, 0, 0, 0);
595 evdev_support_abs(sc->evdev, ABS_MT_POSITION_X, 0, sc->cap_resx, 0, 0,
596 sc->cap_phyx != 0 ? sc->cap_resx / sc->cap_phyx : 0);
597 evdev_support_abs(sc->evdev, ABS_MT_POSITION_Y, 0, sc->cap_resy, 0, 0,
598 sc->cap_phyy != 0 ? sc->cap_resy / sc->cap_phyy : 0);
599 evdev_support_abs(sc->evdev, ABS_MT_PRESSURE, 0, 255, 0, 0, 0);
600
601 if (evdev_register(sc->evdev) != 0) {
602 mtx_destroy(&sc->mutex);
603 return (ENOMEM);
604 }
605 #endif
606
607 /* Postpone start of the polling thread until sleep is available */
608 if (config_intrhook_establish(&sc->intr_hook) != 0) {
609 #ifdef EVDEV_SUPPORT
610 evdev_free(sc->evdev);
611 #endif
612 mtx_destroy(&sc->mutex);
613 return (ENOMEM);
614 }
615
616 sc->devnode = make_dev(&cyapa_cdevsw, unit,
617 UID_ROOT, GID_WHEEL, 0600, "cyapa%d", unit);
618
619 sc->devnode->si_drv1 = sc;
620
621 return (0);
622 }
623
624 static int
cyapa_detach(device_t dev)625 cyapa_detach(device_t dev)
626 {
627 struct cyapa_softc *sc;
628
629 sc = device_get_softc(dev);
630
631 /* Cleanup poller thread */
632 cyapa_lock(sc);
633 while (sc->poll_thread_running) {
634 sc->detaching = 1;
635 mtx_sleep(&sc->detaching, &sc->mutex, PCATCH, "cyapadet", hz);
636 }
637 cyapa_unlock(sc);
638
639 destroy_dev(sc->devnode);
640
641 knlist_clear(&sc->selinfo.si_note, 0);
642 seldrain(&sc->selinfo);
643 knlist_destroy(&sc->selinfo.si_note);
644 #ifdef EVDEV_SUPPORT
645 evdev_free(sc->evdev);
646 #endif
647
648 mtx_destroy(&sc->mutex);
649
650 return (0);
651 }
652
653 /*
654 * USER DEVICE I/O FUNCTIONS
655 */
656 static int
cyapaopen(struct cdev * dev,int oflags,int devtype,struct thread * td)657 cyapaopen(struct cdev *dev, int oflags, int devtype, struct thread *td)
658 {
659 struct cyapa_cdevpriv *priv;
660 int error;
661
662 priv = malloc(sizeof(*priv), M_CYAPA, M_WAITOK | M_ZERO);
663 priv->sc = dev->si_drv1;
664
665 error = devfs_set_cdevpriv(priv, cyapa_cdevpriv_dtor);
666 if (error == 0) {
667 cyapa_lock(priv->sc);
668 priv->sc->count++;
669 cyapa_unlock(priv->sc);
670 }
671 else
672 free(priv, M_CYAPA);
673
674 return (error);
675 }
676
677 static void
cyapa_cdevpriv_dtor(void * data)678 cyapa_cdevpriv_dtor(void *data)
679 {
680 struct cyapa_cdevpriv *priv;
681
682 priv = data;
683 KASSERT(priv != NULL, ("cyapa cdevpriv should not be NULL!"));
684
685 cyapa_lock(priv->sc);
686 priv->sc->count--;
687 cyapa_unlock(priv->sc);
688
689 free(priv, M_CYAPA);
690 }
691
692 static int
cyaparead(struct cdev * dev,struct uio * uio,int ioflag)693 cyaparead(struct cdev *dev, struct uio *uio, int ioflag)
694 {
695 struct cyapa_softc *sc;
696 int error;
697 int didread;
698 size_t n;
699 char* ptr;
700
701 sc = dev->si_drv1;
702 /* If buffer is empty, load a new event if it is ready */
703 cyapa_lock(sc);
704 again:
705 if (fifo_empty(sc, &sc->rfifo) &&
706 (sc->data_signal || sc->delta_x || sc->delta_y ||
707 sc->track_but != sc->reported_but)) {
708 uint8_t c0;
709 uint16_t but;
710 int delta_x;
711 int delta_y;
712 int delta_z;
713
714 /* Accumulate delta_x, delta_y */
715 sc->data_signal = 0;
716 delta_x = sc->delta_x;
717 delta_y = sc->delta_y;
718 delta_z = sc->delta_z;
719 if (delta_x > 255) {
720 delta_x = 255;
721 sc->data_signal = 1;
722 }
723 if (delta_x < -256) {
724 delta_x = -256;
725 sc->data_signal = 1;
726 }
727 if (delta_y > 255) {
728 delta_y = 255;
729 sc->data_signal = 1;
730 }
731 if (delta_y < -256) {
732 delta_y = -256;
733 sc->data_signal = 1;
734 }
735 if (delta_z > 255) {
736 delta_z = 255;
737 sc->data_signal = 1;
738 }
739 if (delta_z < -256) {
740 delta_z = -256;
741 sc->data_signal = 1;
742 }
743 but = sc->track_but;
744
745 /* Adjust baseline for next calculation */
746 sc->delta_x -= delta_x;
747 sc->delta_y -= delta_y;
748 sc->delta_z -= delta_z;
749 sc->reported_but = but;
750
751 /*
752 * Fuzz reduces movement jitter by introducing some
753 * hysteresis. It operates without cumulative error so
754 * if you swish around quickly and return your finger to
755 * where it started, so to will the mouse.
756 */
757 delta_x = cyapa_fuzz(delta_x, &sc->fuzz_x);
758 delta_y = cyapa_fuzz(delta_y, &sc->fuzz_y);
759 delta_z = cyapa_fuzz(delta_z, &sc->fuzz_z);
760
761 /*
762 * Generate report
763 */
764 if (sc->mode.level == 1) {
765 c0 = MOUSE_SYS_SYNC;
766 if (but & CYAPA_FNGR_LEFT)
767 c0 |= MOUSE_SYS_BUTTON1UP;
768 if (but & CYAPA_FNGR_MIDDLE)
769 c0 |= MOUSE_SYS_BUTTON2UP;
770 if (but & CYAPA_FNGR_RIGHT)
771 c0 |= MOUSE_SYS_BUTTON3UP;
772 fifo_write_char(sc, &sc->rfifo, c0);
773 fifo_write_char(sc, &sc->rfifo, delta_x >> 1);
774 fifo_write_char(sc, &sc->rfifo, delta_y >> 1);
775 fifo_write_char(sc, &sc->rfifo, delta_x - (delta_x >> 1));
776 fifo_write_char(sc, &sc->rfifo, delta_y - (delta_y >> 1));
777 fifo_write_char(sc, &sc->rfifo, delta_z >> 1);
778 fifo_write_char(sc, &sc->rfifo, delta_z - (delta_z >> 1));
779 fifo_write_char(sc, &sc->rfifo, MOUSE_SYS_EXTBUTTONS);
780 } else {
781 c0 = 0;
782 if (delta_x < 0)
783 c0 |= 0x10;
784 if (delta_y < 0)
785 c0 |= 0x20;
786 c0 |= 0x08;
787 if (but & CYAPA_FNGR_LEFT)
788 c0 |= 0x01;
789 if (but & CYAPA_FNGR_MIDDLE)
790 c0 |= 0x04;
791 if (but & CYAPA_FNGR_RIGHT)
792 c0 |= 0x02;
793
794 fifo_write_char(sc, &sc->rfifo, c0);
795 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_x);
796 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_y);
797 switch(sc->zenabled) {
798 case 1:
799 /* Z axis all 8 bits */
800 fifo_write_char(sc, &sc->rfifo, (uint8_t)delta_z);
801 break;
802 case 2:
803 /*
804 * Z axis low 4 bits + 4th button and 5th button
805 * (high 2 bits must be left 0). Auto-scale
806 * delta_z to fit to avoid a wrong-direction
807 * overflow (don't try to retain the remainder).
808 */
809 while (delta_z > 7 || delta_z < -8)
810 delta_z >>= 1;
811 c0 = (uint8_t)delta_z & 0x0F;
812 fifo_write_char(sc, &sc->rfifo, c0);
813 break;
814 default:
815 /* basic PS/2 */
816 break;
817 }
818 }
819 cyapa_notify(sc);
820 }
821
822 /* Blocking / Non-blocking */
823 error = 0;
824 didread = (uio->uio_resid == 0);
825
826 while ((ioflag & IO_NDELAY) == 0 && fifo_empty(sc, &sc->rfifo)) {
827 if (sc->data_signal)
828 goto again;
829 sc->blocked = 1;
830 error = mtx_sleep(&sc->blocked, &sc->mutex, PCATCH, "cyablk", 0);
831 if (error)
832 break;
833 }
834
835 /* Return any buffered data */
836 while (error == 0 && uio->uio_resid &&
837 (n = fifo_ready(sc, &sc->rfifo)) > 0) {
838 if (n > uio->uio_resid)
839 n = uio->uio_resid;
840 ptr = fifo_read(sc, &sc->rfifo, 0);
841 cyapa_unlock(sc);
842 error = uiomove(ptr, n, uio);
843 cyapa_lock(sc);
844 if (error)
845 break;
846 fifo_read(sc, &sc->rfifo, n);
847 didread = 1;
848 }
849 cyapa_unlock(sc);
850
851 if (error == 0 && didread == 0) {
852 error = EWOULDBLOCK;
853 }
854 return (didread ? 0 : error);
855 }
856
857 static int
cyapawrite(struct cdev * dev,struct uio * uio,int ioflag)858 cyapawrite(struct cdev *dev, struct uio *uio, int ioflag)
859 {
860 struct cyapa_softc *sc;
861 int error;
862 int cmd_completed;
863 size_t n;
864 uint8_t c0;
865 char* ptr;
866
867 sc = dev->si_drv1;
868 again:
869 /*
870 * Copy data from userland. This will also cross-over the end
871 * of the fifo and keep filling.
872 */
873 cyapa_lock(sc);
874 while ((n = fifo_space(sc, &sc->wfifo)) > 0 && uio->uio_resid) {
875 if (n > uio->uio_resid)
876 n = uio->uio_resid;
877 ptr = fifo_write(sc, &sc->wfifo, 0);
878 cyapa_unlock(sc);
879 error = uiomove(ptr, n, uio);
880 cyapa_lock(sc);
881 if (error)
882 break;
883 fifo_write(sc, &sc->wfifo, n);
884 }
885
886 /* Handle commands */
887 cmd_completed = (fifo_ready(sc, &sc->wfifo) != 0);
888 while (fifo_ready(sc, &sc->wfifo) && cmd_completed && error == 0) {
889 if (sc->ps2_cmd == 0)
890 sc->ps2_cmd = fifo_read_char(sc, &sc->wfifo);
891 switch(sc->ps2_cmd) {
892 case 0xE6:
893 /* SET SCALING 1:1 */
894 sc->scaling_mode = 0;
895 fifo_write_char(sc, &sc->rfifo, 0xFA);
896 break;
897 case 0xE7:
898 /* SET SCALING 2:1 */
899 sc->scaling_mode = 1;
900 fifo_write_char(sc, &sc->rfifo, 0xFA);
901 break;
902 case 0xE8:
903 /* SET RESOLUTION +1 byte */
904 if (sc->ps2_acked == 0) {
905 sc->ps2_acked = 1;
906 fifo_write_char(sc, &sc->rfifo, 0xFA);
907 }
908 if (fifo_ready(sc, &sc->wfifo) == 0) {
909 cmd_completed = 0;
910 break;
911 }
912 sc->mode.resolution = fifo_read_char(sc, &sc->wfifo);
913 fifo_write_char(sc, &sc->rfifo, 0xFA);
914 break;
915 case 0xE9:
916 /*
917 * STATUS REQUEST
918 *
919 * byte1:
920 * bit 7 0
921 * bit 6 Mode (1=remote mode, 0=stream mode)
922 * bit 5 Enable (data reporting enabled)
923 * bit 4 Scaling (0=1:1 1=2:1)
924 * bit 3 0
925 * bit 2 LEFT BUTTON (1 if pressed)
926 * bit 1 MIDDLE BUTTON (1 if pressed)
927 * bit 0 RIGHT BUTTON (1 if pressed)
928 *
929 * byte2: resolution counts/mm
930 * byte3: sample rate
931 */
932 c0 = 0;
933 if (sc->remote_mode)
934 c0 |= 0x40;
935 if (sc->reporting_mode)
936 c0 |= 0x20;
937 if (sc->scaling_mode)
938 c0 |= 0x10;
939 if (sc->track_but & CYAPA_FNGR_LEFT)
940 c0 |= 0x04;
941 if (sc->track_but & CYAPA_FNGR_MIDDLE)
942 c0 |= 0x02;
943 if (sc->track_but & CYAPA_FNGR_RIGHT)
944 c0 |= 0x01;
945 fifo_write_char(sc, &sc->rfifo, 0xFA);
946 fifo_write_char(sc, &sc->rfifo, c0);
947 fifo_write_char(sc, &sc->rfifo, 0x00);
948 fifo_write_char(sc, &sc->rfifo, 100);
949 break;
950 case 0xEA:
951 /* Set stream mode and reset movement counters */
952 sc->remote_mode = 0;
953 fifo_write_char(sc, &sc->rfifo, 0xFA);
954 sc->delta_x = 0;
955 sc->delta_y = 0;
956 sc->delta_z = 0;
957 break;
958 case 0xEB:
959 /*
960 * Read Data (if in remote mode). If not in remote
961 * mode force an event.
962 */
963 fifo_write_char(sc, &sc->rfifo, 0xFA);
964 sc->data_signal = 1;
965 break;
966 case 0xEC:
967 /* Reset Wrap Mode (ignored) */
968 fifo_write_char(sc, &sc->rfifo, 0xFA);
969 break;
970 case 0xEE:
971 /* Set Wrap Mode (ignored) */
972 fifo_write_char(sc, &sc->rfifo, 0xFA);
973 break;
974 case 0xF0:
975 /* Set Remote Mode */
976 sc->remote_mode = 1;
977 fifo_write_char(sc, &sc->rfifo, 0xFA);
978 sc->delta_x = 0;
979 sc->delta_y = 0;
980 sc->delta_z = 0;
981 break;
982 case 0xF2:
983 /*
984 * Get Device ID
985 *
986 * If we send 0x00 - normal PS/2 mouse, no Z-axis
987 *
988 * If we send 0x03 - Intellimouse, data packet has
989 * an additional Z movement byte (8 bits signed).
990 * (also reset movement counters)
991 *
992 * If we send 0x04 - Now includes z-axis and the
993 * 4th and 5th mouse buttons.
994 */
995 fifo_write_char(sc, &sc->rfifo, 0xFA);
996 switch(sc->zenabled) {
997 case 1:
998 fifo_write_char(sc, &sc->rfifo, 0x03);
999 break;
1000 case 2:
1001 fifo_write_char(sc, &sc->rfifo, 0x04);
1002 break;
1003 default:
1004 fifo_write_char(sc, &sc->rfifo, 0x00);
1005 break;
1006 }
1007 sc->delta_x = 0;
1008 sc->delta_y = 0;
1009 sc->delta_z = 0;
1010 break;
1011 case 0xF3:
1012 /*
1013 * Set Sample Rate
1014 *
1015 * byte1: the sample rate
1016 */
1017 if (sc->ps2_acked == 0) {
1018 sc->ps2_acked = 1;
1019 fifo_write_char(sc, &sc->rfifo, 0xFA);
1020 }
1021 if (fifo_ready(sc, &sc->wfifo) == 0) {
1022 cmd_completed = 0;
1023 break;
1024 }
1025 sc->mode.rate = fifo_read_char(sc, &sc->wfifo);
1026 fifo_write_char(sc, &sc->rfifo, 0xFA);
1027
1028 /*
1029 * zenabling sequence: 200,100,80 (device id 0x03)
1030 * 200,200,80 (device id 0x04)
1031 *
1032 * We support id 0x03 (no 4th or 5th button).
1033 * We support id 0x04 (w/ 4th and 5th button).
1034 */
1035 if (sc->zenabled == 0 && sc->mode.rate == 200)
1036 sc->zenabled = -1;
1037 else if (sc->zenabled == -1 && sc->mode.rate == 100)
1038 sc->zenabled = -2;
1039 else if (sc->zenabled == -1 && sc->mode.rate == 200)
1040 sc->zenabled = -3;
1041 else if (sc->zenabled == -2 && sc->mode.rate == 80)
1042 sc->zenabled = 1; /* z-axis mode */
1043 else if (sc->zenabled == -3 && sc->mode.rate == 80)
1044 sc->zenabled = 2; /* z-axis+but4/5 */
1045 if (sc->mode.level)
1046 sc->zenabled = 1;
1047 break;
1048 case 0xF4:
1049 /* Enable data reporting. Only effects stream mode. */
1050 fifo_write_char(sc, &sc->rfifo, 0xFA);
1051 sc->reporting_mode = 1;
1052 break;
1053 case 0xF5:
1054 /*
1055 * Disable data reporting. Only effects stream mode
1056 * and is ignored right now.
1057 */
1058 fifo_write_char(sc, &sc->rfifo, 0xFA);
1059 sc->reporting_mode = 1;
1060 break;
1061 case 0xF6:
1062 /*
1063 * SET DEFAULTS
1064 *
1065 * (reset sampling rate, resolution, scaling and
1066 * enter stream mode)
1067 */
1068 fifo_write_char(sc, &sc->rfifo, 0xFA);
1069 sc->mode.rate = 100;
1070 sc->mode.resolution = 4;
1071 sc->scaling_mode = 0;
1072 sc->reporting_mode = 1;
1073 sc->remote_mode = 0;
1074 sc->delta_x = 0;
1075 sc->delta_y = 0;
1076 sc->delta_z = 0;
1077 /* signal */
1078 break;
1079 case 0xFE:
1080 /*
1081 * RESEND
1082 *
1083 * Force a resend by guaranteeing that reported_but
1084 * differs from track_but.
1085 */
1086 fifo_write_char(sc, &sc->rfifo, 0xFA);
1087 sc->data_signal = 1;
1088 break;
1089 case 0xFF:
1090 /*
1091 * RESET
1092 */
1093 fifo_reset(sc, &sc->rfifo); /* should we do this? */
1094 fifo_reset(sc, &sc->wfifo); /* should we do this? */
1095 fifo_write_char(sc, &sc->rfifo, 0xFA);
1096 sc->delta_x = 0;
1097 sc->delta_y = 0;
1098 sc->delta_z = 0;
1099 sc->zenabled = 0;
1100 sc->mode.level = 0;
1101 break;
1102 default:
1103 printf("unknown command %02x\n", sc->ps2_cmd);
1104 break;
1105 }
1106 if (cmd_completed) {
1107 sc->ps2_cmd = 0;
1108 sc->ps2_acked = 0;
1109 }
1110 cyapa_notify(sc);
1111 }
1112 cyapa_unlock(sc);
1113 if (error == 0 && (cmd_completed || uio->uio_resid))
1114 goto again;
1115 return (error);
1116 }
1117
1118 static void cyapafiltdetach(struct knote *);
1119 static int cyapafilt(struct knote *, long);
1120
1121 static const struct filterops cyapa_filtops = {
1122 .f_isfd = 1,
1123 .f_detach = cyapafiltdetach,
1124 .f_event = cyapafilt,
1125 .f_copy = knote_triv_copy,
1126 };
1127
1128 static int
cyapakqfilter(struct cdev * dev,struct knote * kn)1129 cyapakqfilter(struct cdev *dev, struct knote *kn)
1130 {
1131 struct cyapa_softc *sc;
1132 struct knlist *knlist;
1133
1134 sc = dev->si_drv1;
1135
1136 switch(kn->kn_filter) {
1137 case EVFILT_READ:
1138 kn->kn_fop = &cyapa_filtops;
1139 kn->kn_hook = (void *)sc;
1140 break;
1141 default:
1142 return (EOPNOTSUPP);
1143 }
1144 knlist = &sc->selinfo.si_note;
1145 knlist_add(knlist, kn, 0);
1146
1147 return (0);
1148 }
1149
1150 static int
cyapapoll(struct cdev * dev,int events,struct thread * td)1151 cyapapoll(struct cdev *dev, int events, struct thread *td)
1152 {
1153 struct cyapa_softc *sc;
1154 int revents;
1155
1156 sc = dev->si_drv1;
1157 revents = 0;
1158
1159 cyapa_lock(sc);
1160 if (events & (POLLIN | POLLRDNORM)) {
1161 if (sc->data_signal || !fifo_empty(sc, &sc->rfifo))
1162 revents = events & (POLLIN | POLLRDNORM);
1163 else {
1164 sc->isselect = 1;
1165 selrecord(td, &sc->selinfo);
1166 }
1167 }
1168 cyapa_unlock(sc);
1169
1170 return (revents);
1171 }
1172
1173 static void
cyapafiltdetach(struct knote * kn)1174 cyapafiltdetach(struct knote *kn)
1175 {
1176 struct cyapa_softc *sc;
1177 struct knlist *knlist;
1178
1179 sc = (struct cyapa_softc *)kn->kn_hook;
1180
1181 knlist = &sc->selinfo.si_note;
1182 knlist_remove(knlist, kn, 0);
1183 }
1184
1185 static int
cyapafilt(struct knote * kn,long hint)1186 cyapafilt(struct knote *kn, long hint)
1187 {
1188 struct cyapa_softc *sc;
1189 int ready;
1190
1191 sc = (struct cyapa_softc *)kn->kn_hook;
1192
1193 cyapa_lock(sc);
1194 ready = fifo_ready(sc, &sc->rfifo) || sc->data_signal;
1195 cyapa_unlock(sc);
1196
1197 return (ready);
1198 }
1199
1200 static int
cyapaioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)1201 cyapaioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1202 {
1203 struct cyapa_softc *sc;
1204 int error;
1205
1206 sc = dev->si_drv1;
1207 error = 0;
1208
1209 cyapa_lock(sc);
1210 switch (cmd) {
1211 case MOUSE_GETHWINFO:
1212 *(mousehw_t *)data = sc->hw;
1213 if (sc->mode.level == 0)
1214 ((mousehw_t *)data)->model = MOUSE_MODEL_GENERIC;
1215 break;
1216
1217 case MOUSE_GETMODE:
1218 *(mousemode_t *)data = sc->mode;
1219 ((mousemode_t *)data)->resolution =
1220 MOUSE_RES_LOW - sc->mode.resolution;
1221 switch (sc->mode.level) {
1222 case 0:
1223 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1224 ((mousemode_t *)data)->packetsize =
1225 MOUSE_PS2_PACKETSIZE;
1226 break;
1227 case 1:
1228 ((mousemode_t *)data)->protocol = MOUSE_PROTO_SYSMOUSE;
1229 ((mousemode_t *)data)->packetsize =
1230 MOUSE_SYS_PACKETSIZE;
1231 break;
1232 case 2:
1233 ((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1234 ((mousemode_t *)data)->packetsize =
1235 MOUSE_PS2_PACKETSIZE + 1;
1236 break;
1237 }
1238 break;
1239
1240 case MOUSE_GETLEVEL:
1241 *(int *)data = sc->mode.level;
1242 break;
1243
1244 case MOUSE_SETLEVEL:
1245 if ((*(int *)data < 0) &&
1246 (*(int *)data > 2)) {
1247 error = EINVAL;
1248 break;
1249 }
1250 sc->mode.level = *(int *)data;
1251 sc->zenabled = sc->mode.level ? 1 : 0;
1252 break;
1253
1254 default:
1255 error = ENOTTY;
1256 break;
1257 }
1258 cyapa_unlock(sc);
1259
1260 return (error);
1261 }
1262
1263 /*
1264 * MAJOR SUPPORT FUNCTIONS
1265 */
1266 static void
cyapa_poll_thread(void * arg)1267 cyapa_poll_thread(void *arg)
1268 {
1269 struct cyapa_softc *sc;
1270 struct cyapa_regs regs;
1271 device_t bus; /* iicbus */
1272 int error;
1273 int freq;
1274 int isidle;
1275 int pstate;
1276 int npstate;
1277 int last_reset;
1278
1279 sc = arg;
1280 freq = cyapa_norm_freq;
1281 isidle = 0;
1282 pstate = CMD_POWER_MODE_IDLE;
1283 last_reset = ticks;
1284
1285 bus = device_get_parent(sc->dev);
1286
1287 cyapa_lock(sc);
1288 sc->poll_thread_running = 1;
1289
1290 while (!sc->detaching) {
1291 cyapa_unlock(sc);
1292 error = iicbus_request_bus(bus, sc->dev, IIC_WAIT);
1293 if (error == 0) {
1294 error = cyapa_read_bytes(sc->dev, CMD_DEV_STATUS,
1295 (void *)®s, sizeof(regs));
1296 if (error == 0) {
1297 isidle = cyapa_raw_input(sc, ®s, freq);
1298 }
1299
1300 /*
1301 * For some reason the device can crap-out. If it
1302 * drops back into bootstrap mode try to reinitialize
1303 * it.
1304 */
1305 if (cyapa_reset ||
1306 ((regs.stat & CYAPA_STAT_RUNNING) == 0 &&
1307 (unsigned)(ticks - last_reset) > TIME_TO_RESET)) {
1308 cyapa_reset = 0;
1309 last_reset = ticks;
1310 init_device(sc->dev, NULL, 2);
1311 }
1312 iicbus_release_bus(bus, sc->dev);
1313 }
1314 pause("cyapw", hz / freq);
1315 ++sc->poll_ticks;
1316
1317 if (sc->count == 0) {
1318 freq = cyapa_idle_freq;
1319 npstate = CMD_POWER_MODE_IDLE;
1320 } else if (isidle) {
1321 freq = cyapa_slow_freq;
1322 npstate = CMD_POWER_MODE_IDLE;
1323 } else {
1324 freq = cyapa_norm_freq;
1325 npstate = CMD_POWER_MODE_FULL;
1326 }
1327 if (pstate != npstate) {
1328 pstate = npstate;
1329 cyapa_set_power_mode(sc, pstate);
1330 if (cyapa_debug) {
1331 switch(pstate) {
1332 case CMD_POWER_MODE_OFF:
1333 printf("cyapa: power off\n");
1334 break;
1335 case CMD_POWER_MODE_IDLE:
1336 printf("cyapa: power idle\n");
1337 break;
1338 case CMD_POWER_MODE_FULL:
1339 printf("cyapa: power full\n");
1340 break;
1341 }
1342 }
1343 }
1344
1345 cyapa_lock(sc);
1346 }
1347 sc->poll_thread_running = 0;
1348 cyapa_unlock(sc);
1349 kthread_exit();
1350 }
1351
1352 static int
cyapa_raw_input(struct cyapa_softc * sc,struct cyapa_regs * regs,int freq)1353 cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs, int freq)
1354 {
1355 int nfingers;
1356 int afingers; /* actual fingers after culling */
1357 int i;
1358 int j;
1359 int isidle;
1360 int thumbarea_begin;
1361 int seen_thumb;
1362 int x;
1363 int y;
1364 int z;
1365 int newfinger;
1366 int lessfingers;
1367 int click_x;
1368 int click_y;
1369 uint16_t but; /* high bits used for simulated but4/but5 */
1370
1371 thumbarea_begin = sc->cap_resy -
1372 ((sc->cap_resy * cyapa_thumbarea_percent) / 100);
1373 click_x = click_y = 0;
1374
1375 /*
1376 * If the device is not running the rest of the status
1377 * means something else, set fingers to 0.
1378 */
1379 if ((regs->stat & CYAPA_STAT_RUNNING) == 0) {
1380 regs->fngr = 0;
1381 }
1382
1383 /* Process fingers/movement */
1384 nfingers = CYAPA_FNGR_NUMFINGERS(regs->fngr);
1385 afingers = nfingers;
1386
1387 if (cyapa_debug) {
1388 printf("stat %02x buttons %c%c%c nfngrs=%d ",
1389 regs->stat,
1390 ((regs->fngr & CYAPA_FNGR_LEFT) ? 'L' : '-'),
1391 ((regs->fngr & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
1392 ((regs->fngr & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
1393 nfingers);
1394 }
1395
1396 #ifdef EVDEV_SUPPORT
1397 if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
1398 for (i = 0; i < nfingers; ++i) {
1399 int slot = evdev_mt_id_to_slot(
1400 sc->evdev, regs->touch[i].id);
1401 if (slot == -1) {
1402 if (cyapa_debug)
1403 printf("Slot overflow for i=%d\n",
1404 regs->touch[i].id);
1405 continue;
1406 }
1407 evdev_push_abs(sc->evdev, ABS_MT_SLOT, slot);
1408 evdev_push_abs(sc->evdev, ABS_MT_TRACKING_ID,
1409 regs->touch[i].id);
1410 evdev_push_abs(sc->evdev, ABS_MT_POSITION_X,
1411 CYAPA_TOUCH_X(regs, i));
1412 evdev_push_abs(sc->evdev, ABS_MT_POSITION_Y,
1413 CYAPA_TOUCH_Y(regs, i));
1414 evdev_push_abs(sc->evdev, ABS_MT_PRESSURE,
1415 CYAPA_TOUCH_P(regs, i));
1416 }
1417 if (sc->cap_buttons & CYAPA_FNGR_LEFT)
1418 evdev_push_key(sc->evdev, BTN_LEFT,
1419 regs->fngr & CYAPA_FNGR_LEFT);
1420 if (sc->cap_buttons & CYAPA_FNGR_RIGHT)
1421 evdev_push_key(sc->evdev, BTN_RIGHT,
1422 regs->fngr & CYAPA_FNGR_RIGHT);
1423 if (sc->cap_buttons & CYAPA_FNGR_MIDDLE)
1424 evdev_push_key(sc->evdev, BTN_MIDDLE,
1425 regs->fngr & CYAPA_FNGR_MIDDLE);
1426 evdev_sync(sc->evdev);
1427 }
1428 #endif
1429
1430 seen_thumb = 0;
1431 for (i = 0; i < afingers; ) {
1432 if (cyapa_debug) {
1433 printf(" [x=%04d y=%04d p=%d i=%d]",
1434 CYAPA_TOUCH_X(regs, i),
1435 CYAPA_TOUCH_Y(regs, i),
1436 CYAPA_TOUCH_P(regs, i),
1437 regs->touch[i].id);
1438 }
1439 if ((CYAPA_TOUCH_Y(regs, i) > thumbarea_begin && seen_thumb) ||
1440 CYAPA_TOUCH_P(regs, i) < cyapa_minpressure) {
1441 --afingers;
1442 if (i < afingers) {
1443 regs->touch[i] = regs->touch[i+1];
1444 continue;
1445 }
1446 } else {
1447 if (CYAPA_TOUCH_Y(regs, i) > thumbarea_begin)
1448 seen_thumb = 1;
1449 }
1450 ++i;
1451 }
1452 nfingers = afingers;
1453
1454 /* Tracking for local solutions */
1455 cyapa_lock(sc);
1456
1457 /*
1458 * Track timing for finger-downs. Used to detect false-3-finger
1459 * button-down.
1460 */
1461 switch(afingers) {
1462 case 0:
1463 break;
1464 case 1:
1465 if (sc->track_nfingers == 0)
1466 sc->finger1_ticks = sc->poll_ticks;
1467 break;
1468 case 2:
1469 if (sc->track_nfingers <= 0)
1470 sc->finger1_ticks = sc->poll_ticks;
1471 if (sc->track_nfingers <= 1)
1472 sc->finger2_ticks = sc->poll_ticks;
1473 break;
1474 case 3:
1475 default:
1476 if (sc->track_nfingers <= 0)
1477 sc->finger1_ticks = sc->poll_ticks;
1478 if (sc->track_nfingers <= 1)
1479 sc->finger2_ticks = sc->poll_ticks;
1480 if (sc->track_nfingers <= 2)
1481 sc->finger3_ticks = sc->poll_ticks;
1482 break;
1483 }
1484 newfinger = sc->track_nfingers < afingers;
1485 lessfingers = sc->track_nfingers > afingers;
1486 sc->track_nfingers = afingers;
1487
1488 /*
1489 * Lookup and track finger indexes in the touch[] array.
1490 */
1491 if (afingers == 0) {
1492 click_x = sc->track_x;
1493 click_y = sc->track_y;
1494 sc->track_x = -1;
1495 sc->track_y = -1;
1496 sc->track_z = -1;
1497 sc->fuzz_x = 0;
1498 sc->fuzz_y = 0;
1499 sc->fuzz_z = 0;
1500 sc->touch_x = -1;
1501 sc->touch_y = -1;
1502 sc->touch_z = -1;
1503 sc->track_id = -1;
1504 sc->track_but = 0;
1505 i = 0;
1506 j = 0;
1507 } else {
1508 /*
1509 * The id assigned on touch can move around in the array,
1510 * find it. If that finger is lifted up, assign some other
1511 * finger for mouse tracking and reset track_x and track_y
1512 * to avoid a mouse jump.
1513 *
1514 * If >= 2 fingers are down be sure not to assign i and
1515 * j to the same index.
1516 */
1517 for (i = 0; i < nfingers; ++i) {
1518 if (sc->track_id == regs->touch[i].id)
1519 break;
1520 }
1521 if (i == nfingers) {
1522 i = 0;
1523 sc->track_x = -1;
1524 sc->track_y = -1;
1525 sc->track_z = -1;
1526 while (CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin &&
1527 i < nfingers) ++i;
1528 if (i == nfingers) {
1529 i = 0;
1530 }
1531 sc->track_id = regs->touch[i].id;
1532 }
1533 else if ((sc->track_but ||
1534 CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin) &&
1535 newfinger && afingers == 2) {
1536 j = regs->touch[0].id == sc->track_id ? 1 : 0;
1537 if (CYAPA_TOUCH_Y(regs, j) < thumbarea_begin) {
1538 i = j;
1539 sc->track_x = -1;
1540 sc->track_y = -1;
1541 sc->track_z = -1;
1542 sc->track_id = regs->touch[i].id;
1543 }
1544 }
1545 }
1546
1547 /* Two finger scrolling - reset after timeout */
1548 if (sc->track_z != -1 && afingers != 2 &&
1549 (sc->poll_ticks - sc->track_z_ticks) > cyapa_scroll_stick_ticks) {
1550 sc->track_z = -1;
1551 sc->track_z_ticks = 0;
1552 }
1553
1554 /* Initiate two finger scrolling */
1555 if (!(regs->fngr & CYAPA_FNGR_LEFT) &&
1556 ((afingers && sc->track_z != -1) ||
1557 (afingers == 2 && CYAPA_TOUCH_Y(regs, 0) < thumbarea_begin &&
1558 CYAPA_TOUCH_Y(regs, 1) < thumbarea_begin))) {
1559 if (afingers == 2 && (sc->poll_ticks - sc->finger2_ticks)
1560 > cyapa_scroll_wait_ticks) {
1561 z = (CYAPA_TOUCH_Y(regs, 0) +
1562 CYAPA_TOUCH_Y(regs, 1)) >> 1;
1563 sc->delta_z += z / ZSCALE - sc->track_z;
1564 if (sc->track_z == -1) {
1565 sc->delta_z = 0;
1566 }
1567 if (sc->touch_z == -1)
1568 sc->touch_z = z; /* not used atm */
1569 sc->track_z = z / ZSCALE;
1570 sc->track_z_ticks = sc->poll_ticks;
1571 }
1572 } else if (afingers) {
1573 /* Normal pad position reporting */
1574 x = CYAPA_TOUCH_X(regs, i);
1575 y = CYAPA_TOUCH_Y(regs, i);
1576 click_x = x;
1577 click_y = y;
1578 if (sc->track_x != -1 && sc->track_y < thumbarea_begin &&
1579 (afingers > 1 || (sc->poll_ticks - sc->finger1_ticks)
1580 >= cyapa_move_min_ticks || freq < cyapa_norm_freq)) {
1581 sc->delta_x += x - sc->track_x;
1582 sc->delta_y -= y - sc->track_y;
1583 if (sc->delta_x > sc->cap_resx)
1584 sc->delta_x = sc->cap_resx;
1585 if (sc->delta_x < -sc->cap_resx)
1586 sc->delta_x = -sc->cap_resx;
1587 if (sc->delta_y > sc->cap_resy)
1588 sc->delta_y = sc->cap_resy;
1589 if (sc->delta_y < -sc->cap_resy)
1590 sc->delta_y = -sc->cap_resy;
1591
1592 if (abs(sc->delta_y) > sc->cap_resy / 2 ||
1593 abs(sc->delta_x) > sc->cap_resx / 2) {
1594 if (cyapa_debug)
1595 printf("Detected jump by %i %i\n",
1596 sc->delta_x, sc->delta_y);
1597 sc->delta_x = sc->delta_y = 0;
1598 }
1599 }
1600 if (sc->touch_x == -1) {
1601 sc->touch_x = x;
1602 sc->touch_y = y;
1603 }
1604 sc->track_x = x;
1605 sc->track_y = y;
1606 }
1607
1608 /* Select finger (L = 2/3x, M = 1/3u, R = 1/3d) */
1609 int is_tapclick = (cyapa_enable_tapclick && lessfingers &&
1610 afingers == 0 && sc->poll_ticks - sc->finger1_ticks
1611 >= cyapa_tapclick_min_ticks &&
1612 sc->poll_ticks - sc->finger1_ticks < cyapa_tapclick_max_ticks);
1613
1614 if (regs->fngr & CYAPA_FNGR_LEFT || is_tapclick) {
1615 if (sc->track_but) {
1616 but = sc->track_but;
1617 } else if (afingers == 1) {
1618 if (click_x < sc->cap_resx * 2 / 3)
1619 but = CYAPA_FNGR_LEFT;
1620 else if (click_y < sc->cap_resy / 2)
1621 but = CYAPA_FNGR_MIDDLE;
1622 else
1623 but = CYAPA_FNGR_RIGHT;
1624 } else if (is_tapclick) {
1625 if (click_x < sc->cap_resx * 2 / 3 ||
1626 cyapa_enable_tapclick < 2)
1627 but = CYAPA_FNGR_LEFT;
1628 else if (click_y < sc->cap_resy / 2 &&
1629 cyapa_enable_tapclick > 2)
1630 but = CYAPA_FNGR_MIDDLE;
1631 else
1632 but = CYAPA_FNGR_RIGHT;
1633 } else {
1634 but = CYAPA_FNGR_LEFT;
1635 }
1636 } else {
1637 but = 0;
1638 }
1639
1640 /*
1641 * Detect state change from last reported state and
1642 * determine if we have gone idle.
1643 */
1644 sc->track_but = but;
1645 if (sc->delta_x || sc->delta_y || sc->delta_z ||
1646 sc->track_but != sc->reported_but) {
1647 sc->active_tick = ticks;
1648 if (sc->remote_mode == 0 && sc->reporting_mode)
1649 sc->data_signal = 1;
1650 isidle = 0;
1651 } else if ((unsigned)(ticks - sc->active_tick) >= TIME_TO_IDLE) {
1652 sc->active_tick = ticks - TIME_TO_IDLE; /* prevent overflow */
1653 isidle = 1;
1654 } else {
1655 isidle = 0;
1656 }
1657 cyapa_notify(sc);
1658 cyapa_unlock(sc);
1659
1660 if (cyapa_debug)
1661 printf("%i >> %i << %i\n", isidle, sc->track_id, sc->delta_y);
1662 return (isidle);
1663 }
1664
1665 static void
cyapa_set_power_mode(struct cyapa_softc * sc,int mode)1666 cyapa_set_power_mode(struct cyapa_softc *sc, int mode)
1667 {
1668 uint8_t data;
1669 device_t bus;
1670 int error;
1671
1672 bus = device_get_parent(sc->dev);
1673 error = iicbus_request_bus(bus, sc->dev, IIC_WAIT);
1674 if (error == 0) {
1675 error = cyapa_read_bytes(sc->dev, CMD_POWER_MODE,
1676 &data, 1);
1677 data = (data & ~0xFC) | mode;
1678 if (error == 0) {
1679 error = cyapa_write_bytes(sc->dev, CMD_POWER_MODE,
1680 &data, 1);
1681 }
1682 iicbus_release_bus(bus, sc->dev);
1683 }
1684 }
1685
1686 /*
1687 * FIFO FUNCTIONS
1688 */
1689
1690 /*
1691 * Returns non-zero if the fifo is empty
1692 */
1693 static int
fifo_empty(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1694 fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1695 {
1696
1697 CYAPA_LOCK_ASSERT(sc);
1698
1699 return (fifo->rindex == fifo->windex);
1700 }
1701
1702 /*
1703 * Returns the number of characters available for reading from
1704 * the fifo without wrapping the fifo buffer.
1705 */
1706 static size_t
fifo_ready(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1707 fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1708 {
1709 size_t n;
1710
1711 CYAPA_LOCK_ASSERT(sc);
1712
1713 n = CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK);
1714 if (n > (size_t)(fifo->windex - fifo->rindex))
1715 n = (size_t)(fifo->windex - fifo->rindex);
1716 return (n);
1717 }
1718
1719 /*
1720 * Returns a read pointer into the fifo and then bumps
1721 * rindex. The FIFO must have at least 'n' characters in
1722 * it. The value (n) can cause the index to wrap but users
1723 * of the buffer should never supply a value for (n) that wraps
1724 * the buffer.
1725 */
1726 static char *
fifo_read(struct cyapa_softc * sc,struct cyapa_fifo * fifo,size_t n)1727 fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1728 {
1729 char *ptr;
1730
1731 CYAPA_LOCK_ASSERT(sc);
1732 if (n > (CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK))) {
1733 printf("fifo_read: overflow\n");
1734 return (fifo->buf);
1735 }
1736 ptr = fifo->buf + (fifo->rindex & CYAPA_BUFMASK);
1737 fifo->rindex += n;
1738
1739 return (ptr);
1740 }
1741
1742 static uint8_t
fifo_read_char(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1743 fifo_read_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1744 {
1745 uint8_t c;
1746
1747 CYAPA_LOCK_ASSERT(sc);
1748
1749 if (fifo->rindex == fifo->windex) {
1750 printf("fifo_read_char: overflow\n");
1751 c = 0;
1752 } else {
1753 c = fifo->buf[fifo->rindex & CYAPA_BUFMASK];
1754 ++fifo->rindex;
1755 }
1756 return (c);
1757 }
1758
1759
1760 /*
1761 * Write a character to the FIFO. The character will be discarded
1762 * if the FIFO is full.
1763 */
1764 static void
fifo_write_char(struct cyapa_softc * sc,struct cyapa_fifo * fifo,uint8_t c)1765 fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo, uint8_t c)
1766 {
1767
1768 CYAPA_LOCK_ASSERT(sc);
1769
1770 if (fifo->windex - fifo->rindex < CYAPA_BUFSIZE) {
1771 fifo->buf[fifo->windex & CYAPA_BUFMASK] = c;
1772 ++fifo->windex;
1773 }
1774 }
1775
1776 /*
1777 * Return the amount of space available for writing without wrapping
1778 * the fifo.
1779 */
1780 static size_t
fifo_space(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1781 fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1782 {
1783 size_t n;
1784
1785 CYAPA_LOCK_ASSERT(sc);
1786
1787 n = CYAPA_BUFSIZE - (fifo->windex & CYAPA_BUFMASK);
1788 if (n > (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex)))
1789 n = (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex));
1790 return (n);
1791 }
1792
1793 static char *
fifo_write(struct cyapa_softc * sc,struct cyapa_fifo * fifo,size_t n)1794 fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1795 {
1796 char *ptr;
1797
1798 CYAPA_LOCK_ASSERT(sc);
1799
1800 ptr = fifo->buf + (fifo->windex & CYAPA_BUFMASK);
1801 fifo->windex += n;
1802
1803 return (ptr);
1804 }
1805
1806 static void
fifo_reset(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1807 fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1808 {
1809
1810 CYAPA_LOCK_ASSERT(sc);
1811
1812 fifo->rindex = 0;
1813 fifo->windex = 0;
1814 }
1815
1816 /*
1817 * Fuzz handling
1818 */
1819 static int
cyapa_fuzz(int delta,int * fuzzp)1820 cyapa_fuzz(int delta, int *fuzzp)
1821 {
1822 int fuzz;
1823
1824 fuzz = *fuzzp;
1825 if (fuzz >= 0 && delta < 0) {
1826 ++delta;
1827 --fuzz;
1828 } else if (fuzz <= 0 && delta > 0) {
1829 --delta;
1830 ++fuzz;
1831 }
1832 *fuzzp = fuzz;
1833
1834 return (delta);
1835 }
1836
1837 DRIVER_MODULE(cyapa, iicbus, cyapa_driver, NULL, NULL);
1838 MODULE_DEPEND(cyapa, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
1839 #ifdef EVDEV_SUPPORT
1840 MODULE_DEPEND(cyapa, evdev, 1, 1, 1);
1841 #endif
1842 MODULE_VERSION(cyapa, 1);
1843