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