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*c7e4935fSss150715 * Common Development and Distribution License (the "License"). 6*c7e4935fSss150715 * 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*c7e4935fSss150715 * 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 #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * Initialize and re-initialize synchronous serial clocking and loopback 307c478bd9Sstevel@tonic-gate * options. Interfaces through the S_IOCGETMODE and S_IOCSETMODE ioctls. 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <sys/types.h> 347c478bd9Sstevel@tonic-gate #include <ctype.h> 357c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <unistd.h> 397c478bd9Sstevel@tonic-gate #include <string.h> 407c478bd9Sstevel@tonic-gate #include <sys/stream.h> 417c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 427c478bd9Sstevel@tonic-gate #include <fcntl.h> 437c478bd9Sstevel@tonic-gate #include <errno.h> 447c478bd9Sstevel@tonic-gate #include <sys/ser_sync.h> 457c478bd9Sstevel@tonic-gate #include <libdlpi.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static void usage(void); 487c478bd9Sstevel@tonic-gate static int prefix(char *arg, char *pref); 497c478bd9Sstevel@tonic-gate static int lookup(char **table, char *arg); 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate static char *yesno[] = { 527c478bd9Sstevel@tonic-gate "no", 537c478bd9Sstevel@tonic-gate "yes", 547c478bd9Sstevel@tonic-gate "silent", 557c478bd9Sstevel@tonic-gate 0, 567c478bd9Sstevel@tonic-gate }; 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate static char *txnames[] = { 597c478bd9Sstevel@tonic-gate "txc", 607c478bd9Sstevel@tonic-gate "rxc", 617c478bd9Sstevel@tonic-gate "baud", 627c478bd9Sstevel@tonic-gate "pll", 637c478bd9Sstevel@tonic-gate "sysclk", 647c478bd9Sstevel@tonic-gate "-txc", 657c478bd9Sstevel@tonic-gate 0, 667c478bd9Sstevel@tonic-gate }; 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static char *rxnames[] = { 697c478bd9Sstevel@tonic-gate "rxc", 707c478bd9Sstevel@tonic-gate "txc", 717c478bd9Sstevel@tonic-gate "baud", 727c478bd9Sstevel@tonic-gate "pll", 737c478bd9Sstevel@tonic-gate "sysclk", 747c478bd9Sstevel@tonic-gate "-rxc", 757c478bd9Sstevel@tonic-gate 0, 767c478bd9Sstevel@tonic-gate }; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #ifdef notdef 797c478bd9Sstevel@tonic-gate static char *txdnames[] = { 807c478bd9Sstevel@tonic-gate "txd", 817c478bd9Sstevel@tonic-gate " ", /* dummy entry, do not remove */ 827c478bd9Sstevel@tonic-gate "-txd", 837c478bd9Sstevel@tonic-gate 0, 847c478bd9Sstevel@tonic-gate }; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate static char *rxdnames[] = { 877c478bd9Sstevel@tonic-gate "rxd", 887c478bd9Sstevel@tonic-gate "-rxd", 897c478bd9Sstevel@tonic-gate 0, 907c478bd9Sstevel@tonic-gate }; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate static char *portab[] = { 937c478bd9Sstevel@tonic-gate "rs422", 947c478bd9Sstevel@tonic-gate "v35", 957c478bd9Sstevel@tonic-gate 0, 967c478bd9Sstevel@tonic-gate }; 977c478bd9Sstevel@tonic-gate #endif 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate #define equal(a, b) (strcmp((a), (b)) == 0) 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate int 1027c478bd9Sstevel@tonic-gate main(int argc, char **argv) 1037c478bd9Sstevel@tonic-gate { 104*c7e4935fSss150715 char cnambuf[DLPI_LINKNAME_MAX], device[DLPI_LINKNAME_MAX]; 1057c478bd9Sstevel@tonic-gate struct scc_mode sm; 1067c478bd9Sstevel@tonic-gate struct strioctl sioc; 1077c478bd9Sstevel@tonic-gate int fd, speed; 108*c7e4935fSss150715 int retval; 1097c478bd9Sstevel@tonic-gate char *arg, *cp; 1107c478bd9Sstevel@tonic-gate char loopchange = 0; 1117c478bd9Sstevel@tonic-gate char echochange = 0; 1127c478bd9Sstevel@tonic-gate char clockchange = 0; 113*c7e4935fSss150715 uint_t ppa; 114*c7e4935fSss150715 dlpi_handle_t dh; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate if (argc == 1) { 1177c478bd9Sstevel@tonic-gate usage(); 1187c478bd9Sstevel@tonic-gate exit(1); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate argc--; 1217c478bd9Sstevel@tonic-gate argv++; 122*c7e4935fSss150715 123*c7e4935fSss150715 if (strlcpy(cnambuf, argv[0], sizeof (cnambuf)) >= 124*c7e4935fSss150715 sizeof (cnambuf)) { 1257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 126*c7e4935fSss150715 "syncinit: invalid device name (too long) %s\n", argv[0]); 1277c478bd9Sstevel@tonic-gate exit(1); 1287c478bd9Sstevel@tonic-gate } 129*c7e4935fSss150715 1307c478bd9Sstevel@tonic-gate cp = cnambuf; 1317c478bd9Sstevel@tonic-gate while (*cp) /* find the end of the name */ 1327c478bd9Sstevel@tonic-gate cp++; 1337c478bd9Sstevel@tonic-gate cp--; 1347c478bd9Sstevel@tonic-gate if (!isdigit(*cp)) { 1357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 136*c7e4935fSss150715 "syncinit: %s missing minor device number\n", cnambuf); 1377c478bd9Sstevel@tonic-gate exit(1); 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate 140*c7e4935fSss150715 retval = dlpi_open(cnambuf, &dh, DLPI_EXCL|DLPI_SERIAL); 141*c7e4935fSss150715 if (retval != DLPI_SUCCESS) { 142*c7e4935fSss150715 (void) fprintf(stderr, "syncinit: dlpi_open %s: %s\n", cnambuf, 143*c7e4935fSss150715 dlpi_strerror(retval)); 1447c478bd9Sstevel@tonic-gate exit(1); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 147*c7e4935fSss150715 (void) dlpi_parselink(cnambuf, device, &ppa); 148*c7e4935fSss150715 (void) printf("device: %s ppa: %u\n", device, ppa); 149*c7e4935fSss150715 150*c7e4935fSss150715 fd = dlpi_fd(dh); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate argc--; 1537c478bd9Sstevel@tonic-gate argv++; 1547c478bd9Sstevel@tonic-gate if (argc) { /* setting things */ 1557c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETMODE; 1567c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 1577c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct scc_mode); 1587c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&sm; 159*c7e4935fSss150715 1607c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 1617c478bd9Sstevel@tonic-gate perror("S_IOCGETMODE"); 1627c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 1637c478bd9Sstevel@tonic-gate "syncinit: can't get sync mode info for %s\n", 1647c478bd9Sstevel@tonic-gate cnambuf); 1657c478bd9Sstevel@tonic-gate exit(1); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate while (argc-- > 0) { 1687c478bd9Sstevel@tonic-gate arg = *argv++; 1697c478bd9Sstevel@tonic-gate if (sscanf(arg, "%d", &speed) == 1) 1707c478bd9Sstevel@tonic-gate sm.sm_baudrate = speed; 1717c478bd9Sstevel@tonic-gate else if (strchr(arg, '=')) { 1727c478bd9Sstevel@tonic-gate if (prefix(arg, "loop")) { 1737c478bd9Sstevel@tonic-gate if (lookup(yesno, arg)) 1747c478bd9Sstevel@tonic-gate sm.sm_config |= CONN_LPBK; 1757c478bd9Sstevel@tonic-gate else 1767c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_LPBK; 1777c478bd9Sstevel@tonic-gate loopchange++; 1787c478bd9Sstevel@tonic-gate } else if (prefix(arg, "echo")) { 1797c478bd9Sstevel@tonic-gate if (lookup(yesno, arg)) 1807c478bd9Sstevel@tonic-gate sm.sm_config |= CONN_ECHO; 1817c478bd9Sstevel@tonic-gate else 1827c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_ECHO; 1837c478bd9Sstevel@tonic-gate echochange++; 1847c478bd9Sstevel@tonic-gate } else if (prefix(arg, "nrzi")) { 1857c478bd9Sstevel@tonic-gate if (lookup(yesno, arg)) 1867c478bd9Sstevel@tonic-gate sm.sm_config |= CONN_NRZI; 1877c478bd9Sstevel@tonic-gate else 1887c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_NRZI; 1897c478bd9Sstevel@tonic-gate } else if (prefix(arg, "txc")) { 1907c478bd9Sstevel@tonic-gate sm.sm_txclock = lookup(txnames, arg); 1917c478bd9Sstevel@tonic-gate clockchange++; 1927c478bd9Sstevel@tonic-gate } else if (prefix(arg, "rxc")) { 1937c478bd9Sstevel@tonic-gate sm.sm_rxclock = lookup(rxnames, arg); 1947c478bd9Sstevel@tonic-gate clockchange++; 1957c478bd9Sstevel@tonic-gate } else if (prefix(arg, "speed")) { 1967c478bd9Sstevel@tonic-gate arg = strchr(arg, '=') + 1; 1977c478bd9Sstevel@tonic-gate if (sscanf(arg, "%d", &speed) == 1) { 1987c478bd9Sstevel@tonic-gate sm.sm_baudrate = speed; 1997c478bd9Sstevel@tonic-gate } else 2007c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2017c478bd9Sstevel@tonic-gate "syncinit: %s %s\n", 2027c478bd9Sstevel@tonic-gate "bad speed:", arg); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate } else if (equal(arg, "external")) { 2057c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_TXC; 2067c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_RXC; 2077c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_LPBK; 2087c478bd9Sstevel@tonic-gate } else if (equal(arg, "sender")) { 2097c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_BAUD; 2107c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_RXC; 2117c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_LPBK; 2127c478bd9Sstevel@tonic-gate } else if (equal(arg, "internal")) { 2137c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_PLL; 2147c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_PLL; 2157c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_LPBK; 2167c478bd9Sstevel@tonic-gate } else if (equal(arg, "stop")) { 2177c478bd9Sstevel@tonic-gate sm.sm_baudrate = 0; 2187c478bd9Sstevel@tonic-gate } else 2197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Bad arg: %s\n", arg); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate /* 2237c478bd9Sstevel@tonic-gate * If we're going to change the state of loopback, and we 2247c478bd9Sstevel@tonic-gate * don't have our own plans for clock sources, use defaults. 2257c478bd9Sstevel@tonic-gate */ 2267c478bd9Sstevel@tonic-gate if (loopchange && !clockchange) { 2277c478bd9Sstevel@tonic-gate if (sm.sm_config & CONN_LPBK) { 2287c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_BAUD; 2297c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_BAUD; 2307c478bd9Sstevel@tonic-gate } else { 2317c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_TXC; 2327c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_RXC; 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCSETMODE; 2367c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 2377c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct scc_mode); 2387c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&sm; 2397c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 2407c478bd9Sstevel@tonic-gate perror("S_IOCSETMODE"); 2417c478bd9Sstevel@tonic-gate (void) ioctl(fd, S_IOCGETMODE, &sm); 2427c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2437c478bd9Sstevel@tonic-gate "syncinit: ioctl failure code = %x\n", 2447c478bd9Sstevel@tonic-gate sm.sm_retval); 2457c478bd9Sstevel@tonic-gate exit(1); 2467c478bd9Sstevel@tonic-gate } 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* Report State */ 2507c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETMODE; 2517c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 2527c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct scc_mode); 2537c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&sm; 2547c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 2557c478bd9Sstevel@tonic-gate perror("S_IOCGETMODE"); 2567c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 2577c478bd9Sstevel@tonic-gate "syncinit: can't get sync mode info for %s\n", 2587c478bd9Sstevel@tonic-gate cnambuf); 2597c478bd9Sstevel@tonic-gate exit(1); 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate (void) printf( 2627c478bd9Sstevel@tonic-gate "speed=%d, loopback=%s, echo=%s, nrzi=%s, txc=%s, rxc=%s\n", 2637c478bd9Sstevel@tonic-gate sm.sm_baudrate, 2647c478bd9Sstevel@tonic-gate yesno[((int)(sm.sm_config & CONN_LPBK) > 0)], 2657c478bd9Sstevel@tonic-gate yesno[((int)(sm.sm_config & CONN_ECHO) > 0)], 2667c478bd9Sstevel@tonic-gate yesno[((int)(sm.sm_config & CONN_NRZI) > 0)], 2677c478bd9Sstevel@tonic-gate txnames[sm.sm_txclock], 2687c478bd9Sstevel@tonic-gate rxnames[sm.sm_rxclock]); 2697c478bd9Sstevel@tonic-gate return (0); 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate static void 2737c478bd9Sstevel@tonic-gate usage() 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Usage: syncinit cnambuf \\\n"); 2767c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t[baudrate] [loopback=[yes|no]] "); 2777c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "[echo=[yes|no]] [nrzi=[yes|no]] \\\n"); 2787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t[txc=[txc|rxc|baud|pll]] \\\n"); 2797c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t[rxc=[rxc|txc|baud|pll]]\n"); 2807c478bd9Sstevel@tonic-gate exit(1); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate static int 2847c478bd9Sstevel@tonic-gate prefix(char *arg, char *pref) 2857c478bd9Sstevel@tonic-gate { 2867c478bd9Sstevel@tonic-gate return (strncmp(arg, pref, strlen(pref)) == 0); 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate static int 2907c478bd9Sstevel@tonic-gate lookup(char **table, char *arg) 2917c478bd9Sstevel@tonic-gate { 2927c478bd9Sstevel@tonic-gate char *val = strchr(arg, '=') + 1; 2937c478bd9Sstevel@tonic-gate int ival; 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate for (ival = 0; *table != 0; ival++, table++) 2967c478bd9Sstevel@tonic-gate if (equal(*table, val)) 2977c478bd9Sstevel@tonic-gate return (ival); 2987c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "syncinit: bad arg: %s\n", arg); 2997c478bd9Sstevel@tonic-gate exit(1); 3007c478bd9Sstevel@tonic-gate /* NOTREACHED */ 3017c478bd9Sstevel@tonic-gate } 302