No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / macppc / dev / akbd.c
blob6090c43583fc53b5fab4ba354c73976ccba27fa4
1 /* $NetBSD: akbd.c,v 1.42 2009/03/14 21:04:11 dsl Exp $ */
3 /*
4 * Copyright (C) 1998 Colin Wood
5 * All rights reserved.
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.
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.
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: akbd.c,v 1.42 2009/03/14 21:04:11 dsl Exp $");
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>
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>
51 #include <dev/adb/adb_keymap.h>
53 #include <machine/autoconf.h>
54 #define KEYBOARD_ARRAY
55 #include <machine/keyboard.h>
57 #include <macppc/dev/adbvar.h>
58 #include <macppc/dev/aedvar.h>
59 #include <macppc/dev/akbdvar.h>
60 #include <macppc/dev/pm_direct.h>
62 #include "aed.h"
65 * Function declarations.
67 static int akbdmatch(struct device *, struct cfdata *, void *);
68 static void akbdattach(struct device *, struct device *, void *);
69 static void kbd_processevent(adb_event_t *event, struct akbd_softc *);
70 #ifdef notyet
71 static u_char getleds(int);
72 static int setleds(struct akbd_softc *, u_char);
73 static void blinkleds(struct akbd_softc *);
74 #endif
76 /* Driver definition. */
77 CFATTACH_DECL(akbd, sizeof(struct akbd_softc),
78 akbdmatch, akbdattach, NULL, NULL);
80 extern struct cfdriver akbd_cd;
82 int akbd_enable(void *, int);
83 void akbd_set_leds(void *, int);
84 int akbd_ioctl(void *, u_long, void *, int, struct lwp *);
86 struct wskbd_accessops akbd_accessops = {
87 akbd_enable,
88 akbd_set_leds,
89 akbd_ioctl,
92 void akbd_cngetc(void *, u_int *, int *);
93 void akbd_cnpollc(void *, int);
95 struct wskbd_consops akbd_consops = {
96 akbd_cngetc,
97 akbd_cnpollc,
100 struct wskbd_mapdata akbd_keymapdata = {
101 akbd_keydesctab,
102 #ifdef AKBD_LAYOUT
103 AKBD_LAYOUT,
104 #else
105 KB_US,
106 #endif
109 static int akbd_is_console;
110 static int akbd_console_attached;
111 static int pcmcia_soft_eject;
113 static int
114 akbdmatch(struct device *parent, struct cfdata *cf, void *aux)
116 struct adb_attach_args *aa_args = aux;
118 if (aa_args->origaddr == ADBADDR_KBD)
119 return 1;
120 else
121 return 0;
124 static void
125 akbdattach(struct device *parent, struct device *self, void *aux)
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;
135 /* ohare based models have soft ejectable card slot. */
136 if (OF_finddevice("/bandit/ohare") != -1)
137 pcmcia_soft_eject = 1;
139 sc->origaddr = aa_args->origaddr;
140 sc->adbaddr = aa_args->adbaddr;
141 sc->handler_id = aa_args->handler_id;
143 sc->sc_leds = (u_int8_t)0x00; /* initially off */
145 adbinfo.siServiceRtPtr = (Ptr)kbd_adbcomplete;
146 adbinfo.siDataAreaAddr = (void *)sc;
148 switch (sc->handler_id) {
149 case ADB_STDKBD:
150 printf("standard keyboard\n");
151 break;
152 case ADB_ISOKBD:
153 printf("standard keyboard (ISO layout)\n");
154 break;
155 case ADB_EXTKBD:
156 cmd = ADBTALK(sc->adbaddr, 1);
157 kbd_done =
158 (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd) == 0);
160 /* Ignore Logitech MouseMan/Trackman pseudo keyboard */
161 if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) {
162 printf("Mouseman (non-EMP) pseudo keyboard\n");
163 adbinfo.siServiceRtPtr = (Ptr)0;
164 adbinfo.siDataAreaAddr = (Ptr)0;
165 } else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) {
166 printf("Trackman (non-EMP) pseudo keyboard\n");
167 adbinfo.siServiceRtPtr = (Ptr)0;
168 adbinfo.siDataAreaAddr = (Ptr)0;
169 } else {
170 printf("extended keyboard\n");
171 #ifdef notyet
172 blinkleds(sc);
173 #endif
175 break;
176 case ADB_EXTISOKBD:
177 printf("extended keyboard (ISO layout)\n");
178 #ifdef notyet
179 blinkleds(sc);
180 #endif
181 break;
182 case ADB_KBDII:
183 printf("keyboard II\n");
184 break;
185 case ADB_ISOKBDII:
186 printf("keyboard II (ISO layout)\n");
187 break;
188 case ADB_PBKBD:
189 printf("PowerBook keyboard\n");
190 break;
191 case ADB_PBISOKBD:
192 printf("PowerBook keyboard (ISO layout)\n");
193 break;
194 case ADB_ADJKPD:
195 printf("adjustable keypad\n");
196 break;
197 case ADB_ADJKBD:
198 printf("adjustable keyboard\n");
199 break;
200 case ADB_ADJISOKBD:
201 printf("adjustable keyboard (ISO layout)\n");
202 break;
203 case ADB_ADJJAPKBD:
204 printf("adjustable keyboard (Japanese layout)\n");
205 break;
206 case ADB_PBEXTISOKBD:
207 printf("PowerBook extended keyboard (ISO layout)\n");
208 break;
209 case ADB_PBEXTJAPKBD:
210 printf("PowerBook extended keyboard (Japanese layout)\n");
211 break;
212 case ADB_JPKBDII:
213 printf("keyboard II (Japanese layout)\n");
214 break;
215 case ADB_PBEXTKBD:
216 printf("PowerBook extended keyboard\n");
217 break;
218 case ADB_DESIGNKBD:
219 printf("extended keyboard\n");
220 #ifdef notyet
221 blinkleds(sc);
222 #endif
223 break;
224 case ADB_PBJPKBD:
225 printf("PowerBook keyboard (Japanese layout)\n");
226 break;
227 case ADB_PBG3KBD:
228 printf("PowerBook G3 keyboard\n");
229 break;
230 case ADB_PBG3JPKBD:
231 printf("PowerBook G3 keyboard (Japanese layout)\n");
232 break;
233 default:
234 printf("mapped device (%d)\n", sc->handler_id);
235 break;
237 error = SetADBInfo(&adbinfo, sc->adbaddr);
238 #ifdef ADB_DEBUG
239 if (adb_debug)
240 printf("akbd: returned %d from SetADBInfo\n", error);
241 #endif
243 if (akbd_is_console && !akbd_console_attached) {
244 wskbd_cnattach(&akbd_consops, sc, &akbd_keymapdata);
245 akbd_console_attached = 1;
248 a.console = akbd_is_console;
249 a.keymap = &akbd_keymapdata;
250 a.accessops = &akbd_accessops;
251 a.accesscookie = sc;
253 sc->sc_wskbddev = config_found(self, &a, wskbddevprint);
258 * Handle putting the keyboard data received from the ADB into
259 * an ADB event record.
261 void
262 kbd_adbcomplete(uint8_t *buffer, uint8_t *data_area, int adb_command)
264 adb_event_t event;
265 struct akbd_softc *ksc;
266 int adbaddr;
267 #ifdef ADB_DEBUG
268 int i;
270 if (adb_debug)
271 printf("adb: transaction completion\n");
272 #endif
274 adbaddr = ADB_CMDADDR(adb_command);
275 ksc = (struct akbd_softc *)data_area;
277 event.addr = adbaddr;
278 event.hand_id = ksc->handler_id;
279 event.def_addr = ksc->origaddr;
280 event.byte_count = buffer[0];
281 memcpy(event.bytes, buffer + 1, event.byte_count);
283 #ifdef ADB_DEBUG
284 if (adb_debug) {
285 printf("akbd: from %d at %d (org %d) %d:", event.addr,
286 event.hand_id, event.def_addr, buffer[0]);
287 for (i = 1; i <= buffer[0]; i++)
288 printf(" %x", buffer[i]);
289 printf("\n");
291 #endif
293 microtime(&event.timestamp);
295 kbd_processevent(&event, ksc);
299 * Given a keyboard ADB event, record the keycodes and call the key
300 * repeat handler, optionally passing the event through the mouse
301 * button emulation handler first.
303 static void
304 kbd_processevent(adb_event_t *event, struct akbd_softc *ksc)
306 adb_event_t new_event;
308 new_event = *event;
309 new_event.u.k.key = event->bytes[0];
310 new_event.bytes[1] = 0xff;
311 kbd_intr(&new_event);
312 #if NAED > 0
313 aed_input(&new_event);
314 #endif
315 if (event->bytes[1] != 0xff) {
316 new_event.u.k.key = event->bytes[1];
317 new_event.bytes[0] = event->bytes[1];
318 new_event.bytes[1] = 0xff;
319 kbd_intr(&new_event);
320 #if NAED > 0
321 aed_input(&new_event);
322 #endif
327 #ifdef notyet
329 * Get the actual hardware LED state and convert it to softc format.
331 static u_char
332 getleds(int addr)
334 short cmd;
335 u_char buffer[9], leds;
337 leds = 0x00; /* all off */
338 buffer[0] = 0;
340 /* talk R2 */
341 cmd = ADBTALK(addr, 2);
342 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
343 buffer[0] > 0)
344 leds = ~(buffer[2]) & 0x07;
346 return (leds);
350 * Set the keyboard LED's.
352 * Automatically translates from ioctl/softc format to the
353 * actual keyboard register format
355 static int
356 setleds(struct akbd_softc *ksc, u_char leds)
358 int addr;
359 short cmd;
360 u_char buffer[9];
362 if ((leds & 0x07) == (ksc->sc_leds & 0x07))
363 return (0);
365 addr = ksc->adbaddr;
366 buffer[0] = 0;
368 cmd = ADBTALK(addr, 2);
369 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
370 return (EIO);
372 leds = ~leds & 0x07;
373 buffer[2] &= 0xf8;
374 buffer[2] |= leds;
376 cmd = ADBLISTEN(addr, 2);
377 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
379 cmd = ADBTALK(addr, 2);
380 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
381 return (EIO);
383 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
385 if ((buffer[2] & 0xf8) != leds)
386 return (EIO);
387 else
388 return (0);
392 * Toggle all of the LED's on and off, just for show.
394 static void
395 blinkleds(struct akbd_softc *ksc)
397 int addr, i;
398 u_char blinkleds, origleds;
400 addr = ksc->adbaddr;
401 origleds = getleds(addr);
402 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
404 (void)setleds(ksc, blinkleds);
406 for (i = 0; i < 10000; i++)
407 delay(50);
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));
415 return;
417 #endif
420 akbd_enable(void *v, int on)
422 return 0;
425 void
426 akbd_set_leds(void *v, int on)
431 akbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
433 #ifdef WSDISPLAY_COMPAT_RAWKBD
434 struct akbd_softc *sc = (struct akbd_softc *) v;
435 #endif
437 switch (cmd) {
439 case WSKBDIO_GTYPE:
440 *(int *)data = WSKBD_TYPE_ADB;
441 return 0;
442 case WSKBDIO_SETLEDS:
443 return 0;
444 case WSKBDIO_GETLEDS:
445 *(int *)data = 0;
446 return 0;
447 #ifdef WSDISPLAY_COMPAT_RAWKBD
448 case WSKBDIO_SETMODE:
449 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
450 return 0;
451 #endif
453 /* kbdioctl(...); */
455 return EPASSTHROUGH;
458 extern int adb_polling;
460 void
461 kbd_passup(struct akbd_softc *sc,int key)
463 if (sc->sc_polling) {
464 if (sc->sc_npolledkeys <
465 (sizeof(sc->sc_polledkeys)/sizeof(unsigned char))) {
466 sc->sc_polledkeys[sc->sc_npolledkeys++] = key;
468 #ifdef ADB_DEBUG
469 else {
470 printf("akbd: dumping polled key 0x%02x\n",key);
472 #endif
473 #ifdef WSDISPLAY_COMPAT_RAWKBD
474 } else if (sc->sc_rawkbd) {
475 char cbuf[2];
476 int s;
477 int j = 0;
478 int c = keyboard[ADBK_KEYVAL(key)][3];
480 if (c == 0) /* XXX */
481 return;
483 if (c & 0x80)
484 cbuf[j++] = 0xe0;
486 cbuf[j++] = (c & 0x7f) | (ADBK_PRESS(key)? 0 : 0x80);
488 s = spltty();
489 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
490 splx(s);
491 #endif
492 } else {
493 int press, val;
494 int type;
496 press = ADBK_PRESS(key);
497 val = ADBK_KEYVAL(key);
499 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
501 wskbd_input(sc->sc_wskbddev, type, val);
506 kbd_intr(void *arg)
508 adb_event_t *event = arg;
509 int key;
510 #ifdef CAPS_IS_CONTROL
511 static int shift;
512 #endif
514 struct akbd_softc *sc = device_lookup_private(&akbd_cd, 0);
516 key = event->u.k.key;
518 #ifdef CAPS_IS_CONTROL
520 * Caps lock is weird. The key sequence generated is:
521 * press: down(57) [57] (LED turns on)
522 * release: up(127) [255]
523 * press: up(127) [255]
524 * release: up(57) [185] (LED turns off)
526 if ((key == 57) || (key == 185))
527 shift = 0;
529 if (key == 255) {
530 if (shift == 0) {
531 key = 185;
532 shift = 1;
533 } else {
534 key = 57;
535 shift = 0;
538 #endif
540 switch (key) {
541 #ifndef CAPS_IS_CONTROL
542 case 57: /* Caps Lock pressed */
543 case 185: /* Caps Lock released */
544 key = ADBK_KEYDOWN(ADBK_KEYVAL(key));
545 kbd_passup(sc,key);
546 key = ADBK_KEYUP(ADBK_KEYVAL(key));
547 break;
548 #endif
549 case 245:
550 if (pcmcia_soft_eject)
551 pm_eject_pcmcia(0);
552 break;
553 case 244:
554 if (pcmcia_soft_eject)
555 pm_eject_pcmcia(1);
556 break;
559 kbd_passup(sc,key);
561 return 0;
565 akbd_cnattach(void)
568 akbd_is_console = 1;
569 return 0;
572 void
573 akbd_cngetc(void *v, u_int *type, int *data)
575 int key, press, val;
576 int s;
577 struct akbd_softc *sc = v;
579 s = splhigh();
581 KASSERT(sc->sc_polling);
582 KASSERT(adb_polling);
584 while (sc->sc_npolledkeys == 0) {
585 adb_intr(NULL);
586 DELAY(10000); /* XXX */
589 splx(s);
591 key = sc->sc_polledkeys[0];
592 sc->sc_npolledkeys--;
593 memmove(sc->sc_polledkeys,sc->sc_polledkeys+1,
594 sc->sc_npolledkeys * sizeof(unsigned char));
596 press = ADBK_PRESS(key);
597 val = ADBK_KEYVAL(key);
599 *data = val;
600 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
603 void
604 akbd_cnpollc(void *v, int on)
606 struct akbd_softc *sc = v;
607 sc->sc_polling = on;
608 if (!on) {
609 int i;
610 for(i=0;i<sc->sc_npolledkeys;i++) {
611 kbd_passup(sc,sc->sc_polledkeys[i]);
613 sc->sc_npolledkeys = 0;
615 adb_polling = on;