xref: /netbsd-src/sys/arch/atari/dev/kbd.c (revision 93bf6008f8b7982c1d1a9486e4a4a0e687fe36eb)
1 /*	$NetBSD: kbd.c,v 1.36 2009/03/18 10:22:24 cegger Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman
5  * Copyright (c) 1982, 1986, 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: kbd.c,v 1.36 2009/03/18 10:22:24 cegger Exp $");
35 
36 #include "mouse.h"
37 #include "ite.h"
38 #include "wskbd.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/ioctl.h>
44 #include <sys/tty.h>
45 #include <sys/proc.h>
46 #include <sys/conf.h>
47 #include <sys/file.h>
48 #include <sys/kernel.h>
49 #include <sys/signalvar.h>
50 #include <sys/syslog.h>
51 #include <dev/cons.h>
52 #include <machine/cpu.h>
53 #include <machine/iomap.h>
54 #include <machine/mfp.h>
55 #include <machine/acia.h>
56 #include <atari/dev/itevar.h>
57 #include <atari/dev/event_var.h>
58 #include <atari/dev/vuid_event.h>
59 #include <atari/dev/ym2149reg.h>
60 #include <atari/dev/kbdreg.h>
61 #include <atari/dev/kbdvar.h>
62 #include <atari/dev/kbdmap.h>
63 #include <atari/dev/msvar.h>
64 
65 #if NWSKBD>0
66 #include <dev/wscons/wsconsio.h>
67 #include <dev/wscons/wskbdvar.h>
68 #include <dev/wscons/wsksymdef.h>
69 #include <dev/wscons/wsksymvar.h>
70 #include <atari/dev/wskbdmap_atari.h>
71 #endif
72 
73 /* WSKBD */
74 /*
75  * If NWSKBD>0 we try to attach an wskbd device to us. What follows
76  * is definitions of callback functions and structures that are passed
77  * to wscons when initializing.
78  */
79 
80 /*
81  * Now with wscons this driver exhibits some weird behaviour.
82  * It may act both as a driver of its own and the md part of the
83  * wskbd driver. Therefore it can be accessed through /dev/kbd
84  * and /dev/wskbd0 both.
85  *
86  * The data from they keyboard may end up in at least four different
87  * places:
88  * - If this driver has been opened (/dev/kbd) and the
89  *   direct mode (TIOCDIRECT) has been set, data goes to
90  *   the process who opened the device. Data will transmit itself
91  *   as described by the firm_event structure.
92  * - If wskbd support is compiled in and a wskbd driver has been
93  *   attached then the data is sent to it. Wskbd in turn may
94  *   - Send the data in the wscons_event form to a process that
95  *     has opened /dev/wskbd0
96  *   - Feed the data to a virtual terminal.
97  * - If an ite is present the data may be fed to it.
98  */
99 
100 u_char			kbd_modifier;	/* Modifier mask		*/
101 
102 static u_char		kbd_ring[KBD_RING_SIZE];
103 static volatile u_int	kbd_rbput = 0;	/* 'put' index			*/
104 static u_int		kbd_rbget = 0;	/* 'get' index			*/
105 static u_char		kbd_soft  = 0;	/* 1: Softint has been scheduled*/
106 
107 static struct kbd_softc kbd_softc;
108 
109 /* {b,c}devsw[] function prototypes */
110 dev_type_open(kbdopen);
111 dev_type_close(kbdclose);
112 dev_type_read(kbdread);
113 dev_type_ioctl(kbdioctl);
114 dev_type_poll(kbdpoll);
115 dev_type_kqfilter(kbdkqfilter);
116 
117 /* Interrupt handler */
118 void	kbdintr(int);
119 
120 static void kbdsoft(void *, void *);
121 static void kbdattach(struct device *, struct device *, void *);
122 static int  kbdmatch(struct device *, struct cfdata *, void *);
123 #if NITE>0
124 static int  kbd_do_modifier(u_char);
125 #endif
126 static int  kbd_write_poll(u_char *, int);
127 static void kbd_pkg_start(struct kbd_softc *, u_char);
128 
129 CFATTACH_DECL(kbd, sizeof(struct device),
130     kbdmatch, kbdattach, NULL, NULL);
131 
132 const struct cdevsw kbd_cdevsw = {
133 	kbdopen, kbdclose, kbdread, nowrite, kbdioctl,
134 	nostop, notty, kbdpoll, nommap, kbdkqfilter,
135 };
136 
137 #if NWSKBD>0
138 /* accessops */
139 static int	kbd_enable(void *, int);
140 static void	kbd_set_leds(void *, int);
141 static int	kbd_ioctl(void *, u_long, void *, int, struct lwp *);
142 
143 /* console ops */
144 static void	kbd_getc(void *, u_int *, int *);
145 static void	kbd_pollc(void *, int);
146 static void	kbd_bell(void *, u_int, u_int, u_int);
147 
148 static struct wskbd_accessops kbd_accessops = {
149 	kbd_enable,
150 	kbd_set_leds,
151 	kbd_ioctl
152 };
153 
154 static struct wskbd_consops kbd_consops = {
155         kbd_getc,
156 	kbd_pollc,
157 	kbd_bell
158 };
159 
160 /* Pointer to keymaps. */
161 static struct wskbd_mapdata kbd_mapdata = {
162         atarikbd_keydesctab,
163 	KB_US
164 };
165 #endif /* WSKBD */
166 
167 /*ARGSUSED*/
168 static	int
169 kbdmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
170 {
171 	if (!strcmp((char *)auxp, "kbd"))
172 		return (1);
173 	return (0);
174 }
175 
176 /*ARGSUSED*/
177 static void
178 kbdattach(struct device *pdp, struct device *dp, void *auxp)
179 {
180 	int	timeout;
181 	u_char	kbd_rst[]  = { 0x80, 0x01 };
182 	u_char	kbd_icmd[] = { 0x12, 0x15 };
183 
184 	/*
185 	 * Disable keyboard interrupts from MFP
186 	 */
187 	MFP->mf_ierb &= ~IB_AINT;
188 
189 	/*
190 	 * Reset ACIA and intialize to:
191 	 *    divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
192 	 */
193 	KBD->ac_cs = A_RESET;
194 	delay(100);	/* XXX: enough? */
195 	KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
196 
197 	/*
198 	 * Clear error conditions
199 	 */
200 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
201 		timeout = KBD->ac_da;
202 
203 	/*
204 	 * Now send the reset string, and read+ignore it's response
205 	 */
206 	if (!kbd_write_poll(kbd_rst, 2))
207 		printf("kbd: error cannot reset keyboard\n");
208 	for (timeout = 1000; timeout > 0; timeout--) {
209 		if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
210 			timeout = KBD->ac_da;
211 			timeout = 100;
212 		}
213 		delay(100);
214 	}
215 	/*
216 	 * Send init command: disable mice & joysticks
217 	 */
218 	kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
219 
220 	printf("\n");
221 
222 #if NWSKBD>0
223 	if (dp != NULL) {
224 		/*
225 		 * Try to attach the wskbd.
226 		 */
227 		struct wskbddev_attach_args waa;
228 
229 		/* Maybe should be done before this?... */
230 		wskbd_cnattach(&kbd_consops, NULL, &kbd_mapdata);
231 
232 		waa.console = 1;
233 		waa.keymap = &kbd_mapdata;
234 		waa.accessops = &kbd_accessops;
235 		waa.accesscookie = NULL;
236 		kbd_softc.k_wskbddev = config_found(dp, &waa, wskbddevprint);
237 
238 		kbd_softc.k_pollingmode = 0;
239 
240 		kbdenable();
241 	}
242 #endif /* WSKBD */
243 }
244 
245 void
246 kbdenable(void)
247 {
248 	int	s, code;
249 
250 	s = spltty();
251 
252 	/*
253 	 * Clear error conditions...
254 	 */
255 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
256 		code = KBD->ac_da;
257 	/*
258 	 * Enable interrupts from MFP
259 	 */
260 	MFP->mf_iprb  = (u_int8_t)~IB_AINT;
261 	MFP->mf_ierb |= IB_AINT;
262 	MFP->mf_imrb |= IB_AINT;
263 
264 	kbd_softc.k_event_mode   = 0;
265 	kbd_softc.k_events.ev_io = 0;
266 	kbd_softc.k_pkg_size     = 0;
267 	splx(s);
268 }
269 
270 int kbdopen(dev_t dev, int flags, int mode, struct lwp *l)
271 {
272 	if (kbd_softc.k_events.ev_io)
273 		return EBUSY;
274 
275 	kbd_softc.k_events.ev_io = l->l_proc;
276 	ev_init(&kbd_softc.k_events);
277 	return (0);
278 }
279 
280 int
281 kbdclose(dev_t dev, int flags, int mode, struct lwp *l)
282 {
283 	/* Turn off event mode, dump the queue */
284 	kbd_softc.k_event_mode = 0;
285 	ev_fini(&kbd_softc.k_events);
286 	kbd_softc.k_events.ev_io = NULL;
287 	return (0);
288 }
289 
290 int
291 kbdread(dev_t dev, struct uio *uio, int flags)
292 {
293 	return ev_read(&kbd_softc.k_events, uio, flags);
294 }
295 
296 int
297 kbdioctl(dev_t dev,u_long cmd,register void *data,int flag,struct lwp *l)
298 {
299 	register struct kbd_softc *k = &kbd_softc;
300 	struct kbdbell	*kb;
301 
302 	switch (cmd) {
303 		case KIOCTRANS:
304 			if (*(int *)data == TR_UNTRANS_EVENT)
305 				return 0;
306 			break;
307 
308 		case KIOCGTRANS:
309 			/*
310 			 * Get translation mode
311 			 */
312 			*(int *)data = TR_UNTRANS_EVENT;
313 			return 0;
314 
315 		case KIOCSDIRECT:
316 			k->k_event_mode = *(int *)data;
317 			return 0;
318 
319 		case KIOCRINGBELL:
320 			kb = (struct kbdbell *)data;
321 			if (kb)
322 				kbd_bell_sparms(kb->volume, kb->pitch,
323 							kb->duration);
324 			kbdbell();
325 			return 0;
326 
327 		case FIONBIO:	/* we will remove this someday (soon???) */
328 			return 0;
329 
330 		case FIOASYNC:
331 			k->k_events.ev_async = *(int *)data != 0;
332 				return 0;
333 
334 		case FIOSETOWN:
335 			if (-*(int *)data != k->k_events.ev_io->p_pgid
336 			    && *(int *)data != k->k_events.ev_io->p_pid)
337 				return EPERM;
338 			return 0;
339 
340 		case TIOCSPGRP:
341 			if (*(int *)data != k->k_events.ev_io->p_pgid)
342 				return EPERM;
343 			return 0;
344 
345 		default:
346 			return ENOTTY;
347 	}
348 
349 	/*
350 	 * We identified the ioctl, but we do not handle it.
351 	 */
352 	return EOPNOTSUPP;		/* misuse, but what the heck */
353 }
354 
355 int
356 kbdpoll (dev_t dev, int events, struct lwp *l)
357 {
358   return ev_poll (&kbd_softc.k_events, events, l);
359 }
360 
361 int
362 kbdkqfilter(dev_t dev, struct knote *kn)
363 {
364 
365 	return (ev_kqfilter(&kbd_softc.k_events, kn));
366 }
367 
368 /*
369  * Keyboard interrupt handler called straight from MFP at spl6.
370  */
371 void
372 kbdintr(int sr)
373 	/* sr:	 sr at time of interrupt	*/
374 {
375 	int	code;
376 	int	got_char = 0;
377 
378 	/*
379 	 * There may be multiple keys available. Read them all.
380 	 */
381 	while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
382 		got_char = 1;
383 		if (KBD->ac_cs & (A_OE|A_PE)) {
384 			code = KBD->ac_da;	/* Silently ignore errors */
385 			continue;
386 		}
387 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
388 	}
389 
390 	/*
391 	 * If characters are waiting for transmit, send them.
392 	 */
393 	if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
394 		if (kbd_softc.k_sendp != NULL)
395 			KBD->ac_da = *kbd_softc.k_sendp++;
396 		if (--kbd_softc.k_send_cnt <= 0) {
397 			/*
398 			 * The total package has been transmitted,
399 			 * wakeup anyone waiting for it.
400 			 */
401 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
402 			kbd_softc.k_sendp    = NULL;
403 			kbd_softc.k_send_cnt = 0;
404 			wakeup((void *)&kbd_softc.k_send_cnt);
405 		}
406 	}
407 
408 	/*
409 	 * Activate software-level to handle possible input.
410 	 */
411 	if (got_char) {
412 		if (!BASEPRI(sr)) {
413 			if (!kbd_soft++)
414 				add_sicallback(kbdsoft, 0, 0);
415 		} else {
416 			spl1();
417 			kbdsoft(NULL, NULL);
418 		}
419 	}
420 }
421 
422 /*
423  * Keyboard soft interrupt handler
424  */
425 void
426 kbdsoft(void *junk1, void *junk2)
427 {
428 	int			s;
429 	u_char			code;
430 	struct kbd_softc	*k = &kbd_softc;
431 	struct firm_event	*fe;
432 	int			put;
433 	int			n, get;
434 
435 	kbd_soft = 0;
436 	get      = kbd_rbget;
437 
438 	for (;;) {
439 		n = kbd_rbput;
440 		if (get == n) /* We're done	*/
441 			break;
442 		n -= get;
443 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
444 			get += n - KBD_RING_SIZE;
445 			n    = KBD_RING_SIZE;
446 		}
447 		while (--n >= 0) {
448 			code = kbd_ring[get++ & KBD_RING_MASK];
449 
450 			/*
451 			 * If collecting a package, stuff it in and
452 			 * continue.
453 			 */
454 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
455 			    k->k_package[k->k_pkg_idx++] = code;
456 			    if (k->k_pkg_idx == k->k_pkg_size) {
457 				/*
458 				 * Package is complete.
459 				 */
460 				switch(k->k_pkg_type) {
461 #if NMOUSE > 0
462 				    case KBD_AMS_PKG:
463 				    case KBD_RMS_PKG:
464 				    case KBD_JOY1_PKG:
465 			 		mouse_soft((REL_MOUSE *)k->k_package,
466 						k->k_pkg_size, k->k_pkg_type);
467 #endif /* NMOUSE */
468 				}
469 				k->k_pkg_size = 0;
470 			    }
471 			    continue;
472 			}
473 			/*
474 			 * If this is a package header, init pkg. handling.
475 			 */
476 			if (!KBD_IS_KEY(code)) {
477 				kbd_pkg_start(k, code);
478 				continue;
479 			}
480 #if NWSKBD>0
481 			/*
482 			 * If we have attached a wskbd and not in polling mode and
483 			 * nobody has opened us directly, then send the keystroke
484 			 * to the wskbd.
485 			 */
486 
487 			if (kbd_softc.k_pollingmode == 0
488 			    && kbd_softc.k_wskbddev != NULL
489 			    && k->k_event_mode == 0) {
490 				wskbd_input(kbd_softc.k_wskbddev,
491 					    KBD_RELEASED(code) ?
492 					    WSCONS_EVENT_KEY_UP :
493 					    WSCONS_EVENT_KEY_DOWN,
494 					    KBD_SCANCODE(code));
495 				continue;
496 			}
497 #endif /* NWSKBD */
498 #if NITE>0
499 			if (kbd_do_modifier(code) && !k->k_event_mode)
500 				continue;
501 #endif
502 
503 			/*
504 			 * if not in event mode, deliver straight to ite to
505 			 * process key stroke
506 			 */
507 			if (!k->k_event_mode) {
508 				/* Gets to spltty() by itself	*/
509 #if NITE>0
510 				ite_filter(code, ITEFILT_TTY);
511 #endif
512 				continue;
513 			}
514 
515 			/*
516 			 * Keyboard is generating events.  Turn this keystroke
517 			 * into an event and put it in the queue.  If the queue
518 			 * is full, the keystroke is lost (sorry!).
519 			 */
520 			s = spltty();
521 			put = k->k_events.ev_put;
522 			fe  = &k->k_events.ev_q[put];
523 			put = (put + 1) % EV_QSIZE;
524 			if (put == k->k_events.ev_get) {
525 				log(LOG_WARNING,
526 					"keyboard event queue overflow\n");
527 				splx(s);
528 				continue;
529 			}
530 			fe->id    = KBD_SCANCODE(code);
531 			fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
532 			firm_gettime(fe);
533 			k->k_events.ev_put = put;
534 			EV_WAKEUP(&k->k_events);
535 			splx(s);
536 		}
537 		kbd_rbget = get;
538 	}
539 }
540 
541 static	u_char sound[] = {
542 	0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
543 	0xF8,0x10,0x10,0x10,0x00,0x20,0x03
544 };
545 
546 void
547 kbdbell(void)
548 {
549 	register int	i, sps;
550 
551 	sps = splhigh();
552 	for (i = 0; i < sizeof(sound); i++) {
553 		YM2149->sd_selr = i;
554 		YM2149->sd_wdat = sound[i];
555 	}
556 	splx(sps);
557 }
558 
559 
560 /*
561  * Set the parameters of the 'default' beep.
562  */
563 
564 #define KBDBELLCLOCK	125000	/* 2MHz / 16 */
565 #define KBDBELLDURATION	128	/* 256 / 2MHz */
566 
567 void
568 kbd_bell_gparms(u_int *volume, u_int *pitch, u_int *duration)
569 {
570 	u_int	tmp;
571 
572 	tmp = sound[11] | (sound[12] << 8);
573 	*duration = (tmp * KBDBELLDURATION) / 1000;
574 
575 	tmp = sound[0] | (sound[1] << 8);
576 	*pitch = KBDBELLCLOCK / tmp;
577 
578 	*volume = 0;
579 }
580 
581 void
582 kbd_bell_sparms(u_int volume, u_int pitch, u_int duration)
583 {
584 	u_int	f, t;
585 
586 	f = pitch > 10 ? pitch : 10;	/* minimum pitch */
587 	if (f > 20000)
588 		f = 20000;		/* maximum pitch */
589 
590 	f = KBDBELLCLOCK / f;
591 
592 	t = (duration * 1000) / KBDBELLDURATION;
593 
594 	sound[ 0] = f & 0xff;
595 	sound[ 1] = (f >> 8) & 0xf;
596 	f -= 1;
597 	sound[ 2] = f & 0xff;
598 	sound[ 3] = (f >> 8) & 0xf;
599 	f += 2;
600 	sound[ 4] = f & 0xff;
601 	sound[ 5] = (f >> 8) & 0xf;
602 
603 	sound[11] = t & 0xff;
604 	sound[12] = (t >> 8) & 0xff;
605 
606 	sound[13] = 0x03;
607 }
608 
609 int
610 kbdgetcn(void)
611 {
612 	u_char	code;
613 	int	s = spltty();
614 	int	ints_active;
615 
616 	ints_active = 0;
617 	if (MFP->mf_imrb & IB_AINT) {
618 		ints_active   = 1;
619 		MFP->mf_imrb &= ~IB_AINT;
620 	}
621 	for (;;) {
622 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
623 			;	/* Wait for key	*/
624 		if (KBD->ac_cs & (A_OE|A_PE)) {
625 			code = KBD->ac_da;	/* Silently ignore errors */
626 			continue;
627 		}
628 		code = KBD->ac_da;
629 #if NITE>0
630 		if (!kbd_do_modifier(code))
631 #endif
632 			break;
633 	}
634 
635 	if (ints_active) {
636 		MFP->mf_iprb  = (u_int8_t)~IB_AINT;
637 		MFP->mf_imrb |=  IB_AINT;
638 	}
639 
640 	splx (s);
641 	return code;
642 }
643 
644 /*
645  * Write a command to the keyboard in 'polled' mode.
646  */
647 static int
648 kbd_write_poll(u_char *cmd, int len)
649 {
650 	int	timeout;
651 
652 	while (len-- > 0) {
653 		KBD->ac_da = *cmd++;
654 		for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
655 			delay(10);
656 		if (!(KBD->ac_cs & A_TXRDY))
657 			return (0);
658 	}
659 	return (1);
660 }
661 
662 /*
663  * Write a command to the keyboard. Return when command is send.
664  */
665 void
666 kbd_write(u_char *cmd, int len)
667 {
668 	struct kbd_softc	*k = &kbd_softc;
669 	int			sps;
670 
671 	/*
672 	 * Get to splhigh, 'real' interrupts arrive at spl6!
673 	 */
674 	sps = splhigh();
675 
676 	/*
677 	 * Make sure any privious write has ended...
678 	 */
679 	while (k->k_sendp != NULL)
680 		tsleep((void *)&k->k_sendp, TTOPRI, "kbd_write1", 0);
681 
682 	/*
683 	 * If the KBD-acia is not currently busy, send the first
684 	 * character now.
685 	 */
686 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
687 	if (KBD->ac_cs & A_TXRDY) {
688 		KBD->ac_da = *cmd++;
689 		len--;
690 	}
691 
692 	/*
693 	 * If we're not yet done, wait until all characters are send.
694 	 */
695 	if (len > 0) {
696 		k->k_sendp    = cmd;
697 		k->k_send_cnt = len;
698 		tsleep((void *)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
699 	}
700 	splx(sps);
701 
702 	/*
703 	 * Wakeup all procs waiting for us.
704 	 */
705 	wakeup((void *)&k->k_sendp);
706 }
707 
708 /*
709  * Setup softc-fields to assemble a keyboard package.
710  */
711 static void
712 kbd_pkg_start(struct kbd_softc *kp, u_char msg_start)
713 {
714 	kp->k_pkg_idx    = 1;
715 	kp->k_package[0] = msg_start;
716 	switch (msg_start) {
717 		case 0xf6:
718 			kp->k_pkg_type = KBD_MEM_PKG;
719 			kp->k_pkg_size = 8;
720 			break;
721 		case 0xf7:
722 			kp->k_pkg_type = KBD_AMS_PKG;
723 			kp->k_pkg_size = 6;
724 			break;
725 		case 0xf8:
726 		case 0xf9:
727 		case 0xfa:
728 		case 0xfb:
729 			kp->k_pkg_type = KBD_RMS_PKG;
730 			kp->k_pkg_size = 3;
731 			break;
732 		case 0xfc:
733 			kp->k_pkg_type = KBD_CLK_PKG;
734 			kp->k_pkg_size = 7;
735 			break;
736 		case 0xfe:
737 			kp->k_pkg_type = KBD_JOY0_PKG;
738 			kp->k_pkg_size = 2;
739 			break;
740 		case 0xff:
741 			kp->k_pkg_type = KBD_JOY1_PKG;
742 			kp->k_pkg_size = 2;
743 			break;
744 		default:
745 			printf("kbd: Unknown packet 0x%x\n", msg_start);
746 			break;
747 	}
748 }
749 
750 #if NITE>0
751 /*
752  * Modifier processing
753  */
754 static int
755 kbd_do_modifier(u_char code)
756 {
757 	u_char	up, mask;
758 
759 	up   = KBD_RELEASED(code);
760 	mask = 0;
761 
762 	switch(KBD_SCANCODE(code)) {
763 		case KBD_LEFT_SHIFT:
764 			mask = KBD_MOD_LSHIFT;
765 			break;
766 		case KBD_RIGHT_SHIFT:
767 			mask = KBD_MOD_RSHIFT;
768 			break;
769 		case KBD_CTRL:
770 			mask = KBD_MOD_CTRL;
771 			break;
772 		case KBD_ALT:
773 			mask = KBD_MOD_ALT;
774 			break;
775 		case KBD_CAPS_LOCK:
776 			/* CAPSLOCK is a toggle */
777 			if(!up)
778 				kbd_modifier ^= KBD_MOD_CAPS;
779 			return 1;
780 	}
781 	if(mask) {
782 		if(up)
783 			kbd_modifier &= ~mask;
784 		else
785 			kbd_modifier |= mask;
786 		return 1;
787 	}
788 	return 0;
789 }
790 #endif
791 
792 #if NWSKBD>0
793 /*
794  * These are the callback functions that are passed to wscons.
795  * They really don't do anything worth noting, just call the
796  * other functions above.
797  */
798 
799 static int
800 kbd_enable(void *c, int on)
801 {
802         /* Wonder what this is supposed to do... */
803 	return 0;
804 }
805 
806 static void
807 kbd_set_leds(void *c, int leds)
808 {
809         /* we can not set the leds */
810 }
811 
812 static int
813 kbd_ioctl(void *c, u_long cmd, void *data, int flag, struct proc *p)
814 {
815 	struct wskbd_bell_data *kd;
816 
817 	switch (cmd)
818 	{
819 	case WSKBDIO_COMPLEXBELL:
820 		kd = (struct wskbd_bell_data *)data;
821 		kbd_bell(0, kd->pitch, kd->period, kd->volume);
822 		return 0;
823 	case WSKBDIO_SETLEDS:
824 		return 0;
825 	case WSKBDIO_GETLEDS:
826 		*(int*)data = 0;
827 		return 0;
828 	case WSKBDIO_GTYPE:
829 		*(u_int*)data = WSKBD_TYPE_ATARI;
830 		return 0;
831 	}
832 
833 	/*
834 	 * We are supposed to return EPASSTHROUGH to wscons if we didn't
835 	 * understand.
836 	 */
837 	return (EPASSTHROUGH);
838 }
839 
840 static void
841 kbd_getc(void *c, u_int *type, int *data)
842 {
843 	int key;
844 	key = kbdgetcn();
845 
846 	*data = KBD_SCANCODE(key);
847 	*type = KBD_RELEASED(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
848 }
849 
850 static void
851 kbd_pollc(void *c, int on)
852 {
853         kbd_softc.k_pollingmode = on;
854 }
855 
856 static void
857 kbd_bell(void *v, u_int pitch, u_int duration, u_int volume)
858 {
859         kbd_bell_sparms(volume, pitch, duration);
860 	kbdbell();
861 }
862 #endif /* NWSKBD */
863