akbd.c revision 1.15 1 /* $NetBSD: akbd.c,v 1.15 2001/07/12 04:16:58 nathanw 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_TIBKBD:
224 printf("PowerBook G4/iBook (dual USB) keyboard\n");
225 break;
226 case ADB_PBG3JPKBD:
227 printf("PowerBook G3 keyboard (Japanese layout)\n");
228 break;
229 default:
230 printf("mapped device (%d)\n", sc->handler_id);
231 break;
232 }
233 error = SetADBInfo(&adbinfo, sc->adbaddr);
234 #ifdef ADB_DEBUG
235 if (adb_debug)
236 printf("akbd: returned %d from SetADBInfo\n", error);
237 #endif
238
239 a.console = akbd_is_console;
240 a.keymap = &akbd_keymapdata;
241 a.accessops = &akbd_accessops;
242 a.accesscookie = sc;
243
244 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
245 }
246
247
248 /*
249 * Handle putting the keyboard data received from the ADB into
250 * an ADB event record.
251 */
252 void
253 kbd_adbcomplete(buffer, data_area, adb_command)
254 caddr_t buffer;
255 caddr_t data_area;
256 int adb_command;
257 {
258 adb_event_t event;
259 struct akbd_softc *ksc;
260 int adbaddr;
261 #ifdef ADB_DEBUG
262 int i;
263
264 if (adb_debug)
265 printf("adb: transaction completion\n");
266 #endif
267
268 adbaddr = ADB_CMDADDR(adb_command);
269 ksc = (struct akbd_softc *)data_area;
270
271 event.addr = adbaddr;
272 event.hand_id = ksc->handler_id;
273 event.def_addr = ksc->origaddr;
274 event.byte_count = buffer[0];
275 memcpy(event.bytes, buffer + 1, event.byte_count);
276
277 #ifdef ADB_DEBUG
278 if (adb_debug) {
279 printf("akbd: from %d at %d (org %d) %d:", event.addr,
280 event.hand_id, event.def_addr, buffer[0]);
281 for (i = 1; i <= buffer[0]; i++)
282 printf(" %x", buffer[i]);
283 printf("\n");
284 }
285 #endif
286
287 microtime(&event.timestamp);
288
289 kbd_processevent(&event, ksc);
290 }
291
292 /*
293 * Given a keyboard ADB event, record the keycodes and call the key
294 * repeat handler, optionally passing the event through the mouse
295 * button emulation handler first.
296 */
297 static void
298 kbd_processevent(event, ksc)
299 adb_event_t *event;
300 struct akbd_softc *ksc;
301 {
302 adb_event_t new_event;
303
304 new_event = *event;
305 new_event.u.k.key = event->bytes[0];
306 new_event.bytes[1] = 0xff;
307 kbd_intr(&new_event);
308 #if NAED > 0
309 aed_input(&new_event);
310 #endif
311 if (event->bytes[1] != 0xff) {
312 new_event.u.k.key = event->bytes[1];
313 new_event.bytes[0] = event->bytes[1];
314 new_event.bytes[1] = 0xff;
315 kbd_intr(&new_event);
316 #if NAED > 0
317 aed_input(&new_event);
318 #endif
319 }
320
321 }
322
323 #ifdef notyet
324 /*
325 * Get the actual hardware LED state and convert it to softc format.
326 */
327 static u_char
328 getleds(addr)
329 int addr;
330 {
331 short cmd;
332 u_char buffer[9], leds;
333
334 leds = 0x00; /* all off */
335 buffer[0] = 0;
336
337 /* talk R2 */
338 cmd = ADBTALK(addr, 2);
339 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
340 buffer[0] > 0)
341 leds = ~(buffer[2]) & 0x07;
342
343 return (leds);
344 }
345
346 /*
347 * Set the keyboard LED's.
348 *
349 * Automatically translates from ioctl/softc format to the
350 * actual keyboard register format
351 */
352 static int
353 setleds(ksc, leds)
354 struct akbd_softc *ksc;
355 u_char leds;
356 {
357 int addr;
358 short cmd;
359 u_char buffer[9];
360
361 if ((leds & 0x07) == (ksc->sc_leds & 0x07))
362 return (0);
363
364 addr = ksc->adbaddr;
365 buffer[0] = 0;
366
367 cmd = ADBTALK(addr, 2);
368 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
369 return (EIO);
370
371 leds = ~leds & 0x07;
372 buffer[2] &= 0xf8;
373 buffer[2] |= leds;
374
375 cmd = ADBLISTEN(addr, 2);
376 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
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 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
383
384 if ((buffer[2] & 0xf8) != leds)
385 return (EIO);
386 else
387 return (0);
388 }
389
390 /*
391 * Toggle all of the LED's on and off, just for show.
392 */
393 static void
394 blinkleds(ksc)
395 struct akbd_softc *ksc;
396 {
397 int addr, i;
398 u_char blinkleds, origleds;
399
400 addr = ksc->adbaddr;
401 origleds = getleds(addr);
402 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
403
404 (void)setleds(ksc, blinkleds);
405
406 for (i = 0; i < 10000; i++)
407 delay(50);
408
409 /* make sure that we restore the LED settings */
410 i = 10;
411 do {
412 (void)setleds(ksc, (u_char)0x00);
413 } while (setleds(ksc, (u_char)0x00) && (i-- > 0));
414
415 return;
416 }
417 #endif
418
419 int
420 akbd_enable(v, on)
421 void *v;
422 int on;
423 {
424 return 0;
425 }
426
427 void
428 akbd_set_leds(v, on)
429 void *v;
430 int on;
431 {
432 }
433
434 int
435 akbd_ioctl(v, cmd, data, flag, p)
436 void *v;
437 u_long cmd;
438 caddr_t data;
439 int flag;
440 struct proc *p;
441 {
442 switch (cmd) {
443
444 case WSKBDIO_GTYPE:
445 *(int *)data = 0; /* XXX */
446 return 0;
447 case WSKBDIO_SETLEDS:
448 return 0;
449 case WSKBDIO_GETLEDS:
450 *(int *)data = 0;
451 return 0;
452 }
453 /* kbdioctl(...); */
454
455 return -1;
456 }
457
458 static int polledkey;
459 extern int adb_polling;
460
461 int
462 kbd_intr(arg)
463 void *arg;
464 {
465 adb_event_t *event = arg;
466 int key, press, val;
467 int type;
468
469 struct akbd_softc *sc = akbd_cd.cd_devs[0];
470
471 key = event->u.k.key;
472 press = ADBK_PRESS(key);
473 val = ADBK_KEYVAL(key);
474
475 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
476
477 switch (key) {
478 case 57: /* Caps Lock pressed */
479 case 185: /* Caps Lock released */
480 type = WSCONS_EVENT_KEY_DOWN;
481 wskbd_input(sc->sc_wskbddev, type, val);
482 type = WSCONS_EVENT_KEY_UP;
483 break;
484 case 245:
485 pm_eject_pcmcia(0);
486 break;
487 case 244:
488 pm_eject_pcmcia(1);
489 break;
490 }
491
492 if (adb_polling)
493 polledkey = key;
494 else
495 wskbd_input(sc->sc_wskbddev, type, val);
496
497 return 0;
498 }
499
500 int
501 akbd_cnattach()
502 {
503
504 akbd_is_console = 1;
505 wskbd_cnattach(&akbd_consops, NULL, &akbd_keymapdata);
506 return 0;
507 }
508
509 void
510 akbd_cngetc(v, type, data)
511 void *v;
512 u_int *type;
513 int *data;
514 {
515 int key, press, val;
516 int s;
517
518 s = splhigh();
519
520 polledkey = -1;
521 adb_polling = 1;
522
523 while (polledkey == -1) {
524 adb_intr();
525 DELAY(10000); /* XXX */
526 }
527
528 adb_polling = 0;
529 splx(s);
530
531 key = polledkey;
532 press = ADBK_PRESS(key);
533 val = ADBK_KEYVAL(key);
534
535 *data = val;
536 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
537 }
538
539 void
540 akbd_cnpollc(v, on)
541 void *v;
542 int on;
543 {
544 }
545