akbd.c revision 1.41 1 /* $NetBSD: akbd.c,v 1.41 2009/03/14 15:36:09 dsl 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/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: akbd.c,v 1.41 2009/03/14 15:36:09 dsl Exp $");
35
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>
44
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>
50
51 #include <dev/adb/adb_keymap.h>
52
53 #include <machine/autoconf.h>
54 #define KEYBOARD_ARRAY
55 #include <machine/keyboard.h>
56
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>
61
62 #include "aed.h"
63
64 /*
65 * Function declarations.
66 */
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
75
76 /* Driver definition. */
77 CFATTACH_DECL(akbd, sizeof(struct akbd_softc),
78 akbdmatch, akbdattach, NULL, NULL);
79
80 extern struct cfdriver akbd_cd;
81
82 int akbd_enable(void *, int);
83 void akbd_set_leds(void *, int);
84 int akbd_ioctl(void *, u_long, void *, int, struct lwp *);
85
86 struct wskbd_accessops akbd_accessops = {
87 akbd_enable,
88 akbd_set_leds,
89 akbd_ioctl,
90 };
91
92 void akbd_cngetc(void *, u_int *, int *);
93 void akbd_cnpollc(void *, int);
94
95 struct wskbd_consops akbd_consops = {
96 akbd_cngetc,
97 akbd_cnpollc,
98 };
99
100 struct wskbd_mapdata akbd_keymapdata = {
101 akbd_keydesctab,
102 #ifdef AKBD_LAYOUT
103 AKBD_LAYOUT,
104 #else
105 KB_US,
106 #endif
107 };
108
109 static int akbd_is_console;
110 static int akbd_console_attached;
111 static int pcmcia_soft_eject;
112
113 static int
114 akbdmatch(struct device *parent, struct cfdata *cf, void *aux)
115 {
116 struct adb_attach_args *aa_args = aux;
117
118 if (aa_args->origaddr == ADBADDR_KBD)
119 return 1;
120 else
121 return 0;
122 }
123
124 static void
125 akbdattach(parent, self, aux)
126 struct device *parent, *self;
127 void *aux;
128 {
129 ADBSetInfoBlock adbinfo;
130 struct akbd_softc *sc = (struct akbd_softc *)self;
131 struct adb_attach_args *aa_args = aux;
132 int error, kbd_done;
133 short cmd;
134 u_char buffer[9];
135 struct wskbddev_attach_args a;
136
137 /* ohare based models have soft ejectable card slot. */
138 if (OF_finddevice("/bandit/ohare") != -1)
139 pcmcia_soft_eject = 1;
140
141 sc->origaddr = aa_args->origaddr;
142 sc->adbaddr = aa_args->adbaddr;
143 sc->handler_id = aa_args->handler_id;
144
145 sc->sc_leds = (u_int8_t)0x00; /* initially off */
146
147 adbinfo.siServiceRtPtr = (Ptr)kbd_adbcomplete;
148 adbinfo.siDataAreaAddr = (void *)sc;
149
150 switch (sc->handler_id) {
151 case ADB_STDKBD:
152 printf("standard keyboard\n");
153 break;
154 case ADB_ISOKBD:
155 printf("standard keyboard (ISO layout)\n");
156 break;
157 case ADB_EXTKBD:
158 cmd = ADBTALK(sc->adbaddr, 1);
159 kbd_done =
160 (adb_op_sync((Ptr)buffer, NULL, (Ptr)0, cmd) == 0);
161
162 /* Ignore Logitech MouseMan/Trackman pseudo keyboard */
163 if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) {
164 printf("Mouseman (non-EMP) pseudo keyboard\n");
165 adbinfo.siServiceRtPtr = (Ptr)0;
166 adbinfo.siDataAreaAddr = (Ptr)0;
167 } else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) {
168 printf("Trackman (non-EMP) pseudo keyboard\n");
169 adbinfo.siServiceRtPtr = (Ptr)0;
170 adbinfo.siDataAreaAddr = (Ptr)0;
171 } else {
172 printf("extended keyboard\n");
173 #ifdef notyet
174 blinkleds(sc);
175 #endif
176 }
177 break;
178 case ADB_EXTISOKBD:
179 printf("extended keyboard (ISO layout)\n");
180 #ifdef notyet
181 blinkleds(sc);
182 #endif
183 break;
184 case ADB_KBDII:
185 printf("keyboard II\n");
186 break;
187 case ADB_ISOKBDII:
188 printf("keyboard II (ISO layout)\n");
189 break;
190 case ADB_PBKBD:
191 printf("PowerBook keyboard\n");
192 break;
193 case ADB_PBISOKBD:
194 printf("PowerBook keyboard (ISO layout)\n");
195 break;
196 case ADB_ADJKPD:
197 printf("adjustable keypad\n");
198 break;
199 case ADB_ADJKBD:
200 printf("adjustable keyboard\n");
201 break;
202 case ADB_ADJISOKBD:
203 printf("adjustable keyboard (ISO layout)\n");
204 break;
205 case ADB_ADJJAPKBD:
206 printf("adjustable keyboard (Japanese layout)\n");
207 break;
208 case ADB_PBEXTISOKBD:
209 printf("PowerBook extended keyboard (ISO layout)\n");
210 break;
211 case ADB_PBEXTJAPKBD:
212 printf("PowerBook extended keyboard (Japanese layout)\n");
213 break;
214 case ADB_JPKBDII:
215 printf("keyboard II (Japanese layout)\n");
216 break;
217 case ADB_PBEXTKBD:
218 printf("PowerBook extended keyboard\n");
219 break;
220 case ADB_DESIGNKBD:
221 printf("extended keyboard\n");
222 #ifdef notyet
223 blinkleds(sc);
224 #endif
225 break;
226 case ADB_PBJPKBD:
227 printf("PowerBook keyboard (Japanese layout)\n");
228 break;
229 case ADB_PBG3KBD:
230 printf("PowerBook G3 keyboard\n");
231 break;
232 case ADB_PBG3JPKBD:
233 printf("PowerBook G3 keyboard (Japanese layout)\n");
234 break;
235 default:
236 printf("mapped device (%d)\n", sc->handler_id);
237 break;
238 }
239 error = SetADBInfo(&adbinfo, sc->adbaddr);
240 #ifdef ADB_DEBUG
241 if (adb_debug)
242 printf("akbd: returned %d from SetADBInfo\n", error);
243 #endif
244
245 if (akbd_is_console && !akbd_console_attached) {
246 wskbd_cnattach(&akbd_consops, sc, &akbd_keymapdata);
247 akbd_console_attached = 1;
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(uint8_t *buffer, uint8_t *data_area, 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_CMDADDR(adb_command);
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("akbd: 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(adb_event_t *event, struct akbd_softc *ksc)
307 {
308 adb_event_t new_event;
309
310 new_event = *event;
311 new_event.u.k.key = event->bytes[0];
312 new_event.bytes[1] = 0xff;
313 kbd_intr(&new_event);
314 #if NAED > 0
315 aed_input(&new_event);
316 #endif
317 if (event->bytes[1] != 0xff) {
318 new_event.u.k.key = event->bytes[1];
319 new_event.bytes[0] = event->bytes[1];
320 new_event.bytes[1] = 0xff;
321 kbd_intr(&new_event);
322 #if NAED > 0
323 aed_input(&new_event);
324 #endif
325 }
326
327 }
328
329 #ifdef notyet
330 /*
331 * Get the actual hardware LED state and convert it to softc format.
332 */
333 static u_char
334 getleds(int addr)
335 {
336 short cmd;
337 u_char buffer[9], leds;
338
339 leds = 0x00; /* all off */
340 buffer[0] = 0;
341
342 /* talk R2 */
343 cmd = ADBTALK(addr, 2);
344 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) == 0 &&
345 buffer[0] > 0)
346 leds = ~(buffer[2]) & 0x07;
347
348 return (leds);
349 }
350
351 /*
352 * Set the keyboard LED's.
353 *
354 * Automatically translates from ioctl/softc format to the
355 * actual keyboard register format
356 */
357 static int
358 setleds(struct akbd_softc *ksc, u_char leds)
359 {
360 int addr;
361 short cmd;
362 u_char buffer[9];
363
364 if ((leds & 0x07) == (ksc->sc_leds & 0x07))
365 return (0);
366
367 addr = ksc->adbaddr;
368 buffer[0] = 0;
369
370 cmd = ADBTALK(addr, 2);
371 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
372 return (EIO);
373
374 leds = ~leds & 0x07;
375 buffer[2] &= 0xf8;
376 buffer[2] |= leds;
377
378 cmd = ADBLISTEN(addr, 2);
379 adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd);
380
381 cmd = ADBTALK(addr, 2);
382 if (adb_op_sync((Ptr)buffer, (Ptr)0, (Ptr)0, cmd) || buffer[0] == 0)
383 return (EIO);
384
385 ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
386
387 if ((buffer[2] & 0xf8) != leds)
388 return (EIO);
389 else
390 return (0);
391 }
392
393 /*
394 * Toggle all of the LED's on and off, just for show.
395 */
396 static void
397 blinkleds(struct akbd_softc *ksc)
398 {
399 int addr, i;
400 u_char blinkleds, origleds;
401
402 addr = ksc->adbaddr;
403 origleds = getleds(addr);
404 blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
405
406 (void)setleds(ksc, blinkleds);
407
408 for (i = 0; i < 10000; i++)
409 delay(50);
410
411 /* make sure that we restore the LED settings */
412 i = 10;
413 do {
414 (void)setleds(ksc, (u_char)0x00);
415 } while (setleds(ksc, (u_char)0x00) && (i-- > 0));
416
417 return;
418 }
419 #endif
420
421 int
422 akbd_enable(void *v, int on)
423 {
424 return 0;
425 }
426
427 void
428 akbd_set_leds(void *v, int on)
429 {
430 }
431
432 int
433 akbd_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
434 {
435 #ifdef WSDISPLAY_COMPAT_RAWKBD
436 struct akbd_softc *sc = (struct akbd_softc *) v;
437 #endif
438
439 switch (cmd) {
440
441 case WSKBDIO_GTYPE:
442 *(int *)data = WSKBD_TYPE_ADB;
443 return 0;
444 case WSKBDIO_SETLEDS:
445 return 0;
446 case WSKBDIO_GETLEDS:
447 *(int *)data = 0;
448 return 0;
449 #ifdef WSDISPLAY_COMPAT_RAWKBD
450 case WSKBDIO_SETMODE:
451 sc->sc_rawkbd = *(int *)data == WSKBD_RAW;
452 return 0;
453 #endif
454 }
455 /* kbdioctl(...); */
456
457 return EPASSTHROUGH;
458 }
459
460 extern int adb_polling;
461
462 void
463 kbd_passup(struct akbd_softc *sc,int key)
464 {
465 if (sc->sc_polling) {
466 if (sc->sc_npolledkeys <
467 (sizeof(sc->sc_polledkeys)/sizeof(unsigned char))) {
468 sc->sc_polledkeys[sc->sc_npolledkeys++] = key;
469 }
470 #ifdef ADB_DEBUG
471 else {
472 printf("akbd: dumping polled key 0x%02x\n",key);
473 }
474 #endif
475 #ifdef WSDISPLAY_COMPAT_RAWKBD
476 } else if (sc->sc_rawkbd) {
477 char cbuf[2];
478 int s;
479 int j = 0;
480 int c = keyboard[ADBK_KEYVAL(key)][3];
481
482 if (c == 0) /* XXX */
483 return;
484
485 if (c & 0x80)
486 cbuf[j++] = 0xe0;
487
488 cbuf[j++] = (c & 0x7f) | (ADBK_PRESS(key)? 0 : 0x80);
489
490 s = spltty();
491 wskbd_rawinput(sc->sc_wskbddev, cbuf, j);
492 splx(s);
493 #endif
494 } else {
495 int press, val;
496 int type;
497
498 press = ADBK_PRESS(key);
499 val = ADBK_KEYVAL(key);
500
501 type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
502
503 wskbd_input(sc->sc_wskbddev, type, val);
504 }
505 }
506
507 int
508 kbd_intr(void *arg)
509 {
510 adb_event_t *event = arg;
511 int key;
512 #ifdef CAPS_IS_CONTROL
513 static int shift;
514 #endif
515
516 struct akbd_softc *sc = device_lookup_private(&akbd_cd, 0);
517
518 key = event->u.k.key;
519
520 #ifdef CAPS_IS_CONTROL
521 /*
522 * Caps lock is weird. The key sequence generated is:
523 * press: down(57) [57] (LED turns on)
524 * release: up(127) [255]
525 * press: up(127) [255]
526 * release: up(57) [185] (LED turns off)
527 */
528 if ((key == 57) || (key == 185))
529 shift = 0;
530
531 if (key == 255) {
532 if (shift == 0) {
533 key = 185;
534 shift = 1;
535 } else {
536 key = 57;
537 shift = 0;
538 }
539 }
540 #endif
541
542 switch (key) {
543 #ifndef CAPS_IS_CONTROL
544 case 57: /* Caps Lock pressed */
545 case 185: /* Caps Lock released */
546 key = ADBK_KEYDOWN(ADBK_KEYVAL(key));
547 kbd_passup(sc,key);
548 key = ADBK_KEYUP(ADBK_KEYVAL(key));
549 break;
550 #endif
551 case 245:
552 if (pcmcia_soft_eject)
553 pm_eject_pcmcia(0);
554 break;
555 case 244:
556 if (pcmcia_soft_eject)
557 pm_eject_pcmcia(1);
558 break;
559 }
560
561 kbd_passup(sc,key);
562
563 return 0;
564 }
565
566 int
567 akbd_cnattach()
568 {
569
570 akbd_is_console = 1;
571 return 0;
572 }
573
574 void
575 akbd_cngetc(void *v, u_int *type, int *data)
576 {
577 int key, press, val;
578 int s;
579 struct akbd_softc *sc = v;
580
581 s = splhigh();
582
583 KASSERT(sc->sc_polling);
584 KASSERT(adb_polling);
585
586 while (sc->sc_npolledkeys == 0) {
587 adb_intr(NULL);
588 DELAY(10000); /* XXX */
589 }
590
591 splx(s);
592
593 key = sc->sc_polledkeys[0];
594 sc->sc_npolledkeys--;
595 memmove(sc->sc_polledkeys,sc->sc_polledkeys+1,
596 sc->sc_npolledkeys * sizeof(unsigned char));
597
598 press = ADBK_PRESS(key);
599 val = ADBK_KEYVAL(key);
600
601 *data = val;
602 *type = press ? WSCONS_EVENT_KEY_DOWN : WSCONS_EVENT_KEY_UP;
603 }
604
605 void
606 akbd_cnpollc(void *v, int on)
607 {
608 struct akbd_softc *sc = v;
609 sc->sc_polling = on;
610 if (!on) {
611 int i;
612 for(i=0;i<sc->sc_npolledkeys;i++) {
613 kbd_passup(sc,sc->sc_polledkeys[i]);
614 }
615 sc->sc_npolledkeys = 0;
616 }
617 adb_polling = on;
618 }
619