Home | History | Annotate | Line # | Download | only in dev
kd.c revision 1.12
      1  1.12  mycroft /*	$NetBSD: kd.c,v 1.12 1995/04/10 07:33:53 mycroft 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.12  mycroft }
    163  1.12  mycroft 
    164  1.12  mycroft int
    165  1.12  mycroft kdstop(tp, flag)
    166  1.12  mycroft 	struct tty *tp;
    167  1.12  mycroft 	int flag;
    168  1.12  mycroft {
    169  1.12  mycroft 
    170   1.1      gwr }
    171   1.1      gwr 
    172   1.1      gwr int
    173   1.1      gwr kdioctl(dev, cmd, data, flag, p)
    174   1.1      gwr 	dev_t dev;
    175   1.1      gwr 	int cmd;
    176   1.1      gwr 	caddr_t data;
    177   1.1      gwr 	int flag;
    178   1.1      gwr 	struct proc *p;
    179   1.1      gwr {
    180   1.1      gwr 	int error;
    181   1.1      gwr 	int unit = minor(dev);
    182   1.1      gwr 	struct tty *tp = kd_tty[unit];
    183   1.1      gwr 
    184   1.1      gwr 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
    185   1.1      gwr 	if (error >= 0)
    186   1.1      gwr 		return error;
    187   1.1      gwr 	error = ttioctl(tp, cmd, data, flag, p);
    188   1.1      gwr 	if (error >= 0)
    189   1.1      gwr 		return error;
    190   1.1      gwr 
    191   1.1      gwr 	/* Handle any ioctl commands specific to kbd/display. */
    192   1.1      gwr 	/* XXX - Send KB* ioctls to kbd module? */
    193   1.1      gwr 	/* XXX - Send FB* ioctls to fb module?  */
    194   1.1      gwr 
    195   1.1      gwr 	return ENOTTY;
    196   1.1      gwr }
    197   1.1      gwr 
    198  1.11      gwr static void kd_later(void*);
    199  1.11      gwr static void kd_putfb(struct tty *);
    200  1.11      gwr 
    201   1.1      gwr void
    202   1.1      gwr kdstart(tp)
    203   1.1      gwr 	struct tty *tp;
    204   1.1      gwr {
    205   1.1      gwr 	struct clist *cl;
    206  1.11      gwr 	register int s;
    207   1.1      gwr 
    208   1.1      gwr 	s = spltty();
    209   1.1      gwr 	if (tp->t_state & (TS_BUSY|TS_TTSTOP|TS_TIMEOUT))
    210   1.1      gwr 		goto out;
    211  1.11      gwr 
    212   1.1      gwr 	cl = &tp->t_outq;
    213  1.11      gwr 	if (cl->c_cc) {
    214  1.10      gwr 		if (kd_is_console) {
    215  1.11      gwr 			tp->t_state |= TS_BUSY;
    216  1.11      gwr 			if ((s & PSL_IPL) == 0) {
    217  1.11      gwr 				/* called at level zero - update screen now. */
    218  1.11      gwr 				(void) splsoftclock();
    219  1.11      gwr 				kd_putfb(tp);
    220  1.11      gwr 				(void) spltty();
    221  1.11      gwr 				tp->t_state &= ~TS_BUSY;
    222  1.11      gwr 			} else {
    223  1.11      gwr 				/* called at interrupt level - do it later */
    224  1.11      gwr 				timeout(kd_later, (void*)tp, 0);
    225  1.11      gwr 			}
    226  1.11      gwr 		} else {
    227  1.11      gwr 			/*
    228  1.11      gwr 			 * This driver uses the PROM for writing the screen,
    229  1.11      gwr 			 * and that only works if this is the console device.
    230  1.11      gwr 			 * If this is not the console, just flush the output.
    231  1.11      gwr 			 * Sorry.  (In that case, use xdm instead of getty.)
    232  1.11      gwr 			 */
    233  1.11      gwr 			ndflush(cl, cl->c_cc);
    234  1.11      gwr 		}
    235  1.11      gwr 	}
    236  1.11      gwr 	if (cl->c_cc <= tp->t_lowat) {
    237  1.11      gwr 		if (tp->t_state & TS_ASLEEP) {
    238  1.11      gwr 			tp->t_state &= ~TS_ASLEEP;
    239  1.11      gwr 			wakeup((caddr_t)cl);
    240  1.10      gwr 		}
    241  1.11      gwr 		selwakeup(&tp->t_wsel);
    242   1.1      gwr 	}
    243  1.11      gwr out:
    244  1.11      gwr 	splx(s);
    245  1.11      gwr }
    246  1.11      gwr 
    247  1.11      gwr /*
    248  1.11      gwr  * Timeout function to do delayed writes to the screen.
    249  1.11      gwr  * Called at splsoftclock when requested by kdstart.
    250  1.11      gwr  */
    251  1.11      gwr static void
    252  1.11      gwr kd_later(tpaddr)
    253  1.11      gwr 	void *tpaddr;
    254  1.11      gwr {
    255  1.11      gwr 	struct tty *tp = tpaddr;
    256  1.11      gwr 	register int s;
    257   1.1      gwr 
    258  1.11      gwr 	kd_putfb(tp);
    259  1.11      gwr 
    260  1.11      gwr 	s = spltty();
    261   1.1      gwr 	tp->t_state &= ~TS_BUSY;
    262  1.11      gwr 	if (tp->t_line)
    263  1.11      gwr 		(*linesw[tp->t_line].l_start)(tp);
    264  1.11      gwr 	else
    265  1.11      gwr 		kdstart(tp);
    266  1.11      gwr 	splx(s);
    267  1.11      gwr }
    268  1.11      gwr 
    269  1.11      gwr /*
    270  1.11      gwr  * Put text on the screen using the PROM monitor.
    271  1.11      gwr  * This can take a while, so to avoid missing
    272  1.11      gwr  * interrupts, this is called at splsoftclock.
    273  1.11      gwr  */
    274  1.11      gwr static void kd_putfb(tp)
    275  1.11      gwr 	struct tty *tp;
    276  1.11      gwr {
    277  1.11      gwr 	char buf[BURST];
    278  1.11      gwr 	struct clist *cl = &tp->t_outq;
    279  1.11      gwr 	char *p, *end;
    280  1.11      gwr 	int len;
    281  1.11      gwr 
    282  1.11      gwr 	while ((len = q_to_b(cl, buf, BURST-1)) > 0) {
    283  1.11      gwr 		/* PROM will barf if high bits are set. */
    284  1.11      gwr 		p = buf;
    285  1.11      gwr 		end = buf + len;
    286  1.11      gwr 		while (p < end)
    287  1.11      gwr 			*p++ &= 0x7f;
    288  1.11      gwr 		(romVectorPtr->fbWriteStr)(buf, len);
    289   1.1      gwr 	}
    290   1.1      gwr }
    291   1.1      gwr 
    292  1.11      gwr 
    293   1.1      gwr static int
    294   1.1      gwr kdparam(tp, t)
    295   1.1      gwr 	struct tty *tp;
    296   1.1      gwr 	struct termios *t;
    297   1.1      gwr {
    298   1.1      gwr 	/* XXX - These are ignored... */
    299   1.1      gwr 	tp->t_ispeed = t->c_ispeed;
    300   1.1      gwr 	tp->t_ospeed = t->c_ospeed;
    301   1.1      gwr 	tp->t_cflag = t->c_cflag;
    302   1.1      gwr 	return 0;
    303   1.1      gwr }
    304   1.1      gwr 
    305   1.1      gwr 
    306   1.1      gwr /*
    307   1.1      gwr  * kd console support
    308   1.1      gwr  */
    309   1.1      gwr 
    310   1.8      gwr extern int zscnprobe_kbd(), zscngetc(), kbd_translate();
    311   1.1      gwr 
    312   1.1      gwr kdcnprobe(cp)
    313   1.1      gwr 	struct consdev *cp;
    314   1.1      gwr {
    315   1.1      gwr 	int maj;
    316   1.1      gwr 
    317   1.1      gwr 	/* locate the major number */
    318   1.1      gwr 	for (maj = 0; maj < nchrdev; maj++)
    319   1.6      gwr 		if (cdevsw[maj].d_open == (void*)kdopen)
    320   1.1      gwr 			break;
    321   1.1      gwr 
    322   1.1      gwr 	/* initialize required fields */
    323   1.1      gwr 	cp->cn_dev = makedev(maj, 0);
    324   1.1      gwr 	cp->cn_pri = zscnprobe_kbd();
    325   1.1      gwr }
    326   1.1      gwr 
    327   1.1      gwr kdcninit(cp)
    328   1.1      gwr 	struct consdev *cp;
    329   1.1      gwr {
    330   1.8      gwr 
    331   1.8      gwr 	/* This prepares zscngetc() */
    332   1.8      gwr 	zs_set_conschan(1, 0);
    333   1.8      gwr 
    334   1.8      gwr 	/* This prepares kbd_translate() */
    335   1.8      gwr 	kbd_init_tables();
    336  1.10      gwr 
    337  1.10      gwr 	/* Indicate that it is OK to use the PROM fbwrite */
    338  1.10      gwr 	kd_is_console = 1;
    339   1.8      gwr 
    340   1.1      gwr 	mon_printf("console on kd0 (keyboard/display)\n");
    341   1.1      gwr }
    342   1.1      gwr 
    343   1.1      gwr kdcngetc(dev)
    344   1.1      gwr 	dev_t dev;
    345   1.1      gwr {
    346   1.8      gwr 	int c;
    347   1.1      gwr 
    348   1.8      gwr 	do {
    349   1.8      gwr 		c = zscngetc(0);
    350   1.8      gwr 		c = kbd_translate(c);
    351   1.8      gwr 	} while (c == -1);
    352   1.1      gwr 
    353   1.1      gwr 	return (c);
    354   1.1      gwr }
    355   1.1      gwr 
    356   1.1      gwr kdcnputc(dev, c)
    357   1.1      gwr 	dev_t dev;
    358   1.1      gwr 	int c;
    359   1.1      gwr {
    360  1.11      gwr 	(romVectorPtr->fbWriteChar)(c & 0x7f);
    361   1.9      gwr }
    362   1.9      gwr 
    363   1.9      gwr extern void fb_unblank();
    364   1.9      gwr void kdcnpollc(dev, on)
    365   1.9      gwr 	dev_t dev;
    366   1.9      gwr 	int on;
    367   1.9      gwr {
    368   1.9      gwr 	if (on)
    369   1.9      gwr 		fb_unblank();
    370   1.1      gwr }
    371