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