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