Home | History | Annotate | Line # | Download | only in kern
tty_conf.c revision 1.49.2.1
      1  1.49.2.1      yamt /*	$NetBSD: tty_conf.c,v 1.49.2.1 2006/02/01 14:52:20 yamt Exp $	*/
      2      1.48   thorpej 
      3      1.48   thorpej /*-
      4      1.48   thorpej  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5      1.48   thorpej  * All rights reserved.
      6      1.48   thorpej  *
      7      1.48   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.48   thorpej  * by Jason R. Thorpe.
      9      1.48   thorpej  *
     10      1.48   thorpej  * Redistribution and use in source and binary forms, with or without
     11      1.48   thorpej  * modification, are permitted provided that the following conditions
     12      1.48   thorpej  * are met:
     13      1.48   thorpej  * 1. Redistributions of source code must retain the above copyright
     14      1.48   thorpej  *    notice, this list of conditions and the following disclaimer.
     15      1.48   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.48   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17      1.48   thorpej  *    documentation and/or other materials provided with the distribution.
     18      1.48   thorpej  * 3. All advertising materials mentioning features or use of this software
     19      1.48   thorpej  *    must display the following acknowledgement:
     20      1.48   thorpej  *	This product includes software developed by the NetBSD
     21      1.48   thorpej  *	Foundation, Inc. and its contributors.
     22      1.48   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.48   thorpej  *    contributors may be used to endorse or promote products derived
     24      1.48   thorpej  *    from this software without specific prior written permission.
     25      1.48   thorpej  *
     26      1.48   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.48   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.48   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.48   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.48   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.48   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.48   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.48   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.48   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.48   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.48   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37      1.48   thorpej  */
     38      1.14       cgd 
     39      1.10       cgd /*-
     40      1.10       cgd  * Copyright (c) 1982, 1986, 1991, 1993
     41      1.10       cgd  *	The Regents of the University of California.  All rights reserved.
     42      1.10       cgd  * (c) UNIX System Laboratories, Inc.
     43      1.10       cgd  * All or some portions of this file are derived from material licensed
     44      1.10       cgd  * to the University of California by American Telephone and Telegraph
     45      1.10       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     46      1.10       cgd  * the permission of UNIX System Laboratories, Inc.
     47      1.10       cgd  *
     48      1.10       cgd  * Redistribution and use in source and binary forms, with or without
     49      1.10       cgd  * modification, are permitted provided that the following conditions
     50      1.10       cgd  * are met:
     51      1.10       cgd  * 1. Redistributions of source code must retain the above copyright
     52      1.10       cgd  *    notice, this list of conditions and the following disclaimer.
     53      1.10       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     54      1.10       cgd  *    notice, this list of conditions and the following disclaimer in the
     55      1.10       cgd  *    documentation and/or other materials provided with the distribution.
     56      1.43       agc  * 3. Neither the name of the University nor the names of its contributors
     57      1.10       cgd  *    may be used to endorse or promote products derived from this software
     58      1.10       cgd  *    without specific prior written permission.
     59      1.10       cgd  *
     60      1.10       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61      1.10       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62      1.10       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63      1.10       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64      1.10       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65      1.10       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66      1.10       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67      1.10       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68      1.10       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69      1.10       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70      1.10       cgd  * SUCH DAMAGE.
     71      1.10       cgd  *
     72      1.20      fvdl  *	@(#)tty_conf.c	8.5 (Berkeley) 1/9/95
     73      1.10       cgd  */
     74      1.35     lukem 
     75      1.35     lukem #include <sys/cdefs.h>
     76  1.49.2.1      yamt __KERNEL_RCSID(0, "$NetBSD: tty_conf.c,v 1.49.2.1 2006/02/01 14:52:20 yamt Exp $");
     77      1.10       cgd 
     78      1.10       cgd #include <sys/param.h>
     79      1.10       cgd #include <sys/systm.h>
     80      1.47        ws #include <sys/poll.h>
     81      1.10       cgd #include <sys/proc.h>
     82      1.10       cgd #include <sys/tty.h>
     83      1.28       eeh #include <sys/ttycom.h>
     84      1.10       cgd #include <sys/conf.h>
     85      1.48   thorpej #include <sys/once.h>
     86      1.48   thorpej #include <sys/lock.h>
     87      1.48   thorpej #include <sys/queue.h>
     88      1.48   thorpej 
     89      1.48   thorpej static struct linesw termios_disc = {
     90      1.48   thorpej 	.l_name = "termios",
     91      1.48   thorpej 	.l_open = ttylopen,
     92      1.48   thorpej 	.l_close = ttylclose,
     93      1.48   thorpej 	.l_read = ttread,
     94      1.48   thorpej 	.l_write = ttwrite,
     95      1.48   thorpej 	.l_ioctl = ttynullioctl,
     96      1.48   thorpej 	.l_rint = ttyinput,
     97      1.48   thorpej 	.l_start = ttstart,
     98      1.48   thorpej 	.l_modem = ttymodem,
     99      1.48   thorpej 	.l_poll = ttpoll
    100      1.48   thorpej };
    101      1.10       cgd 
    102      1.48   thorpej /*
    103      1.48   thorpej  * This is for the benefit of old BSD TTY compatbility, but since it is
    104      1.48   thorpej  * identical to termios (except for the name), don't bother conditionalizing
    105      1.48   thorpej  * it.
    106      1.48   thorpej  */
    107      1.48   thorpej static struct linesw ntty_disc = {	/* old NTTYDISC */
    108      1.48   thorpej 	.l_name = "ntty",
    109      1.48   thorpej 	.l_open = ttylopen,
    110      1.48   thorpej 	.l_close = ttylclose,
    111      1.48   thorpej 	.l_read = ttread,
    112      1.48   thorpej 	.l_write = ttwrite,
    113      1.48   thorpej 	.l_ioctl = ttynullioctl,
    114      1.48   thorpej 	.l_rint = ttyinput,
    115      1.48   thorpej 	.l_start = ttstart,
    116      1.48   thorpej 	.l_modem = ttymodem,
    117      1.48   thorpej 	.l_poll = ttpoll
    118      1.48   thorpej };
    119      1.10       cgd 
    120      1.48   thorpej static LIST_HEAD(, linesw) ttyldisc_list = LIST_HEAD_INITIALIZER(ttyldisc_head);
    121      1.48   thorpej static struct simplelock ttyldisc_list_slock = SIMPLELOCK_INITIALIZER;
    122      1.10       cgd 
    123      1.48   thorpej /*
    124      1.48   thorpej  * Note: We don't bother refcounting termios_disc and ntty_disc; they can't
    125      1.48   thorpej  * be removed from the list, and termios_disc is likely to have very many
    126      1.48   thorpej  * references (could we overflow the count?).
    127      1.48   thorpej  */
    128      1.48   thorpej #define	TTYLDISC_ISSTATIC(disc)					\
    129      1.48   thorpej 	((disc) == &termios_disc || (disc) == &ntty_disc)
    130      1.10       cgd 
    131      1.48   thorpej #define	TTYLDISC_HOLD(disc)					\
    132      1.48   thorpej do {								\
    133      1.48   thorpej 	if (! TTYLDISC_ISSTATIC(disc)) {			\
    134      1.48   thorpej 		KASSERT((disc)->l_refcnt != UINT_MAX);		\
    135      1.48   thorpej 		(disc)->l_refcnt++;				\
    136      1.48   thorpej 	}							\
    137      1.48   thorpej } while (/*CONSTCOND*/0)
    138      1.48   thorpej 
    139      1.48   thorpej #define	TTYLDISC_RELE(disc)					\
    140      1.48   thorpej do {								\
    141      1.48   thorpej 	if (! TTYLDISC_ISSTATIC(disc)) {			\
    142      1.48   thorpej 		KASSERT((disc)->l_refcnt != 0);			\
    143      1.48   thorpej 		(disc)->l_refcnt--;				\
    144      1.48   thorpej 	}							\
    145      1.48   thorpej } while (/*CONSTCOND*/0)
    146      1.36  augustss 
    147      1.48   thorpej #define	TTYLDISC_ISINUSE(disc)					\
    148      1.48   thorpej 	(TTYLDISC_ISSTATIC(disc) || (disc)->l_refcnt != 0)
    149      1.10       cgd 
    150      1.10       cgd /*
    151      1.10       cgd  * Do nothing specific version of line
    152      1.10       cgd  * discipline specific ioctl command.
    153      1.10       cgd  */
    154      1.10       cgd /*ARGSUSED*/
    155      1.17  christos int
    156      1.49  christos ttynullioctl(struct tty *tp, u_long cmd, char *data, int flags, struct lwp *l)
    157      1.10       cgd {
    158      1.10       cgd 
    159      1.10       cgd #ifdef lint
    160      1.49  christos 	tp = tp; data = data; flags = flags; l = l;
    161      1.10       cgd #endif
    162      1.38    atatat 	return (EPASSTHROUGH);
    163      1.28       eeh }
    164      1.28       eeh 
    165      1.28       eeh /*
    166      1.47        ws  * Return error to line discipline
    167      1.47        ws  * specific poll call.
    168      1.47        ws  */
    169      1.47        ws /*ARGSUSED*/
    170      1.47        ws int
    171      1.49  christos ttyerrpoll(struct tty *tp, int events, struct lwp *l)
    172      1.47        ws {
    173      1.47        ws 
    174      1.47        ws #ifdef lint
    175      1.49  christos 	tp = tp; events = events; l = l;
    176      1.47        ws #endif
    177      1.47        ws 	return (POLLERR);
    178      1.47        ws }
    179      1.47        ws 
    180      1.48   thorpej static ONCE_DECL(ttyldisc_init_once);
    181      1.48   thorpej 
    182  1.49.2.1      yamt static int
    183      1.48   thorpej ttyldisc_init(void)
    184      1.48   thorpej {
    185      1.48   thorpej 
    186      1.48   thorpej 	if (ttyldisc_attach(&termios_disc) != 0)
    187      1.48   thorpej 		panic("ttyldisc_init: termios_disc");
    188      1.48   thorpej 	if (ttyldisc_attach(&ntty_disc) != 0)
    189      1.48   thorpej 		panic("ttyldisc_init: ntty_disc");
    190  1.49.2.1      yamt 
    191  1.49.2.1      yamt 	return 0;
    192      1.48   thorpej }
    193      1.48   thorpej 
    194      1.48   thorpej static struct linesw *
    195      1.48   thorpej ttyldisc_lookup_locked(const char *name)
    196      1.48   thorpej {
    197      1.28       eeh 	struct linesw *disc;
    198      1.28       eeh 
    199      1.48   thorpej 	LIST_FOREACH(disc, &ttyldisc_list, l_list) {
    200      1.48   thorpej 		if (strcmp(name, disc->l_name) == 0)
    201      1.48   thorpej 			return (disc);
    202      1.48   thorpej 	}
    203      1.28       eeh 
    204      1.48   thorpej 	return (NULL);
    205      1.48   thorpej }
    206      1.28       eeh 
    207      1.48   thorpej /*
    208      1.48   thorpej  * Look up a line discipline by its name.  Caller holds a reference on
    209      1.48   thorpej  * the returned line discipline.
    210      1.48   thorpej  */
    211      1.48   thorpej struct linesw *
    212      1.48   thorpej ttyldisc_lookup(const char *name)
    213      1.48   thorpej {
    214      1.48   thorpej 	struct linesw *disc;
    215      1.28       eeh 
    216      1.48   thorpej 	RUN_ONCE(&ttyldisc_init_once, ttyldisc_init);
    217      1.28       eeh 
    218      1.48   thorpej 	simple_lock(&ttyldisc_list_slock);
    219      1.48   thorpej 	disc = ttyldisc_lookup_locked(name);
    220      1.48   thorpej 	if (disc != NULL)
    221      1.48   thorpej 		TTYLDISC_HOLD(disc);
    222      1.48   thorpej 	simple_unlock(&ttyldisc_list_slock);
    223      1.44  junyoung 
    224      1.48   thorpej 	return (disc);
    225      1.28       eeh }
    226      1.28       eeh 
    227      1.28       eeh /*
    228      1.48   thorpej  * Look up a line discipline by its legacy number.  Caller holds a
    229      1.48   thorpej  * reference on the returned line discipline.
    230      1.28       eeh  */
    231      1.28       eeh struct linesw *
    232      1.48   thorpej ttyldisc_lookup_bynum(int num)
    233      1.28       eeh {
    234      1.28       eeh 	struct linesw *disc;
    235      1.28       eeh 
    236      1.48   thorpej 	RUN_ONCE(&ttyldisc_init_once, ttyldisc_init);
    237      1.48   thorpej 
    238      1.48   thorpej 	simple_lock(&ttyldisc_list_slock);
    239      1.28       eeh 
    240      1.48   thorpej 	LIST_FOREACH(disc, &ttyldisc_list, l_list) {
    241      1.48   thorpej 		if (disc->l_no == num) {
    242      1.48   thorpej 			TTYLDISC_HOLD(disc);
    243      1.48   thorpej 			simple_unlock(&ttyldisc_list_slock);
    244      1.28       eeh 			return (disc);
    245      1.28       eeh 		}
    246      1.28       eeh 	}
    247      1.48   thorpej 
    248      1.48   thorpej 	simple_unlock(&ttyldisc_list_slock);
    249      1.28       eeh 	return (NULL);
    250      1.28       eeh }
    251      1.28       eeh 
    252      1.28       eeh /*
    253      1.48   thorpej  * Release a reference on a line discipline previously added by
    254      1.48   thorpej  * ttyldisc_lookup() or ttyldisc_lookup_bynum().
    255      1.28       eeh  */
    256      1.48   thorpej void
    257      1.48   thorpej ttyldisc_release(struct linesw *disc)
    258      1.48   thorpej {
    259      1.48   thorpej 
    260      1.48   thorpej 	if (disc == NULL)
    261      1.48   thorpej 		return;
    262      1.48   thorpej 
    263      1.48   thorpej 	simple_lock(&ttyldisc_list_slock);
    264      1.48   thorpej 	TTYLDISC_RELE(disc);
    265      1.48   thorpej 	simple_unlock(&ttyldisc_list_slock);
    266      1.48   thorpej }
    267      1.48   thorpej 
    268      1.48   thorpej #define	TTYLDISC_LEGACY_NUMBER_MIN	10
    269      1.48   thorpej #define	TTYLDISC_LEGACY_NUMBER_MAX	INT_MAX
    270      1.48   thorpej 
    271      1.48   thorpej static void
    272      1.48   thorpej ttyldisc_assign_legacy_number(struct linesw *disc)
    273      1.28       eeh {
    274      1.48   thorpej 	static const struct {
    275      1.48   thorpej 		const char *name;
    276      1.48   thorpej 		int num;
    277      1.48   thorpej 	} table[] = {
    278      1.48   thorpej 		{ "termios",		TTYDISC },
    279      1.48   thorpej 		{ "ntty",		2 /* XXX old NTTYDISC */ },
    280      1.48   thorpej 		{ "tablet",		TABLDISC },
    281      1.48   thorpej 		{ "slip",		SLIPDISC },
    282      1.48   thorpej 		{ "ppp",		PPPDISC },
    283      1.48   thorpej 		{ "strip",		STRIPDISC },
    284      1.48   thorpej 		{ "hdlc",		HDLCDISC },
    285      1.48   thorpej 		{ NULL,			0 }
    286      1.48   thorpej 	};
    287      1.48   thorpej 	struct linesw *ldisc;
    288      1.28       eeh 	int i;
    289      1.28       eeh 
    290      1.48   thorpej 	for (i = 0; table[i].name != NULL; i++) {
    291      1.48   thorpej 		if (strcmp(disc->l_name, table[i].name) == 0) {
    292      1.48   thorpej 			disc->l_no = table[i].num;
    293      1.48   thorpej 			return;
    294      1.48   thorpej 		}
    295      1.48   thorpej 	}
    296      1.48   thorpej 
    297      1.48   thorpej 	disc->l_no = TTYLDISC_LEGACY_NUMBER_MIN;
    298      1.48   thorpej 
    299      1.48   thorpej 	LIST_FOREACH(ldisc, &ttyldisc_list, l_list) {
    300      1.48   thorpej 		if (disc->l_no == ldisc->l_no) {
    301      1.48   thorpej 			KASSERT(disc->l_no < TTYLDISC_LEGACY_NUMBER_MAX);
    302      1.48   thorpej 			disc->l_no++;
    303      1.48   thorpej 		}
    304      1.48   thorpej 	}
    305      1.28       eeh }
    306      1.28       eeh 
    307      1.48   thorpej /*
    308      1.48   thorpej  * Register a line discipline.
    309      1.48   thorpej  */
    310      1.48   thorpej int
    311      1.48   thorpej ttyldisc_attach(struct linesw *disc)
    312      1.48   thorpej {
    313      1.48   thorpej 
    314      1.48   thorpej 	KASSERT(disc->l_name != NULL);
    315      1.48   thorpej 	KASSERT(disc->l_open != NULL);
    316      1.48   thorpej 	KASSERT(disc->l_close != NULL);
    317      1.48   thorpej 	KASSERT(disc->l_read != NULL);
    318      1.48   thorpej 	KASSERT(disc->l_write != NULL);
    319      1.48   thorpej 	KASSERT(disc->l_ioctl != NULL);
    320      1.48   thorpej 	KASSERT(disc->l_rint != NULL);
    321      1.48   thorpej 	KASSERT(disc->l_start != NULL);
    322      1.48   thorpej 	KASSERT(disc->l_modem != NULL);
    323      1.48   thorpej 	KASSERT(disc->l_poll != NULL);
    324      1.48   thorpej 
    325      1.48   thorpej 	/* You are not allowed to exceed TTLINEDNAMELEN */
    326      1.48   thorpej 	if (strlen(disc->l_name) >= TTLINEDNAMELEN)
    327      1.48   thorpej 		return (ENAMETOOLONG);
    328      1.48   thorpej 
    329      1.48   thorpej 	simple_lock(&ttyldisc_list_slock);
    330      1.48   thorpej 
    331      1.48   thorpej 	if (ttyldisc_lookup_locked(disc->l_name) != NULL) {
    332      1.48   thorpej 		simple_unlock(&ttyldisc_list_slock);
    333      1.48   thorpej 		return (EEXIST);
    334      1.48   thorpej 	}
    335      1.48   thorpej 
    336      1.48   thorpej 	ttyldisc_assign_legacy_number(disc);
    337      1.48   thorpej 	LIST_INSERT_HEAD(&ttyldisc_list, disc, l_list);
    338      1.48   thorpej 
    339      1.48   thorpej 	simple_unlock(&ttyldisc_list_slock);
    340      1.48   thorpej 
    341      1.48   thorpej 	return (0);
    342      1.48   thorpej }
    343      1.28       eeh 
    344      1.28       eeh /*
    345      1.48   thorpej  * Remove a line discipline.
    346      1.44  junyoung  */
    347      1.48   thorpej int
    348      1.48   thorpej ttyldisc_detach(struct linesw *disc)
    349      1.29     enami {
    350      1.48   thorpej #ifdef DIAGNOSTIC
    351      1.48   thorpej 	struct linesw *ldisc = ttyldisc_lookup(disc->l_name);
    352      1.48   thorpej 
    353      1.48   thorpej 	KASSERT(ldisc != NULL);
    354      1.48   thorpej 	KASSERT(ldisc == disc);
    355      1.48   thorpej 	ttyldisc_release(ldisc);
    356      1.48   thorpej #endif
    357      1.48   thorpej 
    358      1.48   thorpej 	simple_lock(&ttyldisc_list_slock);
    359      1.48   thorpej 
    360      1.48   thorpej 	if (TTYLDISC_ISINUSE(disc)) {
    361      1.48   thorpej 		simple_unlock(&ttyldisc_list_slock);
    362      1.48   thorpej 		return (EBUSY);
    363      1.48   thorpej 	}
    364      1.29     enami 
    365      1.48   thorpej 	LIST_REMOVE(disc, l_list);
    366      1.48   thorpej 
    367      1.48   thorpej 	simple_unlock(&ttyldisc_list_slock);
    368      1.48   thorpej 
    369      1.48   thorpej 	return (0);
    370      1.48   thorpej }
    371      1.28       eeh 
    372      1.48   thorpej /*
    373      1.48   thorpej  * Return the default line discipline.
    374      1.48   thorpej  */
    375      1.48   thorpej struct linesw *
    376      1.48   thorpej ttyldisc_default(void)
    377      1.48   thorpej {
    378      1.28       eeh 
    379      1.48   thorpej 	return (&termios_disc);
    380      1.10       cgd }
    381