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 2005 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 #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * Initialize and re-initialize synchronous serial clocking and loopback 31*7c478bd9Sstevel@tonic-gate * options. Interfaces through the S_IOCGETMODE and S_IOCSETMODE ioctls. 32*7c478bd9Sstevel@tonic-gate */ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 35*7c478bd9Sstevel@tonic-gate #include <ctype.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 37*7c478bd9Sstevel@tonic-gate #include <stdio.h> 38*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 39*7c478bd9Sstevel@tonic-gate #include <unistd.h> 40*7c478bd9Sstevel@tonic-gate #include <string.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/stream.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 43*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 44*7c478bd9Sstevel@tonic-gate #include <errno.h> 45*7c478bd9Sstevel@tonic-gate #include <sys/ser_sync.h> 46*7c478bd9Sstevel@tonic-gate #include <libdlpi.h> 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate static void usage(void); 49*7c478bd9Sstevel@tonic-gate static int prefix(char *arg, char *pref); 50*7c478bd9Sstevel@tonic-gate static int lookup(char **table, char *arg); 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate static char *yesno[] = { 53*7c478bd9Sstevel@tonic-gate "no", 54*7c478bd9Sstevel@tonic-gate "yes", 55*7c478bd9Sstevel@tonic-gate "silent", 56*7c478bd9Sstevel@tonic-gate 0, 57*7c478bd9Sstevel@tonic-gate }; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate static char *txnames[] = { 60*7c478bd9Sstevel@tonic-gate "txc", 61*7c478bd9Sstevel@tonic-gate "rxc", 62*7c478bd9Sstevel@tonic-gate "baud", 63*7c478bd9Sstevel@tonic-gate "pll", 64*7c478bd9Sstevel@tonic-gate "sysclk", 65*7c478bd9Sstevel@tonic-gate "-txc", 66*7c478bd9Sstevel@tonic-gate 0, 67*7c478bd9Sstevel@tonic-gate }; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate static char *rxnames[] = { 70*7c478bd9Sstevel@tonic-gate "rxc", 71*7c478bd9Sstevel@tonic-gate "txc", 72*7c478bd9Sstevel@tonic-gate "baud", 73*7c478bd9Sstevel@tonic-gate "pll", 74*7c478bd9Sstevel@tonic-gate "sysclk", 75*7c478bd9Sstevel@tonic-gate "-rxc", 76*7c478bd9Sstevel@tonic-gate 0, 77*7c478bd9Sstevel@tonic-gate }; 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate #ifdef notdef 80*7c478bd9Sstevel@tonic-gate static char *txdnames[] = { 81*7c478bd9Sstevel@tonic-gate "txd", 82*7c478bd9Sstevel@tonic-gate " ", /* dummy entry, do not remove */ 83*7c478bd9Sstevel@tonic-gate "-txd", 84*7c478bd9Sstevel@tonic-gate 0, 85*7c478bd9Sstevel@tonic-gate }; 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate static char *rxdnames[] = { 88*7c478bd9Sstevel@tonic-gate "rxd", 89*7c478bd9Sstevel@tonic-gate "-rxd", 90*7c478bd9Sstevel@tonic-gate 0, 91*7c478bd9Sstevel@tonic-gate }; 92*7c478bd9Sstevel@tonic-gate 93*7c478bd9Sstevel@tonic-gate static char *portab[] = { 94*7c478bd9Sstevel@tonic-gate "rs422", 95*7c478bd9Sstevel@tonic-gate "v35", 96*7c478bd9Sstevel@tonic-gate 0, 97*7c478bd9Sstevel@tonic-gate }; 98*7c478bd9Sstevel@tonic-gate #endif 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate #define equal(a, b) (strcmp((a), (b)) == 0) 101*7c478bd9Sstevel@tonic-gate #define MAXWAIT 15 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate int 104*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 105*7c478bd9Sstevel@tonic-gate { 106*7c478bd9Sstevel@tonic-gate char cnambuf[MAXPATHLEN]; 107*7c478bd9Sstevel@tonic-gate struct scc_mode sm; 108*7c478bd9Sstevel@tonic-gate struct strioctl sioc; 109*7c478bd9Sstevel@tonic-gate int fd, speed; 110*7c478bd9Sstevel@tonic-gate char *arg, *cp; 111*7c478bd9Sstevel@tonic-gate char loopchange = 0; 112*7c478bd9Sstevel@tonic-gate char echochange = 0; 113*7c478bd9Sstevel@tonic-gate char clockchange = 0; 114*7c478bd9Sstevel@tonic-gate char *devstr = "/dev/"; 115*7c478bd9Sstevel@tonic-gate int devstrlen; 116*7c478bd9Sstevel@tonic-gate ulong_t ppa; 117*7c478bd9Sstevel@tonic-gate 118*7c478bd9Sstevel@tonic-gate if (argc == 1) { 119*7c478bd9Sstevel@tonic-gate usage(); 120*7c478bd9Sstevel@tonic-gate exit(1); 121*7c478bd9Sstevel@tonic-gate } 122*7c478bd9Sstevel@tonic-gate argc--; 123*7c478bd9Sstevel@tonic-gate argv++; 124*7c478bd9Sstevel@tonic-gate devstrlen = strlen(devstr); 125*7c478bd9Sstevel@tonic-gate if (strncmp(devstr, argv[0], devstrlen) != 0) { 126*7c478bd9Sstevel@tonic-gate if (snprintf(cnambuf, sizeof (cnambuf), "%s%s", devstr, 127*7c478bd9Sstevel@tonic-gate argv[0]) >= sizeof (cnambuf)) { 128*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 129*7c478bd9Sstevel@tonic-gate "syncinit: invalid device name (too long) %s\n", 130*7c478bd9Sstevel@tonic-gate argv[0]); 131*7c478bd9Sstevel@tonic-gate exit(1); 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate } 134*7c478bd9Sstevel@tonic-gate cp = cnambuf; 135*7c478bd9Sstevel@tonic-gate while (*cp) /* find the end of the name */ 136*7c478bd9Sstevel@tonic-gate cp++; 137*7c478bd9Sstevel@tonic-gate cp--; 138*7c478bd9Sstevel@tonic-gate if (!isdigit(*cp)) { 139*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 140*7c478bd9Sstevel@tonic-gate "syncinit: %s missing minor device number\n", argv[0]); 141*7c478bd9Sstevel@tonic-gate exit(1); 142*7c478bd9Sstevel@tonic-gate } 143*7c478bd9Sstevel@tonic-gate while (isdigit(*(cp - 1))) 144*7c478bd9Sstevel@tonic-gate cp--; 145*7c478bd9Sstevel@tonic-gate ppa = strtoul(cp, NULL, 10); 146*7c478bd9Sstevel@tonic-gate *cp = '\0'; /* drop number, leaving name of clone device. */ 147*7c478bd9Sstevel@tonic-gate fd = open(cnambuf, O_RDWR|O_EXCL, 0); 148*7c478bd9Sstevel@tonic-gate if (fd < 0) { 149*7c478bd9Sstevel@tonic-gate perror("syncinit: open"); 150*7c478bd9Sstevel@tonic-gate exit(1); 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate if (dlpi_attach(fd, MAXWAIT, ppa) != 0) { 154*7c478bd9Sstevel@tonic-gate perror("syncinit: dlpi_attach"); 155*7c478bd9Sstevel@tonic-gate exit(1); 156*7c478bd9Sstevel@tonic-gate } 157*7c478bd9Sstevel@tonic-gate 158*7c478bd9Sstevel@tonic-gate (void) printf("device: %s ppa: %d\n", cnambuf, (int)ppa); 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate argc--; 161*7c478bd9Sstevel@tonic-gate argv++; 162*7c478bd9Sstevel@tonic-gate if (argc) { /* setting things */ 163*7c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETMODE; 164*7c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 165*7c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct scc_mode); 166*7c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&sm; 167*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 168*7c478bd9Sstevel@tonic-gate perror("S_IOCGETMODE"); 169*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 170*7c478bd9Sstevel@tonic-gate "syncinit: can't get sync mode info for %s\n", 171*7c478bd9Sstevel@tonic-gate cnambuf); 172*7c478bd9Sstevel@tonic-gate exit(1); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate while (argc-- > 0) { 175*7c478bd9Sstevel@tonic-gate arg = *argv++; 176*7c478bd9Sstevel@tonic-gate if (sscanf(arg, "%d", &speed) == 1) 177*7c478bd9Sstevel@tonic-gate sm.sm_baudrate = speed; 178*7c478bd9Sstevel@tonic-gate else if (strchr(arg, '=')) { 179*7c478bd9Sstevel@tonic-gate if (prefix(arg, "loop")) { 180*7c478bd9Sstevel@tonic-gate if (lookup(yesno, arg)) 181*7c478bd9Sstevel@tonic-gate sm.sm_config |= CONN_LPBK; 182*7c478bd9Sstevel@tonic-gate else 183*7c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_LPBK; 184*7c478bd9Sstevel@tonic-gate loopchange++; 185*7c478bd9Sstevel@tonic-gate } else if (prefix(arg, "echo")) { 186*7c478bd9Sstevel@tonic-gate if (lookup(yesno, arg)) 187*7c478bd9Sstevel@tonic-gate sm.sm_config |= CONN_ECHO; 188*7c478bd9Sstevel@tonic-gate else 189*7c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_ECHO; 190*7c478bd9Sstevel@tonic-gate echochange++; 191*7c478bd9Sstevel@tonic-gate } else if (prefix(arg, "nrzi")) { 192*7c478bd9Sstevel@tonic-gate if (lookup(yesno, arg)) 193*7c478bd9Sstevel@tonic-gate sm.sm_config |= CONN_NRZI; 194*7c478bd9Sstevel@tonic-gate else 195*7c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_NRZI; 196*7c478bd9Sstevel@tonic-gate } else if (prefix(arg, "txc")) { 197*7c478bd9Sstevel@tonic-gate sm.sm_txclock = lookup(txnames, arg); 198*7c478bd9Sstevel@tonic-gate clockchange++; 199*7c478bd9Sstevel@tonic-gate } else if (prefix(arg, "rxc")) { 200*7c478bd9Sstevel@tonic-gate sm.sm_rxclock = lookup(rxnames, arg); 201*7c478bd9Sstevel@tonic-gate clockchange++; 202*7c478bd9Sstevel@tonic-gate } else if (prefix(arg, "speed")) { 203*7c478bd9Sstevel@tonic-gate arg = strchr(arg, '=') + 1; 204*7c478bd9Sstevel@tonic-gate if (sscanf(arg, "%d", &speed) == 1) { 205*7c478bd9Sstevel@tonic-gate sm.sm_baudrate = speed; 206*7c478bd9Sstevel@tonic-gate } else 207*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 208*7c478bd9Sstevel@tonic-gate "syncinit: %s %s\n", 209*7c478bd9Sstevel@tonic-gate "bad speed:", arg); 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate } else if (equal(arg, "external")) { 212*7c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_TXC; 213*7c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_RXC; 214*7c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_LPBK; 215*7c478bd9Sstevel@tonic-gate } else if (equal(arg, "sender")) { 216*7c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_BAUD; 217*7c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_RXC; 218*7c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_LPBK; 219*7c478bd9Sstevel@tonic-gate } else if (equal(arg, "internal")) { 220*7c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_PLL; 221*7c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_PLL; 222*7c478bd9Sstevel@tonic-gate sm.sm_config &= ~CONN_LPBK; 223*7c478bd9Sstevel@tonic-gate } else if (equal(arg, "stop")) { 224*7c478bd9Sstevel@tonic-gate sm.sm_baudrate = 0; 225*7c478bd9Sstevel@tonic-gate } else 226*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Bad arg: %s\n", arg); 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate 229*7c478bd9Sstevel@tonic-gate /* 230*7c478bd9Sstevel@tonic-gate * If we're going to change the state of loopback, and we 231*7c478bd9Sstevel@tonic-gate * don't have our own plans for clock sources, use defaults. 232*7c478bd9Sstevel@tonic-gate */ 233*7c478bd9Sstevel@tonic-gate if (loopchange && !clockchange) { 234*7c478bd9Sstevel@tonic-gate if (sm.sm_config & CONN_LPBK) { 235*7c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_BAUD; 236*7c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_BAUD; 237*7c478bd9Sstevel@tonic-gate } else { 238*7c478bd9Sstevel@tonic-gate sm.sm_txclock = TXC_IS_TXC; 239*7c478bd9Sstevel@tonic-gate sm.sm_rxclock = RXC_IS_RXC; 240*7c478bd9Sstevel@tonic-gate } 241*7c478bd9Sstevel@tonic-gate } 242*7c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCSETMODE; 243*7c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 244*7c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct scc_mode); 245*7c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&sm; 246*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 247*7c478bd9Sstevel@tonic-gate perror("S_IOCSETMODE"); 248*7c478bd9Sstevel@tonic-gate (void) ioctl(fd, S_IOCGETMODE, &sm); 249*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 250*7c478bd9Sstevel@tonic-gate "syncinit: ioctl failure code = %x\n", 251*7c478bd9Sstevel@tonic-gate sm.sm_retval); 252*7c478bd9Sstevel@tonic-gate exit(1); 253*7c478bd9Sstevel@tonic-gate } 254*7c478bd9Sstevel@tonic-gate } 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate /* Report State */ 257*7c478bd9Sstevel@tonic-gate sioc.ic_cmd = S_IOCGETMODE; 258*7c478bd9Sstevel@tonic-gate sioc.ic_timout = -1; 259*7c478bd9Sstevel@tonic-gate sioc.ic_len = sizeof (struct scc_mode); 260*7c478bd9Sstevel@tonic-gate sioc.ic_dp = (char *)&sm; 261*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &sioc) < 0) { 262*7c478bd9Sstevel@tonic-gate perror("S_IOCGETMODE"); 263*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 264*7c478bd9Sstevel@tonic-gate "syncinit: can't get sync mode info for %s\n", 265*7c478bd9Sstevel@tonic-gate cnambuf); 266*7c478bd9Sstevel@tonic-gate exit(1); 267*7c478bd9Sstevel@tonic-gate } 268*7c478bd9Sstevel@tonic-gate (void) printf( 269*7c478bd9Sstevel@tonic-gate "speed=%d, loopback=%s, echo=%s, nrzi=%s, txc=%s, rxc=%s\n", 270*7c478bd9Sstevel@tonic-gate sm.sm_baudrate, 271*7c478bd9Sstevel@tonic-gate yesno[((int)(sm.sm_config & CONN_LPBK) > 0)], 272*7c478bd9Sstevel@tonic-gate yesno[((int)(sm.sm_config & CONN_ECHO) > 0)], 273*7c478bd9Sstevel@tonic-gate yesno[((int)(sm.sm_config & CONN_NRZI) > 0)], 274*7c478bd9Sstevel@tonic-gate txnames[sm.sm_txclock], 275*7c478bd9Sstevel@tonic-gate rxnames[sm.sm_rxclock]); 276*7c478bd9Sstevel@tonic-gate return (0); 277*7c478bd9Sstevel@tonic-gate } 278*7c478bd9Sstevel@tonic-gate 279*7c478bd9Sstevel@tonic-gate static void 280*7c478bd9Sstevel@tonic-gate usage() 281*7c478bd9Sstevel@tonic-gate { 282*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Usage: syncinit cnambuf \\\n"); 283*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t[baudrate] [loopback=[yes|no]] "); 284*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "[echo=[yes|no]] [nrzi=[yes|no]] \\\n"); 285*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t[txc=[txc|rxc|baud|pll]] \\\n"); 286*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\t[rxc=[rxc|txc|baud|pll]]\n"); 287*7c478bd9Sstevel@tonic-gate exit(1); 288*7c478bd9Sstevel@tonic-gate } 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate static int 291*7c478bd9Sstevel@tonic-gate prefix(char *arg, char *pref) 292*7c478bd9Sstevel@tonic-gate { 293*7c478bd9Sstevel@tonic-gate return (strncmp(arg, pref, strlen(pref)) == 0); 294*7c478bd9Sstevel@tonic-gate } 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate static int 297*7c478bd9Sstevel@tonic-gate lookup(char **table, char *arg) 298*7c478bd9Sstevel@tonic-gate { 299*7c478bd9Sstevel@tonic-gate char *val = strchr(arg, '=') + 1; 300*7c478bd9Sstevel@tonic-gate int ival; 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate for (ival = 0; *table != 0; ival++, table++) 303*7c478bd9Sstevel@tonic-gate if (equal(*table, val)) 304*7c478bd9Sstevel@tonic-gate return (ival); 305*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "syncinit: bad arg: %s\n", arg); 306*7c478bd9Sstevel@tonic-gate exit(1); 307*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 308*7c478bd9Sstevel@tonic-gate } 309