Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.1
      1 /*
      2  * Copyright (c) 1994 Gordon W. Ross
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. The name of the Author may not be used to endorse or promote products
     14  *    derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY Gordon W. Ross ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  *
     28  * $Header: /tank/opengrok/rsync2/NetBSD/src/sys/arch/sun3/dev/kd.c,v 1.1 1994/05/04 05:27:50 gwr Exp $
     29  */
     30 
     31 /*
     32  * Keyboard/Display device.
     33  *
     34  * This driver exists simply to provide a tty device that
     35  * the indirect console driver can point to.
     36  * The kbd driver sends its input here.
     37  * Output goes to the screen via PROM printf.
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/proc.h>
     42 #include <sys/systm.h>
     43 #include <sys/ioctl.h>
     44 #include <sys/tty.h>
     45 #include <sys/file.h>
     46 #include <sys/conf.h>
     47 #include <sys/device.h>
     48 
     49 #include <machine/autoconf.h>
     50 #include <machine/mon.h>
     51 
     52 #include <dev/cons.h>
     53 
     54 #define BURST	64
     55 
     56 static int  kdmatch(struct device *, struct cfdata *, void *);
     57 static void kdattach(struct device *, struct device *, void *);
     58 
     59 struct cfdriver kdcd = {
     60 	NULL, "kd", kdmatch, kdattach, DV_TTY,
     61 	sizeof(struct device), 0};
     62 
     63 struct tty *kd_tty[1];
     64 
     65 int kdopen(dev_t, int, int, struct proc *);
     66 int kdclose(dev_t, int, int, struct proc *);
     67 int kdread(dev_t, struct uio *, int);
     68 int kdwrite(dev_t, struct uio *, int);
     69 int kdioctl(dev_t, int, caddr_t, int, struct proc *);
     70 
     71 static int kdparam(struct tty *, struct termios *);
     72 static void kdstart(struct tty *);
     73 
     74 static int
     75 kdmatch(struct device *parent, struct cfdata *cf, void *aux)
     76 {
     77 	/* XXX - Enforce unit zero? */
     78 	return 1;
     79 }
     80 
     81 static void
     82 kdattach(parent, self, args)
     83 	struct device *parent;
     84 	struct device *self;
     85 	void *args;
     86 {
     87 	int unit = self->dv_unit;
     88 
     89 	if (unit) {
     90 		printf(" not unit zero?\n");
     91 		return;
     92 	}
     93 	printf("\n");
     94 
     95 	kd_tty[0] = ttymalloc();
     96 
     97 	/* Tell keyboard module where to send read data. */
     98 	kbd_ascii(kd_tty[0]);
     99 }
    100 
    101 int
    102 kdopen(dev, flag, mode, p)
    103 	dev_t dev;
    104 	int flag, mode;
    105 	struct proc *p;
    106 {
    107 	int unit;
    108 	struct tty *tp;
    109 
    110 	unit = minor(dev);
    111 	if (unit) return ENXIO;
    112 
    113 	tp = kd_tty[unit];
    114 	if (tp == NULL)
    115 		return ENXIO;
    116 
    117 	tp->t_oproc = kdstart;
    118 	tp->t_param = kdparam;
    119 	tp->t_dev = dev;
    120 	if ((tp->t_state & TS_ISOPEN) == 0) {
    121 		tp->t_state |= TS_WOPEN;
    122 		ttychars(tp);
    123 		tp->t_iflag = TTYDEF_IFLAG;
    124 		tp->t_oflag = TTYDEF_OFLAG;
    125 		tp->t_cflag = TTYDEF_CFLAG;
    126 		tp->t_lflag = TTYDEF_LFLAG;
    127 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    128 		kdparam(tp, &tp->t_termios);
    129 		ttsetwater(tp);
    130 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
    131 		return EBUSY;
    132 	tp->t_state |= TS_CARR_ON;
    133 
    134 	return ((*linesw[tp->t_line].l_open)(dev, tp));
    135 }
    136 
    137 int
    138 kdclose(dev, flag, mode, p)
    139 	dev_t dev;
    140 	int flag, mode;
    141 	struct proc *p;
    142 {
    143 	int unit = minor(dev);
    144 	struct tty *tp = kd_tty[unit];
    145 
    146 	(*linesw[tp->t_line].l_close)(tp, flag);
    147 	ttyclose(tp);
    148 	return (0);
    149 }
    150 
    151 int
    152 kdread(dev, uio, flag)
    153 	dev_t dev;
    154 	struct uio *uio;
    155 	int flag;
    156 {
    157 	int unit = minor(dev);
    158 	struct tty *tp = kd_tty[unit];
    159 
    160 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    161 }
    162 
    163 int
    164 kdwrite(dev, uio, flag)
    165 	dev_t dev;
    166 	struct uio *uio;
    167 	int flag;
    168 {
    169 	int unit = minor(dev);
    170 	struct tty *tp = kd_tty[unit];
    171 
    172 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    173 }
    174 
    175 int
    176 kdioctl(dev, cmd, data, flag, p)
    177 	dev_t dev;
    178 	int cmd;
    179 	caddr_t data;
    180 	int flag;
    181 	struct proc *p;
    182 {
    183 	int error;
    184 	int unit = minor(dev);
    185 	struct tty *tp = kd_tty[unit];
    186 
    187 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    188 	if (error >= 0)
    189 		return error;
    190 	error = ttioctl(tp, cmd, data, flag, p);
    191 	if (error >= 0)
    192 		return error;
    193 
    194 	/* Handle any ioctl commands specific to kbd/display. */
    195 	/* XXX - Send KB* ioctls to kbd module? */
    196 	/* XXX - Send FB* ioctls to fb module?  */
    197 
    198 	return ENOTTY;
    199 }
    200 
    201 void
    202 kdstart(tp)
    203 	struct tty *tp;
    204 {
    205 	struct clist *cl;
    206 	int s, len;
    207 	u_char buf[BURST];
    208 
    209 	s = spltty();
    210 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    211 		goto out;
    212 	tp->t_state |= TS_BUSY;
    213 	cl = &tp->t_outq;
    214 
    215 	/*
    216 	 * XXX - It might be nice to have our own fbputs() so
    217 	 * we would not have to use the (slow) PROM printf.
    218 	 */
    219 	while ((len = q_to_b(cl, buf, BURST-1)) > 0) {
    220 		buf[len] = '\0';
    221 		(void) splhigh();
    222 #if 0
    223 		/* XXX - Not yet.  Not sure what args should be. */
    224 		(romVectorPtr->fbWriteStr)(buf);
    225 #else
    226 		mon_printf("%s", buf);
    227 #endif
    228 		(void) spltty();
    229 	}
    230 
    231 	tp->t_state &= ~TS_BUSY;
    232 	if (tp->t_state & TS_ASLEEP) {
    233 		tp->t_state &= ~TS_ASLEEP;
    234 		wakeup((caddr_t)cl);
    235 	}
    236 	selwakeup(&tp->t_wsel);
    237 out:
    238 	splx(s);
    239 }
    240 
    241 static int
    242 kdparam(tp, t)
    243 	struct tty *tp;
    244 	struct termios *t;
    245 {
    246 	/* XXX - These are ignored... */
    247 	tp->t_ispeed = t->c_ispeed;
    248 	tp->t_ospeed = t->c_ospeed;
    249 	tp->t_cflag = t->c_cflag;
    250 	return 0;
    251 }
    252 
    253 
    254 /*
    255  * kd console support
    256  *
    257  * XXX - Using prom routines for now...
    258  */
    259 
    260 extern int zscnprobe_kbd();
    261 
    262 kdcnprobe(cp)
    263 	struct consdev *cp;
    264 {
    265 	int maj;
    266 
    267 	/* locate the major number */
    268 	for (maj = 0; maj < nchrdev; maj++)
    269 		if (cdevsw[maj].d_open == kdopen)
    270 			break;
    271 
    272 	/* initialize required fields */
    273 	cp->cn_dev = makedev(maj, 0);
    274 	cp->cn_pri = zscnprobe_kbd();
    275 }
    276 
    277 kdcninit(cp)
    278 	struct consdev *cp;
    279 {
    280 	mon_printf("console on kd0 (keyboard/display)\n");
    281 }
    282 
    283 kdcngetc(dev)
    284 	dev_t dev;
    285 {
    286 	int c, s;
    287 
    288 	/* XXX - Does mon_may_getchar() require the NMI clock? */
    289 	s = splhigh();
    290 	do c = mon_may_getchar();
    291 	while (c < 0);
    292 	splx(s);
    293 
    294 	if (c == '\r')
    295 		c = '\n';
    296 	return (c);
    297 }
    298 
    299 kdcnputc(dev, c)
    300 	dev_t dev;
    301 	int c;
    302 {
    303 	int s;
    304 
    305 	s = splhigh();
    306 #if 0
    307 	/* XXX - Is this worth doing? */
    308 	(romVectorPtr->fbWriteChar)(c);
    309 #else
    310 	mon_putchar(c);
    311 #endif
    312 	splx(s);
    313 }
    314