xref: /netbsd-src/sys/arch/atari/dev/kbd.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: kbd.c,v 1.41 2010/04/13 11:31:11 tsutsui 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.41 2010/04/13 11:31:11 tsutsui 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(struct device *, struct device *, void *);
121 static int  kbdmatch(struct device *, struct cfdata *, 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(kbd, sizeof(struct device),
129     kbdmatch, kbdattach, NULL, NULL);
130 
131 const struct cdevsw kbd_cdevsw = {
132 	kbdopen, kbdclose, kbdread, nowrite, kbdioctl,
133 	nostop, notty, kbdpoll, nommap, kbdkqfilter,
134 };
135 
136 #if NWSKBD>0
137 /* accessops */
138 static int	kbd_enable(void *, int);
139 static void	kbd_set_leds(void *, int);
140 static int	kbd_ioctl(void *, u_long, void *, int, struct lwp *);
141 
142 /* console ops */
143 static void	kbd_getc(void *, u_int *, int *);
144 static void	kbd_pollc(void *, int);
145 static void	kbd_bell(void *, u_int, u_int, u_int);
146 
147 static struct wskbd_accessops kbd_accessops = {
148 	kbd_enable,
149 	kbd_set_leds,
150 	kbd_ioctl
151 };
152 
153 static struct wskbd_consops kbd_consops = {
154         kbd_getc,
155 	kbd_pollc,
156 	kbd_bell
157 };
158 
159 /* Pointer to keymaps. */
160 static struct wskbd_mapdata kbd_mapdata = {
161         atarikbd_keydesctab,
162 	KB_US
163 };
164 #endif /* WSKBD */
165 
166 /*ARGSUSED*/
167 static	int
168 kbdmatch(struct device *pdp, struct cfdata *cfp, void *auxp)
169 {
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 	const uint8_t kbd_rst[]  = { 0x80, 0x01 };
182 	const uint8_t 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 	kbd_softc.k_sicookie = softint_establish(SOFTINT_SERIAL, kbdsoft, NULL);
223 
224 #if NWSKBD>0
225 	if (dp != NULL) {
226 		/*
227 		 * Try to attach the wskbd.
228 		 */
229 		struct wskbddev_attach_args waa;
230 
231 		/* Maybe should be done before this?... */
232 		wskbd_cnattach(&kbd_consops, NULL, &kbd_mapdata);
233 
234 		waa.console = 1;
235 		waa.keymap = &kbd_mapdata;
236 		waa.accessops = &kbd_accessops;
237 		waa.accesscookie = NULL;
238 		kbd_softc.k_wskbddev = config_found(dp, &waa, wskbddevprint);
239 
240 		kbd_softc.k_pollingmode = 0;
241 
242 		kbdenable();
243 	}
244 #endif /* WSKBD */
245 }
246 
247 void
248 kbdenable(void)
249 {
250 	int s, code;
251 
252 	s = spltty();
253 
254 	/*
255 	 * Clear error conditions...
256 	 */
257 	while (KBD->ac_cs & (A_IRQ|A_RXRDY))
258 		code = KBD->ac_da;
259 	/*
260 	 * Enable interrupts from MFP
261 	 */
262 	MFP->mf_iprb  = (u_int8_t)~IB_AINT;
263 	MFP->mf_ierb |= IB_AINT;
264 	MFP->mf_imrb |= IB_AINT;
265 
266 	kbd_softc.k_event_mode   = 0;
267 	kbd_softc.k_events.ev_io = 0;
268 	kbd_softc.k_pkg_size     = 0;
269 	splx(s);
270 }
271 
272 int kbdopen(dev_t dev, int flags, int mode, struct lwp *l)
273 {
274 
275 	if (kbd_softc.k_events.ev_io)
276 		return EBUSY;
277 
278 	kbd_softc.k_events.ev_io = l->l_proc;
279 	ev_init(&kbd_softc.k_events);
280 	return 0;
281 }
282 
283 int
284 kbdclose(dev_t dev, int flags, int mode, struct lwp *l)
285 {
286 
287 	/* Turn off event mode, dump the queue */
288 	kbd_softc.k_event_mode = 0;
289 	ev_fini(&kbd_softc.k_events);
290 	kbd_softc.k_events.ev_io = NULL;
291 	return 0;
292 }
293 
294 int
295 kbdread(dev_t dev, struct uio *uio, int flags)
296 {
297 
298 	return ev_read(&kbd_softc.k_events, uio, flags);
299 }
300 
301 int
302 kbdioctl(dev_t dev, u_long cmd, register void *data, int flag, struct lwp *l)
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 lwp *l)
362 {
363 
364 	return ev_poll(&kbd_softc.k_events, events, l);
365 }
366 
367 int
368 kbdkqfilter(dev_t dev, struct knote *kn)
369 {
370 
371 	return ev_kqfilter(&kbd_softc.k_events, kn);
372 }
373 
374 /*
375  * Keyboard interrupt handler called straight from MFP at spl6.
376  */
377 void
378 kbdintr(int sr)
379 	/* sr:	 sr at time of interrupt	*/
380 {
381 	int	code;
382 	int	got_char = 0;
383 
384 	/*
385 	 * There may be multiple keys available. Read them all.
386 	 */
387 	while (KBD->ac_cs & (A_RXRDY|A_OE|A_PE)) {
388 		got_char = 1;
389 		if (KBD->ac_cs & (A_OE|A_PE)) {
390 			code = KBD->ac_da;	/* Silently ignore errors */
391 			continue;
392 		}
393 		kbd_ring[kbd_rbput++ & KBD_RING_MASK] = KBD->ac_da;
394 	}
395 
396 	/*
397 	 * If characters are waiting for transmit, send them.
398 	 */
399 	if ((kbd_softc.k_soft_cs & A_TXINT) && (KBD->ac_cs & A_TXRDY)) {
400 		if (kbd_softc.k_sendp != NULL)
401 			KBD->ac_da = *kbd_softc.k_sendp++;
402 		if (--kbd_softc.k_send_cnt <= 0) {
403 			/*
404 			 * The total package has been transmitted,
405 			 * wakeup anyone waiting for it.
406 			 */
407 			KBD->ac_cs = (kbd_softc.k_soft_cs &= ~A_TXINT);
408 			kbd_softc.k_sendp    = NULL;
409 			kbd_softc.k_send_cnt = 0;
410 			wakeup((void *)&kbd_softc.k_send_cnt);
411 		}
412 	}
413 
414 	/*
415 	 * Activate software-level to handle possible input.
416 	 */
417 	if (got_char)
418 		softint_schedule(kbd_softc.k_sicookie);
419 }
420 
421 /*
422  * Keyboard soft interrupt handler
423  */
424 static void
425 kbdsoft(void *junk1)
426 {
427 	int s;
428 	uint8_t code;
429 	struct kbd_softc *k = &kbd_softc;
430 	struct firm_event *fe;
431 	int put, get, n;
432 
433 	get      = kbd_rbget;
434 
435 	for (;;) {
436 		n = kbd_rbput;
437 		if (get == n) /* We're done	*/
438 			break;
439 		n -= get;
440 		if (n > KBD_RING_SIZE) { /* Ring buffer overflow	*/
441 			get += n - KBD_RING_SIZE;
442 			n    = KBD_RING_SIZE;
443 		}
444 		while (--n >= 0) {
445 			code = kbd_ring[get++ & KBD_RING_MASK];
446 
447 			/*
448 			 * If collecting a package, stuff it in and
449 			 * continue.
450 			 */
451 			if (k->k_pkg_size && (k->k_pkg_idx < k->k_pkg_size)) {
452 			    k->k_package[k->k_pkg_idx++] = code;
453 			    if (k->k_pkg_idx == k->k_pkg_size) {
454 				/*
455 				 * Package is complete.
456 				 */
457 				switch (k->k_pkg_type) {
458 #if NMOUSE > 0
459 				    case KBD_AMS_PKG:
460 				    case KBD_RMS_PKG:
461 				    case KBD_JOY1_PKG:
462 			 		mouse_soft((REL_MOUSE *)k->k_package,
463 					    k->k_pkg_size, k->k_pkg_type);
464 #endif /* NMOUSE */
465 				}
466 				k->k_pkg_size = 0;
467 			    }
468 			    continue;
469 			}
470 			/*
471 			 * If this is a package header, init pkg. handling.
472 			 */
473 			if (!KBD_IS_KEY(code)) {
474 				kbd_pkg_start(k, code);
475 				continue;
476 			}
477 #if NWSKBD>0
478 			/*
479 			 * If we have attached a wskbd and not in polling mode
480 			 * and nobody has opened us directly, then send the
481 			 * keystroke to the wskbd.
482 			 */
483 
484 			if (kbd_softc.k_pollingmode == 0
485 			    && kbd_softc.k_wskbddev != NULL
486 			    && k->k_event_mode == 0) {
487 				wskbd_input(kbd_softc.k_wskbddev,
488 					    KBD_RELEASED(code) ?
489 					    WSCONS_EVENT_KEY_UP :
490 					    WSCONS_EVENT_KEY_DOWN,
491 					    KBD_SCANCODE(code));
492 				continue;
493 			}
494 #endif /* NWSKBD */
495 #if NITE>0
496 			if (kbd_do_modifier(code) && !k->k_event_mode)
497 				continue;
498 #endif
499 
500 			/*
501 			 * if not in event mode, deliver straight to ite to
502 			 * process key stroke
503 			 */
504 			if (!k->k_event_mode) {
505 				/* Gets to spltty() by itself	*/
506 #if NITE>0
507 				ite_filter(code, ITEFILT_TTY);
508 #endif
509 				continue;
510 			}
511 
512 			/*
513 			 * Keyboard is generating events.  Turn this keystroke
514 			 * into an event and put it in the queue.  If the queue
515 			 * is full, the keystroke is lost (sorry!).
516 			 */
517 			s = spltty();
518 			put = k->k_events.ev_put;
519 			fe  = &k->k_events.ev_q[put];
520 			put = (put + 1) % EV_QSIZE;
521 			if (put == k->k_events.ev_get) {
522 				log(LOG_WARNING,
523 				    "keyboard event queue overflow\n");
524 				splx(s);
525 				continue;
526 			}
527 			fe->id    = KBD_SCANCODE(code);
528 			fe->value = KBD_RELEASED(code) ? VKEY_UP : VKEY_DOWN;
529 			firm_gettime(fe);
530 			k->k_events.ev_put = put;
531 			EV_WAKEUP(&k->k_events);
532 			splx(s);
533 		}
534 		kbd_rbget = get;
535 	}
536 }
537 
538 static	uint8_t sound[] = {
539 	0xA8, 0x01, 0xA9, 0x01, 0xAA, 0x01, 0x00,
540 	0xF8, 0x10, 0x10, 0x10, 0x00, 0x20, 0x03
541 };
542 
543 void
544 kbdbell(void)
545 {
546 	register int	i, sps;
547 
548 	sps = splhigh();
549 	for (i = 0; i < sizeof(sound); i++) {
550 		YM2149->sd_selr = i;
551 		YM2149->sd_wdat = sound[i];
552 	}
553 	splx(sps);
554 }
555 
556 
557 /*
558  * Set the parameters of the 'default' beep.
559  */
560 
561 #define KBDBELLCLOCK	125000	/* 2MHz / 16 */
562 #define KBDBELLDURATION	128	/* 256 / 2MHz */
563 
564 void
565 kbd_bell_gparms(u_int *volume, u_int *pitch, u_int *duration)
566 {
567 	u_int	tmp;
568 
569 	tmp = sound[11] | (sound[12] << 8);
570 	*duration = (tmp * KBDBELLDURATION) / 1000;
571 
572 	tmp = sound[0] | (sound[1] << 8);
573 	*pitch = KBDBELLCLOCK / tmp;
574 
575 	*volume = 0;
576 }
577 
578 void
579 kbd_bell_sparms(u_int volume, u_int pitch, u_int duration)
580 {
581 	u_int	f, t;
582 
583 	f = pitch > 10 ? pitch : 10;	/* minimum pitch */
584 	if (f > 20000)
585 		f = 20000;		/* maximum pitch */
586 
587 	f = KBDBELLCLOCK / f;
588 
589 	t = (duration * 1000) / KBDBELLDURATION;
590 
591 	sound[ 0] = f & 0xff;
592 	sound[ 1] = (f >> 8) & 0xf;
593 	f -= 1;
594 	sound[ 2] = f & 0xff;
595 	sound[ 3] = (f >> 8) & 0xf;
596 	f += 2;
597 	sound[ 4] = f & 0xff;
598 	sound[ 5] = (f >> 8) & 0xf;
599 
600 	sound[11] = t & 0xff;
601 	sound[12] = (t >> 8) & 0xff;
602 
603 	sound[13] = 0x03;
604 }
605 
606 int
607 kbdgetcn(void)
608 {
609 	uint8_t code;
610 	int s = spltty();
611 	int ints_active;
612 
613 	ints_active = 0;
614 	if (MFP->mf_imrb & IB_AINT) {
615 		ints_active   = 1;
616 		MFP->mf_imrb &= ~IB_AINT;
617 	}
618 	for (;;) {
619 		while (!((KBD->ac_cs & (A_IRQ|A_RXRDY)) == (A_IRQ|A_RXRDY)))
620 			;	/* Wait for key	*/
621 		if (KBD->ac_cs & (A_OE|A_PE)) {
622 			code = KBD->ac_da;	/* Silently ignore errors */
623 			continue;
624 		}
625 		code = KBD->ac_da;
626 #if NITE>0
627 		if (!kbd_do_modifier(code))
628 #endif
629 			break;
630 	}
631 
632 	if (ints_active) {
633 		MFP->mf_iprb  = (uint8_t)~IB_AINT;
634 		MFP->mf_imrb |=  IB_AINT;
635 	}
636 
637 	splx(s);
638 	return code;
639 }
640 
641 /*
642  * Write a command to the keyboard in 'polled' mode.
643  */
644 static int
645 kbd_write_poll(const uint8_t *cmd, int len)
646 {
647 	int	timeout;
648 
649 	while (len-- > 0) {
650 		KBD->ac_da = *cmd++;
651 		for (timeout = 100; !(KBD->ac_cs & A_TXRDY); timeout--)
652 			delay(10);
653 		if ((KBD->ac_cs & A_TXRDY) == 0)
654 			return 0;
655 	}
656 	return 1;
657 }
658 
659 /*
660  * Write a command to the keyboard. Return when command is send.
661  */
662 void
663 kbd_write(const uint8_t *cmd, int len)
664 {
665 	struct kbd_softc	*k = &kbd_softc;
666 	int			sps;
667 
668 	/*
669 	 * Get to splhigh, 'real' interrupts arrive at spl6!
670 	 */
671 	sps = splhigh();
672 
673 	/*
674 	 * Make sure any privious write has ended...
675 	 */
676 	while (k->k_sendp != NULL)
677 		tsleep((void *)&k->k_sendp, TTOPRI, "kbd_write1", 0);
678 
679 	/*
680 	 * If the KBD-acia is not currently busy, send the first
681 	 * character now.
682 	 */
683 	KBD->ac_cs = (k->k_soft_cs |= A_TXINT);
684 	if (KBD->ac_cs & A_TXRDY) {
685 		KBD->ac_da = *cmd++;
686 		len--;
687 	}
688 
689 	/*
690 	 * If we're not yet done, wait until all characters are send.
691 	 */
692 	if (len > 0) {
693 		k->k_sendp    = cmd;
694 		k->k_send_cnt = len;
695 		tsleep((void *)&k->k_send_cnt, TTOPRI, "kbd_write2", 0);
696 	}
697 	splx(sps);
698 
699 	/*
700 	 * Wakeup all procs waiting for us.
701 	 */
702 	wakeup((void *)&k->k_sendp);
703 }
704 
705 /*
706  * Setup softc-fields to assemble a keyboard package.
707  */
708 static void
709 kbd_pkg_start(struct kbd_softc *kp, uint8_t msg_start)
710 {
711 
712 	kp->k_pkg_idx    = 1;
713 	kp->k_package[0] = msg_start;
714 	switch (msg_start) {
715 		case 0xf6:
716 			kp->k_pkg_type = KBD_MEM_PKG;
717 			kp->k_pkg_size = 8;
718 			break;
719 		case 0xf7:
720 			kp->k_pkg_type = KBD_AMS_PKG;
721 			kp->k_pkg_size = 6;
722 			break;
723 		case 0xf8:
724 		case 0xf9:
725 		case 0xfa:
726 		case 0xfb:
727 			kp->k_pkg_type = KBD_RMS_PKG;
728 			kp->k_pkg_size = 3;
729 			break;
730 		case 0xfc:
731 			kp->k_pkg_type = KBD_CLK_PKG;
732 			kp->k_pkg_size = 7;
733 			break;
734 		case 0xfe:
735 			kp->k_pkg_type = KBD_JOY0_PKG;
736 			kp->k_pkg_size = 2;
737 			break;
738 		case 0xff:
739 			kp->k_pkg_type = KBD_JOY1_PKG;
740 			kp->k_pkg_size = 2;
741 			break;
742 		default:
743 			printf("kbd: Unknown packet 0x%x\n", msg_start);
744 			break;
745 	}
746 }
747 
748 #if NITE > 0
749 /*
750  * Modifier processing
751  */
752 static int
753 kbd_do_modifier(uint8_t code)
754 {
755 	uint8_t up, mask;
756 
757 	up   = KBD_RELEASED(code);
758 	mask = 0;
759 
760 	switch (KBD_SCANCODE(code)) {
761 		case KBD_LEFT_SHIFT:
762 			mask = KBD_MOD_LSHIFT;
763 			break;
764 		case KBD_RIGHT_SHIFT:
765 			mask = KBD_MOD_RSHIFT;
766 			break;
767 		case KBD_CTRL:
768 			mask = KBD_MOD_CTRL;
769 			break;
770 		case KBD_ALT:
771 			mask = KBD_MOD_ALT;
772 			break;
773 		case KBD_CAPS_LOCK:
774 			/* CAPSLOCK is a toggle */
775 			if (!up)
776 				kbd_modifier ^= KBD_MOD_CAPS;
777 			return 1;
778 	}
779 	if (mask) {
780 		if (up)
781 			kbd_modifier &= ~mask;
782 		else
783 			kbd_modifier |= mask;
784 		return 1;
785 	}
786 	return 0;
787 }
788 #endif
789 
790 #if NWSKBD>0
791 /*
792  * These are the callback functions that are passed to wscons.
793  * They really don't do anything worth noting, just call the
794  * other functions above.
795  */
796 
797 static int
798 kbd_enable(void *c, int on)
799 {
800 
801         /* Wonder what this is supposed to do... */
802 	return 0;
803 }
804 
805 static void
806 kbd_set_leds(void *c, int leds)
807 {
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 lwp *p)
814 {
815 	struct wskbd_bell_data *kd;
816 
817 	switch (cmd) {
818 	case WSKBDIO_COMPLEXBELL:
819 		kd = (struct wskbd_bell_data *)data;
820 		kbd_bell(0, kd->pitch, kd->period, kd->volume);
821 		return 0;
822 	case WSKBDIO_SETLEDS:
823 		return 0;
824 	case WSKBDIO_GETLEDS:
825 		*(int *)data = 0;
826 		return 0;
827 	case WSKBDIO_GTYPE:
828 		*(u_int *)data = WSKBD_TYPE_ATARI;
829 		return 0;
830 	}
831 
832 	/*
833 	 * We are supposed to return EPASSTHROUGH to wscons if we didn't
834 	 * understand.
835 	 */
836 	return EPASSTHROUGH;
837 }
838 
839 static void
840 kbd_getc(void *c, u_int *type, int *data)
841 {
842 	int key;
843 	key = kbdgetcn();
844 
845 	*data = KBD_SCANCODE(key);
846 	*type = KBD_RELEASED(key) ? WSCONS_EVENT_KEY_UP : WSCONS_EVENT_KEY_DOWN;
847 }
848 
849 static void
850 kbd_pollc(void *c, int on)
851 {
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 
860         kbd_bell_sparms(volume, pitch, duration);
861 	kbdbell();
862 }
863 #endif /* NWSKBD */
864