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