Home | History | Annotate | Line # | Download | only in hpc
biconsdev.c revision 1.20
      1 /*	$NetBSD: biconsdev.c,v 1.20 2007/11/19 18:51:46 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999-2001
      5  *         Shin Takemura and PocketBSD Project. All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the PocketBSD project
     18  *	and its contributors.
     19  * 4. Neither the name of the project nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  */
     36 /*
     37  * Copyright (c) 1995
     38  *	The Regents of the University of California.  All rights reserved.
     39  *
     40  * This code is derived from software contributed to Berkeley by
     41  * Ted Lemon.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. Neither the name of the University nor the names of its contributors
     52  *    may be used to endorse or promote products derived from this software
     53  *    without specific prior written permission.
     54  *
     55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  * SUCH DAMAGE.
     66  *
     67  */
     68 
     69 #include <sys/cdefs.h>
     70 __KERNEL_RCSID(0, "$NetBSD: biconsdev.c,v 1.20 2007/11/19 18:51:46 ad Exp $");
     71 
     72 #include "biconsdev.h"
     73 #include <sys/param.h>
     74 #include <sys/proc.h>
     75 #include <sys/systm.h>
     76 #include <sys/buf.h>
     77 #include <sys/ioctl.h>
     78 #include <sys/tty.h>
     79 #include <sys/conf.h>
     80 #include <sys/kauth.h>
     81 
     82 #include <dev/cons.h>
     83 #include <dev/hpc/bicons.h>
     84 #include <dev/hpc/biconsvar.h>
     85 
     86 struct tty biconsdev_tty[NBICONSDEV];
     87 void	biconsdevattach(int);
     88 static	void biconsdev_output(struct tty *);
     89 
     90 dev_type_open(biconsdevopen);
     91 dev_type_close(biconsdevclose);
     92 dev_type_read(biconsdevread);
     93 dev_type_write(biconsdevwrite);
     94 dev_type_ioctl(biconsdevioctl);
     95 dev_type_tty(biconsdevtty);
     96 dev_type_poll(biconsdevpoll);
     97 
     98 const struct cdevsw biconsdev_cdevsw = {
     99 	biconsdevopen, biconsdevclose, biconsdevread, biconsdevwrite,
    100 	biconsdevioctl, nostop, biconsdevtty, biconsdevpoll, nommap,
    101 	ttykqfilter, D_TTY
    102 };
    103 
    104 void
    105 biconsdevattach(int n)
    106 {
    107 	struct tty *tp = &biconsdev_tty[0];
    108 	int maj;
    109 
    110 	/* locate the major number */
    111 	maj = cdevsw_lookup_major(&biconsdev_cdevsw);
    112 
    113 	/* Set up the tty queues now... */
    114 	clalloc(&tp->t_rawq, 1024, 1);
    115 	clalloc(&tp->t_canq, 1024, 1);
    116 	/* output queue doesn't need quoting */
    117 	clalloc(&tp->t_outq, 1024, 0);
    118 	/* Set default line discipline. */
    119 	tp->t_linesw = ttyldisc_default();
    120 
    121 
    122 	tp->t_dev = makedev(maj, 0);
    123 	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    124 	tp->t_param = (int (*)(struct tty *, struct termios *))nullop;
    125 	tp->t_winsize.ws_row = bicons_height;
    126 	tp->t_winsize.ws_col = bicons_width;
    127 	tp->t_winsize.ws_xpixel = bicons_xpixel;
    128 	tp->t_winsize.ws_ypixel = bicons_ypixel;
    129 	tp->t_oproc = biconsdev_output;
    130 }
    131 
    132 
    133 static void
    134 biconsdev_output(struct tty *tp)
    135 {
    136 	int s, n;
    137 	char buf[OBUFSIZ];
    138 
    139 	s = spltty();
    140 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    141 		splx(s);
    142 		return;
    143 	}
    144 	tp->t_state |= TS_BUSY;
    145 	splx(s);
    146 	n = q_to_b(&tp->t_outq, buf, sizeof(buf));
    147 	bicons_putn(buf, n);
    148 
    149 	s = spltty();
    150 	tp->t_state &= ~TS_BUSY;
    151 	/* Come back if there's more to do */
    152 	if (ttypull(tp)) {
    153 		tp->t_state |= TS_TIMEOUT;
    154 		callout_schedule(&tp->t_rstrt_ch, 1);
    155 	}
    156 	splx(s);
    157 }
    158 
    159 
    160 int
    161 biconsdevopen(dev_t dev, int flag, int mode, struct lwp *l)
    162 {
    163 	struct tty *tp = &biconsdev_tty[0];
    164 	int status;
    165 
    166 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    167 		return (EBUSY);
    168 
    169 	if ((tp->t_state & TS_ISOPEN) == 0) {
    170 		/*
    171 		 * Leave baud rate alone!
    172 		 */
    173 		ttychars(tp);
    174 		tp->t_iflag = TTYDEF_IFLAG;
    175 		tp->t_oflag = TTYDEF_OFLAG;
    176 		tp->t_lflag = TTYDEF_LFLAG;
    177 		tp->t_cflag = TTYDEF_CFLAG;
    178 		tp->t_state = TS_ISOPEN | TS_CARR_ON;
    179 		(void)(*tp->t_param)(tp, &tp->t_termios);
    180 		ttsetwater(tp);
    181 	}
    182 
    183 	status = (*tp->t_linesw->l_open)(dev, tp);
    184 	return status;
    185 }
    186 
    187 
    188 int
    189 biconsdevclose(dev_t dev, int flag, int mode, struct lwp *l)
    190 {
    191 	struct tty *tp = &biconsdev_tty[0];
    192 
    193 	(*tp->t_linesw->l_close)(tp, flag);
    194 	ttyclose(tp);
    195 
    196 	return (0);
    197 }
    198 
    199 
    200 int
    201 biconsdevread(dev_t dev, struct uio *uio, int flag)
    202 {
    203 	struct tty *tp = &biconsdev_tty[0];
    204 
    205 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    206 }
    207 
    208 
    209 int
    210 biconsdevwrite(dev_t dev, struct uio *uio, int flag)
    211 {
    212 	struct tty *tp = &biconsdev_tty[0];
    213 
    214 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    215 }
    216 
    217 
    218 int
    219 biconsdevpoll(dev_t dev, int events, struct lwp *l)
    220 {
    221 	struct tty *tp = &biconsdev_tty[0];
    222 
    223 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    224 }
    225 
    226 
    227 struct tty *
    228 biconsdevtty(dev_t dev)
    229 {
    230 	struct tty *tp = &biconsdev_tty[0];
    231 
    232         return (tp);
    233 }
    234 
    235 int
    236 biconsdevioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    237 {
    238 	struct tty *tp = &biconsdev_tty[0];
    239 	int error;
    240 
    241 	if ((error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l)) !=
    242 	    EPASSTHROUGH)
    243 		return (error);
    244 	return (ttioctl(tp, cmd, data, flag, l));
    245 }
    246