xref: /original-bsd/sys/sparc/dev/kbd.c (revision 3705696b)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This software was developed by the Computer Systems Engineering group
6  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7  * contributed to Berkeley.
8  *
9  * All advertising materials mentioning features or use of this software
10  * must display the following acknowledgement:
11  *	This product includes software developed by the University of
12  *	California, Lawrence Berkeley Laboratory.
13  *
14  * %sccs.include.redist.c%
15  *
16  *	@(#)kbd.c	8.1 (Berkeley) 06/11/93
17  *
18  * from: $Header: kbd.c,v 1.16 92/11/26 01:28:44 torek Exp $ (LBL)
19  */
20 
21 /*
22  * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
23  * [yet?]).  Translates incoming bytes to ASCII or to `firm_events' and
24  * passes them up to the appropriate reader.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/conf.h>
29 #include <sys/device.h>
30 #include <sys/ioctl.h>
31 #include <sys/kernel.h>
32 #include <sys/proc.h>
33 #include <sys/syslog.h>
34 #include <sys/systm.h>
35 #include <sys/tty.h>
36 
37 #include <machine/autoconf.h>
38 
39 #include <sparc/dev/vuid_event.h>
40 #include <sparc/dev/event_var.h>
41 #include <sparc/dev/kbd.h>
42 #include <sparc/dev/kbio.h>
43 
44 /*
45  * Sun keyboard definitions (from Sprite).
46  * These apply to type 2, 3 and 4 keyboards.
47  */
48 #define	KEY_CODE(c)	((c) & KBD_KEYMASK)	/* keyboard code index */
49 #define	KEY_UP(c)	((c) & KBD_UP)		/* true => key went up */
50 
51 /*
52  * Each KEY_CODE(x) can be translated via the tables below.
53  * The result is either a valid ASCII value in [0..0x7f] or is one
54  * of the following `magic' values saying something interesting
55  * happened.  If LSHIFT or RSHIFT has changed state the next
56  * lookup should come from the appropriate table; if ALLUP is
57  * sent all keys (including both shifts and the control key) are
58  * now up, and the next byte is the keyboard ID code.
59  *
60  * These tables ignore all function keys (on the theory that if you
61  * want these keys, you should use a window system).  Note that
62  * `caps lock' is just mapped as `ignore' (so there!). (Only the
63  * type 3 and 4 keyboards have a caps lock key anyway.)
64  */
65 #define	KEY_MAGIC	0x80		/* flag => magic value */
66 #define	KEY_IGNORE	0x80
67 #define	KEY_L1		KEY_IGNORE
68 #define	KEY_CAPSLOCK	KEY_IGNORE
69 #define	KEY_LSHIFT	0x81
70 #define	KEY_RSHIFT	0x82
71 #define	KEY_CONTROL	0x83
72 #define	KEY_ALLUP	0x84		/* all keys are now up; also reset */
73 
74 /*
75  * Decode tables for type 2, 3, and 4 keyboards
76  * (stolen from Sprite; see also kbd.h).
77  */
78 static const u_char kbd_unshifted[] = {
79 /*   0 */	KEY_IGNORE,	KEY_L1,		KEY_IGNORE,	KEY_IGNORE,
80 /*   4 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
81 /*   8 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
82 /*  12 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
83 /*  16 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
84 /*  20 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
85 /*  24 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
86 /*  28 */	KEY_IGNORE,	'\033',		'1',		'2',
87 /*  32 */	'3',		'4',		'5',		'6',
88 /*  36 */	'7',		'8',		'9',		'0',
89 /*  40 */	'-',		'=',		'`',		'\b',
90 /*  44 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
91 /*  48 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
92 /*  52 */	KEY_IGNORE,	'\t',		'q',		'w',
93 /*  56 */	'e',		'r',		't',		'y',
94 /*  60 */	'u',		'i',		'o',		'p',
95 /*  64 */	'[',		']',		'\177',		KEY_IGNORE,
96 /*  68 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
97 /*  72 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
98 /*  76 */	KEY_CONTROL,	'a',		's',		'd',
99 /*  80 */	'f',		'g',		'h',		'j',
100 /*  84 */	'k',		'l',		';',		'\'',
101 /*  88 */	'\\',		'\r',		KEY_IGNORE,	KEY_IGNORE,
102 /*  92 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
103 /*  96 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_LSHIFT,
104 /* 100 */	'z',		'x',		'c',		'v',
105 /* 104 */	'b',		'n',		'm',		',',
106 /* 108 */	'.',		'/',		KEY_RSHIFT,	'\n',
107 /* 112 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
108 /* 116 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_CAPSLOCK,
109 /* 120 */	KEY_IGNORE,	' ',		KEY_IGNORE,	KEY_IGNORE,
110 /* 124 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_ALLUP,
111 };
112 
113 static const u_char kbd_shifted[] = {
114 /*   0 */	KEY_IGNORE,	KEY_L1,		KEY_IGNORE,	KEY_IGNORE,
115 /*   4 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
116 /*   8 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
117 /*  12 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
118 /*  16 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
119 /*  20 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
120 /*  24 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
121 /*  28 */	KEY_IGNORE,	'\033',		'!',		'@',
122 /*  32 */	'#',		'$',		'%',		'^',
123 /*  36 */	'&',		'*',		'(',		')',
124 /*  40 */	'_',		'+',		'~',		'\b',
125 /*  44 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
126 /*  48 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
127 /*  52 */	KEY_IGNORE,	'\t',		'Q',		'W',
128 /*  56 */	'E',		'R',		'T',		'Y',
129 /*  60 */	'U',		'I',		'O',		'P',
130 /*  64 */	'{',		'}',		'\177',		KEY_IGNORE,
131 /*  68 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
132 /*  72 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
133 /*  76 */	KEY_CONTROL,	'A',		'S',		'D',
134 /*  80 */	'F',		'G',		'H',		'J',
135 /*  84 */	'K',		'L',		':',		'"',
136 /*  88 */	'|',		'\r',		KEY_IGNORE,	KEY_IGNORE,
137 /*  92 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
138 /*  96 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_LSHIFT,
139 /* 100 */	'Z',		'X',		'C',		'V',
140 /* 104 */	'B',		'N',		'M',		'<',
141 /* 108 */	'>',		'?',		KEY_RSHIFT,	'\n',
142 /* 112 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,
143 /* 116 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_CAPSLOCK,
144 /* 120 */	KEY_IGNORE,	' ',		KEY_IGNORE,	KEY_IGNORE,
145 /* 124 */	KEY_IGNORE,	KEY_IGNORE,	KEY_IGNORE,	KEY_ALLUP,
146 };
147 
148 /*
149  * We need to remember the state of the keyboard's shift and control
150  * keys, and we need a per-type translation table.
151  */
152 struct kbd_state {
153 	const u_char *kbd_unshifted;	/* unshifted keys */
154 	const u_char *kbd_shifted;	/* shifted keys */
155 	const u_char *kbd_cur;	/* current keys (either of the preceding) */
156 	union {
157 		char	c[2];	/* left and right shift keys */
158 		short	s;	/* true => either shift key */
159 	} kbd_shift;
160 #define	kbd_lshift	kbd_shift.c[0]
161 #define	kbd_rshift	kbd_shift.c[1]
162 #define	kbd_anyshift	kbd_shift.s
163 	char	kbd_control;	/* true => ctrl down */
164 	char	kbd_click;	/* true => keyclick enabled */
165 	char	kbd_takeid;	/* take next byte as ID */
166 	u_char	kbd_id;		/* a place to store the ID */
167 };
168 
169 /*
170  * Keyboard driver state.  The ascii and kbd links go up and down and
171  * we just sit in the middle doing translation.  Note that it is possible
172  * to get just one of the two links, in which case /dev/kbd is unavailable.
173  * The downlink supplies us with `internal' open and close routines which
174  * will enable dataflow across the downlink.  We promise to call open when
175  * we are willing to take keystrokes, and to call close when we are not.
176  * If /dev/kbd is not the console tty input source, we do this whenever
177  * /dev/kbd is in use; otherwise we just leave it open forever.
178  */
179 struct kbd_softc {
180 	struct	tty *k_cons;		/* uplink for ASCII data to console */
181 	struct	tty *k_kbd;		/* downlink for output to keyboard */
182 	void	(*k_open) __P((struct tty *));	/* enable dataflow */
183 	void	(*k_close) __P((struct tty *));	/* disable dataflow */
184 	int	k_evmode;		/* set if we should produce events */
185 	struct	kbd_state k_state;	/* ASCII decode state */
186 	struct	evvar k_events;		/* event queue state */
187 } kbd_softc;
188 
189 /* Prototypes */
190 void	kbd_ascii(struct tty *);
191 void	kbd_serial(struct tty *, void (*)(), void (*)());
192 static	void kbd_getid(void *);
193 void	kbd_reset(struct kbd_state *);
194 static	int kbd_translate(int, struct kbd_state *);
195 void	kbd_rint(int);
196 int	kbdopen(dev_t, int, int, struct proc *);
197 int	kbdclose(dev_t, int, int, struct proc *);
198 int	kbdread(dev_t, struct uio *, int);
199 int	kbdwrite(dev_t, struct uio *, int);
200 int	kbdioctl(dev_t, int, caddr_t, int, struct proc *);
201 int	kbdselect(dev_t, int, struct proc *);
202 int	kbd_docmd(int, int);
203 
204 /*
205  * Attach the console keyboard ASCII (up-link) interface.
206  * This happens before kbd_serial.
207  */
208 void
209 kbd_ascii(struct tty *tp)
210 {
211 
212 	kbd_softc.k_cons = tp;
213 }
214 
215 /*
216  * Attach the console keyboard serial (down-link) interface.
217  * We pick up the initial keyboard clock state here as well.
218  */
219 void
220 kbd_serial(struct tty *tp, void (*iopen)(), void (*iclose)())
221 {
222 	register struct kbd_softc *k;
223 	register char *cp;
224 
225 	k = &kbd_softc;
226 	k->k_kbd = tp;
227 	k->k_open = iopen;
228 	k->k_close = iclose;
229 
230 	cp = getpropstring(optionsnode, "keyboard-click?");
231 	if (cp && strcmp(cp, "true") == 0)
232 		k->k_state.kbd_click = 1;
233 
234 	if (k->k_cons) {
235 		/*
236 		 * We supply keys for /dev/console.  Before we can
237 		 * do so, we have to ``open'' the line.  We also need
238 		 * the type, got by sending a RESET down the line ...
239 		 * but clists are not yet set up, so we use a timeout
240 		 * to try constantly until we can get the ID.  (gag)
241 		 */
242 		(*iopen)(tp);		/* never to be closed */
243 		kbd_getid(NULL);
244 	}
245 }
246 
247 /*
248  * Initial keyboard reset, to obtain ID and thus a translation table.
249  * We have to try again and again until the tty subsystem works.
250  */
251 static void
252 kbd_getid(void *arg)
253 {
254 	register struct kbd_softc *k;
255 	register struct tty *tp;
256 	register int retry;
257 	extern int cold;		/* XXX */
258 
259 	k = &kbd_softc;
260 	if (k->k_state.kbd_cur != NULL)
261 		return;
262 	tp = k->k_kbd;
263 	if (cold || ttyoutput(KBD_CMD_RESET, tp) >= 0)
264 		retry = 1;
265 	else {
266 		(*tp->t_oproc)(tp);
267 		retry = 2 * hz;
268 	}
269 	timeout(kbd_getid, NULL, retry);
270 }
271 
272 void
273 kbd_reset(register struct kbd_state *ks)
274 {
275 	/*
276 	 * On first identification, wake up anyone waiting for type
277 	 * and set up the table pointers.
278 	 */
279 	if (ks->kbd_unshifted == NULL) {
280 		wakeup((caddr_t)ks);
281 		ks->kbd_unshifted = kbd_unshifted;
282 		ks->kbd_shifted = kbd_shifted;
283 		ks->kbd_cur = ks->kbd_unshifted;
284 	}
285 
286 	/* Restore keyclick, if necessary */
287 	switch (ks->kbd_id) {
288 
289 	case KB_SUN2:
290 		/* Type 2 keyboards don't support keyclick */
291 		break;
292 
293 	case KB_SUN3:
294 		/* Type 3 keyboards come up with keyclick on */
295 		if (!ks->kbd_click)
296 			(void) kbd_docmd(KBD_CMD_NOCLICK, 0);
297 		break;
298 
299 	case KB_SUN4:
300 		/* Type 4 keyboards come up with keyclick off */
301 		if (ks->kbd_click)
302 			(void) kbd_docmd(KBD_CMD_CLICK, 0);
303 		break;
304 	}
305 }
306 
307 /*
308  * Turn keyboard up/down codes into ASCII.
309  */
310 static int
311 kbd_translate(register int c, register struct kbd_state *ks)
312 {
313 	register int down;
314 
315 	if (ks->kbd_cur == NULL) {
316 		/*
317 		 * Do not know how to translate yet.
318 		 * We will find out when a RESET comes along.
319 		 */
320 		return (-1);
321 	}
322 	down = !KEY_UP(c);
323 	c = ks->kbd_cur[KEY_CODE(c)];
324 	if (c & KEY_MAGIC) {
325 		switch (c) {
326 
327 		case KEY_LSHIFT:
328 			ks->kbd_lshift = down;
329 			break;
330 
331 		case KEY_RSHIFT:
332 			ks->kbd_rshift = down;
333 			break;
334 
335 		case KEY_ALLUP:
336 			ks->kbd_anyshift = 0;
337 			ks->kbd_control = 0;
338 			break;
339 
340 		case KEY_CONTROL:
341 			ks->kbd_control = down;
342 			/* FALLTHROUGH */
343 
344 		case KEY_IGNORE:
345 			return (-1);
346 
347 		default:
348 			panic("kbd_translate");
349 		}
350 		if (ks->kbd_anyshift)
351 			ks->kbd_cur = ks->kbd_shifted;
352 		else
353 			ks->kbd_cur = ks->kbd_unshifted;
354 		return (-1);
355 	}
356 	if (!down)
357 		return (-1);
358 	if (ks->kbd_control) {
359 		/* control space and unshifted control atsign return null */
360 		if (c == ' ' || c == '2')
361 			return (0);
362 		/* unshifted control hat */
363 		if (c == '6')
364 			return ('^' & 0x1f);
365 		/* standard controls */
366 		if (c >= '@' && c < 0x7f)
367 			return (c & 0x1f);
368 	}
369 	return (c);
370 }
371 
372 void
373 kbd_rint(register int c)
374 {
375 	register struct kbd_softc *k = &kbd_softc;
376 	register struct firm_event *fe;
377 	register int put;
378 
379 	/*
380 	 * Reset keyboard after serial port overrun, so we can resynch.
381 	 * The printf below should be shortened and/or replaced with a
382 	 * call to log() after this is tested (and how will we test it?!).
383 	 */
384 	if (c & (TTY_FE|TTY_PE)) {
385 		printf("keyboard input parity or framing error (0x%x)\n", c);
386 		(void) ttyoutput(KBD_CMD_RESET, k->k_kbd);
387 		(*k->k_kbd->t_oproc)(k->k_kbd);
388 		return;
389 	}
390 
391 	/* Read the keyboard id if we read a KBD_RESET last time */
392 	if (k->k_state.kbd_takeid) {
393 		k->k_state.kbd_takeid = 0;
394 		k->k_state.kbd_id = c;
395 		kbd_reset(&k->k_state);
396 		return;
397 	}
398 
399 	/* If we have been reset, setup to grab the keyboard id next time */
400 	if (c == KBD_RESET) {
401 		k->k_state.kbd_takeid = 1;
402 		return;
403 	}
404 
405 	/*
406 	 * If /dev/kbd is not connected in event mode, but we are sending
407 	 * data to /dev/console, translate and send upstream.  Note that
408 	 * we will get this while opening /dev/kbd if it is not already
409 	 * open and we do not know its type.
410 	 */
411 	if (!k->k_evmode) {
412 		c = kbd_translate(c, &k->k_state);
413 		if (c >= 0 && k->k_cons != NULL)
414 			ttyinput(c, k->k_cons);
415 		return;
416 	}
417 
418 	/*
419 	 * IDLEs confuse the MIT X11R4 server badly, so we must drop them.
420 	 * This is bad as it means the server will not automatically resync
421 	 * on all-up IDLEs, but I did not drop them before, and the server
422 	 * goes crazy when it comes time to blank the screen....
423 	 */
424 	if (c == KBD_IDLE)
425 		return;
426 
427 	/*
428 	 * Keyboard is generating events.  Turn this keystroke into an
429 	 * event and put it in the queue.  If the queue is full, the
430 	 * keystroke is lost (sorry!).
431 	 */
432 	put = k->k_events.ev_put;
433 	fe = &k->k_events.ev_q[put];
434 	put = (put + 1) % EV_QSIZE;
435 	if (put == k->k_events.ev_get) {
436 		log(LOG_WARNING, "keyboard event queue overflow\n"); /* ??? */
437 		return;
438 	}
439 	fe->id = KEY_CODE(c);
440 	fe->value = KEY_UP(c) ? VKEY_UP : VKEY_DOWN;
441 	fe->time = time;
442 	k->k_events.ev_put = put;
443 	EV_WAKEUP(&k->k_events);
444 }
445 
446 int
447 kbdopen(dev_t dev, int flags, int mode, struct proc *p)
448 {
449 	int s, error;
450 
451 	if (kbd_softc.k_events.ev_io)
452 		return (EBUSY);
453 	kbd_softc.k_events.ev_io = p;
454 	/*
455 	 * If no console keyboard, tell the device to open up, maybe for
456 	 * the first time.  Then make sure we know what kind of keyboard
457 	 * it is.
458 	 */
459 	if (kbd_softc.k_cons == NULL)
460 		(*kbd_softc.k_open)(kbd_softc.k_kbd);
461 	error = 0;
462 	s = spltty();
463 	if (kbd_softc.k_state.kbd_cur == NULL) {
464 		(void) ttyoutput(KBD_CMD_RESET, kbd_softc.k_kbd);
465 		error = tsleep((caddr_t)&kbd_softc.k_state, PZERO | PCATCH,
466 		    devopn, hz);
467 		if (error == EWOULDBLOCK)	/* no response */
468 			error = ENXIO;
469 	}
470 	splx(s);
471 	if (error) {
472 		kbd_softc.k_events.ev_io = NULL;
473 		return (error);
474 	}
475 	ev_init(&kbd_softc.k_events);
476 	return (0);
477 }
478 
479 int
480 kbdclose(dev_t dev, int flags, int mode, struct proc *p)
481 {
482 
483 	/*
484 	 * Turn off event mode, dump the queue, and close the keyboard
485 	 * unless it is supplying console input.
486 	 */
487 	kbd_softc.k_evmode = 0;
488 	ev_fini(&kbd_softc.k_events);
489 	if (kbd_softc.k_cons == NULL)
490 		(*kbd_softc.k_close)(kbd_softc.k_kbd);
491 	kbd_softc.k_events.ev_io = NULL;
492 	return (0);
493 }
494 
495 int
496 kbdread(dev_t dev, struct uio *uio, int flags)
497 {
498 
499 	return (ev_read(&kbd_softc.k_events, uio, flags));
500 }
501 
502 /* this routine should not exist, but is convenient to write here for now */
503 int
504 kbdwrite(dev_t dev, struct uio *uio, int flags)
505 {
506 
507 	return (EOPNOTSUPP);
508 }
509 
510 int
511 kbdioctl(dev_t dev, int cmd, register caddr_t data, int flag, struct proc *p)
512 {
513 	register struct kbd_softc *k = &kbd_softc;
514 
515 	switch (cmd) {
516 
517 	case KIOCTRANS:
518 		if (*(int *)data == TR_UNTRANS_EVENT)
519 			return (0);
520 		break;
521 
522 	case KIOCGTRANS:
523 		/*
524 		 * Get translation mode
525 		 */
526 		*(int *)data = TR_UNTRANS_EVENT;
527 		return (0);
528 
529 	case KIOCGETKEY:
530 		if (((struct kiockey *)data)->kio_station == 118) {
531 			/*
532 			 * This is X11 asking if a type 3 keyboard is
533 			 * really a type 3 keyboard.  Say yes.
534 			 */
535 			((struct kiockey *)data)->kio_entry = HOLE;
536 			return (0);
537 		}
538 		break;
539 
540 	case KIOCCMD:
541 		/*
542 		 * ``unimplemented commands are ignored'' (blech)
543 		 * so cannot check return value from kbd_docmd
544 		 */
545 #ifdef notyet
546 		while (kbd_docmd(*(int *)data, 1) == ENOSPC) /*ERESTART?*/
547 			(void) sleep((caddr_t)&lbolt, TTOPRI);
548 #else
549 		(void) kbd_docmd(*(int *)data, 1);
550 #endif
551 		return (0);
552 
553 	case KIOCTYPE:
554 		*(int *)data = k->k_state.kbd_id;
555 		return (0);
556 
557 	case KIOCSDIRECT:
558 		k->k_evmode = *(int *)data;
559 		return (0);
560 
561 	case FIONBIO:		/* we will remove this someday (soon???) */
562 		return (0);
563 
564 	case FIOASYNC:
565 		k->k_events.ev_async = *(int *)data != 0;
566 		return (0);
567 
568 	case TIOCSPGRP:
569 		if (*(int *)data != k->k_events.ev_io->p_pgid)
570 			return (EPERM);
571 		return (0);
572 
573 	default:
574 		return (ENOTTY);
575 	}
576 
577 	/*
578 	 * We identified the ioctl, but we do not handle it.
579 	 */
580 	return (EOPNOTSUPP);		/* misuse, but what the heck */
581 }
582 
583 int
584 kbdselect(dev_t dev, int rw, struct proc *p)
585 {
586 
587 	return (ev_select(&kbd_softc.k_events, rw, p));
588 }
589 
590 /*
591  * Execute a keyboard command; return 0 on success.
592  * If `isuser', force a small delay before output if output queue
593  * is flooding.  (The keyboard runs at 1200 baud, or 120 cps.)
594  */
595 int
596 kbd_docmd(int cmd, int isuser)
597 {
598 	register struct tty *tp = kbd_softc.k_kbd;
599 	register struct kbd_softc *k = &kbd_softc;
600 	int s;
601 
602 	if (tp == NULL)
603 		return (ENXIO);		/* ??? */
604 	switch (cmd) {
605 
606 	case KBD_CMD_BELL:
607 	case KBD_CMD_NOBELL:
608 		/* Supported by type 2, 3, and 4 keyboards */
609 		break;
610 
611 	case KBD_CMD_CLICK:
612 		/* Unsupported by type 2 keyboards */
613 		if (k->k_state.kbd_id != KB_SUN2) {
614 			k->k_state.kbd_click = 1;
615 			break;
616 		}
617 		return (EINVAL);
618 
619 	case KBD_CMD_NOCLICK:
620 		/* Unsupported by type 2 keyboards */
621 		if (k->k_state.kbd_id != KB_SUN2) {
622 			k->k_state.kbd_click = 0;
623 			break;
624 		}
625 		return (EINVAL);
626 
627 	default:
628 		return (EINVAL);	/* ENOTTY? EOPNOTSUPP? */
629 	}
630 
631 	if (isuser) {
632 		s = spltty();
633 		if (tp->t_outq.c_cc > 120)
634 			(void) tsleep((caddr_t)&lbolt, TTIPRI,
635 			    ttyout, 0);
636 		splx(s);
637 	}
638 	if (ttyoutput(cmd, tp) >= 0)
639 		return (ENOSPC);	/* ERESTART? */
640 	(*tp->t_oproc)(tp);
641 	return (0);
642 }
643