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