akbd.c revision 1.1 1 1.1 tsubai /* $NetBSD: akbd.c,v 1.1 1998/10/13 11:21:21 tsubai Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (C) 1998 Colin Wood
5 1.1 tsubai * All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * Redistribution and use in source and binary forms, with or without
8 1.1 tsubai * modification, are permitted provided that the following conditions
9 1.1 tsubai * are met:
10 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
11 1.1 tsubai * notice, this list of conditions and the following disclaimer.
12 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
14 1.1 tsubai * documentation and/or other materials provided with the distribution.
15 1.1 tsubai * 3. All advertising materials mentioning features or use of this software
16 1.1 tsubai * must display the following acknowledgement:
17 1.1 tsubai * This product includes software developed by Colin Wood.
18 1.1 tsubai * 4. The name of the author may not be used to endorse or promote products
19 1.1 tsubai * derived from this software without specific prior written permission.
20 1.1 tsubai *
21 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 tsubai */
32 1.1 tsubai
33 1.1 tsubai #include <sys/param.h>
34 1.1 tsubai #include <sys/device.h>
35 1.1 tsubai #include <sys/fcntl.h>
36 1.1 tsubai #include <sys/poll.h>
37 1.1 tsubai #include <sys/select.h>
38 1.1 tsubai #include <sys/proc.h>
39 1.1 tsubai #include <sys/signalvar.h>
40 1.1 tsubai #include <sys/systm.h>
41 1.1 tsubai
42 1.1 tsubai #include <machine/autoconf.h>
43 1.1 tsubai
44 1.1 tsubai #include <macppc/dev/adbvar.h>
45 1.1 tsubai #include <macppc/dev/aedvar.h>
46 1.1 tsubai #include <macppc/dev/akbdvar.h>
47 1.1 tsubai #include <macppc/dev/amsvar.h>
48 1.1 tsubai
49 1.1 tsubai /*
50 1.1 tsubai * Function declarations.
51 1.1 tsubai */
52 1.1 tsubai static int kbdmatch __P((struct device *, struct cfdata *, void *));
53 1.1 tsubai static void kbdattach __P((struct device *, struct device *, void *));
54 1.1 tsubai void kbd_adbcomplete __P((caddr_t buffer, caddr_t data_area, int adb_command));
55 1.1 tsubai static void kbd_processevent __P((adb_event_t *event, struct kbd_softc *));
56 1.1 tsubai #ifdef notyet
57 1.1 tsubai static u_char getleds __P((int));
58 1.1 tsubai static int setleds __P((struct kbd_softc *, u_char));
59 1.1 tsubai static void blinkleds __P((struct kbd_softc *));
60 1.1 tsubai #endif
61 1.1 tsubai
62 1.1 tsubai /*
63 1.1 tsubai * Local variables.
64 1.1 tsubai */
65 1.1 tsubai static volatile int kbd_done; /* Did ADBOp() complete? */
66 1.1 tsubai
67 1.1 tsubai /* Driver definition. */
68 1.1 tsubai struct cfattach akbd_ca = {
69 1.1 tsubai sizeof(struct kbd_softc), kbdmatch, kbdattach
70 1.1 tsubai };
71 1.1 tsubai
72 1.1 tsubai extern struct cfdriver kbd_cd;
73 1.1 tsubai
74 1.1 tsubai static int
75 1.1 tsubai kbdmatch(parent, cf, aux)
76 1.1 tsubai struct device *parent;
77 1.1 tsubai struct cfdata *cf;
78 1.1 tsubai void *aux;
79 1.1 tsubai {
80 1.1 tsubai struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
81 1.1 tsubai
82 1.1 tsubai if (aa_args->origaddr == ADBADDR_KBD)
83 1.1 tsubai return 1;
84 1.1 tsubai else
85 1.1 tsubai return 0;
86 1.1 tsubai }
87 1.1 tsubai
88 1.1 tsubai static void
89 1.1 tsubai kbdattach(parent, self, aux)
90 1.1 tsubai struct device *parent, *self;
91 1.1 tsubai void *aux;
92 1.1 tsubai {
93 1.1 tsubai ADBSetInfoBlock adbinfo;
94 1.1 tsubai struct kbd_softc *sc = (struct kbd_softc *)self;
95 1.1 tsubai struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
96 1.1 tsubai int count, error;
97 1.1 tsubai short cmd;
98 1.1 tsubai u_char buffer[9];
99 1.1 tsubai
100 1.1 tsubai sc->origaddr = aa_args->origaddr;
101 1.1 tsubai sc->adbaddr = aa_args->adbaddr;
102 1.1 tsubai sc->handler_id = aa_args->handler_id;
103 1.1 tsubai
104 1.1 tsubai sc->sc_leds = (u_int8_t)0x00; /* initially off */
105 1.1 tsubai
106 1.1 tsubai adbinfo.siServiceRtPtr = (Ptr)kbd_adbcomplete;
107 1.1 tsubai adbinfo.siDataAreaAddr = (caddr_t)sc;
108 1.1 tsubai
109 1.1 tsubai switch (sc->handler_id) {
110 1.1 tsubai case ADB_STDKBD:
111 1.1 tsubai printf("standard keyboard\n");
112 1.1 tsubai break;
113 1.1 tsubai case ADB_ISOKBD:
114 1.1 tsubai printf("standard keyboard (ISO layout)\n");
115 1.1 tsubai break;
116 1.1 tsubai case ADB_EXTKBD:
117 1.1 tsubai kbd_done = 0;
118 1.1 tsubai cmd = (((sc->adbaddr << 4) & 0xf0) | 0x0d ); /* talk R1 */
119 1.1 tsubai ADBOp((Ptr)buffer, (Ptr)extdms_complete,
120 1.1 tsubai (Ptr)&kbd_done, cmd);
121 1.1 tsubai
122 1.1 tsubai /* Wait until done, but no more than 2 secs */
123 1.1 tsubai count = 40000;
124 1.1 tsubai while (!kbd_done && count-- > 0)
125 1.1 tsubai delay(50);
126 1.1 tsubai
127 1.1 tsubai /* Ignore Logitech MouseMan/Trackman pseudo keyboard */
128 1.1 tsubai if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x20) {
129 1.1 tsubai printf("Mouseman (non-EMP) pseudo keyboard\n");
130 1.1 tsubai adbinfo.siServiceRtPtr = (Ptr)0;
131 1.1 tsubai adbinfo.siDataAreaAddr = (Ptr)0;
132 1.1 tsubai } else if (kbd_done && buffer[1] == 0x9a && buffer[2] == 0x21) {
133 1.1 tsubai printf("Trackman (non-EMP) pseudo keyboard\n");
134 1.1 tsubai adbinfo.siServiceRtPtr = (Ptr)0;
135 1.1 tsubai adbinfo.siDataAreaAddr = (Ptr)0;
136 1.1 tsubai } else {
137 1.1 tsubai printf("extended keyboard\n");
138 1.1 tsubai #ifdef notyet
139 1.1 tsubai blinkleds(sc);
140 1.1 tsubai #endif
141 1.1 tsubai }
142 1.1 tsubai break;
143 1.1 tsubai case ADB_EXTISOKBD:
144 1.1 tsubai printf("extended keyboard (ISO layout)\n");
145 1.1 tsubai #ifdef notyet
146 1.1 tsubai blinkleds(sc);
147 1.1 tsubai #endif
148 1.1 tsubai break;
149 1.1 tsubai case ADB_KBDII:
150 1.1 tsubai printf("keyboard II\n");
151 1.1 tsubai break;
152 1.1 tsubai case ADB_ISOKBDII:
153 1.1 tsubai printf("keyboard II (ISO layout)\n");
154 1.1 tsubai break;
155 1.1 tsubai case ADB_PBKBD:
156 1.1 tsubai printf("PowerBook keyboard\n");
157 1.1 tsubai break;
158 1.1 tsubai case ADB_PBISOKBD:
159 1.1 tsubai printf("PowerBook keyboard (ISO layout)\n");
160 1.1 tsubai break;
161 1.1 tsubai case ADB_ADJKPD:
162 1.1 tsubai printf("adjustable keypad\n");
163 1.1 tsubai break;
164 1.1 tsubai case ADB_ADJKBD:
165 1.1 tsubai printf("adjustable keyboard\n");
166 1.1 tsubai break;
167 1.1 tsubai case ADB_ADJISOKBD:
168 1.1 tsubai printf("adjustable keyboard (ISO layout)\n");
169 1.1 tsubai break;
170 1.1 tsubai case ADB_ADJJAPKBD:
171 1.1 tsubai printf("adjustable keyboard (Japanese layout)\n");
172 1.1 tsubai break;
173 1.1 tsubai case ADB_PBEXTISOKBD:
174 1.1 tsubai printf("PowerBook extended keyboard (ISO layout)\n");
175 1.1 tsubai break;
176 1.1 tsubai case ADB_PBEXTJAPKBD:
177 1.1 tsubai printf("PowerBook extended keyboard (Japanese layout)\n");
178 1.1 tsubai break;
179 1.1 tsubai case ADB_JISKBDII:
180 1.1 tsubai printf("keyboard II (Japanese layout)\n");
181 1.1 tsubai break;
182 1.1 tsubai case ADB_PBEXTKBD:
183 1.1 tsubai printf("PowerBook extended keyboard\n");
184 1.1 tsubai break;
185 1.1 tsubai case ADB_DESIGNKBD:
186 1.1 tsubai printf("extended keyboard\n");
187 1.1 tsubai #ifdef notyet
188 1.1 tsubai blinkleds(sc);
189 1.1 tsubai #endif
190 1.1 tsubai break;
191 1.1 tsubai default:
192 1.1 tsubai printf("mapped device (%d)\n", sc->handler_id);
193 1.1 tsubai break;
194 1.1 tsubai }
195 1.1 tsubai error = SetADBInfo(&adbinfo, sc->adbaddr);
196 1.1 tsubai #ifdef ADB_DEBUG
197 1.1 tsubai if (adb_debug)
198 1.1 tsubai printf("kbd: returned %d from SetADBInfo\n", error);
199 1.1 tsubai #endif
200 1.1 tsubai return;
201 1.1 tsubai }
202 1.1 tsubai
203 1.1 tsubai
204 1.1 tsubai /*
205 1.1 tsubai * Handle putting the keyboard data received from the ADB into
206 1.1 tsubai * an ADB event record.
207 1.1 tsubai */
208 1.1 tsubai void
209 1.1 tsubai kbd_adbcomplete(buffer, data_area, adb_command)
210 1.1 tsubai caddr_t buffer;
211 1.1 tsubai caddr_t data_area;
212 1.1 tsubai int adb_command;
213 1.1 tsubai {
214 1.1 tsubai adb_event_t event;
215 1.1 tsubai struct kbd_softc *ksc;
216 1.1 tsubai int adbaddr;
217 1.1 tsubai #ifdef ADB_DEBUG
218 1.1 tsubai int i;
219 1.1 tsubai
220 1.1 tsubai if (adb_debug)
221 1.1 tsubai printf("adb: transaction completion\n");
222 1.1 tsubai #endif
223 1.1 tsubai
224 1.1 tsubai adbaddr = (adb_command & 0xf0) >> 4;
225 1.1 tsubai ksc = (struct kbd_softc *)data_area;
226 1.1 tsubai
227 1.1 tsubai event.addr = adbaddr;
228 1.1 tsubai event.hand_id = ksc->handler_id;
229 1.1 tsubai event.def_addr = ksc->origaddr;
230 1.1 tsubai event.byte_count = buffer[0];
231 1.1 tsubai memcpy(event.bytes, buffer + 1, event.byte_count);
232 1.1 tsubai
233 1.1 tsubai #ifdef ADB_DEBUG
234 1.1 tsubai if (adb_debug) {
235 1.1 tsubai printf("kbd: from %d at %d (org %d) %d:", event.addr,
236 1.1 tsubai event.hand_id, event.def_addr, buffer[0]);
237 1.1 tsubai for (i = 1; i <= buffer[0]; i++)
238 1.1 tsubai printf(" %x", buffer[i]);
239 1.1 tsubai printf("\n");
240 1.1 tsubai }
241 1.1 tsubai #endif
242 1.1 tsubai
243 1.1 tsubai microtime(&event.timestamp);
244 1.1 tsubai
245 1.1 tsubai kbd_processevent(&event, ksc);
246 1.1 tsubai }
247 1.1 tsubai
248 1.1 tsubai /*
249 1.1 tsubai * Given a keyboard ADB event, record the keycodes and call the key
250 1.1 tsubai * repeat handler, optionally passing the event through the mouse
251 1.1 tsubai * button emulation handler first.
252 1.1 tsubai */
253 1.1 tsubai static void
254 1.1 tsubai kbd_processevent(event, ksc)
255 1.1 tsubai adb_event_t *event;
256 1.1 tsubai struct kbd_softc *ksc;
257 1.1 tsubai {
258 1.1 tsubai adb_event_t new_event;
259 1.1 tsubai
260 1.1 tsubai new_event = *event;
261 1.1 tsubai new_event.u.k.key = event->bytes[0];
262 1.1 tsubai new_event.bytes[1] = 0xff;
263 1.1 tsubai aed_input(&new_event);
264 1.1 tsubai if (event->bytes[1] != 0xff) {
265 1.1 tsubai new_event.u.k.key = event->bytes[1];
266 1.1 tsubai new_event.bytes[0] = event->bytes[1];
267 1.1 tsubai new_event.bytes[1] = 0xff;
268 1.1 tsubai aed_input(&new_event);
269 1.1 tsubai }
270 1.1 tsubai
271 1.1 tsubai }
272 1.1 tsubai
273 1.1 tsubai #ifdef notyet
274 1.1 tsubai /*
275 1.1 tsubai * Get the actual hardware LED state and convert it to softc format.
276 1.1 tsubai */
277 1.1 tsubai static u_char
278 1.1 tsubai getleds(addr)
279 1.1 tsubai int addr;
280 1.1 tsubai {
281 1.1 tsubai short cmd;
282 1.1 tsubai u_char buffer[9], leds;
283 1.1 tsubai
284 1.1 tsubai leds = 0x00; /* all off */
285 1.1 tsubai buffer[0] = 0;
286 1.1 tsubai kbd_done = 0;
287 1.1 tsubai
288 1.1 tsubai /* talk R2 */
289 1.1 tsubai cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
290 1.1 tsubai ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
291 1.1 tsubai while (!kbd_done)
292 1.1 tsubai /* busy-wait until done */ ;
293 1.1 tsubai
294 1.1 tsubai if (buffer[0] > 0)
295 1.1 tsubai leds = ~(buffer[2]) & 0x07;
296 1.1 tsubai
297 1.1 tsubai return (leds);
298 1.1 tsubai }
299 1.1 tsubai
300 1.1 tsubai /*
301 1.1 tsubai * Set the keyboard LED's.
302 1.1 tsubai *
303 1.1 tsubai * Automatically translates from ioctl/softc format to the
304 1.1 tsubai * actual keyboard register format
305 1.1 tsubai */
306 1.1 tsubai static int
307 1.1 tsubai setleds(ksc, leds)
308 1.1 tsubai struct kbd_softc *ksc;
309 1.1 tsubai u_char leds;
310 1.1 tsubai {
311 1.1 tsubai int addr;
312 1.1 tsubai short cmd;
313 1.1 tsubai u_char buffer[9];
314 1.1 tsubai
315 1.1 tsubai if ((leds & 0x07) == (ksc->sc_leds & 0x07))
316 1.1 tsubai return (0);
317 1.1 tsubai
318 1.1 tsubai addr = (int)ksc->adbaddr;
319 1.1 tsubai buffer[0] = 0;
320 1.1 tsubai kbd_done = 0;
321 1.1 tsubai
322 1.1 tsubai /* talk R2 */
323 1.1 tsubai cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
324 1.1 tsubai ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
325 1.1 tsubai while (!kbd_done)
326 1.1 tsubai /* busy-wait until done */ ;
327 1.1 tsubai
328 1.1 tsubai if (buffer[0] == 0)
329 1.1 tsubai return (EIO);
330 1.1 tsubai
331 1.1 tsubai leds = ~leds & 0x07;
332 1.1 tsubai buffer[2] &= 0xf8;
333 1.1 tsubai buffer[2] |= leds;
334 1.1 tsubai
335 1.1 tsubai /* listen R2 */
336 1.1 tsubai cmd = ((addr & 0xf) << 4) | 0x08 | 0x02;
337 1.1 tsubai ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
338 1.1 tsubai while (!kbd_done)
339 1.1 tsubai /* busy-wait until done */ ;
340 1.1 tsubai
341 1.1 tsubai /* talk R2 */
342 1.1 tsubai cmd = ((addr & 0xf) << 4) | 0x0c | 0x02;
343 1.1 tsubai ADBOp((Ptr)buffer, (Ptr)extdms_complete, (Ptr)&kbd_done, cmd);
344 1.1 tsubai while (!kbd_done)
345 1.1 tsubai /* busy-wait until done */ ;
346 1.1 tsubai
347 1.1 tsubai if (buffer[0] == 0)
348 1.1 tsubai return (EIO);
349 1.1 tsubai
350 1.1 tsubai ksc->sc_leds = ~((u_int8_t)buffer[2]) & 0x07;
351 1.1 tsubai
352 1.1 tsubai if ((buffer[2] & 0xf8) != leds)
353 1.1 tsubai return (EIO);
354 1.1 tsubai else
355 1.1 tsubai return (0);
356 1.1 tsubai }
357 1.1 tsubai
358 1.1 tsubai /*
359 1.1 tsubai * Toggle all of the LED's on and off, just for show.
360 1.1 tsubai */
361 1.1 tsubai static void
362 1.1 tsubai blinkleds(ksc)
363 1.1 tsubai struct kbd_softc *ksc;
364 1.1 tsubai {
365 1.1 tsubai int addr, i;
366 1.1 tsubai u_char blinkleds, origleds;
367 1.1 tsubai
368 1.1 tsubai addr = (int)ksc->adbaddr;
369 1.1 tsubai origleds = getleds(addr);
370 1.1 tsubai blinkleds = LED_NUMLOCK | LED_CAPSLOCK | LED_SCROLL_LOCK;
371 1.1 tsubai
372 1.1 tsubai (void)setleds(ksc, blinkleds);
373 1.1 tsubai
374 1.1 tsubai for (i = 0; i < 10000; i++)
375 1.1 tsubai delay(50);
376 1.1 tsubai
377 1.1 tsubai /* make sure that we restore the LED settings */
378 1.1 tsubai i = 10;
379 1.1 tsubai do {
380 1.1 tsubai (void)setleds(ksc, (u_char)0x00);
381 1.1 tsubai } while (setleds(ksc, (u_char)0x00) && (i-- > 0));
382 1.1 tsubai
383 1.1 tsubai return;
384 1.1 tsubai }
385 1.1 tsubai #endif
386 1.1 tsubai
387 1.1 tsubai #if 0
388 1.1 tsubai int
389 1.1 tsubai kbdioctl(dev, cmd, data, flag, p)
390 1.1 tsubai dev_t dev;
391 1.1 tsubai int cmd;
392 1.1 tsubai caddr_t data;
393 1.1 tsubai int flag;
394 1.1 tsubai struct proc *p;
395 1.1 tsubai {
396 1.1 tsubai struct kbd_softc *ksc;
397 1.1 tsubai int error = 0;
398 1.1 tsubai
399 1.1 tsubai ksc = kbd_cd.cd_devs[minor(dev)];
400 1.1 tsubai
401 1.1 tsubai switch (cmd) {
402 1.1 tsubai case KIOCTYPE: /* Get keyboard type */
403 1.1 tsubai *(u_int8_t *)data = ksc->handler_id;
404 1.1 tsubai case KIOCSLED:
405 1.1 tsubai error = setleds(ksc, *(u_char *)data);
406 1.1 tsubai break;
407 1.1 tsubai case KIOCGLED:
408 1.1 tsubai *(u_int8_t *)data = ksc->sc_leds;
409 1.1 tsubai break;
410 1.1 tsubai default:
411 1.1 tsubai error = (EINVAL);
412 1.1 tsubai }
413 1.1 tsubai return (error);
414 1.1 tsubai }
415 1.1 tsubai #endif
416