xref: /freebsd/sys/dev/cyapa/cyapa.c (revision 641f525a87c9a32635ce51a102ebb5c0aa9ff921)
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, &reg },
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, &reg },
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 };
1126 
1127 static int
cyapakqfilter(struct cdev * dev,struct knote * kn)1128 cyapakqfilter(struct cdev *dev, struct knote *kn)
1129 {
1130 	struct cyapa_softc *sc;
1131 	struct knlist *knlist;
1132 
1133 	sc = dev->si_drv1;
1134 
1135 	switch(kn->kn_filter) {
1136 	case EVFILT_READ:
1137 		kn->kn_fop = &cyapa_filtops;
1138 		kn->kn_hook = (void *)sc;
1139 		break;
1140 	default:
1141 		return (EOPNOTSUPP);
1142 	}
1143 	knlist = &sc->selinfo.si_note;
1144 	knlist_add(knlist, kn, 0);
1145 
1146 	return (0);
1147 }
1148 
1149 static int
cyapapoll(struct cdev * dev,int events,struct thread * td)1150 cyapapoll(struct cdev *dev, int events, struct thread *td)
1151 {
1152 	struct cyapa_softc *sc;
1153 	int revents;
1154 
1155 	sc = dev->si_drv1;
1156 	revents = 0;
1157 
1158 	cyapa_lock(sc);
1159 	if (events & (POLLIN | POLLRDNORM)) {
1160 		if (sc->data_signal || !fifo_empty(sc, &sc->rfifo))
1161 			revents = events & (POLLIN | POLLRDNORM);
1162 		else {
1163 			sc->isselect = 1;
1164 			selrecord(td, &sc->selinfo);
1165 		}
1166 	}
1167 	cyapa_unlock(sc);
1168 
1169 	return (revents);
1170 }
1171 
1172 static void
cyapafiltdetach(struct knote * kn)1173 cyapafiltdetach(struct knote *kn)
1174 {
1175 	struct cyapa_softc *sc;
1176 	struct knlist *knlist;
1177 
1178 	sc = (struct cyapa_softc *)kn->kn_hook;
1179 
1180 	knlist = &sc->selinfo.si_note;
1181 	knlist_remove(knlist, kn, 0);
1182 }
1183 
1184 static int
cyapafilt(struct knote * kn,long hint)1185 cyapafilt(struct knote *kn, long hint)
1186 {
1187 	struct cyapa_softc *sc;
1188 	int ready;
1189 
1190 	sc = (struct cyapa_softc *)kn->kn_hook;
1191 
1192 	cyapa_lock(sc);
1193 	ready = fifo_ready(sc, &sc->rfifo) || sc->data_signal;
1194 	cyapa_unlock(sc);
1195 
1196 	return (ready);
1197 }
1198 
1199 static int
cyapaioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)1200 cyapaioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
1201 {
1202 	struct cyapa_softc *sc;
1203 	int error;
1204 
1205 	sc = dev->si_drv1;
1206 	error = 0;
1207 
1208 	cyapa_lock(sc);
1209 	switch (cmd) {
1210 	case MOUSE_GETHWINFO:
1211 		*(mousehw_t *)data = sc->hw;
1212 		if (sc->mode.level == 0)
1213 			((mousehw_t *)data)->model = MOUSE_MODEL_GENERIC;
1214 		break;
1215 
1216 	case MOUSE_GETMODE:
1217 		*(mousemode_t *)data = sc->mode;
1218 		((mousemode_t *)data)->resolution =
1219 		    MOUSE_RES_LOW - sc->mode.resolution;
1220 		switch (sc->mode.level) {
1221 		case 0:
1222 			((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1223 			((mousemode_t *)data)->packetsize =
1224 			    MOUSE_PS2_PACKETSIZE;
1225 			break;
1226 		case 1:
1227 			((mousemode_t *)data)->protocol = MOUSE_PROTO_SYSMOUSE;
1228 			((mousemode_t *)data)->packetsize =
1229 			    MOUSE_SYS_PACKETSIZE;
1230 			break;
1231 		case 2:
1232 			((mousemode_t *)data)->protocol = MOUSE_PROTO_PS2;
1233 			((mousemode_t *)data)->packetsize =
1234 			    MOUSE_PS2_PACKETSIZE + 1;
1235 			break;
1236 		}
1237 		break;
1238 
1239 	case MOUSE_GETLEVEL:
1240 		*(int *)data = sc->mode.level;
1241 		break;
1242 
1243 	case MOUSE_SETLEVEL:
1244 		if ((*(int *)data < 0) &&
1245 		    (*(int *)data > 2)) {
1246 			error = EINVAL;
1247 			break;
1248 		}
1249 		sc->mode.level = *(int *)data;
1250 		sc->zenabled = sc->mode.level ? 1 : 0;
1251 		break;
1252 
1253 	default:
1254 		error = ENOTTY;
1255 		break;
1256 	}
1257 	cyapa_unlock(sc);
1258 
1259 	return (error);
1260 }
1261 
1262 /*
1263  * MAJOR SUPPORT FUNCTIONS
1264  */
1265 static void
cyapa_poll_thread(void * arg)1266 cyapa_poll_thread(void *arg)
1267 {
1268 	struct cyapa_softc *sc;
1269 	struct cyapa_regs regs;
1270 	device_t bus;		/* iicbus */
1271 	int error;
1272 	int freq;
1273 	int isidle;
1274 	int pstate;
1275 	int npstate;
1276 	int last_reset;
1277 
1278 	sc = arg;
1279 	freq = cyapa_norm_freq;
1280 	isidle = 0;
1281 	pstate = CMD_POWER_MODE_IDLE;
1282 	last_reset = ticks;
1283 
1284 	bus = device_get_parent(sc->dev);
1285 
1286 	cyapa_lock(sc);
1287 	sc->poll_thread_running = 1;
1288 
1289 	while (!sc->detaching) {
1290 		cyapa_unlock(sc);
1291 		error = iicbus_request_bus(bus, sc->dev, IIC_WAIT);
1292 		if (error == 0) {
1293 			error = cyapa_read_bytes(sc->dev, CMD_DEV_STATUS,
1294 			    (void *)&regs, sizeof(regs));
1295 			if (error == 0) {
1296 				isidle = cyapa_raw_input(sc, &regs, freq);
1297 			}
1298 
1299 			/*
1300 			 * For some reason the device can crap-out.  If it
1301 			 * drops back into bootstrap mode try to reinitialize
1302 			 * it.
1303 			 */
1304 			if (cyapa_reset ||
1305 			    ((regs.stat & CYAPA_STAT_RUNNING) == 0 &&
1306 			     (unsigned)(ticks - last_reset) > TIME_TO_RESET)) {
1307 				cyapa_reset = 0;
1308 				last_reset = ticks;
1309 				init_device(sc->dev, NULL, 2);
1310 			}
1311 			iicbus_release_bus(bus, sc->dev);
1312 		}
1313 		pause("cyapw", hz / freq);
1314 		++sc->poll_ticks;
1315 
1316 		if (sc->count == 0) {
1317 			freq = cyapa_idle_freq;
1318 			npstate = CMD_POWER_MODE_IDLE;
1319 		} else if (isidle) {
1320 			freq = cyapa_slow_freq;
1321 			npstate = CMD_POWER_MODE_IDLE;
1322 		} else {
1323 			freq = cyapa_norm_freq;
1324 			npstate = CMD_POWER_MODE_FULL;
1325 		}
1326 		if (pstate != npstate) {
1327 			pstate = npstate;
1328 			cyapa_set_power_mode(sc, pstate);
1329 			if (cyapa_debug) {
1330 				switch(pstate) {
1331 				case CMD_POWER_MODE_OFF:
1332 					printf("cyapa: power off\n");
1333 					break;
1334 				case CMD_POWER_MODE_IDLE:
1335 					printf("cyapa: power idle\n");
1336 					break;
1337 				case CMD_POWER_MODE_FULL:
1338 					printf("cyapa: power full\n");
1339 					break;
1340 				}
1341 			}
1342 		}
1343 
1344 		cyapa_lock(sc);
1345 	}
1346 	sc->poll_thread_running = 0;
1347 	cyapa_unlock(sc);
1348 	kthread_exit();
1349 }
1350 
1351 static int
cyapa_raw_input(struct cyapa_softc * sc,struct cyapa_regs * regs,int freq)1352 cyapa_raw_input(struct cyapa_softc *sc, struct cyapa_regs *regs, int freq)
1353 {
1354 	int nfingers;
1355 	int afingers;	/* actual fingers after culling */
1356 	int i;
1357 	int j;
1358 	int isidle;
1359 	int thumbarea_begin;
1360 	int seen_thumb;
1361 	int x;
1362 	int y;
1363 	int z;
1364 	int newfinger;
1365 	int lessfingers;
1366 	int click_x;
1367 	int click_y;
1368 	uint16_t but;	/* high bits used for simulated but4/but5 */
1369 
1370 	thumbarea_begin = sc->cap_resy -
1371 	    ((sc->cap_resy *  cyapa_thumbarea_percent) / 100);
1372 	click_x = click_y = 0;
1373 
1374 	/*
1375 	 * If the device is not running the rest of the status
1376 	 * means something else, set fingers to 0.
1377 	 */
1378 	if ((regs->stat & CYAPA_STAT_RUNNING) == 0) {
1379 		regs->fngr = 0;
1380 	}
1381 
1382 	/* Process fingers/movement */
1383 	nfingers = CYAPA_FNGR_NUMFINGERS(regs->fngr);
1384 	afingers = nfingers;
1385 
1386 	if (cyapa_debug) {
1387 		printf("stat %02x buttons %c%c%c nfngrs=%d ",
1388 		    regs->stat,
1389 		    ((regs->fngr & CYAPA_FNGR_LEFT) ? 'L' : '-'),
1390 		    ((regs->fngr & CYAPA_FNGR_MIDDLE) ? 'M' : '-'),
1391 		    ((regs->fngr & CYAPA_FNGR_RIGHT) ? 'R' : '-'),
1392 		    nfingers);
1393 	}
1394 
1395 #ifdef EVDEV_SUPPORT
1396 	if (evdev_rcpt_mask & EVDEV_RCPT_HW_MOUSE) {
1397 		for (i = 0; i < nfingers; ++i) {
1398 			int slot = evdev_mt_id_to_slot(
1399 			    sc->evdev, regs->touch[i].id);
1400 			if (slot == -1) {
1401 				if (cyapa_debug)
1402 					printf("Slot overflow for i=%d\n",
1403 					    regs->touch[i].id);
1404 				continue;
1405 			}
1406 			evdev_push_abs(sc->evdev, ABS_MT_SLOT, slot);
1407 			evdev_push_abs(sc->evdev, ABS_MT_TRACKING_ID,
1408 			    regs->touch[i].id);
1409 			evdev_push_abs(sc->evdev, ABS_MT_POSITION_X,
1410 			    CYAPA_TOUCH_X(regs, i));
1411 			evdev_push_abs(sc->evdev, ABS_MT_POSITION_Y,
1412 			    CYAPA_TOUCH_Y(regs, i));
1413 			evdev_push_abs(sc->evdev, ABS_MT_PRESSURE,
1414 			    CYAPA_TOUCH_P(regs, i));
1415 		}
1416 		if (sc->cap_buttons & CYAPA_FNGR_LEFT)
1417 			evdev_push_key(sc->evdev, BTN_LEFT,
1418 			    regs->fngr & CYAPA_FNGR_LEFT);
1419 		if (sc->cap_buttons & CYAPA_FNGR_RIGHT)
1420 			evdev_push_key(sc->evdev, BTN_RIGHT,
1421 			    regs->fngr & CYAPA_FNGR_RIGHT);
1422 		if (sc->cap_buttons & CYAPA_FNGR_MIDDLE)
1423 			evdev_push_key(sc->evdev, BTN_MIDDLE,
1424 			    regs->fngr & CYAPA_FNGR_MIDDLE);
1425 		evdev_sync(sc->evdev);
1426 	}
1427 #endif
1428 
1429 	seen_thumb = 0;
1430 	for (i = 0; i < afingers; ) {
1431 		if (cyapa_debug) {
1432 			printf(" [x=%04d y=%04d p=%d i=%d]",
1433 			    CYAPA_TOUCH_X(regs, i),
1434 			    CYAPA_TOUCH_Y(regs, i),
1435 			    CYAPA_TOUCH_P(regs, i),
1436 			    regs->touch[i].id);
1437 		}
1438 		if ((CYAPA_TOUCH_Y(regs, i) > thumbarea_begin && seen_thumb) ||
1439 		     CYAPA_TOUCH_P(regs, i) < cyapa_minpressure) {
1440 			--afingers;
1441 			if (i < afingers) {
1442 			    regs->touch[i] = regs->touch[i+1];
1443 			    continue;
1444 			}
1445 		} else {
1446 			if (CYAPA_TOUCH_Y(regs, i) > thumbarea_begin)
1447 			    seen_thumb = 1;
1448 		}
1449 		++i;
1450 	}
1451 	nfingers = afingers;
1452 
1453 	/* Tracking for local solutions */
1454 	cyapa_lock(sc);
1455 
1456 	/*
1457 	 * Track timing for finger-downs.  Used to detect false-3-finger
1458 	 * button-down.
1459 	 */
1460 	switch(afingers) {
1461 	case 0:
1462 		break;
1463 	case 1:
1464 		if (sc->track_nfingers == 0)
1465 			sc->finger1_ticks = sc->poll_ticks;
1466 		break;
1467 	case 2:
1468 		if (sc->track_nfingers <= 0)
1469 			sc->finger1_ticks = sc->poll_ticks;
1470 		if (sc->track_nfingers <= 1)
1471 			sc->finger2_ticks = sc->poll_ticks;
1472 		break;
1473 	case 3:
1474 	default:
1475 		if (sc->track_nfingers <= 0)
1476 			sc->finger1_ticks = sc->poll_ticks;
1477 		if (sc->track_nfingers <= 1)
1478 			sc->finger2_ticks = sc->poll_ticks;
1479 		if (sc->track_nfingers <= 2)
1480 			sc->finger3_ticks = sc->poll_ticks;
1481 		break;
1482 	}
1483 	newfinger = sc->track_nfingers < afingers;
1484 	lessfingers = sc->track_nfingers > afingers;
1485 	sc->track_nfingers = afingers;
1486 
1487 	/*
1488 	 * Lookup and track finger indexes in the touch[] array.
1489 	 */
1490 	if (afingers == 0) {
1491 		click_x = sc->track_x;
1492 		click_y = sc->track_y;
1493 		sc->track_x = -1;
1494 		sc->track_y = -1;
1495 		sc->track_z = -1;
1496 		sc->fuzz_x = 0;
1497 		sc->fuzz_y = 0;
1498 		sc->fuzz_z = 0;
1499 		sc->touch_x = -1;
1500 		sc->touch_y = -1;
1501 		sc->touch_z = -1;
1502 		sc->track_id = -1;
1503 		sc->track_but = 0;
1504 		i = 0;
1505 		j = 0;
1506 	} else {
1507 		/*
1508 		 * The id assigned on touch can move around in the array,
1509 		 * find it.  If that finger is lifted up, assign some other
1510 		 * finger for mouse tracking and reset track_x and track_y
1511 		 * to avoid a mouse jump.
1512 		 *
1513 		 * If >= 2 fingers are down be sure not to assign i and
1514 		 * j to the same index.
1515 		 */
1516 		for (i = 0; i < nfingers; ++i) {
1517 			if (sc->track_id == regs->touch[i].id)
1518 				break;
1519 		}
1520 		if (i == nfingers) {
1521 			i = 0;
1522 			sc->track_x = -1;
1523 			sc->track_y = -1;
1524 			sc->track_z = -1;
1525 			while (CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin &&
1526 			    i < nfingers) ++i;
1527 			if (i == nfingers) {
1528 				i = 0;
1529 			}
1530 			sc->track_id = regs->touch[i].id;
1531 		}
1532 		else if ((sc->track_but ||
1533 		     CYAPA_TOUCH_Y(regs, i) >= thumbarea_begin) &&
1534 		    newfinger && afingers == 2) {
1535 			j = regs->touch[0].id == sc->track_id ? 1 : 0;
1536 			if (CYAPA_TOUCH_Y(regs, j) < thumbarea_begin) {
1537 			    i = j;
1538 			    sc->track_x = -1;
1539 			    sc->track_y = -1;
1540 			    sc->track_z = -1;
1541 			    sc->track_id = regs->touch[i].id;
1542 			}
1543 		}
1544 	}
1545 
1546 	/* Two finger scrolling - reset after timeout */
1547 	if (sc->track_z != -1 && afingers != 2 &&
1548 	    (sc->poll_ticks - sc->track_z_ticks) > cyapa_scroll_stick_ticks) {
1549 		sc->track_z = -1;
1550 		sc->track_z_ticks = 0;
1551 	}
1552 
1553 	/* Initiate two finger scrolling */
1554 	if (!(regs->fngr & CYAPA_FNGR_LEFT) &&
1555 	    ((afingers && sc->track_z != -1) ||
1556 	     (afingers == 2 && CYAPA_TOUCH_Y(regs, 0) < thumbarea_begin &&
1557 	     CYAPA_TOUCH_Y(regs, 1) < thumbarea_begin))) {
1558 		if (afingers == 2 && (sc->poll_ticks - sc->finger2_ticks)
1559 		    > cyapa_scroll_wait_ticks) {
1560 			z = (CYAPA_TOUCH_Y(regs, 0) +
1561 			    CYAPA_TOUCH_Y(regs, 1)) >> 1;
1562 			sc->delta_z += z / ZSCALE - sc->track_z;
1563 			if (sc->track_z == -1) {
1564 			    sc->delta_z = 0;
1565 			}
1566 			if (sc->touch_z == -1)
1567 			    sc->touch_z = z;	/* not used atm */
1568 			sc->track_z = z / ZSCALE;
1569 			sc->track_z_ticks = sc->poll_ticks;
1570 		}
1571 	} else if (afingers) {
1572 		/* Normal pad position reporting */
1573 		x = CYAPA_TOUCH_X(regs, i);
1574 		y = CYAPA_TOUCH_Y(regs, i);
1575 		click_x = x;
1576 		click_y = y;
1577 		if (sc->track_x != -1 && sc->track_y < thumbarea_begin &&
1578 		    (afingers > 1 || (sc->poll_ticks - sc->finger1_ticks)
1579 		    >= cyapa_move_min_ticks || freq < cyapa_norm_freq)) {
1580 			sc->delta_x += x - sc->track_x;
1581 			sc->delta_y -= y - sc->track_y;
1582 			if (sc->delta_x > sc->cap_resx)
1583 				sc->delta_x = sc->cap_resx;
1584 			if (sc->delta_x < -sc->cap_resx)
1585 				sc->delta_x = -sc->cap_resx;
1586 			if (sc->delta_y > sc->cap_resy)
1587 				sc->delta_y = sc->cap_resy;
1588 			if (sc->delta_y < -sc->cap_resy)
1589 				sc->delta_y = -sc->cap_resy;
1590 
1591 			if (abs(sc->delta_y) > sc->cap_resy / 2 ||
1592 			    abs(sc->delta_x) > sc->cap_resx / 2) {
1593 				if (cyapa_debug)
1594 					printf("Detected jump by %i %i\n",
1595 					    sc->delta_x, sc->delta_y);
1596 			    sc->delta_x = sc->delta_y = 0;
1597 			}
1598 		}
1599 		if (sc->touch_x == -1) {
1600 			sc->touch_x = x;
1601 			sc->touch_y = y;
1602 		}
1603 		sc->track_x = x;
1604 		sc->track_y = y;
1605 	}
1606 
1607 	/* Select finger (L = 2/3x, M = 1/3u, R = 1/3d) */
1608 	int is_tapclick = (cyapa_enable_tapclick && lessfingers &&
1609 	    afingers == 0 && sc->poll_ticks - sc->finger1_ticks
1610 	    >= cyapa_tapclick_min_ticks &&
1611 	    sc->poll_ticks - sc->finger1_ticks < cyapa_tapclick_max_ticks);
1612 
1613 	if (regs->fngr & CYAPA_FNGR_LEFT || is_tapclick) {
1614 		if (sc->track_but) {
1615 			but = sc->track_but;
1616 		} else if (afingers == 1) {
1617 			if (click_x < sc->cap_resx * 2 / 3)
1618 				but = CYAPA_FNGR_LEFT;
1619 			else if (click_y < sc->cap_resy / 2)
1620 				but = CYAPA_FNGR_MIDDLE;
1621 			else
1622 				but = CYAPA_FNGR_RIGHT;
1623 		} else if (is_tapclick) {
1624 			if (click_x < sc->cap_resx * 2 / 3 ||
1625 			    cyapa_enable_tapclick < 2)
1626 				but = CYAPA_FNGR_LEFT;
1627 			else if (click_y < sc->cap_resy / 2 &&
1628 			    cyapa_enable_tapclick > 2)
1629 				but = CYAPA_FNGR_MIDDLE;
1630 			else
1631 				but = CYAPA_FNGR_RIGHT;
1632 		} else {
1633 			but = CYAPA_FNGR_LEFT;
1634 		}
1635 	} else {
1636 		but = 0;
1637 	}
1638 
1639 	/*
1640 	 * Detect state change from last reported state and
1641 	 * determine if we have gone idle.
1642 	 */
1643 	sc->track_but = but;
1644 	if (sc->delta_x || sc->delta_y || sc->delta_z ||
1645 	    sc->track_but != sc->reported_but) {
1646 		sc->active_tick = ticks;
1647 		if (sc->remote_mode == 0 && sc->reporting_mode)
1648 			sc->data_signal = 1;
1649 		isidle = 0;
1650 	} else if ((unsigned)(ticks - sc->active_tick) >= TIME_TO_IDLE) {
1651 		sc->active_tick = ticks - TIME_TO_IDLE; /* prevent overflow */
1652 		isidle = 1;
1653 	} else {
1654 		isidle = 0;
1655 	}
1656 	cyapa_notify(sc);
1657 	cyapa_unlock(sc);
1658 
1659 	if (cyapa_debug)
1660 		printf("%i >> %i << %i\n", isidle, sc->track_id, sc->delta_y);
1661 	return (isidle);
1662 }
1663 
1664 static void
cyapa_set_power_mode(struct cyapa_softc * sc,int mode)1665 cyapa_set_power_mode(struct cyapa_softc *sc, int mode)
1666 {
1667 	uint8_t data;
1668 	device_t bus;
1669 	int error;
1670 
1671 	bus = device_get_parent(sc->dev);
1672 	error = iicbus_request_bus(bus, sc->dev, IIC_WAIT);
1673 	if (error == 0) {
1674 		error = cyapa_read_bytes(sc->dev, CMD_POWER_MODE,
1675 		    &data, 1);
1676 		data = (data & ~0xFC) | mode;
1677 		if (error == 0) {
1678 			error = cyapa_write_bytes(sc->dev, CMD_POWER_MODE,
1679 			    &data, 1);
1680 		}
1681 		iicbus_release_bus(bus, sc->dev);
1682 	}
1683 }
1684 
1685 /*
1686  * FIFO FUNCTIONS
1687  */
1688 
1689 /*
1690  * Returns non-zero if the fifo is empty
1691  */
1692 static int
fifo_empty(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1693 fifo_empty(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1694 {
1695 
1696 	CYAPA_LOCK_ASSERT(sc);
1697 
1698 	return (fifo->rindex == fifo->windex);
1699 }
1700 
1701 /*
1702  * Returns the number of characters available for reading from
1703  * the fifo without wrapping the fifo buffer.
1704  */
1705 static size_t
fifo_ready(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1706 fifo_ready(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1707 {
1708 	size_t n;
1709 
1710 	CYAPA_LOCK_ASSERT(sc);
1711 
1712 	n = CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK);
1713 	if (n > (size_t)(fifo->windex - fifo->rindex))
1714 		n = (size_t)(fifo->windex - fifo->rindex);
1715 	return (n);
1716 }
1717 
1718 /*
1719  * Returns a read pointer into the fifo and then bumps
1720  * rindex.  The FIFO must have at least 'n' characters in
1721  * it.  The value (n) can cause the index to wrap but users
1722  * of the buffer should never supply a value for (n) that wraps
1723  * the buffer.
1724  */
1725 static char *
fifo_read(struct cyapa_softc * sc,struct cyapa_fifo * fifo,size_t n)1726 fifo_read(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1727 {
1728 	char *ptr;
1729 
1730 	CYAPA_LOCK_ASSERT(sc);
1731 	if (n > (CYAPA_BUFSIZE - (fifo->rindex & CYAPA_BUFMASK))) {
1732 		printf("fifo_read: overflow\n");
1733 		return (fifo->buf);
1734 	}
1735 	ptr = fifo->buf + (fifo->rindex & CYAPA_BUFMASK);
1736 	fifo->rindex += n;
1737 
1738 	return (ptr);
1739 }
1740 
1741 static uint8_t
fifo_read_char(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1742 fifo_read_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1743 {
1744 	uint8_t c;
1745 
1746 	CYAPA_LOCK_ASSERT(sc);
1747 
1748 	if (fifo->rindex == fifo->windex) {
1749 		printf("fifo_read_char: overflow\n");
1750 		c = 0;
1751 	} else {
1752 		c = fifo->buf[fifo->rindex & CYAPA_BUFMASK];
1753 		++fifo->rindex;
1754 	}
1755 	return (c);
1756 }
1757 
1758 
1759 /*
1760  * Write a character to the FIFO.  The character will be discarded
1761  * if the FIFO is full.
1762  */
1763 static void
fifo_write_char(struct cyapa_softc * sc,struct cyapa_fifo * fifo,uint8_t c)1764 fifo_write_char(struct cyapa_softc *sc, struct cyapa_fifo *fifo, uint8_t c)
1765 {
1766 
1767 	CYAPA_LOCK_ASSERT(sc);
1768 
1769 	if (fifo->windex - fifo->rindex < CYAPA_BUFSIZE) {
1770 		fifo->buf[fifo->windex & CYAPA_BUFMASK] = c;
1771 		++fifo->windex;
1772 	}
1773 }
1774 
1775 /*
1776  * Return the amount of space available for writing without wrapping
1777  * the fifo.
1778  */
1779 static size_t
fifo_space(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1780 fifo_space(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1781 {
1782 	size_t n;
1783 
1784 	CYAPA_LOCK_ASSERT(sc);
1785 
1786 	n = CYAPA_BUFSIZE - (fifo->windex & CYAPA_BUFMASK);
1787 	if (n > (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex)))
1788 		n = (size_t)(CYAPA_BUFSIZE - (fifo->windex - fifo->rindex));
1789 	return (n);
1790 }
1791 
1792 static char *
fifo_write(struct cyapa_softc * sc,struct cyapa_fifo * fifo,size_t n)1793 fifo_write(struct cyapa_softc *sc, struct cyapa_fifo *fifo, size_t n)
1794 {
1795 	char *ptr;
1796 
1797 	CYAPA_LOCK_ASSERT(sc);
1798 
1799 	ptr = fifo->buf + (fifo->windex & CYAPA_BUFMASK);
1800 	fifo->windex += n;
1801 
1802 	return (ptr);
1803 }
1804 
1805 static void
fifo_reset(struct cyapa_softc * sc,struct cyapa_fifo * fifo)1806 fifo_reset(struct cyapa_softc *sc, struct cyapa_fifo *fifo)
1807 {
1808 
1809 	CYAPA_LOCK_ASSERT(sc);
1810 
1811 	fifo->rindex = 0;
1812 	fifo->windex = 0;
1813 }
1814 
1815 /*
1816  * Fuzz handling
1817  */
1818 static int
cyapa_fuzz(int delta,int * fuzzp)1819 cyapa_fuzz(int delta, int *fuzzp)
1820 {
1821 	int fuzz;
1822 
1823 	fuzz = *fuzzp;
1824 	if (fuzz >= 0 && delta < 0) {
1825 		++delta;
1826 		--fuzz;
1827 	} else if (fuzz <= 0 && delta > 0) {
1828 		--delta;
1829 		++fuzz;
1830 	}
1831 	*fuzzp = fuzz;
1832 
1833 	return (delta);
1834 }
1835 
1836 DRIVER_MODULE(cyapa, iicbus, cyapa_driver, NULL, NULL);
1837 MODULE_DEPEND(cyapa, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
1838 #ifdef EVDEV_SUPPORT
1839 MODULE_DEPEND(cyapa, evdev, 1, 1, 1);
1840 #endif
1841 MODULE_VERSION(cyapa, 1);
1842