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