xref: /netbsd/sys/dev/adb/adb_ms.c (revision 6550d01e)
1 /*	$NetBSD: adb_ms.c,v 1.9 2010/08/11 16:54:10 macallan Exp $	*/
2 
3 /*
4  * Copyright (C) 1998	Colin Wood
5  * Copyright (C) 2006, 2007 Michael Lorenz
6  * All rights reserved.
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  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by Colin Wood.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: adb_ms.c,v 1.9 2010/08/11 16:54:10 macallan Exp $");
36 
37 #include <sys/param.h>
38 #include <sys/device.h>
39 #include <sys/fcntl.h>
40 #include <sys/poll.h>
41 #include <sys/select.h>
42 #include <sys/proc.h>
43 #include <sys/signalvar.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 
48 #include <machine/autoconf.h>
49 
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wsmousevar.h>
52 
53 #include <machine/adbsys.h>
54 #include <dev/adb/adbvar.h>
55 
56 #include "adbdebug.h"
57 
58 #ifdef ADBMS_DEBUG
59 #define DPRINTF printf
60 #else
61 #define DPRINTF while (0) printf
62 #endif
63 
64 /*
65  * State info, per mouse instance.
66  */
67 struct adbms_softc {
68 	device_t	sc_dev;
69 	struct adb_device *sc_adbdev;
70 	struct adb_bus_accessops *sc_ops;
71 
72 	/* Extended Mouse Protocol info, faked for non-EMP mice */
73 	u_int8_t	sc_class;	/* mouse class (mouse, trackball) */
74 	u_int8_t	sc_buttons;	/* number of buttons */
75 	u_int32_t	sc_res;		/* mouse resolution (dpi) */
76 	char		sc_devid[5];	/* device indentifier */
77 	uint8_t		sc_us;		/* cmd to watch for */
78 	int		sc_mb;		/* current button state */
79 	struct device	*sc_wsmousedev;
80 	/* helpers for trackpads */
81 	int		sc_down;
82 	/*
83 	 * trackpad protocol variant. Known so far:
84 	 * 2 buttons - PowerBook 3400, single events on button 3 and 4 indicate
85 	 *             finger down and up
86 	 * 4 buttons - iBook G4, button 6 indicates finger down, button 4 is
87 	 *             always down
88 	 */
89 	int		sc_x, sc_y;
90 	int		sc_tapping;
91 	/* buffers */
92 	int		sc_poll;
93 	int		sc_msg_len;
94 	int		sc_event;
95 	uint8_t		sc_buffer[16];
96 };
97 
98 /* EMP device classes */
99 #define MSCLASS_TABLET		0
100 #define MSCLASS_MOUSE		1
101 #define MSCLASS_TRACKBALL	2
102 #define MSCLASS_TRACKPAD	3
103 
104 /*
105  * Function declarations.
106  */
107 static int	adbms_match(device_t, cfdata_t, void *);
108 static void	adbms_attach(device_t, device_t, void *);
109 static void	ems_init(struct adbms_softc *);
110 static void	init_trackpad(struct adbms_softc *);
111 static void	adbms_init_mouse(struct adbms_softc *);
112 static void	adbms_init_turbo(struct adbms_softc *);
113 static void	adbms_init_uspeed(struct adbms_softc *);
114 static void	adbms_process_event(struct adbms_softc *, int, uint8_t *);
115 static int	adbms_send_sync(struct adbms_softc *, uint8_t, int, uint8_t *);
116 
117 /* Driver definition. */
118 CFATTACH_DECL_NEW(adbms, sizeof(struct adbms_softc),
119     adbms_match, adbms_attach, NULL, NULL);
120 
121 static int adbms_enable(void *);
122 static int adbms_ioctl(void *, u_long, void *, int, struct lwp *);
123 static void adbms_disable(void *);
124 
125 /*
126  * handle tapping the trackpad
127  * different pads report different button counts and use slightly different
128  * protocols
129  */
130 static void adbms_mangle_2(struct adbms_softc *, int);
131 static void adbms_mangle_4(struct adbms_softc *, int);
132 static void adbms_handler(void *, int, uint8_t *);
133 static int  adbms_wait(struct adbms_softc *, int);
134 static int  sysctl_adbms_tap(SYSCTLFN_ARGS);
135 
136 const struct wsmouse_accessops adbms_accessops = {
137 	adbms_enable,
138 	adbms_ioctl,
139 	adbms_disable,
140 };
141 
142 static int
143 adbms_match(device_t parent, cfdata_t cf, void *aux)
144 {
145 	struct adb_attach_args *aaa = aux;
146 
147 	if (aaa->dev->original_addr == ADBADDR_MS)
148 		return 1;
149 	else
150 		return 0;
151 }
152 
153 static void
154 adbms_attach(device_t parent, device_t self, void *aux)
155 {
156 	struct adbms_softc *sc = device_private(self);
157 	struct adb_attach_args *aaa = aux;
158 	struct wsmousedev_attach_args a;
159 
160 	sc->sc_dev = self;
161 	sc->sc_ops = aaa->ops;
162 	sc->sc_adbdev = aaa->dev;
163 	sc->sc_adbdev->cookie = sc;
164 	sc->sc_adbdev->handler = adbms_handler;
165 	sc->sc_us = ADBTALK(sc->sc_adbdev->current_addr, 0);
166 	printf(" addr %d: ", sc->sc_adbdev->current_addr);
167 
168 	sc->sc_class = MSCLASS_MOUSE;
169 	sc->sc_buttons = 1;
170 	sc->sc_res = 100;
171 	sc->sc_devid[0] = 0;
172 	sc->sc_devid[4] = 0;
173 	sc->sc_poll = 0;
174 	sc->sc_msg_len = 0;
175 	sc->sc_tapping = 1;
176 
177 	ems_init(sc);
178 
179 	/* print out the type of mouse we have */
180 	switch (sc->sc_adbdev->handler_id) {
181 	case ADBMS_100DPI:
182 		printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
183 		    (int)(sc->sc_res));
184 		break;
185 	case ADBMS_200DPI:
186 		sc->sc_res = 200;
187 		printf("%d-button, %d dpi mouse\n", sc->sc_buttons,
188 		    (int)(sc->sc_res));
189 		break;
190 	case ADBMS_MSA3:
191 		printf("Mouse Systems A3 mouse, %d-button, %d dpi\n",
192 		    sc->sc_buttons, (int)(sc->sc_res));
193 		break;
194 	case ADBMS_USPEED:
195 		printf("MicroSpeed mouse, default parameters\n");
196 		break;
197 	case ADBMS_UCONTOUR:
198 		printf("Contour mouse, default parameters\n");
199 		break;
200 	case ADBMS_TURBO:
201 		printf("Kensington Turbo Mouse\n");
202 		break;
203 	case ADBMS_EXTENDED:
204 		if (sc->sc_devid[0] == '\0') {
205 			printf("Logitech ");
206 			switch (sc->sc_class) {
207 			case MSCLASS_MOUSE:
208 				printf("MouseMan (non-EMP) mouse");
209 				break;
210 			case MSCLASS_TRACKBALL:
211 				printf("TrackMan (non-EMP) trackball");
212 				break;
213 			default:
214 				printf("non-EMP relative positioning device");
215 				break;
216 			}
217 			printf("\n");
218 		} else {
219 			printf("EMP ");
220 			switch (sc->sc_class) {
221 			case MSCLASS_TABLET:
222 				printf("tablet");
223 				break;
224 			case MSCLASS_MOUSE:
225 				printf("mouse");
226 				break;
227 			case MSCLASS_TRACKBALL:
228 				printf("trackball");
229 				break;
230 			case MSCLASS_TRACKPAD:
231 				printf("trackpad");
232 				init_trackpad(sc);
233 				break;
234 			default:
235 				printf("unknown device");
236 				break;
237 			}
238 			printf(" <%s> %d-button, %d dpi\n", sc->sc_devid,
239 			    sc->sc_buttons, (int)(sc->sc_res));
240 		}
241 		break;
242 	default:
243 		printf("relative positioning device (mouse?) (%d)\n",
244 			sc->sc_adbdev->handler_id);
245 		break;
246 	}
247 
248 	a.accessops = &adbms_accessops;
249 	a.accesscookie = sc;
250 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
251 }
252 
253 
254 /*
255  * Initialize extended mouse support -- probes devices as described
256  * in Inside Macintosh: Devices, Chapter 5 "ADB Manager".
257  *
258  * Extended Mouse Protocol is documented in TechNote HW1:
259  * 	"ADB - The Untold Story:  Space Aliens Ate My Mouse"
260  *
261  * Supports: Extended Mouse Protocol, MicroSpeed Mouse Deluxe,
262  * 	     Mouse Systems A^3 Mouse, Logitech non-EMP MouseMan
263  */
264 void
265 ems_init(struct adbms_softc *sc)
266 {
267 
268 	DPRINTF("ems_init %d\n", sc->sc_adbdev->handler_id);
269 
270 	switch (sc->sc_adbdev->handler_id) {
271 		case ADBMS_USPEED:
272 		case ADBMS_UCONTOUR:
273 			adbms_init_uspeed(sc);
274 			return;
275 		case ADBMS_TURBO:
276 			adbms_init_turbo(sc);
277 			return;
278 		case ADBMS_100DPI:
279 		case ADBMS_200DPI:
280 			adbms_init_mouse(sc);
281 	}
282 }
283 
284 static void
285 adbms_init_uspeed(struct adbms_softc *sc)
286 {
287 	uint8_t cmd, addr, buffer[4];
288 
289 	addr = sc->sc_adbdev->current_addr;
290 
291 	/* Found MicroSpeed Mouse Deluxe Mac or Contour Mouse */
292 	cmd = ADBLISTEN(addr, 1);
293 
294 	/*
295 	 * To setup the MicroSpeed or the Contour, it appears
296 	 * that we can send the following command to the mouse
297 	 * and then expect data back in the form:
298 	 *  buffer[0] = 4 (bytes)
299 	 *  buffer[1], buffer[2] as std. mouse
300 	 *  buffer[3] = buffer[4] = 0xff when no buttons
301 	 *   are down.  When button N down, bit N is clear.
302 	 * buffer[4]'s locking mask enables a
303 	 * click to toggle the button down state--sort of
304 	 * like the "Easy Access" shift/control/etc. keys.
305 	 * buffer[3]'s alternative speed mask enables using
306 	 * different speed when the corr. button is down
307 	 */
308 	buffer[0] = 0x00;	/* Alternative speed */
309 	buffer[1] = 0x00;	/* speed = maximum */
310 	buffer[2] = 0x10;	/* enable extended protocol,
311 				 * lower bits = alt. speed mask
312 				 *            = 0000b
313 				 */
314 	buffer[3] = 0x07;	/* Locking mask = 0000b,
315 				 * enable buttons = 0111b
316 				 */
317 	adbms_send_sync(sc, cmd, 4, buffer);
318 
319 	sc->sc_buttons = 3;
320 	sc->sc_res = 200;
321 }
322 
323 static void
324 adbms_init_turbo(struct adbms_softc *sc)
325 {
326 	uint8_t addr;
327 
328 	/* Found Kensington Turbo Mouse */
329 	static u_char data1[] =
330 		{ 0xe7, 0x8c, 0, 0, 0, 0xff, 0xff, 0x94 };
331 	static u_char data2[] =
332 		{ 0xa5, 0x14, 0, 0, 0x69, 0xff, 0xff, 0x27 };
333 
334 	addr = sc->sc_adbdev->current_addr;
335 
336 	adbms_send_sync(sc, ADBFLUSH(addr), 0, NULL);
337 	adbms_send_sync(sc, ADBLISTEN(addr, 2), 8, data1);
338 	adbms_send_sync(sc, ADBFLUSH(addr), 0, NULL);
339 	adbms_send_sync(sc, ADBLISTEN(addr, 2), 8, data2);
340 }
341 
342 static void
343 adbms_init_mouse(struct adbms_softc *sc)
344 {
345 	int len;
346 	uint8_t cmd, addr, buffer[16];
347 
348 	addr = sc->sc_adbdev->current_addr;
349 	/* found a mouse */
350 	cmd = ADBTALK(addr, 3);
351 	if (!adbms_send_sync(sc, cmd, 0, NULL)) {
352 #ifdef ADBMS_DEBUG
353 		printf("adb: ems_init timed out\n");
354 #endif
355 		return;
356 	}
357 
358 	/* Attempt to initialize Extended Mouse Protocol */
359 	len = sc->sc_msg_len;
360 	memcpy(buffer, sc->sc_buffer, len);
361 	DPRINTF("buffer: %02x %02x\n", buffer[0], buffer[1]);
362 	buffer[1] = 4; /* make handler ID 4 */
363 	cmd = ADBLISTEN(addr, 3);
364 	if (!adbms_send_sync(sc, cmd, len, buffer)) {
365 #ifdef ADBMS_DEBUG
366 		printf("adb: ems_init timed out\n");
367 #endif
368 		return;
369 	}
370 
371 	/*
372 	 * Check to see if successful, if not
373 	 * try to initialize it as other types
374 	 */
375 	cmd = ADBTALK(addr, 3);
376 	if (!adbms_send_sync(sc, cmd, 0, NULL)) {
377 		DPRINTF("timeout checking for EMP switch\n");
378 		return;
379 	}
380 	DPRINTF("new handler ID: %02x\n", sc->sc_buffer[1]);
381 	if (sc->sc_buffer[1] == ADBMS_EXTENDED) {
382 		sc->sc_adbdev->handler_id = ADBMS_EXTENDED;
383 		cmd = ADBTALK(addr, 1);
384 		if(!adbms_send_sync(sc, cmd, 0, NULL)) {
385 			DPRINTF("adb: ems_init timed out\n");
386 			return;
387 		}
388 
389 		len = sc->sc_msg_len;
390 		memcpy(buffer, sc->sc_buffer, len);
391 
392 		if (sc->sc_msg_len == 8) {
393 			/* we have a true EMP device */
394 #ifdef ADB_PRINT_EMP
395 
396 			printf("EMP: %02x %02x %02x %02x %02x %02x %02x %02x\n",
397 			    buffer[0], buffer[1], buffer[2], buffer[3],
398 			    buffer[4], buffer[5], buffer[6], buffer[7]);
399 #endif
400 			sc->sc_class = buffer[6];
401 			sc->sc_buttons = buffer[7];
402 			sc->sc_res = (int)*(short *)&buffer[4];
403 				memcpy(sc->sc_devid, &(buffer[0]), 4);
404 		} else if (buffer[0] == 0x9a &&
405 		    ((buffer[1] == 0x20) || (buffer[1] == 0x21))) {
406 			/*
407 			 * Set up non-EMP Mouseman/Trackman to put
408 			 * button bits in 3rd byte instead of sending
409 			 * via pseudo keyboard device.
410 			 */
411 			if (buffer[1] == 0x21)
412 				sc->sc_class = MSCLASS_TRACKBALL;
413 			else
414 				sc->sc_class = MSCLASS_MOUSE;
415 
416 			cmd = ADBLISTEN(addr, 1);
417 			buffer[0]=0x00;
418 			buffer[1]=0x81;
419 			adbms_send_sync(sc, cmd, 2, buffer);
420 
421 			cmd = ADBLISTEN(addr, 1);
422 			buffer[0]=0x01;
423 			buffer[1]=0x81;
424 			adbms_send_sync(sc, cmd, 2, buffer);
425 
426 			cmd = ADBLISTEN(addr, 1);
427 			buffer[0]=0x02;
428 			buffer[1]=0x81;
429 			adbms_send_sync(sc, cmd, 2, buffer);
430 
431 			cmd = ADBLISTEN(addr, 1);
432 			buffer[0]=0x03;
433 			buffer[1]=0x38;
434 			adbms_send_sync(sc, cmd, 2, buffer);
435 
436 			sc->sc_buttons = 3;
437 			sc->sc_res = 400;
438 		}
439 	} else {
440 		/* Attempt to initialize as an A3 mouse */
441 		buffer[1] = 0x03; /* make handler ID 3 */
442 		cmd = ADBLISTEN(addr, 3);
443 		if (!adbms_send_sync(sc, cmd, len, buffer)) {
444 #ifdef ADBMS_DEBUG
445 			printf("adb: ems_init timed out\n");
446 #endif
447 			return;
448 		}
449 
450 		/*
451 		 * Check to see if successful, if not
452 		 * try to initialize it as other types
453 		 */
454 		cmd = ADBTALK(addr, 3);
455 		if(adbms_send_sync(sc, cmd, 0, NULL)) {
456 			len = sc->sc_msg_len;
457 			memcpy(buffer, sc->sc_buffer, len);
458 			if (buffer[1] == ADBMS_MSA3) {
459 				sc->sc_adbdev->handler_id = ADBMS_MSA3;
460 				/* Initialize as above */
461 				cmd = ADBLISTEN(addr, 2);
462 				/* listen 2 */
463 				buffer[0] = 0x00;
464 				/* Irrelevant, buffer has 0x77 */
465 				buffer[2] = 0x07;
466 				/*
467 				 * enable 3 button mode = 0111b,
468 				 * speed = normal
469 				 */
470 				adbms_send_sync(sc, cmd, 3, buffer);
471 				sc->sc_buttons = 3;
472 				sc->sc_res = 300;
473 			}
474 		}
475 	}
476 }
477 
478 static void
479 adbms_handler(void *cookie, int len, uint8_t *data)
480 {
481 	struct adbms_softc *sc = cookie;
482 
483 #ifdef ADBMS_DEBUG
484 	int i;
485 	printf("%s: %02x - ", device_xname(sc->sc_dev), sc->sc_us);
486 	for (i = 0; i < len; i++) {
487 		printf(" %02x", data[i]);
488 	}
489 	printf("\n");
490 #endif
491 	if (len >= 2) {
492 		memcpy(sc->sc_buffer, &data[2], len - 2);
493 		sc->sc_msg_len = len - 2;
494 		if (data[1] == sc->sc_us) {
495 			/* make sense of the mouse message */
496 			adbms_process_event(sc, sc->sc_msg_len, sc->sc_buffer);
497 			return;
498 		}
499 		wakeup(&sc->sc_event);
500 	} else {
501 		DPRINTF("bogus message\n");
502 	}
503 }
504 
505 static void
506 adbms_process_event(struct adbms_softc *sc, int len, uint8_t *buffer)
507 {
508 	int buttons = 0, mask, dx, dy, i;
509 	int button_bit = 1;
510 
511 	if ((sc->sc_adbdev->handler_id == ADBMS_EXTENDED) && (sc->sc_devid[0] == 0)) {
512 		/* massage the data to look like EMP data */
513 		if ((buffer[2] & 0x04) == 0x04)
514 			buffer[0] &= 0x7f;
515 		else
516 			buffer[0] |= 0x80;
517 		if ((buffer[2] & 0x02) == 0x02)
518 			buffer[1] &= 0x7f;
519 		else
520 			buffer[1] |= 0x80;
521 		if ((buffer[2] & 0x01) == 0x01)
522 			buffer[2] = 0x00;
523 		else
524 			buffer[2] = 0x80;
525 	}
526 
527 	switch (sc->sc_adbdev->handler_id) {
528 		case ADBMS_USPEED:
529 		case ADBMS_UCONTOUR:
530 			/* MicroSpeed mouse and Contour mouse */
531 			if (len == 4)
532 				buttons = (~buffer[3]) & 0xff;
533 			else
534 				buttons = (buffer[1] & 0x80) ? 0 : 1;
535 			break;
536 		case ADBMS_MSA3:
537 			/* Mouse Systems A3 mouse */
538 			if (len == 3)
539 				buttons = (~buffer[2]) & 0x07;
540 			else
541 				buttons = (buffer[0] & 0x80) ? 0 : 1;
542 			break;
543 		default:
544 			/* Classic Mouse Protocol (up to 2 buttons) */
545 			for (i = 0; i < 2; i++, button_bit <<= 1)
546 				/* 0 when button down */
547 				if (!(buffer[i] & 0x80))
548 					buttons |= button_bit;
549 				else
550 					buttons &= ~button_bit;
551 			/* Extended Protocol (up to 6 more buttons) */
552 			for (mask = 0x80; i < len;
553 			     i += (mask == 0x80), button_bit <<= 1) {
554 				/* 0 when button down */
555 				if (!(buffer[i] & mask))
556 					buttons |= button_bit;
557 				else
558 					buttons &= ~button_bit;
559 				mask = ((mask >> 4) & 0xf)
560 					| ((mask & 0xf) << 4);
561 			}
562 			break;
563 	}
564 
565 	dx = ((int)(buffer[1] & 0x3f)) - ((buffer[1] & 0x40) ? 64 : 0);
566 	dy = ((int)(buffer[0] & 0x3f)) - ((buffer[0] & 0x40) ? 64 : 0);
567 
568 	if (sc->sc_class == MSCLASS_TRACKPAD) {
569 
570 		if (sc->sc_tapping == 1) {
571 			if (sc->sc_down) {
572 				/* finger is down - collect motion data */
573 				sc->sc_x += dx;
574 				sc->sc_y += dy;
575 			}
576 			DPRINTF("buttons: %02x\n", buttons);
577 			switch (sc->sc_buttons) {
578 				case 2:
579 					buttons |= ((buttons & 2) >> 1);
580 					adbms_mangle_2(sc, buttons);
581 					break;
582 				case 4:
583 					adbms_mangle_4(sc, buttons);
584 					break;
585 			}
586 		}
587 		/* filter the pseudo-buttons out */
588 		buttons &= 1;
589 	}
590 
591 	if (sc->sc_wsmousedev)
592 		wsmouse_input(sc->sc_wsmousedev, sc->sc_mb | buttons,
593 			      dx, -dy, 0, 0,
594 			      WSMOUSE_INPUT_DELTA);
595 #if NAED > 0
596 	aed_input(&new_event);
597 #endif
598 }
599 
600 static void
601 adbms_mangle_2(struct adbms_softc *sc, int buttons)
602 {
603 
604 	if (buttons & 4) {
605 		/* finger down on pad */
606 		if (sc->sc_down == 0) {
607 			sc->sc_down = 1;
608 			sc->sc_x = 0;
609 			sc->sc_y = 0;
610 		}
611 	}
612 	if (buttons & 8) {
613 		/* finger up */
614 		if (sc->sc_down) {
615 			if (((sc->sc_x * sc->sc_x +
616 			    sc->sc_y * sc->sc_y) < 3) &&
617 			    (sc->sc_wsmousedev)) {
618 				/*
619 				 * if there wasn't much movement between
620 				 * finger down and up again we assume
621 				 * someone tapped the pad and we just
622 				 * send a mouse button event
623 				 */
624 				wsmouse_input(sc->sc_wsmousedev,
625 				    1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
626 			}
627 			sc->sc_down = 0;
628 		}
629 	}
630 }
631 
632 static void
633 adbms_mangle_4(struct adbms_softc *sc, int buttons)
634 {
635 
636 	if (buttons & 0x20) {
637 		/* finger down on pad */
638 		if (sc->sc_down == 0) {
639 			sc->sc_down = 1;
640 			sc->sc_x = 0;
641 			sc->sc_y = 0;
642 		}
643 	}
644 	if ((buttons & 0x20) == 0) {
645 		/* finger up */
646 		if (sc->sc_down) {
647 			if (((sc->sc_x * sc->sc_x +
648 			    sc->sc_y * sc->sc_y) < 3) &&
649 			    (sc->sc_wsmousedev)) {
650 				/*
651 				 * if there wasn't much movement between
652 				 * finger down and up again we assume
653 				 * someone tapped the pad and we just
654 				 * send a mouse button event
655 				 */
656 				wsmouse_input(sc->sc_wsmousedev,
657 				    1, 0, 0, 0, 0, WSMOUSE_INPUT_DELTA);
658 			}
659 			sc->sc_down = 0;
660 		}
661 	}
662 }
663 
664 static int
665 adbms_enable(void *v)
666 {
667 	return 0;
668 }
669 
670 static int
671 adbms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
672 {
673 
674 	switch (cmd) {
675 	case WSMOUSEIO_GTYPE:
676 		*(u_int *)data = WSMOUSE_TYPE_ADB;
677 		break;
678 
679 	default:
680 		return (EPASSTHROUGH);
681 	}
682 	return (0);
683 }
684 
685 static void
686 adbms_disable(void *v)
687 {
688 }
689 
690 static void
691 init_trackpad(struct adbms_softc *sc)
692 {
693 	struct sysctlnode *me = NULL, *node = NULL;
694 	int cmd, addr, ret;
695 	uint8_t buffer[16];
696 	uint8_t b2[] = {0x99, 0x94, 0x19, 0xff, 0xb2, 0x8a, 0x1b, 0x50};
697 
698 	addr = sc->sc_adbdev->current_addr;
699 	cmd = ADBTALK(addr, 1);
700 	if (!adbms_send_sync(sc, cmd, 0, NULL))
701 		return;
702 
703 	if (sc->sc_msg_len != 8)
704 		return;
705 
706 	memcpy(buffer, sc->sc_buffer, 8);
707 
708 	/* now whack the pad */
709 	cmd = ADBLISTEN(addr, 1);
710 	buffer[6] = 0x0d;
711 	adbms_send_sync(sc, cmd, 8, buffer);
712 
713 	delay(1000);
714 	cmd = ADBLISTEN(addr, 2);
715 	adbms_send_sync(sc, cmd, 8, b2);
716 
717 	delay(1000);
718 	cmd = ADBLISTEN(addr, 1);
719 	buffer[6] = 0x03;
720 	adbms_send_sync(sc, cmd, 8, buffer);
721 
722 	cmd = ADBFLUSH(addr);
723 	adbms_send_sync(sc, cmd, 0, NULL);
724 	delay(1000);
725 
726 	/*
727 	 * setup a sysctl node to control wether tapping the pad should
728 	 * trigger mouse button events
729 	 */
730 
731 	sc->sc_tapping = 1;
732 
733 	ret = sysctl_createv(NULL, 0, NULL, (const struct sysctlnode **)&me,
734 	    CTLFLAG_READWRITE,
735 	    CTLTYPE_NODE, device_xname(sc->sc_dev), NULL,
736 	    NULL, 0, NULL, 0,
737 	    CTL_MACHDEP, CTL_CREATE, CTL_EOL);
738 
739 	ret = sysctl_createv(NULL, 0, NULL, (const struct sysctlnode **)&node,
740 	    CTLFLAG_READWRITE | CTLFLAG_OWNDESC | CTLFLAG_IMMEDIATE,
741 	    CTLTYPE_INT, "tapping", "tapping the pad causes button events",
742 	    sysctl_adbms_tap, 1, NULL, 0,
743 	    CTL_MACHDEP, me->sysctl_num, CTL_CREATE, CTL_EOL);
744 	if (node != NULL) {
745 		node->sysctl_data = sc;
746 	}
747 }
748 
749 static int
750 adbms_wait(struct adbms_softc *sc, int timeout)
751 {
752 	int cnt = 0;
753 
754 	if (sc->sc_poll) {
755 		while (sc->sc_msg_len == -1) {
756 			sc->sc_ops->poll(sc->sc_ops->cookie);
757 		}
758 	} else {
759 		while ((sc->sc_msg_len == -1) && (cnt < timeout)) {
760 			tsleep(&sc->sc_event, 0, "adbmsio", hz);
761 			cnt++;
762 		}
763 	}
764 	return (sc->sc_msg_len > 0);
765 }
766 
767 static int
768 adbms_send_sync(struct adbms_softc *sc, uint8_t cmd, int len, uint8_t *msg)
769 {
770 	int i;
771 
772 	sc->sc_msg_len = -1;
773 	DPRINTF("send: %02x", cmd);
774 	for (i = 0; i < len; i++)
775 		DPRINTF(" %02x", msg[i]);
776 	DPRINTF("\n");
777 	sc->sc_ops->send(sc->sc_ops->cookie, sc->sc_poll, cmd, len, msg);
778 	adbms_wait(sc, 1000);
779 	return (sc->sc_msg_len != -1);
780 }
781 
782 static int
783 sysctl_adbms_tap(SYSCTLFN_ARGS)
784 {
785 	struct sysctlnode node = *rnode;
786 	struct adbms_softc *sc = node.sysctl_data;
787 
788 	node.sysctl_idata = sc->sc_tapping;
789 
790 	if (newp) {
791 
792 		/* we're asked to write */
793 		node.sysctl_data = &sc->sc_tapping;
794 		if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) {
795 
796 			sc->sc_tapping = (node.sysctl_idata == 0) ? 0 : 1;
797 			return 0;
798 		}
799 		return EINVAL;
800 	} else {
801 
802 		node.sysctl_size = 4;
803 		return (sysctl_lookup(SYSCTLFN_CALL(&node)));
804 	}
805 
806 	return 0;
807 }
808 
809 SYSCTL_SETUP(sysctl_ams_setup, "sysctl ams subtree setup")
810 {
811 
812 	sysctl_createv(NULL, 0, NULL, NULL,
813 		       CTLFLAG_PERMANENT,
814 		       CTLTYPE_NODE, "machdep", NULL,
815 		       NULL, 0, NULL, 0,
816 		       CTL_MACHDEP, CTL_EOL);
817 }
818