17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*3bb2c156SToomas Soome /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <termio.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <signal.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #include "tmextern.h"
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #define NTRY 5
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * At this time, we only recognize certain speeds.
477c478bd9Sstevel@tonic-gate * This table can be expanded if new patterns are found
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate static struct autobaud {
507c478bd9Sstevel@tonic-gate char *a_speed;
517c478bd9Sstevel@tonic-gate char *a_pattern; /* first byte is length */
527c478bd9Sstevel@tonic-gate } autob2400[] = {
537c478bd9Sstevel@tonic-gate #ifdef i386
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate * These are the bit patterns returned on x86 boxes
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate "110", "\1\000",
587c478bd9Sstevel@tonic-gate #else
597c478bd9Sstevel@tonic-gate "110", "\3\000\000\000",
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate "1200", "\2\346\200",
627c478bd9Sstevel@tonic-gate "2400", "\1\15",
637c478bd9Sstevel@tonic-gate "4800", "\1\371",
647c478bd9Sstevel@tonic-gate "4800", "\1\362",
657c478bd9Sstevel@tonic-gate "9600", "\1\377",
667c478bd9Sstevel@tonic-gate 0, 0
677c478bd9Sstevel@tonic-gate };
687c478bd9Sstevel@tonic-gate
697c478bd9Sstevel@tonic-gate /*
707c478bd9Sstevel@tonic-gate * auto_termio - set termio to allow autobaud
717c478bd9Sstevel@tonic-gate * - the line is set to raw mode, with VMIN = 5, VTIME = 1
727c478bd9Sstevel@tonic-gate * - baud rate is set to 2400
737c478bd9Sstevel@tonic-gate */
747c478bd9Sstevel@tonic-gate int
auto_termio(int fd)757c478bd9Sstevel@tonic-gate auto_termio(int fd)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate struct termio termio;
787c478bd9Sstevel@tonic-gate struct termios termios;
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate if (ioctl(fd, TCGETS, &termios) == -1) {
817c478bd9Sstevel@tonic-gate if (ioctl(fd, TCGETA, &termio) == -1) {
827c478bd9Sstevel@tonic-gate log("auto_termio: ioctl TCGETA failed, fd = %d: %s", fd,
837c478bd9Sstevel@tonic-gate strerror(errno));
84*3bb2c156SToomas Soome return (-1);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate termio.c_iflag = 0;
87*3bb2c156SToomas Soome termio.c_cflag &= ~(CBAUD|CSIZE|PARENB);
887c478bd9Sstevel@tonic-gate termio.c_cflag |= CREAD|HUPCL|(CS8&CSIZE)|(B2400&CBAUD);
897c478bd9Sstevel@tonic-gate termio.c_lflag &= ~(ISIG|ICANON|ECHO|ECHOE|ECHOK);
907c478bd9Sstevel@tonic-gate termio.c_oflag = 0;
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate termio.c_cc[VMIN] = 5;
937c478bd9Sstevel@tonic-gate termio.c_cc[VTIME] = 1;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate if (ioctl(fd, TCSETAF, &termio) == -1) {
967c478bd9Sstevel@tonic-gate log("auto_termio: ioctl TCSETAF failed, fd = %d: %s",
977c478bd9Sstevel@tonic-gate fd, strerror(errno));
98*3bb2c156SToomas Soome return (-1);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate } else {
1017c478bd9Sstevel@tonic-gate termios.c_iflag &= 0xffff0000;
102*3bb2c156SToomas Soome termios.c_cflag &= ~(CSIZE|PARENB);
1037c478bd9Sstevel@tonic-gate termios.c_cflag |= CREAD|HUPCL|(CS8&CSIZE);
1047c478bd9Sstevel@tonic-gate termios.c_lflag &= ~(ISIG|ICANON|ECHO|ECHOE|ECHOK);
1057c478bd9Sstevel@tonic-gate termios.c_oflag &= 0xffff0000;
1067c478bd9Sstevel@tonic-gate
1077c478bd9Sstevel@tonic-gate termios.c_cc[VMIN] = 5;
1087c478bd9Sstevel@tonic-gate termios.c_cc[VTIME] = 1;
109*3bb2c156SToomas Soome (void) cfsetospeed(&termios, B2400);
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate if (ioctl(fd, TCSETSF, &termios) == -1) {
1127c478bd9Sstevel@tonic-gate log("auto_termio: ioctl TCSETSF failed, fd = %d: %s",
113*3bb2c156SToomas Soome fd, strerror(errno));
114*3bb2c156SToomas Soome return (-1);
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate }
117*3bb2c156SToomas Soome return (0);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate * autobaud - determine the baudrate by reading data at 2400 baud rate
122*3bb2c156SToomas Soome * - the program is anticipating <CR>
1237c478bd9Sstevel@tonic-gate * - the bit pattern is matched again an autobaud table
1247c478bd9Sstevel@tonic-gate * - if a match is found, the matched speed is returned
125*3bb2c156SToomas Soome * - otherwise, NULL is returned
1267c478bd9Sstevel@tonic-gate */
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate char *
autobaud(int fd,int timeout)1297c478bd9Sstevel@tonic-gate autobaud(int fd, int timeout)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate int i, k, count;
1327c478bd9Sstevel@tonic-gate static char buf[5];
133*3bb2c156SToomas Soome char *cp = buf;
1347c478bd9Sstevel@tonic-gate struct autobaud *tp;
1357c478bd9Sstevel@tonic-gate struct sigaction sigact;
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate #ifdef DEBUG
1387c478bd9Sstevel@tonic-gate debug("in autobaud");
1397c478bd9Sstevel@tonic-gate #endif
140*3bb2c156SToomas Soome (void) auto_termio(fd);
1417c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
1427c478bd9Sstevel@tonic-gate sigact.sa_handler = SIG_IGN;
143*3bb2c156SToomas Soome (void) sigemptyset(&sigact.sa_mask);
144*3bb2c156SToomas Soome (void) sigaction(SIGINT, &sigact, NULL);
1457c478bd9Sstevel@tonic-gate count = NTRY;
1467c478bd9Sstevel@tonic-gate while (count--) {
1477c478bd9Sstevel@tonic-gate if (timeout) {
1487c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
1497c478bd9Sstevel@tonic-gate sigact.sa_handler = timedout;
150*3bb2c156SToomas Soome (void) sigemptyset(&sigact.sa_mask);
151*3bb2c156SToomas Soome (void) sigaction(SIGALRM, &sigact, NULL);
152*3bb2c156SToomas Soome (void) alarm((unsigned)timeout);
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate cp = &buf[1];
155*3bb2c156SToomas Soome if (peek_ptr != NULL) {
156*3bb2c156SToomas Soome k = peek_ptr->len;
157*3bb2c156SToomas Soome (void) strncpy(cp, peek_ptr->buf, k);
1587c478bd9Sstevel@tonic-gate peek_ptr = NULL;
159*3bb2c156SToomas Soome } else if ((k = read(fd, cp, 5)) < 0) {
1607c478bd9Sstevel@tonic-gate fatal("autobaud: read failed: %s", strerror(errno));
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate if (timeout)
163*3bb2c156SToomas Soome (void) alarm((unsigned)0);
1647c478bd9Sstevel@tonic-gate buf[0] = (char)k;
1657c478bd9Sstevel@tonic-gate for (tp = autob2400; tp->a_speed; tp++) {
166*3bb2c156SToomas Soome for (i = 0; ; i++) {
1677c478bd9Sstevel@tonic-gate if (buf[i] != tp->a_pattern[i])
1687c478bd9Sstevel@tonic-gate break;
1697c478bd9Sstevel@tonic-gate if (i == buf[0]) {
170*3bb2c156SToomas Soome return (tp->a_speed);
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate flush_input(fd);
1757c478bd9Sstevel@tonic-gate } /* end while */
176*3bb2c156SToomas Soome return (NULL); /* autobaud failed */
1777c478bd9Sstevel@tonic-gate }
178