Home | History | Annotate | Line # | Download | only in hpc
biconsdev.c revision 1.17
      1 /*	$NetBSD: biconsdev.c,v 1.17 2006/10/01 19:28:43 elad 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.17 2006/10/01 19:28:43 elad 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 (tp->t_outq.c_cc) {
    153 		tp->t_state |= TS_TIMEOUT;
    154 		callout_reset(&tp->t_rstrt_ch, 1, ttrstrt, tp);
    155 	}
    156 	if (tp->t_outq.c_cc <= tp->t_lowat) {
    157 		if (tp->t_state&TS_ASLEEP) {
    158 			tp->t_state &= ~TS_ASLEEP;
    159 			wakeup((caddr_t)&tp->t_outq);
    160 		}
    161 		selwakeup(&tp->t_wsel);
    162 	}
    163 	splx(s);
    164 }
    165 
    166 
    167 int
    168 biconsdevopen(dev_t dev, int flag, int mode, struct lwp *l)
    169 {
    170 	struct tty *tp = &biconsdev_tty[0];
    171 	int status;
    172 
    173 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    174 		return (EBUSY);
    175 
    176 	if ((tp->t_state & TS_ISOPEN) == 0) {
    177 		/*
    178 		 * Leave baud rate alone!
    179 		 */
    180 		ttychars(tp);
    181 		tp->t_iflag = TTYDEF_IFLAG;
    182 		tp->t_oflag = TTYDEF_OFLAG;
    183 		tp->t_lflag = TTYDEF_LFLAG;
    184 		tp->t_cflag = TTYDEF_CFLAG;
    185 		tp->t_state = TS_ISOPEN | TS_CARR_ON;
    186 		(void)(*tp->t_param)(tp, &tp->t_termios);
    187 		ttsetwater(tp);
    188 	}
    189 
    190 	status = (*tp->t_linesw->l_open)(dev, tp);
    191 	return status;
    192 }
    193 
    194 
    195 int
    196 biconsdevclose(dev_t dev, int flag, int mode, struct lwp *l)
    197 {
    198 	struct tty *tp = &biconsdev_tty[0];
    199 
    200 	(*tp->t_linesw->l_close)(tp, flag);
    201 	ttyclose(tp);
    202 
    203 	return (0);
    204 }
    205 
    206 
    207 int
    208 biconsdevread(dev_t dev, struct uio *uio, int flag)
    209 {
    210 	struct tty *tp = &biconsdev_tty[0];
    211 
    212 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    213 }
    214 
    215 
    216 int
    217 biconsdevwrite(dev_t dev, struct uio *uio, int flag)
    218 {
    219 	struct tty *tp = &biconsdev_tty[0];
    220 
    221 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    222 }
    223 
    224 
    225 int
    226 biconsdevpoll(dev_t dev, int events, struct lwp *l)
    227 {
    228 	struct tty *tp = &biconsdev_tty[0];
    229 
    230 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    231 }
    232 
    233 
    234 struct tty *
    235 biconsdevtty(dev_t dev)
    236 {
    237 	struct tty *tp = &biconsdev_tty[0];
    238 
    239         return (tp);
    240 }
    241 
    242 int
    243 biconsdevioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    244 {
    245 	struct tty *tp = &biconsdev_tty[0];
    246 	int error;
    247 
    248 	if ((error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l)) !=
    249 	    EPASSTHROUGH)
    250 		return (error);
    251 	return (ttioctl(tp, cmd, data, flag, l));
    252 }
    253