]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty_compat.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1991, 1993
25 * The Regents of the University of California. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * @(#)tty_compat.c 8.1 (Berkeley) 6/10/93
59 * mapping routines for old line discipline (yuck)
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/ioctl.h>
65 #include <sys/proc_internal.h>
67 #include <sys/termios.h>
68 #include <sys/file_internal.h>
70 #include <sys/kernel.h>
71 #include <sys/sysctl.h>
72 #include <sys/syslog.h>
74 /* NeXT Move define down here cause COMPAT_43_TTY not valid earlier */
75 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
77 static int ttcompatgetflags(struct tty
*tp
);
78 static void ttcompatsetflags(struct tty
*tp
, struct termios
*t
);
79 static void ttcompatsetlflags(struct tty
*tp
, struct termios
*t
);
80 static int ttcompatspeedtab(int speed
, struct speedtab
*table
);
83 * These two tables encode baud rate to speed code and speed code to
84 * baud rate information. They are a mapping between the <sys/termios.h>
85 * baud rate constants and the <sys/ttydev.h> baud rate constants. We
86 * cannot use those constants directly here because they occupy the same
89 static struct speedtab compatspeeds
[] = {
111 static int compatspcodes
[] = {
112 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200,
113 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200,
119 * Description: Given a baud rate value as an integer, and a speed table,
120 * convert the baud rate to a speed code, according to the
121 * contents of the table. This effectively changes termios.h
122 * baud rate values into ttydev.h baud rate codes.
124 * Parameters: int speed Baud rate, as an integer
125 * struct speedtab *table Baud rate table to speed code table
127 * Returns: 1 B50 speed code; returned if we can
128 * not find an answer in the table.
129 * 0 If a 0 was requested in order to
130 * trigger a hangup (250ms of line
131 * silence, per Bell 103C standard).
132 * * A speed code matching the requested
133 * baud rate (potentially rounded down,
134 * if there is no exact match).
136 * Notes: This function is used for TIOCGETP, TIOCSETP, and TIOCSETN.
139 ttcompatspeedtab(int speed
, struct speedtab
*table
)
142 return (0); /* hangup */
143 for ( ; table
->sp_speed
> 0; table
++)
144 if (table
->sp_speed
<= speed
) /* nearest one, rounded down */
145 return (table
->sp_code
);
146 return (1); /* 50, min and not hangup */
152 * Description: Convert backward compatability set command arguments as
155 * TIOCSETP -> TIOSETAF
156 * TIOCSETN -> TIOCSETA
157 * TIOCSETC -> TIOCSETA
158 * TIOCSLTC -> TIOCSETA
159 * TIOCLBIS -> TIOCSETA
160 * TIOCLBIC -> TIOCSETA
161 * TIOCLSET -> TIOCSETA
163 * The converted command argument and potentially modified 'term'
164 * argument are returned to the caller, which will then call ttioctl(),
165 * if this function returns successfully.
167 * Parameters struct tty *tp The tty on which the operation is
169 * u_long *com A pointer to the terminal input/output
170 * command being requested; its contents
171 * will be modified per the table above,
172 * on a non-error return.
173 * caddr_t data Command specific parameter data; this
174 * data is read but not modified.
175 * struct termios *term A local stack termios structure from
176 * ttcompat(), whose contents are to be
177 * modified based on *com and *data.
179 * Returns: EINVAL An input speed or output speed is
180 * outside the allowable range for a
181 * TIOCSETP or TIOCSETN command.
182 * 0 All other cases return 0.
184 * Notes: This function may modify the contents of the tp->t_flags
185 * field in a successful call to TIOCSETP, TIOCSETN, TIOCLBIS,
186 * TIOCLBIC, or TIOCLSET.
188 * All other tp fields will remain unmodifed, since the struct
189 * termious is a local stack copy from ttcompat(), and not the
190 * real thing. A subsequent call to ttioctl() in ttcompat(),
191 * however, may result in subsequent changes.
193 __private_extern__
int
194 ttsetcompat(struct tty
*tp
, u_long
*com
, caddr_t data
, struct termios
*term
)
199 * Wait for all characters queued for output to drain, then
200 * Discard all characters queued for input, and then set
201 * the input and output speeds and device flags, per the
202 * contents of the struct sgttyb that 'data' points to.
206 * Same as TIOCSETP, but the output is not drained, and any
207 * pending input is not discarded.
210 register struct sgttyb
*sg
= (struct sgttyb
*)data
;
213 if ((speed
= sg
->sg_ispeed
) > MAX_SPEED
|| speed
< 0)
215 else if (speed
!= ttcompatspeedtab(tp
->t_ispeed
, compatspeeds
))
216 term
->c_ispeed
= compatspcodes
[speed
];
218 term
->c_ispeed
= tp
->t_ispeed
;
219 if ((speed
= sg
->sg_ospeed
) > MAX_SPEED
|| speed
< 0)
221 else if (speed
!= ttcompatspeedtab(tp
->t_ospeed
, compatspeeds
))
222 term
->c_ospeed
= compatspcodes
[speed
];
224 term
->c_ospeed
= tp
->t_ospeed
;
225 term
->c_cc
[VERASE
] = sg
->sg_erase
;
226 term
->c_cc
[VKILL
] = sg
->sg_kill
;
227 tp
->t_flags
= (tp
->t_flags
&0xffff0000) | (sg
->sg_flags
&0xffff);
228 ttcompatsetflags(tp
, term
);
229 *com
= (*com
== TIOCSETP
) ? TIOCSETAF
: TIOCSETA
;
234 * Set the terminal control characters per the contents of
235 * the struct tchars that 'data' points to.
238 struct tchars
*tc
= (struct tchars
*)data
;
242 cc
[VINTR
] = tc
->t_intrc
;
243 cc
[VQUIT
] = tc
->t_quitc
;
244 cc
[VSTART
] = tc
->t_startc
;
245 cc
[VSTOP
] = tc
->t_stopc
;
246 cc
[VEOF
] = tc
->t_eofc
;
247 cc
[VEOL
] = tc
->t_brkc
;
248 if (tc
->t_brkc
== -1)
249 cc
[VEOL2
] = _POSIX_VDISABLE
;
255 * Set the terminal control characters per the contents of
256 * the struct ltchars that 'data' points to.
259 struct ltchars
*ltc
= (struct ltchars
*)data
;
263 cc
[VSUSP
] = ltc
->t_suspc
;
264 cc
[VDSUSP
] = ltc
->t_dsuspc
;
265 cc
[VREPRINT
] = ltc
->t_rprntc
;
266 cc
[VDISCARD
] = ltc
->t_flushc
;
267 cc
[VWERASE
] = ltc
->t_werasc
;
268 cc
[VLNEXT
] = ltc
->t_lnextc
;
274 * Set the bits in the terminal state local flags word
275 * (16 bits) for the terminal to the current bits OR
276 * those in the 16 bit value pointed to by 'data'.
280 * Clear the bits in the terminal state local flags word
281 * for the terminal to the current bits AND those bits NOT
282 * in the 16 bit value pointed to by 'data'.
286 * Set the terminal state local flags word to exactly those
287 * bits that correspond to the 16 bit value pointed to by
290 if (*com
== TIOCLSET
)
291 tp
->t_flags
= (tp
->t_flags
&0xffff) | *(int *)data
<<16;
294 (ttcompatgetflags(tp
)&0xffff0000)|(tp
->t_flags
&0xffff);
295 if (*com
== TIOCLBIS
)
296 tp
->t_flags
|= *(int *)data
<<16;
298 tp
->t_flags
&= ~(*(int *)data
<<16);
300 ttcompatsetlflags(tp
, term
);
310 * Description: For 'set' commands, convert the command and arguments as
311 * necessary, and call ttioctl(), returning the result as
312 * our result; for 'get' commands, obtain the requested data
313 * from the appropriate source, and return it in the expected
314 * format. If the command is not recognized, return EINVAL.
316 * Parameters struct tty *tp The tty on which the operation is
318 * u_long com The terminal input/output command
320 * caddr_t data The pointer to the user data argument
321 * provided with the command.
322 * int flag The file open flags (e.g. FREAD).
323 * struct proc *p The current process pointer for the
326 * Returns: 0 Most 'get' operations can't fail, and
327 * therefore return this.
328 * ENOTTY TIOCGSID may return this when you
329 * attempt to get the session ID for a
330 * terminal with no associated session,
331 * or for which there is a session, but
333 * EIOCTL If the command cannot be handled at
334 * this layer, this will be returned.
335 * * Any value returned by ttioctl(), if a
336 * set command is requested.
338 * NOTES: The process pointer may be a proxy on whose behalf we are
339 * operating, so it is not safe to simply use current_process()
343 __private_extern__
int
344 ttcompat(struct tty
*tp
, u_long com
, caddr_t data
, int flag
, struct proc
*p
)
355 * See ttsetcompat() for a full description of these command
356 * values and their meanings.
362 term
= tp
->t_termios
;
363 if ((error
= ttsetcompat(tp
, &com
, data
, &term
)) != 0)
365 return ttioctl(tp
, com
, (caddr_t
) &term
, flag
, p
);
369 * Get the current input and output speeds, and device
370 * flags, into the structure pointed to by 'data'.
373 register struct sgttyb
*sg
= (struct sgttyb
*)data
;
374 register cc_t
*cc
= tp
->t_cc
;
376 sg
->sg_ospeed
= ttcompatspeedtab(tp
->t_ospeed
, compatspeeds
);
377 if (tp
->t_ispeed
== 0)
378 sg
->sg_ispeed
= sg
->sg_ospeed
;
380 sg
->sg_ispeed
= ttcompatspeedtab(tp
->t_ispeed
, compatspeeds
);
381 sg
->sg_erase
= cc
[VERASE
];
382 sg
->sg_kill
= cc
[VKILL
];
383 sg
->sg_flags
= tp
->t_flags
= ttcompatgetflags(tp
);
388 * Get the terminal control characters into the struct
389 * tchars that 'data' points to.
392 struct tchars
*tc
= (struct tchars
*)data
;
393 register cc_t
*cc
= tp
->t_cc
;
395 tc
->t_intrc
= cc
[VINTR
];
396 tc
->t_quitc
= cc
[VQUIT
];
397 tc
->t_startc
= cc
[VSTART
];
398 tc
->t_stopc
= cc
[VSTOP
];
399 tc
->t_eofc
= cc
[VEOF
];
400 tc
->t_brkc
= cc
[VEOL
];
405 * Get the terminal control characters into the struct
406 * ltchars that 'data' points to.
409 struct ltchars
*ltc
= (struct ltchars
*)data
;
410 register cc_t
*cc
= tp
->t_cc
;
412 ltc
->t_suspc
= cc
[VSUSP
];
413 ltc
->t_dsuspc
= cc
[VDSUSP
];
414 ltc
->t_rprntc
= cc
[VREPRINT
];
415 ltc
->t_flushc
= cc
[VDISCARD
];
416 ltc
->t_werasc
= cc
[VWERASE
];
417 ltc
->t_lnextc
= cc
[VLNEXT
];
422 * Get the terminal state local flags word into the 16 bit
423 * value pointed to by 'data'.
426 (ttcompatgetflags(tp
) & 0xffff0000UL
)
427 | (tp
->t_flags
& 0xffff);
428 *(int *)data
= tp
->t_flags
>>16;
433 * Get the current line discipline into the int pointed to
436 *(int *)data
= tp
->t_line
? tp
->t_line
: 2;
441 * Set the current line discipline based on the value of the
442 * int pointed to by 'data'.
447 return (ttioctl(tp
, TIOCSETD
,
448 *(int *)data
== 2 ? (caddr_t
)&ldisczero
: data
, flag
, p
));
453 * Become the console device.
456 return (ttioctl(tp
, TIOCCONS
, data
, flag
, p
));
460 * Get the current session ID (controlling process' PID).
462 if (tp
->t_session
== NULL
)
465 if (tp
->t_session
->s_leader
== NULL
)
468 *(int *) data
= tp
->t_session
->s_leader
->p_pid
;
473 * This ioctl is not handled at this layer.
479 * Successful 'get' operation.
487 * Description: Get the terminal state local flags, device flags, and current
488 * speed code for the device (all 32 bits are returned).
490 * Parameters struct tty *tp The tty on which the operation is
493 * Returns: * Integer value corresponding to the
494 * current terminal state local flags
497 * Notes: Caller is responsible for breaking these bits back out into
498 * separate 16 bit filelds, if that's what was actually desired.
501 ttcompatgetflags(struct tty
*tp
)
503 register tcflag_t iflag
= tp
->t_iflag
;
504 register tcflag_t lflag
= tp
->t_lflag
;
505 register tcflag_t oflag
= tp
->t_oflag
;
506 register tcflag_t cflag
= tp
->t_cflag
;
507 register int flags
= 0;
511 if (iflag
&ICRNL
|| oflag
&ONLCR
)
513 if ((cflag
&CSIZE
) == CS8
) {
518 else if (cflag
&PARENB
) {
525 flags
|= EVENP
| ODDP
;
528 if ((lflag
&ICANON
) == 0) {
530 if (iflag
&(INPCK
|ISTRIP
|IXON
) || lflag
&(IEXTEN
|ISIG
)
531 || (cflag
&(CSIZE
|PARENB
)) != CS8
)
536 if (!(flags
&RAW
) && !(oflag
&OPOST
) && (cflag
&(CSIZE
|PARENB
)) == CS8
)
540 if ((cflag
&HUPCL
) == 0)
545 flags
|= CRTERA
|CRTBS
;
547 flags
|= CRTKIL
|CRTBS
;
552 if ((iflag
&IXANY
) == 0)
554 flags
|= lflag
&(ECHO
|TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
561 * Description: Given a set of compatability flags, convert the compatability
562 * flags in the terminal flags fields into canonical flags in the
563 * provided termios struct.
565 * Parameters: struct tty *tp The tty on which the operation is
567 * struct termios *t The termios structure into which to
568 * return the converted flags.
570 * Returns: void (implicit: *t, modified)
573 ttcompatsetflags(struct tty
*tp
, struct termios
*t
)
575 register int flags
= tp
->t_flags
;
576 register tcflag_t iflag
= t
->c_iflag
;
577 register tcflag_t oflag
= t
->c_oflag
;
578 register tcflag_t lflag
= t
->c_lflag
;
579 register tcflag_t cflag
= t
->c_cflag
;
583 lflag
&= ~(ECHOCTL
|ISIG
|ICANON
|IEXTEN
);
585 iflag
&= ~(PARMRK
|IGNPAR
|IGNCR
|INLCR
);
586 iflag
|= BRKINT
|IXON
|IMAXBEL
;
587 lflag
|= ISIG
|IEXTEN
|ECHOCTL
; /* XXX was echoctl on ? */
609 cflag
&= ~(CSIZE
|PARENB
);
610 if (flags
&(RAW
|LITOUT
|PASS8
)) {
612 if (!(flags
&(RAW
|PASS8
))
613 || (flags
&(RAW
|PASS8
|ANYP
)) == (PASS8
|ANYP
))
617 if (flags
&(RAW
|LITOUT
))
626 /* XXX don't set INPCK if RAW or PASS8? */
627 if ((flags
&(EVENP
|ODDP
)) == EVENP
) {
630 } else if ((flags
&(EVENP
|ODDP
)) == ODDP
) {
639 if ((flags
&DECCTQ
) == 0)
652 * Description: Given a set of compatability terminal state local flags,
653 * convert the compatability flags in the terminal flags
654 * fields into canonical flags in the provided termios struct.
656 * Parameters: struct tty *tp The tty on which the operation is
658 * struct termios *t The termios structure into which to
659 * return the converted local flags.
661 * Returns: void (implicit: *t, modified)
664 ttcompatsetlflags(struct tty
*tp
, struct termios
*t
)
666 register int flags
= tp
->t_flags
;
667 register tcflag_t iflag
= t
->c_iflag
;
668 register tcflag_t oflag
= t
->c_oflag
;
669 register tcflag_t lflag
= t
->c_lflag
;
670 register tcflag_t cflag
= t
->c_cflag
;
672 iflag
&= ~(PARMRK
|IGNPAR
|IGNCR
|INLCR
);
693 if ((flags
&DECCTQ
) == 0)
705 lflag
&= ~(TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
706 lflag
|= flags
&(TOSTOP
|FLUSHO
|PENDIN
|NOFLSH
);
709 * The next if-else statement is copied from above so don't bother
710 * checking it separately. We could avoid fiddlling with the
711 * character size if the mode is already RAW or if neither the
712 * LITOUT bit or the PASS8 bit is being changed, but the delta of
713 * the change is not available here and skipping the RAW case would
714 * make the code different from above.
716 cflag
&= ~(CSIZE
|PARENB
);
717 if (flags
&(RAW
|LITOUT
|PASS8
)) {
719 if (!(flags
&(RAW
|PASS8
))
720 || (flags
&(RAW
|PASS8
|ANYP
)) == (PASS8
|ANYP
))
724 if (flags
&(RAW
|LITOUT
))
738 #endif /* COMPAT_43_TTY || COMPAT_SUNOS */