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