adb.c revision 1.20 1 /* $NetBSD: adb.c,v 1.20 1998/02/21 00:37:07 scottr Exp $ */
2
3 /*-
4 * Copyright (C) 1994 Bradley A. Grantham
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 e* notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bradley A. Grantham.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "opt_adb.h"
34
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/fcntl.h>
38 #include <sys/poll.h>
39 #include <sys/select.h>
40 #include <sys/proc.h>
41 #include <sys/signalvar.h>
42 #include <sys/systm.h>
43
44 #include <machine/autoconf.h>
45 #include <machine/keyboard.h>
46
47 #include <mac68k/mac68k/macrom.h>
48 #include <mac68k/dev/adbvar.h>
49 #include <mac68k/dev/itevar.h>
50
51 /*
52 * Function declarations.
53 */
54 static int adbmatch __P((struct device *, struct cfdata *, void *));
55 static void adbattach __P((struct device *, struct device *, void *));
56
57 /*
58 * Global variables.
59 */
60 int adb_polling = 0; /* Are we polling? (Debugger mode) */
61 int adb_initted = 0; /* adb_init() has completed successfully */
62 #ifdef ADB_DEBUG
63 int adb_debug = 0; /* Output debugging messages */
64 #endif /* ADB_DEBUG */
65
66 /*
67 * Local variables.
68 */
69
70 /* External keyboard translation matrix */
71 extern unsigned char keyboard[128][3];
72
73 /* Event queue definitions */
74 #if !defined(ADB_MAX_EVENTS)
75 #define ADB_MAX_EVENTS 200 /* Maximum events to be kept in queue */
76 /* maybe should be higher for slower macs? */
77 #endif /* !defined(ADB_MAX_EVENTS) */
78 static adb_event_t adb_evq[ADB_MAX_EVENTS]; /* ADB event queue */
79 static int adb_evq_tail = 0; /* event queue tail */
80 static int adb_evq_len = 0; /* event queue length */
81
82 /* ADB device state information */
83 static int adb_isopen = 0; /* Are we queuing events for adb_read? */
84 static struct selinfo adb_selinfo; /* select() info */
85 static struct proc *adb_ioproc = NULL; /* process to wakeup */
86
87 /* Key repeat parameters */
88 static int adb_rptdelay = 20; /* ticks before auto-repeat */
89 static int adb_rptinterval = 6; /* ticks between auto-repeat */
90 static int adb_repeating = -1; /* key that is auto-repeating */
91 static adb_event_t adb_rptevent;/* event to auto-repeat */
92
93 /* Driver definition. -- This should probably be a bus... */
94 struct cfattach adb_ca = {
95 sizeof(struct device), adbmatch, adbattach
96 };
97
98 static int
99 adbmatch(parent, cf, aux)
100 struct device *parent;
101 struct cfdata *cf;
102 void *aux;
103 {
104 static int adb_matched = 0;
105
106 /* Allow only one instance. */
107 if (adb_matched)
108 return (0);
109
110 adb_matched = 1;
111 return (1);
112 }
113
114 static void
115 adbattach(parent, dev, aux)
116 struct device *parent, *dev;
117 void *aux;
118 {
119 printf(" (ADB event device)\n");
120 adb_init();
121 }
122
123 void
124 adb_enqevent(event)
125 adb_event_t *event;
126 {
127 int s;
128
129 s = spladb();
130
131 #ifdef DIAGNOSTIC
132 if (adb_evq_tail < 0 || adb_evq_tail >= ADB_MAX_EVENTS)
133 panic("adb: event queue tail is out of bounds");
134
135 if (adb_evq_len < 0 || adb_evq_len > ADB_MAX_EVENTS)
136 panic("adb: event queue len is out of bounds");
137 #endif
138
139 if (adb_evq_len == ADB_MAX_EVENTS) {
140 splx(s);
141 return; /* Oh, well... */
142 }
143 adb_evq[(adb_evq_len + adb_evq_tail) % ADB_MAX_EVENTS] =
144 *event;
145 adb_evq_len++;
146
147 selwakeup(&adb_selinfo);
148 if (adb_ioproc)
149 psignal(adb_ioproc, SIGIO);
150
151 splx(s);
152 }
153
154 void
155 adb_handoff(event)
156 adb_event_t *event;
157 {
158 if (adb_isopen && !adb_polling) {
159 adb_enqevent(event);
160 } else {
161 if (event->def_addr == 2)
162 ite_intr(event);
163 }
164 }
165
166
167 void
168 adb_autorepeat(keyp)
169 void *keyp;
170 {
171 int key = (int) keyp;
172
173 adb_rptevent.bytes[0] |= 0x80;
174 microtime(&adb_rptevent.timestamp);
175 adb_handoff(&adb_rptevent); /* do key up */
176
177 adb_rptevent.bytes[0] &= 0x7f;
178 microtime(&adb_rptevent.timestamp);
179 adb_handoff(&adb_rptevent); /* do key down */
180
181 if (adb_repeating == key) {
182 timeout(adb_autorepeat, keyp, adb_rptinterval);
183 }
184 }
185
186
187 void
188 adb_dokeyupdown(event)
189 adb_event_t *event;
190 {
191 int adb_key;
192
193 if (event->def_addr == 2) {
194 adb_key = event->u.k.key & 0x7f;
195 if (!(event->u.k.key & 0x80) &&
196 keyboard[event->u.k.key & 0x7f][0] != 0) {
197 /* ignore shift & control */
198 if (adb_repeating != -1) {
199 untimeout(adb_autorepeat,
200 (void *) adb_rptevent.u.k.key);
201 }
202 adb_rptevent = *event;
203 adb_repeating = adb_key;
204 timeout(adb_autorepeat,
205 (void *) adb_key, adb_rptdelay);
206 } else {
207 if (adb_repeating != -1) {
208 adb_repeating = -1;
209 untimeout(adb_autorepeat,
210 (void *) adb_rptevent.u.k.key);
211 }
212 adb_rptevent = *event;
213 }
214 }
215 adb_handoff(event);
216 }
217
218 static adb_ms_buttons = 0;
219
220 void
221 adb_keymaybemouse(event)
222 adb_event_t *event;
223 {
224 static int optionkey_down = 0;
225 adb_event_t new_event;
226
227 if (event->u.k.key == ADBK_KEYDOWN(ADBK_OPTION)) {
228 optionkey_down = 1;
229 } else if (event->u.k.key == ADBK_KEYUP(ADBK_OPTION)) {
230 /* key up */
231 optionkey_down = 0;
232 if (adb_ms_buttons & 0xfe) {
233 adb_ms_buttons &= 1;
234 new_event.def_addr = ADBADDR_MS;
235 new_event.u.m.buttons = adb_ms_buttons;
236 new_event.u.m.dx = new_event.u.m.dy = 0;
237 microtime(&new_event.timestamp);
238 adb_dokeyupdown(&new_event);
239 }
240 } else if (optionkey_down) {
241 if (event->u.k.key == ADBK_KEYDOWN(ADBK_1)) {
242 adb_ms_buttons |= 1; /* left down */
243 new_event.def_addr = ADBADDR_MS;
244 new_event.u.m.buttons = adb_ms_buttons;
245 new_event.u.m.dx = new_event.u.m.dy = 0;
246 microtime(&new_event.timestamp);
247 adb_dokeyupdown(&new_event);
248 } else if (event->u.k.key == ADBK_KEYUP(ADBK_1)) {
249 adb_ms_buttons &= ~1; /* left up */
250 new_event.def_addr = ADBADDR_MS;
251 new_event.u.m.buttons = adb_ms_buttons;
252 new_event.u.m.dx = new_event.u.m.dy = 0;
253 microtime(&new_event.timestamp);
254 adb_dokeyupdown(&new_event);
255 } else if ((event->u.k.key == ADBK_KEYDOWN(ADBK_LEFT)) ||
256 (event->u.k.key == ADBK_KEYDOWN(ADBK_2))) {
257 adb_ms_buttons |= 2; /* middle down */
258 new_event.def_addr = ADBADDR_MS;
259 new_event.u.m.buttons = adb_ms_buttons;
260 new_event.u.m.dx = new_event.u.m.dy = 0;
261 microtime(&new_event.timestamp);
262 adb_dokeyupdown(&new_event);
263 } else if ((event->u.k.key == ADBK_KEYUP(ADBK_LEFT)) ||
264 (event->u.k.key == ADBK_KEYUP(ADBK_2))) {
265 adb_ms_buttons &= ~2; /* middle up */
266 new_event.def_addr = ADBADDR_MS;
267 new_event.u.m.buttons = adb_ms_buttons;
268 new_event.u.m.dx = new_event.u.m.dy = 0;
269 microtime(&new_event.timestamp);
270 adb_dokeyupdown(&new_event);
271 } else if ((event->u.k.key == ADBK_KEYDOWN(ADBK_RIGHT)) ||
272 (event->u.k.key == ADBK_KEYDOWN(ADBK_3))) {
273 adb_ms_buttons |= 4; /* right down */
274 new_event.def_addr = ADBADDR_MS;
275 new_event.u.m.buttons = adb_ms_buttons;
276 new_event.u.m.dx = new_event.u.m.dy = 0;
277 microtime(&new_event.timestamp);
278 adb_dokeyupdown(&new_event);
279 } else if ((event->u.k.key == ADBK_KEYUP(ADBK_RIGHT)) ||
280 (event->u.k.key == ADBK_KEYUP(ADBK_3))) {
281 adb_ms_buttons &= ~4; /* right up */
282 new_event.def_addr = ADBADDR_MS;
283 new_event.u.m.buttons = adb_ms_buttons;
284 new_event.u.m.dx = new_event.u.m.dy = 0;
285 microtime(&new_event.timestamp);
286 adb_dokeyupdown(&new_event);
287 } else if (ADBK_MODIFIER(event->u.k.key)) {
288 /* ctrl, shift, cmd */
289 adb_dokeyupdown(event);
290 } else if (!(event->u.k.key & 0x80)) {
291 /* key down */
292 new_event = *event;
293
294 /* send option-down */
295 new_event.u.k.key = ADBK_KEYDOWN(ADBK_OPTION);
296 new_event.bytes[0] = new_event.u.k.key;
297 microtime(&new_event.timestamp);
298 adb_dokeyupdown(&new_event);
299
300 /* send key-down */
301 new_event.u.k.key = event->bytes[0];
302 new_event.bytes[0] = new_event.u.k.key;
303 microtime(&new_event.timestamp);
304 adb_dokeyupdown(&new_event);
305
306 /* send key-up */
307 new_event.u.k.key =
308 ADBK_KEYUP(ADBK_KEYVAL(event->bytes[0]));
309 microtime(&new_event.timestamp);
310 new_event.bytes[0] = new_event.u.k.key;
311 adb_dokeyupdown(&new_event);
312
313 /* send option-up */
314 new_event.u.k.key = ADBK_KEYUP(ADBK_OPTION);
315 new_event.bytes[0] = new_event.u.k.key;
316 microtime(&new_event.timestamp);
317 adb_dokeyupdown(&new_event);
318 } else {
319 /* option-keyup -- do nothing. */
320 }
321 } else {
322 adb_dokeyupdown(event);
323 }
324 }
325
326
327 void
328 adb_processevent(event)
329 adb_event_t *event;
330 {
331 adb_event_t new_event;
332 int i, button_bit, max_byte, mask, buttons;
333
334 new_event = *event;
335 buttons = 0;
336
337 switch (event->def_addr) {
338 case ADBADDR_KBD:
339 new_event.u.k.key = event->bytes[0];
340 new_event.bytes[1] = 0xff;
341 adb_keymaybemouse(&new_event);
342 if (event->bytes[1] != 0xff) {
343 new_event.u.k.key = event->bytes[1];
344 new_event.bytes[0] = event->bytes[1];
345 new_event.bytes[1] = 0xff;
346 adb_keymaybemouse(&new_event);
347 }
348 break;
349 case ADBADDR_MS:
350 /*
351 * This should handle both plain ol' Apple mice and mice
352 * that claim to support the Extended Apple Mouse Protocol.
353 */
354 max_byte = event->byte_count;
355 button_bit = 1;
356 switch (event->hand_id) {
357 case ADBMS_USPEED:
358 /* MicroSpeed mouse */
359 if (max_byte == 4)
360 buttons = (~event->bytes[2]) & 0xff;
361 else
362 buttons = (event->bytes[0] & 0x80) ? 0 : 1;
363 break;
364 case ADBMS_MSA3:
365 /* Mouse Systems A3 mouse */
366 if (max_byte == 3)
367 buttons = (~event->bytes[2]) & 0x07;
368 else
369 buttons = (event->bytes[0] & 0x80) ? 0 : 1;
370 break;
371 default:
372 /* Classic Mouse Protocol (up to 2 buttons) */
373 for (i = 0; i < 2; i++, button_bit <<= 1)
374 /* 0 when button down */
375 if (!(event->bytes[i] & 0x80))
376 buttons |= button_bit;
377 else
378 buttons &= ~button_bit;
379 /* Extended Protocol (up to 6 more buttons) */
380 for (mask = 0x80; i < max_byte;
381 i += (mask == 0x80), button_bit <<= 1) {
382 /* 0 when button down */
383 if (!(event->bytes[i] & mask))
384 buttons |= button_bit;
385 else
386 buttons &= ~button_bit;
387 mask = ((mask >> 4) & 0xf)
388 | ((mask & 0xf) << 4);
389 }
390 break;
391 }
392 new_event.u.m.buttons = adb_ms_buttons | buttons;
393 new_event.u.m.dx = ((signed int) (event->bytes[1] & 0x3f)) -
394 ((event->bytes[1] & 0x40) ? 64 : 0);
395 new_event.u.m.dy = ((signed int) (event->bytes[0] & 0x3f)) -
396 ((event->bytes[0] & 0x40) ? 64 : 0);
397 adb_dokeyupdown(&new_event);
398 break;
399 default: /* God only knows. */
400 adb_dokeyupdown(event);
401 }
402 }
403
404
405 int
406 adbopen(dev, flag, mode, p)
407 dev_t dev;
408 int flag, mode;
409 struct proc *p;
410 {
411 register int unit;
412 int error = 0;
413 int s;
414
415 unit = minor(dev);
416 if (unit != 0 || !adb_initted)
417 return (ENXIO);
418
419 s = spladb();
420 if (adb_isopen) {
421 splx(s);
422 return (EBUSY);
423 }
424 adb_evq_tail = 0;
425 adb_evq_len = 0;
426 adb_isopen = 1;
427 adb_ioproc = p;
428 splx(s);
429
430 return (error);
431 }
432
433
434 int
435 adbclose(dev, flag, mode, p)
436 dev_t dev;
437 int flag, mode;
438 struct proc *p;
439 {
440 int s = spladb();
441
442 adb_isopen = 0;
443 adb_ioproc = NULL;
444 splx(s);
445
446 return (0);
447 }
448
449
450 int
451 adbread(dev, uio, flag)
452 dev_t dev;
453 struct uio *uio;
454 int flag;
455 {
456 int s, error;
457 int willfit;
458 int total;
459 int firstmove;
460 int moremove;
461
462 if (uio->uio_resid < sizeof(adb_event_t))
463 return (EMSGSIZE); /* close enough. */
464
465 s = spladb();
466 if (adb_evq_len == 0) {
467 splx(s);
468 return (0);
469 }
470 willfit = howmany(uio->uio_resid, sizeof(adb_event_t));
471 total = (adb_evq_len < willfit) ? adb_evq_len : willfit;
472
473 firstmove = (adb_evq_tail + total > ADB_MAX_EVENTS)
474 ? (ADB_MAX_EVENTS - adb_evq_tail) : total;
475
476 error = uiomove((caddr_t) & adb_evq[adb_evq_tail],
477 firstmove * sizeof(adb_event_t), uio);
478 if (error) {
479 splx(s);
480 return (error);
481 }
482 moremove = total - firstmove;
483
484 if (moremove > 0) {
485 error = uiomove((caddr_t) & adb_evq[0],
486 moremove * sizeof(adb_event_t), uio);
487 if (error) {
488 splx(s);
489 return (error);
490 }
491 }
492 adb_evq_tail = (adb_evq_tail + total) % ADB_MAX_EVENTS;
493 adb_evq_len -= total;
494 splx(s);
495 return (0);
496 }
497
498
499 int
500 adbwrite(dev, uio, flag)
501 dev_t dev;
502 struct uio *uio;
503 int flag;
504 {
505 return 0;
506 }
507
508
509 int
510 adbioctl(dev, cmd, data, flag, p)
511 dev_t dev;
512 int cmd;
513 caddr_t data;
514 int flag;
515 struct proc *p;
516 {
517 switch (cmd) {
518 case ADBIOC_DEVSINFO: {
519 adb_devinfo_t *di;
520 ADBDataBlock adbdata;
521 int totaldevs;
522 int adbaddr;
523 int i;
524
525 di = (void *) data;
526
527 /* Initialize to no devices */
528 for (i = 0; i < 16; i++)
529 di->dev[i].addr = -1;
530
531 totaldevs = CountADBs();
532 for (i = 1; i <= totaldevs; i++) {
533 adbaddr = GetIndADB(&adbdata, i);
534 di->dev[adbaddr].addr = adbaddr;
535 di->dev[adbaddr].default_addr = adbdata.origADBAddr;
536 di->dev[adbaddr].handler_id = adbdata.devType;
537 }
538
539 /* Must call ADB Manager to get devices now */
540 break;
541 }
542
543 case ADBIOC_GETREPEAT:{
544 adb_rptinfo_t *ri;
545
546 ri = (void *) data;
547 ri->delay_ticks = adb_rptdelay;
548 ri->interval_ticks = adb_rptinterval;
549 break;
550 }
551
552 case ADBIOC_SETREPEAT:{
553 adb_rptinfo_t *ri;
554
555 ri = (void *) data;
556 adb_rptdelay = ri->delay_ticks;
557 adb_rptinterval = ri->interval_ticks;
558 break;
559 }
560
561 case ADBIOC_RESET:
562 adb_init();
563 break;
564
565 case ADBIOC_LISTENCMD:{
566 adb_listencmd_t *lc;
567
568 lc = (void *) data;
569 }
570
571 default:
572 return (EINVAL);
573 }
574 return (0);
575 }
576
577
578 int
579 adbpoll(dev, events, p)
580 dev_t dev;
581 int events;
582 struct proc *p;
583 {
584 int s, revents;
585
586 revents = events & (POLLOUT | POLLWRNORM);
587
588 if ((events & (POLLIN | POLLRDNORM)) == 0)
589 return (revents);
590
591 s = spladb();
592 if (adb_evq_len > 0)
593 revents |= events & (POLLIN | POLLRDNORM);
594 else
595 selrecord(p, &adb_selinfo);
596 splx(s);
597
598 return (revents);
599 }
600