akbd.c revision 1.21 1 /* $NetBSD: akbd.c,v 1.21 2002/02/24 20:20:20 dbj 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 #include <dev/ofw/openfirm.h>
47
48 #include <machine/autoconf.h>
49 #define KEYBOARD_ARRAY
50 #include <machine/keyboard.h>
51
52 #include <macppc/dev/adbvar.h>
53 #include <macppc/dev/aedvar.h>
54 #include <macppc/dev/akbdmap.h>
55 #include <macppc/dev/akbdvar.h>
56 #include <macppc/dev/pm_direct.h>
57
58 #include "aed.h"
59
60 /*
61 * Function declarations.
62 */
63 static int akbdmatch __P((struct device *, struct cfdata *, void *));
64 static void akbdattach __P((struct device *, struct device *, void *));
65 void kbd_adbcomplete __P((caddr_t buffer, caddr_t data_area, int adb_command));
66 static void kbd_processevent __P((adb_event_t *event, struct akbd_softc *));
67 static void kbd_passup __P((struct akbd_softc *sc, int));
68 #ifdef notyet
69 static u_char getleds __P((int));
70 static int setleds __P((struct akbd_softc *, u_char));
71 static void blinkleds __P((struct akbd_softc *));
72 #endif
73
74 /* Driver definition. */
75 struct cfattach akbd_ca = {
76 sizeof(struct akbd_softc), akbdmatch, akbdattach
77 };
78
79 extern struct cfdriver akbd_cd;
80
81 int akbd_enable __P((void *, int));
82 void akbd_set_leds __P((void *, int));
83 int akbd_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
84
85 struct wskbd_accessops akbd_accessops = {
86 akbd_enable,
87 akbd_set_leds,
88 akbd_ioctl,
89 };
90
91 void akbd_cngetc __P((void *, u_int *, int *));
92 void akbd_cnpollc __P((void *, int));
93
94 struct wskbd_consops akbd_consops = {
95 akbd_cngetc,
96 akbd_cnpollc,
97 };
98
99 struct wskbd_mapdata akbd_keymapdata = {
100 akbd_keydesctab,
101 #ifdef AKBD_LAYOUT
102 AKBD_LAYOUT,
103 #else
104 KB_US,
105 #endif
106 };
107
108 static int akbd_is_console;
109 static int pcmcia_soft_eject;
110
111 static int
112 akbdmatch(parent, cf, aux)
113 struct device *parent;
114 struct cfdata *cf;
115 void *aux;
116 {
117 struct adb_attach_args *aa_args = aux;
118
119 if (aa_args->origaddr == ADBADDR_KBD)
120 return 1;
121 else
122 return 0;
123 }
124
125 static void
126 akbdattach(parent, self, aux)
127 struct device *parent, *self;
128 void *aux;
129 {
130 ADBSetInfoBlock adbinfo;
131 struct akbd_softc *sc = (struct akbd_softc *)self;
132 struct adb_attach_args *aa_args = aux;
133 int error, kbd_done;
134 short cmd;
135 u_char buffer[9];
136 struct wskbddev_attach_args a;
137
138 /* ohare based models have soft ejectable card slot. */
139 if (OF_finddevice("/bandit/ohare") != -1)
140 pcmcia_soft_eject = 1;
141
142 sc->origaddr = aa_args->origaddr;
143 sc->adbaddr = aa_args->adbaddr;
144 sc->handler_id = aa_args->handler_id;
145
146 sc->sc_leds = (u_int8_t)0x00; /* initially off */
147
148 adbinfo.siServiceRtPtr = (Ptr)kbd_adbcomplete;
149 adbinfo.siDataAreaAddr = (caddr_t)sc;
150
151 switch (sc->handler_id) {
152 case ADB_STDKBD:
153 printf("standard keyboard\n");
154 break;
155 case ADB_ISOKBD:
156 printf("standard keyboard (ISO layout)\n");
157 break;
158 case ADB_EXTKBD:
159 cmd = ADBTALK(sc->adbaddr, 1);
160 kbd_done =
161 (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0);
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 case ADB_PBG3KBD:
231 printf("PowerBook G3 keyboard\n");
232 break;
233 case ADB_PBG3JPKBD:
234 printf("PowerBook G3 keyboard (Japanese layout)\n");
235 break;
236 default:
237 printf("mapped device (%d)\n", sc->handler_id);
238 break;
239 }
240 error = SetADBInfo(&adbinfo, sc->adbaddr);
241 #ifdef ADB_DEBUG
242 if (adb_debug)
243 printf("akbd: returned %d from SetADBInfo\n", error);
244 #endif
245
246 if (akbd_is_console) {
247 wskbd_cnattach(&akbd_consops, sc, &akbd_keymapdata);
248 }
249
250 a.console = akbd_is_console;
251 a.keymap = &akbd_keymapdata;
252 a.accessops = &akbd_accessops;
253 a.accesscookie = sc;
254
255 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
256 }
257
258
259 /*
260 * Handle putting the keyboard data received from the ADB into
261 * an ADB event record.
262 */
263 void
264 kbd_adbcomplete(buffer, data_area, adb_command)
265 caddr_t buffer;
266 caddr_t data_area;
267 int adb_command;
268 {
269 adb_event_t event;
270 struct akbd_softc *ksc;
271 int adbaddr;
272 #ifdef ADB_DEBUG
273 int i;
274
275 if (adb_debug)
276 printf("adb: transaction completion\n");
277 #endif
278
279 adbaddr = ADB_CMDADDR(adb_command);
280 ksc = (struct akbd_softc *)data_area;
281
282 event.addr = adbaddr;
283 event.hand_id = ksc->handler_id;
284 event.def_addr = ksc->origaddr;
285 event.byte_count = buffer[0];
286 memcpy(event.bytes, buffer + 1, event.byte_count);
287
288 #ifdef ADB_DEBUG
289 if (adb_debug) {
290 printf("akbd: from %d at %d (org %d) %d:", event.addr,
291 event.hand_id, event.def_addr, buffer[0]);
292 for (i = 1; i <= buffer[0]; i++)
293 printf(" %x", buffer[i]);
294 printf("\n");
295 }
296 #endif
297
298 microtime(&event.timestamp);
299
300 kbd_processevent(&event, ksc);
301 }
302
303 /*
304 * Given a keyboard ADB event, record the keycodes and call the key
305 * repeat handler, optionally passing the event through the mouse
306 * button emulation handler first.
307 */
308 static void
309 kbd_processevent(event, ksc)
310 adb_event_t *event;
311 struct akbd_softc *ksc;
312 {
313 adb_event_t new_event;
314
315 new_event = *event;
316 new_event.u.k.key = event->bytes[0];
317 new_event.bytes[1] = 0xff;
318 kbd_intr(&new_event);
319 #if NAED > 0
320 aed_input(&new_event);
321 #endif
322 if (event->bytes[1] != 0xff) {
323 new_event.u.k.key = event->bytes[1];
324 new_event.bytes[0] = event->bytes[1];
325 new_event.bytes[1] = 0xff;
326 kbd_intr(&new_event);
327 #if NAED > 0
328 aed_input(&new_event);
329 #endif
330 }
331
332 }
333
334 #ifdef notyet
335 /*
336 * Get the actual hardware LED state and convert it to softc format.
337 */
338 static u_char
339 getleds(addr)
340 int addr;
341 {
342 short cmd;
343 u_char buffer[9], leds;
344
345 leds = 0x00; /* all off */
346 buffer[0] = 0;
347
348 /* talk R2 */
349 cmd = ADBTALK(addr, 2);
350 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
351 buffer[0] > 0)
352 leds = ~(buffer[2]) & 0x07;
353
354 return (leds);
355 }
356
357 /*
358 * Set the keyboard LED's.
359 *
360 * Automatically translates from ioctl/softc format to the
361 * actual keyboard register format
362 */
363 static int
364 setleds(ksc, leds)
365 struct akbd_softc *ksc;
366 u_char leds;
367 {
368 int addr;
369 short cmd;
370 u_char buffer[9];
371
372 if ((leds & 0x07) == (ksc->sc_leds & 0x07))
373 return (0);
374
375 addr = ksc->adbaddr;
376 buffer[0] = 0;
377
378 cmd = ADBTALK(addr, 2);
379 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
380 return (EIO);
381
382 leds = ~leds & 0x07;
383 buffer[2] &= 0xf8;
384 buffer[2] |= leds;
385
386 cmd = ADBLISTEN(addr, 2);
387 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
388
389 cmd = ADBTALK(addr, 2);
390 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
391 return (EIO);
392
393 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
394
395 if ((buffer[2] & 0xf8) != leds)
396 return (EIO);
397 else
398 return (0);
399 }
400
401 /*
402 * Toggle all of the LED's on and off, just for show.
403 */
404 static void
405 blinkleds(ksc)
406 struct akbd_softc *ksc;
407 {
408 int addr, i;
409 u_char blinkleds, origleds;
410
411 addr = ksc->adbaddr;
412 origleds = getleds(addr);
413 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
414
415 (void)setleds(ksc, blinkleds);
416
417 for (i = 0; i < 10000; i++)
418 delay(50);
419
420 /* make sure that we restore the LED settings */
421 i = 10;
422 do {
423 (void)setleds(ksc, (u_char)0x00);
424 } while (setleds(ksc, (u_char)0x00) && (i-- > 0));
425
426 return;
427 }
428 #endif
429
430 int
431 akbd_enable(v, on)
432 void *v;
433 int on;
434 {
435 return 0;
436 }
437
438 void
439 akbd_set_leds(v, on)
440 void *v;
441 int on;
442 {
443 }
444
445 int
446 akbd_ioctl(v, cmd, data, flag, p)
447 void *v;
448 u_long cmd;
449 caddr_t data;
450 int flag;
451 struct proc *p;
452 {
453 switch (cmd) {
454
455 case WSKBDIO_GTYPE:
456 *(int *)data = WSKBD_TYPE_ADB;
457 return 0;
458 case WSKBDIO_SETLEDS:
459 return 0;
460 case WSKBDIO_GETLEDS:
461 *(int *)data = 0;
462 return 0;
463 }
464 /* kbdioctl(...); */
465
466 return -1;
467 }
468
469 extern int adb_polling;
470
471 void
472 kbd_passup(sc,key)
473 struct akbd_softc *sc;
474 int key;
475 {
476 if (sc->sc_polling) {
477 if (sc->sc_npolledkeys <
478 (sizeof(sc->sc_polledkeys)/sizeof(unsigned char))) {
479 sc->sc_polledkeys[sc->sc_npolledkeys++] = key;
480 }
481 #ifdef ADB_DEBUG
482 else {
483 printf("akbd: dumping polled key 0x%02x\n",key);
484 }
485 #endif
486 } else {
487 int press, val;
488 int type;
489
490 press = ADBK_PRESS(key);
491 val = ADBK_KEYVAL(key);
492
493 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
494
495 wskbd_input(sc->sc_wskbddev, type, val);
496 }
497 }
498
499 int
500 kbd_intr(arg)
501 void *arg;
502 {
503 adb_event_t *event = arg;
504 int key;
505
506 struct akbd_softc *sc = akbd_cd.cd_devs[0];
507
508 key = event->u.k.key;
509
510 switch (key) {
511 case 57: /* Caps Lock pressed */
512 case 185: /* Caps Lock released */
513 key = ADBK_KEYDOWN(ADBK_KEYVAL(key));
514 kbd_passup(sc,key);
515 key = ADBK_KEYUP(ADBK_KEYVAL(key));
516 break;
517 case 245:
518 if (pcmcia_soft_eject)
519 pm_eject_pcmcia(0);
520 break;
521 case 244:
522 if (pcmcia_soft_eject)
523 pm_eject_pcmcia(1);
524 break;
525 }
526
527 kbd_passup(sc,key);
528
529 return 0;
530 }
531
532 int
533 akbd_cnattach()
534 {
535
536 akbd_is_console = 1;
537 return 0;
538 }
539
540 void
541 akbd_cngetc(v, type, data)
542 void *v;
543 u_int *type;
544 int *data;
545 {
546 int key, press, val;
547 int s;
548 struct akbd_softc *sc = v;
549
550 s = splhigh();
551
552 KASSERT(sc->sc_polling);
553 KASSERT(adb_polling);
554
555 while (sc->sc_npolledkeys == 0) {
556 adb_intr();
557 DELAY(10000); /* XXX */
558 }
559
560 splx(s);
561
562 key = sc->sc_polledkeys[0];
563 sc->sc_npolledkeys--;
564 memmove(sc->sc_polledkeys,sc->sc_polledkeys+1,
565 sc->sc_npolledkeys * sizeof(unsigned char));
566
567 press = ADBK_PRESS(key);
568 val = ADBK_KEYVAL(key);
569
570 *data = val;
571 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
572 }
573
574 void
575 akbd_cnpollc(v, on)
576 void *v;
577 int on;
578 {
579 struct akbd_softc *sc = v;
580 sc->sc_polling = on;
581 if (!on) {
582 int i;
583 for(i=0;i<sc->sc_npolledkeys;i++) {
584 kbd_passup(sc,sc->sc_polledkeys[i]);
585 }
586 sc->sc_npolledkeys = 0;
587 }
588 adb_polling = on;
589 }
590