Home | History | Annotate | Line # | Download | only in kern
tty_conf.c revision 1.47
      1  1.47        ws /*	$NetBSD: tty_conf.c,v 1.47 2005/06/21 14:01:13 ws Exp $	*/
      2  1.14       cgd 
      3  1.10       cgd /*-
      4  1.10       cgd  * Copyright (c) 1982, 1986, 1991, 1993
      5  1.10       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.10       cgd  * (c) UNIX System Laboratories, Inc.
      7  1.10       cgd  * All or some portions of this file are derived from material licensed
      8  1.10       cgd  * to the University of California by American Telephone and Telegraph
      9  1.10       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.10       cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.10       cgd  *
     12  1.10       cgd  * Redistribution and use in source and binary forms, with or without
     13  1.10       cgd  * modification, are permitted provided that the following conditions
     14  1.10       cgd  * are met:
     15  1.10       cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.10       cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.10       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.10       cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.10       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.43       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.10       cgd  *    may be used to endorse or promote products derived from this software
     22  1.10       cgd  *    without specific prior written permission.
     23  1.10       cgd  *
     24  1.10       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.10       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.10       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.10       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.10       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.10       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.10       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.10       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.10       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.10       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.10       cgd  * SUCH DAMAGE.
     35  1.10       cgd  *
     36  1.20      fvdl  *	@(#)tty_conf.c	8.5 (Berkeley) 1/9/95
     37  1.10       cgd  */
     38  1.35     lukem 
     39  1.35     lukem #include <sys/cdefs.h>
     40  1.47        ws __KERNEL_RCSID(0, "$NetBSD: tty_conf.c,v 1.47 2005/06/21 14:01:13 ws Exp $");
     41  1.22   thorpej 
     42  1.22   thorpej #include "opt_compat_freebsd.h"
     43  1.24  christos #include "opt_compat_43.h"
     44  1.10       cgd 
     45  1.10       cgd #include <sys/param.h>
     46  1.10       cgd #include <sys/systm.h>
     47  1.10       cgd #include <sys/buf.h>
     48  1.10       cgd #include <sys/ioctl.h>
     49  1.47        ws #include <sys/poll.h>
     50  1.10       cgd #include <sys/proc.h>
     51  1.10       cgd #include <sys/tty.h>
     52  1.28       eeh #include <sys/ioctl.h>
     53  1.28       eeh #include <sys/ttycom.h>
     54  1.10       cgd #include <sys/conf.h>
     55  1.28       eeh #include <sys/malloc.h>
     56  1.10       cgd 
     57  1.10       cgd #include "tb.h"
     58  1.10       cgd #if NTB > 0
     59  1.44  junyoung int	tbopen(dev_t dev, struct tty *tp);
     60  1.44  junyoung int	tbclose(struct tty *tp, int flags);
     61  1.44  junyoung int	tbread(struct tty *tp, struct uio *uio, int flags);
     62  1.44  junyoung int	tbtioctl(struct tty *tp, u_long cmd, caddr_t data,
     63  1.44  junyoung 		 int flag, struct proc *p);
     64  1.44  junyoung int	tbinput(int c, struct tty *tp);
     65  1.10       cgd #endif
     66  1.10       cgd 
     67  1.10       cgd #include "sl.h"
     68  1.10       cgd #if NSL > 0
     69  1.44  junyoung int	slopen(dev_t dev, struct tty *tp);
     70  1.44  junyoung int	slclose(struct tty *tp, int flags);
     71  1.44  junyoung int	sltioctl(struct tty *tp, u_long cmd, caddr_t data,
     72  1.44  junyoung 		 int flag, struct proc *p);
     73  1.44  junyoung int	slinput(int c, struct tty *tp);
     74  1.44  junyoung int	slstart(struct tty *tp);
     75  1.10       cgd #endif
     76  1.10       cgd 
     77  1.10       cgd #include "ppp.h"
     78  1.10       cgd #if NPPP > 0
     79  1.44  junyoung int	pppopen(dev_t dev, struct tty *tp);
     80  1.44  junyoung int	pppclose(struct tty *tp, int flags);
     81  1.44  junyoung int	ppptioctl(struct tty *tp, u_long cmd, caddr_t data,
     82  1.44  junyoung 		  int flag, struct proc *p);
     83  1.44  junyoung int	pppinput(int c, struct tty *tp);
     84  1.44  junyoung int	pppstart(struct tty *tp);
     85  1.44  junyoung int	pppread(struct tty *tp, struct uio *uio, int flag);
     86  1.44  junyoung int	pppwrite(struct tty *tp, struct uio *uio, int flag);
     87  1.10       cgd #endif
     88  1.10       cgd 
     89  1.18  jonathan #include "strip.h"
     90  1.18  jonathan #if NSTRIP > 0
     91  1.44  junyoung int	stripopen(dev_t dev, struct tty *tp);
     92  1.44  junyoung int	stripclose(struct tty *tp, int flags);
     93  1.44  junyoung int	striptioctl(struct tty *tp, u_long cmd, caddr_t data,
     94  1.44  junyoung 		    int flag, struct proc *p);
     95  1.44  junyoung int	stripinput(int c, struct tty *tp);
     96  1.44  junyoung int	stripstart(struct tty *tp);
     97  1.18  jonathan #endif
     98  1.18  jonathan 
     99  1.36  augustss #include "irframetty.h"
    100  1.36  augustss #if NIRFRAMETTY > 0
    101  1.44  junyoung int	irframetopen(dev_t dev, struct tty *tp);
    102  1.44  junyoung int	irframetclose(struct tty *tp, int flags);
    103  1.44  junyoung int	irframetioctl(struct tty *tp, u_long cmd, caddr_t data,
    104  1.44  junyoung 		      int flag, struct proc *p);
    105  1.44  junyoung int	irframetinput(int c, struct tty *tp);
    106  1.44  junyoung int	irframetstart(struct tty *tp);
    107  1.44  junyoung int	irframetread(struct tty *tp, struct uio *uio, int flag);
    108  1.44  junyoung int	irframetwrite(struct tty *tp, struct uio *uio, int flag);
    109  1.44  junyoung int	irframetpoll(struct tty *tp, int events, struct proc *p);
    110  1.36  augustss #endif
    111  1.36  augustss 
    112  1.10       cgd 
    113  1.28       eeh struct  linesw termios_disc =
    114  1.39   gehenna 	{ "termios", TTYDISC, ttylopen, ttylclose, ttread, ttwrite,
    115  1.39   gehenna 	  ttynullioctl, ttyinput, ttstart, ttymodem, ttpoll };	/* 0- termios */
    116  1.28       eeh struct  linesw defunct_disc =
    117  1.39   gehenna 	{ "defunct", 1, ttynodisc, ttyerrclose, ttyerrio, ttyerrio,
    118  1.39   gehenna 	  ttynullioctl, ttyerrinput, ttyerrstart, nullmodem, ttyerrpoll }; /* 1- defunct */
    119  1.16   mycroft #if defined(COMPAT_43) || defined(COMPAT_FREEBSD)
    120  1.28       eeh struct  linesw ntty_disc =
    121  1.39   gehenna 	{ "ntty", 2, ttylopen, ttylclose, ttread, ttwrite, ttynullioctl,
    122  1.32       scw 	  ttyinput, ttstart, ttymodem, ttpoll };	/* 2- old NTTYDISC */
    123  1.12   mycroft #endif
    124  1.10       cgd #if NTB > 0
    125  1.28       eeh struct  linesw table_disc =
    126  1.36  augustss 	{ "tablet", TABLDISC, tbopen, tbclose, tbread, ttyerrio, tbtioctl,
    127  1.32       scw 	  tbinput, ttstart, nullmodem, ttyerrpoll };	/* 3- TABLDISC */
    128  1.10       cgd #endif
    129  1.10       cgd #if NSL > 0
    130  1.28       eeh struct  linesw slip_disc =
    131  1.36  augustss 	{ "slip", SLIPDISC, slopen, slclose, ttyerrio, ttyerrio, sltioctl,
    132  1.32       scw 	  slinput, slstart, nullmodem, ttyerrpoll };	/* 4- SLIPDISC */
    133  1.10       cgd #endif
    134  1.10       cgd #if NPPP > 0
    135  1.28       eeh struct  linesw ppp_disc =
    136  1.36  augustss 	{ "ppp", PPPDISC, pppopen, pppclose, pppread, pppwrite, ppptioctl,
    137  1.32       scw 	  pppinput, pppstart, ttymodem, ttpoll };	/* 5- PPPDISC */
    138  1.18  jonathan #endif
    139  1.18  jonathan #if NSTRIP > 0
    140  1.28       eeh struct  linesw strip_disc =
    141  1.36  augustss 	{ "strip", STRIPDISC, stripopen, stripclose, ttyerrio, ttyerrio,
    142  1.36  augustss 	  striptioctl, stripinput, stripstart, nullmodem, ttyerrpoll };
    143  1.36  augustss 							/* 6- STRIPDISC */
    144  1.36  augustss #endif
    145  1.36  augustss #if NIRFRAMETTY > 0
    146  1.36  augustss struct  linesw irframet_disc =
    147  1.45     itohy 	{ "irframe", -1, irframetopen, irframetclose, ttyerrio,
    148  1.36  augustss 	  ttyerrio, irframetioctl, irframetinput, irframetstart,
    149  1.45     itohy 	  ttymodem, ttyerrpoll };			/* irframe */
    150  1.25       eeh #endif
    151  1.25       eeh 
    152  1.25       eeh /*
    153  1.28       eeh  * Registered line disciplines.  Don't use this
    154  1.28       eeh  * it will go away.
    155  1.25       eeh  */
    156  1.28       eeh #define LSWITCHBRK	20
    157  1.28       eeh struct	linesw **linesw = NULL;
    158  1.28       eeh int	nlinesw = 0;
    159  1.28       eeh int	slinesw = 0;
    160  1.10       cgd 
    161  1.10       cgd /*
    162  1.10       cgd  * Do nothing specific version of line
    163  1.10       cgd  * discipline specific ioctl command.
    164  1.10       cgd  */
    165  1.10       cgd /*ARGSUSED*/
    166  1.17  christos int
    167  1.42      fvdl ttynullioctl(tp, cmd, data, flags, p)
    168  1.10       cgd 	struct tty *tp;
    169  1.15       cgd 	u_long cmd;
    170  1.10       cgd 	char *data;
    171  1.10       cgd 	int flags;
    172  1.42      fvdl 	struct proc *p;
    173  1.10       cgd {
    174  1.10       cgd 
    175  1.10       cgd #ifdef lint
    176  1.42      fvdl 	tp = tp; data = data; flags = flags; p = p;
    177  1.10       cgd #endif
    178  1.38    atatat 	return (EPASSTHROUGH);
    179  1.28       eeh }
    180  1.28       eeh 
    181  1.28       eeh /*
    182  1.47        ws  * Return error to line discipline
    183  1.47        ws  * specific poll call.
    184  1.47        ws  */
    185  1.47        ws /*ARGSUSED*/
    186  1.47        ws int
    187  1.47        ws ttyerrpoll(tp, events, p)
    188  1.47        ws 	struct tty *tp;
    189  1.47        ws 	int events;
    190  1.47        ws 	struct proc *p;
    191  1.47        ws {
    192  1.47        ws 
    193  1.47        ws #ifdef lint
    194  1.47        ws 	tp = tp; events = events; p = p;
    195  1.47        ws #endif
    196  1.47        ws 	return (POLLERR);
    197  1.47        ws }
    198  1.47        ws 
    199  1.47        ws /*
    200  1.28       eeh  * Register a line discipline, optionally providing a
    201  1.28       eeh  * specific discipline number for compatibility, -1 allocates
    202  1.28       eeh  * a new one.  Returns a discipline number, or -1 on
    203  1.28       eeh  * failure.
    204  1.28       eeh  */
    205  1.28       eeh int
    206  1.44  junyoung ttyldisc_add(disc, no)
    207  1.28       eeh 	struct linesw *disc;
    208  1.28       eeh 	int no;
    209  1.28       eeh {
    210  1.28       eeh 
    211  1.28       eeh 	/* You are not allowed to exceed TTLINEDNAMELEN */
    212  1.37  christos 	if (strlen(disc->l_name) >= TTLINEDNAMELEN)
    213  1.28       eeh 		return (-1);
    214  1.28       eeh 
    215  1.44  junyoung 	/*
    216  1.44  junyoung 	 * You are not allowed to specify a line switch
    217  1.28       eeh 	 * compatibility number greater than 10.
    218  1.28       eeh 	 */
    219  1.44  junyoung 	if (no > 10)
    220  1.28       eeh 		return (-1);
    221  1.28       eeh 
    222  1.28       eeh 	if (linesw == NULL)
    223  1.28       eeh 		panic("adding uninitialized linesw");
    224  1.32       scw 
    225  1.32       scw #ifdef DEBUG
    226  1.32       scw 	/*
    227  1.32       scw 	 * XXX: For the benefit of LKMs
    228  1.32       scw 	 */
    229  1.32       scw 	if (disc->l_poll == NULL)
    230  1.32       scw 		panic("line discipline must now provide l_poll() entry point");
    231  1.32       scw #endif
    232  1.28       eeh 
    233  1.28       eeh 	if (no == -1) {
    234  1.28       eeh 		/* Hunt for any slot */
    235  1.28       eeh 
    236  1.29     enami 		for (no = slinesw; no-- > 0;)
    237  1.29     enami 			if (linesw[no] == NULL)
    238  1.29     enami 				break;
    239  1.30     enami 		/* if no == -1 we should realloc linesw, but for now... */
    240  1.30     enami 		if (no == -1)
    241  1.30     enami 			return (-1);
    242  1.28       eeh 	}
    243  1.28       eeh 
    244  1.28       eeh 	/* Need a specific slot */
    245  1.28       eeh 	if (linesw[no] != NULL)
    246  1.28       eeh 		return (-1);
    247  1.44  junyoung 
    248  1.28       eeh 	linesw[no] = disc;
    249  1.28       eeh 	disc->l_no = no;
    250  1.28       eeh 
    251  1.28       eeh 	/* Define size of table */
    252  1.28       eeh 	if (no >= nlinesw)
    253  1.28       eeh 		nlinesw = no + 1;
    254  1.44  junyoung 
    255  1.28       eeh 	return (no);
    256  1.28       eeh }
    257  1.28       eeh 
    258  1.28       eeh /*
    259  1.28       eeh  * Remove a line discipline by its name.  Returns the
    260  1.28       eeh  * discipline on success or NULL on failure.
    261  1.28       eeh  */
    262  1.28       eeh struct linesw *
    263  1.44  junyoung ttyldisc_remove(name)
    264  1.46       uwe 	const char *name;
    265  1.28       eeh {
    266  1.28       eeh 	struct linesw *disc;
    267  1.28       eeh 	int i;
    268  1.28       eeh 
    269  1.28       eeh 	if (linesw == NULL)
    270  1.28       eeh 		panic("removing uninitialized linesw");
    271  1.28       eeh 
    272  1.29     enami 	for (i = 0; i < nlinesw; i++) {
    273  1.28       eeh 		if (linesw[i] && (strcmp(name, linesw[i]->l_name) == 0)) {
    274  1.28       eeh 			disc = linesw[i];
    275  1.28       eeh 			linesw[i] = NULL;
    276  1.44  junyoung 
    277  1.28       eeh 			if (nlinesw == i + 1) {
    278  1.28       eeh 				/* Need to fix up array sizing */
    279  1.30     enami 				while (i-- > 0 && linesw[i] == NULL)
    280  1.30     enami 					continue;
    281  1.30     enami 				nlinesw = i + 1;
    282  1.28       eeh 			}
    283  1.28       eeh 			return (disc);
    284  1.28       eeh 		}
    285  1.28       eeh 	}
    286  1.28       eeh 	return (NULL);
    287  1.28       eeh }
    288  1.28       eeh 
    289  1.28       eeh /*
    290  1.28       eeh  * Look up a line discipline by its name.
    291  1.28       eeh  */
    292  1.28       eeh struct linesw *
    293  1.28       eeh ttyldisc_lookup(name)
    294  1.46       uwe 	const char *name;
    295  1.28       eeh {
    296  1.28       eeh 	int i;
    297  1.28       eeh 
    298  1.29     enami 	for (i = 0; i < nlinesw; i++)
    299  1.28       eeh 		if (linesw[i] && (strcmp(name, linesw[i]->l_name) == 0))
    300  1.28       eeh 			return (linesw[i]);
    301  1.28       eeh 	return (NULL);
    302  1.28       eeh }
    303  1.28       eeh 
    304  1.28       eeh #define TTYLDISCINIT(s, v) \
    305  1.28       eeh 	do { \
    306  1.28       eeh 		if (ttyldisc_add(&(s), (v)) != (v)) \
    307  1.33  kristerw 			panic("ttyldisc_init: " __STRING(s)); \
    308  1.40     perry 	} while (/*CONSTCOND*/ 0)
    309  1.28       eeh 
    310  1.28       eeh /*
    311  1.28       eeh  * Register the basic line disciplines.
    312  1.44  junyoung  */
    313  1.28       eeh void
    314  1.29     enami ttyldisc_init()
    315  1.29     enami {
    316  1.29     enami 
    317  1.28       eeh 	/* Only initialize once */
    318  1.28       eeh 	if (linesw)
    319  1.28       eeh 		return;
    320  1.28       eeh 
    321  1.28       eeh 	slinesw = LSWITCHBRK;
    322  1.44  junyoung 	linesw = malloc(slinesw * sizeof(struct linesw *),
    323  1.29     enami 	    M_TTYS, M_WAITOK);
    324  1.34   thorpej 	memset(linesw, 0, slinesw * sizeof(struct linesw *));
    325  1.28       eeh 
    326  1.28       eeh 	TTYLDISCINIT(termios_disc, 0);
    327  1.28       eeh 	/* Do we really need this one? */
    328  1.28       eeh 	TTYLDISCINIT(defunct_disc, 1);
    329  1.28       eeh 
    330  1.44  junyoung 	/*
    331  1.28       eeh 	 * The following should really be moved to
    332  1.28       eeh 	 * initialization code for each module.
    333  1.28       eeh 	 */
    334  1.28       eeh 
    335  1.28       eeh #if defined(COMPAT_43) || defined(COMPAT_FREEBSD)
    336  1.28       eeh 	TTYLDISCINIT(ntty_disc, 2);
    337  1.28       eeh #endif
    338  1.28       eeh #if NTB > 0
    339  1.28       eeh 	TTYLDISCINIT(table_disc, 3);
    340  1.28       eeh #endif
    341  1.28       eeh #if NSL > 0
    342  1.28       eeh 	TTYLDISCINIT(slip_disc, 4);
    343  1.28       eeh #endif
    344  1.28       eeh #if NPPP > 0
    345  1.28       eeh 	TTYLDISCINIT(ppp_disc, 5);
    346  1.28       eeh #endif
    347  1.28       eeh #if NSTRIP > 0
    348  1.28       eeh 	TTYLDISCINIT(strip_disc, 6);
    349  1.36  augustss #endif
    350  1.36  augustss #if NIRFRAMETTY > 0
    351  1.36  augustss 	ttyldisc_add(&irframet_disc, -1);
    352  1.28       eeh #endif
    353  1.10       cgd }
    354