Home | History | Annotate | Line # | Download | only in arcbios
arcbios_tty.c revision 1.3
      1 /*	$NetBSD: arcbios_tty.c,v 1.3 2001/11/13 12:53:24 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: arcbios_tty.c,v 1.3 2001/11/13 12:53:24 lukem Exp $");
     32 
     33 #include <sys/param.h>
     34 #include <sys/user.h>
     35 #include <sys/uio.h>
     36 #include <sys/systm.h>
     37 #include <sys/callout.h>
     38 #include <sys/kernel.h>
     39 #include <sys/conf.h>
     40 #include <sys/proc.h>
     41 #include <sys/tty.h>
     42 #include <sys/termios.h>
     43 
     44 #include <dev/cons.h>
     45 
     46 #include <dev/arcbios/arcbios.h>
     47 #include <dev/arcbios/arcbiosvar.h>
     48 
     49 struct callout arcbios_tty_ch = CALLOUT_INITIALIZER;
     50 
     51 static struct tty *arcbios_tty[1];
     52 
     53 void	arcbios_tty_start(struct tty *);
     54 void	arcbios_tty_poll(void *);
     55 int	arcbios_tty_param(struct tty *, struct termios *);
     56 
     57 cdev_decl(arcbios_tty);
     58 
     59 int
     60 arcbios_ttyopen(dev_t dev, int flag, int mode, struct proc *p)
     61 {
     62 	int unit = minor(dev);
     63 	struct tty *tp;
     64 	int s, error = 0, setuptimeout = 0;
     65 
     66 	if (unit != 0)
     67 		return (ENODEV);
     68 
     69 	s = spltty();
     70 
     71 	if (arcbios_tty[unit] == NULL) {
     72 		tp = arcbios_tty[unit] = ttymalloc();
     73 		tty_attach(tp);
     74 	} else
     75 		tp = arcbios_tty[unit];
     76 
     77 	tp->t_oproc = arcbios_tty_start;
     78 	tp->t_param = arcbios_tty_param;
     79 	tp->t_dev = dev;
     80 	if ((tp->t_state & TS_ISOPEN) == 0) {
     81 		tp->t_state |= TS_CARR_ON;
     82 		ttychars(tp);
     83 		tp->t_iflag = TTYDEF_IFLAG;
     84 		tp->t_oflag = TTYDEF_OFLAG;
     85 		tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
     86 		tp->t_lflag = TTYDEF_LFLAG;
     87 		tp->t_ispeed = tp->t_ospeed = 9600;
     88 		ttsetwater(tp);
     89 
     90 		setuptimeout = 1;
     91 	} else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
     92 		splx(s);
     93 		return (EBUSY);
     94 	}
     95 
     96 	splx(s);
     97 
     98 	error = (*tp->t_linesw->l_open)(dev, tp);
     99 	if (error == 0 && setuptimeout)
    100 		callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
    101 
    102 	return (error);
    103 }
    104 
    105 int
    106 arcbios_ttyclose(dev_t dev, int flag, int mode, struct proc *p)
    107 {
    108 	int unit = minor(dev);
    109 	struct tty *tp = arcbios_tty[unit];
    110 
    111 	callout_stop(&arcbios_tty_ch);
    112 	(*tp->t_linesw->l_close)(tp, flag);
    113 	ttyclose(tp);
    114 	return (0);
    115 }
    116 
    117 int
    118 arcbios_ttyread(dev_t dev, struct uio *uio, int flag)
    119 {
    120 	struct tty *tp = arcbios_tty[minor(dev)];
    121 
    122 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    123 }
    124 
    125 int
    126 arcbios_ttywrite(dev_t dev, struct uio *uio, int flag)
    127 {
    128 	struct tty *tp = arcbios_tty[minor(dev)];
    129 
    130 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    131 }
    132 
    133 int
    134 arcbios_ttypoll(dev_t dev, int events, struct proc *p)
    135 {
    136 	struct tty *tp = arcbios_tty[minor(dev)];
    137 
    138 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    139 }
    140 
    141 int
    142 arcbios_ttyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    143 {
    144 	int unit = minor(dev);
    145 	struct tty *tp = arcbios_tty[unit];
    146 	int error;
    147 
    148 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
    149 	if (error >= 0)
    150 		return (error);
    151 	error = ttioctl(tp, cmd, data, flag, p);
    152 	if (error >= 0)
    153 		return (error);
    154 
    155 	return (ENOTTY);
    156 }
    157 
    158 int
    159 arcbios_tty_param(struct tty *tp, struct termios *t)
    160 {
    161 
    162 	return (0);
    163 }
    164 
    165 void
    166 arcbios_tty_start(struct tty *tp)
    167 {
    168 	uint32_t count;
    169 	int s;
    170 
    171 	s = spltty();
    172 	if (tp->t_state & (TS_TTSTOP | TS_BUSY))
    173 		goto out;
    174 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    175 		if (tp->t_state & TS_ASLEEP) {
    176 			tp->t_state &= ~TS_ASLEEP;
    177 			wakeup((caddr_t)&tp->t_outq);
    178 		}
    179 		selwakeup(&tp->t_wsel);
    180 	}
    181 	tp->t_state |= TS_BUSY;
    182 	while (tp->t_outq.c_cc != 0) {
    183 		(*ARCBIOS->Write)(ARCBIOS_STDOUT, tp->t_outq.c_cf,
    184 		    ndqb(&tp->t_outq, 0), &count);
    185 		ndflush(&tp->t_outq, count);
    186 	}
    187 	tp->t_state &= ~TS_BUSY;
    188  out:
    189 	splx(s);
    190 }
    191 
    192 void
    193 arcbios_ttystop(struct tty *tp, int flag)
    194 {
    195 	int s;
    196 
    197 	s = spltty();
    198 	if (tp->t_state & TS_BUSY)
    199 		if ((tp->t_state & TS_TTSTOP) == 0)
    200 			tp->t_state |= TS_FLUSH;
    201 	splx(s);
    202 }
    203 
    204 static int
    205 arcbios_tty_getchar(int *cp)
    206 {
    207 	char c;
    208 	int32_t q;
    209 	u_int32_t count;
    210 
    211 	q = ARCBIOS->GetReadStatus(ARCBIOS_STDIN);
    212 
    213 	if (q == 0) {
    214 		ARCBIOS->Read(ARCBIOS_STDIN, &c, 1, &count);
    215 		*cp = c;
    216 
    217 		return 1;
    218 	}
    219 
    220 	return 0;
    221 }
    222 
    223 void
    224 arcbios_tty_poll(void *v)
    225 {
    226 	struct tty *tp = v;
    227 	int c, l_r;
    228 
    229 	while (arcbios_tty_getchar(&c)) {
    230 		if (tp->t_state & TS_ISOPEN)
    231 			l_r = (*tp->t_linesw->l_rint)(c, tp);
    232 	}
    233 	callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
    234 }
    235 
    236 struct tty *
    237 arcbios_ttytty(dev_t dev)
    238 {
    239 
    240 	if (minor(dev) != 0)
    241 		panic("arcbios_ttytty: bogus");
    242 
    243 	return (arcbios_tty[0]);
    244 }
    245