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
5*c7e4935fSss  * Common Development and Distribution License (the "License").
6*c7e4935fSss  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*c7e4935fSss  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Initialize and re-initialize synchronous serial clocking and loopback
287c478bd9Sstevel@tonic-gate  * options.  Interfaces through the S_IOCGETMODE and S_IOCSETMODE ioctls.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
347c478bd9Sstevel@tonic-gate #include <stdio.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <sys/stream.h>
397c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/ser_sync.h>
437c478bd9Sstevel@tonic-gate #include <libdlpi.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static void usage(void);
467c478bd9Sstevel@tonic-gate static int prefix(char *arg, char *pref);
477c478bd9Sstevel@tonic-gate static int lookup(char **table, char *arg);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate static char *yesno[] = {
507c478bd9Sstevel@tonic-gate 	"no",
517c478bd9Sstevel@tonic-gate 	"yes",
527c478bd9Sstevel@tonic-gate 	"silent",
537c478bd9Sstevel@tonic-gate 	0,
547c478bd9Sstevel@tonic-gate };
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate static char *txnames[] = {
577c478bd9Sstevel@tonic-gate 	"txc",
587c478bd9Sstevel@tonic-gate 	"rxc",
597c478bd9Sstevel@tonic-gate 	"baud",
607c478bd9Sstevel@tonic-gate 	"pll",
617c478bd9Sstevel@tonic-gate 	"sysclk",
627c478bd9Sstevel@tonic-gate 	"-txc",
637c478bd9Sstevel@tonic-gate 	0,
647c478bd9Sstevel@tonic-gate };
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static char *rxnames[] = {
677c478bd9Sstevel@tonic-gate 	"rxc",
687c478bd9Sstevel@tonic-gate 	"txc",
697c478bd9Sstevel@tonic-gate 	"baud",
707c478bd9Sstevel@tonic-gate 	"pll",
717c478bd9Sstevel@tonic-gate 	"sysclk",
727c478bd9Sstevel@tonic-gate 	"-rxc",
737c478bd9Sstevel@tonic-gate 	0,
747c478bd9Sstevel@tonic-gate };
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #ifdef notdef
777c478bd9Sstevel@tonic-gate static char *txdnames[] = {
787c478bd9Sstevel@tonic-gate 	"txd",
797c478bd9Sstevel@tonic-gate 	" ",	/* dummy entry, do not remove */
807c478bd9Sstevel@tonic-gate 	"-txd",
817c478bd9Sstevel@tonic-gate 	0,
827c478bd9Sstevel@tonic-gate };
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static char *rxdnames[] = {
857c478bd9Sstevel@tonic-gate 	"rxd",
867c478bd9Sstevel@tonic-gate 	"-rxd",
877c478bd9Sstevel@tonic-gate 	0,
887c478bd9Sstevel@tonic-gate };
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate static char *portab[] = {
917c478bd9Sstevel@tonic-gate 	"rs422",
927c478bd9Sstevel@tonic-gate 	"v35",
937c478bd9Sstevel@tonic-gate 	0,
947c478bd9Sstevel@tonic-gate };
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #define	equal(a, b)	(strcmp((a), (b)) == 0)
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)1007c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1017c478bd9Sstevel@tonic-gate {
102*c7e4935fSss 	char cnambuf[DLPI_LINKNAME_MAX], device[DLPI_LINKNAME_MAX];
1037c478bd9Sstevel@tonic-gate 	struct scc_mode sm;
1047c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
1057c478bd9Sstevel@tonic-gate 	int fd, speed;
106*c7e4935fSss 	int retval;
1077c478bd9Sstevel@tonic-gate 	char *arg, *cp;
1087c478bd9Sstevel@tonic-gate 	char loopchange = 0;
1097c478bd9Sstevel@tonic-gate 	char echochange = 0;
1107c478bd9Sstevel@tonic-gate 	char clockchange = 0;
111*c7e4935fSss 	uint_t ppa;
112*c7e4935fSss 	dlpi_handle_t dh;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (argc == 1) {
1157c478bd9Sstevel@tonic-gate 		usage();
1167c478bd9Sstevel@tonic-gate 		exit(1);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 	argc--;
1197c478bd9Sstevel@tonic-gate 	argv++;
120*c7e4935fSss 
121*c7e4935fSss 	if (strlcpy(cnambuf, argv[0], sizeof (cnambuf)) >=
122*c7e4935fSss 	    sizeof (cnambuf)) {
123*c7e4935fSss 		(void) fprintf(stderr,
124*c7e4935fSss 		    "syncinit: invalid device name (too long) %s\n", argv[0]);
125*c7e4935fSss 		exit(1);
1267c478bd9Sstevel@tonic-gate 	}
127*c7e4935fSss 
1287c478bd9Sstevel@tonic-gate 	cp = cnambuf;
1297c478bd9Sstevel@tonic-gate 	while (*cp)			/* find the end of the name */
1307c478bd9Sstevel@tonic-gate 		cp++;
1317c478bd9Sstevel@tonic-gate 	cp--;
1327c478bd9Sstevel@tonic-gate 	if (!isdigit(*cp)) {
1337c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
134*c7e4935fSss 		    "syncinit: %s missing minor device number\n", cnambuf);
1357c478bd9Sstevel@tonic-gate 		exit(1);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate 
138*c7e4935fSss 	retval = dlpi_open(cnambuf, &dh, DLPI_EXCL|DLPI_SERIAL);
139*c7e4935fSss 	if (retval != DLPI_SUCCESS) {
140*c7e4935fSss 		(void) fprintf(stderr, "syncinit: dlpi_open %s: %s\n", cnambuf,
141*c7e4935fSss 		    dlpi_strerror(retval));
1427c478bd9Sstevel@tonic-gate 		exit(1);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
145*c7e4935fSss 	(void) dlpi_parselink(cnambuf, device, &ppa);
146*c7e4935fSss 	(void) printf("device: %s  ppa: %u\n", device, ppa);
147*c7e4935fSss 
148*c7e4935fSss 	fd = dlpi_fd(dh);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	argc--;
1517c478bd9Sstevel@tonic-gate 	argv++;
1527c478bd9Sstevel@tonic-gate 	if (argc) {	/* setting things */
1537c478bd9Sstevel@tonic-gate 		sioc.ic_cmd = S_IOCGETMODE;
1547c478bd9Sstevel@tonic-gate 		sioc.ic_timout = -1;
1557c478bd9Sstevel@tonic-gate 		sioc.ic_len = sizeof (struct scc_mode);
1567c478bd9Sstevel@tonic-gate 		sioc.ic_dp = (char *)&sm;
157*c7e4935fSss 
1587c478bd9Sstevel@tonic-gate 		if (ioctl(fd, I_STR, &sioc) < 0) {
1597c478bd9Sstevel@tonic-gate 			perror("S_IOCGETMODE");
1607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1617c478bd9Sstevel@tonic-gate 				"syncinit: can't get sync mode info for %s\n",
1627c478bd9Sstevel@tonic-gate 				cnambuf);
1637c478bd9Sstevel@tonic-gate 			exit(1);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 		while (argc-- > 0) {
1667c478bd9Sstevel@tonic-gate 			arg = *argv++;
1677c478bd9Sstevel@tonic-gate 			if (sscanf(arg, "%d", &speed) == 1)
1687c478bd9Sstevel@tonic-gate 				sm.sm_baudrate = speed;
1697c478bd9Sstevel@tonic-gate 			else if (strchr(arg, '=')) {
1707c478bd9Sstevel@tonic-gate 				if (prefix(arg, "loop")) {
1717c478bd9Sstevel@tonic-gate 					if (lookup(yesno, arg))
1727c478bd9Sstevel@tonic-gate 						sm.sm_config |= CONN_LPBK;
1737c478bd9Sstevel@tonic-gate 					else
1747c478bd9Sstevel@tonic-gate 						sm.sm_config &= ~CONN_LPBK;
1757c478bd9Sstevel@tonic-gate 					loopchange++;
1767c478bd9Sstevel@tonic-gate 				} else if (prefix(arg, "echo")) {
1777c478bd9Sstevel@tonic-gate 					if (lookup(yesno, arg))
1787c478bd9Sstevel@tonic-gate 						sm.sm_config |= CONN_ECHO;
1797c478bd9Sstevel@tonic-gate 					else
1807c478bd9Sstevel@tonic-gate 						sm.sm_config &= ~CONN_ECHO;
1817c478bd9Sstevel@tonic-gate 					echochange++;
1827c478bd9Sstevel@tonic-gate 				} else if (prefix(arg, "nrzi")) {
1837c478bd9Sstevel@tonic-gate 					if (lookup(yesno, arg))
1847c478bd9Sstevel@tonic-gate 						sm.sm_config |= CONN_NRZI;
1857c478bd9Sstevel@tonic-gate 					else
1867c478bd9Sstevel@tonic-gate 						sm.sm_config &= ~CONN_NRZI;
1877c478bd9Sstevel@tonic-gate 				} else if (prefix(arg, "txc")) {
1887c478bd9Sstevel@tonic-gate 					sm.sm_txclock = lookup(txnames, arg);
1897c478bd9Sstevel@tonic-gate 					clockchange++;
1907c478bd9Sstevel@tonic-gate 				} else if (prefix(arg, "rxc")) {
1917c478bd9Sstevel@tonic-gate 					sm.sm_rxclock = lookup(rxnames, arg);
1927c478bd9Sstevel@tonic-gate 					clockchange++;
1937c478bd9Sstevel@tonic-gate 				} else if (prefix(arg, "speed")) {
1947c478bd9Sstevel@tonic-gate 					arg = strchr(arg, '=') + 1;
1957c478bd9Sstevel@tonic-gate 					if (sscanf(arg, "%d", &speed) == 1) {
1967c478bd9Sstevel@tonic-gate 						sm.sm_baudrate = speed;
1977c478bd9Sstevel@tonic-gate 					} else
1987c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr,
1997c478bd9Sstevel@tonic-gate 						    "syncinit: %s %s\n",
2007c478bd9Sstevel@tonic-gate 						    "bad speed:", arg);
2017c478bd9Sstevel@tonic-gate 				}
2027c478bd9Sstevel@tonic-gate 			} else if (equal(arg, "external")) {
2037c478bd9Sstevel@tonic-gate 				sm.sm_txclock = TXC_IS_TXC;
2047c478bd9Sstevel@tonic-gate 				sm.sm_rxclock = RXC_IS_RXC;
2057c478bd9Sstevel@tonic-gate 				sm.sm_config &= ~CONN_LPBK;
2067c478bd9Sstevel@tonic-gate 			} else if (equal(arg, "sender")) {
2077c478bd9Sstevel@tonic-gate 				sm.sm_txclock = TXC_IS_BAUD;
2087c478bd9Sstevel@tonic-gate 				sm.sm_rxclock = RXC_IS_RXC;
2097c478bd9Sstevel@tonic-gate 				sm.sm_config &= ~CONN_LPBK;
2107c478bd9Sstevel@tonic-gate 			} else if (equal(arg, "internal")) {
2117c478bd9Sstevel@tonic-gate 				sm.sm_txclock = TXC_IS_PLL;
2127c478bd9Sstevel@tonic-gate 				sm.sm_rxclock = RXC_IS_PLL;
2137c478bd9Sstevel@tonic-gate 				sm.sm_config &= ~CONN_LPBK;
2147c478bd9Sstevel@tonic-gate 			} else if (equal(arg, "stop")) {
2157c478bd9Sstevel@tonic-gate 				sm.sm_baudrate = 0;
2167c478bd9Sstevel@tonic-gate 			} else
2177c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "Bad arg: %s\n", arg);
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		/*
2217c478bd9Sstevel@tonic-gate 		 * If we're going to change the state of loopback, and we
2227c478bd9Sstevel@tonic-gate 		 * don't have our own plans for clock sources, use defaults.
2237c478bd9Sstevel@tonic-gate 		 */
2247c478bd9Sstevel@tonic-gate 		if (loopchange && !clockchange) {
2257c478bd9Sstevel@tonic-gate 			if (sm.sm_config & CONN_LPBK) {
2267c478bd9Sstevel@tonic-gate 				sm.sm_txclock = TXC_IS_BAUD;
2277c478bd9Sstevel@tonic-gate 				sm.sm_rxclock = RXC_IS_BAUD;
2287c478bd9Sstevel@tonic-gate 			} else {
2297c478bd9Sstevel@tonic-gate 				sm.sm_txclock = TXC_IS_TXC;
2307c478bd9Sstevel@tonic-gate 				sm.sm_rxclock = RXC_IS_RXC;
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 		}
2337c478bd9Sstevel@tonic-gate 		sioc.ic_cmd = S_IOCSETMODE;
2347c478bd9Sstevel@tonic-gate 		sioc.ic_timout = -1;
2357c478bd9Sstevel@tonic-gate 		sioc.ic_len = sizeof (struct scc_mode);
2367c478bd9Sstevel@tonic-gate 		sioc.ic_dp = (char *)&sm;
2377c478bd9Sstevel@tonic-gate 		if (ioctl(fd, I_STR, &sioc) < 0) {
2387c478bd9Sstevel@tonic-gate 			perror("S_IOCSETMODE");
2397c478bd9Sstevel@tonic-gate 			(void) ioctl(fd, S_IOCGETMODE, &sm);
2407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
2417c478bd9Sstevel@tonic-gate 				"syncinit: ioctl failure code = %x\n",
2427c478bd9Sstevel@tonic-gate 				sm.sm_retval);
2437c478bd9Sstevel@tonic-gate 			exit(1);
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	/* Report State */
2487c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
2497c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
2507c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
2517c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
2527c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &sioc) < 0) {
2537c478bd9Sstevel@tonic-gate 		perror("S_IOCGETMODE");
2547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
2557c478bd9Sstevel@tonic-gate 			"syncinit: can't get sync mode info for %s\n",
2567c478bd9Sstevel@tonic-gate 			cnambuf);
2577c478bd9Sstevel@tonic-gate 		exit(1);
2587c478bd9Sstevel@tonic-gate 	}
2597c478bd9Sstevel@tonic-gate 	(void) printf(
260*c7e4935fSss 		"speed=%d, loopback=%s, echo=%s, nrzi=%s, txc=%s, rxc=%s\n",
2617c478bd9Sstevel@tonic-gate 		sm.sm_baudrate,
2627c478bd9Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_LPBK) > 0)],
2637c478bd9Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_ECHO) > 0)],
2647c478bd9Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_NRZI) > 0)],
2657c478bd9Sstevel@tonic-gate 		txnames[sm.sm_txclock],
2667c478bd9Sstevel@tonic-gate 		rxnames[sm.sm_rxclock]);
2677c478bd9Sstevel@tonic-gate 	return (0);
2687c478bd9Sstevel@tonic-gate }
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static void
usage()2717c478bd9Sstevel@tonic-gate usage()
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage: syncinit cnambuf \\\n");
2747c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t[baudrate] [loopback=[yes|no]] ");
2757c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "[echo=[yes|no]] [nrzi=[yes|no]] \\\n");
2767c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t[txc=[txc|rxc|baud|pll]] \\\n");
2777c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\t[rxc=[rxc|txc|baud|pll]]\n");
2787c478bd9Sstevel@tonic-gate 	exit(1);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate static int
prefix(char * arg,char * pref)2827c478bd9Sstevel@tonic-gate prefix(char *arg, char *pref)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	return (strncmp(arg, pref, strlen(pref)) == 0);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate static int
lookup(char ** table,char * arg)2887c478bd9Sstevel@tonic-gate lookup(char **table, char *arg)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	char *val = strchr(arg, '=') + 1;
2917c478bd9Sstevel@tonic-gate 	int ival;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	for (ival = 0; *table != 0; ival++, table++)
2947c478bd9Sstevel@tonic-gate 		if (equal(*table, val))
2957c478bd9Sstevel@tonic-gate 			return (ival);
2967c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "syncinit: bad arg: %s\n", arg);
2977c478bd9Sstevel@tonic-gate 	exit(1);
2987c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2997c478bd9Sstevel@tonic-gate }
300