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