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