aed.c revision 1.36 1 /* $NetBSD: aed.c,v 1.36 2025/01/12 09:07:02 nat 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 * 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 THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: aed.c,v 1.36 2025/01/12 09:07:02 nat Exp $");
30
31 #include <sys/param.h>
32 #include <sys/device.h>
33 #include <sys/fcntl.h>
34 #include <sys/poll.h>
35 #include <sys/select.h>
36 #include <sys/proc.h>
37 #include <sys/signalvar.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40
41 #include <machine/autoconf.h>
42 #include <machine/cpu.h>
43 #include <machine/keyboard.h>
44
45 #include <macppc/dev/adbvar.h>
46 #include <macppc/dev/aedvar.h>
47 #include <macppc/dev/akbdvar.h>
48
49 #define spladb splhigh
50
51 /*
52 * Function declarations.
53 */
54 static int aedmatch(device_t, cfdata_t, void *);
55 static void aedattach(device_t, device_t, void *);
56 static int aed_emulate_mouse(adb_event_t *event);
57 static void aed_kbdrpt(void *kstate);
58 static void aed_dokeyupdown(adb_event_t *event);
59 static void aed_handoff(adb_event_t *event);
60 static void aed_enqevent(adb_event_t *event);
61
62 /*
63 * Global variables.
64 */
65 extern int adb_polling; /* Are we polling? (Debugger mode) */
66
67 /*
68 * Local variables.
69 */
70 static struct aed_softc *aed_sc = NULL;
71 static int aed_options = 0; /* | AED_MSEMUL; */
72
73 /* Driver definition */
74 CFATTACH_DECL_NEW(aed, sizeof(struct aed_softc),
75 aedmatch, aedattach, NULL, NULL);
76
77 extern struct cfdriver aed_cd;
78
79 dev_type_open(aedopen);
80 dev_type_close(aedclose);
81 dev_type_read(aedread);
82 dev_type_ioctl(aedioctl);
83 dev_type_poll(aedpoll);
84 dev_type_kqfilter(aedkqfilter);
85
86 const struct cdevsw aed_cdevsw = {
87 .d_open = aedopen,
88 .d_close = aedclose,
89 .d_read = aedread,
90 .d_write = nullwrite,
91 .d_ioctl = aedioctl,
92 .d_stop = nostop,
93 .d_tty = notty,
94 .d_poll = aedpoll,
95 .d_mmap = nommap,
96 .d_kqfilter = aedkqfilter,
97 .d_discard = nodiscard,
98 .d_flag = 0
99 };
100
101 static int
102 aedmatch(device_t parent, cfdata_t cf, void *aux)
103 {
104 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
105 static int aed_matched = 0;
106
107 /* Allow only one instance. */
108 if ((aa_args->origaddr == 0) && (!aed_matched)) {
109 aed_matched = 1;
110 return (1);
111 } else
112 return (0);
113 }
114
115 static void
116 aedattach(device_t parent, device_t self, void *aux)
117 {
118 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
119 struct aed_softc *sc = device_private(self);
120
121 callout_init(&sc->sc_repeat_ch, 0);
122 selinit(&sc->sc_selinfo);
123
124 sc->origaddr = aa_args->origaddr;
125 sc->adbaddr = aa_args->adbaddr;
126 sc->handler_id = aa_args->handler_id;
127
128 sc->sc_evq_tail = 0;
129 sc->sc_evq_len = 0;
130
131 sc->sc_rptdelay = 20;
132 sc->sc_rptinterval = 6;
133 sc->sc_repeating = -1; /* not repeating */
134
135 /* Pull in the options flags. */
136 sc->sc_options = (device_cfdata(self)->cf_flags | aed_options);
137
138 sc->sc_ioproc = NULL;
139
140 sc->sc_buttons = 0;
141
142 sc->sc_open = 0;
143
144 aed_sc = sc;
145
146 printf("ADB Event device\n");
147
148 return;
149 }
150
151 /*
152 * Given a keyboard ADB event, record the keycode and call the key
153 * repeat handler, optionally passing the event through the mouse
154 * button emulation handler first. Pass mouse events directly to
155 * the handoff function.
156 */
157 void
158 aed_input(adb_event_t *event)
159 {
160 adb_event_t new_event = *event;
161
162 switch (event->def_addr) {
163 case ADBADDR_KBD:
164 if (aed_sc->sc_options & AED_MSEMUL) {
165 rv = aed_emulate_mouse(&new_event);
166 } else
167 aed_dokeyupdown(&new_event);
168 break;
169 case ADBADDR_MS:
170 event->u.m.buttons |= aed_sc->sc_buttons;
171 new_event.u.m.buttons |= aed_sc->sc_buttons;
172 aed_handoff(&new_event);
173 break;
174 default: /* God only knows. */
175 #ifdef DIAGNOSTIC
176 panic("aed: received event from unsupported device!");
177 #endif
178 break;
179 }
180
181 }
182
183 /*
184 * Handles mouse button emulation via the keyboard. If the emulation
185 * modifier key is down, left and right arrows will generate 2nd and
186 * 3rd mouse button events while the 1, 2, and 3 keys will generate
187 * the corresponding mouse button event.
188 */
189 static int
190 aed_emulate_mouse(adb_event_t *event)
191 {
192 static int emulmodkey_down = 0;
193 adb_event_t new_event;
194 int result = 0;
195
196 if (event->u.k.key == ADBK_KEYDOWN(ADBK_OPTION)) {
197 emulmodkey_down = 1;
198 } else if (event->u.k.key == ADBK_KEYUP(ADBK_OPTION)) {
199 /* key up */
200 emulmodkey_down = 0;
201 if (aed_sc->sc_buttons & 0xfe) {
202 aed_sc->sc_buttons &= 1;
203 new_event.def_addr = ADBADDR_MS;
204 new_event.u.m.buttons = aed_sc->sc_buttons;
205 new_event.u.m.dx = new_event.u.m.dy = 0;
206 microtime(&new_event.timestamp);
207 aed_handoff(&new_event);
208 }
209 } else if (emulmodkey_down) {
210 switch(event->u.k.key) {
211 #ifdef ALTXBUTTONS
212 case ADBK_KEYDOWN(ADBK_1):
213 result = 1;
214 aed_sc->sc_buttons |= 1; /* left down */
215 new_event.def_addr = ADBADDR_MS;
216 new_event.u.m.buttons = aed_sc->sc_buttons;
217 new_event.u.m.dx = new_event.u.m.dy = 0;
218 microtime(&new_event.timestamp);
219 aed_handoff(&new_event);
220 break;
221 case ADBK_KEYUP(ADBK_1):
222 result = 1;
223 aed_sc->sc_buttons &= ~1; /* left up */
224 new_event.def_addr = ADBADDR_MS;
225 new_event.u.m.buttons = aed_sc->sc_buttons;
226 new_event.u.m.dx = new_event.u.m.dy = 0;
227 microtime(&new_event.timestamp);
228 aed_handoff(&new_event);
229 break;
230 #endif
231 case ADBK_KEYDOWN(ADBK_LEFT):
232 #ifdef ALTXBUTTONS
233 case ADBK_KEYDOWN(ADBK_2):
234 #endif
235 result = 1;
236 aed_sc->sc_buttons |= 2; /* middle down */
237 new_event.def_addr = ADBADDR_MS;
238 new_event.u.m.buttons = aed_sc->sc_buttons;
239 new_event.u.m.dx = new_event.u.m.dy = 0;
240 microtime(&new_event.timestamp);
241 aed_handoff(&new_event);
242 break;
243 case ADBK_KEYUP(ADBK_LEFT):
244 #ifdef ALTXBUTTONS
245 case ADBK_KEYUP(ADBK_2):
246 #endif
247 result = 1;
248 aed_sc->sc_buttons &= ~2; /* middle up */
249 new_event.def_addr = ADBADDR_MS;
250 new_event.u.m.buttons = aed_sc->sc_buttons;
251 new_event.u.m.dx = new_event.u.m.dy = 0;
252 microtime(&new_event.timestamp);
253 aed_handoff(&new_event);
254 break;
255 case ADBK_KEYDOWN(ADBK_RIGHT):
256 #ifdef ALTXBUTTONS
257 case ADBK_KEYDOWN(ADBK_3):
258 #endif
259 result = 1;
260 aed_sc->sc_buttons |= 4; /* right down */
261 new_event.def_addr = ADBADDR_MS;
262 new_event.u.m.buttons = aed_sc->sc_buttons;
263 new_event.u.m.dx = new_event.u.m.dy = 0;
264 microtime(&new_event.timestamp);
265 aed_handoff(&new_event);
266 break;
267 case ADBK_KEYUP(ADBK_RIGHT):
268 #ifdef ALTXBUTTONS
269 case ADBK_KEYUP(ADBK_3):
270 #endif
271 result = 1;
272 aed_sc->sc_buttons &= ~4; /* right up */
273 new_event.def_addr = ADBADDR_MS;
274 new_event.u.m.buttons = aed_sc->sc_buttons;
275 new_event.u.m.dx = new_event.u.m.dy = 0;
276 microtime(&new_event.timestamp);
277 aed_handoff(&new_event);
278 break;
279 case ADBK_KEYUP(ADBK_SHIFT):
280 case ADBK_KEYDOWN(ADBK_SHIFT):
281 case ADBK_KEYUP(ADBK_CONTROL):
282 case ADBK_KEYDOWN(ADBK_CONTROL):
283 case ADBK_KEYUP(ADBK_FLOWER):
284 case ADBK_KEYDOWN(ADBK_FLOWER):
285 /* ctrl, shift, cmd */
286 aed_dokeyupdown(event);
287 break;
288 default:
289 if (event->u.k.key & 0x80)
290 /* ignore keyup */
291 break;
292
293 /* key down */
294 new_event = *event;
295
296 /* send option-down */
297 new_event.u.k.key = ADBK_KEYDOWN(ADBK_OPTION);
298 new_event.bytes[0] = new_event.u.k.key;
299 microtime(&new_event.timestamp);
300 aed_dokeyupdown(&new_event);
301
302 /* send key-down */
303 new_event.u.k.key = event->bytes[0];
304 new_event.bytes[0] = new_event.u.k.key;
305 microtime(&new_event.timestamp);
306 aed_dokeyupdown(&new_event);
307
308 /* send key-up */
309 new_event.u.k.key =
310 ADBK_KEYUP(ADBK_KEYVAL(event->bytes[0]));
311 microtime(&new_event.timestamp);
312 new_event.bytes[0] = new_event.u.k.key;
313 aed_dokeyupdown(&new_event);
314
315 /* send option-up */
316 new_event.u.k.key = ADBK_KEYUP(ADBK_OPTION);
317 new_event.bytes[0] = new_event.u.k.key;
318 microtime(&new_event.timestamp);
319 aed_dokeyupdown(&new_event);
320 break;
321 }
322 } else {
323 aed_dokeyupdown(event);
324 }
325
326 return result;
327 }
328
329 /*
330 * Keyboard autorepeat timeout function. Sends key up/down events
331 * for the repeating key and schedules the next call at sc_rptinterval
332 * ticks in the future.
333 */
334 static void
335 aed_kbdrpt(void *kstate)
336 {
337 struct aed_softc *sc = (struct aed_softc *)kstate;
338
339 sc->sc_rptevent.bytes[0] |= 0x80;
340 microtime(&sc->sc_rptevent.timestamp);
341 aed_handoff(&sc->sc_rptevent); /* do key up */
342
343 sc->sc_rptevent.bytes[0] &= 0x7f;
344 microtime(&sc->sc_rptevent.timestamp);
345 aed_handoff(&sc->sc_rptevent); /* do key down */
346
347 if (sc->sc_repeating == sc->sc_rptevent.u.k.key) {
348 callout_reset(&sc->sc_repeat_ch, sc->sc_rptinterval,
349 aed_kbdrpt, kstate);
350 }
351 }
352
353
354 /*
355 * Cancels the currently repeating key event if there is one, schedules
356 * a new repeating key event if needed, and hands the event off to the
357 * appropriate subsystem.
358 */
359 static void
360 aed_dokeyupdown(adb_event_t *event)
361 {
362 int kbd_key;
363
364 kbd_key = ADBK_KEYVAL(event->u.k.key);
365 if (ADBK_PRESS(event->u.k.key) && keyboard[kbd_key][0] != 0) {
366 /* ignore shift & control */
367 if (aed_sc->sc_repeating != -1) {
368 callout_stop(&aed_sc->sc_repeat_ch);
369 }
370 aed_sc->sc_rptevent = *event;
371 aed_sc->sc_repeating = kbd_key;
372 callout_reset(&aed_sc->sc_repeat_ch, aed_sc->sc_rptdelay,
373 aed_kbdrpt, (void *)aed_sc);
374 } else {
375 if (aed_sc->sc_repeating != -1) {
376 aed_sc->sc_repeating = -1;
377 callout_stop(&aed_sc->sc_repeat_ch);
378 }
379 aed_sc->sc_rptevent = *event;
380 }
381 aed_handoff(event);
382 }
383
384 /*
385 * Place the event in the event queue if a requesting device is open
386 * and we are not polling.
387 */
388 static void
389 aed_handoff(adb_event_t *event)
390 {
391 if (aed_sc->sc_open && !adb_polling)
392 aed_enqevent(event);
393 }
394
395 /*
396 * Place the event in the event queue and wakeup any waiting processes.
397 */
398 static void
399 aed_enqevent(adb_event_t *event)
400 {
401 int s;
402
403 s = spladb();
404
405 #ifdef DIAGNOSTIC
406 if (aed_sc->sc_evq_tail < 0 || aed_sc->sc_evq_tail >= AED_MAX_EVENTS)
407 panic("adb: event queue tail is out of bounds");
408
409 if (aed_sc->sc_evq_len < 0 || aed_sc->sc_evq_len > AED_MAX_EVENTS)
410 panic("adb: event queue len is out of bounds");
411 #endif
412
413 if (aed_sc->sc_evq_len == AED_MAX_EVENTS) {
414 splx(s);
415 return; /* Oh, well... */
416 }
417 aed_sc->sc_evq[(aed_sc->sc_evq_len + aed_sc->sc_evq_tail) %
418 AED_MAX_EVENTS] = *event;
419 aed_sc->sc_evq_len++;
420
421 selnotify(&aed_sc->sc_selinfo, 0, 0);
422 if (aed_sc->sc_ioproc)
423 psignal(aed_sc->sc_ioproc, SIGIO);
424
425 splx(s);
426 }
427
428 int
429 aedopen(dev_t dev, int flag, int mode, struct lwp *l)
430 {
431 int unit;
432 int error = 0;
433 int s;
434
435 unit = minor(dev);
436
437 if (unit != 0)
438 return (ENXIO);
439
440 s = spladb();
441 if (aed_sc->sc_open) {
442 splx(s);
443 return (EBUSY);
444 }
445 aed_sc->sc_evq_tail = 0;
446 aed_sc->sc_evq_len = 0;
447 aed_sc->sc_open = 1;
448 aed_sc->sc_ioproc = l->l_proc;
449 splx(s);
450
451 return (error);
452 }
453
454
455 int
456 aedclose(dev_t dev, int flag, int mode, struct lwp *l)
457 {
458 int s = spladb();
459
460 aed_sc->sc_open = 0;
461 aed_sc->sc_ioproc = NULL;
462 splx(s);
463
464 return (0);
465 }
466
467
468 int
469 aedread(dev_t dev, struct uio *uio, int flag)
470 {
471 int s, error;
472 int willfit;
473 int total;
474 int firstmove;
475 int moremove;
476
477 if (uio->uio_resid < sizeof(adb_event_t))
478 return (EMSGSIZE); /* close enough. */
479
480 s = spladb();
481 if (aed_sc->sc_evq_len == 0) {
482 splx(s);
483 return (0);
484 }
485 willfit = howmany(uio->uio_resid, sizeof(adb_event_t));
486 total = (aed_sc->sc_evq_len < willfit) ? aed_sc->sc_evq_len : willfit;
487
488 firstmove = (aed_sc->sc_evq_tail + total > AED_MAX_EVENTS)
489 ? (AED_MAX_EVENTS - aed_sc->sc_evq_tail) : total;
490
491 error = uiomove((void *) & aed_sc->sc_evq[aed_sc->sc_evq_tail],
492 firstmove * sizeof(adb_event_t), uio);
493 if (error) {
494 splx(s);
495 return (error);
496 }
497 moremove = total - firstmove;
498
499 if (moremove > 0) {
500 error = uiomove((void *) & aed_sc->sc_evq[0],
501 moremove * sizeof(adb_event_t), uio);
502 if (error) {
503 splx(s);
504 return (error);
505 }
506 }
507 aed_sc->sc_evq_tail = (aed_sc->sc_evq_tail + total) % AED_MAX_EVENTS;
508 aed_sc->sc_evq_len -= total;
509 splx(s);
510 return (0);
511 }
512
513 int
514 aedioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
515 {
516 switch (cmd) {
517 case ADBIOCDEVSINFO: {
518 adb_devinfo_t *di;
519 ADBDataBlock adbdata;
520 int totaldevs;
521 int adbaddr;
522 int i;
523
524 di = (void *)data;
525
526 /* Initialize to no devices */
527 for (i = 0; i < 16; i++)
528 di->dev[i].addr = -1;
529
530 totaldevs = CountADBs();
531 for (i = 1; i <= totaldevs; i++) {
532 adbaddr = GetIndADB(&adbdata, i);
533 di->dev[adbaddr].addr = adbaddr;
534 di->dev[adbaddr].default_addr = (int)(adbdata.origADBAddr);
535 di->dev[adbaddr].handler_id = (int)(adbdata.devType);
536 }
537
538 /* Must call ADB Manager to get devices now */
539 break;
540 }
541
542 case ADBIOCGETREPEAT:{
543 adb_rptinfo_t *ri;
544
545 ri = (void *)data;
546 ri->delay_ticks = aed_sc->sc_rptdelay;
547 ri->interval_ticks = aed_sc->sc_rptinterval;
548 break;
549 }
550
551 case ADBIOCSETREPEAT:{
552 adb_rptinfo_t *ri;
553
554 ri = (void *) data;
555 aed_sc->sc_rptdelay = ri->delay_ticks;
556 aed_sc->sc_rptinterval = ri->interval_ticks;
557 break;
558 }
559
560 case ADBIOCRESET:
561 /* Do nothing for now */
562 break;
563
564 case ADBIOCLISTENCMD:{
565 adb_listencmd_t *lc;
566
567 lc = (void *)data;
568 }
569
570 default:
571 return (EINVAL);
572 }
573 return (0);
574 }
575
576
577 int
578 aedpoll(dev_t dev, int events, struct lwp *l)
579 {
580 int s, revents;
581
582 revents = events & (POLLOUT | POLLWRNORM);
583
584 if ((events & (POLLIN | POLLRDNORM)) == 0)
585 return (revents);
586
587 s = spladb();
588 if (aed_sc->sc_evq_len > 0)
589 revents |= events & (POLLIN | POLLRDNORM);
590 else
591 selrecord(l, &aed_sc->sc_selinfo);
592 splx(s);
593
594 return (revents);
595 }
596
597 static void
598 filt_aedrdetach(struct knote *kn)
599 {
600 int s;
601
602 s = spladb();
603 selremove_knote(&aed_sc->sc_selinfo, kn);
604 splx(s);
605 }
606
607 static int
608 filt_aedread(struct knote *kn, long hint)
609 {
610
611 kn->kn_data = aed_sc->sc_evq_len * sizeof(adb_event_t);
612 return (kn->kn_data > 0);
613 }
614
615 static const struct filterops aedread_filtops = {
616 .f_flags = FILTEROP_ISFD,
617 .f_attach = NULL,
618 .f_detach = filt_aedrdetach,
619 .f_event = filt_aedread
620 };
621
622 int
623 aedkqfilter(dev_t dev, struct knote *kn)
624 {
625 int s;
626
627 switch (kn->kn_filter) {
628 case EVFILT_READ:
629 kn->kn_fop = &aedread_filtops;
630 s = spladb();
631 selrecord_knote(&aed_sc->sc_selinfo, kn);
632 splx(s);
633 break;
634
635 case EVFILT_WRITE:
636 kn->kn_fop = &seltrue_filtops;
637 break;
638
639 default:
640 return (EINVAL);
641 }
642
643 return (0);
644 }
645