akbd.c revision 1.8 1 /* $NetBSD: akbd.c,v 1.8 2000/06/17 17:46:40 scottr Exp $ */
2
3 /*
4 * Copyright (C) 1998 Colin Wood
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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Colin Wood.
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 "aed.h"
45 #include "wskbd.h"
46
47 #include <dev/wscons/wsconsio.h>
48 #include <dev/wscons/wskbdvar.h>
49 #include <dev/wscons/wsksymdef.h>
50 #include <dev/wscons/wsksymvar.h>
51
52 #include <machine/autoconf.h>
53 #include <machine/cpu.h>
54 #define KEYBOARD_ARRAY
55 #include <machine/keyboard.h>
56 #include <machine/viareg.h>
57
58 #include <mac68k/mac68k/macrom.h>
59 #include <mac68k/dev/adbvar.h>
60 #include <mac68k/dev/aedvar.h>
61 #include <mac68k/dev/akbdmap.h>
62 #include <mac68k/dev/akbdvar.h>
63 #include <mac68k/dev/amsvar.h>
64
65 /*
66 * Function declarations.
67 */
68 static int akbdmatch __P((struct device *, struct cfdata *, void *));
69 static void akbdattach __P((struct device *, struct device *, void *));
70 void kbd_adbcomplete __P((caddr_t buffer, caddr_t data_area, int adb_command));
71 static void kbd_processevent __P((adb_event_t *event, struct akbd_softc *));
72 #ifdef notyet
73 static u_char getleds __P((int));
74 static int setleds __P((struct akbd_softc *, u_char));
75 static void blinkleds __P((struct akbd_softc *));
76 #endif
77
78 /*
79 * Local variables.
80 */
81 static volatile int kbd_done; /* Did ADBOp() complete? */
82
83 /* Driver definition. */
84 struct cfattach akbd_ca = {
85 sizeof(struct akbd_softc), akbdmatch, akbdattach
86 };
87
88 extern struct cfdriver akbd_cd;
89
90 int kbd_intr __P((adb_event_t *event));
91 int akbd_enable __P((void *, int));
92 void akbd_set_leds __P((void *, int));
93 int akbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
94
95 struct wskbd_accessops akbd_accessops = {
96 akbd_enable,
97 akbd_set_leds,
98 akbd_ioctl,
99 };
100
101 void akbd_cngetc __P((void *, u_int *, int *));
102 void akbd_cnpollc __P((void *, int));
103 int akbd_cnattach __P((void));
104
105 struct wskbd_consops akbd_consops = {
106 akbd_cngetc,
107 akbd_cnpollc,
108 };
109
110 struct wskbd_mapdata akbd_keymapdata = {
111 akbd_keydesctab,
112 KB_US,
113 };
114
115 static int akbd_is_console __P((void));
116
117 static int
118 akbdmatch(parent, cf, aux)
119 struct device *parent;
120 struct cfdata *cf;
121 void *aux;
122 {
123 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
124
125 if (aa_args->origaddr == ADBADDR_KBD)
126 return 1;
127 else
128 return 0;
129 }
130
131 static void
132 akbdattach(parent, self, aux)
133 struct device *parent, *self;
134 void *aux;
135 {
136 ADBSetInfoBlock adbinfo;
137 struct akbd_softc *sc = (struct akbd_softc *)self;
138 struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
139 int count, error;
140 short cmd;
141 u_char buffer[9];
142 #if NWSKBD > 0
143 struct wskbddev_attach_args a;
144 int wskbd_eligible;
145
146 wskbd_eligible = 1;
147 #endif
148
149 sc->origaddr = aa_args->origaddr;
150 sc->adbaddr = aa_args->adbaddr;
151 sc->handler_id = aa_args->handler_id;
152
153 sc->sc_leds = (u_int8_t)0x00; /* initially off */
154
155 adbinfo.siServiceRtPtr = (Ptr)adb_kbd_asmcomplete;
156 adbinfo.siDataAreaAddr = (caddr_t)sc;
157
158 switch (sc->handler_id) {
159 case ADB_STDKBD:
160 printf("standard keyboard\n");
161 break;
162 case ADB_ISOKBD:
163 printf("standard keyboard (ISO layout)\n");
164 break;
165 case ADB_EXTKBD:
166 kbd_done = 0;
167 cmd = ADBTALK(sc->adbaddr, 1);
168 ADBOp((Ptr)buffer, (Ptr)extdms_complete,
169 (Ptr)&kbd_done, cmd);
170
171 /* Wait until done, but no more than 2 secs */
172 count = 40000;
173 while (!kbd_done && count-- > 0)
174 delay(50);
175
176 /* Ignore Logitech MouseMan/Trackman pseudo keyboard */
177 if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) {
178 printf("Mouseman (non-EMP) pseudo keyboard\n");
179 adbinfo.siServiceRtPtr = (Ptr)0;
180 adbinfo.siDataAreaAddr = (Ptr)0;
181 #if NWSKBD > 0
182 wskbd_eligible = 0;
183 #endif /* NWSKBD > 0 */
184 } else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) {
185 printf("Trackman (non-EMP) pseudo keyboard\n");
186 adbinfo.siServiceRtPtr = (Ptr)0;
187 adbinfo.siDataAreaAddr = (Ptr)0;
188 #if NWSKBD > 0
189 wskbd_eligible = 0;
190 #endif /* NWSKBD > 0 */
191 } else {
192 printf("extended keyboard\n");
193 #ifdef notyet
194 blinkleds(sc);
195 #endif
196 }
197 break;
198 case ADB_EXTISOKBD:
199 printf("extended keyboard (ISO layout)\n");
200 #ifdef notyet
201 blinkleds(sc);
202 #endif
203 break;
204 case ADB_KBDII:
205 printf("keyboard II\n");
206 break;
207 case ADB_ISOKBDII:
208 printf("keyboard II (ISO layout)\n");
209 break;
210 case ADB_PBKBD:
211 printf("PowerBook keyboard\n");
212 break;
213 case ADB_PBISOKBD:
214 printf("PowerBook keyboard (ISO layout)\n");
215 break;
216 case ADB_ADJKPD:
217 printf("adjustable keypad\n");
218 #if NWSKBD > 0
219 wskbd_eligible = 0;
220 #endif /* NWSKBD > 0 */
221 break;
222 case ADB_ADJKBD:
223 printf("adjustable keyboard\n");
224 break;
225 case ADB_ADJISOKBD:
226 printf("adjustable keyboard (ISO layout)\n");
227 break;
228 case ADB_ADJJAPKBD:
229 printf("adjustable keyboard (Japanese layout)\n");
230 break;
231 case ADB_PBEXTISOKBD:
232 printf("PowerBook extended keyboard (ISO layout)\n");
233 break;
234 case ADB_PBEXTJAPKBD:
235 printf("PowerBook extended keyboard (Japanese layout)\n");
236 break;
237 case ADB_JPKBDII:
238 printf("keyboard II (Japanese layout)\n");
239 break;
240 case ADB_PBEXTKBD:
241 printf("PowerBook extended keyboard\n");
242 break;
243 case ADB_DESIGNKBD:
244 printf("extended keyboard\n");
245 #ifdef notyet
246 blinkleds(sc);
247 #endif
248 break;
249 case ADB_PBJPKBD:
250 printf("PowerBook keyboard (Japanese layout)\n");
251 break;
252 default:
253 printf("mapped device (%d)\n", sc->handler_id);
254 #if NWSKBD > 0
255 wskbd_eligible = 0;
256 #endif /* NWSKBD > 0 */
257 break;
258 }
259 error = SetADBInfo(&adbinfo, sc->adbaddr);
260 #ifdef ADB_DEBUG
261 if (adb_debug)
262 printf("akbd: returned %d from SetADBInfo\n", error);
263 #endif
264
265 #if NWSKBD > 0
266 a.console = wskbd_eligible && akbd_is_console();
267 a.keymap = &akbd_keymapdata;
268 a.accessops = &akbd_accessops;
269 a.accesscookie = sc;
270
271 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
272 #endif
273 }
274
275
276 /*
277 * Handle putting the keyboard data received from the ADB into
278 * an ADB event record.
279 */
280 void
281 kbd_adbcomplete(buffer, data_area, adb_command)
282 caddr_t buffer;
283 caddr_t data_area;
284 int adb_command;
285 {
286 adb_event_t event;
287 struct akbd_softc *ksc;
288 int adbaddr;
289 #ifdef ADB_DEBUG
290 int i;
291
292 if (adb_debug)
293 printf("adb: transaction completion\n");
294 #endif
295
296 adbaddr = ADB_CMDADDR(adb_command);
297 ksc = (struct akbd_softc *)data_area;
298
299 event.addr = adbaddr;
300 event.hand_id = ksc->handler_id;
301 event.def_addr = ksc->origaddr;
302 event.byte_count = buffer[0];
303 memcpy(event.bytes, buffer + 1, event.byte_count);
304
305 #ifdef ADB_DEBUG
306 if (adb_debug) {
307 printf("akbd: from %d at %d (org %d) %d:", event.addr,
308 event.hand_id, event.def_addr, buffer[0]);
309 for (i = 1; i <= buffer[0]; i++)
310 printf(" %x", buffer[i]);
311 printf("\n");
312 }
313 #endif
314
315 microtime(&event.timestamp);
316
317 kbd_processevent(&event, ksc);
318 }
319
320 /*
321 * Given a keyboard ADB event, record the keycodes and call the key
322 * repeat handler, optionally passing the event through the mouse
323 * button emulation handler first.
324 */
325 static void
326 kbd_processevent(event, ksc)
327 adb_event_t *event;
328 struct akbd_softc *ksc;
329 {
330 adb_event_t new_event;
331
332 new_event = *event;
333 new_event.u.k.key = event->bytes[0];
334 new_event.bytes[1] = 0xff;
335 #if NAED > 0
336 if (adb_polling || !aed_input(&new_event))
337 #endif
338 #if NWSKBD > 0
339 kbd_intr(&new_event);
340 #else
341 /* do nothing */ ;
342 #endif
343 if (event->bytes[1] != 0xff) {
344 new_event.u.k.key = event->bytes[1];
345 new_event.bytes[0] = event->bytes[1];
346 new_event.bytes[1] = 0xff;
347 #if NAED > 0
348 if (adb_polling || !aed_input(&new_event))
349 #endif
350 #if NWSKBD > 0
351 kbd_intr(&new_event);
352 #else
353 /* do nothing */ ;
354 #endif
355 }
356
357 }
358
359 #ifdef notyet
360 /*
361 * Get the actual hardware LED state and convert it to softc format.
362 */
363 static u_char
364 getleds(addr)
365 int addr;
366 {
367 short cmd;
368 u_char buffer[9], leds;
369
370 leds = 0x00; /* all off */
371 buffer[0] = 0;
372 kbd_done = 0;
373
374 cmd = ADBTALK(addr, 2);
375 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
376 while (!kbd_done)
377 /* busy-wait until done */ ;
378
379 if (buffer[0] > 0)
380 leds = ~(buffer[2]) & 0x07;
381
382 return (leds);
383 }
384
385 /*
386 * Set the keyboard LED's.
387 *
388 * Automatically translates from ioctl/softc format to the
389 * actual keyboard register format
390 */
391 static int
392 setleds(ksc, leds)
393 struct akbd_softc *ksc;
394 u_char leds;
395 {
396 int addr;
397 short cmd;
398 u_char buffer[9];
399
400 if ((leds & 0x07) == (ksc->sc_leds & 0x07))
401 return (0);
402
403 addr = ksc->adbaddr;
404 buffer[0] = 0;
405 kbd_done = 0;
406
407 cmd = ADBTALK(addr, 2);
408 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
409 while (!kbd_done)
410 /* busy-wait until done */ ;
411
412 if (buffer[0] == 0)
413 return (EIO);
414
415 leds = ~leds & 0x07;
416 buffer[2] &= 0xf8;
417 buffer[2] |= leds;
418
419 cmd = ADBLISTEN(addr, 2);
420 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
421 while (!kbd_done)
422 /* busy-wait until done */ ;
423
424 /* talk R2 */
425 cmd = ADBTALK(addr, 2);
426 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
427 while (!kbd_done)
428 /* busy-wait until done */ ;
429
430 if (buffer[0] == 0)
431 return (EIO);
432
433 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
434
435 if ((buffer[2] & 0xf8) != leds)
436 return (EIO);
437 else
438 return (0);
439 }
440
441 /*
442 * Toggle all of the LED's on and off, just for show.
443 */
444 static void
445 blinkleds(ksc)
446 struct akbd_softc *ksc;
447 {
448 int addr, i;
449 u_char blinkleds, origleds;
450
451 addr = ksc->adbaddr;
452 origleds = getleds(addr);
453 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
454
455 (void)setleds(ksc, blinkleds);
456
457 for (i = 0; i < 10000; i++)
458 delay(50);
459
460 /* make sure that we restore the LED settings */
461 i = 10;
462 do {
463 (void)setleds(ksc, (u_char)0x00);
464 } while (setleds(ksc, (u_char)0x00) && (i-- > 0));
465
466 return;
467 }
468 #endif
469
470 int
471 akbd_is_console()
472 {
473 extern struct mac68k_machine_S mac68k_machine;
474 static int akbd_console_initted = 0;
475
476 return ((mac68k_machine.serial_console & 0x03) == 0) &&
477 (++akbd_console_initted == 1);
478 }
479
480 int
481 akbd_enable(v, on)
482 void *v;
483 int on;
484 {
485 return 0;
486 }
487
488 void
489 akbd_set_leds(v, on)
490 void *v;
491 int on;
492 {
493 }
494
495 int
496 akbd_ioctl(v, cmd, data, flag, p)
497 void *v;
498 u_long cmd;
499 caddr_t data;
500 int flag;
501 struct proc *p;
502 {
503 switch (cmd) {
504
505 case WSKBDIO_GTYPE:
506 *(int *)data = 0; /* XXX */
507 return 0;
508 case WSKBDIO_SETLEDS:
509 return 0;
510 case WSKBDIO_GETLEDS:
511 *(int *)data = 0;
512 return 0;
513 case WSKBDIO_BELL:
514 case WSKBDIO_COMPLEXBELL:
515 #define d ((struct wskbd_bell_data *)data)
516 mac68k_ring_bell(d->pitch, d->period * HZ / 1000, 100);
517 /* comes in as msec, goes out as ticks; volume ignored */
518 #undef d
519 return (0);
520 }
521 /* kbdioctl(...); */
522
523 return -1;
524 }
525
526 static int polledkey;
527 extern int adb_polling;
528
529 int
530 kbd_intr(event)
531 adb_event_t *event;
532 {
533 int key, press, val;
534 int type;
535
536 struct akbd_softc *sc = akbd_cd.cd_devs[0];
537
538 key = event->u.k.key;
539 press = ADBK_PRESS(key);
540 val = ADBK_KEYVAL(key);
541
542 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
543
544 if (key == 185) { /* Caps Lock released */
545 type = WSCONS_EVENT_KEY_DOWN;
546 wskbd_input(sc->sc_wskbddev, type, val);
547 type = WSCONS_EVENT_KEY_UP;
548 }
549
550 if (adb_polling)
551 polledkey = key;
552 else
553 wskbd_input(sc->sc_wskbddev, type, val);
554
555 return 0;
556 }
557
558 int
559 akbd_cnattach()
560 {
561 if (!akbd_is_console())
562 return -1;
563
564 wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata);
565
566 return 0;
567 }
568
569 void
570 akbd_cngetc(v, type, data)
571 void *v;
572 u_int *type;
573 int *data;
574 {
575 int intbits, key, press, val;
576 int s;
577
578 s = splhigh();
579
580 polledkey = -1;
581 adb_polling = 1;
582
583 while (polledkey == -1) {
584 intbits = via_reg(VIA1, vIFR);
585
586 if (intbits & V1IF_ADBRDY) {
587 mrg_adbintr();
588 via_reg(VIA1, vIFR) = V1IF_ADBRDY;
589 }
590 if (intbits & 0x10) {
591 mrg_pmintr();
592 via_reg(VIA1, vIFR) = 0x10;
593 }
594 }
595
596 adb_polling = 0;
597 splx(s);
598
599 key = polledkey;
600 press = ADBK_PRESS(key);
601 val = ADBK_KEYVAL(key);
602
603 *data = val;
604 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
605 }
606
607 void
608 akbd_cnpollc(v, on)
609 void *v;
610 int on;
611 {
612 }
613