Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.11
      1  1.11  gwr /*	$NetBSD: kd.c,v 1.11 1995/03/28 16:09:41 gwr Exp $	*/
      2   1.4  cgd 
      3   1.1  gwr /*
      4  1.11  gwr  * Copyright (c) 1994, 1995 Gordon W. Ross
      5   1.1  gwr  * All rights reserved.
      6   1.1  gwr  *
      7   1.1  gwr  * Redistribution and use in source and binary forms, with or without
      8   1.1  gwr  * modification, are permitted provided that the following conditions
      9   1.1  gwr  * are met:
     10   1.1  gwr  * 1. Redistributions of source code must retain the above copyright
     11   1.1  gwr  *    notice, this list of conditions and the following disclaimer.
     12   1.1  gwr  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  gwr  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  gwr  *    documentation and/or other materials provided with the distribution.
     15   1.2  gwr  * 3. The name of the author may not be used to endorse or promote products
     16   1.1  gwr  *    derived from this software without specific prior written permission.
     17  1.11  gwr  * 4. All advertising materials mentioning features or use of this software
     18  1.11  gwr  *    must display the following acknowledgement:
     19  1.11  gwr  *      This product includes software developed by Gordon Ross
     20   1.1  gwr  *
     21   1.2  gwr  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.2  gwr  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.2  gwr  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.2  gwr  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.2  gwr  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.2  gwr  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.2  gwr  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.2  gwr  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.2  gwr  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.2  gwr  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1  gwr  */
     32   1.1  gwr 
     33   1.1  gwr /*
     34   1.1  gwr  * Keyboard/Display device.
     35   1.1  gwr  *
     36   1.1  gwr  * This driver exists simply to provide a tty device that
     37   1.1  gwr  * the indirect console driver can point to.
     38   1.1  gwr  * The kbd driver sends its input here.
     39   1.1  gwr  * Output goes to the screen via PROM printf.
     40   1.1  gwr  */
     41   1.1  gwr 
     42   1.1  gwr #include <sys/param.h>
     43   1.1  gwr #include <sys/proc.h>
     44   1.1  gwr #include <sys/systm.h>
     45   1.1  gwr #include <sys/ioctl.h>
     46   1.1  gwr #include <sys/tty.h>
     47   1.1  gwr #include <sys/file.h>
     48   1.1  gwr #include <sys/conf.h>
     49   1.1  gwr #include <sys/device.h>
     50   1.1  gwr 
     51   1.1  gwr #include <machine/autoconf.h>
     52   1.1  gwr #include <machine/mon.h>
     53  1.11  gwr #include <machine/psl.h>
     54   1.1  gwr 
     55   1.1  gwr #include <dev/cons.h>
     56   1.1  gwr 
     57   1.1  gwr #define BURST	64
     58   1.1  gwr 
     59   1.1  gwr struct tty *kd_tty[1];
     60   1.1  gwr 
     61   1.1  gwr int kdopen(dev_t, int, int, struct proc *);
     62   1.1  gwr int kdclose(dev_t, int, int, struct proc *);
     63   1.1  gwr int kdread(dev_t, struct uio *, int);
     64   1.1  gwr int kdwrite(dev_t, struct uio *, int);
     65   1.1  gwr int kdioctl(dev_t, int, caddr_t, int, struct proc *);
     66   1.1  gwr 
     67   1.1  gwr static int kdparam(struct tty *, struct termios *);
     68   1.1  gwr static void kdstart(struct tty *);
     69   1.1  gwr 
     70  1.10  gwr int kd_is_console;
     71  1.10  gwr 
     72   1.7  gwr /* This is called by kbd_serial() like a pseudo-device. */
     73   1.7  gwr void
     74   1.7  gwr kd_attach(n)
     75   1.7  gwr 	int n;
     76   1.1  gwr {
     77   1.1  gwr 	kd_tty[0] = ttymalloc();
     78   1.1  gwr 
     79   1.1  gwr 	/* Tell keyboard module where to send read data. */
     80   1.1  gwr 	kbd_ascii(kd_tty[0]);
     81   1.1  gwr }
     82   1.1  gwr 
     83   1.1  gwr int
     84   1.1  gwr kdopen(dev, flag, mode, p)
     85   1.1  gwr 	dev_t dev;
     86   1.1  gwr 	int flag, mode;
     87   1.1  gwr 	struct proc *p;
     88   1.1  gwr {
     89   1.3  gwr 	int error, unit;
     90   1.1  gwr 	struct tty *tp;
     91   1.1  gwr 
     92   1.1  gwr 	unit = minor(dev);
     93   1.1  gwr 	if (unit) return ENXIO;
     94   1.1  gwr 
     95   1.1  gwr 	tp = kd_tty[unit];
     96   1.1  gwr 	if (tp == NULL)
     97   1.1  gwr 		return ENXIO;
     98   1.3  gwr 
     99   1.8  gwr 	if ((error = kbd_iopen()) != 0) {
    100   1.8  gwr #ifdef	DIAGNOSTIC
    101   1.8  gwr 		printf("kd: kbd_iopen, error=%d\n", error);
    102   1.8  gwr #endif
    103   1.3  gwr 		return (error);
    104   1.8  gwr 	}
    105   1.1  gwr 
    106   1.1  gwr 	tp->t_oproc = kdstart;
    107   1.1  gwr 	tp->t_param = kdparam;
    108   1.1  gwr 	tp->t_dev = dev;
    109   1.1  gwr 	if ((tp->t_state & TS_ISOPEN) == 0) {
    110   1.1  gwr 		tp->t_state |= TS_WOPEN;
    111   1.1  gwr 		ttychars(tp);
    112   1.1  gwr 		tp->t_iflag = TTYDEF_IFLAG;
    113   1.1  gwr 		tp->t_oflag = TTYDEF_OFLAG;
    114   1.1  gwr 		tp->t_cflag = TTYDEF_CFLAG;
    115   1.1  gwr 		tp->t_lflag = TTYDEF_LFLAG;
    116   1.1  gwr 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    117   1.1  gwr 		kdparam(tp, &tp->t_termios);
    118   1.1  gwr 		ttsetwater(tp);
    119   1.1  gwr 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0)
    120   1.1  gwr 		return EBUSY;
    121   1.1  gwr 	tp->t_state |= TS_CARR_ON;
    122   1.1  gwr 
    123   1.1  gwr 	return ((*linesw[tp->t_line].l_open)(dev, tp));
    124   1.1  gwr }
    125   1.1  gwr 
    126   1.1  gwr int
    127   1.1  gwr kdclose(dev, flag, mode, p)
    128   1.1  gwr 	dev_t dev;
    129   1.1  gwr 	int flag, mode;
    130   1.1  gwr 	struct proc *p;
    131   1.1  gwr {
    132   1.1  gwr 	int unit = minor(dev);
    133   1.1  gwr 	struct tty *tp = kd_tty[unit];
    134   1.1  gwr 
    135   1.1  gwr 	(*linesw[tp->t_line].l_close)(tp, flag);
    136   1.1  gwr 	ttyclose(tp);
    137   1.1  gwr 	return (0);
    138   1.1  gwr }
    139   1.1  gwr 
    140   1.1  gwr int
    141   1.1  gwr kdread(dev, uio, flag)
    142   1.1  gwr 	dev_t dev;
    143   1.1  gwr 	struct uio *uio;
    144   1.1  gwr 	int flag;
    145   1.1  gwr {
    146   1.1  gwr 	int unit = minor(dev);
    147   1.1  gwr 	struct tty *tp = kd_tty[unit];
    148   1.1  gwr 
    149   1.1  gwr 	return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
    150   1.1  gwr }
    151   1.1  gwr 
    152   1.1  gwr int
    153   1.1  gwr kdwrite(dev, uio, flag)
    154   1.1  gwr 	dev_t dev;
    155   1.1  gwr 	struct uio *uio;
    156   1.1  gwr 	int flag;
    157   1.1  gwr {
    158   1.1  gwr 	int unit = minor(dev);
    159   1.1  gwr 	struct tty *tp = kd_tty[unit];
    160   1.1  gwr 
    161   1.1  gwr 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
    162   1.1  gwr }
    163   1.1  gwr 
    164   1.1  gwr int
    165   1.1  gwr kdioctl(dev, cmd, data, flag, p)
    166   1.1  gwr 	dev_t dev;
    167   1.1  gwr 	int cmd;
    168   1.1  gwr 	caddr_t data;
    169   1.1  gwr 	int flag;
    170   1.1  gwr 	struct proc *p;
    171   1.1  gwr {
    172   1.1  gwr 	int error;
    173   1.1  gwr 	int unit = minor(dev);
    174   1.1  gwr 	struct tty *tp = kd_tty[unit];
    175   1.1  gwr 
    176   1.1  gwr 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    177   1.1  gwr 	if (error >= 0)
    178   1.1  gwr 		return error;
    179   1.1  gwr 	error = ttioctl(tp, cmd, data, flag, p);
    180   1.1  gwr 	if (error >= 0)
    181   1.1  gwr 		return error;
    182   1.1  gwr 
    183   1.1  gwr 	/* Handle any ioctl commands specific to kbd/display. */
    184   1.1  gwr 	/* XXX - Send KB* ioctls to kbd module? */
    185   1.1  gwr 	/* XXX - Send FB* ioctls to fb module?  */
    186   1.1  gwr 
    187   1.1  gwr 	return ENOTTY;
    188   1.1  gwr }
    189   1.1  gwr 
    190  1.11  gwr static void kd_later(void*);
    191  1.11  gwr static void kd_putfb(struct tty *);
    192  1.11  gwr 
    193   1.1  gwr void
    194   1.1  gwr kdstart(tp)
    195   1.1  gwr 	struct tty *tp;
    196   1.1  gwr {
    197   1.1  gwr 	struct clist *cl;
    198  1.11  gwr 	register int s;
    199   1.1  gwr 
    200   1.1  gwr 	s = spltty();
    201   1.1  gwr 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    202   1.1  gwr 		goto out;
    203  1.11  gwr 
    204   1.1  gwr 	cl = &tp->t_outq;
    205  1.11  gwr 	if (cl->c_cc) {
    206  1.10  gwr 		if (kd_is_console) {
    207  1.11  gwr 			tp->t_state |= TS_BUSY;
    208  1.11  gwr 			if ((s & PSL_IPL) == 0) {
    209  1.11  gwr 				/* called at level zero - update screen now. */
    210  1.11  gwr 				(void) splsoftclock();
    211  1.11  gwr 				kd_putfb(tp);
    212  1.11  gwr 				(void) spltty();
    213  1.11  gwr 				tp->t_state &= ~TS_BUSY;
    214  1.11  gwr 			} else {
    215  1.11  gwr 				/* called at interrupt level - do it later */
    216  1.11  gwr 				timeout(kd_later, (void*)tp, 0);
    217  1.11  gwr 			}
    218  1.11  gwr 		} else {
    219  1.11  gwr 			/*
    220  1.11  gwr 			 * This driver uses the PROM for writing the screen,
    221  1.11  gwr 			 * and that only works if this is the console device.
    222  1.11  gwr 			 * If this is not the console, just flush the output.
    223  1.11  gwr 			 * Sorry.  (In that case, use xdm instead of getty.)
    224  1.11  gwr 			 */
    225  1.11  gwr 			ndflush(cl, cl->c_cc);
    226  1.11  gwr 		}
    227  1.11  gwr 	}
    228  1.11  gwr 	if (cl->c_cc <= tp->t_lowat) {
    229  1.11  gwr 		if (tp->t_state & TS_ASLEEP) {
    230  1.11  gwr 			tp->t_state &= ~TS_ASLEEP;
    231  1.11  gwr 			wakeup((caddr_t)cl);
    232  1.10  gwr 		}
    233  1.11  gwr 		selwakeup(&tp->t_wsel);
    234   1.1  gwr 	}
    235  1.11  gwr out:
    236  1.11  gwr 	splx(s);
    237  1.11  gwr }
    238  1.11  gwr 
    239  1.11  gwr /*
    240  1.11  gwr  * Timeout function to do delayed writes to the screen.
    241  1.11  gwr  * Called at splsoftclock when requested by kdstart.
    242  1.11  gwr  */
    243  1.11  gwr static void
    244  1.11  gwr kd_later(tpaddr)
    245  1.11  gwr 	void *tpaddr;
    246  1.11  gwr {
    247  1.11  gwr 	struct tty *tp = tpaddr;
    248  1.11  gwr 	register int s;
    249   1.1  gwr 
    250  1.11  gwr 	kd_putfb(tp);
    251  1.11  gwr 
    252  1.11  gwr 	s = spltty();
    253   1.1  gwr 	tp->t_state &= ~TS_BUSY;
    254  1.11  gwr 	if (tp->t_line)
    255  1.11  gwr 		(*linesw[tp->t_line].l_start)(tp);
    256  1.11  gwr 	else
    257  1.11  gwr 		kdstart(tp);
    258  1.11  gwr 	splx(s);
    259  1.11  gwr }
    260  1.11  gwr 
    261  1.11  gwr /*
    262  1.11  gwr  * Put text on the screen using the PROM monitor.
    263  1.11  gwr  * This can take a while, so to avoid missing
    264  1.11  gwr  * interrupts, this is called at splsoftclock.
    265  1.11  gwr  */
    266  1.11  gwr static void kd_putfb(tp)
    267  1.11  gwr 	struct tty *tp;
    268  1.11  gwr {
    269  1.11  gwr 	char buf[BURST];
    270  1.11  gwr 	struct clist *cl = &tp->t_outq;
    271  1.11  gwr 	char *p, *end;
    272  1.11  gwr 	int len;
    273  1.11  gwr 
    274  1.11  gwr 	while ((len = q_to_b(cl, buf, BURST-1)) > 0) {
    275  1.11  gwr 		/* PROM will barf if high bits are set. */
    276  1.11  gwr 		p = buf;
    277  1.11  gwr 		end = buf + len;
    278  1.11  gwr 		while (p < end)
    279  1.11  gwr 			*p++ &= 0x7f;
    280  1.11  gwr 		(romVectorPtr->fbWriteStr)(buf, len);
    281   1.1  gwr 	}
    282   1.1  gwr }
    283   1.1  gwr 
    284  1.11  gwr 
    285   1.1  gwr static int
    286   1.1  gwr kdparam(tp, t)
    287   1.1  gwr 	struct tty *tp;
    288   1.1  gwr 	struct termios *t;
    289   1.1  gwr {
    290   1.1  gwr 	/* XXX - These are ignored... */
    291   1.1  gwr 	tp->t_ispeed = t->c_ispeed;
    292   1.1  gwr 	tp->t_ospeed = t->c_ospeed;
    293   1.1  gwr 	tp->t_cflag = t->c_cflag;
    294   1.1  gwr 	return 0;
    295   1.1  gwr }
    296   1.1  gwr 
    297   1.1  gwr 
    298   1.1  gwr /*
    299   1.1  gwr  * kd console support
    300   1.1  gwr  */
    301   1.1  gwr 
    302   1.8  gwr extern int zscnprobe_kbd(), zscngetc(), kbd_translate();
    303   1.1  gwr 
    304   1.1  gwr kdcnprobe(cp)
    305   1.1  gwr 	struct consdev *cp;
    306   1.1  gwr {
    307   1.1  gwr 	int maj;
    308   1.1  gwr 
    309   1.1  gwr 	/* locate the major number */
    310   1.1  gwr 	for (maj = 0; maj < nchrdev; maj++)
    311   1.6  gwr 		if (cdevsw[maj].d_open == (void*)kdopen)
    312   1.1  gwr 			break;
    313   1.1  gwr 
    314   1.1  gwr 	/* initialize required fields */
    315   1.1  gwr 	cp->cn_dev = makedev(maj, 0);
    316   1.1  gwr 	cp->cn_pri = zscnprobe_kbd();
    317   1.1  gwr }
    318   1.1  gwr 
    319   1.1  gwr kdcninit(cp)
    320   1.1  gwr 	struct consdev *cp;
    321   1.1  gwr {
    322   1.8  gwr 
    323   1.8  gwr 	/* This prepares zscngetc() */
    324   1.8  gwr 	zs_set_conschan(1, 0);
    325   1.8  gwr 
    326   1.8  gwr 	/* This prepares kbd_translate() */
    327   1.8  gwr 	kbd_init_tables();
    328  1.10  gwr 
    329  1.10  gwr 	/* Indicate that it is OK to use the PROM fbwrite */
    330  1.10  gwr 	kd_is_console = 1;
    331   1.8  gwr 
    332   1.1  gwr 	mon_printf("console on kd0 (keyboard/display)\n");
    333   1.1  gwr }
    334   1.1  gwr 
    335   1.1  gwr kdcngetc(dev)
    336   1.1  gwr 	dev_t dev;
    337   1.1  gwr {
    338   1.8  gwr 	int c;
    339   1.1  gwr 
    340   1.8  gwr 	do {
    341   1.8  gwr 		c = zscngetc(0);
    342   1.8  gwr 		c = kbd_translate(c);
    343   1.8  gwr 	} while (c == -1);
    344   1.1  gwr 
    345   1.1  gwr 	return (c);
    346   1.1  gwr }
    347   1.1  gwr 
    348   1.1  gwr kdcnputc(dev, c)
    349   1.1  gwr 	dev_t dev;
    350   1.1  gwr 	int c;
    351   1.1  gwr {
    352  1.11  gwr 	(romVectorPtr->fbWriteChar)(c & 0x7f);
    353   1.9  gwr }
    354   1.9  gwr 
    355   1.9  gwr extern void fb_unblank();
    356   1.9  gwr void kdcnpollc(dev, on)
    357   1.9  gwr 	dev_t dev;
    358   1.9  gwr 	int on;
    359   1.9  gwr {
    360   1.9  gwr 	if (on)
    361   1.9  gwr 		fb_unblank();
    362   1.1  gwr }
    363