Home | History | Annotate | Line # | Download | only in hpc
      1 /*	$NetBSD: biconsdev.c,v 1.23 2015/08/20 14:40:18 christos 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.23 2015/08/20 14:40:18 christos 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 #include "ioconf.h"
     87 
     88 struct tty biconsdev_tty[NBICONSDEV];
     89 static	void biconsdev_output(struct tty *);
     90 
     91 dev_type_open(biconsdevopen);
     92 dev_type_close(biconsdevclose);
     93 dev_type_read(biconsdevread);
     94 dev_type_write(biconsdevwrite);
     95 dev_type_ioctl(biconsdevioctl);
     96 dev_type_tty(biconsdevtty);
     97 dev_type_poll(biconsdevpoll);
     98 
     99 const struct cdevsw biconsdev_cdevsw = {
    100 	.d_open = biconsdevopen,
    101 	.d_close = biconsdevclose,
    102 	.d_read = biconsdevread,
    103 	.d_write = biconsdevwrite,
    104 	.d_ioctl = biconsdevioctl,
    105 	.d_stop = nostop,
    106 	.d_tty = biconsdevtty,
    107 	.d_poll = biconsdevpoll,
    108 	.d_mmap = nommap,
    109 	.d_kqfilter = ttykqfilter,
    110 	.d_discard = nodiscard,
    111 	.d_flag = D_TTY
    112 };
    113 
    114 void
    115 biconsdevattach(int n)
    116 {
    117 	struct tty *tp = &biconsdev_tty[0];
    118 	int maj;
    119 
    120 	/* locate the major number */
    121 	maj = cdevsw_lookup_major(&biconsdev_cdevsw);
    122 
    123 	/* Set up the tty queues now... */
    124 	clalloc(&tp->t_rawq, 1024, 1);
    125 	clalloc(&tp->t_canq, 1024, 1);
    126 	/* output queue doesn't need quoting */
    127 	clalloc(&tp->t_outq, 1024, 0);
    128 	/* Set default line discipline. */
    129 	tp->t_linesw = ttyldisc_default();
    130 
    131 
    132 	tp->t_dev = makedev(maj, 0);
    133 	tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    134 	tp->t_param = (int (*)(struct tty *, struct termios *))nullop;
    135 	tp->t_winsize.ws_row = bicons_height;
    136 	tp->t_winsize.ws_col = bicons_width;
    137 	tp->t_winsize.ws_xpixel = bicons_xpixel;
    138 	tp->t_winsize.ws_ypixel = bicons_ypixel;
    139 	tp->t_oproc = biconsdev_output;
    140 }
    141 
    142 
    143 static void
    144 biconsdev_output(struct tty *tp)
    145 {
    146 	int s, n;
    147 	char buf[OBUFSIZ];
    148 
    149 	s = spltty();
    150 	if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) {
    151 		splx(s);
    152 		return;
    153 	}
    154 	tp->t_state |= TS_BUSY;
    155 	splx(s);
    156 	n = q_to_b(&tp->t_outq, buf, sizeof(buf));
    157 	bicons_putn(buf, n);
    158 
    159 	s = spltty();
    160 	tp->t_state &= ~TS_BUSY;
    161 	/* Come back if there's more to do */
    162 	if (ttypull(tp)) {
    163 		tp->t_state |= TS_TIMEOUT;
    164 		callout_schedule(&tp->t_rstrt_ch, 1);
    165 	}
    166 	splx(s);
    167 }
    168 
    169 
    170 int
    171 biconsdevopen(dev_t dev, int flag, int mode, struct lwp *l)
    172 {
    173 	struct tty *tp = &biconsdev_tty[0];
    174 	int status;
    175 
    176 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    177 		return (EBUSY);
    178 
    179 	if ((tp->t_state & TS_ISOPEN) == 0) {
    180 		/*
    181 		 * Leave baud rate alone!
    182 		 */
    183 		ttychars(tp);
    184 		tp->t_iflag = TTYDEF_IFLAG;
    185 		tp->t_oflag = TTYDEF_OFLAG;
    186 		tp->t_lflag = TTYDEF_LFLAG;
    187 		tp->t_cflag = TTYDEF_CFLAG;
    188 		tp->t_state = TS_ISOPEN | TS_CARR_ON;
    189 		(void)(*tp->t_param)(tp, &tp->t_termios);
    190 		ttsetwater(tp);
    191 	}
    192 
    193 	status = (*tp->t_linesw->l_open)(dev, tp);
    194 	return status;
    195 }
    196 
    197 
    198 int
    199 biconsdevclose(dev_t dev, int flag, int mode, struct lwp *l)
    200 {
    201 	struct tty *tp = &biconsdev_tty[0];
    202 
    203 	(*tp->t_linesw->l_close)(tp, flag);
    204 	ttyclose(tp);
    205 
    206 	return (0);
    207 }
    208 
    209 
    210 int
    211 biconsdevread(dev_t dev, struct uio *uio, int flag)
    212 {
    213 	struct tty *tp = &biconsdev_tty[0];
    214 
    215 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
    216 }
    217 
    218 
    219 int
    220 biconsdevwrite(dev_t dev, struct uio *uio, int flag)
    221 {
    222 	struct tty *tp = &biconsdev_tty[0];
    223 
    224 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
    225 }
    226 
    227 
    228 int
    229 biconsdevpoll(dev_t dev, int events, struct lwp *l)
    230 {
    231 	struct tty *tp = &biconsdev_tty[0];
    232 
    233 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    234 }
    235 
    236 
    237 struct tty *
    238 biconsdevtty(dev_t dev)
    239 {
    240 	struct tty *tp = &biconsdev_tty[0];
    241 
    242         return (tp);
    243 }
    244 
    245 int
    246 biconsdevioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    247 {
    248 	struct tty *tp = &biconsdev_tty[0];
    249 	int error;
    250 
    251 	if ((error = tp->t_linesw->l_ioctl(tp, cmd, data, flag, l)) !=
    252 	    EPASSTHROUGH)
    253 		return (error);
    254 	return (ttioctl(tp, cmd, data, flag, l));
    255 }
    256