xref: /netbsd-src/sys/arch/atari/dev/kbd.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: kbd.c,v 1.28 2004/03/25 10:17:19 leo 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.28 2004/03/25 10:17:19 leo 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 __P((int));
119 
120 static void kbdsoft __P((void *, void *));
121 static void kbdattach __P((struct device *, struct device *, void *));
122 static int  kbdmatch __P((struct device *, struct cfdata *, void *));
123 #if NITE>0
124 static int  kbd_do_modifier __P((u_char));
125 #endif
126 static int  kbd_write_poll __P((u_char *, int));
127 static void kbd_pkg_start __P((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, caddr_t, int, struct proc *);
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(pdp, cfp, auxp)
170 struct	device	*pdp;
171 struct	cfdata	*cfp;
172 void		*auxp;
173 {
174 	if (!strcmp((char *)auxp, "kbd"))
175 		return (1);
176 	return (0);
177 }
178 
179 /*ARGSUSED*/
180 static void
181 kbdattach(pdp, dp, auxp)
182 struct	device *pdp, *dp;
183 void	*auxp;
184 {
185 	int	timeout;
186 	u_char	kbd_rst[]  = { 0x80, 0x01 };
187 	u_char	kbd_icmd[] = { 0x12, 0x15 };
188 
189 	/*
190 	 * Disable keyboard interrupts from MFP
191 	 */
192 	MFP->mf_ierb &= ~IB_AINT;
193 
194 	/*
195 	 * Reset ACIA and intialize to:
196 	 *    divide by 16, 8 data, 1 stop, no parity, enable RX interrupts
197 	 */
198 	KBD->ac_cs = A_RESET;
199 	delay(100);	/* XXX: enough? */
200 	KBD->ac_cs = kbd_softc.k_soft_cs = KBD_INIT | A_RXINT;
201 
202 	/*
203 	 * Clear error conditions
204 	 */
205 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
206 		timeout = KBD->ac_da;
207 
208 	/*
209 	 * Now send the reset string, and read+ignore it's response
210 	 */
211 	if (!kbd_write_poll(kbd_rst, 2))
212 		printf("kbd: error cannot reset keyboard\n");
213 	for (timeout = 1000; timeout > 0; timeout--) {
214 		if (KBD->ac_cs & (A_IRQ|A_RXRDY)) {
215 			timeout = KBD->ac_da;
216 			timeout = 100;
217 		}
218 		delay(100);
219 	}
220 	/*
221 	 * Send init command: disable mice & joysticks
222 	 */
223 	kbd_write_poll(kbd_icmd, sizeof(kbd_icmd));
224 
225 	printf("\n");
226 
227 #if NWSKBD>0
228 	if (dp != NULL) {
229 		/*
230 		 * Try to attach the wskbd.
231 		 */
232 		struct wskbddev_attach_args waa;
233 
234 		/* Maybe should be done before this?... */
235 		wskbd_cnattach(&kbd_consops, NULL, &kbd_mapdata);
236 
237 		waa.console = 1;
238 		waa.keymap = &kbd_mapdata;
239 		waa.accessops = &kbd_accessops;
240 		waa.accesscookie = NULL;
241 		kbd_softc.k_wskbddev = config_found(dp, &waa, wskbddevprint);
242 
243 		kbd_softc.k_pollingmode = 0;
244 
245 		kbdenable();
246 	}
247 #endif /* WSKBD */
248 }
249 
250 void
251 kbdenable()
252 {
253 	int	s, code;
254 
255 	s = spltty();
256 
257 	/*
258 	 * Clear error conditions...
259 	 */
260 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
261 		code = KBD->ac_da;
262 	/*
263 	 * Enable interrupts from MFP
264 	 */
265 	MFP->mf_iprb  = (u_int8_t)~IB_AINT;
266 	MFP->mf_ierb |= IB_AINT;
267 	MFP->mf_imrb |= IB_AINT;
268 
269 	kbd_softc.k_event_mode   = 0;
270 	kbd_softc.k_events.ev_io = 0;
271 	kbd_softc.k_pkg_size     = 0;
272 	splx(s);
273 }
274 
275 int kbdopen(dev_t dev, int flags, int mode, struct proc *p)
276 {
277 	if (kbd_softc.k_events.ev_io)
278 		return EBUSY;
279 
280 	kbd_softc.k_events.ev_io = p;
281 	ev_init(&kbd_softc.k_events);
282 	return (0);
283 }
284 
285 int
286 kbdclose(dev_t dev, int flags, int mode, struct proc *p)
287 {
288 	/* Turn off event mode, dump the queue */
289 	kbd_softc.k_event_mode = 0;
290 	ev_fini(&kbd_softc.k_events);
291 	kbd_softc.k_events.ev_io = NULL;
292 	return (0);
293 }
294 
295 int
296 kbdread(dev_t dev, struct uio *uio, int flags)
297 {
298 	return ev_read(&kbd_softc.k_events, uio, flags);
299 }
300 
301 int
302 kbdioctl(dev_t dev,u_long cmd,register caddr_t data,int flag,struct proc *p)
303 {
304 	register struct kbd_softc *k = &kbd_softc;
305 	struct kbdbell	*kb;
306 
307 	switch (cmd) {
308 		case KIOCTRANS:
309 			if (*(int *)data == TR_UNTRANS_EVENT)
310 				return 0;
311 			break;
312 
313 		case KIOCGTRANS:
314 			/*
315 			 * Get translation mode
316 			 */
317 			*(int *)data = TR_UNTRANS_EVENT;
318 			return 0;
319 
320 		case KIOCSDIRECT:
321 			k->k_event_mode = *(int *)data;
322 			return 0;
323 
324 		case KIOCRINGBELL:
325 			kb = (struct kbdbell *)data;
326 			if (kb)
327 				kbd_bell_sparms(kb->volume, kb->pitch,
328 							kb->duration);
329 			kbdbell();
330 			return 0;
331 
332 		case FIONBIO:	/* we will remove this someday (soon???) */
333 			return 0;
334 
335 		case FIOASYNC:
336 			k->k_events.ev_async = *(int *)data != 0;
337 				return 0;
338 
339 		case FIOSETOWN:
340 			if (-*(int *)data != k->k_events.ev_io->p_pgid
341 			    && *(int *)data != k->k_events.ev_io->p_pid)
342 				return EPERM;
343 			return 0;
344 
345 		case TIOCSPGRP:
346 			if (*(int *)data != k->k_events.ev_io->p_pgid)
347 				return EPERM;
348 			return 0;
349 
350 		default:
351 			return ENOTTY;
352 	}
353 
354 	/*
355 	 * We identified the ioctl, but we do not handle it.
356 	 */
357 	return EOPNOTSUPP;		/* misuse, but what the heck */
358 }
359 
360 int
361 kbdpoll (dev_t dev, int events, struct proc *p)
362 {
363   return ev_poll (&kbd_softc.k_events, events, p);
364 }
365 
366 int
367 kbdkqfilter(dev_t dev, struct knote *kn)
368 {
369 
370 	return (ev_kqfilter(&kbd_softc.k_events, kn));
371 }
372 
373 /*
374  * Keyboard interrupt handler called straight from MFP at spl6.
375  */
376 void
377 kbdintr(sr)
378 int sr;	/* sr at time of interrupt	*/
379 {
380 	int	code;
381 	int	got_char = 0;
382 
383 	/*
384 	 * There may be multiple keys available. Read them all.
385 	 */
386 	while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
387 		got_char = 1;
388 		if (KBD->ac_cs & (A_OE|A_PE)) {
389 			code = KBD->ac_da;	/* Silently ignore errors */
390 			continue;
391 		}
392 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
393 	}
394 
395 	/*
396 	 * If characters are waiting for transmit, send them.
397 	 */
398 	if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
399 		if (kbd_softc.k_sendp != NULL)
400 			KBD->ac_da = *kbd_softc.k_sendp++;
401 		if (--kbd_softc.k_send_cnt <= 0) {
402 			/*
403 			 * The total package has been transmitted,
404 			 * wakeup anyone waiting for it.
405 			 */
406 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
407 			kbd_softc.k_sendp    = NULL;
408 			kbd_softc.k_send_cnt = 0;
409 			wakeup((caddr_t)&kbd_softc.k_send_cnt);
410 		}
411 	}
412 
413 	/*
414 	 * Activate software-level to handle possible input.
415 	 */
416 	if (got_char) {
417 		if (!BASEPRI(sr)) {
418 			if (!kbd_soft++)
419 				add_sicallback(kbdsoft, 0, 0);
420 		} else {
421 			spl1();
422 			kbdsoft(NULL, NULL);
423 		}
424 	}
425 }
426 
427 /*
428  * Keyboard soft interrupt handler
429  */
430 void
431 kbdsoft(junk1, junk2)
432 void	*junk1, *junk2;
433 {
434 	int			s;
435 	u_char			code;
436 	struct kbd_softc	*k = &kbd_softc;
437 	struct firm_event	*fe;
438 	int			put;
439 	int			n, get;
440 
441 	kbd_soft = 0;
442 	get      = kbd_rbget;
443 
444 	for (;;) {
445 		n = kbd_rbput;
446 		if (get == n) /* We're done	*/
447 			break;
448 		n -= get;
449 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
450 			get += n - KBD_RING_SIZE;
451 			n    = KBD_RING_SIZE;
452 		}
453 		while (--n >= 0) {
454 			code = kbd_ring[get++ & KBD_RING_MASK];
455 
456 			/*
457 			 * If collecting a package, stuff it in and
458 			 * continue.
459 			 */
460 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
461 			    k->k_package[k->k_pkg_idx++] = code;
462 			    if (k->k_pkg_idx == k->k_pkg_size) {
463 				/*
464 				 * Package is complete.
465 				 */
466 				switch(k->k_pkg_type) {
467 #if NMOUSE > 0
468 				    case KBD_AMS_PKG:
469 				    case KBD_RMS_PKG:
470 				    case KBD_JOY1_PKG:
471 			 		mouse_soft((REL_MOUSE *)k->k_package,
472 						k->k_pkg_size, k->k_pkg_type);
473 #endif /* NMOUSE */
474 				}
475 				k->k_pkg_size = 0;
476 			    }
477 			    continue;
478 			}
479 			/*
480 			 * If this is a package header, init pkg. handling.
481 			 */
482 			if (!KBD_IS_KEY(code)) {
483 				kbd_pkg_start(k, code);
484 				continue;
485 			}
486 #if NWSKBD>0
487 			/*
488 			 * If we have attached a wskbd and not in polling mode and
489 			 * nobody has opened us directly, then send the keystroke
490 			 * to the wskbd.
491 			 */
492 
493 			if (kbd_softc.k_pollingmode == 0
494 			    && kbd_softc.k_wskbddev != NULL
495 			    && k->k_event_mode == 0) {
496 				wskbd_input(kbd_softc.k_wskbddev,
497 					    KBD_RELEASED(code) ?
498 					    WSCONS_EVENT_KEY_UP :
499 					    WSCONS_EVENT_KEY_DOWN,
500 					    KBD_SCANCODE(code));
501 				continue;
502 			}
503 #endif /* NWSKBD */
504 #if NITE>0
505 			if (kbd_do_modifier(code) && !k->k_event_mode)
506 				continue;
507 #endif
508 
509 			/*
510 			 * if not in event mode, deliver straight to ite to
511 			 * process key stroke
512 			 */
513 			if (!k->k_event_mode) {
514 				/* Gets to spltty() by itself	*/
515 #if NITE>0
516 				ite_filter(code, ITEFILT_TTY);
517 #endif
518 				continue;
519 			}
520 
521 			/*
522 			 * Keyboard is generating events.  Turn this keystroke
523 			 * into an event and put it in the queue.  If the queue
524 			 * is full, the keystroke is lost (sorry!).
525 			 */
526 			s = spltty();
527 			put = k->k_events.ev_put;
528 			fe  = &k->k_events.ev_q[put];
529 			put = (put + 1) % EV_QSIZE;
530 			if (put == k->k_events.ev_get) {
531 				log(LOG_WARNING,
532 					"keyboard event queue overflow\n");
533 				splx(s);
534 				continue;
535 			}
536 			fe->id    = KBD_SCANCODE(code);
537 			fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
538 			fe->time  = time;
539 			k->k_events.ev_put = put;
540 			EV_WAKEUP(&k->k_events);
541 			splx(s);
542 		}
543 		kbd_rbget = get;
544 	}
545 }
546 
547 static	u_char sound[] = {
548 	0xA8,0x01,0xA9,0x01,0xAA,0x01,0x00,
549 	0xF8,0x10,0x10,0x10,0x00,0x20,0x03
550 };
551 
552 void
553 kbdbell()
554 {
555 	register int	i, sps;
556 
557 	sps = splhigh();
558 	for (i = 0; i < sizeof(sound); i++) {
559 		YM2149->sd_selr = i;
560 		YM2149->sd_wdat = sound[i];
561 	}
562 	splx(sps);
563 }
564 
565 
566 /*
567  * Set the parameters of the 'default' beep.
568  */
569 
570 #define KBDBELLCLOCK	125000	/* 2MHz / 16 */
571 #define KBDBELLDURATION	128	/* 256 / 2MHz */
572 
573 void
574 kbd_bell_gparms(volume, pitch, duration)
575 	u_int	*volume, *pitch, *duration;
576 {
577 	u_int	tmp;
578 
579 	tmp = sound[11] | (sound[12] << 8);
580 	*duration = (tmp * KBDBELLDURATION) / 1000;
581 
582 	tmp = sound[0] | (sound[1] << 8);
583 	*pitch = KBDBELLCLOCK / tmp;
584 
585 	*volume = 0;
586 }
587 
588 void
589 kbd_bell_sparms(volume, pitch, duration)
590 	u_int	volume, pitch, duration;
591 {
592 	u_int	f, t;
593 
594 	f = pitch > 10 ? pitch : 10;	/* minimum pitch */
595 	if (f > 20000)
596 		f = 20000;		/* maximum pitch */
597 
598 	f = KBDBELLCLOCK / f;
599 
600 	t = (duration * 1000) / KBDBELLDURATION;
601 
602 	sound[ 0] = f & 0xff;
603 	sound[ 1] = (f >> 8) & 0xf;
604 	f -= 1;
605 	sound[ 2] = f & 0xff;
606 	sound[ 3] = (f >> 8) & 0xf;
607 	f += 2;
608 	sound[ 4] = f & 0xff;
609 	sound[ 5] = (f >> 8) & 0xf;
610 
611 	sound[11] = t & 0xff;
612 	sound[12] = (t >> 8) & 0xff;
613 
614 	sound[13] = 0x03;
615 }
616 
617 int
618 kbdgetcn()
619 {
620 	u_char	code;
621 	int	s = spltty();
622 	int	ints_active;
623 
624 	ints_active = 0;
625 	if (MFP->mf_imrb & IB_AINT) {
626 		ints_active   = 1;
627 		MFP->mf_imrb &= ~IB_AINT;
628 	}
629 	for (;;) {
630 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
631 			;	/* Wait for key	*/
632 		if (KBD->ac_cs & (A_OE|A_PE)) {
633 			code = KBD->ac_da;	/* Silently ignore errors */
634 			continue;
635 		}
636 		code = KBD->ac_da;
637 #if NITE>0
638 		if (!kbd_do_modifier(code))
639 #endif
640 			break;
641 	}
642 
643 	if (ints_active) {
644 		MFP->mf_iprb  = (u_int8_t)~IB_AINT;
645 		MFP->mf_imrb |=  IB_AINT;
646 	}
647 
648 	splx (s);
649 	return code;
650 }
651 
652 /*
653  * Write a command to the keyboard in 'polled' mode.
654  */
655 static int
656 kbd_write_poll(cmd, len)
657 u_char	*cmd;
658 int	len;
659 {
660 	int	timeout;
661 
662 	while (len-- > 0) {
663 		KBD->ac_da = *cmd++;
664 		for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
665 			delay(10);
666 		if (!(KBD->ac_cs & A_TXRDY))
667 			return (0);
668 	}
669 	return (1);
670 }
671 
672 /*
673  * Write a command to the keyboard. Return when command is send.
674  */
675 void
676 kbd_write(cmd, len)
677 u_char	*cmd;
678 int	len;
679 {
680 	struct kbd_softc	*k = &kbd_softc;
681 	int			sps;
682 
683 	/*
684 	 * Get to splhigh, 'real' interrupts arrive at spl6!
685 	 */
686 	sps = splhigh();
687 
688 	/*
689 	 * Make sure any privious write has ended...
690 	 */
691 	while (k->k_sendp != NULL)
692 		tsleep((caddr_t)&k->k_sendp, TTOPRI, "kbd_write1", 0);
693 
694 	/*
695 	 * If the KBD-acia is not currently busy, send the first
696 	 * character now.
697 	 */
698 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
699 	if (KBD->ac_cs & A_TXRDY) {
700 		KBD->ac_da = *cmd++;
701 		len--;
702 	}
703 
704 	/*
705 	 * If we're not yet done, wait until all characters are send.
706 	 */
707 	if (len > 0) {
708 		k->k_sendp    = cmd;
709 		k->k_send_cnt = len;
710 		tsleep((caddr_t)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
711 	}
712 	splx(sps);
713 
714 	/*
715 	 * Wakeup all procs waiting for us.
716 	 */
717 	wakeup((caddr_t)&k->k_sendp);
718 }
719 
720 /*
721  * Setup softc-fields to assemble a keyboard package.
722  */
723 static void
724 kbd_pkg_start(kp, msg_start)
725 struct kbd_softc *kp;
726 u_char		 msg_start;
727 {
728 	kp->k_pkg_idx    = 1;
729 	kp->k_package[0] = msg_start;
730 	switch (msg_start) {
731 		case 0xf6:
732 			kp->k_pkg_type = KBD_MEM_PKG;
733 			kp->k_pkg_size = 8;
734 			break;
735 		case 0xf7:
736 			kp->k_pkg_type = KBD_AMS_PKG;
737 			kp->k_pkg_size = 6;
738 			break;
739 		case 0xf8:
740 		case 0xf9:
741 		case 0xfa:
742 		case 0xfb:
743 			kp->k_pkg_type = KBD_RMS_PKG;
744 			kp->k_pkg_size = 3;
745 			break;
746 		case 0xfc:
747 			kp->k_pkg_type = KBD_CLK_PKG;
748 			kp->k_pkg_size = 7;
749 			break;
750 		case 0xfe:
751 			kp->k_pkg_type = KBD_JOY0_PKG;
752 			kp->k_pkg_size = 2;
753 			break;
754 		case 0xff:
755 			kp->k_pkg_type = KBD_JOY1_PKG;
756 			kp->k_pkg_size = 2;
757 			break;
758 		default:
759 			printf("kbd: Unknown packet 0x%x\n", msg_start);
760 			break;
761 	}
762 }
763 
764 #if NITE>0
765 /*
766  * Modifier processing
767  */
768 static int
769 kbd_do_modifier(code)
770 u_char	code;
771 {
772 	u_char	up, mask;
773 
774 	up   = KBD_RELEASED(code);
775 	mask = 0;
776 
777 	switch(KBD_SCANCODE(code)) {
778 		case KBD_LEFT_SHIFT:
779 			mask = KBD_MOD_LSHIFT;
780 			break;
781 		case KBD_RIGHT_SHIFT:
782 			mask = KBD_MOD_RSHIFT;
783 			break;
784 		case KBD_CTRL:
785 			mask = KBD_MOD_CTRL;
786 			break;
787 		case KBD_ALT:
788 			mask = KBD_MOD_ALT;
789 			break;
790 		case KBD_CAPS_LOCK:
791 			/* CAPSLOCK is a toggle */
792 			if(!up)
793 				kbd_modifier ^= KBD_MOD_CAPS;
794 			return 1;
795 	}
796 	if(mask) {
797 		if(up)
798 			kbd_modifier &= ~mask;
799 		else
800 			kbd_modifier |= mask;
801 		return 1;
802 	}
803 	return 0;
804 }
805 #endif
806 
807 #if NWSKBD>0
808 /*
809  * These are the callback functions that are passed to wscons.
810  * They really don't do anything worth noting, just call the
811  * other functions above.
812  */
813 
814 static int
815 kbd_enable(void *c, int on)
816 {
817         /* Wonder what this is supposed to do... */
818 	return 0;
819 }
820 
821 static void
822 kbd_set_leds(void *c, int leds)
823 {
824         /* we can not set the leds */
825 }
826 
827 static int
828 kbd_ioctl(void *c, u_long cmd, caddr_t data, int flag, struct proc *p)
829 {
830 	struct wskbd_bell_data *kd;
831 
832 	switch (cmd)
833 	{
834 	case WSKBDIO_COMPLEXBELL:
835 		kd = (struct wskbd_bell_data *)data;
836 		kbd_bell(0, kd->pitch, kd->period, kd->volume);
837 		return 0;
838 	case WSKBDIO_SETLEDS:
839 		return 0;
840 	case WSKBDIO_GETLEDS:
841 		*(int*)data = 0;
842 		return 0;
843 	case WSKBDIO_GTYPE:
844 		*(u_int*)data = WSKBD_TYPE_ATARI;
845 		return 0;
846 	}
847 
848 	/*
849 	 * We are supposed to return EPASSTHROUGH to wscons if we didn't
850 	 * understand.
851 	 */
852 	return (EPASSTHROUGH);
853 }
854 
855 static void
856 kbd_getc(void *c, u_int *type, int *data)
857 {
858 	int key;
859 	key = kbdgetcn();
860 
861 	*data = KBD_SCANCODE(key);
862 	*type = KBD_RELEASED(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
863 }
864 
865 static void
866 kbd_pollc(void *c, int on)
867 {
868         kbd_softc.k_pollingmode = on;
869 }
870 
871 static void
872 kbd_bell(void *v, u_int pitch, u_int duration, u_int volume)
873 {
874         kbd_bell_sparms(volume, pitch, duration);
875 	kbdbell();
876 }
877 #endif /* NWSKBD */
878