akbd.c revision 1.7.2.1 1 /* $NetBSD: akbd.c,v 1.7.2.1 2000/06/22 17:01:13 minoura 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 if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */
340 kbd_intr(&new_event);
341 #else
342 /* do nothing */ ;
343 #endif
344 if (event->bytes[1] != 0xff) {
345 new_event.u.k.key = event->bytes[1];
346 new_event.bytes[0] = event->bytes[1];
347 new_event.bytes[1] = 0xff;
348 #if NAED > 0
349 if (adb_polling || !aed_input(&new_event))
350 #endif
351 #if NWSKBD > 0
352 if (ksc->sc_wskbddev != NULL) /* wskbd is attached? */
353 kbd_intr(&new_event);
354 #else
355 /* do nothing */ ;
356 #endif
357 }
358
359 }
360
361 #ifdef notyet
362 /*
363 * Get the actual hardware LED state and convert it to softc format.
364 */
365 static u_char
366 getleds(addr)
367 int addr;
368 {
369 short cmd;
370 u_char buffer[9], leds;
371
372 leds = 0x00; /* all off */
373 buffer[0] = 0;
374 kbd_done = 0;
375
376 cmd = ADBTALK(addr, 2);
377 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
378 while (!kbd_done)
379 /* busy-wait until done */ ;
380
381 if (buffer[0] > 0)
382 leds = ~(buffer[2]) & 0x07;
383
384 return (leds);
385 }
386
387 /*
388 * Set the keyboard LED's.
389 *
390 * Automatically translates from ioctl/softc format to the
391 * actual keyboard register format
392 */
393 static int
394 setleds(ksc, leds)
395 struct akbd_softc *ksc;
396 u_char leds;
397 {
398 int addr;
399 short cmd;
400 u_char buffer[9];
401
402 if ((leds & 0x07) == (ksc->sc_leds & 0x07))
403 return (0);
404
405 addr = ksc->adbaddr;
406 buffer[0] = 0;
407 kbd_done = 0;
408
409 cmd = ADBTALK(addr, 2);
410 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
411 while (!kbd_done)
412 /* busy-wait until done */ ;
413
414 if (buffer[0] == 0)
415 return (EIO);
416
417 leds = ~leds & 0x07;
418 buffer[2] &= 0xf8;
419 buffer[2] |= leds;
420
421 cmd = ADBLISTEN(addr, 2);
422 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
423 while (!kbd_done)
424 /* busy-wait until done */ ;
425
426 /* talk R2 */
427 cmd = ADBTALK(addr, 2);
428 ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
429 while (!kbd_done)
430 /* busy-wait until done */ ;
431
432 if (buffer[0] == 0)
433 return (EIO);
434
435 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
436
437 if ((buffer[2] & 0xf8) != leds)
438 return (EIO);
439 else
440 return (0);
441 }
442
443 /*
444 * Toggle all of the LED's on and off, just for show.
445 */
446 static void
447 blinkleds(ksc)
448 struct akbd_softc *ksc;
449 {
450 int addr, i;
451 u_char blinkleds, origleds;
452
453 addr = ksc->adbaddr;
454 origleds = getleds(addr);
455 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
456
457 (void)setleds(ksc, blinkleds);
458
459 for (i = 0; i < 10000; i++)
460 delay(50);
461
462 /* make sure that we restore the LED settings */
463 i = 10;
464 do {
465 (void)setleds(ksc, (u_char)0x00);
466 } while (setleds(ksc, (u_char)0x00) && (i-- > 0));
467
468 return;
469 }
470 #endif
471
472 int
473 akbd_is_console()
474 {
475 extern struct mac68k_machine_S mac68k_machine;
476 static int akbd_console_initted = 0;
477
478 return ((mac68k_machine.serial_console & 0x03) == 0) &&
479 (++akbd_console_initted == 1);
480 }
481
482 int
483 akbd_enable(v, on)
484 void *v;
485 int on;
486 {
487 return 0;
488 }
489
490 void
491 akbd_set_leds(v, on)
492 void *v;
493 int on;
494 {
495 }
496
497 int
498 akbd_ioctl(v, cmd, data, flag, p)
499 void *v;
500 u_long cmd;
501 caddr_t data;
502 int flag;
503 struct proc *p;
504 {
505 switch (cmd) {
506
507 case WSKBDIO_GTYPE:
508 *(int *)data = 0; /* XXX */
509 return 0;
510 case WSKBDIO_SETLEDS:
511 return 0;
512 case WSKBDIO_GETLEDS:
513 *(int *)data = 0;
514 return 0;
515 case WSKBDIO_BELL:
516 case WSKBDIO_COMPLEXBELL:
517 #define d ((struct wskbd_bell_data *)data)
518 mac68k_ring_bell(d->pitch, d->period * HZ / 1000, 100);
519 /* comes in as msec, goes out as ticks; volume ignored */
520 #undef d
521 return (0);
522 }
523 /* kbdioctl(...); */
524
525 return -1;
526 }
527
528 static int polledkey;
529 extern int adb_polling;
530
531 int
532 kbd_intr(event)
533 adb_event_t *event;
534 {
535 int key, press, val;
536 int type;
537
538 struct akbd_softc *sc = akbd_cd.cd_devs[0];
539
540 key = event->u.k.key;
541 press = ADBK_PRESS(key);
542 val = ADBK_KEYVAL(key);
543
544 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
545
546 if (key == 185) { /* Caps Lock released */
547 type = WSCONS_EVENT_KEY_DOWN;
548 wskbd_input(sc->sc_wskbddev, type, val);
549 type = WSCONS_EVENT_KEY_UP;
550 }
551
552 if (adb_polling)
553 polledkey = key;
554 else
555 wskbd_input(sc->sc_wskbddev, type, val);
556
557 return 0;
558 }
559
560 int
561 akbd_cnattach()
562 {
563 if (!akbd_is_console())
564 return -1;
565
566 wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata);
567
568 return 0;
569 }
570
571 void
572 akbd_cngetc(v, type, data)
573 void *v;
574 u_int *type;
575 int *data;
576 {
577 int intbits, key, press, val;
578 int s;
579
580 s = splhigh();
581
582 polledkey = -1;
583 adb_polling = 1;
584
585 while (polledkey == -1) {
586 intbits = via_reg(VIA1, vIFR);
587
588 if (intbits & V1IF_ADBRDY) {
589 mrg_adbintr();
590 via_reg(VIA1, vIFR) = V1IF_ADBRDY;
591 }
592 if (intbits & 0x10) {
593 mrg_pmintr();
594 via_reg(VIA1, vIFR) = 0x10;
595 }
596 }
597
598 adb_polling = 0;
599 splx(s);
600
601 key = polledkey;
602 press = ADBK_PRESS(key);
603 val = ADBK_KEYVAL(key);
604
605 *data = val;
606 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
607 }
608
609 void
610 akbd_cnpollc(v, on)
611 void *v;
612 int on;
613 {
614 }
615