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