xref: /freebsd/sys/dev/adb/adb_mouse.c (revision 1f40866feb2135a4cf764a07b1b90a8a3398ff0a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2008 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/lock.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/mouse.h>
39 #include <sys/poll.h>
40 #include <sys/condvar.h>
41 #include <sys/selinfo.h>
42 #include <sys/sysctl.h>
43 #include <sys/uio.h>
44 #include <sys/fcntl.h>
45 #include <sys/kernel.h>
46 
47 #include <machine/bus.h>
48 
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 
52 #include "adb.h"
53 
54 #define CDEV_GET_SOFTC(x) (x)->si_drv1
55 
56 static int adb_mouse_probe(device_t dev);
57 static int adb_mouse_attach(device_t dev);
58 static int adb_mouse_detach(device_t dev);
59 static void adb_init_trackpad(device_t dev);
60 static int adb_tapping_sysctl(SYSCTL_HANDLER_ARGS);
61 
62 static d_open_t  ams_open;
63 static d_close_t ams_close;
64 static d_read_t  ams_read;
65 static d_ioctl_t ams_ioctl;
66 static d_poll_t  ams_poll;
67 
68 static u_int adb_mouse_receive_packet(device_t dev, u_char status,
69     u_char command, u_char reg, int len, u_char *data);
70 
71 struct adb_mouse_softc {
72 	device_t sc_dev;
73 
74 	struct mtx sc_mtx;
75 	struct cv  sc_cv;
76 
77 	int flags;
78 #define	AMS_EXTENDED	0x1
79 #define	AMS_TOUCHPAD	0x2
80 	uint16_t dpi;
81 
82 	mousehw_t hw;
83 	mousemode_t mode;
84 	u_char id[4];
85 
86 	int buttons;
87 	u_int sc_tapping;
88 	int button_buf;
89 	int last_buttons;
90 	int xdelta, ydelta;
91 
92 	int8_t packet[8];
93 	size_t packet_read_len;
94 
95 	struct cdev *cdev;
96 	struct selinfo rsel;
97 };
98 
99 static device_method_t adb_mouse_methods[] = {
100 	/* Device interface */
101 	DEVMETHOD(device_probe,         adb_mouse_probe),
102         DEVMETHOD(device_attach,        adb_mouse_attach),
103         DEVMETHOD(device_detach,        adb_mouse_detach),
104         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
105         DEVMETHOD(device_suspend,       bus_generic_suspend),
106         DEVMETHOD(device_resume,        bus_generic_resume),
107 
108 	/* ADB interface */
109 	DEVMETHOD(adb_receive_packet,	adb_mouse_receive_packet),
110 	{ 0, 0 }
111 };
112 
113 static driver_t adb_mouse_driver = {
114 	"ams",
115 	adb_mouse_methods,
116 	sizeof(struct adb_mouse_softc),
117 };
118 
119 DRIVER_MODULE(ams, adb, adb_mouse_driver, 0, 0);
120 
121 static struct cdevsw ams_cdevsw = {
122 	.d_version = 	D_VERSION,
123 	.d_flags   = 	0,
124 	.d_open    =	ams_open,
125 	.d_close   =	ams_close,
126 	.d_read    =	ams_read,
127 	.d_ioctl   =	ams_ioctl,
128 	.d_poll    =	ams_poll,
129 	.d_name    =	"ams",
130 };
131 
132 static int
133 adb_mouse_probe(device_t dev)
134 {
135 	uint8_t type;
136 
137 	type = adb_get_device_type(dev);
138 
139 	if (type != ADB_DEVICE_MOUSE)
140 		return (ENXIO);
141 
142 	device_set_desc(dev,"ADB Mouse");
143 	return (0);
144 }
145 
146 static int
147 adb_mouse_attach(device_t dev)
148 {
149 	struct adb_mouse_softc *sc;
150 	char *description = "Unknown Pointing Device";
151 
152 	size_t r1_len;
153 	u_char r1[8];
154 
155 	sc = device_get_softc(dev);
156 	sc->sc_dev = dev;
157 
158 	mtx_init(&sc->sc_mtx, "ams", NULL, MTX_DEF);
159 	cv_init(&sc->sc_cv,"ams");
160 
161 	sc->flags = 0;
162 
163 	sc->hw.buttons = 2;
164 	sc->hw.iftype = MOUSE_IF_UNKNOWN;
165 	sc->hw.type = MOUSE_UNKNOWN;
166 	sc->hw.model = sc->hw.hwid = 0;
167 
168 	sc->mode.protocol = MOUSE_PROTO_SYSMOUSE;
169 	sc->mode.rate = -1;
170 	sc->mode.resolution = 100;
171 	sc->mode.accelfactor = 0;
172 	sc->mode.level = 0;
173 	sc->mode.packetsize = 5;
174 
175 	sc->buttons = 0;
176 	sc->sc_tapping = 0;
177 	sc->button_buf = 0;
178 	sc->last_buttons = 0;
179 	sc->packet_read_len = 0;
180 
181 	/* Try to switch to extended protocol */
182 	adb_set_device_handler(dev,4);
183 
184 	switch(adb_get_device_handler(dev)) {
185 	case 1:
186 		sc->mode.resolution = 100;
187 		break;
188 	case 2:
189 		sc->mode.resolution = 200;
190 		break;
191 	case 4:
192 		r1_len = adb_read_register(dev,1,r1);
193 		if (r1_len < 8)
194 			break;
195 
196 		sc->flags |= AMS_EXTENDED;
197 		memcpy(&sc->hw.hwid,r1,4);
198 		sc->mode.resolution = (r1[4] << 8) | r1[5];
199 
200 		switch (r1[6]) {
201 		case 0:
202 			sc->hw.type = MOUSE_PAD;
203 			description = "Tablet";
204 			break;
205 		case 1:
206 			sc->hw.type = MOUSE_MOUSE;
207 			description = "Mouse";
208 			break;
209 		case 2:
210 			sc->hw.type = MOUSE_TRACKBALL;
211 			description = "Trackball";
212 			break;
213 		case 3:
214 			sc->flags |= AMS_TOUCHPAD;
215 			sc->hw.type = MOUSE_PAD;
216 			adb_init_trackpad(dev);
217 			description = "Touchpad";
218 			break;
219 		}
220 
221 		sc->hw.buttons = r1[7];
222 
223 		device_printf(dev,"%d-button %d-dpi %s\n",
224 		    sc->hw.buttons, sc->mode.resolution,description);
225 
226 		/*
227 		 * Check for one of MacAlly's non-compliant 2-button mice.
228 		 * These claim to speak the extended mouse protocol, but
229 		 * instead speak the standard protocol and only when their
230 		 * handler is set to 0x42.
231 		 */
232 
233 		if (sc->hw.hwid == 0x4b4f4954) {
234 			adb_set_device_handler(dev,0x42);
235 
236 			if (adb_get_device_handler(dev) == 0x42) {
237 				device_printf(dev, "MacAlly 2-Button Mouse\n");
238 				sc->flags &= ~AMS_EXTENDED;
239 			}
240 		}
241 
242 		break;
243 	}
244 
245 	sc->cdev = make_dev(&ams_cdevsw, device_get_unit(dev),
246 		       UID_ROOT, GID_OPERATOR, 0644, "ams%d",
247 		       device_get_unit(dev));
248 	sc->cdev->si_drv1 = sc;
249 
250 	adb_set_autopoll(dev,1);
251 
252 	return (0);
253 }
254 
255 static int
256 adb_mouse_detach(device_t dev)
257 {
258 	struct adb_mouse_softc *sc;
259 
260 	adb_set_autopoll(dev,0);
261 
262 	sc = device_get_softc(dev);
263 	destroy_dev(sc->cdev);
264 
265 	mtx_destroy(&sc->sc_mtx);
266 	cv_destroy(&sc->sc_cv);
267 
268 	return (0);
269 }
270 
271 static void
272 adb_init_trackpad(device_t dev)
273 {
274 	struct adb_mouse_softc *sc;
275 	struct sysctl_ctx_list *ctx;
276 	struct sysctl_oid *tree;
277 
278 	size_t r1_len;
279 	u_char r1[8];
280 	u_char r2[8];
281 
282 	sc = device_get_softc(dev);
283 
284 	r1_len = adb_read_register(dev, 1, r1);
285 
286 	/* An Extended Mouse register1 must return 8 bytes. */
287 	if (r1_len != 8)
288 		return;
289 
290 	if((r1[6] != 0x0d))
291 	{
292 		r1[6] = 0x0d;
293 
294 		adb_write_register(dev, 1, 8, r1);
295 
296 		r1_len = adb_read_register(dev, 1, r1);
297 
298 		if (r1[6] != 0x0d)
299 		{
300 			device_printf(dev, "ADB Mouse = 0x%x "
301 				      "(non-Extended Mode)\n", r1[6]);
302 			return;
303 		} else {
304 			device_printf(dev, "ADB Mouse = 0x%x "
305 				      "(Extended Mode)\n", r1[6]);
306 
307 			/* Set ADB Extended Features to default values,
308 			   enabled. */
309 			r2[0] = 0x19; /* Clicking: 0x19 disabled 0x99 enabled */
310 			r2[1] = 0x94; /* Dragging: 0x14 disabled 0x94 enabled */
311 			r2[2] = 0x19;
312 			r2[3] = 0xff; /* DragLock: 0xff disabled 0xb2 enabled */
313 			r2[4] = 0xb2;
314 			r2[5] = 0x8a;
315 			r2[6] = 0x1b;
316 
317 			r2[7] = 0x57;  /* 0x57 bits 3:0 for W mode */
318 
319 			adb_write_register(dev, 2, 8, r2);
320 
321 		}
322 	}
323 
324 	/*
325 	 * Set up sysctl
326 	 */
327 	ctx = device_get_sysctl_ctx(dev);
328 	tree = device_get_sysctl_tree(dev);
329 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "tapping",
330 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
331 	    adb_tapping_sysctl, "I", "Tapping the pad causes button events");
332 	return;
333 }
334 
335 static u_int
336 adb_mouse_receive_packet(device_t dev, u_char status, u_char command,
337     u_char reg, int len, u_char *data)
338 {
339 	struct adb_mouse_softc *sc;
340 	int i = 0;
341 	int xdelta, ydelta;
342 	int buttons, tmp_buttons;
343 
344 	sc = device_get_softc(dev);
345 
346 	if (command != ADB_COMMAND_TALK || reg != 0 || len < 2)
347 		return (0);
348 
349 	ydelta = data[0] & 0x7f;
350 	xdelta = data[1] & 0x7f;
351 
352 	buttons = 0;
353 	buttons |= !(data[0] & 0x80);
354 	buttons |= !(data[1] & 0x80) << 1;
355 
356 	if (sc->flags & AMS_EXTENDED) {
357 		for (i = 2; i < len && i < 5; i++) {
358 			xdelta |= (data[i] & 0x07) << (3*i + 1);
359 			ydelta |= (data[i] & 0x70) << (3*i - 3);
360 
361 			buttons |= !(data[i] & 0x08) << (2*i - 2);
362 			buttons |= !(data[i] & 0x80) << (2*i - 1);
363 		}
364 	} else {
365 		len = 2; /* Ignore extra data */
366 	}
367 
368 	/* Do sign extension as necessary */
369 	if (xdelta & (0x40 << 3*(len-2)))
370 		xdelta |= 0xffffffc0 << 3*(len - 2);
371 	if (ydelta & (0x40 << 3*(len-2)))
372 		ydelta |= 0xffffffc0 << 3*(len - 2);
373 
374 	if ((sc->flags & AMS_TOUCHPAD) && (sc->sc_tapping == 1)) {
375 		tmp_buttons = buttons;
376 		if (buttons == 0x12) {
377 			/* Map a double tap on button 3.
378 			   Keep the button state for the next sequence.
379 			   A double tap sequence is followed by a single tap
380 			   sequence.
381 			*/
382 			tmp_buttons = 0x3;
383 			sc->button_buf = tmp_buttons;
384 		} else if (buttons == 0x2) {
385 			/* Map a single tap on button 2. But only if it is
386 			   not a successor from a double tap.
387 			*/
388 			if (sc->button_buf != 0x3)
389 				tmp_buttons = 0x2;
390 			else
391 				tmp_buttons = 0;
392 
393 			sc->button_buf = 0;
394 		}
395 		buttons = tmp_buttons;
396 	}
397 
398 	/*
399 	 * Some mice report high-numbered buttons on the wrong button number,
400 	 * so set the highest-numbered real button as pressed if there are
401 	 * mysterious high-numbered ones set.
402 	 *
403 	 * Don't do this for touchpads, because touchpads also trigger
404 	 * high button events when they are touched.
405 	 */
406 
407 	if (rounddown2(buttons, 1 << sc->hw.buttons)
408 	    && !(sc->flags & AMS_TOUCHPAD)) {
409 		buttons |= 1 << (sc->hw.buttons - 1);
410 	}
411 	buttons &= (1 << sc->hw.buttons) - 1;
412 
413 	mtx_lock(&sc->sc_mtx);
414 
415 	/* Add in our new deltas, and take into account
416 	   Apple's opposite meaning for Y axis motion */
417 
418 	sc->xdelta += xdelta;
419 	sc->ydelta -= ydelta;
420 
421 	sc->buttons = buttons;
422 
423 	mtx_unlock(&sc->sc_mtx);
424 
425 	cv_broadcast(&sc->sc_cv);
426 	selwakeuppri(&sc->rsel, PZERO);
427 
428 	return (0);
429 }
430 
431 static int
432 ams_open(struct cdev *dev, int flag, int fmt, struct thread *p)
433 {
434 	struct adb_mouse_softc *sc;
435 
436 	sc = CDEV_GET_SOFTC(dev);
437 	if (sc == NULL)
438 		return (ENXIO);
439 
440 	mtx_lock(&sc->sc_mtx);
441 	sc->packet_read_len = 0;
442 	sc->xdelta = 0;
443 	sc->ydelta = 0;
444 	sc->buttons = 0;
445 	mtx_unlock(&sc->sc_mtx);
446 
447 	return (0);
448 }
449 
450 static int
451 ams_close(struct cdev *dev, int flag, int fmt, struct thread *p)
452 {
453 	struct adb_mouse_softc *sc;
454 
455 	sc = CDEV_GET_SOFTC(dev);
456 
457 	cv_broadcast(&sc->sc_cv);
458 	selwakeuppri(&sc->rsel, PZERO);
459 	return (0);
460 }
461 
462 static int
463 ams_poll(struct cdev *dev, int events, struct thread *p)
464 {
465 	struct adb_mouse_softc *sc;
466 
467 	sc = CDEV_GET_SOFTC(dev);
468 	if (sc == NULL)
469 		return (EIO);
470 
471 	if (events & (POLLIN | POLLRDNORM)) {
472 		mtx_lock(&sc->sc_mtx);
473 
474 		if (sc->xdelta == 0 && sc->ydelta == 0 &&
475 		   sc->buttons == sc->last_buttons &&
476 		   sc->packet_read_len == 0) {
477 			selrecord(p, &sc->rsel);
478 			events = 0;
479 		} else {
480 			events &= (POLLIN | POLLRDNORM);
481 		}
482 
483 		mtx_unlock(&sc->sc_mtx);
484 	}
485 
486 	return events;
487 }
488 
489 static int
490 ams_read(struct cdev *dev, struct uio *uio, int flag)
491 {
492 	struct adb_mouse_softc *sc;
493 	size_t len;
494 	int8_t outpacket[8];
495 	int error;
496 
497 	sc = CDEV_GET_SOFTC(dev);
498 	if (sc == NULL)
499 		return (EIO);
500 
501 	if (uio->uio_resid <= 0)
502 		return (0);
503 
504 	mtx_lock(&sc->sc_mtx);
505 
506 	if (!sc->packet_read_len) {
507 		if (sc->xdelta == 0 && sc->ydelta == 0 &&
508 		   sc->buttons == sc->last_buttons) {
509 			if (flag & O_NONBLOCK) {
510 				mtx_unlock(&sc->sc_mtx);
511 				return EWOULDBLOCK;
512 			}
513 
514 			/* Otherwise, block on new data */
515 			error = cv_wait_sig(&sc->sc_cv, &sc->sc_mtx);
516 			if (error) {
517 				mtx_unlock(&sc->sc_mtx);
518 				return (error);
519 			}
520 		}
521 
522 		sc->packet[0] = 1U << 7;
523 		sc->packet[0] |= (!(sc->buttons & 1)) << 2;
524 		sc->packet[0] |= (!(sc->buttons & 4)) << 1;
525 		sc->packet[0] |= (!(sc->buttons & 2));
526 
527 		if (sc->xdelta > 127) {
528 			sc->packet[1] = 127;
529 			sc->packet[3] = sc->xdelta - 127;
530 		} else if (sc->xdelta < -127) {
531 			sc->packet[1] = -127;
532 			sc->packet[3] = sc->xdelta + 127;
533 		} else {
534 			sc->packet[1] = sc->xdelta;
535 			sc->packet[3] = 0;
536 		}
537 
538 		if (sc->ydelta > 127) {
539 			sc->packet[2] = 127;
540 			sc->packet[4] = sc->ydelta - 127;
541 		} else if (sc->ydelta < -127) {
542 			sc->packet[2] = -127;
543 			sc->packet[4] = sc->ydelta + 127;
544 		} else {
545 			sc->packet[2] = sc->ydelta;
546 			sc->packet[4] = 0;
547 		}
548 
549 		/* No Z movement */
550 		sc->packet[5] = 0;
551 		sc->packet[6] = 0;
552 
553 		sc->packet[7] = ~((uint8_t)(sc->buttons >> 3)) & 0x7f;
554 
555 		sc->last_buttons = sc->buttons;
556 		sc->xdelta = 0;
557 		sc->ydelta = 0;
558 
559 		sc->packet_read_len = sc->mode.packetsize;
560 	}
561 
562 	len = (sc->packet_read_len > uio->uio_resid) ?
563 		uio->uio_resid : sc->packet_read_len;
564 
565 	memcpy(outpacket,sc->packet +
566 		(sc->mode.packetsize - sc->packet_read_len),len);
567 	sc->packet_read_len -= len;
568 
569 	mtx_unlock(&sc->sc_mtx);
570 
571 	error = uiomove(outpacket,len,uio);
572 
573 	return (error);
574 }
575 
576 static int
577 ams_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
578     struct thread *p)
579 {
580 	struct adb_mouse_softc *sc;
581 	mousemode_t mode;
582 
583 	sc = CDEV_GET_SOFTC(dev);
584 	if (sc == NULL)
585 		return (EIO);
586 
587 	switch (cmd) {
588 	case MOUSE_GETHWINFO:
589 		*(mousehw_t *)addr = sc->hw;
590 		break;
591 	case MOUSE_GETMODE:
592 		*(mousemode_t *)addr = sc->mode;
593 		break;
594 	case MOUSE_SETMODE:
595 		mode = *(mousemode_t *)addr;
596 		addr = (caddr_t)&mode.level;
597 
598 		/* Fallthrough */
599 
600 	case MOUSE_SETLEVEL:
601 		if (*(int *)addr == -1)
602 			break;
603 		else if (*(int *)addr == 1) {
604 			sc->mode.level = 1;
605 			sc->mode.packetsize = 8;
606 			break;
607 		} else if (*(int *)addr == 0) {
608 			sc->mode.level = 0;
609 			sc->mode.packetsize = 5;
610 			break;
611 		}
612 
613 		return EINVAL;
614 	case MOUSE_GETLEVEL:
615 		*(int *)addr = sc->mode.level;
616 		break;
617 
618 	case MOUSE_GETSTATUS: {
619 		mousestatus_t *status = (mousestatus_t *) addr;
620 
621 		mtx_lock(&sc->sc_mtx);
622 
623 		status->button = sc->buttons;
624 		status->obutton = sc->last_buttons;
625 
626 		status->flags = status->button ^ status->obutton;
627 
628 		if (sc->xdelta != 0 || sc->ydelta)
629 			status->flags |= MOUSE_POSCHANGED;
630 		if (status->button != status->obutton)
631 			status->flags |= MOUSE_BUTTONSCHANGED;
632 
633 		status->dx = sc->xdelta;
634 		status->dy = sc->ydelta;
635 		status->dz = 0;
636 
637 		sc->xdelta = 0;
638 		sc->ydelta = 0;
639 		sc->last_buttons = sc->buttons;
640 
641 		mtx_unlock(&sc->sc_mtx);
642 		break; }
643 	default:
644 		return ENOTTY;
645 	}
646 
647 	return (0);
648 }
649 
650 static int
651 adb_tapping_sysctl(SYSCTL_HANDLER_ARGS)
652 {
653 	struct adb_mouse_softc *sc = arg1;
654 	device_t dev;
655         int error;
656 	u_char r2[8];
657 	u_int tapping;
658 
659 	dev = sc->sc_dev;
660 	tapping = sc->sc_tapping;
661 
662 	error = sysctl_handle_int(oidp, &tapping, 0, req);
663 
664 	if (error || !req->newptr)
665 		return (error);
666 
667 	if (tapping == 1) {
668 		adb_read_register(dev, 2, r2);
669 		r2[0] = 0x99; /* enable tapping. */
670 		adb_write_register(dev, 2, 8, r2);
671                 sc->sc_tapping = 1;
672 	} else if (tapping == 0) {
673 		adb_read_register(dev, 2, r2);
674 		r2[0] = 0x19; /* disable tapping. */
675 		adb_write_register(dev, 2, 8, r2);
676 		sc->sc_tapping = 0;
677 	}
678         else
679 		return (EINVAL);
680 
681 	return (0);
682 }
683