Home | History | Annotate | Line # | Download | only in sun
sunkbd.c revision 1.6.4.2
      1 /*	$NetBSD: sunkbd.c,v 1.6.4.2 2001/10/11 00:02:27 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This software was developed by the Computer Systems Engineering group
      8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
      9  * contributed to Berkeley.
     10  *
     11  * All advertising materials mentioning features or use of this software
     12  * must display the following acknowledgement:
     13  *	This product includes software developed by the University of
     14  *	California, Lawrence Berkeley Laboratory.
     15  *
     16  * Redistribution and use in source and binary forms, with or without
     17  * modification, are permitted provided that the following conditions
     18  * are met:
     19  * 1. Redistributions of source code must retain the above copyright
     20  *    notice, this list of conditions and the following disclaimer.
     21  * 2. Redistributions in binary form must reproduce the above copyright
     22  *    notice, this list of conditions and the following disclaimer in the
     23  *    documentation and/or other materials provided with the distribution.
     24  * 3. All advertising materials mentioning features or use of this software
     25  *    must display the following acknowledgement:
     26  *	This product includes software developed by the University of
     27  *	California, Berkeley and its contributors.
     28  * 4. Neither the name of the University nor the names of its contributors
     29  *    may be used to endorse or promote products derived from this software
     30  *    without specific prior written permission.
     31  *
     32  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     33  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     34  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     35  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     36  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     40  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     41  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     42  * SUCH DAMAGE.
     43  *
     44  *	@(#)kbd.c	8.2 (Berkeley) 10/30/93
     45  */
     46 
     47 /*
     48  * Keyboard driver (/dev/kbd -- note that we do not have minor numbers
     49  * [yet?]).  Translates incoming bytes to ASCII or to `firm_events' and
     50  * passes them up to the appropriate reader.
     51  */
     52 
     53 /*
     54  * Keyboard interface line discipline.
     55  *
     56  */
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/conf.h>
     61 #include <sys/device.h>
     62 #include <sys/kernel.h>
     63 #include <sys/malloc.h>
     64 #include <sys/proc.h>
     65 #include <sys/signal.h>
     66 #include <sys/signalvar.h>
     67 #include <sys/time.h>
     68 #include <sys/select.h>
     69 #include <sys/syslog.h>
     70 #include <sys/fcntl.h>
     71 #include <sys/tty.h>
     72 #include <sys/vnode.h>
     73 
     74 #include <dev/cons.h>
     75 #include <machine/vuid_event.h>
     76 #include <machine/kbd.h>
     77 #include <dev/sun/event_var.h>
     78 #include <dev/sun/kbd_xlate.h>
     79 #include <dev/sun/kbdvar.h>
     80 #include <dev/sun/kbd_ms_ttyvar.h>
     81 
     82 #include "kbd.h"
     83 #if NKBD > 0
     84 
     85 /****************************************************************
     86  * Interface to the lower layer (ttycc)
     87  ****************************************************************/
     88 
     89 static int	sunkbd_match(struct device *, struct cfdata *, void *);
     90 static void	sunkbd_attach(struct device *, struct device *, void *);
     91 static void	sunkbd_write_data(struct kbd_softc *, int);
     92 static int	sunkbdiopen(struct device *, int mode, struct vnode *);
     93 static int	sunkbdiclose(struct device *, int mode, struct vnode *);
     94 
     95 int	sunkbdinput(int, struct tty *);
     96 int	sunkbdstart(struct tty *);
     97 
     98 
     99 struct cfattach kbd_ca = {
    100 	sizeof(struct kbd_softc), sunkbd_match, sunkbd_attach
    101 };
    102 
    103 struct  linesw sunkbd_disc =
    104 	{ "sunkbd", 7, ttylopen, ttylclose, ttyerrio, ttyerrio, nullioctl,
    105 	  sunkbdinput, sunkbdstart, nullmodem, ttpoll }; /* 7- SUNKBDDISC */
    106 
    107 /*
    108  * sunkbd_match: how is this tty channel configured?
    109  */
    110 int
    111 sunkbd_match(parent, cf, aux)
    112 	struct device *parent;
    113 	struct cfdata *cf;
    114 	void   *aux;
    115 {
    116 	struct kbd_ms_tty_attach_args *args = aux;
    117 
    118 	if (strcmp(args->kmta_name, "keyboard") == 0)
    119 		return (1);
    120 
    121 	return 0;
    122 }
    123 
    124 void
    125 sunkbd_attach(parent, self, aux)
    126 	struct device *parent, *self;
    127 	void   *aux;
    128 
    129 {
    130 	struct kbd_softc *k = (void *) self;
    131 	struct kbd_ms_tty_attach_args *args = aux;
    132 	struct cfdata *cf;
    133 	struct tty *tp = args->kmta_tp;
    134 	struct cons_channel *cc;
    135 	int kbd_unit;
    136 
    137 	/* Set up the proper line discipline. */
    138 	ttyldisc_init();
    139 	if (ttyldisc_add(&sunkbd_disc, -1) == -1)
    140 		panic("sunkbd_attach: sunkbd_disc");
    141 	tp->t_linesw = &sunkbd_disc;
    142 	tp->t_oflag &= ~OPOST;
    143 
    144 	/* link the structures together. */
    145 	k->k_priv = tp;
    146 	tp->t_sc = (void *)k;
    147 
    148 	cf = k->k_dev.dv_cfdata;
    149 	kbd_unit = k->k_dev.dv_unit;
    150 
    151 	k->k_deviopen = sunkbdiopen;
    152 	k->k_deviclose = sunkbdiclose;
    153 	k->k_rdev = args->kmta_dev;
    154 	k->k_write_data = sunkbd_write_data;
    155 
    156 	if ((cc = malloc(sizeof *cc, M_DEVBUF, M_NOWAIT)) == NULL)
    157 		return;
    158 
    159 	cc->cc_dev = self;
    160 	cc->cc_iopen = kbd_cc_open;
    161 	cc->cc_iclose = kbd_cc_close;
    162 	cc->cc_upstream = NULL;
    163 	if (args->kmta_consdev) {
    164 		char magic[4];
    165 
    166 		/*
    167 		 * Hookup ourselves as the console input channel
    168 		 */
    169 		args->kmta_baud = KBD_BPS;
    170 		args->kmta_cflag = CLOCAL|CS8;
    171 		cons_attach_input(cc, args->kmta_consdev);
    172 
    173 		/* Tell our parent what the console should be. */
    174 		args->kmta_consdev = cn_tab;
    175 		k->k_isconsole = 1;
    176 		printf(" (console input)");
    177 
    178 		/* Set magic to "L1-A" */
    179 		magic[0] = KBD_L1;
    180 		magic[1] = KBD_A;
    181 		magic[2] = 0;
    182 		cn_set_magic(magic);
    183 	} else {
    184 		extern void kd_attach_input(struct cons_channel *);
    185 
    186 		kd_attach_input(cc);
    187 	}
    188 	k->k_cc = cc;
    189 
    190 	printf("\n");
    191 
    192 	callout_init(&k->k_repeat_ch);
    193 
    194 	/* Do this before any calls to kbd_rint(). */
    195 	kbd_xlate_init(&k->k_state);
    196 
    197 	/* XXX - Do this in open? */
    198 	k->k_repeat_start = hz/2;
    199 	k->k_repeat_step = hz/20;
    200 
    201 	/* Magic sequence. */
    202 	k->k_magic1 = KBD_L1;
    203 	k->k_magic2 = KBD_A;
    204 }
    205 
    206 /*
    207  * Internal open routine.  This really should be inside com.c
    208  * But I'm putting it here until we have a generic internal open
    209  * mechanism.
    210  */
    211 static int
    212 sunkbdiopen(dev, flags, devvp)
    213 	struct device *dev;
    214 	int flags;
    215 	struct vnode *devvp;
    216 {
    217 	struct kbd_softc *k = (void *) dev;
    218 	struct tty *tp = (struct tty *)k->k_priv;
    219 	struct proc *p = curproc;
    220 	struct termios t;
    221 	int maj;
    222 	int error;
    223 	dev_t rdev;
    224 
    225 	rdev = k->k_rdev;
    226 	maj = major(rdev);
    227 	if (p == NULL)
    228 		p = &proc0;
    229 
    230 	error = cdevvp(rdev, &k->k_devvp);
    231 	if (error != 0)
    232 		return error;
    233 
    234 	/* Open the lower device */
    235 	if ((error = (*cdevsw[maj].d_open)(k->k_devvp, O_NONBLOCK|flags,
    236 					   0/* ignored? */, p)) != 0)
    237 		return (error);
    238 
    239 	/* Now configure it for the console. */
    240 	tp->t_ospeed = 0;
    241 	tp->t_devvp = k->k_devvp;
    242 	t.c_ispeed = KBD_BPS;
    243 	t.c_ospeed = KBD_BPS;
    244 	t.c_cflag =  CLOCAL|CS8;
    245 	(*tp->t_param)(tp, &t);
    246 
    247 	return (0);
    248 }
    249 
    250 static int
    251 sunkbdiclose(dev, flags, devvp)
    252 	struct device *dev;
    253 	int flags;
    254 	struct vnode *devvp;
    255 {
    256 	/* XXX why is this never called? should relese the vnode. */
    257 	return 0;
    258 }
    259 
    260 /*
    261  * TTY interface to handle input.
    262  */
    263 int
    264 sunkbdinput(c, tp)
    265 	int c;
    266 	struct tty *tp;
    267 {
    268 	struct kbd_softc *k = (void *) tp->t_sc;
    269 	u_char *cc;
    270 	int error;
    271 
    272 
    273 	cc = tp->t_cc;
    274 
    275 	/*
    276 	 * Handle exceptional conditions (break, parity, framing).
    277 	 */
    278 	if ((error = ((c & TTY_ERRORMASK))) != 0) {
    279 		/*
    280 		 * After garbage, flush pending input, and
    281 		 * send a reset to resync key translation.
    282 		 */
    283 		log(LOG_ERR, "%s: input error (0x%x)\n",
    284 			k->k_dev.dv_xname, c);
    285 		c &= TTY_CHARMASK;
    286 		if (!k->k_txflags & K_TXBUSY) {
    287 			ttyflush(tp, FREAD | FWRITE);
    288 			goto send_reset;
    289 		}
    290 	}
    291 
    292 	/*
    293 	 * Check for input buffer overflow
    294 	 */
    295 	if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
    296 		log(LOG_ERR, "%s: input overrun\n",
    297 		    k->k_dev.dv_xname);
    298 		goto send_reset;
    299 	}
    300 
    301 	/* Pass this up to the "middle" layer. */
    302 	kbd_input_raw(k, c);
    303 	return (-1);
    304 
    305 send_reset:
    306 	/* Send a reset to resync translation. */
    307 	kbd_output(k, KBD_CMD_RESET);
    308 	return (ttstart(tp));
    309 
    310 }
    311 
    312 int
    313 sunkbdstart(tp)
    314 	struct tty *tp;
    315 {
    316 	struct kbd_softc *k = (void *) tp->t_sc;
    317 
    318 	/*
    319 	 * Transmit done.  Try to send more, or
    320 	 * clear busy and wakeup drain waiters.
    321 	 */
    322 	k->k_txflags &= ~K_TXBUSY;
    323 	kbd_start_tx(k);
    324 	ttstart(tp);
    325 	return (0);
    326 }
    327 /*
    328  * used by kbd_start_tx();
    329  */
    330 void
    331 sunkbd_write_data(k, c)
    332 	struct kbd_softc *k;
    333 	int c;
    334 {
    335 	struct tty *tp = (struct tty *)k->k_priv;
    336 	int	s;
    337 
    338 	s = spltty();
    339 	ttyoutput(c, tp);
    340 	ttstart(tp);
    341 	splx(s);
    342 }
    343 
    344 #endif
    345