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