4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
41 #include <sys/types.h>
42 #include <sys/stropts.h>
49 * At this time, we only recognize certain speeds.
50 * This table can be expanded if new patterns are found
52 static struct autobaud
{
54 char *a_pattern
; /* first byte is length */
58 * These are the bit patterns returned on x86 boxes
62 "110", "\3\000\000\000",
72 extern struct strbuf
*peek_ptr
;
75 * auto_termio - set termio to allow autobaud
76 * - the line is set to raw mode, with VMIN = 5, VTIME = 1
77 * - baud rate is set to 2400
83 struct termios termios
;
85 if (ioctl(fd
, TCGETS
, &termios
) == -1) {
86 if (ioctl(fd
, TCGETA
, &termio
) == -1) {
87 log("auto_termio: ioctl TCGETA failed, fd = %d: %s", fd
,
92 termio
.c_cflag
&= ~(CBAUD
|CSIZE
|PARENB
);
93 termio
.c_cflag
|= CREAD
|HUPCL
|(CS8
&CSIZE
)|(B2400
&CBAUD
);
94 termio
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
|ECHOE
|ECHOK
);
97 termio
.c_cc
[VMIN
] = 5;
98 termio
.c_cc
[VTIME
] = 1;
100 if (ioctl(fd
, TCSETAF
, &termio
) == -1) {
101 log("auto_termio: ioctl TCSETAF failed, fd = %d: %s",
102 fd
, strerror(errno
));
106 termios
.c_iflag
&= 0xffff0000;
107 termios
.c_cflag
&= ~(CSIZE
|PARENB
);
108 termios
.c_cflag
|= CREAD
|HUPCL
|(CS8
&CSIZE
);
109 termios
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
|ECHOE
|ECHOK
);
110 termios
.c_oflag
&= 0xffff0000;
112 termios
.c_cc
[VMIN
] = 5;
113 termios
.c_cc
[VTIME
] = 1;
114 cfsetospeed(&termios
, B2400
);
116 if (ioctl(fd
, TCSETSF
, &termios
) == -1) {
117 log("auto_termio: ioctl TCSETSF failed, fd = %d: %s",
118 fd
, strerror(errno
));
126 * autobaud - determine the baudrate by reading data at 2400 baud rate
127 * - the program is anticipating <CR>
128 * - the bit pattern is matched again an autobaud table
129 * - if a match is found, the matched speed is returned
130 * - otherwise, NULL is returned
134 autobaud(int fd
, int timeout
)
138 register char *cp
= buf
;
140 struct sigaction sigact
;
141 extern void timedout();
142 extern void flush_input();
145 debug("in autobaud");
149 sigact
.sa_handler
= SIG_IGN
;
150 (void)sigemptyset(&sigact
.sa_mask
);
151 (void)sigaction(SIGINT
, &sigact
, NULL
);
156 sigact
.sa_handler
= timedout
;
157 (void)sigemptyset(&sigact
.sa_mask
);
158 (void)sigaction(SIGALRM
, &sigact
, NULL
);
159 (void)alarm((unsigned)timeout
);
163 strncpy(cp
, peek_ptr
->buf
, k
=peek_ptr
->len
);
165 } else if ((k
=read(fd
, cp
, 5)) < 0) {
166 fatal("autobaud: read failed: %s", strerror(errno
));
169 (void)alarm((unsigned)0);
171 for (tp
= autob2400
; tp
->a_speed
; tp
++) {
173 if (buf
[i
] != tp
->a_pattern
[i
])
182 return(NULL
); /* autobaud failed */