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