1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
29*7c478bd9Sstevel@tonic-gate
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
36*7c478bd9Sstevel@tonic-gate #include <string.h>
37*7c478bd9Sstevel@tonic-gate #include <termio.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <signal.h>
40*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #include "tmextern.h"
45*7c478bd9Sstevel@tonic-gate
46*7c478bd9Sstevel@tonic-gate #define NTRY 5
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate /*
49*7c478bd9Sstevel@tonic-gate * At this time, we only recognize certain speeds.
50*7c478bd9Sstevel@tonic-gate * This table can be expanded if new patterns are found
51*7c478bd9Sstevel@tonic-gate */
52*7c478bd9Sstevel@tonic-gate static struct autobaud {
53*7c478bd9Sstevel@tonic-gate char *a_speed;
54*7c478bd9Sstevel@tonic-gate char *a_pattern; /* first byte is length */
55*7c478bd9Sstevel@tonic-gate } autob2400[] = {
56*7c478bd9Sstevel@tonic-gate #ifdef i386
57*7c478bd9Sstevel@tonic-gate /*
58*7c478bd9Sstevel@tonic-gate * These are the bit patterns returned on x86 boxes
59*7c478bd9Sstevel@tonic-gate */
60*7c478bd9Sstevel@tonic-gate "110", "\1\000",
61*7c478bd9Sstevel@tonic-gate #else
62*7c478bd9Sstevel@tonic-gate "110", "\3\000\000\000",
63*7c478bd9Sstevel@tonic-gate #endif
64*7c478bd9Sstevel@tonic-gate "1200", "\2\346\200",
65*7c478bd9Sstevel@tonic-gate "2400", "\1\15",
66*7c478bd9Sstevel@tonic-gate "4800", "\1\371",
67*7c478bd9Sstevel@tonic-gate "4800", "\1\362",
68*7c478bd9Sstevel@tonic-gate "9600", "\1\377",
69*7c478bd9Sstevel@tonic-gate 0, 0
70*7c478bd9Sstevel@tonic-gate };
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate extern struct strbuf *peek_ptr;
73*7c478bd9Sstevel@tonic-gate
74*7c478bd9Sstevel@tonic-gate /*
75*7c478bd9Sstevel@tonic-gate * auto_termio - set termio to allow autobaud
76*7c478bd9Sstevel@tonic-gate * - the line is set to raw mode, with VMIN = 5, VTIME = 1
77*7c478bd9Sstevel@tonic-gate * - baud rate is set to 2400
78*7c478bd9Sstevel@tonic-gate */
79*7c478bd9Sstevel@tonic-gate int
auto_termio(int fd)80*7c478bd9Sstevel@tonic-gate auto_termio(int fd)
81*7c478bd9Sstevel@tonic-gate {
82*7c478bd9Sstevel@tonic-gate struct termio termio;
83*7c478bd9Sstevel@tonic-gate struct termios termios;
84*7c478bd9Sstevel@tonic-gate
85*7c478bd9Sstevel@tonic-gate if (ioctl(fd, TCGETS, &termios) == -1) {
86*7c478bd9Sstevel@tonic-gate if (ioctl(fd, TCGETA, &termio) == -1) {
87*7c478bd9Sstevel@tonic-gate log("auto_termio: ioctl TCGETA failed, fd = %d: %s", fd,
88*7c478bd9Sstevel@tonic-gate strerror(errno));
89*7c478bd9Sstevel@tonic-gate return(-1);
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate termio.c_iflag = 0;
92*7c478bd9Sstevel@tonic-gate termio.c_cflag &= ~(CBAUD|CSIZE|PARENB);
93*7c478bd9Sstevel@tonic-gate termio.c_cflag |= CREAD|HUPCL|(CS8&CSIZE)|(B2400&CBAUD);
94*7c478bd9Sstevel@tonic-gate termio.c_lflag &= ~(ISIG|ICANON|ECHO|ECHOE|ECHOK);
95*7c478bd9Sstevel@tonic-gate termio.c_oflag = 0;
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate termio.c_cc[VMIN] = 5;
98*7c478bd9Sstevel@tonic-gate termio.c_cc[VTIME] = 1;
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate if (ioctl(fd, TCSETAF, &termio) == -1) {
101*7c478bd9Sstevel@tonic-gate log("auto_termio: ioctl TCSETAF failed, fd = %d: %s",
102*7c478bd9Sstevel@tonic-gate fd, strerror(errno));
103*7c478bd9Sstevel@tonic-gate return(-1);
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate } else {
106*7c478bd9Sstevel@tonic-gate termios.c_iflag &= 0xffff0000;
107*7c478bd9Sstevel@tonic-gate termios.c_cflag &= ~(CSIZE|PARENB);
108*7c478bd9Sstevel@tonic-gate termios.c_cflag |= CREAD|HUPCL|(CS8&CSIZE);
109*7c478bd9Sstevel@tonic-gate termios.c_lflag &= ~(ISIG|ICANON|ECHO|ECHOE|ECHOK);
110*7c478bd9Sstevel@tonic-gate termios.c_oflag &= 0xffff0000;
111*7c478bd9Sstevel@tonic-gate
112*7c478bd9Sstevel@tonic-gate termios.c_cc[VMIN] = 5;
113*7c478bd9Sstevel@tonic-gate termios.c_cc[VTIME] = 1;
114*7c478bd9Sstevel@tonic-gate cfsetospeed(&termios, B2400);
115*7c478bd9Sstevel@tonic-gate
116*7c478bd9Sstevel@tonic-gate if (ioctl(fd, TCSETSF, &termios) == -1) {
117*7c478bd9Sstevel@tonic-gate log("auto_termio: ioctl TCSETSF failed, fd = %d: %s",
118*7c478bd9Sstevel@tonic-gate fd, strerror(errno));
119*7c478bd9Sstevel@tonic-gate return(-1);
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate }
122*7c478bd9Sstevel@tonic-gate return(0);
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate
125*7c478bd9Sstevel@tonic-gate /*
126*7c478bd9Sstevel@tonic-gate * autobaud - determine the baudrate by reading data at 2400 baud rate
127*7c478bd9Sstevel@tonic-gate * - the program is anticipating <CR>
128*7c478bd9Sstevel@tonic-gate * - the bit pattern is matched again an autobaud table
129*7c478bd9Sstevel@tonic-gate * - if a match is found, the matched speed is returned
130*7c478bd9Sstevel@tonic-gate * - otherwise, NULL is returned
131*7c478bd9Sstevel@tonic-gate */
132*7c478bd9Sstevel@tonic-gate
133*7c478bd9Sstevel@tonic-gate char *
autobaud(int fd,int timeout)134*7c478bd9Sstevel@tonic-gate autobaud(int fd, int timeout)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate int i, k, count;
137*7c478bd9Sstevel@tonic-gate static char buf[5];
138*7c478bd9Sstevel@tonic-gate register char *cp = buf;
139*7c478bd9Sstevel@tonic-gate struct autobaud *tp;
140*7c478bd9Sstevel@tonic-gate struct sigaction sigact;
141*7c478bd9Sstevel@tonic-gate extern void timedout();
142*7c478bd9Sstevel@tonic-gate extern void flush_input();
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate #ifdef DEBUG
145*7c478bd9Sstevel@tonic-gate debug("in autobaud");
146*7c478bd9Sstevel@tonic-gate #endif
147*7c478bd9Sstevel@tonic-gate auto_termio(fd);
148*7c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
149*7c478bd9Sstevel@tonic-gate sigact.sa_handler = SIG_IGN;
150*7c478bd9Sstevel@tonic-gate (void)sigemptyset(&sigact.sa_mask);
151*7c478bd9Sstevel@tonic-gate (void)sigaction(SIGINT, &sigact, NULL);
152*7c478bd9Sstevel@tonic-gate count = NTRY;
153*7c478bd9Sstevel@tonic-gate while (count--) {
154*7c478bd9Sstevel@tonic-gate if (timeout) {
155*7c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
156*7c478bd9Sstevel@tonic-gate sigact.sa_handler = timedout;
157*7c478bd9Sstevel@tonic-gate (void)sigemptyset(&sigact.sa_mask);
158*7c478bd9Sstevel@tonic-gate (void)sigaction(SIGALRM, &sigact, NULL);
159*7c478bd9Sstevel@tonic-gate (void)alarm((unsigned)timeout);
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate cp = &buf[1];
162*7c478bd9Sstevel@tonic-gate if ( peek_ptr ) {
163*7c478bd9Sstevel@tonic-gate strncpy(cp, peek_ptr->buf, k=peek_ptr->len);
164*7c478bd9Sstevel@tonic-gate peek_ptr = NULL;
165*7c478bd9Sstevel@tonic-gate } else if ((k=read(fd, cp, 5)) < 0) {
166*7c478bd9Sstevel@tonic-gate fatal("autobaud: read failed: %s", strerror(errno));
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate if (timeout)
169*7c478bd9Sstevel@tonic-gate (void)alarm((unsigned)0);
170*7c478bd9Sstevel@tonic-gate buf[0] = (char)k;
171*7c478bd9Sstevel@tonic-gate for (tp = autob2400; tp->a_speed; tp++) {
172*7c478bd9Sstevel@tonic-gate for (i = 0;; i++) {
173*7c478bd9Sstevel@tonic-gate if (buf[i] != tp->a_pattern[i])
174*7c478bd9Sstevel@tonic-gate break;
175*7c478bd9Sstevel@tonic-gate if (i == buf[0]) {
176*7c478bd9Sstevel@tonic-gate return(tp->a_speed);
177*7c478bd9Sstevel@tonic-gate }
178*7c478bd9Sstevel@tonic-gate }
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate flush_input(fd);
181*7c478bd9Sstevel@tonic-gate } /* end while */
182*7c478bd9Sstevel@tonic-gate return(NULL); /* autobaud failed */
183*7c478bd9Sstevel@tonic-gate }
184