Home | History | Annotate | Line # | Download | only in dev
nullcons_subr.c revision 1.6.8.1
      1  1.6.8.1     skrll /*	$NetBSD: nullcons_subr.c,v 1.6.8.1 2009/03/03 18:30:31 skrll Exp $	*/
      2      1.1       cdi 
      3      1.1       cdi /*-
      4      1.1       cdi  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5      1.1       cdi  * All rights reserved.
      6      1.1       cdi  *
      7      1.1       cdi  * Redistribution and use in source and binary forms, with or without
      8      1.1       cdi  * modification, are permitted provided that the following conditions
      9      1.1       cdi  * are met:
     10      1.1       cdi  * 1. Redistributions of source code must retain the above copyright
     11      1.1       cdi  *    notice, this list of conditions and the following disclaimer.
     12      1.1       cdi  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1       cdi  *    notice, this list of conditions and the following disclaimer in the
     14      1.1       cdi  *    documentation and/or other materials provided with the distribution.
     15      1.1       cdi  *
     16      1.1       cdi  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17      1.1       cdi  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18      1.1       cdi  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19      1.1       cdi  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20      1.1       cdi  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21      1.1       cdi  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22      1.1       cdi  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23      1.1       cdi  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24      1.1       cdi  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25      1.1       cdi  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26      1.1       cdi  * POSSIBILITY OF SUCH DAMAGE.
     27      1.1       cdi  */
     28      1.1       cdi 
     29      1.1       cdi #include <sys/cdefs.h>
     30  1.6.8.1     skrll __KERNEL_RCSID(0, "$NetBSD: nullcons_subr.c,v 1.6.8.1 2009/03/03 18:30:31 skrll Exp $");
     31      1.1       cdi 
     32      1.1       cdi #include <sys/param.h>
     33      1.1       cdi #include <sys/proc.h>
     34      1.1       cdi #include <sys/user.h>
     35      1.1       cdi #include <sys/systm.h>
     36      1.1       cdi #include <sys/buf.h>
     37      1.1       cdi #include <sys/ioctl.h>
     38      1.1       cdi #include <sys/tty.h>
     39      1.1       cdi #include <sys/file.h>
     40      1.1       cdi #include <sys/conf.h>
     41      1.1       cdi #include <sys/vnode.h>
     42      1.1       cdi 
     43      1.1       cdi #include <dev/cons.h>
     44      1.1       cdi 
     45      1.1       cdi 
     46      1.1       cdi extern struct consdev *cn_tab;		/* physical console device info */
     47      1.1       cdi 
     48      1.1       cdi static struct tty *nulltty;		/* null console tty */
     49      1.1       cdi 
     50      1.1       cdi cons_decl(null);
     51      1.1       cdi 
     52      1.1       cdi dev_type_read(nullcndev_read);
     53      1.1       cdi dev_type_ioctl(nullcndev_ioctl);
     54      1.1       cdi dev_type_tty(nullcndev_tty);
     55      1.1       cdi 
     56      1.2     perry static int	nullcons_newdev(struct consdev *);
     57      1.1       cdi 
     58      1.1       cdi const struct cdevsw nullcn_devsw = {
     59      1.1       cdi 	nullopen, nullclose, nullcndev_read, nullwrite, nullcndev_ioctl,
     60      1.1       cdi 	nullstop, nullcndev_tty, nopoll, nommap, ttykqfilter, D_TTY
     61      1.1       cdi };
     62      1.1       cdi 
     63      1.1       cdi /*
     64      1.1       cdi  * null console device. We need it because of the ioctl() it handles,
     65      1.1       cdi  * which in particular allows control terminal allocation through
     66      1.1       cdi  * TIOCSCTTY ioctl. Without the latter, system won't even boot past init(8)
     67      1.1       cdi  * invocation.
     68      1.1       cdi  */
     69      1.1       cdi int
     70      1.1       cdi nullcndev_read(dev, uio, flag)
     71      1.1       cdi 	dev_t dev;
     72      1.1       cdi 	struct uio *uio;
     73      1.1       cdi 	int flag;
     74      1.1       cdi {
     75      1.1       cdi 
     76  1.6.8.1     skrll 	return EIO;
     77      1.1       cdi }
     78      1.1       cdi 
     79      1.1       cdi int
     80      1.4  christos nullcndev_ioctl(dev, cmd, data, flag, l)
     81      1.1       cdi 	dev_t dev;
     82      1.1       cdi 	u_long cmd;
     83      1.5  christos 	void *data;
     84      1.1       cdi 	int flag;
     85      1.4  christos 	struct lwp *l;
     86      1.1       cdi {
     87      1.1       cdi 	int error;
     88      1.1       cdi 
     89      1.4  christos 	error = (*nulltty->t_linesw->l_ioctl)(nulltty, cmd, data, flag, l);
     90      1.1       cdi 	if (error != EPASSTHROUGH)
     91      1.1       cdi 		return (error);
     92      1.1       cdi 
     93      1.4  christos 	error = ttioctl(nulltty, cmd, data, flag, l);
     94      1.1       cdi 	if (error != EPASSTHROUGH)
     95      1.1       cdi 		return (error);
     96      1.1       cdi 
     97      1.1       cdi 	return (0);
     98      1.1       cdi }
     99      1.1       cdi 
    100      1.1       cdi struct tty*
    101      1.1       cdi nullcndev_tty(dev)
    102      1.1       cdi 	dev_t dev;
    103      1.1       cdi {
    104      1.1       cdi 
    105      1.1       cdi 	return nulltty;
    106      1.1       cdi }
    107      1.1       cdi 
    108      1.1       cdi /*
    109      1.1       cdi  * Mark console as no-op (null) console. Proper initialization is deferred
    110      1.1       cdi  * to nullconsattach().
    111      1.1       cdi  */
    112      1.1       cdi void
    113      1.1       cdi nullcnprobe(cn)
    114      1.1       cdi 	struct consdev *cn;
    115      1.1       cdi {
    116      1.1       cdi 
    117      1.1       cdi 	cn->cn_pri = CN_NULL;
    118      1.1       cdi 	cn->cn_dev = NODEV;
    119      1.1       cdi }
    120      1.1       cdi 
    121      1.1       cdi /*
    122      1.1       cdi  * null console initialization. This includes allocation of a new device and
    123      1.1       cdi  * a new tty.
    124      1.1       cdi  */
    125      1.1       cdi void
    126      1.1       cdi nullcninit(cn)
    127      1.1       cdi 	struct consdev *cn;
    128      1.1       cdi {
    129      1.1       cdi 	static struct consdev nullcn = cons_init(null);
    130      1.1       cdi 
    131      1.1       cdi 	nullcnprobe(&nullcn);
    132      1.1       cdi 	cn_tab = &nullcn;
    133      1.1       cdi }
    134      1.1       cdi 
    135      1.1       cdi /*
    136      1.1       cdi  * Dumb getc() implementation. Simply blocks on call.
    137      1.1       cdi  */
    138      1.1       cdi int
    139      1.1       cdi nullcngetc(dev)
    140      1.1       cdi 	dev_t dev;
    141      1.1       cdi {
    142      1.1       cdi 
    143      1.1       cdi 	for(;;);
    144      1.1       cdi 	return (0);
    145      1.1       cdi }
    146      1.1       cdi 
    147      1.1       cdi /*
    148      1.1       cdi  * Dumb putc() implementation.
    149      1.1       cdi  */
    150      1.1       cdi void
    151      1.1       cdi nullcnputc(dev, c)
    152      1.1       cdi 	dev_t dev;
    153      1.1       cdi 	int c;
    154      1.1       cdi {
    155      1.1       cdi 
    156      1.1       cdi }
    157      1.1       cdi 
    158      1.1       cdi /*
    159      1.1       cdi  * Allocate a new console device and a tty to handle console ioctls.
    160      1.1       cdi  */
    161      1.1       cdi int
    162      1.1       cdi nullcons_newdev(cn)
    163      1.1       cdi 	struct consdev *cn;
    164      1.1       cdi {
    165      1.1       cdi 	int error;
    166      1.1       cdi 	int bmajor = -1, cmajor = -1;
    167      1.1       cdi 
    168      1.1       cdi 	if ((cn == NULL) || (cn->cn_pri != CN_NULL) || (cn->cn_dev != NODEV))
    169      1.1       cdi 		return (0);
    170      1.1       cdi 
    171      1.1       cdi 	/*
    172      1.1       cdi 	 * Attach no-op device to the device list.
    173      1.1       cdi 	 */
    174      1.1       cdi 	error = devsw_attach("nullcn", NULL, &bmajor, &nullcn_devsw, &cmajor);
    175      1.1       cdi 	if (error != 0)
    176      1.1       cdi 		return (error);
    177      1.1       cdi 
    178      1.1       cdi 	/*
    179      1.1       cdi 	 * Allocate tty (mostly to have sane ioctl()).
    180      1.1       cdi 	 */
    181      1.1       cdi 	nulltty = ttymalloc();
    182      1.1       cdi 	nulltty->t_dev = makedev(cmajor, 0);
    183      1.1       cdi 	tty_attach(nulltty);
    184      1.1       cdi 	cn->cn_dev = nulltty->t_dev;
    185      1.1       cdi 
    186      1.1       cdi 	return (0);
    187      1.1       cdi }
    188      1.1       cdi 
    189      1.1       cdi /*
    190      1.1       cdi  * Pseudo-device attach function -- it's the right time to do the rest of
    191      1.3     perry  * initialization.
    192      1.1       cdi  */
    193      1.1       cdi void
    194      1.1       cdi nullconsattach(pdev_count)
    195      1.1       cdi 	int pdev_count;
    196      1.1       cdi {
    197      1.1       cdi 
    198      1.1       cdi 	nullcons_newdev(cn_tab);
    199      1.1       cdi }
    200