Home | History | Annotate | Line # | Download | only in arcbios
arcbios_tty.c revision 1.22.14.1
      1  1.22.14.1       tls /*	$NetBSD: arcbios_tty.c,v 1.22.14.1 2014/08/20 00:03:35 tls 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.8     perry  *
      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.8     perry  *
     15        1.8     perry  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16        1.8     perry  * 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.8     perry  *
     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.3     lukem 
     30        1.3     lukem #include <sys/cdefs.h>
     31  1.22.14.1       tls __KERNEL_RCSID(0, "$NetBSD: arcbios_tty.c,v 1.22.14.1 2014/08/20 00:03:35 tls Exp $");
     32        1.1   thorpej 
     33        1.1   thorpej #include <sys/param.h>
     34        1.8     perry #include <sys/uio.h>
     35        1.1   thorpej #include <sys/systm.h>
     36        1.1   thorpej #include <sys/callout.h>
     37        1.1   thorpej #include <sys/kernel.h>
     38        1.1   thorpej #include <sys/conf.h>
     39        1.1   thorpej #include <sys/proc.h>
     40        1.1   thorpej #include <sys/tty.h>
     41        1.1   thorpej #include <sys/termios.h>
     42       1.13      yamt #include <sys/kauth.h>
     43        1.1   thorpej 
     44        1.1   thorpej #include <dev/cons.h>
     45        1.1   thorpej 
     46        1.1   thorpej #include <dev/arcbios/arcbios.h>
     47        1.1   thorpej #include <dev/arcbios/arcbiosvar.h>
     48        1.1   thorpej 
     49       1.17        ad callout_t  arcbios_tty_ch;
     50       1.17        ad bool arcbios_ch_init;
     51        1.1   thorpej static struct tty *arcbios_tty[1];
     52        1.1   thorpej 
     53        1.1   thorpej void	arcbios_tty_start(struct tty *);
     54        1.1   thorpej void	arcbios_tty_poll(void *);
     55        1.1   thorpej int	arcbios_tty_param(struct tty *, struct termios *);
     56        1.1   thorpej 
     57        1.5   gehenna dev_type_open(arcbios_ttyopen);
     58        1.5   gehenna dev_type_close(arcbios_ttyclose);
     59        1.5   gehenna dev_type_read(arcbios_ttyread);
     60        1.5   gehenna dev_type_write(arcbios_ttywrite);
     61        1.5   gehenna dev_type_ioctl(arcbios_ttyioctl);
     62        1.5   gehenna dev_type_stop(arcbios_ttystop);
     63        1.5   gehenna dev_type_tty(arcbios_ttytty);
     64        1.5   gehenna dev_type_poll(arcbios_ttypoll);
     65        1.5   gehenna 
     66        1.5   gehenna const struct cdevsw arcbios_cdevsw = {
     67  1.22.14.1       tls 	.d_open = arcbios_ttyopen,
     68  1.22.14.1       tls 	.d_close = arcbios_ttyclose,
     69  1.22.14.1       tls 	.d_read = arcbios_ttyread,
     70  1.22.14.1       tls 	.d_write = arcbios_ttywrite,
     71  1.22.14.1       tls 	.d_ioctl = arcbios_ttyioctl,
     72  1.22.14.1       tls 	.d_stop = arcbios_ttystop,
     73  1.22.14.1       tls 	.d_tty = arcbios_ttytty,
     74  1.22.14.1       tls 	.d_poll = arcbios_ttypoll,
     75  1.22.14.1       tls 	.d_mmap = nommap,
     76  1.22.14.1       tls 	.d_kqfilter = ttykqfilter,
     77  1.22.14.1       tls 	.d_discard = nodiscard,
     78  1.22.14.1       tls 	.d_flag = D_TTY,
     79        1.5   gehenna };
     80        1.1   thorpej 
     81        1.1   thorpej int
     82       1.11  christos arcbios_ttyopen(dev_t dev, int flag, int mode, struct lwp *l)
     83        1.1   thorpej {
     84        1.1   thorpej 	int unit = minor(dev);
     85        1.1   thorpej 	struct tty *tp;
     86        1.2     rafal 	int s, error = 0, setuptimeout = 0;
     87        1.1   thorpej 
     88       1.17        ad 	if (!arcbios_ch_init) {
     89       1.17        ad 		arcbios_ch_init = true;
     90       1.18        ad 		callout_init(&arcbios_tty_ch, 0);
     91       1.17        ad 	}
     92       1.17        ad 
     93        1.1   thorpej 	if (unit != 0)
     94        1.1   thorpej 		return (ENODEV);
     95        1.1   thorpej 
     96        1.1   thorpej 	s = spltty();
     97        1.1   thorpej 
     98        1.1   thorpej 	if (arcbios_tty[unit] == NULL) {
     99       1.22     rmind 		tp = arcbios_tty[unit] = tty_alloc();
    100        1.1   thorpej 		tty_attach(tp);
    101        1.1   thorpej 	} else
    102        1.1   thorpej 		tp = arcbios_tty[unit];
    103        1.1   thorpej 
    104        1.1   thorpej 	tp->t_oproc = arcbios_tty_start;
    105        1.1   thorpej 	tp->t_param = arcbios_tty_param;
    106        1.1   thorpej 	tp->t_dev = dev;
    107       1.15      elad 
    108       1.15      elad 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) {
    109       1.15      elad 		splx(s);
    110       1.15      elad 		return (EBUSY);
    111       1.15      elad 	}
    112       1.15      elad 
    113        1.1   thorpej 	if ((tp->t_state & TS_ISOPEN) == 0) {
    114        1.1   thorpej 		tp->t_state |= TS_CARR_ON;
    115        1.1   thorpej 		ttychars(tp);
    116        1.1   thorpej 		tp->t_iflag = TTYDEF_IFLAG;
    117        1.1   thorpej 		tp->t_oflag = TTYDEF_OFLAG;
    118        1.1   thorpej 		tp->t_cflag = TTYDEF_CFLAG | CLOCAL;
    119        1.1   thorpej 		tp->t_lflag = TTYDEF_LFLAG;
    120        1.1   thorpej 		tp->t_ispeed = tp->t_ospeed = 9600;
    121        1.1   thorpej 		ttsetwater(tp);
    122        1.1   thorpej 
    123        1.1   thorpej 		setuptimeout = 1;
    124        1.1   thorpej 	}
    125        1.1   thorpej 
    126        1.1   thorpej 	splx(s);
    127        1.1   thorpej 
    128        1.1   thorpej 	error = (*tp->t_linesw->l_open)(dev, tp);
    129        1.1   thorpej 	if (error == 0 && setuptimeout)
    130        1.1   thorpej 		callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
    131        1.1   thorpej 
    132        1.1   thorpej 	return (error);
    133        1.1   thorpej }
    134        1.8     perry 
    135        1.1   thorpej int
    136       1.11  christos arcbios_ttyclose(dev_t dev, int flag, int mode, struct lwp *l)
    137        1.1   thorpej {
    138        1.1   thorpej 	int unit = minor(dev);
    139        1.1   thorpej 	struct tty *tp = arcbios_tty[unit];
    140        1.1   thorpej 
    141        1.1   thorpej 	callout_stop(&arcbios_tty_ch);
    142        1.1   thorpej 	(*tp->t_linesw->l_close)(tp, flag);
    143        1.1   thorpej 	ttyclose(tp);
    144        1.1   thorpej 	return (0);
    145        1.1   thorpej }
    146        1.8     perry 
    147        1.1   thorpej int
    148        1.1   thorpej arcbios_ttyread(dev_t dev, struct uio *uio, int flag)
    149        1.1   thorpej {
    150        1.1   thorpej 	struct tty *tp = arcbios_tty[minor(dev)];
    151        1.1   thorpej 
    152        1.1   thorpej 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    153        1.1   thorpej }
    154        1.8     perry 
    155        1.1   thorpej int
    156        1.1   thorpej arcbios_ttywrite(dev_t dev, struct uio *uio, int flag)
    157        1.1   thorpej {
    158        1.1   thorpej 	struct tty *tp = arcbios_tty[minor(dev)];
    159        1.8     perry 
    160        1.1   thorpej 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    161        1.1   thorpej }
    162        1.1   thorpej 
    163        1.1   thorpej int
    164       1.11  christos arcbios_ttypoll(dev_t dev, int events, struct lwp *l)
    165        1.1   thorpej {
    166        1.1   thorpej 	struct tty *tp = arcbios_tty[minor(dev)];
    167        1.8     perry 
    168       1.11  christos 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    169        1.1   thorpej }
    170        1.8     perry 
    171        1.1   thorpej int
    172       1.16  christos arcbios_ttyioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    173        1.1   thorpej {
    174        1.1   thorpej 	int unit = minor(dev);
    175        1.1   thorpej 	struct tty *tp = arcbios_tty[unit];
    176        1.1   thorpej 	int error;
    177        1.1   thorpej 
    178       1.11  christos 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
    179        1.4    atatat 	if (error != EPASSTHROUGH)
    180        1.1   thorpej 		return (error);
    181       1.11  christos 	return (ttioctl(tp, cmd, data, flag, l));
    182        1.1   thorpej }
    183        1.1   thorpej 
    184        1.1   thorpej int
    185        1.1   thorpej arcbios_tty_param(struct tty *tp, struct termios *t)
    186        1.1   thorpej {
    187        1.1   thorpej 
    188        1.1   thorpej 	return (0);
    189        1.1   thorpej }
    190        1.1   thorpej 
    191        1.1   thorpej void
    192        1.1   thorpej arcbios_tty_start(struct tty *tp)
    193        1.1   thorpej {
    194        1.9   tsutsui 	u_long count;
    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_TTSTOP | TS_BUSY))
    199        1.1   thorpej 		goto out;
    200       1.19        ad 	ttypull(tp);
    201        1.1   thorpej 	tp->t_state |= TS_BUSY;
    202        1.1   thorpej 	while (tp->t_outq.c_cc != 0) {
    203       1.21      matt 		arcbios_Write(ARCBIOS_STDOUT, tp->t_outq.c_cf,
    204        1.1   thorpej 		    ndqb(&tp->t_outq, 0), &count);
    205        1.1   thorpej 		ndflush(&tp->t_outq, count);
    206        1.1   thorpej 	}
    207        1.1   thorpej 	tp->t_state &= ~TS_BUSY;
    208        1.1   thorpej  out:
    209        1.1   thorpej 	splx(s);
    210        1.1   thorpej }
    211        1.1   thorpej 
    212        1.1   thorpej void
    213        1.1   thorpej arcbios_ttystop(struct tty *tp, int flag)
    214        1.1   thorpej {
    215        1.1   thorpej 	int s;
    216        1.1   thorpej 
    217        1.1   thorpej 	s = spltty();
    218        1.1   thorpej 	if (tp->t_state & TS_BUSY)
    219        1.1   thorpej 		if ((tp->t_state & TS_TTSTOP) == 0)
    220        1.1   thorpej 			tp->t_state |= TS_FLUSH;
    221        1.1   thorpej 	splx(s);
    222        1.1   thorpej }
    223        1.1   thorpej 
    224        1.1   thorpej static int
    225        1.1   thorpej arcbios_tty_getchar(int *cp)
    226        1.1   thorpej {
    227        1.1   thorpej 	char c;
    228        1.1   thorpej 	int32_t q;
    229        1.9   tsutsui 	u_long count;
    230        1.1   thorpej 
    231       1.21      matt 	q = arcbios_GetReadStatus(ARCBIOS_STDIN);
    232        1.1   thorpej 
    233        1.1   thorpej 	if (q == 0) {
    234       1.21      matt 		arcbios_Read(ARCBIOS_STDIN, &c, 1, &count);
    235        1.8     perry 		*cp = c;
    236        1.1   thorpej 
    237        1.1   thorpej 		return 1;
    238        1.1   thorpej 	}
    239        1.1   thorpej 
    240        1.1   thorpej 	return 0;
    241        1.1   thorpej }
    242        1.1   thorpej 
    243        1.1   thorpej void
    244        1.1   thorpej arcbios_tty_poll(void *v)
    245        1.1   thorpej {
    246        1.1   thorpej 	struct tty *tp = v;
    247  1.22.14.1       tls 	int c;
    248        1.1   thorpej 
    249        1.1   thorpej 	while (arcbios_tty_getchar(&c)) {
    250        1.1   thorpej 		if (tp->t_state & TS_ISOPEN)
    251  1.22.14.1       tls 			(void)(*tp->t_linesw->l_rint)(c, tp);
    252        1.1   thorpej 	}
    253        1.1   thorpej 	callout_reset(&arcbios_tty_ch, 1, arcbios_tty_poll, tp);
    254        1.1   thorpej }
    255        1.1   thorpej 
    256        1.1   thorpej struct tty *
    257        1.1   thorpej arcbios_ttytty(dev_t dev)
    258        1.1   thorpej {
    259        1.1   thorpej 
    260        1.1   thorpej 	if (minor(dev) != 0)
    261        1.1   thorpej 		panic("arcbios_ttytty: bogus");
    262        1.1   thorpej 
    263        1.1   thorpej 	return (arcbios_tty[0]);
    264        1.1   thorpej }
    265